Whamcloud - gitweb
51d6f6e3a60a7c821202037873da703bd2d8a40e
[fs/lustre-release.git] / lustre / utils / lfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/lfs.c
32  *
33  * Author: Peter J. Braam <braam@clusterfs.com>
34  * Author: Phil Schwan <phil@clusterfs.com>
35  * Author: Robert Read <rread@clusterfs.com>
36  */
37
38 /* for O_DIRECTORY */
39 #ifndef _GNU_SOURCE
40 #define _GNU_SOURCE
41 #endif
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <inttypes.h>
46 #include <getopt.h>
47 #include <string.h>
48 #include <mntent.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
61 #include <fcntl.h>
62 #include <dirent.h>
63 #include <time.h>
64 #include <ctype.h>
65 #include <zlib.h>
66 #include <libgen.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
69
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
78 #include <lnetconfig/cyaml.h>
79 #include "lstddef.h"
80
81 /* all functions */
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_unlink_foreign(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
97 #endif
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_rmfid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_getsom(int argc, char **argv);
119 static int lfs_heat_get(int argc, char **argv);
120 static int lfs_heat_set(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
129 static int lfs_pcc_attach(int argc, char **argv);
130 static int lfs_pcc_attach_fid(int argc, char **argv);
131 static int lfs_pcc_detach(int argc, char **argv);
132 static int lfs_pcc_detach_fid(int argc, char **argv);
133 static int lfs_pcc_state(int argc, char **argv);
134 static int lfs_pcc(int argc, char **argv);
135 static int lfs_pcc_list_commands(int argc, char **argv);
136 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
137                               __u64 migration_flags);
138
139 struct pool_to_id_cbdata {
140         const char *pool;
141         __u32 id;
142 };
143 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
144 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
145
146 enum setstripe_origin {
147         SO_SETSTRIPE,
148         SO_MIGRATE,
149         SO_MIGRATE_MDT,
150         SO_MIRROR_CREATE,
151         SO_MIRROR_EXTEND,
152         SO_MIRROR_SPLIT,
153         SO_MIRROR_DELETE,
154 };
155
156 static int lfs_setstripe_internal(int argc, char **argv,
157                                   enum setstripe_origin opc);
158
159 static inline int lfs_setstripe(int argc, char **argv)
160 {
161         return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
162 }
163
164 static inline int lfs_setstripe_migrate(int argc, char **argv)
165 {
166         return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
167 }
168
169 static inline int lfs_mirror_create(int argc, char **argv)
170 {
171         return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
172 }
173
174 static inline int lfs_mirror_extend(int argc, char **argv)
175 {
176         return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
177 }
178
179 static inline int lfs_mirror_split(int argc, char **argv)
180 {
181         return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
182 }
183
184 static inline int lfs_mirror_delete(int argc, char **argv)
185 {
186         return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
187 }
188
189 /* Setstripe and migrate share mostly the same parameters */
190 #define SSM_CMD_COMMON(cmd) \
191         "usage: "cmd" [--component-end|-E COMP_END]\n"                  \
192         "                 [--copy=LUSTRE_SRC]\n"                        \
193         "                 [--extension-size|--ext-size|-z SIZE]\n"      \
194         "                 [--help|-h] [--layout|-L PATTERN]\n"          \
195         "                 [--layout|-L PATTERN]\n"                      \
196         "                 [--mirror-count|-N[MIRROR_COUNT]]\n"          \
197         "                 [--ost|-o OST_INDICES]\n"                     \
198         "                 [--overstripe-count|-C STRIPE_COUNT]\n"       \
199         "                 [--pool|-p POOL_NAME]\n"                      \
200         "                 [--stripe-count|-c STRIPE_COUNT]\n"           \
201         "                 [--stripe-index|-i START_OST_IDX]\n"          \
202         "                 [--stripe-size|-S STRIPE_SIZE]\n"             \
203         "                 [--yaml|-y YAML_TEMPLATE_FILE]\n"
204
205 #define MIRROR_EXTEND_USAGE                                             \
206         "                 {--mirror-count|-N[MIRROR_COUNT]}\n"          \
207         "                 [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n"  \
208         "                 [--no-verify]\n"
209
210 #define SETSTRIPE_USAGE                                                 \
211         SSM_CMD_COMMON("setstripe")                                     \
212         MIRROR_EXTEND_USAGE                                             \
213         "                 DIRECTORY|FILENAME\n"
214
215 #define MIGRATE_USAGE                                                   \
216         SSM_CMD_COMMON("migrate  ")                                     \
217         "                 [--block|-b] [--non-block|-n]\n"              \
218         "                 [--non-direct|-D] [--verbose|-v]\n"           \
219         "                 FILENAME\n"
220
221 #define SETDIRSTRIPE_USAGE                                              \
222         "               [--mdt-count|-c stripe_count>\n"                \
223         "               [--help|-h] [--mdt-hash|-H mdt_hash]\n"         \
224         "               [--mdt-index|-i mdt_index[,mdt_index,...]\n"    \
225         "               [--default|-D] [--mode|-o mode]\n"              \
226         "               [--max-inherit|-X max_inherit]\n"               \
227         "               [--max-inherit-rr max_inherit_rr] <dir>\n"      \
228         "To create dir with a foreign (free format) layout :\n"         \
229         "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
230         "               [--mode|-o MODE] [--flags HEX] DIRECTORY\n"
231
232 /**
233  * command_t mirror_cmdlist - lfs mirror commands.
234  */
235 command_t mirror_cmdlist[] = {
236         { .pc_name = "create", .pc_func = lfs_mirror_create,
237           .pc_help = "Create a mirrored file.\n"
238                 "usage: lfs mirror create --mirror-count|-N[MIRROR_COUNT]\n"
239                 "           [SETSTRIPE_OPTIONS] ... FILENAME|DIRECTORY ...\n" },
240         { .pc_name = "delete", .pc_func = lfs_mirror_delete,
241           .pc_help = "Delete a mirror from a file.\n"
242         "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
243         "\t               --component-id|--comp-id|-I COMP_ID |\n"
244         "\t               -p <pool>} MIRRORED_FILE ...\n"
245         },
246         { .pc_name = "extend", .pc_func = lfs_mirror_extend,
247           .pc_help = "Extend a mirrored file.\n"
248                 "usage: lfs mirror extend "
249                 "{--mirror-count|-N[MIRROR_COUNT]} [--no-verify] "
250                 "[SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... FILENAME ...\n" },
251         { .pc_name = "split", .pc_func = lfs_mirror_split,
252           .pc_help = "Split a mirrored file.\n"
253         "usage: lfs mirror split {--mirror-id MIRROR_ID |\n"
254         "\t             --component-id|-I COMP_ID|-p POOL} [--destroy|-d]\n"
255         "\t             [-f NEW_FILE] MIRRORED_FILE ...\n" },
256         { .pc_name = "read", .pc_func = lfs_mirror_read,
257           .pc_help = "Read the content of a specified mirror of a file.\n"
258                 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
259                 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
260         { .pc_name = "write", .pc_func = lfs_mirror_write,
261           .pc_help = "Write to a specified mirror of a file.\n"
262                 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
263                 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
264         { .pc_name = "copy", .pc_func = lfs_mirror_copy,
265           .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
266                 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
267                 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
268         { .pc_name = "resync", .pc_func = lfs_mirror_resync,
269           .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
270                 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]\n"
271                 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
272         { .pc_name = "verify", .pc_func = lfs_mirror_verify,
273           .pc_help = "Verify mirrored file(s).\n"
274                 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
275                 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
276         { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
277           .pc_help = "list commands supported by lfs mirror"},
278         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
279         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
280         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
281         { .pc_help = NULL }
282 };
283
284 /**
285  * command_t pcc_cmdlist - lfs pcc commands.
286  */
287 command_t pcc_cmdlist[] = {
288         { .pc_name = "attach", .pc_func = lfs_pcc_attach,
289           .pc_help = "Attach given files to the Persistent Client Cache.\n"
290                 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
291                 "\t-i: archive id for RW-PCC\n" },
292         { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
293           .pc_help = "Attach given files into PCC by FID(s).\n"
294                 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
295                 "\t-i: archive id for RW-PCC\n"
296                 "\t-m: Lustre mount point\n" },
297         { .pc_name = "state", .pc_func = lfs_pcc_state,
298           .pc_help = "Display the PCC state for given files.\n"
299                 "usage: lfs pcc state <file> ...\n" },
300         { .pc_name = "detach", .pc_func = lfs_pcc_detach,
301           .pc_help = "Detach given files from the Persistent Client Cache.\n"
302                 "usage: lfs pcc detach <file> ...\n" },
303         { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
304           .pc_help = "Detach given files from PCC by FID(s).\n"
305                 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
306         { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
307           .pc_help = "list commands supported by lfs pcc"},
308         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
309         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
310         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
311         { .pc_help = NULL }
312 };
313
314 /* all available commands */
315 command_t cmdlist[] = {
316         {"setstripe", lfs_setstripe, 0,
317          "To create a file with specified striping/composite layout, or\n"
318          "create/replace the default layout on an existing directory:\n"
319          SSM_CMD_COMMON("setstripe")
320          "                 [--mode MODE]\n"
321          "                 <directory|filename>\n"
322          " or\n"
323          "To add component(s) to an existing composite file:\n"
324          SSM_CMD_COMMON("setstripe --component-add")
325          "To totally delete the default striping from an existing directory:\n"
326          "usage: setstripe [--delete|-d] <directory>\n"
327          " or\n"
328          "To create a mirrored file or set s default mirror layout on a directory:\n"
329          "usage: setstripe {--mirror-count|-N}[MIRROR_COUNT] [SETSTRIPE_OPTIONS] <directory|filename>\n"
330          " or\n"
331          "To delete the last component(s) from an existing composite file\n"
332          "(note that this will also delete any data in those components):\n"
333          "usage: setstripe --component-del [--component-id|-I COMP_ID]\n"
334          "                               [--component-flags|-F COMP_FLAGS]\n"
335          "                               <filename>\n"
336          "\tCOMP_ID:     Unique component ID to delete\n"
337          "\tCOMP_FLAGS:  'init' indicating all instantiated components\n"
338          "\t             '^init' indicating all uninstantiated components\n"
339          "\t-I and -F cannot be specified at the same time\n"
340          " or\n"
341          "To set or clear flags on a specific component\n"
342          "(note that this command can only be applied to mirrored files:\n"
343          "usage: setstripe --comp-set {-I COMP_ID|--comp-flags=COMP_FLAGS}\n"
344          "                            <filename>\n"
345          " or\n"
346          "To create a file with a foreign (free format) layout:\n"
347          "usage: setstripe --foreign[=FOREIGN_TYPE]\n"
348          "                 --xattr|-x LAYOUT_STRING [--flags HEX]\n"
349          "                 [--mode MODE] <filename>\n"},
350         {"getstripe", lfs_getstripe, 0,
351          "To list the layout pattern for a given file or files in a\n"
352          "directory or recursively for all files in a directory tree.\n"
353          "usage: getstripe [--ost|-O UUID] [--quiet|-q] [--verbose|-v]\n"
354          "                 [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
355          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
356          "                 [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
357          "                 [--layout|-L] [--generation|-g] [--yaml|-y]\n"
358          "                 [--help|-h] [--component-id|-I[=COMP_ID]]\n"
359          "                 [--component-flags[=COMP_FLAGS]]\n"
360          "                 [--component-count]\n"
361          "                 [--extension-size|--ext-size|-z]\n"
362          "                 [--component-start[=[+-]COMP_START]]\n"
363          "                 [--component-end[=[+-]COMP_END]|-E[[+-]comp_end]]\n"
364          "                 [[!] --mirror-index=[+-]INDEX |\n"
365          "                 [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
366          "                 [--no-follow]\n"
367          "                 <directory|filename> ..."},
368         {"setdirstripe", lfs_setdirstripe, 0,
369          "Create striped directory on specified MDT, same as mkdir.\n"
370          "May be restricted to root or group users, depending on settings.\n"
371          "usage: setdirstripe [OPTION] <directory>\n"
372          SETDIRSTRIPE_USAGE},
373         {"getdirstripe", lfs_getdirstripe, 0,
374          "To list the layout pattern info for a given directory\n"
375          "or recursively for all directories in a directory tree.\n"
376          "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
377          "                    [--help|-h] [--mdt-hash|-H] [--obd|-O UUID]\n"
378          "                    [--recursive|-r] [--yaml|-y]\n"
379          "                    [--verbose|-v] [--default|-D]\n"
380          "                    [--max-inherit|-X]\n"
381          "                    [--max-inherit-rr] <dir> ..."},
382         {"mkdir", lfs_setdirstripe, 0,
383          "Create striped directory on specified MDT, same as setdirstripe.\n"
384          "usage: mkdir [OPTION] <directory>\n"
385          SETDIRSTRIPE_USAGE},
386         {"rm_entry", lfs_rmentry, 0,
387          "To remove the name entry of the remote directory. Note: This\n"
388          "command will only delete the name entry, i.e. the remote directory\n"
389          "will become inaccessable after this command. This can only be done\n"
390          "by the administrator\n"
391          "usage: rm_entry <dir>\n"},
392         {"rmentry", lfs_rmentry, 0, "remove a dir entry, same as 'rm_entry'\n"},
393         {"unlink_foreign", lfs_unlink_foreign, 0,
394          "To remove the foreign file/dir.\n"
395          "Note: This is for files/dirs prevented to be removed using\n"
396          "unlink/rmdir, but works also for regular ones\n"
397          "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
398         {"pool_list", lfs_poollist, 0,
399          "List pools or pool OSTs\n"
400          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
401         {"find", lfs_find, 0,
402          "find files matching given attributes recursively in directory tree.\n"
403          "usage: find <directory|filename> ...\n"
404          "     [[!] --atime|-A [+-]N[smhdwy]] [[!] --btime|-B [+-]N[smhdwy]]\n"
405          "     [[!] --ctime|-C [+-]N[smhdwy]] [[!] --mtime|-M [+-]N[smhdwy]]\n"
406          "     [[!] --blocks|-b N] [[!] --component-count [+-]<comp_cnt>]\n"
407          "     [[!] --component-start [+-]N[kMGTPE]]\n"
408          "     [[!] --component-end|-E [+-]N[kMGTPE]]\n"
409          "     [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
410          "     [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
411          "     [[!] --foreign[=<foreign_type>]]\n"
412          "     [[!] --gid|-g|--group|-G <gid>|<gname>] [--help|-h]\n"
413          "     [[!] --layout|-L released,raid0,mdt] [--lazy]\n"
414          "     [--maxdepth|-D N] [[!] --mdt-count|-T [+-]<stripes>]\n"
415          "     [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
416          "     [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
417          "     [[!] --mirror-count|-N [+-]<n>]\n"
418          "     [[!] --mirror-state <[^]state>]\n"
419          "     [[!] --name|-n <pattern>] [[!] --newer[XY] <reference>]\n"
420          "     [[!] --ost|-O <uuid|index,...>] [[!] --perm [/-]mode]\n"
421          "     [[!] --pool <pool>] [--print|-P] [--print0|-0] [--printf <format>]\n"
422          "     [[!] --projid <projid>] [[!] --size|-s [+-]N[bkMGTPE]]\n"
423          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
424          "     [[!] --stripe-index|-i <index,...>]\n"
425          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
426          "     [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
427          "\t !: used before an option indicates 'NOT' requested attribute\n"
428          "\t -: used before a value indicates less than requested value\n"
429          "\t +: used before a value indicates more than requested value\n"
430          "\t ^: used before a flag indicates to exclude it\n"},
431         {"check", lfs_check, 0,
432          "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
433          "or all the servers (MGTs, MDTs and OSTs) [for specified path only].\n"
434          "usage: check {mgts|osts|mdts|all} [path]"},
435         {"osts", lfs_osts, 0, "list OSTs connected to client "
436          "[for specified path only]\n" "usage: osts [path]"},
437         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
438          "[for specified path only]\n" "usage: mdts [path]"},
439         {"df", lfs_df, 0,
440          "report filesystem disk space usage or inodes usage "
441          "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
442          "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
443          "          [--pool|-p <fsname>[.<pool>]] [path]"},
444         {"getname", lfs_getname, 0,
445          "list instances and specified mount points [for specified path only]\n"
446          "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
447 #ifdef HAVE_SYS_QUOTA_H
448         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
449          "usage: setquota [-t][-D] {-u|-U|-g|-G|-p|-P} {-b|-B|-i|-I LIMIT} [--pool POOL] FILESYSTEM\n"
450          "       setquota {-u|-g|-p} --delete FILESYSTEM\n"},
451         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
452          "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
453          "             [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
454          "             [--pool <OST pool name>] <filesystem>\n"
455          "       quota -t <-u|-g|-p> [--pool <OST pool name>] <filesystem>\n"
456          "       quota [-q] [-v] [h] {-U|-G|-P} [--pool <OST pool name>] <filesystem>"},
457         {"project", lfs_project, 0,
458          "Change or list project attribute for specified file or directory.\n"
459          "usage: project [-d|-r] <file|directory...>\n"
460          "         list project ID and flags on file(s) or directories\n"
461          "       project [-p id] [-s] [-r] <file|directory...>\n"
462          "         set project ID and/or inherit flag for specified file(s) or directories\n"
463          "       project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
464          "         check project ID and flags on file(s) or directories, print outliers\n"
465          "       project -C [-d|-r] [-k] <file|directory...>\n"
466          "         clear the project inherit flag and ID on the file or directory\n"
467         },
468 #endif
469         {"flushctx", lfs_flushctx, 0,
470          "Flush security context for current user.\n"
471          "usage: flushctx [-k] [-r] [mountpoint...]"},
472         {"changelog", lfs_changelog, 0,
473          "Show the metadata changes on an MDT."
474          "\nusage: changelog <mdtname> [startrec [endrec]]"},
475         {"changelog_clear", lfs_changelog_clear, 0,
476          "Indicate that old changelog records up to <endrec> are no longer of "
477          "interest to consumer <id>, allowing the system to free up space.\n"
478          "An <endrec> of 0 means all records.\n"
479          "usage: changelog_clear <mdtname> <id> <endrec>"},
480         {"fid2path", lfs_fid2path, 0,
481          "Resolve the full path(s) for given FID(s). For a specific hardlink "
482          "specify link number <linkno>.\n"
483          "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
484          "<fsname|root> <fid>..."},
485         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
486          "usage: path2fid [--parents] <path> ..."},
487         {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
488          "usage: rmfid <fsname|rootpath> <fid> ..."},
489         {"data_version", lfs_data_version, 0, "Display file data version for "
490          "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
491         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
492          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
493         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
494          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
495          "[--archived] [--lost] [--archive-id NUM] <file> ..."},
496         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
497          "files.\n"
498          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
499          "[--archived] [--lost] <file> ..."},
500         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
501          "given files.\n" "usage: hsm_action <file> ..."},
502         {"hsm_archive", lfs_hsm_archive, 0,
503          "Archive file to external storage.\n"
504          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
505          "<file> ..."},
506         {"hsm_restore", lfs_hsm_restore, 0,
507          "Restore file from external storage.\n"
508          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
509         {"hsm_release", lfs_hsm_release, 0,
510          "Release files from Lustre.\n"
511          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
512         {"hsm_remove", lfs_hsm_remove, 0,
513          "Remove file copy from external storage.\n"
514          "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
515          "[--archive NUM]\n"
516          "                  (FILE [FILE ...] | "
517          "--mntpath MOUNTPATH FID [FID ...])\n"
518          "\n"
519          "Note: To remove an archived copy of a file already deleted from a "
520          "Lustre FS, the\n"
521          "--mntpath option and a list of FIDs must be specified"
522         },
523         {"hsm_cancel", lfs_hsm_cancel, 0,
524          "Cancel requests related to specified files.\n"
525          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
526         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
527          "usage: swap_layouts <path1> <path2>"},
528         {"migrate", lfs_setstripe_migrate, 0,
529          "migrate directories and their inodes between MDTs.\n"
530          "usage: migrate [--mdt-count|-c STRIPE_COUNT] [--directory|-d]\n"
531          "               [--mdt-hash|-H HASH_TYPE]\n"
532          "               [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
533          "               DIRECTORY\n"
534          "\n"
535          "migrate file objects from one OST layout to another\n"
536          "(may be not safe with concurent writes).\n"
537          MIGRATE_USAGE },
538         {"mv", lfs_mv, 0,
539          "To move directories between MDTs. This command is deprecated, "
540          "use \"migrate\" instead.\n"
541          "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
542          "[--verbose|-v]\n"},
543         {"ladvise", lfs_ladvise, 0,
544          "Provide servers with advice about access patterns for a file.\n"
545          "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
546          "               [--background|-b] [--unset|-u]\n\n"
547          "               {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
548          "               {[--mode|-m [READ,WRITE]}\n"
549          "               <file> ...\n"},
550         {"mirror", lfs_mirror, mirror_cmdlist,
551          "lfs commands used to manage files with mirrored components:\n"
552          "lfs mirror create - create a mirrored file or directory\n"
553          "lfs mirror extend - add mirror(s) to an existing file\n"
554          "lfs mirror split  - split a mirror from an existing mirrored file\n"
555          "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
556          "lfs mirror read   - read a mirror content of a mirrored file\n"
557          "lfs mirror write  - write to a mirror of a mirrored file\n"
558          "lfs mirror copy   - copy a mirror to other mirror(s) of a file\n"
559          "lfs mirror verify - verify mirrored file(s)\n"},
560         {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
561          "usage: getsom [-s] [-b] [-f] <path>\n"
562          "\t-s: Only show the size value of the SOM data for a given file\n"
563          "\t-b: Only show the blocks value of the SOM data for a given file\n"
564          "\t-f: Only show the flags value of the SOM data for a given file\n"},
565         {"heat_get", lfs_heat_get, 0,
566          "To get heat of files.\n"
567          "usage: heat_get <file> ...\n"},
568         {"heat_set", lfs_heat_set, 0,
569          "To set heat flags of files.\n"
570          "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
571          "\t--clear|-c: Clear file heat for given files\n"
572          "\t--off|-o:   Turn off file heat for given files\n"
573          "\t--on|-O:    Turn on file heat for given files\n"},
574         {"pcc", lfs_pcc, pcc_cmdlist,
575          "lfs commands used to interact with PCC features:\n"
576          "lfs pcc attach - attach given files to Persistent Client Cache\n"
577          "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
578          "lfs pcc state  - display the PCC state for given files\n"
579          "lfs pcc detach - detach given files from Persistent Client Cache\n"
580          "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
581         {"help", Parser_help, 0, "help"},
582         {"exit", Parser_quit, 0, "quit"},
583         {"quit", Parser_quit, 0, "quit"},
584         {"--version", Parser_version, 0,
585          "output build version of the utility and exit"},
586         {"--list-commands", lfs_list_commands, 0,
587          "list commands supported by the utility and exit"},
588         { 0, 0, 0, NULL }
589 };
590
591 static int check_hashtype(const char *hashtype)
592 {
593         int type_num = atoi(hashtype);
594         int i;
595
596         /* numeric hash type */
597         if (hashtype && lmv_is_known_hash_type(type_num))
598                 return type_num;
599         /* string hash type */
600         for (i = LMV_HASH_TYPE_ALL_CHARS; i < ARRAY_SIZE(mdt_hash_name); i++)
601                 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
602                         return i;
603
604         return 0;
605 }
606
607 static uint32_t check_foreign_type_name(const char *foreign_type_name)
608 {
609         uint32_t i;
610
611         for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
612                 if (!lu_foreign_types[i].lft_name)
613                         break;
614                 if (strcmp(foreign_type_name,
615                            lu_foreign_types[i].lft_name) == 0)
616                         return lu_foreign_types[i].lft_type;
617         }
618
619         return LU_FOREIGN_TYPE_UNKNOWN;
620 }
621
622 static const char *error_loc = "syserror";
623
624 static int
625 migrate_open_files(const char *name, __u64 migration_flags,
626                    const struct llapi_stripe_param *param,
627                    struct llapi_layout *layout, int *fd_src, int *fd_tgt)
628 {
629         int                      fd = -1;
630         int                      fdv = -1;
631         int                      rflags;
632         int                      mdt_index;
633         int                      random_value;
634         char                     parent[PATH_MAX];
635         char                     volatile_file[PATH_MAX];
636         char                    *ptr;
637         int                      rc;
638         struct stat              st;
639         struct stat              stv;
640
641         if (!param && !layout) {
642                 error_loc = "layout information";
643                 return -EINVAL;
644         }
645
646         /* search for file directory pathname */
647         if (strlen(name) > sizeof(parent) - 1) {
648                 error_loc = "source file name";
649                 return -ERANGE;
650         }
651
652         strncpy(parent, name, sizeof(parent));
653         ptr = strrchr(parent, '/');
654         if (!ptr) {
655                 if (!getcwd(parent, sizeof(parent))) {
656                         error_loc = "getcwd";
657                         return -errno;
658                 }
659         } else {
660                 if (ptr == parent) /* leading '/' */
661                         ptr = parent + 1;
662                 *ptr = '\0';
663         }
664
665         /* even if the file is only read, WR mode is nedeed to allow
666          * layout swap on fd
667          */
668         /* Allow migrating even without the key on encrypted files */
669         rflags = O_RDWR | O_NOATIME | O_FILE_ENC;
670         if (!(migration_flags & LLAPI_MIGRATION_NONDIRECT))
671                 rflags |= O_DIRECT;
672 source_open:
673         fd = open(name, rflags);
674         if (fd < 0) {
675                 /* If encrypted file without the key,
676                  * retry mirror extend in O_DIRECT.
677                  */
678                 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
679                     migration_flags & LLAPI_MIGRATION_MIRROR) {
680                         rflags |= O_DIRECT;
681                         goto source_open;
682                 }
683                 rc = -errno;
684                 error_loc = "cannot open source file";
685                 return rc;
686         }
687
688         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
689         if (rc < 0) {
690                 error_loc = "cannot get MDT index";
691                 goto out;
692         }
693
694         do {
695                 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
696                         /* Allow migrating without the key on encrypted files */
697                         O_FILE_ENC;
698                 mode_t open_mode = S_IRUSR | S_IWUSR;
699
700                 if (rflags & O_DIRECT)
701                         open_flags |= O_DIRECT;
702                 random_value = random();
703                 rc = snprintf(volatile_file, sizeof(volatile_file),
704                               "%s/%s:%.4X:%.4X:fd=%.2d", parent,
705                               LUSTRE_VOLATILE_HDR, mdt_index,
706                               random_value, fd);
707                 if (rc >= sizeof(volatile_file)) {
708                         rc = -ENAMETOOLONG;
709                         break;
710                 }
711
712                 /* create, open a volatile file, use caching (ie no directio) */
713                 if (layout) {
714                         /* Returns -1 and sets errno on error: */
715                         fdv = llapi_layout_file_open(volatile_file, open_flags,
716                                                      open_mode, layout);
717                         if (fdv < 0)
718                                 fdv = -errno;
719                 } else {
720                         /* Does the right thing on error: */
721                         fdv = llapi_file_open_param(volatile_file, open_flags,
722                                                     open_mode, param);
723                 }
724         } while (fdv < 0 && (rc = fdv) == -EEXIST);
725
726         if (rc < 0) {
727                 error_loc = "cannot create volatile file";
728                 goto out;
729         }
730
731         /*
732          * In case the MDT does not support creation of volatile files
733          * we should try to unlink it.
734          */
735         (void)unlink(volatile_file);
736
737         /*
738          * Not-owner (root?) special case.
739          * Need to set owner/group of volatile file like original.
740          * This will allow to pass related check during layout_swap.
741          */
742         rc = fstat(fd, &st);
743         if (rc != 0) {
744                 rc = -errno;
745                 error_loc = "cannot stat source file";
746                 goto out;
747         }
748
749         rc = fstat(fdv, &stv);
750         if (rc != 0) {
751                 rc = -errno;
752                 error_loc = "cannot stat volatile";
753                 goto out;
754         }
755
756         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
757                 rc = fchown(fdv, st.st_uid, st.st_gid);
758                 if (rc != 0) {
759                         rc = -errno;
760                         error_loc = "cannot change ownwership of volatile";
761                         goto out;
762                 }
763         }
764
765 out:
766         if (rc < 0) {
767                 if (fd > 0)
768                         close(fd);
769                 if (fdv > 0)
770                         close(fdv);
771         } else {
772                 *fd_src = fd;
773                 *fd_tgt = fdv;
774                 error_loc = NULL;
775         }
776         return rc;
777 }
778
779 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
780 {
781         struct llapi_layout *layout;
782         size_t buf_size = 4 * 1024 * 1024;
783         void *buf = NULL;
784         off_t pos = 0;
785         off_t data_end = 0;
786         size_t page_size = sysconf(_SC_PAGESIZE);
787         bool sparse;
788         int rc;
789
790         layout = llapi_layout_get_by_fd(fd_src, 0);
791         if (layout) {
792                 uint64_t stripe_size;
793
794                 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
795                 if (rc == 0)
796                         buf_size = stripe_size;
797
798                 llapi_layout_free(layout);
799         }
800
801         /* Use a page-aligned buffer for direct I/O */
802         rc = posix_memalign(&buf, page_size, buf_size);
803         if (rc != 0)
804                 return -rc;
805
806         sparse = llapi_file_is_sparse(fd_src);
807         if (sparse) {
808                 rc = ftruncate(fd_dst, pos);
809                 if (rc < 0) {
810                         rc = -errno;
811                         return rc;
812                 }
813         }
814
815         while (1) {
816                 off_t data_off;
817                 size_t to_read, to_write;
818                 ssize_t rsize;
819
820                 if (sparse && pos >= data_end) {
821                         size_t data_size;
822
823                         data_off = llapi_data_seek(fd_src, pos, &data_size);
824                         if (data_off < 0) {
825                                 /* Non-fatal, switch to full copy */
826                                 sparse = false;
827                                 continue;
828                         }
829                         /* hole at the end of file, truncate up to it */
830                         if (!data_size) {
831                                 rc = ftruncate(fd_dst, data_off);
832                                 if (rc < 0)
833                                         goto out;
834                         }
835                         pos = data_off & ~(page_size - 1);
836                         data_end = data_off + data_size;
837                         to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
838                         to_read = MIN(to_read, buf_size);
839                 } else {
840                         to_read = buf_size;
841                 }
842
843                 if (check_file) {
844                         rc = check_file(fd_src);
845                         if (rc < 0)
846                                 goto out;
847                 }
848
849                 rsize = pread(fd_src, buf, to_read, pos);
850                 if (rsize < 0) {
851                         rc = -errno;
852                         goto out;
853                 }
854                 /* EOF */
855                 if (rsize == 0)
856                         break;
857
858                 to_write = rsize;
859                 while (to_write > 0) {
860                         ssize_t written;
861
862                         written = pwrite(fd_dst, buf, to_write, pos);
863                         if (written < 0) {
864                                 rc = -errno;
865                                 goto out;
866                         }
867                         pos += written;
868                         to_write -= written;
869                 }
870                 if (rc || rsize < to_read)
871                         break;
872         }
873
874         rc = fsync(fd_dst);
875         if (rc < 0)
876                 rc = -errno;
877 out:
878         /* Try to avoid page cache pollution after migration. */
879         (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
880         (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
881
882         free(buf);
883         return rc;
884 }
885
886 static int migrate_set_timestamps(int fd, const struct stat *st)
887 {
888         struct timeval tv[2] = {
889                 {.tv_sec = st->st_atime},
890                 {.tv_sec = st->st_mtime}
891         };
892
893         return futimes(fd, tv);
894 }
895
896 static int migrate_block(int fd, int fdv)
897 {
898         struct stat st;
899         __u64   dv1;
900         int     gid;
901         int     rc;
902         int     rc2;
903
904         rc = fstat(fd, &st);
905         if (rc < 0) {
906                 error_loc = "cannot stat source file";
907                 return -errno;
908         }
909
910         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
911         if (rc < 0) {
912                 error_loc = "cannot get dataversion";
913                 return rc;
914         }
915
916         do
917                 gid = random();
918         while (gid == 0);
919
920         /*
921          * The grouplock blocks all concurrent accesses to the file.
922          * It has to be taken after llapi_get_data_version as it would
923          * block it too.
924          */
925         rc = llapi_group_lock(fd, gid);
926         if (rc < 0) {
927                 error_loc = "cannot get group lock";
928                 return rc;
929         }
930
931         rc = migrate_copy_data(fd, fdv, NULL);
932         if (rc < 0) {
933                 error_loc = "data copy failed";
934                 goto out_unlock;
935         }
936
937         /* Make sure we keep original atime/mtime values */
938         rc = migrate_set_timestamps(fdv, &st);
939         if (rc < 0) {
940                 error_loc = "set target file timestamp failed";
941                 goto out_unlock;
942         }
943
944         /*
945          * swap layouts
946          * for a migration we need to check data version on file did
947          * not change.
948          *
949          * Pass in gid=0 since we already own grouplock.
950          */
951         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
952                                            SWAP_LAYOUTS_CHECK_DV1);
953         if (rc == -EAGAIN) {
954                 error_loc = "file changed";
955                 goto out_unlock;
956         } else if (rc < 0) {
957                 error_loc = "cannot swap layout";
958                 goto out_unlock;
959         }
960
961 out_unlock:
962         rc2 = llapi_group_unlock(fd, gid);
963         if (rc2 < 0 && rc == 0) {
964                 error_loc = "unlock group lock";
965                 rc = rc2;
966         }
967
968         return rc;
969 }
970
971 /**
972  * Internal helper for migrate_copy_data(). Check lease and report error if
973  * need be.
974  *
975  * \param[in]  fd           File descriptor on which to check the lease.
976  *
977  * \retval 0       Migration can keep on going.
978  * \retval -errno  Error occurred, abort migration.
979  */
980 static int check_lease(int fd)
981 {
982         int rc;
983
984         rc = llapi_lease_check(fd);
985         if (rc > 0)
986                 return 0; /* llapi_check_lease returns > 0 on success. */
987
988         return -EBUSY;
989 }
990
991 static int migrate_nonblock(int fd, int fdv)
992 {
993         struct stat st;
994         __u64   dv1;
995         __u64   dv2;
996         int     rc;
997
998         rc = fstat(fd, &st);
999         if (rc < 0) {
1000                 error_loc = "cannot stat source file";
1001                 return -errno;
1002         }
1003
1004         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1005         if (rc < 0) {
1006                 error_loc = "cannot get data version";
1007                 return rc;
1008         }
1009
1010         rc = migrate_copy_data(fd, fdv, check_lease);
1011         if (rc < 0) {
1012                 error_loc = "data copy failed";
1013                 return rc;
1014         }
1015
1016         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1017         if (rc != 0) {
1018                 error_loc = "cannot get data version";
1019                 return rc;
1020         }
1021
1022         if (dv1 != dv2) {
1023                 rc = -EAGAIN;
1024                 error_loc = "source file changed";
1025                 return rc;
1026         }
1027
1028         /* Make sure we keep original atime/mtime values */
1029         rc = migrate_set_timestamps(fdv, &st);
1030         if (rc < 0) {
1031                 error_loc = "set target file timestamp failed";
1032                 return -errno;
1033         }
1034         return 0;
1035 }
1036
1037 static
1038 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1039 {
1040         struct pool_to_id_cbdata data = { .pool = pool };
1041         struct llapi_layout *layout = NULL;
1042         int rc;
1043
1044         layout = llapi_layout_get_by_path(fname, 0);
1045         if (!layout) {
1046                 fprintf(stderr,
1047                         "error %s: file '%s' couldn't get layout: rc=%d\n",
1048                         progname, fname, errno);
1049                 rc = -errno;
1050                 goto free_layout;
1051         }
1052         rc = llapi_layout_sanity(layout, false, true);
1053         if (rc < 0) {
1054                 llapi_layout_sanity_perror(errno);
1055                 goto free_layout;
1056         }
1057         rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1058         if (rc < 0)
1059                 goto free_layout;
1060
1061         *comp_id = data.id;
1062         rc = 0;
1063
1064 free_layout:
1065         if (layout)
1066                 llapi_layout_free(layout);
1067         return rc;
1068 }
1069
1070 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1071                              __u32 flags, __u32 neg_flags)
1072 {
1073         __u32 ids[2];
1074         __u32 flags_array[2];
1075         size_t count = 0;
1076         int rc;
1077
1078         if (!comp_id) {
1079                 if (pool == NULL) {
1080                         fprintf(stderr,
1081                                 "error %s: neither component id nor pool is specified\n",
1082                                 progname);
1083                         return -EINVAL;
1084                 }
1085                 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1086                 if (rc)
1087                         return rc;
1088         }
1089
1090         if (flags) {
1091                 ids[count] = comp_id;
1092                 flags_array[count] = flags;
1093                 ++count;
1094         }
1095
1096         if (neg_flags) {
1097                 if (neg_flags & LCME_FL_STALE) {
1098                         fprintf(stderr,
1099                                 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1100                                 progname);
1101                         return -EINVAL;
1102                 }
1103
1104                 ids[count] = comp_id;
1105                 flags_array[count] = neg_flags | LCME_FL_NEG;
1106                 ++count;
1107         }
1108
1109         rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1110         if (rc) {
1111                 if (errno == EUCLEAN) {
1112                         rc = -errno;
1113                         fprintf(stderr,
1114                                 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1115                                 progname, comp_id, fname);
1116                 } else {
1117                         fprintf(stderr,
1118                                 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1119                                 progname, comp_id, fname, flags, neg_flags);
1120                 }
1121         }
1122
1123         return rc;
1124 }
1125
1126 static int lfs_component_del(char *fname, __u32 comp_id,
1127                              __u32 flags, __u32 neg_flags)
1128 {
1129         int     rc = 0;
1130
1131         if (flags && neg_flags) {
1132                 fprintf(stderr,
1133                         "%s: cannot specify both positive and negative flags\n",
1134                         progname);
1135                 return -EINVAL;
1136         }
1137
1138         if (!flags && neg_flags)
1139                 flags = neg_flags | LCME_FL_NEG;
1140
1141         if (flags && comp_id) {
1142                 fprintf(stderr,
1143                         "%s: cannot specify component ID and flags at the same time\n",
1144                         progname);
1145                 return -EINVAL;
1146         }
1147
1148         if (!flags && !comp_id) {
1149                 fprintf(stderr,
1150                         "%s: neither flags nor component ID is specified\n",
1151                         progname);
1152                 return -EINVAL;
1153         }
1154
1155         if (flags) {
1156                 if (flags & ~LCME_KNOWN_FLAGS) {
1157                         fprintf(stderr,
1158                                 "%s setstripe: unknown flags %#x\n",
1159                                 progname, flags);
1160                         return -EINVAL;
1161                 }
1162         } else if (comp_id > LCME_ID_MAX) {
1163                 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1164                         progname, comp_id);
1165                 return -EINVAL;
1166         }
1167
1168         rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1169         if (rc)
1170                 fprintf(stderr,
1171                         "%s setstripe: cannot delete component %#x from '%s': %s\n",
1172                         progname, comp_id, fname, strerror(errno));
1173         return rc;
1174 }
1175
1176 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1177 {
1178         int     rc;
1179
1180         if (!layout)
1181                 return -EINVAL;
1182
1183         rc = llapi_layout_file_comp_add(fname, layout);
1184         if (rc)
1185                 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1186                         fname, strerror(errno));
1187         return rc;
1188 }
1189
1190 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1191                                 struct llapi_layout *layout)
1192 {
1193         struct stat     st;
1194         int     fd;
1195
1196         if (!layout)
1197                 return -EINVAL;
1198
1199         fd = lstat(fname, &st);
1200         if (fd == 0 && S_ISDIR(st.st_mode))
1201                 open_flags = O_DIRECTORY | O_RDONLY;
1202
1203         fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1204         if (fd < 0)
1205                 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1206                         S_ISDIR(st.st_mode) ?
1207                                 "set default composite layout for" :
1208                                 "create composite file",
1209                         fname, strerror(errno));
1210         return fd;
1211 }
1212
1213 static int lfs_migrate(char *name, __u64 migration_flags,
1214                        struct llapi_stripe_param *param,
1215                        struct llapi_layout *layout)
1216 {
1217         struct llapi_layout *existing;
1218         uint64_t dom_new, dom_cur;
1219         int fd = -1;
1220         int fdv = -1;
1221         int rc;
1222
1223         rc = migrate_open_files(name, migration_flags, param, layout,
1224                                 &fd, &fdv);
1225         if (rc < 0)
1226                 goto out;
1227
1228         rc = llapi_layout_dom_size(layout, &dom_new);
1229         if (rc) {
1230                 error_loc = "cannot get new layout DoM size";
1231                 goto out;
1232         }
1233         /* special case for migration to DOM layout*/
1234         existing = llapi_layout_get_by_fd(fd, 0);
1235         if (!existing) {
1236                 error_loc = "cannot get existing layout";
1237                 goto out;
1238         }
1239
1240         rc = llapi_layout_dom_size(existing, &dom_cur);
1241         if (rc) {
1242                 error_loc = "cannot get current layout DoM size";
1243                 goto out;
1244         }
1245
1246         /*
1247          * if file has DoM layout already then migration is possible to
1248          * the new layout with the same DoM component via swap layout,
1249          * if new layout used bigger DOM size, then mirroring is used
1250          */
1251         if (dom_new > dom_cur) {
1252                 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags);
1253                 if (rc)
1254                         error_loc = "cannot migrate to DOM layout";
1255                 goto out_closed;
1256         }
1257
1258         if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1259                 /*
1260                  * Blocking mode (forced if servers do not support file lease).
1261                  * It is also the default mode, since we cannot distinguish
1262                  * between a broken lease and a server that does not support
1263                  * atomic swap/close (LU-6785)
1264                  */
1265                 rc = migrate_block(fd, fdv);
1266                 goto out;
1267         }
1268
1269         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1270         if (rc < 0) {
1271                 error_loc = "cannot get lease";
1272                 goto out;
1273         }
1274
1275         rc = migrate_nonblock(fd, fdv);
1276         if (rc < 0) {
1277                 llapi_lease_release(fd);
1278                 goto out;
1279         }
1280
1281         /*
1282          * Atomically put lease, swap layouts and close.
1283          * for a migration we need to check data version on file did
1284          * not change.
1285          */
1286         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1287         if (rc < 0) {
1288                 error_loc = "cannot swap layout";
1289                 goto out;
1290         }
1291
1292 out:
1293         if (fd >= 0)
1294                 close(fd);
1295
1296         if (fdv >= 0)
1297                 close(fdv);
1298 out_closed:
1299         if (rc < 0)
1300                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1301                         progname, name, error_loc, strerror(-rc));
1302         else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1303                 printf("%s\n", name);
1304
1305         return rc;
1306 }
1307
1308 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1309 {
1310         char *name;
1311         char *dup_string = NULL;
1312         int rc = 0;
1313
1314         *flags = 0;
1315         *neg_flags = 0;
1316
1317         if (!string || !string[0])
1318                 return -EINVAL;
1319
1320         dup_string = strdup(string);
1321         if (!dup_string) {
1322                 llapi_printf(LLAPI_MSG_ERROR,
1323                              "%s: insufficient memory\n",
1324                              progname);
1325                 return -ENOMEM;
1326         }
1327
1328         for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1329                 bool found = false;
1330                 int i;
1331
1332                 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1333                         __u32 comp_flag = comp_flags_table[i].cfn_flag;
1334                         const char *comp_name = comp_flags_table[i].cfn_name;
1335
1336                         if (strcmp(name, comp_name) == 0) {
1337                                 *flags |= comp_flag;
1338                                 found = true;
1339                         } else if (strncmp(name, "^", 1) == 0 &&
1340                                    strcmp(name + 1, comp_name) == 0) {
1341                                 *neg_flags |= comp_flag;
1342                                 found = true;
1343                         }
1344                 }
1345                 if (!found) {
1346                         llapi_printf(LLAPI_MSG_ERROR,
1347                                      "%s: component flag '%s' not supported\n",
1348                                      progname, name);
1349                         rc = -EINVAL;
1350                         goto out_free;
1351                 }
1352         }
1353
1354         if (!*flags && !*neg_flags)
1355                 rc = -EINVAL;
1356
1357         /* don't allow to set and exclude the same flag */
1358         if (*flags & *neg_flags)
1359                 rc = -EINVAL;
1360
1361 out_free:
1362         free(dup_string);
1363         return rc;
1364 }
1365
1366 static int mdthash_input(char *string, __u32 *inflags,
1367                          __u32 *exflags, __u32 *type)
1368 {
1369         char *name;
1370         struct mhf_list {
1371                 char *name;
1372                 __u32 flag;
1373         } mhflist[] = {
1374                 {"migrating", LMV_HASH_FLAG_MIGRATION},
1375                 {"bad_type", LMV_HASH_FLAG_BAD_TYPE},
1376                 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1377                 {"lost_lmv", LMV_HASH_FLAG_LOST_LMV},
1378                 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1379         };
1380
1381         if (string == NULL)
1382                 return -EINVAL;
1383
1384         *inflags = 0;
1385         *exflags = 0;
1386         *type = 0;
1387         for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1388                 bool found = false;
1389                 int i;
1390
1391                 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1392                         if (strcmp(name, mhflist[i].name) == 0 ||
1393                             name[0] == mhflist[i].name[0]) {
1394                                 *inflags |= mhflist[i].flag;
1395                                 found = true;
1396                         } else if (name[0] == '^' &&
1397                                    (strcmp(name + 1, mhflist[i].name) == 0 ||
1398                                     name[1] == mhflist[i].name[0])) {
1399                                 *exflags |= mhflist[i].flag;
1400                                 found = true;
1401                         }
1402                 }
1403                 if (!found) {
1404                         i = check_hashtype(name);
1405                         if (i > 0) {
1406                                 *type |= 1 << i;
1407                                 continue;
1408                         }
1409                         llapi_printf(LLAPI_MSG_ERROR,
1410                                      "%s: invalid mdt_hash value '%s'\n",
1411                                      progname, name);
1412                         return -EINVAL;
1413                 }
1414         }
1415
1416         /* don't allow to include and exclude the same flag */
1417         if (*inflags & *exflags) {
1418                 llapi_printf(LLAPI_MSG_ERROR,
1419                              "%s: include and exclude same flag '%s'\n",
1420                              progname, string);
1421                 return -EINVAL;
1422         }
1423
1424         return 0;
1425 }
1426
1427 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1428 {
1429         if (!string)
1430                 return -EINVAL;
1431
1432         *state = 0;
1433         *neg_state = 0;
1434
1435         if (strncmp(string, "^", 1) == 0) {
1436                 *neg_state = llapi_layout_string_flags(string + 1);
1437                 if (*neg_state != 0)
1438                         return 0;
1439         } else {
1440                 *state = llapi_layout_string_flags(string);
1441                 if (*state != 0)
1442                         return 0;
1443         }
1444
1445         llapi_printf(LLAPI_MSG_ERROR,
1446                      "%s: mirrored file state '%s' not supported\n",
1447                      progname, string);
1448         return -EINVAL;
1449 }
1450
1451 /**
1452  * struct mirror_args - Command-line arguments for mirror(s).
1453  * @m_count:  Number of mirrors to be created with this layout.
1454  * @m_flags:  Mirror level flags, only 'prefer' is supported.
1455  * @m_layout: Mirror layout.
1456  * @m_file:   A victim file. Its layout will be split and used as a mirror.
1457  * @m_next:   Point to the next node of the list.
1458  *
1459  * Command-line arguments for mirror(s) will be parsed and stored in
1460  * a linked list that consists of this structure.
1461  */
1462 struct mirror_args {
1463         __u32                   m_count;
1464         __u32                   m_flags;
1465         struct llapi_layout     *m_layout;
1466         const char              *m_file;
1467         struct mirror_args      *m_next;
1468         bool                    m_inherit;
1469 };
1470
1471 /**
1472  * enum mirror_flags - Flags for extending a mirrored file.
1473  * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1474  *             in case the victim file(s) contains the same data as the
1475  *             original mirrored file.
1476  * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1477  * @MF_COMP_ID: specified component id instead of mirror id
1478  *
1479  * Flags for extending a mirrored file.
1480  */
1481 enum mirror_flags {
1482         MF_NO_VERIFY    = 0x1,
1483         MF_DESTROY      = 0x2,
1484         MF_COMP_ID      = 0x4,
1485         MF_COMP_POOL    = 0x8,
1486 };
1487
1488 /**
1489  * mirror_create_sanity_check() - Check mirror list.
1490  * @list:  A linked list that stores the mirror arguments.
1491  *
1492  * This function does a sanity check on @list for creating
1493  * a mirrored file.
1494  *
1495  * Return: 0 on success or a negative error code on failure.
1496  */
1497 static int mirror_create_sanity_check(const char *fname,
1498                                       struct mirror_args *list,
1499                                       bool check_fname)
1500 {
1501         int rc = 0;
1502         bool has_m_file = false;
1503         bool has_m_layout = false;
1504
1505         if (!list)
1506                 return -EINVAL;
1507
1508         if (fname && check_fname) {
1509                 struct llapi_layout *layout;
1510
1511                 layout = llapi_layout_get_by_path(fname, 0);
1512                 if (!layout) {
1513                         fprintf(stderr,
1514                                 "error: %s: file '%s' couldn't get layout\n",
1515                                 progname, fname);
1516                         return -ENODATA;
1517                 }
1518
1519                 rc = llapi_layout_sanity(layout, false, true);
1520
1521                 llapi_layout_free(layout);
1522
1523                 if (rc) {
1524                         llapi_layout_sanity_perror(rc);
1525                         return rc;
1526                 }
1527         }
1528
1529         while (list) {
1530                 if (list->m_file) {
1531                         has_m_file = true;
1532                         llapi_layout_free(list->m_layout);
1533
1534                         list->m_layout =
1535                                 llapi_layout_get_by_path(list->m_file, 0);
1536                         if (!list->m_layout) {
1537                                 fprintf(stderr,
1538                                         "error: %s: file '%s' has no layout\n",
1539                                         progname, list->m_file);
1540                                 return -ENODATA;
1541                         }
1542                 } else {
1543                         has_m_layout = true;
1544                         if (!list->m_layout) {
1545                                 fprintf(stderr, "error: %s: no mirror layout\n",
1546                                         progname);
1547                                 return -EINVAL;
1548                         }
1549                 }
1550
1551                 rc = llapi_layout_sanity(list->m_layout, false, true);
1552                 if (rc) {
1553                         llapi_layout_sanity_perror(rc);
1554                         return rc;
1555                 }
1556
1557                 list = list->m_next;
1558         }
1559
1560         if (has_m_file && has_m_layout) {
1561                 fprintf(stderr,
1562                         "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1563                         progname);
1564                 return -EINVAL;
1565         }
1566
1567         return 0;
1568 }
1569
1570 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1571 {
1572         __u32 mirror_flags = *(__u32 *)cbdata;
1573         uint32_t flags;
1574         int rc;
1575
1576         rc = llapi_layout_comp_flags_get(layout, &flags);
1577         if (rc < 0)
1578                 return rc;
1579
1580         if (!flags) {
1581                 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1582                 if (rc)
1583                         return rc;
1584         }
1585
1586         return LLAPI_LAYOUT_ITER_CONT;
1587 }
1588
1589 /**
1590  * mirror_create() - Create a mirrored file.
1591  * @fname:        The file to be created.
1592  * @mirror_list:  A linked list that stores the mirror arguments.
1593  *
1594  * This function creates a mirrored file @fname with the mirror(s)
1595  * from @mirror_list.
1596  *
1597  * Return: 0 on success or a negative error code on failure.
1598  */
1599 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1600 {
1601         struct llapi_layout *layout = NULL;
1602         struct mirror_args *cur_mirror = NULL;
1603         uint16_t mirror_count = 0;
1604         int i = 0;
1605         int rc = 0;
1606
1607         rc = mirror_create_sanity_check(fname, mirror_list, false);
1608         if (rc)
1609                 return rc;
1610
1611         cur_mirror = mirror_list;
1612         while (cur_mirror) {
1613                 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1614                                                mirror_set_flags,
1615                                                &cur_mirror->m_flags);
1616                 if (rc) {
1617                         rc = -errno;
1618                         fprintf(stderr, "%s: failed to set mirror flags\n",
1619                                 progname);
1620                         goto error;
1621                 }
1622
1623                 for (i = 0; i < cur_mirror->m_count; i++) {
1624                         rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1625                         if (rc) {
1626                                 rc = -errno;
1627                                 fprintf(stderr,
1628                                         "error: %s: merge layout failed: %s\n",
1629                                         progname, strerror(errno));
1630                                 goto error;
1631                         }
1632                 }
1633                 mirror_count += cur_mirror->m_count;
1634                 cur_mirror = cur_mirror->m_next;
1635         }
1636
1637         if (!layout) {
1638                 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1639                 return -EINVAL;
1640         }
1641
1642         rc = llapi_layout_mirror_count_set(layout, mirror_count);
1643         if (rc) {
1644                 rc = -errno;
1645                 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1646                         progname, strerror(errno));
1647                 goto error;
1648         }
1649
1650         rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1651                                   layout);
1652         if (rc >= 0) {
1653                 close(rc);
1654                 rc = 0;
1655         }
1656
1657 error:
1658         llapi_layout_free(layout);
1659         return rc;
1660 }
1661
1662 /**
1663  * Compare files and check lease on @fd.
1664  *
1665  * \retval bytes number of bytes are the same
1666  */
1667 static ssize_t mirror_file_compare(int fd, int fdv)
1668 {
1669         const size_t buflen = 4 * 1024 * 1024; /* 4M */
1670         void *buf;
1671         ssize_t bytes_done = 0;
1672         ssize_t bytes_read = 0;
1673
1674         buf = malloc(buflen * 2);
1675         if (!buf)
1676                 return -ENOMEM;
1677
1678         while (1) {
1679                 if (!llapi_lease_check(fd)) {
1680                         bytes_done = -EBUSY;
1681                         break;
1682                 }
1683
1684                 bytes_read = read(fd, buf, buflen);
1685                 if (bytes_read <= 0)
1686                         break;
1687
1688                 if (bytes_read != read(fdv, buf + buflen, buflen))
1689                         break;
1690
1691                 /*
1692                  * XXX: should compute the checksum on each buffer and then
1693                  * compare checksum to avoid cache collision
1694                  */
1695                 if (memcmp(buf, buf + buflen, bytes_read))
1696                         break;
1697
1698                 bytes_done += bytes_read;
1699         }
1700
1701         free(buf);
1702
1703         return bytes_done;
1704 }
1705
1706 static int mirror_extend_file(const char *fname, const char *victim_file,
1707                               enum mirror_flags mirror_flags)
1708 {
1709         int fd = -1;
1710         int fdv = -1;
1711         struct stat stbuf;
1712         struct stat stbuf_v;
1713         struct ll_ioc_lease *data = NULL;
1714         int rc;
1715
1716         fd = open(fname, O_RDWR);
1717         if (fd < 0) {
1718                 error_loc = "open source file";
1719                 rc = -errno;
1720                 goto out;
1721         }
1722
1723         fdv = open(victim_file, O_RDWR);
1724         if (fdv < 0) {
1725                 error_loc = "open target file";
1726                 rc = -errno;
1727                 goto out;
1728         }
1729
1730         if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1731                 error_loc = "stat source or target file";
1732                 rc = -errno;
1733                 goto out;
1734         }
1735
1736         if (stbuf.st_dev != stbuf_v.st_dev) {
1737                 error_loc = "stat source and target file";
1738                 rc = -EXDEV;
1739                 goto out;
1740         }
1741
1742         /* mirrors should be of the same size */
1743         if (stbuf.st_size != stbuf_v.st_size) {
1744                 error_loc = "file sizes don't match";
1745                 rc = -EINVAL;
1746                 goto out;
1747         }
1748
1749         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1750         if (rc < 0) {
1751                 error_loc = "cannot get lease";
1752                 goto out;
1753         }
1754
1755         if (!(mirror_flags & MF_NO_VERIFY)) {
1756                 ssize_t ret;
1757                 /* mirrors should have the same contents */
1758                 ret = mirror_file_compare(fd, fdv);
1759                 if (ret != stbuf.st_size) {
1760                         error_loc = "file busy or contents don't match";
1761                         rc = ret < 0 ? ret : -EINVAL;
1762                         goto out;
1763                 }
1764         }
1765
1766         /* Get rid of caching pages from clients */
1767         rc = llapi_file_flush(fd);
1768         if (rc < 0) {
1769                 error_loc = "cannot get data version";
1770                 goto out;
1771         }
1772
1773         rc = llapi_file_flush(fdv);
1774         if (rc < 0) {
1775                 error_loc = "cannot get data version";
1776                 goto out;
1777         }
1778
1779         rc = migrate_set_timestamps(fd, &stbuf);
1780         if (rc < 0) {
1781                 error_loc = "cannot set source file timestamp";
1782                 goto out;
1783         }
1784
1785         /* Atomically put lease, merge layouts and close. */
1786         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1787         if (!data) {
1788                 error_loc = "memory allocation";
1789                 goto out;
1790         }
1791         data->lil_mode = LL_LEASE_UNLCK;
1792         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1793         data->lil_count = 1;
1794         data->lil_ids[0] = fdv;
1795         rc = llapi_lease_set(fd, data);
1796         if (rc < 0) {
1797                 error_loc = "cannot merge layout";
1798                 goto out;
1799         } else if (rc == 0) {
1800                 rc = -EBUSY;
1801                 error_loc = "lost lease lock";
1802                 goto out;
1803         }
1804         rc = 0;
1805
1806 out:
1807         if (data)
1808                 free(data);
1809         if (fd >= 0)
1810                 close(fd);
1811         if (fdv >= 0)
1812                 close(fdv);
1813         if (!rc)
1814                 (void) unlink(victim_file);
1815         if (rc < 0)
1816                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1817                         progname, fname, error_loc, strerror(-rc));
1818         return rc;
1819 }
1820
1821 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1822                                 bool inherit, uint32_t flags)
1823 {
1824         struct llapi_layout *f_layout = NULL;
1825         struct ll_ioc_lease *data = NULL;
1826         struct stat st;
1827         int fd = -1;
1828         int fdv = -1;
1829         int rc = 0;
1830
1831         if (inherit) {
1832                 f_layout = llapi_layout_get_by_path(name, 0);
1833                 if (!f_layout) {
1834                         rc = -EINVAL;
1835                         fprintf(stderr, "%s: cannot get layout\n", progname);
1836                         goto out;
1837                 }
1838                 rc = llapi_layout_get_last_init_comp(f_layout);
1839                 if (rc) {
1840                         fprintf(stderr, "%s: cannot get the last init comp\n",
1841                                 progname);
1842                         goto out;
1843                 }
1844                 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1845                 if (rc) {
1846                         fprintf(stderr,
1847                                 "%s: cannot inherit from the last init comp\n",
1848                                 progname);
1849                         goto out;
1850                 }
1851         }
1852
1853         llapi_layout_comp_flags_set(m_layout, flags);
1854         rc = migrate_open_files(name,
1855                              LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1856                              NULL, m_layout, &fd, &fdv);
1857         if (rc < 0)
1858                 goto out;
1859
1860         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1861         if (rc < 0) {
1862                 error_loc = "cannot get lease";
1863                 goto out;
1864         }
1865
1866         rc = fstat(fd, &st);
1867         if (rc < 0) {
1868                 error_loc = "cannot stat source file";
1869                 goto out;
1870         }
1871
1872         rc = migrate_nonblock(fd, fdv);
1873         if (rc < 0) {
1874                 llapi_lease_release(fd);
1875                 goto out;
1876         }
1877
1878         rc = migrate_set_timestamps(fd, &st);
1879         if (rc < 0) {
1880                 error_loc = "cannot set source file timestamp";
1881                 goto out;
1882         }
1883
1884         /* Atomically put lease, merge layouts and close. */
1885         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1886         if (!data) {
1887                 error_loc = "memory allocation";
1888                 goto out;
1889         }
1890         data->lil_mode = LL_LEASE_UNLCK;
1891         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1892         data->lil_count = 1;
1893         data->lil_ids[0] = fdv;
1894         rc = llapi_lease_set(fd, data);
1895         if (rc < 0) {
1896                 error_loc = "cannot merge layout";
1897                 goto out;
1898         } else if (rc == 0) {
1899                 rc = -EBUSY;
1900                 error_loc = "lost lease lock";
1901                 goto out;
1902         }
1903         rc = 0;
1904
1905 out:
1906         if (data)
1907                 free(data);
1908         if (fd >= 0)
1909                 close(fd);
1910         if (fdv >= 0)
1911                 close(fdv);
1912         if (rc < 0)
1913                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1914                         progname, name, error_loc, strerror(-rc));
1915         return rc;
1916 }
1917
1918 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1919                          enum mirror_flags mirror_flags)
1920 {
1921         int rc = 0;
1922
1923         while (mirror_list) {
1924                 if (mirror_list->m_file) {
1925                         rc = mirror_extend_file(fname, mirror_list->m_file,
1926                                                 mirror_flags);
1927                 } else {
1928                         __u32 mirror_count = mirror_list->m_count;
1929
1930                         while (mirror_count > 0) {
1931                                 rc = mirror_extend_layout(fname,
1932                                                         mirror_list->m_layout,
1933                                                         mirror_list->m_inherit,
1934                                                         mirror_list->m_flags);
1935                                 if (rc)
1936                                         break;
1937
1938                                 --mirror_count;
1939                         }
1940                 }
1941                 if (rc)
1942                         break;
1943
1944                 mirror_list = mirror_list->m_next;
1945         }
1946
1947         return rc;
1948 }
1949
1950 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1951 {
1952         uint32_t id;
1953         int rc;
1954
1955         rc = llapi_layout_mirror_id_get(layout, &id);
1956         if (rc < 0)
1957                 return rc;
1958
1959         if ((__u16)id == *(__u16 *)cbdata)
1960                 return LLAPI_LAYOUT_ITER_STOP;
1961
1962         return LLAPI_LAYOUT_ITER_CONT;
1963 }
1964
1965 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1966 {
1967         uint32_t id;
1968         int rc;
1969
1970         rc = llapi_layout_comp_id_get(layout, &id);
1971         if (rc < 0)
1972                 return rc;
1973
1974         if (id == *(__u32 *)cbdata)
1975                 return LLAPI_LAYOUT_ITER_STOP;
1976
1977         return LLAPI_LAYOUT_ITER_CONT;
1978 }
1979
1980 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
1981 {
1982         char buf[LOV_MAXPOOLNAME + 1];
1983         struct pool_to_id_cbdata *d = (void *)cbdata;
1984         uint32_t id;
1985         int rc;
1986
1987         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1988         if (rc < 0)
1989                 return rc;
1990         if (strcmp(d->pool, buf))
1991                 return LLAPI_LAYOUT_ITER_CONT;
1992
1993         rc = llapi_layout_mirror_id_get(layout, &id);
1994         if (rc < 0)
1995                 return rc;
1996         d->id = id;
1997
1998         return LLAPI_LAYOUT_ITER_STOP;
1999 }
2000
2001 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2002 {
2003         char buf[LOV_MAXPOOLNAME + 1];
2004         struct pool_to_id_cbdata *d = (void *)cbdata;
2005         uint32_t id;
2006         int rc;
2007
2008         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2009         if (rc < 0)
2010                 return rc;
2011         if (strcmp(d->pool, buf))
2012                 return LLAPI_LAYOUT_ITER_CONT;
2013
2014         rc = llapi_layout_comp_id_get(layout, &id);
2015         if (rc < 0)
2016                 return rc;
2017         d->id = id;
2018
2019         return LLAPI_LAYOUT_ITER_STOP;
2020 }
2021
2022 struct collect_ids_data {
2023         __u16   *cid_ids;
2024         int     cid_count;
2025         __u16   cid_exclude;
2026 };
2027
2028 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2029 {
2030         struct collect_ids_data *cid = cbdata;
2031         uint32_t id;
2032         int rc;
2033
2034         rc = llapi_layout_mirror_id_get(layout, &id);
2035         if (rc < 0)
2036                 return rc;
2037
2038         if ((__u16)id != cid->cid_exclude) {
2039                 int i;
2040
2041                 for (i = 0; i < cid->cid_count; i++) {
2042                         /* already collected the mirror id */
2043                         if (id == cid->cid_ids[i])
2044                                 return LLAPI_LAYOUT_ITER_CONT;
2045                 }
2046                 cid->cid_ids[cid->cid_count] = id;
2047                 cid->cid_count++;
2048         }
2049
2050         return LLAPI_LAYOUT_ITER_CONT;
2051 }
2052
2053 /**
2054  * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2055  * @mirror_id: Mirror id to be checked.
2056  * @layout:    Mirror component list.
2057  *
2058  * This function checks if a mirror with specified @mirror_id is the last
2059  * non-stale mirror of a layout @layout.
2060  *
2061  * Return: true or false.
2062  */
2063 static inline
2064 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2065 {
2066         __u16 mirror_ids[128] = { 0 };
2067         struct collect_ids_data cid = { .cid_ids = mirror_ids,
2068                                         .cid_count = 0,
2069                                         .cid_exclude = mirror_id, };
2070         int i;
2071
2072         llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2073
2074         for (i = 0; i < cid.cid_count; i++) {
2075                 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2076                 int comp_size = 0;
2077
2078                 comp_size = llapi_mirror_find_stale(layout, comp_array,
2079                                                     ARRAY_SIZE(comp_array),
2080                                                     &mirror_ids[i], 1);
2081                 if (comp_size == 0)
2082                         return false;
2083         }
2084
2085         return true;
2086 }
2087
2088 static int mirror_split(const char *fname, __u32 id, const char *pool,
2089                         enum mirror_flags mflags, const char *victim_file)
2090 {
2091         struct llapi_layout *layout;
2092         char parent[PATH_MAX];
2093         char victim[PATH_MAX];
2094         int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2095         char *ptr;
2096         struct ll_ioc_lease *data;
2097         uint16_t mirror_count;
2098         __u32 mirror_id;
2099         int mdt_index;
2100         int fd, fdv;
2101         bool purge = true; /* delete mirror by setting fdv=fd */
2102         bool is_encrypted;
2103         int rc;
2104
2105         if (victim_file && (strcmp(fname, victim_file) == 0)) {
2106                 fprintf(stderr,
2107                         "error %s: the source file '%s' and -f file are the same\n",
2108                         progname, fname);
2109                 return -EINVAL;
2110         }
2111
2112         /* check fname contains mirror with mirror_id/comp_id */
2113         layout = llapi_layout_get_by_path(fname, 0);
2114         if (!layout) {
2115                 fprintf(stderr,
2116                         "error %s: file '%s' couldn't get layout\n",
2117                         progname, fname);
2118                 return -EINVAL;
2119         }
2120
2121         rc = llapi_layout_sanity(layout, false, true);
2122         if (rc) {
2123                 llapi_layout_sanity_perror(rc);
2124                 goto free_layout;
2125         }
2126
2127         rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2128         if (rc) {
2129                 fprintf(stderr,
2130                         "error %s: file '%s' couldn't get mirror count\n",
2131                         progname, fname);
2132                 goto free_layout;
2133         }
2134         if (mirror_count < 2) {
2135                 fprintf(stderr,
2136                         "error %s: file '%s' has %d component, cannot split\n",
2137                         progname, fname, mirror_count);
2138                 goto free_layout;
2139         }
2140
2141         if (mflags & MF_COMP_POOL) {
2142                 struct pool_to_id_cbdata data = { .pool = pool };
2143
2144                 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2145                                                &data);
2146                 mirror_id = data.id;
2147         } else if (mflags & MF_COMP_ID) {
2148                 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2149                 mirror_id = mirror_id_of(id);
2150         } else {
2151                 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2152                 mirror_id = id;
2153         }
2154         if (rc < 0) {
2155                 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2156                         progname, fname);
2157                 goto free_layout;
2158         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2159                 if (mflags & MF_COMP_POOL) {
2160                         fprintf(stderr,
2161                                 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2162                                 progname, fname, pool);
2163                         goto free_layout;
2164                 } else if (mflags & MF_COMP_ID) {
2165                         fprintf(stderr,
2166                                 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2167                                 progname, fname, id);
2168                         goto free_layout;
2169                 } else {
2170                         fprintf(stderr,
2171                                 "error %s: file '%s' does not contain mirror with id %u\n",
2172                                 progname, fname, id);
2173                         goto free_layout;
2174                 }
2175         }
2176
2177         if (!victim_file && mflags & MF_DESTROY)
2178                 /* Allow mirror split even without the key on encrypted files,
2179                  * and in this case of a 'split -d', open file with O_DIRECT
2180                  * (no IOs will be done).
2181                  */
2182                 fd = open(fname, O_RDWR | O_DIRECT | O_FILE_ENC);
2183         else
2184                 fd = open(fname, O_RDWR);
2185
2186         if (fd < 0) {
2187                 fprintf(stderr,
2188                         "error %s: open file '%s' failed: %s\n",
2189                         progname, fname, strerror(errno));
2190                 goto free_layout;
2191         }
2192
2193         /* get victim file directory pathname */
2194         if (strlen(fname) > sizeof(parent) - 1) {
2195                 fprintf(stderr, "error %s: file name of '%s' too long\n",
2196                         progname, fname);
2197                 rc = -ERANGE;
2198                 goto close_fd;
2199         }
2200         strncpy(parent, fname, sizeof(parent));
2201         ptr = strrchr(parent, '/');
2202         if (!ptr) {
2203                 if (!getcwd(parent, sizeof(parent))) {
2204                         fprintf(stderr, "error %s: getcwd failed: %s\n",
2205                                 progname, strerror(errno));
2206                         rc = -errno;
2207                         goto close_fd;
2208                 }
2209         } else {
2210                 if (ptr == parent)
2211                         ptr = parent + 1;
2212                 *ptr = '\0';
2213         }
2214
2215         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2216         if (rc < 0) {
2217                 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2218                         progname, fname);
2219                 goto close_fd;
2220         }
2221
2222         rc = llapi_file_is_encrypted(fd);
2223         if (rc < 0) {
2224                 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2225                         progname, fname, rc);
2226                 goto close_fd;
2227         }
2228         is_encrypted = rc;
2229
2230 again:
2231         if (!victim_file) {
2232                 /* use a temp file to store the splitted layout */
2233                 if (mflags & MF_DESTROY) {
2234                         char file_path[PATH_MAX];
2235                         unsigned int rnumber;
2236                         int open_flags;
2237
2238                         if (last_non_stale_mirror(mirror_id, layout)) {
2239                                 rc = -EUCLEAN;
2240                                 fprintf(stderr,
2241                                         "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2242                                         progname, fname);
2243                                 goto close_fd;
2244                         }
2245
2246                         if (purge) {
2247                                 /* don't use volatile file for mirror destroy */
2248                                 fdv = fd;
2249                         } else {
2250                                 /**
2251                                  * try the old way to delete mirror using
2252                                  * volatile file.
2253                                  */
2254                                 do {
2255                                         rnumber = random();
2256                                         rc = snprintf(file_path,
2257                                                       sizeof(file_path),
2258                                                       "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2259                                                       parent, mdt_index,
2260                                                       rnumber, fd);
2261                                         if (rc < 0 ||
2262                                             rc >= sizeof(file_path)) {
2263                                                 fdv = -ENAMETOOLONG;
2264                                                 break;
2265                                         }
2266
2267                                         open_flags = O_RDWR |
2268                                              (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2269                                              O_CREAT | O_EXCL | O_NOFOLLOW |
2270                                              /* O_DIRECT for mirror split -d */
2271                                              O_DIRECT |
2272                                              /* Allow split without the key */
2273                                              O_FILE_ENC;
2274                                         fdv = open(file_path, open_flags,
2275                                                    S_IRUSR | S_IWUSR);
2276                                         if (fdv < 0)
2277                                                 rc = -errno;
2278                                 } while (fdv < 0 && rc == -EEXIST);
2279                         }
2280                 } else {
2281                         if (is_encrypted) {
2282                                 rc = -1;
2283                                 fprintf(stderr,
2284                                         "error %s: not permitted on encrypted file '%s': %d\n",
2285                                         progname, fname, rc);
2286                                 goto close_fd;
2287                         }
2288
2289                         snprintf(victim, sizeof(victim), "%s.mirror~%u",
2290                                  fname, mirror_id);
2291                         fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2292                 }
2293         } else {
2294                 /* user specified victim file */
2295                 if (is_encrypted) {
2296                         rc = -1;
2297                         fprintf(stderr,
2298                                 "error %s: not permitted on encrypted file '%s': %d\n",
2299                                 progname, fname, rc);
2300                         goto close_fd;
2301                 }
2302                 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2303         }
2304
2305         if (fdv < 0) {
2306                 fprintf(stderr,
2307                         "error %s: create victim file failed: %s\n",
2308                         progname, strerror(errno));
2309                 goto close_fd;
2310         }
2311
2312         /* get lease lock of fname */
2313         rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2314         if (rc < 0) {
2315                 fprintf(stderr,
2316                         "error %s: cannot get lease of file '%s': %d\n",
2317                         progname, fname, rc);
2318                 goto close_victim;
2319         }
2320
2321         /* Atomatically put lease, split layouts and close. */
2322         data = malloc(offsetof(typeof(*data), lil_ids[2]));
2323         if (!data) {
2324                 rc = -ENOMEM;
2325                 goto close_victim;
2326         }
2327
2328         data->lil_mode = LL_LEASE_UNLCK;
2329         data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2330         data->lil_count = 2;
2331         data->lil_ids[0] = fdv;
2332         data->lil_ids[1] = mirror_id;
2333         rc = llapi_lease_set(fd, data);
2334         if (rc <= 0) {
2335                 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2336                         /* could be old MDS which prohibit fd==fdv */
2337                         purge = false;
2338                         goto again;
2339
2340                 }
2341                 if (rc == 0) /* lost lease lock */
2342                         rc = -EBUSY;
2343                 fprintf(stderr,
2344                         "error %s: cannot split '%s': %s\n",
2345                         progname, fname, strerror(-rc));
2346         } else {
2347                 rc = 0;
2348         }
2349         free(data);
2350
2351 close_victim:
2352         if (!purge)
2353                 close(fdv);
2354 close_fd:
2355         close(fd);
2356 free_layout:
2357         llapi_layout_free(layout);
2358         return rc;
2359 }
2360
2361 static inline
2362 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2363                            __u16 *mirror_ids, int ids_nr);
2364
2365 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2366                               __u64 migration_flags)
2367 {
2368         struct ll_ioc_lease *data = NULL;
2369         int rc;
2370
2371         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2372         if (rc < 0) {
2373                 error_loc = "cannot get lease";
2374                 goto out_close;
2375         }
2376
2377         rc = migrate_nonblock(fd, fdv);
2378         if (rc < 0)
2379                 goto out_release;
2380
2381         /* Atomically put lease, merge layouts, resync and close. */
2382         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2383         if (!data) {
2384                 error_loc = "memory allocation";
2385                 goto out_release;
2386         }
2387         data->lil_mode = LL_LEASE_UNLCK;
2388         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2389         data->lil_count = 1;
2390         data->lil_ids[0] = fdv;
2391         rc = llapi_lease_set(fd, data);
2392         if (rc < 0) {
2393                 error_loc = "cannot merge layout";
2394                 goto out_close;
2395         } else if (rc == 0) {
2396                 rc = -EBUSY;
2397                 error_loc = "lost lease lock";
2398                 goto out_close;
2399         }
2400         close(fd);
2401         close(fdv);
2402
2403         rc = lfs_mirror_resync_file(name, data, NULL, 0);
2404         if (rc) {
2405                 error_loc = "cannot resync file";
2406                 goto out;
2407         }
2408
2409         /* delete first mirror now */
2410         rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2411         if (rc < 0)
2412                 error_loc = "cannot delete old layout";
2413         goto out;
2414
2415 out_release:
2416         llapi_lease_release(fd);
2417 out_close:
2418         close(fd);
2419         close(fdv);
2420 out:
2421         if (rc < 0)
2422                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2423                         progname, name, error_loc, strerror(-rc));
2424         else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2425                 printf("%s\n", name);
2426         if (data)
2427                 free(data);
2428         return rc;
2429 }
2430
2431 /**
2432  * Parse a string containing an target index list into an array of integers.
2433  *
2434  * The input string contains a comma delimited list of individual
2435  * indices and ranges, for example "1,2-4,7". Add the indices into the
2436  * \a tgts array and remove duplicates.
2437  *
2438  * \param[out] tgts             array to store indices in
2439  * \param[in] size              size of \a tgts array
2440  * \param[in] offset            starting index in \a tgts
2441  * \param[in] arg               string containing OST index list
2442  * \param[in/out] overstriping  index list may contain duplicates
2443  *
2444  * \retval positive    number of indices in \a tgts
2445  * \retval -EINVAL     unable to parse \a arg
2446  */
2447 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2448                          unsigned long long *pattern)
2449 {
2450         int rc;
2451         int nr = offset;
2452         int slots = size - offset;
2453         char *ptr = NULL;
2454         bool overstriped = false;
2455         bool end_of_loop;
2456
2457         if (!arg)
2458                 return -EINVAL;
2459
2460         end_of_loop = false;
2461         while (!end_of_loop) {
2462                 int start_index = 0;
2463                 int end_index = 0;
2464                 int i;
2465                 char *endptr = NULL;
2466
2467                 rc = -EINVAL;
2468
2469                 ptr = strchrnul(arg, ',');
2470
2471                 end_of_loop = *ptr == '\0';
2472                 *ptr = '\0';
2473
2474                 errno = 0;
2475                 start_index = strtol(arg, &endptr, 0);
2476                 if (endptr == arg) /* no data at all */
2477                         break;
2478                 if (errno != 0 || start_index < -1 ||
2479                     (*endptr != '-' && *endptr != '\0'))
2480                         break;
2481
2482                 end_index = start_index;
2483                 if (*endptr == '-') {
2484                         errno = 0;
2485                         end_index = strtol(endptr + 1, &endptr, 0);
2486                         if (errno != 0 || *endptr != '\0' || end_index < -1)
2487                                 break;
2488                         if (end_index < start_index)
2489                                 break;
2490                 }
2491
2492                 for (i = start_index; i <= end_index && slots > 0; i++) {
2493                         int j;
2494
2495                         /* remove duplicate */
2496                         for (j = 0; j < offset; j++) {
2497                                 if (tgts[j] == i && pattern &&
2498                                     *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2499                                         overstriped = true;
2500                                 else if (tgts[j] == i)
2501                                         return -EINVAL;
2502                         }
2503
2504                         j = offset;
2505
2506                         if (j == offset) { /* check complete */
2507                                 tgts[nr++] = i;
2508                                 --slots;
2509                         }
2510                 }
2511
2512                 if (slots == 0 && i < end_index)
2513                         break;
2514
2515                 *ptr = ',';
2516                 arg = ++ptr;
2517                 offset = nr;
2518                 rc = 0;
2519         }
2520         if (!end_of_loop && ptr)
2521                 *ptr = ',';
2522
2523         if (!overstriped && pattern)
2524                 *pattern = LLAPI_LAYOUT_DEFAULT;
2525
2526         return rc < 0 ? rc : nr;
2527 }
2528
2529 struct lfs_setstripe_args {
2530         unsigned long long       lsa_comp_end;
2531         unsigned long long       lsa_stripe_size;
2532         unsigned long long       lsa_extension_size;
2533         long long                lsa_stripe_count;
2534         long long                lsa_stripe_off;
2535         __u32                    lsa_comp_flags;
2536         __u32                    lsa_comp_neg_flags;
2537         unsigned long long       lsa_pattern;
2538         unsigned int             lsa_mirror_count;
2539         int                      lsa_nr_tgts;
2540         bool                     lsa_first_comp;
2541         bool                     lsa_extension_comp;
2542         __u32                   *lsa_tgts;
2543         char                    *lsa_pool_name;
2544 };
2545
2546 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2547 {
2548         unsigned int mirror_count = lsa->lsa_mirror_count;
2549         bool first_comp = lsa->lsa_first_comp;
2550
2551         memset(lsa, 0, sizeof(*lsa));
2552
2553         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2554         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2555         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2556         lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2557         lsa->lsa_pool_name = NULL;
2558
2559         lsa->lsa_mirror_count = mirror_count;
2560         lsa->lsa_first_comp = first_comp;
2561 }
2562
2563 /**
2564  * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2565  * @lsa: Stripe options to be initialized and inherited.
2566  *
2567  * This function initializes stripe options in @lsa and inherit
2568  * stripe_size, stripe_count and OST pool_name options.
2569  *
2570  * Return: void.
2571  */
2572 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2573 {
2574         unsigned long long stripe_size;
2575         long long stripe_count;
2576         char *pool_name = NULL;
2577
2578         stripe_size = lsa->lsa_stripe_size;
2579         stripe_count = lsa->lsa_stripe_count;
2580         pool_name = lsa->lsa_pool_name;
2581
2582         setstripe_args_init(lsa);
2583
2584         lsa->lsa_stripe_size = stripe_size;
2585         lsa->lsa_stripe_count = stripe_count;
2586         lsa->lsa_pool_name = pool_name;
2587 }
2588
2589 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2590 {
2591         return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2592                 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2593                 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2594                 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2595                 lsa->lsa_comp_end != 0);
2596 }
2597
2598 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2599 {
2600         if (lsa->lsa_nr_tgts) {
2601                 if (lsa->lsa_nr_tgts < 0 ||
2602                     lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2603                         fprintf(stderr, "Invalid nr_tgts(%d)\n",
2604                                 lsa->lsa_nr_tgts);
2605                         errno = EINVAL;
2606                         return -1;
2607                 }
2608
2609                 if (lsa->lsa_stripe_count > 0 &&
2610                     lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2611                     lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2612                     lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2613                         fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2614                                 lsa->lsa_stripe_count,
2615                                 lsa->lsa_nr_tgts);
2616                         errno = EINVAL;
2617                         return -1;
2618                 }
2619         }
2620
2621         return 0;
2622
2623 }
2624
2625 /**
2626  * comp_args_to_layout() - Create or extend a composite layout.
2627  * @composite:       Pointer to the composite layout.
2628  * @lsa:             Stripe options for the new component.
2629  *
2630  * This function creates or extends a composite layout by adding a new
2631  * component with stripe options from @lsa.
2632  *
2633  * When modified, adjust llapi_stripe_param_verify() if needed as well.
2634  *
2635  * Return: 0 on success or an error code on failure.
2636  */
2637 static int comp_args_to_layout(struct llapi_layout **composite,
2638                                struct lfs_setstripe_args *lsa,
2639                                bool set_extent)
2640 {
2641         struct llapi_layout *layout = *composite;
2642         uint64_t prev_end = 0;
2643         uint64_t size;
2644         int i = 0, rc;
2645
2646 new_comp:
2647         if (!layout) {
2648                 layout = llapi_layout_alloc();
2649                 if (!layout) {
2650                         fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2651                                 strerror(errno));
2652                         errno = ENOMEM;
2653                         return -1;
2654                 }
2655                 *composite = layout;
2656                 lsa->lsa_first_comp = true;
2657         } else {
2658                 uint64_t start;
2659
2660                 /*
2661                  * Get current component extent, current component
2662                  * must be the tail component.
2663                  */
2664                 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2665                 if (rc) {
2666                         fprintf(stderr, "Get comp extent failed. %s\n",
2667                                 strerror(errno));
2668                         return rc;
2669                 }
2670
2671                 if (lsa->lsa_first_comp) {
2672                         prev_end = 0;
2673                         rc = llapi_layout_add_first_comp(layout);
2674                 } else {
2675                         rc = llapi_layout_comp_add(layout);
2676                 }
2677                 if (rc) {
2678                         fprintf(stderr, "Add component failed. %s\n",
2679                                 strerror(errno));
2680                         return rc;
2681                 }
2682         }
2683
2684         rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2685         if (rc) {
2686                 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2687                         lsa->lsa_comp_flags, strerror(errno));
2688                 return rc;
2689         }
2690
2691         if (set_extent) {
2692                 uint64_t comp_end = lsa->lsa_comp_end;
2693
2694                 /*
2695                  * The extendable component is 0-length, so it can be removed
2696                  * if there is insufficient space to extend it.
2697                  */
2698                 if (lsa->lsa_extension_comp)
2699                         comp_end = prev_end;
2700
2701                 rc = llapi_layout_comp_extent_set(layout, prev_end,
2702                                                   comp_end);
2703                 if (rc) {
2704                         fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2705                                 prev_end, comp_end, strerror(errno));
2706                         return rc;
2707                 }
2708         }
2709         /* reset lsa_first_comp */
2710         lsa->lsa_first_comp = false;
2711
2712         /* Data-on-MDT component setting */
2713         if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2714                 /* Yaml support */
2715                 if (lsa->lsa_stripe_count == 0)
2716                         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2717                 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2718                         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2719                 if (lsa->lsa_stripe_off == -1 ||
2720                     lsa->lsa_stripe_off == 0)
2721                         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2722                 /*
2723                  * In case of Data-on-MDT patterns the only extra option
2724                  * applicable is stripe size option.
2725                  */
2726                 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2727                         fprintf(stderr,
2728                                 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2729                                 lsa->lsa_stripe_count);
2730                         errno = EINVAL;
2731                         return -1;
2732                 }
2733                 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2734                     lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2735                         fprintf(stderr,
2736                                 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2737                                 lsa->lsa_stripe_size);
2738                         errno = EINVAL;
2739                         return -1;
2740                 }
2741                 if (lsa->lsa_nr_tgts != 0) {
2742                         fprintf(stderr,
2743                                 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2744                                 lsa->lsa_nr_tgts);
2745                         errno = EINVAL;
2746                         return -1;
2747                 }
2748                 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2749                         fprintf(stderr,
2750                                 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2751                                 lsa->lsa_stripe_off);
2752                         errno = EINVAL;
2753                         return -1;
2754                 }
2755                 if (lsa->lsa_pool_name != 0) {
2756                         fprintf(stderr,
2757                                 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2758                                 lsa->lsa_pool_name);
2759                         errno = EINVAL;
2760                         return -1;
2761                 }
2762
2763                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2764                 if (rc) {
2765                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2766                                 lsa->lsa_pattern,
2767                                 strerror(errno));
2768                         return rc;
2769                 }
2770                 /* Data-on-MDT component has always single stripe up to end */
2771                 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2772         } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2773                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2774                 if (rc) {
2775                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2776                                 lsa->lsa_pattern,
2777                                 strerror(errno));
2778                         return rc;
2779                 }
2780         }
2781
2782         size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2783                 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2784
2785         if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2786                 rc = llapi_layout_extension_size_set(layout, size);
2787         else
2788                 rc = llapi_layout_stripe_size_set(layout, size);
2789
2790         if (rc) {
2791                 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2792                         size, strerror(errno));
2793                 return rc;
2794         }
2795
2796         rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2797         if (rc) {
2798                 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2799                         lsa->lsa_stripe_count, strerror(errno));
2800                 return rc;
2801         }
2802
2803         if (lsa->lsa_pool_name) {
2804                 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2805                 if (rc) {
2806                         fprintf(stderr, "Set pool name: %s failed. %s\n",
2807                                 lsa->lsa_pool_name, strerror(errno));
2808                         return rc;
2809                 }
2810         } else {
2811                 rc = llapi_layout_pool_name_set(layout, "");
2812                 if (rc) {
2813                         fprintf(stderr, "Clear pool name failed: %s\n",
2814                                 strerror(errno));
2815                         return rc;
2816                 }
2817         }
2818
2819         rc = lsa_args_stripe_count_check(lsa);
2820         if (rc)
2821                 return rc;
2822
2823         if (lsa->lsa_nr_tgts > 0) {
2824                 bool found = false;
2825
2826                 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2827                         rc = llapi_layout_ost_index_set(layout, i,
2828                                                         lsa->lsa_tgts[i]);
2829                         if (rc)
2830                                 break;
2831
2832                         /* Make sure stripe offset is in OST list. */
2833                         if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2834                                 found = true;
2835                 }
2836                 if (!found) {
2837                         fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2838                                 lsa->lsa_stripe_off);
2839                         errno = EINVAL;
2840                         return -1;
2841                 }
2842         } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2843                    lsa->lsa_stripe_off != -1) {
2844                 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2845         }
2846         if (rc) {
2847                 fprintf(stderr, "Set ost index %d failed. %s\n",
2848                         i, strerror(errno));
2849                 return rc;
2850         }
2851
2852         /* Create the second, virtual component of extension space */
2853         if (lsa->lsa_extension_comp) {
2854                 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2855                 lsa->lsa_extension_comp = false;
2856                 goto new_comp;
2857         }
2858
2859         return rc;
2860 }
2861
2862 static int build_component(struct llapi_layout **layout,
2863                            struct lfs_setstripe_args *lsa, bool set_extent)
2864 {
2865         int rc;
2866
2867         rc = comp_args_to_layout(layout, lsa, set_extent);
2868         if (rc)
2869                 return rc;
2870
2871         if (lsa->lsa_mirror_count > 0) {
2872                 rc = llapi_layout_mirror_count_set(*layout,
2873                                                    lsa->lsa_mirror_count);
2874                 if (rc)
2875                         return rc;
2876
2877                 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2878                 if (rc)
2879                         return rc;
2880                 lsa->lsa_mirror_count = 0;
2881         }
2882
2883         return rc;
2884 }
2885
2886 static int build_prev_component(struct llapi_layout **layout,
2887                                 struct lfs_setstripe_args *prev,
2888                                 struct lfs_setstripe_args *lsa,
2889                                 bool set_extent)
2890 {
2891         int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2892         int rc;
2893
2894         if (prev->lsa_stripe_size) {
2895                 if (extension) {
2896                         prev->lsa_comp_end = lsa->lsa_comp_end;
2897                         prev->lsa_extension_size = lsa->lsa_extension_size;
2898                         prev->lsa_extension_comp = true;
2899                 }
2900
2901                 rc = build_component(layout, prev, true);
2902                 if (rc)
2903                         return rc;
2904         }
2905
2906         /*
2907          * Copy lsa to previous lsa;
2908          * if this is an extension component, make the previous invalid;
2909          */
2910         if (extension)
2911                 prev->lsa_stripe_size = 0;
2912         else
2913                 *prev = *lsa;
2914
2915         return 0;
2916 }
2917
2918 #ifndef LCME_TEMPLATE_FLAGS
2919 #define LCME_TEMPLATE_FLAGS     (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
2920                                  LCME_FL_EXTENSION)
2921 #endif
2922
2923 static int build_layout_from_yaml_node(struct cYAML *node,
2924                                        struct llapi_layout **layout,
2925                                        struct lfs_setstripe_args *lsa,
2926                                        struct lfs_setstripe_args *prevp)
2927 {
2928         struct lfs_setstripe_args prev = { 0 };
2929         __u32 *osts = lsa->lsa_tgts;
2930         char *string;
2931         int rc = 0;
2932
2933         if (!prevp)
2934                 prevp = &prev;
2935
2936         while (node) {
2937                 string = node->cy_string;
2938
2939                 if (node->cy_type == CYAML_TYPE_OBJECT) {
2940                         /* go deep to sub blocks */
2941                         if (string && !strncmp(string, "component", 9) &&
2942                             strncmp(string, "component0", 10) &&
2943                             strncmp(string, "components", 10)) {
2944                                 rc = build_prev_component(layout, prevp, lsa,
2945                                                           true);
2946                                 if (rc)
2947                                         return rc;
2948
2949                                 /* initialize lsa. */
2950                                 setstripe_args_init(lsa);
2951                                 lsa->lsa_first_comp = false;
2952                                 lsa->lsa_tgts = osts;
2953                         }
2954
2955                         rc = build_layout_from_yaml_node(node->cy_child, layout,
2956                                                          lsa, prevp);
2957                         if (rc)
2958                                 return rc;
2959                 } else {
2960                         if (!node->cy_string)
2961                                 return -EINVAL;
2962
2963                         /* skip leading lmm_ if present, to simplify parsing */
2964                         if (strncmp(string, "lmm_", 4) == 0)
2965                                 string += 4;
2966
2967                         if (node->cy_type == CYAML_TYPE_STRING) {
2968                                 if (!strcmp(string, "lcme_extent.e_end")) {
2969                                         if (!strcmp(node->cy_valuestring, "EOF") ||
2970                                             !strcmp(node->cy_valuestring, "eof"))
2971                                                 lsa->lsa_comp_end = LUSTRE_EOF;
2972                                 } else if (!strcmp(string, "pool")) {
2973                                         lsa->lsa_pool_name = node->cy_valuestring;
2974                                 } else if (!strcmp(string, "pattern")) {
2975                                         if (!strcmp(node->cy_valuestring, "mdt"))
2976                                                 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2977                                         if (!strcmp(node->cy_valuestring,
2978                                                     "raid0,overstriped"))
2979                                                 lsa->lsa_pattern =
2980                                                         LLAPI_LAYOUT_OVERSTRIPING;
2981                                 } else if (!strcmp(string, "lcme_flags")) {
2982                                         rc = comp_str2flags(node->cy_valuestring,
2983                                                             &lsa->lsa_comp_flags,
2984                                                             &lsa->lsa_comp_neg_flags);
2985                                         if (rc)
2986                                                 return rc;
2987                                         /*
2988                                          * Only template flags have meaning in
2989                                          * the layout for a new file
2990                                          */
2991                                         lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2992                                 }
2993                         } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2994                                 if (!strcmp(string, "lcm_mirror_count")) {
2995                                         lsa->lsa_mirror_count = node->cy_valueint;
2996                                 } else if (!strcmp(string, "lcme_extent.e_start")) {
2997                                         if (node->cy_valueint == 0)
2998                                                 lsa->lsa_first_comp = true;
2999                                 } else if (!strcmp(string, "lcme_extent.e_end")) {
3000                                         if (node->cy_valueint == -1)
3001                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3002                                         else
3003                                                 lsa->lsa_comp_end = node->cy_valueint;
3004                                 } else if (!strcmp(string, "stripe_count")) {
3005                                         lsa->lsa_stripe_count = node->cy_valueint;
3006                                 } else if (!strcmp(string, "stripe_size")) {
3007                                         lsa->lsa_stripe_size = node->cy_valueint;
3008                                 } else if (!strcmp(string, "extension_size")) {
3009                                         lsa->lsa_extension_size = node->cy_valueint;
3010                                         lsa->lsa_extension_comp = true;
3011                                 } else if (!strcmp(string, "stripe_offset")) {
3012                                         lsa->lsa_stripe_off = node->cy_valueint;
3013                                 } else if (!strcmp(string, "l_ost_idx")) {
3014                                         osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3015                                         lsa->lsa_nr_tgts++;
3016                                 }
3017                         }
3018                 }
3019                 node = node->cy_next;
3020         }
3021
3022         if (prevp == &prev) {
3023                 rc = build_prev_component(layout, prevp, lsa, true);
3024                 if (rc)
3025                         return rc;
3026
3027                 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3028                         rc = build_component(layout, lsa, *layout != NULL);
3029         }
3030
3031         return rc;
3032 }
3033
3034 static int lfs_comp_create_from_yaml(char *template,
3035                                      struct llapi_layout **layout,
3036                                      struct lfs_setstripe_args *lsa,
3037                                      __u32 *osts)
3038 {
3039         struct cYAML *tree = NULL, *err_rc = NULL;
3040         int rc = 0;
3041
3042         tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3043         if (!tree) {
3044                 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3045                         progname, template);
3046                 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3047                                   "can't parse", &err_rc);
3048                 cYAML_print_tree2file(stderr, err_rc);
3049                 cYAML_free_tree(err_rc);
3050                 rc = -EINVAL;
3051                 goto err;
3052         }
3053
3054         /* initialize lsa for plain file */
3055         setstripe_args_init(lsa);
3056         lsa->lsa_tgts = osts;
3057
3058         rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3059         if (rc) {
3060                 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3061                         progname, template);
3062                 goto err;
3063         }
3064         /* clean clean lsa */
3065         setstripe_args_init(lsa);
3066
3067 err:
3068         if (tree)
3069                 cYAML_free_tree(tree);
3070         return rc;
3071 }
3072
3073 /**
3074  * Get the extension size from the next (SEL) component and extend the
3075  * current component on it. The start of the next component is to be
3076  * adjusted as well.
3077  *
3078  * \param[in] layout    the current layout
3079  * \param[in] start     the start of the current component
3080  * \param[in,out] end   the end of the current component
3081  * \param[in] offset    the offset to adjust the end position to instead of
3082  *                      extension size
3083  *
3084  * \retval 0            - extended successfully
3085  * \retval < 0          - error
3086  */
3087 static int layout_extend_comp(struct llapi_layout *layout,
3088                               uint64_t start, uint64_t *end,
3089                               uint64_t offset)
3090 {
3091         uint64_t size, next_start, next_end;
3092         int rc;
3093
3094         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3095         if (rc < 0) {
3096                 fprintf(stderr,
3097                         "%s setstripe: cannot move component cursor: %s\n",
3098                         progname, strerror(errno));
3099                 return rc;
3100         }
3101
3102         /*
3103          * Even if the @size will not be used below, this will fail if
3104          * this is not a SEL component - a good confirmation we are
3105          * working on right components.
3106          */
3107         rc = llapi_layout_extension_size_get(layout, &size);
3108         if (rc < 0) {
3109                 fprintf(stderr,
3110                         "%s setstripe: cannot get component ext size: %s\n",
3111                         progname, strerror(errno));
3112                 return rc;
3113         }
3114
3115         rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3116         if (rc) {
3117                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3118                         progname, strerror(errno));
3119                 return rc;
3120         }
3121
3122         next_start += offset ?: size;
3123         rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3124         if (rc) {
3125                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3126                         progname, strerror(errno));
3127                 return rc;
3128         }
3129
3130         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3131         if (rc < 0) {
3132                 fprintf(stderr,
3133                         "%s setstripe: cannot move component cursor: %s\n",
3134                         progname, strerror(errno));
3135                 return rc;
3136         }
3137
3138         *end += offset ?: size;
3139         rc = llapi_layout_comp_extent_set(layout, start, *end);
3140         if (rc) {
3141                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3142                         progname, strerror(errno));
3143                 return rc;
3144         }
3145
3146         return 0;
3147 }
3148
3149 /**
3150  * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3151  * end of the last component in the existing file, and adjust the
3152  * first extent start of the components to be added accordingly.
3153  *
3154  * In the create mode, we need to check if the first component is an extendable
3155  * SEL component and extend its length to the extension size (first component
3156  * of the PFL file is initialised at the create time, cannot be 0-lenght.
3157  */
3158 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3159                                       bool comp_add)
3160 {
3161         struct llapi_layout *head;
3162         uint64_t start = 0, prev_end = 0;
3163         uint64_t end;
3164         int rc, ret = 0;
3165
3166         if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3167                 return 0;
3168
3169         errno = 0;
3170         while (comp_add) {
3171                 head = llapi_layout_get_by_path(fname, 0);
3172                 if (!head) {
3173                         fprintf(stderr,
3174                                 "%s setstripe: cannot read layout from '%s': %s\n",
3175                                 progname, fname, strerror(errno));
3176                         return -EINVAL;
3177                 } else if (errno == ENODATA) {
3178                         /*
3179                          * file without LOVEA, this component-add will be turned
3180                          * into a component-create.
3181                          */
3182                         llapi_layout_free(head);
3183                         ret = -ENODATA;
3184
3185                         /*
3186                          * the new layout will be added to an empty one, it
3187                          * still needs to be adjusted below
3188                          */
3189                         comp_add = 0;
3190                         break;
3191                 } else if (!llapi_layout_is_composite(head)) {
3192                         fprintf(stderr,
3193                                 "%s setstripe: '%s' not a composite file\n",
3194                                 progname, fname);
3195                         llapi_layout_free(head);
3196                         return -EINVAL;
3197                 }
3198
3199                 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3200                 if (rc) {
3201                         fprintf(stderr,
3202                                 "%s setstripe: cannot get prev extent: %s\n",
3203                                 progname, strerror(errno));
3204                         llapi_layout_free(head);
3205                         return rc;
3206                 }
3207
3208                 llapi_layout_free(head);
3209                 break;
3210         }
3211
3212         /* Make sure we use the first component of the layout to be added. */
3213         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3214         if (rc < 0) {
3215                 fprintf(stderr,
3216                         "%s setstripe: cannot move component cursor: %s\n",
3217                         progname, strerror(errno));
3218                 return rc;
3219         }
3220
3221         rc = llapi_layout_comp_extent_get(layout, &start, &end);
3222         if (rc) {
3223                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3224                         progname, strerror(errno));
3225                 return rc;
3226         }
3227
3228         if (start == 0 && end == 0) {
3229                 rc = layout_extend_comp(layout, start, &end,
3230                                         comp_add ? prev_end : 0);
3231                 if (rc)
3232                         return rc;
3233         }
3234
3235         if (start > prev_end || end < prev_end) {
3236                 fprintf(stderr,
3237                         "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3238                         progname, start, end, prev_end);
3239                 return -EINVAL;
3240         }
3241
3242         rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3243         if (rc) {
3244                 fprintf(stderr,
3245                         "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3246                         progname, prev_end, end, strerror(errno));
3247                 return rc;
3248         }
3249
3250         return ret;
3251 }
3252
3253 static int mirror_adjust_first_extents(struct mirror_args *list)
3254 {
3255         int rc = 0;
3256
3257         if (!list)
3258                 return 0;
3259
3260         while (list) {
3261                 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3262                 if (rc)
3263                         break;
3264                 list = list->m_next;
3265         }
3266
3267         return rc;
3268 }
3269
3270 static inline bool arg_is_eof(char *arg)
3271 {
3272         return !strncmp(arg, "-1", strlen("-1")) ||
3273                !strncmp(arg, "EOF", strlen("EOF")) ||
3274                !strncmp(arg, "eof", strlen("eof"));
3275 }
3276
3277 /**
3278  * lfs_mirror_alloc() - Allocate a mirror argument structure.
3279  *
3280  * Return: Valid mirror_args pointer on success and
3281  *         NULL if memory allocation fails.
3282  */
3283 static struct mirror_args *lfs_mirror_alloc(void)
3284 {
3285         struct mirror_args *mirror = NULL;
3286
3287         while (1) {
3288                 mirror = calloc(1, sizeof(*mirror));
3289                 if (mirror) {
3290                         mirror->m_inherit = false;
3291                         break;
3292                 }
3293
3294                 sleep(1);
3295         }
3296
3297         return mirror;
3298 }
3299
3300 /**
3301  * lfs_mirror_free() - Free memory allocated for a mirror argument
3302  *                     structure.
3303  * @mirror: Previously allocated mirror argument structure by
3304  *          lfs_mirror_alloc().
3305  *
3306  * Free memory allocated for @mirror.
3307  *
3308  * Return: void.
3309  */
3310 static void lfs_mirror_free(struct mirror_args *mirror)
3311 {
3312         if (mirror->m_layout)
3313                 llapi_layout_free(mirror->m_layout);
3314         free(mirror);
3315 }
3316
3317 /**
3318  * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3319  * @mirror_list: Previously allocated mirror list.
3320  *
3321  * Free memory allocated for @mirror_list.
3322  *
3323  * Return: void.
3324  */
3325 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3326 {
3327         struct mirror_args *next_mirror = NULL;
3328
3329         while (mirror_list) {
3330                 next_mirror = mirror_list->m_next;
3331                 lfs_mirror_free(mirror_list);
3332                 mirror_list = next_mirror;
3333         }
3334 }
3335
3336 enum {
3337         LFS_SETQUOTA_DELETE = 1,
3338         LFS_POOL_OPT = 3,
3339         LFS_COMP_COUNT_OPT,
3340         LFS_COMP_START_OPT,
3341         LFS_COMP_FLAGS_OPT,
3342         LFS_COMP_DEL_OPT,
3343         LFS_COMP_SET_OPT,
3344         LFS_COMP_ADD_OPT,
3345         LFS_COMP_NO_VERIFY_OPT,
3346         LFS_PROJID_OPT,
3347         LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3348         LFS_MIRROR_ID_OPT,
3349         LFS_MIRROR_STATE_OPT,
3350         LFS_LAYOUT_COPY,
3351         LFS_MIRROR_INDEX_OPT,
3352         LFS_LAYOUT_FOREIGN_OPT,
3353         LFS_MODE_OPT,
3354         LFS_NEWERXY_OPT,
3355         LFS_INHERIT_RR_OPT,
3356         LFS_FIND_PERM,
3357         LFS_PRINTF_OPT,
3358         LFS_NO_FOLLOW_OPT,
3359 };
3360
3361 #ifndef LCME_USER_MIRROR_FLAGS
3362 /* The mirror flags can be set by users at creation time. */
3363 #define LCME_USER_MIRROR_FLAGS  (LCME_FL_PREF_RW)
3364 #endif
3365
3366 /* functions */
3367 static int lfs_setstripe_internal(int argc, char **argv,
3368                                   enum setstripe_origin opc)
3369 {
3370         struct lfs_setstripe_args        lsa = { 0 };
3371         struct llapi_stripe_param       *param = NULL;
3372         struct find_param                migrate_mdt_param = {
3373                 .fp_max_depth = -1,
3374                 .fp_mdt_index = -1,
3375         };
3376         char                            *fname;
3377         int                              result = 0;
3378         int                              result2 = 0;
3379         char                            *end;
3380         int                              c;
3381         int                              delete = 0;
3382         unsigned long long               size_units = 1;
3383         bool                             migrate_mode = false;
3384         bool                             migrate_mdt_mode = false;
3385         bool                             setstripe_mode = false;
3386         bool                             migration_block = false;
3387         __u64                            migration_flags = 0;
3388         __u32                            tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3389         int                              comp_del = 0, comp_set = 0;
3390         int                              comp_add = 0;
3391         __u32                            comp_id = 0;
3392         struct llapi_layout             *layout = NULL;
3393         struct llapi_layout             **lpp = &layout;
3394         bool                             mirror_mode = false;
3395         bool                             has_m_file = false;
3396         __u32                            mirror_count = 0;
3397         enum mirror_flags                mirror_flags = 0;
3398         struct mirror_args              *mirror_list = NULL;
3399         struct mirror_args              *new_mirror = NULL;
3400         struct mirror_args              *last_mirror = NULL;
3401         __u16                            mirror_id = 0;
3402         char                             cmd[PATH_MAX];
3403         bool from_yaml = false;
3404         bool from_copy = false;
3405         char *template = NULL;
3406         bool foreign_mode = false;
3407         char *xattr = NULL;
3408         uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3409         char *mode_opt = NULL;
3410         mode_t previous_umask = 0;
3411         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3412
3413         struct option long_opts[] = {
3414 /* find { .val = '0',   .name = "null",         .has_arg = no_argument }, */
3415 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
3416         /* --block is only valid in migrate mode */
3417         { .val = 'b',   .name = "block",        .has_arg = no_argument },
3418 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
3419         { .val = LFS_COMP_ADD_OPT,
3420                         .name = "comp-add",     .has_arg = no_argument },
3421         { .val = LFS_COMP_ADD_OPT,
3422                         .name = "component-add", .has_arg = no_argument },
3423         { .val = LFS_COMP_DEL_OPT,
3424                         .name = "comp-del",     .has_arg = no_argument },
3425         { .val = LFS_COMP_DEL_OPT,
3426                         .name = "component-del", .has_arg = no_argument },
3427         { .val = LFS_COMP_FLAGS_OPT,
3428                         .name = "comp-flags",   .has_arg = required_argument },
3429         { .val = LFS_COMP_FLAGS_OPT,
3430                         .name = "component-flags",
3431                                                 .has_arg = required_argument },
3432         { .val = LFS_COMP_SET_OPT,
3433                         .name = "comp-set",     .has_arg = no_argument },
3434         { .val = LFS_COMP_SET_OPT,
3435                         .name = "component-set",
3436                                                 .has_arg = no_argument},
3437         { .val = LFS_COMP_NO_VERIFY_OPT,
3438                         .name = "no-verify",    .has_arg = no_argument},
3439         { .val = LFS_LAYOUT_FLAGS_OPT,
3440                         .name = "flags",        .has_arg = required_argument},
3441         { .val = LFS_LAYOUT_FOREIGN_OPT,
3442                         .name = "foreign",      .has_arg = optional_argument},
3443         { .val = LFS_MIRROR_ID_OPT,
3444                         .name = "mirror-id",    .has_arg = required_argument},
3445         { .val = LFS_MODE_OPT,
3446                         .name = "mode",         .has_arg = required_argument},
3447         { .val = LFS_LAYOUT_COPY,
3448                         .name = "copy",         .has_arg = required_argument},
3449         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument},
3450         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument},
3451         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument},
3452         { .val = 'C',   .name = "overstripe-count",
3453                                                 .has_arg = required_argument},
3454         { .val = 'd',   .name = "delete",       .has_arg = no_argument},
3455         { .val = 'd',   .name = "destroy",      .has_arg = no_argument},
3456         /* used with "lfs migrate -m" */
3457         { .val = 'd',   .name = "directory",    .has_arg = no_argument},
3458         /* --non-direct is only valid in migrate mode */
3459         { .val = 'D',   .name = "non-direct",   .has_arg = no_argument },
3460         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument},
3461         { .val = 'E',   .name = "component-end",
3462                                                 .has_arg = required_argument},
3463         { .val = 'f',   .name = "file",         .has_arg = required_argument },
3464 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
3465 /* find { .val = 'g',   .name = "gid",          .has_arg = no_argument }, */
3466 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
3467         { .val = 'h',   .name = "help",         .has_arg = no_argument },
3468         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument},
3469         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument},
3470         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument},
3471         { .val = 'I',   .name = "comp-id",      .has_arg = required_argument},
3472         { .val = 'I',   .name = "component-id", .has_arg = required_argument},
3473 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
3474         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
3475         { .val = 'm',   .name = "mdt",          .has_arg = required_argument},
3476         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument},
3477         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument},
3478         /* --non-block is only valid in migrate mode */
3479         { .val = 'n',   .name = "non-block",    .has_arg = no_argument },
3480         { .val = 'N',   .name = "mirror-count", .has_arg = optional_argument},
3481         { .val = 'o',   .name = "ost",          .has_arg = required_argument },
3482 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3483         { .val = 'o',   .name = "ost-list",     .has_arg = required_argument },
3484         { .val = 'o',   .name = "ost_list",     .has_arg = required_argument },
3485 #endif
3486         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
3487 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
3488 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
3489 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
3490         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
3491         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
3492 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
3493 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3494 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
3495 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
3496         /* --verbose is only valid in migrate mode */
3497         { .val = 'v',   .name = "verbose",      .has_arg = no_argument},
3498         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
3499 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3500         { .val = 'y',   .name = "yaml",         .has_arg = required_argument },
3501         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument},
3502         { .val = 'z',   .name = "extension-size", .has_arg = required_argument},
3503         { .name = NULL } };
3504
3505         setstripe_args_init(&lsa);
3506
3507         migrate_mode = (opc == SO_MIGRATE);
3508         mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3509         setstripe_mode = (opc == SO_SETSTRIPE);
3510         if (opc == SO_MIRROR_DELETE) {
3511                 delete = 1;
3512                 mirror_flags = MF_DESTROY;
3513         }
3514
3515         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3516         progname = cmd;
3517         while ((c = getopt_long(argc, argv,
3518                                 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:y:z:",
3519                                 long_opts, NULL)) >= 0) {
3520                 size_units = 1;
3521                 switch (c) {
3522                 case 0:
3523                         /* Long options. */
3524                         break;
3525                 case LFS_COMP_ADD_OPT:
3526                         comp_add = 1;
3527                         break;
3528                 case LFS_COMP_DEL_OPT:
3529                         comp_del = 1;
3530                         break;
3531                 case LFS_COMP_FLAGS_OPT:
3532                         result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3533                                                 &lsa.lsa_comp_neg_flags);
3534                         if (result != 0)
3535                                 goto usage_error;
3536                         if (mirror_mode && lsa.lsa_comp_neg_flags) {
3537                                 fprintf(stderr,
3538                                         "%s: inverted flags are not supported\n",
3539                                         progname);
3540                                 goto usage_error;
3541                         }
3542                         break;
3543                 case LFS_COMP_SET_OPT:
3544                         comp_set = 1;
3545                         break;
3546                 case LFS_COMP_NO_VERIFY_OPT:
3547                         mirror_flags |= MF_NO_VERIFY;
3548                         break;
3549                 case LFS_MIRROR_ID_OPT: {
3550                         unsigned long int id;
3551
3552                         errno = 0;
3553                         id = strtoul(optarg, &end, 0);
3554                         if (errno != 0 || *end != '\0' || id == 0 ||
3555                             id > UINT16_MAX) {
3556                                 fprintf(stderr,
3557                                         "%s %s: invalid mirror ID '%s'\n",
3558                                         progname, argv[0], optarg);
3559                                 goto usage_error;
3560                         }
3561
3562                         mirror_id = (__u16)id;
3563                         break;
3564                 }
3565                 case LFS_LAYOUT_FLAGS_OPT: {
3566                         uint32_t neg_flags;
3567
3568                         /* check for numeric flags (foreign and mirror cases) */
3569                         if (setstripe_mode && !mirror_mode && !last_mirror) {
3570                                 errno = 0;
3571                                 flags = strtoul(optarg, &end, 16);
3572                                 if (errno != 0 || *end != '\0' ||
3573                                     flags >= UINT32_MAX) {
3574                                         fprintf(stderr,
3575                                                 "%s %s: invalid hex flags '%s'\n",
3576                                                 progname, argv[0], optarg);
3577                                         return CMD_HELP;
3578                                 }
3579                                 if (!foreign_mode) {
3580                                         fprintf(stderr,
3581                                                 "%s %s: hex flags must be specified with --foreign option\n",
3582                                                 progname, argv[0]);
3583                                         return CMD_HELP;
3584                                 }
3585                                 break;
3586                         }
3587
3588                         if (!mirror_mode || !last_mirror) {
3589                                 fprintf(stderr,
3590                                         "error: %s: --flags must be specified with --mirror-count|-N option\n",
3591                                         progname);
3592                                 goto usage_error;
3593                         }
3594
3595                         result = comp_str2flags(optarg, &last_mirror->m_flags,
3596                                                 &neg_flags);
3597                         if (result != 0)
3598                                 goto usage_error;
3599
3600                         if (neg_flags) {
3601                                 fprintf(stderr,
3602                                         "%s: inverted flags are not supported\n",
3603                                         progname);
3604                                 result = -EINVAL;
3605                                 goto usage_error;
3606                         }
3607                         if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3608                                 fprintf(stderr,
3609                                         "%s: unsupported mirror flags: %s\n",
3610                                         progname, optarg);
3611                                 result = -EINVAL;
3612                                 goto error;
3613                         }
3614                         break;
3615                 }
3616                 case LFS_LAYOUT_FOREIGN_OPT:
3617                         if (optarg) {
3618                                 /* check pure numeric */
3619                                 type = strtoul(optarg, &end, 0);
3620                                 if (*end) {
3621                                         /* check name */
3622                                         type = check_foreign_type_name(optarg);
3623                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3624                                                 fprintf(stderr,
3625                                                         "%s %s: unrecognized foreign type '%s'\n",
3626                                                         progname, argv[0],
3627                                                         optarg);
3628                                                 return CMD_HELP;
3629                                         }
3630                                 } else if (type >= UINT32_MAX) {
3631                                         fprintf(stderr,
3632                                                 "%s %s: invalid foreign type '%s'\n",
3633                                                 progname, argv[0], optarg);
3634                                         return CMD_HELP;
3635                                 }
3636                         }
3637                         foreign_mode = true;
3638                         break;
3639                 case LFS_MODE_OPT:
3640                         mode_opt = optarg;
3641                         if (mode_opt) {
3642                                 mode = strtoul(mode_opt, &end, 8);
3643                                 if (*end != '\0') {
3644                                         fprintf(stderr,
3645                                                 "%s %s: bad mode '%s'\n",
3646                                                 progname, argv[0], mode_opt);
3647                                         return CMD_HELP;
3648                                 }
3649                                 previous_umask = umask(0);
3650                         }
3651                         break;
3652                 case LFS_LAYOUT_COPY:
3653                         from_copy = true;
3654                         template = optarg;
3655                         break;
3656                 case 'b':
3657                         if (!migrate_mode) {
3658                                 fprintf(stderr,
3659                                         "%s %s: -b|--block valid only for migrate command\n",
3660                                         progname, argv[0]);
3661                                 goto usage_error;
3662                         }
3663                         migration_block = true;
3664                         break;
3665                 case 'C':
3666                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3667                                 fprintf(stderr,
3668                                         "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3669                                         progname, argv[0]);
3670                                 goto usage_error;
3671                         }
3672                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3673                         fallthrough;
3674                 case 'c':
3675                         errno = 0;
3676                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3677                         if (errno != 0 || *end != '\0'|| optarg == end ||
3678                             lsa.lsa_stripe_count < -1 ||
3679                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3680                                 fprintf(stderr,
3681                                         "%s %s: invalid stripe count '%s'\n",
3682                                         progname, argv[0], optarg);
3683                                 goto usage_error;
3684                         }
3685
3686                         if (lsa.lsa_stripe_count == -1)
3687                                 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3688                         break;
3689                 case 'd':
3690                         if (migrate_mode) {
3691                                 migrate_mdt_param.fp_max_depth = 1;
3692                         } else {
3693                                 /* delete the default striping pattern */
3694                                 delete = 1;
3695                                 if (opc == SO_MIRROR_SPLIT) {
3696                                         if (has_m_file) {
3697                                                 fprintf(stderr,
3698                                                       "%s %s: -d cannot used with -f\n",
3699                                                         progname, argv[0]);
3700                                                 goto usage_error;
3701                                         }
3702                                         mirror_flags |= MF_DESTROY;
3703                                 }
3704                         }
3705                         break;
3706                 case 'D':
3707                         if (!migrate_mode) {
3708                                 fprintf(stderr,
3709                                         "%s %s: -D|--non-direct is valid only for migrate command\n",
3710                                         progname, argv[0]);
3711                                 goto usage_error;
3712                         }
3713                         migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3714                         break;
3715                 case 'E':
3716                         if (lsa.lsa_comp_end != 0) {
3717                                 result = comp_args_to_layout(lpp, &lsa, true);
3718                                 if (result) {
3719                                         fprintf(stderr, "%s: invalid layout\n",
3720                                                 progname);
3721                                         goto usage_error;
3722                                 }
3723
3724                                 setstripe_args_init_inherit(&lsa);
3725                         }
3726
3727                         if (arg_is_eof(optarg)) {
3728                                 lsa.lsa_comp_end = LUSTRE_EOF;
3729                         } else {
3730                                 result = llapi_parse_size(optarg,
3731                                                           &lsa.lsa_comp_end,
3732                                                           &size_units, 0);
3733                                 /* assume units of KB if too small */
3734                                 if (lsa.lsa_comp_end < 4096)
3735                                         lsa.lsa_comp_end *= 1024;
3736                                 if (result ||
3737                                     lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3738                                         fprintf(stderr,
3739                                                 "%s %s: invalid component end '%s'\n",
3740                                                 progname, argv[0], optarg);
3741                                         goto usage_error;
3742                                 }
3743                         }
3744                         break;
3745                 case 'H':
3746                         if (!migrate_mode) {
3747                                 fprintf(stderr,
3748                                         "--mdt-hash is valid only for migrate command\n");
3749                                 return CMD_HELP;
3750                         }
3751
3752                         lsa.lsa_pattern = check_hashtype(optarg);
3753                         if (lsa.lsa_pattern == 0) {
3754                                 fprintf(stderr,
3755                                         "%s %s: bad stripe hash type '%s'\n",
3756                                         progname, argv[0], optarg);
3757                                 return CMD_HELP;
3758                         }
3759                         break;
3760                 case 'i':
3761                         errno = 0;
3762                         lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3763                         if (errno != 0 || *end != '\0' || optarg == end ||
3764                             lsa.lsa_stripe_off < -1 ||
3765                             lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3766                                 fprintf(stderr,
3767                                         "%s %s: invalid stripe offset '%s'\n",
3768                                         progname, argv[0], optarg);
3769                                 goto usage_error;
3770                         }
3771                         if (lsa.lsa_stripe_off == -1)
3772                                 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3773                         break;
3774                 case 'I':
3775                         comp_id = strtoul(optarg, &end, 0);
3776                         if (*end != '\0' || comp_id == 0 ||
3777                             comp_id > LCME_ID_MAX) {
3778                                 fprintf(stderr,
3779                                         "%s %s: invalid component ID '%s'\n",
3780                                         progname, argv[0], optarg);
3781                                 goto usage_error;
3782                         }
3783                         break;
3784                 case 'f':
3785                         if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3786                                 fprintf(stderr,
3787                                         "error: %s: invalid option: %s\n",
3788                                         progname, argv[optopt + 1]);
3789                                 goto usage_error;
3790                         }
3791                         if (opc == SO_MIRROR_EXTEND) {
3792                                 if (!last_mirror) {
3793                                         fprintf(stderr,
3794                                 "error: %s: '-N' must exist in front of '%s'\n",
3795                                                 progname, argv[optopt + 1]);
3796                                         goto usage_error;
3797                                 }
3798                                 last_mirror->m_file = optarg;
3799                                 last_mirror->m_count = 1;
3800                         } else {
3801                                 /* mirror split */
3802                                 if (!mirror_list)
3803                                         mirror_list = lfs_mirror_alloc();
3804                                 mirror_list->m_file = optarg;
3805                         }
3806                         has_m_file = true;
3807                         break;
3808                 case 'L':
3809                         if (strcmp(argv[optind - 1], "mdt") == 0) {
3810                                 /* Can be only the first component */
3811                                 if (layout) {
3812                                         result = -EINVAL;
3813                                         fprintf(stderr,
3814                                                 "error: 'mdt' layout can be only the first one\n");
3815                                         goto error;
3816                                 }
3817                                 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3818                                         result = -EFBIG;
3819                                         fprintf(stderr,
3820                                                 "error: 'mdt' layout size is too big\n");
3821                                         goto error;
3822                                 }
3823                                 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3824                                 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3825                         } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3826                                 result = -EINVAL;
3827                                 fprintf(stderr,
3828                                         "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3829                                         argv[optind]);
3830                                 goto error;
3831                         }
3832                         break;
3833                 case 'm':
3834                         if (!migrate_mode) {
3835                                 fprintf(stderr,
3836                                         "%s %s: -m|--mdt-index is valid only for migrate command\n",
3837                                         progname, argv[0]);
3838                                 goto usage_error;
3839                         }
3840                         migrate_mdt_mode = true;
3841                         lsa.lsa_nr_tgts = parse_targets(tgts,
3842                                                 sizeof(tgts) / sizeof(__u32),
3843                                                 lsa.lsa_nr_tgts, optarg, NULL);
3844                         if (lsa.lsa_nr_tgts < 0) {
3845                                 fprintf(stderr,
3846                                         "%s: invalid MDT target(s) '%s'\n",
3847                                         progname, optarg);
3848                                 goto usage_error;
3849                         }
3850
3851                         lsa.lsa_tgts = tgts;
3852                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3853                                 lsa.lsa_stripe_off = tgts[0];
3854                         break;
3855                 case 'n':
3856                         if (!migrate_mode) {
3857                                 fprintf(stderr,
3858                                         "%s %s: -n|--non-block valid only for migrate command\n",
3859                                         progname, argv[0]);
3860                                 goto usage_error;
3861                         }
3862                         migration_flags |= LLAPI_MIGRATION_NONBLOCK;
3863                         break;
3864                 case 'N':
3865                         if (opc == SO_SETSTRIPE) {
3866                                 opc = SO_MIRROR_CREATE;
3867                                 mirror_mode = true;
3868                         }
3869                         mirror_count = 1;
3870                         if (optarg) {
3871                                 errno = 0;
3872                                 mirror_count = strtoul(optarg, &end, 0);
3873                                 if (errno != 0 || *end != '\0' ||
3874                                     mirror_count == 0 ||
3875                                     mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3876                                         fprintf(stderr,
3877                                                 "error: %s: bad mirror count: %s\n",
3878                                                 progname, optarg);
3879                                         result = -EINVAL;
3880                                         goto error;
3881                                 }
3882                         }
3883
3884                         new_mirror = lfs_mirror_alloc();
3885                         new_mirror->m_count = mirror_count;
3886
3887                         if (!mirror_list)
3888                                 mirror_list = new_mirror;
3889
3890                         if (last_mirror) {
3891                                 /* wrap up last mirror */
3892                                 if (!setstripe_args_specified(&lsa))
3893                                         last_mirror->m_inherit = true;
3894                                 if (lsa.lsa_comp_end == 0)
3895                                         lsa.lsa_comp_end = LUSTRE_EOF;
3896
3897                                 result = comp_args_to_layout(lpp, &lsa, true);
3898                                 if (result) {
3899                                         lfs_mirror_free(new_mirror);
3900                                         goto error;
3901                                 }
3902
3903                                 setstripe_args_init_inherit(&lsa);
3904
3905                                 last_mirror->m_next = new_mirror;
3906                         }
3907
3908                         last_mirror = new_mirror;
3909                         lpp = &last_mirror->m_layout;
3910                         break;
3911                 case 'o':
3912 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3913                         if (strcmp(argv[optind - 1], "--ost-list") == 0)
3914                                 fprintf(stderr,
3915                                         "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3916 #endif
3917                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3918                                 fprintf(stderr,
3919                                         "%s %s: -o|--ost incompatible with DoM layout\n",
3920                                         progname, argv[0]);
3921                                 goto usage_error;
3922                         }
3923                         /*
3924                          * -o allows overstriping, and must note it because
3925                          * parse_targets is shared with MDT striping, which
3926                          * does not allow duplicates
3927                          */
3928                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3929                         lsa.lsa_nr_tgts = parse_targets(tgts,
3930                                                 sizeof(tgts) / sizeof(__u32),
3931                                                 lsa.lsa_nr_tgts, optarg,
3932                                                 &lsa.lsa_pattern);
3933                         if (lsa.lsa_nr_tgts < 0) {
3934                                 fprintf(stderr,
3935                                         "%s %s: invalid OST target(s) '%s'\n",
3936                                         progname, argv[0], optarg);
3937                                 goto usage_error;
3938                         }
3939
3940                         lsa.lsa_tgts = tgts;
3941                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3942                                 lsa.lsa_stripe_off = tgts[0];
3943                         break;
3944                 case 'p':
3945                         if (!optarg)
3946                                 goto usage_error;
3947
3948                         if (optarg[0] == '\0' || lov_pool_is_inherited(optarg))
3949                                 lsa.lsa_pool_name = NULL;
3950                         else
3951                                 lsa.lsa_pool_name = optarg;
3952                         break;
3953                 case 'S':
3954                         result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3955                                                   &size_units, 0);
3956                         /* assume units of KB if too small to be valid */
3957                         if (lsa.lsa_stripe_size < 4096)
3958                                 lsa.lsa_stripe_size *= 1024;
3959                         if (result ||
3960                             lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3961                                 fprintf(stderr,
3962                                         "%s %s: invalid stripe size '%s'\n",
3963                                         progname, argv[0], optarg);
3964                                 goto usage_error;
3965                         }
3966                         break;
3967                 case 'v':
3968                         if (!migrate_mode) {
3969                                 fprintf(stderr,
3970                                         "%s %s: -v|--verbose valid only for migrate command\n",
3971                                         progname, argv[0]);
3972                                 goto usage_error;
3973                         }
3974                         migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3975                         migration_flags = LLAPI_MIGRATION_VERBOSE;
3976                         break;
3977                 case 'x':
3978                         xattr = optarg;
3979                         break;
3980                 case 'y':
3981                         from_yaml = true;
3982                         template = optarg;
3983                         break;
3984                 case 'z':
3985                         result = llapi_parse_size(optarg,
3986                                                   &lsa.lsa_extension_size,
3987                                                   &size_units, 0);
3988                         if (result) {
3989                                 fprintf(stderr,
3990                                         "%s %s: invalid extension size '%s'\n",
3991                                         progname, argv[0], optarg);
3992                                 goto usage_error;
3993                         }
3994
3995                         lsa.lsa_extension_comp = true;
3996                         break;
3997                 default:
3998                         fprintf(stderr, "%s: unrecognized option '%s'\n",
3999                                 progname, argv[optind - 1]);
4000                 case 'h':
4001                         goto usage_error;
4002                 }
4003         }
4004
4005         fname = argv[optind];
4006
4007         if (optind == argc) {
4008                 fprintf(stderr, "%s %s: FILE must be specified\n",
4009                         progname, argv[0]);
4010                 goto usage_error;
4011         }
4012
4013         /* lfs migrate $filename should keep the file's layout by default */
4014         if (migrate_mode && !layout && !from_yaml &&
4015             !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4016                 from_copy = true;
4017
4018         if (xattr && !foreign_mode) {
4019                 /*
4020                  * only print a warning as this is harmless and will be ignored
4021                  */
4022                 fprintf(stderr,
4023                         "%s %s: xattr has been specified for non-foreign layout\n",
4024                         progname, argv[0]);
4025         } else if (foreign_mode && !xattr) {
4026                 fprintf(stderr,
4027                         "%s %s: xattr must be provided in foreign mode\n",
4028                         progname, argv[0]);
4029                 goto usage_error;
4030         }
4031
4032         if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4033             comp_set || comp_id || delete || from_copy ||
4034             setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4035             lsa.lsa_tgts)) {
4036                 fprintf(stderr,
4037                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4038                         progname, argv[0]);
4039                 return CMD_HELP;
4040         }
4041
4042         if (mirror_mode && mirror_count == 0) {
4043                 fprintf(stderr,
4044                         "error: %s: --mirror-count|-N option is required\n",
4045                         progname);
4046                 result = -EINVAL;
4047                 goto error;
4048         }
4049
4050         if (mirror_mode) {
4051                 if (!setstripe_args_specified(&lsa))
4052                         last_mirror->m_inherit = true;
4053                 if (lsa.lsa_comp_end == 0)
4054                         lsa.lsa_comp_end = LUSTRE_EOF;
4055         }
4056
4057         if (lsa.lsa_comp_end != 0) {
4058                 result = comp_args_to_layout(lpp, &lsa, true);
4059                 if (result) {
4060                         fprintf(stderr, "error: %s: invalid layout\n",
4061                                 progname);
4062                         result = -EINVAL;
4063                         goto error;
4064                 }
4065         }
4066
4067         if (mirror_flags & MF_NO_VERIFY) {
4068                 if (opc != SO_MIRROR_EXTEND) {
4069                         fprintf(stderr,
4070                                 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4071                                 progname);
4072                         result = -EINVAL;
4073                         goto error;
4074                 } else if (!has_m_file) {
4075                         fprintf(stderr,
4076                                 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4077                                 progname);
4078                         result = -EINVAL;
4079                         goto error;
4080                 }
4081         }
4082
4083         if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4084                 fprintf(stderr,
4085                         "%s %s: --component-set doesn't have component-id set\n",
4086                         progname, argv[0]);
4087                 goto usage_error;
4088         }
4089
4090         if ((delete + comp_set + comp_del + comp_add) > 1) {
4091                 fprintf(stderr,
4092                         "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4093                         progname, argv[0]);
4094                 goto usage_error;
4095         }
4096
4097         if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4098                        lsa.lsa_comp_flags != 0 || layout != NULL)) {
4099                 fprintf(stderr,
4100                         "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4101                         progname, argv[0]);
4102                 goto usage_error;
4103         }
4104
4105         if ((comp_set || comp_del) &&
4106             (setstripe_args_specified(&lsa) || layout != NULL)) {
4107                 fprintf(stderr,
4108                         "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4109                         progname, argv[0]);
4110                 goto usage_error;
4111         }
4112
4113         if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4114                 fprintf(stderr,
4115                         "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4116                         progname, argv[0]);
4117                 goto usage_error;
4118         }
4119
4120         if (comp_add || comp_del) {
4121                 struct stat st;
4122
4123                 result = lstat(fname, &st);
4124                 if (result == 0 && S_ISDIR(st.st_mode)) {
4125                         fprintf(stderr,
4126                                 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4127                                 progname);
4128                         goto usage_error;
4129                 }
4130
4131                 if (mirror_mode) {
4132                         fprintf(stderr,
4133                                 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4134                                 progname);
4135                         goto usage_error;
4136                 }
4137         }
4138
4139         if (comp_add) {
4140                 if (!layout) {
4141                         fprintf(stderr,
4142                                 "%s %s: option -E must be specified with --component-add\n",
4143                                 progname, argv[0]);
4144                         goto usage_error;
4145                 }
4146         }
4147
4148         if (from_yaml && from_copy) {
4149                 fprintf(stderr,
4150                         "%s: can't specify --yaml and --copy together\n",
4151                         progname);
4152                 goto error;
4153         }
4154
4155         if ((from_yaml || from_copy) &&
4156             (setstripe_args_specified(&lsa) || layout != NULL)) {
4157                 fprintf(stderr,
4158                         "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4159                         argv[0]);
4160                 goto error;
4161         }
4162
4163         if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4164                 fprintf(stderr,
4165                         "%s %s: options --non-block and --block are mutually exclusive\n",
4166                         progname, argv[0]);
4167                 goto usage_error;
4168         }
4169
4170         if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4171             opc != SO_MIRROR_DELETE && comp_id != 0) {
4172                 fprintf(stderr,
4173                         "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4174                         progname);
4175                 goto usage_error;
4176         }
4177
4178         if (migrate_mdt_mode) {
4179                 struct lmv_user_md *lmu;
4180
4181                 /* initialize migrate mdt parameters */
4182                 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4183                                                  LMV_USER_MAGIC_SPECIFIC));
4184                 if (!lmu) {
4185                         fprintf(stderr,
4186                                 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4187                                 progname, argv[0], strerror(ENOMEM));
4188                         result = -ENOMEM;
4189                         goto error;
4190                 }
4191                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4192                         lmu->lum_stripe_count = lsa.lsa_stripe_count;
4193                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4194                         fprintf(stderr,
4195                                 "%s %s: migrate should specify MDT index\n",
4196                                 progname, argv[0]);
4197                         free(lmu);
4198                         goto usage_error;
4199                 }
4200                 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4201                 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4202                         lmu->lum_hash_type = lsa.lsa_pattern;
4203                 else
4204                         lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4205                 if (lsa.lsa_pool_name) {
4206                         strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4207                                 sizeof(lmu->lum_pool_name) - 1);
4208                         lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4209                 }
4210                 if (lsa.lsa_nr_tgts > 1) {
4211                         int i;
4212
4213                         if (lsa.lsa_stripe_count > 0 &&
4214                             lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4215                             lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4216                                 fprintf(stderr,
4217                                         "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4218                                         progname, lsa.lsa_stripe_count,
4219                                         lsa.lsa_nr_tgts);
4220                                 free(lmu);
4221                                 goto usage_error;
4222                         }
4223
4224                         lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4225                         lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4226                         for (i = 0; i < lsa.lsa_nr_tgts; i++)
4227                                 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4228                 } else {
4229                         lmu->lum_magic = LMV_USER_MAGIC;
4230                 }
4231
4232                 migrate_mdt_param.fp_lmv_md = lmu;
4233                 migrate_mdt_param.fp_migrate = 1;
4234         } else if (!layout) {
4235                 if (lsa_args_stripe_count_check(&lsa))
4236                         goto usage_error;
4237
4238                 /* initialize stripe parameters */
4239                 param = calloc(1, offsetof(typeof(*param),
4240                                lsp_osts[lsa.lsa_nr_tgts]));
4241                 if (!param) {
4242                         fprintf(stderr,
4243                                 "%s %s: cannot allocate memory for parameters: %s\n",
4244                                 progname, argv[0], strerror(ENOMEM));
4245                         result = -ENOMEM;
4246                         goto error;
4247                 }
4248
4249                 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4250                         param->lsp_stripe_size = lsa.lsa_stripe_size;
4251                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4252                         if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4253                                 param->lsp_stripe_count = -1;
4254                         else
4255                                 param->lsp_stripe_count = lsa.lsa_stripe_count;
4256                 }
4257                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4258                         param->lsp_stripe_offset = -1;
4259                 else
4260                         param->lsp_stripe_offset = lsa.lsa_stripe_off;
4261                 param->lsp_stripe_pattern =
4262                                 llapi_pattern_to_lov(lsa.lsa_pattern);
4263                 if (param->lsp_stripe_pattern == EINVAL) {
4264                         fprintf(stderr, "error: %s: invalid stripe pattern\n",
4265                                 argv[0]);
4266                         free(param);
4267                         goto usage_error;
4268                 }
4269                 param->lsp_pool = lsa.lsa_pool_name;
4270                 param->lsp_is_specific = false;
4271
4272                 if (lsa.lsa_nr_tgts > 0) {
4273                         param->lsp_is_specific = true;
4274                         param->lsp_stripe_count = lsa.lsa_nr_tgts;
4275                         memcpy(param->lsp_osts, tgts,
4276                                sizeof(*tgts) * lsa.lsa_nr_tgts);
4277                 }
4278         }
4279
4280         if (from_yaml) {
4281                 /* generate a layout from a YAML template */
4282                 result = lfs_comp_create_from_yaml(template, &layout,
4283                                                    &lsa, tgts);
4284                 if (result) {
4285                         fprintf(stderr,
4286                                 "error: %s: can't create composite layout from template file %s\n",
4287                                 argv[0], template);
4288                         goto error;
4289                 }
4290         }
4291
4292         if (layout != NULL || mirror_list != NULL) {
4293                 if (mirror_list)
4294                         result = mirror_adjust_first_extents(mirror_list);
4295                 else
4296                         result = layout_adjust_first_extent(fname, layout,
4297                                                             comp_add);
4298                 if (result == -ENODATA)
4299                         comp_add = 0;
4300                 else if (result != 0) {
4301                         fprintf(stderr, "error: %s: invalid layout\n",
4302                                 progname);
4303                         goto error;
4304                 }
4305         }
4306
4307         for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4308                 if (from_copy) {
4309                         layout = llapi_layout_get_by_path(template ?: fname, 0);
4310                         if (!layout) {
4311                                 fprintf(stderr,
4312                                         "%s: can't create composite layout from file %s: %s\n",
4313                                         progname, template ?: fname,
4314                                         strerror(errno));
4315                                 result = -errno;
4316                                 goto error;
4317                         }
4318                 }
4319
4320                 if (migrate_mdt_mode) {
4321                         result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4322                 } else if (migrate_mode) {
4323                         result = lfs_migrate(fname, migration_flags, param,
4324                                              layout);
4325                 } else if (comp_set != 0) {
4326                         result = lfs_component_set(fname, comp_id,
4327                                                    lsa.lsa_pool_name,
4328                                                    lsa.lsa_comp_flags,
4329                                                    lsa.lsa_comp_neg_flags);
4330                 } else if (comp_del != 0) {
4331                         result = lfs_component_del(fname, comp_id,
4332                                                    lsa.lsa_comp_flags,
4333                                                    lsa.lsa_comp_neg_flags);
4334                 } else if (comp_add != 0) {
4335                         result = lfs_component_add(fname, layout);
4336                 } else if (opc == SO_MIRROR_CREATE) {
4337                         result = mirror_create(fname, mirror_list);
4338                 } else if (opc == SO_MIRROR_EXTEND) {
4339                         result = mirror_extend(fname, mirror_list,
4340                                                mirror_flags);
4341                 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4342                         if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4343                                 fprintf(stderr,
4344                                         "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4345                                         progname, fname);
4346                                 goto usage_error;
4347                         }
4348                         if (lsa.lsa_pool_name)
4349                                 mirror_flags |= MF_COMP_POOL;
4350                         else if (mirror_id != 0)
4351                                 comp_id = mirror_id;
4352                         else
4353                                 mirror_flags |= MF_COMP_ID;
4354                         if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4355                                 fprintf(stderr,
4356                                         "%s: the file specified by -f cannot be same as the source file '%s'\n",
4357                                         progname, fname);
4358                                 goto usage_error;
4359                         }
4360                         result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4361                                               mirror_flags,
4362                                               has_m_file ? mirror_list->m_file :
4363                                               NULL);
4364                 } else if (layout) {
4365                         result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4366                                                       mode, layout);
4367                         if (result >= 0) {
4368                                 close(result);
4369                                 result = 0;
4370                         }
4371                 } else if (foreign_mode) {
4372                         result = llapi_file_create_foreign(fname, mode, type,
4373                                                            flags, xattr);
4374                         if (result >= 0) {
4375                                 close(result);
4376                                 result = 0;
4377                         }
4378                 } else {
4379                         result = llapi_file_open_param(fname,
4380                                                        O_CREAT | O_WRONLY,
4381                                                        mode, param);
4382                         if (result >= 0) {
4383                                 close(result);
4384                                 result = 0;
4385                         }
4386                 }
4387                 if (result) {
4388                         /* Save the first error encountered. */
4389                         if (result2 == 0)
4390                                 result2 = result;
4391                         continue;
4392                 }
4393         }
4394
4395         if (mode_opt)
4396                 umask(previous_umask);
4397
4398         free(param);
4399         free(migrate_mdt_param.fp_lmv_md);
4400         llapi_layout_free(layout);
4401         lfs_mirror_list_free(mirror_list);
4402         return result2;
4403 usage_error:
4404         result = CMD_HELP;
4405 error:
4406         llapi_layout_free(layout);
4407         lfs_mirror_list_free(mirror_list);
4408         return result;
4409 }
4410
4411 static int lfs_poollist(int argc, char **argv)
4412 {
4413         if (argc != 2)
4414                 return CMD_HELP;
4415
4416         return llapi_poollist(argv[1]);
4417 }
4418
4419 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4420 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4421                        char *str)
4422 {
4423         long long t = 0;
4424         int sign = 0;
4425         char *endptr = "AD";
4426         char *timebuf;
4427
4428         if (str[0] == '+')
4429                 sign = 1;
4430         else if (str[0] == '-')
4431                 sign = -1;
4432
4433         if (sign)
4434                 str++;
4435
4436         for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4437                 long long val = strtoll(timebuf, &endptr, 0);
4438                 int unit = 1;
4439
4440                 switch (*endptr) {
4441                 case  'y':
4442                         unit *= 52; /* 52 weeks + 1 day below */
4443                         fallthrough;
4444                 case  'w':
4445                         unit *= 7;
4446                         if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4447                                 param->fp_time_margin *= (1 + unit / 52);
4448                         unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4449                         fallthrough;
4450                 case '\0': /* days are default unit if none used */
4451                         fallthrough;
4452                 case  'd':
4453                         unit *= 24;
4454                         fallthrough;
4455                 case  'h':
4456                         unit *= 60;
4457                         fallthrough;
4458                 case  'm':
4459                         unit *= 60;
4460                         fallthrough;
4461                 case  's':
4462                         break;
4463                         /* don't need to multiply by 1 for seconds */
4464                 default:
4465                         fprintf(stderr,
4466                                 "%s find: bad time string '%s': %s\n",
4467                                 progname, timebuf, strerror(EINVAL));
4468                         return LONG_MAX;
4469                 }
4470
4471                 if (param->fp_time_margin == 0 ||
4472                     (*endptr && unit < param->fp_time_margin))
4473                         param->fp_time_margin = unit;
4474
4475                 t += val * unit;
4476         }
4477         if (*time < t) {
4478                 if (sign != 0)
4479                         str--;
4480                 fprintf(stderr, "%s find: bad time '%s': too large\n",
4481                         progname, str);
4482                 return LONG_MAX;
4483         }
4484
4485         *set = *time - t;
4486
4487         return sign;
4488 }
4489
4490 static int str2quotaid(__u32 *id, const char *arg)
4491 {
4492         unsigned long int projid_tmp = 0;
4493         char *endptr = NULL;
4494
4495         projid_tmp = strtoul(arg, &endptr, 10);
4496         if (*endptr != '\0')
4497                 return -EINVAL;
4498         /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4499         if (projid_tmp >= UINT32_MAX)
4500                 return -ERANGE;
4501
4502         *id = projid_tmp;
4503         return 0;
4504 }
4505
4506 static int name2uid(unsigned int *id, const char *name)
4507 {
4508         struct passwd *passwd;
4509
4510         passwd = getpwnam(name);
4511         if (!passwd)
4512                 return -ENOENT;
4513         *id = passwd->pw_uid;
4514
4515         return 0;
4516 }
4517
4518 static int name2gid(unsigned int *id, const char *name)
4519 {
4520         struct group *group;
4521
4522         group = getgrnam(name);
4523         if (!group)
4524                 return -ENOENT;
4525         *id = group->gr_gid;
4526
4527         return 0;
4528 }
4529
4530 static inline int name2projid(unsigned int *id, const char *name)
4531 {
4532         return -ENOTSUP;
4533 }
4534
4535 static int uid2name(char **name, unsigned int id)
4536 {
4537         struct passwd *passwd;
4538
4539         passwd = getpwuid(id);
4540         if (!passwd)
4541                 return -ENOENT;
4542         *name = passwd->pw_name;
4543
4544         return 0;
4545 }
4546
4547 static inline int gid2name(char **name, unsigned int id)
4548 {
4549         struct group *group;
4550
4551         group = getgrgid(id);
4552         if (!group)
4553                 return -ENOENT;
4554         *name = group->gr_name;
4555
4556         return 0;
4557 }
4558
4559 static int name2layout(__u32 *layout, char *name)
4560 {
4561         char *ptr, *layout_name;
4562
4563         *layout = 0;
4564         for (ptr = name; ; ptr = NULL) {
4565                 layout_name = strtok(ptr, ",");
4566                 if (!layout_name)
4567                         break;
4568                 if (strcmp(layout_name, "released") == 0)
4569                         *layout |= LOV_PATTERN_F_RELEASED;
4570                 else if (strcmp(layout_name, "raid0") == 0)
4571                         *layout |= LOV_PATTERN_RAID0;
4572                 else if (strcmp(layout_name, "mdt") == 0)
4573                         *layout |= LOV_PATTERN_MDT;
4574                 else if (strcmp(layout_name, "overstriping") == 0)
4575                         *layout |= LOV_PATTERN_OVERSTRIPING;
4576                 else
4577                         return -1;
4578         }
4579         return 0;
4580 }
4581
4582 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4583 {
4584         int loop;
4585         int user, group, other;
4586         int who, all;
4587         char c, op;
4588         mode_t perm;
4589         mode_t usermask;
4590         mode_t previous_flags;
4591
4592         user = group = other = 0;
4593         all = 0;
4594         loop = 1;
4595         perm = 0;
4596         previous_flags = 0;
4597         *end = input;
4598         usermask = 0;
4599
4600         while (loop) {
4601                 switch (*input) {
4602                 case 'u':
4603                         user = 1;
4604                         break;
4605                 case 'g':
4606                         group = 1;
4607                         break;
4608                 case 'o':
4609                         other = 1;
4610                         break;
4611                 case 'a':
4612                         user = group = other = 1;
4613                         all = 1;
4614                         break;
4615                 default:
4616                         loop = 0;
4617                 }
4618
4619                 if (loop)
4620                         input++;
4621         }
4622
4623         who = user || group || other;
4624         if (!who) {
4625                 /* get the umask */
4626                 usermask = umask(0022);
4627                 umask(usermask);
4628                 usermask &= 07777;
4629         }
4630
4631         if (*input == '-' || *input == '+' || *input == '=')
4632                 op = *input++;
4633         else
4634                 /* operation is required */
4635                 return -1;
4636
4637         /* get the flags in *outmode */
4638         switch (*input) {
4639         case 'u':
4640                 previous_flags = (*outmode & 0700);
4641                 perm |= user  ? previous_flags : 0;
4642                 perm |= group ? (previous_flags >> 3) : 0;
4643                 perm |= other ? (previous_flags >> 6) : 0;
4644                 input++;
4645                 goto write_perm;
4646         case 'g':
4647                 previous_flags = (*outmode & 0070);
4648                 perm |= user  ? (previous_flags << 3) : 0;
4649                 perm |= group ? previous_flags : 0;
4650                 perm |= other ? (previous_flags >> 3) : 0;
4651                 input++;
4652                 goto write_perm;
4653         case 'o':
4654                 previous_flags = (*outmode & 0007);
4655                 perm |= user  ? (previous_flags << 6) : 0;
4656                 perm |= group ? (previous_flags << 3) : 0;
4657                 perm |= other ? previous_flags : 0;
4658                 input++;
4659                 goto write_perm;
4660         default:
4661                 break;
4662         }
4663
4664         /* this part is optional,
4665          * if empty perm = 0 and *outmode is not modified
4666          */
4667         loop = 1;
4668         while (loop) {
4669                 c = *input;
4670                 switch (c) {
4671                 case 'r':
4672                         perm |= user  ? 0400 : 0;
4673                         perm |= group ? 0040 : 0;
4674                         perm |= other ? 0004 : 0;
4675                         /* set read permission for uog except for umask's
4676                          * permissions
4677                          */
4678                         perm |= who   ? 0 : (0444 & ~usermask);
4679                         break;
4680                 case 'w':
4681                         perm |= user  ? 0200 : 0;
4682                         perm |= group ? 0020 : 0;
4683                         perm |= other ? 0002 : 0;
4684                         /* set write permission for uog except for umask'
4685                          * permissions
4686                          */
4687                         perm |= who   ? 0 : (0222 & ~usermask);
4688                         break;
4689                 case 'x':
4690                         perm |= user  ? 0100 : 0;
4691                         perm |= group ? 0010 : 0;
4692                         perm |= other ? 0001 : 0;
4693                         /* set execute permission for uog except for umask'
4694                          * permissions
4695                          */
4696                         perm |= who   ? 0 : (0111 & ~usermask);
4697                         break;
4698                 case 'X':
4699                         /*
4700                          * Adds execute permission to 'u', 'g' and/or 'g' if
4701                          * specified and either 'u', 'g' or 'o' already has
4702                          * execute permissions.
4703                          */
4704                         if ((*outmode & 0111) != 0) {
4705                                 perm |= user  ? 0100 : 0;
4706                                 perm |= group ? 0010 : 0;
4707                                 perm |= other ? 0001 : 0;
4708                                 perm |= !who  ? 0111 : 0;
4709                         }
4710                         break;
4711                 case 's':
4712                         /* s is ignored if o is given, but it's not an error */
4713                         if (other && !group && !user)
4714                                 break;
4715                         perm |= user  ? S_ISUID : 0;
4716                         perm |= group ? S_ISGID : 0;
4717                         break;
4718                 case 't':
4719                         /* 't' should be used when 'a' is given
4720                          * or who is empty
4721                          */
4722                         perm |= (!who || all) ? S_ISVTX : 0;
4723                         /* using ugo with t is not an error */
4724                         break;
4725                 default:
4726                         loop = 0;
4727                         break;
4728                 }
4729                 if (loop)
4730                         input++;
4731         }
4732
4733 write_perm:
4734         /* uog flags should be only one character long */
4735         if (previous_flags && (*input != '\0' && *input != ','))
4736                 return -1;
4737
4738         switch (op) {
4739         case '-':
4740                 /* remove the flags from outmode */
4741                 *outmode &= ~perm;
4742                 break;
4743         case '+':
4744                 /* add the flags to outmode */
4745                 *outmode |= perm;
4746                 break;
4747         case '=':
4748                 /* set the flags of outmode to perm */
4749                 if (perm != 0)
4750                         *outmode = perm;
4751                 break;
4752         }
4753
4754         *end = input;
4755         return 0;
4756 }
4757
4758 static int str2mode_t(const char *input, mode_t *outmode)
4759 {
4760         int ret;
4761         const char *iter;
4762
4763         ret = 0;
4764
4765         if (*input >= '0' && *input <= '7') {
4766                 /* parse octal representation */
4767                 char *end;
4768
4769                 iter = input;
4770
4771                 /* look for invalid digits in octal representation */
4772                 while (isdigit(*iter))
4773                         if (*iter++ > '7')
4774                                 return -1;
4775
4776                 errno = 0;
4777                 *outmode = strtoul(input, &end, 8);
4778
4779                 if (errno != 0 || *outmode > 07777) {
4780                         *outmode = 0;
4781                         ret = -1;
4782                 }
4783
4784         } else if (*input == '8' || *input == '9') {
4785                 /* error: invalid octal number */
4786                 ret = -1;
4787         } else {
4788                 /* parse coma seperated list of symbolic representation */
4789                 int rc;
4790                 const char *end;
4791
4792                 *outmode = 0;
4793                 rc = 0;
4794                 end = NULL;
4795
4796                 do {
4797                         rc = parse_symbolic(input, outmode, &end);
4798                         if (rc)
4799                                 return -1;
4800
4801                         input = end+1;
4802                 } while (*end == ',');
4803
4804                 if (*end != '\0')
4805                         ret = -1;
4806         }
4807         return ret;
4808 }
4809
4810 static int lfs_find(int argc, char **argv)
4811 {
4812         int c, rc;
4813         int ret = 0;
4814         time_t t;
4815         struct find_param param = {
4816                 .fp_max_depth = -1,
4817                 .fp_quiet = 1,
4818                 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4819         };
4820         struct option long_opts[] = {
4821         { .val = 'A',   .name = "atime",        .has_arg = required_argument },
4822         { .val = 'b',   .name = "blocks",       .has_arg = required_argument },
4823         { .val = 'B',   .name = "btime",        .has_arg = required_argument },
4824         { .val = 'B',   .name = "Btime",        .has_arg = required_argument },
4825         { .val = LFS_COMP_COUNT_OPT,
4826                         .name = "comp-count",   .has_arg = required_argument },
4827         { .val = LFS_COMP_COUNT_OPT,
4828                         .name = "component-count",
4829                                                 .has_arg = required_argument },
4830         { .val = LFS_COMP_FLAGS_OPT,
4831                         .name = "comp-flags",   .has_arg = required_argument },
4832         { .val = LFS_COMP_FLAGS_OPT,
4833                         .name = "component-flags",
4834                                                 .has_arg = required_argument },
4835         { .val = LFS_COMP_START_OPT,
4836                         .name = "comp-start",   .has_arg = required_argument },
4837         { .val = LFS_COMP_START_OPT,
4838                         .name = "component-start",
4839                                                 .has_arg = required_argument },
4840         { .val = LFS_MIRROR_STATE_OPT,
4841                         .name = "mirror-state", .has_arg = required_argument },
4842         { .val = LFS_NEWERXY_OPT,
4843                         .name = "newer",        .has_arg = required_argument},
4844         { .val = LFS_NEWERXY_OPT,
4845                         .name = "neweraa",      .has_arg = required_argument},
4846         { .val = LFS_NEWERXY_OPT,
4847                         .name = "neweram",      .has_arg = required_argument},
4848         { .val = LFS_NEWERXY_OPT,
4849                         .name = "newerac",      .has_arg = required_argument},
4850         { .val = LFS_NEWERXY_OPT,
4851                         .name = "newerab",      .has_arg = required_argument},
4852         { .val = LFS_NEWERXY_OPT,
4853                         .name = "newerma",      .has_arg = required_argument},
4854         { .val = LFS_NEWERXY_OPT,
4855                         .name = "newermm",      .has_arg = required_argument},
4856         { .val = LFS_NEWERXY_OPT,
4857                         .name = "newermc",      .has_arg = required_argument},
4858         { .val = LFS_NEWERXY_OPT,
4859                         .name = "newermb",      .has_arg = required_argument},
4860         { .val = LFS_NEWERXY_OPT,
4861                         .name = "newerca",      .has_arg = required_argument},
4862         { .val = LFS_NEWERXY_OPT,
4863                         .name = "newercm",      .has_arg = required_argument},
4864         { .val = LFS_NEWERXY_OPT,
4865                         .name = "newercc",      .has_arg = required_argument},
4866         { .val = LFS_NEWERXY_OPT,
4867                         .name = "newercb",      .has_arg = required_argument},
4868         { .val = LFS_NEWERXY_OPT,
4869                         .name = "newerba",      .has_arg = required_argument},
4870         { .val = LFS_NEWERXY_OPT,
4871                         .name = "newerbm",      .has_arg = required_argument},
4872         { .val = LFS_NEWERXY_OPT,
4873                         .name = "newerbc",      .has_arg = required_argument},
4874         { .val = LFS_NEWERXY_OPT,
4875                         .name = "newerbb",      .has_arg = required_argument},
4876         { .val = LFS_NEWERXY_OPT,
4877                         .name = "newerBa",      .has_arg = required_argument},
4878         { .val = LFS_NEWERXY_OPT,
4879                         .name = "newerBm",      .has_arg = required_argument},
4880         { .val = LFS_NEWERXY_OPT,
4881                         .name = "newerBc",      .has_arg = required_argument},
4882         { .val = LFS_NEWERXY_OPT,
4883                         .name = "newerBB",      .has_arg = required_argument},
4884         { .val = LFS_NEWERXY_OPT,
4885                         .name = "newerat",      .has_arg = required_argument},
4886         { .val = LFS_NEWERXY_OPT,
4887                         .name = "newermt",      .has_arg = required_argument},
4888         { .val = LFS_NEWERXY_OPT,
4889                         .name = "newerct",      .has_arg = required_argument},
4890         { .val = LFS_NEWERXY_OPT,
4891                         .name = "newerbt",      .has_arg = required_argument},
4892         { .val = LFS_NEWERXY_OPT,
4893                         .name = "newerBt",      .has_arg = required_argument},
4894         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument },
4895         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument },
4896         { .val = 'C',   .name = "ctime",        .has_arg = required_argument },
4897 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4898         { .val = 'D',   .name = "maxdepth",     .has_arg = required_argument },
4899         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument },
4900         { .val = 'E',   .name = "component-end",
4901                                                 .has_arg = required_argument },
4902 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
4903         { .val = LFS_LAYOUT_FOREIGN_OPT,
4904                         .name = "foreign",      .has_arg = optional_argument},
4905         { .val = 'g',   .name = "gid",          .has_arg = required_argument },
4906         { .val = 'G',   .name = "group",        .has_arg = required_argument },
4907         { .val = 'h',   .name = "help",         .has_arg = no_argument },
4908         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
4909         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument },
4910         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
4911 /* getstripe { .val = 'I', .name = "comp-id",   .has_arg = required_argument }*/
4912         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
4913         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
4914         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
4915         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
4916         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument },
4917         { .val = 'M',   .name = "mtime",        .has_arg = required_argument },
4918         { .val = 'n',   .name = "name",         .has_arg = required_argument },
4919         { .val = 'N',   .name = "mirror-count", .has_arg = required_argument },
4920 /* find { .val = 'o'    .name = "or", .has_arg = no_argument }, like find(1) */
4921         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
4922         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
4923         { .val = LFS_FIND_PERM,
4924                         .name = "perm",         .has_arg = required_argument },
4925         /* no short option for pool yet, can be 'p' after 2.18 */
4926         { .val = LFS_POOL_OPT,
4927                         .name = "pool",         .has_arg = required_argument },
4928         { .val = '0',   .name = "print0",       .has_arg = no_argument },
4929         { .val = 'P',   .name = "print",        .has_arg = no_argument },
4930         { .val = LFS_PRINTF_OPT,
4931                         .name = "printf",       .has_arg = required_argument },
4932         { .val = LFS_PROJID_OPT,
4933                         .name = "projid",       .has_arg = required_argument },
4934 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
4935 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4936 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
4937         { .val = 's',   .name = "size",         .has_arg = required_argument },
4938         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
4939         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
4940         { .val = 't',   .name = "type",         .has_arg = required_argument },
4941         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
4942         { .val = 'u',   .name = "uid",          .has_arg = required_argument },
4943         { .val = 'U',   .name = "user",         .has_arg = required_argument },
4944 /* getstripe { .val = 'v', .name = "verbose",   .has_arg = no_argument }, */
4945         { .val = 'z',   .name = "extension-size",
4946                                                 .has_arg = required_argument },
4947         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument },
4948         { .name = NULL } };
4949         int optidx = 0;
4950         int pathstart = -1;
4951         int pathend = -1;
4952         int pathbad = -1;
4953         int neg_opt = 0;
4954         time_t *xtime;
4955         int *xsign;
4956         int isoption;
4957         char *endptr;
4958
4959         time(&t);
4960
4961         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4962         while ((c = getopt_long_only(argc, argv,
4963                 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
4964                 long_opts, &optidx)) >= 0) {
4965                 xtime = NULL;
4966                 xsign = NULL;
4967                 if (neg_opt)
4968                         --neg_opt;
4969                 /* '!' is part of option */
4970                 /*
4971                  * when getopt_long_only() finds a string which is not
4972                  * an option nor a known option argument it returns 1
4973                  * in that case if we already have found pathstart and pathend
4974                  * (i.e. we have the list of pathnames),
4975                  * the only supported value is "!"
4976                  */
4977                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4978                 if (!isoption && pathend != -1) {
4979                         fprintf(stderr,
4980                                 "err: %s: filename|dirname must either precede options or follow options\n",
4981                                 argv[0]);
4982                         ret = CMD_HELP;
4983                         goto err;
4984                 }
4985                 if (!isoption && pathstart == -1)
4986                         pathstart = optind - 1;
4987                 if (isoption && pathstart != -1 && pathend == -1)
4988                         pathend = optind - 2;
4989                 switch (c) {
4990                 case 0:
4991                         /* Long options. */
4992                         break;
4993                 case 1:
4994                         /*
4995                          * unknown; opt is "!" or path component,
4996                          * checking done above.
4997                          */
4998                         if (strcmp(optarg, "!") == 0)
4999                                 neg_opt = 2;
5000                         break;
5001                 case 'A':
5002                         xtime = &param.fp_atime;
5003                         xsign = &param.fp_asign;
5004                         param.fp_exclude_atime = !!neg_opt;
5005                         /* no break, this falls through to 'B' for btime */
5006                         fallthrough;
5007                 case 'B':
5008                         if (c == 'B') {
5009                                 xtime = &param.fp_btime;
5010                                 xsign = &param.fp_bsign;
5011                                 param.fp_exclude_btime = !!neg_opt;
5012                         }
5013                         /* no break, this falls through to 'C' for ctime */
5014                         fallthrough;
5015                 case 'C':
5016                         if (c == 'C') {
5017                                 xtime = &param.fp_ctime;
5018                                 xsign = &param.fp_csign;
5019                                 param.fp_exclude_ctime = !!neg_opt;
5020                         }
5021                         /* no break, this falls through to 'M' for mtime */
5022                         fallthrough;
5023                 case 'M':
5024                         if (c == 'M') {
5025                                 xtime = &param.fp_mtime;
5026                                 xsign = &param.fp_msign;
5027                                 param.fp_exclude_mtime = !!neg_opt;
5028                         }
5029                         rc = set_time(&param, &t, xtime, optarg);
5030                         if (rc == LONG_MAX) {
5031                                 ret = -1;
5032                                 goto err;
5033                         }
5034                         if (rc)
5035                                 *xsign = rc;
5036                         break;
5037                 case 'b':
5038                         if (optarg[0] == '+') {
5039                                 param.fp_blocks_sign = -1;
5040                                 optarg++;
5041                         } else if (optarg[0] == '-') {
5042                                 param.fp_blocks_sign =  1;
5043                                 optarg++;
5044                         }
5045
5046                         param.fp_blocks_units = 1024;
5047                         ret = llapi_parse_size(optarg, &param.fp_blocks,
5048                                                &param.fp_blocks_units, 0);
5049                         if (ret) {
5050                                 fprintf(stderr, "error: bad blocks '%s'\n",
5051                                         optarg);
5052                                 goto err;
5053                         }
5054                         param.fp_check_blocks = 1;
5055                         param.fp_exclude_blocks = !!neg_opt;
5056                         break;
5057                 case LFS_COMP_COUNT_OPT:
5058                         if (optarg[0] == '+') {
5059                                 param.fp_comp_count_sign = -1;
5060                                 optarg++;
5061                         } else if (optarg[0] == '-') {
5062                                 param.fp_comp_count_sign =  1;
5063                                 optarg++;
5064                         }
5065
5066                         errno = 0;
5067                         param.fp_comp_count = strtoul(optarg, &endptr, 0);
5068                         if (errno != 0 || *endptr != '\0' ||
5069                             param.fp_comp_count > UINT32_MAX) {
5070                                 fprintf(stderr,
5071                                         "error: bad component count '%s'\n",
5072                                         optarg);
5073                                 goto err;
5074                         }
5075                         param.fp_check_comp_count = 1;
5076                         param.fp_exclude_comp_count = !!neg_opt;
5077                         break;
5078                 case LFS_COMP_FLAGS_OPT:
5079                         rc = comp_str2flags(optarg, &param.fp_comp_flags,
5080                                             &param.fp_comp_neg_flags);
5081                         if (rc) {
5082                                 fprintf(stderr,
5083                                         "error: bad component flags '%s'\n",
5084                                         optarg);
5085                                 goto err;
5086                         }
5087                         param.fp_check_comp_flags = 1;
5088                         if (neg_opt) {
5089                                 __u32 flags = param.fp_comp_neg_flags;
5090
5091                                 param.fp_comp_neg_flags = param.fp_comp_flags;
5092                                 param.fp_comp_flags = flags;
5093                         }
5094                         break;
5095                 case LFS_COMP_START_OPT:
5096                         if (optarg[0] == '+') {
5097                                 param.fp_comp_start_sign = -1;
5098                                 optarg++;
5099                         } else if (optarg[0] == '-') {
5100                                 param.fp_comp_start_sign =  1;
5101                                 optarg++;
5102                         }
5103
5104                         rc = llapi_parse_size(optarg, &param.fp_comp_start,
5105                                               &param.fp_comp_start_units, 0);
5106                         if (rc) {
5107                                 fprintf(stderr,
5108                                         "error: bad component start '%s'\n",
5109                                         optarg);
5110                                 goto err;
5111                         }
5112                         param.fp_check_comp_start = 1;
5113                         param.fp_exclude_comp_start = !!neg_opt;
5114                         break;
5115                 case LFS_MIRROR_STATE_OPT:
5116                         rc = mirror_str2state(optarg, &param.fp_mirror_state,
5117                                               &param.fp_mirror_neg_state);
5118                         if (rc) {
5119                                 fprintf(stderr,
5120                                         "error: bad mirrored file state '%s'\n",
5121                                         optarg);
5122                                 goto err;
5123                         }
5124                         param.fp_check_mirror_state = 1;
5125                         if (neg_opt) {
5126                                 __u16 state = param.fp_mirror_neg_state;
5127
5128                                 param.fp_mirror_neg_state =
5129                                         param.fp_mirror_state;
5130                                 param.fp_mirror_state = state;
5131                         }
5132                         break;
5133                 case 'c':
5134                         if (optarg[0] == '+') {
5135                                 param.fp_stripe_count_sign = -1;
5136                                 optarg++;
5137                         } else if (optarg[0] == '-') {
5138                                 param.fp_stripe_count_sign =  1;
5139                                 optarg++;
5140                         }
5141
5142                         errno = 0;
5143                         param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5144                         if (errno != 0 || *endptr != '\0' ||
5145                             param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5146                                 fprintf(stderr,
5147                                         "error: bad stripe_count '%s'\n",
5148                                         optarg);
5149                                 ret = -1;
5150                                 goto err;
5151                         }
5152                         param.fp_check_stripe_count = 1;
5153                         param.fp_exclude_stripe_count = !!neg_opt;
5154                         break;
5155                 case 'D':
5156                         errno = 0;
5157                         param.fp_max_depth = strtol(optarg, 0, 0);
5158                         if (errno != 0 || param.fp_max_depth < 0) {
5159                                 fprintf(stderr,
5160                                         "error: bad maxdepth '%s'\n",
5161                                         optarg);
5162                                 ret = -1;
5163                                 goto err;
5164                         }
5165                         break;
5166                 case 'E':
5167                         if (optarg[0] == '+') {
5168                                 param.fp_comp_end_sign = -1;
5169                                 optarg++;
5170                         } else if (optarg[0] == '-') {
5171                                 param.fp_comp_end_sign =  1;
5172                                 optarg++;
5173                         }
5174
5175                         if (arg_is_eof(optarg)) {
5176                                 param.fp_comp_end = LUSTRE_EOF;
5177                                 param.fp_comp_end_units = 1;
5178                                 rc = 0;
5179                         } else {
5180                                 rc = llapi_parse_size(optarg,
5181                                                 &param.fp_comp_end,
5182                                                 &param.fp_comp_end_units, 0);
5183                                 /* assume units of KB if too small */
5184                                 if (param.fp_comp_end < 4096)
5185                                         param.fp_comp_end *= 1024;
5186                         }
5187                         if (rc) {
5188                                 fprintf(stderr,
5189                                         "error: bad component end '%s'\n",
5190                                         optarg);
5191                                 goto err;
5192                         }
5193                         param.fp_check_comp_end = 1;
5194                         param.fp_exclude_comp_end = !!neg_opt;
5195                         break;
5196                 case LFS_LAYOUT_FOREIGN_OPT: {
5197                         /* all types by default */
5198                         uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5199
5200                         if (optarg) {
5201                                 /* check pure numeric */
5202                                 type = strtoul(optarg, &endptr, 0);
5203                                 if (*endptr) {
5204                                         /* check name */
5205                                         type = check_foreign_type_name(optarg);
5206                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5207                                                 fprintf(stderr,
5208                                                         "%s %s: unknown foreign type '%s'\n",
5209                                                         progname, argv[0],
5210                                                         optarg);
5211                                                 return CMD_HELP;
5212                                         }
5213                                 } else if (type >= UINT32_MAX) {
5214                                         fprintf(stderr,
5215                                                 "%s %s: invalid foreign type '%s'\n",
5216                                                 progname, argv[0], optarg);
5217                                         return CMD_HELP;
5218                                 }
5219                         }
5220                         param.fp_foreign_type = type;
5221                         param.fp_check_foreign = 1;
5222                         param.fp_exclude_foreign = !!neg_opt;
5223                         break;
5224                 }
5225                 case LFS_NEWERXY_OPT: {
5226                         char x = 'm';
5227                         char y = 'm';
5228                         int xidx;
5229                         int negidx;
5230                         time_t *newery;
5231                         time_t ref = time(NULL);
5232
5233                         /* no need to check bad options, they won't get here */
5234                         if (strlen(long_opts[optidx].name) == 7) {
5235                                 x = long_opts[optidx].name[5];
5236                                 y = long_opts[optidx].name[6];
5237                         }
5238
5239                         if (y == 't') {
5240                                 static const char *const fmts[] = {
5241                                         "%Y-%m-%d %H:%M:%S",
5242                                         "%Y-%m-%d %H:%M",
5243                                         "%Y-%m-%d",
5244                                         "%H:%M:%S", /* sometime today */
5245                                         "%H:%M",
5246                                         "@%s",
5247                                         "%s",
5248                                         NULL };
5249                                 struct tm tm;
5250                                 bool found = false;
5251                                 int i;
5252
5253                                 for (i = 0; fmts[i] != NULL; i++) {
5254                                         char *ptr;
5255
5256                                         /* Init for times relative to today */
5257                                         if (strncmp(fmts[i], "%H", 2) == 0) {
5258                                                 localtime_r(&ref, &tm);
5259                                         } else {
5260                                                 memset(&tm, 0, sizeof(tm));
5261                                                 tm.tm_isdst = -1;
5262                                         }
5263                                         ptr = strptime(optarg, fmts[i], &tm);
5264                                         /* Skip spaces */
5265                                         while (ptr && isspace(*ptr))
5266                                                 ptr++;
5267                                         if (ptr == optarg + strlen(optarg)) {
5268                                                 found = true;
5269                                                 break;
5270                                         }
5271                                 }
5272
5273                                 if (!found) {
5274                                         fprintf(stderr,
5275                                                 "%s: invalid time '%s'\n",
5276                                                 progname, optarg);
5277                                         fprintf(stderr,
5278                                                 "supported formats are:\n  ");
5279                                         for (i = 0; fmts[i] != NULL; i++)
5280                                                 fprintf(stderr, "'%s', ",
5281                                                         fmts[i]);
5282                                         fprintf(stderr, "\n");
5283                                         ret = -EINVAL;
5284                                         goto err;
5285                                 }
5286
5287                                 ref = mktime(&tm);
5288                         } else if (y == 'b' || y == 'B') {
5289                                 lstatx_t stx;
5290
5291                                 rc = llapi_get_lum_file(optarg, NULL, &stx,
5292                                                         NULL, 0);
5293                                 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5294                                         if (!(stx.stx_mask & STATX_BTIME))
5295                                                 ret = -EOPNOTSUPP;
5296                                         else
5297                                                 ret = -errno;
5298                                         fprintf(stderr,
5299                                                 "%s: get btime failed '%s': %s\n",
5300                                                 progname, optarg,
5301                                                 strerror(-ret));
5302                                         goto err;
5303                                 }
5304
5305                                 ref = stx.stx_btime.tv_sec;
5306                         } else {
5307                                 struct stat statbuf;
5308
5309                                 if (stat(optarg, &statbuf) < 0) {
5310                                         fprintf(stderr,
5311                                                 "%s: cannot stat file '%s': %s\n",
5312                                                 progname, optarg,
5313                                                 strerror(errno));
5314                                         ret = -errno;
5315                                         goto err;
5316                                 }
5317
5318                                 switch (y) {
5319                                 case 'a':
5320                                         ref = statbuf.st_atime;
5321                                         break;
5322                                 case 'm':
5323                                         ref = statbuf.st_mtime;
5324                                         break;
5325                                 case 'c':
5326                                         ref = statbuf.st_ctime;
5327                                         break;
5328                                 default:
5329                                         fprintf(stderr,
5330                                                 "%s: invalid Y argument: '%c'\n",
5331                                                 progname, x);
5332                                         ret = -EINVAL;
5333                                         goto err;
5334                                 }
5335                         }
5336
5337                         switch (x) {
5338                         case 'a':
5339                                 xidx = NEWERXY_ATIME;
5340                                 break;
5341                         case 'm':
5342                                 xidx = NEWERXY_MTIME;
5343                                 break;
5344                         case 'c':
5345                                 xidx = NEWERXY_CTIME;
5346                                 break;
5347                         case 'b':
5348                         case 'B':
5349                                 xidx = NEWERXY_BTIME;
5350                                 break;
5351                         default:
5352                                 fprintf(stderr,
5353                                         "%s: invalid X argument: '%c'\n",
5354                                         progname, x);
5355                                 ret = -EINVAL;
5356                                 goto err;
5357                         }
5358
5359                         negidx = !!neg_opt;
5360                         newery = &param.fp_newery[xidx][negidx];
5361
5362                         if (*newery == 0) {
5363                                 *newery = ref;
5364                         } else {
5365                                 if (negidx)
5366                                         *newery = *newery > ref ? ref : *newery;
5367                                 else
5368                                         *newery = *newery > ref ? *newery : ref;
5369                         }
5370                         param.fp_newerxy = 1;
5371                         break;
5372                 }
5373                 case 'g':
5374                 case 'G':
5375                         rc = name2gid(&param.fp_gid, optarg);
5376                         if (rc) {
5377                                 if (str2quotaid(&param.fp_gid, optarg)) {
5378                                         fprintf(stderr,
5379                                                 "Group/GID: %s cannot be found.\n",
5380                                                 optarg);
5381                                         ret = -1;
5382                                         goto err;
5383                                 }
5384                         }
5385                         param.fp_exclude_gid = !!neg_opt;
5386                         param.fp_check_gid = 1;
5387                         break;
5388                 case 'H':
5389                         rc = mdthash_input(optarg, &param.fp_hash_inflags,
5390                                            &param.fp_hash_exflags,
5391                                            &param.fp_hash_type);
5392                         if (rc) {
5393                                 ret = -1;
5394                                 goto err;
5395                         }
5396                         if (param.fp_hash_inflags || param.fp_hash_exflags)
5397                                 param.fp_check_hash_flag = 1;
5398                         param.fp_exclude_hash_type = !!neg_opt;
5399                         break;
5400                 case 'l':
5401                         param.fp_lazy = 1;
5402                         break;
5403                 case 'L':
5404                         ret = name2layout(&param.fp_layout, optarg);
5405                         if (ret)
5406                                 goto err;
5407                         param.fp_exclude_layout = !!neg_opt;
5408                         param.fp_check_layout = 1;
5409                         break;
5410                 case 'u':
5411                 case 'U':
5412                         rc = name2uid(&param.fp_uid, optarg);
5413                         if (rc) {
5414                                 if (str2quotaid(&param.fp_uid, optarg)) {
5415                                         fprintf(stderr,
5416                                                 "User/UID: %s cannot be found.\n",
5417                                                 optarg);
5418                                         ret = -1;
5419                                         goto err;
5420                                 }
5421                         }
5422                         param.fp_exclude_uid = !!neg_opt;
5423                         param.fp_check_uid = 1;
5424                         break;
5425                 case 'n':
5426                         param.fp_pattern = (char *)optarg;
5427                         param.fp_exclude_pattern = !!neg_opt;
5428                         break;
5429                 case 'N':
5430                         if (optarg[0] == '+') {
5431                                 param.fp_mirror_count_sign = -1;
5432                                 optarg++;
5433                         } else if (optarg[0] == '-') {
5434                                 param.fp_mirror_count_sign =  1;
5435                                 optarg++;
5436                         }
5437
5438                         errno = 0;
5439                         param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5440                         if (errno != 0 || *endptr != '\0' ||
5441                             param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5442                                 fprintf(stderr,
5443                                         "error: bad mirror count '%s'\n",
5444                                         optarg);
5445                                 goto err;
5446                         }
5447                         param.fp_check_mirror_count = 1;
5448                         param.fp_exclude_mirror_count = !!neg_opt;
5449                         break;
5450                 case 'm':
5451                 case 'i':
5452                 case 'O': {
5453                         char *buf, *token, *next, *p;
5454                         int len = 1;
5455                         void *tmp;
5456
5457                         buf = strdup(optarg);
5458                         if (!buf) {
5459                                 ret = -ENOMEM;
5460                                 goto err;
5461                         }
5462
5463                         param.fp_exclude_obd = !!neg_opt;
5464
5465                         token = buf;
5466                         while (token && *token) {
5467                                 token = strchr(token, ',');
5468                                 if (token) {
5469                                         len++;
5470                                         token++;
5471                                 }
5472                         }
5473                         if (c == 'm') {
5474                                 param.fp_exclude_mdt = !!neg_opt;
5475                                 param.fp_num_alloc_mdts += len;
5476                                 tmp = realloc(param.fp_mdt_uuid,
5477                                               param.fp_num_alloc_mdts *
5478                                               sizeof(*param.fp_mdt_uuid));
5479                                 if (!tmp) {
5480                                         ret = -ENOMEM;
5481                                         goto err_free;
5482                                 }
5483
5484                                 param.fp_mdt_uuid = tmp;
5485                         } else {
5486                                 param.fp_exclude_obd = !!neg_opt;
5487                                 param.fp_num_alloc_obds += len;
5488                                 tmp = realloc(param.fp_obd_uuid,
5489                                               param.fp_num_alloc_obds *
5490                                               sizeof(*param.fp_obd_uuid));
5491                                 if (!tmp) {
5492                                         ret = -ENOMEM;
5493                                         goto err_free;
5494                                 }
5495
5496                                 param.fp_obd_uuid = tmp;
5497                         }
5498                         for (token = buf; token && *token; token = next) {
5499                                 struct obd_uuid *puuid;
5500
5501                                 if (c == 'm') {
5502                                         puuid =
5503                                         &param.fp_mdt_uuid[param.fp_num_mdts++];
5504                                 } else {
5505                                         puuid =
5506                                         &param.fp_obd_uuid[param.fp_num_obds++];
5507                                 }
5508                                 p = strchr(token, ',');
5509                                 next = 0;
5510                                 if (p) {
5511                                         *p = 0;
5512                                         next = p+1;
5513                                 }
5514
5515                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5516                                         ret = -E2BIG;
5517                                         goto err_free;
5518                                 }
5519
5520                                 strncpy(puuid->uuid, token,
5521                                         sizeof(puuid->uuid));
5522                         }
5523 err_free:
5524                         if (buf)
5525                                 free(buf);
5526                         break;
5527                 }
5528 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5529                 case 'p':
5530 #endif
5531                 case LFS_POOL_OPT:
5532                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
5533                                 fprintf(stderr,
5534                                         "Pool name %s is too long (max %d)\n",
5535                                         optarg, LOV_MAXPOOLNAME);
5536                                 ret = -1;
5537                                 goto err;
5538                         }
5539                         /*
5540                          * We do check for empty pool because empty pool
5541                          * is used to find V1 LOV attributes
5542                          */
5543                         strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5544                         param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5545                         param.fp_exclude_pool = !!neg_opt;
5546                         param.fp_check_pool = 1;
5547                         break;
5548                 case '0':
5549                         param.fp_zero_end = 1;
5550                         break;
5551                 case 'P': /* we always print, this option is a no-op */
5552                         break;
5553                 case LFS_PRINTF_OPT:
5554                         param.fp_format_printf_str = optarg;
5555                         break;
5556                 case LFS_PROJID_OPT:
5557                         rc = name2projid(&param.fp_projid, optarg);
5558                         if (rc) {
5559                                 if (str2quotaid(&param.fp_projid, optarg)) {
5560                                         fprintf(stderr,
5561                                                 "Invalid project ID: %s\n",
5562                                                 optarg);
5563                                         ret = -1;
5564                                         goto err;
5565                                 }
5566                         }
5567                         param.fp_exclude_projid = !!neg_opt;
5568                         param.fp_check_projid = 1;
5569                         break;
5570                 case 's':
5571                         if (optarg[0] == '+') {
5572                                 param.fp_size_sign = -1;
5573                                 optarg++;
5574                         } else if (optarg[0] == '-') {
5575                                 param.fp_size_sign =  1;
5576                                 optarg++;
5577                         }
5578
5579                         ret = llapi_parse_size(optarg, &param.fp_size,
5580                                                &param.fp_size_units, 0);
5581                         if (ret) {
5582                                 fprintf(stderr, "error: bad file size '%s'\n",
5583                                         optarg);
5584                                 goto err;
5585                         }
5586                         param.fp_check_size = 1;
5587                         param.fp_exclude_size = !!neg_opt;
5588                         break;
5589                 case 'S':
5590                         if (optarg[0] == '+') {
5591                                 param.fp_stripe_size_sign = -1;
5592                                 optarg++;
5593                         } else if (optarg[0] == '-') {
5594                                 param.fp_stripe_size_sign =  1;
5595                                 optarg++;
5596                         }
5597
5598                         ret = llapi_parse_size(optarg, &param.fp_stripe_size,
5599                                                &param.fp_stripe_size_units, 0);
5600                         /* assume units of KB if too small to be valid */
5601                         if (param.fp_stripe_size < 4096)
5602                                 param.fp_stripe_size *= 1024;
5603                         if (ret) {
5604                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
5605                                         optarg);
5606                                 goto err;
5607                         }
5608                         param.fp_check_stripe_size = 1;
5609                         param.fp_exclude_stripe_size = !!neg_opt;
5610                         break;
5611                 case 't':
5612                         param.fp_exclude_type = !!neg_opt;
5613                         switch (optarg[0]) {
5614                         case 'b':
5615                                 param.fp_type = S_IFBLK;
5616                                 break;
5617                         case 'c':
5618                                 param.fp_type = S_IFCHR;
5619                                 break;
5620                         case 'd':
5621                                 param.fp_type = S_IFDIR;
5622                                 break;
5623                         case 'f':
5624                                 param.fp_type = S_IFREG;
5625                                 break;
5626                         case 'l':
5627                                 param.fp_type = S_IFLNK;
5628                                 break;
5629                         case 'p':
5630                                 param.fp_type = S_IFIFO;
5631                                 break;
5632                         case 's':
5633                                 param.fp_type = S_IFSOCK;
5634                                 break;
5635                         default:
5636                                 fprintf(stderr, "%s: bad type '%s'\n",
5637                                         progname, optarg);
5638                                 ret = CMD_HELP;
5639                                 goto err;
5640                         }
5641                         break;
5642                 case LFS_FIND_PERM:
5643                         param.fp_exclude_perm = !!neg_opt;
5644                         param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5645                         if (*optarg == '/') {
5646                                 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5647                                 optarg++;
5648                         } else if (*optarg == '-') {
5649                                 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5650                                 optarg++;
5651                         }
5652
5653                         if (str2mode_t(optarg, &param.fp_perm)) {
5654                                 fprintf(stderr, "error: invalid mode '%s'\n",
5655                                         optarg);
5656                                 ret = -1;
5657                                 goto err;
5658                         }
5659                         break;
5660                 case 'T':
5661                         if (optarg[0] == '+') {
5662                                 param.fp_mdt_count_sign = -1;
5663                                 optarg++;
5664                         } else if (optarg[0] == '-') {
5665                                 param.fp_mdt_count_sign =  1;
5666                                 optarg++;
5667                         }
5668
5669                         errno = 0;
5670                         param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5671                         if (errno != 0 || *endptr != '\0' ||
5672                             param.fp_mdt_count >= UINT32_MAX) {
5673                                 fprintf(stderr, "error: bad mdt_count '%s'\n",
5674                                         optarg);
5675                                 ret = -1;
5676                                 goto err;
5677                         }
5678                         param.fp_check_mdt_count = 1;
5679                         param.fp_exclude_mdt_count = !!neg_opt;
5680                         break;
5681                 case 'z':
5682                         if (optarg[0] == '+') {
5683                                 param.fp_ext_size_sign = -1;
5684                                 optarg++;
5685                         } else if (optarg[0] == '-') {
5686                                 param.fp_ext_size_sign =  1;
5687                                 optarg++;
5688                         }
5689
5690                         ret = llapi_parse_size(optarg, &param.fp_ext_size,
5691                                                &param.fp_ext_size_units, 0);
5692                         if (ret) {
5693                                 fprintf(stderr, "error: bad ext-size '%s'\n",
5694                                         optarg);
5695                                 goto err;
5696                         }
5697                         param.fp_ext_size /= SEL_UNIT_SIZE;
5698                         param.fp_ext_size_units /= SEL_UNIT_SIZE;
5699                         param.fp_check_ext_size = 1;
5700                         param.fp_exclude_ext_size = !!neg_opt;
5701                         break;
5702                 default:
5703                         fprintf(stderr, "%s: unrecognized option '%s'\n",
5704                                 progname, argv[optind - 1]);
5705                 case 'h':
5706                         ret = CMD_HELP;
5707                         goto err;
5708                 }
5709         }
5710         if (!param.fp_verbose)
5711                 param.fp_verbose = VERBOSE_DEFAULT;
5712
5713         if (pathstart == -1) {
5714                 fprintf(stderr, "error: %s: no filename|pathname\n",
5715                         argv[0]);
5716                 ret = CMD_HELP;
5717                 goto err;
5718         } else if (pathend == -1) {
5719                 /* no options */
5720                 pathend = argc;
5721         }
5722
5723         do {
5724                 rc = llapi_find(argv[pathstart], &param);
5725                 if (rc && !ret) {
5726                         ret = rc;
5727                         pathbad = pathstart;
5728                 }
5729         } while (++pathstart < pathend);
5730
5731         if (ret)
5732                 fprintf(stderr, "%s: failed for '%s': %s\n",
5733                         progname, argv[pathbad], strerror(-rc));
5734
5735 err:
5736         if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5737                 free(param.fp_obd_uuid);
5738
5739         if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5740                 free(param.fp_mdt_uuid);
5741
5742         return ret;
5743 }
5744
5745 static int lfs_getstripe_internal(int argc, char **argv,
5746                                   struct find_param *param)
5747 {
5748         struct option long_opts[] = {
5749 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
5750 /* find { .val = 'b',   .name = "blocks",       .has_arg = required_argument }*/
5751 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
5752 /* find { .val = 'B',   .name = "Btime",        .has_arg = required_argument }*/
5753         { .val = LFS_COMP_COUNT_OPT,
5754                         .name = "comp-count",   .has_arg = no_argument },
5755         { .val = LFS_COMP_COUNT_OPT,
5756                 .name = "component-count",      .has_arg = no_argument },
5757         { .val = LFS_COMP_FLAGS_OPT,
5758                         .name = "comp-flags",   .has_arg = optional_argument },
5759         { .val = LFS_COMP_FLAGS_OPT,
5760                 .name = "component-flags",      .has_arg = optional_argument },
5761         { .val = LFS_COMP_START_OPT,
5762                         .name = "comp-start",   .has_arg = optional_argument },
5763         { .val = LFS_COMP_START_OPT,
5764                 .name = "component-start",      .has_arg = optional_argument },
5765         { .val = LFS_MIRROR_INDEX_OPT,
5766                 .name = "mirror-index",         .has_arg = required_argument },
5767         { .val = LFS_MIRROR_ID_OPT,
5768                 .name = "mirror-id",            .has_arg = required_argument },
5769         { .val = LFS_NO_FOLLOW_OPT,
5770                 .name = "no-follow",            .has_arg = no_argument },
5771         { .val = 'c',   .name = "stripe-count", .has_arg = no_argument },
5772         { .val = 'c',   .name = "stripe_count", .has_arg = no_argument },
5773 /* find { .val = 'C',   .name = "ctime",        .has_arg = required_argument }*/
5774         { .val = 'd',   .name = "directory",    .has_arg = no_argument },
5775         { .val = 'D',   .name = "default",      .has_arg = no_argument },
5776         { .val = 'E',   .name = "comp-end",     .has_arg = optional_argument },
5777         { .val = 'E',   .name = "component-end", .has_arg = optional_argument },
5778         { .val = 'F',   .name = "fid",          .has_arg = no_argument },
5779         { .val = 'g',   .name = "generation",   .has_arg = no_argument },
5780 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
5781         { .val = 'h',   .name = "help",         .has_arg = no_argument },
5782 /* dirstripe { .val = 'H', .name = "mdt-hash",  .has_arg = required_argument }*/
5783         { .val = 'i',   .name = "stripe-index", .has_arg = no_argument },
5784         { .val = 'i',   .name = "stripe_index", .has_arg = no_argument },
5785         { .val = 'I',   .name = "comp-id",      .has_arg = optional_argument },
5786         { .val = 'I',   .name = "component-id", .has_arg = optional_argument },
5787 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
5788         { .val = 'L',   .name = "layout",       .has_arg = no_argument },
5789         { .val = 'm',   .name = "mdt",          .has_arg = no_argument },
5790         { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
5791         { .val = 'm',   .name = "mdt_index",    .has_arg = no_argument },
5792 /* find { .val = 'M',   .name = "mtime",        .has_arg = required_argument }*/
5793 /* find { .val = 'n',   .name = "name",         .has_arg = required_argument }*/
5794         { .val = 'N',   .name = "mirror-count", .has_arg = no_argument },
5795         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
5796         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
5797         { .val = 'p',   .name = "pool",         .has_arg = no_argument },
5798 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
5799         { .val = 'q',   .name = "quiet",        .has_arg = no_argument },
5800         { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
5801         { .val = 'R',   .name = "raw",          .has_arg = no_argument },
5802         { .val = 'S',   .name = "stripe-size",  .has_arg = no_argument },
5803         { .val = 'S',   .name = "stripe_size",  .has_arg = no_argument },
5804 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
5805 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5806 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
5807 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
5808         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
5809 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5810         { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
5811         { .val = 'z',   .name = "extension-size", .has_arg = no_argument },
5812         { .val = 'z',   .name = "ext-size",     .has_arg = no_argument },
5813         { .name = NULL } };
5814         int c, rc;
5815         int neg_opt = 0;
5816         int pathstart = -1, pathend = -1;
5817         int isoption;
5818         char *end, *tmp;
5819
5820         while ((c = getopt_long(argc, argv,
5821                         "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5822                         long_opts, NULL)) != -1) {
5823                 if (neg_opt)
5824                         --neg_opt;
5825
5826                 /* '!' is part of option */
5827                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5828                 if (!isoption && pathend != -1) {
5829                         fprintf(stderr,
5830                                 "error: %s: filename|dirname must either precede options or follow options\n",
5831                                 argv[0]);
5832                         return CMD_HELP;
5833                 }
5834                 if (!isoption && pathstart == -1)
5835                         pathstart = optind - 1;
5836                 if (isoption && pathstart != -1 && pathend == -1)
5837                         pathend = optind - 2;
5838
5839                 switch (c) {
5840                 case 1:
5841                         /* unknown: opt is "!" */
5842                         if (strcmp(optarg, "!") == 0)
5843                                 neg_opt = 2;
5844                         break;
5845                 case 'c':
5846                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5847                                 param->fp_verbose |= VERBOSE_COUNT;
5848                                 param->fp_max_depth = 0;
5849                         }
5850                         break;
5851                 case LFS_COMP_COUNT_OPT:
5852                         param->fp_verbose |= VERBOSE_COMP_COUNT;
5853                         param->fp_max_depth = 0;
5854                         break;
5855                 case LFS_COMP_FLAGS_OPT:
5856                         if (optarg) {
5857                                 rc = comp_str2flags(optarg,
5858                                                     &param->fp_comp_flags,
5859                                                     &param->fp_comp_neg_flags);
5860                                 if (rc != 0) {
5861                                         fprintf(stderr,
5862                                                 "error: %s bad component flags '%s'.\n",
5863                                                 argv[0], optarg);
5864                                         return CMD_HELP;
5865                                 }
5866                                 param->fp_check_comp_flags = 1;
5867                         } else {
5868                                 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5869                                 param->fp_max_depth = 0;
5870                         }
5871                         break;
5872                 case LFS_COMP_START_OPT:
5873                         if (optarg) {
5874                                 tmp = optarg;
5875                                 if (tmp[0] == '+') {
5876                                         param->fp_comp_start_sign = -1;
5877                                         tmp++;
5878                                 } else if (tmp[0] == '-') {
5879                                         param->fp_comp_start_sign = 1;
5880                                         tmp++;
5881                                 }
5882                                 rc = llapi_parse_size(tmp,
5883                                                 &param->fp_comp_start,
5884                                                 &param->fp_comp_start_units, 0);
5885                                 if (rc != 0) {
5886                                         fprintf(stderr,
5887                                                 "error: %s bad component start '%s'.\n",
5888                                                 argv[0], tmp);
5889                                         return CMD_HELP;
5890                                 }
5891                                 param->fp_check_comp_start = 1;
5892                         } else {
5893                                 param->fp_verbose |= VERBOSE_COMP_START;
5894                                 param->fp_max_depth = 0;
5895                         }
5896                         break;
5897                 case LFS_MIRROR_INDEX_OPT: {
5898                         unsigned long int mirror_index;
5899
5900                         if (optarg[0] == '+') {
5901                                 param->fp_mirror_index_sign = -1;
5902                                 optarg++;
5903                         } else if (optarg[0] == '-') {
5904                                 param->fp_mirror_index_sign = 1;
5905                                 optarg++;
5906                         }
5907
5908                         errno = 0;
5909                         mirror_index = strtoul(optarg, &end, 0);
5910                         if (errno != 0 || *end != '\0' ||
5911                             mirror_index > UINT16_MAX || (mirror_index == 0 &&
5912                             param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5913                                 fprintf(stderr,
5914                                         "%s %s: invalid mirror index '%s'\n",
5915                                         progname, argv[0], optarg);
5916                                 return CMD_HELP;
5917                         }
5918
5919                         param->fp_mirror_index = (__u16)mirror_index;
5920
5921                         if (param->fp_mirror_id != 0) {
5922                                 fprintf(stderr,
5923                                         "%s %s: can't specify both mirror index and mirror ID\n",
5924                                         progname, argv[0]);
5925                                 return CMD_HELP;
5926                         }
5927                         param->fp_check_mirror_index = 1;
5928                         param->fp_exclude_mirror_index = !!neg_opt;
5929                         break;
5930                 }
5931                 case LFS_MIRROR_ID_OPT: {
5932                         unsigned long int mirror_id;
5933
5934                         if (optarg[0] == '+') {
5935                                 param->fp_mirror_id_sign = -1;
5936                                 optarg++;
5937                         } else if (optarg[0] == '-') {
5938                                 param->fp_mirror_id_sign = 1;
5939                                 optarg++;
5940                         }
5941
5942                         errno = 0;
5943                         mirror_id = strtoul(optarg, &end, 0);
5944                         if (errno != 0 || *end != '\0' ||
5945                             mirror_id > UINT16_MAX || (mirror_id == 0 &&
5946                             param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5947                                 fprintf(stderr,
5948                                         "%s %s: invalid mirror ID '%s'\n",
5949                                         progname, argv[0], optarg);
5950                                 return CMD_HELP;
5951                         }
5952
5953                         param->fp_mirror_id = (__u16)mirror_id;
5954
5955                         if (param->fp_mirror_index != 0) {
5956                                 fprintf(stderr,
5957                                         "%s %s: can't specify both mirror index and mirror ID\n",
5958                                         progname, argv[0]);
5959                                 return CMD_HELP;
5960                         }
5961                         param->fp_check_mirror_id = 1;
5962                         param->fp_exclude_mirror_id = !!neg_opt;
5963                         break;
5964                 }
5965                 case LFS_NO_FOLLOW_OPT:
5966                         param->fp_no_follow = true;
5967                         break;
5968                 case 'd':
5969                         param->fp_max_depth = 0;
5970                         break;
5971                 case 'D':
5972                         param->fp_get_default_lmv = 1;
5973                         break;
5974                 case 'E':
5975                         if (optarg) {
5976                                 tmp = optarg;
5977                                 if (tmp[0] == '+') {
5978                                         param->fp_comp_end_sign = -1;
5979                                         tmp++;
5980                                 } else if (tmp[0] == '-') {
5981                                         param->fp_comp_end_sign = 1;
5982                                         tmp++;
5983                                 }
5984
5985                                 if (arg_is_eof(tmp)) {
5986                                         param->fp_comp_end = LUSTRE_EOF;
5987                                         param->fp_comp_end_units = 1;
5988                                         rc = 0;
5989                                 } else {
5990                                         rc = llapi_parse_size(tmp,
5991                                                 &param->fp_comp_end,
5992                                                 &param->fp_comp_end_units, 0);
5993                                         /* assume units of KB if too small */
5994                                         if (param->fp_comp_end < 4096)
5995                                                 param->fp_comp_end *= 1024;
5996                                 }
5997                                 if (rc != 0) {
5998                                         fprintf(stderr,
5999                                                 "error: %s bad component end '%s'.\n",
6000                                                 argv[0], tmp);
6001                                         return CMD_HELP;
6002                                 }
6003                                 param->fp_check_comp_end = 1;
6004                         } else {
6005                                 param->fp_verbose |= VERBOSE_COMP_END;
6006                                 param->fp_max_depth = 0;
6007                         }
6008                         break;
6009                 case 'F':
6010                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6011                                 param->fp_verbose |= VERBOSE_DFID;
6012                                 param->fp_max_depth = 0;
6013                         }
6014                         break;
6015                 case 'g':
6016                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6017                                 param->fp_verbose |= VERBOSE_GENERATION;
6018                                 param->fp_max_depth = 0;
6019                         }
6020                         break;
6021                 case 'i':
6022                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6023                                 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6024                                 param->fp_max_depth = 0;
6025                         }
6026                         break;
6027                 case 'I':
6028                         if (optarg) {
6029                                 param->fp_comp_id = strtoul(optarg, &end, 0);
6030                                 if (*end != '\0' || param->fp_comp_id == 0 ||
6031                                     param->fp_comp_id > LCME_ID_MAX) {
6032                                         fprintf(stderr,
6033                                                 "error: %s bad component id '%s'\n",
6034                                                 argv[0], optarg);
6035                                         return CMD_HELP;
6036                                 }
6037                                 param->fp_check_comp_id = 1;
6038                         } else {
6039                                 param->fp_max_depth = 0;
6040                                 param->fp_verbose |= VERBOSE_COMP_ID;
6041                         }
6042                         break;
6043                 case 'L':
6044                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6045                                 param->fp_verbose |= VERBOSE_PATTERN;
6046                                 param->fp_max_depth = 0;
6047                         }
6048                         break;
6049 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6050                 case 'M':
6051                         fprintf(stderr,
6052                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6053 #endif
6054                 case 'm':
6055                         if (!(param->fp_verbose & VERBOSE_DETAIL))
6056                                 param->fp_max_depth = 0;
6057                         param->fp_verbose |= VERBOSE_MDTINDEX;
6058                         break;
6059                 case 'N':
6060                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6061                                 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6062                                 param->fp_max_depth = 0;
6063                         }
6064                         break;
6065                 case 'O':
6066                         if (param->fp_obd_uuid) {
6067                                 fprintf(stderr,
6068                                         "error: %s: only one obduuid allowed",
6069                                         argv[0]);
6070                                 return CMD_HELP;
6071                         }
6072                         param->fp_obd_uuid = (struct obd_uuid *)optarg;
6073                         break;
6074                 case 'p':
6075                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6076                                 param->fp_verbose |= VERBOSE_POOL;
6077                                 param->fp_max_depth = 0;
6078                         }
6079                         break;
6080                 case 'q':
6081                         param->fp_quiet++;
6082                         break;
6083                 case 'r':
6084                         param->fp_recursive = 1;
6085                         break;
6086                 case 'R':
6087                         param->fp_raw = 1;
6088                         break;
6089                 case 'S':
6090                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6091                                 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6092                                 param->fp_max_depth = 0;
6093                         }
6094                         break;
6095                 case 'v':
6096                         param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6097                         break;
6098                 case 'y':
6099                         param->fp_yaml = 1;
6100                         break;
6101                 case 'z':
6102                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6103                                 param->fp_verbose |= VERBOSE_EXT_SIZE;
6104                                 param->fp_max_depth = 0;
6105                         }
6106                         break;
6107                 default:
6108                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6109                                 progname, argv[optind - 1]);
6110                 case 'h':
6111                         return CMD_HELP;
6112                 }
6113         }
6114
6115         if (pathstart == -1) {
6116                 fprintf(stderr, "error: %s: no filename|pathname\n",
6117                                 argv[0]);
6118                 return CMD_HELP;
6119         } else if (pathend == -1) {
6120                 /* no options */
6121                 pathend = argc;
6122         }
6123
6124         if (pathend > argc)
6125                 return CMD_HELP;
6126
6127         if (param->fp_recursive)
6128                 param->fp_max_depth = -1;
6129         else if (param->fp_verbose & VERBOSE_DETAIL)
6130                 param->fp_max_depth = 1;
6131
6132         if (!param->fp_verbose)
6133                 param->fp_verbose = VERBOSE_DEFAULT;
6134         if (param->fp_quiet)
6135                 param->fp_verbose = VERBOSE_OBJID;
6136
6137         do {
6138                 rc = llapi_getstripe(argv[pathstart], param);
6139         } while (++pathstart < pathend && !rc);
6140
6141         if (rc)
6142                 fprintf(stderr, "error: %s failed for %s.\n",
6143                         argv[0], argv[optind - 1]);
6144         return rc;
6145 }
6146
6147 static int lfs_tgts(int argc, char **argv)
6148 {
6149         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6150         struct find_param param;
6151         int index = 0, rc = 0;
6152
6153         if (argc > 2)
6154                 return CMD_HELP;
6155
6156         if (argc == 2 && !realpath(argv[1], path)) {
6157                 rc = -errno;
6158                 fprintf(stderr, "error: invalid path '%s': %s\n",
6159                         argv[1], strerror(-rc));
6160                 return rc;
6161         }
6162
6163         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6164                 /* Check if we have a mount point */
6165                 if (mntdir[0] == '\0')
6166                         continue;
6167
6168                 memset(&param, 0, sizeof(param));
6169                 if (!strcmp(argv[0], "mdts"))
6170                         param.fp_get_lmv = 1;
6171
6172                 rc = llapi_ostlist(mntdir, &param);
6173                 if (rc) {
6174                         fprintf(stderr, "error: %s: failed on %s\n",
6175                                 argv[0], mntdir);
6176                 }
6177                 if (path[0] != '\0')
6178                         break;
6179                 memset(mntdir, 0, PATH_MAX);
6180         }
6181
6182         return rc;
6183 }
6184
6185 static int lfs_getstripe(int argc, char **argv)
6186 {
6187         struct find_param param = { 0 };
6188
6189         param.fp_max_depth = 1;
6190         return lfs_getstripe_internal(argc, argv, &param);
6191 }
6192
6193 /* functions */
6194 static int lfs_getdirstripe(int argc, char **argv)
6195 {
6196         struct find_param param = { 0 };
6197         struct option long_opts[] = {
6198         { .val = 'c',   .name = "mdt-count",     .has_arg = no_argument },
6199         { .val = 'D',   .name = "default",       .has_arg = no_argument },
6200         { .val = 'h',   .name = "help",         .has_arg = no_argument },
6201         { .val = 'H',   .name = "mdt-hash",      .has_arg = no_argument },
6202         { .val = 'i',   .name = "mdt-index",     .has_arg = no_argument },
6203         { .val = 'm',   .name = "mdt-index",     .has_arg = no_argument },
6204         { .val = 'O',   .name = "obd",           .has_arg = required_argument },
6205         { .val = 'r',   .name = "recursive",     .has_arg = no_argument },
6206         { .val = 'T',   .name = "mdt-count",     .has_arg = no_argument },
6207         { .val = 'v',   .name = "verbose",       .has_arg = no_argument },
6208         { .val = 'X',   .name = "max-inherit",   .has_arg = no_argument },
6209         { .val = 'y',   .name = "yaml",          .has_arg = no_argument },
6210         { .val = LFS_INHERIT_RR_OPT,
6211                         .name = "max-inherit-rr", .has_arg = no_argument },
6212         { .name = NULL } };
6213         int c, rc;
6214
6215         param.fp_get_lmv = 1;
6216
6217         while ((c = getopt_long(argc, argv,
6218                                 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6219                 switch (c) {
6220                 case 'c':
6221                 case 'T':
6222                         param.fp_verbose |= VERBOSE_COUNT;
6223                         break;
6224                 case 'D':
6225                         param.fp_get_default_lmv = 1;
6226                         break;
6227 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6228                 case 't':
6229                         fprintf(stderr,
6230                                 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6231                         fallthrough;
6232 #endif
6233                 case 'H':
6234                         param.fp_verbose |= VERBOSE_HASH_TYPE;
6235                         break;
6236                 case 'i':
6237                         fallthrough;
6238                 case 'm':
6239                         param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6240                         break;
6241                 case 'O':
6242                         if (param.fp_obd_uuid) {
6243                                 fprintf(stderr,
6244                                         "%s: only one obduuid allowed",
6245                                         progname);
6246                                 return CMD_HELP;
6247                         }
6248                         param.fp_obd_uuid = (struct obd_uuid *)optarg;
6249                         break;
6250                 case 'r':
6251                         param.fp_recursive = 1;
6252                         break;
6253                 case 'v':
6254                         param.fp_verbose |= VERBOSE_DETAIL;
6255                         break;
6256                 case 'X':
6257                         param.fp_verbose |= VERBOSE_INHERIT;
6258                         break;
6259                 case LFS_INHERIT_RR_OPT:
6260                         param.fp_verbose |= VERBOSE_INHERIT_RR;
6261                         break;
6262                 case 'y':
6263                         param.fp_yaml = 1;
6264                         break;
6265                 default:
6266                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6267                                 progname, argv[optind - 1]);
6268                         fallthrough;
6269                 case 'h':
6270                         return CMD_HELP;
6271                 }
6272         }
6273
6274         if (optind >= argc)
6275                 return CMD_HELP;
6276
6277         if (param.fp_recursive)
6278                 param.fp_max_depth = -1;
6279
6280         if (!param.fp_verbose)
6281                 param.fp_verbose = VERBOSE_DEFAULT;
6282
6283         do {
6284                 rc = llapi_getstripe(argv[optind], &param);
6285         } while (++optind < argc && !rc);
6286
6287         if (rc)
6288                 fprintf(stderr, "error: %s failed for %s.\n",
6289                         argv[0], argv[optind - 1]);
6290         return rc;
6291 }
6292
6293 enum mntdf_flags {
6294         MNTDF_INODES    = 0x0001,
6295         MNTDF_COOKED    = 0x0002,
6296         MNTDF_LAZY      = 0x0004,
6297         MNTDF_VERBOSE   = 0x0008,
6298         MNTDF_SHOW      = 0x0010,
6299         MNTDF_DECIMAL   = 0x0020,
6300 };
6301
6302 #define COOK(value, base)                                       \
6303 ({                                                              \
6304         int radix = 0;                                          \
6305         while (value > base) {                                  \
6306                 value /= base;                                  \
6307                 radix++;                                        \
6308         }                                                       \
6309         radix;                                                  \
6310 })
6311 #define UUF     "%-20s"
6312 #define CSF     "%11s"
6313 #define CDF     "%11llu"
6314 #define HDF     "%8.1f%c"
6315 #define RSF     "%4s"
6316 #define RDF     "%3d%%"
6317
6318 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6319 {
6320         double avail, used, ratio = 0;
6321
6322         if (inodes) {
6323                 avail = st->os_ffree;
6324                 used = st->os_files - st->os_ffree;
6325         } else {
6326                 avail = st->os_bavail;
6327                 used = st->os_blocks - st->os_bfree;
6328         }
6329         if (avail + used > 0)
6330                 ratio = used / (used + avail) * 100;
6331
6332         /* Round up to match df(1) usage percentage */
6333         return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6334 }
6335
6336 /*
6337  * This is to identify various problem states for "lfs df" if .osn_err = true,
6338  * so only show flags reflecting those states by default. Informational states
6339  * are only shown with "-v" and use lower-case names to distinguish them.
6340  * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6341  */
6342 static struct obd_statfs_state_names {
6343         enum obd_statfs_state   osn_state;
6344         const char              osn_name;
6345         bool                    osn_err;
6346 } oss_names[] = {
6347         { .osn_state = OS_STATFS_DEGRADED,   .osn_name = 'D', .osn_err = true },
6348         { .osn_state = OS_STATFS_READONLY,   .osn_name = 'R', .osn_err = true },
6349         { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6350         { .osn_state = OS_STATFS_UNUSED1,    .osn_name = '?', .osn_err = true },
6351         { .osn_state = OS_STATFS_UNUSED2,    .osn_name = '?', .osn_err = true },
6352         { .osn_state = OS_STATFS_ENOSPC,     .osn_name = 'S', .osn_err = true },
6353         { .osn_state = OS_STATFS_ENOINO,     .osn_name = 'I', .osn_err = true },
6354         { .osn_state = OS_STATFS_SUM,        .osn_name = 'a', /* aggregate */ },
6355         { .osn_state = OS_STATFS_NONROT,     .osn_name = 'f', /* flash */     },
6356 };
6357
6358 static int showdf(char *mntdir, struct obd_statfs *stat,
6359                   char *uuid, enum mntdf_flags flags,
6360                   char *type, int index, int rc)
6361 {
6362         long long avail, used, total;
6363         int ratio = 0;
6364         char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6365         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6366         char tbuf[3 * sizeof(__u64)];
6367         char ubuf[3 * sizeof(__u64)];
6368         char abuf[3 * sizeof(__u64)];
6369         char rbuf[3 * sizeof(__u64)];
6370
6371         if (!uuid || !stat)
6372                 return -EINVAL;
6373
6374         switch (rc) {
6375         case 0:
6376                 if (flags & MNTDF_INODES) {
6377                         avail = stat->os_ffree;
6378                         used = stat->os_files - stat->os_ffree;
6379                         total = stat->os_files;
6380                 } else {
6381                         int shift = flags & MNTDF_COOKED ? 0 : 10;
6382
6383                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
6384                         used  = ((stat->os_blocks - stat->os_bfree) *
6385                                  stat->os_bsize) >> shift;
6386                         total = (stat->os_blocks * stat->os_bsize) >> shift;
6387                 }
6388
6389                 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6390
6391                 if (flags & MNTDF_COOKED) {
6392                         int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6393                         double cook_val;
6394                         int i;
6395
6396                         cook_val = (double)total;
6397                         i = COOK(cook_val, base);
6398                         if (i > 0)
6399                                 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6400                                          suffix[i - 1]);
6401                         else
6402                                 snprintf(tbuf, sizeof(tbuf), CDF, total);
6403
6404                         cook_val = (double)used;
6405                         i = COOK(cook_val, base);
6406                         if (i > 0)
6407                                 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6408                                          suffix[i - 1]);
6409                         else
6410                                 snprintf(ubuf, sizeof(ubuf), CDF, used);
6411
6412                         cook_val = (double)avail;
6413                         i = COOK(cook_val, base);
6414                         if (i > 0)
6415                                 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6416                                          suffix[i - 1]);
6417                         else
6418                                 snprintf(abuf, sizeof(abuf), CDF, avail);
6419                 } else {
6420                         snprintf(tbuf, sizeof(tbuf), CDF, total);
6421                         snprintf(ubuf, sizeof(tbuf), CDF, used);
6422                         snprintf(abuf, sizeof(tbuf), CDF, avail);
6423                 }
6424
6425                 sprintf(rbuf, RDF, ratio);
6426                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6427                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6428                 if (type)
6429                         printf("[%s:%d]", type, index);
6430
6431                 if (stat->os_state) {
6432                         uint32_t i;
6433
6434                         printf(" ");
6435                         for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6436                                 if (oss_names[i].osn_state & stat->os_state &&
6437                                     (oss_names[i].osn_err ||
6438                                      flags & MNTDF_VERBOSE))
6439                                         printf("%c", oss_names[i].osn_name);
6440                         }
6441                 }
6442
6443                 printf("\n");
6444                 break;
6445         case -ENODATA:
6446                 printf(UUF": inactive device\n", uuid);
6447                 break;
6448         default:
6449                 printf(UUF": %s\n", uuid, strerror(-rc));
6450                 break;
6451         }
6452
6453         return 0;
6454 }
6455
6456 struct ll_stat_type {
6457         int   st_op;
6458         char *st_name;
6459 };
6460
6461 #define LL_STATFS_MAX   LOV_MAX_STRIPE_COUNT
6462
6463 struct ll_statfs_data {
6464         int                     sd_index;
6465         struct obd_statfs       sd_st;
6466 };
6467
6468 struct ll_statfs_buf {
6469         int                     sb_count;
6470         struct ll_statfs_data   sb_buf[LL_STATFS_MAX];
6471 };
6472
6473 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6474                  int ops, struct ll_statfs_buf *lsb)
6475 {
6476         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6477         struct obd_uuid uuid_buf;
6478         char *poolname = NULL;
6479         struct ll_stat_type types[] = {
6480                 { .st_op = LL_STATFS_LMV,       .st_name = "MDT" },
6481                 { .st_op = LL_STATFS_LOV,       .st_name = "OST" },
6482                 { .st_name = NULL } };
6483         struct ll_stat_type *tp;
6484         __u64 ost_files = 0;
6485         __u64 ost_ffree = 0;
6486         __u32 index;
6487         __u32 type;
6488         int fd;
6489         int rc = 0;
6490         int rc2;
6491
6492         if (pool) {
6493                 poolname = strchr(pool, '.');
6494                 if (poolname) {
6495                         if (strncmp(fsname, pool, strlen(fsname))) {
6496                                 fprintf(stderr, "filesystem name incorrect\n");
6497                                 return -ENODEV;
6498                         }
6499                         poolname++;
6500                 } else
6501                         poolname = pool;
6502         }
6503
6504         fd = open(mntdir, O_RDONLY);
6505         if (fd < 0) {
6506                 rc = -errno;
6507                 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6508                         strerror(errno));
6509                 return rc;
6510         }
6511
6512         if (flags & MNTDF_SHOW) {
6513                 if (flags & MNTDF_INODES)
6514                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6515                                "UUID", "Inodes", "IUsed", "IFree",
6516                                "IUse%", "Mounted on");
6517                 else
6518                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6519                                "UUID",
6520                                flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6521                                "Used", "Available", "Use%", "Mounted on");
6522         }
6523
6524         for (tp = types; tp->st_name != NULL; tp++) {
6525                 bool have_ost = false;
6526
6527                 if (!(tp->st_op & ops))
6528                         continue;
6529
6530                 for (index = 0; index < LOV_ALL_STRIPES &&
6531                      (!lsb || lsb->sb_count < LL_STATFS_MAX); index++) {
6532                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
6533                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6534                         type = flags & MNTDF_LAZY ?
6535                                 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6536                         rc2 = llapi_obd_fstatfs(fd, type, index,
6537                                                 &stat_buf, &uuid_buf);
6538                         if (rc2 == -ENODEV)
6539                                 break;
6540                         if (rc2 == -EAGAIN)
6541                                 continue;
6542                         if (rc2 == -ENODATA) { /* Inactive device, OK. */
6543                                 if (!(flags & MNTDF_VERBOSE))
6544                                         continue;
6545                         } else if (rc2 < 0 && rc == 0) {
6546                                 rc = rc2;
6547                         }
6548
6549                         /*
6550                          * If we have OSTs then don't report MDT block counts.
6551                          * For MDT-only filesystems the expectation is that all
6552                          * layouts have a DoM component.  For filesystems with
6553                          * OSTs, files are not necessarily going to store data
6554                          * on MDTs, and MDT space is limited to a fraction of
6555                          * OST space, so don't include it in the summary.
6556                          */
6557                         if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6558                                 have_ost = true;
6559                                 sum.os_blocks = 0;
6560                                 sum.os_bfree = 0;
6561                                 sum.os_bavail = 0;
6562                         }
6563
6564                         if (poolname && tp->st_op == LL_STATFS_LOV &&
6565                             llapi_search_ost(fsname, poolname,
6566                                              obd_uuid2str(&uuid_buf)) != 1)
6567                                 continue;
6568
6569                         /*
6570                          * the llapi_obd_fstatfs() call may have returned with
6571                          * an error, but if it filled in uuid_buf we will at
6572                          * lease use that to print out a message for that OBD.
6573                          * If we didn't get anything in the uuid_buf, then fill
6574                          * it in so that we can print an error message.
6575                          */
6576                         if (uuid_buf.uuid[0] == '\0')
6577                                 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6578                                          "%s%04x", tp->st_name, index);
6579                         if (!rc && lsb) {
6580                                 lsb->sb_buf[lsb->sb_count].sd_index = index;
6581                                 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6582                                 lsb->sb_count++;
6583                         }
6584                         if (flags & MNTDF_SHOW)
6585                                 showdf(mntdir, &stat_buf,
6586                                        obd_uuid2str(&uuid_buf), flags,
6587                                        tp->st_name, index, rc2);
6588
6589                         if (rc2)
6590                                 continue;
6591
6592                         if (tp->st_op == LL_STATFS_LMV) {
6593                                 sum.os_ffree += stat_buf.os_ffree;
6594                                 sum.os_files += stat_buf.os_files;
6595                         } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6596                                 ost_files += stat_buf.os_files;
6597                                 ost_ffree += stat_buf.os_ffree;
6598                         }
6599                         sum.os_blocks += stat_buf.os_blocks *
6600                                          stat_buf.os_bsize;
6601                         sum.os_bfree  += stat_buf.os_bfree *
6602                                          stat_buf.os_bsize;
6603                         sum.os_bavail += stat_buf.os_bavail *
6604                                          stat_buf.os_bsize;
6605                 }
6606         }
6607
6608         close(fd);
6609
6610         /*
6611          * If we have _some_ OSTs, but don't have as many free objects on the
6612          * OST as inodes on the MDTs, reduce the reported number of inodes
6613          * to compensate, so that the "inodes in use" number is correct.
6614          * This should be kept in sync with ll_statfs_internal().
6615          */
6616         if (ost_files && ost_ffree < sum.os_ffree) {
6617                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6618                 sum.os_ffree = ost_ffree;
6619         }
6620         if (flags & MNTDF_SHOW) {
6621                 printf("\n");
6622                 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6623                 printf("\n");
6624         }
6625
6626         return rc;
6627 }
6628
6629 enum {
6630         LAYOUT_INHERIT_UNSET    = -2,
6631 };
6632
6633 /* functions */
6634 static int lfs_setdirstripe(int argc, char **argv)
6635 {
6636         char *dname;
6637         struct lfs_setstripe_args lsa = { 0 };
6638         struct llapi_stripe_param *param = NULL;
6639         __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6640         char *end;
6641         int c;
6642         char *mode_opt = NULL;
6643         bool default_stripe = false;
6644         bool delete = false;
6645         bool foreign_mode = false;
6646         bool mdt_count_set = false;
6647         mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6648         mode_t previous_mode = 0;
6649         char *xattr = NULL;
6650         __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6651         int max_inherit = LAYOUT_INHERIT_UNSET;
6652         int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6653         struct option long_opts[] = {
6654         { .val = 'c',   .name = "count",        .has_arg = required_argument },
6655         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument },
6656         { .val = 'd',   .name = "delete",       .has_arg = no_argument },
6657         { .val = 'D',   .name = "default",      .has_arg = no_argument },
6658         { .val = 'D',   .name = "default_stripe", .has_arg = no_argument },
6659         { .val = LFS_LAYOUT_FLAGS_OPT,
6660                         .name = "flags",        .has_arg = required_argument },
6661         { .val = LFS_LAYOUT_FOREIGN_OPT,
6662                         .name = "foreign",      .has_arg = optional_argument},
6663         { .val = 'h',   .name = "help",         .has_arg = no_argument },
6664         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
6665 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6666         { .val = 'i',   .name = "mdt-index",    .has_arg = required_argument },
6667         { .val = 'i',   .name = "mdt",          .has_arg = required_argument },
6668 #else
6669 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
6670         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
6671         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
6672 #endif
6673 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6674         { .val = 'i',   .name = "index",        .has_arg = required_argument },
6675 #endif
6676         { .val = 'o',   .name = "mode",         .has_arg = required_argument },
6677 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6678         { .val = 't',   .name = "hash-type",    .has_arg = required_argument },
6679 #endif
6680         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
6681         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
6682         { .val = 'X',   .name = "max-inherit",  .has_arg = required_argument },
6683         { .val = LFS_INHERIT_RR_OPT,
6684                         .name = "max-inherit-rr", .has_arg = required_argument},
6685 /* setstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
6686         { .name = NULL } };
6687         int result = 0;
6688
6689         setstripe_args_init(&lsa);
6690
6691         while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6692                                 long_opts, NULL)) >= 0) {
6693                 switch (c) {
6694                 case 0:
6695                         /* Long options. */
6696                         break;
6697                 case 'c':
6698                 case 'T':
6699                         errno = 0;
6700                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6701                         if (errno != 0 || *end != '\0' ||
6702                             lsa.lsa_stripe_count < -1 ||
6703                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6704                                 fprintf(stderr,
6705                                         "%s: invalid stripe count '%s'\n",
6706                                         progname, optarg);
6707                                 return CMD_HELP;
6708                         }
6709                         mdt_count_set = true;
6710                         break;
6711                 case 'd':
6712                         delete = true;
6713                         default_stripe = true;
6714                         break;
6715                 case 'D':
6716                         default_stripe = true;
6717                         break;
6718                 case LFS_LAYOUT_FOREIGN_OPT:
6719                         if (optarg) {
6720                                 /* check pure numeric */
6721                                 type = strtoul(optarg, &end, 0);
6722                                 if (*end) {
6723                                         /* check name */
6724                                         type = check_foreign_type_name(optarg);
6725                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6726                                                 fprintf(stderr,
6727                                                         "%s %s: unknown foreign type '%s'\n",
6728                                                         progname, argv[0],
6729                                                         optarg);
6730                                                 return CMD_HELP;
6731                                         }
6732                                 } else if (type >= UINT32_MAX) {
6733                                         fprintf(stderr,
6734                                                 "%s %s: invalid foreign type '%s'\n",
6735                                                 progname, argv[0], optarg);
6736                                         return CMD_HELP;
6737                                 }
6738                         }
6739                         foreign_mode = true;
6740                         break;
6741                 case LFS_LAYOUT_FLAGS_OPT:
6742                         errno = 0;
6743                         flags = strtoul(optarg, &end, 16);
6744                         if (errno != 0 || *end != '\0' ||
6745                             flags >= UINT32_MAX) {
6746                                 fprintf(stderr,
6747                                         "%s %s: invalid hex flags '%s'\n",
6748                                         progname, argv[0], optarg);
6749                                 return CMD_HELP;
6750                         }
6751                         if (!foreign_mode) {
6752                                 fprintf(stderr,
6753                                         "%s %s: hex flags must be specified with --foreign option\n",
6754                                         progname, argv[0]);
6755                                 return CMD_HELP;
6756                         }
6757                         break;
6758 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6759                 case 't':
6760                         fprintf(stderr,
6761                                 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6762                         fallthrough;
6763 #endif
6764                 case 'H':
6765                         lsa.lsa_pattern = check_hashtype(optarg);
6766                         if (lsa.lsa_pattern == 0) {
6767                                 fprintf(stderr,
6768                                         "%s %s: bad directory hash type '%s'\n",
6769                                         progname, argv[0], optarg);
6770                                 return CMD_HELP;
6771                         }
6772                         break;
6773                 case 'i':
6774 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6775                 case 'm':
6776 #endif
6777 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6778                         if (strcmp(argv[optind - 1], "--index") == 0)
6779                                 fprintf(stderr,
6780                                         "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6781                                         progname, argv[0]);
6782 #endif
6783                         lsa.lsa_nr_tgts = parse_targets(mdts,
6784                                                 sizeof(mdts) / sizeof(__u32),
6785                                                 lsa.lsa_nr_tgts, optarg, NULL);
6786                         if (lsa.lsa_nr_tgts < 0) {
6787                                 fprintf(stderr,
6788                                         "%s %s: invalid MDT target(s) '%s'\n",
6789                                         progname, argv[0], optarg);
6790                                 return CMD_HELP;
6791                         }
6792
6793                         lsa.lsa_tgts = mdts;
6794                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6795                                 lsa.lsa_stripe_off = mdts[0];
6796                         break;
6797                 case 'o':
6798                         mode_opt = optarg;
6799                         break;
6800                 case 'x':
6801                         xattr = optarg;
6802                         break;
6803                 case 'X':
6804                         errno = 0;
6805                         max_inherit = strtol(optarg, &end, 10);
6806                         if (errno != 0 || *end != '\0' || max_inherit < -2) {
6807                                 fprintf(stderr,
6808                                         "%s %s: invalid max-inherit '%s'\n",
6809                                         progname, argv[0], optarg);
6810                                 return CMD_HELP;
6811                         }
6812                         if (max_inherit == 0) {
6813                                 max_inherit = LMV_INHERIT_NONE;
6814                         } else if (max_inherit == -1) {
6815                                 max_inherit = LMV_INHERIT_UNLIMITED;
6816                         } else if (max_inherit > LMV_INHERIT_MAX) {
6817                                 fprintf(stderr,
6818                                         "%s %s: max-inherit %d exceeds maximum %u\n",
6819                                         progname, argv[0], max_inherit,
6820                                         LMV_INHERIT_MAX);
6821                                 return CMD_HELP;
6822                         }
6823                         break;
6824                 case LFS_INHERIT_RR_OPT:
6825                         if (!default_stripe) {
6826                                 fprintf(stderr,
6827                                         "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6828                                         progname, argv[0]);
6829                                 return CMD_HELP;
6830                         }
6831                         errno = 0;
6832                         max_inherit_rr = strtol(optarg, &end, 10);
6833                         if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6834                                 fprintf(stderr,
6835                                         "%s %s: invalid max-inherit-rr '%s'\n",
6836                                         progname, argv[0], optarg);
6837                                 return CMD_HELP;
6838                         }
6839                         if (max_inherit_rr == 0) {
6840                                 max_inherit_rr = LMV_INHERIT_RR_NONE;
6841                         } else if (max_inherit_rr == -1) {
6842                                 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6843                         } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6844                                 fprintf(stderr,
6845                                         "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6846                                         progname, argv[0], max_inherit_rr,
6847                                         LMV_INHERIT_RR_MAX);
6848                                 return CMD_HELP;
6849                         }
6850                         break;
6851                 default:
6852                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6853                                 progname, argv[optind - 1]);
6854                         fallthrough;
6855                 case 'h':
6856                         return CMD_HELP;
6857                 }
6858         }
6859
6860         if (optind == argc) {
6861                 fprintf(stderr, "%s %s: DIR must be specified\n",
6862                         progname, argv[0]);
6863                 return CMD_HELP;
6864         }
6865
6866         if (xattr && !foreign_mode) {
6867                 /*
6868                  * only print a warning as this is armless and will be
6869                  * ignored
6870                  */
6871                 fprintf(stderr,
6872                         "%s %s: xattr has been specified for non-foreign layout\n",
6873                         progname, argv[0]);
6874         } else if (foreign_mode && !xattr) {
6875                 fprintf(stderr,
6876                         "%s %s: xattr must be provided in foreign mode\n",
6877                         progname, argv[0]);
6878                 return CMD_HELP;
6879         }
6880
6881         if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6882             lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6883                 fprintf(stderr,
6884                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6885                         progname, argv[0]);
6886                 return CMD_HELP;
6887         }
6888
6889         if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6890             lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6891                 /* if no parameters set, create directory on least-used MDTs */
6892                 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
6893                 lsa.lsa_stripe_count = 1;
6894         }
6895
6896         if (delete &&
6897             (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6898              lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6899                 fprintf(stderr,
6900                         "%s %s: cannot specify -d with -c or -i options\n",
6901                         progname, argv[0]);
6902                 return CMD_HELP;
6903         }
6904
6905         if (mode_opt) {
6906                 mode = strtoul(mode_opt, &end, 8);
6907                 if (*end != '\0') {
6908                         fprintf(stderr,
6909                                 "%s %s: bad MODE '%s'\n",
6910                                 progname, argv[0], mode_opt);
6911                         return CMD_HELP;
6912                 }
6913                 previous_mode = umask(0);
6914         }
6915
6916         /* check max-inherit and warn user in some cases */
6917         if (default_stripe &&
6918             (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
6919                 if (max_inherit == LMV_INHERIT_UNLIMITED)
6920                         fprintf(stderr,
6921                         "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
6922                         progname, argv[0], lsa.lsa_stripe_count);
6923                 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
6924                          max_inherit != LMV_INHERIT_NONE)
6925                         fprintf(stderr,
6926                                 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
6927                                 progname, argv[0], max_inherit,
6928                                 lsa.lsa_stripe_count);
6929         }
6930
6931         if (default_stripe && lsa.lsa_nr_tgts > 1 && !mdt_count_set) {
6932                 fprintf(stderr,
6933                         "%s %s: trying to create unrecommended default striped directory layout,\n"
6934                         "       '-D -i x,y,z' will stripe every new directory across all MDTs,\n"
6935                         "       add -c with the number of MDTs to do this anyway\n",
6936                         progname, argv[0]);
6937                 return CMD_HELP;
6938         }
6939
6940         if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6941             lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6942             lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
6943                 fprintf(stderr,
6944                         "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
6945                         progname, argv[0], lsa.lsa_stripe_off);
6946                 return CMD_HELP;
6947         }
6948
6949         /* foreign LMV/dir case */
6950         if (foreign_mode) {
6951                 if (argc > optind + 1) {
6952                         fprintf(stderr,
6953                                 "%s %s: cannot specify multiple foreign dirs\n",
6954                                 progname, argv[0]);
6955                         return CMD_HELP;
6956                 }
6957
6958                 dname = argv[optind];
6959                 result = llapi_dir_create_foreign(dname, mode, type, flags,
6960                                                   xattr);
6961                 if (result != 0)
6962                         fprintf(stderr,
6963                                 "%s mkdir: can't create foreign dir '%s': %s\n",
6964                                 progname, dname, strerror(-result));
6965                 return result;
6966         }
6967
6968         /*
6969          * initialize stripe parameters, in case param is converted to specific,
6970          * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6971          */
6972         param = calloc(1, offsetof(typeof(*param),
6973                        lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6974                                 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6975         if (!param) {
6976                 fprintf(stderr,
6977                         "%s %s: cannot allocate memory for parameters: %s\n",
6978                         progname, argv[0], strerror(ENOMEM));
6979                 return CMD_HELP;
6980         }
6981
6982         /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6983         if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
6984             (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6985              lsa.lsa_stripe_count == 0))
6986                 lsa.lsa_stripe_count = 1;
6987         if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6988                 param->lsp_stripe_count = lsa.lsa_stripe_count;
6989         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6990                 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
6991         else
6992                 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6993         if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6994                 param->lsp_stripe_pattern = lsa.lsa_pattern;
6995         else
6996                 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6997         param->lsp_pool = lsa.lsa_pool_name;
6998         param->lsp_is_specific = false;
6999         if (max_inherit == LAYOUT_INHERIT_UNSET) {
7000                 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1)
7001                         max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
7002                 else
7003                         max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
7004         }
7005         param->lsp_max_inherit = max_inherit;
7006         if (default_stripe) {
7007
7008                 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
7009                         max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
7010                 param->lsp_max_inherit_rr = max_inherit_rr;
7011         }
7012         if (strcmp(argv[0], "mkdir") == 0)
7013                 param->lsp_is_create = true;
7014         if (lsa.lsa_nr_tgts > 1) {
7015                 if (lsa.lsa_stripe_count > 0 &&
7016                     lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7017                     lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7018                         fprintf(stderr,
7019                                 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7020                                 argv[0], lsa.lsa_stripe_count,
7021                                 lsa.lsa_nr_tgts);
7022                         free(param);
7023                         return CMD_HELP;
7024                 }
7025
7026                 param->lsp_is_specific = true;
7027                 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7028                 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7029         }
7030
7031         dname = argv[optind];
7032         do {
7033                 if (default_stripe) {
7034                         result = llapi_dir_set_default_lmv(dname, param);
7035                         if (result)
7036                                 fprintf(stderr,
7037                                         "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7038                                         progname, dname, strerror(-result));
7039                         continue;
7040                 }
7041
7042                 result = llapi_dir_create(dname, mode, param);
7043                 if (result)
7044                         fprintf(stderr,
7045                                 "%s setdirstripe: cannot create dir '%s': %s\n",
7046                                 progname, dname, strerror(-result));
7047         } while (!result && (dname = argv[++optind]));
7048
7049         if (mode_opt)
7050                 umask(previous_mode);
7051
7052         free(param);
7053         return result;
7054 }
7055
7056 /* functions */
7057 static int lfs_rmentry(int argc, char **argv)
7058 {
7059         char *dname;
7060         int   index;
7061         int   result = 0;
7062
7063         if (argc <= 1) {
7064                 fprintf(stderr, "error: %s: missing dirname\n",
7065                         argv[0]);
7066                 return CMD_HELP;
7067         }
7068
7069         index = 1;
7070         dname = argv[index];
7071         while (dname) {
7072                 result = llapi_direntry_remove(dname);
7073                 if (result) {
7074                         fprintf(stderr,
7075                                 "error: %s: remove dir entry '%s' failed\n",
7076                                 argv[0], dname);
7077                         break;
7078                 }
7079                 dname = argv[++index];
7080         }
7081         return result;
7082 }
7083
7084 static int lfs_unlink_foreign(int argc, char **argv)
7085 {
7086         char *name;
7087         int   index;
7088         int   result = 0;
7089
7090         if (argc <= 1) {
7091                 fprintf(stderr, "error: %s: missing pathname\n",
7092                         argv[0]);
7093                 return CMD_HELP;
7094         }
7095
7096         index = 1;
7097         name = argv[index];
7098         while (name != NULL) {
7099                 result = llapi_unlink_foreign(name);
7100                 if (result) {
7101                         fprintf(stderr,
7102                                 "error: %s: unlink foreign entry '%s' failed\n",
7103                                 argv[0], name);
7104                         break;
7105                 }
7106                 name = argv[++index];
7107         }
7108         return result;
7109 }
7110
7111 static int lfs_mv(int argc, char **argv)
7112 {
7113         struct lmv_user_md lmu = { LMV_USER_MAGIC };
7114         struct find_param param = {
7115                 .fp_max_depth = -1,
7116                 .fp_mdt_index = -1,
7117         };
7118         char *end;
7119         int c;
7120         int rc = 0;
7121         struct option long_opts[] = {
7122         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
7123         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
7124         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7125         { .name = NULL } };
7126
7127         while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7128                 switch (c) {
7129 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7130                 case 'M':
7131                         fprintf(stderr,
7132                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7133 #endif
7134                 case 'm':
7135                         errno = 0;
7136                         lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7137                         if (errno != 0 || *end != '\0' ||
7138                             lmu.lum_stripe_offset >= UINT32_MAX) {
7139                                 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7140                                         progname, optarg);
7141                                 return CMD_HELP;
7142                         }
7143                         break;
7144                 case 'v':
7145                         param.fp_verbose = VERBOSE_DETAIL;
7146                         break;
7147                 default:
7148                         fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7149                                 progname, argv[optind - 1]);
7150                         return CMD_HELP;
7151                 }
7152         }
7153
7154         if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7155                 fprintf(stderr, "%s mv: MDT index must be specified\n",
7156                         progname);
7157                 return CMD_HELP;
7158         }
7159
7160         if (optind >= argc) {
7161                 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7162                 return CMD_HELP;
7163         }
7164
7165         lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7166
7167         /* initialize migrate mdt parameters */
7168         param.fp_lmv_md = &lmu;
7169         param.fp_migrate = 1;
7170         rc = llapi_migrate_mdt(argv[optind], &param);
7171         if (rc != 0)
7172                 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7173                         progname, argv[optind], lmu.lum_stripe_offset,
7174                         strerror(-rc));
7175         return rc;
7176 }
7177
7178 static int lfs_osts(int argc, char **argv)
7179 {
7180         return lfs_tgts(argc, argv);
7181 }
7182
7183 static int lfs_mdts(int argc, char **argv)
7184 {
7185         return lfs_tgts(argc, argv);
7186 }
7187
7188 static int lfs_df(int argc, char **argv)
7189 {
7190         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7191         enum mntdf_flags flags = MNTDF_SHOW;
7192         int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7193         int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7194         char fsname[PATH_MAX] = "", *pool_name = NULL;
7195         struct option long_opts[] = {
7196         { .val = 'h',   .name = "human-readable", .has_arg = no_argument },
7197         { .val = 'H',   .name = "si",           .has_arg = no_argument },
7198         { .val = 'i',   .name = "inodes",       .has_arg = no_argument },
7199         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
7200         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
7201         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7202         { .name = NULL} };
7203
7204         while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7205                 switch (c) {
7206                 case 'h':
7207                         flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7208                         break;
7209                 case 'H':
7210                         flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7211                         break;
7212                 case 'i':
7213                         flags |= MNTDF_INODES;
7214                         break;
7215                 case 'l':
7216                         flags |= MNTDF_LAZY;
7217                         break;
7218                 case 'p':
7219                         pool_name = optarg;
7220                         break;
7221                 case 'v':
7222                         flags |= MNTDF_VERBOSE;
7223                         break;
7224                 default:
7225                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7226                                 progname, argv[optind - 1]);
7227                         return CMD_HELP;
7228                 }
7229         }
7230
7231         /* Handle case where path is not specified */
7232         if (optind == argc) {
7233                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7234                         /* Check if we have a mount point */
7235                         if (mntdir[0] == '\0')
7236                                 continue;
7237
7238                         rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7239                         if (rc || path[0] != '\0')
7240                                 break;
7241
7242                         fsname[0] = '\0'; /* avoid matching in next loop */
7243                         mntdir[0] = '\0'; /* avoid matching in next loop */
7244                         path[0] = '\0'; /* clean for next loop */
7245                 }
7246                 return rc;
7247         }
7248
7249         /* Loop through all the remaining arguments. These are Lustre FS
7250          * paths.
7251          */
7252         for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7253                 bool valid = false;
7254
7255                 fsname[0] = '\0'; /* start clean */
7256                 mntdir[0] = '\0'; /* start clean */
7257                 path[0] = '\0';   /* start clean */
7258
7259                 /* path does not exists at all */
7260                 if (!realpath(argv[arg_idx], path)) {
7261                         rc = -errno;
7262                         fprintf(stderr, "error: invalid path '%s': %s\n",
7263                                 argv[arg_idx], strerror(-rc));
7264                         /* save first seen error */
7265                         if (!rc1)
7266                                 rc1 = rc;
7267
7268                         continue;
7269                 }
7270
7271                 /* path exists but may not be a Lustre filesystem */
7272                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7273                         /* Check if we have a mount point */
7274                         if (mntdir[0] == '\0')
7275                                 continue;
7276
7277                         rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7278                         if (rc || path[0] != '\0') {
7279                                 valid = true;
7280
7281                                 /* save first seen error */
7282                                 if (!rc1)
7283                                         rc1 = rc;
7284                                 break;
7285                         }
7286                 }
7287
7288                 if (!valid) {
7289                         llapi_printf(LLAPI_MSG_ERROR,
7290                                      "%s:%s Not a Lustre filesystem\n",
7291                                      argv[0], argv[arg_idx]);
7292                         /* save first seen error */
7293                         if (!rc1)
7294                                 rc1 = -EOPNOTSUPP;
7295                 }
7296         }
7297
7298         return rc1;
7299 }
7300
7301 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7302                           bool opt_instance, bool opt_fsname, bool opt_mntdir)
7303 {
7304         int rc = 0;
7305
7306         if (opt_fsname == opt_instance) { /* both true or both false */
7307                 rc = llapi_getname(mntdir, buf, buflen);
7308         } else if (opt_fsname) {
7309                 /*
7310                  * llapi_search_mounts() fills @buf with fsname, but that is not
7311                  * called if explicit paths are specified on the command-line
7312                  */
7313                 if (buf[0] == '\0')
7314                         rc = llapi_get_fsname(mntdir, buf, buflen);
7315         } else /* if (opt_instance) */ {
7316                 rc = llapi_get_instance(mntdir, buf, buflen);
7317         }
7318
7319         if (rc < 0) {
7320                 fprintf(stderr, "cannot get instance for '%s': %s\n",
7321                         mntdir, strerror(-rc));
7322                 return rc;
7323         }
7324
7325         if (opt_mntdir)
7326                 printf("%s %s\n", buf, mntdir);
7327         else
7328                 printf("%s\n", buf);
7329
7330         return 0;
7331 }
7332
7333 static int lfs_getname(int argc, char **argv)
7334 {
7335         struct option long_opts[] = {
7336         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7337         { .val = 'i',   .name = "instance",     .has_arg = no_argument },
7338         { .val = 'n',   .name = "fsname",       .has_arg = no_argument },
7339         { .name = NULL} };
7340         bool opt_instance = false, opt_fsname = false;
7341         char fsname[PATH_MAX] = "";
7342         int rc = 0, rc2, c;
7343
7344         while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7345                 switch (c) {
7346                 case 'i':
7347                         opt_instance = true;
7348                         break;
7349                 case 'n':
7350                         opt_fsname = true;
7351                         break;
7352                 default:
7353                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7354                                 progname, argv[optind - 1]);
7355                         fallthrough;
7356                 case 'h':
7357                         return CMD_HELP;
7358                 }
7359         }
7360
7361         if (optind == argc) { /* no paths specified, get all paths. */
7362                 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7363                 int index = 0;
7364
7365                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7366                         rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7367                                              opt_instance, opt_fsname, true);
7368                         if (!rc)
7369                                 rc = rc2;
7370                         path[0] = fsname[0] = mntdir[0] = '\0';
7371                 }
7372         } else { /* paths specified, only attempt to search these. */
7373                 bool opt_mntdir;
7374
7375                 /* if only one path is given, print only requested info */
7376                 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7377
7378                 for (; optind < argc; optind++) {
7379                         rc2 = print_instance(argv[optind], fsname,
7380                                              sizeof(fsname), opt_instance,
7381                                              opt_fsname, opt_mntdir);
7382                         if (!rc)
7383                                 rc = rc2;
7384                         fsname[0] = '\0';
7385                 }
7386         }
7387
7388         return rc;
7389 }
7390
7391 static int lfs_check(int argc, char **argv)
7392 {
7393         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7394         int num_types = 1;
7395         char *obd_types[3];
7396         char obd_type1[4];
7397         char obd_type2[4];
7398         char obd_type3[4];
7399         int rc;
7400
7401         if (argc < 2 || argc > 3) {
7402                 fprintf(stderr, "%s check: server type must be specified\n",
7403                         progname);
7404                 return CMD_HELP;
7405         }
7406
7407         obd_types[0] = obd_type1;
7408         obd_types[1] = obd_type2;
7409         obd_types[2] = obd_type3;
7410
7411         if (strcmp(argv[1], "osts") == 0) {
7412                 strcpy(obd_types[0], "osc");
7413         } else if (strcmp(argv[1], "mdts") == 0 ||
7414                    strcmp(argv[1], "mds") == 0) {
7415                 strcpy(obd_types[0], "mdc");
7416         } else if (strcmp(argv[1], "mgts") == 0) {
7417                 strcpy(obd_types[0], "mgc");
7418         } else if (strcmp(argv[1], "all") == 0 ||
7419                    strcmp(argv[1], "servers") == 0) {
7420                 num_types = 3;
7421                 strcpy(obd_types[0], "osc");
7422                 strcpy(obd_types[1], "mdc");
7423                 strcpy(obd_types[2], "mgc");
7424         } else {
7425                 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7426                         progname, argv[1]);
7427                 return CMD_HELP;
7428         }
7429
7430         if (argc >= 3 && !realpath(argv[2], path)) {
7431                 rc = -errno;
7432                 fprintf(stderr, "error: invalid path '%s': %s\n",
7433                         argv[2], strerror(-rc));
7434                 return rc;
7435         }
7436
7437         rc = llapi_search_mounts(path, 0, mntdir, NULL);
7438         if (rc < 0 || mntdir[0] == '\0') {
7439                 fprintf(stderr,
7440                         "%s check: cannot find mounted Lustre filesystem: %s\n",
7441                         progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7442                 return rc;
7443         }
7444
7445         rc = llapi_target_check(num_types, obd_types, path);
7446         if (rc)
7447                 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7448                         progname, argv[1], strerror(-rc));
7449
7450         return rc;
7451 }
7452
7453 #ifdef HAVE_SYS_QUOTA_H
7454 #define ADD_OVERFLOW(a, b) \
7455                      ((((a) + (b)) < (a)) ? \
7456                       ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7457
7458 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7459  * returns the value or ULONG_MAX on integer overflow or incorrect format
7460  * Notes:
7461  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7462  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7463  *        3. empty integer value is interpreted as 0
7464  */
7465 static unsigned long str2sec(const char *timestr)
7466 {
7467         const char spec[] = "smhdw";
7468         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7469         unsigned long val = 0;
7470         char *tail;
7471
7472         if (strpbrk(timestr, spec) == NULL) {
7473                 /*
7474                  * no specifiers inside the time string,
7475                  * should treat it as an integer value
7476                  */
7477                 val = strtoul(timestr, &tail, 10);
7478                 return *tail ? ULONG_MAX : val;
7479         }
7480
7481         /* format string is XXwXXdXXhXXmXXs */
7482         while (*timestr) {
7483                 unsigned long v;
7484                 int ind;
7485                 char *ptr;
7486
7487                 v = strtoul(timestr, &tail, 10);
7488                 if (v == ULONG_MAX || *tail == '\0')
7489                         /*
7490                          * value too large (ULONG_MAX or more)
7491                          * or missing specifier
7492                          */
7493                         goto error;
7494
7495                 ptr = strchr(spec, *tail);
7496                 if (!ptr)
7497                         /* unknown specifier */
7498                         goto error;
7499
7500                 ind = ptr - spec;
7501
7502                 /* check if product will overflow the type */
7503                 if (!(v < ULONG_MAX / mult[ind]))
7504                         goto error;
7505
7506                 ADD_OVERFLOW(val, mult[ind] * v);
7507                 if (val == ULONG_MAX)
7508                         goto error;
7509
7510                 timestr = tail + 1;
7511         }
7512
7513         return val;
7514
7515 error:
7516         return ULONG_MAX;
7517 }
7518
7519 #define ARG2ULL(nr, str, def_units)                                     \
7520 do {                                                                    \
7521         unsigned long long limit, units = def_units;                    \
7522         int rc;                                                         \
7523                                                                         \
7524         rc = llapi_parse_size(str, &limit, &units, 1);                  \
7525         if (rc < 0) {                                                   \
7526                 fprintf(stderr, "%s: invalid limit '%s'\n",             \
7527                         progname, str);                                 \
7528                 return CMD_HELP;                                        \
7529         }                                                               \
7530         nr = limit;                                                     \
7531 } while (0)
7532
7533 static inline int has_times_option(int argc, char **argv)
7534 {
7535         int i;
7536
7537         for (i = 1; i < argc; i++)
7538                 if (!strcmp(argv[i], "-t"))
7539                         return 1;
7540
7541         return 0;
7542 }
7543
7544 static inline int lfs_verify_poolarg(char *pool)
7545 {
7546         if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7547                 fprintf(stderr,
7548                         "Pool name '%.*s' is longer than %d\n",
7549                         LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7550                 return 1;
7551         }
7552         return 0;
7553 }
7554
7555 /* special grace time, only notify the user when its quota is over soft limit
7556  * but doesn't block new writes until the hard limit is reached.
7557  */
7558 #define NOTIFY_GRACE            "notify"
7559 #define NOTIFY_GRACE_TIME       LQUOTA_GRACE_MASK
7560
7561 #ifndef toqb
7562 static inline __u64 lustre_stoqb(size_t space)
7563 {
7564         return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7565 }
7566 #else
7567 #define lustre_stoqb   toqb
7568 #endif
7569
7570 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7571 {
7572         int c, rc;
7573         char *mnt, *obd_type = (char *)qctl->obd_type;
7574         struct obd_dqblk *dqb = &qctl->qc_dqblk;
7575         struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7576         struct option long_opts[] = {
7577         { .val = 'b',   .name = "block-grace",  .has_arg = required_argument },
7578         { .val = 'g',   .name = "group",        .has_arg = no_argument },
7579         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7580         { .val = 'i',   .name = "inode-grace",  .has_arg = required_argument },
7581         { .val = 'p',   .name = "projid",       .has_arg = no_argument },
7582         { .val = 't',   .name = "times",        .has_arg = no_argument },
7583         { .val = 'u',   .name = "user",         .has_arg = no_argument },
7584         { .val = LFS_POOL_OPT,
7585                         .name = "pool",         .has_arg = required_argument },
7586         { .name = NULL } };
7587         int qtype;
7588
7589         qctl->qc_cmd  = LUSTRE_Q_SETINFO;
7590         qctl->qc_type = ALLQUOTA;
7591
7592         while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7593                                 long_opts, NULL)) != -1) {
7594                 switch (c) {
7595                 case 'u':
7596                         qtype = USRQUOTA;
7597                         goto quota_type;
7598                 case 'g':
7599                         qtype = GRPQUOTA;
7600                         goto quota_type;
7601                 case 'p':
7602                         qtype = PRJQUOTA;
7603 quota_type:
7604                         if (qctl->qc_type != ALLQUOTA) {
7605                                 fprintf(stderr,
7606                                         "%s: -u/g/p cannot be used more than once\n",
7607                                         progname);
7608                                 return CMD_HELP;
7609                         }
7610                         qctl->qc_type = qtype;
7611                         break;
7612                 case 'b':
7613                         if (strncmp(optarg, NOTIFY_GRACE,
7614                                     strlen(NOTIFY_GRACE)) == 0) {
7615                                 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7616                         } else {
7617                                 dqi->dqi_bgrace = str2sec(optarg);
7618                                 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7619                                         fprintf(stderr,
7620                                                 "%s: bad block-grace: %s\n",
7621                                                 progname, optarg);
7622                                         return CMD_HELP;
7623                                 }
7624                         }
7625                         dqb->dqb_valid |= QIF_BTIME;
7626                         break;
7627                 case 'i':
7628                         if (strncmp(optarg, NOTIFY_GRACE,
7629                                     strlen(NOTIFY_GRACE)) == 0) {
7630                                 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7631                         } else {
7632                                 dqi->dqi_igrace = str2sec(optarg);
7633                                 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7634                                         fprintf(stderr,
7635                                                 "%s: bad inode-grace: %s\n",
7636                                                 progname, optarg);
7637                                         return CMD_HELP;
7638                                 }
7639                         }
7640                         dqb->dqb_valid |= QIF_ITIME;
7641                         break;
7642                 case 't': /* Yes, of course! */
7643                         break;
7644                 case LFS_POOL_OPT:
7645                         if (lfs_verify_poolarg(optarg))
7646                                 return -1;
7647                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7648                         qctl->qc_cmd  = LUSTRE_Q_SETINFOPOOL;
7649                         break;
7650                 /* getopt prints error message for us when opterr != 0 */
7651                 default:
7652                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7653                                 progname, argv[optind - 1]);
7654                         fallthrough;
7655                 case 'h':
7656                         return CMD_HELP;
7657                 }
7658         }
7659
7660         if (qctl->qc_type == ALLQUOTA) {
7661                 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7662                         progname);
7663                 return CMD_HELP;
7664         }
7665
7666         if (optind != argc - 1) {
7667                 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7668                         progname, argv[optind + 1]);
7669                 return CMD_HELP;
7670         }
7671
7672         mnt = argv[optind];
7673         rc = llapi_quotactl(mnt, qctl);
7674         if (rc) {
7675                 if (*obd_type)
7676                         fprintf(stderr, "%s %s ", obd_type,
7677                                 obd_uuid2str(&qctl->obd_uuid));
7678                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7679                 return rc;
7680         }
7681
7682         return 0;
7683 }
7684
7685 static int lfs_reset_quota(char *mnt, struct if_quotactl *qctl)
7686 {
7687         struct if_quotactl tmp_qctl;
7688         int index, md_count, dt_count;
7689         int wait_phase = 0, wait_index = 0, wait_count = 0;
7690         int rc, rc2;
7691
7692         /* reset the quota ID, the existing quota setting will be returned */
7693         rc = llapi_quotactl(mnt, qctl);
7694         if (rc)
7695                 return rc;
7696
7697         /* sanity check */
7698         if ((qctl->qc_dqblk.dqb_valid & QIF_LIMITS) != QIF_LIMITS) {
7699                 fprintf(stderr,
7700                         "the existing quota settings are not returned!\n");
7701                 return -EINVAL;
7702         }
7703
7704         rc = llapi_get_obd_count(mnt, &md_count, 1);
7705         if (rc) {
7706                 fprintf(stderr, "can not get mdt count: %s\n", strerror(-rc));
7707                 return rc;
7708         }
7709
7710         rc = llapi_get_obd_count(mnt, &dt_count, 0);
7711         if (rc) {
7712                 fprintf(stderr, "can not get ost count: %s\n", strerror(-rc));
7713                 return rc;
7714         }
7715
7716         memset(&tmp_qctl, 0, sizeof(tmp_qctl));
7717         tmp_qctl.qc_type = qctl->qc_type;
7718         tmp_qctl.qc_id = qctl->qc_id;
7719         tmp_qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7720
7721 retry:
7722         if (wait_phase == 0) {
7723                 for (index = wait_index; index < md_count; index++) {
7724                         tmp_qctl.qc_idx = index;
7725                         tmp_qctl.qc_valid = QC_MDTIDX;
7726                         rc = llapi_quotactl(mnt, &tmp_qctl);
7727                         if (rc == -ENODEV || rc == -ENODATA)
7728                                 continue;
7729                         if (rc) {
7730                                 fprintf(stderr, "quotactl mdt%d failed: %s\n",
7731                                         index, strerror(-rc));
7732                                 break;
7733                         }
7734                         /* check whether the md quota grant is reset */
7735                         if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
7736                             tmp_qctl.qc_dqblk.dqb_ihardlimit != 0)
7737                                 break;
7738                 }
7739
7740                 if (index < md_count) {
7741                         wait_phase = 0;
7742                         wait_index = index;
7743                         goto wait;
7744                 }
7745         } else {
7746                 for (index = wait_index; index < dt_count; index++) {
7747                         tmp_qctl.qc_idx = index;
7748                         tmp_qctl.qc_valid = QC_OSTIDX;
7749                         rc = llapi_quotactl(mnt, &tmp_qctl);
7750                         if (rc == -ENODEV || rc == -ENODATA)
7751                                 continue;
7752                         if (rc) {
7753                                 fprintf(stderr, "quotactl mdt%d failed: %s\n",
7754                                         index, strerror(-rc));
7755                                 break;
7756                         }
7757                         /* check whether the dt quota grant is reset */
7758                         if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
7759                             tmp_qctl.qc_dqblk.dqb_bhardlimit != 0)
7760                                 break;
7761                 }
7762
7763                 if (index < dt_count) {
7764                         wait_phase = 1;
7765                         wait_index = index;
7766                         goto wait;
7767                 }
7768         }
7769
7770         if (wait_phase == 0) {
7771                 wait_phase = 1;
7772                 goto retry;
7773         }
7774
7775         goto out;
7776
7777 wait:
7778         if (rc || wait_count > 30) {
7779                 fprintf(stderr, "fail to reset the quota ID %d on OBDs\n",
7780                         qctl->qc_id);
7781                 goto out;
7782         }
7783
7784         wait_count++;
7785         sleep(1);
7786         fprintf(stdout, "wait %d seconds for OBDs to reset the quota ID %u\n",
7787                 wait_count, qctl->qc_id);
7788         goto retry;
7789
7790
7791 out:
7792         /* restore the quota setting */
7793         if (qctl->qc_dqblk.dqb_isoftlimit == 0 &&
7794             qctl->qc_dqblk.dqb_ihardlimit == 0 &&
7795             qctl->qc_dqblk.dqb_bsoftlimit == 0 &&
7796             qctl->qc_dqblk.dqb_bhardlimit == 0)
7797                 return rc;
7798
7799         memcpy(&tmp_qctl, qctl, sizeof(tmp_qctl));
7800         tmp_qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
7801         rc2 = llapi_quotactl(mnt, &tmp_qctl);
7802         if (!rc2)
7803                 return rc;
7804
7805         fprintf(stderr,
7806                 "fail to restore the quota setting: %s, please restore it manually by\n  lfs setquota %s %d",
7807                 strerror(-rc2),
7808                 qctl->qc_type == USRQUOTA ? "-u" :
7809                                 (qctl->qc_type == GRPQUOTA ? "-g" : "-p"),
7810                 qctl->qc_id);
7811
7812         if (qctl->qc_dqblk.dqb_isoftlimit != 0)
7813                 fprintf(stderr, " -i %llu",
7814                         (unsigned long long)qctl->qc_dqblk.dqb_isoftlimit);
7815         if (qctl->qc_dqblk.dqb_ihardlimit != 0)
7816                 fprintf(stderr, " -I %llu",
7817                         (unsigned long long)qctl->qc_dqblk.dqb_ihardlimit);
7818         if (qctl->qc_dqblk.dqb_bsoftlimit != 0)
7819                 fprintf(stderr, " -b %llu",
7820                         (unsigned long long)qctl->qc_dqblk.dqb_bsoftlimit);
7821         if (qctl->qc_dqblk.dqb_bhardlimit != 0)
7822                 fprintf(stderr, " -B %llu",
7823                         (unsigned long long)qctl->qc_dqblk.dqb_bhardlimit);
7824
7825         fprintf(stderr, " %s\n", mnt);
7826         if (!rc)
7827                 rc = rc2;
7828
7829         return rc;
7830 }
7831
7832 #define BSLIMIT (1 << 0)
7833 #define BHLIMIT (1 << 1)
7834 #define ISLIMIT (1 << 2)
7835 #define IHLIMIT (1 << 3)
7836
7837 int lfs_setquota(int argc, char **argv)
7838 {
7839         int c, rc = 0;
7840         struct if_quotactl *qctl;
7841         char *mnt, *obd_type;
7842         struct obd_dqblk *dqb;
7843         struct option long_opts[] = {
7844         { .val = 'b',   .name = "block-softlimit",
7845                                                 .has_arg = required_argument },
7846         { .val = 'B',   .name = "block-hardlimit",
7847                                                 .has_arg = required_argument },
7848         { .val = 'd',   .name = "default",      .has_arg = no_argument },
7849         { .val = LFS_SETQUOTA_DELETE,
7850                         .name = "delete",       .has_arg = no_argument },
7851         { .val = 'g',   .name = "group",        .has_arg = required_argument },
7852         { .val = 'G',   .name = "default-grp",  .has_arg = no_argument },
7853         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7854         { .val = 'i',   .name = "inode-softlimit",
7855                                                 .has_arg = required_argument },
7856         { .val = 'I',   .name = "inode-hardlimit",
7857                                                 .has_arg = required_argument },
7858         { .val = 'p',   .name = "projid",       .has_arg = required_argument },
7859         { .val = 'P',   .name = "default-prj",  .has_arg = no_argument },
7860         { .val = 'r',   .name = "reset",        .has_arg = no_argument },
7861         { .val = 'u',   .name = "user",         .has_arg = required_argument },
7862         { .val = 'U',   .name = "default-usr",  .has_arg = no_argument },
7863         { .val = LFS_POOL_OPT,
7864                         .name = "pool",         .has_arg = required_argument },
7865         { .name = NULL } };
7866         unsigned int limit_mask = 0;
7867         bool use_default = false;
7868         int qtype, qctl_len;
7869
7870         qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7871         qctl = malloc(qctl_len);
7872         if (!qctl)
7873                 return -ENOMEM;
7874
7875         memset(qctl, 0, qctl_len);
7876         obd_type = (char *)qctl->obd_type;
7877         dqb = &qctl->qc_dqblk;
7878
7879         if (has_times_option(argc, argv)) {
7880                 rc = lfs_setquota_times(argc, argv, qctl);
7881                 goto out;
7882         }
7883
7884         qctl->qc_cmd  = LUSTRE_Q_SETQUOTA;
7885         qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7886                                    * so it can be used as a marker that qc_type
7887                                    * isn't reinitialized from command line
7888                                    */
7889         while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pru:U",
7890                 long_opts, NULL)) != -1) {
7891                 switch (c) {
7892                 case 'U':
7893                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7894                         qtype = USRQUOTA;
7895                         qctl->qc_id = 0;
7896                         goto quota_type_def;
7897                 case 'u':
7898                         qtype = USRQUOTA;
7899                         rc = name2uid(&qctl->qc_id, optarg);
7900                         goto quota_type;
7901                 case 'G':
7902                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7903                         qtype = GRPQUOTA;
7904                         qctl->qc_id = 0;
7905                         goto quota_type_def;
7906                 case 'g':
7907                         qtype = GRPQUOTA;
7908                         rc = name2gid(&qctl->qc_id, optarg);
7909                         goto quota_type;
7910                 case 'P':
7911                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7912                         qtype = PRJQUOTA;
7913                         qctl->qc_id = 0;
7914                         goto quota_type_def;
7915                 case 'p':
7916                         qtype = PRJQUOTA;
7917                         rc = name2projid(&qctl->qc_id, optarg);
7918 quota_type:
7919                         if (rc) {
7920                                 if (str2quotaid(&qctl->qc_id, optarg)) {
7921                                         fprintf(stderr,
7922                                                 "%s setquota: invalid id '%s'\n",
7923                                                 progname, optarg);
7924                                         rc = -1;
7925                                         goto out;
7926                                 }
7927                         }
7928
7929                         if (qctl->qc_id == 0) {
7930                                 fprintf(stderr,
7931                                         "%s setquota: can't set quota for root usr/group/project.\n",
7932                                         progname);
7933                                 rc = -1;
7934                                 goto out;
7935                         }
7936
7937 quota_type_def:
7938                         if (qctl->qc_type != ALLQUOTA) {
7939                                 fprintf(stderr,
7940                                         "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7941                                         progname);
7942                                 rc = CMD_HELP;
7943                                 goto out;
7944                         }
7945                         qctl->qc_type = qtype;
7946                         break;
7947 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7948                 case 'd':
7949                         fprintf(stderr,
7950                                 "%s setquota: '-d' deprecated, use '-D' or '--default'\n",
7951                                 progname);
7952                         fallthrough;
7953 #endif
7954                 case 'D':
7955                         use_default = true;
7956                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7957                         break;
7958                 case LFS_SETQUOTA_DELETE:
7959                         qctl->qc_cmd = LUSTRE_Q_DELETEQID;
7960                         break;
7961                 case 'b':
7962                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7963                         dqb->dqb_bsoftlimit >>= 10;
7964                         limit_mask |= BSLIMIT;
7965                         if (dqb->dqb_bsoftlimit &&
7966                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7967                                 fprintf(stderr,
7968                                         "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7969                                         progname,
7970                                         (unsigned long long)dqb->dqb_bsoftlimit,
7971                                         progname);
7972                         break;
7973                 case 'B':
7974                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7975                         dqb->dqb_bhardlimit >>= 10;
7976                         limit_mask |= BHLIMIT;
7977                         if (dqb->dqb_bhardlimit &&
7978                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7979                                 fprintf(stderr,
7980                                         "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7981                                         "See '%s help setquota' or Lustre manual for details\n",
7982                                         progname,
7983                                         (unsigned long long)dqb->dqb_bhardlimit,
7984                                         progname);
7985                         break;
7986                 case 'i':
7987                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7988                         limit_mask |= ISLIMIT;
7989                         if (dqb->dqb_isoftlimit &&
7990                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7991                                 fprintf(stderr,
7992                                         "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7993                                         progname,
7994                                         (unsigned long long)dqb->dqb_isoftlimit,
7995                                         progname);
7996                         break;
7997                 case 'I':
7998                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7999                         limit_mask |= IHLIMIT;
8000                         if (dqb->dqb_ihardlimit &&
8001                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
8002                                 fprintf(stderr,
8003                                         "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8004                                         progname,
8005                                         (unsigned long long)dqb->dqb_ihardlimit,
8006                                         progname);
8007                         break;
8008                 case LFS_POOL_OPT:
8009                         if (lfs_verify_poolarg(optarg)) {
8010                                 rc = -1;
8011                                 goto out;
8012                         }
8013                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8014                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
8015                                                 LUSTRE_Q_SETDEFAULT_POOL :
8016                                                 LUSTRE_Q_SETQUOTAPOOL;
8017                         break;
8018                 case 'r':
8019                         qctl->qc_cmd = LUSTRE_Q_RESETQID;
8020                         break;
8021                 default:
8022                         fprintf(stderr,
8023                                 "%s setquota: unrecognized option '%s'\n",
8024                                 progname, argv[optind - 1]);
8025                         fallthrough;
8026                 case 'h':
8027                         rc = CMD_HELP;
8028                         goto out;
8029                 }
8030         }
8031
8032         if (qctl->qc_type == ALLQUOTA) {
8033                 fprintf(stderr,
8034                         "%s setquota: either -u or -g must be specified\n",
8035                         progname);
8036                 rc = CMD_HELP;
8037                 goto out;
8038         }
8039
8040         if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
8041             qctl->qc_cmd != LUSTRE_Q_RESETQID && limit_mask == 0) {
8042                 fprintf(stderr,
8043                         "%s setquota: at least one limit must be specified\n",
8044                         progname);
8045                 rc = CMD_HELP;
8046                 goto out;
8047         }
8048
8049         if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8050              qctl->qc_cmd == LUSTRE_Q_RESETQID) && limit_mask != 0) {
8051                 fprintf(stderr,
8052                         "%s setquota: limits should not be specified when using default quota, deleting or resetting quota ID\n",
8053                         progname);
8054                 rc = CMD_HELP;
8055                 goto out;
8056         }
8057
8058         if (use_default && qctl->qc_id == 0) {
8059                 fprintf(stderr,
8060                         "%s setquota: can not set default quota for root user/group/project\n",
8061                         progname);
8062                 rc = CMD_HELP;
8063                 goto out;
8064         }
8065
8066         if ((qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8067              qctl->qc_cmd == LUSTRE_Q_RESETQID)  && qctl->qc_id == 0) {
8068                 fprintf(stderr,
8069                         "%s setquota: can not delete or reset root user/group/project\n",
8070                         progname);
8071                 rc = CMD_HELP;
8072                 goto out;
8073         }
8074
8075         if (optind != argc - 1) {
8076                 fprintf(stderr,
8077                         "%s setquota: filesystem not specified or unexpected argument '%s'\n",
8078                         progname, argv[optind]);
8079                 rc = CMD_HELP;
8080                 goto out;
8081         }
8082
8083         mnt = argv[optind];
8084
8085         if (use_default) {
8086                 dqb->dqb_bhardlimit = 0;
8087                 dqb->dqb_bsoftlimit = 0;
8088                 dqb->dqb_ihardlimit = 0;
8089                 dqb->dqb_isoftlimit = 0;
8090                 dqb->dqb_itime = 0;
8091                 dqb->dqb_btime = 0;
8092                 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
8093                 /* do not set inode limits for Pool Quotas */
8094                 if (qctl->qc_cmd  == LUSTRE_Q_SETDEFAULT_POOL)
8095                         dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
8096         } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
8097                    (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
8098                 /* sigh, we can't just set blimits/ilimits */
8099                 struct if_quotactl *tmp_qctl;
8100
8101                 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8102                 if (!tmp_qctl)
8103                         goto out;
8104
8105                 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
8106                         tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8107                         strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
8108                                 LOV_MAXPOOLNAME);
8109                 } else {
8110                         tmp_qctl->qc_cmd  = LUSTRE_Q_GETQUOTA;
8111                 }
8112                 tmp_qctl->qc_type = qctl->qc_type;
8113                 tmp_qctl->qc_id = qctl->qc_id;
8114
8115                 rc = llapi_quotactl(mnt, tmp_qctl);
8116                 if (rc < 0) {
8117                         free(tmp_qctl);
8118                         goto out;
8119                 }
8120
8121                 if (!(limit_mask & BHLIMIT))
8122                         dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
8123                 if (!(limit_mask & BSLIMIT))
8124                         dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
8125                 if (!(limit_mask & IHLIMIT))
8126                         dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
8127                 if (!(limit_mask & ISLIMIT))
8128                         dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
8129
8130                 /* Keep grace times if we have got no softlimit arguments */
8131                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
8132                         dqb->dqb_valid |= QIF_BTIME;
8133                         dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
8134                 }
8135
8136                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
8137                         dqb->dqb_valid |= QIF_ITIME;
8138                         dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
8139                 }
8140                 free(tmp_qctl);
8141         }
8142
8143         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
8144         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
8145
8146         if (qctl->qc_cmd == LUSTRE_Q_RESETQID)
8147                 rc = lfs_reset_quota(mnt, qctl);
8148         else
8149                 rc = llapi_quotactl(mnt, qctl);
8150
8151         if (rc) {
8152                 if (*obd_type)
8153                         fprintf(stderr,
8154                                 "%s setquota: cannot quotactl '%s' '%s': %s\n",
8155                                 progname, obd_type,
8156                                 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
8157                 else
8158                         fprintf(stderr,
8159                                 "%s setquota: quotactl failed: %s\n",
8160                                 progname, strerror(-rc));
8161         }
8162 out:
8163         if (rc)
8164                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8165
8166         free(qctl);
8167         return rc;
8168 }
8169
8170 /* Converts seconds value into format string
8171  * result is returned in buf
8172  * Notes:
8173  *        1. result is in descenting order: 1w2d3h4m5s
8174  *        2. zero fields are not filled (except for p. 3): 5d1s
8175  *        3. zero seconds value is presented as "0s"
8176  */
8177 static char *__sec2str(time_t seconds, char *buf)
8178 {
8179         const char spec[] = "smhdw";
8180         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8181         unsigned long c;
8182         char *tail = buf;
8183         int i;
8184
8185         for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8186                 c = seconds / mult[i];
8187
8188                 if (c > 0 || (i == 0 && buf == tail))
8189                         tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8190                                           spec[i]);
8191
8192                 seconds %= mult[i];
8193         }
8194
8195         return tail;
8196 }
8197
8198 static void sec2str(time_t seconds, char *buf, int rc)
8199 {
8200         char *tail = buf;
8201
8202         if (rc)
8203                 *tail++ = '[';
8204
8205         tail = __sec2str(seconds, tail);
8206
8207         if (rc && tail - buf < 39) {
8208                 *tail++ = ']';
8209                 *tail++ = 0;
8210         }
8211 }
8212
8213 static void diff2str(time_t seconds, char *buf, time_t now)
8214 {
8215         buf[0] = 0;
8216         if (!seconds)
8217                 return;
8218         if (seconds <= now) {
8219                 strcpy(buf, "expired");
8220                 return;
8221         }
8222         __sec2str(seconds - now, buf);
8223 }
8224
8225 static void print_quota_title(char *name, struct if_quotactl *qctl,
8226                               bool human_readable, bool show_default)
8227 {
8228         if (show_default) {
8229                 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8230                 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8231                        "Filesystem", "bquota", "blimit", "bgrace",
8232                        "iquota", "ilimit", "igrace");
8233         } else {
8234                 printf("Disk quotas for %s %s (%cid %u):\n",
8235                        qtype_name(qctl->qc_type), name,
8236                        *qtype_name(qctl->qc_type), qctl->qc_id);
8237                 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8238                        "Filesystem", human_readable ? "used" : "kbytes",
8239                        "quota", "limit", "grace",
8240                        "files", "quota", "limit", "grace");
8241         }
8242 }
8243
8244 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8245 {
8246         if (!h) {
8247                 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8248         } else {
8249                 if (num >> 40)
8250                         snprintf(buf, buflen, "%5.4gP",
8251                                  (double)num / ((__u64)1 << 40));
8252                 else if (num >> 30)
8253                         snprintf(buf, buflen, "%5.4gT",
8254                                  (double)num / (1 << 30));
8255                 else if (num >> 20)
8256                         snprintf(buf, buflen, "%5.4gG",
8257                                  (double)num / (1 << 20));
8258                 else if (num >> 10)
8259                         snprintf(buf, buflen, "%5.4gM",
8260                                  (double)num / (1 << 10));
8261                 else
8262                         snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8263         }
8264 }
8265
8266 #ifdef HAVE_NATIVE_CLIENT
8267 /* In the current Lustre implementation, the grace time is either the time
8268  * or the timestamp to be used after some quota ID exceeds the soft limt,
8269  * 48 bits should be enough, its high 16 bits can be used as quota flags.
8270  */
8271 #define LQUOTA_GRACE_BITS       48
8272 #define LQUOTA_GRACE_MASK       ((1ULL << LQUOTA_GRACE_BITS) - 1)
8273 #define LQUOTA_GRACE_MAX        LQUOTA_GRACE_MASK
8274 #define LQUOTA_GRACE(t)         (t & LQUOTA_GRACE_MASK)
8275 #define LQUOTA_FLAG(t)          (t >> LQUOTA_GRACE_BITS)
8276 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8277 #endif
8278
8279 #define STRBUF_LEN      24
8280 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8281                         int rc, bool h, bool show_default)
8282 {
8283         time_t now;
8284
8285         time(&now);
8286
8287         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8288             qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8289             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8290             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8291                 int bover = 0, iover = 0;
8292                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8293                 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8294                 char timebuf[40];
8295                 char strbuf[STRBUF_LEN];
8296
8297                 dqb->dqb_btime &= LQUOTA_GRACE_MASK;
8298                 dqb->dqb_itime &= LQUOTA_GRACE_MASK;
8299
8300                 if (dqb->dqb_bhardlimit &&
8301                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8302                         bover = 1;
8303                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8304                         if (dqb->dqb_btime > now)
8305                                 bover = 2;
8306                         else
8307                                 bover = 3;
8308                 }
8309
8310                 if (dqb->dqb_ihardlimit &&
8311                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8312                         iover = 1;
8313                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8314                         if (dqb->dqb_itime > now)
8315                                 iover = 2;
8316                         else
8317                                 iover = 3;
8318                 }
8319
8320                 if (strlen(mnt) > 15)
8321                         printf("%s\n%15s", mnt, "");
8322                 else
8323                         printf("%15s", mnt);
8324
8325                 if (show_default)
8326                         snprintf(timebuf, sizeof(timebuf), "%llu",
8327                                  (unsigned long long)dqb->dqb_btime);
8328                 else if (bover)
8329                         diff2str(dqb->dqb_btime, timebuf, now);
8330
8331                 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8332                            strbuf, sizeof(strbuf), h);
8333                 if (rc == -EREMOTEIO)
8334                         sprintf(numbuf[0], "%s*", strbuf);
8335                 else
8336                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8337                                 "%s" : "[%s]", strbuf);
8338
8339                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8340                 if (type == QC_GENERAL)
8341                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8342                                 "%s" : "[%s]", strbuf);
8343                 else
8344                         sprintf(numbuf[1], "%s", "-");
8345
8346                 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8347                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8348                         "%s" : "[%s]", strbuf);
8349
8350                 if (show_default)
8351                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8352                 else
8353                         printf(" %7s%c %6s %7s %7s",
8354                                numbuf[0], bover ? '*' : ' ', numbuf[1],
8355                                numbuf[2], bover > 1 ? timebuf : "-");
8356
8357                 if (show_default)
8358                         snprintf(timebuf, sizeof(timebuf), "%llu",
8359                                  (unsigned long long)dqb->dqb_itime);
8360                 else if (iover)
8361                         diff2str(dqb->dqb_itime, timebuf, now);
8362
8363                 snprintf(numbuf[0], sizeof(numbuf),
8364                          (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8365                          (uintmax_t)dqb->dqb_curinodes);
8366
8367                 if (type == QC_GENERAL)
8368                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8369                                 "%ju" : "[%ju]",
8370                                 (uintmax_t)dqb->dqb_isoftlimit);
8371                 else
8372                         sprintf(numbuf[1], "%s", "-");
8373
8374                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8375                         "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8376
8377                 if (show_default)
8378                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8379                 else if (type != QC_OSTIDX)
8380                         printf(" %7s%c %6s %7s %7s",
8381                                numbuf[0], iover ? '*' : ' ', numbuf[1],
8382                                numbuf[2], iover > 1 ? timebuf : "-");
8383                 else
8384                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8385                 printf("\n");
8386         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8387                    qctl->qc_cmd == Q_GETOINFO) {
8388                 char bgtimebuf[40];
8389                 char igtimebuf[40];
8390
8391                 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8392                         strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8393                 else
8394                         sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8395                 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8396                         strncpy(igtimebuf, NOTIFY_GRACE, 40);
8397                 else
8398                         sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8399
8400                 printf("Block grace time: %s; Inode grace time: %s\n",
8401                        bgtimebuf, igtimebuf);
8402         }
8403 }
8404
8405 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8406 {
8407         char *dash, *endp;
8408
8409         /* format is "lustre-OST0001" */
8410         dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8411         if (!dash) {
8412                 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8413                 return -EINVAL;
8414         }
8415         dash += 4;
8416
8417         *idx = strtoul(dash, &endp, 16);
8418         if (*idx > 0xffff) {
8419                 fprintf(stderr, "wrong index %s\n", tgtname);
8420                 return -ERANGE;
8421         }
8422
8423         return 0;
8424 }
8425
8426 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8427                            bool h, __u64 *total)
8428 {
8429         int rc = 0, rc1 = 0, count = 0, i = 0;
8430         char **list = NULL, *buffer = NULL;
8431         __u32 valid = qctl->qc_valid;
8432
8433         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8434                 return 0;
8435
8436         /* Is it correct for the case OST0000, OST0002, OST0003 -
8437          * we will ask OST0001 that is absent and won't ask OST0003? */
8438         rc = llapi_get_obd_count(mnt, &count, is_mdt);
8439         if (rc) {
8440                 fprintf(stderr, "can not get %s count: %s\n",
8441                         is_mdt ? "mdt" : "ost", strerror(-rc));
8442                 return rc;
8443         }
8444
8445         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8446                 char fname[PATH_MAX];
8447                 char fsname[LUSTRE_MAXFSNAME + 1];
8448                 int bufsize = sizeof(struct obd_uuid) * count;
8449
8450                 rc = llapi_search_fsname(mnt, fsname);
8451                 if (rc) {
8452                         fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8453                                 mnt);
8454                         goto out;
8455                 }
8456                 buffer = malloc(bufsize + sizeof(*list) * count);
8457                 if (!buffer)
8458                         return -ENOMEM;
8459                 list = (char **)(buffer + bufsize);
8460                 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8461                 count = llapi_get_poolmembers(fname, list, count,
8462                                               buffer, bufsize);
8463                 if (count <= 0)
8464                         goto out;
8465         }
8466
8467         for (i = 0; i < count; i++) {
8468                 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8469                         unsigned int index;
8470
8471                         if (tgt_name2index(list[i], &index))
8472                                 continue;
8473                         qctl->qc_idx = index;
8474                 } else {
8475                         qctl->qc_idx = i;
8476                 }
8477
8478                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8479                 rc = llapi_quotactl(mnt, qctl);
8480                 if (rc) {
8481                         /* It is remote client case. */
8482                         if (rc == -EOPNOTSUPP) {
8483                                 rc = 0;
8484                                 goto out;
8485                         }
8486
8487                         /* no target for this index yet */
8488                         if (rc == -ENODEV) {
8489                                 rc = 0;
8490                                 continue;
8491                         }
8492
8493                         /* inactive target */
8494                         if (rc == -ENODATA) {
8495                                 char name[UUID_MAX+8];
8496
8497                                 snprintf(name, sizeof(name), "%s[inact]",
8498                                         obd_uuid2str(&qctl->obd_uuid));
8499                                 memset(&qctl->qc_dqinfo, 0,
8500                                        sizeof(qctl->qc_dqinfo));
8501                                 memset(&qctl->qc_dqblk, 0,
8502                                        sizeof(qctl->qc_dqblk));
8503                                 print_quota(name, qctl, qctl->qc_valid, 0, h,
8504                                             false);
8505                                 rc = 0;
8506                                 continue;
8507                         }
8508
8509                         if (!rc1)
8510                                 rc1 = rc;
8511                         fprintf(stderr, "quotactl %s%d failed.\n",
8512                                 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8513                         continue;
8514                 }
8515
8516                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8517                             qctl->qc_valid, 0, h, false);
8518                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8519                                    qctl->qc_dqblk.dqb_bhardlimit;
8520         }
8521 out:
8522         if (buffer)
8523                 free(buffer);
8524         qctl->qc_valid = valid;
8525         return rc ? : rc1;
8526 }
8527
8528 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8529                            int verbose, int quiet, bool human_readable,
8530                            bool show_default)
8531 {
8532         int rc1 = 0, rc2 = 0, rc3 = 0;
8533         char *obd_type = (char *)qctl->obd_type;
8534         char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8535         __u64 total_ialloc = 0, total_balloc = 0;
8536         bool use_default_for_blk = false;
8537         bool use_default_for_file = false;
8538         int inacc;
8539
8540         rc1 = llapi_quotactl(mnt, qctl);
8541         if (rc1 < 0) {
8542                 switch (rc1) {
8543                 case -ESRCH:
8544                         fprintf(stderr, "%s quotas are not enabled.\n",
8545                                 qtype_name(qctl->qc_type));
8546                         goto out;
8547                 case -EPERM:
8548                         fprintf(stderr, "Permission denied.\n");
8549                 case -ENODEV:
8550                 case -ENOENT:
8551                         /* We already got error message. */
8552                         goto out;
8553                 default:
8554                         fprintf(stderr, "Unexpected quotactl error: %s\n",
8555                                 strerror(-rc1));
8556                 }
8557         }
8558
8559         if (!show_default && qctl->qc_id == 0) {
8560                 qctl->qc_dqblk.dqb_bhardlimit = 0;
8561                 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8562                 qctl->qc_dqblk.dqb_ihardlimit = 0;
8563                 qctl->qc_dqblk.dqb_isoftlimit = 0;
8564                 qctl->qc_dqblk.dqb_btime = 0;
8565                 qctl->qc_dqblk.dqb_itime = 0;
8566                 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8567         }
8568
8569         if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8570             LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8571                 use_default_for_blk = true;
8572                 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8573         }
8574
8575         if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8576             LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8577                 use_default_for_file = true;
8578                 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8579         }
8580
8581         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8582              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8583              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8584              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8585                 print_quota_title(name, qctl, human_readable, show_default);
8586
8587         if (rc1 && *obd_type)
8588                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8589
8590         if (qctl->qc_valid != QC_GENERAL)
8591                 mnt = "";
8592
8593         inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8594                  qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8595                 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8596                  (QIF_LIMITS|QIF_USAGE));
8597
8598         print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8599
8600         if (!show_default && verbose &&
8601             qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8602             qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8603                 char strbuf[STRBUF_LEN];
8604
8605                 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8606                                       &total_ialloc);
8607                 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8608                                       &total_balloc);
8609                 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8610                            human_readable);
8611                 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8612                        (uintmax_t)total_ialloc, strbuf);
8613         }
8614
8615         if (use_default_for_blk)
8616                 printf("%cid %u is using default block quota setting\n",
8617                        *qtype_name(qctl->qc_type), qctl->qc_id);
8618
8619         if (use_default_for_file)
8620                 printf("%cid %u is using default file quota setting\n",
8621                        *qtype_name(qctl->qc_type), qctl->qc_id);
8622
8623         if (rc1 || rc2 || rc3 || inacc)
8624                 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8625 out:
8626         if (rc1)
8627                 return rc1;
8628         if (rc2)
8629                 return rc2;
8630         if (rc3)
8631                 return rc3;
8632         if (inacc)
8633                 return -EIO;
8634
8635         return 0;
8636 }
8637
8638 static int lfs_project(int argc, char **argv)
8639 {
8640         int ret = 0, err = 0, c, i;
8641         struct project_handle_control phc = { 0 };
8642         enum lfs_project_ops_t op;
8643
8644         phc.newline = true;
8645         phc.assign_projid = false;
8646         /* default action */
8647         op = LFS_PROJECT_LIST;
8648
8649         while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8650                 switch (c) {
8651                 case 'c':
8652                         if (op != LFS_PROJECT_LIST) {
8653                                 fprintf(stderr,
8654                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8655                                         progname);
8656                                 return CMD_HELP;
8657                         }
8658
8659                         op = LFS_PROJECT_CHECK;
8660                         break;
8661                 case 'C':
8662                         if (op != LFS_PROJECT_LIST) {
8663                                 fprintf(stderr,
8664                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8665                                         progname);
8666                                 return CMD_HELP;
8667                         }
8668
8669                         op = LFS_PROJECT_CLEAR;
8670                         break;
8671                 case 's':
8672                         if (op != LFS_PROJECT_LIST) {
8673                                 fprintf(stderr,
8674                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8675                                         progname);
8676                                 return CMD_HELP;
8677                         }
8678
8679                         phc.set_inherit = true;
8680                         op = LFS_PROJECT_SET;
8681                         break;
8682                 case 'd':
8683                         phc.dironly = true;
8684                         break;
8685                 case 'k':
8686                         phc.keep_projid = true;
8687                         break;
8688                 case 'r':
8689                         phc.recursive = true;
8690                         break;
8691                 case 'p':
8692                         if (str2quotaid(&phc.projid, optarg)) {
8693                                 fprintf(stderr,
8694                                         "Invalid project ID: %s\n",
8695                                         optarg);
8696                                 return CMD_HELP;
8697                         }
8698
8699                         phc.assign_projid = true;
8700
8701                         break;
8702                 case '0':
8703                         phc.newline = false;
8704                         break;
8705                 default:
8706                         fprintf(stderr, "%s: invalid option '%c'\n",
8707                                 progname, optopt);
8708                         return CMD_HELP;
8709                 }
8710         }
8711
8712         if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8713                 op = LFS_PROJECT_SET;
8714                 phc.set_projid = true;
8715         } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8716                 phc.set_projid = true;
8717         }
8718
8719         switch (op) {
8720         case LFS_PROJECT_CHECK:
8721                 if (phc.keep_projid) {
8722                         fprintf(stderr,
8723                                 "%s: '-k' is useless together with '-c'\n",
8724                                 progname);
8725                         return CMD_HELP;
8726                 }
8727                 break;
8728         case LFS_PROJECT_CLEAR:
8729                 if (!phc.newline) {
8730                         fprintf(stderr,
8731                                 "%s: '-0' is useless together with '-C'\n",
8732                                 progname);
8733                         return CMD_HELP;
8734                 }
8735                 if (phc.assign_projid) {
8736                         fprintf(stderr,
8737                                 "%s: '-p' is useless together with '-C'\n",
8738                                 progname);
8739                         return CMD_HELP;
8740                 }
8741                 break;
8742         case LFS_PROJECT_SET:
8743                 if (!phc.newline) {
8744                         fprintf(stderr,
8745                                 "%s: '-0' is useless together with '-s'\n",
8746                                 progname);
8747                         return CMD_HELP;
8748                 }
8749                 if (phc.keep_projid) {
8750                         fprintf(stderr,
8751                                 "%s: '-k' is useless together with '-s'\n",
8752                                 progname);
8753                         return CMD_HELP;
8754                 }
8755                 break;
8756         default:
8757                 if (!phc.newline) {
8758                         fprintf(stderr,
8759                                 "%s: '-0' is useless for list operations\n",
8760                                 progname);
8761                         return CMD_HELP;
8762                 }
8763                 break;
8764         }
8765
8766         argv += optind;
8767         argc -= optind;
8768         if (argc == 0) {
8769                 fprintf(stderr, "%s: missing file or directory target(s)\n",
8770                         progname);
8771                 return CMD_HELP;
8772         }
8773
8774         for (i = 0; i < argc; i++) {
8775                 switch (op) {
8776                 case LFS_PROJECT_CHECK:
8777                         err = lfs_project_check(argv[i], &phc);
8778                         break;
8779                 case LFS_PROJECT_LIST:
8780                         err = lfs_project_list(argv[i], &phc);
8781                         break;
8782                 case LFS_PROJECT_CLEAR:
8783                         err = lfs_project_clear(argv[i], &phc);
8784                         break;
8785                 case LFS_PROJECT_SET:
8786                         err = lfs_project_set(argv[i], &phc);
8787                         break;
8788                 default:
8789                         break;
8790                 }
8791                 if (err && !ret)
8792                         ret = err;
8793         }
8794
8795         return ret;
8796 }
8797
8798 static int lfs_quota(int argc, char **argv)
8799 {
8800         int c;
8801         char *mnt, *name = NULL;
8802         struct if_quotactl *qctl;
8803         char *obd_uuid;
8804         int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8805         __u32 valid = QC_GENERAL, idx = 0;
8806         bool human_readable = false;
8807         bool show_default = false;
8808         int qtype;
8809         bool show_pools = false;
8810         struct option long_opts[] = {
8811         { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8812         { .name = NULL } };
8813         char **poollist = NULL;
8814         char *buf = NULL;
8815         int poolcount, i;
8816
8817         qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8818         if (!qctl)
8819                 return -ENOMEM;
8820
8821         qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8822         qctl->qc_type = ALLQUOTA;
8823         obd_uuid = (char *)qctl->obd_uuid.uuid;
8824
8825         while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8826                 long_opts, NULL)) != -1) {
8827                 switch (c) {
8828                 case 'U':
8829                         show_default = true;
8830                 case 'u':
8831                         qtype = USRQUOTA;
8832                         goto quota_type;
8833                 case 'G':
8834                         show_default = true;
8835                 case 'g':
8836                         qtype = GRPQUOTA;
8837                         goto quota_type;
8838                 case 'P':
8839                         show_default = true;
8840                 case 'p':
8841                         qtype = PRJQUOTA;
8842 quota_type:
8843                         if (qctl->qc_type != ALLQUOTA) {
8844                                 fprintf(stderr,
8845                                         "%s quota: only one of -u, -g, or -p may be specified\n",
8846                                         progname);
8847                                 rc = CMD_HELP;
8848                                 goto out;
8849                         }
8850                         qctl->qc_type = qtype;
8851                         break;
8852                 case 't':
8853                         qctl->qc_cmd = LUSTRE_Q_GETINFO;
8854                         break;
8855                 case 'o':
8856                         valid = qctl->qc_valid = QC_UUID;
8857                         snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8858                         break;
8859                 case 'i':
8860                         valid = qctl->qc_valid = QC_MDTIDX;
8861                         idx = qctl->qc_idx = atoi(optarg);
8862                         if (idx == 0 && *optarg != '0') {
8863                                 fprintf(stderr,
8864                                         "%s quota: invalid MDT index '%s'\n",
8865                                         progname, optarg);
8866                                 rc = CMD_HELP;
8867                                 goto out;
8868                         }
8869                         break;
8870                 case 'I':
8871                         valid = qctl->qc_valid = QC_OSTIDX;
8872                         idx = qctl->qc_idx = atoi(optarg);
8873                         if (idx == 0 && *optarg != '0') {
8874                                 fprintf(stderr,
8875                                         "%s quota: invalid OST index '%s'\n",
8876                                         progname, optarg);
8877                                 rc = CMD_HELP;
8878                                 goto out;
8879                         }
8880                         break;
8881                 case 'v':
8882                         verbose = 1;
8883                         break;
8884                 case 'q':
8885                         quiet = 1;
8886                         break;
8887                 case 'h':
8888                         human_readable = true;
8889                         break;
8890                 case LFS_POOL_OPT:
8891                         if ((!optarg) && (argv[optind] != NULL) &&
8892                                 (argv[optind][0] != '-') &&
8893                                 (argv[optind][0] != '/')) {
8894                                 optarg = argv[optind++];
8895                                 if (lfs_verify_poolarg(optarg)) {
8896                                         rc = -EINVAL;
8897                                         goto out;
8898                                 }
8899                                 strncpy(qctl->qc_poolname, optarg,
8900                                         LOV_MAXPOOLNAME);
8901                                 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8902                                         qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8903                                 else
8904                                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8905                                 break;
8906                         }
8907
8908                         /* optarg is NULL */
8909                         show_pools = true;
8910                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8911                         break;
8912                 default:
8913                         fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8914                                 progname, argv[optind - 1]);
8915                         rc = CMD_HELP;
8916                         goto out;
8917                 }
8918         }
8919
8920         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8921         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8922              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8923              qctl->qc_type == ALLQUOTA &&
8924              optind == argc - 1 && !show_default) {
8925                 qctl->qc_idx = idx;
8926
8927                 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8928                         qctl->qc_type = qtype;
8929                         qctl->qc_valid = valid;
8930                         if (qtype == USRQUOTA) {
8931                                 qctl->qc_id = geteuid();
8932                                 rc = uid2name(&name, qctl->qc_id);
8933                         } else {
8934                                 qctl->qc_id = getegid();
8935                                 rc = gid2name(&name, qctl->qc_id);
8936                                 memset(&qctl->qc_dqblk, 0,
8937                                        sizeof(qctl->qc_dqblk));
8938                         }
8939                         if (rc)
8940                                 name = "<unknown>";
8941                         mnt = argv[optind];
8942                         rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8943                                               human_readable, show_default);
8944                         if (rc1 && !rc)
8945                                 rc = rc1;
8946                 }
8947                 goto out;
8948         /* lfs quota -u username /path/to/lustre/mount */
8949         } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8950                    qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8951                 /* options should be followed by u/g-name and mntpoint */
8952                 if ((!show_default && optind + 2 != argc) ||
8953                     (show_default && optind + 1 != argc) ||
8954                     qctl->qc_type == ALLQUOTA) {
8955                         fprintf(stderr,
8956                                 "%s quota: name and mount point must be specified\n",
8957                                 progname);
8958                         rc = CMD_HELP;
8959                         goto out;
8960                 }
8961
8962                 if (!show_default) {
8963                         name = argv[optind++];
8964                         switch (qctl->qc_type) {
8965                         case USRQUOTA:
8966                                 rc = name2uid(&qctl->qc_id, name);
8967                                 break;
8968                         case GRPQUOTA:
8969                                 rc = name2gid(&qctl->qc_id, name);
8970                                 break;
8971                         case PRJQUOTA:
8972                                 rc = name2projid(&qctl->qc_id, name);
8973                                 break;
8974                         default:
8975                                 rc = -ENOTSUP;
8976                                 break;
8977                         }
8978                 } else {
8979                         qctl->qc_valid = QC_GENERAL;
8980                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8981                                         LUSTRE_Q_GETDEFAULT_POOL :
8982                                         LUSTRE_Q_GETDEFAULT;
8983                         qctl->qc_id = 0;
8984                 }
8985
8986                 if (rc) {
8987                         if (str2quotaid(&qctl->qc_id, name)) {
8988                                 fprintf(stderr, "%s quota: invalid id '%s'\n",
8989                                         progname, name);
8990                                 rc = CMD_HELP;
8991                                 goto out;
8992                         }
8993                 }
8994         } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8995                 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8996                         progname);
8997                 rc = CMD_HELP;
8998                 goto out;
8999         }
9000
9001         mnt = argv[optind];
9002         if (show_pools) {
9003                 char *p;
9004
9005                 i = 0;
9006                 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
9007                 if (rc)
9008                         goto out;
9009
9010                 for (i = 0; i < poolcount; i++) {
9011                         p = memchr(poollist[i], '.', MAXNAMLEN);
9012                         if (!p) {
9013                                 fprintf(stderr, "bad string format %.*s\n",
9014                                         MAXNAMLEN, poollist[i]);
9015                                 rc = -EINVAL;
9016                                 goto out;
9017                         }
9018                         p++;
9019                         printf("Quotas for pool: %s\n", p);
9020                         strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
9021                         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9022                                              human_readable, show_default);
9023                         if (rc)
9024                                 break;
9025                 }
9026                 goto out;
9027         }
9028
9029         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9030                              human_readable, show_default);
9031 out:
9032         free(buf);
9033         free(qctl);
9034         return rc;
9035 }
9036 #endif /* HAVE_SYS_QUOTA_H! */
9037
9038 static int flushctx_ioctl(char *mp)
9039 {
9040         int fd, rc;
9041
9042         fd = open(mp, O_RDONLY);
9043         if (fd == -1) {
9044                 fprintf(stderr, "flushctx: error open %s: %s\n",
9045                         mp, strerror(errno));
9046                 return -1;
9047         }
9048
9049         rc = ioctl(fd, LL_IOC_FLUSHCTX);
9050         if (rc == -1)
9051                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
9052                         mp, strerror(errno));
9053
9054         close(fd);
9055         return rc;
9056 }
9057
9058 static int lfs_flushctx(int argc, char **argv)
9059 {
9060         int     kdestroy = 0, reap = 0, c;
9061         char    mntdir[PATH_MAX] = {'\0'};
9062         int     index = 0;
9063         int     rc = 0;
9064
9065         while ((c = getopt(argc, argv, "kr")) != -1) {
9066                 switch (c) {
9067                 case 'k':
9068                         kdestroy = 1;
9069                         break;
9070                 case 'r':
9071                         reap = 1;
9072                         break;
9073                 default:
9074                         fprintf(stderr,
9075                                 "error: %s: option '-%c' unrecognized\n",
9076                                 argv[0], c);
9077                         return CMD_HELP;
9078                 }
9079         }
9080
9081         if (kdestroy) {
9082                 rc = system("kdestroy > /dev/null");
9083                 if (rc) {
9084                         rc = WEXITSTATUS(rc);
9085                         fprintf(stderr,
9086                                 "error destroying tickets: %d, continuing\n",
9087                                 rc);
9088                 }
9089         }
9090
9091         if (optind >= argc) {
9092                 /* flush for all mounted lustre fs. */
9093                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
9094                         /* Check if we have a mount point */
9095                         if (mntdir[0] == '\0')
9096                                 continue;
9097
9098                         if (flushctx_ioctl(mntdir))
9099                                 rc = -1;
9100
9101                         mntdir[0] = '\0'; /* avoid matching in next loop */
9102                 }
9103         } else {
9104                 /* flush fs as specified */
9105                 while (optind < argc) {
9106                         if (flushctx_ioctl(argv[optind++]))
9107                                 rc = -1;
9108                 }
9109         }
9110
9111         if (reap) {
9112                 rc = system("keyctl reap > /dev/null");
9113                 if (rc != 0) {
9114                         rc = WEXITSTATUS(rc);
9115                         fprintf(stderr, "error reaping keyring: %d\n", rc);
9116                 }
9117         }
9118
9119         return rc;
9120 }
9121
9122 static int lfs_changelog(int argc, char **argv)
9123 {
9124         void *changelog_priv;
9125         struct changelog_rec *rec;
9126         long long startrec = 0, endrec = 0;
9127         char *mdd;
9128         struct option long_opts[] = {
9129                 { .val = 'f', .name = "follow", .has_arg = no_argument },
9130                 { .name = NULL } };
9131         char short_opts[] = "f";
9132         int rc, follow = 0;
9133
9134         while ((rc = getopt_long(argc, argv, short_opts,
9135                 long_opts, NULL)) != -1) {
9136                 switch (rc) {
9137                 case 'f':
9138                         follow++;
9139                         break;
9140                 default:
9141                         fprintf(stderr,
9142                                 "%s changelog: unrecognized option '%s'\n",
9143                                 progname, argv[optind - 1]);
9144                         return CMD_HELP;
9145                 }
9146         }
9147         if (optind >= argc) {
9148                 fprintf(stderr, "%s changelog: mdtname must be specified\n",
9149                         progname);
9150                 return CMD_HELP;
9151         }
9152
9153         mdd = argv[optind++];
9154         if (argc > optind) {
9155                 errno = 0;
9156                 startrec = strtoll(argv[optind++], NULL, 10);
9157                 if (errno != 0 || startrec < 0) {
9158                         fprintf(stderr,
9159                                 "%s changelog: bad startrec\n",
9160                                 progname);
9161                         return CMD_HELP;
9162                 }
9163         }
9164
9165         if (argc > optind) {
9166                 errno = 0;
9167                 endrec = strtoll(argv[optind++], NULL, 10);
9168                 if (errno != 0 || endrec < 0) {
9169                         fprintf(stderr,
9170                                 "%s changelog: bad endrec\n",
9171                                 progname);
9172                         return CMD_HELP;
9173                 }
9174         }
9175
9176         rc = llapi_changelog_start(&changelog_priv,
9177                                    CHANGELOG_FLAG_BLOCK |
9178                                    CHANGELOG_FLAG_JOBID |
9179                                    CHANGELOG_FLAG_EXTRA_FLAGS |
9180                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
9181                                    mdd, startrec);
9182         if (rc < 0) {
9183                 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
9184                         progname, strerror(errno = -rc));
9185                 return rc;
9186         }
9187
9188         rc = llapi_changelog_set_xflags(changelog_priv,
9189                                         CHANGELOG_EXTRA_FLAG_UIDGID |
9190                                         CHANGELOG_EXTRA_FLAG_NID |
9191                                         CHANGELOG_EXTRA_FLAG_OMODE |
9192                                         CHANGELOG_EXTRA_FLAG_XATTR);
9193         if (rc < 0) {
9194                 fprintf(stderr,
9195                         "%s changelog: cannot set xflags for changelog: %s\n",
9196                         progname, strerror(errno = -rc));
9197                 return rc;
9198         }
9199
9200         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9201                 time_t secs;
9202                 struct tm ts;
9203
9204                 if (endrec && rec->cr_index > endrec) {
9205                         llapi_changelog_free(&rec);
9206                         break;
9207                 }
9208                 if (rec->cr_index < startrec) {
9209                         llapi_changelog_free(&rec);
9210                         continue;
9211                 }
9212
9213                 secs = rec->cr_time >> 30;
9214                 gmtime_r(&secs, &ts);
9215                 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9216                        "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9217                        changelog_type2str(rec->cr_type),
9218                        ts.tm_hour, ts.tm_min, ts.tm_sec,
9219                        (int)(rec->cr_time & ((1 << 30) - 1)),
9220                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9221                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9222
9223                 if (rec->cr_flags & CLF_JOBID) {
9224                         struct changelog_ext_jobid *jid =
9225                                 changelog_rec_jobid(rec);
9226
9227                         if (jid->cr_jobid[0] != '\0')
9228                                 printf(" j=%s", jid->cr_jobid);
9229                 }
9230
9231                 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
9232                         struct changelog_ext_extra_flags *ef =
9233                                 changelog_rec_extra_flags(rec);
9234
9235                         printf(" ef=0x%llx",
9236                                (unsigned long long)ef->cr_extra_flags);
9237
9238                         if (ef->cr_extra_flags & CLFE_UIDGID) {
9239                                 struct changelog_ext_uidgid *uidgid =
9240                                         changelog_rec_uidgid(rec);
9241
9242                                 printf(" u=%llu:%llu",
9243                                        (unsigned long long)uidgid->cr_uid,
9244                                        (unsigned long long)uidgid->cr_gid);
9245                         }
9246                         if (ef->cr_extra_flags & CLFE_NID) {
9247                                 struct changelog_ext_nid *nid =
9248                                         changelog_rec_nid(rec);
9249
9250                                 printf(" nid=%s",
9251                                        libcfs_nid2str(nid->cr_nid));
9252                         }
9253
9254                         if (ef->cr_extra_flags & CLFE_OPEN) {
9255                                 struct changelog_ext_openmode *omd =
9256                                         changelog_rec_openmode(rec);
9257                                 char mode[] = "---";
9258
9259                                 /* exec mode must be exclusive */
9260                                 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9261                                         mode[2] = 'x';
9262                                 } else {
9263                                         if (omd->cr_openflags & MDS_FMODE_READ)
9264                                                 mode[0] = 'r';
9265                                         if (omd->cr_openflags &
9266                                             (MDS_FMODE_WRITE |
9267                                              MDS_OPEN_TRUNC |
9268                                              MDS_OPEN_APPEND))
9269                                                 mode[1] = 'w';
9270                                 }
9271
9272                                 if (strcmp(mode, "---") != 0)
9273                                         printf(" m=%s", mode);
9274                         }
9275
9276                         if (ef->cr_extra_flags & CLFE_XATTR) {
9277                                 struct changelog_ext_xattr *xattr =
9278                                         changelog_rec_xattr(rec);
9279
9280                                 if (xattr->cr_xattr[0] != '\0')
9281                                         printf(" x=%s", xattr->cr_xattr);
9282                         }
9283                 }
9284
9285                 if (!fid_is_zero(&rec->cr_pfid))
9286                         printf(" p="DFID, PFID(&rec->cr_pfid));
9287                 if (rec->cr_namelen)
9288                         printf(" %.*s", rec->cr_namelen,
9289                                changelog_rec_name(rec));
9290
9291                 if (rec->cr_flags & CLF_RENAME) {
9292                         struct changelog_ext_rename *rnm =
9293                                 changelog_rec_rename(rec);
9294
9295                         if (!fid_is_zero(&rnm->cr_sfid))
9296                                 printf(" s="DFID" sp="DFID" %.*s",
9297                                        PFID(&rnm->cr_sfid),
9298                                        PFID(&rnm->cr_spfid),
9299                                        (int)changelog_rec_snamelen(rec),
9300                                        changelog_rec_sname(rec));
9301                 }
9302                 printf("\n");
9303
9304                 llapi_changelog_free(&rec);
9305         }
9306
9307         llapi_changelog_fini(&changelog_priv);
9308
9309         if (rc < 0)
9310                 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9311                         progname, strerror(errno = -rc));
9312
9313         return (rc == 1 ? 0 : rc);
9314 }
9315
9316 static int lfs_changelog_clear(int argc, char **argv)
9317 {
9318         long long endrec;
9319         int rc;
9320
9321         if (argc != 4)
9322                 return CMD_HELP;
9323
9324         errno = 0;
9325         endrec = strtoll(argv[3], NULL, 10);
9326         if (errno != 0 || endrec < 0) {
9327                 fprintf(stderr,
9328                         "%s: bad endrec '%s'\n",
9329                         argv[0], argv[3]);
9330                 return CMD_HELP;
9331         }
9332
9333         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9334
9335         if (rc == -EINVAL)
9336                 fprintf(stderr, "%s: record out of range: %llu\n",
9337                         argv[0], endrec);
9338         else if (rc == -ENOENT)
9339                 fprintf(stderr, "%s: no changelog user: %s\n",
9340                         argv[0], argv[2]);
9341         else if (rc)
9342                 fprintf(stderr, "%s error: %s\n", argv[0],
9343                         strerror(-rc));
9344
9345         if (rc)
9346                 errno = -rc;
9347
9348         return rc;
9349 }
9350
9351 static void rstripc(char *str, int c)
9352 {
9353         char *end = str + strlen(str);
9354
9355         for (; str < end && end[-1] == c; --end)
9356                 end[-1] = '\0';
9357 }
9358
9359 static int lfs_fid2path(int argc, char **argv)
9360 {
9361         struct option long_opts[] = {
9362                 { .val = 'c',   .name = "cur",  .has_arg = no_argument },
9363                 { .val = 'c',   .name = "current",      .has_arg = no_argument },
9364                 { .val = 'c',   .name = "print-link",   .has_arg = no_argument },
9365                 { .val = 'f',   .name = "print-fid",    .has_arg = no_argument },
9366                 { .val = 'l',   .name = "link", .has_arg = required_argument },
9367                 { .name = NULL } };
9368         char short_opts[] = "cfl:pr:";
9369         bool print_link = false;
9370         bool print_fid = false;
9371         bool print_mnt_dir;
9372         char mnt_dir[PATH_MAX] = "";
9373         int mnt_fd = -1;
9374         char *path_or_fsname;
9375         long long recno = -1;
9376         int linkno = -1;
9377         char *endptr = NULL;
9378         int rc = 0;
9379         int c;
9380         int i;
9381
9382         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9383                 switch (c) {
9384                 case 'c':
9385                         print_link = true;
9386                         break;
9387                 case 'f':
9388                         print_fid = true;
9389                         break;
9390                 case 'l':
9391                         errno = 0;
9392                         linkno = strtol(optarg, &endptr, 10);
9393                         if (errno != 0 || *endptr != '\0' || linkno < 0) {
9394                                 fprintf(stderr,
9395                                         "%s fid2path: invalid linkno '%s'\n",
9396                                         progname, optarg);
9397                                 return CMD_HELP;
9398                         }
9399                         break;
9400                 case 'r':
9401                         /* recno is something to do with changelogs
9402                          * that was never implemented. We just pass it
9403                          * through for the MDT to ignore.
9404                          */
9405                         errno = 0;
9406                         recno = strtoll(optarg, &endptr, 10);
9407                         if (errno != 0 || *endptr != '\0' || recno < 0) {
9408                                 fprintf(stderr,
9409                                         "%s fid2path: invalid recno '%s'\n",
9410                                         progname, optarg);
9411                                 return CMD_HELP;
9412                         }
9413                         break;
9414                 default:
9415                         fprintf(stderr,
9416                                 "%s fid2path: unrecognized option '%s'\n",
9417                                 progname, argv[optind - 1]);
9418                         return CMD_HELP;
9419                 }
9420         }
9421
9422         if (argc - optind < 2) {
9423                 fprintf(stderr,
9424                         "Usage: %s fid2path FSNAME|ROOT FID...\n",
9425                         progname);
9426                 return CMD_HELP;
9427         }
9428
9429         path_or_fsname = argv[optind];
9430
9431         if (*path_or_fsname == '/') {
9432                 print_mnt_dir = true;
9433                 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9434         } else {
9435                 print_mnt_dir = false;
9436                 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9437         }
9438
9439         if (rc < 0) {
9440                 fprintf(stderr,
9441                         "%s fid2path: cannot resolve mount point for '%s': %s\n",
9442                         progname, path_or_fsname, strerror(-rc));
9443                 goto out;
9444         }
9445
9446         mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9447         if (mnt_fd < 0) {
9448                 fprintf(stderr,
9449                         "%s fid2path: cannot open mount point for '%s': %s\n",
9450                         progname, path_or_fsname, strerror(-rc));
9451                 goto out;
9452         }
9453
9454         /* Strip trailing slashes from mnt_dir. */
9455         rstripc(mnt_dir + 1, '/');
9456
9457         for (i = optind + 1; i < argc; i++) {
9458                 const char *fid_str = argv[i];
9459                 struct lu_fid fid;
9460                 int rc2;
9461
9462                 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9463                 if (rc2 < 0) {
9464                         fprintf(stderr,
9465                                 "%s fid2path: invalid FID '%s'\n",
9466                                 progname, fid_str);
9467                         if (rc == 0)
9468                                 rc = rc2;
9469
9470                         continue;
9471                 }
9472
9473                 int linktmp = (linkno >= 0) ? linkno : 0;
9474                 while (1) {
9475                         int oldtmp = linktmp;
9476                         long long rectmp = recno;
9477                         char path_buf[PATH_MAX];
9478
9479                         rc2 = llapi_fid2path_at(mnt_fd, &fid,
9480                                 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9481                         if (rc2 < 0) {
9482                                 fprintf(stderr,
9483                                         "%s fid2path: cannot find %s %s: %s\n",
9484                                         progname, path_or_fsname, fid_str,
9485                                         strerror(-rc2));
9486                                 if (rc == 0)
9487                                         rc = rc2;
9488                                 break;
9489                         }
9490
9491                         if (print_fid)
9492                                 printf("%s ", fid_str);
9493
9494                         if (print_link)
9495                                 printf("%d ", linktmp);
9496
9497                         /* You may think this looks wrong or weird (and it is!)
9498                          * but we are actually trying to preserve the old quirky
9499                          * behaviors (enforced by our old quirky tests!) that
9500                          * make lfs so much fun to work on:
9501                          *
9502                          *   lustre 0x200000007:0x1:0x0 => "/"
9503                          *   /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9504                          *
9505                          * Note that llapi_fid2path() returns "" for the root
9506                          * FID. */
9507
9508                         printf("%s%s%s\n",
9509                                print_mnt_dir ? mnt_dir : "",
9510                                (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9511                                path_buf);
9512
9513                         if (linkno >= 0)
9514                                 /* specified linkno */
9515                                 break;
9516
9517                         if (oldtmp == linktmp)
9518                                 /* no more links */
9519                                 break;
9520                 }
9521         }
9522 out:
9523         if (!(mnt_fd < 0))
9524                 close(mnt_fd);
9525
9526         return rc;
9527 }
9528
9529 static int lfs_path2fid(int argc, char **argv)
9530 {
9531         struct option long_opts[] = {
9532                 { .val = 'p', .name = "parents", .has_arg = no_argument },
9533                 { .name = NULL } };
9534         char            **path;
9535         const char        short_opts[] = "p";
9536         const char       *sep = "";
9537         struct lu_fid     fid;
9538         int               rc = 0;
9539         bool              show_parents = false;
9540
9541         while ((rc = getopt_long(argc, argv, short_opts,
9542                                  long_opts, NULL)) != -1) {
9543                 switch (rc) {
9544                 case 'p':
9545                         show_parents = true;
9546                         break;
9547                 default:
9548                         fprintf(stderr,
9549                                 "%s path2fid: unrecognized option '%s'\n",
9550                                 progname, argv[optind - 1]);
9551                         return CMD_HELP;
9552                 }
9553         }
9554
9555         if (optind > argc - 1) {
9556                 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9557                         progname);
9558                 return CMD_HELP;
9559         } else if (optind < argc - 1) {
9560                 sep = ": ";
9561         }
9562
9563         rc = 0;
9564         for (path = argv + optind; *path != NULL; path++) {
9565                 int err = 0;
9566
9567                 if (!show_parents) {
9568                         err = llapi_path2fid(*path, &fid);
9569                         if (!err)
9570                                 printf("%s%s"DFID"\n",
9571                                        *sep != '\0' ? *path : "", sep,
9572                                        PFID(&fid));
9573                 } else {
9574                         char            name[NAME_MAX + 1];
9575                         unsigned int    linkno = 0;
9576
9577                         while ((err = llapi_path2parent(*path, linkno, &fid,
9578                                                 name, sizeof(name))) == 0) {
9579                                 if (*sep != '\0' && linkno == 0)
9580                                         printf("%s%s", *path, sep);
9581
9582                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9583                                        PFID(&fid), name);
9584                                 linkno++;
9585                         }
9586
9587                         /* err == -ENODATA is end-of-loop */
9588                         if (linkno > 0 && err == -ENODATA) {
9589                                 printf("\n");
9590                                 err = 0;
9591                         }
9592                 }
9593
9594                 if (err) {
9595                         fprintf(stderr,
9596                                 "%s path2fid: cannot get %sfid for '%s': %s\n",
9597                                 progname, show_parents ? "parent " : "", *path,
9598                                 strerror(-err));
9599                         if (rc == 0) {
9600                                 rc = err;
9601                                 errno = -err;
9602                         }
9603                 }
9604         }
9605
9606         return rc;
9607 }
9608
9609 #define MAX_ERRNO       4095
9610 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9611
9612 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9613 {
9614         int rc, rc2, k;
9615
9616         rc = llapi_rmfid(device, fa);
9617         if (rc < 0) {
9618                 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9619                         progname, strerror(-rc));
9620                 return rc;
9621         }
9622
9623         for (k = 0; k < fa->fa_nr; k++) {
9624                 rc2 = (__s32)fa->fa_fids[k].f_ver;
9625                 if (!IS_ERR_VALUE(rc2))
9626                         continue;
9627
9628                 if (rc == 0)
9629                         rc = rc2;
9630
9631                 fa->fa_fids[k].f_ver = 0;
9632                 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9633                         progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9634         }
9635
9636         return rc;
9637 }
9638
9639 static int lfs_rmfid(int argc, char **argv)
9640 {
9641         char *fidstr, *device;
9642         int rc = 0, rc2, nr;
9643         struct fid_array *fa;
9644
9645         if (optind > argc - 1) {
9646                 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9647                 return CMD_HELP;
9648         }
9649
9650         device = argv[optind++];
9651
9652         nr = argc - optind;
9653         fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9654         if (!fa)
9655                 return -ENOMEM;
9656
9657         fa->fa_nr = 0;
9658         rc = 0;
9659         while (optind < argc) {
9660                 int found;
9661
9662                 fidstr = argv[optind++];
9663                 while (*fidstr == '[')
9664                         fidstr++;
9665                 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9666                 if (found != 3) {
9667                         fprintf(stderr, "unrecognized FID: %s\n",
9668                                 argv[optind - 1]);
9669                         exit(1);
9670                 }
9671                 fa->fa_nr++;
9672                 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9673                         /* start another batch */
9674                         rc2 = lfs_rmfid_and_show_errors(device, fa);
9675                         if (rc2 && !rc)
9676                                 rc = rc2;
9677                         fa->fa_nr = 0;
9678                 }
9679         }
9680         if (fa->fa_nr) {
9681                 rc2 = lfs_rmfid_and_show_errors(device, fa);
9682                 if (rc2 && !rc)
9683                         rc = rc2;
9684         }
9685
9686         return rc;
9687 }
9688
9689 static int lfs_data_version(int argc, char **argv)
9690 {
9691         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9692         __u64 data_version;
9693         char *path;
9694         int fd;
9695         int rc;
9696         int c;
9697
9698         if (argc < 2) {
9699                 fprintf(stderr, "%s: FILE must be specified\n",
9700                         progname);
9701                 return CMD_HELP;
9702         }
9703
9704         while ((c = getopt(argc, argv, "hnrw")) != -1) {
9705                 switch (c) {
9706                 case 'n':
9707                         data_version_flags = 0;
9708                         break;
9709                 case 'r':
9710                         data_version_flags |= LL_DV_RD_FLUSH;
9711                         break;
9712                 case 'w':
9713                         data_version_flags |= LL_DV_WR_FLUSH;
9714                         break;
9715                 default:
9716                         fprintf(stderr,
9717                                 "%s data_version: unrecognized option '%s'\n",
9718                                 progname, argv[optind - 1]);
9719                         fallthrough;
9720                 case 'h':
9721                         return CMD_HELP;
9722                 }
9723         }
9724         if (optind == argc) {
9725                 fprintf(stderr, "%s data_version: FILE must be specified\n",
9726                         progname);
9727                 return CMD_HELP;
9728         }
9729
9730         path = argv[optind];
9731         fd = open(path, O_RDONLY);
9732         if (fd < 0) {
9733                 rc = -errno;
9734                 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9735                         progname, path, strerror(-rc));
9736                 return rc;
9737         }
9738
9739         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9740         if (rc < 0)
9741                 fprintf(stderr,
9742                         "%s data_version: cannot get version for '%s': %s\n",
9743                         progname, path, strerror(-rc));
9744         else
9745                 printf("%ju" "\n", (uintmax_t)data_version);
9746
9747         close(fd);
9748         return rc;
9749 }
9750
9751 static int lfs_hsm_state(int argc, char **argv)
9752 {
9753         int rc;
9754         int i = 1;
9755         char *path;
9756         struct hsm_user_state hus;
9757
9758         if (argc < 2)
9759                 return CMD_HELP;
9760
9761         do {
9762                 path = argv[i];
9763
9764                 rc = llapi_hsm_state_get(path, &hus);
9765                 if (rc) {
9766                         fprintf(stderr, "can't get hsm state for %s: %s\n",
9767                                 path, strerror(errno = -rc));
9768                         return rc;
9769                 }
9770
9771                 /* Display path name and status flags */
9772                 printf("%s: (0x%08x)", path, hus.hus_states);
9773
9774                 if (hus.hus_states & HS_RELEASED)
9775                         printf(" released");
9776                 if (hus.hus_states & HS_EXISTS)
9777                         printf(" exists");
9778                 if (hus.hus_states & HS_DIRTY)
9779                         printf(" dirty");
9780                 if (hus.hus_states & HS_ARCHIVED)
9781                         printf(" archived");
9782                 /* Display user-settable flags */
9783                 if (hus.hus_states & HS_NORELEASE)
9784                         printf(" never_release");
9785                 if (hus.hus_states & HS_NOARCHIVE)
9786                         printf(" never_archive");
9787                 if (hus.hus_states & HS_LOST)
9788                         printf(" lost_from_hsm");
9789
9790                 if (hus.hus_archive_id != 0)
9791                         printf(", archive_id:%d", hus.hus_archive_id);
9792                 printf("\n");
9793
9794         } while (++i < argc);
9795
9796         return 0;
9797 }
9798
9799 #define LFS_HSM_SET   0
9800 #define LFS_HSM_CLEAR 1
9801
9802 /**
9803  * Generic function to set or clear HSM flags.
9804  * Used by hsm_set and hsm_clear.
9805  *
9806  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9807  */
9808 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9809 {
9810         struct option long_opts[] = {
9811         { .val = 'A',   .name = "archived",     .has_arg = no_argument },
9812         { .val = 'a',   .name = "noarchive",    .has_arg = no_argument },
9813         { .val = 'd',   .name = "dirty",        .has_arg = no_argument },
9814         { .val = 'e',   .name = "exists",       .has_arg = no_argument },
9815         { .val = 'h',   .name = "help",         .has_arg = no_argument },
9816         { .val = 'i',   .name = "archive-id",   .has_arg = required_argument },
9817         { .val = 'l',   .name = "lost",         .has_arg = no_argument },
9818         { .val = 'r',   .name = "norelease",    .has_arg = no_argument },
9819         { .name = NULL } };
9820         __u64 mask = 0;
9821         int c, rc;
9822         char *path;
9823         __u32 archive_id = 0;
9824         char *end = NULL;
9825
9826         if (argc < 3)
9827                 return CMD_HELP;
9828
9829         while ((c = getopt_long(argc, argv, "aAdehi:lr",
9830                                 long_opts, NULL)) != -1) {
9831                 switch (c) {
9832                 case 'l':
9833                         mask |= HS_LOST;
9834                         break;
9835                 case 'a':
9836                         mask |= HS_NOARCHIVE;
9837                         break;
9838                 case 'A':
9839                         mask |= HS_ARCHIVED;
9840                         break;
9841                 case 'r':
9842                         mask |= HS_NORELEASE;
9843                         break;
9844                 case 'd':
9845                         mask |= HS_DIRTY;
9846                         break;
9847                 case 'e':
9848                         mask |= HS_EXISTS;
9849                         break;
9850                 case 'i':
9851                         errno = 0;
9852                         archive_id = strtol(optarg, &end, 10);
9853                         if (errno != 0 || *end != '\0' || archive_id < 0) {
9854                                 fprintf(stderr,
9855                                         "%s: invalid archive_id: '%s'\n",
9856                                         progname, end);
9857                                 return CMD_HELP;
9858                         }
9859                         break;
9860                 default:
9861                         fprintf(stderr, "%s: unrecognized option '%s'\n",
9862                                 progname, argv[optind - 1]);
9863                         fallthrough;
9864                 case 'h':
9865                         return CMD_HELP;
9866                 }
9867         }
9868
9869         /* User should have specified a flag */
9870         if (mask == 0)
9871                 return CMD_HELP;
9872
9873         while (optind < argc) {
9874                 path = argv[optind];
9875
9876                 /* If mode == 0, this means we apply the mask. */
9877                 if (mode == LFS_HSM_SET)
9878                         rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9879                 else
9880                         rc = llapi_hsm_state_set(path, 0, mask, 0);
9881
9882                 if (rc != 0) {
9883                         fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9884                                 path, strerror(errno = -rc));
9885                         return rc;
9886                 }
9887                 optind++;
9888         }
9889
9890         return 0;
9891 }
9892
9893 static int lfs_hsm_action(int argc, char **argv)
9894 {
9895         int                              rc;
9896         int                              i = 1;
9897         char                            *path;
9898         struct hsm_current_action        hca;
9899         struct hsm_extent                he;
9900         enum hsm_user_action             hua;
9901         enum hsm_progress_states         hps;
9902
9903         if (argc < 2)
9904                 return CMD_HELP;
9905
9906         do {
9907                 path = argv[i];
9908
9909                 rc = llapi_hsm_current_action(path, &hca);
9910                 if (rc) {
9911                         fprintf(stderr, "can't get hsm action for %s: %s\n",
9912                                 path, strerror(errno = -rc));
9913                         return rc;
9914                 }
9915                 he = hca.hca_location;
9916                 hua = hca.hca_action;
9917                 hps = hca.hca_state;
9918
9919                 printf("%s: %s", path, hsm_user_action2name(hua));
9920
9921                 /* Skip file without action */
9922                 if (hca.hca_action == HUA_NONE) {
9923                         printf("\n");
9924                         continue;
9925                 }
9926
9927                 printf(" %s ", hsm_progress_state2name(hps));
9928
9929                 if ((hps == HPS_RUNNING) &&
9930                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9931                         printf("(%llu bytes moved)\n",
9932                                (unsigned long long)he.length);
9933                 else if ((he.offset + he.length) == LUSTRE_EOF)
9934                         printf("(from %llu to EOF)\n",
9935                                (unsigned long long)he.offset);
9936                 else
9937                         printf("(from %llu to %llu)\n",
9938                                (unsigned long long)he.offset,
9939                                (unsigned long long)(he.offset + he.length));
9940
9941         } while (++i < argc);
9942
9943         return 0;
9944 }
9945
9946 static int lfs_hsm_set(int argc, char **argv)
9947 {
9948         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9949 }
9950
9951 static int lfs_hsm_clear(int argc, char **argv)
9952 {
9953         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9954 }
9955
9956 /**
9957  * Check file state and return its fid, to be used by lfs_hsm_request().
9958  *
9959  * \param[in]     file      Path to file to check
9960  * \param[in,out] fid       Pointer to allocated lu_fid struct.
9961  * \param[in,out] last_dev  Pointer to last device id used.
9962  *
9963  * \return 0 on success.
9964  */
9965 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9966                                 dev_t *last_dev)
9967 {
9968         struct stat     st;
9969         int             rc;
9970
9971         rc = lstat(file, &st);
9972         if (rc) {
9973                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9974                 return -errno;
9975         }
9976         /*
9977          * Checking for regular file as archiving as posix copytool
9978          * rejects archiving files other than regular files
9979          */
9980         if (!S_ISREG(st.st_mode)) {
9981                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9982                 return CMD_HELP;
9983         }
9984         /* A request should be ... */
9985         if (*last_dev != st.st_dev && *last_dev != 0) {
9986                 fprintf(stderr,
9987                         "All files should be on the same filesystem: %s\n",
9988                         file);
9989                 return -EINVAL;
9990         }
9991         *last_dev = st.st_dev;
9992
9993         rc = llapi_path2fid(file, fid);
9994         if (rc) {
9995                 fprintf(stderr, "Cannot read FID of %s: %s\n",
9996                         file, strerror(-rc));
9997                 return rc;
9998         }
9999         return 0;
10000 }
10001
10002 /* Fill an HSM HUR item with a given file name.
10003  *
10004  * If mntpath is set, then the filename is actually a FID, and no
10005  * lookup on the filesystem will be performed.
10006  *
10007  * \param[in]  hur         the user request to fill
10008  * \param[in]  idx         index of the item inside the HUR to fill
10009  * \param[in]  mntpath     mountpoint of Lustre
10010  * \param[in]  fname       filename (if mtnpath is NULL)
10011  *                         or FID (if mntpath is set)
10012  * \param[in]  last_dev    pointer to last device id used
10013  *
10014  * \retval 0 on success
10015  * \retval CMD_HELP or a negative errno on error
10016  */
10017 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
10018                          const char *mntpath, const char *fname,
10019                          dev_t *last_dev)
10020 {
10021         struct hsm_user_item *hui = &hur->hur_user_item[idx];
10022         int rc;
10023
10024         hui->hui_extent.length = -1;
10025
10026         if (mntpath) {
10027                 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
10028                 if (rc)
10029                         fprintf(stderr, "hsm: '%s' is not a valid FID\n",
10030                                 fname);
10031         } else {
10032                 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
10033         }
10034
10035         if (rc == 0)
10036                 hur->hur_request.hr_itemcount++;
10037
10038         return rc;
10039 }
10040
10041 static int lfs_hsm_request(int argc, char **argv, int action)
10042 {
10043         struct option long_opts[] = {
10044         { .val = 'a',   .name = "archive",      .has_arg = required_argument },
10045         { .val = 'D',   .name = "data",         .has_arg = required_argument },
10046         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10047         { .val = 'l',   .name = "filelist",     .has_arg = required_argument },
10048         { .val = 'm',   .name = "mntpath",      .has_arg = required_argument },
10049         { .name = NULL } };
10050         dev_t last_dev = 0;
10051         struct hsm_user_request *hur, *oldhur;
10052         int c, i;
10053         size_t len;
10054         int nbfile;
10055         char *line = NULL;
10056         char *filelist = NULL;
10057         char fullpath[PATH_MAX];
10058         char *opaque = NULL;
10059         int opaque_len = 0;
10060         int archive_id = 0;
10061         FILE *fp;
10062         int nbfile_alloc = 0;
10063         char *some_file = NULL;
10064         char *mntpath = NULL;
10065         int rc;
10066
10067         if (argc < 2)
10068                 return CMD_HELP;
10069
10070         while ((c = getopt_long(argc, argv, "a:D:hl:m:",
10071                                 long_opts, NULL)) != -1) {
10072                 switch (c) {
10073                 case 'l':
10074                         filelist = optarg;
10075                         break;
10076                 case 'D':
10077                         opaque = optarg;
10078                         break;
10079                 case 'a':
10080                         if (action != HUA_ARCHIVE &&
10081                             action != HUA_REMOVE) {
10082                                 fprintf(stderr,
10083                                         "error: -a is supported only when archiving or removing\n");
10084                                 return CMD_HELP;
10085                         }
10086                         archive_id = atoi(optarg);
10087                         break;
10088                 case 'm':
10089                         if (!some_file) {
10090                                 mntpath = optarg;
10091                                 some_file = strdup(optarg);
10092                         }
10093                         break;
10094                 default:
10095                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10096                                 progname, argv[optind - 1]);
10097                         fallthrough;
10098                 case 'h':
10099                         return CMD_HELP;
10100                 }
10101         }
10102
10103         /* All remaining args are files, so we have at least nbfile */
10104         nbfile = argc - optind;
10105
10106         if ((nbfile == 0) && (!filelist))
10107                 return CMD_HELP;
10108
10109         if (opaque)
10110                 opaque_len = strlen(opaque);
10111
10112         /*
10113          * Alloc the request structure with enough place to store all files
10114          * from command line.
10115          */
10116         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
10117         if (!hur) {
10118                 fprintf(stderr, "Cannot create the request: %s\n",
10119                         strerror(errno));
10120                 return errno;
10121         }
10122         nbfile_alloc = nbfile;
10123
10124         hur->hur_request.hr_action = action;
10125         hur->hur_request.hr_archive_id = archive_id;
10126         hur->hur_request.hr_flags = 0;
10127
10128         /* All remaining args are files, add them */
10129         if (nbfile != 0 && some_file == NULL)
10130                 some_file = strdup(argv[optind]);
10131
10132         for (i = 0; i < nbfile; i++) {
10133                 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
10134                                    &last_dev);
10135                 if (rc)
10136                         goto out_free;
10137         }
10138
10139         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
10140
10141         /* If a filelist was specified, read the filelist from it. */
10142         if (filelist) {
10143                 fp = fopen(filelist, "r");
10144                 if (!fp) {
10145                         fprintf(stderr, "Cannot read the file list %s: %s\n",
10146                                 filelist, strerror(errno));
10147                         rc = -errno;
10148                         goto out_free;
10149                 }
10150
10151                 while ((rc = getline(&line, &len, fp)) != -1) {
10152                         /*
10153                          * If allocated buffer was too small, get something
10154                          * larger
10155                          */
10156                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
10157                                 ssize_t size;
10158
10159                                 nbfile_alloc = nbfile_alloc * 2 + 1;
10160                                 oldhur = hur;
10161                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
10162                                                                    opaque_len);
10163                                 if (!hur) {
10164                                         fprintf(stderr,
10165                                                 "hsm: cannot allocate the request: %s\n",
10166                                                 strerror(errno));
10167                                         hur = oldhur;
10168                                         rc = -errno;
10169                                         fclose(fp);
10170                                         goto out_free;
10171                                 }
10172                                 size = hur_len(oldhur);
10173                                 if (size < 0) {
10174                                         fprintf(stderr,
10175                                                 "hsm: cannot allocate %u files + %u bytes data\n",
10176                                                 oldhur->hur_request.hr_itemcount,
10177                                                 oldhur->hur_request.hr_data_len);
10178                                         free(hur);
10179                                         hur = oldhur;
10180                                         rc = -E2BIG;
10181                                         fclose(fp);
10182                                         goto out_free;
10183                                 }
10184                                 memcpy(hur, oldhur, size);
10185                                 free(oldhur);
10186                         }
10187
10188                         /* Chop CR */
10189                         if (line[strlen(line) - 1] == '\n')
10190                                 line[strlen(line) - 1] = '\0';
10191
10192                         rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
10193                                            mntpath, line, &last_dev);
10194                         if (rc) {
10195                                 fclose(fp);
10196                                 goto out_free;
10197                         }
10198
10199                         if (!some_file) {
10200                                 some_file = line;
10201                                 line = NULL;
10202                         }
10203                 }
10204
10205                 rc = fclose(fp);
10206                 free(line);
10207         }
10208
10209         /* If a --data was used, add it to the request */
10210         hur->hur_request.hr_data_len = opaque_len;
10211         if (opaque)
10212                 memcpy(hur_data(hur), opaque, opaque_len);
10213
10214         /* Send the HSM request */
10215         if (realpath(some_file, fullpath) == NULL) {
10216                 fprintf(stderr, "Could not find path '%s': %s\n",
10217                         some_file, strerror(errno));
10218         }
10219         rc = llapi_hsm_request(fullpath, hur);
10220         if (rc) {
10221                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
10222                         some_file, strerror(-rc));
10223                 goto out_free;
10224         }
10225
10226 out_free:
10227         free(some_file);
10228         free(hur);
10229         return rc;
10230 }
10231
10232 static int lfs_hsm_archive(int argc, char **argv)
10233 {
10234         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
10235 }
10236
10237 static int lfs_hsm_restore(int argc, char **argv)
10238 {
10239         return lfs_hsm_request(argc, argv, HUA_RESTORE);
10240 }
10241
10242 static int lfs_hsm_release(int argc, char **argv)
10243 {
10244         return lfs_hsm_request(argc, argv, HUA_RELEASE);
10245 }
10246
10247 static int lfs_hsm_remove(int argc, char **argv)
10248 {
10249         return lfs_hsm_request(argc, argv, HUA_REMOVE);
10250 }
10251
10252 static int lfs_hsm_cancel(int argc, char **argv)
10253 {
10254         return lfs_hsm_request(argc, argv, HUA_CANCEL);
10255 }
10256
10257 static int lfs_swap_layouts(int argc, char **argv)
10258 {
10259         if (argc != 3)
10260                 return CMD_HELP;
10261
10262         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
10263                                   SWAP_LAYOUTS_KEEP_MTIME |
10264                                   SWAP_LAYOUTS_KEEP_ATIME);
10265 }
10266
10267 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10268
10269 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10270
10271 int lfs_get_mode(const char *string)
10272 {
10273         enum lock_mode_user mode;
10274
10275         for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10276                 if (lock_mode_names[mode] == NULL)
10277                         continue;
10278                 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10279                         return mode;
10280         }
10281
10282         return -EINVAL;
10283 }
10284
10285 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10286 {
10287         enum lu_ladvise_type advice;
10288
10289         for (advice = 0;
10290              advice < ARRAY_SIZE(ladvise_names); advice++) {
10291                 if (ladvise_names[advice] == NULL)
10292                         continue;
10293                 if (strcmp(string, ladvise_names[advice]) == 0)
10294                         return advice;
10295         }
10296
10297         return LU_LADVISE_INVALID;
10298 }
10299
10300 static int lfs_ladvise(int argc, char **argv)
10301 {
10302         struct option long_opts[] = {
10303         { .val = 'a',   .name = "advice",       .has_arg = required_argument },
10304         { .val = 'b',   .name = "background",   .has_arg = no_argument },
10305         { .val = 'e',   .name = "end",          .has_arg = required_argument },
10306         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10307         { .val = 'l',   .name = "length",       .has_arg = required_argument },
10308         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
10309         { .val = 's',   .name = "start",        .has_arg = required_argument },
10310         { .val = 'u',   .name = "unset",        .has_arg = no_argument },
10311         { .name = NULL } };
10312         struct llapi_lu_ladvise advice;
10313         enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10314         unsigned long long start = 0;
10315         unsigned long long end = LUSTRE_EOF;
10316         unsigned long long length = 0;
10317         unsigned long long size_units;
10318         unsigned long long flags = 0;
10319         int c, fd, rc = 0;
10320         const char *path;
10321         int mode = 0;
10322
10323         optind = 0;
10324         while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10325                                 long_opts, NULL)) != -1) {
10326                 switch (c) {
10327                 case 'a':
10328                         advice_type = lfs_get_ladvice(optarg);
10329                         if (advice_type == LU_LADVISE_INVALID) {
10330                                 fprintf(stderr,
10331                                         "%s: invalid advice type '%s'\n",
10332                                         progname, optarg);
10333                                 fprintf(stderr, "Valid types:");
10334
10335                                 for (advice_type = 0;
10336                                      advice_type < ARRAY_SIZE(ladvise_names);
10337                                      advice_type++) {
10338                                         if (ladvise_names[advice_type] == NULL)
10339                                                 continue;
10340                                         fprintf(stderr, " %s",
10341                                                 ladvise_names[advice_type]);
10342                                 }
10343                                 fprintf(stderr, "\n");
10344
10345                                 return CMD_HELP;
10346                         }
10347                         break;
10348                 case 'b':
10349                         flags |= LF_ASYNC;
10350                         break;
10351                 case 'u':
10352                         flags |= LF_UNSET;
10353                         break;
10354                 case 'e':
10355                         size_units = 1;
10356                         rc = llapi_parse_size(optarg, &end,
10357                                               &size_units, 0);
10358                         if (rc) {
10359                                 fprintf(stderr, "%s: bad end offset '%s'\n",
10360                                         argv[0], optarg);
10361                                 return CMD_HELP;
10362                         }
10363                         break;
10364                 case 's':
10365                         size_units = 1;
10366                         rc = llapi_parse_size(optarg, &start,
10367                                               &size_units, 0);
10368                         if (rc) {
10369                                 fprintf(stderr,
10370                                         "%s: bad start offset '%s'\n",
10371                                         argv[0], optarg);
10372                                 return CMD_HELP;
10373                         }
10374                         break;
10375                 case 'l':
10376                         size_units = 1;
10377                         rc = llapi_parse_size(optarg, &length,
10378                                               &size_units, 0);
10379                         if (rc) {
10380                                 fprintf(stderr, "%s: bad length '%s'\n",
10381                                         argv[0], optarg);
10382                                 return CMD_HELP;
10383                         }
10384                         break;
10385                 case 'm':
10386                         mode = lfs_get_mode(optarg);
10387                         if (mode < 0) {
10388                                 fprintf(stderr,
10389                                         "%s: bad mode '%s', valid modes are READ or WRITE\n",
10390                                         argv[0], optarg);
10391                                 return CMD_HELP;
10392                         }
10393                         break;
10394                 default:
10395                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10396                                 progname, argv[optind - 1]);
10397                         fallthrough;
10398                 case 'h':
10399                         return CMD_HELP;
10400                 }
10401         }
10402
10403         if (advice_type == LU_LADVISE_INVALID) {
10404                 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10405                 fprintf(stderr, "Valid types:");
10406                 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10407                      advice_type++) {
10408                         if (ladvise_names[advice_type] == NULL)
10409                                 continue;
10410                         fprintf(stderr, " %s", ladvise_names[advice_type]);
10411                 }
10412                 fprintf(stderr, "\n");
10413                 return CMD_HELP;
10414         }
10415
10416         if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10417                 fprintf(stderr,
10418                         "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10419                         argv[0]);
10420                 return CMD_HELP;
10421         }
10422
10423         if (argc <= optind) {
10424                 fprintf(stderr, "%s: please give one or more file names\n",
10425                         argv[0]);
10426                 return CMD_HELP;
10427         }
10428
10429         if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10430                 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10431                         argv[0]);
10432                 return CMD_HELP;
10433         }
10434
10435         if (end == LUSTRE_EOF && length != 0)
10436                 end = start + length;
10437
10438         if (end <= start) {
10439                 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10440                         argv[0], start, end);
10441                 return CMD_HELP;
10442         }
10443
10444         if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10445                 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10446                         argv[0]);
10447                 return CMD_HELP;
10448         }
10449
10450         if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10451                 fprintf(stderr, "%s: mode is required with lockahead\n",
10452                         argv[0]);
10453                 return CMD_HELP;
10454         }
10455
10456         while (optind < argc) {
10457                 int rc2;
10458
10459                 path = argv[optind++];
10460
10461                 fd = open(path, O_RDONLY);
10462                 if (fd < 0) {
10463                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10464                                 argv[0], path, strerror(errno));
10465                         rc2 = -errno;
10466                         goto next;
10467                 }
10468
10469                 advice.lla_start = start;
10470                 advice.lla_end = end;
10471                 advice.lla_advice = advice_type;
10472                 advice.lla_value1 = 0;
10473                 advice.lla_value2 = 0;
10474                 advice.lla_value3 = 0;
10475                 advice.lla_value4 = 0;
10476                 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10477                         advice.lla_lockahead_mode = mode;
10478                         advice.lla_peradvice_flags = flags;
10479                 }
10480
10481                 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10482                 close(fd);
10483                 if (rc2 < 0) {
10484                         fprintf(stderr,
10485                                 "%s: cannot give advice '%s' to file '%s': %s\n",
10486                                 argv[0], ladvise_names[advice_type],
10487                                 path, strerror(errno));
10488
10489                         goto next;
10490                 }
10491
10492 next:
10493                 if (rc == 0 && rc2 < 0)
10494                         rc = rc2;
10495         }
10496         return rc;
10497 }
10498
10499 static const char *const heat_names[] = LU_HEAT_NAMES;
10500
10501 static int lfs_heat_get(int argc, char **argv)
10502 {
10503         struct lu_heat *heat;
10504         int rc = 0, rc2;
10505         char *path;
10506         int fd;
10507         int i;
10508
10509         if (argc <= 1)
10510                 return CMD_HELP;
10511
10512         heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10513         if (!heat) {
10514                 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10515                 return -ENOMEM;
10516         }
10517
10518         optind = 1;
10519         while (optind < argc) {
10520                 path = argv[optind++];
10521
10522                 fd = open(path, O_RDONLY);
10523                 if (fd < 0) {
10524                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10525                                 argv[0], path, strerror(errno));
10526                         rc2 = -errno;
10527                         goto next;
10528                 }
10529
10530                 heat->lh_count = OBD_HEAT_COUNT;
10531                 rc2 = llapi_heat_get(fd, heat);
10532                 close(fd);
10533                 if (rc2 < 0) {
10534                         fprintf(stderr,
10535                                 "%s: cannot get heat of file '%s': %s\n",
10536                                 argv[0], path, strerror(errno));
10537                         goto next;
10538                 }
10539
10540                 printf("flags: %x\n", heat->lh_flags);
10541                 for (i = 0; i < heat->lh_count; i++)
10542                         printf("%s: %llu\n", heat_names[i],
10543                                (unsigned long long)heat->lh_heat[i]);
10544 next:
10545                 if (rc == 0 && rc2 < 0)
10546                         rc = rc2;
10547         }
10548
10549         free(heat);
10550         return rc;
10551 }
10552
10553 static int lfs_heat_set(int argc, char **argv)
10554 {
10555         struct option long_opts[] = {
10556         { .val = 'c',   .name = "clear",        .has_arg = no_argument },
10557         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10558         { .val = 'o',   .name = "off",          .has_arg = no_argument },
10559         { .val = 'O',   .name = "on",           .has_arg = no_argument },
10560         { .name = NULL } };
10561         enum lu_heat_flag flags = 0;
10562         int rc = 0, rc2;
10563         char *path;
10564         int fd;
10565         int c;
10566
10567         if (argc <= 1)
10568                 return CMD_HELP;
10569
10570         optind = 0;
10571         while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10572                 switch (c) {
10573                 case 'c':
10574                         flags |= LU_HEAT_FLAG_CLEAR;
10575                         break;
10576                 case 'o':
10577                         flags |= LU_HEAT_FLAG_CLEAR;
10578                         flags |= LU_HEAT_FLAG_OFF;
10579                         break;
10580                 case 'O':
10581                         flags &= ~LU_HEAT_FLAG_OFF;
10582                         break;
10583                 default:
10584                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10585                                 progname, argv[optind - 1]);
10586                         fallthrough;
10587                 case 'h':
10588                         return CMD_HELP;
10589                 }
10590         }
10591
10592         if (argc <= optind) {
10593                 fprintf(stderr, "%s: please give one or more file names\n",
10594                         argv[0]);
10595                 return CMD_HELP;
10596         }
10597
10598         while (optind < argc) {
10599                 path = argv[optind++];
10600
10601                 fd = open(path, O_RDONLY);
10602                 if (fd < 0) {
10603                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10604                                 argv[0], path, strerror(errno));
10605                         rc2 = -errno;
10606                         goto next;
10607                 }
10608
10609                 rc2 = llapi_heat_set(fd, flags);
10610                 close(fd);
10611                 if (rc2 < 0) {
10612                         fprintf(stderr,
10613                                 "%s: cannot setflags heat of file '%s': %s\n",
10614                                 argv[0], path, strerror(errno));
10615                         goto next;
10616                 }
10617 next:
10618                 if (rc == 0 && rc2 < 0)
10619                         rc = rc2;
10620         }
10621         return rc;
10622 }
10623
10624 /**
10625  * The input string contains a comma delimited list of component ids and
10626  * ranges, for example "1,2-4,7".
10627  */
10628 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10629 {
10630         bool end_of_loop = false;
10631         char *ptr = NULL;
10632         int nr = 0;
10633         int rc;
10634
10635         if (!arg)
10636                 return -EINVAL;
10637
10638         while (!end_of_loop) {
10639                 int start_index;
10640                 int end_index;
10641                 int i;
10642                 char *endptr = NULL;
10643
10644                 rc = -EINVAL;
10645                 ptr = strchrnul(arg, ',');
10646                 end_of_loop = *ptr == '\0';
10647                 *ptr = '\0';
10648
10649                 start_index = strtol(arg, &endptr, 0);
10650                 if (endptr == arg) /* no data at all */
10651                         break;
10652                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10653                         break;
10654                 if (start_index < 0)
10655                         break;
10656
10657                 end_index = start_index;
10658                 if (*endptr == '-') {
10659                         end_index = strtol(endptr + 1, &endptr, 0);
10660                         if (*endptr != '\0')
10661                                 break;
10662                         if (end_index < start_index)
10663                                 break;
10664                 }
10665
10666                 for (i = start_index; i <= end_index && size > 0; i++) {
10667                         int j;
10668
10669                         /* remove duplicate */
10670                         for (j = 0; j < nr; j++) {
10671                                 if (ids[j] == i)
10672                                         break;
10673                         }
10674                         if (j == nr) { /* no duplicate */
10675                                 ids[nr++] = i;
10676                                 --size;
10677                         }
10678                 }
10679
10680                 if (size == 0 && i < end_index)
10681                         break;
10682
10683                 *ptr = ',';
10684                 arg = ++ptr;
10685                 rc = 0;
10686         }
10687         if (!end_of_loop && ptr)
10688                 *ptr = ',';
10689
10690         return rc < 0 ? rc : nr;
10691 }
10692
10693 /**
10694  * struct verify_mirror_id - Mirror id to be verified.
10695  * @mirror_id:   A specified mirror id.
10696  * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10697  */
10698 struct verify_mirror_id {
10699         __u16 mirror_id;
10700         bool is_valid_id;
10701 };
10702
10703 /**
10704  * compare_mirror_ids() - Compare mirror ids.
10705  * @layout: Mirror component list.
10706  * @cbdata: Callback data in verify_mirror_id structure.
10707  *
10708  * This is a callback function called by llapi_layout_comp_iterate()
10709  * to compare the specified mirror id with the one in the current
10710  * component of @layout. If they are the same, then the specified
10711  * mirror id is valid.
10712  *
10713  * Return: a negative error code on failure or
10714  *         LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10715  *         LLAPI_LAYOUT_ITER_STOP: Stop iteration
10716  */
10717 static inline
10718 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10719 {
10720         struct verify_mirror_id *mirror_id_cbdata =
10721                                  (struct verify_mirror_id *)cbdata;
10722         uint32_t mirror_id;
10723         int rc = 0;
10724
10725         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10726         if (rc < 0) {
10727                 rc = -errno;
10728                 fprintf(stderr,
10729                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
10730                         progname, strerror(errno));
10731                 return rc;
10732         }
10733
10734         if (mirror_id_cbdata->mirror_id == mirror_id) {
10735                 mirror_id_cbdata->is_valid_id = true;
10736                 return LLAPI_LAYOUT_ITER_STOP;
10737         }
10738
10739         return LLAPI_LAYOUT_ITER_CONT;
10740 }
10741
10742 /**
10743  * verify_mirror_ids() - Verify specified mirror ids.
10744  * @fname:      Mirrored file name.
10745  * @mirror_ids: Specified mirror ids to be verified.
10746  * @ids_nr:     Number of specified mirror ids.
10747  *
10748  * This function verifies that specified @mirror_ids are valid
10749  * in the mirrored file @fname.
10750  *
10751  * Return: 0 on success or a negative error code on failure.
10752  */
10753 static inline
10754 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10755 {
10756         struct llapi_layout *layout = NULL;
10757         struct verify_mirror_id mirror_id_cbdata = { 0 };
10758         struct stat stbuf;
10759         uint32_t flr_state;
10760         int i;
10761         int fd;
10762         int rc = 0;
10763         int rc2 = 0;
10764
10765         if (ids_nr <= 0)
10766                 return -EINVAL;
10767
10768         if (stat(fname, &stbuf) < 0) {
10769                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10770                         progname, fname, strerror(errno));
10771                 rc = -errno;
10772                 goto error;
10773         }
10774
10775         if (!S_ISREG(stbuf.st_mode)) {
10776                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10777                         progname, fname);
10778                 rc = -EINVAL;
10779                 goto error;
10780         }
10781
10782         fd = open(fname, O_DIRECT | O_RDONLY);
10783         if (fd < 0) {
10784                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10785                         progname, fname, strerror(errno));
10786                 rc = -errno;
10787                 goto error;
10788         }
10789
10790         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10791         if (rc < 0) {
10792                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10793                         progname, fname, strerror(errno));
10794                 goto close_fd;
10795         }
10796
10797         layout = llapi_layout_get_by_fd(fd, 0);
10798         if (!layout) {
10799                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10800                         progname, fname, strerror(errno));
10801                 rc = -errno;
10802                 llapi_lease_release(fd);
10803                 goto close_fd;
10804         }
10805
10806         rc = llapi_layout_flags_get(layout, &flr_state);
10807         if (rc < 0) {
10808                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10809                         progname, fname, strerror(errno));
10810                 rc = -errno;
10811                 goto free_layout;
10812         }
10813
10814         flr_state &= LCM_FL_FLR_MASK;
10815         switch (flr_state) {
10816         case LCM_FL_NONE:
10817                 rc = -EINVAL;
10818                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10819                         progname, fname, llapi_layout_flags_string(flr_state));
10820                 goto free_layout;
10821         default:
10822                 break;
10823         }
10824
10825         rc2 = 0;
10826         for (i = 0; i < ids_nr; i++) {
10827                 mirror_id_cbdata.mirror_id = mirror_ids[i];
10828                 mirror_id_cbdata.is_valid_id = false;
10829
10830                 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10831                                                &mirror_id_cbdata);
10832                 if (rc < 0) {
10833                         rc = -errno;
10834                         fprintf(stderr,
10835                                 "%s: '%s' failed to verify mirror id: %u.\n",
10836                                 progname, fname, mirror_ids[i]);
10837                         goto free_layout;
10838                 }
10839
10840                 if (!mirror_id_cbdata.is_valid_id) {
10841                         rc2 = -EINVAL;
10842                         fprintf(stderr,
10843                                 "%s: '%s' invalid specified mirror id: %u.\n",
10844                                 progname, fname, mirror_ids[i]);
10845                 }
10846         }
10847         rc = rc2;
10848
10849 free_layout:
10850         llapi_layout_free(layout);
10851         llapi_lease_release(fd);
10852 close_fd:
10853         close(fd);
10854 error:
10855         return rc;
10856 }
10857
10858 static inline
10859 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10860                            __u16 *mirror_ids, int ids_nr)
10861 {
10862         struct llapi_resync_comp comp_array[1024] = { { 0 } };
10863         struct llapi_layout *layout;
10864         struct stat stbuf;
10865         uint32_t flr_state;
10866         uint64_t start;
10867         uint64_t end;
10868         int comp_size = 0;
10869         int idx;
10870         int fd;
10871         int rc;
10872         int rc2;
10873
10874         if (stat(fname, &stbuf) < 0) {
10875                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10876                         progname, fname, strerror(errno));
10877                 rc = -errno;
10878                 goto error;
10879         }
10880         if (!S_ISREG(stbuf.st_mode)) {
10881                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10882                         progname, fname);
10883                 rc = -EINVAL;
10884                 goto error;
10885         }
10886
10887         /* Allow mirror resync even without the key on encrypted files */
10888         fd = open(fname, O_DIRECT | O_RDWR | O_FILE_ENC);
10889         if (fd < 0) {
10890                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10891                         progname, fname, strerror(errno));
10892                 rc = -errno;
10893                 goto error;
10894         }
10895
10896         layout = llapi_layout_get_by_fd(fd, 0);
10897         if (!layout) {
10898                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10899                         progname, fname, strerror(errno));
10900                 rc = -errno;
10901                 goto close_fd;
10902         }
10903
10904         rc = llapi_layout_flags_get(layout, &flr_state);
10905         if (rc) {
10906                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10907                         progname, fname, strerror(errno));
10908                 rc = -errno;
10909                 goto free_layout;
10910         }
10911
10912         flr_state &= LCM_FL_FLR_MASK;
10913         if (flr_state == LCM_FL_NONE) {
10914                 rc = -EINVAL;
10915                 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10916                         progname, fname);
10917                 goto free_layout;
10918         }
10919
10920         /* get stale component info */
10921         comp_size = llapi_mirror_find_stale(layout, comp_array,
10922                                             ARRAY_SIZE(comp_array),
10923                                             mirror_ids, ids_nr);
10924         if (comp_size <= 0) {
10925                 rc = comp_size;
10926                 goto free_layout;
10927         }
10928
10929         ioc->lil_mode = LL_LEASE_WRLCK;
10930         ioc->lil_flags = LL_LEASE_RESYNC;
10931         rc = llapi_lease_set(fd, ioc);
10932         if (rc < 0) {
10933                 if (rc == -EALREADY)
10934                         rc = 0;
10935                 else
10936                         fprintf(stderr,
10937                             "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10938                                 progname, fname, strerror(-rc));
10939                 goto free_layout;
10940         }
10941
10942         /* get the read range [start, end) */
10943         start = comp_array[0].lrc_start;
10944         end = comp_array[0].lrc_end;
10945         for (idx = 1; idx < comp_size; idx++) {
10946                 if (comp_array[idx].lrc_start < start)
10947                         start = comp_array[idx].lrc_start;
10948                 if (end < comp_array[idx].lrc_end)
10949                         end = comp_array[idx].lrc_end;
10950         }
10951
10952         rc = llapi_lease_check(fd);
10953         if (rc != LL_LEASE_WRLCK) {
10954                 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10955                         progname, fname);
10956                 goto free_layout;
10957         }
10958
10959         rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10960                                       start, end);
10961         if (rc < 0)
10962                 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10963                         progname, fname, strerror(-rc));
10964
10965         rc = migrate_set_timestamps(fd, &stbuf);
10966         if (rc < 0) {
10967                 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
10968                         progname, fname, strerror(-rc));
10969                 goto free_layout;
10970         }
10971
10972         /* need to do the lease unlock even resync fails */
10973         ioc->lil_mode = LL_LEASE_UNLCK;
10974         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10975         ioc->lil_count = 0;
10976         for (idx = 0; idx < comp_size; idx++) {
10977                 if (comp_array[idx].lrc_synced) {
10978                         ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10979                         ioc->lil_count++;
10980                 }
10981         }
10982
10983         rc2 = llapi_lease_set(fd, ioc);
10984         /**
10985          * llapi_lease_set returns lease mode when it request to unlock
10986          * the lease lock.
10987          */
10988         if (rc2 <= 0) {
10989                 /* rc2 == 0 means lost lease lock */
10990                 if (rc2 == 0 && rc == 0)
10991                         rc = -EBUSY;
10992                 else
10993                         rc = rc2;
10994                 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10995                         progname, fname,
10996                         rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10997
10998                 llapi_lease_release(fd);
10999                 goto free_layout;
11000         }
11001
11002 free_layout:
11003         llapi_layout_free(layout);
11004 close_fd:
11005         close(fd);
11006 error:
11007         return rc;
11008 }
11009
11010 static inline int lfs_mirror_resync(int argc, char **argv)
11011 {
11012         struct option long_opts[] = {
11013         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11014         { .val = 'o',   .name = "only",         .has_arg = required_argument },
11015         { .name = NULL } };
11016         struct ll_ioc_lease *ioc = NULL;
11017         __u16 mirror_ids[128] = { 0 };
11018         int ids_nr = 0;
11019         int c;
11020         int rc = 0;
11021
11022         while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
11023                 switch (c) {
11024                 case 'o':
11025                         rc = parse_mirror_ids(mirror_ids,
11026                                         sizeof(mirror_ids) / sizeof(__u16),
11027                                         optarg);
11028                         if (rc < 0) {
11029                                 fprintf(stderr,
11030                                         "%s: bad mirror ids '%s'.\n",
11031                                         argv[0], optarg);
11032                                 goto error;
11033                         }
11034                         ids_nr = rc;
11035                         break;
11036                 default:
11037                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11038                                 progname, argv[optind - 1]);
11039                         fallthrough;
11040                 case 'h':
11041                         rc = CMD_HELP;
11042                         goto error;
11043                 }
11044         }
11045
11046         if (argc == optind) {
11047                 fprintf(stderr, "%s: no file name given.\n", argv[0]);
11048                 rc = CMD_HELP;
11049                 goto error;
11050         }
11051
11052         if (ids_nr > 0 && argc > optind + 1) {
11053                 fprintf(stderr,
11054                     "%s: option '--only' cannot be used upon multiple files.\n",
11055                         argv[0]);
11056                 rc = CMD_HELP;
11057                 goto error;
11058         }
11059
11060         if (ids_nr > 0) {
11061                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11062                 if (rc < 0)
11063                         goto error;
11064         }
11065
11066         /* set the lease on the file */
11067         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11068         if (!ioc) {
11069                 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
11070                         argv[0], strerror(errno));
11071                 rc = -errno;
11072                 goto error;
11073         }
11074
11075         for (; optind < argc; optind++) {
11076                 rc = lfs_mirror_resync_file(argv[optind], ioc,
11077                                             mirror_ids, ids_nr);
11078                 /* ignore previous file's error, continue with next file */
11079
11080                 /* reset ioc */
11081                 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
11082         }
11083
11084         free(ioc);
11085 error:
11086         return rc;
11087 }
11088
11089 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
11090 {
11091         struct llapi_layout *layout;
11092         int rc;
11093
11094         layout = llapi_layout_get_by_fd(fd, 0);
11095         if (!layout) {
11096                 fprintf(stderr, "could not get layout.\n");
11097                 return  -EINVAL;
11098         }
11099
11100         rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
11101         if (rc < 0) {
11102                 fprintf(stderr, "failed to iterate layout\n");
11103                 llapi_layout_free(layout);
11104
11105                 return rc;
11106         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
11107                 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
11108                 llapi_layout_free(layout);
11109
11110                 return -EINVAL;
11111         }
11112         llapi_layout_free(layout);
11113
11114         return 0;
11115 }
11116
11117 /**
11118  * Check whether two files are the same file
11119  * \retval      0  same file
11120  * \retval      1  not the same file
11121  * \retval      <0 error code
11122  */
11123 static inline int check_same_file(int fd, const char *f2)
11124 {
11125         struct stat stbuf1;
11126         struct stat stbuf2;
11127
11128         if (fstat(fd, &stbuf1) < 0)
11129                 return -errno;
11130
11131         if (stat(f2, &stbuf2) < 0)
11132                 return 1;
11133
11134         if (stbuf1.st_rdev == stbuf2.st_rdev &&
11135             stbuf1.st_ino == stbuf2.st_ino)
11136                 return 0;
11137
11138         return 1;
11139 }
11140
11141 static inline int lfs_mirror_read(int argc, char **argv)
11142 {
11143         int rc = CMD_HELP;
11144         __u16 mirror_id = 0;
11145         const char *outfile = NULL;
11146         char *fname;
11147         int fd = 0;
11148         int outfd;
11149         int c;
11150         void *buf;
11151         const size_t buflen = 4 << 20;
11152         off_t pos;
11153         struct option long_opts[] = {
11154         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11155         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
11156         { .val = 'o',   .name = "outfile",      .has_arg = required_argument },
11157         { .name = NULL } };
11158
11159         while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
11160                 char *end;
11161
11162                 switch (c) {
11163                 case 'N': {
11164                         unsigned long int id;
11165
11166                         errno = 0;
11167                         id = strtoul(optarg, &end, 0);
11168                         if (errno != 0 || *end != '\0' || id == 0 ||
11169                             id > UINT16_MAX) {
11170                                 fprintf(stderr,
11171                                         "%s %s: invalid mirror ID '%s'\n",
11172                                         progname, argv[0], optarg);
11173                                 return rc;
11174                         }
11175
11176                         mirror_id = (__u16)id;
11177                         break;
11178                 }
11179                 case 'o':
11180                         outfile = optarg;
11181                         break;
11182                 default:
11183                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11184                                 progname, argv[optind - 1]);
11185                         fallthrough;
11186                 case 'h':
11187                         return CMD_HELP;
11188                 }
11189         }
11190
11191         if (argc == optind) {
11192                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11193                         progname, argv[0]);
11194                 return rc;
11195         } else if (argc > optind + 1) {
11196                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11197                 return rc;
11198         }
11199
11200         if (mirror_id == 0) {
11201                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11202                         progname, argv[0]);
11203                 return rc;
11204         }
11205
11206         /* open mirror file */
11207         fname = argv[optind];
11208         fd = open(fname, O_DIRECT | O_RDONLY);
11209         if (fd < 0) {
11210                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11211                         progname, argv[0], fname, strerror(errno));
11212                 return rc;
11213         }
11214
11215         /* verify mirror id */
11216         rc = verify_mirror_id_by_fd(fd, mirror_id);
11217         if (rc) {
11218                 fprintf(stderr,
11219                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11220                         progname, argv[0], mirror_id, fname);
11221                 goto close_fd;
11222         }
11223
11224         /* open output file - O_EXCL ensures output is not the same as input */
11225         if (outfile) {
11226                 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
11227                 if (outfd < 0) {
11228                         fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
11229                                 progname, argv[0], outfile, strerror(errno));
11230                         rc = -errno;
11231                         goto close_fd;
11232                 }
11233         } else {
11234                 outfd = STDOUT_FILENO;
11235         }
11236
11237         /* allocate buffer */
11238         rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
11239         if (rc) {
11240                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11241                                 progname, argv[0], rc);
11242                 goto close_outfd;
11243         }
11244
11245         pos = 0;
11246         while (1) {
11247                 ssize_t bytes_read;
11248                 ssize_t written = 0;
11249
11250                 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
11251                 if (bytes_read < 0) {
11252                         rc = bytes_read;
11253                         fprintf(stderr,
11254                                 "%s %s: fail to read data from mirror %u: %s\n",
11255                                 progname, argv[0], mirror_id, strerror(-rc));
11256                         goto free_buf;
11257                 }
11258
11259                 /* EOF reached */
11260                 if (bytes_read == 0)
11261                         break;
11262
11263                 while (written < bytes_read) {
11264                         ssize_t written2;
11265
11266                         written2 = write(outfd, buf + written,
11267                                          bytes_read - written);
11268                         if (written2 < 0) {
11269                                 fprintf(stderr,
11270                                         "%s %s: fail to write %s: %s\n",
11271                                         progname, argv[0], outfile ? : "STDOUT",
11272                                         strerror(errno));
11273                                 rc = -errno;
11274                                 goto free_buf;
11275                         }
11276                         written += written2;
11277                 }
11278
11279                 if (written != bytes_read) {
11280                         fprintf(stderr,
11281                 "%s %s: written %ld bytes does not match with %ld read.\n",
11282                                 progname, argv[0], written, bytes_read);
11283                         rc = -EIO;
11284                         goto free_buf;
11285                 }
11286
11287                 pos += bytes_read;
11288         }
11289
11290         fsync(outfd);
11291         rc = 0;
11292
11293 free_buf:
11294         free(buf);
11295 close_outfd:
11296         if (outfile)
11297                 close(outfd);
11298 close_fd:
11299         close(fd);
11300
11301         return rc;
11302 }
11303
11304 static inline int lfs_mirror_write(int argc, char **argv)
11305 {
11306         int rc = CMD_HELP;
11307         __u16 mirror_id = 0;
11308         const char *inputfile = NULL;
11309         char *fname;
11310         int fd = 0;
11311         int inputfd;
11312         int c;
11313         void *buf;
11314         const size_t buflen = 4 << 20;
11315         off_t pos;
11316         size_t page_size = sysconf(_SC_PAGESIZE);
11317         struct ll_ioc_lease_id ioc;
11318         struct option long_opts[] = {
11319         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11320         { .val = 'i',   .name = "inputfile",    .has_arg = required_argument },
11321         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
11322         { .name = NULL } };
11323
11324         while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11325                 char *end;
11326
11327                 switch (c) {
11328                 case 'N': {
11329                         unsigned long int id;
11330
11331                         errno = 0;
11332                         id = strtoul(optarg, &end, 0);
11333                         if (errno != 0 || *end != '\0' || id == 0 ||
11334                             id > UINT16_MAX) {
11335                                 fprintf(stderr,
11336                                         "%s %s: invalid mirror ID '%s'\n",
11337                                         progname, argv[0], optarg);
11338                                 return rc;
11339                         }
11340
11341                         mirror_id = (__u16)id;
11342                         break;
11343                 }
11344                 case 'i':
11345                         inputfile = optarg;
11346                         break;
11347                 default:
11348                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11349                                 progname, argv[optind - 1]);
11350                         fallthrough;
11351                 case 'h':
11352                         return CMD_HELP;
11353                 }
11354         }
11355
11356         if (argc == optind) {
11357                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11358                         progname, argv[0]);
11359                 return rc;
11360         } else if (argc > optind + 1) {
11361                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11362                 return rc;
11363         }
11364
11365         if (mirror_id == 0) {
11366                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11367                         progname, argv[0]);
11368                 return rc;
11369         }
11370
11371         /* open mirror file */
11372         fname = argv[optind];
11373         fd = open(fname, O_DIRECT | O_WRONLY);
11374         if (fd < 0) {
11375                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11376                         progname, argv[0], fname, strerror(errno));
11377                 return rc;
11378         }
11379
11380         /* verify mirror id */
11381         rc = verify_mirror_id_by_fd(fd, mirror_id);
11382         if (rc) {
11383                 fprintf(stderr,
11384                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11385                         progname, argv[0], mirror_id, fname);
11386                 goto close_fd;
11387         }
11388
11389         /* open input file */
11390         if (inputfile) {
11391                 rc = check_same_file(fd, inputfile);
11392                 if (rc == 0) {
11393                         fprintf(stderr,
11394                         "%s %s: input file cannot be the mirrored file\n",
11395                                 progname, argv[0]);
11396                         goto close_fd;
11397                 }
11398                 if (rc < 0)
11399                         goto close_fd;
11400
11401                 inputfd = open(inputfile, O_RDONLY, 0644);
11402                 if (inputfd < 0) {
11403                         fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11404                                 progname, argv[0], inputfile, strerror(errno));
11405                         rc = -errno;
11406                         goto close_fd;
11407                 }
11408         } else {
11409                 inputfd = STDIN_FILENO;
11410         }
11411
11412         /* allocate buffer */
11413         rc = posix_memalign(&buf, page_size, buflen);
11414         if (rc) {
11415                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11416                         progname, argv[0], rc);
11417                 goto close_inputfd;
11418         }
11419
11420         /* prepare target mirror components instantiation */
11421         ioc.lil_mode = LL_LEASE_WRLCK;
11422         ioc.lil_flags = LL_LEASE_RESYNC;
11423         ioc.lil_mirror_id = mirror_id;
11424         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11425         if (rc < 0) {
11426                 fprintf(stderr,
11427                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11428                         progname, argv[0], fname, strerror(errno));
11429                 goto free_buf;
11430         }
11431
11432         pos = 0;
11433         while (1) {
11434                 ssize_t bytes_read;
11435                 ssize_t written;
11436                 size_t to_write;
11437
11438                 rc = llapi_lease_check(fd);
11439                 if (rc != LL_LEASE_WRLCK) {
11440                         fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11441                                 progname, argv[0], fname);
11442                         goto free_buf;
11443                 }
11444
11445                 bytes_read = read(inputfd, buf, buflen);
11446                 if (bytes_read < 0) {
11447                         rc = bytes_read;
11448                         fprintf(stderr,
11449                                 "%s %s: fail to read data from '%s': %s\n",
11450                                 progname, argv[0], inputfile ? : "STDIN",
11451                                 strerror(errno));
11452                         rc = -errno;
11453                         goto free_buf;
11454                 }
11455
11456                 /* EOF reached */
11457                 if (bytes_read == 0)
11458                         break;
11459
11460                 /* round up to page align to make direct IO happy. */
11461                 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11462
11463                 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11464                                              pos);
11465                 if (written < 0) {
11466                         rc = written;
11467                         fprintf(stderr,
11468                               "%s %s: fail to write to mirror %u: %s\n",
11469                                 progname, argv[0], mirror_id,
11470                                 strerror(-rc));
11471                         goto free_buf;
11472                 }
11473
11474                 pos += bytes_read;
11475         }
11476
11477         if (pos & (page_size - 1)) {
11478                 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11479                 if (rc < 0)
11480                         goto free_buf;
11481         }
11482
11483         ioc.lil_mode = LL_LEASE_UNLCK;
11484         ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11485         ioc.lil_count = 0;
11486         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11487         if (rc <= 0) {
11488                 if (rc == 0)
11489                         rc = -EBUSY;
11490                 fprintf(stderr,
11491                         "%s %s: release lease lock of '%s' failed: %s\n",
11492                         progname, argv[0], fname, strerror(errno));
11493                 goto free_buf;
11494         }
11495
11496         rc = 0;
11497
11498 free_buf:
11499         free(buf);
11500 close_inputfd:
11501         if (inputfile)
11502                 close(inputfd);
11503 close_fd:
11504         close(fd);
11505
11506         return rc;
11507 }
11508
11509 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11510 {
11511         struct llapi_layout *layout;
11512         struct collect_ids_data cid = { .cid_ids = ids,
11513                                         .cid_count = 0,
11514                                         .cid_exclude = exclude_id, };
11515         int rc;
11516
11517         layout = llapi_layout_get_by_fd(fd, 0);
11518         if (!layout) {
11519                 fprintf(stderr, "could not get layout\n");
11520                 return -EINVAL;
11521         }
11522
11523         rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11524         if (rc < 0) {
11525                 fprintf(stderr, "failed to iterate layout\n");
11526                 llapi_layout_free(layout);
11527
11528                 return rc;
11529         }
11530         llapi_layout_free(layout);
11531
11532         return cid.cid_count;
11533 }
11534
11535 #ifndef MIRROR_ID_NEG
11536 #define MIRROR_ID_NEG         0x8000
11537 #endif
11538
11539 static inline int lfs_mirror_copy(int argc, char **argv)
11540 {
11541         int rc = CMD_HELP;
11542         __u16 read_mirror_id = 0;
11543         __u16 ids[128] = { 0 };
11544         int count = 0;
11545         struct llapi_layout *layout = NULL;
11546         struct llapi_resync_comp comp_array[1024] = { { 0 } };
11547         int comp_size = 0;
11548         char *fname;
11549         int fd = 0;
11550         int c;
11551         int i;
11552         ssize_t copied;
11553         struct ll_ioc_lease *ioc = NULL;
11554         struct ll_ioc_lease_id *resync_ioc;
11555         struct option long_opts[] = {
11556         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11557         { .val = 'i',   .name = "read-mirror",  .has_arg = required_argument },
11558         { .val = 'o',   .name = "write-mirror", .has_arg = required_argument },
11559         { .name = NULL } };
11560         char cmd[PATH_MAX];
11561
11562         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11563         progname = cmd;
11564         while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11565                 char *end;
11566
11567                 switch (c) {
11568                 case 'i': {
11569                         unsigned long int id;
11570
11571                         errno = 0;
11572                         id = strtoul(optarg, &end, 0);
11573                         if (errno != 0 || *end != '\0' || id == 0 ||
11574                             id > UINT16_MAX) {
11575                                 fprintf(stderr,
11576                                         "%s: invalid read mirror ID '%s'\n",
11577                                         progname, optarg);
11578                                 return rc;
11579                         }
11580
11581                         read_mirror_id = (__u16)id;
11582                         break;
11583                 }
11584                 case 'o':
11585                         if (!strcmp(optarg, "-1")) {
11586                                 /* specify all other mirrors */
11587                                 ids[0] = (__u16)-1;
11588                                 count = 1;
11589                         } else {
11590                                 count = parse_mirror_ids((__u16 *)ids,
11591                                                          ARRAY_SIZE(ids),
11592                                                          optarg);
11593                                 if (count < 0)
11594                                         return rc;
11595                         }
11596                         break;
11597                 default:
11598                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11599                                 progname, argv[optind - 1]);
11600                         fallthrough;
11601                 case 'h':
11602                         return CMD_HELP;
11603                 }
11604         }
11605
11606         if (argc == optind) {
11607                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11608                         progname, argv[0]);
11609                 return rc;
11610         } else if (argc > optind + 1) {
11611                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11612                 return rc;
11613         }
11614
11615         if (read_mirror_id == 0) {
11616                 fprintf(stderr,
11617                         "%s %s: no valid read mirror ID %d is provided\n",
11618                         progname, argv[0], read_mirror_id);
11619                 return rc;
11620         }
11621
11622         if (count == 0) {
11623                 fprintf(stderr,
11624                         "%s %s: no write mirror ID is provided\n",
11625                         progname, argv[0]);
11626                 return rc;
11627         }
11628
11629         for (i = 0; i < count; i++) {
11630                 if (read_mirror_id == ids[i]) {
11631                         fprintf(stderr,
11632                         "%s %s: read and write mirror ID cannot be the same\n",
11633                                 progname, argv[0]);
11634                         return rc;
11635                 }
11636         }
11637
11638         /* open mirror file */
11639         fname = argv[optind];
11640
11641         fd = open(fname, O_DIRECT | O_RDWR);
11642         if (fd < 0) {
11643                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11644                         progname, argv[0], fname, strerror(errno));
11645                 return rc;
11646         }
11647
11648         /* write to all other mirrors */
11649         if (ids[0] == (__u16)-1) {
11650                 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11651                 if (count <= 0) {
11652                         rc = count;
11653                         fprintf(stderr,
11654                         "%s %s: failed to get other mirror ids in '%s': %d\n",
11655                                 progname, argv[0], fname, rc);
11656                         goto close_fd;
11657                 }
11658         }
11659
11660         /* verify mirror id */
11661         rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11662         if (rc) {
11663                 fprintf(stderr,
11664                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11665                         progname, argv[0], read_mirror_id, fname);
11666                 goto close_fd;
11667         }
11668
11669         for (i = 0; i < count; i++) {
11670                 rc = verify_mirror_id_by_fd(fd, ids[i]);
11671                 if (rc) {
11672                         fprintf(stderr,
11673                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11674                                 progname, argv[0], ids[i], fname);
11675                         goto close_fd;
11676                 }
11677         }
11678
11679         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11680         if (!ioc) {
11681                 fprintf(stderr,
11682                         "%s %s: cannot alloc comp id array for ioc: %s\n",
11683                         progname, argv[0], strerror(errno));
11684                 rc = -errno;
11685                 goto close_fd;
11686         }
11687
11688         /* get stale component info */
11689         layout = llapi_layout_get_by_fd(fd, 0);
11690         if (!layout) {
11691                 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11692                         progname, argv[0], fname, strerror(errno));
11693                 rc = -errno;
11694                 goto free_ioc;
11695         }
11696         comp_size = llapi_mirror_find_stale(layout, comp_array,
11697                                             ARRAY_SIZE(comp_array),
11698                                             ids, count);
11699         llapi_layout_free(layout);
11700         if (comp_size < 0) {
11701                 rc = comp_size;
11702                 goto free_ioc;
11703         }
11704
11705         /* prepare target mirror components instantiation */
11706         resync_ioc = (struct ll_ioc_lease_id *)ioc;
11707         resync_ioc->lil_mode = LL_LEASE_WRLCK;
11708         resync_ioc->lil_flags = LL_LEASE_RESYNC;
11709         if (count == 1)
11710                 resync_ioc->lil_mirror_id = ids[0];
11711         else
11712                 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11713         rc = llapi_lease_set(fd, ioc);
11714         if (rc < 0) {
11715                 fprintf(stderr,
11716                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11717                         progname, argv[0], fname, strerror(errno));
11718                 goto free_ioc;
11719         }
11720
11721         copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11722         if (copied < 0) {
11723                 rc = copied;
11724                 fprintf(stderr, "%s %s: copy error: %d\n",
11725                         progname, argv[0], rc);
11726                 goto free_ioc;
11727         }
11728
11729         fprintf(stdout, "mirror copied successfully: ");
11730         for (i = 0; i < copied; i++)
11731                 fprintf(stdout, "%d ", ids[i]);
11732         fprintf(stdout, "\n");
11733
11734         ioc->lil_mode = LL_LEASE_UNLCK;
11735         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11736         ioc->lil_count = 0;
11737         for (i = 0; i < comp_size; i++) {
11738                 int j;
11739
11740                 for (j = 0; j < copied; j++) {
11741                         if (comp_array[i].lrc_mirror_id != ids[j])
11742                                 continue;
11743
11744                         ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11745                         ioc->lil_count++;
11746                 }
11747         }
11748         rc = llapi_lease_set(fd, ioc);
11749         if (rc <= 0) {
11750                 if (rc == 0)
11751                         rc = -EBUSY;
11752                 fprintf(stderr,
11753                         "%s %s: release lease lock of '%s' failed: %s\n",
11754                         progname, argv[0], fname, strerror(errno));
11755                 goto free_ioc;
11756         }
11757
11758         rc = 0;
11759
11760 free_ioc:
11761         free(ioc);
11762 close_fd:
11763         close(fd);
11764
11765         return rc;
11766 }
11767
11768 /**
11769  * struct verify_chunk - Mirror chunk to be verified.
11770  * @chunk:        [start, end) of the chunk.
11771  * @mirror_count: Number of mirror ids in @mirror_id array.
11772  * @mirror_id:    Array of valid mirror ids that cover the chunk.
11773  */
11774 struct verify_chunk {
11775         struct lu_extent chunk;
11776         unsigned int mirror_count;
11777         __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11778 };
11779
11780 /**
11781  * print_chunks() - Print chunk information.
11782  * @fname:       Mirrored file name.
11783  * @chunks:      Array of chunks.
11784  * @chunk_count: Number of chunks in @chunks array.
11785  *
11786  * This function prints [start, end) of each chunk in @chunks
11787  * for mirrored file @fname, and also prints the valid mirror ids
11788  * that cover the chunk.
11789  *
11790  * Return: void.
11791  */
11792 static inline
11793 void print_chunks(const char *fname, struct verify_chunk *chunks,
11794                   int chunk_count)
11795 {
11796         int i;
11797         int j;
11798
11799         fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11800         for (i = 0; i < chunk_count; i++) {
11801                 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11802
11803                 if (chunks[i].mirror_count == 0)
11804                         fprintf(stdout, "\t[");
11805                 else {
11806                         fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11807                         for (j = 1; j < chunks[i].mirror_count; j++)
11808                                 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11809                 }
11810                 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11811         }
11812         fprintf(stdout, "\n");
11813 }
11814
11815 /**
11816  * print_checksums() - Print CRC-32 checksum values.
11817  * @chunk: A chunk and its corresponding valid mirror ids.
11818  * @crc:   CRC-32 checksum values on the chunk for each valid mirror.
11819  *
11820  * This function prints CRC-32 checksum values on @chunk for
11821  * each valid mirror that covers it.
11822  *
11823  * Return: void.
11824  */
11825 static inline
11826 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
11827                      unsigned long long pos, unsigned long long len)
11828 {
11829         int i;
11830
11831         fprintf(stdout,
11832                 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
11833         for (i = 0; i < chunk->mirror_count; i++)
11834                 fprintf(stdout, "Mirror %u:\t%#lx\n",
11835                         chunk->mirror_id[i], crc[i]);
11836         fprintf(stdout, "\n");
11837 }
11838
11839 /**
11840  * filter_mirror_id() - Filter specified mirror ids.
11841  * @chunks:      Array of chunks.
11842  * @chunk_count: Number of chunks in @chunks array.
11843  * @mirror_ids:  Specified mirror ids to be verified.
11844  * @ids_nr:      Number of specified mirror ids.
11845  *
11846  * This function scans valid mirror ids that cover each chunk in @chunks
11847  * and filters specified mirror ids.
11848  *
11849  * Return: void.
11850  */
11851 static inline
11852 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11853                       __u16 *mirror_ids, int ids_nr)
11854 {
11855         int i;
11856         int j;
11857         int k;
11858         __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11859         unsigned int valid_count = 0;
11860
11861         for (i = 0; i < chunk_count; i++) {
11862                 if (chunks[i].mirror_count == 0)
11863                         continue;
11864
11865                 valid_count = 0;
11866                 for (j = 0; j < ids_nr; j++) {
11867                         for (k = 0; k < chunks[i].mirror_count; k++) {
11868                                 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11869                                         valid_id[valid_count] = mirror_ids[j];
11870                                         valid_count++;
11871                                         break;
11872                                 }
11873                         }
11874                 }
11875
11876                 memcpy(chunks[i].mirror_id, valid_id,
11877                        sizeof(__u16) * valid_count);
11878                 chunks[i].mirror_count = valid_count;
11879         }
11880 }
11881
11882 /**
11883  * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11884  * @layout:      Mirror component list.
11885  * @chunks:      Array of chunks.
11886  * @chunks_size: Array size of @chunks.
11887  *
11888  * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11889  * to find out chunk segments and store them in @chunks array.
11890  *
11891  * The @mirror_id array in each element of @chunks will store the valid
11892  * mirror ids that cover the chunk. If a mirror component covering the
11893  * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11894  * will not be stored into the @mirror_id array, and the chunk for that
11895  * mirror will not be verified.
11896  *
11897  * The @mirror_count in each element of @chunks will store the number of
11898  * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11899  * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11900  * indicates the chunk is valid in only one mirror. In both cases, the
11901  * chunk will not be verified.
11902  *
11903  * Here is an example:
11904  *
11905  *  0      1M     2M     3M     4M           EOF
11906  *  +------+-------------+--------------------+
11907  *  |      |             |      S             |       mirror1
11908  *  +------+------+------+------+-------------+
11909  *  |             |   S  |   S  |             |       mirror2
11910  *  +-------------+------+------+-------------+
11911  *
11912  * prepared @chunks array will contain 5 elements:
11913  * (([0, 1M), [1, 2], 2),
11914  *  ([1M, 2M), [1, 2], 2),
11915  *  ([2M, 3M), [1], 1),
11916  *  ([3M, 4M], [], 0),
11917  *  ([4M, EOF), [2], 1))
11918  *
11919  * Return: the actual array size of @chunks on success
11920  *         or a negative error code on failure.
11921  */
11922 static inline
11923 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11924                              struct verify_chunk *chunks,
11925                              size_t chunks_size)
11926 {
11927         uint64_t start;
11928         uint64_t end;
11929         uint32_t mirror_id;
11930         uint32_t flags;
11931         int idx = 0;
11932         int i = 0;
11933         int rc = 0;
11934
11935         memset(chunks, 0, sizeof(*chunks) * chunks_size);
11936
11937         while (1) {
11938                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11939                 if (rc < 0) {
11940                         fprintf(stderr,
11941                                 "%s: move to the first layout component: %s.\n",
11942                                 progname, strerror(errno));
11943                         goto error;
11944                 }
11945
11946                 i = 0;
11947                 rc = 0;
11948                 chunks[idx].chunk.e_end = LUSTRE_EOF;
11949                 while (rc == 0) {
11950                         rc = llapi_layout_comp_extent_get(layout, &start, &end);
11951                         if (rc < 0) {
11952                                 fprintf(stderr,
11953                                         "%s: llapi_layout_comp_extent_get failed: %s.\n",
11954                                         progname, strerror(errno));
11955                                 goto error;
11956                         }
11957
11958                         if (start > chunks[idx].chunk.e_start ||
11959                             end <= chunks[idx].chunk.e_start)
11960                                 goto next;
11961
11962                         if (end < chunks[idx].chunk.e_end)
11963                                 chunks[idx].chunk.e_end = end;
11964
11965                         rc = llapi_layout_comp_flags_get(layout, &flags);
11966                         if (rc < 0) {
11967                                 fprintf(stderr,
11968                                         "%s: llapi_layout_comp_flags_get failed: %s.\n",
11969                                         progname, strerror(errno));
11970                                 goto error;
11971                         }
11972
11973                         if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11974                                 goto next;
11975
11976                         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11977                         if (rc < 0) {
11978                                 fprintf(stderr,
11979                                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
11980                                         progname, strerror(errno));
11981                                 goto error;
11982                         }
11983
11984                         if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11985                                 fprintf(stderr,
11986                                         "%s: mirror_id array is too small.\n",
11987                                         progname);
11988                                 rc = -EINVAL;
11989                                 goto error;
11990                         }
11991                         chunks[idx].mirror_id[i] = mirror_id;
11992                         i++;
11993
11994 next:
11995                         rc = llapi_layout_comp_use(layout,
11996                                                    LLAPI_LAYOUT_COMP_USE_NEXT);
11997                         if (rc < 0) {
11998                                 fprintf(stderr,
11999                                         "%s: move to the next layout component: %s.\n",
12000                                         progname, strerror(errno));
12001                                 goto error;
12002                         }
12003                 } /* loop through all components */
12004
12005                 chunks[idx].mirror_count = i;
12006
12007                 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
12008                         break;
12009
12010                 idx++;
12011                 if (idx >= chunks_size) {
12012                         fprintf(stderr, "%s: chunks array is too small.\n",
12013                                 progname);
12014                         rc = -EINVAL;
12015                         goto error;
12016                 }
12017
12018                 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
12019         }
12020
12021 error:
12022         return rc < 0 ? rc : idx + 1;
12023 }
12024
12025 /**
12026  * lfs_mirror_verify_chunk() - Verify a chunk.
12027  * @fd:        File descriptor of the mirrored file.
12028  * @file_size: Size of the mirrored file.
12029  * @chunk:     A chunk and its corresponding valid mirror ids.
12030  * @verbose:   Verbose mode.
12031  *
12032  * This function verifies a @chunk contains exactly the same data
12033  * ammong the mirrors that cover it.
12034  *
12035  * If @verbose is specified, then the function will print where the
12036  * differences are if the data do not match. Otherwise, it will
12037  * just return an error in that case.
12038  *
12039  * Return: 0 on success or a negative error code on failure.
12040  */
12041 static inline
12042 int lfs_mirror_verify_chunk(int fd, size_t file_size,
12043                             struct verify_chunk *chunk, int verbose)
12044 {
12045         const size_t buflen = 4 * 1024 * 1024; /* 4M */
12046         void *buf;
12047         size_t page_size = sysconf(_SC_PAGESIZE);
12048         ssize_t bytes_read;
12049         ssize_t bytes_done;
12050         size_t count;
12051         off_t pos;
12052         unsigned long crc;
12053         unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12054         int i;
12055         int rc = 0;
12056
12057         if (file_size == 0)
12058                 return 0;
12059
12060         rc = posix_memalign(&buf, page_size, buflen);
12061         if (rc) /* error code is returned directly */
12062                 return -rc;
12063
12064         if (verbose > 1) {
12065                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12066                         PEXT(&chunk->chunk));
12067                 for (i = 0; i < chunk->mirror_count; i++)
12068                         fprintf(stdout, " %u", chunk->mirror_id[i]);
12069                 fprintf(stdout, "\n");
12070         }
12071
12072         bytes_done = 0;
12073         count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
12074         pos = chunk->chunk.e_start;
12075         while (bytes_done < count) {
12076                 /* compute initial CRC-32 checksum */
12077                 crc = crc32(0L, Z_NULL, 0);
12078                 memset(crc_array, 0, sizeof(crc_array));
12079
12080                 bytes_read = 0;
12081                 for (i = 0; i < chunk->mirror_count; i++) {
12082                         bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
12083                                                        buf, buflen, pos);
12084                         if (bytes_read < 0) {
12085                                 rc = bytes_read;
12086                                 fprintf(stderr,
12087                                         "%s: failed to read data from mirror %u: %s.\n",
12088                                         progname, chunk->mirror_id[i],
12089                                         strerror(-rc));
12090                                 goto error;
12091                         }
12092
12093                         /* compute new CRC-32 checksum */
12094                         crc_array[i] = crc32(crc, buf, bytes_read);
12095                 }
12096
12097                 if (verbose)
12098                         print_checksums(chunk, crc_array, pos, buflen);
12099
12100                 /* compare CRC-32 checksum values */
12101                 for (i = 1; i < chunk->mirror_count; i++) {
12102                         if (crc_array[i] != crc_array[0]) {
12103                                 rc = -EINVAL;
12104
12105                                 fprintf(stderr,
12106                                         "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
12107                                         progname, PEXT(&chunk->chunk),
12108                                         chunk->mirror_id[0],
12109                                         chunk->mirror_id[i]);
12110                         }
12111                 }
12112
12113                 pos += bytes_read;
12114                 bytes_done += bytes_read;
12115         }
12116
12117         if (verbose > 1 && rc == 0) {
12118                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12119                         PEXT(&chunk->chunk));
12120                 for (i = 0; i < chunk->mirror_count; i++)
12121                         fprintf(stdout, " %u", chunk->mirror_id[i]);
12122                 fprintf(stdout, " PASS\n\n");
12123         }
12124
12125 error:
12126         free(buf);
12127         return rc;
12128 }
12129
12130 /**
12131  * lfs_mirror_verify_file() - Verify a mirrored file.
12132  * @fname:      Mirrored file name.
12133  * @mirror_ids: Specified mirror ids to be verified.
12134  * @ids_nr:     Number of specified mirror ids.
12135  * @verbose:    Verbose mode.
12136  *
12137  * This function verifies that each SYNC mirror of a mirrored file
12138  * specified by @fname contains exactly the same data.
12139  *
12140  * If @mirror_ids is specified, then the function will verify the
12141  * mirrors specified by @mirror_ids contain exactly the same data.
12142  *
12143  * If @verbose is specified, then the function will print where the
12144  * differences are if the data do not match. Otherwise, it will
12145  * just return an error in that case.
12146  *
12147  * Return: 0 on success or a negative error code on failure.
12148  */
12149 static inline
12150 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
12151                            int verbose)
12152 {
12153         struct verify_chunk chunks_array[1024] = { };
12154         struct llapi_layout *layout = NULL;
12155         struct stat stbuf;
12156         uint32_t flr_state;
12157         int fd;
12158         int chunk_count = 0;
12159         int idx = 0;
12160         int rc = 0;
12161         int rc1 = 0;
12162         int rc2 = 0;
12163
12164         if (stat(fname, &stbuf) < 0) {
12165                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
12166                         progname, fname, strerror(errno));
12167                 rc = -errno;
12168                 goto error;
12169         }
12170
12171         if (!S_ISREG(stbuf.st_mode)) {
12172                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
12173                         progname, fname);
12174                 rc = -EINVAL;
12175                 goto error;
12176         }
12177
12178         if (stbuf.st_size == 0) {
12179                 if (verbose)
12180                         fprintf(stdout, "%s: '%s' file size is 0.\n",
12181                                 progname, fname);
12182                 rc = 0;
12183                 goto error;
12184         }
12185
12186         /* Allow mirror verify even without the key on encrypted files */
12187         fd = open(fname, O_DIRECT | O_RDONLY | O_FILE_ENC);
12188         if (fd < 0) {
12189                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
12190                         progname, fname, strerror(errno));
12191                 rc = -errno;
12192                 goto error;
12193         }
12194
12195         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
12196         if (rc < 0) {
12197                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
12198                         progname, fname, strerror(errno));
12199                 goto close_fd;
12200         }
12201
12202         layout = llapi_layout_get_by_fd(fd, 0);
12203         if (!layout) {
12204                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
12205                         progname, fname, strerror(errno));
12206                 rc = -errno;
12207                 llapi_lease_release(fd);
12208                 goto close_fd;
12209         }
12210
12211         rc = llapi_layout_flags_get(layout, &flr_state);
12212         if (rc < 0) {
12213                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
12214                         progname, fname, strerror(errno));
12215                 rc = -errno;
12216                 goto free_layout;
12217         }
12218
12219         flr_state &= LCM_FL_FLR_MASK;
12220         switch (flr_state) {
12221         case LCM_FL_NONE:
12222                 rc = -EINVAL;
12223                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
12224                         progname, fname, llapi_layout_flags_string(flr_state));
12225                 goto free_layout;
12226         default:
12227                 break;
12228         }
12229
12230         /* find out mirror chunks to be verified */
12231         chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
12232                                                ARRAY_SIZE(chunks_array));
12233         if (chunk_count < 0) {
12234                 rc = chunk_count;
12235                 goto free_layout;
12236         }
12237
12238         if (ids_nr > 0)
12239                 /* filter specified mirror ids */
12240                 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
12241
12242         if (verbose > 2)
12243                 print_chunks(fname, chunks_array, chunk_count);
12244
12245         for (idx = 0; idx < chunk_count; idx++) {
12246                 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
12247                         if (verbose)
12248                                 fprintf(stdout,
12249                                         "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
12250                                         progname, fname,
12251                                         PEXT(&chunks_array[idx].chunk),
12252                                         (unsigned long long)stbuf.st_size);
12253                         break;
12254                 }
12255
12256                 if (chunks_array[idx].mirror_count == 0) {
12257                         fprintf(stderr,
12258                                 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12259                                 progname, fname,
12260                                 PEXT(&chunks_array[idx].chunk));
12261                         if (verbose) {
12262                                 fprintf(stderr, "skipped\n");
12263                                 continue;
12264                         }
12265                         rc = -EINVAL;
12266                         fprintf(stderr, "failed\n");
12267                         goto free_layout;
12268                 }
12269
12270                 if (chunks_array[idx].mirror_count == 1) {
12271                         if (verbose)
12272                                 fprintf(stdout,
12273                                         "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12274                                         progname, fname,
12275                                         PEXT(&chunks_array[idx].chunk),
12276                                         chunks_array[idx].mirror_id[0]);
12277                         continue;
12278                 }
12279
12280                 rc = llapi_lease_check(fd);
12281                 if (rc != LL_LEASE_RDLCK) {
12282                         fprintf(stderr, "%s: '%s' lost lease lock.\n",
12283                                 progname, fname);
12284                         goto free_layout;
12285                 }
12286
12287                 /* verify one chunk */
12288                 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12289                                               &chunks_array[idx], verbose);
12290                 if (rc1 < 0) {
12291                         rc2 = rc1;
12292                         if (!verbose) {
12293                                 rc = rc1;
12294                                 goto free_layout;
12295                         }
12296                 }
12297         }
12298
12299         if (rc2 < 0)
12300                 rc = rc2;
12301
12302 free_layout:
12303         llapi_layout_free(layout);
12304         llapi_lease_release(fd);
12305 close_fd:
12306         close(fd);
12307 error:
12308         return rc;
12309 }
12310
12311 /**
12312  * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12313  * @argc: The count of lfs mirror verify command line arguments.
12314  * @argv: Array of strings for lfs mirror verify command line arguments.
12315  *
12316  * This function parses lfs mirror verify command and verifies the
12317  * specified mirrored file(s).
12318  *
12319  * Return: 0 on success or a negative error code on failure.
12320  */
12321 static inline int lfs_mirror_verify(int argc, char **argv)
12322 {
12323         __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12324         int ids_nr = 0;
12325         int c;
12326         int verbose = 0;
12327         int rc = 0;
12328         int rc1 = 0;
12329         char cmd[PATH_MAX];
12330
12331         struct option long_opts[] = {
12332         { .val = 'h',   .name = "help",         .has_arg = no_argument },
12333         { .val = 'o',   .name = "only",         .has_arg = required_argument },
12334         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
12335         { .name = NULL } };
12336
12337         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12338         progname = cmd;
12339         while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12340                 switch (c) {
12341                 case 'o':
12342                         rc = parse_mirror_ids(mirror_ids,
12343                                               ARRAY_SIZE(mirror_ids),
12344                                               optarg);
12345                         if (rc < 0) {
12346                                 fprintf(stderr,
12347                                         "%s: bad mirror ids '%s'.\n",
12348                                         progname, optarg);
12349                                 goto error;
12350                         }
12351                         ids_nr = rc;
12352                         if (ids_nr < 2) {
12353                                 fprintf(stderr,
12354                                         "%s: at least 2 mirror ids needed with '--only' option.\n",
12355                                         progname);
12356                                 rc = CMD_HELP;
12357                                 goto error;
12358                         }
12359                         break;
12360                 case 'v':
12361                         verbose++;
12362                         break;
12363                 default:
12364                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12365                                 progname, argv[optind - 1]);
12366                         fallthrough;
12367                 case 'h':
12368                         rc = CMD_HELP;
12369                         goto error;
12370                 }
12371         }
12372
12373         if (argc == optind) {
12374                 fprintf(stderr, "%s: no file name given.\n", progname);
12375                 rc = CMD_HELP;
12376                 goto error;
12377         }
12378
12379         if (ids_nr > 0 && argc > optind + 1) {
12380                 fprintf(stderr,
12381                         "%s: '--only' cannot be used upon multiple files.\n",
12382                         progname);
12383                 rc = CMD_HELP;
12384                 goto error;
12385         }
12386
12387         if (ids_nr > 0) {
12388                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12389                 if (rc < 0)
12390                         goto error;
12391         }
12392
12393         rc = 0;
12394         for (; optind < argc; optind++) {
12395                 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12396                                              verbose);
12397                 if (rc1 < 0)
12398                         rc = rc1;
12399         }
12400 error:
12401         return rc;
12402 }
12403
12404 /**
12405  * lfs_mirror() - Parse and execute lfs mirror commands.
12406  * @argc: The count of lfs mirror command line arguments.
12407  * @argv: Array of strings for lfs mirror command line arguments.
12408  *
12409  * This function parses lfs mirror commands and performs the
12410  * corresponding functions specified in mirror_cmdlist[].
12411  *
12412  * Return: 0 on success or an error code on failure.
12413  */
12414 static int lfs_mirror(int argc, char **argv)
12415 {
12416         char cmd[PATH_MAX];
12417         int rc = 0;
12418
12419         setlinebuf(stdout);
12420
12421         Parser_init("lfs-mirror > ", mirror_cmdlist);
12422
12423         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12424         progname = cmd;
12425         program_invocation_short_name = cmd;
12426         if (argc > 1)
12427                 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12428         else
12429                 rc = Parser_commands();
12430
12431         return rc < 0 ? -rc : rc;
12432 }
12433
12434 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12435 {
12436 #if __BYTE_ORDER == __BIG_ENDIAN
12437         __swab16s(&attrs->lsa_valid);
12438         __swab64s(&attrs->lsa_size);
12439         __swab64s(&attrs->lsa_blocks);
12440 #endif
12441 }
12442
12443 enum lfs_som_type {
12444         LFS_SOM_SIZE = 0x1,
12445         LFS_SOM_BLOCKS = 0x2,
12446         LFS_SOM_FLAGS = 0x4,
12447         LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12448                            LFS_SOM_FLAGS,
12449 };
12450
12451 static int lfs_getsom(int argc, char **argv)
12452 {
12453         const char *path;
12454         struct lustre_som_attrs *attrs;
12455         char buf[sizeof(*attrs) + 64];
12456         enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12457         int rc = 0, c;
12458
12459         while ((c = getopt(argc, argv, "bfhs")) != -1) {
12460                 switch (c) {
12461                 case 'b':
12462                         type = LFS_SOM_BLOCKS;
12463                         break;
12464                 case 'f':
12465                         type = LFS_SOM_FLAGS;
12466                         break;
12467                 case 's':
12468                         type = LFS_SOM_SIZE;
12469                         break;
12470                 default:
12471                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12472                                 progname, argv[optind - 1]);
12473                         fallthrough;
12474                 case 'h':
12475                         return CMD_HELP;
12476                 }
12477         }
12478
12479         argc -= optind;
12480         argv += optind;
12481
12482         if (argc != 1) {
12483                 fprintf(stderr, "%s: %s\n",
12484                         progname, argc == 0 ? "miss file target" :
12485                         "input more than 2 files");
12486                 return CMD_HELP;
12487         }
12488
12489         path = argv[0];
12490         attrs = (void *)buf;
12491         rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12492         if (rc < 0) {
12493                 rc = -errno;
12494                 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12495                         argv[0], strerror(errno), errno);
12496                 return rc;
12497         }
12498
12499         lustre_som_swab(attrs);
12500
12501         switch (type) {
12502         case LFS_SOM_ATTR_ALL:
12503                 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12504                        path, (unsigned long long)attrs->lsa_size,
12505                        (unsigned long long)attrs->lsa_blocks,
12506                        attrs->lsa_valid);
12507                 break;
12508         case LFS_SOM_SIZE:
12509                 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12510                 break;
12511         case LFS_SOM_BLOCKS:
12512                 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12513                 break;
12514         case LFS_SOM_FLAGS:
12515                 printf("%x\n", attrs->lsa_valid);
12516                 break;
12517         default:
12518                 fprintf(stderr, "%s: unknown option\n", progname);
12519                 return CMD_HELP;
12520         }
12521
12522         return 0;
12523 }
12524
12525 /**
12526  * lfs_mirror_list_commands() - List lfs mirror commands.
12527  * @argc: The count of command line arguments.
12528  * @argv: Array of strings for command line arguments.
12529  *
12530  * This function lists lfs mirror commands defined in mirror_cmdlist[].
12531  *
12532  * Return: 0 on success.
12533  */
12534 static int lfs_mirror_list_commands(int argc, char **argv)
12535 {
12536         char buffer[81] = "";
12537
12538         Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12539                              NULL, 0, 4);
12540
12541         return 0;
12542 }
12543
12544 static int lfs_pcc_attach(int argc, char **argv)
12545 {
12546         struct option long_opts[] = {
12547         { .val = 'h',   .name = "help", .has_arg = no_argument },
12548         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12549         { .name = NULL } };
12550         int c;
12551         int rc = 0;
12552         __u32 archive_id = 0;
12553         const char *path;
12554         char *end;
12555         char fullpath[PATH_MAX];
12556         enum lu_pcc_type type = LU_PCC_READWRITE;
12557
12558         optind = 0;
12559         while ((c = getopt_long(argc, argv, "hi:",
12560                                 long_opts, NULL)) != -1) {
12561                 switch (c) {
12562                 case 'i':
12563                         errno = 0;
12564                         archive_id = strtoul(optarg, &end, 0);
12565                         if (errno != 0 || *end != '\0' ||
12566                             archive_id == 0 || archive_id > UINT32_MAX) {
12567                                 fprintf(stderr,
12568                                         "error: %s: bad archive ID '%s'\n",
12569                                         progname, optarg);
12570                                 return CMD_HELP;
12571                         }
12572                         break;
12573                 default:
12574                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12575                                 progname, argv[optind - 1]);
12576                         fallthrough;
12577                 case 'h':
12578                         return CMD_HELP;
12579                 }
12580         }
12581
12582         if (archive_id == 0) {
12583                 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12584                 return CMD_HELP;
12585         }
12586
12587         if (argc <= optind) {
12588                 fprintf(stderr, "%s: must specify one or more file names\n",
12589                         argv[0]);
12590                 return CMD_HELP;
12591         }
12592
12593         while (optind < argc) {
12594                 int rc2;
12595
12596                 path = argv[optind++];
12597                 if (!realpath(path, fullpath)) {
12598                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12599                                 argv[0], path, strerror(errno));
12600                         if (rc == 0)
12601                                 rc = -EINVAL;
12602                         continue;
12603                 }
12604
12605                 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12606                 if (rc2 < 0) {
12607                         fprintf(stderr,
12608                                 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12609                                 argv[0], path, archive_id, strerror(-rc2));
12610                         if (rc == 0)
12611                                 rc = rc2;
12612                 }
12613         }
12614         return rc;
12615 }
12616
12617 static int lfs_pcc_attach_fid(int argc, char **argv)
12618 {
12619         struct option long_opts[] = {
12620         { .val = 'h',   .name = "help", .has_arg = no_argument },
12621         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12622         { .val = 'm',   .name = "mnt",  .has_arg = required_argument },
12623         { .name = NULL } };
12624         int c;
12625         int rc = 0;
12626         __u32 archive_id = 0;
12627         char *end;
12628         const char *mntpath = NULL;
12629         const char *fidstr;
12630         enum lu_pcc_type type = LU_PCC_READWRITE;
12631
12632         optind = 0;
12633         while ((c = getopt_long(argc, argv, "hi:m:",
12634                                 long_opts, NULL)) != -1) {
12635                 switch (c) {
12636                 case 'i':
12637                         errno = 0;
12638                         archive_id = strtoul(optarg, &end, 0);
12639                         if (errno != 0 || *end != '\0' ||
12640                             archive_id > UINT32_MAX) {
12641                                 fprintf(stderr,
12642                                         "error: %s: bad archive ID '%s'\n",
12643                                         argv[0], optarg);
12644                                 return CMD_HELP;
12645                         }
12646                         break;
12647                 case 'm':
12648                         mntpath = optarg;
12649                         break;
12650                 default:
12651                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12652                                 progname, argv[optind - 1]);
12653                         fallthrough;
12654                 case 'h':
12655                         return CMD_HELP;
12656                 }
12657         }
12658
12659         if (archive_id == 0) {
12660                 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12661                 return CMD_HELP;
12662         }
12663
12664         if (!mntpath) {
12665                 fprintf(stderr, "%s: must specify Lustre mount point\n",
12666                         argv[0]);
12667                 return CMD_HELP;
12668         }
12669
12670         if (argc <= optind) {
12671                 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12672                 return CMD_HELP;
12673         }
12674
12675         while (optind < argc) {
12676                 int rc2;
12677
12678                 fidstr = argv[optind++];
12679
12680                 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12681                                                archive_id, type);
12682                 if (rc2 < 0) {
12683                         fprintf(stderr,
12684                                 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12685                                 argv[0], fidstr, mntpath, archive_id,
12686                                 strerror(rc2));
12687                 }
12688                 if (rc == 0 && rc2 < 0)
12689                         rc = rc2;
12690         }
12691         return rc;
12692 }
12693
12694 static int lfs_pcc_detach(int argc, char **argv)
12695 {
12696         struct option long_opts[] = {
12697         { .val = 'h',   .name = "help", .has_arg = no_argument },
12698         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12699         { .name = NULL } };
12700         int c;
12701         int rc = 0;
12702         const char *path;
12703         char fullpath[PATH_MAX];
12704         __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12705
12706         optind = 0;
12707         while ((c = getopt_long(argc, argv, "hk",
12708                                 long_opts, NULL)) != -1) {
12709                 switch (c) {
12710                 case 'k':
12711                         detach_opt = PCC_DETACH_OPT_NONE;
12712                         break;
12713                 default:
12714                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12715                                 progname, argv[optind - 1]);
12716                         fallthrough;
12717                 case 'h':
12718                         return CMD_HELP;
12719                 }
12720         }
12721
12722         while (optind < argc) {
12723                 int rc2;
12724
12725                 path = argv[optind++];
12726                 if (!realpath(path, fullpath)) {
12727                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12728                                 argv[0], path, strerror(errno));
12729                         if (rc == 0)
12730                                 rc = -EINVAL;
12731                         continue;
12732                 }
12733
12734                 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12735                 if (rc2 < 0) {
12736                         rc2 = -errno;
12737                         fprintf(stderr,
12738                                 "%s: cannot detach '%s' from PCC: %s\n",
12739                                 argv[0], path, strerror(errno));
12740                         if (rc == 0)
12741                                 rc = rc2;
12742                 }
12743         }
12744         return rc;
12745 }
12746
12747 static int lfs_pcc_detach_fid(int argc, char **argv)
12748 {
12749         struct option long_opts[] = {
12750         { .val = 'h',   .name = "help", .has_arg = no_argument },
12751         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12752         { .name = NULL } };
12753         int c;
12754         int rc = 0;
12755         const char *fid;
12756         const char *mntpath;
12757         __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12758
12759         optind = 0;
12760         while ((c = getopt_long(argc, argv, "hk",
12761                                 long_opts, NULL)) != -1) {
12762                 switch (c) {
12763                 case 'k':
12764                         detach_opt = PCC_DETACH_OPT_NONE;
12765                         break;
12766                 default:
12767                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12768                                 progname, argv[optind - 1]);
12769                         fallthrough;
12770                 case 'h':
12771                         return CMD_HELP;
12772                 }
12773         }
12774
12775         mntpath = argv[optind++];
12776
12777         while (optind < argc) {
12778                 int rc2;
12779
12780                 fid = argv[optind++];
12781
12782                 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12783                 if (rc2 < 0) {
12784                         fprintf(stderr,
12785                                 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12786                                 argv[0], fid, mntpath, strerror(-rc2));
12787                         if (rc == 0)
12788                                 rc = rc2;
12789                 }
12790         }
12791         return rc;
12792 }
12793
12794 static int lfs_pcc_state(int argc, char **argv)
12795 {
12796         int rc = 0;
12797         const char *path;
12798         char fullpath[PATH_MAX];
12799         struct lu_pcc_state state;
12800
12801         optind = 1;
12802
12803         if (argc <= 1) {
12804                 fprintf(stderr, "%s: must specify one or more file names\n",
12805                         progname);
12806                 return CMD_HELP;
12807         }
12808
12809         while (optind < argc) {
12810                 int rc2;
12811
12812                 path = argv[optind++];
12813                 if (!realpath(path, fullpath)) {
12814                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12815                                 argv[0], path, strerror(errno));
12816                         if (rc == 0)
12817                                 rc = -EINVAL;
12818                         continue;
12819                 }
12820
12821                 rc2 = llapi_pcc_state_get(fullpath, &state);
12822                 if (rc2 < 0) {
12823                         if (rc == 0)
12824                                 rc = rc2;
12825                         fprintf(stderr,
12826                                 "%s: cannot get PCC state of '%s': %s\n",
12827                                 argv[0], path, strerror(-rc2));
12828                         continue;
12829                 }
12830
12831                 printf("file: %s", path);
12832                 printf(", type: %s", pcc_type2string(state.pccs_type));
12833                 if (state.pccs_type == LU_PCC_NONE &&
12834                     state.pccs_open_count == 0) {
12835                         printf("\n");
12836                         continue;
12837                 }
12838
12839                 printf(", PCC file: %s", state.pccs_path);
12840                 printf(", user number: %u", state.pccs_open_count);
12841                 printf(", flags: %x", state.pccs_flags);
12842                 printf("\n");
12843         }
12844         return rc;
12845 }
12846
12847 /**
12848  * lfs_pcc_list_commands() - List lfs pcc commands.
12849  * @argc: The count of command line arguments.
12850  * @argv: Array of strings for command line arguments.
12851  *
12852  * This function lists lfs pcc commands defined in pcc_cmdlist[].
12853  *
12854  * Return: 0 on success.
12855  */
12856 static int lfs_pcc_list_commands(int argc, char **argv)
12857 {
12858         char buffer[81] = "";
12859
12860         Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12861                              NULL, 0, 4);
12862
12863         return 0;
12864 }
12865
12866 /**
12867  * lfs_pcc() - Parse and execute lfs pcc commands.
12868  * @argc: The count of lfs pcc command line arguments.
12869  * @argv: Array of strings for lfs pcc command line arguments.
12870  *
12871  * This function parses lfs pcc commands and performs the
12872  * corresponding functions specified in pcc_cmdlist[].
12873  *
12874  * Return: 0 on success or an error code on failure.
12875  */
12876 static int lfs_pcc(int argc, char **argv)
12877 {
12878         char cmd[PATH_MAX];
12879         int rc = 0;
12880
12881         setlinebuf(stdout);
12882
12883         Parser_init("lfs-pcc > ", pcc_cmdlist);
12884
12885         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12886         progname = cmd;
12887         program_invocation_short_name = cmd;
12888         if (argc > 1)
12889                 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12890         else
12891                 rc = Parser_commands();
12892
12893         return rc < 0 ? -rc : rc;
12894 }
12895
12896 static int lfs_list_commands(int argc, char **argv)
12897 {
12898         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12899
12900         Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12901
12902         return 0;
12903 }
12904
12905 int main(int argc, char **argv)
12906 {
12907         int rc;
12908
12909         /* Ensure that liblustreapi constructor has run */
12910         if (!llapi_liblustreapi_initialized())
12911                 fprintf(stderr, "liblustreapi was not properly initialized\n");
12912
12913         setlinebuf(stdout);
12914         opterr = 0;
12915
12916         Parser_init("lfs > ", cmdlist);
12917
12918         progname = program_invocation_short_name; /* Used in error messages */
12919         if (argc > 1) {
12920                 llapi_set_command_name(argv[1]);
12921                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12922                 llapi_clear_command_name();
12923         } else {
12924                 rc = Parser_commands();
12925         }
12926
12927         return rc < 0 ? -rc : rc;
12928 }
12929
12930 #ifdef _LUSTRE_IDL_H_
12931 /* Everything we need here should be included by lustreapi.h. */
12932 # error "lfs should not depend on lustre_idl.h"
12933 #endif /* _LUSTRE_IDL_H_ */