Whamcloud - gitweb
LU-5170 utils: Continue on error when multiple files requested
[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         do
1027                 gid = random();
1028         while (gid == 0);
1029
1030
1031         /* The grouplock blocks all concurrent accesses to the file. */
1032         rc = llapi_group_lock(fd, gid);
1033         if (rc < 0) {
1034                 error_loc = "cannot get group lock";
1035                 return rc;
1036         }
1037
1038         rc = fstat(fd, &st);
1039         if (rc < 0) {
1040                 error_loc = "cannot stat source file";
1041                 rc = -errno;
1042                 goto out_unlock;
1043         }
1044
1045         /*
1046          * LL_DV_RD_FLUSH should not be set, otherwise the servers will try to
1047          * get extent locks on the OST objects. This will conflict with our
1048          * extent group locks.
1049          */
1050         rc = llapi_get_data_version(fd, &dv1, 0);
1051         if (rc < 0) {
1052                 error_loc = "cannot get dataversion";
1053                 goto out_unlock;
1054         }
1055
1056         rc = migrate_copy_data(fd, fdv, NULL, bandwidth_bytes_sec,
1057                                stats_flag, stats_interval_sec,
1058                                st.st_size);
1059         if (rc < 0) {
1060                 error_loc = "data copy failed";
1061                 goto out_unlock;
1062         }
1063
1064         /* Make sure we keep original atime/mtime values */
1065         rc = migrate_set_timestamps(fdv, &st);
1066         if (rc < 0) {
1067                 error_loc = "set target file timestamp failed";
1068                 goto out_unlock;
1069         }
1070
1071         /*
1072          * swap layouts
1073          * for a migration we need to check data version on file did
1074          * not change.
1075          *
1076          * Pass in gid=0 since we already own grouplock.
1077          */
1078         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1079                                            SWAP_LAYOUTS_CHECK_DV1);
1080         if (rc == -EAGAIN) {
1081                 error_loc = "file changed";
1082                 goto out_unlock;
1083         } else if (rc < 0) {
1084                 error_loc = "cannot swap layout";
1085                 goto out_unlock;
1086         }
1087
1088 out_unlock:
1089         rc2 = llapi_group_unlock(fd, gid);
1090         if (rc2 < 0 && rc == 0) {
1091                 error_loc = "unlock group lock";
1092                 rc = rc2;
1093         }
1094
1095         return rc;
1096 }
1097
1098 /**
1099  * Internal helper for migrate_copy_data(). Check lease and report error if
1100  * need be.
1101  *
1102  * \param[in]  fd           File descriptor on which to check the lease.
1103  *
1104  * \retval 0       Migration can keep on going.
1105  * \retval -errno  Error occurred, abort migration.
1106  */
1107 static int check_lease(int fd)
1108 {
1109         int rc;
1110
1111         rc = llapi_lease_check(fd);
1112         if (rc > 0)
1113                 return 0; /* llapi_check_lease returns > 0 on success. */
1114
1115         return -EBUSY;
1116 }
1117
1118 static int migrate_nonblock(int fd, int fdv,
1119                             unsigned long long bandwidth_bytes_sec,
1120                             enum stats_flag stats_flag,
1121                             long stats_interval_sec)
1122 {
1123         struct stat st;
1124         __u64   dv1;
1125         __u64   dv2;
1126         int     rc;
1127
1128         rc = fstat(fd, &st);
1129         if (rc < 0) {
1130                 error_loc = "cannot stat source file";
1131                 return -errno;
1132         }
1133
1134         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1135         if (rc < 0) {
1136                 error_loc = "cannot get data version";
1137                 return rc;
1138         }
1139
1140         rc = migrate_copy_data(fd, fdv, check_lease, bandwidth_bytes_sec,
1141                                stats_flag, stats_interval_sec,
1142                                st.st_size);
1143         if (rc < 0) {
1144                 error_loc = "data copy failed";
1145                 return rc;
1146         }
1147
1148         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1149         if (rc != 0) {
1150                 error_loc = "cannot get data version";
1151                 return rc;
1152         }
1153
1154         if (dv1 != dv2) {
1155                 rc = -EAGAIN;
1156                 error_loc = "source file changed";
1157                 return rc;
1158         }
1159
1160         /* Make sure we keep original atime/mtime values */
1161         rc = migrate_set_timestamps(fdv, &st);
1162         if (rc < 0) {
1163                 error_loc = "set target file timestamp failed";
1164                 return -errno;
1165         }
1166         return 0;
1167 }
1168
1169 static
1170 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1171 {
1172         struct pool_to_id_cbdata data = { .pool = pool };
1173         struct llapi_layout *layout = NULL;
1174         int rc;
1175
1176         layout = llapi_layout_get_by_path(fname, 0);
1177         if (!layout) {
1178                 fprintf(stderr,
1179                         "error %s: file '%s' couldn't get layout: rc=%d\n",
1180                         progname, fname, errno);
1181                 rc = -errno;
1182                 goto free_layout;
1183         }
1184         rc = llapi_layout_sanity(layout, false, true);
1185         if (rc < 0) {
1186                 llapi_layout_sanity_perror(errno);
1187                 goto free_layout;
1188         }
1189         rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1190         if (rc < 0)
1191                 goto free_layout;
1192
1193         *comp_id = data.id;
1194         rc = 0;
1195
1196 free_layout:
1197         if (layout)
1198                 llapi_layout_free(layout);
1199         return rc;
1200 }
1201
1202 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1203                              __u32 flags, __u32 neg_flags)
1204 {
1205         __u32 ids[2];
1206         __u32 flags_array[2];
1207         size_t count = 0;
1208         int rc;
1209
1210         if (!comp_id) {
1211                 if (pool == NULL) {
1212                         fprintf(stderr,
1213                                 "error %s: neither component id nor pool is specified\n",
1214                                 progname);
1215                         return -EINVAL;
1216                 }
1217                 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1218                 if (rc)
1219                         return rc;
1220         }
1221
1222         if (flags) {
1223                 ids[count] = comp_id;
1224                 flags_array[count] = flags;
1225                 ++count;
1226         }
1227
1228         if (neg_flags) {
1229                 if (neg_flags & LCME_FL_STALE) {
1230                         fprintf(stderr,
1231                                 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1232                                 progname);
1233                         return -EINVAL;
1234                 }
1235
1236                 ids[count] = comp_id;
1237                 flags_array[count] = neg_flags | LCME_FL_NEG;
1238                 ++count;
1239         }
1240
1241         rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1242         if (rc) {
1243                 if (errno == EUCLEAN) {
1244                         rc = -errno;
1245                         fprintf(stderr,
1246                                 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1247                                 progname, comp_id, fname);
1248                 } else {
1249                         fprintf(stderr,
1250                                 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1251                                 progname, comp_id, fname, flags, neg_flags);
1252                 }
1253         }
1254
1255         return rc;
1256 }
1257
1258 static int lfs_component_del(char *fname, __u32 comp_id,
1259                              __u32 flags, __u32 neg_flags)
1260 {
1261         int     rc = 0;
1262
1263         if (flags && neg_flags) {
1264                 fprintf(stderr,
1265                         "%s: cannot specify both positive and negative flags\n",
1266                         progname);
1267                 return -EINVAL;
1268         }
1269
1270         if (!flags && neg_flags)
1271                 flags = neg_flags | LCME_FL_NEG;
1272
1273         if (flags && comp_id) {
1274                 fprintf(stderr,
1275                         "%s: cannot specify component ID and flags at the same time\n",
1276                         progname);
1277                 return -EINVAL;
1278         }
1279
1280         if (!flags && !comp_id) {
1281                 fprintf(stderr,
1282                         "%s: neither flags nor component ID is specified\n",
1283                         progname);
1284                 return -EINVAL;
1285         }
1286
1287         if (flags) {
1288                 if (flags & ~LCME_KNOWN_FLAGS) {
1289                         fprintf(stderr,
1290                                 "%s setstripe: unknown flags %#x\n",
1291                                 progname, flags);
1292                         return -EINVAL;
1293                 }
1294         } else if (comp_id > LCME_ID_MAX) {
1295                 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1296                         progname, comp_id);
1297                 return -EINVAL;
1298         }
1299
1300         rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1301         if (rc)
1302                 fprintf(stderr,
1303                         "%s setstripe: cannot delete component %#x from '%s': %s\n",
1304                         progname, comp_id, fname, strerror(errno));
1305         return rc;
1306 }
1307
1308 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1309 {
1310         int     rc;
1311
1312         if (!layout)
1313                 return -EINVAL;
1314
1315         rc = llapi_layout_file_comp_add(fname, layout);
1316         if (rc)
1317                 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1318                         fname, strerror(errno));
1319         return rc;
1320 }
1321
1322 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1323                                 struct llapi_layout *layout)
1324 {
1325         struct stat     st;
1326         int     fd;
1327
1328         if (!layout)
1329                 return -EINVAL;
1330
1331         fd = lstat(fname, &st);
1332         if (fd == 0 && S_ISDIR(st.st_mode))
1333                 open_flags = O_DIRECTORY | O_RDONLY;
1334
1335         fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1336         if (fd < 0)
1337                 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1338                         S_ISDIR(st.st_mode) ?
1339                                 "set default composite layout for" :
1340                                 "create composite file",
1341                         fname, strerror(errno));
1342         return fd;
1343 }
1344
1345 static int lfs_migrate(char *name, __u64 migration_flags,
1346                         struct llapi_stripe_param *param,
1347                         struct llapi_layout *layout,
1348                         unsigned long long bandwidth_bytes_sec,
1349                         enum stats_flag stats_flag, long stats_interval_sec)
1350 {
1351         struct llapi_layout *existing;
1352         uint64_t dom_new, dom_cur;
1353         int fd = -1;
1354         int fdv = -1;
1355         int rc;
1356
1357         rc = migrate_open_files(name, migration_flags, param, layout,
1358                                 &fd, &fdv);
1359         if (rc < 0)
1360                 goto out;
1361
1362         rc = llapi_layout_dom_size(layout, &dom_new);
1363         if (rc) {
1364                 error_loc = "cannot get new layout DoM size";
1365                 goto out;
1366         }
1367         /* special case for migration to DOM layout*/
1368         existing = llapi_layout_get_by_fd(fd, 0);
1369         if (!existing) {
1370                 error_loc = "cannot get existing layout";
1371                 goto out;
1372         }
1373
1374         rc = llapi_layout_dom_size(existing, &dom_cur);
1375         if (rc) {
1376                 error_loc = "cannot get current layout DoM size";
1377                 goto out;
1378         }
1379
1380         /*
1381          * if file has DoM layout already then migration is possible to
1382          * the new layout with the same DoM component via swap layout,
1383          * if new layout used bigger DOM size, then mirroring is used
1384          */
1385         if (dom_new > dom_cur) {
1386                 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags,
1387                                         bandwidth_bytes_sec, stats_flag,
1388                                         stats_interval_sec);
1389                 if (rc)
1390                         error_loc = "cannot migrate to DOM layout";
1391                 goto out_closed;
1392         }
1393
1394         if (stats_flag == STATS_ON)
1395                 printf("%s:\n", name);
1396
1397         if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1398                 /*
1399                  * Blocking mode (forced if servers do not support file lease).
1400                  * It is also the default mode, since we cannot distinguish
1401                  * between a broken lease and a server that does not support
1402                  * atomic swap/close (LU-6785)
1403                  */
1404                 rc = migrate_block(fd, fdv, bandwidth_bytes_sec, stats_flag,
1405                                    stats_interval_sec);
1406                 goto out;
1407         }
1408
1409         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1410         if (rc < 0) {
1411                 error_loc = "cannot get lease";
1412                 goto out;
1413         }
1414
1415         rc = migrate_nonblock(fd, fdv, bandwidth_bytes_sec, stats_flag,
1416                               stats_interval_sec);
1417         if (rc < 0) {
1418                 llapi_lease_release(fd);
1419                 goto out;
1420         }
1421
1422         /*
1423          * Atomically put lease, swap layouts and close.
1424          * for a migration we need to check data version on file did
1425          * not change.
1426          */
1427         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1428         if (rc < 0) {
1429                 error_loc = "cannot swap layout";
1430                 goto out;
1431         }
1432
1433 out:
1434         if (fd >= 0)
1435                 close(fd);
1436
1437         if (fdv >= 0)
1438                 close(fdv);
1439 out_closed:
1440         if (rc < 0)
1441                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1442                         progname, name, error_loc, strerror(-rc));
1443         else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1444                 printf("%s\n", name);
1445
1446         return rc;
1447 }
1448
1449 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1450 {
1451         char *name;
1452         char *dup_string = NULL;
1453         int rc = 0;
1454
1455         *flags = 0;
1456         *neg_flags = 0;
1457
1458         if (!string || !string[0])
1459                 return -EINVAL;
1460
1461         dup_string = strdup(string);
1462         if (!dup_string) {
1463                 llapi_printf(LLAPI_MSG_ERROR,
1464                              "%s: insufficient memory\n",
1465                              progname);
1466                 return -ENOMEM;
1467         }
1468
1469         for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1470                 bool found = false;
1471                 int i;
1472
1473                 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1474                         __u32 comp_flag = comp_flags_table[i].cfn_flag;
1475                         const char *comp_name = comp_flags_table[i].cfn_name;
1476
1477                         if (strcmp(name, comp_name) == 0) {
1478                                 *flags |= comp_flag;
1479                                 found = true;
1480                         } else if (strncmp(name, "^", 1) == 0 &&
1481                                    strcmp(name + 1, comp_name) == 0) {
1482                                 *neg_flags |= comp_flag;
1483                                 found = true;
1484                         }
1485                 }
1486                 if (!found) {
1487                         llapi_printf(LLAPI_MSG_ERROR,
1488                                      "%s: component flag '%s' not supported\n",
1489                                      progname, name);
1490                         rc = -EINVAL;
1491                         goto out_free;
1492                 }
1493         }
1494
1495         if (!*flags && !*neg_flags)
1496                 rc = -EINVAL;
1497
1498         /* don't allow to set and exclude the same flag */
1499         if (*flags & *neg_flags)
1500                 rc = -EINVAL;
1501
1502 out_free:
1503         free(dup_string);
1504         return rc;
1505 }
1506
1507 static int mdthash_input(char *string, __u32 *inflags,
1508                          __u32 *exflags, __u32 *type)
1509 {
1510         char *name;
1511         struct mhf_list {
1512                 char *name;
1513                 __u32 flag;
1514         } mhflist[] = {
1515                 {"migrating", LMV_HASH_FLAG_MIGRATION},
1516                 {"bad_type", LMV_HASH_FLAG_BAD_TYPE},
1517                 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1518                 {"lost_lmv", LMV_HASH_FLAG_LOST_LMV},
1519                 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1520         };
1521
1522         if (string == NULL)
1523                 return -EINVAL;
1524
1525         *inflags = 0;
1526         *exflags = 0;
1527         *type = 0;
1528         for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1529                 bool found = false;
1530                 int i;
1531
1532                 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1533                         if (strcmp(name, mhflist[i].name) == 0 ||
1534                             name[0] == mhflist[i].name[0]) {
1535                                 *inflags |= mhflist[i].flag;
1536                                 found = true;
1537                         } else if (name[0] == '^' &&
1538                                    (strcmp(name + 1, mhflist[i].name) == 0 ||
1539                                     name[1] == mhflist[i].name[0])) {
1540                                 *exflags |= mhflist[i].flag;
1541                                 found = true;
1542                         }
1543                 }
1544                 if (!found) {
1545                         i = check_hashtype(name);
1546                         if (i > 0) {
1547                                 *type |= 1 << i;
1548                                 continue;
1549                         }
1550                         llapi_printf(LLAPI_MSG_ERROR,
1551                                      "%s: invalid mdt_hash value '%s'\n",
1552                                      progname, name);
1553                         return -EINVAL;
1554                 }
1555         }
1556
1557         /* don't allow to include and exclude the same flag */
1558         if (*inflags & *exflags) {
1559                 llapi_printf(LLAPI_MSG_ERROR,
1560                              "%s: include and exclude same flag '%s'\n",
1561                              progname, string);
1562                 return -EINVAL;
1563         }
1564
1565         return 0;
1566 }
1567
1568 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1569 {
1570         if (!string)
1571                 return -EINVAL;
1572
1573         *state = 0;
1574         *neg_state = 0;
1575
1576         if (strncmp(string, "^", 1) == 0) {
1577                 *neg_state = llapi_layout_string_flags(string + 1);
1578                 if (*neg_state != 0)
1579                         return 0;
1580         } else {
1581                 *state = llapi_layout_string_flags(string);
1582                 if (*state != 0)
1583                         return 0;
1584         }
1585
1586         llapi_printf(LLAPI_MSG_ERROR,
1587                      "%s: mirrored file state '%s' not supported\n",
1588                      progname, string);
1589         return -EINVAL;
1590 }
1591
1592 /**
1593  * struct mirror_args - Command-line arguments for mirror(s).
1594  * @m_count:  Number of mirrors to be created with this layout.
1595  * @m_flags:  Mirror level flags, only 'prefer' is supported.
1596  * @m_layout: Mirror layout.
1597  * @m_file:   A victim file. Its layout will be split and used as a mirror.
1598  * @m_next:   Point to the next node of the list.
1599  *
1600  * Command-line arguments for mirror(s) will be parsed and stored in
1601  * a linked list that consists of this structure.
1602  */
1603 struct mirror_args {
1604         __u32                   m_count;
1605         __u32                   m_flags;
1606         struct llapi_layout     *m_layout;
1607         const char              *m_file;
1608         struct mirror_args      *m_next;
1609         bool                    m_inherit;
1610 };
1611
1612 /**
1613  * enum mirror_flags - Flags for extending a mirrored file.
1614  * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1615  *             in case the victim file(s) contains the same data as the
1616  *             original mirrored file.
1617  * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1618  * @MF_COMP_ID: specified component id instead of mirror id
1619  *
1620  * Flags for extending a mirrored file.
1621  */
1622 enum mirror_flags {
1623         MF_NO_VERIFY    = 0x1,
1624         MF_DESTROY      = 0x2,
1625         MF_COMP_ID      = 0x4,
1626         MF_COMP_POOL    = 0x8,
1627 };
1628
1629 /**
1630  * mirror_create_sanity_check() - Check mirror list.
1631  * @list:  A linked list that stores the mirror arguments.
1632  *
1633  * This function does a sanity check on @list for creating
1634  * a mirrored file.
1635  *
1636  * Return: 0 on success or a negative error code on failure.
1637  */
1638 static int mirror_create_sanity_check(const char *fname,
1639                                       struct mirror_args *list,
1640                                       bool check_fname)
1641 {
1642         int rc = 0;
1643         bool has_m_file = false;
1644         bool has_m_layout = false;
1645
1646         if (!list)
1647                 return -EINVAL;
1648
1649         if (fname && check_fname) {
1650                 struct llapi_layout *layout;
1651
1652                 layout = llapi_layout_get_by_path(fname, 0);
1653                 if (!layout) {
1654                         fprintf(stderr,
1655                                 "error: %s: file '%s' couldn't get layout\n",
1656                                 progname, fname);
1657                         return -ENODATA;
1658                 }
1659
1660                 rc = llapi_layout_sanity(layout, false, true);
1661
1662                 llapi_layout_free(layout);
1663
1664                 if (rc) {
1665                         llapi_layout_sanity_perror(rc);
1666                         return rc;
1667                 }
1668         }
1669
1670         while (list) {
1671                 if (list->m_file) {
1672                         has_m_file = true;
1673                         llapi_layout_free(list->m_layout);
1674
1675                         list->m_layout =
1676                                 llapi_layout_get_by_path(list->m_file, 0);
1677                         if (!list->m_layout) {
1678                                 fprintf(stderr,
1679                                         "error: %s: file '%s' has no layout\n",
1680                                         progname, list->m_file);
1681                                 return -ENODATA;
1682                         }
1683                 } else {
1684                         has_m_layout = true;
1685                         if (!list->m_layout) {
1686                                 fprintf(stderr, "error: %s: no mirror layout\n",
1687                                         progname);
1688                                 return -EINVAL;
1689                         }
1690                 }
1691
1692                 rc = llapi_layout_sanity(list->m_layout, false, true);
1693                 if (rc) {
1694                         llapi_layout_sanity_perror(rc);
1695                         return rc;
1696                 }
1697
1698                 list = list->m_next;
1699         }
1700
1701         if (has_m_file && has_m_layout) {
1702                 fprintf(stderr,
1703                         "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1704                         progname);
1705                 return -EINVAL;
1706         }
1707
1708         return 0;
1709 }
1710
1711 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1712 {
1713         __u32 mirror_flags = *(__u32 *)cbdata;
1714         uint32_t flags;
1715         int rc;
1716
1717         rc = llapi_layout_comp_flags_get(layout, &flags);
1718         if (rc < 0)
1719                 return rc;
1720
1721         if (!flags) {
1722                 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1723                 if (rc)
1724                         return rc;
1725         }
1726
1727         return LLAPI_LAYOUT_ITER_CONT;
1728 }
1729
1730 /**
1731  * mirror_create() - Create a mirrored file.
1732  * @fname:        The file to be created.
1733  * @mirror_list:  A linked list that stores the mirror arguments.
1734  *
1735  * This function creates a mirrored file @fname with the mirror(s)
1736  * from @mirror_list.
1737  *
1738  * Return: 0 on success or a negative error code on failure.
1739  */
1740 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1741 {
1742         struct llapi_layout *layout = NULL;
1743         struct mirror_args *cur_mirror = NULL;
1744         uint16_t mirror_count = 0;
1745         int i = 0;
1746         int rc = 0;
1747
1748         rc = mirror_create_sanity_check(fname, mirror_list, false);
1749         if (rc)
1750                 return rc;
1751
1752         cur_mirror = mirror_list;
1753         while (cur_mirror) {
1754                 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1755                                                mirror_set_flags,
1756                                                &cur_mirror->m_flags);
1757                 if (rc) {
1758                         rc = -errno;
1759                         fprintf(stderr, "%s: failed to set mirror flags\n",
1760                                 progname);
1761                         goto error;
1762                 }
1763
1764                 for (i = 0; i < cur_mirror->m_count; i++) {
1765                         rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1766                         if (rc) {
1767                                 rc = -errno;
1768                                 fprintf(stderr,
1769                                         "error: %s: merge layout failed: %s\n",
1770                                         progname, strerror(errno));
1771                                 goto error;
1772                         }
1773                 }
1774                 mirror_count += cur_mirror->m_count;
1775                 cur_mirror = cur_mirror->m_next;
1776         }
1777
1778         if (!layout) {
1779                 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1780                 return -EINVAL;
1781         }
1782
1783         rc = llapi_layout_mirror_count_set(layout, mirror_count);
1784         if (rc) {
1785                 rc = -errno;
1786                 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1787                         progname, strerror(errno));
1788                 goto error;
1789         }
1790
1791         rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1792                                   layout);
1793         if (rc >= 0) {
1794                 close(rc);
1795                 rc = 0;
1796         }
1797
1798 error:
1799         llapi_layout_free(layout);
1800         return rc;
1801 }
1802
1803 /**
1804  * Compare files and check lease on @fd.
1805  *
1806  * \retval bytes number of bytes are the same
1807  */
1808 static ssize_t mirror_file_compare(int fd, int fdv)
1809 {
1810         const size_t buflen = 4 * 1024 * 1024; /* 4M */
1811         void *buf;
1812         ssize_t bytes_done = 0;
1813         ssize_t bytes_read = 0;
1814
1815         buf = malloc(buflen * 2);
1816         if (!buf)
1817                 return -ENOMEM;
1818
1819         while (1) {
1820                 if (!llapi_lease_check(fd)) {
1821                         bytes_done = -EBUSY;
1822                         break;
1823                 }
1824
1825                 bytes_read = read(fd, buf, buflen);
1826                 if (bytes_read <= 0)
1827                         break;
1828
1829                 if (bytes_read != read(fdv, buf + buflen, buflen))
1830                         break;
1831
1832                 /*
1833                  * XXX: should compute the checksum on each buffer and then
1834                  * compare checksum to avoid cache collision
1835                  */
1836                 if (memcmp(buf, buf + buflen, bytes_read))
1837                         break;
1838
1839                 bytes_done += bytes_read;
1840         }
1841
1842         free(buf);
1843
1844         return bytes_done;
1845 }
1846
1847 static int mirror_extend_file(const char *fname, const char *victim_file,
1848                               enum mirror_flags mirror_flags)
1849 {
1850         int fd = -1;
1851         int fdv = -1;
1852         struct stat stbuf;
1853         struct stat stbuf_v;
1854         struct ll_ioc_lease *data = NULL;
1855         int rc;
1856
1857         fd = open(fname, O_RDWR);
1858         if (fd < 0) {
1859                 error_loc = "open source file";
1860                 rc = -errno;
1861                 goto out;
1862         }
1863
1864         fdv = open(victim_file, O_RDWR);
1865         if (fdv < 0) {
1866                 error_loc = "open target file";
1867                 rc = -errno;
1868                 goto out;
1869         }
1870
1871         if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1872                 error_loc = "stat source or target file";
1873                 rc = -errno;
1874                 goto out;
1875         }
1876
1877         if (stbuf.st_dev != stbuf_v.st_dev) {
1878                 error_loc = "stat source and target file";
1879                 rc = -EXDEV;
1880                 goto out;
1881         }
1882
1883         /* mirrors should be of the same size */
1884         if (stbuf.st_size != stbuf_v.st_size) {
1885                 error_loc = "file sizes don't match";
1886                 rc = -EINVAL;
1887                 goto out;
1888         }
1889
1890         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1891         if (rc < 0) {
1892                 error_loc = "cannot get lease";
1893                 goto out;
1894         }
1895
1896         if (!(mirror_flags & MF_NO_VERIFY)) {
1897                 ssize_t ret;
1898                 /* mirrors should have the same contents */
1899                 ret = mirror_file_compare(fd, fdv);
1900                 if (ret != stbuf.st_size) {
1901                         error_loc = "file busy or contents don't match";
1902                         rc = ret < 0 ? ret : -EINVAL;
1903                         goto out;
1904                 }
1905         }
1906
1907         /* Get rid of caching pages from clients */
1908         rc = llapi_file_flush(fd);
1909         if (rc < 0) {
1910                 error_loc = "cannot get data version";
1911                 goto out;
1912         }
1913
1914         rc = llapi_file_flush(fdv);
1915         if (rc < 0) {
1916                 error_loc = "cannot get data version";
1917                 goto out;
1918         }
1919
1920         rc = migrate_set_timestamps(fd, &stbuf);
1921         if (rc < 0) {
1922                 error_loc = "cannot set source file timestamp";
1923                 goto out;
1924         }
1925
1926         /* Atomically put lease, merge layouts and close. */
1927         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1928         if (!data) {
1929                 error_loc = "memory allocation";
1930                 goto out;
1931         }
1932         data->lil_mode = LL_LEASE_UNLCK;
1933         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1934         data->lil_count = 1;
1935         data->lil_ids[0] = fdv;
1936         rc = llapi_lease_set(fd, data);
1937         if (rc < 0) {
1938                 error_loc = "cannot merge layout";
1939                 goto out;
1940         } else if (rc == 0) {
1941                 rc = -EBUSY;
1942                 error_loc = "lost lease lock";
1943                 goto out;
1944         }
1945         rc = 0;
1946
1947 out:
1948         if (data)
1949                 free(data);
1950         if (fd >= 0)
1951                 close(fd);
1952         if (fdv >= 0)
1953                 close(fdv);
1954         if (!rc)
1955                 (void) unlink(victim_file);
1956         if (rc < 0)
1957                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1958                         progname, fname, error_loc, strerror(-rc));
1959         return rc;
1960 }
1961
1962 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1963                                 bool inherit, uint32_t flags,
1964                                 unsigned long long bandwidth_bytes_sec,
1965                                 enum stats_flag stats_flag,
1966                                 long stats_interval_sec)
1967 {
1968         struct llapi_layout *f_layout = NULL;
1969         struct ll_ioc_lease *data = NULL;
1970         struct stat st;
1971         int fd = -1;
1972         int fdv = -1;
1973         int rc = 0;
1974
1975         if (inherit) {
1976                 f_layout = llapi_layout_get_by_path(name, 0);
1977                 if (!f_layout) {
1978                         rc = -EINVAL;
1979                         fprintf(stderr, "%s: cannot get layout\n", progname);
1980                         goto out;
1981                 }
1982                 rc = llapi_layout_get_last_init_comp(f_layout);
1983                 if (rc) {
1984                         fprintf(stderr, "%s: cannot get the last init comp\n",
1985                                 progname);
1986                         goto out;
1987                 }
1988                 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1989                 if (rc) {
1990                         fprintf(stderr,
1991                                 "%s: cannot inherit from the last init comp\n",
1992                                 progname);
1993                         goto out;
1994                 }
1995         }
1996
1997         llapi_layout_comp_flags_set(m_layout, flags);
1998         rc = migrate_open_files(name,
1999                              LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
2000                              NULL, m_layout, &fd, &fdv);
2001         if (rc < 0)
2002                 goto out;
2003
2004         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2005         if (rc < 0) {
2006                 error_loc = "cannot get lease";
2007                 goto out;
2008         }
2009
2010         rc = fstat(fd, &st);
2011         if (rc < 0) {
2012                 error_loc = "cannot stat source file";
2013                 goto out;
2014         }
2015
2016         if (stats_flag)
2017                 printf("%s:\n", name);
2018
2019         rc = migrate_nonblock(fd, fdv, bandwidth_bytes_sec, stats_flag,
2020                               stats_interval_sec);
2021         if (rc < 0) {
2022                 llapi_lease_release(fd);
2023                 goto out;
2024         }
2025
2026         rc = migrate_set_timestamps(fd, &st);
2027         if (rc < 0) {
2028                 error_loc = "cannot set source file timestamp";
2029                 goto out;
2030         }
2031
2032         /* Atomically put lease, merge layouts and close. */
2033         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2034         if (!data) {
2035                 error_loc = "memory allocation";
2036                 goto out;
2037         }
2038         data->lil_mode = LL_LEASE_UNLCK;
2039         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2040         data->lil_count = 1;
2041         data->lil_ids[0] = fdv;
2042         rc = llapi_lease_set(fd, data);
2043         if (rc < 0) {
2044                 error_loc = "cannot merge layout";
2045                 goto out;
2046         } else if (rc == 0) {
2047                 rc = -EBUSY;
2048                 error_loc = "lost lease lock";
2049                 goto out;
2050         }
2051         rc = 0;
2052
2053 out:
2054         if (data)
2055                 free(data);
2056         if (fd >= 0)
2057                 close(fd);
2058         if (fdv >= 0)
2059                 close(fdv);
2060         if (rc < 0)
2061                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2062                         progname, name, error_loc, strerror(-rc));
2063         return rc;
2064 }
2065
2066 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2067                          enum mirror_flags mirror_flags,
2068                          unsigned long long bandwidth_bytes_sec,
2069                          enum stats_flag stats_flag, long stats_interval_sec)
2070 {
2071         int rc = 0;
2072
2073         while (mirror_list) {
2074                 if (mirror_list->m_file) {
2075                         rc = mirror_extend_file(fname, mirror_list->m_file,
2076                                                 mirror_flags);
2077                 } else {
2078                         __u32 mirror_count = mirror_list->m_count;
2079
2080                         while (mirror_count > 0) {
2081                                 rc = mirror_extend_layout(fname,
2082                                                         mirror_list->m_layout,
2083                                                         mirror_list->m_inherit,
2084                                                         mirror_list->m_flags,
2085                                                         bandwidth_bytes_sec,
2086                                                         stats_flag,
2087                                                         stats_interval_sec);
2088                                 if (rc)
2089                                         break;
2090
2091                                 --mirror_count;
2092                         }
2093                 }
2094                 if (rc)
2095                         break;
2096
2097                 mirror_list = mirror_list->m_next;
2098         }
2099
2100         return rc;
2101 }
2102
2103 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2104 {
2105         uint32_t id;
2106         int rc;
2107
2108         rc = llapi_layout_mirror_id_get(layout, &id);
2109         if (rc < 0)
2110                 return rc;
2111
2112         if ((__u16)id == *(__u16 *)cbdata)
2113                 return LLAPI_LAYOUT_ITER_STOP;
2114
2115         return LLAPI_LAYOUT_ITER_CONT;
2116 }
2117
2118 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2119 {
2120         uint32_t id;
2121         int rc;
2122
2123         rc = llapi_layout_comp_id_get(layout, &id);
2124         if (rc < 0)
2125                 return rc;
2126
2127         if (id == *(__u32 *)cbdata)
2128                 return LLAPI_LAYOUT_ITER_STOP;
2129
2130         return LLAPI_LAYOUT_ITER_CONT;
2131 }
2132
2133 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2134 {
2135         char buf[LOV_MAXPOOLNAME + 1];
2136         struct pool_to_id_cbdata *d = (void *)cbdata;
2137         uint32_t id;
2138         int rc;
2139
2140         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2141         if (rc < 0)
2142                 return rc;
2143         if (strcmp(d->pool, buf))
2144                 return LLAPI_LAYOUT_ITER_CONT;
2145
2146         rc = llapi_layout_mirror_id_get(layout, &id);
2147         if (rc < 0)
2148                 return rc;
2149         d->id = id;
2150
2151         return LLAPI_LAYOUT_ITER_STOP;
2152 }
2153
2154 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2155 {
2156         char buf[LOV_MAXPOOLNAME + 1];
2157         struct pool_to_id_cbdata *d = (void *)cbdata;
2158         uint32_t id;
2159         int rc;
2160
2161         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2162         if (rc < 0)
2163                 return rc;
2164         if (strcmp(d->pool, buf))
2165                 return LLAPI_LAYOUT_ITER_CONT;
2166
2167         rc = llapi_layout_comp_id_get(layout, &id);
2168         if (rc < 0)
2169                 return rc;
2170         d->id = id;
2171
2172         return LLAPI_LAYOUT_ITER_STOP;
2173 }
2174
2175 struct collect_ids_data {
2176         __u16   *cid_ids;
2177         int     cid_count;
2178         __u16   cid_exclude;
2179 };
2180
2181 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2182 {
2183         struct collect_ids_data *cid = cbdata;
2184         uint32_t id;
2185         int rc;
2186
2187         rc = llapi_layout_mirror_id_get(layout, &id);
2188         if (rc < 0)
2189                 return rc;
2190
2191         if ((__u16)id != cid->cid_exclude) {
2192                 int i;
2193
2194                 for (i = 0; i < cid->cid_count; i++) {
2195                         /* already collected the mirror id */
2196                         if (id == cid->cid_ids[i])
2197                                 return LLAPI_LAYOUT_ITER_CONT;
2198                 }
2199                 cid->cid_ids[cid->cid_count] = id;
2200                 cid->cid_count++;
2201         }
2202
2203         return LLAPI_LAYOUT_ITER_CONT;
2204 }
2205
2206 /**
2207  * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2208  * @mirror_id: Mirror id to be checked.
2209  * @layout:    Mirror component list.
2210  *
2211  * This function checks if a mirror with specified @mirror_id is the last
2212  * non-stale mirror of a layout @layout.
2213  *
2214  * Return: true or false.
2215  */
2216 static inline
2217 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2218 {
2219         __u16 mirror_ids[128] = { 0 };
2220         struct collect_ids_data cid = { .cid_ids = mirror_ids,
2221                                         .cid_count = 0,
2222                                         .cid_exclude = mirror_id, };
2223         int i;
2224
2225         llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2226
2227         for (i = 0; i < cid.cid_count; i++) {
2228                 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2229                 int comp_size = 0;
2230
2231                 comp_size = llapi_mirror_find_stale(layout, comp_array,
2232                                                     ARRAY_SIZE(comp_array),
2233                                                     &mirror_ids[i], 1);
2234                 if (comp_size == 0)
2235                         return false;
2236         }
2237
2238         return true;
2239 }
2240
2241 static int mirror_split(const char *fname, __u32 id, const char *pool,
2242                         enum mirror_flags mflags, const char *victim_file)
2243 {
2244         struct llapi_layout *layout;
2245         char parent[PATH_MAX];
2246         char victim[PATH_MAX];
2247         int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2248         char *ptr;
2249         struct ll_ioc_lease *data;
2250         uint16_t mirror_count;
2251         __u32 mirror_id;
2252         int mdt_index;
2253         int fd, fdv;
2254         bool purge = true; /* delete mirror by setting fdv=fd */
2255         bool is_encrypted;
2256         int rc;
2257
2258         if (victim_file && (strcmp(fname, victim_file) == 0)) {
2259                 fprintf(stderr,
2260                         "error %s: the source file '%s' and -f file are the same\n",
2261                         progname, fname);
2262                 return -EINVAL;
2263         }
2264
2265         /* check fname contains mirror with mirror_id/comp_id */
2266         layout = llapi_layout_get_by_path(fname, 0);
2267         if (!layout) {
2268                 fprintf(stderr,
2269                         "error %s: file '%s' couldn't get layout\n",
2270                         progname, fname);
2271                 return -EINVAL;
2272         }
2273
2274         rc = llapi_layout_sanity(layout, false, true);
2275         if (rc) {
2276                 llapi_layout_sanity_perror(rc);
2277                 goto free_layout;
2278         }
2279
2280         rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2281         if (rc) {
2282                 fprintf(stderr,
2283                         "error %s: file '%s' couldn't get mirror count\n",
2284                         progname, fname);
2285                 goto free_layout;
2286         }
2287         if (mirror_count < 2) {
2288                 fprintf(stderr,
2289                         "error %s: file '%s' has %d component, cannot split\n",
2290                         progname, fname, mirror_count);
2291                 goto free_layout;
2292         }
2293
2294         if (mflags & MF_COMP_POOL) {
2295                 struct pool_to_id_cbdata data = { .pool = pool };
2296
2297                 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2298                                                &data);
2299                 mirror_id = data.id;
2300         } else if (mflags & MF_COMP_ID) {
2301                 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2302                 mirror_id = mirror_id_of(id);
2303         } else {
2304                 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2305                 mirror_id = id;
2306         }
2307         if (rc < 0) {
2308                 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2309                         progname, fname);
2310                 goto free_layout;
2311         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2312                 if (mflags & MF_COMP_POOL) {
2313                         fprintf(stderr,
2314                                 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2315                                 progname, fname, pool);
2316                         goto free_layout;
2317                 } else if (mflags & MF_COMP_ID) {
2318                         fprintf(stderr,
2319                                 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2320                                 progname, fname, id);
2321                         goto free_layout;
2322                 } else {
2323                         fprintf(stderr,
2324                                 "error %s: file '%s' does not contain mirror with id %u\n",
2325                                 progname, fname, id);
2326                         goto free_layout;
2327                 }
2328         }
2329
2330         if (!victim_file && mflags & MF_DESTROY)
2331                 /* Allow mirror split even without the key on encrypted files,
2332                  * and in this case of a 'split -d', open file with O_DIRECT
2333                  * (no IOs will be done).
2334                  */
2335                 fd = open(fname, O_RDWR | O_DIRECT | O_FILE_ENC);
2336         else
2337                 fd = open(fname, O_RDWR);
2338
2339         if (fd < 0) {
2340                 fprintf(stderr,
2341                         "error %s: open file '%s' failed: %s\n",
2342                         progname, fname, strerror(errno));
2343                 goto free_layout;
2344         }
2345
2346         /* get victim file directory pathname */
2347         if (strlen(fname) > sizeof(parent) - 1) {
2348                 fprintf(stderr, "error %s: file name of '%s' too long\n",
2349                         progname, fname);
2350                 rc = -ERANGE;
2351                 goto close_fd;
2352         }
2353         strncpy(parent, fname, sizeof(parent));
2354         ptr = strrchr(parent, '/');
2355         if (!ptr) {
2356                 if (!getcwd(parent, sizeof(parent))) {
2357                         fprintf(stderr, "error %s: getcwd failed: %s\n",
2358                                 progname, strerror(errno));
2359                         rc = -errno;
2360                         goto close_fd;
2361                 }
2362         } else {
2363                 if (ptr == parent)
2364                         ptr = parent + 1;
2365                 *ptr = '\0';
2366         }
2367
2368         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2369         if (rc < 0) {
2370                 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2371                         progname, fname);
2372                 goto close_fd;
2373         }
2374
2375         rc = llapi_file_is_encrypted(fd);
2376         if (rc < 0) {
2377                 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2378                         progname, fname, rc);
2379                 goto close_fd;
2380         }
2381         is_encrypted = rc;
2382
2383 again:
2384         if (!victim_file) {
2385                 /* use a temp file to store the splitted layout */
2386                 if (mflags & MF_DESTROY) {
2387                         char file_path[PATH_MAX];
2388                         unsigned int rnumber;
2389                         int open_flags;
2390
2391                         if (last_non_stale_mirror(mirror_id, layout)) {
2392                                 rc = -EUCLEAN;
2393                                 fprintf(stderr,
2394                                         "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2395                                         progname, fname);
2396                                 goto close_fd;
2397                         }
2398
2399                         if (purge) {
2400                                 /* don't use volatile file for mirror destroy */
2401                                 fdv = fd;
2402                         } else {
2403                                 /**
2404                                  * try the old way to delete mirror using
2405                                  * volatile file.
2406                                  */
2407                                 do {
2408                                         rnumber = random();
2409                                         rc = snprintf(file_path,
2410                                                       sizeof(file_path),
2411                                                       "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2412                                                       parent, mdt_index,
2413                                                       rnumber, fd);
2414                                         if (rc < 0 ||
2415                                             rc >= sizeof(file_path)) {
2416                                                 fdv = -ENAMETOOLONG;
2417                                                 break;
2418                                         }
2419
2420                                         open_flags = O_RDWR |
2421                                              (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2422                                              O_CREAT | O_EXCL | O_NOFOLLOW |
2423                                              /* O_DIRECT for mirror split -d */
2424                                              O_DIRECT |
2425                                              /* Allow split without the key */
2426                                              O_FILE_ENC;
2427                                         fdv = open(file_path, open_flags,
2428                                                    S_IRUSR | S_IWUSR);
2429                                         if (fdv < 0)
2430                                                 rc = -errno;
2431                                 } while (fdv < 0 && rc == -EEXIST);
2432                         }
2433                 } else {
2434                         if (is_encrypted) {
2435                                 rc = -1;
2436                                 fprintf(stderr,
2437                                         "error %s: not permitted on encrypted file '%s': %d\n",
2438                                         progname, fname, rc);
2439                                 goto close_fd;
2440                         }
2441
2442                         snprintf(victim, sizeof(victim), "%s.mirror~%u",
2443                                  fname, mirror_id);
2444                         fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2445                 }
2446         } else {
2447                 /* user specified victim file */
2448                 if (is_encrypted) {
2449                         rc = -1;
2450                         fprintf(stderr,
2451                                 "error %s: not permitted on encrypted file '%s': %d\n",
2452                                 progname, fname, rc);
2453                         goto close_fd;
2454                 }
2455                 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2456         }
2457
2458         if (fdv < 0) {
2459                 fprintf(stderr,
2460                         "error %s: create victim file failed: %s\n",
2461                         progname, strerror(errno));
2462                 goto close_fd;
2463         }
2464
2465         /* get lease lock of fname */
2466         rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2467         if (rc < 0) {
2468                 fprintf(stderr,
2469                         "error %s: cannot get lease of file '%s': %d\n",
2470                         progname, fname, rc);
2471                 goto close_victim;
2472         }
2473
2474         /* Atomatically put lease, split layouts and close. */
2475         data = malloc(offsetof(typeof(*data), lil_ids[2]));
2476         if (!data) {
2477                 rc = -ENOMEM;
2478                 goto close_victim;
2479         }
2480
2481         data->lil_mode = LL_LEASE_UNLCK;
2482         data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2483         data->lil_count = 2;
2484         data->lil_ids[0] = fdv;
2485         data->lil_ids[1] = mirror_id;
2486         rc = llapi_lease_set(fd, data);
2487         if (rc <= 0) {
2488                 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2489                         /* could be old MDS which prohibit fd==fdv */
2490                         purge = false;
2491                         goto again;
2492
2493                 }
2494                 if (rc == 0) /* lost lease lock */
2495                         rc = -EBUSY;
2496                 fprintf(stderr,
2497                         "error %s: cannot split '%s': %s\n",
2498                         progname, fname, strerror(-rc));
2499         } else {
2500                 rc = 0;
2501         }
2502         free(data);
2503
2504 close_victim:
2505         if (!purge)
2506                 close(fdv);
2507 close_fd:
2508         close(fd);
2509 free_layout:
2510         llapi_layout_free(layout);
2511         return rc;
2512 }
2513
2514 static inline
2515 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2516                            __u16 *mirror_ids, int ids_nr);
2517
2518 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2519                               __u64 migration_flags,
2520                               unsigned long long bandwidth_bytes_sec,
2521                               enum stats_flag stats_flag,
2522                               long stats_interval_sec)
2523 {
2524         struct ll_ioc_lease *data = NULL;
2525         int rc;
2526
2527         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2528         if (rc < 0) {
2529                 error_loc = "cannot get lease";
2530                 goto out_close;
2531         }
2532
2533         if (stats_flag)
2534                 printf("%s:\n", name);
2535
2536         rc = migrate_nonblock(fd, fdv, bandwidth_bytes_sec, stats_flag,
2537                               stats_interval_sec);
2538         if (rc < 0)
2539                 goto out_release;
2540
2541         /* Atomically put lease, merge layouts, resync and close. */
2542         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2543         if (!data) {
2544                 error_loc = "memory allocation";
2545                 goto out_release;
2546         }
2547         data->lil_mode = LL_LEASE_UNLCK;
2548         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2549         data->lil_count = 1;
2550         data->lil_ids[0] = fdv;
2551         rc = llapi_lease_set(fd, data);
2552         if (rc < 0) {
2553                 error_loc = "cannot merge layout";
2554                 goto out_close;
2555         } else if (rc == 0) {
2556                 rc = -EBUSY;
2557                 error_loc = "lost lease lock";
2558                 goto out_close;
2559         }
2560         close(fd);
2561         close(fdv);
2562
2563         rc = lfs_mirror_resync_file(name, data, NULL, 0);
2564         if (rc) {
2565                 error_loc = "cannot resync file";
2566                 goto out;
2567         }
2568
2569         /* delete first mirror now */
2570         rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2571         if (rc < 0)
2572                 error_loc = "cannot delete old layout";
2573         goto out;
2574
2575 out_release:
2576         llapi_lease_release(fd);
2577 out_close:
2578         close(fd);
2579         close(fdv);
2580 out:
2581         if (rc < 0)
2582                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2583                         progname, name, error_loc, strerror(-rc));
2584         else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2585                 printf("%s\n", name);
2586         if (data)
2587                 free(data);
2588         return rc;
2589 }
2590
2591 /**
2592  * Parse a string containing an target index list into an array of integers.
2593  *
2594  * The input string contains a comma delimited list of individual
2595  * indices and ranges, for example "1,2-4,7". Add the indices into the
2596  * \a tgts array and remove duplicates.
2597  *
2598  * \param[out] tgts             array to store indices in
2599  * \param[in] size              size of \a tgts array
2600  * \param[in] offset            starting index in \a tgts
2601  * \param[in] arg               string containing OST index list
2602  * \param[in/out] overstriping  index list may contain duplicates
2603  *
2604  * \retval positive    number of indices in \a tgts
2605  * \retval -EINVAL     unable to parse \a arg
2606  */
2607 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2608                          unsigned long long *pattern)
2609 {
2610         int rc;
2611         int nr = offset;
2612         int slots = size - offset;
2613         char *ptr = NULL;
2614         bool overstriped = false;
2615         bool end_of_loop;
2616
2617         if (!arg)
2618                 return -EINVAL;
2619
2620         end_of_loop = false;
2621         while (!end_of_loop) {
2622                 int start_index = 0;
2623                 int end_index = 0;
2624                 int i;
2625                 char *endptr = NULL;
2626
2627                 rc = -EINVAL;
2628
2629                 ptr = strchrnul(arg, ',');
2630
2631                 end_of_loop = *ptr == '\0';
2632                 *ptr = '\0';
2633
2634                 errno = 0;
2635                 start_index = strtol(arg, &endptr, 0);
2636                 if (endptr == arg) /* no data at all */
2637                         break;
2638                 if (errno != 0 || start_index < -1 ||
2639                     (*endptr != '-' && *endptr != '\0'))
2640                         break;
2641
2642                 end_index = start_index;
2643                 if (*endptr == '-') {
2644                         errno = 0;
2645                         end_index = strtol(endptr + 1, &endptr, 0);
2646                         if (errno != 0 || *endptr != '\0' || end_index < -1)
2647                                 break;
2648                         if (end_index < start_index)
2649                                 break;
2650                 }
2651
2652                 for (i = start_index; i <= end_index && slots > 0; i++) {
2653                         int j;
2654
2655                         /* remove duplicate */
2656                         for (j = 0; j < offset; j++) {
2657                                 if (tgts[j] == i && pattern &&
2658                                     *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2659                                         overstriped = true;
2660                                 else if (tgts[j] == i)
2661                                         return -EINVAL;
2662                         }
2663
2664                         j = offset;
2665
2666                         if (j == offset) { /* check complete */
2667                                 tgts[nr++] = i;
2668                                 --slots;
2669                         }
2670                 }
2671
2672                 if (slots == 0 && i < end_index)
2673                         break;
2674
2675                 *ptr = ',';
2676                 arg = ++ptr;
2677                 offset = nr;
2678                 rc = 0;
2679         }
2680         if (!end_of_loop && ptr)
2681                 *ptr = ',';
2682
2683         if (!overstriped && pattern)
2684                 *pattern = LLAPI_LAYOUT_DEFAULT;
2685
2686         return rc < 0 ? rc : nr;
2687 }
2688
2689 struct lfs_setstripe_args {
2690         unsigned long long       lsa_comp_end;
2691         unsigned long long       lsa_stripe_size;
2692         unsigned long long       lsa_extension_size;
2693         long long                lsa_stripe_count;
2694         long long                lsa_stripe_off;
2695         __u32                    lsa_comp_flags;
2696         __u32                    lsa_comp_neg_flags;
2697         unsigned long long       lsa_pattern;
2698         unsigned int             lsa_mirror_count;
2699         int                      lsa_nr_tgts;
2700         bool                     lsa_first_comp;
2701         bool                     lsa_extension_comp;
2702         __u32                   *lsa_tgts;
2703         char                    *lsa_pool_name;
2704 };
2705
2706 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2707 {
2708         unsigned int mirror_count = lsa->lsa_mirror_count;
2709         bool first_comp = lsa->lsa_first_comp;
2710
2711         memset(lsa, 0, sizeof(*lsa));
2712
2713         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2714         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2715         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2716         lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2717         lsa->lsa_pool_name = NULL;
2718
2719         lsa->lsa_mirror_count = mirror_count;
2720         lsa->lsa_first_comp = first_comp;
2721 }
2722
2723 /**
2724  * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2725  * @lsa: Stripe options to be initialized and inherited.
2726  *
2727  * This function initializes stripe options in @lsa and inherit
2728  * stripe_size, stripe_count and OST pool_name options.
2729  *
2730  * Return: void.
2731  */
2732 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2733 {
2734         unsigned long long stripe_size;
2735         long long stripe_count;
2736         char *pool_name = NULL;
2737
2738         stripe_size = lsa->lsa_stripe_size;
2739         stripe_count = lsa->lsa_stripe_count;
2740         pool_name = lsa->lsa_pool_name;
2741
2742         setstripe_args_init(lsa);
2743
2744         lsa->lsa_stripe_size = stripe_size;
2745         lsa->lsa_stripe_count = stripe_count;
2746         lsa->lsa_pool_name = pool_name;
2747 }
2748
2749 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2750 {
2751         return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2752                 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2753                 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2754                 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2755                 lsa->lsa_comp_end != 0);
2756 }
2757
2758 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2759 {
2760         if (lsa->lsa_nr_tgts) {
2761                 if (lsa->lsa_nr_tgts < 0 ||
2762                     lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2763                         fprintf(stderr, "Invalid nr_tgts(%d)\n",
2764                                 lsa->lsa_nr_tgts);
2765                         errno = EINVAL;
2766                         return -1;
2767                 }
2768
2769                 if (lsa->lsa_stripe_count > 0 &&
2770                     lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2771                     lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2772                     lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2773                         fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2774                                 lsa->lsa_stripe_count,
2775                                 lsa->lsa_nr_tgts);
2776                         errno = EINVAL;
2777                         return -1;
2778                 }
2779         }
2780
2781         return 0;
2782
2783 }
2784
2785 /**
2786  * comp_args_to_layout() - Create or extend a composite layout.
2787  * @composite:       Pointer to the composite layout.
2788  * @lsa:             Stripe options for the new component.
2789  *
2790  * This function creates or extends a composite layout by adding a new
2791  * component with stripe options from @lsa.
2792  *
2793  * When modified, adjust llapi_stripe_param_verify() if needed as well.
2794  *
2795  * Return: 0 on success or an error code on failure.
2796  */
2797 static int comp_args_to_layout(struct llapi_layout **composite,
2798                                struct lfs_setstripe_args *lsa,
2799                                bool set_extent)
2800 {
2801         struct llapi_layout *layout = *composite;
2802         uint64_t prev_end = 0;
2803         uint64_t size;
2804         int i = 0, rc;
2805
2806 new_comp:
2807         if (!layout) {
2808                 layout = llapi_layout_alloc();
2809                 if (!layout) {
2810                         fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2811                                 strerror(errno));
2812                         errno = ENOMEM;
2813                         return -1;
2814                 }
2815                 *composite = layout;
2816                 lsa->lsa_first_comp = true;
2817         } else {
2818                 uint64_t start;
2819
2820                 /*
2821                  * Get current component extent, current component
2822                  * must be the tail component.
2823                  */
2824                 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2825                 if (rc) {
2826                         fprintf(stderr, "Get comp extent failed. %s\n",
2827                                 strerror(errno));
2828                         return rc;
2829                 }
2830
2831                 if (lsa->lsa_first_comp) {
2832                         prev_end = 0;
2833                         rc = llapi_layout_add_first_comp(layout);
2834                 } else {
2835                         rc = llapi_layout_comp_add(layout);
2836                 }
2837                 if (rc) {
2838                         fprintf(stderr, "Add component failed. %s\n",
2839                                 strerror(errno));
2840                         return rc;
2841                 }
2842         }
2843
2844         rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2845         if (rc) {
2846                 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2847                         lsa->lsa_comp_flags, strerror(errno));
2848                 return rc;
2849         }
2850
2851         if (set_extent) {
2852                 uint64_t comp_end = lsa->lsa_comp_end;
2853
2854                 /*
2855                  * The extendable component is 0-length, so it can be removed
2856                  * if there is insufficient space to extend it.
2857                  */
2858                 if (lsa->lsa_extension_comp)
2859                         comp_end = prev_end;
2860
2861                 rc = llapi_layout_comp_extent_set(layout, prev_end,
2862                                                   comp_end);
2863                 if (rc) {
2864                         fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2865                                 prev_end, comp_end, strerror(errno));
2866                         return rc;
2867                 }
2868         }
2869         /* reset lsa_first_comp */
2870         lsa->lsa_first_comp = false;
2871
2872         /* Data-on-MDT component setting */
2873         if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2874                 /* Yaml support */
2875                 if (lsa->lsa_stripe_count == 0)
2876                         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2877                 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2878                         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2879                 if (lsa->lsa_stripe_off == -1 ||
2880                     lsa->lsa_stripe_off == 0)
2881                         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2882                 /*
2883                  * In case of Data-on-MDT patterns the only extra option
2884                  * applicable is stripe size option.
2885                  */
2886                 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2887                         fprintf(stderr,
2888                                 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2889                                 lsa->lsa_stripe_count);
2890                         errno = EINVAL;
2891                         return -1;
2892                 }
2893                 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2894                     lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2895                         fprintf(stderr,
2896                                 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2897                                 lsa->lsa_stripe_size);
2898                         errno = EINVAL;
2899                         return -1;
2900                 }
2901                 if (lsa->lsa_nr_tgts != 0) {
2902                         fprintf(stderr,
2903                                 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2904                                 lsa->lsa_nr_tgts);
2905                         errno = EINVAL;
2906                         return -1;
2907                 }
2908                 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2909                         fprintf(stderr,
2910                                 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2911                                 lsa->lsa_stripe_off);
2912                         errno = EINVAL;
2913                         return -1;
2914                 }
2915                 if (lsa->lsa_pool_name != 0) {
2916                         fprintf(stderr,
2917                                 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2918                                 lsa->lsa_pool_name);
2919                         errno = EINVAL;
2920                         return -1;
2921                 }
2922
2923                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2924                 if (rc) {
2925                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2926                                 lsa->lsa_pattern,
2927                                 strerror(errno));
2928                         return rc;
2929                 }
2930                 /* Data-on-MDT component has always single stripe up to end */
2931                 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2932         } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2933                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2934                 if (rc) {
2935                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2936                                 lsa->lsa_pattern,
2937                                 strerror(errno));
2938                         return rc;
2939                 }
2940         }
2941
2942         size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2943                 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2944
2945         if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2946                 rc = llapi_layout_extension_size_set(layout, size);
2947         else
2948                 rc = llapi_layout_stripe_size_set(layout, size);
2949
2950         if (rc) {
2951                 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2952                         size, strerror(errno));
2953                 return rc;
2954         }
2955
2956         rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2957         if (rc) {
2958                 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2959                         lsa->lsa_stripe_count, strerror(errno));
2960                 return rc;
2961         }
2962
2963         if (lsa->lsa_pool_name) {
2964                 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2965                 if (rc) {
2966                         fprintf(stderr, "Set pool name: %s failed. %s\n",
2967                                 lsa->lsa_pool_name, strerror(errno));
2968                         return rc;
2969                 }
2970         } else {
2971                 rc = llapi_layout_pool_name_set(layout, "");
2972                 if (rc) {
2973                         fprintf(stderr, "Clear pool name failed: %s\n",
2974                                 strerror(errno));
2975                         return rc;
2976                 }
2977         }
2978
2979         rc = lsa_args_stripe_count_check(lsa);
2980         if (rc)
2981                 return rc;
2982
2983         if (lsa->lsa_nr_tgts > 0) {
2984                 bool found = false;
2985
2986                 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2987                         rc = llapi_layout_ost_index_set(layout, i,
2988                                                         lsa->lsa_tgts[i]);
2989                         if (rc)
2990                                 break;
2991
2992                         /* Make sure stripe offset is in OST list. */
2993                         if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2994                                 found = true;
2995                 }
2996                 if (!found) {
2997                         fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2998                                 lsa->lsa_stripe_off);
2999                         errno = EINVAL;
3000                         return -1;
3001                 }
3002         } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
3003                    lsa->lsa_stripe_off != -1) {
3004                 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
3005         }
3006         if (rc) {
3007                 fprintf(stderr, "Set ost index %d failed. %s\n",
3008                         i, strerror(errno));
3009                 return rc;
3010         }
3011
3012         /* Create the second, virtual component of extension space */
3013         if (lsa->lsa_extension_comp) {
3014                 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
3015                 lsa->lsa_extension_comp = false;
3016                 goto new_comp;
3017         }
3018
3019         return rc;
3020 }
3021
3022 static int build_component(struct llapi_layout **layout,
3023                            struct lfs_setstripe_args *lsa, bool set_extent)
3024 {
3025         int rc;
3026
3027         rc = comp_args_to_layout(layout, lsa, set_extent);
3028         if (rc)
3029                 return rc;
3030
3031         if (lsa->lsa_mirror_count > 0) {
3032                 rc = llapi_layout_mirror_count_set(*layout,
3033                                                    lsa->lsa_mirror_count);
3034                 if (rc)
3035                         return rc;
3036
3037                 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
3038                 if (rc)
3039                         return rc;
3040                 lsa->lsa_mirror_count = 0;
3041         }
3042
3043         return rc;
3044 }
3045
3046 static int build_prev_component(struct llapi_layout **layout,
3047                                 struct lfs_setstripe_args *prev,
3048                                 struct lfs_setstripe_args *lsa,
3049                                 bool set_extent)
3050 {
3051         int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
3052         int rc;
3053
3054         if (prev->lsa_stripe_size) {
3055                 if (extension) {
3056                         prev->lsa_comp_end = lsa->lsa_comp_end;
3057                         prev->lsa_extension_size = lsa->lsa_extension_size;
3058                         prev->lsa_extension_comp = true;
3059                 }
3060
3061                 rc = build_component(layout, prev, true);
3062                 if (rc)
3063                         return rc;
3064         }
3065
3066         /*
3067          * Copy lsa to previous lsa;
3068          * if this is an extension component, make the previous invalid;
3069          */
3070         if (extension)
3071                 prev->lsa_stripe_size = 0;
3072         else
3073                 *prev = *lsa;
3074
3075         return 0;
3076 }
3077
3078 #ifndef LCME_TEMPLATE_FLAGS
3079 #define LCME_TEMPLATE_FLAGS     (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
3080                                  LCME_FL_EXTENSION)
3081 #endif
3082
3083 static int build_layout_from_yaml_node(struct cYAML *node,
3084                                        struct llapi_layout **layout,
3085                                        struct lfs_setstripe_args *lsa,
3086                                        struct lfs_setstripe_args *prevp)
3087 {
3088         struct lfs_setstripe_args prev = { 0 };
3089         __u32 *osts = lsa->lsa_tgts;
3090         char *string;
3091         int rc = 0;
3092
3093         if (!prevp)
3094                 prevp = &prev;
3095
3096         while (node) {
3097                 string = node->cy_string;
3098
3099                 if (node->cy_type == CYAML_TYPE_OBJECT) {
3100                         /* go deep to sub blocks */
3101                         if (string && !strncmp(string, "component", 9) &&
3102                             strncmp(string, "component0", 10) &&
3103                             strncmp(string, "components", 10)) {
3104                                 rc = build_prev_component(layout, prevp, lsa,
3105                                                           true);
3106                                 if (rc)
3107                                         return rc;
3108
3109                                 /* initialize lsa. */
3110                                 setstripe_args_init(lsa);
3111                                 lsa->lsa_first_comp = false;
3112                                 lsa->lsa_tgts = osts;
3113                         }
3114
3115                         rc = build_layout_from_yaml_node(node->cy_child, layout,
3116                                                          lsa, prevp);
3117                         if (rc)
3118                                 return rc;
3119                 } else {
3120                         if (!node->cy_string)
3121                                 return -EINVAL;
3122
3123                         /* skip leading lmm_ if present, to simplify parsing */
3124                         if (strncmp(string, "lmm_", 4) == 0)
3125                                 string += 4;
3126
3127                         if (node->cy_type == CYAML_TYPE_STRING) {
3128                                 if (!strcmp(string, "lcme_extent.e_end")) {
3129                                         if (!strcmp(node->cy_valuestring, "EOF") ||
3130                                             !strcmp(node->cy_valuestring, "eof"))
3131                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3132                                 } else if (!strcmp(string, "pool")) {
3133                                         lsa->lsa_pool_name = node->cy_valuestring;
3134                                 } else if (!strcmp(string, "pattern")) {
3135                                         if (!strcmp(node->cy_valuestring, "mdt"))
3136                                                 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3137                                         if (!strcmp(node->cy_valuestring,
3138                                                     "raid0,overstriped"))
3139                                                 lsa->lsa_pattern =
3140                                                         LLAPI_LAYOUT_OVERSTRIPING;
3141                                 } else if (!strcmp(string, "lcme_flags")) {
3142                                         rc = comp_str2flags(node->cy_valuestring,
3143                                                             &lsa->lsa_comp_flags,
3144                                                             &lsa->lsa_comp_neg_flags);
3145                                         if (rc)
3146                                                 return rc;
3147                                         /*
3148                                          * Only template flags have meaning in
3149                                          * the layout for a new file
3150                                          */
3151                                         lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3152                                 }
3153                         } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3154                                 if (!strcmp(string, "lcm_mirror_count")) {
3155                                         lsa->lsa_mirror_count = node->cy_valueint;
3156                                 } else if (!strcmp(string, "lcme_extent.e_start")) {
3157                                         if (node->cy_valueint == 0)
3158                                                 lsa->lsa_first_comp = true;
3159                                 } else if (!strcmp(string, "lcme_extent.e_end")) {
3160                                         if (node->cy_valueint == -1)
3161                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3162                                         else
3163                                                 lsa->lsa_comp_end = node->cy_valueint;
3164                                 } else if (!strcmp(string, "stripe_count")) {
3165                                         lsa->lsa_stripe_count = node->cy_valueint;
3166                                 } else if (!strcmp(string, "stripe_size")) {
3167                                         lsa->lsa_stripe_size = node->cy_valueint;
3168                                 } else if (!strcmp(string, "extension_size")) {
3169                                         lsa->lsa_extension_size = node->cy_valueint;
3170                                         lsa->lsa_extension_comp = true;
3171                                 } else if (!strcmp(string, "stripe_offset")) {
3172                                         lsa->lsa_stripe_off = node->cy_valueint;
3173                                 } else if (!strcmp(string, "l_ost_idx")) {
3174                                         osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3175                                         lsa->lsa_nr_tgts++;
3176                                 }
3177                         }
3178                 }
3179                 node = node->cy_next;
3180         }
3181
3182         if (prevp == &prev) {
3183                 rc = build_prev_component(layout, prevp, lsa, true);
3184                 if (rc)
3185                         return rc;
3186
3187                 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3188                         rc = build_component(layout, lsa, *layout != NULL);
3189         }
3190
3191         return rc;
3192 }
3193
3194 static int lfs_comp_create_from_yaml(char *template,
3195                                      struct llapi_layout **layout,
3196                                      struct lfs_setstripe_args *lsa,
3197                                      __u32 *osts)
3198 {
3199         struct cYAML *tree = NULL, *err_rc = NULL;
3200         int rc = 0;
3201
3202         tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3203         if (!tree) {
3204                 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3205                         progname, template);
3206                 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3207                                   "can't parse", &err_rc);
3208                 cYAML_print_tree2file(stderr, err_rc);
3209                 cYAML_free_tree(err_rc);
3210                 rc = -EINVAL;
3211                 goto err;
3212         }
3213
3214         /* initialize lsa for plain file */
3215         setstripe_args_init(lsa);
3216         lsa->lsa_tgts = osts;
3217
3218         rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3219         if (rc) {
3220                 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3221                         progname, template);
3222                 goto err;
3223         }
3224         /* clean clean lsa */
3225         setstripe_args_init(lsa);
3226
3227 err:
3228         if (tree)
3229                 cYAML_free_tree(tree);
3230         return rc;
3231 }
3232
3233 /**
3234  * Get the extension size from the next (SEL) component and extend the
3235  * current component on it. The start of the next component is to be
3236  * adjusted as well.
3237  *
3238  * \param[in] layout    the current layout
3239  * \param[in] start     the start of the current component
3240  * \param[in,out] end   the end of the current component
3241  * \param[in] offset    the offset to adjust the end position to instead of
3242  *                      extension size
3243  *
3244  * \retval 0            - extended successfully
3245  * \retval < 0          - error
3246  */
3247 static int layout_extend_comp(struct llapi_layout *layout,
3248                               uint64_t start, uint64_t *end,
3249                               uint64_t offset)
3250 {
3251         uint64_t size, next_start, next_end;
3252         int rc;
3253
3254         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3255         if (rc < 0) {
3256                 fprintf(stderr,
3257                         "%s setstripe: cannot move component cursor: %s\n",
3258                         progname, strerror(errno));
3259                 return rc;
3260         }
3261
3262         /*
3263          * Even if the @size will not be used below, this will fail if
3264          * this is not a SEL component - a good confirmation we are
3265          * working on right components.
3266          */
3267         rc = llapi_layout_extension_size_get(layout, &size);
3268         if (rc < 0) {
3269                 fprintf(stderr,
3270                         "%s setstripe: cannot get component ext size: %s\n",
3271                         progname, strerror(errno));
3272                 return rc;
3273         }
3274
3275         rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3276         if (rc) {
3277                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3278                         progname, strerror(errno));
3279                 return rc;
3280         }
3281
3282         next_start += offset ?: size;
3283         rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3284         if (rc) {
3285                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3286                         progname, strerror(errno));
3287                 return rc;
3288         }
3289
3290         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3291         if (rc < 0) {
3292                 fprintf(stderr,
3293                         "%s setstripe: cannot move component cursor: %s\n",
3294                         progname, strerror(errno));
3295                 return rc;
3296         }
3297
3298         *end += offset ?: size;
3299         rc = llapi_layout_comp_extent_set(layout, start, *end);
3300         if (rc) {
3301                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3302                         progname, strerror(errno));
3303                 return rc;
3304         }
3305
3306         return 0;
3307 }
3308
3309 /**
3310  * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3311  * end of the last component in the existing file, and adjust the
3312  * first extent start of the components to be added accordingly.
3313  *
3314  * In the create mode, we need to check if the first component is an extendable
3315  * SEL component and extend its length to the extension size (first component
3316  * of the PFL file is initialised at the create time, cannot be 0-lenght.
3317  */
3318 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3319                                       bool comp_add)
3320 {
3321         struct llapi_layout *head;
3322         uint64_t start = 0, prev_end = 0;
3323         uint64_t end;
3324         int rc, ret = 0;
3325
3326         if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3327                 return 0;
3328
3329         errno = 0;
3330         while (comp_add) {
3331                 head = llapi_layout_get_by_path(fname, 0);
3332                 if (!head) {
3333                         fprintf(stderr,
3334                                 "%s setstripe: cannot read layout from '%s': %s\n",
3335                                 progname, fname, strerror(errno));
3336                         return -EINVAL;
3337                 } else if (errno == ENODATA) {
3338                         /*
3339                          * file without LOVEA, this component-add will be turned
3340                          * into a component-create.
3341                          */
3342                         llapi_layout_free(head);
3343                         ret = -ENODATA;
3344
3345                         /*
3346                          * the new layout will be added to an empty one, it
3347                          * still needs to be adjusted below
3348                          */
3349                         comp_add = 0;
3350                         break;
3351                 } else if (!llapi_layout_is_composite(head)) {
3352                         fprintf(stderr,
3353                                 "%s setstripe: '%s' not a composite file\n",
3354                                 progname, fname);
3355                         llapi_layout_free(head);
3356                         return -EINVAL;
3357                 }
3358
3359                 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3360                 if (rc) {
3361                         fprintf(stderr,
3362                                 "%s setstripe: cannot get prev extent: %s\n",
3363                                 progname, strerror(errno));
3364                         llapi_layout_free(head);
3365                         return rc;
3366                 }
3367
3368                 llapi_layout_free(head);
3369                 break;
3370         }
3371
3372         /* Make sure we use the first component of the layout to be added. */
3373         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3374         if (rc < 0) {
3375                 fprintf(stderr,
3376                         "%s setstripe: cannot move component cursor: %s\n",
3377                         progname, strerror(errno));
3378                 return rc;
3379         }
3380
3381         rc = llapi_layout_comp_extent_get(layout, &start, &end);
3382         if (rc) {
3383                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3384                         progname, strerror(errno));
3385                 return rc;
3386         }
3387
3388         if (start == 0 && end == 0) {
3389                 rc = layout_extend_comp(layout, start, &end,
3390                                         comp_add ? prev_end : 0);
3391                 if (rc)
3392                         return rc;
3393         }
3394
3395         if (start > prev_end || end < prev_end) {
3396                 fprintf(stderr,
3397                         "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3398                         progname, start, end, prev_end);
3399                 return -EINVAL;
3400         }
3401
3402         rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3403         if (rc) {
3404                 fprintf(stderr,
3405                         "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3406                         progname, prev_end, end, strerror(errno));
3407                 return rc;
3408         }
3409
3410         return ret;
3411 }
3412
3413 static int mirror_adjust_first_extents(struct mirror_args *list)
3414 {
3415         int rc = 0;
3416
3417         if (!list)
3418                 return 0;
3419
3420         while (list) {
3421                 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3422                 if (rc)
3423                         break;
3424                 list = list->m_next;
3425         }
3426
3427         return rc;
3428 }
3429
3430 static inline bool arg_is_eof(char *arg)
3431 {
3432         return !strncmp(arg, "-1", strlen("-1")) ||
3433                !strncmp(arg, "EOF", strlen("EOF")) ||
3434                !strncmp(arg, "eof", strlen("eof"));
3435 }
3436
3437 /**
3438  * lfs_mirror_alloc() - Allocate a mirror argument structure.
3439  *
3440  * Return: Valid mirror_args pointer on success and
3441  *         NULL if memory allocation fails.
3442  */
3443 static struct mirror_args *lfs_mirror_alloc(void)
3444 {
3445         struct mirror_args *mirror = NULL;
3446
3447         while (1) {
3448                 mirror = calloc(1, sizeof(*mirror));
3449                 if (mirror) {
3450                         mirror->m_inherit = false;
3451                         break;
3452                 }
3453
3454                 sleep(1);
3455         }
3456
3457         return mirror;
3458 }
3459
3460 /**
3461  * lfs_mirror_free() - Free memory allocated for a mirror argument
3462  *                     structure.
3463  * @mirror: Previously allocated mirror argument structure by
3464  *          lfs_mirror_alloc().
3465  *
3466  * Free memory allocated for @mirror.
3467  *
3468  * Return: void.
3469  */
3470 static void lfs_mirror_free(struct mirror_args *mirror)
3471 {
3472         if (mirror->m_layout)
3473                 llapi_layout_free(mirror->m_layout);
3474         free(mirror);
3475 }
3476
3477 /**
3478  * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3479  * @mirror_list: Previously allocated mirror list.
3480  *
3481  * Free memory allocated for @mirror_list.
3482  *
3483  * Return: void.
3484  */
3485 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3486 {
3487         struct mirror_args *next_mirror = NULL;
3488
3489         while (mirror_list) {
3490                 next_mirror = mirror_list->m_next;
3491                 lfs_mirror_free(mirror_list);
3492                 mirror_list = next_mirror;
3493         }
3494 }
3495
3496 enum {
3497         LFS_SETQUOTA_DELETE = (CHAR_MAX + 1),
3498         LFS_POOL_OPT,
3499         LFS_COMP_COUNT_OPT,
3500         LFS_COMP_START_OPT,
3501         LFS_COMP_FLAGS_OPT,
3502         LFS_COMP_DEL_OPT,
3503         LFS_COMP_SET_OPT,
3504         LFS_COMP_ADD_OPT,
3505         LFS_COMP_NO_VERIFY_OPT,
3506         LFS_PROJID_OPT,
3507         LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3508         LFS_MIRROR_ID_OPT,
3509         LFS_MIRROR_STATE_OPT,
3510         LFS_LAYOUT_COPY,
3511         LFS_MIRROR_INDEX_OPT,
3512         LFS_LAYOUT_FOREIGN_OPT,
3513         LFS_MODE_OPT,
3514         LFS_NEWERXY_OPT,
3515         LFS_INHERIT_RR_OPT,
3516         LFS_FIND_PERM,
3517         LFS_PRINTF_OPT,
3518         LFS_NO_FOLLOW_OPT,
3519         LFS_STATS_OPT,
3520         LFS_STATS_INTERVAL_OPT
3521 };
3522
3523 #ifndef LCME_USER_MIRROR_FLAGS
3524 /* The mirror flags can be set by users at creation time. */
3525 #define LCME_USER_MIRROR_FLAGS  (LCME_FL_PREF_RW)
3526 #endif
3527
3528 /* functions */
3529 static int lfs_setstripe_internal(int argc, char **argv,
3530                                   enum setstripe_origin opc)
3531 {
3532         struct lfs_setstripe_args        lsa = { 0 };
3533         struct llapi_stripe_param       *param = NULL;
3534         struct find_param                migrate_mdt_param = {
3535                 .fp_max_depth = -1,
3536                 .fp_mdt_index = -1,
3537         };
3538         char                            *fname;
3539         int                              result = 0;
3540         int                              result2 = 0;
3541         char                            *end;
3542         int                              c;
3543         int                              delete = 0;
3544         unsigned long long               size_units = 1;
3545         bool                             migrate_mode = false;
3546         bool                             migrate_mdt_mode = false;
3547         bool                             setstripe_mode = false;
3548         bool                             migration_block = false;
3549         __u64                            migration_flags = 0;
3550         __u32                            tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3551         int                              comp_del = 0, comp_set = 0;
3552         int                              comp_add = 0;
3553         __u32                            comp_id = 0;
3554         struct llapi_layout             *layout = NULL;
3555         struct llapi_layout             **lpp = &layout;
3556         bool                             mirror_mode = false;
3557         bool                             has_m_file = false;
3558         __u32                            mirror_count = 0;
3559         enum mirror_flags                mirror_flags = 0;
3560         struct mirror_args              *mirror_list = NULL;
3561         struct mirror_args              *new_mirror = NULL;
3562         struct mirror_args              *last_mirror = NULL;
3563         __u16                            mirror_id = 0;
3564         char                             cmd[PATH_MAX];
3565         bool from_yaml = false;
3566         bool from_copy = false;
3567         char *template = NULL;
3568         bool foreign_mode = false;
3569         char *xattr = NULL;
3570         uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3571         char *mode_opt = NULL;
3572         mode_t previous_umask = 0;
3573         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3574         unsigned long long bandwidth_bytes_sec = 0;
3575         unsigned long long bandwidth_unit = ONE_MB;
3576         enum stats_flag stats_flag = STATS_OFF;
3577         long stats_interval_sec = 5;
3578
3579         struct option long_opts[] = {
3580 /* find { .val = '0',   .name = "null",         .has_arg = no_argument }, */
3581 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
3582         /* --block is only valid in migrate mode */
3583         { .val = 'b',   .name = "block",        .has_arg = no_argument },
3584 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
3585         { .val = LFS_COMP_ADD_OPT,
3586                         .name = "comp-add",     .has_arg = no_argument },
3587         { .val = LFS_COMP_ADD_OPT,
3588                         .name = "component-add", .has_arg = no_argument },
3589         { .val = LFS_COMP_DEL_OPT,
3590                         .name = "comp-del",     .has_arg = no_argument },
3591         { .val = LFS_COMP_DEL_OPT,
3592                         .name = "component-del", .has_arg = no_argument },
3593         { .val = LFS_COMP_FLAGS_OPT,
3594                         .name = "comp-flags",   .has_arg = required_argument },
3595         { .val = LFS_COMP_FLAGS_OPT,
3596                         .name = "component-flags",
3597                                                 .has_arg = required_argument },
3598         { .val = LFS_COMP_SET_OPT,
3599                         .name = "comp-set",     .has_arg = no_argument },
3600         { .val = LFS_COMP_SET_OPT,
3601                         .name = "component-set",
3602                                                 .has_arg = no_argument},
3603         { .val = LFS_COMP_NO_VERIFY_OPT,
3604                         .name = "no-verify",    .has_arg = no_argument},
3605         { .val = LFS_LAYOUT_FLAGS_OPT,
3606                         .name = "flags",        .has_arg = required_argument},
3607         { .val = LFS_LAYOUT_FOREIGN_OPT,
3608                         .name = "foreign",      .has_arg = optional_argument},
3609         { .val = LFS_MIRROR_ID_OPT,
3610                         .name = "mirror-id",    .has_arg = required_argument},
3611         { .val = LFS_MODE_OPT,
3612                         .name = "mode",         .has_arg = required_argument},
3613         { .val = LFS_LAYOUT_COPY,
3614                         .name = "copy",         .has_arg = required_argument},
3615         { .val = LFS_STATS_OPT,
3616                         .name = "stats",        .has_arg = no_argument},
3617         { .val = LFS_STATS_INTERVAL_OPT,
3618                         .name = "stats-interval",
3619                                                 .has_arg = required_argument},
3620         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument},
3621         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument},
3622         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument},
3623         { .val = 'C',   .name = "overstripe-count",
3624                                                 .has_arg = required_argument},
3625         { .val = 'd',   .name = "delete",       .has_arg = no_argument},
3626         { .val = 'd',   .name = "destroy",      .has_arg = no_argument},
3627         /* used with "lfs migrate -m" */
3628         { .val = 'd',   .name = "directory",    .has_arg = no_argument},
3629         /* --non-direct is only valid in migrate mode */
3630         { .val = 'D',   .name = "non-direct",   .has_arg = no_argument },
3631         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument},
3632         { .val = 'E',   .name = "component-end",
3633                                                 .has_arg = required_argument},
3634         { .val = 'f',   .name = "file",         .has_arg = required_argument },
3635 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
3636 /* find { .val = 'g',   .name = "gid",          .has_arg = no_argument }, */
3637 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
3638         { .val = 'h',   .name = "help",         .has_arg = no_argument },
3639         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument},
3640         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument},
3641         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument},
3642         { .val = 'I',   .name = "comp-id",      .has_arg = required_argument},
3643         { .val = 'I',   .name = "component-id", .has_arg = required_argument},
3644 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
3645         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
3646         { .val = 'm',   .name = "mdt",          .has_arg = required_argument},
3647         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument},
3648         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument},
3649         /* --non-block is only valid in migrate mode */
3650         { .val = 'n',   .name = "non-block",    .has_arg = no_argument },
3651         { .val = 'N',   .name = "mirror-count", .has_arg = optional_argument},
3652         { .val = 'o',   .name = "ost",          .has_arg = required_argument },
3653 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3654         { .val = 'o',   .name = "ost-list",     .has_arg = required_argument },
3655         { .val = 'o',   .name = "ost_list",     .has_arg = required_argument },
3656 #endif
3657         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
3658 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
3659 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
3660 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
3661         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
3662         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
3663 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
3664 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3665 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
3666 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
3667         /* --verbose is only valid in migrate mode */
3668         { .val = 'v',   .name = "verbose",      .has_arg = no_argument},
3669         { .val = 'W',   .name = "bandwidth",    .has_arg = required_argument },
3670         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
3671 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3672         { .val = 'y',   .name = "yaml",         .has_arg = required_argument },
3673         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument},
3674         { .val = 'z',   .name = "extension-size", .has_arg = required_argument},
3675         { .name = NULL } };
3676
3677         setstripe_args_init(&lsa);
3678
3679         migrate_mode = (opc == SO_MIGRATE);
3680         mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3681         setstripe_mode = (opc == SO_SETSTRIPE);
3682         if (opc == SO_MIRROR_DELETE) {
3683                 delete = 1;
3684                 mirror_flags = MF_DESTROY;
3685         }
3686
3687         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3688         progname = cmd;
3689         while ((c = getopt_long(argc, argv,
3690                                 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:W:y:z:",
3691                                 long_opts, NULL)) >= 0) {
3692                 size_units = 1;
3693                 switch (c) {
3694                 case 0:
3695                         /* Long options. */
3696                         break;
3697                 case LFS_COMP_ADD_OPT:
3698                         comp_add = 1;
3699                         break;
3700                 case LFS_COMP_DEL_OPT:
3701                         comp_del = 1;
3702                         break;
3703                 case LFS_COMP_FLAGS_OPT:
3704                         result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3705                                                 &lsa.lsa_comp_neg_flags);
3706                         if (result != 0)
3707                                 goto usage_error;
3708                         if (mirror_mode && lsa.lsa_comp_neg_flags) {
3709                                 fprintf(stderr,
3710                                         "%s: inverted flags are not supported\n",
3711                                         progname);
3712                                 goto usage_error;
3713                         }
3714                         break;
3715                 case LFS_COMP_SET_OPT:
3716                         comp_set = 1;
3717                         break;
3718                 case LFS_COMP_NO_VERIFY_OPT:
3719                         mirror_flags |= MF_NO_VERIFY;
3720                         break;
3721                 case LFS_MIRROR_ID_OPT: {
3722                         unsigned long int id;
3723
3724                         errno = 0;
3725                         id = strtoul(optarg, &end, 0);
3726                         if (errno != 0 || *end != '\0' || id == 0 ||
3727                             id > UINT16_MAX) {
3728                                 fprintf(stderr,
3729                                         "%s %s: invalid mirror ID '%s'\n",
3730                                         progname, argv[0], optarg);
3731                                 goto usage_error;
3732                         }
3733
3734                         mirror_id = (__u16)id;
3735                         break;
3736                 }
3737                 case LFS_LAYOUT_FLAGS_OPT: {
3738                         uint32_t neg_flags;
3739
3740                         /* check for numeric flags (foreign and mirror cases) */
3741                         if (setstripe_mode && !mirror_mode && !last_mirror) {
3742                                 errno = 0;
3743                                 flags = strtoul(optarg, &end, 16);
3744                                 if (errno != 0 || *end != '\0' ||
3745                                     flags >= UINT32_MAX) {
3746                                         fprintf(stderr,
3747                                                 "%s %s: invalid hex flags '%s'\n",
3748                                                 progname, argv[0], optarg);
3749                                         return CMD_HELP;
3750                                 }
3751                                 if (!foreign_mode) {
3752                                         fprintf(stderr,
3753                                                 "%s %s: hex flags must be specified with --foreign option\n",
3754                                                 progname, argv[0]);
3755                                         return CMD_HELP;
3756                                 }
3757                                 break;
3758                         }
3759
3760                         if (!mirror_mode || !last_mirror) {
3761                                 fprintf(stderr,
3762                                         "error: %s: --flags must be specified with --mirror-count|-N option\n",
3763                                         progname);
3764                                 goto usage_error;
3765                         }
3766
3767                         result = comp_str2flags(optarg, &last_mirror->m_flags,
3768                                                 &neg_flags);
3769                         if (result != 0)
3770                                 goto usage_error;
3771
3772                         if (neg_flags) {
3773                                 fprintf(stderr,
3774                                         "%s: inverted flags are not supported\n",
3775                                         progname);
3776                                 result = -EINVAL;
3777                                 goto usage_error;
3778                         }
3779                         if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3780                                 fprintf(stderr,
3781                                         "%s: unsupported mirror flags: %s\n",
3782                                         progname, optarg);
3783                                 result = -EINVAL;
3784                                 goto error;
3785                         }
3786                         break;
3787                 }
3788                 case LFS_LAYOUT_FOREIGN_OPT:
3789                         if (optarg) {
3790                                 /* check pure numeric */
3791                                 type = strtoul(optarg, &end, 0);
3792                                 if (*end) {
3793                                         /* check name */
3794                                         type = check_foreign_type_name(optarg);
3795                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3796                                                 fprintf(stderr,
3797                                                         "%s %s: unrecognized foreign type '%s'\n",
3798                                                         progname, argv[0],
3799                                                         optarg);
3800                                                 return CMD_HELP;
3801                                         }
3802                                 } else if (type >= UINT32_MAX) {
3803                                         fprintf(stderr,
3804                                                 "%s %s: invalid foreign type '%s'\n",
3805                                                 progname, argv[0], optarg);
3806                                         return CMD_HELP;
3807                                 }
3808                         }
3809                         foreign_mode = true;
3810                         break;
3811                 case LFS_MODE_OPT:
3812                         mode_opt = optarg;
3813                         if (mode_opt) {
3814                                 mode = strtoul(mode_opt, &end, 8);
3815                                 if (*end != '\0') {
3816                                         fprintf(stderr,
3817                                                 "%s %s: bad mode '%s'\n",
3818                                                 progname, argv[0], mode_opt);
3819                                         return CMD_HELP;
3820                                 }
3821                                 previous_umask = umask(0);
3822                         }
3823                         break;
3824                 case LFS_LAYOUT_COPY:
3825                         from_copy = true;
3826                         template = optarg;
3827                         break;
3828                 case LFS_STATS_OPT:
3829                         stats_flag = STATS_ON;
3830                         break;
3831                 case LFS_STATS_INTERVAL_OPT:
3832                         stats_flag = STATS_ON;
3833                         stats_interval_sec = strtol(optarg, &end, 0);
3834                         if (stats_interval_sec == 0)
3835                                 stats_interval_sec = 5;
3836                         break;
3837                 case 'b':
3838                         if (!migrate_mode) {
3839                                 fprintf(stderr,
3840                                         "%s %s: -b|--block valid only for migrate command\n",
3841                                         progname, argv[0]);
3842                                 goto usage_error;
3843                         }
3844                         migration_block = true;
3845                         break;
3846                 case 'C':
3847                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3848                                 fprintf(stderr,
3849                                         "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3850                                         progname, argv[0]);
3851                                 goto usage_error;
3852                         }
3853                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3854                         fallthrough;
3855                 case 'c':
3856                         errno = 0;
3857                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3858                         if (errno != 0 || *end != '\0'|| optarg == end ||
3859                             lsa.lsa_stripe_count < -1 ||
3860                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3861                                 fprintf(stderr,
3862                                         "%s %s: invalid stripe count '%s'\n",
3863                                         progname, argv[0], optarg);
3864                                 goto usage_error;
3865                         }
3866
3867                         if (lsa.lsa_stripe_count == -1)
3868                                 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3869                         break;
3870                 case 'd':
3871                         if (migrate_mode) {
3872                                 migrate_mdt_param.fp_max_depth = 1;
3873                         } else {
3874                                 /* delete the default striping pattern */
3875                                 delete = 1;
3876                                 if (opc == SO_MIRROR_SPLIT) {
3877                                         if (has_m_file) {
3878                                                 fprintf(stderr,
3879                                                       "%s %s: -d cannot used with -f\n",
3880                                                         progname, argv[0]);
3881                                                 goto usage_error;
3882                                         }
3883                                         mirror_flags |= MF_DESTROY;
3884                                 }
3885                         }
3886                         break;
3887                 case 'D':
3888                         if (!migrate_mode) {
3889                                 fprintf(stderr,
3890                                         "%s %s: -D|--non-direct is valid only for migrate command\n",
3891                                         progname, argv[0]);
3892                                 goto usage_error;
3893                         }
3894                         migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3895                         break;
3896                 case 'E':
3897                         if (lsa.lsa_comp_end != 0) {
3898                                 result = comp_args_to_layout(lpp, &lsa, true);
3899                                 if (result) {
3900                                         fprintf(stderr, "%s: invalid layout\n",
3901                                                 progname);
3902                                         goto usage_error;
3903                                 }
3904
3905                                 setstripe_args_init_inherit(&lsa);
3906                         }
3907
3908                         if (arg_is_eof(optarg)) {
3909                                 lsa.lsa_comp_end = LUSTRE_EOF;
3910                         } else {
3911                                 result = llapi_parse_size(optarg,
3912                                                           &lsa.lsa_comp_end,
3913                                                           &size_units, 0);
3914                                 /* assume units of KB if too small */
3915                                 if (lsa.lsa_comp_end < 4096)
3916                                         lsa.lsa_comp_end *= 1024;
3917                                 if (result ||
3918                                     lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3919                                         fprintf(stderr,
3920                                                 "%s %s: invalid component end '%s'\n",
3921                                                 progname, argv[0], optarg);
3922                                         goto usage_error;
3923                                 }
3924                         }
3925                         break;
3926                 case 'H':
3927                         if (!migrate_mode) {
3928                                 fprintf(stderr,
3929                                         "--mdt-hash is valid only for migrate command\n");
3930                                 return CMD_HELP;
3931                         }
3932
3933                         lsa.lsa_pattern = check_hashtype(optarg);
3934                         if (lsa.lsa_pattern == 0) {
3935                                 fprintf(stderr,
3936                                         "%s %s: bad stripe hash type '%s'\n",
3937                                         progname, argv[0], optarg);
3938                                 return CMD_HELP;
3939                         }
3940                         break;
3941                 case 'i':
3942                         errno = 0;
3943                         lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3944                         if (errno != 0 || *end != '\0' || optarg == end ||
3945                             lsa.lsa_stripe_off < -1 ||
3946                             lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3947                                 fprintf(stderr,
3948                                         "%s %s: invalid stripe offset '%s'\n",
3949                                         progname, argv[0], optarg);
3950                                 goto usage_error;
3951                         }
3952                         if (lsa.lsa_stripe_off == -1)
3953                                 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3954                         break;
3955                 case 'I':
3956                         comp_id = strtoul(optarg, &end, 0);
3957                         if (*end != '\0' || comp_id == 0 ||
3958                             comp_id > LCME_ID_MAX) {
3959                                 fprintf(stderr,
3960                                         "%s %s: invalid component ID '%s'\n",
3961                                         progname, argv[0], optarg);
3962                                 goto usage_error;
3963                         }
3964                         break;
3965                 case 'f':
3966                         if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3967                                 fprintf(stderr,
3968                                         "error: %s: invalid option: %s\n",
3969                                         progname, argv[optopt + 1]);
3970                                 goto usage_error;
3971                         }
3972                         if (opc == SO_MIRROR_EXTEND) {
3973                                 if (!last_mirror) {
3974                                         fprintf(stderr,
3975                                 "error: %s: '-N' must exist in front of '%s'\n",
3976                                                 progname, argv[optopt + 1]);
3977                                         goto usage_error;
3978                                 }
3979                                 last_mirror->m_file = optarg;
3980                                 last_mirror->m_count = 1;
3981                         } else {
3982                                 /* mirror split */
3983                                 if (!mirror_list)
3984                                         mirror_list = lfs_mirror_alloc();
3985                                 mirror_list->m_file = optarg;
3986                         }
3987                         has_m_file = true;
3988                         break;
3989                 case 'L':
3990                         if (strcmp(argv[optind - 1], "mdt") == 0) {
3991                                 /* Can be only the first component */
3992                                 if (layout) {
3993                                         result = -EINVAL;
3994                                         fprintf(stderr,
3995                                                 "error: 'mdt' layout can be only the first one\n");
3996                                         goto error;
3997                                 }
3998                                 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3999                                         result = -EFBIG;
4000                                         fprintf(stderr,
4001                                                 "error: 'mdt' layout size is too big\n");
4002                                         goto error;
4003                                 }
4004                                 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
4005                                 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
4006                         } else if (strcmp(argv[optind - 1], "raid0") != 0) {
4007                                 result = -EINVAL;
4008                                 fprintf(stderr,
4009                                         "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
4010                                         argv[optind]);
4011                                 goto error;
4012                         }
4013                         break;
4014                 case 'm':
4015                         if (!migrate_mode) {
4016                                 fprintf(stderr,
4017                                         "%s %s: -m|--mdt-index is valid only for migrate command\n",
4018                                         progname, argv[0]);
4019                                 goto usage_error;
4020                         }
4021                         migrate_mdt_mode = true;
4022                         lsa.lsa_nr_tgts = parse_targets(tgts,
4023                                                 sizeof(tgts) / sizeof(__u32),
4024                                                 lsa.lsa_nr_tgts, optarg, NULL);
4025                         if (lsa.lsa_nr_tgts < 0) {
4026                                 fprintf(stderr,
4027                                         "%s: invalid MDT target(s) '%s'\n",
4028                                         progname, optarg);
4029                                 goto usage_error;
4030                         }
4031
4032                         lsa.lsa_tgts = tgts;
4033                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4034                                 lsa.lsa_stripe_off = tgts[0];
4035                         break;
4036                 case 'n':
4037                         if (!migrate_mode) {
4038                                 fprintf(stderr,
4039                                         "%s %s: -n|--non-block valid only for migrate command\n",
4040                                         progname, argv[0]);
4041                                 goto usage_error;
4042                         }
4043                         migration_flags |= LLAPI_MIGRATION_NONBLOCK;
4044                         break;
4045                 case 'N':
4046                         if (opc == SO_SETSTRIPE) {
4047                                 opc = SO_MIRROR_CREATE;
4048                                 mirror_mode = true;
4049                         }
4050                         mirror_count = 1;
4051                         if (optarg) {
4052                                 errno = 0;
4053                                 mirror_count = strtoul(optarg, &end, 0);
4054                                 if (errno != 0 || *end != '\0' ||
4055                                     mirror_count == 0 ||
4056                                     mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
4057                                         fprintf(stderr,
4058                                                 "error: %s: bad mirror count: %s\n",
4059                                                 progname, optarg);
4060                                         result = -EINVAL;
4061                                         goto error;
4062                                 }
4063                         }
4064
4065                         new_mirror = lfs_mirror_alloc();
4066                         new_mirror->m_count = mirror_count;
4067
4068                         if (!mirror_list)
4069                                 mirror_list = new_mirror;
4070
4071                         if (last_mirror) {
4072                                 /* wrap up last mirror */
4073                                 if (!setstripe_args_specified(&lsa))
4074                                         last_mirror->m_inherit = true;
4075                                 if (lsa.lsa_comp_end == 0)
4076                                         lsa.lsa_comp_end = LUSTRE_EOF;
4077
4078                                 result = comp_args_to_layout(lpp, &lsa, true);
4079                                 if (result) {
4080                                         lfs_mirror_free(new_mirror);
4081                                         goto error;
4082                                 }
4083
4084                                 setstripe_args_init_inherit(&lsa);
4085
4086                                 last_mirror->m_next = new_mirror;
4087                         }
4088
4089                         last_mirror = new_mirror;
4090                         lpp = &last_mirror->m_layout;
4091                         break;
4092                 case 'o':
4093 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4094                         if (strcmp(argv[optind - 1], "--ost-list") == 0)
4095                                 fprintf(stderr,
4096                                         "warning: '--ost-list' is deprecated, use '--ost' instead\n");
4097 #endif
4098                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
4099                                 fprintf(stderr,
4100                                         "%s %s: -o|--ost incompatible with DoM layout\n",
4101                                         progname, argv[0]);
4102                                 goto usage_error;
4103                         }
4104                         /*
4105                          * -o allows overstriping, and must note it because
4106                          * parse_targets is shared with MDT striping, which
4107                          * does not allow duplicates
4108                          */
4109                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
4110                         lsa.lsa_nr_tgts = parse_targets(tgts,
4111                                                 sizeof(tgts) / sizeof(__u32),
4112                                                 lsa.lsa_nr_tgts, optarg,
4113                                                 &lsa.lsa_pattern);
4114                         if (lsa.lsa_nr_tgts < 0) {
4115                                 fprintf(stderr,
4116                                         "%s %s: invalid OST target(s) '%s'\n",
4117                                         progname, argv[0], optarg);
4118                                 goto usage_error;
4119                         }
4120
4121                         lsa.lsa_tgts = tgts;
4122                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4123                                 lsa.lsa_stripe_off = tgts[0];
4124                         break;
4125                 case 'p':
4126                         if (!optarg)
4127                                 goto usage_error;
4128
4129                         if (optarg[0] == '\0' || lov_pool_is_inherited(optarg))
4130                                 lsa.lsa_pool_name = NULL;
4131                         else
4132                                 lsa.lsa_pool_name = optarg;
4133                         break;
4134                 case 'S':
4135                         result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
4136                                                   &size_units, 0);
4137                         /* assume units of KB if too small to be valid */
4138                         if (lsa.lsa_stripe_size < 4096)
4139                                 lsa.lsa_stripe_size *= 1024;
4140                         if (result ||
4141                             lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
4142                                 fprintf(stderr,
4143                                         "%s %s: invalid stripe size '%s'\n",
4144                                         progname, argv[0], optarg);
4145                                 goto usage_error;
4146                         }
4147                         break;
4148                 case 'v':
4149                         if (!migrate_mode) {
4150                                 fprintf(stderr,
4151                                         "%s %s: -v|--verbose valid only for migrate command\n",
4152                                         progname, argv[0]);
4153                                 goto usage_error;
4154                         }
4155                         migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
4156                         migration_flags = LLAPI_MIGRATION_VERBOSE;
4157                         break;
4158                 case 'x':
4159                         xattr = optarg;
4160                         break;
4161                 case 'W':
4162                         if (!migrate_mode) {
4163                                 fprintf(stderr,
4164                                         "--bandwidth is valid only for migrate and mirror mode\n");
4165                                 goto error;
4166                         }
4167                         if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
4168                                              &bandwidth_unit, 0) < 0) {
4169                                 fprintf(stderr,
4170                                         "error: %s: bad value for bandwidth '%s'\n",
4171                                         argv[0], optarg);
4172                                 goto error;
4173                         }
4174                         break;
4175                 case 'y':
4176                         from_yaml = true;
4177                         template = optarg;
4178                         break;
4179                 case 'z':
4180                         result = llapi_parse_size(optarg,
4181                                                   &lsa.lsa_extension_size,
4182                                                   &size_units, 0);
4183                         if (result) {
4184                                 fprintf(stderr,
4185                                         "%s %s: invalid extension size '%s'\n",
4186                                         progname, argv[0], optarg);
4187                                 goto usage_error;
4188                         }
4189
4190                         lsa.lsa_extension_comp = true;
4191                         break;
4192                 default:
4193                         fprintf(stderr, "%s: unrecognized option '%s'\n",
4194                                 progname, argv[optind - 1]);
4195                 case 'h':
4196                         goto usage_error;
4197                 }
4198         }
4199
4200         fname = argv[optind];
4201
4202         if (optind == argc) {
4203                 fprintf(stderr, "%s %s: FILE must be specified\n",
4204                         progname, argv[0]);
4205                 goto usage_error;
4206         }
4207
4208         /* lfs migrate $filename should keep the file's layout by default */
4209         if (migrate_mode && !layout && !from_yaml &&
4210             !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4211                 from_copy = true;
4212
4213         if (xattr && !foreign_mode) {
4214                 /*
4215                  * only print a warning as this is harmless and will be ignored
4216                  */
4217                 fprintf(stderr,
4218                         "%s %s: xattr has been specified for non-foreign layout\n",
4219                         progname, argv[0]);
4220         } else if (foreign_mode && !xattr) {
4221                 fprintf(stderr,
4222                         "%s %s: xattr must be provided in foreign mode\n",
4223                         progname, argv[0]);
4224                 goto usage_error;
4225         }
4226
4227         if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4228             comp_set || comp_id || delete || from_copy ||
4229             setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4230             lsa.lsa_tgts)) {
4231                 fprintf(stderr,
4232                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4233                         progname, argv[0]);
4234                 return CMD_HELP;
4235         }
4236
4237         if (mirror_mode && mirror_count == 0) {
4238                 fprintf(stderr,
4239                         "error: %s: --mirror-count|-N option is required\n",
4240                         progname);
4241                 result = -EINVAL;
4242                 goto error;
4243         }
4244
4245         if (mirror_mode) {
4246                 if (!setstripe_args_specified(&lsa))
4247                         last_mirror->m_inherit = true;
4248                 if (lsa.lsa_comp_end == 0)
4249                         lsa.lsa_comp_end = LUSTRE_EOF;
4250         }
4251
4252         if (lsa.lsa_comp_end != 0) {
4253                 result = comp_args_to_layout(lpp, &lsa, true);
4254                 if (result) {
4255                         fprintf(stderr, "error: %s: invalid layout\n",
4256                                 progname);
4257                         result = -EINVAL;
4258                         goto error;
4259                 }
4260         }
4261
4262         if (mirror_flags & MF_NO_VERIFY) {
4263                 if (opc != SO_MIRROR_EXTEND) {
4264                         fprintf(stderr,
4265                                 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4266                                 progname);
4267                         result = -EINVAL;
4268                         goto error;
4269                 } else if (!has_m_file) {
4270                         fprintf(stderr,
4271                                 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4272                                 progname);
4273                         result = -EINVAL;
4274                         goto error;
4275                 }
4276         }
4277
4278         if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4279                 fprintf(stderr,
4280                         "%s %s: --component-set doesn't have component-id set\n",
4281                         progname, argv[0]);
4282                 goto usage_error;
4283         }
4284
4285         if ((delete + comp_set + comp_del + comp_add) > 1) {
4286                 fprintf(stderr,
4287                         "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4288                         progname, argv[0]);
4289                 goto usage_error;
4290         }
4291
4292         if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4293                        lsa.lsa_comp_flags != 0 || layout != NULL)) {
4294                 fprintf(stderr,
4295                         "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4296                         progname, argv[0]);
4297                 goto usage_error;
4298         }
4299
4300         if ((comp_set || comp_del) &&
4301             (setstripe_args_specified(&lsa) || layout != NULL)) {
4302                 fprintf(stderr,
4303                         "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4304                         progname, argv[0]);
4305                 goto usage_error;
4306         }
4307
4308         if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4309                 fprintf(stderr,
4310                         "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4311                         progname, argv[0]);
4312                 goto usage_error;
4313         }
4314
4315         if (comp_add || comp_del) {
4316                 struct stat st;
4317
4318                 result = lstat(fname, &st);
4319                 if (result == 0 && S_ISDIR(st.st_mode)) {
4320                         fprintf(stderr,
4321                                 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4322                                 progname);
4323                         goto usage_error;
4324                 }
4325
4326                 if (mirror_mode) {
4327                         fprintf(stderr,
4328                                 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4329                                 progname);
4330                         goto usage_error;
4331                 }
4332         }
4333
4334         if (comp_add) {
4335                 if (!layout) {
4336                         fprintf(stderr,
4337                                 "%s %s: option -E must be specified with --component-add\n",
4338                                 progname, argv[0]);
4339                         goto usage_error;
4340                 }
4341         }
4342
4343         if (from_yaml && from_copy) {
4344                 fprintf(stderr,
4345                         "%s: can't specify --yaml and --copy together\n",
4346                         progname);
4347                 goto error;
4348         }
4349
4350         if ((from_yaml || from_copy) &&
4351             (setstripe_args_specified(&lsa) || layout != NULL)) {
4352                 fprintf(stderr,
4353                         "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4354                         argv[0]);
4355                 goto error;
4356         }
4357
4358         if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4359                 fprintf(stderr,
4360                         "%s %s: options --non-block and --block are mutually exclusive\n",
4361                         progname, argv[0]);
4362                 goto usage_error;
4363         }
4364
4365         if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4366             opc != SO_MIRROR_DELETE && comp_id != 0) {
4367                 fprintf(stderr,
4368                         "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4369                         progname);
4370                 goto usage_error;
4371         }
4372
4373         if (migrate_mdt_mode) {
4374                 struct lmv_user_md *lmu;
4375
4376                 /* initialize migrate mdt parameters */
4377                 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4378                                                  LMV_USER_MAGIC_SPECIFIC));
4379                 if (!lmu) {
4380                         fprintf(stderr,
4381                                 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4382                                 progname, argv[0], strerror(ENOMEM));
4383                         result = -ENOMEM;
4384                         goto error;
4385                 }
4386                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4387                         lmu->lum_stripe_count = lsa.lsa_stripe_count;
4388                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4389                         fprintf(stderr,
4390                                 "%s %s: migrate should specify MDT index\n",
4391                                 progname, argv[0]);
4392                         free(lmu);
4393                         goto usage_error;
4394                 }
4395                 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4396                 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4397                         lmu->lum_hash_type = lsa.lsa_pattern;
4398                 else
4399                         lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4400                 if (lsa.lsa_pool_name) {
4401                         strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4402                                 sizeof(lmu->lum_pool_name) - 1);
4403                         lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4404                 }
4405                 if (lsa.lsa_nr_tgts > 1) {
4406                         int i;
4407
4408                         if (lsa.lsa_stripe_count > 0 &&
4409                             lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4410                             lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4411                                 fprintf(stderr,
4412                                         "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4413                                         progname, lsa.lsa_stripe_count,
4414                                         lsa.lsa_nr_tgts);
4415                                 free(lmu);
4416                                 goto usage_error;
4417                         }
4418
4419                         lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4420                         lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4421                         for (i = 0; i < lsa.lsa_nr_tgts; i++)
4422                                 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4423                 } else {
4424                         lmu->lum_magic = LMV_USER_MAGIC;
4425                 }
4426
4427                 migrate_mdt_param.fp_lmv_md = lmu;
4428                 migrate_mdt_param.fp_migrate = 1;
4429         } else if (!layout) {
4430                 if (lsa_args_stripe_count_check(&lsa))
4431                         goto usage_error;
4432
4433                 /* initialize stripe parameters */
4434                 param = calloc(1, offsetof(typeof(*param),
4435                                lsp_osts[lsa.lsa_nr_tgts]));
4436                 if (!param) {
4437                         fprintf(stderr,
4438                                 "%s %s: cannot allocate memory for parameters: %s\n",
4439                                 progname, argv[0], strerror(ENOMEM));
4440                         result = -ENOMEM;
4441                         goto error;
4442                 }
4443
4444                 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4445                         param->lsp_stripe_size = lsa.lsa_stripe_size;
4446                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4447                         if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4448                                 param->lsp_stripe_count = -1;
4449                         else
4450                                 param->lsp_stripe_count = lsa.lsa_stripe_count;
4451                 }
4452                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4453                         param->lsp_stripe_offset = -1;
4454                 else
4455                         param->lsp_stripe_offset = lsa.lsa_stripe_off;
4456                 param->lsp_stripe_pattern =
4457                                 llapi_pattern_to_lov(lsa.lsa_pattern);
4458                 if (param->lsp_stripe_pattern == EINVAL) {
4459                         fprintf(stderr, "error: %s: invalid stripe pattern\n",
4460                                 argv[0]);
4461                         free(param);
4462                         goto usage_error;
4463                 }
4464                 param->lsp_pool = lsa.lsa_pool_name;
4465                 param->lsp_is_specific = false;
4466
4467                 if (lsa.lsa_nr_tgts > 0) {
4468                         param->lsp_is_specific = true;
4469                         param->lsp_stripe_count = lsa.lsa_nr_tgts;
4470                         memcpy(param->lsp_osts, tgts,
4471                                sizeof(*tgts) * lsa.lsa_nr_tgts);
4472                 }
4473         }
4474
4475         if (from_yaml) {
4476                 /* generate a layout from a YAML template */
4477                 result = lfs_comp_create_from_yaml(template, &layout,
4478                                                    &lsa, tgts);
4479                 if (result) {
4480                         fprintf(stderr,
4481                                 "error: %s: can't create composite layout from template file %s\n",
4482                                 argv[0], template);
4483                         goto error;
4484                 }
4485         }
4486
4487         if (layout != NULL || mirror_list != NULL) {
4488                 if (mirror_list)
4489                         result = mirror_adjust_first_extents(mirror_list);
4490                 else
4491                         result = layout_adjust_first_extent(fname, layout,
4492                                                             comp_add);
4493                 if (result == -ENODATA)
4494                         comp_add = 0;
4495                 else if (result != 0) {
4496                         fprintf(stderr, "error: %s: invalid layout\n",
4497                                 progname);
4498                         goto error;
4499                 }
4500         }
4501
4502         for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4503                 if (from_copy) {
4504                         layout = llapi_layout_get_by_path(template ?: fname, 0);
4505                         if (!layout) {
4506                                 fprintf(stderr,
4507                                         "%s: can't create composite layout from file %s: %s\n",
4508                                         progname, template ?: fname,
4509                                         strerror(errno));
4510                                 result = -errno;
4511                                 goto error;
4512                         }
4513                 }
4514
4515                 if (migrate_mdt_mode) {
4516                         result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4517                 } else if (migrate_mode) {
4518                         if (from_copy) {
4519                                 /*
4520                                  * Strip the source layout of specific
4521                                  * OST object/index values.
4522                                  */
4523                                 result = llapi_layout_ost_index_set(layout, 0,
4524                                                 LLAPI_LAYOUT_DEFAULT);
4525                                 if (result) {
4526                                         fprintf(stderr,
4527                                                 "%s: set default ost index failed: %s\n",
4528                                                 progname, strerror(errno));
4529                                         result = -errno;
4530                                         goto error;
4531                                 }
4532                         }
4533
4534                         result = lfs_migrate(fname, migration_flags, param,
4535                                              layout, bandwidth_bytes_sec,
4536                                              stats_flag, stats_interval_sec);
4537                 } else if (comp_set != 0) {
4538                         result = lfs_component_set(fname, comp_id,
4539                                                    lsa.lsa_pool_name,
4540                                                    lsa.lsa_comp_flags,
4541                                                    lsa.lsa_comp_neg_flags);
4542                 } else if (comp_del != 0) {
4543                         result = lfs_component_del(fname, comp_id,
4544                                                    lsa.lsa_comp_flags,
4545                                                    lsa.lsa_comp_neg_flags);
4546                 } else if (comp_add != 0) {
4547                         result = lfs_component_add(fname, layout);
4548                 } else if (opc == SO_MIRROR_CREATE) {
4549                         result = mirror_create(fname, mirror_list);
4550                 } else if (opc == SO_MIRROR_EXTEND) {
4551                         result = mirror_extend(fname, mirror_list,
4552                                                mirror_flags,
4553                                                bandwidth_bytes_sec,
4554                                                stats_flag, stats_interval_sec);
4555                 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4556                         if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4557                                 fprintf(stderr,
4558                                         "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4559                                         progname, fname);
4560                                 goto usage_error;
4561                         }
4562                         if (lsa.lsa_pool_name)
4563                                 mirror_flags |= MF_COMP_POOL;
4564                         else if (mirror_id != 0)
4565                                 comp_id = mirror_id;
4566                         else
4567                                 mirror_flags |= MF_COMP_ID;
4568                         if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4569                                 fprintf(stderr,
4570                                         "%s: the file specified by -f cannot be same as the source file '%s'\n",
4571                                         progname, fname);
4572                                 goto usage_error;
4573                         }
4574                         result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4575                                               mirror_flags,
4576                                               has_m_file ? mirror_list->m_file :
4577                                               NULL);
4578                 } else if (layout) {
4579                         result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4580                                                       mode, layout);
4581                         if (result >= 0) {
4582                                 close(result);
4583                                 result = 0;
4584                         }
4585                 } else if (foreign_mode) {
4586                         result = llapi_file_create_foreign(fname, mode, type,
4587                                                            flags, xattr);
4588                         if (result >= 0) {
4589                                 close(result);
4590                                 result = 0;
4591                         }
4592                 } else {
4593                         result = llapi_file_open_param(fname,
4594                                                        O_CREAT | O_WRONLY,
4595                                                        mode, param);
4596                         if (result >= 0) {
4597                                 close(result);
4598                                 result = 0;
4599                         }
4600                 }
4601                 if (result) {
4602                         /* Save the first error encountered. */
4603                         if (result2 == 0)
4604                                 result2 = result;
4605                         continue;
4606                 }
4607         }
4608
4609         if (mode_opt)
4610                 umask(previous_umask);
4611
4612         free(param);
4613         free(migrate_mdt_param.fp_lmv_md);
4614         llapi_layout_free(layout);
4615         lfs_mirror_list_free(mirror_list);
4616         return result2;
4617 usage_error:
4618         result = CMD_HELP;
4619 error:
4620         llapi_layout_free(layout);
4621         lfs_mirror_list_free(mirror_list);
4622         return result;
4623 }
4624
4625 static int lfs_poollist(int argc, char **argv)
4626 {
4627         if (argc != 2)
4628                 return CMD_HELP;
4629
4630         return llapi_poollist(argv[1]);
4631 }
4632
4633 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4634 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4635                        char *str)
4636 {
4637         long long t = 0;
4638         int sign = 0;
4639         char *endptr = "AD";
4640         char *timebuf;
4641
4642         if (str[0] == '+')
4643                 sign = 1;
4644         else if (str[0] == '-')
4645                 sign = -1;
4646
4647         if (sign)
4648                 str++;
4649
4650         for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4651                 long long val = strtoll(timebuf, &endptr, 0);
4652                 int unit = 1;
4653
4654                 switch (*endptr) {
4655                 case  'y':
4656                         unit *= 52; /* 52 weeks + 1 day below */
4657                         fallthrough;
4658                 case  'w':
4659                         unit *= 7;
4660                         if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4661                                 param->fp_time_margin *= (1 + unit / 52);
4662                         unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4663                         fallthrough;
4664                 case '\0': /* days are default unit if none used */
4665                         fallthrough;
4666                 case  'd':
4667                         unit *= 24;
4668                         fallthrough;
4669                 case  'h':
4670                         unit *= 60;
4671                         fallthrough;
4672                 case  'm':
4673                         unit *= 60;
4674                         fallthrough;
4675                 case  's':
4676                         break;
4677                         /* don't need to multiply by 1 for seconds */
4678                 default:
4679                         fprintf(stderr,
4680                                 "%s find: bad time string '%s': %s\n",
4681                                 progname, timebuf, strerror(EINVAL));
4682                         return LONG_MAX;
4683                 }
4684
4685                 if (param->fp_time_margin == 0 ||
4686                     (*endptr && unit < param->fp_time_margin))
4687                         param->fp_time_margin = unit;
4688
4689                 t += val * unit;
4690         }
4691         if (*time < t) {
4692                 if (sign != 0)
4693                         str--;
4694                 fprintf(stderr, "%s find: bad time '%s': too large\n",
4695                         progname, str);
4696                 return LONG_MAX;
4697         }
4698
4699         *set = *time - t;
4700
4701         return sign;
4702 }
4703
4704 static int str2quotaid(__u32 *id, const char *arg)
4705 {
4706         unsigned long int projid_tmp = 0;
4707         char *endptr = NULL;
4708
4709         projid_tmp = strtoul(arg, &endptr, 10);
4710         if (*endptr != '\0')
4711                 return -EINVAL;
4712         /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4713         if (projid_tmp >= UINT32_MAX)
4714                 return -ERANGE;
4715
4716         *id = projid_tmp;
4717         return 0;
4718 }
4719
4720 static int name2uid(unsigned int *id, const char *name)
4721 {
4722         struct passwd *passwd;
4723
4724         passwd = getpwnam(name);
4725         if (!passwd)
4726                 return -ENOENT;
4727         *id = passwd->pw_uid;
4728
4729         return 0;
4730 }
4731
4732 static int name2gid(unsigned int *id, const char *name)
4733 {
4734         struct group *group;
4735
4736         group = getgrnam(name);
4737         if (!group)
4738                 return -ENOENT;
4739         *id = group->gr_gid;
4740
4741         return 0;
4742 }
4743
4744 static inline int name2projid(unsigned int *id, const char *name)
4745 {
4746         return -ENOTSUP;
4747 }
4748
4749 static int uid2name(char **name, unsigned int id)
4750 {
4751         struct passwd *passwd;
4752
4753         passwd = getpwuid(id);
4754         if (!passwd)
4755                 return -ENOENT;
4756         *name = passwd->pw_name;
4757
4758         return 0;
4759 }
4760
4761 static inline int gid2name(char **name, unsigned int id)
4762 {
4763         struct group *group;
4764
4765         group = getgrgid(id);
4766         if (!group)
4767                 return -ENOENT;
4768         *name = group->gr_name;
4769
4770         return 0;
4771 }
4772
4773 static int name2layout(__u32 *layout, char *name)
4774 {
4775         char *ptr, *layout_name;
4776
4777         *layout = 0;
4778         for (ptr = name; ; ptr = NULL) {
4779                 layout_name = strtok(ptr, ",");
4780                 if (!layout_name)
4781                         break;
4782                 if (strcmp(layout_name, "released") == 0)
4783                         *layout |= LOV_PATTERN_F_RELEASED;
4784                 else if (strcmp(layout_name, "raid0") == 0)
4785                         *layout |= LOV_PATTERN_RAID0;
4786                 else if (strcmp(layout_name, "mdt") == 0)
4787                         *layout |= LOV_PATTERN_MDT;
4788                 else if (strcmp(layout_name, "overstriping") == 0)
4789                         *layout |= LOV_PATTERN_OVERSTRIPING;
4790                 else
4791                         return -1;
4792         }
4793         return 0;
4794 }
4795
4796 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4797 {
4798         int loop;
4799         int user, group, other;
4800         int who, all;
4801         char c, op;
4802         mode_t perm;
4803         mode_t usermask;
4804         mode_t previous_flags;
4805
4806         user = group = other = 0;
4807         all = 0;
4808         loop = 1;
4809         perm = 0;
4810         previous_flags = 0;
4811         *end = input;
4812         usermask = 0;
4813
4814         while (loop) {
4815                 switch (*input) {
4816                 case 'u':
4817                         user = 1;
4818                         break;
4819                 case 'g':
4820                         group = 1;
4821                         break;
4822                 case 'o':
4823                         other = 1;
4824                         break;
4825                 case 'a':
4826                         user = group = other = 1;
4827                         all = 1;
4828                         break;
4829                 default:
4830                         loop = 0;
4831                 }
4832
4833                 if (loop)
4834                         input++;
4835         }
4836
4837         who = user || group || other;
4838         if (!who) {
4839                 /* get the umask */
4840                 usermask = umask(0022);
4841                 umask(usermask);
4842                 usermask &= 07777;
4843         }
4844
4845         if (*input == '-' || *input == '+' || *input == '=')
4846                 op = *input++;
4847         else
4848                 /* operation is required */
4849                 return -1;
4850
4851         /* get the flags in *outmode */
4852         switch (*input) {
4853         case 'u':
4854                 previous_flags = (*outmode & 0700);
4855                 perm |= user  ? previous_flags : 0;
4856                 perm |= group ? (previous_flags >> 3) : 0;
4857                 perm |= other ? (previous_flags >> 6) : 0;
4858                 input++;
4859                 goto write_perm;
4860         case 'g':
4861                 previous_flags = (*outmode & 0070);
4862                 perm |= user  ? (previous_flags << 3) : 0;
4863                 perm |= group ? previous_flags : 0;
4864                 perm |= other ? (previous_flags >> 3) : 0;
4865                 input++;
4866                 goto write_perm;
4867         case 'o':
4868                 previous_flags = (*outmode & 0007);
4869                 perm |= user  ? (previous_flags << 6) : 0;
4870                 perm |= group ? (previous_flags << 3) : 0;
4871                 perm |= other ? previous_flags : 0;
4872                 input++;
4873                 goto write_perm;
4874         default:
4875                 break;
4876         }
4877
4878         /* this part is optional,
4879          * if empty perm = 0 and *outmode is not modified
4880          */
4881         loop = 1;
4882         while (loop) {
4883                 c = *input;
4884                 switch (c) {
4885                 case 'r':
4886                         perm |= user  ? 0400 : 0;
4887                         perm |= group ? 0040 : 0;
4888                         perm |= other ? 0004 : 0;
4889                         /* set read permission for uog except for umask's
4890                          * permissions
4891                          */
4892                         perm |= who   ? 0 : (0444 & ~usermask);
4893                         break;
4894                 case 'w':
4895                         perm |= user  ? 0200 : 0;
4896                         perm |= group ? 0020 : 0;
4897                         perm |= other ? 0002 : 0;
4898                         /* set write permission for uog except for umask'
4899                          * permissions
4900                          */
4901                         perm |= who   ? 0 : (0222 & ~usermask);
4902                         break;
4903                 case 'x':
4904                         perm |= user  ? 0100 : 0;
4905                         perm |= group ? 0010 : 0;
4906                         perm |= other ? 0001 : 0;
4907                         /* set execute permission for uog except for umask'
4908                          * permissions
4909                          */
4910                         perm |= who   ? 0 : (0111 & ~usermask);
4911                         break;
4912                 case 'X':
4913                         /*
4914                          * Adds execute permission to 'u', 'g' and/or 'g' if
4915                          * specified and either 'u', 'g' or 'o' already has
4916                          * execute permissions.
4917                          */
4918                         if ((*outmode & 0111) != 0) {
4919                                 perm |= user  ? 0100 : 0;
4920                                 perm |= group ? 0010 : 0;
4921                                 perm |= other ? 0001 : 0;
4922                                 perm |= !who  ? 0111 : 0;
4923                         }
4924                         break;
4925                 case 's':
4926                         /* s is ignored if o is given, but it's not an error */
4927                         if (other && !group && !user)
4928                                 break;
4929                         perm |= user  ? S_ISUID : 0;
4930                         perm |= group ? S_ISGID : 0;
4931                         break;
4932                 case 't':
4933                         /* 't' should be used when 'a' is given
4934                          * or who is empty
4935                          */
4936                         perm |= (!who || all) ? S_ISVTX : 0;
4937                         /* using ugo with t is not an error */
4938                         break;
4939                 default:
4940                         loop = 0;
4941                         break;
4942                 }
4943                 if (loop)
4944                         input++;
4945         }
4946
4947 write_perm:
4948         /* uog flags should be only one character long */
4949         if (previous_flags && (*input != '\0' && *input != ','))
4950                 return -1;
4951
4952         switch (op) {
4953         case '-':
4954                 /* remove the flags from outmode */
4955                 *outmode &= ~perm;
4956                 break;
4957         case '+':
4958                 /* add the flags to outmode */
4959                 *outmode |= perm;
4960                 break;
4961         case '=':
4962                 /* set the flags of outmode to perm */
4963                 if (perm != 0)
4964                         *outmode = perm;
4965                 break;
4966         }
4967
4968         *end = input;
4969         return 0;
4970 }
4971
4972 static int str2mode_t(const char *input, mode_t *outmode)
4973 {
4974         int ret;
4975         const char *iter;
4976
4977         ret = 0;
4978
4979         if (*input >= '0' && *input <= '7') {
4980                 /* parse octal representation */
4981                 char *end;
4982
4983                 iter = input;
4984
4985                 /* look for invalid digits in octal representation */
4986                 while (isdigit(*iter))
4987                         if (*iter++ > '7')
4988                                 return -1;
4989
4990                 errno = 0;
4991                 *outmode = strtoul(input, &end, 8);
4992
4993                 if (errno != 0 || *outmode > 07777) {
4994                         *outmode = 0;
4995                         ret = -1;
4996                 }
4997
4998         } else if (*input == '8' || *input == '9') {
4999                 /* error: invalid octal number */
5000                 ret = -1;
5001         } else {
5002                 /* parse coma seperated list of symbolic representation */
5003                 int rc;
5004                 const char *end;
5005
5006                 *outmode = 0;
5007                 rc = 0;
5008                 end = NULL;
5009
5010                 do {
5011                         rc = parse_symbolic(input, outmode, &end);
5012                         if (rc)
5013                                 return -1;
5014
5015                         input = end+1;
5016                 } while (*end == ',');
5017
5018                 if (*end != '\0')
5019                         ret = -1;
5020         }
5021         return ret;
5022 }
5023
5024 static int lfs_find(int argc, char **argv)
5025 {
5026         int c, rc;
5027         int ret = 0;
5028         time_t t;
5029         struct find_param param = {
5030                 .fp_max_depth = -1,
5031                 .fp_quiet = 1,
5032                 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
5033         };
5034         struct option long_opts[] = {
5035         { .val = 'A',   .name = "atime",        .has_arg = required_argument },
5036         { .val = 'b',   .name = "blocks",       .has_arg = required_argument },
5037         { .val = 'B',   .name = "btime",        .has_arg = required_argument },
5038         { .val = 'B',   .name = "Btime",        .has_arg = required_argument },
5039         { .val = LFS_COMP_COUNT_OPT,
5040                         .name = "comp-count",   .has_arg = required_argument },
5041         { .val = LFS_COMP_COUNT_OPT,
5042                         .name = "component-count",
5043                                                 .has_arg = required_argument },
5044         { .val = LFS_COMP_FLAGS_OPT,
5045                         .name = "comp-flags",   .has_arg = required_argument },
5046         { .val = LFS_COMP_FLAGS_OPT,
5047                         .name = "component-flags",
5048                                                 .has_arg = required_argument },
5049         { .val = LFS_COMP_START_OPT,
5050                         .name = "comp-start",   .has_arg = required_argument },
5051         { .val = LFS_COMP_START_OPT,
5052                         .name = "component-start",
5053                                                 .has_arg = required_argument },
5054         { .val = LFS_MIRROR_STATE_OPT,
5055                         .name = "mirror-state", .has_arg = required_argument },
5056         { .val = LFS_NEWERXY_OPT,
5057                         .name = "newer",        .has_arg = required_argument},
5058         { .val = LFS_NEWERXY_OPT,
5059                         .name = "neweraa",      .has_arg = required_argument},
5060         { .val = LFS_NEWERXY_OPT,
5061                         .name = "neweram",      .has_arg = required_argument},
5062         { .val = LFS_NEWERXY_OPT,
5063                         .name = "newerac",      .has_arg = required_argument},
5064         { .val = LFS_NEWERXY_OPT,
5065                         .name = "newerab",      .has_arg = required_argument},
5066         { .val = LFS_NEWERXY_OPT,
5067                         .name = "newerma",      .has_arg = required_argument},
5068         { .val = LFS_NEWERXY_OPT,
5069                         .name = "newermm",      .has_arg = required_argument},
5070         { .val = LFS_NEWERXY_OPT,
5071                         .name = "newermc",      .has_arg = required_argument},
5072         { .val = LFS_NEWERXY_OPT,
5073                         .name = "newermb",      .has_arg = required_argument},
5074         { .val = LFS_NEWERXY_OPT,
5075                         .name = "newerca",      .has_arg = required_argument},
5076         { .val = LFS_NEWERXY_OPT,
5077                         .name = "newercm",      .has_arg = required_argument},
5078         { .val = LFS_NEWERXY_OPT,
5079                         .name = "newercc",      .has_arg = required_argument},
5080         { .val = LFS_NEWERXY_OPT,
5081                         .name = "newercb",      .has_arg = required_argument},
5082         { .val = LFS_NEWERXY_OPT,
5083                         .name = "newerba",      .has_arg = required_argument},
5084         { .val = LFS_NEWERXY_OPT,
5085                         .name = "newerbm",      .has_arg = required_argument},
5086         { .val = LFS_NEWERXY_OPT,
5087                         .name = "newerbc",      .has_arg = required_argument},
5088         { .val = LFS_NEWERXY_OPT,
5089                         .name = "newerbb",      .has_arg = required_argument},
5090         { .val = LFS_NEWERXY_OPT,
5091                         .name = "newerBa",      .has_arg = required_argument},
5092         { .val = LFS_NEWERXY_OPT,
5093                         .name = "newerBm",      .has_arg = required_argument},
5094         { .val = LFS_NEWERXY_OPT,
5095                         .name = "newerBc",      .has_arg = required_argument},
5096         { .val = LFS_NEWERXY_OPT,
5097                         .name = "newerBB",      .has_arg = required_argument},
5098         { .val = LFS_NEWERXY_OPT,
5099                         .name = "newerat",      .has_arg = required_argument},
5100         { .val = LFS_NEWERXY_OPT,
5101                         .name = "newermt",      .has_arg = required_argument},
5102         { .val = LFS_NEWERXY_OPT,
5103                         .name = "newerct",      .has_arg = required_argument},
5104         { .val = LFS_NEWERXY_OPT,
5105                         .name = "newerbt",      .has_arg = required_argument},
5106         { .val = LFS_NEWERXY_OPT,
5107                         .name = "newerBt",      .has_arg = required_argument},
5108         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument },
5109         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument },
5110         { .val = 'C',   .name = "ctime",        .has_arg = required_argument },
5111 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
5112         { .val = 'D',   .name = "maxdepth",     .has_arg = required_argument },
5113         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument },
5114         { .val = 'E',   .name = "component-end",
5115                                                 .has_arg = required_argument },
5116 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
5117         { .val = LFS_LAYOUT_FOREIGN_OPT,
5118                         .name = "foreign",      .has_arg = optional_argument},
5119         { .val = 'g',   .name = "gid",          .has_arg = required_argument },
5120         { .val = 'G',   .name = "group",        .has_arg = required_argument },
5121         { .val = 'h',   .name = "help",         .has_arg = no_argument },
5122         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
5123         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument },
5124         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
5125 /* getstripe { .val = 'I', .name = "comp-id",   .has_arg = required_argument }*/
5126         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
5127         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
5128         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
5129         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
5130         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument },
5131         { .val = 'M',   .name = "mtime",        .has_arg = required_argument },
5132         { .val = 'n',   .name = "name",         .has_arg = required_argument },
5133         { .val = 'N',   .name = "mirror-count", .has_arg = required_argument },
5134 /* find { .val = 'o'    .name = "or", .has_arg = no_argument }, like find(1) */
5135         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
5136         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
5137         { .val = LFS_FIND_PERM,
5138                         .name = "perm",         .has_arg = required_argument },
5139         /* no short option for pool yet, can be 'p' after 2.18 */
5140         { .val = LFS_POOL_OPT,
5141                         .name = "pool",         .has_arg = required_argument },
5142         { .val = '0',   .name = "print0",       .has_arg = no_argument },
5143         { .val = 'P',   .name = "print",        .has_arg = no_argument },
5144         { .val = LFS_PRINTF_OPT,
5145                         .name = "printf",       .has_arg = required_argument },
5146         { .val = LFS_PROJID_OPT,
5147                         .name = "projid",       .has_arg = required_argument },
5148 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
5149 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
5150 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
5151         { .val = 's',   .name = "size",         .has_arg = required_argument },
5152         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
5153         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
5154         { .val = 't',   .name = "type",         .has_arg = required_argument },
5155         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
5156         { .val = 'u',   .name = "uid",          .has_arg = required_argument },
5157         { .val = 'U',   .name = "user",         .has_arg = required_argument },
5158 /* getstripe { .val = 'v', .name = "verbose",   .has_arg = no_argument }, */
5159 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
5160         { .val = 'z',   .name = "extension-size",
5161                                                 .has_arg = required_argument },
5162         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument },
5163         { .name = NULL } };
5164         int optidx = 0;
5165         int pathstart = -1;
5166         int pathend = -1;
5167         int pathbad = -1;
5168         int neg_opt = 0;
5169         time_t *xtime;
5170         int *xsign;
5171         int isoption;
5172         char *endptr;
5173
5174         time(&t);
5175
5176         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
5177         while ((c = getopt_long_only(argc, argv,
5178                 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
5179                 long_opts, &optidx)) >= 0) {
5180                 xtime = NULL;
5181                 xsign = NULL;
5182                 if (neg_opt)
5183                         --neg_opt;
5184                 /* '!' is part of option */
5185                 /*
5186                  * when getopt_long_only() finds a string which is not
5187                  * an option nor a known option argument it returns 1
5188                  * in that case if we already have found pathstart and pathend
5189                  * (i.e. we have the list of pathnames),
5190                  * the only supported value is "!"
5191                  */
5192                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5193                 if (!isoption && pathend != -1) {
5194                         fprintf(stderr,
5195                                 "err: %s: filename|dirname must either precede options or follow options\n",
5196                                 argv[0]);
5197                         ret = CMD_HELP;
5198                         goto err;
5199                 }
5200                 if (!isoption && pathstart == -1)
5201                         pathstart = optind - 1;
5202                 if (isoption && pathstart != -1 && pathend == -1)
5203                         pathend = optind - 2;
5204                 switch (c) {
5205                 case 0:
5206                         /* Long options. */
5207                         break;
5208                 case 1:
5209                         /*
5210                          * unknown; opt is "!" or path component,
5211                          * checking done above.
5212                          */
5213                         if (strcmp(optarg, "!") == 0)
5214                                 neg_opt = 2;
5215                         break;
5216                 case 'A':
5217                         xtime = &param.fp_atime;
5218                         xsign = &param.fp_asign;
5219                         param.fp_exclude_atime = !!neg_opt;
5220                         /* no break, this falls through to 'B' for btime */
5221                         fallthrough;
5222                 case 'B':
5223                         if (c == 'B') {
5224                                 xtime = &param.fp_btime;
5225                                 xsign = &param.fp_bsign;
5226                                 param.fp_exclude_btime = !!neg_opt;
5227                         }
5228                         /* no break, this falls through to 'C' for ctime */
5229                         fallthrough;
5230                 case 'C':
5231                         if (c == 'C') {
5232                                 xtime = &param.fp_ctime;
5233                                 xsign = &param.fp_csign;
5234                                 param.fp_exclude_ctime = !!neg_opt;
5235                         }
5236                         /* no break, this falls through to 'M' for mtime */
5237                         fallthrough;
5238                 case 'M':
5239                         if (c == 'M') {
5240                                 xtime = &param.fp_mtime;
5241                                 xsign = &param.fp_msign;
5242                                 param.fp_exclude_mtime = !!neg_opt;
5243                         }
5244                         rc = set_time(&param, &t, xtime, optarg);
5245                         if (rc == LONG_MAX) {
5246                                 ret = -1;
5247                                 goto err;
5248                         }
5249                         if (rc)
5250                                 *xsign = rc;
5251                         break;
5252                 case 'b':
5253                         if (optarg[0] == '+') {
5254                                 param.fp_blocks_sign = -1;
5255                                 optarg++;
5256                         } else if (optarg[0] == '-') {
5257                                 param.fp_blocks_sign =  1;
5258                                 optarg++;
5259                         }
5260
5261                         param.fp_blocks_units = 1024;
5262                         ret = llapi_parse_size(optarg, &param.fp_blocks,
5263                                                &param.fp_blocks_units, 0);
5264                         if (ret) {
5265                                 fprintf(stderr, "error: bad blocks '%s'\n",
5266                                         optarg);
5267                                 goto err;
5268                         }
5269                         param.fp_check_blocks = 1;
5270                         param.fp_exclude_blocks = !!neg_opt;
5271                         break;
5272                 case LFS_COMP_COUNT_OPT:
5273                         if (optarg[0] == '+') {
5274                                 param.fp_comp_count_sign = -1;
5275                                 optarg++;
5276                         } else if (optarg[0] == '-') {
5277                                 param.fp_comp_count_sign =  1;
5278                                 optarg++;
5279                         }
5280
5281                         errno = 0;
5282                         param.fp_comp_count = strtoul(optarg, &endptr, 0);
5283                         if (errno != 0 || *endptr != '\0' ||
5284                             param.fp_comp_count > UINT32_MAX) {
5285                                 fprintf(stderr,
5286                                         "error: bad component count '%s'\n",
5287                                         optarg);
5288                                 goto err;
5289                         }
5290                         param.fp_check_comp_count = 1;
5291                         param.fp_exclude_comp_count = !!neg_opt;
5292                         break;
5293                 case LFS_COMP_FLAGS_OPT:
5294                         rc = comp_str2flags(optarg, &param.fp_comp_flags,
5295                                             &param.fp_comp_neg_flags);
5296                         if (rc) {
5297                                 fprintf(stderr,
5298                                         "error: bad component flags '%s'\n",
5299                                         optarg);
5300                                 goto err;
5301                         }
5302                         param.fp_check_comp_flags = 1;
5303                         if (neg_opt) {
5304                                 __u32 flags = param.fp_comp_neg_flags;
5305
5306                                 param.fp_comp_neg_flags = param.fp_comp_flags;
5307                                 param.fp_comp_flags = flags;
5308                         }
5309                         break;
5310                 case LFS_COMP_START_OPT:
5311                         if (optarg[0] == '+') {
5312                                 param.fp_comp_start_sign = -1;
5313                                 optarg++;
5314                         } else if (optarg[0] == '-') {
5315                                 param.fp_comp_start_sign =  1;
5316                                 optarg++;
5317                         }
5318
5319                         rc = llapi_parse_size(optarg, &param.fp_comp_start,
5320                                               &param.fp_comp_start_units, 0);
5321                         if (rc) {
5322                                 fprintf(stderr,
5323                                         "error: bad component start '%s'\n",
5324                                         optarg);
5325                                 goto err;
5326                         }
5327                         param.fp_check_comp_start = 1;
5328                         param.fp_exclude_comp_start = !!neg_opt;
5329                         break;
5330                 case LFS_MIRROR_STATE_OPT:
5331                         rc = mirror_str2state(optarg, &param.fp_mirror_state,
5332                                               &param.fp_mirror_neg_state);
5333                         if (rc) {
5334                                 fprintf(stderr,
5335                                         "error: bad mirrored file state '%s'\n",
5336                                         optarg);
5337                                 goto err;
5338                         }
5339                         param.fp_check_mirror_state = 1;
5340                         if (neg_opt) {
5341                                 __u16 state = param.fp_mirror_neg_state;
5342
5343                                 param.fp_mirror_neg_state =
5344                                         param.fp_mirror_state;
5345                                 param.fp_mirror_state = state;
5346                         }
5347                         break;
5348                 case 'c':
5349                         if (optarg[0] == '+') {
5350                                 param.fp_stripe_count_sign = -1;
5351                                 optarg++;
5352                         } else if (optarg[0] == '-') {
5353                                 param.fp_stripe_count_sign =  1;
5354                                 optarg++;
5355                         }
5356
5357                         errno = 0;
5358                         param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5359                         if (errno != 0 || *endptr != '\0' ||
5360                             param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5361                                 fprintf(stderr,
5362                                         "error: bad stripe_count '%s'\n",
5363                                         optarg);
5364                                 ret = -1;
5365                                 goto err;
5366                         }
5367                         param.fp_check_stripe_count = 1;
5368                         param.fp_exclude_stripe_count = !!neg_opt;
5369                         break;
5370                 case 'D':
5371                         errno = 0;
5372                         param.fp_max_depth = strtol(optarg, 0, 0);
5373                         if (errno != 0 || param.fp_max_depth < 0) {
5374                                 fprintf(stderr,
5375                                         "error: bad maxdepth '%s'\n",
5376                                         optarg);
5377                                 ret = -1;
5378                                 goto err;
5379                         }
5380                         break;
5381                 case 'E':
5382                         if (optarg[0] == '+') {
5383                                 param.fp_comp_end_sign = -1;
5384                                 optarg++;
5385                         } else if (optarg[0] == '-') {
5386                                 param.fp_comp_end_sign =  1;
5387                                 optarg++;
5388                         }
5389
5390                         if (arg_is_eof(optarg)) {
5391                                 param.fp_comp_end = LUSTRE_EOF;
5392                                 param.fp_comp_end_units = 1;
5393                                 rc = 0;
5394                         } else {
5395                                 rc = llapi_parse_size(optarg,
5396                                                 &param.fp_comp_end,
5397                                                 &param.fp_comp_end_units, 0);
5398                                 /* assume units of KB if too small */
5399                                 if (param.fp_comp_end < 4096)
5400                                         param.fp_comp_end *= 1024;
5401                         }
5402                         if (rc) {
5403                                 fprintf(stderr,
5404                                         "error: bad component end '%s'\n",
5405                                         optarg);
5406                                 goto err;
5407                         }
5408                         param.fp_check_comp_end = 1;
5409                         param.fp_exclude_comp_end = !!neg_opt;
5410                         break;
5411                 case LFS_LAYOUT_FOREIGN_OPT: {
5412                         /* all types by default */
5413                         uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5414
5415                         if (optarg) {
5416                                 /* check pure numeric */
5417                                 type = strtoul(optarg, &endptr, 0);
5418                                 if (*endptr) {
5419                                         /* check name */
5420                                         type = check_foreign_type_name(optarg);
5421                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5422                                                 fprintf(stderr,
5423                                                         "%s %s: unknown foreign type '%s'\n",
5424                                                         progname, argv[0],
5425                                                         optarg);
5426                                                 return CMD_HELP;
5427                                         }
5428                                 } else if (type >= UINT32_MAX) {
5429                                         fprintf(stderr,
5430                                                 "%s %s: invalid foreign type '%s'\n",
5431                                                 progname, argv[0], optarg);
5432                                         return CMD_HELP;
5433                                 }
5434                         }
5435                         param.fp_foreign_type = type;
5436                         param.fp_check_foreign = 1;
5437                         param.fp_exclude_foreign = !!neg_opt;
5438                         break;
5439                 }
5440                 case LFS_NEWERXY_OPT: {
5441                         char x = 'm';
5442                         char y = 'm';
5443                         int xidx;
5444                         int negidx;
5445                         time_t *newery;
5446                         time_t ref = time(NULL);
5447
5448                         /* no need to check bad options, they won't get here */
5449                         if (strlen(long_opts[optidx].name) == 7) {
5450                                 x = long_opts[optidx].name[5];
5451                                 y = long_opts[optidx].name[6];
5452                         }
5453
5454                         if (y == 't') {
5455                                 static const char *const fmts[] = {
5456                                         "%Y-%m-%d %H:%M:%S",
5457                                         "%Y-%m-%d %H:%M",
5458                                         "%Y-%m-%d",
5459                                         "%H:%M:%S", /* sometime today */
5460                                         "%H:%M",
5461                                         "@%s",
5462                                         "%s",
5463                                         NULL };
5464                                 struct tm tm;
5465                                 bool found = false;
5466                                 int i;
5467
5468                                 for (i = 0; fmts[i] != NULL; i++) {
5469                                         char *ptr;
5470
5471                                         /* Init for times relative to today */
5472                                         if (strncmp(fmts[i], "%H", 2) == 0) {
5473                                                 localtime_r(&ref, &tm);
5474                                         } else {
5475                                                 memset(&tm, 0, sizeof(tm));
5476                                                 tm.tm_isdst = -1;
5477                                         }
5478                                         ptr = strptime(optarg, fmts[i], &tm);
5479                                         /* Skip spaces */
5480                                         while (ptr && isspace(*ptr))
5481                                                 ptr++;
5482                                         if (ptr == optarg + strlen(optarg)) {
5483                                                 found = true;
5484                                                 break;
5485                                         }
5486                                 }
5487
5488                                 if (!found) {
5489                                         fprintf(stderr,
5490                                                 "%s: invalid time '%s'\n",
5491                                                 progname, optarg);
5492                                         fprintf(stderr,
5493                                                 "supported formats are:\n  ");
5494                                         for (i = 0; fmts[i] != NULL; i++)
5495                                                 fprintf(stderr, "'%s', ",
5496                                                         fmts[i]);
5497                                         fprintf(stderr, "\n");
5498                                         ret = -EINVAL;
5499                                         goto err;
5500                                 }
5501
5502                                 ref = mktime(&tm);
5503                         } else if (y == 'b' || y == 'B') {
5504                                 lstatx_t stx;
5505
5506                                 rc = llapi_get_lum_file(optarg, NULL, &stx,
5507                                                         NULL, 0);
5508                                 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5509                                         if (!(stx.stx_mask & STATX_BTIME))
5510                                                 ret = -EOPNOTSUPP;
5511                                         else
5512                                                 ret = -errno;
5513                                         fprintf(stderr,
5514                                                 "%s: get btime failed '%s': %s\n",
5515                                                 progname, optarg,
5516                                                 strerror(-ret));
5517                                         goto err;
5518                                 }
5519
5520                                 ref = stx.stx_btime.tv_sec;
5521                         } else {
5522                                 struct stat statbuf;
5523
5524                                 if (stat(optarg, &statbuf) < 0) {
5525                                         fprintf(stderr,
5526                                                 "%s: cannot stat file '%s': %s\n",
5527                                                 progname, optarg,
5528                                                 strerror(errno));
5529                                         ret = -errno;
5530                                         goto err;
5531                                 }
5532
5533                                 switch (y) {
5534                                 case 'a':
5535                                         ref = statbuf.st_atime;
5536                                         break;
5537                                 case 'm':
5538                                         ref = statbuf.st_mtime;
5539                                         break;
5540                                 case 'c':
5541                                         ref = statbuf.st_ctime;
5542                                         break;
5543                                 default:
5544                                         fprintf(stderr,
5545                                                 "%s: invalid Y argument: '%c'\n",
5546                                                 progname, x);
5547                                         ret = -EINVAL;
5548                                         goto err;
5549                                 }
5550                         }
5551
5552                         switch (x) {
5553                         case 'a':
5554                                 xidx = NEWERXY_ATIME;
5555                                 break;
5556                         case 'm':
5557                                 xidx = NEWERXY_MTIME;
5558                                 break;
5559                         case 'c':
5560                                 xidx = NEWERXY_CTIME;
5561                                 break;
5562                         case 'b':
5563                         case 'B':
5564                                 xidx = NEWERXY_BTIME;
5565                                 break;
5566                         default:
5567                                 fprintf(stderr,
5568                                         "%s: invalid X argument: '%c'\n",
5569                                         progname, x);
5570                                 ret = -EINVAL;
5571                                 goto err;
5572                         }
5573
5574                         negidx = !!neg_opt;
5575                         newery = &param.fp_newery[xidx][negidx];
5576
5577                         if (*newery == 0) {
5578                                 *newery = ref;
5579                         } else {
5580                                 if (negidx)
5581                                         *newery = *newery > ref ? ref : *newery;
5582                                 else
5583                                         *newery = *newery > ref ? *newery : ref;
5584                         }
5585                         param.fp_newerxy = 1;
5586                         break;
5587                 }
5588                 case 'g':
5589                 case 'G':
5590                         rc = name2gid(&param.fp_gid, optarg);
5591                         if (rc) {
5592                                 if (str2quotaid(&param.fp_gid, optarg)) {
5593                                         fprintf(stderr,
5594                                                 "Group/GID: %s cannot be found.\n",
5595                                                 optarg);
5596                                         ret = -1;
5597                                         goto err;
5598                                 }
5599                         }
5600                         param.fp_exclude_gid = !!neg_opt;
5601                         param.fp_check_gid = 1;
5602                         break;
5603                 case 'H':
5604                         rc = mdthash_input(optarg, &param.fp_hash_inflags,
5605                                            &param.fp_hash_exflags,
5606                                            &param.fp_hash_type);
5607                         if (rc) {
5608                                 ret = -1;
5609                                 goto err;
5610                         }
5611                         if (param.fp_hash_inflags || param.fp_hash_exflags)
5612                                 param.fp_check_hash_flag = 1;
5613                         param.fp_exclude_hash_type = !!neg_opt;
5614                         break;
5615                 case 'l':
5616                         param.fp_lazy = 1;
5617                         break;
5618                 case 'L':
5619                         ret = name2layout(&param.fp_layout, optarg);
5620                         if (ret)
5621                                 goto err;
5622                         param.fp_exclude_layout = !!neg_opt;
5623                         param.fp_check_layout = 1;
5624                         break;
5625                 case 'u':
5626                 case 'U':
5627                         rc = name2uid(&param.fp_uid, optarg);
5628                         if (rc) {
5629                                 if (str2quotaid(&param.fp_uid, optarg)) {
5630                                         fprintf(stderr,
5631                                                 "User/UID: %s cannot be found.\n",
5632                                                 optarg);
5633                                         ret = -1;
5634                                         goto err;
5635                                 }
5636                         }
5637                         param.fp_exclude_uid = !!neg_opt;
5638                         param.fp_check_uid = 1;
5639                         break;
5640                 case 'n':
5641                         param.fp_pattern = (char *)optarg;
5642                         param.fp_exclude_pattern = !!neg_opt;
5643                         break;
5644                 case 'N':
5645                         if (optarg[0] == '+') {
5646                                 param.fp_mirror_count_sign = -1;
5647                                 optarg++;
5648                         } else if (optarg[0] == '-') {
5649                                 param.fp_mirror_count_sign =  1;
5650                                 optarg++;
5651                         }
5652
5653                         errno = 0;
5654                         param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5655                         if (errno != 0 || *endptr != '\0' ||
5656                             param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5657                                 fprintf(stderr,
5658                                         "error: bad mirror count '%s'\n",
5659                                         optarg);
5660                                 goto err;
5661                         }
5662                         param.fp_check_mirror_count = 1;
5663                         param.fp_exclude_mirror_count = !!neg_opt;
5664                         break;
5665                 case 'm':
5666                 case 'i':
5667                 case 'O': {
5668                         char *buf, *token, *next, *p;
5669                         int len = 1;
5670                         void *tmp;
5671
5672                         buf = strdup(optarg);
5673                         if (!buf) {
5674                                 ret = -ENOMEM;
5675                                 goto err;
5676                         }
5677
5678                         param.fp_exclude_obd = !!neg_opt;
5679
5680                         token = buf;
5681                         while (token && *token) {
5682                                 token = strchr(token, ',');
5683                                 if (token) {
5684                                         len++;
5685                                         token++;
5686                                 }
5687                         }
5688                         if (c == 'm') {
5689                                 param.fp_exclude_mdt = !!neg_opt;
5690                                 param.fp_num_alloc_mdts += len;
5691                                 tmp = realloc(param.fp_mdt_uuid,
5692                                               param.fp_num_alloc_mdts *
5693                                               sizeof(*param.fp_mdt_uuid));
5694                                 if (!tmp) {
5695                                         ret = -ENOMEM;
5696                                         goto err_free;
5697                                 }
5698
5699                                 param.fp_mdt_uuid = tmp;
5700                         } else {
5701                                 param.fp_exclude_obd = !!neg_opt;
5702                                 param.fp_num_alloc_obds += len;
5703                                 tmp = realloc(param.fp_obd_uuid,
5704                                               param.fp_num_alloc_obds *
5705                                               sizeof(*param.fp_obd_uuid));
5706                                 if (!tmp) {
5707                                         ret = -ENOMEM;
5708                                         goto err_free;
5709                                 }
5710
5711                                 param.fp_obd_uuid = tmp;
5712                         }
5713                         for (token = buf; token && *token; token = next) {
5714                                 struct obd_uuid *puuid;
5715
5716                                 if (c == 'm') {
5717                                         puuid =
5718                                         &param.fp_mdt_uuid[param.fp_num_mdts++];
5719                                 } else {
5720                                         puuid =
5721                                         &param.fp_obd_uuid[param.fp_num_obds++];
5722                                 }
5723                                 p = strchr(token, ',');
5724                                 next = 0;
5725                                 if (p) {
5726                                         *p = 0;
5727                                         next = p+1;
5728                                 }
5729
5730                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5731                                         ret = -E2BIG;
5732                                         goto err_free;
5733                                 }
5734
5735                                 strncpy(puuid->uuid, token,
5736                                         sizeof(puuid->uuid));
5737                         }
5738 err_free:
5739                         if (buf)
5740                                 free(buf);
5741                         break;
5742                 }
5743 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5744                 case 'p':
5745 #endif
5746                 case LFS_POOL_OPT:
5747                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
5748                                 fprintf(stderr,
5749                                         "Pool name %s is too long (max %d)\n",
5750                                         optarg, LOV_MAXPOOLNAME);
5751                                 ret = -1;
5752                                 goto err;
5753                         }
5754                         /*
5755                          * We do check for empty pool because empty pool
5756                          * is used to find V1 LOV attributes
5757                          */
5758                         strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5759                         param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5760                         param.fp_exclude_pool = !!neg_opt;
5761                         param.fp_check_pool = 1;
5762                         break;
5763                 case '0':
5764                         param.fp_zero_end = 1;
5765                         break;
5766                 case 'P': /* we always print, this option is a no-op */
5767                         break;
5768                 case LFS_PRINTF_OPT:
5769                         param.fp_format_printf_str = optarg;
5770                         break;
5771                 case LFS_PROJID_OPT:
5772                         rc = name2projid(&param.fp_projid, optarg);
5773                         if (rc) {
5774                                 if (str2quotaid(&param.fp_projid, optarg)) {
5775                                         fprintf(stderr,
5776                                                 "Invalid project ID: %s\n",
5777                                                 optarg);
5778                                         ret = -1;
5779                                         goto err;
5780                                 }
5781                         }
5782                         param.fp_exclude_projid = !!neg_opt;
5783                         param.fp_check_projid = 1;
5784                         break;
5785                 case 's':
5786                         if (optarg[0] == '+') {
5787                                 param.fp_size_sign = -1;
5788                                 optarg++;
5789                         } else if (optarg[0] == '-') {
5790                                 param.fp_size_sign =  1;
5791                                 optarg++;
5792                         }
5793
5794                         ret = llapi_parse_size(optarg, &param.fp_size,
5795                                                &param.fp_size_units, 0);
5796                         if (ret) {
5797                                 fprintf(stderr, "error: bad file size '%s'\n",
5798                                         optarg);
5799                                 goto err;
5800                         }
5801                         param.fp_check_size = 1;
5802                         param.fp_exclude_size = !!neg_opt;
5803                         break;
5804                 case 'S':
5805                         if (optarg[0] == '+') {
5806                                 param.fp_stripe_size_sign = -1;
5807                                 optarg++;
5808                         } else if (optarg[0] == '-') {
5809                                 param.fp_stripe_size_sign =  1;
5810                                 optarg++;
5811                         }
5812
5813                         ret = llapi_parse_size(optarg, &param.fp_stripe_size,
5814                                                &param.fp_stripe_size_units, 0);
5815                         /* assume units of KB if too small to be valid */
5816                         if (param.fp_stripe_size < 4096)
5817                                 param.fp_stripe_size *= 1024;
5818                         if (ret) {
5819                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
5820                                         optarg);
5821                                 goto err;
5822                         }
5823                         param.fp_check_stripe_size = 1;
5824                         param.fp_exclude_stripe_size = !!neg_opt;
5825                         break;
5826                 case 't':
5827                         param.fp_exclude_type = !!neg_opt;
5828                         switch (optarg[0]) {
5829                         case 'b':
5830                                 param.fp_type = S_IFBLK;
5831                                 break;
5832                         case 'c':
5833                                 param.fp_type = S_IFCHR;
5834                                 break;
5835                         case 'd':
5836                                 param.fp_type = S_IFDIR;
5837                                 break;
5838                         case 'f':
5839                                 param.fp_type = S_IFREG;
5840                                 break;
5841                         case 'l':
5842                                 param.fp_type = S_IFLNK;
5843                                 break;
5844                         case 'p':
5845                                 param.fp_type = S_IFIFO;
5846                                 break;
5847                         case 's':
5848                                 param.fp_type = S_IFSOCK;
5849                                 break;
5850                         default:
5851                                 fprintf(stderr, "%s: bad type '%s'\n",
5852                                         progname, optarg);
5853                                 ret = CMD_HELP;
5854                                 goto err;
5855                         }
5856                         break;
5857                 case LFS_FIND_PERM:
5858                         param.fp_exclude_perm = !!neg_opt;
5859                         param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5860                         if (*optarg == '/') {
5861                                 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5862                                 optarg++;
5863                         } else if (*optarg == '-') {
5864                                 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5865                                 optarg++;
5866                         }
5867
5868                         if (str2mode_t(optarg, &param.fp_perm)) {
5869                                 fprintf(stderr, "error: invalid mode '%s'\n",
5870                                         optarg);
5871                                 ret = -1;
5872                                 goto err;
5873                         }
5874                         break;
5875                 case 'T':
5876                         if (optarg[0] == '+') {
5877                                 param.fp_mdt_count_sign = -1;
5878                                 optarg++;
5879                         } else if (optarg[0] == '-') {
5880                                 param.fp_mdt_count_sign =  1;
5881                                 optarg++;
5882                         }
5883
5884                         errno = 0;
5885                         param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5886                         if (errno != 0 || *endptr != '\0' ||
5887                             param.fp_mdt_count >= UINT32_MAX) {
5888                                 fprintf(stderr, "error: bad mdt_count '%s'\n",
5889                                         optarg);
5890                                 ret = -1;
5891                                 goto err;
5892                         }
5893                         param.fp_check_mdt_count = 1;
5894                         param.fp_exclude_mdt_count = !!neg_opt;
5895                         break;
5896                 case 'z':
5897                         if (optarg[0] == '+') {
5898                                 param.fp_ext_size_sign = -1;
5899                                 optarg++;
5900                         } else if (optarg[0] == '-') {
5901                                 param.fp_ext_size_sign =  1;
5902                                 optarg++;
5903                         }
5904
5905                         ret = llapi_parse_size(optarg, &param.fp_ext_size,
5906                                                &param.fp_ext_size_units, 0);
5907                         if (ret) {
5908                                 fprintf(stderr, "error: bad ext-size '%s'\n",
5909                                         optarg);
5910                                 goto err;
5911                         }
5912                         param.fp_ext_size /= SEL_UNIT_SIZE;
5913                         param.fp_ext_size_units /= SEL_UNIT_SIZE;
5914                         param.fp_check_ext_size = 1;
5915                         param.fp_exclude_ext_size = !!neg_opt;
5916                         break;
5917                 default:
5918                         fprintf(stderr, "%s: unrecognized option '%s'\n",
5919                                 progname, argv[optind - 1]);
5920                 case 'h':
5921                         ret = CMD_HELP;
5922                         goto err;
5923                 }
5924         }
5925         if (!param.fp_verbose)
5926                 param.fp_verbose = VERBOSE_DEFAULT;
5927
5928         if (pathstart == -1) {
5929                 fprintf(stderr, "error: %s: no filename|pathname\n",
5930                         argv[0]);
5931                 ret = CMD_HELP;
5932                 goto err;
5933         } else if (pathend == -1) {
5934                 /* no options */
5935                 pathend = argc;
5936         }
5937
5938         do {
5939                 rc = llapi_find(argv[pathstart], &param);
5940                 if (rc && !ret) {
5941                         ret = rc;
5942                         pathbad = pathstart;
5943                 }
5944         } while (++pathstart < pathend);
5945
5946         if (ret)
5947                 fprintf(stderr, "%s: failed for '%s': %s\n",
5948                         progname, argv[pathbad], strerror(-rc));
5949
5950 err:
5951         if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5952                 free(param.fp_obd_uuid);
5953
5954         if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5955                 free(param.fp_mdt_uuid);
5956
5957         return ret;
5958 }
5959
5960 static int lfs_getstripe_internal(int argc, char **argv,
5961                                   struct find_param *param)
5962 {
5963         struct option long_opts[] = {
5964 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
5965 /* find { .val = 'b',   .name = "blocks",       .has_arg = required_argument }*/
5966 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
5967 /* find { .val = 'B',   .name = "Btime",        .has_arg = required_argument }*/
5968         { .val = LFS_COMP_COUNT_OPT,
5969                         .name = "comp-count",   .has_arg = no_argument },
5970         { .val = LFS_COMP_COUNT_OPT,
5971                 .name = "component-count",      .has_arg = no_argument },
5972         { .val = LFS_COMP_FLAGS_OPT,
5973                         .name = "comp-flags",   .has_arg = optional_argument },
5974         { .val = LFS_COMP_FLAGS_OPT,
5975                 .name = "component-flags",      .has_arg = optional_argument },
5976         { .val = LFS_COMP_START_OPT,
5977                         .name = "comp-start",   .has_arg = optional_argument },
5978         { .val = LFS_COMP_START_OPT,
5979                 .name = "component-start",      .has_arg = optional_argument },
5980         { .val = LFS_MIRROR_INDEX_OPT,
5981                 .name = "mirror-index",         .has_arg = required_argument },
5982         { .val = LFS_MIRROR_ID_OPT,
5983                 .name = "mirror-id",            .has_arg = required_argument },
5984         { .val = LFS_NO_FOLLOW_OPT,
5985                 .name = "no-follow",            .has_arg = no_argument },
5986         { .val = 'c',   .name = "stripe-count", .has_arg = no_argument },
5987         { .val = 'c',   .name = "stripe_count", .has_arg = no_argument },
5988 /* find { .val = 'C',   .name = "ctime",        .has_arg = required_argument }*/
5989         { .val = 'd',   .name = "directory",    .has_arg = no_argument },
5990         { .val = 'D',   .name = "default",      .has_arg = no_argument },
5991         { .val = 'E',   .name = "comp-end",     .has_arg = optional_argument },
5992         { .val = 'E',   .name = "component-end", .has_arg = optional_argument },
5993         { .val = 'F',   .name = "fid",          .has_arg = no_argument },
5994         { .val = 'g',   .name = "generation",   .has_arg = no_argument },
5995 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
5996         { .val = 'h',   .name = "help",         .has_arg = no_argument },
5997 /* dirstripe { .val = 'H', .name = "mdt-hash",  .has_arg = required_argument }*/
5998         { .val = 'i',   .name = "stripe-index", .has_arg = no_argument },
5999         { .val = 'i',   .name = "stripe_index", .has_arg = no_argument },
6000         { .val = 'I',   .name = "comp-id",      .has_arg = optional_argument },
6001         { .val = 'I',   .name = "component-id", .has_arg = optional_argument },
6002 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
6003         { .val = 'L',   .name = "layout",       .has_arg = no_argument },
6004         { .val = 'm',   .name = "mdt",          .has_arg = no_argument },
6005         { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
6006         { .val = 'm',   .name = "mdt_index",    .has_arg = no_argument },
6007 /* find { .val = 'M',   .name = "mtime",        .has_arg = required_argument }*/
6008 /* find { .val = 'n',   .name = "name",         .has_arg = required_argument }*/
6009         { .val = 'N',   .name = "mirror-count", .has_arg = no_argument },
6010         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
6011         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
6012         { .val = 'p',   .name = "pool",         .has_arg = no_argument },
6013 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
6014         { .val = 'q',   .name = "quiet",        .has_arg = no_argument },
6015         { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
6016         { .val = 'R',   .name = "raw",          .has_arg = no_argument },
6017         { .val = 'S',   .name = "stripe-size",  .has_arg = no_argument },
6018         { .val = 'S',   .name = "stripe_size",  .has_arg = no_argument },
6019 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
6020 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
6021 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
6022 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
6023         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
6024 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
6025 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }*/
6026         { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
6027         { .val = 'z',   .name = "extension-size", .has_arg = no_argument },
6028         { .val = 'z',   .name = "ext-size",     .has_arg = no_argument },
6029         { .name = NULL } };
6030         int c, rc = 0;
6031         int neg_opt = 0;
6032         int pathstart = -1, pathend = -1;
6033         int isoption;
6034         char *end, *tmp;
6035
6036         while ((c = getopt_long(argc, argv,
6037                         "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
6038                         long_opts, NULL)) != -1) {
6039                 if (neg_opt)
6040                         --neg_opt;
6041
6042                 /* '!' is part of option */
6043                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
6044                 if (!isoption && pathend != -1) {
6045                         fprintf(stderr,
6046                                 "error: %s: filename|dirname must either precede options or follow options\n",
6047                                 argv[0]);
6048                         return CMD_HELP;
6049                 }
6050                 if (!isoption && pathstart == -1)
6051                         pathstart = optind - 1;
6052                 if (isoption && pathstart != -1 && pathend == -1)
6053                         pathend = optind - 2;
6054
6055                 switch (c) {
6056                 case 1:
6057                         /* unknown: opt is "!" */
6058                         if (strcmp(optarg, "!") == 0)
6059                                 neg_opt = 2;
6060                         break;
6061                 case 'c':
6062                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6063                                 param->fp_verbose |= VERBOSE_COUNT;
6064                                 param->fp_max_depth = 0;
6065                         }
6066                         break;
6067                 case LFS_COMP_COUNT_OPT:
6068                         param->fp_verbose |= VERBOSE_COMP_COUNT;
6069                         param->fp_max_depth = 0;
6070                         break;
6071                 case LFS_COMP_FLAGS_OPT:
6072                         if (optarg) {
6073                                 rc = comp_str2flags(optarg,
6074                                                     &param->fp_comp_flags,
6075                                                     &param->fp_comp_neg_flags);
6076                                 if (rc != 0) {
6077                                         fprintf(stderr,
6078                                                 "error: %s bad component flags '%s'.\n",
6079                                                 argv[0], optarg);
6080                                         return CMD_HELP;
6081                                 }
6082                                 param->fp_check_comp_flags = 1;
6083                         } else {
6084                                 param->fp_verbose |= VERBOSE_COMP_FLAGS;
6085                                 param->fp_max_depth = 0;
6086                         }
6087                         break;
6088                 case LFS_COMP_START_OPT:
6089                         if (optarg) {
6090                                 tmp = optarg;
6091                                 if (tmp[0] == '+') {
6092                                         param->fp_comp_start_sign = -1;
6093                                         tmp++;
6094                                 } else if (tmp[0] == '-') {
6095                                         param->fp_comp_start_sign = 1;
6096                                         tmp++;
6097                                 }
6098                                 rc = llapi_parse_size(tmp,
6099                                                 &param->fp_comp_start,
6100                                                 &param->fp_comp_start_units, 0);
6101                                 if (rc != 0) {
6102                                         fprintf(stderr,
6103                                                 "error: %s bad component start '%s'.\n",
6104                                                 argv[0], tmp);
6105                                         return CMD_HELP;
6106                                 }
6107                                 param->fp_check_comp_start = 1;
6108                         } else {
6109                                 param->fp_verbose |= VERBOSE_COMP_START;
6110                                 param->fp_max_depth = 0;
6111                         }
6112                         break;
6113                 case LFS_MIRROR_INDEX_OPT: {
6114                         unsigned long int mirror_index;
6115
6116                         if (optarg[0] == '+') {
6117                                 param->fp_mirror_index_sign = -1;
6118                                 optarg++;
6119                         } else if (optarg[0] == '-') {
6120                                 param->fp_mirror_index_sign = 1;
6121                                 optarg++;
6122                         }
6123
6124                         errno = 0;
6125                         mirror_index = strtoul(optarg, &end, 0);
6126                         if (errno != 0 || *end != '\0' ||
6127                             mirror_index > UINT16_MAX || (mirror_index == 0 &&
6128                             param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
6129                                 fprintf(stderr,
6130                                         "%s %s: invalid mirror index '%s'\n",
6131                                         progname, argv[0], optarg);
6132                                 return CMD_HELP;
6133                         }
6134
6135                         param->fp_mirror_index = (__u16)mirror_index;
6136
6137                         if (param->fp_mirror_id != 0) {
6138                                 fprintf(stderr,
6139                                         "%s %s: can't specify both mirror index and mirror ID\n",
6140                                         progname, argv[0]);
6141                                 return CMD_HELP;
6142                         }
6143                         param->fp_check_mirror_index = 1;
6144                         param->fp_exclude_mirror_index = !!neg_opt;
6145                         break;
6146                 }
6147                 case LFS_MIRROR_ID_OPT: {
6148                         unsigned long int mirror_id;
6149
6150                         if (optarg[0] == '+') {
6151                                 param->fp_mirror_id_sign = -1;
6152                                 optarg++;
6153                         } else if (optarg[0] == '-') {
6154                                 param->fp_mirror_id_sign = 1;
6155                                 optarg++;
6156                         }
6157
6158                         errno = 0;
6159                         mirror_id = strtoul(optarg, &end, 0);
6160                         if (errno != 0 || *end != '\0' ||
6161                             mirror_id > UINT16_MAX || (mirror_id == 0 &&
6162                             param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
6163                                 fprintf(stderr,
6164                                         "%s %s: invalid mirror ID '%s'\n",
6165                                         progname, argv[0], optarg);
6166                                 return CMD_HELP;
6167                         }
6168
6169                         param->fp_mirror_id = (__u16)mirror_id;
6170
6171                         if (param->fp_mirror_index != 0) {
6172                                 fprintf(stderr,
6173                                         "%s %s: can't specify both mirror index and mirror ID\n",
6174                                         progname, argv[0]);
6175                                 return CMD_HELP;
6176                         }
6177                         param->fp_check_mirror_id = 1;
6178                         param->fp_exclude_mirror_id = !!neg_opt;
6179                         break;
6180                 }
6181                 case LFS_NO_FOLLOW_OPT:
6182                         param->fp_no_follow = true;
6183                         break;
6184                 case 'd':
6185                         param->fp_max_depth = 0;
6186                         break;
6187                 case 'D':
6188                         param->fp_get_default_lmv = 1;
6189                         break;
6190                 case 'E':
6191                         if (optarg) {
6192                                 tmp = optarg;
6193                                 if (tmp[0] == '+') {
6194                                         param->fp_comp_end_sign = -1;
6195                                         tmp++;
6196                                 } else if (tmp[0] == '-') {
6197                                         param->fp_comp_end_sign = 1;
6198                                         tmp++;
6199                                 }
6200
6201                                 if (arg_is_eof(tmp)) {
6202                                         param->fp_comp_end = LUSTRE_EOF;
6203                                         param->fp_comp_end_units = 1;
6204                                         rc = 0;
6205                                 } else {
6206                                         rc = llapi_parse_size(tmp,
6207                                                 &param->fp_comp_end,
6208                                                 &param->fp_comp_end_units, 0);
6209                                         /* assume units of KB if too small */
6210                                         if (param->fp_comp_end < 4096)
6211                                                 param->fp_comp_end *= 1024;
6212                                 }
6213                                 if (rc != 0) {
6214                                         fprintf(stderr,
6215                                                 "error: %s bad component end '%s'.\n",
6216                                                 argv[0], tmp);
6217                                         return CMD_HELP;
6218                                 }
6219                                 param->fp_check_comp_end = 1;
6220                         } else {
6221                                 param->fp_verbose |= VERBOSE_COMP_END;
6222                                 param->fp_max_depth = 0;
6223                         }
6224                         break;
6225                 case 'F':
6226                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6227                                 param->fp_verbose |= VERBOSE_DFID;
6228                                 param->fp_max_depth = 0;
6229                         }
6230                         break;
6231                 case 'g':
6232                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6233                                 param->fp_verbose |= VERBOSE_GENERATION;
6234                                 param->fp_max_depth = 0;
6235                         }
6236                         break;
6237                 case 'i':
6238                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6239                                 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6240                                 param->fp_max_depth = 0;
6241                         }
6242                         break;
6243                 case 'I':
6244                         if (optarg) {
6245                                 param->fp_comp_id = strtoul(optarg, &end, 0);
6246                                 if (*end != '\0' || param->fp_comp_id == 0 ||
6247                                     param->fp_comp_id > LCME_ID_MAX) {
6248                                         fprintf(stderr,
6249                                                 "error: %s bad component id '%s'\n",
6250                                                 argv[0], optarg);
6251                                         return CMD_HELP;
6252                                 }
6253                                 param->fp_check_comp_id = 1;
6254                         } else {
6255                                 param->fp_max_depth = 0;
6256                                 param->fp_verbose |= VERBOSE_COMP_ID;
6257                         }
6258                         break;
6259                 case 'L':
6260                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6261                                 param->fp_verbose |= VERBOSE_PATTERN;
6262                                 param->fp_max_depth = 0;
6263                         }
6264                         break;
6265 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6266                 case 'M':
6267                         fprintf(stderr,
6268                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6269 #endif
6270                 case 'm':
6271                         if (!(param->fp_verbose & VERBOSE_DETAIL))
6272                                 param->fp_max_depth = 0;
6273                         param->fp_verbose |= VERBOSE_MDTINDEX;
6274                         break;
6275                 case 'N':
6276                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6277                                 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6278                                 param->fp_max_depth = 0;
6279                         }
6280                         break;
6281                 case 'O':
6282                         if (param->fp_obd_uuid) {
6283                                 fprintf(stderr,
6284                                         "error: %s: only one obduuid allowed",
6285                                         argv[0]);
6286                                 return CMD_HELP;
6287                         }
6288                         param->fp_obd_uuid = (struct obd_uuid *)optarg;
6289                         break;
6290                 case 'p':
6291                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6292                                 param->fp_verbose |= VERBOSE_POOL;
6293                                 param->fp_max_depth = 0;
6294                         }
6295                         break;
6296                 case 'q':
6297                         param->fp_quiet++;
6298                         break;
6299                 case 'r':
6300                         param->fp_recursive = 1;
6301                         break;
6302                 case 'R':
6303                         param->fp_raw = 1;
6304                         break;
6305                 case 'S':
6306                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6307                                 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6308                                 param->fp_max_depth = 0;
6309                         }
6310                         break;
6311                 case 'v':
6312                         param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6313                         break;
6314                 case 'y':
6315                         param->fp_yaml = 1;
6316                         break;
6317                 case 'z':
6318                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6319                                 param->fp_verbose |= VERBOSE_EXT_SIZE;
6320                                 param->fp_max_depth = 0;
6321                         }
6322                         break;
6323                 default:
6324                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6325                                 progname, argv[optind - 1]);
6326                 case 'h':
6327                         return CMD_HELP;
6328                 }
6329         }
6330
6331         if (pathstart == -1) {
6332                 fprintf(stderr, "error: %s: no filename|pathname\n",
6333                                 argv[0]);
6334                 return CMD_HELP;
6335         } else if (pathend == -1) {
6336                 /* no options */
6337                 pathend = argc;
6338         }
6339
6340         if (pathend > argc)
6341                 return CMD_HELP;
6342
6343         if (param->fp_recursive)
6344                 param->fp_max_depth = -1;
6345         else if (param->fp_verbose & VERBOSE_DETAIL)
6346                 param->fp_max_depth = 1;
6347
6348         if (!param->fp_verbose)
6349                 param->fp_verbose = VERBOSE_DEFAULT;
6350         if (param->fp_quiet)
6351                 param->fp_verbose = VERBOSE_OBJID;
6352
6353         do {
6354                 int rc2;
6355
6356                 rc2 = llapi_getstripe(argv[pathstart], param);
6357                 if (rc2) {
6358                         fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6359                                 progname, argv[0], argv[optind - 1],
6360                                 strerror(-rc2));
6361                         if (!rc)
6362                                 rc = rc2;
6363                 }
6364         } while (++pathstart < pathend);
6365
6366         return rc;
6367 }
6368
6369 static int lfs_tgts(int argc, char **argv)
6370 {
6371         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6372         struct find_param param;
6373         int index = 0, rc = 0;
6374
6375         if (argc > 2)
6376                 return CMD_HELP;
6377
6378         if (argc == 2 && !realpath(argv[1], path)) {
6379                 rc = -errno;
6380                 fprintf(stderr, "error: invalid path '%s': %s\n",
6381                         argv[1], strerror(-rc));
6382                 return rc;
6383         }
6384
6385         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6386                 /* Check if we have a mount point */
6387                 if (mntdir[0] == '\0')
6388                         continue;
6389
6390                 memset(&param, 0, sizeof(param));
6391                 if (!strcmp(argv[0], "mdts"))
6392                         param.fp_get_lmv = 1;
6393
6394                 rc = llapi_ostlist(mntdir, &param);
6395                 if (rc) {
6396                         fprintf(stderr, "error: %s: failed on %s\n",
6397                                 argv[0], mntdir);
6398                 }
6399                 if (path[0] != '\0')
6400                         break;
6401                 memset(mntdir, 0, PATH_MAX);
6402         }
6403
6404         return rc;
6405 }
6406
6407 static int lfs_getstripe(int argc, char **argv)
6408 {
6409         struct find_param param = { 0 };
6410
6411         param.fp_max_depth = 1;
6412         return lfs_getstripe_internal(argc, argv, &param);
6413 }
6414
6415 /* functions */
6416 static int lfs_getdirstripe(int argc, char **argv)
6417 {
6418         struct find_param param = { 0 };
6419         struct option long_opts[] = {
6420         { .val = 'c',   .name = "mdt-count",     .has_arg = no_argument },
6421         { .val = 'D',   .name = "default",       .has_arg = no_argument },
6422         { .val = 'h',   .name = "help",         .has_arg = no_argument },
6423         { .val = 'H',   .name = "mdt-hash",      .has_arg = no_argument },
6424         { .val = 'i',   .name = "mdt-index",     .has_arg = no_argument },
6425         { .val = 'm',   .name = "mdt-index",     .has_arg = no_argument },
6426         { .val = 'O',   .name = "obd",           .has_arg = required_argument },
6427         { .val = 'r',   .name = "recursive",     .has_arg = no_argument },
6428         { .val = 'T',   .name = "mdt-count",     .has_arg = no_argument },
6429         { .val = 'v',   .name = "verbose",       .has_arg = no_argument },
6430         { .val = 'X',   .name = "max-inherit",   .has_arg = no_argument },
6431         { .val = 'y',   .name = "yaml",          .has_arg = no_argument },
6432         { .val = LFS_INHERIT_RR_OPT,
6433                         .name = "max-inherit-rr", .has_arg = no_argument },
6434         { .name = NULL } };
6435         int c, rc = 0;
6436
6437         param.fp_get_lmv = 1;
6438
6439         while ((c = getopt_long(argc, argv,
6440                                 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6441                 switch (c) {
6442                 case 'c':
6443                 case 'T':
6444                         param.fp_verbose |= VERBOSE_COUNT;
6445                         break;
6446                 case 'D':
6447                         param.fp_get_default_lmv = 1;
6448                         break;
6449 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6450                 case 't':
6451                         fprintf(stderr,
6452                                 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6453                         fallthrough;
6454 #endif
6455                 case 'H':
6456                         param.fp_verbose |= VERBOSE_HASH_TYPE;
6457                         break;
6458                 case 'i':
6459                         fallthrough;
6460                 case 'm':
6461                         param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6462                         break;
6463                 case 'O':
6464                         if (param.fp_obd_uuid) {
6465                                 fprintf(stderr,
6466                                         "%s: only one obduuid allowed",
6467                                         progname);
6468                                 return CMD_HELP;
6469                         }
6470                         param.fp_obd_uuid = (struct obd_uuid *)optarg;
6471                         break;
6472                 case 'r':
6473                         param.fp_recursive = 1;
6474                         break;
6475                 case 'v':
6476                         param.fp_verbose |= VERBOSE_DETAIL;
6477                         break;
6478                 case 'X':
6479                         param.fp_verbose |= VERBOSE_INHERIT;
6480                         break;
6481                 case LFS_INHERIT_RR_OPT:
6482                         param.fp_verbose |= VERBOSE_INHERIT_RR;
6483                         break;
6484                 case 'y':
6485                         param.fp_yaml = 1;
6486                         break;
6487                 default:
6488                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6489                                 progname, argv[optind - 1]);
6490                         fallthrough;
6491                 case 'h':
6492                         return CMD_HELP;
6493                 }
6494         }
6495
6496         if (optind >= argc)
6497                 return CMD_HELP;
6498
6499         if (param.fp_recursive)
6500                 param.fp_max_depth = -1;
6501
6502         if (!param.fp_verbose)
6503                 param.fp_verbose = VERBOSE_DEFAULT;
6504
6505         do {
6506                 int rc2;
6507
6508                 rc2 = llapi_getstripe(argv[optind], &param);
6509                 if (rc2) {
6510                         fprintf(stderr, "%s: %s for '%s' failed: %s\n",
6511                                 progname, argv[0], argv[optind],
6512                                 strerror(-rc2));
6513                         if (!rc)
6514                                 rc = rc2;
6515                 }
6516         } while (++optind < argc);
6517
6518         return rc;
6519 }
6520
6521 enum mntdf_flags {
6522         MNTDF_INODES    = 0x0001,
6523         MNTDF_COOKED    = 0x0002,
6524         MNTDF_LAZY      = 0x0004,
6525         MNTDF_VERBOSE   = 0x0008,
6526         MNTDF_SHOW      = 0x0010,
6527         MNTDF_DECIMAL   = 0x0020,
6528 };
6529
6530 #define COOK(value, base)                                       \
6531 ({                                                              \
6532         int radix = 0;                                          \
6533         while (value > base) {                                  \
6534                 value /= base;                                  \
6535                 radix++;                                        \
6536         }                                                       \
6537         radix;                                                  \
6538 })
6539 #define UUF     "%-20s"
6540 #define CSF     "%11s"
6541 #define CDF     "%11llu"
6542 #define HDF     "%8.1f%c"
6543 #define RSF     "%4s"
6544 #define RDF     "%3d%%"
6545
6546 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6547 {
6548         double avail, used, ratio = 0;
6549
6550         if (inodes) {
6551                 avail = st->os_ffree;
6552                 used = st->os_files - st->os_ffree;
6553         } else {
6554                 avail = st->os_bavail;
6555                 used = st->os_blocks - st->os_bfree;
6556         }
6557         if (avail + used > 0)
6558                 ratio = used / (used + avail) * 100;
6559
6560         /* Round up to match df(1) usage percentage */
6561         return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6562 }
6563
6564 /*
6565  * This is to identify various problem states for "lfs df" if .osn_err = true,
6566  * so only show flags reflecting those states by default. Informational states
6567  * are only shown with "-v" and use lower-case names to distinguish them.
6568  * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6569  */
6570 static struct obd_statfs_state_names {
6571         enum obd_statfs_state   osn_state;
6572         const char              osn_name;
6573         bool                    osn_err;
6574 } oss_names[] = {
6575         { .osn_state = OS_STATFS_DEGRADED,   .osn_name = 'D', .osn_err = true },
6576         { .osn_state = OS_STATFS_READONLY,   .osn_name = 'R', .osn_err = true },
6577         { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6578         { .osn_state = OS_STATFS_UNUSED1,    .osn_name = '?', .osn_err = true },
6579         { .osn_state = OS_STATFS_UNUSED2,    .osn_name = '?', .osn_err = true },
6580         { .osn_state = OS_STATFS_ENOSPC,     .osn_name = 'S', .osn_err = true },
6581         { .osn_state = OS_STATFS_ENOINO,     .osn_name = 'I', .osn_err = true },
6582         { .osn_state = OS_STATFS_SUM,        .osn_name = 'a', /* aggregate */ },
6583         { .osn_state = OS_STATFS_NONROT,     .osn_name = 'f', /* flash */     },
6584 };
6585
6586 static int showdf(char *mntdir, struct obd_statfs *stat,
6587                   char *uuid, enum mntdf_flags flags,
6588                   char *type, int index, int rc)
6589 {
6590         long long avail, used, total;
6591         int ratio = 0;
6592         char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6593         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6594         char tbuf[3 * sizeof(__u64)];
6595         char ubuf[3 * sizeof(__u64)];
6596         char abuf[3 * sizeof(__u64)];
6597         char rbuf[3 * sizeof(__u64)];
6598
6599         if (!uuid || !stat)
6600                 return -EINVAL;
6601
6602         switch (rc) {
6603         case 0:
6604                 if (flags & MNTDF_INODES) {
6605                         avail = stat->os_ffree;
6606                         used = stat->os_files - stat->os_ffree;
6607                         total = stat->os_files;
6608                 } else {
6609                         int shift = flags & MNTDF_COOKED ? 0 : 10;
6610
6611                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
6612                         used  = ((stat->os_blocks - stat->os_bfree) *
6613                                  stat->os_bsize) >> shift;
6614                         total = (stat->os_blocks * stat->os_bsize) >> shift;
6615                 }
6616
6617                 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6618
6619                 if (flags & MNTDF_COOKED) {
6620                         int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6621                         double cook_val;
6622                         int i;
6623
6624                         cook_val = (double)total;
6625                         i = COOK(cook_val, base);
6626                         if (i > 0)
6627                                 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6628                                          suffix[i - 1]);
6629                         else
6630                                 snprintf(tbuf, sizeof(tbuf), CDF, total);
6631
6632                         cook_val = (double)used;
6633                         i = COOK(cook_val, base);
6634                         if (i > 0)
6635                                 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6636                                          suffix[i - 1]);
6637                         else
6638                                 snprintf(ubuf, sizeof(ubuf), CDF, used);
6639
6640                         cook_val = (double)avail;
6641                         i = COOK(cook_val, base);
6642                         if (i > 0)
6643                                 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6644                                          suffix[i - 1]);
6645                         else
6646                                 snprintf(abuf, sizeof(abuf), CDF, avail);
6647                 } else {
6648                         snprintf(tbuf, sizeof(tbuf), CDF, total);
6649                         snprintf(ubuf, sizeof(tbuf), CDF, used);
6650                         snprintf(abuf, sizeof(tbuf), CDF, avail);
6651                 }
6652
6653                 sprintf(rbuf, RDF, ratio);
6654                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6655                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6656                 if (type)
6657                         printf("[%s:%d]", type, index);
6658
6659                 if (stat->os_state) {
6660                         uint32_t i;
6661
6662                         printf(" ");
6663                         for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6664                                 if (oss_names[i].osn_state & stat->os_state &&
6665                                     (oss_names[i].osn_err ||
6666                                      flags & MNTDF_VERBOSE))
6667                                         printf("%c", oss_names[i].osn_name);
6668                         }
6669                 }
6670
6671                 printf("\n");
6672                 break;
6673         case -ENODATA:
6674                 printf(UUF": inactive device\n", uuid);
6675                 break;
6676         default:
6677                 printf(UUF": %s\n", uuid, strerror(-rc));
6678                 break;
6679         }
6680
6681         return 0;
6682 }
6683
6684 struct ll_stat_type {
6685         int   st_op;
6686         char *st_name;
6687 };
6688
6689 #define LL_STATFS_MAX   LOV_MAX_STRIPE_COUNT
6690
6691 struct ll_statfs_data {
6692         int                     sd_index;
6693         struct obd_statfs       sd_st;
6694 };
6695
6696 struct ll_statfs_buf {
6697         int                     sb_count;
6698         struct ll_statfs_data   sb_buf[LL_STATFS_MAX];
6699 };
6700
6701 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6702                  int ops, struct ll_statfs_buf *lsb)
6703 {
6704         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6705         struct obd_uuid uuid_buf;
6706         char *poolname = NULL;
6707         struct ll_stat_type types[] = {
6708                 { .st_op = LL_STATFS_LMV,       .st_name = "MDT" },
6709                 { .st_op = LL_STATFS_LOV,       .st_name = "OST" },
6710                 { .st_name = NULL } };
6711         struct ll_stat_type *tp;
6712         __u64 ost_files = 0;
6713         __u64 ost_ffree = 0;
6714         __u32 index;
6715         __u32 type;
6716         int fd;
6717         int rc = 0;
6718         int rc2;
6719
6720         if (pool) {
6721                 poolname = strchr(pool, '.');
6722                 if (poolname) {
6723                         if (strncmp(fsname, pool, strlen(fsname))) {
6724                                 fprintf(stderr, "filesystem name incorrect\n");
6725                                 return -ENODEV;
6726                         }
6727                         poolname++;
6728                 } else
6729                         poolname = pool;
6730         }
6731
6732         fd = open(mntdir, O_RDONLY);
6733         if (fd < 0) {
6734                 rc = -errno;
6735                 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6736                         strerror(errno));
6737                 return rc;
6738         }
6739
6740         if (flags & MNTDF_SHOW) {
6741                 if (flags & MNTDF_INODES)
6742                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6743                                "UUID", "Inodes", "IUsed", "IFree",
6744                                "IUse%", "Mounted on");
6745                 else
6746                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6747                                "UUID",
6748                                flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6749                                "Used", "Available", "Use%", "Mounted on");
6750         }
6751
6752         for (tp = types; tp->st_name != NULL; tp++) {
6753                 bool have_ost = false;
6754
6755                 if (!(tp->st_op & ops))
6756                         continue;
6757
6758                 for (index = 0; index < LOV_ALL_STRIPES &&
6759                      (!lsb || lsb->sb_count < LL_STATFS_MAX); index++) {
6760                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
6761                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6762                         type = flags & MNTDF_LAZY ?
6763                                 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6764                         rc2 = llapi_obd_fstatfs(fd, type, index,
6765                                                 &stat_buf, &uuid_buf);
6766                         if (rc2 == -ENODEV)
6767                                 break;
6768                         if (rc2 == -EAGAIN)
6769                                 continue;
6770                         if (rc2 == -ENODATA) { /* Inactive device, OK. */
6771                                 if (!(flags & MNTDF_VERBOSE))
6772                                         continue;
6773                         } else if (rc2 < 0 && rc == 0) {
6774                                 rc = rc2;
6775                         }
6776
6777                         /*
6778                          * If we have OSTs then don't report MDT block counts.
6779                          * For MDT-only filesystems the expectation is that all
6780                          * layouts have a DoM component.  For filesystems with
6781                          * OSTs, files are not necessarily going to store data
6782                          * on MDTs, and MDT space is limited to a fraction of
6783                          * OST space, so don't include it in the summary.
6784                          */
6785                         if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6786                                 have_ost = true;
6787                                 sum.os_blocks = 0;
6788                                 sum.os_bfree = 0;
6789                                 sum.os_bavail = 0;
6790                         }
6791
6792                         if (poolname && tp->st_op == LL_STATFS_LOV &&
6793                             llapi_search_ost(fsname, poolname,
6794                                              obd_uuid2str(&uuid_buf)) != 1)
6795                                 continue;
6796
6797                         /*
6798                          * the llapi_obd_fstatfs() call may have returned with
6799                          * an error, but if it filled in uuid_buf we will at
6800                          * lease use that to print out a message for that OBD.
6801                          * If we didn't get anything in the uuid_buf, then fill
6802                          * it in so that we can print an error message.
6803                          */
6804                         if (uuid_buf.uuid[0] == '\0')
6805                                 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6806                                          "%s%04x", tp->st_name, index);
6807                         if (!rc && lsb) {
6808                                 lsb->sb_buf[lsb->sb_count].sd_index = index;
6809                                 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6810                                 lsb->sb_count++;
6811                         }
6812                         if (flags & MNTDF_SHOW)
6813                                 showdf(mntdir, &stat_buf,
6814                                        obd_uuid2str(&uuid_buf), flags,
6815                                        tp->st_name, index, rc2);
6816
6817                         if (rc2)
6818                                 continue;
6819
6820                         if (tp->st_op == LL_STATFS_LMV) {
6821                                 sum.os_ffree += stat_buf.os_ffree;
6822                                 sum.os_files += stat_buf.os_files;
6823                         } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6824                                 ost_files += stat_buf.os_files;
6825                                 ost_ffree += stat_buf.os_ffree;
6826                         }
6827                         sum.os_blocks += stat_buf.os_blocks *
6828                                          stat_buf.os_bsize;
6829                         sum.os_bfree  += stat_buf.os_bfree *
6830                                          stat_buf.os_bsize;
6831                         sum.os_bavail += stat_buf.os_bavail *
6832                                          stat_buf.os_bsize;
6833                 }
6834         }
6835
6836         close(fd);
6837
6838         /*
6839          * If we have _some_ OSTs, but don't have as many free objects on the
6840          * OST as inodes on the MDTs, reduce the reported number of inodes
6841          * to compensate, so that the "inodes in use" number is correct.
6842          * This should be kept in sync with ll_statfs_internal().
6843          */
6844         if (ost_files && ost_ffree < sum.os_ffree) {
6845                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6846                 sum.os_ffree = ost_ffree;
6847         }
6848         if (flags & MNTDF_SHOW) {
6849                 printf("\n");
6850                 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6851                 printf("\n");
6852         }
6853
6854         return rc;
6855 }
6856
6857 enum {
6858         LAYOUT_INHERIT_UNSET    = -2,
6859 };
6860
6861 /* functions */
6862 static int lfs_setdirstripe(int argc, char **argv)
6863 {
6864         char *dname;
6865         struct lfs_setstripe_args lsa = { 0 };
6866         struct llapi_stripe_param *param = NULL;
6867         __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6868         char *end;
6869         int c;
6870         char *mode_opt = NULL;
6871         bool default_stripe = false;
6872         bool delete = false;
6873         bool foreign_mode = false;
6874         bool mdt_count_set = false;
6875         mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6876         mode_t previous_mode = 0;
6877         char *xattr = NULL;
6878         __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6879         int max_inherit = LAYOUT_INHERIT_UNSET;
6880         int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6881         struct option long_opts[] = {
6882         { .val = 'c',   .name = "count",        .has_arg = required_argument },
6883         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument },
6884         { .val = 'd',   .name = "delete",       .has_arg = no_argument },
6885         { .val = 'D',   .name = "default",      .has_arg = no_argument },
6886         { .val = 'D',   .name = "default_stripe", .has_arg = no_argument },
6887         { .val = LFS_LAYOUT_FLAGS_OPT,
6888                         .name = "flags",        .has_arg = required_argument },
6889         { .val = LFS_LAYOUT_FOREIGN_OPT,
6890                         .name = "foreign",      .has_arg = optional_argument},
6891         { .val = 'h',   .name = "help",         .has_arg = no_argument },
6892         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
6893 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6894         { .val = 'i',   .name = "mdt-index",    .has_arg = required_argument },
6895         { .val = 'i',   .name = "mdt",          .has_arg = required_argument },
6896 #else
6897 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
6898         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
6899         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
6900 #endif
6901 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6902         { .val = 'i',   .name = "index",        .has_arg = required_argument },
6903 #endif
6904         { .val = 'o',   .name = "mode",         .has_arg = required_argument },
6905 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6906         { .val = 't',   .name = "hash-type",    .has_arg = required_argument },
6907 #endif
6908         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
6909         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
6910         { .val = 'X',   .name = "max-inherit",  .has_arg = required_argument },
6911         { .val = LFS_INHERIT_RR_OPT,
6912                         .name = "max-inherit-rr", .has_arg = required_argument},
6913 /* setstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
6914 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
6915         { .name = NULL } };
6916         int result = 0;
6917
6918         setstripe_args_init(&lsa);
6919
6920         while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6921                                 long_opts, NULL)) >= 0) {
6922                 switch (c) {
6923                 case 0:
6924                         /* Long options. */
6925                         break;
6926                 case 'c':
6927                 case 'T':
6928                         errno = 0;
6929                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6930                         if (errno != 0 || *end != '\0' ||
6931                             lsa.lsa_stripe_count < -1 ||
6932                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6933                                 fprintf(stderr,
6934                                         "%s: invalid stripe count '%s'\n",
6935                                         progname, optarg);
6936                                 return CMD_HELP;
6937                         }
6938                         mdt_count_set = true;
6939                         break;
6940                 case 'd':
6941                         delete = true;
6942                         default_stripe = true;
6943                         break;
6944                 case 'D':
6945                         default_stripe = true;
6946                         break;
6947                 case LFS_LAYOUT_FOREIGN_OPT:
6948                         if (optarg) {
6949                                 /* check pure numeric */
6950                                 type = strtoul(optarg, &end, 0);
6951                                 if (*end) {
6952                                         /* check name */
6953                                         type = check_foreign_type_name(optarg);
6954                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6955                                                 fprintf(stderr,
6956                                                         "%s %s: unknown foreign type '%s'\n",
6957                                                         progname, argv[0],
6958                                                         optarg);
6959                                                 return CMD_HELP;
6960                                         }
6961                                 } else if (type >= UINT32_MAX) {
6962                                         fprintf(stderr,
6963                                                 "%s %s: invalid foreign type '%s'\n",
6964                                                 progname, argv[0], optarg);
6965                                         return CMD_HELP;
6966                                 }
6967                         }
6968                         foreign_mode = true;
6969                         break;
6970                 case LFS_LAYOUT_FLAGS_OPT:
6971                         errno = 0;
6972                         flags = strtoul(optarg, &end, 16);
6973                         if (errno != 0 || *end != '\0' ||
6974                             flags >= UINT32_MAX) {
6975                                 fprintf(stderr,
6976                                         "%s %s: invalid hex flags '%s'\n",
6977                                         progname, argv[0], optarg);
6978                                 return CMD_HELP;
6979                         }
6980                         if (!foreign_mode) {
6981                                 fprintf(stderr,
6982                                         "%s %s: hex flags must be specified with --foreign option\n",
6983                                         progname, argv[0]);
6984                                 return CMD_HELP;
6985                         }
6986                         break;
6987 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6988                 case 't':
6989                         fprintf(stderr,
6990                                 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6991                         fallthrough;
6992 #endif
6993                 case 'H':
6994                         lsa.lsa_pattern = check_hashtype(optarg);
6995                         if (lsa.lsa_pattern == 0) {
6996                                 fprintf(stderr,
6997                                         "%s %s: bad directory hash type '%s'\n",
6998                                         progname, argv[0], optarg);
6999                                 return CMD_HELP;
7000                         }
7001                         break;
7002                 case 'i':
7003 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
7004                 case 'm':
7005 #endif
7006 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7007                         if (strcmp(argv[optind - 1], "--index") == 0)
7008                                 fprintf(stderr,
7009                                         "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
7010                                         progname, argv[0]);
7011 #endif
7012                         lsa.lsa_nr_tgts = parse_targets(mdts,
7013                                                 sizeof(mdts) / sizeof(__u32),
7014                                                 lsa.lsa_nr_tgts, optarg, NULL);
7015                         if (lsa.lsa_nr_tgts < 0) {
7016                                 fprintf(stderr,
7017                                         "%s %s: invalid MDT target(s) '%s'\n",
7018                                         progname, argv[0], optarg);
7019                                 return CMD_HELP;
7020                         }
7021
7022                         lsa.lsa_tgts = mdts;
7023                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7024                                 lsa.lsa_stripe_off = mdts[0];
7025                         break;
7026                 case 'o':
7027                         mode_opt = optarg;
7028                         break;
7029                 case 'x':
7030                         xattr = optarg;
7031                         break;
7032                 case 'X':
7033                         errno = 0;
7034                         max_inherit = strtol(optarg, &end, 10);
7035                         if (errno != 0 || *end != '\0' || max_inherit < -2) {
7036                                 fprintf(stderr,
7037                                         "%s %s: invalid max-inherit '%s'\n",
7038                                         progname, argv[0], optarg);
7039                                 return CMD_HELP;
7040                         }
7041                         if (max_inherit == 0) {
7042                                 max_inherit = LMV_INHERIT_NONE;
7043                         } else if (max_inherit == -1) {
7044                                 max_inherit = LMV_INHERIT_UNLIMITED;
7045                         } else if (max_inherit > LMV_INHERIT_MAX) {
7046                                 fprintf(stderr,
7047                                         "%s %s: max-inherit %d exceeds maximum %u\n",
7048                                         progname, argv[0], max_inherit,
7049                                         LMV_INHERIT_MAX);
7050                                 return CMD_HELP;
7051                         }
7052                         break;
7053                 case LFS_INHERIT_RR_OPT:
7054                         if (!default_stripe) {
7055                                 fprintf(stderr,
7056                                         "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
7057                                         progname, argv[0]);
7058                                 return CMD_HELP;
7059                         }
7060                         errno = 0;
7061                         max_inherit_rr = strtol(optarg, &end, 10);
7062                         if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
7063                                 fprintf(stderr,
7064                                         "%s %s: invalid max-inherit-rr '%s'\n",
7065                                         progname, argv[0], optarg);
7066                                 return CMD_HELP;
7067                         }
7068                         if (max_inherit_rr == 0) {
7069                                 max_inherit_rr = LMV_INHERIT_RR_NONE;
7070                         } else if (max_inherit_rr == -1) {
7071                                 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
7072                         } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
7073                                 fprintf(stderr,
7074                                         "%s %s: max-inherit-rr %d exceeds maximum %u\n",
7075                                         progname, argv[0], max_inherit_rr,
7076                                         LMV_INHERIT_RR_MAX);
7077                                 return CMD_HELP;
7078                         }
7079                         break;
7080                 default:
7081                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7082                                 progname, argv[optind - 1]);
7083                         fallthrough;
7084                 case 'h':
7085                         return CMD_HELP;
7086                 }
7087         }
7088
7089         if (optind == argc) {
7090                 fprintf(stderr, "%s %s: DIR must be specified\n",
7091                         progname, argv[0]);
7092                 return CMD_HELP;
7093         }
7094
7095         if (xattr && !foreign_mode) {
7096                 /*
7097                  * only print a warning as this is armless and will be
7098                  * ignored
7099                  */
7100                 fprintf(stderr,
7101                         "%s %s: xattr has been specified for non-foreign layout\n",
7102                         progname, argv[0]);
7103         } else if (foreign_mode && !xattr) {
7104                 fprintf(stderr,
7105                         "%s %s: xattr must be provided in foreign mode\n",
7106                         progname, argv[0]);
7107                 return CMD_HELP;
7108         }
7109
7110         if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
7111             lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
7112                 fprintf(stderr,
7113                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
7114                         progname, argv[0]);
7115                 return CMD_HELP;
7116         }
7117
7118         if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
7119             lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
7120                 /* if no parameters set, create directory on least-used MDTs */
7121                 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
7122                 lsa.lsa_stripe_count = 1;
7123         }
7124
7125         if (delete &&
7126             (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
7127              lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
7128                 fprintf(stderr,
7129                         "%s %s: cannot specify -d with -c or -i options\n",
7130                         progname, argv[0]);
7131                 return CMD_HELP;
7132         }
7133
7134         if (mode_opt) {
7135                 mode = strtoul(mode_opt, &end, 8);
7136                 if (*end != '\0') {
7137                         fprintf(stderr,
7138                                 "%s %s: bad MODE '%s'\n",
7139                                 progname, argv[0], mode_opt);
7140                         return CMD_HELP;
7141                 }
7142                 previous_mode = umask(0);
7143         }
7144
7145         /* check max-inherit and warn user in some cases */
7146         if (default_stripe &&
7147             (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
7148                 if (max_inherit == LMV_INHERIT_UNLIMITED)
7149                         fprintf(stderr,
7150                         "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
7151                         progname, argv[0], lsa.lsa_stripe_count);
7152                 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
7153                          max_inherit != LMV_INHERIT_NONE)
7154                         fprintf(stderr,
7155                                 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
7156                                 progname, argv[0], max_inherit,
7157                                 lsa.lsa_stripe_count);
7158         }
7159
7160         if (default_stripe && lsa.lsa_nr_tgts > 1 && !mdt_count_set) {
7161                 fprintf(stderr,
7162                         "%s %s: trying to create unrecommended default striped directory layout,\n"
7163                         "       '-D -i x,y,z' will stripe every new directory across all MDTs,\n"
7164                         "       add -c with the number of MDTs to do this anyway\n",
7165                         progname, argv[0]);
7166                 return CMD_HELP;
7167         }
7168
7169         if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
7170             lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
7171             lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
7172                 fprintf(stderr,
7173                         "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
7174                         progname, argv[0], lsa.lsa_stripe_off);
7175                 return CMD_HELP;
7176         }
7177
7178         /* foreign LMV/dir case */
7179         if (foreign_mode) {
7180                 if (argc > optind + 1) {
7181                         fprintf(stderr,
7182                                 "%s %s: cannot specify multiple foreign dirs\n",
7183                                 progname, argv[0]);
7184                         return CMD_HELP;
7185                 }
7186
7187                 dname = argv[optind];
7188                 result = llapi_dir_create_foreign(dname, mode, type, flags,
7189                                                   xattr);
7190                 if (result != 0)
7191                         fprintf(stderr,
7192                                 "%s mkdir: can't create foreign dir '%s': %s\n",
7193                                 progname, dname, strerror(-result));
7194                 return result;
7195         }
7196
7197         /*
7198          * initialize stripe parameters, in case param is converted to specific,
7199          * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
7200          */
7201         param = calloc(1, offsetof(typeof(*param),
7202                        lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
7203                                 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
7204         if (!param) {
7205                 fprintf(stderr,
7206                         "%s %s: cannot allocate memory for parameters: %s\n",
7207                         progname, argv[0], strerror(ENOMEM));
7208                 return CMD_HELP;
7209         }
7210
7211         /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
7212         if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
7213             (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
7214              lsa.lsa_stripe_count == 0))
7215                 lsa.lsa_stripe_count = 1;
7216         if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
7217                 param->lsp_stripe_count = lsa.lsa_stripe_count;
7218         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7219                 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
7220         else
7221                 param->lsp_stripe_offset = lsa.lsa_stripe_off;
7222         if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
7223                 param->lsp_stripe_pattern = lsa.lsa_pattern;
7224         else
7225                 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
7226         param->lsp_pool = lsa.lsa_pool_name;
7227         param->lsp_is_specific = false;
7228         if (max_inherit == LAYOUT_INHERIT_UNSET) {
7229                 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1)
7230                         max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
7231                 else
7232                         max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
7233         }
7234         param->lsp_max_inherit = max_inherit;
7235         if (default_stripe) {
7236
7237                 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
7238                         max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
7239                 param->lsp_max_inherit_rr = max_inherit_rr;
7240         }
7241         if (strcmp(argv[0], "mkdir") == 0)
7242                 param->lsp_is_create = true;
7243         if (lsa.lsa_nr_tgts > 1) {
7244                 if (lsa.lsa_stripe_count > 0 &&
7245                     lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7246                     lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7247                         fprintf(stderr,
7248                                 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7249                                 argv[0], lsa.lsa_stripe_count,
7250                                 lsa.lsa_nr_tgts);
7251                         free(param);
7252                         return CMD_HELP;
7253                 }
7254
7255                 param->lsp_is_specific = true;
7256                 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7257                 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7258         }
7259
7260         dname = argv[optind];
7261         do {
7262                 if (default_stripe) {
7263                         result = llapi_dir_set_default_lmv(dname, param);
7264                         if (result)
7265                                 fprintf(stderr,
7266                                         "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7267                                         progname, dname, strerror(-result));
7268                         continue;
7269                 }
7270
7271                 result = llapi_dir_create(dname, mode, param);
7272                 if (result)
7273                         fprintf(stderr,
7274                                 "%s setdirstripe: cannot create dir '%s': %s\n",
7275                                 progname, dname, strerror(-result));
7276         } while (!result && (dname = argv[++optind]));
7277
7278         if (mode_opt)
7279                 umask(previous_mode);
7280
7281         free(param);
7282         return result;
7283 }
7284
7285 static int lfs_rmentry(int argc, char **argv)
7286 {
7287         char *dname;
7288         int index;
7289         int result = 0;
7290
7291         if (argc <= 1) {
7292                 fprintf(stderr, "error: %s: missing dirname\n",
7293                         argv[0]);
7294                 return CMD_HELP;
7295         }
7296
7297         index = 1;
7298         dname = argv[index];
7299         while (dname) {
7300                 int rc2;
7301
7302                 rc2 = llapi_direntry_remove(dname);
7303                 if (rc2) {
7304                         fprintf(stderr,
7305                                 "%s %s: remove dir entry '%s' failed: %s\n",
7306                                 progname, argv[0], dname, strerror(-rc2));
7307                         if (!result)
7308                                 result = rc2;
7309                 }
7310                 dname = argv[++index];
7311         }
7312         return result;
7313 }
7314
7315 static int lfs_unlink_foreign(int argc, char **argv)
7316 {
7317         char *name;
7318         int   index;
7319         int   result = 0;
7320
7321         if (argc <= 1) {
7322                 fprintf(stderr, "error: %s: missing pathname\n",
7323                         argv[0]);
7324                 return CMD_HELP;
7325         }
7326
7327         index = 1;
7328         name = argv[index];
7329         while (name != NULL) {
7330                 result = llapi_unlink_foreign(name);
7331                 if (result) {
7332                         fprintf(stderr,
7333                                 "error: %s: unlink foreign entry '%s' failed\n",
7334                                 argv[0], name);
7335                         break;
7336                 }
7337                 name = argv[++index];
7338         }
7339         return result;
7340 }
7341
7342 static int lfs_mv(int argc, char **argv)
7343 {
7344         struct lmv_user_md lmu = { LMV_USER_MAGIC };
7345         struct find_param param = {
7346                 .fp_max_depth = -1,
7347                 .fp_mdt_index = -1,
7348         };
7349         char *end;
7350         int c;
7351         int rc = 0;
7352         struct option long_opts[] = {
7353         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
7354         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
7355         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7356         { .name = NULL } };
7357
7358         while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7359                 switch (c) {
7360 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7361                 case 'M':
7362                         fprintf(stderr,
7363                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7364 #endif
7365                 case 'm':
7366                         errno = 0;
7367                         lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7368                         if (errno != 0 || *end != '\0' ||
7369                             lmu.lum_stripe_offset >= UINT32_MAX) {
7370                                 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7371                                         progname, optarg);
7372                                 return CMD_HELP;
7373                         }
7374                         break;
7375                 case 'v':
7376                         param.fp_verbose = VERBOSE_DETAIL;
7377                         break;
7378                 default:
7379                         fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7380                                 progname, argv[optind - 1]);
7381                         return CMD_HELP;
7382                 }
7383         }
7384
7385         if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7386                 fprintf(stderr, "%s mv: MDT index must be specified\n",
7387                         progname);
7388                 return CMD_HELP;
7389         }
7390
7391         if (optind >= argc) {
7392                 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7393                 return CMD_HELP;
7394         }
7395
7396         lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7397
7398         /* initialize migrate mdt parameters */
7399         param.fp_lmv_md = &lmu;
7400         param.fp_migrate = 1;
7401         rc = llapi_migrate_mdt(argv[optind], &param);
7402         if (rc != 0)
7403                 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7404                         progname, argv[optind], lmu.lum_stripe_offset,
7405                         strerror(-rc));
7406         return rc;
7407 }
7408
7409 static int lfs_osts(int argc, char **argv)
7410 {
7411         return lfs_tgts(argc, argv);
7412 }
7413
7414 static int lfs_mdts(int argc, char **argv)
7415 {
7416         return lfs_tgts(argc, argv);
7417 }
7418
7419 static int lfs_df(int argc, char **argv)
7420 {
7421         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7422         enum mntdf_flags flags = MNTDF_SHOW;
7423         int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7424         int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7425         char fsname[PATH_MAX] = "", *pool_name = NULL;
7426         struct option long_opts[] = {
7427         { .val = 'h',   .name = "human-readable", .has_arg = no_argument },
7428         { .val = 'H',   .name = "si",           .has_arg = no_argument },
7429         { .val = 'i',   .name = "inodes",       .has_arg = no_argument },
7430         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
7431         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
7432         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7433         { .name = NULL} };
7434
7435         while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7436                 switch (c) {
7437                 case 'h':
7438                         flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7439                         break;
7440                 case 'H':
7441                         flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7442                         break;
7443                 case 'i':
7444                         flags |= MNTDF_INODES;
7445                         break;
7446                 case 'l':
7447                         flags |= MNTDF_LAZY;
7448                         break;
7449                 case 'p':
7450                         pool_name = optarg;
7451                         break;
7452                 case 'v':
7453                         flags |= MNTDF_VERBOSE;
7454                         break;
7455                 default:
7456                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7457                                 progname, argv[optind - 1]);
7458                         return CMD_HELP;
7459                 }
7460         }
7461
7462         /* Handle case where path is not specified */
7463         if (optind == argc) {
7464                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7465                         /* Check if we have a mount point */
7466                         if (mntdir[0] == '\0')
7467                                 continue;
7468
7469                         rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7470                         if (rc || path[0] != '\0')
7471                                 break;
7472
7473                         fsname[0] = '\0'; /* avoid matching in next loop */
7474                         mntdir[0] = '\0'; /* avoid matching in next loop */
7475                         path[0] = '\0'; /* clean for next loop */
7476                 }
7477                 return rc;
7478         }
7479
7480         /* Loop through all the remaining arguments. These are Lustre FS
7481          * paths.
7482          */
7483         for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7484                 bool valid = false;
7485
7486                 fsname[0] = '\0'; /* start clean */
7487                 mntdir[0] = '\0'; /* start clean */
7488                 path[0] = '\0';   /* start clean */
7489
7490                 /* path does not exists at all */
7491                 if (!realpath(argv[arg_idx], path)) {
7492                         rc = -errno;
7493                         fprintf(stderr, "error: invalid path '%s': %s\n",
7494                                 argv[arg_idx], strerror(-rc));
7495                         /* save first seen error */
7496                         if (!rc1)
7497                                 rc1 = rc;
7498
7499                         continue;
7500                 }
7501
7502                 /* path exists but may not be a Lustre filesystem */
7503                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7504                         /* Check if we have a mount point */
7505                         if (mntdir[0] == '\0')
7506                                 continue;
7507
7508                         rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7509                         if (rc || path[0] != '\0') {
7510                                 valid = true;
7511
7512                                 /* save first seen error */
7513                                 if (!rc1)
7514                                         rc1 = rc;
7515                                 break;
7516                         }
7517                 }
7518
7519                 if (!valid) {
7520                         llapi_printf(LLAPI_MSG_ERROR,
7521                                      "%s:%s Not a Lustre filesystem\n",
7522                                      argv[0], argv[arg_idx]);
7523                         /* save first seen error */
7524                         if (!rc1)
7525                                 rc1 = -EOPNOTSUPP;
7526                 }
7527         }
7528
7529         return rc1;
7530 }
7531
7532 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7533                           bool opt_instance, bool opt_fsname, bool opt_mntdir)
7534 {
7535         int rc = 0;
7536
7537         if (opt_fsname == opt_instance) { /* both true or both false */
7538                 rc = llapi_getname(mntdir, buf, buflen);
7539         } else if (opt_fsname) {
7540                 /*
7541                  * llapi_search_mounts() fills @buf with fsname, but that is not
7542                  * called if explicit paths are specified on the command-line
7543                  */
7544                 if (buf[0] == '\0')
7545                         rc = llapi_get_fsname(mntdir, buf, buflen);
7546         } else /* if (opt_instance) */ {
7547                 rc = llapi_get_instance(mntdir, buf, buflen);
7548         }
7549
7550         if (rc < 0) {
7551                 fprintf(stderr, "cannot get instance for '%s': %s\n",
7552                         mntdir, strerror(-rc));
7553                 return rc;
7554         }
7555
7556         if (opt_mntdir)
7557                 printf("%s %s\n", buf, mntdir);
7558         else
7559                 printf("%s\n", buf);
7560
7561         return 0;
7562 }
7563
7564 static int lfs_getname(int argc, char **argv)
7565 {
7566         struct option long_opts[] = {
7567         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7568         { .val = 'i',   .name = "instance",     .has_arg = no_argument },
7569         { .val = 'n',   .name = "fsname",       .has_arg = no_argument },
7570         { .name = NULL} };
7571         bool opt_instance = false, opt_fsname = false;
7572         char fsname[PATH_MAX] = "";
7573         int rc = 0, rc2, c;
7574
7575         while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7576                 switch (c) {
7577                 case 'i':
7578                         opt_instance = true;
7579                         break;
7580                 case 'n':
7581                         opt_fsname = true;
7582                         break;
7583                 default:
7584                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7585                                 progname, argv[optind - 1]);
7586                         fallthrough;
7587                 case 'h':
7588                         return CMD_HELP;
7589                 }
7590         }
7591
7592         if (optind == argc) { /* no paths specified, get all paths. */
7593                 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7594                 int index = 0;
7595
7596                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7597                         rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7598                                              opt_instance, opt_fsname, true);
7599                         if (!rc)
7600                                 rc = rc2;
7601                         path[0] = fsname[0] = mntdir[0] = '\0';
7602                 }
7603         } else { /* paths specified, only attempt to search these. */
7604                 bool opt_mntdir;
7605
7606                 /* if only one path is given, print only requested info */
7607                 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7608
7609                 for (; optind < argc; optind++) {
7610                         rc2 = print_instance(argv[optind], fsname,
7611                                              sizeof(fsname), opt_instance,
7612                                              opt_fsname, opt_mntdir);
7613                         if (!rc)
7614                                 rc = rc2;
7615                         fsname[0] = '\0';
7616                 }
7617         }
7618
7619         return rc;
7620 }
7621
7622 static int lfs_check(int argc, char **argv)
7623 {
7624         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7625         int num_types = 1;
7626         char *obd_types[3];
7627         char obd_type1[4];
7628         char obd_type2[4];
7629         char obd_type3[4];
7630         int rc;
7631
7632         if (argc < 2 || argc > 3) {
7633                 fprintf(stderr, "%s check: server type must be specified\n",
7634                         progname);
7635                 return CMD_HELP;
7636         }
7637
7638         obd_types[0] = obd_type1;
7639         obd_types[1] = obd_type2;
7640         obd_types[2] = obd_type3;
7641
7642         if (strcmp(argv[1], "osts") == 0) {
7643                 strcpy(obd_types[0], "osc");
7644         } else if (strcmp(argv[1], "mdts") == 0 ||
7645                    strcmp(argv[1], "mds") == 0) {
7646                 strcpy(obd_types[0], "mdc");
7647         } else if (strcmp(argv[1], "mgts") == 0) {
7648                 strcpy(obd_types[0], "mgc");
7649         } else if (strcmp(argv[1], "all") == 0 ||
7650                    strcmp(argv[1], "servers") == 0) {
7651                 num_types = 3;
7652                 strcpy(obd_types[0], "osc");
7653                 strcpy(obd_types[1], "mdc");
7654                 strcpy(obd_types[2], "mgc");
7655         } else {
7656                 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7657                         progname, argv[1]);
7658                 return CMD_HELP;
7659         }
7660
7661         if (argc >= 3 && !realpath(argv[2], path)) {
7662                 rc = -errno;
7663                 fprintf(stderr, "error: invalid path '%s': %s\n",
7664                         argv[2], strerror(-rc));
7665                 return rc;
7666         }
7667
7668         rc = llapi_search_mounts(path, 0, mntdir, NULL);
7669         if (rc < 0 || mntdir[0] == '\0') {
7670                 fprintf(stderr,
7671                         "%s %s: cannot find mounted Lustre filesystem: %s\n",
7672                         progname, argv[0],
7673                         (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7674                 return rc;
7675         }
7676
7677         rc = llapi_target_check(num_types, obd_types, path);
7678         if (rc)
7679                 fprintf(stderr, "%s %s: cannot check target '%s': %s\n",
7680                         progname, argv[0], argv[1], strerror(-rc));
7681
7682         return rc;
7683 }
7684
7685 #ifdef HAVE_SYS_QUOTA_H
7686 #define ADD_OVERFLOW(a, b) \
7687                      ((((a) + (b)) < (a)) ? \
7688                       ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7689
7690 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7691  * returns the value or ULONG_MAX on integer overflow or incorrect format
7692  * Notes:
7693  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7694  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7695  *        3. empty integer value is interpreted as 0
7696  */
7697 static unsigned long str2sec(const char *timestr)
7698 {
7699         const char spec[] = "smhdw";
7700         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7701         unsigned long val = 0;
7702         char *tail;
7703
7704         if (strpbrk(timestr, spec) == NULL) {
7705                 /*
7706                  * no specifiers inside the time string,
7707                  * should treat it as an integer value
7708                  */
7709                 val = strtoul(timestr, &tail, 10);
7710                 return *tail ? ULONG_MAX : val;
7711         }
7712
7713         /* format string is XXwXXdXXhXXmXXs */
7714         while (*timestr) {
7715                 unsigned long v;
7716                 int ind;
7717                 char *ptr;
7718
7719                 v = strtoul(timestr, &tail, 10);
7720                 if (v == ULONG_MAX || *tail == '\0')
7721                         /*
7722                          * value too large (ULONG_MAX or more)
7723                          * or missing specifier
7724                          */
7725                         goto error;
7726
7727                 ptr = strchr(spec, *tail);
7728                 if (!ptr)
7729                         /* unknown specifier */
7730                         goto error;
7731
7732                 ind = ptr - spec;
7733
7734                 /* check if product will overflow the type */
7735                 if (!(v < ULONG_MAX / mult[ind]))
7736                         goto error;
7737
7738                 ADD_OVERFLOW(val, mult[ind] * v);
7739                 if (val == ULONG_MAX)
7740                         goto error;
7741
7742                 timestr = tail + 1;
7743         }
7744
7745         return val;
7746
7747 error:
7748         return ULONG_MAX;
7749 }
7750
7751 #define ARG2ULL(nr, str, def_units)                                     \
7752 do {                                                                    \
7753         unsigned long long limit, units = def_units;                    \
7754         int rc;                                                         \
7755                                                                         \
7756         rc = llapi_parse_size(str, &limit, &units, 1);                  \
7757         if (rc < 0) {                                                   \
7758                 fprintf(stderr, "%s: invalid limit '%s'\n",             \
7759                         progname, str);                                 \
7760                 return CMD_HELP;                                        \
7761         }                                                               \
7762         nr = limit;                                                     \
7763 } while (0)
7764
7765 static inline int has_times_option(int argc, char **argv)
7766 {
7767         int i;
7768
7769         for (i = 1; i < argc; i++)
7770                 if (!strcmp(argv[i], "-t"))
7771                         return 1;
7772
7773         return 0;
7774 }
7775
7776 static inline int lfs_verify_poolarg(char *pool)
7777 {
7778         if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7779                 fprintf(stderr,
7780                         "Pool name '%.*s' is longer than %d\n",
7781                         LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7782                 return 1;
7783         }
7784         return 0;
7785 }
7786
7787 /* special grace time, only notify the user when its quota is over soft limit
7788  * but doesn't block new writes until the hard limit is reached.
7789  */
7790 #define NOTIFY_GRACE            "notify"
7791 #define NOTIFY_GRACE_TIME       LQUOTA_GRACE_MASK
7792
7793 #ifndef toqb
7794 static inline __u64 lustre_stoqb(size_t space)
7795 {
7796         return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7797 }
7798 #else
7799 #define lustre_stoqb   toqb
7800 #endif
7801
7802 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7803 {
7804         int c, rc;
7805         char *mnt, *obd_type = (char *)qctl->obd_type;
7806         struct obd_dqblk *dqb = &qctl->qc_dqblk;
7807         struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7808         struct option long_opts[] = {
7809         { .val = 'b',   .name = "block-grace",  .has_arg = required_argument },
7810         { .val = 'g',   .name = "group",        .has_arg = no_argument },
7811         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7812         { .val = 'i',   .name = "inode-grace",  .has_arg = required_argument },
7813         { .val = 'p',   .name = "projid",       .has_arg = no_argument },
7814         { .val = 't',   .name = "times",        .has_arg = no_argument },
7815         { .val = 'u',   .name = "user",         .has_arg = no_argument },
7816         { .val = LFS_POOL_OPT,
7817                         .name = "pool",         .has_arg = required_argument },
7818         { .name = NULL } };
7819         int qtype;
7820
7821         qctl->qc_cmd  = LUSTRE_Q_SETINFO;
7822         qctl->qc_type = ALLQUOTA;
7823
7824         while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7825                                 long_opts, NULL)) != -1) {
7826                 switch (c) {
7827                 case 'u':
7828                         qtype = USRQUOTA;
7829                         goto quota_type;
7830                 case 'g':
7831                         qtype = GRPQUOTA;
7832                         goto quota_type;
7833                 case 'p':
7834                         qtype = PRJQUOTA;
7835 quota_type:
7836                         if (qctl->qc_type != ALLQUOTA) {
7837                                 fprintf(stderr,
7838                                         "%s: -u/g/p cannot be used more than once\n",
7839                                         progname);
7840                                 return CMD_HELP;
7841                         }
7842                         qctl->qc_type = qtype;
7843                         break;
7844                 case 'b':
7845                         if (strncmp(optarg, NOTIFY_GRACE,
7846                                     strlen(NOTIFY_GRACE)) == 0) {
7847                                 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7848                         } else {
7849                                 dqi->dqi_bgrace = str2sec(optarg);
7850                                 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7851                                         fprintf(stderr,
7852                                                 "%s: bad block-grace: %s\n",
7853                                                 progname, optarg);
7854                                         return CMD_HELP;
7855                                 }
7856                         }
7857                         dqb->dqb_valid |= QIF_BTIME;
7858                         break;
7859                 case 'i':
7860                         if (strncmp(optarg, NOTIFY_GRACE,
7861                                     strlen(NOTIFY_GRACE)) == 0) {
7862                                 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7863                         } else {
7864                                 dqi->dqi_igrace = str2sec(optarg);
7865                                 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7866                                         fprintf(stderr,
7867                                                 "%s: bad inode-grace: %s\n",
7868                                                 progname, optarg);
7869                                         return CMD_HELP;
7870                                 }
7871                         }
7872                         dqb->dqb_valid |= QIF_ITIME;
7873                         break;
7874                 case 't': /* Yes, of course! */
7875                         break;
7876                 case LFS_POOL_OPT:
7877                         if (lfs_verify_poolarg(optarg))
7878                                 return -1;
7879                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7880                         qctl->qc_cmd  = LUSTRE_Q_SETINFOPOOL;
7881                         break;
7882                 /* getopt prints error message for us when opterr != 0 */
7883                 default:
7884                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7885                                 progname, argv[optind - 1]);
7886                         fallthrough;
7887                 case 'h':
7888                         return CMD_HELP;
7889                 }
7890         }
7891
7892         if (qctl->qc_type == ALLQUOTA) {
7893                 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7894                         progname);
7895                 return CMD_HELP;
7896         }
7897
7898         if (optind != argc - 1) {
7899                 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7900                         progname, argv[optind + 1]);
7901                 return CMD_HELP;
7902         }
7903
7904         mnt = argv[optind];
7905         rc = llapi_quotactl(mnt, qctl);
7906         if (rc) {
7907                 if (*obd_type)
7908                         fprintf(stderr, "%s %s ", obd_type,
7909                                 obd_uuid2str(&qctl->obd_uuid));
7910                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7911                 return rc;
7912         }
7913
7914         return 0;
7915 }
7916
7917 static int lfs_reset_quota(char *mnt, struct if_quotactl *qctl)
7918 {
7919         struct if_quotactl tmp_qctl;
7920         int index, md_count, dt_count;
7921         int wait_phase = 0, wait_index = 0, wait_count = 0;
7922         int rc, rc2;
7923
7924         /* reset the quota ID, the existing quota setting will be returned */
7925         rc = llapi_quotactl(mnt, qctl);
7926         if (rc)
7927                 return rc;
7928
7929         /* sanity check */
7930         if ((qctl->qc_dqblk.dqb_valid & QIF_LIMITS) != QIF_LIMITS) {
7931                 fprintf(stderr,
7932                         "the existing quota settings are not returned!\n");
7933                 return -EINVAL;
7934         }
7935
7936         rc = llapi_get_obd_count(mnt, &md_count, 1);
7937         if (rc) {
7938                 fprintf(stderr, "can not get mdt count: %s\n", strerror(-rc));
7939                 return rc;
7940         }
7941
7942         rc = llapi_get_obd_count(mnt, &dt_count, 0);
7943         if (rc) {
7944                 fprintf(stderr, "can not get ost count: %s\n", strerror(-rc));
7945                 return rc;
7946         }
7947
7948         memset(&tmp_qctl, 0, sizeof(tmp_qctl));
7949         tmp_qctl.qc_type = qctl->qc_type;
7950         tmp_qctl.qc_id = qctl->qc_id;
7951         tmp_qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7952
7953 retry:
7954         if (wait_phase == 0) {
7955                 for (index = wait_index; index < md_count; index++) {
7956                         tmp_qctl.qc_idx = index;
7957                         tmp_qctl.qc_valid = QC_MDTIDX;
7958                         rc = llapi_quotactl(mnt, &tmp_qctl);
7959                         if (rc == -ENODEV || rc == -ENODATA)
7960                                 continue;
7961                         if (rc) {
7962                                 fprintf(stderr, "quotactl mdt%d failed: %s\n",
7963                                         index, strerror(-rc));
7964                                 break;
7965                         }
7966                         /* check whether the md quota grant is reset */
7967                         if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
7968                             tmp_qctl.qc_dqblk.dqb_ihardlimit != 0)
7969                                 break;
7970                 }
7971
7972                 if (index < md_count) {
7973                         wait_phase = 0;
7974                         wait_index = index;
7975                         goto wait;
7976                 }
7977         } else {
7978                 for (index = wait_index; index < dt_count; index++) {
7979                         tmp_qctl.qc_idx = index;
7980                         tmp_qctl.qc_valid = QC_OSTIDX;
7981                         rc = llapi_quotactl(mnt, &tmp_qctl);
7982                         if (rc == -ENODEV || rc == -ENODATA)
7983                                 continue;
7984                         if (rc) {
7985                                 fprintf(stderr, "quotactl mdt%d failed: %s\n",
7986                                         index, strerror(-rc));
7987                                 break;
7988                         }
7989                         /* check whether the dt quota grant is reset */
7990                         if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
7991                             tmp_qctl.qc_dqblk.dqb_bhardlimit != 0)
7992                                 break;
7993                 }
7994
7995                 if (index < dt_count) {
7996                         wait_phase = 1;
7997                         wait_index = index;
7998                         goto wait;
7999                 }
8000         }
8001
8002         if (wait_phase == 0) {
8003                 wait_phase = 1;
8004                 goto retry;
8005         }
8006
8007         goto out;
8008
8009 wait:
8010         if (rc || wait_count > 30) {
8011                 fprintf(stderr, "fail to reset the quota ID %d on OBDs\n",
8012                         qctl->qc_id);
8013                 goto out;
8014         }
8015
8016         wait_count++;
8017         sleep(1);
8018         fprintf(stdout, "wait %d seconds for OBDs to reset the quota ID %u\n",
8019                 wait_count, qctl->qc_id);
8020         goto retry;
8021
8022
8023 out:
8024         /* restore the quota setting */
8025         if (qctl->qc_dqblk.dqb_isoftlimit == 0 &&
8026             qctl->qc_dqblk.dqb_ihardlimit == 0 &&
8027             qctl->qc_dqblk.dqb_bsoftlimit == 0 &&
8028             qctl->qc_dqblk.dqb_bhardlimit == 0)
8029                 return rc;
8030
8031         memcpy(&tmp_qctl, qctl, sizeof(tmp_qctl));
8032         tmp_qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
8033         rc2 = llapi_quotactl(mnt, &tmp_qctl);
8034         if (!rc2)
8035                 return rc;
8036
8037         fprintf(stderr,
8038                 "fail to restore the quota setting: %s, please restore it manually by\n  lfs setquota %s %d",
8039                 strerror(-rc2),
8040                 qctl->qc_type == USRQUOTA ? "-u" :
8041                                 (qctl->qc_type == GRPQUOTA ? "-g" : "-p"),
8042                 qctl->qc_id);
8043
8044         if (qctl->qc_dqblk.dqb_isoftlimit != 0)
8045                 fprintf(stderr, " -i %llu",
8046                         (unsigned long long)qctl->qc_dqblk.dqb_isoftlimit);
8047         if (qctl->qc_dqblk.dqb_ihardlimit != 0)
8048                 fprintf(stderr, " -I %llu",
8049                         (unsigned long long)qctl->qc_dqblk.dqb_ihardlimit);
8050         if (qctl->qc_dqblk.dqb_bsoftlimit != 0)
8051                 fprintf(stderr, " -b %llu",
8052                         (unsigned long long)qctl->qc_dqblk.dqb_bsoftlimit);
8053         if (qctl->qc_dqblk.dqb_bhardlimit != 0)
8054                 fprintf(stderr, " -B %llu",
8055                         (unsigned long long)qctl->qc_dqblk.dqb_bhardlimit);
8056
8057         fprintf(stderr, " %s\n", mnt);
8058         if (!rc)
8059                 rc = rc2;
8060
8061         return rc;
8062 }
8063
8064 #define BSLIMIT (1 << 0)
8065 #define BHLIMIT (1 << 1)
8066 #define ISLIMIT (1 << 2)
8067 #define IHLIMIT (1 << 3)
8068
8069 int lfs_setquota(int argc, char **argv)
8070 {
8071         int c, rc = 0;
8072         struct if_quotactl *qctl;
8073         char *mnt, *obd_type;
8074         struct obd_dqblk *dqb;
8075         struct option long_opts[] = {
8076         { .val = 'b',   .name = "block-softlimit",
8077                                                 .has_arg = required_argument },
8078         { .val = 'B',   .name = "block-hardlimit",
8079                                                 .has_arg = required_argument },
8080         { .val = 'd',   .name = "default",      .has_arg = no_argument },
8081         { .val = LFS_SETQUOTA_DELETE,
8082                         .name = "delete",       .has_arg = no_argument },
8083         { .val = 'g',   .name = "group",        .has_arg = required_argument },
8084         { .val = 'G',   .name = "default-grp",  .has_arg = no_argument },
8085         { .val = 'h',   .name = "help",         .has_arg = no_argument },
8086         { .val = 'i',   .name = "inode-softlimit",
8087                                                 .has_arg = required_argument },
8088         { .val = 'I',   .name = "inode-hardlimit",
8089                                                 .has_arg = required_argument },
8090         { .val = 'p',   .name = "projid",       .has_arg = required_argument },
8091         { .val = 'P',   .name = "default-prj",  .has_arg = no_argument },
8092         { .val = 'r',   .name = "reset",        .has_arg = no_argument },
8093         { .val = 'u',   .name = "user",         .has_arg = required_argument },
8094         { .val = 'U',   .name = "default-usr",  .has_arg = no_argument },
8095         { .val = LFS_POOL_OPT,
8096                         .name = "pool",         .has_arg = required_argument },
8097         { .name = NULL } };
8098         unsigned int limit_mask = 0;
8099         bool use_default = false;
8100         int qtype, qctl_len;
8101
8102         qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
8103         qctl = malloc(qctl_len);
8104         if (!qctl)
8105                 return -ENOMEM;
8106
8107         memset(qctl, 0, qctl_len);
8108         obd_type = (char *)qctl->obd_type;
8109         dqb = &qctl->qc_dqblk;
8110
8111         if (has_times_option(argc, argv)) {
8112                 rc = lfs_setquota_times(argc, argv, qctl);
8113                 goto out;
8114         }
8115
8116         qctl->qc_cmd  = LUSTRE_Q_SETQUOTA;
8117         qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
8118                                    * so it can be used as a marker that qc_type
8119                                    * isn't reinitialized from command line
8120                                    */
8121         while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pru:U",
8122                 long_opts, NULL)) != -1) {
8123                 switch (c) {
8124                 case 'U':
8125                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8126                         qtype = USRQUOTA;
8127                         qctl->qc_id = 0;
8128                         goto quota_type_def;
8129                 case 'u':
8130                         qtype = USRQUOTA;
8131                         rc = name2uid(&qctl->qc_id, optarg);
8132                         goto quota_type;
8133                 case 'G':
8134                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8135                         qtype = GRPQUOTA;
8136                         qctl->qc_id = 0;
8137                         goto quota_type_def;
8138                 case 'g':
8139                         qtype = GRPQUOTA;
8140                         rc = name2gid(&qctl->qc_id, optarg);
8141                         goto quota_type;
8142                 case 'P':
8143                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8144                         qtype = PRJQUOTA;
8145                         qctl->qc_id = 0;
8146                         goto quota_type_def;
8147                 case 'p':
8148                         qtype = PRJQUOTA;
8149                         rc = name2projid(&qctl->qc_id, optarg);
8150 quota_type:
8151                         if (rc) {
8152                                 if (str2quotaid(&qctl->qc_id, optarg)) {
8153                                         fprintf(stderr,
8154                                                 "%s setquota: invalid id '%s'\n",
8155                                                 progname, optarg);
8156                                         rc = -1;
8157                                         goto out;
8158                                 }
8159                         }
8160
8161                         if (qctl->qc_id == 0) {
8162                                 fprintf(stderr,
8163                                         "%s setquota: can't set quota for root usr/group/project.\n",
8164                                         progname);
8165                                 rc = -1;
8166                                 goto out;
8167                         }
8168
8169 quota_type_def:
8170                         if (qctl->qc_type != ALLQUOTA) {
8171                                 fprintf(stderr,
8172                                         "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
8173                                         progname);
8174                                 rc = CMD_HELP;
8175                                 goto out;
8176                         }
8177                         qctl->qc_type = qtype;
8178                         break;
8179 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
8180                 case 'd':
8181                         fprintf(stderr,
8182                                 "%s setquota: '-d' deprecated, use '-D' or '--default'\n",
8183                                 progname);
8184                         fallthrough;
8185 #endif
8186                 case 'D':
8187                         use_default = true;
8188                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8189                         break;
8190                 case LFS_SETQUOTA_DELETE:
8191                         qctl->qc_cmd = LUSTRE_Q_DELETEQID;
8192                         break;
8193                 case 'b':
8194                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
8195                         dqb->dqb_bsoftlimit >>= 10;
8196                         limit_mask |= BSLIMIT;
8197                         if (dqb->dqb_bsoftlimit &&
8198                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
8199                                 fprintf(stderr,
8200                                         "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8201                                         progname,
8202                                         (unsigned long long)dqb->dqb_bsoftlimit,
8203                                         progname);
8204                         break;
8205                 case 'B':
8206                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
8207                         dqb->dqb_bhardlimit >>= 10;
8208                         limit_mask |= BHLIMIT;
8209                         if (dqb->dqb_bhardlimit &&
8210                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
8211                                 fprintf(stderr,
8212                                         "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
8213                                         "See '%s help setquota' or Lustre manual for details\n",
8214                                         progname,
8215                                         (unsigned long long)dqb->dqb_bhardlimit,
8216                                         progname);
8217                         break;
8218                 case 'i':
8219                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
8220                         limit_mask |= ISLIMIT;
8221                         if (dqb->dqb_isoftlimit &&
8222                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
8223                                 fprintf(stderr,
8224                                         "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8225                                         progname,
8226                                         (unsigned long long)dqb->dqb_isoftlimit,
8227                                         progname);
8228                         break;
8229                 case 'I':
8230                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
8231                         limit_mask |= IHLIMIT;
8232                         if (dqb->dqb_ihardlimit &&
8233                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
8234                                 fprintf(stderr,
8235                                         "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8236                                         progname,
8237                                         (unsigned long long)dqb->dqb_ihardlimit,
8238                                         progname);
8239                         break;
8240                 case LFS_POOL_OPT:
8241                         if (lfs_verify_poolarg(optarg)) {
8242                                 rc = -1;
8243                                 goto out;
8244                         }
8245                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8246                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
8247                                                 LUSTRE_Q_SETDEFAULT_POOL :
8248                                                 LUSTRE_Q_SETQUOTAPOOL;
8249                         break;
8250                 case 'r':
8251                         qctl->qc_cmd = LUSTRE_Q_RESETQID;
8252                         break;
8253                 default:
8254                         fprintf(stderr,
8255                                 "%s setquota: unrecognized option '%s'\n",
8256                                 progname, argv[optind - 1]);
8257                         fallthrough;
8258                 case 'h':
8259                         rc = CMD_HELP;
8260                         goto out;
8261                 }
8262         }
8263
8264         if (qctl->qc_type == ALLQUOTA) {
8265                 fprintf(stderr,
8266                         "%s setquota: either -u or -g must be specified\n",
8267                         progname);
8268                 rc = CMD_HELP;
8269                 goto out;
8270         }
8271
8272         if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
8273             qctl->qc_cmd != LUSTRE_Q_RESETQID && limit_mask == 0) {
8274                 fprintf(stderr,
8275                         "%s setquota: at least one limit must be specified\n",
8276                         progname);
8277                 rc = CMD_HELP;
8278                 goto out;
8279         }
8280
8281         if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8282              qctl->qc_cmd == LUSTRE_Q_RESETQID) && limit_mask != 0) {
8283                 fprintf(stderr,
8284                         "%s setquota: limits should not be specified when using default quota, deleting or resetting quota ID\n",
8285                         progname);
8286                 rc = CMD_HELP;
8287                 goto out;
8288         }
8289
8290         if (use_default && qctl->qc_id == 0) {
8291                 fprintf(stderr,
8292                         "%s setquota: can not set default quota for root user/group/project\n",
8293                         progname);
8294                 rc = CMD_HELP;
8295                 goto out;
8296         }
8297
8298         if ((qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8299              qctl->qc_cmd == LUSTRE_Q_RESETQID)  && qctl->qc_id == 0) {
8300                 fprintf(stderr,
8301                         "%s setquota: can not delete or reset root user/group/project\n",
8302                         progname);
8303                 rc = CMD_HELP;
8304                 goto out;
8305         }
8306
8307         if (optind != argc - 1) {
8308                 fprintf(stderr,
8309                         "%s setquota: filesystem not specified or unexpected argument '%s'\n",
8310                         progname, argv[optind]);
8311                 rc = CMD_HELP;
8312                 goto out;
8313         }
8314
8315         mnt = argv[optind];
8316
8317         if (use_default) {
8318                 dqb->dqb_bhardlimit = 0;
8319                 dqb->dqb_bsoftlimit = 0;
8320                 dqb->dqb_ihardlimit = 0;
8321                 dqb->dqb_isoftlimit = 0;
8322                 dqb->dqb_itime = 0;
8323                 dqb->dqb_btime = 0;
8324                 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
8325                 /* do not set inode limits for Pool Quotas */
8326                 if (qctl->qc_cmd  == LUSTRE_Q_SETDEFAULT_POOL)
8327                         dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
8328         } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
8329                    (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
8330                 /* sigh, we can't just set blimits/ilimits */
8331                 struct if_quotactl *tmp_qctl;
8332
8333                 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8334                 if (!tmp_qctl)
8335                         goto out;
8336
8337                 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
8338                         tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8339                         strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
8340                                 LOV_MAXPOOLNAME);
8341                 } else {
8342                         tmp_qctl->qc_cmd  = LUSTRE_Q_GETQUOTA;
8343                 }
8344                 tmp_qctl->qc_type = qctl->qc_type;
8345                 tmp_qctl->qc_id = qctl->qc_id;
8346
8347                 rc = llapi_quotactl(mnt, tmp_qctl);
8348                 if (rc < 0) {
8349                         free(tmp_qctl);
8350                         goto out;
8351                 }
8352
8353                 if (!(limit_mask & BHLIMIT))
8354                         dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
8355                 if (!(limit_mask & BSLIMIT))
8356                         dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
8357                 if (!(limit_mask & IHLIMIT))
8358                         dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
8359                 if (!(limit_mask & ISLIMIT))
8360                         dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
8361
8362                 /* Keep grace times if we have got no softlimit arguments */
8363                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
8364                         dqb->dqb_valid |= QIF_BTIME;
8365                         dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
8366                 }
8367
8368                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
8369                         dqb->dqb_valid |= QIF_ITIME;
8370                         dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
8371                 }
8372                 free(tmp_qctl);
8373         }
8374
8375         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
8376         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
8377
8378         if (qctl->qc_cmd == LUSTRE_Q_RESETQID)
8379                 rc = lfs_reset_quota(mnt, qctl);
8380         else
8381                 rc = llapi_quotactl(mnt, qctl);
8382
8383         if (rc) {
8384                 if (*obd_type)
8385                         fprintf(stderr,
8386                                 "%s setquota: cannot quotactl '%s' '%s': %s\n",
8387                                 progname, obd_type,
8388                                 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
8389                 else
8390                         fprintf(stderr,
8391                                 "%s setquota: quotactl failed: %s\n",
8392                                 progname, strerror(-rc));
8393         }
8394 out:
8395         if (rc)
8396                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8397
8398         free(qctl);
8399         return rc;
8400 }
8401
8402 /* Converts seconds value into format string
8403  * result is returned in buf
8404  * Notes:
8405  *        1. result is in descenting order: 1w2d3h4m5s
8406  *        2. zero fields are not filled (except for p. 3): 5d1s
8407  *        3. zero seconds value is presented as "0s"
8408  */
8409 static char *__sec2str(time_t seconds, char *buf)
8410 {
8411         const char spec[] = "smhdw";
8412         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8413         unsigned long c;
8414         char *tail = buf;
8415         int i;
8416
8417         for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8418                 c = seconds / mult[i];
8419
8420                 if (c > 0 || (i == 0 && buf == tail))
8421                         tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8422                                           spec[i]);
8423
8424                 seconds %= mult[i];
8425         }
8426
8427         return tail;
8428 }
8429
8430 static void sec2str(time_t seconds, char *buf, int rc)
8431 {
8432         char *tail = buf;
8433
8434         if (rc)
8435                 *tail++ = '[';
8436
8437         tail = __sec2str(seconds, tail);
8438
8439         if (rc && tail - buf < 39) {
8440                 *tail++ = ']';
8441                 *tail++ = 0;
8442         }
8443 }
8444
8445 static void diff2str(time_t seconds, char *buf, time_t now)
8446 {
8447         buf[0] = 0;
8448         if (!seconds)
8449                 return;
8450         if (seconds <= now) {
8451                 strcpy(buf, "expired");
8452                 return;
8453         }
8454         __sec2str(seconds - now, buf);
8455 }
8456
8457 static void print_quota_title(char *name, struct if_quotactl *qctl,
8458                               bool human_readable, bool show_default)
8459 {
8460         if (show_default) {
8461                 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8462                 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8463                        "Filesystem", "bquota", "blimit", "bgrace",
8464                        "iquota", "ilimit", "igrace");
8465         } else {
8466                 printf("Disk quotas for %s %s (%cid %u):\n",
8467                        qtype_name(qctl->qc_type), name,
8468                        *qtype_name(qctl->qc_type), qctl->qc_id);
8469                 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8470                        "Filesystem", human_readable ? "used" : "kbytes",
8471                        "quota", "limit", "grace",
8472                        "files", "quota", "limit", "grace");
8473         }
8474 }
8475
8476 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8477 {
8478         if (!h) {
8479                 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8480         } else {
8481                 if (num >> 40)
8482                         snprintf(buf, buflen, "%5.4gP",
8483                                  (double)num / ((__u64)1 << 40));
8484                 else if (num >> 30)
8485                         snprintf(buf, buflen, "%5.4gT",
8486                                  (double)num / (1 << 30));
8487                 else if (num >> 20)
8488                         snprintf(buf, buflen, "%5.4gG",
8489                                  (double)num / (1 << 20));
8490                 else if (num >> 10)
8491                         snprintf(buf, buflen, "%5.4gM",
8492                                  (double)num / (1 << 10));
8493                 else
8494                         snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8495         }
8496 }
8497
8498 #ifdef HAVE_NATIVE_CLIENT
8499 /* In the current Lustre implementation, the grace time is either the time
8500  * or the timestamp to be used after some quota ID exceeds the soft limt,
8501  * 48 bits should be enough, its high 16 bits can be used as quota flags.
8502  */
8503 #define LQUOTA_GRACE_BITS       48
8504 #define LQUOTA_GRACE_MASK       ((1ULL << LQUOTA_GRACE_BITS) - 1)
8505 #define LQUOTA_GRACE_MAX        LQUOTA_GRACE_MASK
8506 #define LQUOTA_GRACE(t)         (t & LQUOTA_GRACE_MASK)
8507 #define LQUOTA_FLAG(t)          (t >> LQUOTA_GRACE_BITS)
8508 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8509 #endif
8510
8511 #define STRBUF_LEN      24
8512 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8513                         int rc, bool h, bool show_default)
8514 {
8515         time_t now;
8516
8517         time(&now);
8518
8519         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8520             qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8521             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8522             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8523                 int bover = 0, iover = 0;
8524                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8525                 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8526                 char timebuf[40];
8527                 char strbuf[STRBUF_LEN];
8528
8529                 dqb->dqb_btime &= LQUOTA_GRACE_MASK;
8530                 dqb->dqb_itime &= LQUOTA_GRACE_MASK;
8531
8532                 if (dqb->dqb_bhardlimit &&
8533                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8534                         bover = 1;
8535                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8536                         if (dqb->dqb_btime > now)
8537                                 bover = 2;
8538                         else
8539                                 bover = 3;
8540                 }
8541
8542                 if (dqb->dqb_ihardlimit &&
8543                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8544                         iover = 1;
8545                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8546                         if (dqb->dqb_itime > now)
8547                                 iover = 2;
8548                         else
8549                                 iover = 3;
8550                 }
8551
8552                 if (strlen(mnt) > 15)
8553                         printf("%s\n%15s", mnt, "");
8554                 else
8555                         printf("%15s", mnt);
8556
8557                 if (show_default)
8558                         snprintf(timebuf, sizeof(timebuf), "%llu",
8559                                  (unsigned long long)dqb->dqb_btime);
8560                 else if (bover)
8561                         diff2str(dqb->dqb_btime, timebuf, now);
8562
8563                 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8564                            strbuf, sizeof(strbuf), h);
8565                 if (rc == -EREMOTEIO)
8566                         sprintf(numbuf[0], "%s*", strbuf);
8567                 else
8568                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8569                                 "%s" : "[%s]", strbuf);
8570
8571                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8572                 if (type == QC_GENERAL)
8573                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8574                                 "%s" : "[%s]", strbuf);
8575                 else
8576                         sprintf(numbuf[1], "%s", "-");
8577
8578                 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8579                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8580                         "%s" : "[%s]", strbuf);
8581
8582                 if (show_default)
8583                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8584                 else
8585                         printf(" %7s%c %6s %7s %7s",
8586                                numbuf[0], bover ? '*' : ' ', numbuf[1],
8587                                numbuf[2], bover > 1 ? timebuf : "-");
8588
8589                 if (show_default)
8590                         snprintf(timebuf, sizeof(timebuf), "%llu",
8591                                  (unsigned long long)dqb->dqb_itime);
8592                 else if (iover)
8593                         diff2str(dqb->dqb_itime, timebuf, now);
8594
8595                 snprintf(numbuf[0], sizeof(numbuf),
8596                          (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8597                          (uintmax_t)dqb->dqb_curinodes);
8598
8599                 if (type == QC_GENERAL)
8600                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8601                                 "%ju" : "[%ju]",
8602                                 (uintmax_t)dqb->dqb_isoftlimit);
8603                 else
8604                         sprintf(numbuf[1], "%s", "-");
8605
8606                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8607                         "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8608
8609                 if (show_default)
8610                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8611                 else if (type != QC_OSTIDX)
8612                         printf(" %7s%c %6s %7s %7s",
8613                                numbuf[0], iover ? '*' : ' ', numbuf[1],
8614                                numbuf[2], iover > 1 ? timebuf : "-");
8615                 else
8616                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8617                 printf("\n");
8618         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8619                    qctl->qc_cmd == Q_GETOINFO) {
8620                 char bgtimebuf[40];
8621                 char igtimebuf[40];
8622
8623                 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8624                         strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8625                 else
8626                         sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8627                 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8628                         strncpy(igtimebuf, NOTIFY_GRACE, 40);
8629                 else
8630                         sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8631
8632                 printf("Block grace time: %s; Inode grace time: %s\n",
8633                        bgtimebuf, igtimebuf);
8634         }
8635 }
8636
8637 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8638 {
8639         char *dash, *endp;
8640
8641         /* format is "lustre-OST0001" */
8642         dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8643         if (!dash) {
8644                 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8645                 return -EINVAL;
8646         }
8647         dash += 4;
8648
8649         *idx = strtoul(dash, &endp, 16);
8650         if (*idx > 0xffff) {
8651                 fprintf(stderr, "wrong index %s\n", tgtname);
8652                 return -ERANGE;
8653         }
8654
8655         return 0;
8656 }
8657
8658 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8659                            bool h, __u64 *total)
8660 {
8661         int rc = 0, rc1 = 0, count = 0, i = 0;
8662         char **list = NULL, *buffer = NULL;
8663         __u32 valid = qctl->qc_valid;
8664
8665         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8666                 return 0;
8667
8668         /* Is it correct for the case OST0000, OST0002, OST0003 -
8669          * we will ask OST0001 that is absent and won't ask OST0003? */
8670         rc = llapi_get_obd_count(mnt, &count, is_mdt);
8671         if (rc) {
8672                 fprintf(stderr, "can not get %s count: %s\n",
8673                         is_mdt ? "mdt" : "ost", strerror(-rc));
8674                 return rc;
8675         }
8676
8677         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8678                 char fname[PATH_MAX];
8679                 char fsname[LUSTRE_MAXFSNAME + 1];
8680                 int bufsize = sizeof(struct obd_uuid) * count;
8681
8682                 rc = llapi_search_fsname(mnt, fsname);
8683                 if (rc) {
8684                         fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8685                                 mnt);
8686                         goto out;
8687                 }
8688                 buffer = malloc(bufsize + sizeof(*list) * count);
8689                 if (!buffer)
8690                         return -ENOMEM;
8691                 list = (char **)(buffer + bufsize);
8692                 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8693                 count = llapi_get_poolmembers(fname, list, count,
8694                                               buffer, bufsize);
8695                 if (count <= 0)
8696                         goto out;
8697         }
8698
8699         for (i = 0; i < count; i++) {
8700                 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8701                         unsigned int index;
8702
8703                         if (tgt_name2index(list[i], &index))
8704                                 continue;
8705                         qctl->qc_idx = index;
8706                 } else {
8707                         qctl->qc_idx = i;
8708                 }
8709
8710                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8711                 rc = llapi_quotactl(mnt, qctl);
8712                 if (rc) {
8713                         /* It is remote client case. */
8714                         if (rc == -EOPNOTSUPP) {
8715                                 rc = 0;
8716                                 goto out;
8717                         }
8718
8719                         /* no target for this index yet */
8720                         if (rc == -ENODEV) {
8721                                 rc = 0;
8722                                 continue;
8723                         }
8724
8725                         /* inactive target */
8726                         if (rc == -ENODATA) {
8727                                 char name[UUID_MAX+8];
8728
8729                                 snprintf(name, sizeof(name), "%s[inact]",
8730                                         obd_uuid2str(&qctl->obd_uuid));
8731                                 memset(&qctl->qc_dqinfo, 0,
8732                                        sizeof(qctl->qc_dqinfo));
8733                                 memset(&qctl->qc_dqblk, 0,
8734                                        sizeof(qctl->qc_dqblk));
8735                                 print_quota(name, qctl, qctl->qc_valid, 0, h,
8736                                             false);
8737                                 rc = 0;
8738                                 continue;
8739                         }
8740
8741                         if (!rc1)
8742                                 rc1 = rc;
8743                         fprintf(stderr, "quotactl %s%d failed.\n",
8744                                 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8745                         continue;
8746                 }
8747
8748                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8749                             qctl->qc_valid, 0, h, false);
8750                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8751                                    qctl->qc_dqblk.dqb_bhardlimit;
8752         }
8753 out:
8754         if (buffer)
8755                 free(buffer);
8756         qctl->qc_valid = valid;
8757         return rc ? : rc1;
8758 }
8759
8760 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8761                            int verbose, int quiet, bool human_readable,
8762                            bool show_default)
8763 {
8764         int rc1 = 0, rc2 = 0, rc3 = 0;
8765         char *obd_type = (char *)qctl->obd_type;
8766         char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8767         __u64 total_ialloc = 0, total_balloc = 0;
8768         bool use_default_for_blk = false;
8769         bool use_default_for_file = false;
8770         int inacc;
8771
8772         rc1 = llapi_quotactl(mnt, qctl);
8773         if (rc1 < 0) {
8774                 switch (rc1) {
8775                 case -ESRCH:
8776                         fprintf(stderr, "%s quotas are not enabled.\n",
8777                                 qtype_name(qctl->qc_type));
8778                         goto out;
8779                 case -EPERM:
8780                         fprintf(stderr, "Permission denied.\n");
8781                 case -ENODEV:
8782                 case -ENOENT:
8783                         /* We already got error message. */
8784                         goto out;
8785                 default:
8786                         fprintf(stderr, "Unexpected quotactl error: %s\n",
8787                                 strerror(-rc1));
8788                 }
8789         }
8790
8791         if (!show_default && qctl->qc_id == 0) {
8792                 qctl->qc_dqblk.dqb_bhardlimit = 0;
8793                 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8794                 qctl->qc_dqblk.dqb_ihardlimit = 0;
8795                 qctl->qc_dqblk.dqb_isoftlimit = 0;
8796                 qctl->qc_dqblk.dqb_btime = 0;
8797                 qctl->qc_dqblk.dqb_itime = 0;
8798                 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8799         }
8800
8801         if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8802             LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8803                 use_default_for_blk = true;
8804                 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8805         }
8806
8807         if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8808             LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8809                 use_default_for_file = true;
8810                 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8811         }
8812
8813         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8814              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8815              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8816              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8817                 print_quota_title(name, qctl, human_readable, show_default);
8818
8819         if (rc1 && *obd_type)
8820                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8821
8822         if (qctl->qc_valid != QC_GENERAL)
8823                 mnt = "";
8824
8825         inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8826                  qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8827                 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8828                  (QIF_LIMITS|QIF_USAGE));
8829
8830         print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8831
8832         if (!show_default && verbose &&
8833             qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8834             qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8835                 char strbuf[STRBUF_LEN];
8836
8837                 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8838                                       &total_ialloc);
8839                 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8840                                       &total_balloc);
8841                 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8842                            human_readable);
8843                 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8844                        (uintmax_t)total_ialloc, strbuf);
8845         }
8846
8847         if (use_default_for_blk)
8848                 printf("%cid %u is using default block quota setting\n",
8849                        *qtype_name(qctl->qc_type), qctl->qc_id);
8850
8851         if (use_default_for_file)
8852                 printf("%cid %u is using default file quota setting\n",
8853                        *qtype_name(qctl->qc_type), qctl->qc_id);
8854
8855         if (rc1 || rc2 || rc3 || inacc)
8856                 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8857 out:
8858         if (rc1)
8859                 return rc1;
8860         if (rc2)
8861                 return rc2;
8862         if (rc3)
8863                 return rc3;
8864         if (inacc)
8865                 return -EIO;
8866
8867         return 0;
8868 }
8869
8870 static int lfs_project(int argc, char **argv)
8871 {
8872         int ret = 0, err = 0, c, i;
8873         struct project_handle_control phc = { 0 };
8874         enum lfs_project_ops_t op;
8875
8876         phc.newline = true;
8877         phc.assign_projid = false;
8878         /* default action */
8879         op = LFS_PROJECT_LIST;
8880
8881         while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8882                 switch (c) {
8883                 case 'c':
8884                         if (op != LFS_PROJECT_LIST) {
8885                                 fprintf(stderr,
8886                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8887                                         progname);
8888                                 return CMD_HELP;
8889                         }
8890
8891                         op = LFS_PROJECT_CHECK;
8892                         break;
8893                 case 'C':
8894                         if (op != LFS_PROJECT_LIST) {
8895                                 fprintf(stderr,
8896                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8897                                         progname);
8898                                 return CMD_HELP;
8899                         }
8900
8901                         op = LFS_PROJECT_CLEAR;
8902                         break;
8903                 case 's':
8904                         if (op != LFS_PROJECT_LIST) {
8905                                 fprintf(stderr,
8906                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8907                                         progname);
8908                                 return CMD_HELP;
8909                         }
8910
8911                         phc.set_inherit = true;
8912                         op = LFS_PROJECT_SET;
8913                         break;
8914                 case 'd':
8915                         phc.dironly = true;
8916                         break;
8917                 case 'k':
8918                         phc.keep_projid = true;
8919                         break;
8920                 case 'r':
8921                         phc.recursive = true;
8922                         break;
8923                 case 'p':
8924                         if (str2quotaid(&phc.projid, optarg)) {
8925                                 fprintf(stderr,
8926                                         "Invalid project ID: %s\n",
8927                                         optarg);
8928                                 return CMD_HELP;
8929                         }
8930
8931                         phc.assign_projid = true;
8932
8933                         break;
8934                 case '0':
8935                         phc.newline = false;
8936                         break;
8937                 default:
8938                         fprintf(stderr, "%s: invalid option '%c'\n",
8939                                 progname, optopt);
8940                         return CMD_HELP;
8941                 }
8942         }
8943
8944         if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8945                 op = LFS_PROJECT_SET;
8946                 phc.set_projid = true;
8947         } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8948                 phc.set_projid = true;
8949         }
8950
8951         switch (op) {
8952         case LFS_PROJECT_CHECK:
8953                 if (phc.keep_projid) {
8954                         fprintf(stderr,
8955                                 "%s: '-k' is useless together with '-c'\n",
8956                                 progname);
8957                         return CMD_HELP;
8958                 }
8959                 break;
8960         case LFS_PROJECT_CLEAR:
8961                 if (!phc.newline) {
8962                         fprintf(stderr,
8963                                 "%s: '-0' is useless together with '-C'\n",
8964                                 progname);
8965                         return CMD_HELP;
8966                 }
8967                 if (phc.assign_projid) {
8968                         fprintf(stderr,
8969                                 "%s: '-p' is useless together with '-C'\n",
8970                                 progname);
8971                         return CMD_HELP;
8972                 }
8973                 break;
8974         case LFS_PROJECT_SET:
8975                 if (!phc.newline) {
8976                         fprintf(stderr,
8977                                 "%s: '-0' is useless together with '-s'\n",
8978                                 progname);
8979                         return CMD_HELP;
8980                 }
8981                 if (phc.keep_projid) {
8982                         fprintf(stderr,
8983                                 "%s: '-k' is useless together with '-s'\n",
8984                                 progname);
8985                         return CMD_HELP;
8986                 }
8987                 break;
8988         default:
8989                 if (!phc.newline) {
8990                         fprintf(stderr,
8991                                 "%s: '-0' is useless for list operations\n",
8992                                 progname);
8993                         return CMD_HELP;
8994                 }
8995                 break;
8996         }
8997
8998         argv += optind;
8999         argc -= optind;
9000         if (argc == 0) {
9001                 fprintf(stderr, "%s: missing file or directory target(s)\n",
9002                         progname);
9003                 return CMD_HELP;
9004         }
9005
9006         for (i = 0; i < argc; i++) {
9007                 switch (op) {
9008                 case LFS_PROJECT_CHECK:
9009                         err = lfs_project_check(argv[i], &phc);
9010                         break;
9011                 case LFS_PROJECT_LIST:
9012                         err = lfs_project_list(argv[i], &phc);
9013                         break;
9014                 case LFS_PROJECT_CLEAR:
9015                         err = lfs_project_clear(argv[i], &phc);
9016                         break;
9017                 case LFS_PROJECT_SET:
9018                         err = lfs_project_set(argv[i], &phc);
9019                         break;
9020                 default:
9021                         break;
9022                 }
9023                 if (err && !ret)
9024                         ret = err;
9025         }
9026
9027         return ret;
9028 }
9029
9030 static int lfs_quota(int argc, char **argv)
9031 {
9032         int c;
9033         char *mnt, *name = NULL;
9034         struct if_quotactl *qctl;
9035         char *obd_uuid;
9036         int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
9037         __u32 valid = QC_GENERAL, idx = 0;
9038         bool human_readable = false;
9039         bool show_default = false;
9040         int qtype;
9041         bool show_pools = false;
9042         struct option long_opts[] = {
9043         { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
9044         { .name = NULL } };
9045         char **poollist = NULL;
9046         char *buf = NULL;
9047         int poolcount, i;
9048
9049         qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
9050         if (!qctl)
9051                 return -ENOMEM;
9052
9053         qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
9054         qctl->qc_type = ALLQUOTA;
9055         obd_uuid = (char *)qctl->obd_uuid.uuid;
9056
9057         while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
9058                 long_opts, NULL)) != -1) {
9059                 switch (c) {
9060                 case 'U':
9061                         show_default = true;
9062                 case 'u':
9063                         qtype = USRQUOTA;
9064                         goto quota_type;
9065                 case 'G':
9066                         show_default = true;
9067                 case 'g':
9068                         qtype = GRPQUOTA;
9069                         goto quota_type;
9070                 case 'P':
9071                         show_default = true;
9072                 case 'p':
9073                         qtype = PRJQUOTA;
9074 quota_type:
9075                         if (qctl->qc_type != ALLQUOTA) {
9076                                 fprintf(stderr,
9077                                         "%s quota: only one of -u, -g, or -p may be specified\n",
9078                                         progname);
9079                                 rc = CMD_HELP;
9080                                 goto out;
9081                         }
9082                         qctl->qc_type = qtype;
9083                         break;
9084                 case 't':
9085                         qctl->qc_cmd = LUSTRE_Q_GETINFO;
9086                         break;
9087                 case 'o':
9088                         valid = qctl->qc_valid = QC_UUID;
9089                         snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
9090                         break;
9091                 case 'i':
9092                         valid = qctl->qc_valid = QC_MDTIDX;
9093                         idx = qctl->qc_idx = atoi(optarg);
9094                         if (idx == 0 && *optarg != '0') {
9095                                 fprintf(stderr,
9096                                         "%s quota: invalid MDT index '%s'\n",
9097                                         progname, optarg);
9098                                 rc = CMD_HELP;
9099                                 goto out;
9100                         }
9101                         break;
9102                 case 'I':
9103                         valid = qctl->qc_valid = QC_OSTIDX;
9104                         idx = qctl->qc_idx = atoi(optarg);
9105                         if (idx == 0 && *optarg != '0') {
9106                                 fprintf(stderr,
9107                                         "%s quota: invalid OST index '%s'\n",
9108                                         progname, optarg);
9109                                 rc = CMD_HELP;
9110                                 goto out;
9111                         }
9112                         break;
9113                 case 'v':
9114                         verbose = 1;
9115                         break;
9116                 case 'q':
9117                         quiet = 1;
9118                         break;
9119                 case 'h':
9120                         human_readable = true;
9121                         break;
9122                 case LFS_POOL_OPT:
9123                         if ((!optarg) && (argv[optind] != NULL) &&
9124                                 (argv[optind][0] != '-') &&
9125                                 (argv[optind][0] != '/')) {
9126                                 optarg = argv[optind++];
9127                                 if (lfs_verify_poolarg(optarg)) {
9128                                         rc = -EINVAL;
9129                                         goto out;
9130                                 }
9131                                 strncpy(qctl->qc_poolname, optarg,
9132                                         LOV_MAXPOOLNAME);
9133                                 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
9134                                         qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
9135                                 else
9136                                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9137                                 break;
9138                         }
9139
9140                         /* optarg is NULL */
9141                         show_pools = true;
9142                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9143                         break;
9144                 default:
9145                         fprintf(stderr, "%s quota: unrecognized option '%s'\n",
9146                                 progname, argv[optind - 1]);
9147                         rc = CMD_HELP;
9148                         goto out;
9149                 }
9150         }
9151
9152         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
9153         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9154              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
9155              qctl->qc_type == ALLQUOTA &&
9156              optind == argc - 1 && !show_default) {
9157                 qctl->qc_idx = idx;
9158
9159                 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
9160                         qctl->qc_type = qtype;
9161                         qctl->qc_valid = valid;
9162                         if (qtype == USRQUOTA) {
9163                                 qctl->qc_id = geteuid();
9164                                 rc = uid2name(&name, qctl->qc_id);
9165                         } else {
9166                                 qctl->qc_id = getegid();
9167                                 rc = gid2name(&name, qctl->qc_id);
9168                                 memset(&qctl->qc_dqblk, 0,
9169                                        sizeof(qctl->qc_dqblk));
9170                         }
9171                         if (rc)
9172                                 name = "<unknown>";
9173                         mnt = argv[optind];
9174                         rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
9175                                               human_readable, show_default);
9176                         if (rc1 && !rc)
9177                                 rc = rc1;
9178                 }
9179                 goto out;
9180         /* lfs quota -u username /path/to/lustre/mount */
9181         } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9182                    qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
9183                 /* options should be followed by u/g-name and mntpoint */
9184                 if ((!show_default && optind + 2 != argc) ||
9185                     (show_default && optind + 1 != argc) ||
9186                     qctl->qc_type == ALLQUOTA) {
9187                         fprintf(stderr,
9188                                 "%s quota: name and mount point must be specified\n",
9189                                 progname);
9190                         rc = CMD_HELP;
9191                         goto out;
9192                 }
9193
9194                 if (!show_default) {
9195                         name = argv[optind++];
9196                         switch (qctl->qc_type) {
9197                         case USRQUOTA:
9198                                 rc = name2uid(&qctl->qc_id, name);
9199                                 break;
9200                         case GRPQUOTA:
9201                                 rc = name2gid(&qctl->qc_id, name);
9202                                 break;
9203                         case PRJQUOTA:
9204                                 rc = name2projid(&qctl->qc_id, name);
9205                                 break;
9206                         default:
9207                                 rc = -ENOTSUP;
9208                                 break;
9209                         }
9210                 } else {
9211                         qctl->qc_valid = QC_GENERAL;
9212                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
9213                                         LUSTRE_Q_GETDEFAULT_POOL :
9214                                         LUSTRE_Q_GETDEFAULT;
9215                         qctl->qc_id = 0;
9216                 }
9217
9218                 if (rc) {
9219                         if (str2quotaid(&qctl->qc_id, name)) {
9220                                 fprintf(stderr, "%s quota: invalid id '%s'\n",
9221                                         progname, name);
9222                                 rc = CMD_HELP;
9223                                 goto out;
9224                         }
9225                 }
9226         } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
9227                 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
9228                         progname);
9229                 rc = CMD_HELP;
9230                 goto out;
9231         }
9232
9233         mnt = argv[optind];
9234         if (show_pools) {
9235                 char *p;
9236
9237                 i = 0;
9238                 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
9239                 if (rc)
9240                         goto out;
9241
9242                 for (i = 0; i < poolcount; i++) {
9243                         p = memchr(poollist[i], '.', MAXNAMLEN);
9244                         if (!p) {
9245                                 fprintf(stderr, "bad string format %.*s\n",
9246                                         MAXNAMLEN, poollist[i]);
9247                                 rc = -EINVAL;
9248                                 goto out;
9249                         }
9250                         p++;
9251                         printf("Quotas for pool: %s\n", p);
9252                         strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
9253                         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9254                                              human_readable, show_default);
9255                         if (rc)
9256                                 break;
9257                 }
9258                 goto out;
9259         }
9260
9261         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9262                              human_readable, show_default);
9263 out:
9264         free(buf);
9265         free(qctl);
9266         return rc;
9267 }
9268 #endif /* HAVE_SYS_QUOTA_H! */
9269
9270 static int flushctx_ioctl(char *mp)
9271 {
9272         int fd, rc;
9273
9274         fd = open(mp, O_RDONLY);
9275         if (fd == -1) {
9276                 fprintf(stderr, "flushctx: error open %s: %s\n",
9277                         mp, strerror(errno));
9278                 return -1;
9279         }
9280
9281         rc = ioctl(fd, LL_IOC_FLUSHCTX);
9282         if (rc == -1)
9283                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
9284                         mp, strerror(errno));
9285
9286         close(fd);
9287         return rc;
9288 }
9289
9290 static int lfs_flushctx(int argc, char **argv)
9291 {
9292         int     kdestroy = 0, reap = 0, c;
9293         char    mntdir[PATH_MAX] = {'\0'};
9294         int     index = 0;
9295         int     rc = 0;
9296
9297         while ((c = getopt(argc, argv, "kr")) != -1) {
9298                 switch (c) {
9299                 case 'k':
9300                         kdestroy = 1;
9301                         break;
9302                 case 'r':
9303                         reap = 1;
9304                         break;
9305                 default:
9306                         fprintf(stderr,
9307                                 "error: %s: option '-%c' unrecognized\n",
9308                                 argv[0], c);
9309                         return CMD_HELP;
9310                 }
9311         }
9312
9313         if (kdestroy) {
9314                 rc = system("kdestroy > /dev/null");
9315                 if (rc) {
9316                         rc = WEXITSTATUS(rc);
9317                         fprintf(stderr,
9318                                 "error destroying tickets: %d, continuing\n",
9319                                 rc);
9320                 }
9321         }
9322
9323         if (optind >= argc) {
9324                 /* flush for all mounted lustre fs. */
9325                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
9326                         /* Check if we have a mount point */
9327                         if (mntdir[0] == '\0')
9328                                 continue;
9329
9330                         if (flushctx_ioctl(mntdir))
9331                                 rc = -1;
9332
9333                         mntdir[0] = '\0'; /* avoid matching in next loop */
9334                 }
9335         } else {
9336                 /* flush fs as specified */
9337                 while (optind < argc) {
9338                         if (flushctx_ioctl(argv[optind++]))
9339                                 rc = -1;
9340                 }
9341         }
9342
9343         if (reap) {
9344                 rc = system("keyctl reap > /dev/null");
9345                 if (rc != 0) {
9346                         rc = WEXITSTATUS(rc);
9347                         fprintf(stderr, "error reaping keyring: %d\n", rc);
9348                 }
9349         }
9350
9351         return rc;
9352 }
9353
9354 static int lfs_changelog(int argc, char **argv)
9355 {
9356         void *changelog_priv;
9357         struct changelog_rec *rec;
9358         long long startrec = 0, endrec = 0;
9359         char *mdd;
9360         struct option long_opts[] = {
9361                 { .val = 'f', .name = "follow", .has_arg = no_argument },
9362                 { .name = NULL } };
9363         char short_opts[] = "f";
9364         int rc, follow = 0;
9365
9366         while ((rc = getopt_long(argc, argv, short_opts,
9367                 long_opts, NULL)) != -1) {
9368                 switch (rc) {
9369                 case 'f':
9370                         follow++;
9371                         break;
9372                 default:
9373                         fprintf(stderr,
9374                                 "%s changelog: unrecognized option '%s'\n",
9375                                 progname, argv[optind - 1]);
9376                         return CMD_HELP;
9377                 }
9378         }
9379         if (optind >= argc) {
9380                 fprintf(stderr, "%s changelog: mdtname must be specified\n",
9381                         progname);
9382                 return CMD_HELP;
9383         }
9384
9385         mdd = argv[optind++];
9386         if (argc > optind) {
9387                 errno = 0;
9388                 startrec = strtoll(argv[optind++], NULL, 10);
9389                 if (errno != 0 || startrec < 0) {
9390                         fprintf(stderr,
9391                                 "%s changelog: bad startrec\n",
9392                                 progname);
9393                         return CMD_HELP;
9394                 }
9395         }
9396
9397         if (argc > optind) {
9398                 errno = 0;
9399                 endrec = strtoll(argv[optind++], NULL, 10);
9400                 if (errno != 0 || endrec < 0) {
9401                         fprintf(stderr,
9402                                 "%s changelog: bad endrec\n",
9403                                 progname);
9404                         return CMD_HELP;
9405                 }
9406         }
9407
9408         rc = llapi_changelog_start(&changelog_priv,
9409                                    CHANGELOG_FLAG_BLOCK |
9410                                    CHANGELOG_FLAG_JOBID |
9411                                    CHANGELOG_FLAG_EXTRA_FLAGS |
9412                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
9413                                    mdd, startrec);
9414         if (rc < 0) {
9415                 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
9416                         progname, strerror(errno = -rc));
9417                 return rc;
9418         }
9419
9420         rc = llapi_changelog_set_xflags(changelog_priv,
9421                                         CHANGELOG_EXTRA_FLAG_UIDGID |
9422                                         CHANGELOG_EXTRA_FLAG_NID |
9423                                         CHANGELOG_EXTRA_FLAG_OMODE |
9424                                         CHANGELOG_EXTRA_FLAG_XATTR);
9425         if (rc < 0) {
9426                 fprintf(stderr,
9427                         "%s changelog: cannot set xflags for changelog: %s\n",
9428                         progname, strerror(errno = -rc));
9429                 return rc;
9430         }
9431
9432         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9433                 time_t secs;
9434                 struct tm ts;
9435
9436                 if (endrec && rec->cr_index > endrec) {
9437                         llapi_changelog_free(&rec);
9438                         break;
9439                 }
9440                 if (rec->cr_index < startrec) {
9441                         llapi_changelog_free(&rec);
9442                         continue;
9443                 }
9444
9445                 secs = rec->cr_time >> 30;
9446                 gmtime_r(&secs, &ts);
9447                 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9448                        "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9449                        changelog_type2str(rec->cr_type),
9450                        ts.tm_hour, ts.tm_min, ts.tm_sec,
9451                        (int)(rec->cr_time & ((1 << 30) - 1)),
9452                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9453                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9454
9455                 if (rec->cr_flags & CLF_JOBID) {
9456                         struct changelog_ext_jobid *jid =
9457                                 changelog_rec_jobid(rec);
9458
9459                         if (jid->cr_jobid[0] != '\0')
9460                                 printf(" j=%s", jid->cr_jobid);
9461                 }
9462
9463                 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
9464                         struct changelog_ext_extra_flags *ef =
9465                                 changelog_rec_extra_flags(rec);
9466
9467                         printf(" ef=0x%llx",
9468                                (unsigned long long)ef->cr_extra_flags);
9469
9470                         if (ef->cr_extra_flags & CLFE_UIDGID) {
9471                                 struct changelog_ext_uidgid *uidgid =
9472                                         changelog_rec_uidgid(rec);
9473
9474                                 printf(" u=%llu:%llu",
9475                                        (unsigned long long)uidgid->cr_uid,
9476                                        (unsigned long long)uidgid->cr_gid);
9477                         }
9478                         if (ef->cr_extra_flags & CLFE_NID) {
9479                                 struct changelog_ext_nid *nid =
9480                                         changelog_rec_nid(rec);
9481
9482                                 printf(" nid=%s",
9483                                        libcfs_nid2str(nid->cr_nid));
9484                         }
9485
9486                         if (ef->cr_extra_flags & CLFE_OPEN) {
9487                                 struct changelog_ext_openmode *omd =
9488                                         changelog_rec_openmode(rec);
9489                                 char mode[] = "---";
9490
9491                                 /* exec mode must be exclusive */
9492                                 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9493                                         mode[2] = 'x';
9494                                 } else {
9495                                         if (omd->cr_openflags & MDS_FMODE_READ)
9496                                                 mode[0] = 'r';
9497                                         if (omd->cr_openflags &
9498                                             (MDS_FMODE_WRITE |
9499                                              MDS_OPEN_TRUNC |
9500                                              MDS_OPEN_APPEND))
9501                                                 mode[1] = 'w';
9502                                 }
9503
9504                                 if (strcmp(mode, "---") != 0)
9505                                         printf(" m=%s", mode);
9506                         }
9507
9508                         if (ef->cr_extra_flags & CLFE_XATTR) {
9509                                 struct changelog_ext_xattr *xattr =
9510                                         changelog_rec_xattr(rec);
9511
9512                                 if (xattr->cr_xattr[0] != '\0')
9513                                         printf(" x=%s", xattr->cr_xattr);
9514                         }
9515                 }
9516
9517                 if (!fid_is_zero(&rec->cr_pfid))
9518                         printf(" p="DFID, PFID(&rec->cr_pfid));
9519                 if (rec->cr_namelen)
9520                         printf(" %.*s", rec->cr_namelen,
9521                                changelog_rec_name(rec));
9522
9523                 if (rec->cr_flags & CLF_RENAME) {
9524                         struct changelog_ext_rename *rnm =
9525                                 changelog_rec_rename(rec);
9526
9527                         if (!fid_is_zero(&rnm->cr_sfid))
9528                                 printf(" s="DFID" sp="DFID" %.*s",
9529                                        PFID(&rnm->cr_sfid),
9530                                        PFID(&rnm->cr_spfid),
9531                                        (int)changelog_rec_snamelen(rec),
9532                                        changelog_rec_sname(rec));
9533                 }
9534                 printf("\n");
9535
9536                 llapi_changelog_free(&rec);
9537         }
9538
9539         llapi_changelog_fini(&changelog_priv);
9540
9541         if (rc < 0)
9542                 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9543                         progname, strerror(errno = -rc));
9544
9545         return (rc == 1 ? 0 : rc);
9546 }
9547
9548 static int lfs_changelog_clear(int argc, char **argv)
9549 {
9550         long long endrec;
9551         int rc;
9552
9553         if (argc != 4)
9554                 return CMD_HELP;
9555
9556         errno = 0;
9557         endrec = strtoll(argv[3], NULL, 10);
9558         if (errno != 0 || endrec < 0) {
9559                 fprintf(stderr,
9560                         "%s: bad endrec '%s'\n",
9561                         argv[0], argv[3]);
9562                 return CMD_HELP;
9563         }
9564
9565         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9566
9567         if (rc == -EINVAL)
9568                 fprintf(stderr, "%s: record out of range: %llu\n",
9569                         argv[0], endrec);
9570         else if (rc == -ENOENT)
9571                 fprintf(stderr, "%s: no changelog user: %s\n",
9572                         argv[0], argv[2]);
9573         else if (rc)
9574                 fprintf(stderr, "%s error: %s\n", argv[0],
9575                         strerror(-rc));
9576
9577         if (rc)
9578                 errno = -rc;
9579
9580         return rc;
9581 }
9582
9583 static void rstripc(char *str, int c)
9584 {
9585         char *end = str + strlen(str);
9586
9587         for (; str < end && end[-1] == c; --end)
9588                 end[-1] = '\0';
9589 }
9590
9591 static int lfs_fid2path(int argc, char **argv)
9592 {
9593         struct option long_opts[] = {
9594                 { .val = 'c',   .name = "cur",  .has_arg = no_argument },
9595                 { .val = 'c',   .name = "current",      .has_arg = no_argument },
9596                 { .val = 'c',   .name = "print-link",   .has_arg = no_argument },
9597                 { .val = 'f',   .name = "print-fid",    .has_arg = no_argument },
9598                 { .val = 'l',   .name = "link", .has_arg = required_argument },
9599                 { .name = NULL } };
9600         char short_opts[] = "cfl:pr:";
9601         bool print_link = false;
9602         bool print_fid = false;
9603         bool print_mnt_dir;
9604         char mnt_dir[PATH_MAX] = "";
9605         int mnt_fd = -1;
9606         char *path_or_fsname;
9607         long long recno = -1;
9608         int linkno = -1;
9609         char *endptr = NULL;
9610         int rc = 0;
9611         int c;
9612         int i;
9613
9614         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9615                 switch (c) {
9616                 case 'c':
9617                         print_link = true;
9618                         break;
9619                 case 'f':
9620                         print_fid = true;
9621                         break;
9622                 case 'l':
9623                         errno = 0;
9624                         linkno = strtol(optarg, &endptr, 10);
9625                         if (errno != 0 || *endptr != '\0' || linkno < 0) {
9626                                 fprintf(stderr,
9627                                         "%s fid2path: invalid linkno '%s'\n",
9628                                         progname, optarg);
9629                                 return CMD_HELP;
9630                         }
9631                         break;
9632                 case 'r':
9633                         /* recno is something to do with changelogs
9634                          * that was never implemented. We just pass it
9635                          * through for the MDT to ignore.
9636                          */
9637                         errno = 0;
9638                         recno = strtoll(optarg, &endptr, 10);
9639                         if (errno != 0 || *endptr != '\0' || recno < 0) {
9640                                 fprintf(stderr,
9641                                         "%s fid2path: invalid recno '%s'\n",
9642                                         progname, optarg);
9643                                 return CMD_HELP;
9644                         }
9645                         break;
9646                 default:
9647                         fprintf(stderr,
9648                                 "%s fid2path: unrecognized option '%s'\n",
9649                                 progname, argv[optind - 1]);
9650                         return CMD_HELP;
9651                 }
9652         }
9653
9654         if (argc - optind < 2) {
9655                 fprintf(stderr,
9656                         "Usage: %s fid2path FSNAME|ROOT FID...\n",
9657                         progname);
9658                 return CMD_HELP;
9659         }
9660
9661         path_or_fsname = argv[optind];
9662
9663         if (*path_or_fsname == '/') {
9664                 print_mnt_dir = true;
9665                 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9666         } else {
9667                 print_mnt_dir = false;
9668                 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9669         }
9670
9671         if (rc < 0) {
9672                 fprintf(stderr,
9673                         "%s fid2path: cannot resolve mount point for '%s': %s\n",
9674                         progname, path_or_fsname, strerror(-rc));
9675                 goto out;
9676         }
9677
9678         mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9679         if (mnt_fd < 0) {
9680                 fprintf(stderr,
9681                         "%s fid2path: cannot open mount point for '%s': %s\n",
9682                         progname, path_or_fsname, strerror(-rc));
9683                 goto out;
9684         }
9685
9686         /* Strip trailing slashes from mnt_dir. */
9687         rstripc(mnt_dir + 1, '/');
9688
9689         for (i = optind + 1; i < argc; i++) {
9690                 const char *fid_str = argv[i];
9691                 struct lu_fid fid;
9692                 int rc2;
9693
9694                 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9695                 if (rc2 < 0) {
9696                         fprintf(stderr,
9697                                 "%s fid2path: invalid FID '%s'\n",
9698                                 progname, fid_str);
9699                         if (rc == 0)
9700                                 rc = rc2;
9701
9702                         continue;
9703                 }
9704
9705                 int linktmp = (linkno >= 0) ? linkno : 0;
9706                 while (1) {
9707                         int oldtmp = linktmp;
9708                         long long rectmp = recno;
9709                         char path_buf[PATH_MAX];
9710
9711                         rc2 = llapi_fid2path_at(mnt_fd, &fid,
9712                                 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9713                         if (rc2 < 0) {
9714                                 fprintf(stderr,
9715                                         "%s fid2path: cannot find %s %s: %s\n",
9716                                         progname, path_or_fsname, fid_str,
9717                                         strerror(-rc2));
9718                                 if (rc == 0)
9719                                         rc = rc2;
9720                                 break;
9721                         }
9722
9723                         if (print_fid)
9724                                 printf("%s ", fid_str);
9725
9726                         if (print_link)
9727                                 printf("%d ", linktmp);
9728
9729                         /* You may think this looks wrong or weird (and it is!)
9730                          * but we are actually trying to preserve the old quirky
9731                          * behaviors (enforced by our old quirky tests!) that
9732                          * make lfs so much fun to work on:
9733                          *
9734                          *   lustre 0x200000007:0x1:0x0 => "/"
9735                          *   /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9736                          *
9737                          * Note that llapi_fid2path() returns "" for the root
9738                          * FID. */
9739
9740                         printf("%s%s%s\n",
9741                                print_mnt_dir ? mnt_dir : "",
9742                                (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9743                                path_buf);
9744
9745                         if (linkno >= 0)
9746                                 /* specified linkno */
9747                                 break;
9748
9749                         if (oldtmp == linktmp)
9750                                 /* no more links */
9751                                 break;
9752                 }
9753         }
9754 out:
9755         if (!(mnt_fd < 0))
9756                 close(mnt_fd);
9757
9758         return rc;
9759 }
9760
9761 static int lfs_path2fid(int argc, char **argv)
9762 {
9763         struct option long_opts[] = {
9764                 { .val = 'p', .name = "parents", .has_arg = no_argument },
9765                 { .name = NULL } };
9766         char            **path;
9767         const char        short_opts[] = "p";
9768         const char       *sep = "";
9769         struct lu_fid     fid;
9770         int               rc = 0;
9771         bool              show_parents = false;
9772
9773         while ((rc = getopt_long(argc, argv, short_opts,
9774                                  long_opts, NULL)) != -1) {
9775                 switch (rc) {
9776                 case 'p':
9777                         show_parents = true;
9778                         break;
9779                 default:
9780                         fprintf(stderr,
9781                                 "%s path2fid: unrecognized option '%s'\n",
9782                                 progname, argv[optind - 1]);
9783                         return CMD_HELP;
9784                 }
9785         }
9786
9787         if (optind > argc - 1) {
9788                 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9789                         progname);
9790                 return CMD_HELP;
9791         } else if (optind < argc - 1) {
9792                 sep = ": ";
9793         }
9794
9795         rc = 0;
9796         for (path = argv + optind; *path != NULL; path++) {
9797                 int err = 0;
9798
9799                 if (!show_parents) {
9800                         err = llapi_path2fid(*path, &fid);
9801                         if (!err)
9802                                 printf("%s%s"DFID"\n",
9803                                        *sep != '\0' ? *path : "", sep,
9804                                        PFID(&fid));
9805                 } else {
9806                         char            name[NAME_MAX + 1];
9807                         unsigned int    linkno = 0;
9808
9809                         while ((err = llapi_path2parent(*path, linkno, &fid,
9810                                                 name, sizeof(name))) == 0) {
9811                                 if (*sep != '\0' && linkno == 0)
9812                                         printf("%s%s", *path, sep);
9813
9814                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9815                                        PFID(&fid), name);
9816                                 linkno++;
9817                         }
9818
9819                         /* err == -ENODATA is end-of-loop */
9820                         if (linkno > 0 && err == -ENODATA) {
9821                                 printf("\n");
9822                                 err = 0;
9823                         }
9824                 }
9825
9826                 if (err) {
9827                         fprintf(stderr,
9828                                 "%s path2fid: cannot get %sfid for '%s': %s\n",
9829                                 progname, show_parents ? "parent " : "", *path,
9830                                 strerror(-err));
9831                         if (rc == 0) {
9832                                 rc = err;
9833                                 errno = -err;
9834                         }
9835                 }
9836         }
9837
9838         return rc;
9839 }
9840
9841 #define MAX_ERRNO       4095
9842 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9843
9844 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9845 {
9846         int rc, rc2, k;
9847
9848         rc = llapi_rmfid(device, fa);
9849         if (rc < 0) {
9850                 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9851                         progname, strerror(-rc));
9852                 return rc;
9853         }
9854
9855         for (k = 0; k < fa->fa_nr; k++) {
9856                 rc2 = (__s32)fa->fa_fids[k].f_ver;
9857                 if (!IS_ERR_VALUE(rc2))
9858                         continue;
9859
9860                 if (rc == 0)
9861                         rc = rc2;
9862
9863                 fa->fa_fids[k].f_ver = 0;
9864                 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9865                         progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9866         }
9867
9868         return rc;
9869 }
9870
9871 static int lfs_rmfid(int argc, char **argv)
9872 {
9873         char *fidstr, *device;
9874         int rc = 0, rc2, nr;
9875         struct fid_array *fa;
9876
9877         if (optind > argc - 1) {
9878                 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9879                 return CMD_HELP;
9880         }
9881
9882         device = argv[optind++];
9883
9884         nr = argc - optind;
9885         fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9886         if (!fa)
9887                 return -ENOMEM;
9888
9889         fa->fa_nr = 0;
9890         rc = 0;
9891         while (optind < argc) {
9892                 int found;
9893
9894                 fidstr = argv[optind++];
9895                 while (*fidstr == '[')
9896                         fidstr++;
9897                 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9898                 if (found != 3) {
9899                         fprintf(stderr, "unrecognized FID: %s\n",
9900                                 argv[optind - 1]);
9901                         exit(1);
9902                 }
9903                 fa->fa_nr++;
9904                 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9905                         /* start another batch */
9906                         rc2 = lfs_rmfid_and_show_errors(device, fa);
9907                         if (rc2 && !rc)
9908                                 rc = rc2;
9909                         fa->fa_nr = 0;
9910                 }
9911         }
9912         if (fa->fa_nr) {
9913                 rc2 = lfs_rmfid_and_show_errors(device, fa);
9914                 if (rc2 && !rc)
9915                         rc = rc2;
9916         }
9917
9918         return rc;
9919 }
9920
9921 static int lfs_data_version(int argc, char **argv)
9922 {
9923         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9924         __u64 data_version;
9925         char *path;
9926         int fd;
9927         int rc;
9928         int c;
9929
9930         if (argc < 2) {
9931                 fprintf(stderr, "%s: FILE must be specified\n",
9932                         progname);
9933                 return CMD_HELP;
9934         }
9935
9936         while ((c = getopt(argc, argv, "hnrw")) != -1) {
9937                 switch (c) {
9938                 case 'n':
9939                         data_version_flags = 0;
9940                         break;
9941                 case 'r':
9942                         data_version_flags |= LL_DV_RD_FLUSH;
9943                         break;
9944                 case 'w':
9945                         data_version_flags |= LL_DV_WR_FLUSH;
9946                         break;
9947                 default:
9948                         fprintf(stderr,
9949                                 "%s data_version: unrecognized option '%s'\n",
9950                                 progname, argv[optind - 1]);
9951                         fallthrough;
9952                 case 'h':
9953                         return CMD_HELP;
9954                 }
9955         }
9956         if (optind == argc) {
9957                 fprintf(stderr, "%s data_version: FILE must be specified\n",
9958                         progname);
9959                 return CMD_HELP;
9960         }
9961
9962         path = argv[optind];
9963         fd = open(path, O_RDONLY);
9964         if (fd < 0) {
9965                 rc = -errno;
9966                 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9967                         progname, path, strerror(-rc));
9968                 return rc;
9969         }
9970
9971         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9972         if (rc < 0)
9973                 fprintf(stderr,
9974                         "%s data_version: cannot get version for '%s': %s\n",
9975                         progname, path, strerror(-rc));
9976         else
9977                 printf("%ju" "\n", (uintmax_t)data_version);
9978
9979         close(fd);
9980         return rc;
9981 }
9982
9983 static int lfs_hsm_state(int argc, char **argv)
9984 {
9985         int rc = 0;
9986         int i = 1;
9987         char *path;
9988         struct hsm_user_state hus;
9989
9990         if (argc < 2)
9991                 return CMD_HELP;
9992
9993         do {
9994                 int rc2;
9995                 path = argv[i];
9996
9997                 rc2 = llapi_hsm_state_get(path, &hus);
9998                 if (rc2) {
9999                         fprintf(stderr,
10000                                 "%s %s: get HSM state for '%s' failed: %s\n",
10001                                 progname, argv[0], path, strerror(-rc2));
10002                         if (!rc)
10003                                 rc = rc2;
10004                         continue;
10005                 }
10006
10007                 /* Display path name and status flags */
10008                 printf("%s: (0x%08x)", path, hus.hus_states);
10009
10010                 if (hus.hus_states & HS_RELEASED)
10011                         printf(" released");
10012                 if (hus.hus_states & HS_EXISTS)
10013                         printf(" exists");
10014                 if (hus.hus_states & HS_DIRTY)
10015                         printf(" dirty");
10016                 if (hus.hus_states & HS_ARCHIVED)
10017                         printf(" archived");
10018                 /* Display user-settable flags */
10019                 if (hus.hus_states & HS_NORELEASE)
10020                         printf(" never_release");
10021                 if (hus.hus_states & HS_NOARCHIVE)
10022                         printf(" never_archive");
10023                 if (hus.hus_states & HS_LOST)
10024                         printf(" lost_from_hsm");
10025
10026                 if (hus.hus_archive_id != 0)
10027                         printf(", archive_id:%d", hus.hus_archive_id);
10028                 printf("\n");
10029
10030         } while (++i < argc);
10031
10032         return rc;
10033 }
10034
10035 #define LFS_HSM_SET   0
10036 #define LFS_HSM_CLEAR 1
10037
10038 /**
10039  * Generic function to set or clear HSM flags.
10040  * Used by hsm_set and hsm_clear.
10041  *
10042  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
10043  */
10044 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
10045 {
10046         struct option long_opts[] = {
10047         { .val = 'A',   .name = "archived",     .has_arg = no_argument },
10048         { .val = 'a',   .name = "noarchive",    .has_arg = no_argument },
10049         { .val = 'd',   .name = "dirty",        .has_arg = no_argument },
10050         { .val = 'e',   .name = "exists",       .has_arg = no_argument },
10051         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10052         { .val = 'i',   .name = "archive-id",   .has_arg = required_argument },
10053         { .val = 'l',   .name = "lost",         .has_arg = no_argument },
10054         { .val = 'r',   .name = "norelease",    .has_arg = no_argument },
10055         { .name = NULL } };
10056         __u64 mask = 0;
10057         int c, rc = 0;
10058         char *path;
10059         __u32 archive_id = 0;
10060         char *end = NULL;
10061
10062         if (argc < 3)
10063                 return CMD_HELP;
10064
10065         while ((c = getopt_long(argc, argv, "aAdehi:lr",
10066                                 long_opts, NULL)) != -1) {
10067                 switch (c) {
10068                 case 'l':
10069                         mask |= HS_LOST;
10070                         break;
10071                 case 'a':
10072                         mask |= HS_NOARCHIVE;
10073                         break;
10074                 case 'A':
10075                         mask |= HS_ARCHIVED;
10076                         break;
10077                 case 'r':
10078                         mask |= HS_NORELEASE;
10079                         break;
10080                 case 'd':
10081                         mask |= HS_DIRTY;
10082                         break;
10083                 case 'e':
10084                         mask |= HS_EXISTS;
10085                         break;
10086                 case 'i':
10087                         errno = 0;
10088                         archive_id = strtol(optarg, &end, 10);
10089                         if (errno != 0 || *end != '\0' || archive_id < 0) {
10090                                 fprintf(stderr,
10091                                         "%s: invalid archive_id: '%s'\n",
10092                                         progname, end);
10093                                 return CMD_HELP;
10094                         }
10095                         break;
10096                 default:
10097                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10098                                 progname, argv[optind - 1]);
10099                         fallthrough;
10100                 case 'h':
10101                         return CMD_HELP;
10102                 }
10103         }
10104
10105         /* User should have specified a flag */
10106         if (mask == 0)
10107                 return CMD_HELP;
10108
10109         while (optind < argc) {
10110                 int rc2;
10111                 path = argv[optind];
10112
10113                 /* If mode == 0, this means we apply the mask. */
10114                 if (mode == LFS_HSM_SET)
10115                         rc2 = llapi_hsm_state_set(path, mask, 0, archive_id);
10116                 else
10117                         rc2 = llapi_hsm_state_set(path, 0, mask, 0);
10118
10119                 if (rc2) {
10120                         fprintf(stderr,
10121                                 "%s %s: change hsm flags for '%s' failed: %s\n",
10122                                 progname, argv[0], path, strerror(-rc2));
10123                         if (!rc)
10124                                 rc = rc2;
10125                 }
10126                 optind++;
10127         }
10128
10129         return rc;
10130 }
10131
10132 static int lfs_hsm_action(int argc, char **argv)
10133 {
10134         struct hsm_current_action hca;
10135         struct hsm_extent he;
10136         enum hsm_user_action hua;
10137         enum hsm_progress_states hps;
10138         int rc = 0;
10139         int i = 1;
10140         char *path;
10141
10142         if (argc < 2)
10143                 return CMD_HELP;
10144
10145         do {
10146                 int rc2;
10147                 path = argv[i];
10148
10149                 rc2 = llapi_hsm_current_action(path, &hca);
10150                 if (rc2) {
10151                         fprintf(stderr,
10152                                 "%s %s: get hsm action for '%s' failed: %s\n",
10153                                 progname, argv[0], path, strerror(-rc2));
10154
10155                         if (!rc)
10156                                 rc = rc2;
10157                         continue;
10158                 }
10159                 he = hca.hca_location;
10160                 hua = hca.hca_action;
10161                 hps = hca.hca_state;
10162
10163                 printf("%s: %s", path, hsm_user_action2name(hua));
10164
10165                 /* Skip file without action */
10166                 if (hca.hca_action == HUA_NONE) {
10167                         printf("\n");
10168                         continue;
10169                 }
10170
10171                 printf(" %s ", hsm_progress_state2name(hps));
10172
10173                 if ((hps == HPS_RUNNING) &&
10174                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
10175                         printf("(%llu bytes moved)\n",
10176                                (unsigned long long)he.length);
10177                 else if ((he.offset + he.length) == LUSTRE_EOF)
10178                         printf("(from %llu to EOF)\n",
10179                                (unsigned long long)he.offset);
10180                 else
10181                         printf("(from %llu to %llu)\n",
10182                                (unsigned long long)he.offset,
10183                                (unsigned long long)(he.offset + he.length));
10184
10185         } while (++i < argc);
10186
10187         return rc;
10188 }
10189
10190 static int lfs_hsm_set(int argc, char **argv)
10191 {
10192         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
10193 }
10194
10195 static int lfs_hsm_clear(int argc, char **argv)
10196 {
10197         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
10198 }
10199
10200 /**
10201  * Check file state and return its fid, to be used by lfs_hsm_request().
10202  *
10203  * \param[in]     file      Path to file to check
10204  * \param[in,out] fid       Pointer to allocated lu_fid struct.
10205  * \param[in,out] last_dev  Pointer to last device id used.
10206  *
10207  * \return 0 on success.
10208  */
10209 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
10210                                 dev_t *last_dev)
10211 {
10212         struct stat     st;
10213         int             rc;
10214
10215         rc = lstat(file, &st);
10216         if (rc) {
10217                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
10218                 return -errno;
10219         }
10220         /*
10221          * Checking for regular file as archiving as posix copytool
10222          * rejects archiving files other than regular files
10223          */
10224         if (!S_ISREG(st.st_mode)) {
10225                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
10226                 return CMD_HELP;
10227         }
10228         /* A request should be ... */
10229         if (*last_dev != st.st_dev && *last_dev != 0) {
10230                 fprintf(stderr,
10231                         "All files should be on the same filesystem: %s\n",
10232                         file);
10233                 return -EINVAL;
10234         }
10235         *last_dev = st.st_dev;
10236
10237         rc = llapi_path2fid(file, fid);
10238         if (rc) {
10239                 fprintf(stderr, "Cannot read FID of %s: %s\n",
10240                         file, strerror(-rc));
10241                 return rc;
10242         }
10243         return 0;
10244 }
10245
10246 /* Fill an HSM HUR item with a given file name.
10247  *
10248  * If mntpath is set, then the filename is actually a FID, and no
10249  * lookup on the filesystem will be performed.
10250  *
10251  * \param[in]  hur         the user request to fill
10252  * \param[in]  idx         index of the item inside the HUR to fill
10253  * \param[in]  mntpath     mountpoint of Lustre
10254  * \param[in]  fname       filename (if mtnpath is NULL)
10255  *                         or FID (if mntpath is set)
10256  * \param[in]  last_dev    pointer to last device id used
10257  *
10258  * \retval 0 on success
10259  * \retval CMD_HELP or a negative errno on error
10260  */
10261 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
10262                          const char *mntpath, const char *fname,
10263                          dev_t *last_dev)
10264 {
10265         struct hsm_user_item *hui = &hur->hur_user_item[idx];
10266         int rc;
10267
10268         hui->hui_extent.length = -1;
10269
10270         if (mntpath) {
10271                 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
10272                 if (rc)
10273                         fprintf(stderr, "hsm: '%s' is not a valid FID\n",
10274                                 fname);
10275         } else {
10276                 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
10277         }
10278
10279         if (rc == 0)
10280                 hur->hur_request.hr_itemcount++;
10281
10282         return rc;
10283 }
10284
10285 static int lfs_hsm_request(int argc, char **argv, int action)
10286 {
10287         struct option long_opts[] = {
10288         { .val = 'a',   .name = "archive",      .has_arg = required_argument },
10289         { .val = 'D',   .name = "data",         .has_arg = required_argument },
10290         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10291         { .val = 'l',   .name = "filelist",     .has_arg = required_argument },
10292         { .val = 'm',   .name = "mntpath",      .has_arg = required_argument },
10293         { .name = NULL } };
10294         dev_t last_dev = 0;
10295         struct hsm_user_request *hur, *oldhur;
10296         int c, i;
10297         size_t len;
10298         int nbfile;
10299         char *line = NULL;
10300         char *filelist = NULL;
10301         char fullpath[PATH_MAX];
10302         char *opaque = NULL;
10303         int opaque_len = 0;
10304         int archive_id = 0;
10305         FILE *fp;
10306         int nbfile_alloc = 0;
10307         char *some_file = NULL;
10308         char *mntpath = NULL;
10309         int rc;
10310
10311         if (argc < 2)
10312                 return CMD_HELP;
10313
10314         while ((c = getopt_long(argc, argv, "a:D:hl:m:",
10315                                 long_opts, NULL)) != -1) {
10316                 switch (c) {
10317                 case 'l':
10318                         filelist = optarg;
10319                         break;
10320                 case 'D':
10321                         opaque = optarg;
10322                         break;
10323                 case 'a':
10324                         if (action != HUA_ARCHIVE &&
10325                             action != HUA_REMOVE) {
10326                                 fprintf(stderr,
10327                                         "error: -a is supported only when archiving or removing\n");
10328                                 return CMD_HELP;
10329                         }
10330                         archive_id = atoi(optarg);
10331                         break;
10332                 case 'm':
10333                         if (!some_file) {
10334                                 mntpath = optarg;
10335                                 some_file = strdup(optarg);
10336                         }
10337                         break;
10338                 default:
10339                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10340                                 progname, argv[optind - 1]);
10341                         fallthrough;
10342                 case 'h':
10343                         return CMD_HELP;
10344                 }
10345         }
10346
10347         /* All remaining args are files, so we have at least nbfile */
10348         nbfile = argc - optind;
10349
10350         if ((nbfile == 0) && (!filelist))
10351                 return CMD_HELP;
10352
10353         if (opaque)
10354                 opaque_len = strlen(opaque);
10355
10356         /*
10357          * Alloc the request structure with enough place to store all files
10358          * from command line.
10359          */
10360         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
10361         if (!hur) {
10362                 fprintf(stderr, "Cannot create the request: %s\n",
10363                         strerror(errno));
10364                 return errno;
10365         }
10366         nbfile_alloc = nbfile;
10367
10368         hur->hur_request.hr_action = action;
10369         hur->hur_request.hr_archive_id = archive_id;
10370         hur->hur_request.hr_flags = 0;
10371
10372         /* All remaining args are files, add them */
10373         if (nbfile != 0 && some_file == NULL)
10374                 some_file = strdup(argv[optind]);
10375
10376         for (i = 0; i < nbfile; i++) {
10377                 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
10378                                    &last_dev);
10379                 if (rc)
10380                         goto out_free;
10381         }
10382
10383         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
10384
10385         /* If a filelist was specified, read the filelist from it. */
10386         if (filelist) {
10387                 fp = fopen(filelist, "r");
10388                 if (!fp) {
10389                         fprintf(stderr, "Cannot read the file list %s: %s\n",
10390                                 filelist, strerror(errno));
10391                         rc = -errno;
10392                         goto out_free;
10393                 }
10394
10395                 while ((rc = getline(&line, &len, fp)) != -1) {
10396                         /*
10397                          * If allocated buffer was too small, get something
10398                          * larger
10399                          */
10400                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
10401                                 ssize_t size;
10402
10403                                 nbfile_alloc = nbfile_alloc * 2 + 1;
10404                                 oldhur = hur;
10405                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
10406                                                                    opaque_len);
10407                                 if (!hur) {
10408                                         fprintf(stderr,
10409                                                 "hsm: cannot allocate the request: %s\n",
10410                                                 strerror(errno));
10411                                         hur = oldhur;
10412                                         rc = -errno;
10413                                         fclose(fp);
10414                                         goto out_free;
10415                                 }
10416                                 size = hur_len(oldhur);
10417                                 if (size < 0) {
10418                                         fprintf(stderr,
10419                                                 "hsm: cannot allocate %u files + %u bytes data\n",
10420                                                 oldhur->hur_request.hr_itemcount,
10421                                                 oldhur->hur_request.hr_data_len);
10422                                         free(hur);
10423                                         hur = oldhur;
10424                                         rc = -E2BIG;
10425                                         fclose(fp);
10426                                         goto out_free;
10427                                 }
10428                                 memcpy(hur, oldhur, size);
10429                                 free(oldhur);
10430                         }
10431
10432                         /* Chop CR */
10433                         if (line[strlen(line) - 1] == '\n')
10434                                 line[strlen(line) - 1] = '\0';
10435
10436                         rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
10437                                            mntpath, line, &last_dev);
10438                         if (rc) {
10439                                 fclose(fp);
10440                                 goto out_free;
10441                         }
10442
10443                         if (!some_file) {
10444                                 some_file = line;
10445                                 line = NULL;
10446                         }
10447                 }
10448
10449                 rc = fclose(fp);
10450                 free(line);
10451         }
10452
10453         /* If a --data was used, add it to the request */
10454         hur->hur_request.hr_data_len = opaque_len;
10455         if (opaque)
10456                 memcpy(hur_data(hur), opaque, opaque_len);
10457
10458         /* Send the HSM request */
10459         if (realpath(some_file, fullpath) == NULL) {
10460                 fprintf(stderr, "Could not find path '%s': %s\n",
10461                         some_file, strerror(errno));
10462         }
10463         rc = llapi_hsm_request(fullpath, hur);
10464         if (rc) {
10465                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
10466                         some_file, strerror(-rc));
10467                 goto out_free;
10468         }
10469
10470 out_free:
10471         free(some_file);
10472         free(hur);
10473         return rc;
10474 }
10475
10476 static int lfs_hsm_archive(int argc, char **argv)
10477 {
10478         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
10479 }
10480
10481 static int lfs_hsm_restore(int argc, char **argv)
10482 {
10483         return lfs_hsm_request(argc, argv, HUA_RESTORE);
10484 }
10485
10486 static int lfs_hsm_release(int argc, char **argv)
10487 {
10488         return lfs_hsm_request(argc, argv, HUA_RELEASE);
10489 }
10490
10491 static int lfs_hsm_remove(int argc, char **argv)
10492 {
10493         return lfs_hsm_request(argc, argv, HUA_REMOVE);
10494 }
10495
10496 static int lfs_hsm_cancel(int argc, char **argv)
10497 {
10498         return lfs_hsm_request(argc, argv, HUA_CANCEL);
10499 }
10500
10501 static int lfs_swap_layouts(int argc, char **argv)
10502 {
10503         if (argc != 3)
10504                 return CMD_HELP;
10505
10506         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
10507                                   SWAP_LAYOUTS_KEEP_MTIME |
10508                                   SWAP_LAYOUTS_KEEP_ATIME);
10509 }
10510
10511 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10512
10513 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10514
10515 int lfs_get_mode(const char *string)
10516 {
10517         enum lock_mode_user mode;
10518
10519         for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10520                 if (lock_mode_names[mode] == NULL)
10521                         continue;
10522                 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10523                         return mode;
10524         }
10525
10526         return -EINVAL;
10527 }
10528
10529 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10530 {
10531         enum lu_ladvise_type advice;
10532
10533         for (advice = 0;
10534              advice < ARRAY_SIZE(ladvise_names); advice++) {
10535                 if (ladvise_names[advice] == NULL)
10536                         continue;
10537                 if (strcmp(string, ladvise_names[advice]) == 0)
10538                         return advice;
10539         }
10540
10541         return LU_LADVISE_INVALID;
10542 }
10543
10544 static int lfs_ladvise(int argc, char **argv)
10545 {
10546         struct option long_opts[] = {
10547         { .val = 'a',   .name = "advice",       .has_arg = required_argument },
10548         { .val = 'b',   .name = "background",   .has_arg = no_argument },
10549         { .val = 'e',   .name = "end",          .has_arg = required_argument },
10550         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10551         { .val = 'l',   .name = "length",       .has_arg = required_argument },
10552         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
10553         { .val = 's',   .name = "start",        .has_arg = required_argument },
10554         { .val = 'u',   .name = "unset",        .has_arg = no_argument },
10555         { .name = NULL } };
10556         struct llapi_lu_ladvise advice;
10557         enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10558         unsigned long long start = 0;
10559         unsigned long long end = LUSTRE_EOF;
10560         unsigned long long length = 0;
10561         unsigned long long size_units;
10562         unsigned long long flags = 0;
10563         int c, fd, rc = 0;
10564         const char *path;
10565         int mode = 0;
10566
10567         optind = 0;
10568         while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10569                                 long_opts, NULL)) != -1) {
10570                 switch (c) {
10571                 case 'a':
10572                         advice_type = lfs_get_ladvice(optarg);
10573                         if (advice_type == LU_LADVISE_INVALID) {
10574                                 fprintf(stderr,
10575                                         "%s: invalid advice type '%s'\n",
10576                                         progname, optarg);
10577                                 fprintf(stderr, "Valid types:");
10578
10579                                 for (advice_type = 0;
10580                                      advice_type < ARRAY_SIZE(ladvise_names);
10581                                      advice_type++) {
10582                                         if (ladvise_names[advice_type] == NULL)
10583                                                 continue;
10584                                         fprintf(stderr, " %s",
10585                                                 ladvise_names[advice_type]);
10586                                 }
10587                                 fprintf(stderr, "\n");
10588
10589                                 return CMD_HELP;
10590                         }
10591                         break;
10592                 case 'b':
10593                         flags |= LF_ASYNC;
10594                         break;
10595                 case 'u':
10596                         flags |= LF_UNSET;
10597                         break;
10598                 case 'e':
10599                         size_units = 1;
10600                         rc = llapi_parse_size(optarg, &end,
10601                                               &size_units, 0);
10602                         if (rc) {
10603                                 fprintf(stderr, "%s: bad end offset '%s'\n",
10604                                         argv[0], optarg);
10605                                 return CMD_HELP;
10606                         }
10607                         break;
10608                 case 's':
10609                         size_units = 1;
10610                         rc = llapi_parse_size(optarg, &start,
10611                                               &size_units, 0);
10612                         if (rc) {
10613                                 fprintf(stderr,
10614                                         "%s: bad start offset '%s'\n",
10615                                         argv[0], optarg);
10616                                 return CMD_HELP;
10617                         }
10618                         break;
10619                 case 'l':
10620                         size_units = 1;
10621                         rc = llapi_parse_size(optarg, &length,
10622                                               &size_units, 0);
10623                         if (rc) {
10624                                 fprintf(stderr, "%s: bad length '%s'\n",
10625                                         argv[0], optarg);
10626                                 return CMD_HELP;
10627                         }
10628                         break;
10629                 case 'm':
10630                         mode = lfs_get_mode(optarg);
10631                         if (mode < 0) {
10632                                 fprintf(stderr,
10633                                         "%s: bad mode '%s', valid modes are READ or WRITE\n",
10634                                         argv[0], optarg);
10635                                 return CMD_HELP;
10636                         }
10637                         break;
10638                 default:
10639                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10640                                 progname, argv[optind - 1]);
10641                         fallthrough;
10642                 case 'h':
10643                         return CMD_HELP;
10644                 }
10645         }
10646
10647         if (advice_type == LU_LADVISE_INVALID) {
10648                 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10649                 fprintf(stderr, "Valid types:");
10650                 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10651                      advice_type++) {
10652                         if (ladvise_names[advice_type] == NULL)
10653                                 continue;
10654                         fprintf(stderr, " %s", ladvise_names[advice_type]);
10655                 }
10656                 fprintf(stderr, "\n");
10657                 return CMD_HELP;
10658         }
10659
10660         if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10661                 fprintf(stderr,
10662                         "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10663                         argv[0]);
10664                 return CMD_HELP;
10665         }
10666
10667         if (argc <= optind) {
10668                 fprintf(stderr, "%s: please give one or more file names\n",
10669                         argv[0]);
10670                 return CMD_HELP;
10671         }
10672
10673         if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10674                 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10675                         argv[0]);
10676                 return CMD_HELP;
10677         }
10678
10679         if (end == LUSTRE_EOF && length != 0)
10680                 end = start + length;
10681
10682         if (end <= start) {
10683                 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10684                         argv[0], start, end);
10685                 return CMD_HELP;
10686         }
10687
10688         if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10689                 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10690                         argv[0]);
10691                 return CMD_HELP;
10692         }
10693
10694         if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10695                 fprintf(stderr, "%s: mode is required with lockahead\n",
10696                         argv[0]);
10697                 return CMD_HELP;
10698         }
10699
10700         while (optind < argc) {
10701                 int rc2;
10702
10703                 path = argv[optind++];
10704
10705                 fd = open(path, O_RDONLY);
10706                 if (fd < 0) {
10707                         rc2 = -errno;
10708                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10709                                 argv[0], path, strerror(-rc2));
10710                         if (!rc)
10711                                 rc = rc2;
10712                         continue;
10713                 }
10714
10715                 advice.lla_start = start;
10716                 advice.lla_end = end;
10717                 advice.lla_advice = advice_type;
10718                 advice.lla_value1 = 0;
10719                 advice.lla_value2 = 0;
10720                 advice.lla_value3 = 0;
10721                 advice.lla_value4 = 0;
10722                 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10723                         advice.lla_lockahead_mode = mode;
10724                         advice.lla_peradvice_flags = flags;
10725                 }
10726
10727                 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10728                 close(fd);
10729                 if (rc2 < 0) {
10730                         fprintf(stderr,
10731                                 "%s: cannot give advice '%s' to file '%s': %s\n",
10732                                 argv[0], ladvise_names[advice_type],
10733                                 path, strerror(errno));
10734
10735                         if (!rc)
10736                                 rc = rc2;
10737                         continue;
10738                 }
10739         }
10740
10741         return rc;
10742 }
10743
10744 static const char *const heat_names[] = LU_HEAT_NAMES;
10745
10746 static int lfs_heat_get(int argc, char **argv)
10747 {
10748         struct lu_heat *heat;
10749         int rc = 0, rc2;
10750         char *path;
10751         int fd;
10752         int i;
10753
10754         if (argc <= 1)
10755                 return CMD_HELP;
10756
10757         heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10758         if (!heat) {
10759                 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10760                 return -ENOMEM;
10761         }
10762
10763         optind = 1;
10764         while (optind < argc) {
10765                 path = argv[optind++];
10766
10767                 fd = open(path, O_RDONLY);
10768                 if (fd < 0) {
10769                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10770                                 argv[0], path, strerror(errno));
10771                         rc2 = -errno;
10772                         goto next;
10773                 }
10774
10775                 heat->lh_count = OBD_HEAT_COUNT;
10776                 rc2 = llapi_heat_get(fd, heat);
10777                 close(fd);
10778                 if (rc2 < 0) {
10779                         fprintf(stderr,
10780                                 "%s: cannot get heat of file '%s': %s\n",
10781                                 argv[0], path, strerror(errno));
10782                         goto next;
10783                 }
10784
10785                 printf("flags: %x\n", heat->lh_flags);
10786                 for (i = 0; i < heat->lh_count; i++)
10787                         printf("%s: %llu\n", heat_names[i],
10788                                (unsigned long long)heat->lh_heat[i]);
10789 next:
10790                 if (rc == 0 && rc2 < 0)
10791                         rc = rc2;
10792         }
10793
10794         free(heat);
10795         return rc;
10796 }
10797
10798 static int lfs_heat_set(int argc, char **argv)
10799 {
10800         struct option long_opts[] = {
10801         { .val = 'c',   .name = "clear",        .has_arg = no_argument },
10802         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10803         { .val = 'o',   .name = "off",          .has_arg = no_argument },
10804         { .val = 'O',   .name = "on",           .has_arg = no_argument },
10805         { .name = NULL } };
10806         enum lu_heat_flag flags = 0;
10807         int rc = 0, rc2;
10808         char *path;
10809         int fd;
10810         int c;
10811
10812         if (argc <= 1)
10813                 return CMD_HELP;
10814
10815         optind = 0;
10816         while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10817                 switch (c) {
10818                 case 'c':
10819                         flags |= LU_HEAT_FLAG_CLEAR;
10820                         break;
10821                 case 'o':
10822                         flags |= LU_HEAT_FLAG_CLEAR;
10823                         flags |= LU_HEAT_FLAG_OFF;
10824                         break;
10825                 case 'O':
10826                         flags &= ~LU_HEAT_FLAG_OFF;
10827                         break;
10828                 default:
10829                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10830                                 progname, argv[optind - 1]);
10831                         fallthrough;
10832                 case 'h':
10833                         return CMD_HELP;
10834                 }
10835         }
10836
10837         if (argc <= optind) {
10838                 fprintf(stderr, "%s: please give one or more file names\n",
10839                         argv[0]);
10840                 return CMD_HELP;
10841         }
10842
10843         while (optind < argc) {
10844                 path = argv[optind++];
10845
10846                 fd = open(path, O_RDONLY);
10847                 if (fd < 0) {
10848                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10849                                 argv[0], path, strerror(errno));
10850                         rc2 = -errno;
10851                         goto next;
10852                 }
10853
10854                 rc2 = llapi_heat_set(fd, flags);
10855                 close(fd);
10856                 if (rc2 < 0) {
10857                         fprintf(stderr,
10858                                 "%s: cannot setflags heat of file '%s': %s\n",
10859                                 argv[0], path, strerror(errno));
10860                         goto next;
10861                 }
10862 next:
10863                 if (rc == 0 && rc2 < 0)
10864                         rc = rc2;
10865         }
10866         return rc;
10867 }
10868
10869 /**
10870  * The input string contains a comma delimited list of component ids and
10871  * ranges, for example "1,2-4,7".
10872  */
10873 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10874 {
10875         bool end_of_loop = false;
10876         char *ptr = NULL;
10877         int nr = 0;
10878         int rc;
10879
10880         if (!arg)
10881                 return -EINVAL;
10882
10883         while (!end_of_loop) {
10884                 int start_index;
10885                 int end_index;
10886                 int i;
10887                 char *endptr = NULL;
10888
10889                 rc = -EINVAL;
10890                 ptr = strchrnul(arg, ',');
10891                 end_of_loop = *ptr == '\0';
10892                 *ptr = '\0';
10893
10894                 start_index = strtol(arg, &endptr, 0);
10895                 if (endptr == arg) /* no data at all */
10896                         break;
10897                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10898                         break;
10899                 if (start_index < 0)
10900                         break;
10901
10902                 end_index = start_index;
10903                 if (*endptr == '-') {
10904                         end_index = strtol(endptr + 1, &endptr, 0);
10905                         if (*endptr != '\0')
10906                                 break;
10907                         if (end_index < start_index)
10908                                 break;
10909                 }
10910
10911                 for (i = start_index; i <= end_index && size > 0; i++) {
10912                         int j;
10913
10914                         /* remove duplicate */
10915                         for (j = 0; j < nr; j++) {
10916                                 if (ids[j] == i)
10917                                         break;
10918                         }
10919                         if (j == nr) { /* no duplicate */
10920                                 ids[nr++] = i;
10921                                 --size;
10922                         }
10923                 }
10924
10925                 if (size == 0 && i < end_index)
10926                         break;
10927
10928                 *ptr = ',';
10929                 arg = ++ptr;
10930                 rc = 0;
10931         }
10932         if (!end_of_loop && ptr)
10933                 *ptr = ',';
10934
10935         return rc < 0 ? rc : nr;
10936 }
10937
10938 /**
10939  * struct verify_mirror_id - Mirror id to be verified.
10940  * @mirror_id:   A specified mirror id.
10941  * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10942  */
10943 struct verify_mirror_id {
10944         __u16 mirror_id;
10945         bool is_valid_id;
10946 };
10947
10948 /**
10949  * compare_mirror_ids() - Compare mirror ids.
10950  * @layout: Mirror component list.
10951  * @cbdata: Callback data in verify_mirror_id structure.
10952  *
10953  * This is a callback function called by llapi_layout_comp_iterate()
10954  * to compare the specified mirror id with the one in the current
10955  * component of @layout. If they are the same, then the specified
10956  * mirror id is valid.
10957  *
10958  * Return: a negative error code on failure or
10959  *         LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10960  *         LLAPI_LAYOUT_ITER_STOP: Stop iteration
10961  */
10962 static inline
10963 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10964 {
10965         struct verify_mirror_id *mirror_id_cbdata =
10966                                  (struct verify_mirror_id *)cbdata;
10967         uint32_t mirror_id;
10968         int rc = 0;
10969
10970         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10971         if (rc < 0) {
10972                 rc = -errno;
10973                 fprintf(stderr,
10974                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
10975                         progname, strerror(errno));
10976                 return rc;
10977         }
10978
10979         if (mirror_id_cbdata->mirror_id == mirror_id) {
10980                 mirror_id_cbdata->is_valid_id = true;
10981                 return LLAPI_LAYOUT_ITER_STOP;
10982         }
10983
10984         return LLAPI_LAYOUT_ITER_CONT;
10985 }
10986
10987 /**
10988  * verify_mirror_ids() - Verify specified mirror ids.
10989  * @fname:      Mirrored file name.
10990  * @mirror_ids: Specified mirror ids to be verified.
10991  * @ids_nr:     Number of specified mirror ids.
10992  *
10993  * This function verifies that specified @mirror_ids are valid
10994  * in the mirrored file @fname.
10995  *
10996  * Return: 0 on success or a negative error code on failure.
10997  */
10998 static inline
10999 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
11000 {
11001         struct llapi_layout *layout = NULL;
11002         struct verify_mirror_id mirror_id_cbdata = { 0 };
11003         struct stat stbuf;
11004         uint32_t flr_state;
11005         int i;
11006         int fd;
11007         int rc = 0;
11008         int rc2 = 0;
11009
11010         if (ids_nr <= 0)
11011                 return -EINVAL;
11012
11013         if (stat(fname, &stbuf) < 0) {
11014                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11015                         progname, fname, strerror(errno));
11016                 rc = -errno;
11017                 goto error;
11018         }
11019
11020         if (!S_ISREG(stbuf.st_mode)) {
11021                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11022                         progname, fname);
11023                 rc = -EINVAL;
11024                 goto error;
11025         }
11026
11027         fd = open(fname, O_DIRECT | O_RDONLY);
11028         if (fd < 0) {
11029                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11030                         progname, fname, strerror(errno));
11031                 rc = -errno;
11032                 goto error;
11033         }
11034
11035         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11036         if (rc < 0) {
11037                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11038                         progname, fname, strerror(errno));
11039                 goto close_fd;
11040         }
11041
11042         layout = llapi_layout_get_by_fd(fd, 0);
11043         if (!layout) {
11044                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11045                         progname, fname, strerror(errno));
11046                 rc = -errno;
11047                 llapi_lease_release(fd);
11048                 goto close_fd;
11049         }
11050
11051         rc = llapi_layout_flags_get(layout, &flr_state);
11052         if (rc < 0) {
11053                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11054                         progname, fname, strerror(errno));
11055                 rc = -errno;
11056                 goto free_layout;
11057         }
11058
11059         flr_state &= LCM_FL_FLR_MASK;
11060         switch (flr_state) {
11061         case LCM_FL_NONE:
11062                 rc = -EINVAL;
11063                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11064                         progname, fname, llapi_layout_flags_string(flr_state));
11065                 goto free_layout;
11066         default:
11067                 break;
11068         }
11069
11070         rc2 = 0;
11071         for (i = 0; i < ids_nr; i++) {
11072                 mirror_id_cbdata.mirror_id = mirror_ids[i];
11073                 mirror_id_cbdata.is_valid_id = false;
11074
11075                 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
11076                                                &mirror_id_cbdata);
11077                 if (rc < 0) {
11078                         rc = -errno;
11079                         fprintf(stderr,
11080                                 "%s: '%s' failed to verify mirror id: %u.\n",
11081                                 progname, fname, mirror_ids[i]);
11082                         goto free_layout;
11083                 }
11084
11085                 if (!mirror_id_cbdata.is_valid_id) {
11086                         rc2 = -EINVAL;
11087                         fprintf(stderr,
11088                                 "%s: '%s' invalid specified mirror id: %u.\n",
11089                                 progname, fname, mirror_ids[i]);
11090                 }
11091         }
11092         rc = rc2;
11093
11094 free_layout:
11095         llapi_layout_free(layout);
11096         llapi_lease_release(fd);
11097 close_fd:
11098         close(fd);
11099 error:
11100         return rc;
11101 }
11102
11103 static inline
11104 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
11105                            __u16 *mirror_ids, int ids_nr)
11106 {
11107         struct llapi_resync_comp comp_array[1024] = { { 0 } };
11108         struct llapi_layout *layout;
11109         struct stat stbuf;
11110         uint32_t flr_state;
11111         uint64_t start;
11112         uint64_t end;
11113         int comp_size = 0;
11114         int idx;
11115         int fd;
11116         int rc;
11117         int rc2;
11118
11119         if (stat(fname, &stbuf) < 0) {
11120                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11121                         progname, fname, strerror(errno));
11122                 rc = -errno;
11123                 goto error;
11124         }
11125         if (!S_ISREG(stbuf.st_mode)) {
11126                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11127                         progname, fname);
11128                 rc = -EINVAL;
11129                 goto error;
11130         }
11131
11132         /* Allow mirror resync even without the key on encrypted files */
11133         fd = open(fname, O_DIRECT | O_RDWR | O_FILE_ENC);
11134         if (fd < 0) {
11135                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11136                         progname, fname, strerror(errno));
11137                 rc = -errno;
11138                 goto error;
11139         }
11140
11141         layout = llapi_layout_get_by_fd(fd, 0);
11142         if (!layout) {
11143                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11144                         progname, fname, strerror(errno));
11145                 rc = -errno;
11146                 goto close_fd;
11147         }
11148
11149         rc = llapi_layout_flags_get(layout, &flr_state);
11150         if (rc) {
11151                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11152                         progname, fname, strerror(errno));
11153                 rc = -errno;
11154                 goto free_layout;
11155         }
11156
11157         flr_state &= LCM_FL_FLR_MASK;
11158         if (flr_state == LCM_FL_NONE) {
11159                 rc = -EINVAL;
11160                 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
11161                         progname, fname);
11162                 goto free_layout;
11163         }
11164
11165         /* get stale component info */
11166         comp_size = llapi_mirror_find_stale(layout, comp_array,
11167                                             ARRAY_SIZE(comp_array),
11168                                             mirror_ids, ids_nr);
11169         if (comp_size <= 0) {
11170                 rc = comp_size;
11171                 goto free_layout;
11172         }
11173
11174         ioc->lil_mode = LL_LEASE_WRLCK;
11175         ioc->lil_flags = LL_LEASE_RESYNC;
11176         rc = llapi_lease_set(fd, ioc);
11177         if (rc < 0) {
11178                 if (rc == -EALREADY)
11179                         rc = 0;
11180                 else
11181                         fprintf(stderr,
11182                             "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
11183                                 progname, fname, strerror(-rc));
11184                 goto free_layout;
11185         }
11186
11187         /* get the read range [start, end) */
11188         start = comp_array[0].lrc_start;
11189         end = comp_array[0].lrc_end;
11190         for (idx = 1; idx < comp_size; idx++) {
11191                 if (comp_array[idx].lrc_start < start)
11192                         start = comp_array[idx].lrc_start;
11193                 if (end < comp_array[idx].lrc_end)
11194                         end = comp_array[idx].lrc_end;
11195         }
11196
11197         rc = llapi_lease_check(fd);
11198         if (rc != LL_LEASE_WRLCK) {
11199                 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11200                         progname, fname);
11201                 goto free_layout;
11202         }
11203
11204         rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
11205                                       start, end);
11206         if (rc < 0)
11207                 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
11208                         progname, fname, strerror(-rc));
11209
11210         rc = migrate_set_timestamps(fd, &stbuf);
11211         if (rc < 0) {
11212                 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
11213                         progname, fname, strerror(-rc));
11214                 goto free_layout;
11215         }
11216
11217         /* need to do the lease unlock even resync fails */
11218         ioc->lil_mode = LL_LEASE_UNLCK;
11219         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11220         ioc->lil_count = 0;
11221         for (idx = 0; idx < comp_size; idx++) {
11222                 if (comp_array[idx].lrc_synced) {
11223                         ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
11224                         ioc->lil_count++;
11225                 }
11226         }
11227
11228         rc2 = llapi_lease_set(fd, ioc);
11229         /**
11230          * llapi_lease_set returns lease mode when it request to unlock
11231          * the lease lock.
11232          */
11233         if (rc2 <= 0) {
11234                 /* rc2 == 0 means lost lease lock */
11235                 if (rc2 == 0 && rc == 0)
11236                         rc = -EBUSY;
11237                 else
11238                         rc = rc2;
11239                 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
11240                         progname, fname,
11241                         rc2 == 0 ? "lost lease lock" : strerror(-rc2));
11242
11243                 llapi_lease_release(fd);
11244                 goto free_layout;
11245         }
11246
11247 free_layout:
11248         llapi_layout_free(layout);
11249 close_fd:
11250         close(fd);
11251 error:
11252         return rc;
11253 }
11254
11255 static inline int lfs_mirror_resync(int argc, char **argv)
11256 {
11257         struct option long_opts[] = {
11258         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11259         { .val = 'o',   .name = "only",         .has_arg = required_argument },
11260         { .name = NULL } };
11261         struct ll_ioc_lease *ioc = NULL;
11262         __u16 mirror_ids[128] = { 0 };
11263         int ids_nr = 0;
11264         int c;
11265         int rc = 0;
11266
11267         while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
11268                 switch (c) {
11269                 case 'o':
11270                         rc = parse_mirror_ids(mirror_ids,
11271                                         sizeof(mirror_ids) / sizeof(__u16),
11272                                         optarg);
11273                         if (rc < 0) {
11274                                 fprintf(stderr,
11275                                         "%s: bad mirror ids '%s'.\n",
11276                                         argv[0], optarg);
11277                                 goto error;
11278                         }
11279                         ids_nr = rc;
11280                         break;
11281                 default:
11282                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11283                                 progname, argv[optind - 1]);
11284                         fallthrough;
11285                 case 'h':
11286                         rc = CMD_HELP;
11287                         goto error;
11288                 }
11289         }
11290
11291         if (argc == optind) {
11292                 fprintf(stderr, "%s: no file name given.\n", argv[0]);
11293                 rc = CMD_HELP;
11294                 goto error;
11295         }
11296
11297         if (ids_nr > 0 && argc > optind + 1) {
11298                 fprintf(stderr,
11299                     "%s: option '--only' cannot be used upon multiple files.\n",
11300                         argv[0]);
11301                 rc = CMD_HELP;
11302                 goto error;
11303         }
11304
11305         if (ids_nr > 0) {
11306                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11307                 if (rc < 0)
11308                         goto error;
11309         }
11310
11311         /* set the lease on the file */
11312         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11313         if (!ioc) {
11314                 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
11315                         argv[0], strerror(errno));
11316                 rc = -errno;
11317                 goto error;
11318         }
11319
11320         for (; optind < argc; optind++) {
11321                 rc = lfs_mirror_resync_file(argv[optind], ioc,
11322                                             mirror_ids, ids_nr);
11323                 /* ignore previous file's error, continue with next file */
11324
11325                 /* reset ioc */
11326                 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
11327         }
11328
11329         free(ioc);
11330 error:
11331         return rc;
11332 }
11333
11334 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
11335 {
11336         struct llapi_layout *layout;
11337         int rc;
11338
11339         layout = llapi_layout_get_by_fd(fd, 0);
11340         if (!layout) {
11341                 fprintf(stderr, "could not get layout.\n");
11342                 return  -EINVAL;
11343         }
11344
11345         rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
11346         if (rc < 0) {
11347                 fprintf(stderr, "failed to iterate layout\n");
11348                 llapi_layout_free(layout);
11349
11350                 return rc;
11351         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
11352                 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
11353                 llapi_layout_free(layout);
11354
11355                 return -EINVAL;
11356         }
11357         llapi_layout_free(layout);
11358
11359         return 0;
11360 }
11361
11362 /**
11363  * Check whether two files are the same file
11364  * \retval      0  same file
11365  * \retval      1  not the same file
11366  * \retval      <0 error code
11367  */
11368 static inline int check_same_file(int fd, const char *f2)
11369 {
11370         struct stat stbuf1;
11371         struct stat stbuf2;
11372
11373         if (fstat(fd, &stbuf1) < 0)
11374                 return -errno;
11375
11376         if (stat(f2, &stbuf2) < 0)
11377                 return 1;
11378
11379         if (stbuf1.st_rdev == stbuf2.st_rdev &&
11380             stbuf1.st_ino == stbuf2.st_ino)
11381                 return 0;
11382
11383         return 1;
11384 }
11385
11386 static inline int lfs_mirror_read(int argc, char **argv)
11387 {
11388         int rc = CMD_HELP;
11389         __u16 mirror_id = 0;
11390         const char *outfile = NULL;
11391         char *fname;
11392         int fd = 0;
11393         int outfd;
11394         int c;
11395         void *buf;
11396         const size_t buflen = 4 << 20;
11397         off_t pos;
11398         struct option long_opts[] = {
11399         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11400         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
11401         { .val = 'o',   .name = "outfile",      .has_arg = required_argument },
11402         { .name = NULL } };
11403
11404         while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
11405                 char *end;
11406
11407                 switch (c) {
11408                 case 'N': {
11409                         unsigned long int id;
11410
11411                         errno = 0;
11412                         id = strtoul(optarg, &end, 0);
11413                         if (errno != 0 || *end != '\0' || id == 0 ||
11414                             id > UINT16_MAX) {
11415                                 fprintf(stderr,
11416                                         "%s %s: invalid mirror ID '%s'\n",
11417                                         progname, argv[0], optarg);
11418                                 return rc;
11419                         }
11420
11421                         mirror_id = (__u16)id;
11422                         break;
11423                 }
11424                 case 'o':
11425                         outfile = optarg;
11426                         break;
11427                 default:
11428                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11429                                 progname, argv[optind - 1]);
11430                         fallthrough;
11431                 case 'h':
11432                         return CMD_HELP;
11433                 }
11434         }
11435
11436         if (argc == optind) {
11437                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11438                         progname, argv[0]);
11439                 return rc;
11440         } else if (argc > optind + 1) {
11441                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11442                 return rc;
11443         }
11444
11445         if (mirror_id == 0) {
11446                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11447                         progname, argv[0]);
11448                 return rc;
11449         }
11450
11451         /* open mirror file */
11452         fname = argv[optind];
11453         fd = open(fname, O_DIRECT | O_RDONLY);
11454         if (fd < 0) {
11455                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11456                         progname, argv[0], fname, strerror(errno));
11457                 return rc;
11458         }
11459
11460         /* verify mirror id */
11461         rc = verify_mirror_id_by_fd(fd, mirror_id);
11462         if (rc) {
11463                 fprintf(stderr,
11464                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11465                         progname, argv[0], mirror_id, fname);
11466                 goto close_fd;
11467         }
11468
11469         /* open output file - O_EXCL ensures output is not the same as input */
11470         if (outfile) {
11471                 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
11472                 if (outfd < 0) {
11473                         fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
11474                                 progname, argv[0], outfile, strerror(errno));
11475                         rc = -errno;
11476                         goto close_fd;
11477                 }
11478         } else {
11479                 outfd = STDOUT_FILENO;
11480         }
11481
11482         /* allocate buffer */
11483         rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
11484         if (rc) {
11485                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11486                                 progname, argv[0], rc);
11487                 goto close_outfd;
11488         }
11489
11490         pos = 0;
11491         while (1) {
11492                 ssize_t bytes_read;
11493                 ssize_t written = 0;
11494
11495                 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
11496                 if (bytes_read < 0) {
11497                         rc = bytes_read;
11498                         fprintf(stderr,
11499                                 "%s %s: fail to read data from mirror %u: %s\n",
11500                                 progname, argv[0], mirror_id, strerror(-rc));
11501                         goto free_buf;
11502                 }
11503
11504                 /* EOF reached */
11505                 if (bytes_read == 0)
11506                         break;
11507
11508                 while (written < bytes_read) {
11509                         ssize_t written2;
11510
11511                         written2 = write(outfd, buf + written,
11512                                          bytes_read - written);
11513                         if (written2 < 0) {
11514                                 fprintf(stderr,
11515                                         "%s %s: fail to write %s: %s\n",
11516                                         progname, argv[0], outfile ? : "STDOUT",
11517                                         strerror(errno));
11518                                 rc = -errno;
11519                                 goto free_buf;
11520                         }
11521                         written += written2;
11522                 }
11523
11524                 if (written != bytes_read) {
11525                         fprintf(stderr,
11526                 "%s %s: written %ld bytes does not match with %ld read.\n",
11527                                 progname, argv[0], written, bytes_read);
11528                         rc = -EIO;
11529                         goto free_buf;
11530                 }
11531
11532                 pos += bytes_read;
11533         }
11534
11535         fsync(outfd);
11536         rc = 0;
11537
11538 free_buf:
11539         free(buf);
11540 close_outfd:
11541         if (outfile)
11542                 close(outfd);
11543 close_fd:
11544         close(fd);
11545
11546         return rc;
11547 }
11548
11549 static inline int lfs_mirror_write(int argc, char **argv)
11550 {
11551         int rc = CMD_HELP;
11552         __u16 mirror_id = 0;
11553         const char *inputfile = NULL;
11554         char *fname;
11555         int fd = 0;
11556         int inputfd;
11557         int c;
11558         void *buf;
11559         const size_t buflen = 4 << 20;
11560         off_t pos;
11561         size_t page_size = sysconf(_SC_PAGESIZE);
11562         struct ll_ioc_lease_id ioc;
11563         struct option long_opts[] = {
11564         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11565         { .val = 'i',   .name = "inputfile",    .has_arg = required_argument },
11566         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
11567         { .name = NULL } };
11568
11569         while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11570                 char *end;
11571
11572                 switch (c) {
11573                 case 'N': {
11574                         unsigned long int id;
11575
11576                         errno = 0;
11577                         id = strtoul(optarg, &end, 0);
11578                         if (errno != 0 || *end != '\0' || id == 0 ||
11579                             id > UINT16_MAX) {
11580                                 fprintf(stderr,
11581                                         "%s %s: invalid mirror ID '%s'\n",
11582                                         progname, argv[0], optarg);
11583                                 return rc;
11584                         }
11585
11586                         mirror_id = (__u16)id;
11587                         break;
11588                 }
11589                 case 'i':
11590                         inputfile = optarg;
11591                         break;
11592                 default:
11593                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11594                                 progname, argv[optind - 1]);
11595                         fallthrough;
11596                 case 'h':
11597                         return CMD_HELP;
11598                 }
11599         }
11600
11601         if (argc == optind) {
11602                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11603                         progname, argv[0]);
11604                 return rc;
11605         } else if (argc > optind + 1) {
11606                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11607                 return rc;
11608         }
11609
11610         if (mirror_id == 0) {
11611                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11612                         progname, argv[0]);
11613                 return rc;
11614         }
11615
11616         /* open mirror file */
11617         fname = argv[optind];
11618         fd = open(fname, O_DIRECT | O_WRONLY);
11619         if (fd < 0) {
11620                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11621                         progname, argv[0], fname, strerror(errno));
11622                 return rc;
11623         }
11624
11625         /* verify mirror id */
11626         rc = verify_mirror_id_by_fd(fd, mirror_id);
11627         if (rc) {
11628                 fprintf(stderr,
11629                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11630                         progname, argv[0], mirror_id, fname);
11631                 goto close_fd;
11632         }
11633
11634         /* open input file */
11635         if (inputfile) {
11636                 rc = check_same_file(fd, inputfile);
11637                 if (rc == 0) {
11638                         fprintf(stderr,
11639                         "%s %s: input file cannot be the mirrored file '%s'\n",
11640                                 progname, argv[0], fname);
11641                         goto close_fd;
11642                 }
11643                 if (rc < 0)
11644                         goto close_fd;
11645
11646                 inputfd = open(inputfile, O_RDONLY, 0644);
11647                 if (inputfd < 0) {
11648                         fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11649                                 progname, argv[0], inputfile, strerror(errno));
11650                         rc = -errno;
11651                         goto close_fd;
11652                 }
11653         } else {
11654                 inputfd = STDIN_FILENO;
11655         }
11656
11657         /* allocate buffer */
11658         rc = posix_memalign(&buf, page_size, buflen);
11659         if (rc) {
11660                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11661                         progname, argv[0], rc);
11662                 goto close_inputfd;
11663         }
11664
11665         /* prepare target mirror components instantiation */
11666         ioc.lil_mode = LL_LEASE_WRLCK;
11667         ioc.lil_flags = LL_LEASE_RESYNC;
11668         ioc.lil_mirror_id = mirror_id;
11669         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11670         if (rc < 0) {
11671                 fprintf(stderr,
11672                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11673                         progname, argv[0], fname, strerror(errno));
11674                 goto free_buf;
11675         }
11676
11677         pos = 0;
11678         while (1) {
11679                 ssize_t bytes_read;
11680                 ssize_t written;
11681                 size_t to_write;
11682
11683                 rc = llapi_lease_check(fd);
11684                 if (rc != LL_LEASE_WRLCK) {
11685                         fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11686                                 progname, argv[0], fname);
11687                         goto free_buf;
11688                 }
11689
11690                 bytes_read = read(inputfd, buf, buflen);
11691                 if (bytes_read < 0) {
11692                         rc = bytes_read;
11693                         fprintf(stderr,
11694                                 "%s %s: fail to read data from '%s': %s\n",
11695                                 progname, argv[0], inputfile ? : "STDIN",
11696                                 strerror(errno));
11697                         rc = -errno;
11698                         goto free_buf;
11699                 }
11700
11701                 /* EOF reached */
11702                 if (bytes_read == 0)
11703                         break;
11704
11705                 /* round up to page align to make direct IO happy. */
11706                 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11707
11708                 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11709                                              pos);
11710                 if (written < 0) {
11711                         rc = written;
11712                         fprintf(stderr,
11713                               "%s %s: fail to write to mirror %u: %s\n",
11714                                 progname, argv[0], mirror_id,
11715                                 strerror(-rc));
11716                         goto free_buf;
11717                 }
11718
11719                 pos += bytes_read;
11720         }
11721
11722         if (pos & (page_size - 1)) {
11723                 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11724                 if (rc < 0)
11725                         goto free_buf;
11726         }
11727
11728         ioc.lil_mode = LL_LEASE_UNLCK;
11729         ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11730         ioc.lil_count = 0;
11731         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11732         if (rc <= 0) {
11733                 if (rc == 0)
11734                         rc = -EBUSY;
11735                 fprintf(stderr,
11736                         "%s %s: release lease lock of '%s' failed: %s\n",
11737                         progname, argv[0], fname, strerror(-rc));
11738                 goto free_buf;
11739         }
11740
11741         rc = 0;
11742
11743 free_buf:
11744         free(buf);
11745 close_inputfd:
11746         if (inputfile)
11747                 close(inputfd);
11748 close_fd:
11749         close(fd);
11750
11751         return rc;
11752 }
11753
11754 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11755 {
11756         struct llapi_layout *layout;
11757         struct collect_ids_data cid = { .cid_ids = ids,
11758                                         .cid_count = 0,
11759                                         .cid_exclude = exclude_id, };
11760         int rc;
11761
11762         layout = llapi_layout_get_by_fd(fd, 0);
11763         if (!layout) {
11764                 fprintf(stderr, "could not get layout\n");
11765                 return -EINVAL;
11766         }
11767
11768         rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11769         if (rc < 0) {
11770                 fprintf(stderr, "failed to iterate layout\n");
11771                 llapi_layout_free(layout);
11772
11773                 return rc;
11774         }
11775         llapi_layout_free(layout);
11776
11777         return cid.cid_count;
11778 }
11779
11780 #ifndef MIRROR_ID_NEG
11781 #define MIRROR_ID_NEG         0x8000
11782 #endif
11783
11784 static inline int lfs_mirror_copy(int argc, char **argv)
11785 {
11786         int rc = CMD_HELP;
11787         __u16 read_mirror_id = 0;
11788         __u16 ids[128] = { 0 };
11789         int count = 0;
11790         struct llapi_layout *layout = NULL;
11791         struct llapi_resync_comp comp_array[1024] = { { 0 } };
11792         int comp_size = 0;
11793         char *fname;
11794         int fd = 0;
11795         int c;
11796         int i;
11797         ssize_t copied;
11798         struct ll_ioc_lease *ioc = NULL;
11799         struct ll_ioc_lease_id *resync_ioc;
11800         struct option long_opts[] = {
11801         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11802         { .val = 'i',   .name = "read-mirror",  .has_arg = required_argument },
11803         { .val = 'o',   .name = "write-mirror", .has_arg = required_argument },
11804         { .name = NULL } };
11805         char cmd[PATH_MAX];
11806
11807         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11808         progname = cmd;
11809         while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11810                 char *end;
11811
11812                 switch (c) {
11813                 case 'i': {
11814                         unsigned long int id;
11815
11816                         errno = 0;
11817                         id = strtoul(optarg, &end, 0);
11818                         if (errno != 0 || *end != '\0' || id == 0 ||
11819                             id > UINT16_MAX) {
11820                                 fprintf(stderr,
11821                                         "%s: invalid read mirror ID '%s'\n",
11822                                         progname, optarg);
11823                                 return rc;
11824                         }
11825
11826                         read_mirror_id = (__u16)id;
11827                         break;
11828                 }
11829                 case 'o':
11830                         if (!strcmp(optarg, "-1")) {
11831                                 /* specify all other mirrors */
11832                                 ids[0] = (__u16)-1;
11833                                 count = 1;
11834                         } else {
11835                                 count = parse_mirror_ids((__u16 *)ids,
11836                                                          ARRAY_SIZE(ids),
11837                                                          optarg);
11838                                 if (count < 0)
11839                                         return rc;
11840                         }
11841                         break;
11842                 default:
11843                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11844                                 progname, argv[optind - 1]);
11845                         fallthrough;
11846                 case 'h':
11847                         return CMD_HELP;
11848                 }
11849         }
11850
11851         if (argc == optind) {
11852                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11853                         progname, argv[0]);
11854                 return rc;
11855         } else if (argc > optind + 1) {
11856                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11857                 return rc;
11858         }
11859
11860         if (read_mirror_id == 0) {
11861                 fprintf(stderr,
11862                         "%s %s: no valid read mirror ID %d is provided\n",
11863                         progname, argv[0], read_mirror_id);
11864                 return rc;
11865         }
11866
11867         if (count == 0) {
11868                 fprintf(stderr,
11869                         "%s %s: no write mirror ID is provided\n",
11870                         progname, argv[0]);
11871                 return rc;
11872         }
11873
11874         for (i = 0; i < count; i++) {
11875                 if (read_mirror_id == ids[i]) {
11876                         fprintf(stderr,
11877                         "%s %s: read and write mirror ID cannot be the same\n",
11878                                 progname, argv[0]);
11879                         return rc;
11880                 }
11881         }
11882
11883         /* open mirror file */
11884         fname = argv[optind];
11885
11886         fd = open(fname, O_DIRECT | O_RDWR);
11887         if (fd < 0) {
11888                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11889                         progname, argv[0], fname, strerror(errno));
11890                 return rc;
11891         }
11892
11893         /* write to all other mirrors */
11894         if (ids[0] == (__u16)-1) {
11895                 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11896                 if (count <= 0) {
11897                         rc = count;
11898                         fprintf(stderr,
11899                         "%s %s: failed to get other mirror ids in '%s': %d\n",
11900                                 progname, argv[0], fname, rc);
11901                         goto close_fd;
11902                 }
11903         }
11904
11905         /* verify mirror id */
11906         rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11907         if (rc) {
11908                 fprintf(stderr,
11909                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11910                         progname, argv[0], read_mirror_id, fname);
11911                 goto close_fd;
11912         }
11913
11914         for (i = 0; i < count; i++) {
11915                 rc = verify_mirror_id_by_fd(fd, ids[i]);
11916                 if (rc) {
11917                         fprintf(stderr,
11918                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11919                                 progname, argv[0], ids[i], fname);
11920                         goto close_fd;
11921                 }
11922         }
11923
11924         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11925         if (!ioc) {
11926                 fprintf(stderr,
11927                         "%s %s: cannot alloc comp id array for ioc: %s\n",
11928                         progname, argv[0], strerror(errno));
11929                 rc = -errno;
11930                 goto close_fd;
11931         }
11932
11933         /* get stale component info */
11934         layout = llapi_layout_get_by_fd(fd, 0);
11935         if (!layout) {
11936                 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11937                         progname, argv[0], fname, strerror(errno));
11938                 rc = -errno;
11939                 goto free_ioc;
11940         }
11941         comp_size = llapi_mirror_find_stale(layout, comp_array,
11942                                             ARRAY_SIZE(comp_array),
11943                                             ids, count);
11944         llapi_layout_free(layout);
11945         if (comp_size < 0) {
11946                 rc = comp_size;
11947                 goto free_ioc;
11948         }
11949
11950         /* prepare target mirror components instantiation */
11951         resync_ioc = (struct ll_ioc_lease_id *)ioc;
11952         resync_ioc->lil_mode = LL_LEASE_WRLCK;
11953         resync_ioc->lil_flags = LL_LEASE_RESYNC;
11954         if (count == 1)
11955                 resync_ioc->lil_mirror_id = ids[0];
11956         else
11957                 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11958         rc = llapi_lease_set(fd, ioc);
11959         if (rc < 0) {
11960                 fprintf(stderr,
11961                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11962                         progname, argv[0], fname, strerror(errno));
11963                 goto free_ioc;
11964         }
11965
11966         copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11967         if (copied < 0) {
11968                 rc = copied;
11969                 fprintf(stderr, "%s %s: copy error: %d\n",
11970                         progname, argv[0], rc);
11971                 goto free_ioc;
11972         }
11973
11974         fprintf(stdout, "mirror copied successfully: ");
11975         for (i = 0; i < copied; i++)
11976                 fprintf(stdout, "%d ", ids[i]);
11977         fprintf(stdout, "\n");
11978
11979         ioc->lil_mode = LL_LEASE_UNLCK;
11980         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11981         ioc->lil_count = 0;
11982         for (i = 0; i < comp_size; i++) {
11983                 int j;
11984
11985                 for (j = 0; j < copied; j++) {
11986                         if (comp_array[i].lrc_mirror_id != ids[j])
11987                                 continue;
11988
11989                         ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11990                         ioc->lil_count++;
11991                 }
11992         }
11993         rc = llapi_lease_set(fd, ioc);
11994         if (rc <= 0) {
11995                 if (rc == 0)
11996                         rc = -EBUSY;
11997                 fprintf(stderr,
11998                         "%s %s: release lease lock of '%s' failed: %s\n",
11999                         progname, argv[0], fname, strerror(errno));
12000                 goto free_ioc;
12001         }
12002
12003         rc = 0;
12004
12005 free_ioc:
12006         free(ioc);
12007 close_fd:
12008         close(fd);
12009
12010         return rc;
12011 }
12012
12013 /**
12014  * struct verify_chunk - Mirror chunk to be verified.
12015  * @chunk:        [start, end) of the chunk.
12016  * @mirror_count: Number of mirror ids in @mirror_id array.
12017  * @mirror_id:    Array of valid mirror ids that cover the chunk.
12018  */
12019 struct verify_chunk {
12020         struct lu_extent chunk;
12021         unsigned int mirror_count;
12022         __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
12023 };
12024
12025 /**
12026  * print_chunks() - Print chunk information.
12027  * @fname:       Mirrored file name.
12028  * @chunks:      Array of chunks.
12029  * @chunk_count: Number of chunks in @chunks array.
12030  *
12031  * This function prints [start, end) of each chunk in @chunks
12032  * for mirrored file @fname, and also prints the valid mirror ids
12033  * that cover the chunk.
12034  *
12035  * Return: void.
12036  */
12037 static inline
12038 void print_chunks(const char *fname, struct verify_chunk *chunks,
12039                   int chunk_count)
12040 {
12041         int i;
12042         int j;
12043
12044         fprintf(stdout, "Chunks to be verified in %s:\n", fname);
12045         for (i = 0; i < chunk_count; i++) {
12046                 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
12047
12048                 if (chunks[i].mirror_count == 0)
12049                         fprintf(stdout, "\t[");
12050                 else {
12051                         fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
12052                         for (j = 1; j < chunks[i].mirror_count; j++)
12053                                 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
12054                 }
12055                 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
12056         }
12057         fprintf(stdout, "\n");
12058 }
12059
12060 /**
12061  * print_checksums() - Print CRC-32 checksum values.
12062  * @chunk: A chunk and its corresponding valid mirror ids.
12063  * @crc:   CRC-32 checksum values on the chunk for each valid mirror.
12064  *
12065  * This function prints CRC-32 checksum values on @chunk for
12066  * each valid mirror that covers it.
12067  *
12068  * Return: void.
12069  */
12070 static inline
12071 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
12072                      unsigned long long pos, unsigned long long len)
12073 {
12074         int i;
12075
12076         fprintf(stdout,
12077                 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
12078         for (i = 0; i < chunk->mirror_count; i++)
12079                 fprintf(stdout, "Mirror %u:\t%#lx\n",
12080                         chunk->mirror_id[i], crc[i]);
12081         fprintf(stdout, "\n");
12082 }
12083
12084 /**
12085  * filter_mirror_id() - Filter specified mirror ids.
12086  * @chunks:      Array of chunks.
12087  * @chunk_count: Number of chunks in @chunks array.
12088  * @mirror_ids:  Specified mirror ids to be verified.
12089  * @ids_nr:      Number of specified mirror ids.
12090  *
12091  * This function scans valid mirror ids that cover each chunk in @chunks
12092  * and filters specified mirror ids.
12093  *
12094  * Return: void.
12095  */
12096 static inline
12097 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
12098                       __u16 *mirror_ids, int ids_nr)
12099 {
12100         int i;
12101         int j;
12102         int k;
12103         __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12104         unsigned int valid_count = 0;
12105
12106         for (i = 0; i < chunk_count; i++) {
12107                 if (chunks[i].mirror_count == 0)
12108                         continue;
12109
12110                 valid_count = 0;
12111                 for (j = 0; j < ids_nr; j++) {
12112                         for (k = 0; k < chunks[i].mirror_count; k++) {
12113                                 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
12114                                         valid_id[valid_count] = mirror_ids[j];
12115                                         valid_count++;
12116                                         break;
12117                                 }
12118                         }
12119                 }
12120
12121                 memcpy(chunks[i].mirror_id, valid_id,
12122                        sizeof(__u16) * valid_count);
12123                 chunks[i].mirror_count = valid_count;
12124         }
12125 }
12126
12127 /**
12128  * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
12129  * @layout:      Mirror component list.
12130  * @chunks:      Array of chunks.
12131  * @chunks_size: Array size of @chunks.
12132  *
12133  * This function scans the components in @layout from offset 0 to LUSTRE_EOF
12134  * to find out chunk segments and store them in @chunks array.
12135  *
12136  * The @mirror_id array in each element of @chunks will store the valid
12137  * mirror ids that cover the chunk. If a mirror component covering the
12138  * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
12139  * will not be stored into the @mirror_id array, and the chunk for that
12140  * mirror will not be verified.
12141  *
12142  * The @mirror_count in each element of @chunks will store the number of
12143  * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
12144  * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
12145  * indicates the chunk is valid in only one mirror. In both cases, the
12146  * chunk will not be verified.
12147  *
12148  * Here is an example:
12149  *
12150  *  0      1M     2M     3M     4M           EOF
12151  *  +------+-------------+--------------------+
12152  *  |      |             |      S             |       mirror1
12153  *  +------+------+------+------+-------------+
12154  *  |             |   S  |   S  |             |       mirror2
12155  *  +-------------+------+------+-------------+
12156  *
12157  * prepared @chunks array will contain 5 elements:
12158  * (([0, 1M), [1, 2], 2),
12159  *  ([1M, 2M), [1, 2], 2),
12160  *  ([2M, 3M), [1], 1),
12161  *  ([3M, 4M], [], 0),
12162  *  ([4M, EOF), [2], 1))
12163  *
12164  * Return: the actual array size of @chunks on success
12165  *         or a negative error code on failure.
12166  */
12167 static inline
12168 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
12169                              struct verify_chunk *chunks,
12170                              size_t chunks_size)
12171 {
12172         uint64_t start;
12173         uint64_t end;
12174         uint32_t mirror_id;
12175         uint32_t flags;
12176         int idx = 0;
12177         int i = 0;
12178         int rc = 0;
12179
12180         memset(chunks, 0, sizeof(*chunks) * chunks_size);
12181
12182         while (1) {
12183                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
12184                 if (rc < 0) {
12185                         fprintf(stderr,
12186                                 "%s: move to the first layout component: %s.\n",
12187                                 progname, strerror(errno));
12188                         goto error;
12189                 }
12190
12191                 i = 0;
12192                 rc = 0;
12193                 chunks[idx].chunk.e_end = LUSTRE_EOF;
12194                 while (rc == 0) {
12195                         rc = llapi_layout_comp_extent_get(layout, &start, &end);
12196                         if (rc < 0) {
12197                                 fprintf(stderr,
12198                                         "%s: llapi_layout_comp_extent_get failed: %s.\n",
12199                                         progname, strerror(errno));
12200                                 goto error;
12201                         }
12202
12203                         if (start > chunks[idx].chunk.e_start ||
12204                             end <= chunks[idx].chunk.e_start)
12205                                 goto next;
12206
12207                         if (end < chunks[idx].chunk.e_end)
12208                                 chunks[idx].chunk.e_end = end;
12209
12210                         rc = llapi_layout_comp_flags_get(layout, &flags);
12211                         if (rc < 0) {
12212                                 fprintf(stderr,
12213                                         "%s: llapi_layout_comp_flags_get failed: %s.\n",
12214                                         progname, strerror(errno));
12215                                 goto error;
12216                         }
12217
12218                         if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
12219                                 goto next;
12220
12221                         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
12222                         if (rc < 0) {
12223                                 fprintf(stderr,
12224                                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
12225                                         progname, strerror(errno));
12226                                 goto error;
12227                         }
12228
12229                         if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
12230                                 fprintf(stderr,
12231                                         "%s: mirror_id array is too small.\n",
12232                                         progname);
12233                                 rc = -EINVAL;
12234                                 goto error;
12235                         }
12236                         chunks[idx].mirror_id[i] = mirror_id;
12237                         i++;
12238
12239 next:
12240                         rc = llapi_layout_comp_use(layout,
12241                                                    LLAPI_LAYOUT_COMP_USE_NEXT);
12242                         if (rc < 0) {
12243                                 fprintf(stderr,
12244                                         "%s: move to the next layout component: %s.\n",
12245                                         progname, strerror(errno));
12246                                 goto error;
12247                         }
12248                 } /* loop through all components */
12249
12250                 chunks[idx].mirror_count = i;
12251
12252                 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
12253                         break;
12254
12255                 idx++;
12256                 if (idx >= chunks_size) {
12257                         fprintf(stderr, "%s: chunks array is too small.\n",
12258                                 progname);
12259                         rc = -EINVAL;
12260                         goto error;
12261                 }
12262
12263                 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
12264         }
12265
12266 error:
12267         return rc < 0 ? rc : idx + 1;
12268 }
12269
12270 /**
12271  * lfs_mirror_verify_chunk() - Verify a chunk.
12272  * @fd:        File descriptor of the mirrored file.
12273  * @file_size: Size of the mirrored file.
12274  * @chunk:     A chunk and its corresponding valid mirror ids.
12275  * @verbose:   Verbose mode.
12276  *
12277  * This function verifies a @chunk contains exactly the same data
12278  * ammong the mirrors that cover it.
12279  *
12280  * If @verbose is specified, then the function will print where the
12281  * differences are if the data do not match. Otherwise, it will
12282  * just return an error in that case.
12283  *
12284  * Return: 0 on success or a negative error code on failure.
12285  */
12286 static inline
12287 int lfs_mirror_verify_chunk(int fd, size_t file_size,
12288                             struct verify_chunk *chunk, int verbose)
12289 {
12290         const size_t buflen = 4 * 1024 * 1024; /* 4M */
12291         void *buf;
12292         size_t page_size = sysconf(_SC_PAGESIZE);
12293         ssize_t bytes_read;
12294         ssize_t bytes_done;
12295         size_t count;
12296         off_t pos;
12297         unsigned long crc;
12298         unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12299         int i;
12300         int rc = 0;
12301
12302         if (file_size == 0)
12303                 return 0;
12304
12305         rc = posix_memalign(&buf, page_size, buflen);
12306         if (rc) /* error code is returned directly */
12307                 return -rc;
12308
12309         if (verbose > 1) {
12310                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12311                         PEXT(&chunk->chunk));
12312                 for (i = 0; i < chunk->mirror_count; i++)
12313                         fprintf(stdout, " %u", chunk->mirror_id[i]);
12314                 fprintf(stdout, "\n");
12315         }
12316
12317         bytes_done = 0;
12318         count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
12319         pos = chunk->chunk.e_start;
12320         while (bytes_done < count) {
12321                 /* compute initial CRC-32 checksum */
12322                 crc = crc32(0L, Z_NULL, 0);
12323                 memset(crc_array, 0, sizeof(crc_array));
12324
12325                 bytes_read = 0;
12326                 for (i = 0; i < chunk->mirror_count; i++) {
12327                         bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
12328                                                        buf, buflen, pos);
12329                         if (bytes_read < 0) {
12330                                 rc = bytes_read;
12331                                 fprintf(stderr,
12332                                         "%s: failed to read data from mirror %u: %s.\n",
12333                                         progname, chunk->mirror_id[i],
12334                                         strerror(-rc));
12335                                 goto error;
12336                         }
12337
12338                         /* compute new CRC-32 checksum */
12339                         crc_array[i] = crc32(crc, buf, bytes_read);
12340                 }
12341
12342                 if (verbose)
12343                         print_checksums(chunk, crc_array, pos, buflen);
12344
12345                 /* compare CRC-32 checksum values */
12346                 for (i = 1; i < chunk->mirror_count; i++) {
12347                         if (crc_array[i] != crc_array[0]) {
12348                                 rc = -EINVAL;
12349
12350                                 fprintf(stderr,
12351                                         "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
12352                                         progname, PEXT(&chunk->chunk),
12353                                         chunk->mirror_id[0],
12354                                         chunk->mirror_id[i]);
12355                         }
12356                 }
12357
12358                 pos += bytes_read;
12359                 bytes_done += bytes_read;
12360         }
12361
12362         if (verbose > 1 && rc == 0) {
12363                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12364                         PEXT(&chunk->chunk));
12365                 for (i = 0; i < chunk->mirror_count; i++)
12366                         fprintf(stdout, " %u", chunk->mirror_id[i]);
12367                 fprintf(stdout, " PASS\n\n");
12368         }
12369
12370 error:
12371         free(buf);
12372         return rc;
12373 }
12374
12375 /**
12376  * lfs_mirror_verify_file() - Verify a mirrored file.
12377  * @fname:      Mirrored file name.
12378  * @mirror_ids: Specified mirror ids to be verified.
12379  * @ids_nr:     Number of specified mirror ids.
12380  * @verbose:    Verbose mode.
12381  *
12382  * This function verifies that each SYNC mirror of a mirrored file
12383  * specified by @fname contains exactly the same data.
12384  *
12385  * If @mirror_ids is specified, then the function will verify the
12386  * mirrors specified by @mirror_ids contain exactly the same data.
12387  *
12388  * If @verbose is specified, then the function will print where the
12389  * differences are if the data do not match. Otherwise, it will
12390  * just return an error in that case.
12391  *
12392  * Return: 0 on success or a negative error code on failure.
12393  */
12394 static inline
12395 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
12396                            int verbose)
12397 {
12398         struct verify_chunk chunks_array[1024] = { };
12399         struct llapi_layout *layout = NULL;
12400         struct stat stbuf;
12401         uint32_t flr_state;
12402         int fd;
12403         int chunk_count = 0;
12404         int idx = 0;
12405         int rc = 0;
12406         int rc1 = 0;
12407         int rc2 = 0;
12408
12409         if (stat(fname, &stbuf) < 0) {
12410                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
12411                         progname, fname, strerror(errno));
12412                 rc = -errno;
12413                 goto error;
12414         }
12415
12416         if (!S_ISREG(stbuf.st_mode)) {
12417                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
12418                         progname, fname);
12419                 rc = -EINVAL;
12420                 goto error;
12421         }
12422
12423         if (stbuf.st_size == 0) {
12424                 if (verbose)
12425                         fprintf(stdout, "%s: '%s' file size is 0.\n",
12426                                 progname, fname);
12427                 rc = 0;
12428                 goto error;
12429         }
12430
12431         /* Allow mirror verify even without the key on encrypted files */
12432         fd = open(fname, O_DIRECT | O_RDONLY | O_FILE_ENC);
12433         if (fd < 0) {
12434                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
12435                         progname, fname, strerror(errno));
12436                 rc = -errno;
12437                 goto error;
12438         }
12439
12440         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
12441         if (rc < 0) {
12442                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
12443                         progname, fname, strerror(errno));
12444                 goto close_fd;
12445         }
12446
12447         layout = llapi_layout_get_by_fd(fd, 0);
12448         if (!layout) {
12449                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
12450                         progname, fname, strerror(errno));
12451                 rc = -errno;
12452                 llapi_lease_release(fd);
12453                 goto close_fd;
12454         }
12455
12456         rc = llapi_layout_flags_get(layout, &flr_state);
12457         if (rc < 0) {
12458                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
12459                         progname, fname, strerror(errno));
12460                 rc = -errno;
12461                 goto free_layout;
12462         }
12463
12464         flr_state &= LCM_FL_FLR_MASK;
12465         switch (flr_state) {
12466         case LCM_FL_NONE:
12467                 rc = -EINVAL;
12468                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
12469                         progname, fname, llapi_layout_flags_string(flr_state));
12470                 goto free_layout;
12471         default:
12472                 break;
12473         }
12474
12475         /* find out mirror chunks to be verified */
12476         chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
12477                                                ARRAY_SIZE(chunks_array));
12478         if (chunk_count < 0) {
12479                 rc = chunk_count;
12480                 goto free_layout;
12481         }
12482
12483         if (ids_nr > 0)
12484                 /* filter specified mirror ids */
12485                 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
12486
12487         if (verbose > 2)
12488                 print_chunks(fname, chunks_array, chunk_count);
12489
12490         for (idx = 0; idx < chunk_count; idx++) {
12491                 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
12492                         if (verbose)
12493                                 fprintf(stdout,
12494                                         "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
12495                                         progname, fname,
12496                                         PEXT(&chunks_array[idx].chunk),
12497                                         (unsigned long long)stbuf.st_size);
12498                         break;
12499                 }
12500
12501                 if (chunks_array[idx].mirror_count == 0) {
12502                         fprintf(stderr,
12503                                 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12504                                 progname, fname,
12505                                 PEXT(&chunks_array[idx].chunk));
12506                         if (verbose) {
12507                                 fprintf(stderr, "skipped\n");
12508                                 continue;
12509                         }
12510                         rc = -EINVAL;
12511                         fprintf(stderr, "failed\n");
12512                         goto free_layout;
12513                 }
12514
12515                 if (chunks_array[idx].mirror_count == 1) {
12516                         if (verbose)
12517                                 fprintf(stdout,
12518                                         "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12519                                         progname, fname,
12520                                         PEXT(&chunks_array[idx].chunk),
12521                                         chunks_array[idx].mirror_id[0]);
12522                         continue;
12523                 }
12524
12525                 rc = llapi_lease_check(fd);
12526                 if (rc != LL_LEASE_RDLCK) {
12527                         fprintf(stderr, "%s: '%s' lost lease lock.\n",
12528                                 progname, fname);
12529                         goto free_layout;
12530                 }
12531
12532                 /* verify one chunk */
12533                 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12534                                               &chunks_array[idx], verbose);
12535                 if (rc1 < 0) {
12536                         rc2 = rc1;
12537                         if (!verbose) {
12538                                 rc = rc1;
12539                                 goto free_layout;
12540                         }
12541                 }
12542         }
12543
12544         if (rc2 < 0)
12545                 rc = rc2;
12546
12547 free_layout:
12548         llapi_layout_free(layout);
12549         llapi_lease_release(fd);
12550 close_fd:
12551         close(fd);
12552 error:
12553         return rc;
12554 }
12555
12556 /**
12557  * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12558  * @argc: The count of lfs mirror verify command line arguments.
12559  * @argv: Array of strings for lfs mirror verify command line arguments.
12560  *
12561  * This function parses lfs mirror verify command and verifies the
12562  * specified mirrored file(s).
12563  *
12564  * Return: 0 on success or a negative error code on failure.
12565  */
12566 static inline int lfs_mirror_verify(int argc, char **argv)
12567 {
12568         __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12569         int ids_nr = 0;
12570         int c;
12571         int verbose = 0;
12572         int rc = 0;
12573         int rc1 = 0;
12574         char cmd[PATH_MAX];
12575
12576         struct option long_opts[] = {
12577         { .val = 'h',   .name = "help",         .has_arg = no_argument },
12578         { .val = 'o',   .name = "only",         .has_arg = required_argument },
12579         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
12580         { .name = NULL } };
12581
12582         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12583         progname = cmd;
12584         while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12585                 switch (c) {
12586                 case 'o':
12587                         rc = parse_mirror_ids(mirror_ids,
12588                                               ARRAY_SIZE(mirror_ids),
12589                                               optarg);
12590                         if (rc < 0) {
12591                                 fprintf(stderr,
12592                                         "%s: bad mirror ids '%s'.\n",
12593                                         progname, optarg);
12594                                 goto error;
12595                         }
12596                         ids_nr = rc;
12597                         if (ids_nr < 2) {
12598                                 fprintf(stderr,
12599                                         "%s: at least 2 mirror ids needed with '--only' option.\n",
12600                                         progname);
12601                                 rc = CMD_HELP;
12602                                 goto error;
12603                         }
12604                         break;
12605                 case 'v':
12606                         verbose++;
12607                         break;
12608                 default:
12609                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12610                                 progname, argv[optind - 1]);
12611                         fallthrough;
12612                 case 'h':
12613                         rc = CMD_HELP;
12614                         goto error;
12615                 }
12616         }
12617
12618         if (argc == optind) {
12619                 fprintf(stderr, "%s: no file name given.\n", progname);
12620                 rc = CMD_HELP;
12621                 goto error;
12622         }
12623
12624         if (ids_nr > 0 && argc > optind + 1) {
12625                 fprintf(stderr,
12626                         "%s: '--only' cannot be used upon multiple files.\n",
12627                         progname);
12628                 rc = CMD_HELP;
12629                 goto error;
12630         }
12631
12632         if (ids_nr > 0) {
12633                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12634                 if (rc < 0)
12635                         goto error;
12636         }
12637
12638         rc = 0;
12639         for (; optind < argc; optind++) {
12640                 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12641                                              verbose);
12642                 if (rc1 < 0)
12643                         rc = rc1;
12644         }
12645 error:
12646         return rc;
12647 }
12648
12649 /**
12650  * lfs_mirror() - Parse and execute lfs mirror commands.
12651  * @argc: The count of lfs mirror command line arguments.
12652  * @argv: Array of strings for lfs mirror command line arguments.
12653  *
12654  * This function parses lfs mirror commands and performs the
12655  * corresponding functions specified in mirror_cmdlist[].
12656  *
12657  * Return: 0 on success or an error code on failure.
12658  */
12659 static int lfs_mirror(int argc, char **argv)
12660 {
12661         char cmd[PATH_MAX];
12662         int rc = 0;
12663
12664         setlinebuf(stdout);
12665
12666         Parser_init("lfs-mirror > ", mirror_cmdlist);
12667
12668         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12669         progname = cmd;
12670         program_invocation_short_name = cmd;
12671         if (argc > 1)
12672                 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12673         else
12674                 rc = Parser_commands();
12675
12676         return rc < 0 ? -rc : rc;
12677 }
12678
12679 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12680 {
12681 #if __BYTE_ORDER == __BIG_ENDIAN
12682         __swab16s(&attrs->lsa_valid);
12683         __swab64s(&attrs->lsa_size);
12684         __swab64s(&attrs->lsa_blocks);
12685 #endif
12686 }
12687
12688 enum lfs_som_type {
12689         LFS_SOM_SIZE = 0x1,
12690         LFS_SOM_BLOCKS = 0x2,
12691         LFS_SOM_FLAGS = 0x4,
12692         LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12693                            LFS_SOM_FLAGS,
12694 };
12695
12696 static int lfs_getsom(int argc, char **argv)
12697 {
12698         const char *path;
12699         struct lustre_som_attrs *attrs;
12700         char buf[sizeof(*attrs) + 64];
12701         enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12702         int rc = 0, c;
12703
12704         while ((c = getopt(argc, argv, "bfhs")) != -1) {
12705                 switch (c) {
12706                 case 'b':
12707                         type = LFS_SOM_BLOCKS;
12708                         break;
12709                 case 'f':
12710                         type = LFS_SOM_FLAGS;
12711                         break;
12712                 case 's':
12713                         type = LFS_SOM_SIZE;
12714                         break;
12715                 default:
12716                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12717                                 progname, argv[optind - 1]);
12718                         fallthrough;
12719                 case 'h':
12720                         return CMD_HELP;
12721                 }
12722         }
12723
12724         argc -= optind;
12725         argv += optind;
12726
12727         if (argc != 1) {
12728                 fprintf(stderr, "%s: %s\n",
12729                         progname, argc == 0 ? "miss file target" :
12730                         "input more than 2 files");
12731                 return CMD_HELP;
12732         }
12733
12734         path = argv[0];
12735         attrs = (void *)buf;
12736         rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12737         if (rc < 0) {
12738                 rc = -errno;
12739                 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12740                         argv[0], strerror(errno), errno);
12741                 return rc;
12742         }
12743
12744         lustre_som_swab(attrs);
12745
12746         switch (type) {
12747         case LFS_SOM_ATTR_ALL:
12748                 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12749                        path, (unsigned long long)attrs->lsa_size,
12750                        (unsigned long long)attrs->lsa_blocks,
12751                        attrs->lsa_valid);
12752                 break;
12753         case LFS_SOM_SIZE:
12754                 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12755                 break;
12756         case LFS_SOM_BLOCKS:
12757                 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12758                 break;
12759         case LFS_SOM_FLAGS:
12760                 printf("%x\n", attrs->lsa_valid);
12761                 break;
12762         default:
12763                 fprintf(stderr, "%s: unknown option\n", progname);
12764                 return CMD_HELP;
12765         }
12766
12767         return 0;
12768 }
12769
12770 /**
12771  * lfs_mirror_list_commands() - List lfs mirror commands.
12772  * @argc: The count of command line arguments.
12773  * @argv: Array of strings for command line arguments.
12774  *
12775  * This function lists lfs mirror commands defined in mirror_cmdlist[].
12776  *
12777  * Return: 0 on success.
12778  */
12779 static int lfs_mirror_list_commands(int argc, char **argv)
12780 {
12781         Parser_list_commands(mirror_cmdlist, 80, 4);
12782
12783         return 0;
12784 }
12785
12786 static int lfs_pcc_attach(int argc, char **argv)
12787 {
12788         struct option long_opts[] = {
12789         { .val = 'h',   .name = "help", .has_arg = no_argument },
12790         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12791         { .name = NULL } };
12792         int c;
12793         int rc = 0;
12794         __u32 archive_id = 0;
12795         const char *path;
12796         char *end;
12797         char fullpath[PATH_MAX];
12798         enum lu_pcc_type type = LU_PCC_READWRITE;
12799
12800         optind = 0;
12801         while ((c = getopt_long(argc, argv, "hi:",
12802                                 long_opts, NULL)) != -1) {
12803                 switch (c) {
12804                 case 'i':
12805                         errno = 0;
12806                         archive_id = strtoul(optarg, &end, 0);
12807                         if (errno != 0 || *end != '\0' ||
12808                             archive_id == 0 || archive_id > UINT32_MAX) {
12809                                 fprintf(stderr,
12810                                         "error: %s: bad archive ID '%s'\n",
12811                                         progname, optarg);
12812                                 return CMD_HELP;
12813                         }
12814                         break;
12815                 default:
12816                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12817                                 progname, argv[optind - 1]);
12818                         fallthrough;
12819                 case 'h':
12820                         return CMD_HELP;
12821                 }
12822         }
12823
12824         if (archive_id == 0) {
12825                 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12826                 return CMD_HELP;
12827         }
12828
12829         if (argc <= optind) {
12830                 fprintf(stderr, "%s: must specify one or more file names\n",
12831                         argv[0]);
12832                 return CMD_HELP;
12833         }
12834
12835         while (optind < argc) {
12836                 int rc2;
12837
12838                 path = argv[optind++];
12839                 if (!realpath(path, fullpath)) {
12840                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12841                                 argv[0], path, strerror(errno));
12842                         if (rc == 0)
12843                                 rc = -EINVAL;
12844                         continue;
12845                 }
12846
12847                 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12848                 if (rc2 < 0) {
12849                         fprintf(stderr,
12850                                 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12851                                 argv[0], path, archive_id, strerror(-rc2));
12852                         if (rc == 0)
12853                                 rc = rc2;
12854                 }
12855         }
12856         return rc;
12857 }
12858
12859 static int lfs_pcc_attach_fid(int argc, char **argv)
12860 {
12861         struct option long_opts[] = {
12862         { .val = 'h',   .name = "help", .has_arg = no_argument },
12863         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12864         { .val = 'm',   .name = "mnt",  .has_arg = required_argument },
12865         { .name = NULL } };
12866         int c;
12867         int rc = 0;
12868         __u32 archive_id = 0;
12869         char *end;
12870         const char *mntpath = NULL;
12871         const char *fidstr;
12872         enum lu_pcc_type type = LU_PCC_READWRITE;
12873
12874         optind = 0;
12875         while ((c = getopt_long(argc, argv, "hi:m:",
12876                                 long_opts, NULL)) != -1) {
12877                 switch (c) {
12878                 case 'i':
12879                         errno = 0;
12880                         archive_id = strtoul(optarg, &end, 0);
12881                         if (errno != 0 || *end != '\0' ||
12882                             archive_id > UINT32_MAX) {
12883                                 fprintf(stderr,
12884                                         "error: %s: bad archive ID '%s'\n",
12885                                         argv[0], optarg);
12886                                 return CMD_HELP;
12887                         }
12888                         break;
12889                 case 'm':
12890                         mntpath = optarg;
12891                         break;
12892                 default:
12893                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12894                                 progname, argv[optind - 1]);
12895                         fallthrough;
12896                 case 'h':
12897                         return CMD_HELP;
12898                 }
12899         }
12900
12901         if (archive_id == 0) {
12902                 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12903                 return CMD_HELP;
12904         }
12905
12906         if (!mntpath) {
12907                 fprintf(stderr, "%s: must specify Lustre mount point\n",
12908                         argv[0]);
12909                 return CMD_HELP;
12910         }
12911
12912         if (argc <= optind) {
12913                 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12914                 return CMD_HELP;
12915         }
12916
12917         while (optind < argc) {
12918                 int rc2;
12919
12920                 fidstr = argv[optind++];
12921
12922                 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12923                                                archive_id, type);
12924                 if (rc2 < 0) {
12925                         fprintf(stderr,
12926                                 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12927                                 argv[0], fidstr, mntpath, archive_id,
12928                                 strerror(rc2));
12929                 }
12930                 if (rc == 0 && rc2 < 0)
12931                         rc = rc2;
12932         }
12933         return rc;
12934 }
12935
12936 static int lfs_pcc_detach(int argc, char **argv)
12937 {
12938         struct option long_opts[] = {
12939         { .val = 'h',   .name = "help", .has_arg = no_argument },
12940         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12941         { .name = NULL } };
12942         int c;
12943         int rc = 0;
12944         const char *path;
12945         char fullpath[PATH_MAX];
12946         __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12947
12948         optind = 0;
12949         while ((c = getopt_long(argc, argv, "hk",
12950                                 long_opts, NULL)) != -1) {
12951                 switch (c) {
12952                 case 'k':
12953                         detach_opt = PCC_DETACH_OPT_NONE;
12954                         break;
12955                 default:
12956                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12957                                 progname, argv[optind - 1]);
12958                         fallthrough;
12959                 case 'h':
12960                         return CMD_HELP;
12961                 }
12962         }
12963
12964         while (optind < argc) {
12965                 int rc2;
12966
12967                 path = argv[optind++];
12968                 if (!realpath(path, fullpath)) {
12969                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12970                                 argv[0], path, strerror(errno));
12971                         if (rc == 0)
12972                                 rc = -EINVAL;
12973                         continue;
12974                 }
12975
12976                 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12977                 if (rc2 < 0) {
12978                         rc2 = -errno;
12979                         fprintf(stderr,
12980                                 "%s: cannot detach '%s' from PCC: %s\n",
12981                                 argv[0], path, strerror(errno));
12982                         if (rc == 0)
12983                                 rc = rc2;
12984                 }
12985         }
12986         return rc;
12987 }
12988
12989 static int lfs_pcc_detach_fid(int argc, char **argv)
12990 {
12991         struct option long_opts[] = {
12992         { .val = 'h',   .name = "help", .has_arg = no_argument },
12993         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12994         { .name = NULL } };
12995         int c;
12996         int rc = 0;
12997         const char *fid;
12998         const char *mntpath;
12999         __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
13000
13001         optind = 0;
13002         while ((c = getopt_long(argc, argv, "hk",
13003                                 long_opts, NULL)) != -1) {
13004                 switch (c) {
13005                 case 'k':
13006                         detach_opt = PCC_DETACH_OPT_NONE;
13007                         break;
13008                 default:
13009                         fprintf(stderr, "%s: unrecognized option '%s'\n",
13010                                 progname, argv[optind - 1]);
13011                         fallthrough;
13012                 case 'h':
13013                         return CMD_HELP;
13014                 }
13015         }
13016
13017         mntpath = argv[optind++];
13018
13019         while (optind < argc) {
13020                 int rc2;
13021
13022                 fid = argv[optind++];
13023
13024                 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
13025                 if (rc2 < 0) {
13026                         fprintf(stderr,
13027                                 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
13028                                 argv[0], fid, mntpath, strerror(-rc2));
13029                         if (rc == 0)
13030                                 rc = rc2;
13031                 }
13032         }
13033         return rc;
13034 }
13035
13036 static int lfs_pcc_state(int argc, char **argv)
13037 {
13038         int rc = 0;
13039         const char *path;
13040         char fullpath[PATH_MAX];
13041         struct lu_pcc_state state;
13042
13043         optind = 1;
13044
13045         if (argc <= 1) {
13046                 fprintf(stderr, "%s: must specify one or more file names\n",
13047                         progname);
13048                 return CMD_HELP;
13049         }
13050
13051         while (optind < argc) {
13052                 int rc2;
13053
13054                 path = argv[optind++];
13055                 if (!realpath(path, fullpath)) {
13056                         fprintf(stderr, "%s: could not find path '%s': %s\n",
13057                                 argv[0], path, strerror(errno));
13058                         if (rc == 0)
13059                                 rc = -EINVAL;
13060                         continue;
13061                 }
13062
13063                 rc2 = llapi_pcc_state_get(fullpath, &state);
13064                 if (rc2 < 0) {
13065                         if (rc == 0)
13066                                 rc = rc2;
13067                         fprintf(stderr,
13068                                 "%s: cannot get PCC state of '%s': %s\n",
13069                                 argv[0], path, strerror(-rc2));
13070                         continue;
13071                 }
13072
13073                 printf("file: %s", path);
13074                 printf(", type: %s", pcc_type2string(state.pccs_type));
13075                 if (state.pccs_type == LU_PCC_NONE &&
13076                     state.pccs_open_count == 0) {
13077                         printf("\n");
13078                         continue;
13079                 }
13080
13081                 printf(", PCC file: %s", state.pccs_path);
13082                 printf(", user number: %u", state.pccs_open_count);
13083                 printf(", flags: %x", state.pccs_flags);
13084                 printf("\n");
13085         }
13086         return rc;
13087 }
13088
13089 /**
13090  * lfs_pcc_list_commands() - List lfs pcc commands.
13091  * @argc: The count of command line arguments.
13092  * @argv: Array of strings for command line arguments.
13093  *
13094  * This function lists lfs pcc commands defined in pcc_cmdlist[].
13095  *
13096  * Return: 0 on success.
13097  */
13098 static int lfs_pcc_list_commands(int argc, char **argv)
13099 {
13100         Parser_list_commands(pcc_cmdlist, 80, 4);
13101
13102         return 0;
13103 }
13104
13105 /**
13106  * lfs_pcc() - Parse and execute lfs pcc commands.
13107  * @argc: The count of lfs pcc command line arguments.
13108  * @argv: Array of strings for lfs pcc command line arguments.
13109  *
13110  * This function parses lfs pcc commands and performs the
13111  * corresponding functions specified in pcc_cmdlist[].
13112  *
13113  * Return: 0 on success or an error code on failure.
13114  */
13115 static int lfs_pcc(int argc, char **argv)
13116 {
13117         char cmd[PATH_MAX];
13118         int rc = 0;
13119
13120         setlinebuf(stdout);
13121
13122         Parser_init("lfs-pcc > ", pcc_cmdlist);
13123
13124         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
13125         progname = cmd;
13126         program_invocation_short_name = cmd;
13127         if (argc > 1)
13128                 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
13129         else
13130                 rc = Parser_commands();
13131
13132         return rc < 0 ? -rc : rc;
13133 }
13134
13135 static int lfs_list_commands(int argc, char **argv)
13136 {
13137         Parser_list_commands(cmdlist, 80, 4);
13138
13139         return 0;
13140 }
13141
13142 int main(int argc, char **argv)
13143 {
13144         int rc;
13145
13146         /* Ensure that liblustreapi constructor has run */
13147         if (!llapi_liblustreapi_initialized())
13148                 fprintf(stderr, "liblustreapi was not properly initialized\n");
13149
13150         setlinebuf(stdout);
13151         opterr = 0;
13152
13153         Parser_init("lfs > ", cmdlist);
13154
13155         progname = program_invocation_short_name; /* Used in error messages */
13156         if (argc > 1) {
13157                 llapi_set_command_name(argv[1]);
13158                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
13159                 llapi_clear_command_name();
13160         } else {
13161                 rc = Parser_commands();
13162         }
13163
13164         Parser_exit(argc, argv);
13165
13166         return rc < 0 ? -rc : rc;
13167 }
13168
13169 #ifdef _LUSTRE_IDL_H_
13170 /* Everything we need here should be included by lustreapi.h. */
13171 # error "lfs should not depend on lustre_idl.h"
13172 #endif /* _LUSTRE_IDL_H_ */