Whamcloud - gitweb
LU-14677 sec: no encryption key migrate/extend/resync/split
[fs/lustre-release.git] / lustre / utils / lfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/lfs.c
32  *
33  * Author: Peter J. Braam <braam@clusterfs.com>
34  * Author: Phil Schwan <phil@clusterfs.com>
35  * Author: Robert Read <rread@clusterfs.com>
36  */
37
38 /* for O_DIRECTORY */
39 #ifndef _GNU_SOURCE
40 #define _GNU_SOURCE
41 #endif
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <inttypes.h>
46 #include <getopt.h>
47 #include <string.h>
48 #include <mntent.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
61 #include <fcntl.h>
62 #include <dirent.h>
63 #include <time.h>
64 #include <ctype.h>
65 #include <zlib.h>
66 #include <libgen.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
69
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
78 #include <lnetconfig/cyaml.h>
79 #include "lstddef.h"
80
81 /* all functions */
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_unlink_foreign(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
97 #endif
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_rmfid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_getsom(int argc, char **argv);
119 static int lfs_heat_get(int argc, char **argv);
120 static int lfs_heat_set(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
129 static int lfs_pcc_attach(int argc, char **argv);
130 static int lfs_pcc_attach_fid(int argc, char **argv);
131 static int lfs_pcc_detach(int argc, char **argv);
132 static int lfs_pcc_detach_fid(int argc, char **argv);
133 static int lfs_pcc_state(int argc, char **argv);
134 static int lfs_pcc(int argc, char **argv);
135 static int lfs_pcc_list_commands(int argc, char **argv);
136 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
137                               __u64 migration_flags,
138                               struct llapi_stripe_param *param,
139                               struct llapi_layout *layout);
140
141 struct pool_to_id_cbdata {
142         const char *pool;
143         __u32 id;
144 };
145 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
146 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
147
148 enum setstripe_origin {
149         SO_SETSTRIPE,
150         SO_MIGRATE,
151         SO_MIGRATE_MDT,
152         SO_MIRROR_CREATE,
153         SO_MIRROR_EXTEND,
154         SO_MIRROR_SPLIT,
155         SO_MIRROR_DELETE,
156 };
157
158 static int lfs_setstripe_internal(int argc, char **argv,
159                                   enum setstripe_origin opc);
160
161 static inline int lfs_setstripe(int argc, char **argv)
162 {
163         return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
164 }
165
166 static inline int lfs_setstripe_migrate(int argc, char **argv)
167 {
168         return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
169 }
170
171 static inline int lfs_mirror_create(int argc, char **argv)
172 {
173         return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
174 }
175
176 static inline int lfs_mirror_extend(int argc, char **argv)
177 {
178         return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
179 }
180
181 static inline int lfs_mirror_split(int argc, char **argv)
182 {
183         return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
184 }
185
186 static inline int lfs_mirror_delete(int argc, char **argv)
187 {
188         return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
189 }
190
191 /* Setstripe and migrate share mostly the same parameters */
192 #define SSM_CMD_COMMON(cmd) \
193         "usage: "cmd" [--component-end|-E COMP_END]\n"                  \
194         "                 [--copy=LUSTRE_SRC]\n"                        \
195         "                 [--extension-size|--ext-size|-z SIZE]\n"      \
196         "                 [--help|-h] [--layout|-L PATTERN]\n"          \
197         "                 [--layout|-L PATTERN]\n"                      \
198         "                 [--mirror-count|-N[MIRROR_COUNT]]\n"          \
199         "                 [--ost|-o OST_INDICES]\n"                     \
200         "                 [--overstripe-count|-C STRIPE_COUNT]\n"       \
201         "                 [--pool|-p POOL_NAME]\n"                      \
202         "                 [--stripe-count|-c STRIPE_COUNT]\n"           \
203         "                 [--stripe-index|-i START_OST_IDX]\n"          \
204         "                 [--stripe-size|-S STRIPE_SIZE]\n"             \
205         "                 [--yaml|-y YAML_TEMPLATE_FILE]\n"
206
207 #define MIRROR_EXTEND_USAGE                                             \
208         "                 {--mirror-count|-N[MIRROR_COUNT]}\n"          \
209         "                 [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n"  \
210         "                 [--no-verify]\n"
211
212 #define SETSTRIPE_USAGE                                                 \
213         SSM_CMD_COMMON("setstripe")                                     \
214         MIRROR_EXTEND_USAGE                                             \
215         "                 DIRECTORY|FILENAME\n"
216
217 #define MIGRATE_USAGE                                                   \
218         SSM_CMD_COMMON("migrate  ")                                     \
219         "                 [--block|-b] [--non-block|-n]\n"              \
220         "                 [--non-direct|-D] [--verbose|-v]\n"           \
221         "                 FILENAME\n"
222
223 #define SETDIRSTRIPE_USAGE                                              \
224         "               [--mdt-count|-c stripe_count>\n"                \
225         "               [--help|-h] [--mdt-hash|-H mdt_hash]\n"         \
226         "               [--mdt-index|-i mdt_index[,mdt_index,...]\n"    \
227         "               [--default|-D] [--mode|-o mode]\n"              \
228         "               [--max-inherit|-X max_inherit]\n"               \
229         "               [--max-inherit-rr max_inherit_rr] <dir>\n"      \
230         "To create dir with a foreign (free format) layout :\n"         \
231         "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
232         "               [--mode|-o MODE] [--flags HEX] DIRECTORY\n"
233
234 /**
235  * command_t mirror_cmdlist - lfs mirror commands.
236  */
237 command_t mirror_cmdlist[] = {
238         { .pc_name = "create", .pc_func = lfs_mirror_create,
239           .pc_help = "Create a mirrored file.\n"
240                 "usage: lfs mirror create --mirror-count|-N[MIRROR_COUNT]\n"
241                 "           [SETSTRIPE_OPTIONS] ... FILENAME|DIRECTORY ...\n" },
242         { .pc_name = "delete", .pc_func = lfs_mirror_delete,
243           .pc_help = "Delete a mirror from a file.\n"
244         "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
245         "\t               --component-id|--comp-id|-I COMP_ID |\n"
246         "\t               -p <pool>} MIRRORED_FILE ...\n"
247         },
248         { .pc_name = "extend", .pc_func = lfs_mirror_extend,
249           .pc_help = "Extend a mirrored file.\n"
250                 "usage: lfs mirror extend "
251                 "{--mirror-count|-N[MIRROR_COUNT]} [--no-verify] "
252                 "[SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... FILENAME ...\n" },
253         { .pc_name = "split", .pc_func = lfs_mirror_split,
254           .pc_help = "Split a mirrored file.\n"
255         "usage: lfs mirror split {--mirror-id MIRROR_ID |\n"
256         "\t             --component-id|-I COMP_ID|-p POOL} [--destroy|-d]\n"
257         "\t             [-f NEW_FILE] MIRRORED_FILE ...\n" },
258         { .pc_name = "read", .pc_func = lfs_mirror_read,
259           .pc_help = "Read the content of a specified mirror of a file.\n"
260                 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
261                 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
262         { .pc_name = "write", .pc_func = lfs_mirror_write,
263           .pc_help = "Write to a specified mirror of a file.\n"
264                 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
265                 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
266         { .pc_name = "copy", .pc_func = lfs_mirror_copy,
267           .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
268                 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
269                 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
270         { .pc_name = "resync", .pc_func = lfs_mirror_resync,
271           .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
272                 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]\n"
273                 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
274         { .pc_name = "verify", .pc_func = lfs_mirror_verify,
275           .pc_help = "Verify mirrored file(s).\n"
276                 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
277                 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
278         { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
279           .pc_help = "list commands supported by lfs mirror"},
280         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
281         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
282         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
283         { .pc_help = NULL }
284 };
285
286 /**
287  * command_t pcc_cmdlist - lfs pcc commands.
288  */
289 command_t pcc_cmdlist[] = {
290         { .pc_name = "attach", .pc_func = lfs_pcc_attach,
291           .pc_help = "Attach given files to the Persistent Client Cache.\n"
292                 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
293                 "\t-i: archive id for RW-PCC\n" },
294         { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
295           .pc_help = "Attach given files into PCC by FID(s).\n"
296                 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
297                 "\t-i: archive id for RW-PCC\n"
298                 "\t-m: Lustre mount point\n" },
299         { .pc_name = "state", .pc_func = lfs_pcc_state,
300           .pc_help = "Display the PCC state for given files.\n"
301                 "usage: lfs pcc state <file> ...\n" },
302         { .pc_name = "detach", .pc_func = lfs_pcc_detach,
303           .pc_help = "Detach given files from the Persistent Client Cache.\n"
304                 "usage: lfs pcc detach <file> ...\n" },
305         { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
306           .pc_help = "Detach given files from PCC by FID(s).\n"
307                 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
308         { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
309           .pc_help = "list commands supported by lfs pcc"},
310         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
311         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
312         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
313         { .pc_help = NULL }
314 };
315
316 /* all available commands */
317 command_t cmdlist[] = {
318         {"setstripe", lfs_setstripe, 0,
319          "To create a file with specified striping/composite layout, or\n"
320          "create/replace the default layout on an existing directory:\n"
321          SSM_CMD_COMMON("setstripe")
322          "                 [--mode MODE]\n"
323          "                 <directory|filename>\n"
324          " or\n"
325          "To add component(s) to an existing composite file:\n"
326          SSM_CMD_COMMON("setstripe --component-add")
327          "To totally delete the default striping from an existing directory:\n"
328          "usage: setstripe [--delete|-d] <directory>\n"
329          " or\n"
330          "To create a mirrored file or set s default mirror layout on a directory:\n"
331          "usage: setstripe {--mirror-count|-N}[MIRROR_COUNT] [SETSTRIPE_OPTIONS] <directory|filename>\n"
332          " or\n"
333          "To delete the last component(s) from an existing composite file\n"
334          "(note that this will also delete any data in those components):\n"
335          "usage: setstripe --component-del [--component-id|-I COMP_ID]\n"
336          "                               [--component-flags|-F COMP_FLAGS]\n"
337          "                               <filename>\n"
338          "\tCOMP_ID:     Unique component ID to delete\n"
339          "\tCOMP_FLAGS:  'init' indicating all instantiated components\n"
340          "\t             '^init' indicating all uninstantiated components\n"
341          "\t-I and -F cannot be specified at the same time\n"
342          " or\n"
343          "To set or clear flags on a specific component\n"
344          "(note that this command can only be applied to mirrored files:\n"
345          "usage: setstripe --comp-set {-I COMP_ID|--comp-flags=COMP_FLAGS}\n"
346          "                            <filename>\n"
347          " or\n"
348          "To create a file with a foreign (free format) layout:\n"
349          "usage: setstripe --foreign[=FOREIGN_TYPE]\n"
350          "                 --xattr|-x LAYOUT_STRING [--flags HEX]\n"
351          "                 [--mode MODE] <filename>\n"},
352         {"getstripe", lfs_getstripe, 0,
353          "To list the layout pattern for a given file or files in a\n"
354          "directory or recursively for all files in a directory tree.\n"
355          "usage: getstripe [--ost|-O UUID] [--quiet|-q] [--verbose|-v]\n"
356          "                 [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
357          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
358          "                 [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
359          "                 [--layout|-L] [--generation|-g] [--yaml|-y]\n"
360          "                 [--help|-h] [--component-id|-I[=COMP_ID]]\n"
361          "                 [--component-flags[=COMP_FLAGS]]\n"
362          "                 [--component-count]\n"
363          "                 [--extension-size|--ext-size|-z]\n"
364          "                 [--component-start[=[+-]COMP_START]]\n"
365          "                 [--component-end[=[+-]COMP_END]|-E[[+-]comp_end]]\n"
366          "                 [[!] --mirror-index=[+-]INDEX |\n"
367          "                 [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
368          "                 <directory|filename> ..."},
369         {"setdirstripe", lfs_setdirstripe, 0,
370          "Create striped directory on specified MDT, same as mkdir.\n"
371          "May be restricted to root or group users, depending on settings.\n"
372          "usage: setdirstripe [OPTION] <directory>\n"
373          SETDIRSTRIPE_USAGE},
374         {"getdirstripe", lfs_getdirstripe, 0,
375          "To list the layout pattern info for a given directory\n"
376          "or recursively for all directories in a directory tree.\n"
377          "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
378          "                    [--help|-h] [--mdt-hash|-H] [--obd|-O UUID]\n"
379          "                    [--recursive|-r] [--yaml|-y]\n"
380          "                    [--verbose|-v] [--default|-D]\n"
381          "                    [--max-inherit|-X]\n"
382          "                    [--max-inherit-rr] <dir> ..."},
383         {"mkdir", lfs_setdirstripe, 0,
384          "Create striped directory on specified MDT, same as setdirstripe.\n"
385          "usage: mkdir [OPTION] <directory>\n"
386          SETDIRSTRIPE_USAGE},
387         {"rm_entry", lfs_rmentry, 0,
388          "To remove the name entry of the remote directory. Note: This\n"
389          "command will only delete the name entry, i.e. the remote directory\n"
390          "will become inaccessable after this command. This can only be done\n"
391          "by the administrator\n"
392          "usage: rm_entry <dir>\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]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
405          "     [[!] --mtime|-M [+-]N[smhdwy]]\n"
406          "     [[!] --btime|--Btime|-B [+-]N[smhdwy]] [--help|-h]\n"
407          "     [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
408          "     [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
409          "     [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
410          "     [[!] --perm [/-]mode] [[!] --pool <pool>] [--print|-P]\n"
411          "     [--print0|-0] [[!] --projid <projid>]\n"
412          "     [[!] --size|-s [+-]N[bkMGTPE]]\n"
413          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
414          "     [[!] --stripe-index|-i <index,...>]\n"
415          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
416          "     [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
417          "     [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
418          "     [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
419          "     [[!] --layout|-L released,raid0,mdt]\n"
420          "     [[!] --foreign[=<foreign_type>]]\n"
421          "     [[!] --component-count [+-]<comp_cnt>]\n"
422          "     [[!] --component-start [+-]N[kMGTPE]]\n"
423          "     [[!] --component-end|-E [+-]N[kMGTPE]]\n"
424          "     [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
425          "     [[!] --mirror-count|-N [+-]<n>]\n"
426          "     [[!] --mirror-state <[^]state>]\n"
427          "     [[!] --mdt-count|-T [+-]<stripes>]\n"
428          "     [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
429          "     [[!] --mdt-index|-m <uuid|index,...>]\n"
430          "\t !: used before an option indicates 'NOT' requested attribute\n"
431          "\t -: used before a value indicates less than requested value\n"
432          "\t +: used before a value indicates more than requested value\n"
433          "\t ^: used before a flag indicates to exclude it\n"},
434         {"check", lfs_check, 0,
435          "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
436          "or all the servers (MGTs, MDTs and OSTs).\n"
437          "usage: check {mgts|osts|mdts|all}"},
438         {"osts", lfs_osts, 0, "list OSTs connected to client "
439          "[for specified path only]\n" "usage: osts [path]"},
440         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
441          "[for specified path only]\n" "usage: mdts [path]"},
442         {"df", lfs_df, 0,
443          "report filesystem disk space usage or inodes usage "
444          "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
445          "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
446          "          [--pool|-p <fsname>[.<pool>]] [path]"},
447         {"getname", lfs_getname, 0,
448          "list instances and specified mount points [for specified path only]\n"
449          "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
450 #ifdef HAVE_SYS_QUOTA_H
451         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
452          "usage: setquota [-t][-d] {-u|-U|-g|-G|-p|-P} {-b|-B|-i|-I LIMIT} [--pool POOL] FILESYSTEM"},
453         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
454          "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
455          "             [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
456          "             [--pool <OST pool name>] <filesystem>\n"
457          "       quota -t <-u|-g|-p> [--pool <OST pool name>] <filesystem>\n"
458          "       quota [-q] [-v] [h] {-U|-G|-P} [--pool <OST pool name>] <filesystem>"},
459         {"project", lfs_project, 0,
460          "Change or list project attribute for specified file or directory.\n"
461          "usage: project [-d|-r] <file|directory...>\n"
462          "         list project ID and flags on file(s) or directories\n"
463          "       project [-p id] [-s] [-r] <file|directory...>\n"
464          "         set project ID and/or inherit flag for specified file(s) or directories\n"
465          "       project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
466          "         check project ID and flags on file(s) or directories, print outliers\n"
467          "       project -C [-r] [-k] <file|directory...>\n"
468          "         clear the project inherit flag and ID on the file or directory\n"
469         },
470 #endif
471         {"flushctx", lfs_flushctx, 0,
472          "Flush security context for current user.\n"
473          "usage: flushctx [-k] [-r] [mountpoint...]"},
474         {"changelog", lfs_changelog, 0,
475          "Show the metadata changes on an MDT."
476          "\nusage: changelog <mdtname> [startrec [endrec]]"},
477         {"changelog_clear", lfs_changelog_clear, 0,
478          "Indicate that old changelog records up to <endrec> are no longer of "
479          "interest to consumer <id>, allowing the system to free up space.\n"
480          "An <endrec> of 0 means all records.\n"
481          "usage: changelog_clear <mdtname> <id> <endrec>"},
482         {"fid2path", lfs_fid2path, 0,
483          "Resolve the full path(s) for given FID(s). For a specific hardlink "
484          "specify link number <linkno>.\n"
485          "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
486          "<fsname|root> <fid>..."},
487         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
488          "usage: path2fid [--parents] <path> ..."},
489         {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
490          "usage: rmfid <fsname|rootpath> <fid> ..."},
491         {"data_version", lfs_data_version, 0, "Display file data version for "
492          "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
493         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
494          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
495         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
496          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
497          "[--archived] [--lost] [--archive-id NUM] <file> ..."},
498         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
499          "files.\n"
500          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
501          "[--archived] [--lost] <file> ..."},
502         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
503          "given files.\n" "usage: hsm_action <file> ..."},
504         {"hsm_archive", lfs_hsm_archive, 0,
505          "Archive file to external storage.\n"
506          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
507          "<file> ..."},
508         {"hsm_restore", lfs_hsm_restore, 0,
509          "Restore file from external storage.\n"
510          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
511         {"hsm_release", lfs_hsm_release, 0,
512          "Release files from Lustre.\n"
513          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
514         {"hsm_remove", lfs_hsm_remove, 0,
515          "Remove file copy from external storage.\n"
516          "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
517          "[--archive NUM]\n"
518          "                  (FILE [FILE ...] | "
519          "--mntpath MOUNTPATH FID [FID ...])\n"
520          "\n"
521          "Note: To remove an archived copy of a file already deleted from a "
522          "Lustre FS, the\n"
523          "--mntpath option and a list of FIDs must be specified"
524         },
525         {"hsm_cancel", lfs_hsm_cancel, 0,
526          "Cancel requests related to specified files.\n"
527          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
528         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
529          "usage: swap_layouts <path1> <path2>"},
530         {"migrate", lfs_setstripe_migrate, 0,
531          "migrate directories and their inodes between MDTs.\n"
532          "usage: migrate [--mdt-count|-c STRIPE_COUNT] [--directory|-d]\n"
533          "               [--mdt-hash|-H HASH_TYPE]\n"
534          "               [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
535          "               DIRECTORY\n"
536          "\n"
537          "migrate file objects from one OST layout to another\n"
538          "(may be not safe with concurent writes).\n"
539          MIGRATE_USAGE },
540         {"mv", lfs_mv, 0,
541          "To move directories between MDTs. This command is deprecated, "
542          "use \"migrate\" instead.\n"
543          "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
544          "[--verbose|-v]\n"},
545         {"ladvise", lfs_ladvise, 0,
546          "Provide servers with advice about access patterns for a file.\n"
547          "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
548          "               [--background|-b] [--unset|-u]\n\n"
549          "               {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
550          "               {[--mode|-m [READ,WRITE]}\n"
551          "               <file> ...\n"},
552         {"mirror", lfs_mirror, mirror_cmdlist,
553          "lfs commands used to manage files with mirrored components:\n"
554          "lfs mirror create - create a mirrored file or directory\n"
555          "lfs mirror extend - add mirror(s) to an existing file\n"
556          "lfs mirror split  - split a mirror from an existing mirrored file\n"
557          "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
558          "lfs mirror read   - read a mirror content of a mirrored file\n"
559          "lfs mirror write  - write to a mirror of a mirrored file\n"
560          "lfs mirror copy   - copy a mirror to other mirror(s) of a file\n"
561          "lfs mirror verify - verify mirrored file(s)\n"},
562         {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
563          "usage: getsom [-s] [-b] [-f] <path>\n"
564          "\t-s: Only show the size value of the SOM data for a given file\n"
565          "\t-b: Only show the blocks value of the SOM data for a given file\n"
566          "\t-f: Only show the flags value of the SOM data for a given file\n"},
567         {"heat_get", lfs_heat_get, 0,
568          "To get heat of files.\n"
569          "usage: heat_get <file> ...\n"},
570         {"heat_set", lfs_heat_set, 0,
571          "To set heat flags of files.\n"
572          "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
573          "\t--clear|-c: Clear file heat for given files\n"
574          "\t--off|-o:   Turn off file heat for given files\n"
575          "\t--on|-O:    Turn on file heat for given files\n"},
576         {"pcc", lfs_pcc, pcc_cmdlist,
577          "lfs commands used to interact with PCC features:\n"
578          "lfs pcc attach - attach given files to Persistent Client Cache\n"
579          "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
580          "lfs pcc state  - display the PCC state for given files\n"
581          "lfs pcc detach - detach given files from Persistent Client Cache\n"
582          "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
583         {"help", Parser_help, 0, "help"},
584         {"exit", Parser_quit, 0, "quit"},
585         {"quit", Parser_quit, 0, "quit"},
586         {"--version", Parser_version, 0,
587          "output build version of the utility and exit"},
588         {"--list-commands", lfs_list_commands, 0,
589          "list commands supported by the utility and exit"},
590         { 0, 0, 0, NULL }
591 };
592
593 static int check_hashtype(const char *hashtype)
594 {
595         int type_num = atoi(hashtype);
596         int i;
597
598         /* numeric hash type */
599         if (hashtype && strlen(hashtype) == 1 &&
600             (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
601                 return type_num;
602         /* string hash type */
603         for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
604                 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
605                         return i;
606
607         return 0;
608 }
609
610 static uint32_t check_foreign_type_name(const char *foreign_type_name)
611 {
612         uint32_t i;
613
614         for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
615                 if (!lu_foreign_types[i].lft_name)
616                         break;
617                 if (strcmp(foreign_type_name,
618                            lu_foreign_types[i].lft_name) == 0)
619                         return lu_foreign_types[i].lft_type;
620         }
621
622         return LU_FOREIGN_TYPE_UNKNOWN;
623 }
624
625 static const char *error_loc = "syserror";
626
627 #define MIGRATION_NONBLOCK      LLAPI_MIGRATION_NONBLOCK
628 #define MIGRATION_MIRROR        LLAPI_MIGRATION_MIRROR
629 #define MIGRATION_NONDIRECT     LLAPI_MIGRATION_NONDIRECT
630 #define MIGRATION_VERBOSE       LLAPI_MIGRATION_VERBOSE
631
632 static int
633 migrate_open_files(const char *name, __u64 migration_flags,
634                    const struct llapi_stripe_param *param,
635                    struct llapi_layout *layout, int *fd_src, int *fd_tgt)
636 {
637         int                      fd = -1;
638         int                      fdv = -1;
639         int                      rflags;
640         int                      mdt_index;
641         int                      random_value;
642         char                     parent[PATH_MAX];
643         char                     volatile_file[PATH_MAX];
644         char                    *ptr;
645         int                      rc;
646         struct stat              st;
647         struct stat              stv;
648
649         if (!param && !layout) {
650                 error_loc = "layout information";
651                 return -EINVAL;
652         }
653
654         /* search for file directory pathname */
655         if (strlen(name) > sizeof(parent) - 1) {
656                 error_loc = "source file name";
657                 return -ERANGE;
658         }
659
660         strncpy(parent, name, sizeof(parent));
661         ptr = strrchr(parent, '/');
662         if (!ptr) {
663                 if (!getcwd(parent, sizeof(parent))) {
664                         error_loc = "getcwd";
665                         return -errno;
666                 }
667         } else {
668                 if (ptr == parent) /* leading '/' */
669                         ptr = parent + 1;
670                 *ptr = '\0';
671         }
672
673         /* even if the file is only read, WR mode is nedeed to allow
674          * layout swap on fd
675          */
676         /* Allow migrating even without the key on encrypted files */
677         rflags = O_RDWR | O_NOATIME | O_FILE_ENC;
678         if (!(migration_flags & MIGRATION_NONDIRECT))
679                 rflags |= O_DIRECT;
680 source_open:
681         fd = open(name, rflags);
682         if (fd < 0) {
683                 /* If encrypted file without the key,
684                  * retry mirror extend in O_DIRECT.
685                  */
686                 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
687                     migration_flags & MIGRATION_MIRROR) {
688                         rflags |= O_DIRECT;
689                         goto source_open;
690                 }
691                 rc = -errno;
692                 error_loc = "cannot open source file";
693                 return rc;
694         }
695
696         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
697         if (rc < 0) {
698                 error_loc = "cannot get MDT index";
699                 goto out;
700         }
701
702         do {
703                 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
704                         /* Allow migrating without the key on encrypted files */
705                         O_FILE_ENC;
706                 mode_t open_mode = S_IRUSR | S_IWUSR;
707
708                 if (rflags & O_DIRECT)
709                         open_flags |= O_DIRECT;
710                 random_value = random();
711                 rc = snprintf(volatile_file, sizeof(volatile_file),
712                               "%s/%s:%.4X:%.4X:fd=%.2d", parent,
713                               LUSTRE_VOLATILE_HDR, mdt_index,
714                               random_value, fd);
715                 if (rc >= sizeof(volatile_file)) {
716                         rc = -ENAMETOOLONG;
717                         break;
718                 }
719
720                 /* create, open a volatile file, use caching (ie no directio) */
721                 if (layout) {
722                         /* Returns -1 and sets errno on error: */
723                         fdv = llapi_layout_file_open(volatile_file, open_flags,
724                                                      open_mode, layout);
725                         if (fdv < 0)
726                                 fdv = -errno;
727                 } else {
728                         /* Does the right thing on error: */
729                         fdv = llapi_file_open_param(volatile_file, open_flags,
730                                                     open_mode, param);
731                 }
732         } while (fdv < 0 && (rc = fdv) == -EEXIST);
733
734         if (rc < 0) {
735                 error_loc = "cannot create volatile file";
736                 goto out;
737         }
738
739         /*
740          * In case the MDT does not support creation of volatile files
741          * we should try to unlink it.
742          */
743         (void)unlink(volatile_file);
744
745         /*
746          * Not-owner (root?) special case.
747          * Need to set owner/group of volatile file like original.
748          * This will allow to pass related check during layout_swap.
749          */
750         rc = fstat(fd, &st);
751         if (rc != 0) {
752                 rc = -errno;
753                 error_loc = "cannot stat source file";
754                 goto out;
755         }
756
757         rc = fstat(fdv, &stv);
758         if (rc != 0) {
759                 rc = -errno;
760                 error_loc = "cannot stat volatile";
761                 goto out;
762         }
763
764         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
765                 rc = fchown(fdv, st.st_uid, st.st_gid);
766                 if (rc != 0) {
767                         rc = -errno;
768                         error_loc = "cannot change ownwership of volatile";
769                         goto out;
770                 }
771         }
772
773 out:
774         if (rc < 0) {
775                 if (fd > 0)
776                         close(fd);
777                 if (fdv > 0)
778                         close(fdv);
779         } else {
780                 *fd_src = fd;
781                 *fd_tgt = fdv;
782                 error_loc = NULL;
783         }
784         return rc;
785 }
786
787 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
788 {
789         struct llapi_layout *layout;
790         size_t buf_size = 4 * 1024 * 1024;
791         void *buf = NULL;
792         off_t pos = 0;
793         off_t data_end = 0;
794         size_t page_size = sysconf(_SC_PAGESIZE);
795         bool sparse;
796         int rc;
797
798         layout = llapi_layout_get_by_fd(fd_src, 0);
799         if (layout) {
800                 uint64_t stripe_size;
801
802                 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
803                 if (rc == 0)
804                         buf_size = stripe_size;
805
806                 llapi_layout_free(layout);
807         }
808
809         /* Use a page-aligned buffer for direct I/O */
810         rc = posix_memalign(&buf, page_size, buf_size);
811         if (rc != 0)
812                 return -rc;
813
814         sparse = llapi_file_is_sparse(fd_src);
815         if (sparse) {
816                 rc = ftruncate(fd_dst, pos);
817                 if (rc < 0) {
818                         rc = -errno;
819                         return rc;
820                 }
821         }
822
823         while (1) {
824                 off_t data_off;
825                 size_t to_read, to_write;
826                 ssize_t rsize;
827
828                 if (sparse && pos >= data_end) {
829                         size_t data_size;
830
831                         data_off = llapi_data_seek(fd_src, pos, &data_size);
832                         if (data_off < 0) {
833                                 /* Non-fatal, switch to full copy */
834                                 sparse = false;
835                                 continue;
836                         }
837                         /* hole at the end of file, truncate up to it */
838                         if (!data_size) {
839                                 rc = ftruncate(fd_dst, data_off);
840                                 if (rc < 0)
841                                         goto out;
842                         }
843                         pos = data_off & ~(page_size - 1);
844                         data_end = data_off + data_size;
845                         to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
846                         to_read = MIN(to_read, buf_size);
847                 } else {
848                         to_read = buf_size;
849                 }
850
851                 if (check_file) {
852                         rc = check_file(fd_src);
853                         if (rc < 0)
854                                 goto out;
855                 }
856
857                 rsize = pread(fd_src, buf, to_read, pos);
858                 if (rsize < 0) {
859                         rc = -errno;
860                         goto out;
861                 }
862                 /* EOF */
863                 if (rsize == 0)
864                         break;
865
866                 to_write = rsize;
867                 while (to_write > 0) {
868                         ssize_t written;
869
870                         written = pwrite(fd_dst, buf, to_write, pos);
871                         if (written < 0) {
872                                 rc = -errno;
873                                 goto out;
874                         }
875                         pos += written;
876                         to_write -= written;
877                 }
878                 if (rc || rsize < to_read)
879                         break;
880         }
881
882         rc = fsync(fd_dst);
883         if (rc < 0)
884                 rc = -errno;
885 out:
886         /* Try to avoid page cache pollution after migration. */
887         (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
888         (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
889
890         free(buf);
891         return rc;
892 }
893
894 static int migrate_set_timestamps(int fd, const struct stat *st)
895 {
896         struct timeval tv[2] = {
897                 {.tv_sec = st->st_atime},
898                 {.tv_sec = st->st_mtime}
899         };
900
901         return futimes(fd, tv);
902 }
903
904 static int migrate_block(int fd, int fdv)
905 {
906         struct stat st;
907         __u64   dv1;
908         int     gid;
909         int     rc;
910         int     rc2;
911
912         rc = fstat(fd, &st);
913         if (rc < 0) {
914                 error_loc = "cannot stat source file";
915                 return -errno;
916         }
917
918         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
919         if (rc < 0) {
920                 error_loc = "cannot get dataversion";
921                 return rc;
922         }
923
924         do
925                 gid = random();
926         while (gid == 0);
927
928         /*
929          * The grouplock blocks all concurrent accesses to the file.
930          * It has to be taken after llapi_get_data_version as it would
931          * block it too.
932          */
933         rc = llapi_group_lock(fd, gid);
934         if (rc < 0) {
935                 error_loc = "cannot get group lock";
936                 return rc;
937         }
938
939         rc = migrate_copy_data(fd, fdv, NULL);
940         if (rc < 0) {
941                 error_loc = "data copy failed";
942                 goto out_unlock;
943         }
944
945         /* Make sure we keep original atime/mtime values */
946         rc = migrate_set_timestamps(fdv, &st);
947         if (rc < 0) {
948                 error_loc = "set target file timestamp failed";
949                 goto out_unlock;
950         }
951
952         /*
953          * swap layouts
954          * for a migration we need to check data version on file did
955          * not change.
956          *
957          * Pass in gid=0 since we already own grouplock.
958          */
959         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
960                                            SWAP_LAYOUTS_CHECK_DV1);
961         if (rc == -EAGAIN) {
962                 error_loc = "file changed";
963                 goto out_unlock;
964         } else if (rc < 0) {
965                 error_loc = "cannot swap layout";
966                 goto out_unlock;
967         }
968
969 out_unlock:
970         rc2 = llapi_group_unlock(fd, gid);
971         if (rc2 < 0 && rc == 0) {
972                 error_loc = "unlock group lock";
973                 rc = rc2;
974         }
975
976         return rc;
977 }
978
979 /**
980  * Internal helper for migrate_copy_data(). Check lease and report error if
981  * need be.
982  *
983  * \param[in]  fd           File descriptor on which to check the lease.
984  *
985  * \retval 0       Migration can keep on going.
986  * \retval -errno  Error occurred, abort migration.
987  */
988 static int check_lease(int fd)
989 {
990         int rc;
991
992         rc = llapi_lease_check(fd);
993         if (rc > 0)
994                 return 0; /* llapi_check_lease returns > 0 on success. */
995
996         return -EBUSY;
997 }
998
999 static int migrate_nonblock(int fd, int fdv)
1000 {
1001         struct stat st;
1002         __u64   dv1;
1003         __u64   dv2;
1004         int     rc;
1005
1006         rc = fstat(fd, &st);
1007         if (rc < 0) {
1008                 error_loc = "cannot stat source file";
1009                 return -errno;
1010         }
1011
1012         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1013         if (rc < 0) {
1014                 error_loc = "cannot get data version";
1015                 return rc;
1016         }
1017
1018         rc = migrate_copy_data(fd, fdv, check_lease);
1019         if (rc < 0) {
1020                 error_loc = "data copy failed";
1021                 return rc;
1022         }
1023
1024         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1025         if (rc != 0) {
1026                 error_loc = "cannot get data version";
1027                 return rc;
1028         }
1029
1030         if (dv1 != dv2) {
1031                 rc = -EAGAIN;
1032                 error_loc = "source file changed";
1033                 return rc;
1034         }
1035
1036         /* Make sure we keep original atime/mtime values */
1037         rc = migrate_set_timestamps(fdv, &st);
1038         if (rc < 0) {
1039                 error_loc = "set target file timestamp failed";
1040                 return -errno;
1041         }
1042         return 0;
1043 }
1044
1045 static
1046 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1047 {
1048         struct pool_to_id_cbdata data = { .pool = pool };
1049         struct llapi_layout *layout = NULL;
1050         int rc;
1051
1052         layout = llapi_layout_get_by_path(fname, 0);
1053         if (!layout) {
1054                 fprintf(stderr,
1055                         "error %s: file '%s' couldn't get layout: rc=%d\n",
1056                         progname, fname, errno);
1057                 rc = -errno;
1058                 goto free_layout;
1059         }
1060         rc = llapi_layout_sanity(layout, fname, false, true);
1061         if (rc < 0) {
1062                 llapi_layout_sanity_perror(errno);
1063                 goto free_layout;
1064         }
1065         rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1066         if (rc < 0)
1067                 goto free_layout;
1068
1069         *comp_id = data.id;
1070         rc = 0;
1071
1072 free_layout:
1073         if (layout)
1074                 llapi_layout_free(layout);
1075         return rc;
1076 }
1077
1078 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1079                              __u32 flags, __u32 neg_flags)
1080 {
1081         __u32 ids[2];
1082         __u32 flags_array[2];
1083         size_t count = 0;
1084         int rc;
1085
1086         if (!comp_id) {
1087                 if (pool == NULL) {
1088                         fprintf(stderr,
1089                                 "error %s: neither component id nor pool is specified\n",
1090                                 progname);
1091                         return -EINVAL;
1092                 }
1093                 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1094                 if (rc)
1095                         return rc;
1096         }
1097
1098         if (flags) {
1099                 ids[count] = comp_id;
1100                 flags_array[count] = flags;
1101                 ++count;
1102         }
1103
1104         if (neg_flags) {
1105                 if (neg_flags & LCME_FL_STALE) {
1106                         fprintf(stderr,
1107                                 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1108                                 progname);
1109                         return -EINVAL;
1110                 }
1111
1112                 ids[count] = comp_id;
1113                 flags_array[count] = neg_flags | LCME_FL_NEG;
1114                 ++count;
1115         }
1116
1117         rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1118         if (rc) {
1119                 if (errno == EUCLEAN) {
1120                         rc = -errno;
1121                         fprintf(stderr,
1122                                 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1123                                 progname, comp_id, fname);
1124                 } else {
1125                         fprintf(stderr,
1126                                 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1127                                 progname, comp_id, fname, flags, neg_flags);
1128                 }
1129         }
1130
1131         return rc;
1132 }
1133
1134 static int lfs_component_del(char *fname, __u32 comp_id,
1135                              __u32 flags, __u32 neg_flags)
1136 {
1137         int     rc = 0;
1138
1139         if (flags && neg_flags)
1140                 return -EINVAL;
1141
1142         if (!flags && neg_flags)
1143                 flags = neg_flags | LCME_FL_NEG;
1144
1145         if ((flags && comp_id) || (!flags && !comp_id))
1146                 return -EINVAL;
1147
1148         if (flags) {
1149                 if (flags & ~LCME_KNOWN_FLAGS) {
1150                         fprintf(stderr,
1151                                 "%s setstripe: unknown flags %#x\n",
1152                                 progname, flags);
1153                         return -EINVAL;
1154                 }
1155         } else if (comp_id > LCME_ID_MAX) {
1156                 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1157                         progname, comp_id);
1158                 return -EINVAL;
1159         }
1160
1161         rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1162         if (rc)
1163                 fprintf(stderr,
1164                         "%s setstripe: cannot delete component %#x from '%s': %s\n",
1165                         progname, comp_id, fname, strerror(errno));
1166         return rc;
1167 }
1168
1169 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1170 {
1171         int     rc;
1172
1173         if (!layout)
1174                 return -EINVAL;
1175
1176         rc = llapi_layout_file_comp_add(fname, layout);
1177         if (rc)
1178                 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1179                         fname, strerror(errno));
1180         return rc;
1181 }
1182
1183 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1184                                 struct llapi_layout *layout)
1185 {
1186         struct stat     st;
1187         int     fd;
1188
1189         if (!layout)
1190                 return -EINVAL;
1191
1192         fd = lstat(fname, &st);
1193         if (fd == 0 && S_ISDIR(st.st_mode))
1194                 open_flags = O_DIRECTORY | O_RDONLY;
1195
1196         fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1197         if (fd < 0)
1198                 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1199                         S_ISDIR(st.st_mode) ?
1200                                 "set default composite layout for" :
1201                                 "create composite file",
1202                         fname, strerror(errno));
1203         return fd;
1204 }
1205
1206 static int lfs_migrate(char *name, __u64 migration_flags,
1207                        struct llapi_stripe_param *param,
1208                        struct llapi_layout *layout)
1209 {
1210         struct llapi_layout *existing;
1211         uint64_t dom_new, dom_cur;
1212         int fd = -1;
1213         int fdv = -1;
1214         int rc;
1215
1216         rc = migrate_open_files(name, migration_flags, param, layout,
1217                                 &fd, &fdv);
1218         if (rc < 0)
1219                 goto out;
1220
1221         rc = llapi_layout_dom_size(layout, &dom_new);
1222         if (rc) {
1223                 error_loc = "cannot get new layout DoM size";
1224                 goto out;
1225         }
1226         /* special case for migration to DOM layout*/
1227         existing = llapi_layout_get_by_fd(fd, 0);
1228         if (!existing) {
1229                 error_loc = "cannot get existing layout";
1230                 goto out;
1231         }
1232
1233         rc = llapi_layout_dom_size(existing, &dom_cur);
1234         if (rc) {
1235                 error_loc = "cannot get current layout DoM size";
1236                 goto out;
1237         }
1238
1239         /*
1240          * if file has DoM layout already then migration is possible to
1241          * the new layout with the same DoM component via swap layout,
1242          * if new layout used bigger DOM size, then mirroring is used
1243          */
1244         if (dom_new > dom_cur) {
1245                 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1246                                         layout);
1247                 if (rc)
1248                         error_loc = "cannot migrate to DOM layout";
1249                 goto out_closed;
1250         }
1251
1252         if (!(migration_flags & MIGRATION_NONBLOCK)) {
1253                 /*
1254                  * Blocking mode (forced if servers do not support file lease).
1255                  * It is also the default mode, since we cannot distinguish
1256                  * between a broken lease and a server that does not support
1257                  * atomic swap/close (LU-6785)
1258                  */
1259                 rc = migrate_block(fd, fdv);
1260                 goto out;
1261         }
1262
1263         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1264         if (rc < 0) {
1265                 error_loc = "cannot get lease";
1266                 goto out;
1267         }
1268
1269         rc = migrate_nonblock(fd, fdv);
1270         if (rc < 0) {
1271                 llapi_lease_release(fd);
1272                 goto out;
1273         }
1274
1275         /*
1276          * Atomically put lease, swap layouts and close.
1277          * for a migration we need to check data version on file did
1278          * not change.
1279          */
1280         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1281         if (rc < 0) {
1282                 error_loc = "cannot swap layout";
1283                 goto out;
1284         }
1285
1286 out:
1287         if (fd >= 0)
1288                 close(fd);
1289
1290         if (fdv >= 0)
1291                 close(fdv);
1292 out_closed:
1293         if (rc < 0)
1294                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1295                         progname, name, error_loc, strerror(-rc));
1296         else if (migration_flags & MIGRATION_VERBOSE)
1297                 printf("%s\n", name);
1298
1299         return rc;
1300 }
1301
1302 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1303 {
1304         char *name;
1305         char *dup_string = NULL;
1306         int rc = 0;
1307
1308         *flags = 0;
1309         *neg_flags = 0;
1310
1311         if (!string || !string[0])
1312                 return -EINVAL;
1313
1314         dup_string = strdup(string);
1315         if (!dup_string) {
1316                 llapi_printf(LLAPI_MSG_ERROR,
1317                              "%s: insufficient memory\n",
1318                              progname);
1319                 return -ENOMEM;
1320         }
1321
1322         for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1323                 bool found = false;
1324                 int i;
1325
1326                 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1327                         __u32 comp_flag = comp_flags_table[i].cfn_flag;
1328                         const char *comp_name = comp_flags_table[i].cfn_name;
1329
1330                         if (strcmp(name, comp_name) == 0) {
1331                                 *flags |= comp_flag;
1332                                 found = true;
1333                         } else if (strncmp(name, "^", 1) == 0 &&
1334                                    strcmp(name + 1, comp_name) == 0) {
1335                                 *neg_flags |= comp_flag;
1336                                 found = true;
1337                         }
1338                 }
1339                 if (!found) {
1340                         llapi_printf(LLAPI_MSG_ERROR,
1341                                      "%s: component flag '%s' not supported\n",
1342                                      progname, name);
1343                         rc = -EINVAL;
1344                         goto out_free;
1345                 }
1346         }
1347
1348         if (!*flags && !*neg_flags)
1349                 rc = -EINVAL;
1350
1351         /* don't allow to set and exclude the same flag */
1352         if (*flags & *neg_flags)
1353                 rc = -EINVAL;
1354
1355 out_free:
1356         free(dup_string);
1357         return rc;
1358 }
1359
1360 static int mdthash_input(char *string, __u32 *inflags,
1361                          __u32 *exflags, __u32 *type)
1362 {
1363         char *name;
1364         struct mhf_list {
1365                 char *name;
1366                 __u32 flag;
1367         } mhflist[] = {
1368                 {"migrating", LMV_HASH_FLAG_MIGRATION},
1369                 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1370                 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1371         };
1372
1373         if (string == NULL)
1374                 return -EINVAL;
1375
1376         *inflags = 0;
1377         *exflags = 0;
1378         *type = 0;
1379         for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1380                 bool found = false;
1381                 int i;
1382
1383                 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1384                         if (strcmp(name, mhflist[i].name) == 0 ||
1385                             name[0] == mhflist[i].name[0]) {
1386                                 *inflags |= mhflist[i].flag;
1387                                 found = true;
1388                         } else if (name[0] == '^' &&
1389                                    (strcmp(name + 1, mhflist[i].name) == 0 ||
1390                                     name[1] == mhflist[i].name[0])) {
1391                                 *exflags |= mhflist[i].flag;
1392                                 found = true;
1393                         }
1394                 }
1395                 if (!found) {
1396                         i = check_hashtype(name);
1397                         if (i > 0) {
1398                                 *type |= 1 << i;
1399                                 continue;
1400                         }
1401                         llapi_printf(LLAPI_MSG_ERROR,
1402                                      "%s: invalid mdt_hash value '%s'\n",
1403                                      progname, name);
1404                         return -EINVAL;
1405                 }
1406         }
1407
1408         /* don't allow to include and exclude the same flag */
1409         if (*inflags & *exflags) {
1410                 llapi_printf(LLAPI_MSG_ERROR,
1411                              "%s: include and exclude same flag '%s'\n",
1412                              progname, string);
1413                 return -EINVAL;
1414         }
1415
1416         return 0;
1417 }
1418
1419 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1420 {
1421         if (!string)
1422                 return -EINVAL;
1423
1424         *state = 0;
1425         *neg_state = 0;
1426
1427         if (strncmp(string, "^", 1) == 0) {
1428                 *neg_state = llapi_layout_string_flags(string + 1);
1429                 if (*neg_state != 0)
1430                         return 0;
1431         } else {
1432                 *state = llapi_layout_string_flags(string);
1433                 if (*state != 0)
1434                         return 0;
1435         }
1436
1437         llapi_printf(LLAPI_MSG_ERROR,
1438                      "%s: mirrored file state '%s' not supported\n",
1439                      progname, string);
1440         return -EINVAL;
1441 }
1442
1443 /**
1444  * struct mirror_args - Command-line arguments for mirror(s).
1445  * @m_count:  Number of mirrors to be created with this layout.
1446  * @m_flags:  Mirror level flags, only 'prefer' is supported.
1447  * @m_layout: Mirror layout.
1448  * @m_file:   A victim file. Its layout will be split and used as a mirror.
1449  * @m_next:   Point to the next node of the list.
1450  *
1451  * Command-line arguments for mirror(s) will be parsed and stored in
1452  * a linked list that consists of this structure.
1453  */
1454 struct mirror_args {
1455         __u32                   m_count;
1456         __u32                   m_flags;
1457         struct llapi_layout     *m_layout;
1458         const char              *m_file;
1459         struct mirror_args      *m_next;
1460         bool                    m_inherit;
1461 };
1462
1463 /**
1464  * enum mirror_flags - Flags for extending a mirrored file.
1465  * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1466  *             in case the victim file(s) contains the same data as the
1467  *             original mirrored file.
1468  * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1469  * @MF_COMP_ID: specified component id instead of mirror id
1470  *
1471  * Flags for extending a mirrored file.
1472  */
1473 enum mirror_flags {
1474         MF_NO_VERIFY    = 0x1,
1475         MF_DESTROY      = 0x2,
1476         MF_COMP_ID      = 0x4,
1477         MF_COMP_POOL    = 0x8,
1478 };
1479
1480 /**
1481  * mirror_create_sanity_check() - Check mirror list.
1482  * @list:  A linked list that stores the mirror arguments.
1483  *
1484  * This function does a sanity check on @list for creating
1485  * a mirrored file.
1486  *
1487  * Return: 0 on success or a negative error code on failure.
1488  */
1489 static int mirror_create_sanity_check(const char *fname,
1490                                       struct mirror_args *list,
1491                                       bool check_fname)
1492 {
1493         int rc = 0;
1494         bool has_m_file = false;
1495         bool has_m_layout = false;
1496
1497         if (!list)
1498                 return -EINVAL;
1499
1500         if (fname && check_fname) {
1501                 struct llapi_layout *layout;
1502
1503                 layout = llapi_layout_get_by_path(fname, 0);
1504                 if (!layout) {
1505                         fprintf(stderr,
1506                                 "error: %s: file '%s' couldn't get layout\n",
1507                                 progname, fname);
1508                         return -ENODATA;
1509                 }
1510
1511                 rc = llapi_layout_sanity(layout, fname, false, true);
1512
1513                 llapi_layout_free(layout);
1514
1515                 if (rc) {
1516                         llapi_layout_sanity_perror(rc);
1517                         return rc;
1518                 }
1519         }
1520
1521         while (list) {
1522                 if (list->m_file) {
1523                         has_m_file = true;
1524                         llapi_layout_free(list->m_layout);
1525
1526                         list->m_layout =
1527                                 llapi_layout_get_by_path(list->m_file, 0);
1528                         if (!list->m_layout) {
1529                                 fprintf(stderr,
1530                                         "error: %s: file '%s' has no layout\n",
1531                                         progname, list->m_file);
1532                                 return -ENODATA;
1533                         }
1534                 } else {
1535                         has_m_layout = true;
1536                         if (!list->m_layout) {
1537                                 fprintf(stderr, "error: %s: no mirror layout\n",
1538                                         progname);
1539                                 return -EINVAL;
1540                         }
1541                 }
1542
1543                 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1544                 if (rc) {
1545                         llapi_layout_sanity_perror(rc);
1546                         return rc;
1547                 }
1548
1549                 list = list->m_next;
1550         }
1551
1552         if (has_m_file && has_m_layout) {
1553                 fprintf(stderr,
1554                         "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1555                         progname);
1556                 return -EINVAL;
1557         }
1558
1559         return 0;
1560 }
1561
1562 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1563 {
1564         __u32 mirror_flags = *(__u32 *)cbdata;
1565         uint32_t flags;
1566         int rc;
1567
1568         rc = llapi_layout_comp_flags_get(layout, &flags);
1569         if (rc < 0)
1570                 return rc;
1571
1572         if (!flags) {
1573                 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1574                 if (rc)
1575                         return rc;
1576         }
1577
1578         return LLAPI_LAYOUT_ITER_CONT;
1579 }
1580
1581 /**
1582  * mirror_create() - Create a mirrored file.
1583  * @fname:        The file to be created.
1584  * @mirror_list:  A linked list that stores the mirror arguments.
1585  *
1586  * This function creates a mirrored file @fname with the mirror(s)
1587  * from @mirror_list.
1588  *
1589  * Return: 0 on success or a negative error code on failure.
1590  */
1591 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1592 {
1593         struct llapi_layout *layout = NULL;
1594         struct mirror_args *cur_mirror = NULL;
1595         uint16_t mirror_count = 0;
1596         int i = 0;
1597         int rc = 0;
1598
1599         rc = mirror_create_sanity_check(fname, mirror_list, false);
1600         if (rc)
1601                 return rc;
1602
1603         cur_mirror = mirror_list;
1604         while (cur_mirror) {
1605                 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1606                                                mirror_set_flags,
1607                                                &cur_mirror->m_flags);
1608                 if (rc) {
1609                         rc = -errno;
1610                         fprintf(stderr, "%s: failed to set mirror flags\n",
1611                                 progname);
1612                         goto error;
1613                 }
1614
1615                 for (i = 0; i < cur_mirror->m_count; i++) {
1616                         rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1617                         if (rc) {
1618                                 rc = -errno;
1619                                 fprintf(stderr,
1620                                         "error: %s: merge layout failed: %s\n",
1621                                         progname, strerror(errno));
1622                                 goto error;
1623                         }
1624                 }
1625                 mirror_count += cur_mirror->m_count;
1626                 cur_mirror = cur_mirror->m_next;
1627         }
1628
1629         if (!layout) {
1630                 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1631                 return -EINVAL;
1632         }
1633
1634         rc = llapi_layout_mirror_count_set(layout, mirror_count);
1635         if (rc) {
1636                 rc = -errno;
1637                 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1638                         progname, strerror(errno));
1639                 goto error;
1640         }
1641
1642         rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1643                                   layout);
1644         if (rc >= 0) {
1645                 close(rc);
1646                 rc = 0;
1647         }
1648
1649 error:
1650         llapi_layout_free(layout);
1651         return rc;
1652 }
1653
1654 /**
1655  * Compare files and check lease on @fd.
1656  *
1657  * \retval bytes number of bytes are the same
1658  */
1659 static ssize_t mirror_file_compare(int fd, int fdv)
1660 {
1661         const size_t buflen = 4 * 1024 * 1024; /* 4M */
1662         void *buf;
1663         ssize_t bytes_done = 0;
1664         ssize_t bytes_read = 0;
1665
1666         buf = malloc(buflen * 2);
1667         if (!buf)
1668                 return -ENOMEM;
1669
1670         while (1) {
1671                 if (!llapi_lease_check(fd)) {
1672                         bytes_done = -EBUSY;
1673                         break;
1674                 }
1675
1676                 bytes_read = read(fd, buf, buflen);
1677                 if (bytes_read <= 0)
1678                         break;
1679
1680                 if (bytes_read != read(fdv, buf + buflen, buflen))
1681                         break;
1682
1683                 /*
1684                  * XXX: should compute the checksum on each buffer and then
1685                  * compare checksum to avoid cache collision
1686                  */
1687                 if (memcmp(buf, buf + buflen, bytes_read))
1688                         break;
1689
1690                 bytes_done += bytes_read;
1691         }
1692
1693         free(buf);
1694
1695         return bytes_done;
1696 }
1697
1698 static int mirror_extend_file(const char *fname, const char *victim_file,
1699                               enum mirror_flags mirror_flags)
1700 {
1701         int fd = -1;
1702         int fdv = -1;
1703         struct stat stbuf;
1704         struct stat stbuf_v;
1705         struct ll_ioc_lease *data = NULL;
1706         int rc;
1707
1708         fd = open(fname, O_RDWR);
1709         if (fd < 0) {
1710                 error_loc = "open source file";
1711                 rc = -errno;
1712                 goto out;
1713         }
1714
1715         fdv = open(victim_file, O_RDWR);
1716         if (fdv < 0) {
1717                 error_loc = "open target file";
1718                 rc = -errno;
1719                 goto out;
1720         }
1721
1722         if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1723                 error_loc = "stat source or target file";
1724                 rc = -errno;
1725                 goto out;
1726         }
1727
1728         if (stbuf.st_dev != stbuf_v.st_dev) {
1729                 error_loc = "stat source and target file";
1730                 rc = -EXDEV;
1731                 goto out;
1732         }
1733
1734         /* mirrors should be of the same size */
1735         if (stbuf.st_size != stbuf_v.st_size) {
1736                 error_loc = "file sizes don't match";
1737                 rc = -EINVAL;
1738                 goto out;
1739         }
1740
1741         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1742         if (rc < 0) {
1743                 error_loc = "cannot get lease";
1744                 goto out;
1745         }
1746
1747         if (!(mirror_flags & MF_NO_VERIFY)) {
1748                 ssize_t ret;
1749                 /* mirrors should have the same contents */
1750                 ret = mirror_file_compare(fd, fdv);
1751                 if (ret != stbuf.st_size) {
1752                         error_loc = "file busy or contents don't match";
1753                         rc = ret < 0 ? ret : -EINVAL;
1754                         goto out;
1755                 }
1756         }
1757
1758         /* Get rid of caching pages from clients */
1759         rc = llapi_file_flush(fd);
1760         if (rc < 0) {
1761                 error_loc = "cannot get data version";
1762                 goto out;
1763         }
1764
1765         rc = llapi_file_flush(fdv);
1766         if (rc < 0) {
1767                 error_loc = "cannot get data version";
1768                 goto out;
1769         }
1770
1771         rc = migrate_set_timestamps(fd, &stbuf);
1772         if (rc < 0) {
1773                 error_loc = "cannot set source file timestamp";
1774                 goto out;
1775         }
1776
1777         /* Atomically put lease, merge layouts and close. */
1778         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1779         if (!data) {
1780                 error_loc = "memory allocation";
1781                 goto out;
1782         }
1783         data->lil_mode = LL_LEASE_UNLCK;
1784         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1785         data->lil_count = 1;
1786         data->lil_ids[0] = fdv;
1787         rc = llapi_lease_set(fd, data);
1788         if (rc < 0) {
1789                 error_loc = "cannot merge layout";
1790                 goto out;
1791         } else if (rc == 0) {
1792                 rc = -EBUSY;
1793                 error_loc = "lost lease lock";
1794                 goto out;
1795         }
1796         rc = 0;
1797
1798 out:
1799         if (data)
1800                 free(data);
1801         if (fd >= 0)
1802                 close(fd);
1803         if (fdv >= 0)
1804                 close(fdv);
1805         if (!rc)
1806                 (void) unlink(victim_file);
1807         if (rc < 0)
1808                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1809                         progname, fname, error_loc, strerror(-rc));
1810         return rc;
1811 }
1812
1813 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1814                                 bool inherit, uint32_t flags)
1815 {
1816         struct llapi_layout *f_layout = NULL;
1817         struct ll_ioc_lease *data = NULL;
1818         struct stat st;
1819         int fd = -1;
1820         int fdv = -1;
1821         int rc = 0;
1822
1823         if (inherit) {
1824                 f_layout = llapi_layout_get_by_path(name, 0);
1825                 if (!f_layout) {
1826                         rc = -EINVAL;
1827                         fprintf(stderr, "%s: cannot get layout\n", progname);
1828                         goto out;
1829                 }
1830                 rc = llapi_layout_get_last_init_comp(f_layout);
1831                 if (rc) {
1832                         fprintf(stderr, "%s: cannot get the last init comp\n",
1833                                 progname);
1834                         goto out;
1835                 }
1836                 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1837                 if (rc) {
1838                         fprintf(stderr,
1839                                 "%s: cannot inherit from the last init comp\n",
1840                                 progname);
1841                         goto out;
1842                 }
1843         }
1844         llapi_layout_comp_flags_set(m_layout, flags);
1845         rc = migrate_open_files(name, MIGRATION_NONDIRECT | MIGRATION_MIRROR,
1846                                 NULL, m_layout, &fd, &fdv);
1847         if (rc < 0)
1848                 goto out;
1849
1850         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1851         if (rc < 0) {
1852                 error_loc = "cannot get lease";
1853                 goto out;
1854         }
1855
1856         rc = fstat(fd, &st);
1857         if (rc < 0) {
1858                 error_loc = "cannot stat source file";
1859                 goto out;
1860         }
1861
1862         rc = migrate_nonblock(fd, fdv);
1863         if (rc < 0) {
1864                 llapi_lease_release(fd);
1865                 goto out;
1866         }
1867
1868         rc = migrate_set_timestamps(fd, &st);
1869         if (rc < 0) {
1870                 error_loc = "cannot set source file timestamp";
1871                 goto out;
1872         }
1873
1874         /* Atomically put lease, merge layouts and close. */
1875         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1876         if (!data) {
1877                 error_loc = "memory allocation";
1878                 goto out;
1879         }
1880         data->lil_mode = LL_LEASE_UNLCK;
1881         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1882         data->lil_count = 1;
1883         data->lil_ids[0] = fdv;
1884         rc = llapi_lease_set(fd, data);
1885         if (rc < 0) {
1886                 error_loc = "cannot merge layout";
1887                 goto out;
1888         } else if (rc == 0) {
1889                 rc = -EBUSY;
1890                 error_loc = "lost lease lock";
1891                 goto out;
1892         }
1893         rc = 0;
1894
1895 out:
1896         if (data)
1897                 free(data);
1898         if (fd >= 0)
1899                 close(fd);
1900         if (fdv >= 0)
1901                 close(fdv);
1902         if (rc < 0)
1903                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1904                         progname, name, error_loc, strerror(-rc));
1905         return rc;
1906 }
1907
1908 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1909                          enum mirror_flags mirror_flags)
1910 {
1911         int rc = 0;
1912
1913         while (mirror_list) {
1914                 if (mirror_list->m_file) {
1915                         rc = mirror_extend_file(fname, mirror_list->m_file,
1916                                                 mirror_flags);
1917                 } else {
1918                         __u32 mirror_count = mirror_list->m_count;
1919
1920                         while (mirror_count > 0) {
1921                                 rc = mirror_extend_layout(fname,
1922                                                         mirror_list->m_layout,
1923                                                         mirror_list->m_inherit,
1924                                                         mirror_list->m_flags);
1925                                 if (rc)
1926                                         break;
1927
1928                                 --mirror_count;
1929                         }
1930                 }
1931                 if (rc)
1932                         break;
1933
1934                 mirror_list = mirror_list->m_next;
1935         }
1936
1937         return rc;
1938 }
1939
1940 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1941 {
1942         uint32_t id;
1943         int rc;
1944
1945         rc = llapi_layout_mirror_id_get(layout, &id);
1946         if (rc < 0)
1947                 return rc;
1948
1949         if ((__u16)id == *(__u16 *)cbdata)
1950                 return LLAPI_LAYOUT_ITER_STOP;
1951
1952         return LLAPI_LAYOUT_ITER_CONT;
1953 }
1954
1955 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1956 {
1957         uint32_t id;
1958         int rc;
1959
1960         rc = llapi_layout_comp_id_get(layout, &id);
1961         if (rc < 0)
1962                 return rc;
1963
1964         if (id == *(__u32 *)cbdata)
1965                 return LLAPI_LAYOUT_ITER_STOP;
1966
1967         return LLAPI_LAYOUT_ITER_CONT;
1968 }
1969
1970 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
1971 {
1972         char buf[LOV_MAXPOOLNAME + 1];
1973         struct pool_to_id_cbdata *d = (void *)cbdata;
1974         uint32_t id;
1975         int rc;
1976
1977         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1978         if (rc < 0)
1979                 return rc;
1980         if (strcmp(d->pool, buf))
1981                 return LLAPI_LAYOUT_ITER_CONT;
1982
1983         rc = llapi_layout_mirror_id_get(layout, &id);
1984         if (rc < 0)
1985                 return rc;
1986         d->id = id;
1987
1988         return LLAPI_LAYOUT_ITER_STOP;
1989 }
1990
1991 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1992 {
1993         char buf[LOV_MAXPOOLNAME + 1];
1994         struct pool_to_id_cbdata *d = (void *)cbdata;
1995         uint32_t id;
1996         int rc;
1997
1998         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1999         if (rc < 0)
2000                 return rc;
2001         if (strcmp(d->pool, buf))
2002                 return LLAPI_LAYOUT_ITER_CONT;
2003
2004         rc = llapi_layout_comp_id_get(layout, &id);
2005         if (rc < 0)
2006                 return rc;
2007         d->id = id;
2008
2009         return LLAPI_LAYOUT_ITER_STOP;
2010 }
2011
2012 struct collect_ids_data {
2013         __u16   *cid_ids;
2014         int     cid_count;
2015         __u16   cid_exclude;
2016 };
2017
2018 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2019 {
2020         struct collect_ids_data *cid = cbdata;
2021         uint32_t id;
2022         int rc;
2023
2024         rc = llapi_layout_mirror_id_get(layout, &id);
2025         if (rc < 0)
2026                 return rc;
2027
2028         if ((__u16)id != cid->cid_exclude) {
2029                 int i;
2030
2031                 for (i = 0; i < cid->cid_count; i++) {
2032                         /* already collected the mirror id */
2033                         if (id == cid->cid_ids[i])
2034                                 return LLAPI_LAYOUT_ITER_CONT;
2035                 }
2036                 cid->cid_ids[cid->cid_count] = id;
2037                 cid->cid_count++;
2038         }
2039
2040         return LLAPI_LAYOUT_ITER_CONT;
2041 }
2042
2043 /**
2044  * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2045  * @mirror_id: Mirror id to be checked.
2046  * @layout:    Mirror component list.
2047  *
2048  * This function checks if a mirror with specified @mirror_id is the last
2049  * non-stale mirror of a layout @layout.
2050  *
2051  * Return: true or false.
2052  */
2053 static inline
2054 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2055 {
2056         __u16 mirror_ids[128] = { 0 };
2057         struct collect_ids_data cid = { .cid_ids = mirror_ids,
2058                                         .cid_count = 0,
2059                                         .cid_exclude = mirror_id, };
2060         int i;
2061
2062         llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2063
2064         for (i = 0; i < cid.cid_count; i++) {
2065                 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2066                 int comp_size = 0;
2067
2068                 comp_size = llapi_mirror_find_stale(layout, comp_array,
2069                                                     ARRAY_SIZE(comp_array),
2070                                                     &mirror_ids[i], 1);
2071                 if (comp_size == 0)
2072                         return false;
2073         }
2074
2075         return true;
2076 }
2077
2078 static int mirror_split(const char *fname, __u32 id, const char *pool,
2079                         enum mirror_flags mflags, const char *victim_file)
2080 {
2081         struct llapi_layout *layout;
2082         char parent[PATH_MAX];
2083         char victim[PATH_MAX];
2084         int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2085         char *ptr;
2086         struct ll_ioc_lease *data;
2087         uint16_t mirror_count;
2088         __u32 mirror_id;
2089         int mdt_index;
2090         int fd, fdv;
2091         bool purge = true; /* delete mirror by setting fdv=fd */
2092         bool is_encrypted;
2093         int rc;
2094
2095         if (victim_file && (strcmp(fname, victim_file) == 0)) {
2096                 fprintf(stderr,
2097                         "error %s: the source file '%s' and -f file are the same\n",
2098                         progname, fname);
2099                 return -EINVAL;
2100         }
2101
2102         /* check fname contains mirror with mirror_id/comp_id */
2103         layout = llapi_layout_get_by_path(fname, 0);
2104         if (!layout) {
2105                 fprintf(stderr,
2106                         "error %s: file '%s' couldn't get layout\n",
2107                         progname, fname);
2108                 return -EINVAL;
2109         }
2110
2111         rc = llapi_layout_sanity(layout, fname, false, true);
2112         if (rc) {
2113                 llapi_layout_sanity_perror(rc);
2114                 goto free_layout;
2115         }
2116
2117         rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2118         if (rc) {
2119                 fprintf(stderr,
2120                         "error %s: file '%s' couldn't get mirror count\n",
2121                         progname, fname);
2122                 goto free_layout;
2123         }
2124         if (mirror_count < 2) {
2125                 fprintf(stderr,
2126                         "error %s: file '%s' has %d component, cannot split\n",
2127                         progname, fname, mirror_count);
2128                 goto free_layout;
2129         }
2130
2131         if (mflags & MF_COMP_POOL) {
2132                 struct pool_to_id_cbdata data = { .pool = pool };
2133
2134                 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2135                                                &data);
2136                 mirror_id = data.id;
2137         } else if (mflags & MF_COMP_ID) {
2138                 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2139                 mirror_id = mirror_id_of(id);
2140         } else {
2141                 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2142                 mirror_id = id;
2143         }
2144         if (rc < 0) {
2145                 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2146                         progname, fname);
2147                 goto free_layout;
2148         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2149                 if (mflags & MF_COMP_POOL) {
2150                         fprintf(stderr,
2151                                 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2152                                 progname, fname, pool);
2153                         goto free_layout;
2154                 } else if (mflags & MF_COMP_ID) {
2155                         fprintf(stderr,
2156                                 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2157                                 progname, fname, id);
2158                         goto free_layout;
2159                 } else {
2160                         fprintf(stderr,
2161                                 "error %s: file '%s' does not contain mirror with id %u\n",
2162                                 progname, fname, id);
2163                         goto free_layout;
2164                 }
2165         }
2166
2167         if (!victim_file && mflags & MF_DESTROY)
2168                 /* Allow mirror split even without the key on encrypted files,
2169                  * and in this case of a 'split -d', open file with O_DIRECT
2170                  * (no IOs will be done).
2171                  */
2172                 fd = open(fname, O_RDWR | O_DIRECT | O_FILE_ENC);
2173         else
2174                 fd = open(fname, O_RDWR);
2175
2176         if (fd < 0) {
2177                 fprintf(stderr,
2178                         "error %s: open file '%s' failed: %s\n",
2179                         progname, fname, strerror(errno));
2180                 goto free_layout;
2181         }
2182
2183         /* get victim file directory pathname */
2184         if (strlen(fname) > sizeof(parent) - 1) {
2185                 fprintf(stderr, "error %s: file name of '%s' too long\n",
2186                         progname, fname);
2187                 rc = -ERANGE;
2188                 goto close_fd;
2189         }
2190         strncpy(parent, fname, sizeof(parent));
2191         ptr = strrchr(parent, '/');
2192         if (!ptr) {
2193                 if (!getcwd(parent, sizeof(parent))) {
2194                         fprintf(stderr, "error %s: getcwd failed: %s\n",
2195                                 progname, strerror(errno));
2196                         rc = -errno;
2197                         goto close_fd;
2198                 }
2199         } else {
2200                 if (ptr == parent)
2201                         ptr = parent + 1;
2202                 *ptr = '\0';
2203         }
2204
2205         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2206         if (rc < 0) {
2207                 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2208                         progname, fname);
2209                 goto close_fd;
2210         }
2211
2212         rc = llapi_file_is_encrypted(fd);
2213         if (rc < 0) {
2214                 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2215                         progname, fname, rc);
2216                 goto close_fd;
2217         }
2218         is_encrypted = rc;
2219
2220 again:
2221         if (!victim_file) {
2222                 /* use a temp file to store the splitted layout */
2223                 if (mflags & MF_DESTROY) {
2224                         char file_path[PATH_MAX];
2225                         unsigned int rnumber;
2226                         int open_flags;
2227
2228                         if (last_non_stale_mirror(mirror_id, layout)) {
2229                                 rc = -EUCLEAN;
2230                                 fprintf(stderr,
2231                                         "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2232                                         progname, fname);
2233                                 goto close_fd;
2234                         }
2235
2236                         if (purge) {
2237                                 /* don't use volatile file for mirror destroy */
2238                                 fdv = fd;
2239                         } else {
2240                                 /**
2241                                  * try the old way to delete mirror using
2242                                  * volatile file.
2243                                  */
2244                                 do {
2245                                         rnumber = random();
2246                                         rc = snprintf(file_path,
2247                                                       sizeof(file_path),
2248                                                       "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2249                                                       parent, mdt_index,
2250                                                       rnumber, fd);
2251                                         if (rc < 0 ||
2252                                             rc >= sizeof(file_path)) {
2253                                                 fdv = -ENAMETOOLONG;
2254                                                 break;
2255                                         }
2256
2257                                         open_flags = O_RDWR |
2258                                              (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2259                                              O_CREAT | O_EXCL | O_NOFOLLOW |
2260                                              /* O_DIRECT for mirror split -d */
2261                                              O_DIRECT |
2262                                              /* Allow split without the key */
2263                                              O_FILE_ENC;
2264                                         fdv = open(file_path, open_flags,
2265                                                    S_IRUSR | S_IWUSR);
2266                                         if (fdv < 0)
2267                                                 rc = -errno;
2268                                 } while (fdv < 0 && rc == -EEXIST);
2269                         }
2270                 } else {
2271                         if (is_encrypted) {
2272                                 rc = -1;
2273                                 fprintf(stderr,
2274                                         "error %s: not permitted on encrypted file '%s': %d\n",
2275                                         progname, fname, rc);
2276                                 goto close_fd;
2277                         }
2278
2279                         snprintf(victim, sizeof(victim), "%s.mirror~%u",
2280                                  fname, mirror_id);
2281                         fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2282                 }
2283         } else {
2284                 /* user specified victim file */
2285                 if (is_encrypted) {
2286                         rc = -1;
2287                         fprintf(stderr,
2288                                 "error %s: not permitted on encrypted file '%s': %d\n",
2289                                 progname, fname, rc);
2290                         goto close_fd;
2291                 }
2292                 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2293         }
2294
2295         if (fdv < 0) {
2296                 fprintf(stderr,
2297                         "error %s: create victim file failed: %s\n",
2298                         progname, strerror(errno));
2299                 goto close_fd;
2300         }
2301
2302         /* get lease lock of fname */
2303         rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2304         if (rc < 0) {
2305                 fprintf(stderr,
2306                         "error %s: cannot get lease of file '%s': %d\n",
2307                         progname, fname, rc);
2308                 goto close_victim;
2309         }
2310
2311         /* Atomatically put lease, split layouts and close. */
2312         data = malloc(offsetof(typeof(*data), lil_ids[2]));
2313         if (!data) {
2314                 rc = -ENOMEM;
2315                 goto close_victim;
2316         }
2317
2318         data->lil_mode = LL_LEASE_UNLCK;
2319         data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2320         data->lil_count = 2;
2321         data->lil_ids[0] = fdv;
2322         data->lil_ids[1] = mirror_id;
2323         rc = llapi_lease_set(fd, data);
2324         if (rc <= 0) {
2325                 if (rc == -EINVAL && purge) {
2326                         /* could be old MDS which prohibit fd==fdv */
2327                         purge = false;
2328                         goto again;
2329
2330                 }
2331                 if (rc == 0) /* lost lease lock */
2332                         rc = -EBUSY;
2333                 fprintf(stderr,
2334                         "error %s: cannot split '%s': %s\n",
2335                         progname, fname, strerror(-rc));
2336         } else {
2337                 rc = 0;
2338         }
2339         free(data);
2340
2341 close_victim:
2342         if (!purge)
2343                 close(fdv);
2344 close_fd:
2345         close(fd);
2346 free_layout:
2347         llapi_layout_free(layout);
2348         return rc;
2349 }
2350
2351 static inline
2352 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2353                            __u16 *mirror_ids, int ids_nr);
2354
2355 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2356                               __u64 migration_flags,
2357                               struct llapi_stripe_param *param,
2358                               struct llapi_layout *layout)
2359 {
2360         struct ll_ioc_lease *data = NULL;
2361         int rc;
2362
2363         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2364         if (rc < 0) {
2365                 error_loc = "cannot get lease";
2366                 goto out_close;
2367         }
2368
2369         /* Atomically put lease, merge layouts, resync and close. */
2370         data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2371         if (!data) {
2372                 error_loc = "memory allocation";
2373                 goto out_close;
2374         }
2375         data->lil_mode = LL_LEASE_UNLCK;
2376         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2377         data->lil_count = 1;
2378         data->lil_ids[0] = fdv;
2379         rc = llapi_lease_set(fd, data);
2380         if (rc < 0) {
2381                 error_loc = "cannot merge layout";
2382                 goto out_close;
2383         } else if (rc == 0) {
2384                 rc = -EBUSY;
2385                 error_loc = "lost lease lock";
2386                 goto out_close;
2387         }
2388         close(fd);
2389         close(fdv);
2390
2391         rc = lfs_mirror_resync_file(name, data, NULL, 0);
2392         if (rc) {
2393                 error_loc = "cannot resync file";
2394                 goto out;
2395         }
2396
2397         /* delete first mirror now */
2398         rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2399         if (rc < 0)
2400                 error_loc = "cannot delete old layout";
2401         goto out;
2402
2403 out_close:
2404         close(fd);
2405         close(fdv);
2406 out:
2407         if (rc < 0)
2408                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2409                         progname, name, error_loc, strerror(-rc));
2410         else if (migration_flags & MIGRATION_VERBOSE)
2411                 printf("%s\n", name);
2412         if (data)
2413                 free(data);
2414         return rc;
2415 }
2416
2417 /**
2418  * Parse a string containing an target index list into an array of integers.
2419  *
2420  * The input string contains a comma delimited list of individual
2421  * indices and ranges, for example "1,2-4,7". Add the indices into the
2422  * \a tgts array and remove duplicates.
2423  *
2424  * \param[out] tgts             array to store indices in
2425  * \param[in] size              size of \a tgts array
2426  * \param[in] offset            starting index in \a tgts
2427  * \param[in] arg               string containing OST index list
2428  * \param[in/out] overstriping  index list may contain duplicates
2429  *
2430  * \retval positive    number of indices in \a tgts
2431  * \retval -EINVAL     unable to parse \a arg
2432  */
2433 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2434                          unsigned long long *pattern)
2435 {
2436         int rc;
2437         int nr = offset;
2438         int slots = size - offset;
2439         char *ptr = NULL;
2440         bool overstriped = false;
2441         bool end_of_loop;
2442
2443         if (!arg)
2444                 return -EINVAL;
2445
2446         end_of_loop = false;
2447         while (!end_of_loop) {
2448                 int start_index = 0;
2449                 int end_index = 0;
2450                 int i;
2451                 char *endptr = NULL;
2452
2453                 rc = -EINVAL;
2454
2455                 ptr = strchrnul(arg, ',');
2456
2457                 end_of_loop = *ptr == '\0';
2458                 *ptr = '\0';
2459
2460                 errno = 0;
2461                 start_index = strtol(arg, &endptr, 0);
2462                 if (endptr == arg) /* no data at all */
2463                         break;
2464                 if (errno != 0 || start_index < -1 ||
2465                     (*endptr != '-' && *endptr != '\0'))
2466                         break;
2467
2468                 end_index = start_index;
2469                 if (*endptr == '-') {
2470                         errno = 0;
2471                         end_index = strtol(endptr + 1, &endptr, 0);
2472                         if (errno != 0 || *endptr != '\0' || end_index < -1)
2473                                 break;
2474                         if (end_index < start_index)
2475                                 break;
2476                 }
2477
2478                 for (i = start_index; i <= end_index && slots > 0; i++) {
2479                         int j;
2480
2481                         /* remove duplicate */
2482                         for (j = 0; j < offset; j++) {
2483                                 if (tgts[j] == i && pattern &&
2484                                     *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2485                                         overstriped = true;
2486                                 else if (tgts[j] == i)
2487                                         return -EINVAL;
2488                         }
2489
2490                         j = offset;
2491
2492                         if (j == offset) { /* check complete */
2493                                 tgts[nr++] = i;
2494                                 --slots;
2495                         }
2496                 }
2497
2498                 if (slots == 0 && i < end_index)
2499                         break;
2500
2501                 *ptr = ',';
2502                 arg = ++ptr;
2503                 offset = nr;
2504                 rc = 0;
2505         }
2506         if (!end_of_loop && ptr)
2507                 *ptr = ',';
2508
2509         if (!overstriped && pattern)
2510                 *pattern = LLAPI_LAYOUT_DEFAULT;
2511
2512         return rc < 0 ? rc : nr;
2513 }
2514
2515 struct lfs_setstripe_args {
2516         unsigned long long       lsa_comp_end;
2517         unsigned long long       lsa_stripe_size;
2518         unsigned long long       lsa_extension_size;
2519         long long                lsa_stripe_count;
2520         long long                lsa_stripe_off;
2521         __u32                    lsa_comp_flags;
2522         __u32                    lsa_comp_neg_flags;
2523         unsigned long long       lsa_pattern;
2524         unsigned int             lsa_mirror_count;
2525         int                      lsa_nr_tgts;
2526         bool                     lsa_first_comp;
2527         bool                     lsa_extension_comp;
2528         __u32                   *lsa_tgts;
2529         char                    *lsa_pool_name;
2530 };
2531
2532 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2533 {
2534         unsigned int mirror_count = lsa->lsa_mirror_count;
2535         bool first_comp = lsa->lsa_first_comp;
2536
2537         memset(lsa, 0, sizeof(*lsa));
2538
2539         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2540         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2541         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2542         lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2543         lsa->lsa_pool_name = NULL;
2544
2545         lsa->lsa_mirror_count = mirror_count;
2546         lsa->lsa_first_comp = first_comp;
2547 }
2548
2549 /**
2550  * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2551  * @lsa: Stripe options to be initialized and inherited.
2552  *
2553  * This function initializes stripe options in @lsa and inherit
2554  * stripe_size, stripe_count and OST pool_name options.
2555  *
2556  * Return: void.
2557  */
2558 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2559 {
2560         unsigned long long stripe_size;
2561         long long stripe_count;
2562         char *pool_name = NULL;
2563
2564         stripe_size = lsa->lsa_stripe_size;
2565         stripe_count = lsa->lsa_stripe_count;
2566         pool_name = lsa->lsa_pool_name;
2567
2568         setstripe_args_init(lsa);
2569
2570         lsa->lsa_stripe_size = stripe_size;
2571         lsa->lsa_stripe_count = stripe_count;
2572         lsa->lsa_pool_name = pool_name;
2573 }
2574
2575 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2576 {
2577         return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2578                 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2579                 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2580                 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2581                 lsa->lsa_comp_end != 0);
2582 }
2583
2584 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2585 {
2586         if (lsa->lsa_nr_tgts) {
2587                 if (lsa->lsa_nr_tgts < 0 ||
2588                     lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2589                         fprintf(stderr, "Invalid nr_tgts(%d)\n",
2590                                 lsa->lsa_nr_tgts);
2591                         errno = EINVAL;
2592                         return -1;
2593                 }
2594
2595                 if (lsa->lsa_stripe_count > 0 &&
2596                     lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2597                     lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2598                     lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2599                         fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2600                                 lsa->lsa_stripe_count,
2601                                 lsa->lsa_nr_tgts);
2602                         errno = EINVAL;
2603                         return -1;
2604                 }
2605         }
2606
2607         return 0;
2608
2609 }
2610
2611 /**
2612  * comp_args_to_layout() - Create or extend a composite layout.
2613  * @composite:       Pointer to the composite layout.
2614  * @lsa:             Stripe options for the new component.
2615  *
2616  * This function creates or extends a composite layout by adding a new
2617  * component with stripe options from @lsa.
2618  *
2619  * When modified, adjust llapi_stripe_param_verify() if needed as well.
2620  *
2621  * Return: 0 on success or an error code on failure.
2622  */
2623 static int comp_args_to_layout(struct llapi_layout **composite,
2624                                struct lfs_setstripe_args *lsa,
2625                                bool set_extent)
2626 {
2627         struct llapi_layout *layout = *composite;
2628         uint64_t prev_end = 0;
2629         uint64_t size;
2630         int i = 0, rc;
2631
2632 new_comp:
2633         if (!layout) {
2634                 layout = llapi_layout_alloc();
2635                 if (!layout) {
2636                         fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2637                                 strerror(errno));
2638                         errno = ENOMEM;
2639                         return -1;
2640                 }
2641                 *composite = layout;
2642                 lsa->lsa_first_comp = true;
2643         } else {
2644                 uint64_t start;
2645
2646                 /*
2647                  * Get current component extent, current component
2648                  * must be the tail component.
2649                  */
2650                 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2651                 if (rc) {
2652                         fprintf(stderr, "Get comp extent failed. %s\n",
2653                                 strerror(errno));
2654                         return rc;
2655                 }
2656
2657                 if (lsa->lsa_first_comp) {
2658                         prev_end = 0;
2659                         rc = llapi_layout_add_first_comp(layout);
2660                 } else {
2661                         rc = llapi_layout_comp_add(layout);
2662                 }
2663                 if (rc) {
2664                         fprintf(stderr, "Add component failed. %s\n",
2665                                 strerror(errno));
2666                         return rc;
2667                 }
2668         }
2669
2670         rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2671         if (rc) {
2672                 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2673                         lsa->lsa_comp_flags, strerror(errno));
2674                 return rc;
2675         }
2676
2677         if (set_extent) {
2678                 uint64_t comp_end = lsa->lsa_comp_end;
2679
2680                 /*
2681                  * The extendable component is 0-length, so it can be removed
2682                  * if there is insufficient space to extend it.
2683                  */
2684                 if (lsa->lsa_extension_comp)
2685                         comp_end = prev_end;
2686
2687                 rc = llapi_layout_comp_extent_set(layout, prev_end,
2688                                                   comp_end);
2689                 if (rc) {
2690                         fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2691                                 prev_end, comp_end, strerror(errno));
2692                         return rc;
2693                 }
2694         }
2695         /* reset lsa_first_comp */
2696         lsa->lsa_first_comp = false;
2697
2698         /* Data-on-MDT component setting */
2699         if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2700                 /* Yaml support */
2701                 if (lsa->lsa_stripe_count == 0)
2702                         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2703                 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2704                         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2705                 if (lsa->lsa_stripe_off == -1 ||
2706                     lsa->lsa_stripe_off == 0)
2707                         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2708                 /*
2709                  * In case of Data-on-MDT patterns the only extra option
2710                  * applicable is stripe size option.
2711                  */
2712                 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2713                         fprintf(stderr,
2714                                 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2715                                 lsa->lsa_stripe_count);
2716                         errno = EINVAL;
2717                         return -1;
2718                 }
2719                 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2720                     lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2721                         fprintf(stderr,
2722                                 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2723                                 lsa->lsa_stripe_size);
2724                         errno = EINVAL;
2725                         return -1;
2726                 }
2727                 if (lsa->lsa_nr_tgts != 0) {
2728                         fprintf(stderr,
2729                                 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2730                                 lsa->lsa_nr_tgts);
2731                         errno = EINVAL;
2732                         return -1;
2733                 }
2734                 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2735                         fprintf(stderr,
2736                                 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2737                                 lsa->lsa_stripe_off);
2738                         errno = EINVAL;
2739                         return -1;
2740                 }
2741                 if (lsa->lsa_pool_name != 0) {
2742                         fprintf(stderr,
2743                                 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2744                                 lsa->lsa_pool_name);
2745                         errno = EINVAL;
2746                         return -1;
2747                 }
2748
2749                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2750                 if (rc) {
2751                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2752                                 lsa->lsa_pattern,
2753                                 strerror(errno));
2754                         return rc;
2755                 }
2756                 /* Data-on-MDT component has always single stripe up to end */
2757                 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2758         } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2759                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2760                 if (rc) {
2761                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2762                                 lsa->lsa_pattern,
2763                                 strerror(errno));
2764                         return rc;
2765                 }
2766         }
2767
2768         size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2769                 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2770
2771         if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2772                 rc = llapi_layout_extension_size_set(layout, size);
2773         else
2774                 rc = llapi_layout_stripe_size_set(layout, size);
2775
2776         if (rc) {
2777                 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2778                         size, strerror(errno));
2779                 return rc;
2780         }
2781
2782         rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2783         if (rc) {
2784                 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2785                         lsa->lsa_stripe_count, strerror(errno));
2786                 return rc;
2787         }
2788
2789         if (lsa->lsa_pool_name) {
2790                 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2791                 if (rc) {
2792                         fprintf(stderr, "Set pool name: %s failed. %s\n",
2793                                 lsa->lsa_pool_name, strerror(errno));
2794                         return rc;
2795                 }
2796         } else {
2797                 rc = llapi_layout_pool_name_set(layout, "");
2798                 if (rc) {
2799                         fprintf(stderr, "Clear pool name failed: %s\n",
2800                                 strerror(errno));
2801                         return rc;
2802                 }
2803         }
2804
2805         rc = lsa_args_stripe_count_check(lsa);
2806         if (rc)
2807                 return rc;
2808
2809         if (lsa->lsa_nr_tgts > 0) {
2810                 bool found = false;
2811
2812                 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2813                         rc = llapi_layout_ost_index_set(layout, i,
2814                                                         lsa->lsa_tgts[i]);
2815                         if (rc)
2816                                 break;
2817
2818                         /* Make sure stripe offset is in OST list. */
2819                         if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2820                                 found = true;
2821                 }
2822                 if (!found) {
2823                         fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2824                                 lsa->lsa_stripe_off);
2825                         errno = EINVAL;
2826                         return -1;
2827                 }
2828         } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2829                    lsa->lsa_stripe_off != -1) {
2830                 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2831         }
2832         if (rc) {
2833                 fprintf(stderr, "Set ost index %d failed. %s\n",
2834                         i, strerror(errno));
2835                 return rc;
2836         }
2837
2838         /* Create the second, virtual component of extension space */
2839         if (lsa->lsa_extension_comp) {
2840                 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2841                 lsa->lsa_extension_comp = false;
2842                 goto new_comp;
2843         }
2844
2845         return rc;
2846 }
2847
2848 static int build_component(struct llapi_layout **layout,
2849                            struct lfs_setstripe_args *lsa, bool set_extent)
2850 {
2851         int rc;
2852
2853         rc = comp_args_to_layout(layout, lsa, set_extent);
2854         if (rc)
2855                 return rc;
2856
2857         if (lsa->lsa_mirror_count > 0) {
2858                 rc = llapi_layout_mirror_count_set(*layout,
2859                                                    lsa->lsa_mirror_count);
2860                 if (rc)
2861                         return rc;
2862
2863                 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2864                 if (rc)
2865                         return rc;
2866                 lsa->lsa_mirror_count = 0;
2867         }
2868
2869         return rc;
2870 }
2871
2872 static int build_prev_component(struct llapi_layout **layout,
2873                                 struct lfs_setstripe_args *prev,
2874                                 struct lfs_setstripe_args *lsa,
2875                                 bool set_extent)
2876 {
2877         int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2878         int rc;
2879
2880         if (prev->lsa_stripe_size) {
2881                 if (extension) {
2882                         prev->lsa_comp_end = lsa->lsa_comp_end;
2883                         prev->lsa_extension_size = lsa->lsa_extension_size;
2884                         prev->lsa_extension_comp = true;
2885                 }
2886
2887                 rc = build_component(layout, prev, true);
2888                 if (rc)
2889                         return rc;
2890         }
2891
2892         /*
2893          * Copy lsa to previous lsa;
2894          * if this is an extension component, make the previous invalid;
2895          */
2896         if (extension)
2897                 prev->lsa_stripe_size = 0;
2898         else
2899                 *prev = *lsa;
2900
2901         return 0;
2902 }
2903
2904 #ifndef LCME_TEMPLATE_FLAGS
2905 #define LCME_TEMPLATE_FLAGS     (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
2906                                  LCME_FL_EXTENSION)
2907 #endif
2908
2909 static int build_layout_from_yaml_node(struct cYAML *node,
2910                                        struct llapi_layout **layout,
2911                                        struct lfs_setstripe_args *lsa,
2912                                        struct lfs_setstripe_args *prevp)
2913 {
2914         struct lfs_setstripe_args prev = { 0 };
2915         __u32 *osts = lsa->lsa_tgts;
2916         char *string;
2917         int rc = 0;
2918
2919         if (!prevp)
2920                 prevp = &prev;
2921
2922         while (node) {
2923                 string = node->cy_string;
2924
2925                 if (node->cy_type == CYAML_TYPE_OBJECT) {
2926                         /* go deep to sub blocks */
2927                         if (string && !strncmp(string, "component", 9) &&
2928                             strncmp(string, "component0", 10) &&
2929                             strncmp(string, "components", 10)) {
2930                                 rc = build_prev_component(layout, prevp, lsa,
2931                                                           true);
2932                                 if (rc)
2933                                         return rc;
2934
2935                                 /* initialize lsa. */
2936                                 setstripe_args_init(lsa);
2937                                 lsa->lsa_first_comp = false;
2938                                 lsa->lsa_tgts = osts;
2939                         }
2940
2941                         rc = build_layout_from_yaml_node(node->cy_child, layout,
2942                                                          lsa, prevp);
2943                         if (rc)
2944                                 return rc;
2945                 } else {
2946                         if (!node->cy_string)
2947                                 return -EINVAL;
2948
2949                         /* skip leading lmm_ if present, to simplify parsing */
2950                         if (strncmp(string, "lmm_", 4) == 0)
2951                                 string += 4;
2952
2953                         if (node->cy_type == CYAML_TYPE_STRING) {
2954                                 if (!strcmp(string, "lcme_extent.e_end")) {
2955                                         if (!strcmp(node->cy_valuestring, "EOF") ||
2956                                             !strcmp(node->cy_valuestring, "eof"))
2957                                                 lsa->lsa_comp_end = LUSTRE_EOF;
2958                                 } else if (!strcmp(string, "pool")) {
2959                                         lsa->lsa_pool_name = node->cy_valuestring;
2960                                 } else if (!strcmp(string, "pattern")) {
2961                                         if (!strcmp(node->cy_valuestring, "mdt"))
2962                                                 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2963                                         if (!strcmp(node->cy_valuestring,
2964                                                     "raid0,overstriped"))
2965                                                 lsa->lsa_pattern =
2966                                                         LLAPI_LAYOUT_OVERSTRIPING;
2967                                 } else if (!strcmp(string, "lcme_flags")) {
2968                                         rc = comp_str2flags(node->cy_valuestring,
2969                                                             &lsa->lsa_comp_flags,
2970                                                             &lsa->lsa_comp_neg_flags);
2971                                         if (rc)
2972                                                 return rc;
2973                                         /*
2974                                          * Only template flags have meaning in
2975                                          * the layout for a new file
2976                                          */
2977                                         lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2978                                 }
2979                         } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2980                                 if (!strcmp(string, "lcm_mirror_count")) {
2981                                         lsa->lsa_mirror_count = node->cy_valueint;
2982                                 } else if (!strcmp(string, "lcme_extent.e_start")) {
2983                                         if (node->cy_valueint == 0)
2984                                                 lsa->lsa_first_comp = true;
2985                                 } else if (!strcmp(string, "lcme_extent.e_end")) {
2986                                         if (node->cy_valueint == -1)
2987                                                 lsa->lsa_comp_end = LUSTRE_EOF;
2988                                         else
2989                                                 lsa->lsa_comp_end = node->cy_valueint;
2990                                 } else if (!strcmp(string, "stripe_count")) {
2991                                         lsa->lsa_stripe_count = node->cy_valueint;
2992                                 } else if (!strcmp(string, "stripe_size")) {
2993                                         lsa->lsa_stripe_size = node->cy_valueint;
2994                                 } else if (!strcmp(string, "extension_size")) {
2995                                         lsa->lsa_extension_size = node->cy_valueint;
2996                                         lsa->lsa_extension_comp = true;
2997                                 } else if (!strcmp(string, "stripe_offset")) {
2998                                         lsa->lsa_stripe_off = node->cy_valueint;
2999                                 } else if (!strcmp(string, "l_ost_idx")) {
3000                                         osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3001                                         lsa->lsa_nr_tgts++;
3002                                 }
3003                         }
3004                 }
3005                 node = node->cy_next;
3006         }
3007
3008         if (prevp == &prev) {
3009                 rc = build_prev_component(layout, prevp, lsa, true);
3010                 if (rc)
3011                         return rc;
3012
3013                 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3014                         rc = build_component(layout, lsa, *layout != NULL);
3015         }
3016
3017         return rc;
3018 }
3019
3020 static int lfs_comp_create_from_yaml(char *template,
3021                                      struct llapi_layout **layout,
3022                                      struct lfs_setstripe_args *lsa,
3023                                      __u32 *osts)
3024 {
3025         struct cYAML *tree = NULL, *err_rc = NULL;
3026         int rc = 0;
3027
3028         tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3029         if (!tree) {
3030                 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3031                         progname, template);
3032                 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3033                                   "can't parse", &err_rc);
3034                 cYAML_print_tree2file(stderr, err_rc);
3035                 cYAML_free_tree(err_rc);
3036                 rc = -EINVAL;
3037                 goto err;
3038         }
3039
3040         /* initialize lsa for plain file */
3041         setstripe_args_init(lsa);
3042         lsa->lsa_tgts = osts;
3043
3044         rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3045         if (rc) {
3046                 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3047                         progname, template);
3048                 goto err;
3049         }
3050         /* clean clean lsa */
3051         setstripe_args_init(lsa);
3052
3053 err:
3054         if (tree)
3055                 cYAML_free_tree(tree);
3056         return rc;
3057 }
3058
3059 /**
3060  * Get the extension size from the next (SEL) component and extend the
3061  * current component on it. The start of the next component is to be
3062  * adjusted as well.
3063  *
3064  * \param[in] layout    the current layout
3065  * \param[in] start     the start of the current component
3066  * \param[in,out] end   the end of the current component
3067  * \param[in] offset    the offset to adjust the end position to instead of
3068  *                      extension size
3069  *
3070  * \retval 0            - extended successfully
3071  * \retval < 0          - error
3072  */
3073 static int layout_extend_comp(struct llapi_layout *layout,
3074                               uint64_t start, uint64_t *end,
3075                               uint64_t offset)
3076 {
3077         uint64_t size, next_start, next_end;
3078         int rc;
3079
3080         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3081         if (rc < 0) {
3082                 fprintf(stderr,
3083                         "%s setstripe: cannot move component cursor: %s\n",
3084                         progname, strerror(errno));
3085                 return rc;
3086         }
3087
3088         /*
3089          * Even if the @size will not be used below, this will fail if
3090          * this is not a SEL component - a good confirmation we are
3091          * working on right components.
3092          */
3093         rc = llapi_layout_extension_size_get(layout, &size);
3094         if (rc < 0) {
3095                 fprintf(stderr,
3096                         "%s setstripe: cannot get component ext size: %s\n",
3097                         progname, strerror(errno));
3098                 return rc;
3099         }
3100
3101         rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3102         if (rc) {
3103                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3104                         progname, strerror(errno));
3105                 return rc;
3106         }
3107
3108         next_start += offset ?: size;
3109         rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3110         if (rc) {
3111                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3112                         progname, strerror(errno));
3113                 return rc;
3114         }
3115
3116         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3117         if (rc < 0) {
3118                 fprintf(stderr,
3119                         "%s setstripe: cannot move component cursor: %s\n",
3120                         progname, strerror(errno));
3121                 return rc;
3122         }
3123
3124         *end += offset ?: size;
3125         rc = llapi_layout_comp_extent_set(layout, start, *end);
3126         if (rc) {
3127                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3128                         progname, strerror(errno));
3129                 return rc;
3130         }
3131
3132         return 0;
3133 }
3134
3135 /**
3136  * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3137  * end of the last component in the existing file, and adjust the
3138  * first extent start of the components to be added accordingly.
3139  *
3140  * In the create mode, we need to check if the first component is an extendable
3141  * SEL component and extend its length to the extension size (first component
3142  * of the PFL file is initialised at the create time, cannot be 0-lenght.
3143  */
3144 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3145                                       bool comp_add)
3146 {
3147         struct llapi_layout *head;
3148         uint64_t start = 0, prev_end = 0;
3149         uint64_t end;
3150         int rc, ret = 0;
3151
3152         if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3153                 return 0;
3154
3155         errno = 0;
3156         while (comp_add) {
3157                 head = llapi_layout_get_by_path(fname, 0);
3158                 if (!head) {
3159                         fprintf(stderr,
3160                                 "%s setstripe: cannot read layout from '%s': %s\n",
3161                                 progname, fname, strerror(errno));
3162                         return -EINVAL;
3163                 } else if (errno == ENODATA) {
3164                         /*
3165                          * file without LOVEA, this component-add will be turned
3166                          * into a component-create.
3167                          */
3168                         llapi_layout_free(head);
3169                         ret = -ENODATA;
3170
3171                         /*
3172                          * the new layout will be added to an empty one, it
3173                          * still needs to be adjusted below
3174                          */
3175                         comp_add = 0;
3176                         break;
3177                 } else if (!llapi_layout_is_composite(head)) {
3178                         fprintf(stderr,
3179                                 "%s setstripe: '%s' not a composite file\n",
3180                                 progname, fname);
3181                         llapi_layout_free(head);
3182                         return -EINVAL;
3183                 }
3184
3185                 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3186                 if (rc) {
3187                         fprintf(stderr,
3188                                 "%s setstripe: cannot get prev extent: %s\n",
3189                                 progname, strerror(errno));
3190                         llapi_layout_free(head);
3191                         return rc;
3192                 }
3193
3194                 llapi_layout_free(head);
3195                 break;
3196         }
3197
3198         /* Make sure we use the first component of the layout to be added. */
3199         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3200         if (rc < 0) {
3201                 fprintf(stderr,
3202                         "%s setstripe: cannot move component cursor: %s\n",
3203                         progname, strerror(errno));
3204                 return rc;
3205         }
3206
3207         rc = llapi_layout_comp_extent_get(layout, &start, &end);
3208         if (rc) {
3209                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3210                         progname, strerror(errno));
3211                 return rc;
3212         }
3213
3214         if (start == 0 && end == 0) {
3215                 rc = layout_extend_comp(layout, start, &end,
3216                                         comp_add ? prev_end : 0);
3217                 if (rc)
3218                         return rc;
3219         }
3220
3221         if (start > prev_end || end < prev_end) {
3222                 fprintf(stderr,
3223                         "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3224                         progname, start, end, prev_end);
3225                 return -EINVAL;
3226         }
3227
3228         rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3229         if (rc) {
3230                 fprintf(stderr,
3231                         "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3232                         progname, prev_end, end, strerror(errno));
3233                 return rc;
3234         }
3235
3236         return ret;
3237 }
3238
3239 static int mirror_adjust_first_extents(struct mirror_args *list)
3240 {
3241         int rc = 0;
3242
3243         if (!list)
3244                 return 0;
3245
3246         while (list) {
3247                 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3248                 if (rc)
3249                         break;
3250                 list = list->m_next;
3251         }
3252
3253         return rc;
3254 }
3255
3256 static inline bool arg_is_eof(char *arg)
3257 {
3258         return !strncmp(arg, "-1", strlen("-1")) ||
3259                !strncmp(arg, "EOF", strlen("EOF")) ||
3260                !strncmp(arg, "eof", strlen("eof"));
3261 }
3262
3263 /**
3264  * lfs_mirror_alloc() - Allocate a mirror argument structure.
3265  *
3266  * Return: Valid mirror_args pointer on success and
3267  *         NULL if memory allocation fails.
3268  */
3269 static struct mirror_args *lfs_mirror_alloc(void)
3270 {
3271         struct mirror_args *mirror = NULL;
3272
3273         while (1) {
3274                 mirror = calloc(1, sizeof(*mirror));
3275                 if (mirror) {
3276                         mirror->m_inherit = false;
3277                         break;
3278                 }
3279
3280                 sleep(1);
3281         }
3282
3283         return mirror;
3284 }
3285
3286 /**
3287  * lfs_mirror_free() - Free memory allocated for a mirror argument
3288  *                     structure.
3289  * @mirror: Previously allocated mirror argument structure by
3290  *          lfs_mirror_alloc().
3291  *
3292  * Free memory allocated for @mirror.
3293  *
3294  * Return: void.
3295  */
3296 static void lfs_mirror_free(struct mirror_args *mirror)
3297 {
3298         if (mirror->m_layout)
3299                 llapi_layout_free(mirror->m_layout);
3300         free(mirror);
3301 }
3302
3303 /**
3304  * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3305  * @mirror_list: Previously allocated mirror list.
3306  *
3307  * Free memory allocated for @mirror_list.
3308  *
3309  * Return: void.
3310  */
3311 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3312 {
3313         struct mirror_args *next_mirror = NULL;
3314
3315         while (mirror_list) {
3316                 next_mirror = mirror_list->m_next;
3317                 lfs_mirror_free(mirror_list);
3318                 mirror_list = next_mirror;
3319         }
3320 }
3321
3322 enum {
3323         LFS_POOL_OPT = 3,
3324         LFS_COMP_COUNT_OPT,
3325         LFS_COMP_START_OPT,
3326         LFS_COMP_FLAGS_OPT,
3327         LFS_COMP_DEL_OPT,
3328         LFS_COMP_SET_OPT,
3329         LFS_COMP_ADD_OPT,
3330         LFS_COMP_NO_VERIFY_OPT,
3331         LFS_PROJID_OPT,
3332         LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3333         LFS_MIRROR_ID_OPT,
3334         LFS_MIRROR_STATE_OPT,
3335         LFS_LAYOUT_COPY,
3336         LFS_MIRROR_INDEX_OPT,
3337         LFS_LAYOUT_FOREIGN_OPT,
3338         LFS_MODE_OPT,
3339         LFS_NEWERXY_OPT,
3340         LFS_INHERIT_RR_OPT,
3341         LFS_FIND_PERM,
3342 };
3343
3344 #ifndef LCME_USER_MIRROR_FLAGS
3345 /* The mirror flags can be set by users at creation time. */
3346 #define LCME_USER_MIRROR_FLAGS  (LCME_FL_PREF_RW)
3347 #endif
3348
3349 /* functions */
3350 static int lfs_setstripe_internal(int argc, char **argv,
3351                                   enum setstripe_origin opc)
3352 {
3353         struct lfs_setstripe_args        lsa = { 0 };
3354         struct llapi_stripe_param       *param = NULL;
3355         struct find_param                migrate_mdt_param = {
3356                 .fp_max_depth = -1,
3357                 .fp_mdt_index = -1,
3358         };
3359         char                            *fname;
3360         int                              result = 0;
3361         int                              result2 = 0;
3362         char                            *end;
3363         int                              c;
3364         int                              delete = 0;
3365         unsigned long long               size_units = 1;
3366         bool                             migrate_mode = false;
3367         bool                             migrate_mdt_mode = false;
3368         bool                             setstripe_mode = false;
3369         bool                             migration_block = false;
3370         __u64                            migration_flags = 0;
3371         __u32                            tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3372         int                              comp_del = 0, comp_set = 0;
3373         int                              comp_add = 0;
3374         __u32                            comp_id = 0;
3375         struct llapi_layout             *layout = NULL;
3376         struct llapi_layout             **lpp = &layout;
3377         bool                             mirror_mode = false;
3378         bool                             has_m_file = false;
3379         __u32                            mirror_count = 0;
3380         enum mirror_flags                mirror_flags = 0;
3381         struct mirror_args              *mirror_list = NULL;
3382         struct mirror_args              *new_mirror = NULL;
3383         struct mirror_args              *last_mirror = NULL;
3384         __u16                            mirror_id = 0;
3385         char                             cmd[PATH_MAX];
3386         bool from_yaml = false;
3387         bool from_copy = false;
3388         char *template = NULL;
3389         bool foreign_mode = false;
3390         char *xattr = NULL;
3391         uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3392         char *mode_opt = NULL;
3393         mode_t previous_umask = 0;
3394         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3395
3396         struct option long_opts[] = {
3397 /* find { .val = '0',   .name = "null",         .has_arg = no_argument }, */
3398 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
3399         /* --block is only valid in migrate mode */
3400         { .val = 'b',   .name = "block",        .has_arg = no_argument },
3401 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
3402         { .val = LFS_COMP_ADD_OPT,
3403                         .name = "comp-add",     .has_arg = no_argument },
3404         { .val = LFS_COMP_ADD_OPT,
3405                         .name = "component-add", .has_arg = no_argument },
3406         { .val = LFS_COMP_DEL_OPT,
3407                         .name = "comp-del",     .has_arg = no_argument },
3408         { .val = LFS_COMP_DEL_OPT,
3409                         .name = "component-del", .has_arg = no_argument },
3410         { .val = LFS_COMP_FLAGS_OPT,
3411                         .name = "comp-flags",   .has_arg = required_argument },
3412         { .val = LFS_COMP_FLAGS_OPT,
3413                         .name = "component-flags",
3414                                                 .has_arg = required_argument },
3415         { .val = LFS_COMP_SET_OPT,
3416                         .name = "comp-set",     .has_arg = no_argument },
3417         { .val = LFS_COMP_SET_OPT,
3418                         .name = "component-set",
3419                                                 .has_arg = no_argument},
3420         { .val = LFS_COMP_NO_VERIFY_OPT,
3421                         .name = "no-verify",    .has_arg = no_argument},
3422         { .val = LFS_LAYOUT_FLAGS_OPT,
3423                         .name = "flags",        .has_arg = required_argument},
3424         { .val = LFS_LAYOUT_FOREIGN_OPT,
3425                         .name = "foreign",      .has_arg = optional_argument},
3426         { .val = LFS_MIRROR_ID_OPT,
3427                         .name = "mirror-id",    .has_arg = required_argument},
3428         { .val = LFS_MODE_OPT,
3429                         .name = "mode",         .has_arg = required_argument},
3430         { .val = LFS_LAYOUT_COPY,
3431                         .name = "copy",         .has_arg = required_argument},
3432         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument},
3433         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument},
3434         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument},
3435         { .val = 'C',   .name = "overstripe-count",
3436                                                 .has_arg = required_argument},
3437         { .val = 'd',   .name = "delete",       .has_arg = no_argument},
3438         { .val = 'd',   .name = "destroy",      .has_arg = no_argument},
3439         /* used with "lfs migrate -m" */
3440         { .val = 'd',   .name = "directory",    .has_arg = no_argument},
3441         /* --non-direct is only valid in migrate mode */
3442         { .val = 'D',   .name = "non-direct",   .has_arg = no_argument },
3443         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument},
3444         { .val = 'E',   .name = "component-end",
3445                                                 .has_arg = required_argument},
3446         { .val = 'f',   .name = "file",         .has_arg = required_argument },
3447 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
3448 /* find { .val = 'g',   .name = "gid",          .has_arg = no_argument }, */
3449 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
3450         { .val = 'h',   .name = "help",         .has_arg = no_argument },
3451         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument},
3452         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument},
3453         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument},
3454         { .val = 'I',   .name = "comp-id",      .has_arg = required_argument},
3455         { .val = 'I',   .name = "component-id", .has_arg = required_argument},
3456 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
3457         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
3458         { .val = 'm',   .name = "mdt",          .has_arg = required_argument},
3459         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument},
3460         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument},
3461         /* --non-block is only valid in migrate mode */
3462         { .val = 'n',   .name = "non-block",    .has_arg = no_argument },
3463         { .val = 'N',   .name = "mirror-count", .has_arg = optional_argument},
3464         { .val = 'o',   .name = "ost",          .has_arg = required_argument },
3465 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3466         { .val = 'o',   .name = "ost-list",     .has_arg = required_argument },
3467         { .val = 'o',   .name = "ost_list",     .has_arg = required_argument },
3468 #endif
3469         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
3470 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
3471 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
3472 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
3473         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
3474         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
3475 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
3476 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3477 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
3478 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
3479         /* --verbose is only valid in migrate mode */
3480         { .val = 'v',   .name = "verbose",      .has_arg = no_argument},
3481         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
3482 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3483         { .val = 'y',   .name = "yaml",         .has_arg = required_argument },
3484         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument},
3485         { .val = 'z',   .name = "extension-size", .has_arg = required_argument},
3486         { .name = NULL } };
3487
3488         setstripe_args_init(&lsa);
3489
3490         migrate_mode = (opc == SO_MIGRATE);
3491         mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3492         setstripe_mode = (opc == SO_SETSTRIPE);
3493         if (opc == SO_MIRROR_DELETE) {
3494                 delete = 1;
3495                 mirror_flags = MF_DESTROY;
3496         }
3497
3498         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3499         progname = cmd;
3500         while ((c = getopt_long(argc, argv,
3501                                 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:y:z:",
3502                                 long_opts, NULL)) >= 0) {
3503                 size_units = 1;
3504                 switch (c) {
3505                 case 0:
3506                         /* Long options. */
3507                         break;
3508                 case LFS_COMP_ADD_OPT:
3509                         comp_add = 1;
3510                         break;
3511                 case LFS_COMP_DEL_OPT:
3512                         comp_del = 1;
3513                         break;
3514                 case LFS_COMP_FLAGS_OPT:
3515                         result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3516                                                 &lsa.lsa_comp_neg_flags);
3517                         if (result != 0)
3518                                 goto usage_error;
3519                         if (mirror_mode && lsa.lsa_comp_neg_flags) {
3520                                 fprintf(stderr,
3521                                         "%s: inverted flags are not supported\n",
3522                                         progname);
3523                                 goto usage_error;
3524                         }
3525                         break;
3526                 case LFS_COMP_SET_OPT:
3527                         comp_set = 1;
3528                         break;
3529                 case LFS_COMP_NO_VERIFY_OPT:
3530                         mirror_flags |= MF_NO_VERIFY;
3531                         break;
3532                 case LFS_MIRROR_ID_OPT: {
3533                         unsigned long int id;
3534
3535                         errno = 0;
3536                         id = strtoul(optarg, &end, 0);
3537                         if (errno != 0 || *end != '\0' || id == 0 ||
3538                             id > UINT16_MAX) {
3539                                 fprintf(stderr,
3540                                         "%s %s: invalid mirror ID '%s'\n",
3541                                         progname, argv[0], optarg);
3542                                 goto usage_error;
3543                         }
3544
3545                         mirror_id = (__u16)id;
3546                         break;
3547                 }
3548                 case LFS_LAYOUT_FLAGS_OPT: {
3549                         uint32_t neg_flags;
3550
3551                         /* check for numeric flags (foreign and mirror cases) */
3552                         if (setstripe_mode && !mirror_mode && !last_mirror) {
3553                                 errno = 0;
3554                                 flags = strtoul(optarg, &end, 16);
3555                                 if (errno != 0 || *end != '\0' ||
3556                                     flags >= UINT32_MAX) {
3557                                         fprintf(stderr,
3558                                                 "%s %s: invalid hex flags '%s'\n",
3559                                                 progname, argv[0], optarg);
3560                                         return CMD_HELP;
3561                                 }
3562                                 if (!foreign_mode) {
3563                                         fprintf(stderr,
3564                                                 "%s %s: hex flags must be specified with --foreign option\n",
3565                                                 progname, argv[0]);
3566                                         return CMD_HELP;
3567                                 }
3568                                 break;
3569                         }
3570
3571                         if (!mirror_mode || !last_mirror) {
3572                                 fprintf(stderr,
3573                                         "error: %s: --flags must be specified with --mirror-count|-N option\n",
3574                                         progname);
3575                                 goto usage_error;
3576                         }
3577
3578                         result = comp_str2flags(optarg, &last_mirror->m_flags,
3579                                                 &neg_flags);
3580                         if (result != 0)
3581                                 goto usage_error;
3582
3583                         if (neg_flags) {
3584                                 fprintf(stderr,
3585                                         "%s: inverted flags are not supported\n",
3586                                         progname);
3587                                 result = -EINVAL;
3588                                 goto usage_error;
3589                         }
3590                         if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3591                                 fprintf(stderr,
3592                                         "%s: unsupported mirror flags: %s\n",
3593                                         progname, optarg);
3594                                 result = -EINVAL;
3595                                 goto error;
3596                         }
3597                         break;
3598                 }
3599                 case LFS_LAYOUT_FOREIGN_OPT:
3600                         if (optarg) {
3601                                 /* check pure numeric */
3602                                 type = strtoul(optarg, &end, 0);
3603                                 if (*end) {
3604                                         /* check name */
3605                                         type = check_foreign_type_name(optarg);
3606                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3607                                                 fprintf(stderr,
3608                                                         "%s %s: unrecognized foreign type '%s'\n",
3609                                                         progname, argv[0],
3610                                                         optarg);
3611                                                 return CMD_HELP;
3612                                         }
3613                                 } else if (type >= UINT32_MAX) {
3614                                         fprintf(stderr,
3615                                                 "%s %s: invalid foreign type '%s'\n",
3616                                                 progname, argv[0], optarg);
3617                                         return CMD_HELP;
3618                                 }
3619                         }
3620                         foreign_mode = true;
3621                         break;
3622                 case LFS_MODE_OPT:
3623                         mode_opt = optarg;
3624                         if (mode_opt) {
3625                                 mode = strtoul(mode_opt, &end, 8);
3626                                 if (*end != '\0') {
3627                                         fprintf(stderr,
3628                                                 "%s %s: bad mode '%s'\n",
3629                                                 progname, argv[0], mode_opt);
3630                                         return CMD_HELP;
3631                                 }
3632                                 previous_umask = umask(0);
3633                         }
3634                         break;
3635                 case LFS_LAYOUT_COPY:
3636                         from_copy = true;
3637                         template = optarg;
3638                         break;
3639                 case 'b':
3640                         if (!migrate_mode) {
3641                                 fprintf(stderr,
3642                                         "%s %s: -b|--block valid only for migrate command\n",
3643                                         progname, argv[0]);
3644                                 goto usage_error;
3645                         }
3646                         migration_block = true;
3647                         break;
3648                 case 'C':
3649                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3650                                 fprintf(stderr,
3651                                         "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3652                                         progname, argv[0]);
3653                                 goto usage_error;
3654                         }
3655                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3656                         /* fall through */
3657                 case 'c':
3658                         errno = 0;
3659                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3660                         if (errno != 0 || *end != '\0'||
3661                             lsa.lsa_stripe_count < -1 ||
3662                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3663                                 fprintf(stderr,
3664                                         "%s %s: invalid stripe count '%s'\n",
3665                                         progname, argv[0], optarg);
3666                                 goto usage_error;
3667                         }
3668
3669                         if (lsa.lsa_stripe_count == -1)
3670                                 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3671                         break;
3672                 case 'd':
3673                         if (migrate_mode) {
3674                                 migrate_mdt_param.fp_max_depth = 1;
3675                         } else {
3676                                 /* delete the default striping pattern */
3677                                 delete = 1;
3678                                 if (opc == SO_MIRROR_SPLIT) {
3679                                         if (has_m_file) {
3680                                                 fprintf(stderr,
3681                                                       "%s %s: -d cannot used with -f\n",
3682                                                         progname, argv[0]);
3683                                                 goto usage_error;
3684                                         }
3685                                         mirror_flags |= MF_DESTROY;
3686                                 }
3687                         }
3688                         break;
3689                 case 'D':
3690                         if (!migrate_mode) {
3691                                 fprintf(stderr,
3692                                         "%s %s: -D|--non-direct is valid only for migrate command\n",
3693                                         progname, argv[0]);
3694                                 goto usage_error;
3695                         }
3696                         migration_flags |= MIGRATION_NONDIRECT;
3697                         break;
3698                 case 'E':
3699                         if (lsa.lsa_comp_end != 0) {
3700                                 result = comp_args_to_layout(lpp, &lsa, true);
3701                                 if (result) {
3702                                         fprintf(stderr, "%s: invalid layout\n",
3703                                                 progname);
3704                                         goto usage_error;
3705                                 }
3706
3707                                 setstripe_args_init_inherit(&lsa);
3708                         }
3709
3710                         if (arg_is_eof(optarg)) {
3711                                 lsa.lsa_comp_end = LUSTRE_EOF;
3712                         } else {
3713                                 result = llapi_parse_size(optarg,
3714                                                           &lsa.lsa_comp_end,
3715                                                           &size_units, 0);
3716                                 /* assume units of KB if too small */
3717                                 if (lsa.lsa_comp_end < 4096)
3718                                         lsa.lsa_comp_end *= 1024;
3719                                 if (result ||
3720                                     lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3721                                         fprintf(stderr,
3722                                                 "%s %s: invalid component end '%s'\n",
3723                                                 progname, argv[0], optarg);
3724                                         goto usage_error;
3725                                 }
3726                         }
3727                         break;
3728                 case 'H':
3729                         if (!migrate_mode) {
3730                                 fprintf(stderr,
3731                                         "--mdt-hash is valid only for migrate command\n");
3732                                 return CMD_HELP;
3733                         }
3734
3735                         lsa.lsa_pattern = check_hashtype(optarg);
3736                         if (lsa.lsa_pattern == 0) {
3737                                 fprintf(stderr,
3738                                         "%s %s: bad stripe hash type '%s'\n",
3739                                         progname, argv[0], optarg);
3740                                 return CMD_HELP;
3741                         }
3742                         break;
3743                 case 'i':
3744                         errno = 0;
3745                         lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3746                         if (errno != 0 || *end != '\0' ||
3747                             lsa.lsa_stripe_off < -1 ||
3748                             lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3749                                 fprintf(stderr,
3750                                         "%s %s: invalid stripe offset '%s'\n",
3751                                         progname, argv[0], optarg);
3752                                 goto usage_error;
3753                         }
3754                         if (lsa.lsa_stripe_off == -1)
3755                                 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3756                         break;
3757                 case 'I':
3758                         comp_id = strtoul(optarg, &end, 0);
3759                         if (*end != '\0' || comp_id == 0 ||
3760                             comp_id > LCME_ID_MAX) {
3761                                 fprintf(stderr,
3762                                         "%s %s: invalid component ID '%s'\n",
3763                                         progname, argv[0], optarg);
3764                                 goto usage_error;
3765                         }
3766                         break;
3767                 case 'f':
3768                         if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3769                                 fprintf(stderr,
3770                                         "error: %s: invalid option: %s\n",
3771                                         progname, argv[optopt + 1]);
3772                                 goto usage_error;
3773                         }
3774                         if (opc == SO_MIRROR_EXTEND) {
3775                                 if (!last_mirror) {
3776                                         fprintf(stderr,
3777                                 "error: %s: '-N' must exist in front of '%s'\n",
3778                                                 progname, argv[optopt + 1]);
3779                                         goto usage_error;
3780                                 }
3781                                 last_mirror->m_file = optarg;
3782                                 last_mirror->m_count = 1;
3783                         } else {
3784                                 /* mirror split */
3785                                 if (!mirror_list)
3786                                         mirror_list = lfs_mirror_alloc();
3787                                 mirror_list->m_file = optarg;
3788                         }
3789                         has_m_file = true;
3790                         break;
3791                 case 'L':
3792                         if (strcmp(argv[optind - 1], "mdt") == 0) {
3793                                 /* Can be only the first component */
3794                                 if (layout) {
3795                                         result = -EINVAL;
3796                                         fprintf(stderr,
3797                                                 "error: 'mdt' layout can be only the first one\n");
3798                                         goto error;
3799                                 }
3800                                 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3801                                         result = -EFBIG;
3802                                         fprintf(stderr,
3803                                                 "error: 'mdt' layout size is too big\n");
3804                                         goto error;
3805                                 }
3806                                 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3807                                 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3808                         } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3809                                 result = -EINVAL;
3810                                 fprintf(stderr,
3811                                         "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3812                                         argv[optind]);
3813                                 goto error;
3814                         }
3815                         break;
3816                 case 'm':
3817                         if (!migrate_mode) {
3818                                 fprintf(stderr,
3819                                         "%s %s: -m|--mdt-index is valid only for migrate command\n",
3820                                         progname, argv[0]);
3821                                 goto usage_error;
3822                         }
3823                         migrate_mdt_mode = true;
3824                         lsa.lsa_nr_tgts = parse_targets(tgts,
3825                                                 sizeof(tgts) / sizeof(__u32),
3826                                                 lsa.lsa_nr_tgts, optarg, NULL);
3827                         if (lsa.lsa_nr_tgts < 0) {
3828                                 fprintf(stderr,
3829                                         "%s: invalid MDT target(s) '%s'\n",
3830                                         progname, optarg);
3831                                 goto usage_error;
3832                         }
3833
3834                         lsa.lsa_tgts = tgts;
3835                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3836                                 lsa.lsa_stripe_off = tgts[0];
3837                         break;
3838                 case 'n':
3839                         if (!migrate_mode) {
3840                                 fprintf(stderr,
3841                                         "%s %s: -n|--non-block valid only for migrate command\n",
3842                                         progname, argv[0]);
3843                                 goto usage_error;
3844                         }
3845                         migration_flags |= MIGRATION_NONBLOCK;
3846                         break;
3847                 case 'N':
3848                         if (opc == SO_SETSTRIPE) {
3849                                 opc = SO_MIRROR_CREATE;
3850                                 mirror_mode = true;
3851                         }
3852                         mirror_count = 1;
3853                         if (optarg) {
3854                                 errno = 0;
3855                                 mirror_count = strtoul(optarg, &end, 0);
3856                                 if (errno != 0 || *end != '\0' ||
3857                                     mirror_count == 0 ||
3858                                     mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3859                                         fprintf(stderr,
3860                                                 "error: %s: bad mirror count: %s\n",
3861                                                 progname, optarg);
3862                                         result = -EINVAL;
3863                                         goto error;
3864                                 }
3865                         }
3866
3867                         new_mirror = lfs_mirror_alloc();
3868                         new_mirror->m_count = mirror_count;
3869
3870                         if (!mirror_list)
3871                                 mirror_list = new_mirror;
3872
3873                         if (last_mirror) {
3874                                 /* wrap up last mirror */
3875                                 if (!setstripe_args_specified(&lsa))
3876                                         last_mirror->m_inherit = true;
3877                                 if (lsa.lsa_comp_end == 0)
3878                                         lsa.lsa_comp_end = LUSTRE_EOF;
3879
3880                                 result = comp_args_to_layout(lpp, &lsa, true);
3881                                 if (result) {
3882                                         lfs_mirror_free(new_mirror);
3883                                         goto error;
3884                                 }
3885
3886                                 setstripe_args_init_inherit(&lsa);
3887
3888                                 last_mirror->m_next = new_mirror;
3889                         }
3890
3891                         last_mirror = new_mirror;
3892                         lpp = &last_mirror->m_layout;
3893                         break;
3894                 case 'o':
3895 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3896                         if (strcmp(argv[optind - 1], "--ost-list") == 0)
3897                                 fprintf(stderr,
3898                                         "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3899 #endif
3900                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3901                                 fprintf(stderr,
3902                                         "%s %s: -o|--ost incompatible with DoM layout\n",
3903                                         progname, argv[0]);
3904                                 goto usage_error;
3905                         }
3906                         /*
3907                          * -o allows overstriping, and must note it because
3908                          * parse_targets is shared with MDT striping, which
3909                          * does not allow duplicates
3910                          */
3911                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3912                         lsa.lsa_nr_tgts = parse_targets(tgts,
3913                                                 sizeof(tgts) / sizeof(__u32),
3914                                                 lsa.lsa_nr_tgts, optarg,
3915                                                 &lsa.lsa_pattern);
3916                         if (lsa.lsa_nr_tgts < 0) {
3917                                 fprintf(stderr,
3918                                         "%s %s: invalid OST target(s) '%s'\n",
3919                                         progname, argv[0], optarg);
3920                                 goto usage_error;
3921                         }
3922
3923                         lsa.lsa_tgts = tgts;
3924                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3925                                 lsa.lsa_stripe_off = tgts[0];
3926                         break;
3927                 case 'p':
3928                         if (!optarg)
3929                                 goto usage_error;
3930                         lsa.lsa_pool_name = optarg;
3931
3932                         if (strlen(lsa.lsa_pool_name) == 0 ||
3933                             strncmp(lsa.lsa_pool_name, "none",
3934                                     LOV_MAXPOOLNAME) == 0)
3935                                 lsa.lsa_pool_name = NULL;
3936                         break;
3937                 case 'S':
3938                         result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3939                                                   &size_units, 0);
3940                         /* assume units of KB if too small to be valid */
3941                         if (lsa.lsa_stripe_size < 4096)
3942                                 lsa.lsa_stripe_size *= 1024;
3943                         if (result ||
3944                             lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3945                                 fprintf(stderr,
3946                                         "%s %s: invalid stripe size '%s'\n",
3947                                         progname, argv[0], optarg);
3948                                 goto usage_error;
3949                         }
3950                         break;
3951                 case 'v':
3952                         if (!migrate_mode) {
3953                                 fprintf(stderr,
3954                                         "%s %s: -v|--verbose valid only for migrate command\n",
3955                                         progname, argv[0]);
3956                                 goto usage_error;
3957                         }
3958                         migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3959                         migration_flags = MIGRATION_VERBOSE;
3960                         break;
3961                 case 'x':
3962                         xattr = optarg;
3963                         break;
3964                 case 'y':
3965                         from_yaml = true;
3966                         template = optarg;
3967                         break;
3968                 case 'z':
3969                         result = llapi_parse_size(optarg,
3970                                                   &lsa.lsa_extension_size,
3971                                                   &size_units, 0);
3972                         if (result) {
3973                                 fprintf(stderr,
3974                                         "%s %s: invalid extension size '%s'\n",
3975                                         progname, argv[0], optarg);
3976                                 goto usage_error;
3977                         }
3978
3979                         lsa.lsa_extension_comp = true;
3980                         break;
3981                 default:
3982                         fprintf(stderr, "%s: unrecognized option '%s'\n",
3983                                 progname, argv[optind - 1]);
3984                 case 'h':
3985                         goto usage_error;
3986                 }
3987         }
3988
3989         fname = argv[optind];
3990
3991         if (optind == argc) {
3992                 fprintf(stderr, "%s %s: FILE must be specified\n",
3993                         progname, argv[0]);
3994                 goto usage_error;
3995         }
3996
3997         /* lfs migrate $filename should keep the file's layout by default */
3998         if (migrate_mode && !layout && !from_yaml &&
3999             !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4000                 from_copy = true;
4001
4002         if (xattr && !foreign_mode) {
4003                 /*
4004                  * only print a warning as this is harmless and will be ignored
4005                  */
4006                 fprintf(stderr,
4007                         "%s %s: xattr has been specified for non-foreign layout\n",
4008                         progname, argv[0]);
4009         } else if (foreign_mode && !xattr) {
4010                 fprintf(stderr,
4011                         "%s %s: xattr must be provided in foreign mode\n",
4012                         progname, argv[0]);
4013                 goto usage_error;
4014         }
4015
4016         if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4017             comp_set || comp_id || delete || from_copy ||
4018             setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4019             lsa.lsa_tgts)) {
4020                 fprintf(stderr,
4021                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4022                         progname, argv[0]);
4023                 return CMD_HELP;
4024         }
4025
4026         if (mirror_mode && mirror_count == 0) {
4027                 fprintf(stderr,
4028                         "error: %s: --mirror-count|-N option is required\n",
4029                         progname);
4030                 result = -EINVAL;
4031                 goto error;
4032         }
4033
4034         if (mirror_mode) {
4035                 if (!setstripe_args_specified(&lsa))
4036                         last_mirror->m_inherit = true;
4037                 if (lsa.lsa_comp_end == 0)
4038                         lsa.lsa_comp_end = LUSTRE_EOF;
4039         }
4040
4041         if (lsa.lsa_comp_end != 0) {
4042                 result = comp_args_to_layout(lpp, &lsa, true);
4043                 if (result) {
4044                         fprintf(stderr, "error: %s: invalid layout\n",
4045                                 progname);
4046                         result = -EINVAL;
4047                         goto error;
4048                 }
4049         }
4050
4051         if (mirror_flags & MF_NO_VERIFY) {
4052                 if (opc != SO_MIRROR_EXTEND) {
4053                         fprintf(stderr,
4054                                 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4055                                 progname);
4056                         result = -EINVAL;
4057                         goto error;
4058                 } else if (!has_m_file) {
4059                         fprintf(stderr,
4060                                 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4061                                 progname);
4062                         result = -EINVAL;
4063                         goto error;
4064                 }
4065         }
4066
4067         if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4068                 fprintf(stderr,
4069                         "%s %s: --component-set doesn't have component-id set\n",
4070                         progname, argv[0]);
4071                 goto usage_error;
4072         }
4073
4074         if ((delete + comp_set + comp_del + comp_add) > 1) {
4075                 fprintf(stderr,
4076                         "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4077                         progname, argv[0]);
4078                 goto usage_error;
4079         }
4080
4081         if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4082                        lsa.lsa_comp_flags != 0 || layout != NULL)) {
4083                 fprintf(stderr,
4084                         "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4085                         progname, argv[0]);
4086                 goto usage_error;
4087         }
4088
4089         if ((comp_set || comp_del) &&
4090             (setstripe_args_specified(&lsa) || layout != NULL)) {
4091                 fprintf(stderr,
4092                         "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4093                         progname, argv[0]);
4094                 goto usage_error;
4095         }
4096
4097         if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4098                 fprintf(stderr,
4099                         "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4100                         progname, argv[0]);
4101                 goto usage_error;
4102         }
4103
4104         if (comp_add || comp_del) {
4105                 struct stat st;
4106
4107                 result = lstat(fname, &st);
4108                 if (result == 0 && S_ISDIR(st.st_mode)) {
4109                         fprintf(stderr,
4110                                 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4111                                 progname);
4112                         goto usage_error;
4113                 }
4114
4115                 if (mirror_mode) {
4116                         fprintf(stderr,
4117                                 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4118                                 progname);
4119                         goto usage_error;
4120                 }
4121         }
4122
4123         if (comp_add) {
4124                 if (!layout) {
4125                         fprintf(stderr,
4126                                 "%s %s: option -E must be specified with --component-add\n",
4127                                 progname, argv[0]);
4128                         goto usage_error;
4129                 }
4130         }
4131
4132         if (from_yaml && from_copy) {
4133                 fprintf(stderr,
4134                         "%s: can't specify --yaml and --copy together\n",
4135                         progname);
4136                 goto error;
4137         }
4138
4139         if ((from_yaml || from_copy) &&
4140             (setstripe_args_specified(&lsa) || layout != NULL)) {
4141                 fprintf(stderr,
4142                         "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4143                         argv[0]);
4144                 goto error;
4145         }
4146
4147         if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4148                 fprintf(stderr,
4149                         "%s %s: options --non-block and --block are mutually exclusive\n",
4150                         progname, argv[0]);
4151                 goto usage_error;
4152         }
4153
4154         if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4155             opc != SO_MIRROR_DELETE && comp_id != 0) {
4156                 fprintf(stderr,
4157                         "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4158                         progname);
4159                 goto usage_error;
4160         }
4161
4162         if (migrate_mdt_mode) {
4163                 struct lmv_user_md *lmu;
4164
4165                 /* initialize migrate mdt parameters */
4166                 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4167                                                  LMV_USER_MAGIC_SPECIFIC));
4168                 if (!lmu) {
4169                         fprintf(stderr,
4170                                 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4171                                 progname, argv[0], strerror(ENOMEM));
4172                         result = -ENOMEM;
4173                         goto error;
4174                 }
4175                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4176                         lmu->lum_stripe_count = lsa.lsa_stripe_count;
4177                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4178                         fprintf(stderr,
4179                                 "%s %s: migrate should specify MDT index\n",
4180                                 progname, argv[0]);
4181                         free(lmu);
4182                         goto usage_error;
4183                 }
4184                 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4185                 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4186                         lmu->lum_hash_type = lsa.lsa_pattern;
4187                 else
4188                         lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4189                 if (lsa.lsa_pool_name) {
4190                         strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4191                                 sizeof(lmu->lum_pool_name) - 1);
4192                         lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4193                 }
4194                 if (lsa.lsa_nr_tgts > 1) {
4195                         int i;
4196
4197                         if (lsa.lsa_stripe_count > 0 &&
4198                             lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4199                             lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4200                                 fprintf(stderr,
4201                                         "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4202                                         progname, lsa.lsa_stripe_count,
4203                                         lsa.lsa_nr_tgts);
4204                                 free(lmu);
4205                                 goto usage_error;
4206                         }
4207
4208                         lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4209                         lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4210                         for (i = 0; i < lsa.lsa_nr_tgts; i++)
4211                                 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4212                 } else {
4213                         lmu->lum_magic = LMV_USER_MAGIC;
4214                 }
4215
4216                 migrate_mdt_param.fp_lmv_md = lmu;
4217                 migrate_mdt_param.fp_migrate = 1;
4218         } else if (!layout) {
4219                 if (lsa_args_stripe_count_check(&lsa))
4220                         goto usage_error;
4221
4222                 /* initialize stripe parameters */
4223                 param = calloc(1, offsetof(typeof(*param),
4224                                lsp_osts[lsa.lsa_nr_tgts]));
4225                 if (!param) {
4226                         fprintf(stderr,
4227                                 "%s %s: cannot allocate memory for parameters: %s\n",
4228                                 progname, argv[0], strerror(ENOMEM));
4229                         result = -ENOMEM;
4230                         goto error;
4231                 }
4232
4233                 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4234                         param->lsp_stripe_size = lsa.lsa_stripe_size;
4235                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4236                         if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4237                                 param->lsp_stripe_count = -1;
4238                         else
4239                                 param->lsp_stripe_count = lsa.lsa_stripe_count;
4240                 }
4241                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4242                         param->lsp_stripe_offset = -1;
4243                 else
4244                         param->lsp_stripe_offset = lsa.lsa_stripe_off;
4245                 param->lsp_stripe_pattern =
4246                                 llapi_pattern_to_lov(lsa.lsa_pattern);
4247                 if (param->lsp_stripe_pattern == EINVAL) {
4248                         fprintf(stderr, "error: %s: invalid stripe pattern\n",
4249                                 argv[0]);
4250                         free(param);
4251                         goto usage_error;
4252                 }
4253                 param->lsp_pool = lsa.lsa_pool_name;
4254                 param->lsp_is_specific = false;
4255
4256                 if (lsa.lsa_nr_tgts > 0) {
4257                         param->lsp_is_specific = true;
4258                         param->lsp_stripe_count = lsa.lsa_nr_tgts;
4259                         memcpy(param->lsp_osts, tgts,
4260                                sizeof(*tgts) * lsa.lsa_nr_tgts);
4261                 }
4262         }
4263
4264         if (from_yaml) {
4265                 /* generate a layout from a YAML template */
4266                 result = lfs_comp_create_from_yaml(template, &layout,
4267                                                    &lsa, tgts);
4268                 if (result) {
4269                         fprintf(stderr,
4270                                 "error: %s: can't create composite layout from template file %s\n",
4271                                 argv[0], template);
4272                         goto error;
4273                 }
4274         }
4275
4276         if (layout != NULL || mirror_list != NULL) {
4277                 if (mirror_list)
4278                         result = mirror_adjust_first_extents(mirror_list);
4279                 else
4280                         result = layout_adjust_first_extent(fname, layout,
4281                                                             comp_add);
4282                 if (result == -ENODATA)
4283                         comp_add = 0;
4284                 else if (result != 0) {
4285                         fprintf(stderr, "error: %s: invalid layout\n",
4286                                 progname);
4287                         goto error;
4288                 }
4289         }
4290
4291         for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4292                 if (from_copy) {
4293                         layout = llapi_layout_get_by_path(template ?: fname, 0);
4294                         if (!layout) {
4295                                 fprintf(stderr,
4296                                         "%s: can't create composite layout from file %s: %s\n",
4297                                         progname, template ?: fname,
4298                                         strerror(errno));
4299                                 result = -errno;
4300                                 goto error;
4301                         }
4302                 }
4303
4304                 if (migrate_mdt_mode) {
4305                         result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4306                 } else if (migrate_mode) {
4307                         result = lfs_migrate(fname, migration_flags, param,
4308                                              layout);
4309                 } else if (comp_set != 0) {
4310                         result = lfs_component_set(fname, comp_id,
4311                                                    lsa.lsa_pool_name,
4312                                                    lsa.lsa_comp_flags,
4313                                                    lsa.lsa_comp_neg_flags);
4314                 } else if (comp_del != 0) {
4315                         result = lfs_component_del(fname, comp_id,
4316                                                    lsa.lsa_comp_flags,
4317                                                    lsa.lsa_comp_neg_flags);
4318                 } else if (comp_add != 0) {
4319                         result = lfs_component_add(fname, layout);
4320                 } else if (opc == SO_MIRROR_CREATE) {
4321                         result = mirror_create(fname, mirror_list);
4322                 } else if (opc == SO_MIRROR_EXTEND) {
4323                         result = mirror_extend(fname, mirror_list,
4324                                                mirror_flags);
4325                 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4326                         if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4327                                 fprintf(stderr,
4328                                         "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4329                                         progname, fname);
4330                                 goto usage_error;
4331                         }
4332                         if (lsa.lsa_pool_name)
4333                                 mirror_flags |= MF_COMP_POOL;
4334                         else if (mirror_id != 0)
4335                                 comp_id = mirror_id;
4336                         else
4337                                 mirror_flags |= MF_COMP_ID;
4338                         if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4339                                 fprintf(stderr,
4340                                         "%s: the file specified by -f cannot be same as the source file '%s'\n",
4341                                         progname, fname);
4342                                 goto usage_error;
4343                         }
4344                         result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4345                                               mirror_flags,
4346                                               has_m_file ? mirror_list->m_file :
4347                                               NULL);
4348                 } else if (layout) {
4349                         result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4350                                                       mode, layout);
4351                         if (result >= 0) {
4352                                 close(result);
4353                                 result = 0;
4354                         }
4355                 } else if (foreign_mode) {
4356                         result = llapi_file_create_foreign(fname, mode, type,
4357                                                            flags, xattr);
4358                         if (result >= 0) {
4359                                 close(result);
4360                                 result = 0;
4361                         }
4362                 } else {
4363                         result = llapi_file_open_param(fname,
4364                                                        O_CREAT | O_WRONLY,
4365                                                        mode, param);
4366                         if (result >= 0) {
4367                                 close(result);
4368                                 result = 0;
4369                         }
4370                 }
4371                 if (result) {
4372                         /* Save the first error encountered. */
4373                         if (result2 == 0)
4374                                 result2 = result;
4375                         continue;
4376                 }
4377         }
4378
4379         if (mode_opt)
4380                 umask(previous_umask);
4381
4382         free(param);
4383         free(migrate_mdt_param.fp_lmv_md);
4384         llapi_layout_free(layout);
4385         lfs_mirror_list_free(mirror_list);
4386         return result2;
4387 usage_error:
4388         result = CMD_HELP;
4389 error:
4390         llapi_layout_free(layout);
4391         lfs_mirror_list_free(mirror_list);
4392         return result;
4393 }
4394
4395 static int lfs_poollist(int argc, char **argv)
4396 {
4397         if (argc != 2)
4398                 return CMD_HELP;
4399
4400         return llapi_poollist(argv[1]);
4401 }
4402
4403 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4404 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4405                        char *str)
4406 {
4407         long long t = 0;
4408         int sign = 0;
4409         char *endptr = "AD";
4410         char *timebuf;
4411
4412         if (str[0] == '+')
4413                 sign = 1;
4414         else if (str[0] == '-')
4415                 sign = -1;
4416
4417         if (sign)
4418                 str++;
4419
4420         for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4421                 long long val = strtoll(timebuf, &endptr, 0);
4422                 int unit = 1;
4423
4424                 switch (*endptr) {
4425                 case  'y':
4426                         unit *= 52; /* 52 weeks + 1 day below */
4427                 case  'w':      /* fallthrough */
4428                         unit *= 7;
4429                         if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4430                                 param->fp_time_margin *= (1 + unit / 52);
4431                         unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4432                 case '\0': /* days are default unit if none used */
4433                 case  'd':      /* fallthrough */
4434                         unit *= 24;
4435                 case  'h':      /* fallthrough */
4436                         unit *= 60;
4437                 case  'm':      /* fallthrough */
4438                         unit *= 60;
4439                 case  's':      /* fallthrough */
4440                         break;
4441                         /* don't need to multiply by 1 for seconds */
4442                 default:
4443                         fprintf(stderr,
4444                                 "%s find: bad time string '%s': %s\n",
4445                                 progname, timebuf, strerror(EINVAL));
4446                         return LONG_MAX;
4447                 }
4448
4449                 if (param->fp_time_margin == 0 ||
4450                     (*endptr && unit < param->fp_time_margin))
4451                         param->fp_time_margin = unit;
4452
4453                 t += val * unit;
4454         }
4455         if (*time < t) {
4456                 if (sign != 0)
4457                         str--;
4458                 fprintf(stderr, "%s find: bad time '%s': too large\n",
4459                         progname, str);
4460                 return LONG_MAX;
4461         }
4462
4463         *set = *time - t;
4464
4465         return sign;
4466 }
4467
4468 static int str2quotaid(__u32 *id, const char *arg)
4469 {
4470         unsigned long int projid_tmp = 0;
4471         char *endptr = NULL;
4472
4473         projid_tmp = strtoul(arg, &endptr, 10);
4474         if (*endptr != '\0')
4475                 return -EINVAL;
4476         /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4477         if (projid_tmp >= UINT32_MAX)
4478                 return -ERANGE;
4479
4480         *id = projid_tmp;
4481         return 0;
4482 }
4483
4484 static int name2uid(unsigned int *id, const char *name)
4485 {
4486         struct passwd *passwd;
4487
4488         passwd = getpwnam(name);
4489         if (!passwd)
4490                 return -ENOENT;
4491         *id = passwd->pw_uid;
4492
4493         return 0;
4494 }
4495
4496 static int name2gid(unsigned int *id, const char *name)
4497 {
4498         struct group *group;
4499
4500         group = getgrnam(name);
4501         if (!group)
4502                 return -ENOENT;
4503         *id = group->gr_gid;
4504
4505         return 0;
4506 }
4507
4508 static inline int name2projid(unsigned int *id, const char *name)
4509 {
4510         return -ENOTSUP;
4511 }
4512
4513 static int uid2name(char **name, unsigned int id)
4514 {
4515         struct passwd *passwd;
4516
4517         passwd = getpwuid(id);
4518         if (!passwd)
4519                 return -ENOENT;
4520         *name = passwd->pw_name;
4521
4522         return 0;
4523 }
4524
4525 static inline int gid2name(char **name, unsigned int id)
4526 {
4527         struct group *group;
4528
4529         group = getgrgid(id);
4530         if (!group)
4531                 return -ENOENT;
4532         *name = group->gr_name;
4533
4534         return 0;
4535 }
4536
4537 static int name2layout(__u32 *layout, char *name)
4538 {
4539         char *ptr, *layout_name;
4540
4541         *layout = 0;
4542         for (ptr = name; ; ptr = NULL) {
4543                 layout_name = strtok(ptr, ",");
4544                 if (!layout_name)
4545                         break;
4546                 if (strcmp(layout_name, "released") == 0)
4547                         *layout |= LOV_PATTERN_F_RELEASED;
4548                 else if (strcmp(layout_name, "raid0") == 0)
4549                         *layout |= LOV_PATTERN_RAID0;
4550                 else if (strcmp(layout_name, "mdt") == 0)
4551                         *layout |= LOV_PATTERN_MDT;
4552                 else if (strcmp(layout_name, "overstriping") == 0)
4553                         *layout |= LOV_PATTERN_OVERSTRIPING;
4554                 else
4555                         return -1;
4556         }
4557         return 0;
4558 }
4559
4560 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4561 {
4562         int loop;
4563         int user, group, other;
4564         int who, all;
4565         char c, op;
4566         mode_t perm;
4567         mode_t usermask;
4568         mode_t previous_flags;
4569
4570         user = group = other = 0;
4571         all = 0;
4572         loop = 1;
4573         perm = 0;
4574         previous_flags = 0;
4575         *end = input;
4576         usermask = 0;
4577
4578         while (loop) {
4579                 switch (*input) {
4580                 case 'u':
4581                         user = 1;
4582                         break;
4583                 case 'g':
4584                         group = 1;
4585                         break;
4586                 case 'o':
4587                         other = 1;
4588                         break;
4589                 case 'a':
4590                         user = group = other = 1;
4591                         all = 1;
4592                         break;
4593                 default:
4594                         loop = 0;
4595                 }
4596
4597                 if (loop)
4598                         input++;
4599         }
4600
4601         who = user || group || other;
4602         if (!who) {
4603                 /* get the umask */
4604                 usermask = umask(0022);
4605                 umask(usermask);
4606                 usermask &= 07777;
4607         }
4608
4609         if (*input == '-' || *input == '+' || *input == '=')
4610                 op = *input++;
4611         else
4612                 /* operation is required */
4613                 return -1;
4614
4615         /* get the flags in *outmode */
4616         switch (*input) {
4617         case 'u':
4618                 previous_flags = (*outmode & 0700);
4619                 perm |= user  ? previous_flags : 0;
4620                 perm |= group ? (previous_flags >> 3) : 0;
4621                 perm |= other ? (previous_flags >> 6) : 0;
4622                 input++;
4623                 goto write_perm;
4624         case 'g':
4625                 previous_flags = (*outmode & 0070);
4626                 perm |= user  ? (previous_flags << 3) : 0;
4627                 perm |= group ? previous_flags : 0;
4628                 perm |= other ? (previous_flags >> 3) : 0;
4629                 input++;
4630                 goto write_perm;
4631         case 'o':
4632                 previous_flags = (*outmode & 0007);
4633                 perm |= user  ? (previous_flags << 6) : 0;
4634                 perm |= group ? (previous_flags << 3) : 0;
4635                 perm |= other ? previous_flags : 0;
4636                 input++;
4637                 goto write_perm;
4638         default:
4639                 break;
4640         }
4641
4642         /* this part is optional,
4643          * if empty perm = 0 and *outmode is not modified
4644          */
4645         loop = 1;
4646         while (loop) {
4647                 c = *input;
4648                 switch (c) {
4649                 case 'r':
4650                         perm |= user  ? 0400 : 0;
4651                         perm |= group ? 0040 : 0;
4652                         perm |= other ? 0004 : 0;
4653                         /* set read permission for uog except for umask's
4654                          * permissions
4655                          */
4656                         perm |= who   ? 0 : (0444 & ~usermask);
4657                         break;
4658                 case 'w':
4659                         perm |= user  ? 0200 : 0;
4660                         perm |= group ? 0020 : 0;
4661                         perm |= other ? 0002 : 0;
4662                         /* set write permission for uog except for umask'
4663                          * permissions
4664                          */
4665                         perm |= who   ? 0 : (0222 & ~usermask);
4666                         break;
4667                 case 'x':
4668                         perm |= user  ? 0100 : 0;
4669                         perm |= group ? 0010 : 0;
4670                         perm |= other ? 0001 : 0;
4671                         /* set execute permission for uog except for umask'
4672                          * permissions
4673                          */
4674                         perm |= who   ? 0 : (0111 & ~usermask);
4675                         break;
4676                 case 'X':
4677                         /*
4678                          * Adds execute permission to 'u', 'g' and/or 'g' if
4679                          * specified and either 'u', 'g' or 'o' already has
4680                          * execute permissions.
4681                          */
4682                         if ((*outmode & 0111) != 0) {
4683                                 perm |= user  ? 0100 : 0;
4684                                 perm |= group ? 0010 : 0;
4685                                 perm |= other ? 0001 : 0;
4686                                 perm |= !who  ? 0111 : 0;
4687                         }
4688                         break;
4689                 case 's':
4690                         /* s is ignored if o is given, but it's not an error */
4691                         if (other && !group && !user)
4692                                 break;
4693                         perm |= user  ? S_ISUID : 0;
4694                         perm |= group ? S_ISGID : 0;
4695                         break;
4696                 case 't':
4697                         /* 't' should be used when 'a' is given
4698                          * or who is empty
4699                          */
4700                         perm |= (!who || all) ? S_ISVTX : 0;
4701                         /* using ugo with t is not an error */
4702                         break;
4703                 default:
4704                         loop = 0;
4705                         break;
4706                 }
4707                 if (loop)
4708                         input++;
4709         }
4710
4711 write_perm:
4712         /* uog flags should be only one character long */
4713         if (previous_flags && (*input != '\0' && *input != ','))
4714                 return -1;
4715
4716         switch (op) {
4717         case '-':
4718                 /* remove the flags from outmode */
4719                 *outmode &= ~perm;
4720                 break;
4721         case '+':
4722                 /* add the flags to outmode */
4723                 *outmode |= perm;
4724                 break;
4725         case '=':
4726                 /* set the flags of outmode to perm */
4727                 if (perm != 0)
4728                         *outmode = perm;
4729                 break;
4730         }
4731
4732         *end = input;
4733         return 0;
4734 }
4735
4736 static int str2mode_t(const char *input, mode_t *outmode)
4737 {
4738         int ret;
4739         const char *iter;
4740
4741         ret = 0;
4742
4743         if (*input >= '0' && *input <= '7') {
4744                 /* parse octal representation */
4745                 char *end;
4746
4747                 iter = input;
4748
4749                 /* look for invalid digits in octal representation */
4750                 while (isdigit(*iter))
4751                         if (*iter++ > '7')
4752                                 return -1;
4753
4754                 errno = 0;
4755                 *outmode = strtoul(input, &end, 8);
4756
4757                 if (errno != 0 || *outmode > 07777) {
4758                         *outmode = 0;
4759                         ret = -1;
4760                 }
4761
4762         } else if (*input == '8' || *input == '9') {
4763                 /* error: invalid octal number */
4764                 ret = -1;
4765         } else {
4766                 /* parse coma seperated list of symbolic representation */
4767                 int rc;
4768                 const char *end;
4769
4770                 *outmode = 0;
4771                 rc = 0;
4772                 end = NULL;
4773
4774                 do {
4775                         rc = parse_symbolic(input, outmode, &end);
4776                         if (rc)
4777                                 return -1;
4778
4779                         input = end+1;
4780                 } while (*end == ',');
4781
4782                 if (*end != '\0')
4783                         ret = -1;
4784         }
4785         return ret;
4786 }
4787
4788 static int lfs_find(int argc, char **argv)
4789 {
4790         int c, rc;
4791         int ret = 0;
4792         time_t t;
4793         struct find_param param = {
4794                 .fp_max_depth = -1,
4795                 .fp_quiet = 1,
4796                 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4797         };
4798         struct option long_opts[] = {
4799         { .val = 'A',   .name = "atime",        .has_arg = required_argument },
4800         { .val = 'b',   .name = "blocks",       .has_arg = required_argument },
4801         { .val = 'B',   .name = "btime",        .has_arg = required_argument },
4802         { .val = 'B',   .name = "Btime",        .has_arg = required_argument },
4803         { .val = LFS_COMP_COUNT_OPT,
4804                         .name = "comp-count",   .has_arg = required_argument },
4805         { .val = LFS_COMP_COUNT_OPT,
4806                         .name = "component-count",
4807                                                 .has_arg = required_argument },
4808         { .val = LFS_COMP_FLAGS_OPT,
4809                         .name = "comp-flags",   .has_arg = required_argument },
4810         { .val = LFS_COMP_FLAGS_OPT,
4811                         .name = "component-flags",
4812                                                 .has_arg = required_argument },
4813         { .val = LFS_COMP_START_OPT,
4814                         .name = "comp-start",   .has_arg = required_argument },
4815         { .val = LFS_COMP_START_OPT,
4816                         .name = "component-start",
4817                                                 .has_arg = required_argument },
4818         { .val = LFS_MIRROR_STATE_OPT,
4819                         .name = "mirror-state", .has_arg = required_argument },
4820         { .val = LFS_NEWERXY_OPT,
4821                         .name = "newer",        .has_arg = required_argument},
4822         { .val = LFS_NEWERXY_OPT,
4823                         .name = "neweraa",      .has_arg = required_argument},
4824         { .val = LFS_NEWERXY_OPT,
4825                         .name = "neweram",      .has_arg = required_argument},
4826         { .val = LFS_NEWERXY_OPT,
4827                         .name = "newerac",      .has_arg = required_argument},
4828         { .val = LFS_NEWERXY_OPT,
4829                         .name = "newerab",      .has_arg = required_argument},
4830         { .val = LFS_NEWERXY_OPT,
4831                         .name = "newerma",      .has_arg = required_argument},
4832         { .val = LFS_NEWERXY_OPT,
4833                         .name = "newermm",      .has_arg = required_argument},
4834         { .val = LFS_NEWERXY_OPT,
4835                         .name = "newermc",      .has_arg = required_argument},
4836         { .val = LFS_NEWERXY_OPT,
4837                         .name = "newermb",      .has_arg = required_argument},
4838         { .val = LFS_NEWERXY_OPT,
4839                         .name = "newerca",      .has_arg = required_argument},
4840         { .val = LFS_NEWERXY_OPT,
4841                         .name = "newercm",      .has_arg = required_argument},
4842         { .val = LFS_NEWERXY_OPT,
4843                         .name = "newercc",      .has_arg = required_argument},
4844         { .val = LFS_NEWERXY_OPT,
4845                         .name = "newercb",      .has_arg = required_argument},
4846         { .val = LFS_NEWERXY_OPT,
4847                         .name = "newerba",      .has_arg = required_argument},
4848         { .val = LFS_NEWERXY_OPT,
4849                         .name = "newerbm",      .has_arg = required_argument},
4850         { .val = LFS_NEWERXY_OPT,
4851                         .name = "newerbc",      .has_arg = required_argument},
4852         { .val = LFS_NEWERXY_OPT,
4853                         .name = "newerbb",      .has_arg = required_argument},
4854         { .val = LFS_NEWERXY_OPT,
4855                         .name = "newerBa",      .has_arg = required_argument},
4856         { .val = LFS_NEWERXY_OPT,
4857                         .name = "newerBm",      .has_arg = required_argument},
4858         { .val = LFS_NEWERXY_OPT,
4859                         .name = "newerBc",      .has_arg = required_argument},
4860         { .val = LFS_NEWERXY_OPT,
4861                         .name = "newerBB",      .has_arg = required_argument},
4862         { .val = LFS_NEWERXY_OPT,
4863                         .name = "newerat",      .has_arg = required_argument},
4864         { .val = LFS_NEWERXY_OPT,
4865                         .name = "newermt",      .has_arg = required_argument},
4866         { .val = LFS_NEWERXY_OPT,
4867                         .name = "newerct",      .has_arg = required_argument},
4868         { .val = LFS_NEWERXY_OPT,
4869                         .name = "newerbt",      .has_arg = required_argument},
4870         { .val = LFS_NEWERXY_OPT,
4871                         .name = "newerBt",      .has_arg = required_argument},
4872         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument },
4873         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument },
4874         { .val = 'C',   .name = "ctime",        .has_arg = required_argument },
4875 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4876         { .val = 'D',   .name = "maxdepth",     .has_arg = required_argument },
4877         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument },
4878         { .val = 'E',   .name = "component-end",
4879                                                 .has_arg = required_argument },
4880 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
4881         { .val = LFS_LAYOUT_FOREIGN_OPT,
4882                         .name = "foreign",      .has_arg = optional_argument},
4883         { .val = 'g',   .name = "gid",          .has_arg = required_argument },
4884         { .val = 'G',   .name = "group",        .has_arg = required_argument },
4885         { .val = 'h',   .name = "help",         .has_arg = no_argument },
4886         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
4887         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument },
4888         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
4889 /* getstripe { .val = 'I', .name = "comp-id",   .has_arg = required_argument }*/
4890         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
4891         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
4892         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
4893         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
4894         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument },
4895         { .val = 'M',   .name = "mtime",        .has_arg = required_argument },
4896         { .val = 'n',   .name = "name",         .has_arg = required_argument },
4897         { .val = 'N',   .name = "mirror-count", .has_arg = required_argument },
4898 /* find { .val = 'o'    .name = "or", .has_arg = no_argument }, like find(1) */
4899         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
4900         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
4901         /* no short option for pool yet, can be 'p' after 2.18 */
4902         { .val = LFS_POOL_OPT,
4903                         .name = "pool",         .has_arg = required_argument },
4904         { .val = '0',   .name = "print0",       .has_arg = no_argument },
4905         { .val = 'P',   .name = "print",        .has_arg = no_argument },
4906         { .val = LFS_PROJID_OPT,
4907                         .name = "projid",       .has_arg = required_argument },
4908 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
4909 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4910 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
4911         { .val = 's',   .name = "size",         .has_arg = required_argument },
4912         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
4913         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
4914         { .val = 't',   .name = "type",         .has_arg = required_argument },
4915         { .val = LFS_FIND_PERM,
4916                         .name = "perm",         .has_arg = required_argument },
4917         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
4918         { .val = 'u',   .name = "uid",          .has_arg = required_argument },
4919         { .val = 'U',   .name = "user",         .has_arg = required_argument },
4920         { .val = 'z',   .name = "extension-size",
4921                                                 .has_arg = required_argument },
4922         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument },
4923 /* getstripe { .val = 'v', .name = "verbose",   .has_arg = no_argument }, */
4924 /* getstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
4925         { .name = NULL } };
4926         int optidx = 0;
4927         int pathstart = -1;
4928         int pathend = -1;
4929         int pathbad = -1;
4930         int neg_opt = 0;
4931         time_t *xtime;
4932         int *xsign;
4933         int isoption;
4934         char *endptr;
4935
4936         time(&t);
4937
4938         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4939         while ((c = getopt_long_only(argc, argv,
4940                 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4941                 long_opts, &optidx)) >= 0) {
4942                 xtime = NULL;
4943                 xsign = NULL;
4944                 if (neg_opt)
4945                         --neg_opt;
4946                 /* '!' is part of option */
4947                 /*
4948                  * when getopt_long_only() finds a string which is not
4949                  * an option nor a known option argument it returns 1
4950                  * in that case if we already have found pathstart and pathend
4951                  * (i.e. we have the list of pathnames),
4952                  * the only supported value is "!"
4953                  */
4954                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4955                 if (!isoption && pathend != -1) {
4956                         fprintf(stderr,
4957                                 "err: %s: filename|dirname must either precede options or follow options\n",
4958                                 argv[0]);
4959                         ret = CMD_HELP;
4960                         goto err;
4961                 }
4962                 if (!isoption && pathstart == -1)
4963                         pathstart = optind - 1;
4964                 if (isoption && pathstart != -1 && pathend == -1)
4965                         pathend = optind - 2;
4966                 switch (c) {
4967                 case 0:
4968                         /* Long options. */
4969                         break;
4970                 case 1:
4971                         /*
4972                          * unknown; opt is "!" or path component,
4973                          * checking done above.
4974                          */
4975                         if (strcmp(optarg, "!") == 0)
4976                                 neg_opt = 2;
4977                         break;
4978                 case 'A':
4979                         xtime = &param.fp_atime;
4980                         xsign = &param.fp_asign;
4981                         param.fp_exclude_atime = !!neg_opt;
4982                         /* no break, this falls through to 'B' for btime */
4983                 case 'B':
4984                         if (c == 'B') {
4985                                 xtime = &param.fp_btime;
4986                                 xsign = &param.fp_bsign;
4987                                 param.fp_exclude_btime = !!neg_opt;
4988                         }
4989                         /* no break, this falls through to 'C' for ctime */
4990                 case 'C':
4991                         if (c == 'C') {
4992                                 xtime = &param.fp_ctime;
4993                                 xsign = &param.fp_csign;
4994                                 param.fp_exclude_ctime = !!neg_opt;
4995                         }
4996                         /* no break, this falls through to 'M' for mtime */
4997                 case 'M':
4998                         if (c == 'M') {
4999                                 xtime = &param.fp_mtime;
5000                                 xsign = &param.fp_msign;
5001                                 param.fp_exclude_mtime = !!neg_opt;
5002                         }
5003                         rc = set_time(&param, &t, xtime, optarg);
5004                         if (rc == LONG_MAX) {
5005                                 ret = -1;
5006                                 goto err;
5007                         }
5008                         if (rc)
5009                                 *xsign = rc;
5010                         break;
5011                 case 'b':
5012                         if (optarg[0] == '+') {
5013                                 param.fp_blocks_sign = -1;
5014                                 optarg++;
5015                         } else if (optarg[0] == '-') {
5016                                 param.fp_blocks_sign =  1;
5017                                 optarg++;
5018                         }
5019
5020                         param.fp_blocks_units = 1024;
5021                         ret = llapi_parse_size(optarg, &param.fp_blocks,
5022                                                &param.fp_blocks_units, 0);
5023                         if (ret) {
5024                                 fprintf(stderr, "error: bad blocks '%s'\n",
5025                                         optarg);
5026                                 goto err;
5027                         }
5028                         param.fp_check_blocks = 1;
5029                         param.fp_exclude_blocks = !!neg_opt;
5030                         break;
5031                 case LFS_COMP_COUNT_OPT:
5032                         if (optarg[0] == '+') {
5033                                 param.fp_comp_count_sign = -1;
5034                                 optarg++;
5035                         } else if (optarg[0] == '-') {
5036                                 param.fp_comp_count_sign =  1;
5037                                 optarg++;
5038                         }
5039
5040                         errno = 0;
5041                         param.fp_comp_count = strtoul(optarg, &endptr, 0);
5042                         if (errno != 0 || *endptr != '\0' ||
5043                             param.fp_comp_count > UINT32_MAX) {
5044                                 fprintf(stderr,
5045                                         "error: bad component count '%s'\n",
5046                                         optarg);
5047                                 goto err;
5048                         }
5049                         param.fp_check_comp_count = 1;
5050                         param.fp_exclude_comp_count = !!neg_opt;
5051                         break;
5052                 case LFS_COMP_FLAGS_OPT:
5053                         rc = comp_str2flags(optarg, &param.fp_comp_flags,
5054                                             &param.fp_comp_neg_flags);
5055                         if (rc) {
5056                                 fprintf(stderr,
5057                                         "error: bad component flags '%s'\n",
5058                                         optarg);
5059                                 goto err;
5060                         }
5061                         param.fp_check_comp_flags = 1;
5062                         if (neg_opt) {
5063                                 __u32 flags = param.fp_comp_neg_flags;
5064
5065                                 param.fp_comp_neg_flags = param.fp_comp_flags;
5066                                 param.fp_comp_flags = flags;
5067                         }
5068                         break;
5069                 case LFS_COMP_START_OPT:
5070                         if (optarg[0] == '+') {
5071                                 param.fp_comp_start_sign = -1;
5072                                 optarg++;
5073                         } else if (optarg[0] == '-') {
5074                                 param.fp_comp_start_sign =  1;
5075                                 optarg++;
5076                         }
5077
5078                         rc = llapi_parse_size(optarg, &param.fp_comp_start,
5079                                               &param.fp_comp_start_units, 0);
5080                         if (rc) {
5081                                 fprintf(stderr,
5082                                         "error: bad component start '%s'\n",
5083                                         optarg);
5084                                 goto err;
5085                         }
5086                         param.fp_check_comp_start = 1;
5087                         param.fp_exclude_comp_start = !!neg_opt;
5088                         break;
5089                 case LFS_MIRROR_STATE_OPT:
5090                         rc = mirror_str2state(optarg, &param.fp_mirror_state,
5091                                               &param.fp_mirror_neg_state);
5092                         if (rc) {
5093                                 fprintf(stderr,
5094                                         "error: bad mirrored file state '%s'\n",
5095                                         optarg);
5096                                 goto err;
5097                         }
5098                         param.fp_check_mirror_state = 1;
5099                         if (neg_opt) {
5100                                 __u16 state = param.fp_mirror_neg_state;
5101
5102                                 param.fp_mirror_neg_state =
5103                                         param.fp_mirror_state;
5104                                 param.fp_mirror_state = state;
5105                         }
5106                         break;
5107                 case 'c':
5108                         if (optarg[0] == '+') {
5109                                 param.fp_stripe_count_sign = -1;
5110                                 optarg++;
5111                         } else if (optarg[0] == '-') {
5112                                 param.fp_stripe_count_sign =  1;
5113                                 optarg++;
5114                         }
5115
5116                         errno = 0;
5117                         param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5118                         if (errno != 0 || *endptr != '\0' ||
5119                             param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5120                                 fprintf(stderr,
5121                                         "error: bad stripe_count '%s'\n",
5122                                         optarg);
5123                                 ret = -1;
5124                                 goto err;
5125                         }
5126                         param.fp_check_stripe_count = 1;
5127                         param.fp_exclude_stripe_count = !!neg_opt;
5128                         break;
5129                 case 'D':
5130                         errno = 0;
5131                         param.fp_max_depth = strtol(optarg, 0, 0);
5132                         if (errno != 0 || param.fp_max_depth < 0) {
5133                                 fprintf(stderr,
5134                                         "error: bad maxdepth '%s'\n",
5135                                         optarg);
5136                                 ret = -1;
5137                                 goto err;
5138                         }
5139                         break;
5140                 case 'E':
5141                         if (optarg[0] == '+') {
5142                                 param.fp_comp_end_sign = -1;
5143                                 optarg++;
5144                         } else if (optarg[0] == '-') {
5145                                 param.fp_comp_end_sign =  1;
5146                                 optarg++;
5147                         }
5148
5149                         if (arg_is_eof(optarg)) {
5150                                 param.fp_comp_end = LUSTRE_EOF;
5151                                 param.fp_comp_end_units = 1;
5152                                 rc = 0;
5153                         } else {
5154                                 rc = llapi_parse_size(optarg,
5155                                                 &param.fp_comp_end,
5156                                                 &param.fp_comp_end_units, 0);
5157                                 /* assume units of KB if too small */
5158                                 if (param.fp_comp_end < 4096)
5159                                         param.fp_comp_end *= 1024;
5160                         }
5161                         if (rc) {
5162                                 fprintf(stderr,
5163                                         "error: bad component end '%s'\n",
5164                                         optarg);
5165                                 goto err;
5166                         }
5167                         param.fp_check_comp_end = 1;
5168                         param.fp_exclude_comp_end = !!neg_opt;
5169                         break;
5170                 case LFS_LAYOUT_FOREIGN_OPT: {
5171                         /* all types by default */
5172                         uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5173
5174                         if (optarg) {
5175                                 /* check pure numeric */
5176                                 type = strtoul(optarg, &endptr, 0);
5177                                 if (*endptr) {
5178                                         /* check name */
5179                                         type = check_foreign_type_name(optarg);
5180                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5181                                                 fprintf(stderr,
5182                                                         "%s %s: unknown foreign type '%s'\n",
5183                                                         progname, argv[0],
5184                                                         optarg);
5185                                                 return CMD_HELP;
5186                                         }
5187                                 } else if (type >= UINT32_MAX) {
5188                                         fprintf(stderr,
5189                                                 "%s %s: invalid foreign type '%s'\n",
5190                                                 progname, argv[0], optarg);
5191                                         return CMD_HELP;
5192                                 }
5193                         }
5194                         param.fp_foreign_type = type;
5195                         param.fp_check_foreign = 1;
5196                         param.fp_exclude_foreign = !!neg_opt;
5197                         break;
5198                 }
5199                 case LFS_NEWERXY_OPT: {
5200                         char x = 'm';
5201                         char y = 'm';
5202                         int xidx;
5203                         int negidx;
5204                         time_t *newery;
5205                         time_t ref = time(NULL);
5206
5207                         /* no need to check bad options, they won't get here */
5208                         if (strlen(long_opts[optidx].name) == 7) {
5209                                 x = long_opts[optidx].name[5];
5210                                 y = long_opts[optidx].name[6];
5211                         }
5212
5213                         if (y == 't') {
5214                                 static const char *const fmts[] = {
5215                                         "%Y-%m-%d %H:%M:%S",
5216                                         "%Y-%m-%d %H:%M",
5217                                         "%Y-%m-%d",
5218                                         "%H:%M:%S", /* sometime today */
5219                                         "%H:%M",
5220                                         "@%s",
5221                                         "%s",
5222                                         NULL };
5223                                 struct tm tm;
5224                                 bool found = false;
5225                                 int i;
5226
5227                                 for (i = 0; fmts[i] != NULL; i++) {
5228                                         char *ptr;
5229
5230                                         /* Init for times relative to today */
5231                                         if (strncmp(fmts[i], "%H", 2) == 0)
5232                                                 localtime_r(&ref, &tm);
5233                                         else
5234                                                 memset(&tm, 0, sizeof(tm));
5235                                         ptr = strptime(optarg, fmts[i], &tm);
5236                                         /* Skip spaces */
5237                                         while (ptr && isspace(*ptr))
5238                                                 ptr++;
5239                                         if (ptr == optarg + strlen(optarg)) {
5240                                                 found = true;
5241                                                 break;
5242                                         }
5243                                 }
5244
5245                                 if (!found) {
5246                                         fprintf(stderr,
5247                                                 "%s: invalid time '%s'\n",
5248                                                 progname, optarg);
5249                                         fprintf(stderr,
5250                                                 "supported formats are:\n  ");
5251                                         for (i = 0; fmts[i] != NULL; i++)
5252                                                 fprintf(stderr, "'%s', ",
5253                                                         fmts[i]);
5254                                         fprintf(stderr, "\n");
5255                                         ret = -EINVAL;
5256                                         goto err;
5257                                 }
5258
5259                                 ref = mktime(&tm);
5260                         } else if (y == 'b' || y == 'B') {
5261                                 lstatx_t stx;
5262
5263                                 rc = llapi_get_lum_file(optarg, NULL, &stx,
5264                                                         NULL, 0);
5265                                 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5266                                         if (!(stx.stx_mask & STATX_BTIME))
5267                                                 ret = -EOPNOTSUPP;
5268                                         else
5269                                                 ret = -errno;
5270                                         fprintf(stderr,
5271                                                 "%s: get btime failed '%s': %s\n",
5272                                                 progname, optarg,
5273                                                 strerror(-ret));
5274                                         goto err;
5275                                 }
5276
5277                                 ref = stx.stx_btime.tv_sec;
5278                         } else {
5279                                 struct stat statbuf;
5280
5281                                 if (stat(optarg, &statbuf) < 0) {
5282                                         fprintf(stderr,
5283                                                 "%s: cannot stat file '%s': %s\n",
5284                                                 progname, optarg,
5285                                                 strerror(errno));
5286                                         ret = -errno;
5287                                         goto err;
5288                                 }
5289
5290                                 switch (y) {
5291                                 case 'a':
5292                                         ref = statbuf.st_atime;
5293                                         break;
5294                                 case 'm':
5295                                         ref = statbuf.st_mtime;
5296                                         break;
5297                                 case 'c':
5298                                         ref = statbuf.st_ctime;
5299                                         break;
5300                                 default:
5301                                         fprintf(stderr,
5302                                                 "%s: invalid Y argument: '%c'\n",
5303                                                 progname, x);
5304                                         ret = -EINVAL;
5305                                         goto err;
5306                                 }
5307                         }
5308
5309                         switch (x) {
5310                         case 'a':
5311                                 xidx = NEWERXY_ATIME;
5312                                 break;
5313                         case 'm':
5314                                 xidx = NEWERXY_MTIME;
5315                                 break;
5316                         case 'c':
5317                                 xidx = NEWERXY_CTIME;
5318                                 break;
5319                         case 'b':
5320                         case 'B':
5321                                 xidx = NEWERXY_BTIME;
5322                                 break;
5323                         default:
5324                                 fprintf(stderr,
5325                                         "%s: invalid X argument: '%c'\n",
5326                                         progname, x);
5327                                 ret = -EINVAL;
5328                                 goto err;
5329                         }
5330
5331                         negidx = !!neg_opt;
5332                         newery = &param.fp_newery[xidx][negidx];
5333
5334                         if (*newery == 0) {
5335                                 *newery = ref;
5336                         } else {
5337                                 if (negidx)
5338                                         *newery = *newery > ref ? ref : *newery;
5339                                 else
5340                                         *newery = *newery > ref ? *newery : ref;
5341                         }
5342                         param.fp_newerxy = 1;
5343                         break;
5344                 }
5345                 case 'g':
5346                 case 'G':
5347                         rc = name2gid(&param.fp_gid, optarg);
5348                         if (rc) {
5349                                 if (str2quotaid(&param.fp_gid, optarg)) {
5350                                         fprintf(stderr,
5351                                                 "Group/GID: %s cannot be found.\n",
5352                                                 optarg);
5353                                         ret = -1;
5354                                         goto err;
5355                                 }
5356                         }
5357                         param.fp_exclude_gid = !!neg_opt;
5358                         param.fp_check_gid = 1;
5359                         break;
5360                 case 'H':
5361                         rc = mdthash_input(optarg, &param.fp_hash_inflags,
5362                                            &param.fp_hash_exflags,
5363                                            &param.fp_hash_type);
5364                         if (rc) {
5365                                 ret = -1;
5366                                 goto err;
5367                         }
5368                         if (param.fp_hash_inflags || param.fp_hash_exflags)
5369                                 param.fp_check_hash_flag = 1;
5370                         param.fp_exclude_hash_type = !!neg_opt;
5371                         break;
5372                 case 'l':
5373                         param.fp_lazy = 1;
5374                         break;
5375                 case 'L':
5376                         ret = name2layout(&param.fp_layout, optarg);
5377                         if (ret)
5378                                 goto err;
5379                         param.fp_exclude_layout = !!neg_opt;
5380                         param.fp_check_layout = 1;
5381                         break;
5382                 case 'u':
5383                 case 'U':
5384                         rc = name2uid(&param.fp_uid, optarg);
5385                         if (rc) {
5386                                 if (str2quotaid(&param.fp_uid, optarg)) {
5387                                         fprintf(stderr,
5388                                                 "User/UID: %s cannot be found.\n",
5389                                                 optarg);
5390                                         ret = -1;
5391                                         goto err;
5392                                 }
5393                         }
5394                         param.fp_exclude_uid = !!neg_opt;
5395                         param.fp_check_uid = 1;
5396                         break;
5397                 case 'n':
5398                         param.fp_pattern = (char *)optarg;
5399                         param.fp_exclude_pattern = !!neg_opt;
5400                         break;
5401                 case 'N':
5402                         if (optarg[0] == '+') {
5403                                 param.fp_mirror_count_sign = -1;
5404                                 optarg++;
5405                         } else if (optarg[0] == '-') {
5406                                 param.fp_mirror_count_sign =  1;
5407                                 optarg++;
5408                         }
5409
5410                         errno = 0;
5411                         param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5412                         if (errno != 0 || *endptr != '\0' ||
5413                             param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5414                                 fprintf(stderr,
5415                                         "error: bad mirror count '%s'\n",
5416                                         optarg);
5417                                 goto err;
5418                         }
5419                         param.fp_check_mirror_count = 1;
5420                         param.fp_exclude_mirror_count = !!neg_opt;
5421                         break;
5422                 case 'm':
5423                 case 'i':
5424                 case 'O': {
5425                         char *buf, *token, *next, *p;
5426                         int len = 1;
5427                         void *tmp;
5428
5429                         buf = strdup(optarg);
5430                         if (!buf) {
5431                                 ret = -ENOMEM;
5432                                 goto err;
5433                         }
5434
5435                         param.fp_exclude_obd = !!neg_opt;
5436
5437                         token = buf;
5438                         while (token && *token) {
5439                                 token = strchr(token, ',');
5440                                 if (token) {
5441                                         len++;
5442                                         token++;
5443                                 }
5444                         }
5445                         if (c == 'm') {
5446                                 param.fp_exclude_mdt = !!neg_opt;
5447                                 param.fp_num_alloc_mdts += len;
5448                                 tmp = realloc(param.fp_mdt_uuid,
5449                                               param.fp_num_alloc_mdts *
5450                                               sizeof(*param.fp_mdt_uuid));
5451                                 if (!tmp) {
5452                                         ret = -ENOMEM;
5453                                         goto err_free;
5454                                 }
5455
5456                                 param.fp_mdt_uuid = tmp;
5457                         } else {
5458                                 param.fp_exclude_obd = !!neg_opt;
5459                                 param.fp_num_alloc_obds += len;
5460                                 tmp = realloc(param.fp_obd_uuid,
5461                                               param.fp_num_alloc_obds *
5462                                               sizeof(*param.fp_obd_uuid));
5463                                 if (!tmp) {
5464                                         ret = -ENOMEM;
5465                                         goto err_free;
5466                                 }
5467
5468                                 param.fp_obd_uuid = tmp;
5469                         }
5470                         for (token = buf; token && *token; token = next) {
5471                                 struct obd_uuid *puuid;
5472
5473                                 if (c == 'm') {
5474                                         puuid =
5475                                         &param.fp_mdt_uuid[param.fp_num_mdts++];
5476                                 } else {
5477                                         puuid =
5478                                         &param.fp_obd_uuid[param.fp_num_obds++];
5479                                 }
5480                                 p = strchr(token, ',');
5481                                 next = 0;
5482                                 if (p) {
5483                                         *p = 0;
5484                                         next = p+1;
5485                                 }
5486
5487                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5488                                         ret = -E2BIG;
5489                                         goto err_free;
5490                                 }
5491
5492                                 strncpy(puuid->uuid, token,
5493                                         sizeof(puuid->uuid));
5494                         }
5495 err_free:
5496                         if (buf)
5497                                 free(buf);
5498                         break;
5499                 }
5500 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5501                 case 'p':
5502 #endif
5503                 case LFS_POOL_OPT:
5504                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
5505                                 fprintf(stderr,
5506                                         "Pool name %s is too long (max %d)\n",
5507                                         optarg, LOV_MAXPOOLNAME);
5508                                 ret = -1;
5509                                 goto err;
5510                         }
5511                         /*
5512                          * We do check for empty pool because empty pool
5513                          * is used to find V1 LOV attributes
5514                          */
5515                         strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5516                         param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5517                         param.fp_exclude_pool = !!neg_opt;
5518                         param.fp_check_pool = 1;
5519                         break;
5520 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5521                 case 'p': /* want this for --pool, to match getstripe/find */
5522                         fprintf(stderr,
5523                                 "warning: -p deprecated, use --print0 or -0\n");
5524 #endif
5525                 case '0':
5526                         param.fp_zero_end = 1;
5527                         break;
5528                 case 'P': /* we always print, this option is a no-op */
5529                         break;
5530                 case LFS_PROJID_OPT:
5531                         rc = name2projid(&param.fp_projid, optarg);
5532                         if (rc) {
5533                                 if (str2quotaid(&param.fp_projid, optarg)) {
5534                                         fprintf(stderr,
5535                                                 "Invalid project ID: %s\n",
5536                                                 optarg);
5537                                         ret = -1;
5538                                         goto err;
5539                                 }
5540                         }
5541                         param.fp_exclude_projid = !!neg_opt;
5542                         param.fp_check_projid = 1;
5543                         break;
5544                 case 's':
5545                         if (optarg[0] == '+') {
5546                                 param.fp_size_sign = -1;
5547                                 optarg++;
5548                         } else if (optarg[0] == '-') {
5549                                 param.fp_size_sign =  1;
5550                                 optarg++;
5551                         }
5552
5553                         ret = llapi_parse_size(optarg, &param.fp_size,
5554                                                &param.fp_size_units, 0);
5555                         if (ret) {
5556                                 fprintf(stderr, "error: bad file size '%s'\n",
5557                                         optarg);
5558                                 goto err;
5559                         }
5560                         param.fp_check_size = 1;
5561                         param.fp_exclude_size = !!neg_opt;
5562                         break;
5563                 case 'S':
5564                         if (optarg[0] == '+') {
5565                                 param.fp_stripe_size_sign = -1;
5566                                 optarg++;
5567                         } else if (optarg[0] == '-') {
5568                                 param.fp_stripe_size_sign =  1;
5569                                 optarg++;
5570                         }
5571
5572                         ret = llapi_parse_size(optarg, &param.fp_stripe_size,
5573                                                &param.fp_stripe_size_units, 0);
5574                         /* assume units of KB if too small to be valid */
5575                         if (param.fp_stripe_size < 4096)
5576                                 param.fp_stripe_size *= 1024;
5577                         if (ret) {
5578                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
5579                                         optarg);
5580                                 goto err;
5581                         }
5582                         param.fp_check_stripe_size = 1;
5583                         param.fp_exclude_stripe_size = !!neg_opt;
5584                         break;
5585                 case 't':
5586                         param.fp_exclude_type = !!neg_opt;
5587                         switch (optarg[0]) {
5588                         case 'b':
5589                                 param.fp_type = S_IFBLK;
5590                                 break;
5591                         case 'c':
5592                                 param.fp_type = S_IFCHR;
5593                                 break;
5594                         case 'd':
5595                                 param.fp_type = S_IFDIR;
5596                                 break;
5597                         case 'f':
5598                                 param.fp_type = S_IFREG;
5599                                 break;
5600                         case 'l':
5601                                 param.fp_type = S_IFLNK;
5602                                 break;
5603                         case 'p':
5604                                 param.fp_type = S_IFIFO;
5605                                 break;
5606                         case 's':
5607                                 param.fp_type = S_IFSOCK;
5608                                 break;
5609                         default:
5610                                 fprintf(stderr, "%s: bad type '%s'\n",
5611                                         progname, optarg);
5612                                 ret = CMD_HELP;
5613                                 goto err;
5614                         }
5615                         break;
5616                 case LFS_FIND_PERM:
5617                         param.fp_exclude_perm = !!neg_opt;
5618                         param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5619                         if (*optarg == '/') {
5620                                 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5621                                 optarg++;
5622                         } else if (*optarg == '-') {
5623                                 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5624                                 optarg++;
5625                         }
5626
5627                         if (str2mode_t(optarg, &param.fp_perm)) {
5628                                 fprintf(stderr, "error: invalid mode '%s'\n",
5629                                         optarg);
5630                                 ret = -1;
5631                                 goto err;
5632                         }
5633                         break;
5634                 case 'T':
5635                         if (optarg[0] == '+') {
5636                                 param.fp_mdt_count_sign = -1;
5637                                 optarg++;
5638                         } else if (optarg[0] == '-') {
5639                                 param.fp_mdt_count_sign =  1;
5640                                 optarg++;
5641                         }
5642
5643                         errno = 0;
5644                         param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5645                         if (errno != 0 || *endptr != '\0' ||
5646                             param.fp_mdt_count >= UINT32_MAX) {
5647                                 fprintf(stderr, "error: bad mdt_count '%s'\n",
5648                                         optarg);
5649                                 ret = -1;
5650                                 goto err;
5651                         }
5652                         param.fp_check_mdt_count = 1;
5653                         param.fp_exclude_mdt_count = !!neg_opt;
5654                         break;
5655                 case 'z':
5656                         if (optarg[0] == '+') {
5657                                 param.fp_ext_size_sign = -1;
5658                                 optarg++;
5659                         } else if (optarg[0] == '-') {
5660                                 param.fp_ext_size_sign =  1;
5661                                 optarg++;
5662                         }
5663
5664                         ret = llapi_parse_size(optarg, &param.fp_ext_size,
5665                                                &param.fp_ext_size_units, 0);
5666                         if (ret) {
5667                                 fprintf(stderr, "error: bad ext-size '%s'\n",
5668                                         optarg);
5669                                 goto err;
5670                         }
5671                         param.fp_ext_size /= SEL_UNIT_SIZE;
5672                         param.fp_ext_size_units /= SEL_UNIT_SIZE;
5673                         param.fp_check_ext_size = 1;
5674                         param.fp_exclude_ext_size = !!neg_opt;
5675                         break;
5676                 default:
5677                         fprintf(stderr, "%s: unrecognized option '%s'\n",
5678                                 progname, argv[optind - 1]);
5679                 case 'h':
5680                         ret = CMD_HELP;
5681                         goto err;
5682                 }
5683         }
5684
5685         if (pathstart == -1) {
5686                 fprintf(stderr, "error: %s: no filename|pathname\n",
5687                         argv[0]);
5688                 ret = CMD_HELP;
5689                 goto err;
5690         } else if (pathend == -1) {
5691                 /* no options */
5692                 pathend = argc;
5693         }
5694
5695         do {
5696                 rc = llapi_find(argv[pathstart], &param);
5697                 if (rc && !ret) {
5698                         ret = rc;
5699                         pathbad = pathstart;
5700                 }
5701         } while (++pathstart < pathend);
5702
5703         if (ret)
5704                 fprintf(stderr, "%s: failed for '%s': %s\n",
5705                         progname, argv[pathbad], strerror(-rc));
5706
5707 err:
5708         if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5709                 free(param.fp_obd_uuid);
5710
5711         if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5712                 free(param.fp_mdt_uuid);
5713
5714         return ret;
5715 }
5716
5717 static int lfs_getstripe_internal(int argc, char **argv,
5718                                   struct find_param *param)
5719 {
5720         struct option long_opts[] = {
5721 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
5722 /* find { .val = 'b',   .name = "blocks",       .has_arg = required_argument }*/
5723 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
5724 /* find { .val = 'B',   .name = "Btime",        .has_arg = required_argument }*/
5725         { .val = LFS_COMP_COUNT_OPT,
5726                         .name = "comp-count",   .has_arg = no_argument },
5727         { .val = LFS_COMP_COUNT_OPT,
5728                 .name = "component-count",      .has_arg = no_argument },
5729         { .val = LFS_COMP_FLAGS_OPT,
5730                         .name = "comp-flags",   .has_arg = optional_argument },
5731         { .val = LFS_COMP_FLAGS_OPT,
5732                 .name = "component-flags",      .has_arg = optional_argument },
5733         { .val = LFS_COMP_START_OPT,
5734                         .name = "comp-start",   .has_arg = optional_argument },
5735         { .val = LFS_COMP_START_OPT,
5736                 .name = "component-start",      .has_arg = optional_argument },
5737         { .val = LFS_MIRROR_INDEX_OPT,
5738                 .name = "mirror-index",         .has_arg = required_argument },
5739         { .val = LFS_MIRROR_ID_OPT,
5740                 .name = "mirror-id",            .has_arg = required_argument },
5741         { .val = 'c',   .name = "stripe-count", .has_arg = no_argument },
5742         { .val = 'c',   .name = "stripe_count", .has_arg = no_argument },
5743 /* find { .val = 'C',   .name = "ctime",        .has_arg = required_argument }*/
5744         { .val = 'd',   .name = "directory",    .has_arg = no_argument },
5745         { .val = 'D',   .name = "default",      .has_arg = no_argument },
5746         { .val = 'E',   .name = "comp-end",     .has_arg = optional_argument },
5747         { .val = 'E',   .name = "component-end", .has_arg = optional_argument },
5748         { .val = 'F',   .name = "fid",          .has_arg = no_argument },
5749         { .val = 'g',   .name = "generation",   .has_arg = no_argument },
5750 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
5751         { .val = 'h',   .name = "help",         .has_arg = no_argument },
5752 /* dirstripe { .val = 'H', .name = "mdt-hash",  .has_arg = required_argument }*/
5753         { .val = 'i',   .name = "stripe-index", .has_arg = no_argument },
5754         { .val = 'i',   .name = "stripe_index", .has_arg = no_argument },
5755         { .val = 'I',   .name = "comp-id",      .has_arg = optional_argument },
5756         { .val = 'I',   .name = "component-id", .has_arg = optional_argument },
5757 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
5758         { .val = 'L',   .name = "layout",       .has_arg = no_argument },
5759         { .val = 'm',   .name = "mdt",          .has_arg = no_argument },
5760         { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
5761         { .val = 'm',   .name = "mdt_index",    .has_arg = no_argument },
5762 /* find { .val = 'M',   .name = "mtime",        .has_arg = required_argument }*/
5763 /* find { .val = 'n',   .name = "name",         .has_arg = required_argument }*/
5764         { .val = 'N',   .name = "mirror-count", .has_arg = no_argument },
5765         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
5766         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
5767         { .val = 'p',   .name = "pool",         .has_arg = no_argument },
5768 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
5769         { .val = 'q',   .name = "quiet",        .has_arg = no_argument },
5770         { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
5771         { .val = 'R',   .name = "raw",          .has_arg = no_argument },
5772         { .val = 'S',   .name = "stripe-size",  .has_arg = no_argument },
5773         { .val = 'S',   .name = "stripe_size",  .has_arg = no_argument },
5774 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
5775 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5776 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
5777 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
5778         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
5779 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5780         { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
5781         { .val = 'z',   .name = "extension-size", .has_arg = no_argument },
5782         { .val = 'z',   .name = "ext-size",     .has_arg = no_argument },
5783         { .name = NULL } };
5784         int c, rc;
5785         int neg_opt = 0;
5786         int pathstart = -1, pathend = -1;
5787         int isoption;
5788         char *end, *tmp;
5789
5790         while ((c = getopt_long(argc, argv,
5791                         "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5792                         long_opts, NULL)) != -1) {
5793                 if (neg_opt)
5794                         --neg_opt;
5795
5796                 /* '!' is part of option */
5797                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5798                 if (!isoption && pathend != -1) {
5799                         fprintf(stderr,
5800                                 "error: %s: filename|dirname must either precede options or follow options\n",
5801                                 argv[0]);
5802                         return CMD_HELP;
5803                 }
5804                 if (!isoption && pathstart == -1)
5805                         pathstart = optind - 1;
5806                 if (isoption && pathstart != -1 && pathend == -1)
5807                         pathend = optind - 2;
5808
5809                 switch (c) {
5810                 case 1:
5811                         /* unknown: opt is "!" */
5812                         if (strcmp(optarg, "!") == 0)
5813                                 neg_opt = 2;
5814                         break;
5815                 case 'c':
5816                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5817                                 param->fp_verbose |= VERBOSE_COUNT;
5818                                 param->fp_max_depth = 0;
5819                         }
5820                         break;
5821                 case LFS_COMP_COUNT_OPT:
5822                         param->fp_verbose |= VERBOSE_COMP_COUNT;
5823                         param->fp_max_depth = 0;
5824                         break;
5825                 case LFS_COMP_FLAGS_OPT:
5826                         if (optarg) {
5827                                 rc = comp_str2flags(optarg,
5828                                                     &param->fp_comp_flags,
5829                                                     &param->fp_comp_neg_flags);
5830                                 if (rc != 0) {
5831                                         fprintf(stderr,
5832                                                 "error: %s bad component flags '%s'.\n",
5833                                                 argv[0], optarg);
5834                                         return CMD_HELP;
5835                                 }
5836                                 param->fp_check_comp_flags = 1;
5837                         } else {
5838                                 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5839                                 param->fp_max_depth = 0;
5840                         }
5841                         break;
5842                 case LFS_COMP_START_OPT:
5843                         if (optarg) {
5844                                 tmp = optarg;
5845                                 if (tmp[0] == '+') {
5846                                         param->fp_comp_start_sign = -1;
5847                                         tmp++;
5848                                 } else if (tmp[0] == '-') {
5849                                         param->fp_comp_start_sign = 1;
5850                                         tmp++;
5851                                 }
5852                                 rc = llapi_parse_size(tmp,
5853                                                 &param->fp_comp_start,
5854                                                 &param->fp_comp_start_units, 0);
5855                                 if (rc != 0) {
5856                                         fprintf(stderr,
5857                                                 "error: %s bad component start '%s'.\n",
5858                                                 argv[0], tmp);
5859                                         return CMD_HELP;
5860                                 }
5861                                 param->fp_check_comp_start = 1;
5862                         } else {
5863                                 param->fp_verbose |= VERBOSE_COMP_START;
5864                                 param->fp_max_depth = 0;
5865                         }
5866                         break;
5867                 case LFS_MIRROR_INDEX_OPT: {
5868                         unsigned long int mirror_index;
5869
5870                         if (optarg[0] == '+') {
5871                                 param->fp_mirror_index_sign = -1;
5872                                 optarg++;
5873                         } else if (optarg[0] == '-') {
5874                                 param->fp_mirror_index_sign = 1;
5875                                 optarg++;
5876                         }
5877
5878                         errno = 0;
5879                         mirror_index = strtoul(optarg, &end, 0);
5880                         if (errno != 0 || *end != '\0' ||
5881                             mirror_index > UINT16_MAX || (mirror_index == 0 &&
5882                             param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5883                                 fprintf(stderr,
5884                                         "%s %s: invalid mirror index '%s'\n",
5885                                         progname, argv[0], optarg);
5886                                 return CMD_HELP;
5887                         }
5888
5889                         param->fp_mirror_index = (__u16)mirror_index;
5890
5891                         if (param->fp_mirror_id != 0) {
5892                                 fprintf(stderr,
5893                                         "%s %s: can't specify both mirror index and mirror ID\n",
5894                                         progname, argv[0]);
5895                                 return CMD_HELP;
5896                         }
5897                         param->fp_check_mirror_index = 1;
5898                         param->fp_exclude_mirror_index = !!neg_opt;
5899                         break;
5900                 }
5901                 case LFS_MIRROR_ID_OPT: {
5902                         unsigned long int mirror_id;
5903
5904                         if (optarg[0] == '+') {
5905                                 param->fp_mirror_id_sign = -1;
5906                                 optarg++;
5907                         } else if (optarg[0] == '-') {
5908                                 param->fp_mirror_id_sign = 1;
5909                                 optarg++;
5910                         }
5911
5912                         errno = 0;
5913                         mirror_id = strtoul(optarg, &end, 0);
5914                         if (errno != 0 || *end != '\0' ||
5915                             mirror_id > UINT16_MAX || (mirror_id == 0 &&
5916                             param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5917                                 fprintf(stderr,
5918                                         "%s %s: invalid mirror ID '%s'\n",
5919                                         progname, argv[0], optarg);
5920                                 return CMD_HELP;
5921                         }
5922
5923                         param->fp_mirror_id = (__u16)mirror_id;
5924
5925                         if (param->fp_mirror_index != 0) {
5926                                 fprintf(stderr,
5927                                         "%s %s: can't specify both mirror index and mirror ID\n",
5928                                         progname, argv[0]);
5929                                 return CMD_HELP;
5930                         }
5931                         param->fp_check_mirror_id = 1;
5932                         param->fp_exclude_mirror_id = !!neg_opt;
5933                         break;
5934                 }
5935                 case 'd':
5936                         param->fp_max_depth = 0;
5937                         break;
5938                 case 'D':
5939                         param->fp_get_default_lmv = 1;
5940                         break;
5941                 case 'E':
5942                         if (optarg) {
5943                                 tmp = optarg;
5944                                 if (tmp[0] == '+') {
5945                                         param->fp_comp_end_sign = -1;
5946                                         tmp++;
5947                                 } else if (tmp[0] == '-') {
5948                                         param->fp_comp_end_sign = 1;
5949                                         tmp++;
5950                                 }
5951
5952                                 if (arg_is_eof(tmp)) {
5953                                         param->fp_comp_end = LUSTRE_EOF;
5954                                         param->fp_comp_end_units = 1;
5955                                         rc = 0;
5956                                 } else {
5957                                         rc = llapi_parse_size(tmp,
5958                                                 &param->fp_comp_end,
5959                                                 &param->fp_comp_end_units, 0);
5960                                         /* assume units of KB if too small */
5961                                         if (param->fp_comp_end < 4096)
5962                                                 param->fp_comp_end *= 1024;
5963                                 }
5964                                 if (rc != 0) {
5965                                         fprintf(stderr,
5966                                                 "error: %s bad component end '%s'.\n",
5967                                                 argv[0], tmp);
5968                                         return CMD_HELP;
5969                                 }
5970                                 param->fp_check_comp_end = 1;
5971                         } else {
5972                                 param->fp_verbose |= VERBOSE_COMP_END;
5973                                 param->fp_max_depth = 0;
5974                         }
5975                         break;
5976                 case 'F':
5977                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5978                                 param->fp_verbose |= VERBOSE_DFID;
5979                                 param->fp_max_depth = 0;
5980                         }
5981                         break;
5982                 case 'g':
5983                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5984                                 param->fp_verbose |= VERBOSE_GENERATION;
5985                                 param->fp_max_depth = 0;
5986                         }
5987                         break;
5988                 case 'i':
5989                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5990                                 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5991                                 param->fp_max_depth = 0;
5992                         }
5993                         break;
5994                 case 'I':
5995                         if (optarg) {
5996                                 param->fp_comp_id = strtoul(optarg, &end, 0);
5997                                 if (*end != '\0' || param->fp_comp_id == 0 ||
5998                                     param->fp_comp_id > LCME_ID_MAX) {
5999                                         fprintf(stderr,
6000                                                 "error: %s bad component id '%s'\n",
6001                                                 argv[0], optarg);
6002                                         return CMD_HELP;
6003                                 }
6004                                 param->fp_check_comp_id = 1;
6005                         } else {
6006                                 param->fp_max_depth = 0;
6007                                 param->fp_verbose |= VERBOSE_COMP_ID;
6008                         }
6009                         break;
6010                 case 'L':
6011                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6012                                 param->fp_verbose |= VERBOSE_PATTERN;
6013                                 param->fp_max_depth = 0;
6014                         }
6015                         break;
6016 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6017                 case 'M':
6018                         fprintf(stderr,
6019                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6020 #endif
6021                 case 'm':
6022                         if (!(param->fp_verbose & VERBOSE_DETAIL))
6023                                 param->fp_max_depth = 0;
6024                         param->fp_verbose |= VERBOSE_MDTINDEX;
6025                         break;
6026                 case 'N':
6027                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6028                                 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6029                                 param->fp_max_depth = 0;
6030                         }
6031                         break;
6032                 case 'O':
6033                         if (param->fp_obd_uuid) {
6034                                 fprintf(stderr,
6035                                         "error: %s: only one obduuid allowed",
6036                                         argv[0]);
6037                                 return CMD_HELP;
6038                         }
6039                         param->fp_obd_uuid = (struct obd_uuid *)optarg;
6040                         break;
6041                 case 'p':
6042                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6043                                 param->fp_verbose |= VERBOSE_POOL;
6044                                 param->fp_max_depth = 0;
6045                         }
6046                         break;
6047                 case 'q':
6048                         param->fp_quiet++;
6049                         break;
6050                 case 'r':
6051                         param->fp_recursive = 1;
6052                         break;
6053                 case 'R':
6054                         param->fp_raw = 1;
6055                         break;
6056                 case 'S':
6057                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6058                                 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6059                                 param->fp_max_depth = 0;
6060                         }
6061                         break;
6062                 case 'v':
6063                         param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6064                         break;
6065                 case 'y':
6066                         param->fp_yaml = 1;
6067                         break;
6068                 case 'z':
6069                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6070                                 param->fp_verbose |= VERBOSE_EXT_SIZE;
6071                                 param->fp_max_depth = 0;
6072                         }
6073                         break;
6074                 default:
6075                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6076                                 progname, argv[optind - 1]);
6077                 case 'h':
6078                         return CMD_HELP;
6079                 }
6080         }
6081
6082         if (pathstart == -1) {
6083                 fprintf(stderr, "error: %s: no filename|pathname\n",
6084                                 argv[0]);
6085                 return CMD_HELP;
6086         } else if (pathend == -1) {
6087                 /* no options */
6088                 pathend = argc;
6089         }
6090
6091         if (pathend > argc)
6092                 return CMD_HELP;
6093
6094         if (param->fp_recursive)
6095                 param->fp_max_depth = -1;
6096         else if (param->fp_verbose & VERBOSE_DETAIL)
6097                 param->fp_max_depth = 1;
6098
6099         if (!param->fp_verbose)
6100                 param->fp_verbose = VERBOSE_DEFAULT;
6101         if (param->fp_quiet)
6102                 param->fp_verbose = VERBOSE_OBJID;
6103
6104         do {
6105                 rc = llapi_getstripe(argv[pathstart], param);
6106         } while (++pathstart < pathend && !rc);
6107
6108         if (rc)
6109                 fprintf(stderr, "error: %s failed for %s.\n",
6110                         argv[0], argv[optind - 1]);
6111         return rc;
6112 }
6113
6114 static int lfs_tgts(int argc, char **argv)
6115 {
6116         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6117         struct find_param param;
6118         int index = 0, rc = 0;
6119
6120         if (argc > 2)
6121                 return CMD_HELP;
6122
6123         if (argc == 2 && !realpath(argv[1], path)) {
6124                 rc = -errno;
6125                 fprintf(stderr, "error: invalid path '%s': %s\n",
6126                         argv[1], strerror(-rc));
6127                 return rc;
6128         }
6129
6130         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6131                 /* Check if we have a mount point */
6132                 if (mntdir[0] == '\0')
6133                         continue;
6134
6135                 memset(&param, 0, sizeof(param));
6136                 if (!strcmp(argv[0], "mdts"))
6137                         param.fp_get_lmv = 1;
6138
6139                 rc = llapi_ostlist(mntdir, &param);
6140                 if (rc) {
6141                         fprintf(stderr, "error: %s: failed on %s\n",
6142                                 argv[0], mntdir);
6143                 }
6144                 if (path[0] != '\0')
6145                         break;
6146                 memset(mntdir, 0, PATH_MAX);
6147         }
6148
6149         return rc;
6150 }
6151
6152 static int lfs_getstripe(int argc, char **argv)
6153 {
6154         struct find_param param = { 0 };
6155
6156         param.fp_max_depth = 1;
6157         return lfs_getstripe_internal(argc, argv, &param);
6158 }
6159
6160 /* functions */
6161 static int lfs_getdirstripe(int argc, char **argv)
6162 {
6163         struct find_param param = { 0 };
6164         struct option long_opts[] = {
6165         { .val = 'c',   .name = "mdt-count",     .has_arg = no_argument },
6166         { .val = 'D',   .name = "default",       .has_arg = no_argument },
6167         { .val = 'h',   .name = "help",         .has_arg = no_argument },
6168         { .val = 'H',   .name = "mdt-hash",      .has_arg = no_argument },
6169         { .val = 'i',   .name = "mdt-index",     .has_arg = no_argument },
6170         { .val = 'm',   .name = "mdt-index",     .has_arg = no_argument },
6171         { .val = 'O',   .name = "obd",           .has_arg = required_argument },
6172         { .val = 'r',   .name = "recursive",     .has_arg = no_argument },
6173         { .val = 'T',   .name = "mdt-count",     .has_arg = no_argument },
6174         { .val = 'v',   .name = "verbose",       .has_arg = no_argument },
6175         { .val = 'X',   .name = "max-inherit",   .has_arg = no_argument },
6176         { .val = 'y',   .name = "yaml",          .has_arg = no_argument },
6177         { .val = LFS_INHERIT_RR_OPT,
6178                         .name = "max-inherit-rr", .has_arg = no_argument },
6179         { .name = NULL } };
6180         int c, rc;
6181
6182         param.fp_get_lmv = 1;
6183
6184         while ((c = getopt_long(argc, argv,
6185                                 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6186                 switch (c) {
6187                 case 'c':
6188                 case 'T':
6189                         param.fp_verbose |= VERBOSE_COUNT;
6190                         break;
6191                 case 'D':
6192                         param.fp_get_default_lmv = 1;
6193                         break;
6194 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6195                 case 't':
6196                         fprintf(stderr,
6197                                 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6198                         /* fallthrough */
6199 #endif
6200                 case 'H':
6201                         param.fp_verbose |= VERBOSE_HASH_TYPE;
6202                         break;
6203                 case 'i': /* fallthrough */
6204                 case 'm':
6205                         param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6206                         break;
6207                 case 'O':
6208                         if (param.fp_obd_uuid) {
6209                                 fprintf(stderr,
6210                                         "%s: only one obduuid allowed",
6211                                         progname);
6212                                 return CMD_HELP;
6213                         }
6214                         param.fp_obd_uuid = (struct obd_uuid *)optarg;
6215                         break;
6216                 case 'r':
6217                         param.fp_recursive = 1;
6218                         break;
6219                 case 'v':
6220                         param.fp_verbose |= VERBOSE_DETAIL;
6221                         break;
6222                 case 'X':
6223                         param.fp_verbose |= VERBOSE_INHERIT;
6224                         break;
6225                 case LFS_INHERIT_RR_OPT:
6226                         param.fp_verbose |= VERBOSE_INHERIT_RR;
6227                         break;
6228                 case 'y':
6229                         param.fp_yaml = 1;
6230                         break;
6231                 default:
6232                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6233                                 progname, argv[optind - 1]);
6234                         /* fallthrough */
6235                 case 'h':
6236                         return CMD_HELP;
6237                 }
6238         }
6239
6240         if (optind >= argc)
6241                 return CMD_HELP;
6242
6243         if (param.fp_recursive)
6244                 param.fp_max_depth = -1;
6245
6246         if (!param.fp_verbose)
6247                 param.fp_verbose = VERBOSE_DEFAULT;
6248
6249         do {
6250                 rc = llapi_getstripe(argv[optind], &param);
6251         } while (++optind < argc && !rc);
6252
6253         if (rc)
6254                 fprintf(stderr, "error: %s failed for %s.\n",
6255                         argv[0], argv[optind - 1]);
6256         return rc;
6257 }
6258
6259 enum mntdf_flags {
6260         MNTDF_INODES    = 0x0001,
6261         MNTDF_COOKED    = 0x0002,
6262         MNTDF_LAZY      = 0x0004,
6263         MNTDF_VERBOSE   = 0x0008,
6264         MNTDF_SHOW      = 0x0010,
6265         MNTDF_DECIMAL   = 0x0020,
6266 };
6267
6268 #define COOK(value, base)                                       \
6269 ({                                                              \
6270         int radix = 0;                                          \
6271         while (value > base) {                                  \
6272                 value /= base;                                  \
6273                 radix++;                                        \
6274         }                                                       \
6275         radix;                                                  \
6276 })
6277 #define UUF     "%-20s"
6278 #define CSF     "%11s"
6279 #define CDF     "%11llu"
6280 #define HDF     "%8.1f%c"
6281 #define RSF     "%4s"
6282 #define RDF     "%3d%%"
6283
6284 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6285 {
6286         double avail, used, ratio = 0;
6287
6288         if (inodes) {
6289                 avail = st->os_ffree;
6290                 used = st->os_files - st->os_ffree;
6291         } else {
6292                 avail = st->os_bavail;
6293                 used = st->os_blocks - st->os_bfree;
6294         }
6295         if (avail + used > 0)
6296                 ratio = used / (used + avail) * 100;
6297
6298         /* Round up to match df(1) usage percentage */
6299         return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6300 }
6301
6302 /*
6303  * This is to identify various problem states for "lfs df" if .osn_err = true,
6304  * so only show flags reflecting those states by default. Informational states
6305  * are only shown with "-v" and use lower-case names to distinguish them.
6306  * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6307  */
6308 static struct obd_statfs_state_names {
6309         enum obd_statfs_state   osn_state;
6310         const char              osn_name;
6311         bool                    osn_err;
6312 } oss_names[] = {
6313         { .osn_state = OS_STATFS_DEGRADED,   .osn_name = 'D', .osn_err = true },
6314         { .osn_state = OS_STATFS_READONLY,   .osn_name = 'R', .osn_err = true },
6315         { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6316         { .osn_state = OS_STATFS_UNUSED1,    .osn_name = '?', .osn_err = true },
6317         { .osn_state = OS_STATFS_UNUSED2,    .osn_name = '?', .osn_err = true },
6318         { .osn_state = OS_STATFS_ENOSPC,     .osn_name = 'S', .osn_err = true },
6319         { .osn_state = OS_STATFS_ENOINO,     .osn_name = 'I', .osn_err = true },
6320         { .osn_state = OS_STATFS_SUM,        .osn_name = 'a', /* aggregate */ },
6321         { .osn_state = OS_STATFS_NONROT,     .osn_name = 'f', /* flash */     },
6322 };
6323
6324 static int showdf(char *mntdir, struct obd_statfs *stat,
6325                   char *uuid, enum mntdf_flags flags,
6326                   char *type, int index, int rc)
6327 {
6328         long long avail, used, total;
6329         int ratio = 0;
6330         char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6331         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6332         char tbuf[3 * sizeof(__u64)];
6333         char ubuf[3 * sizeof(__u64)];
6334         char abuf[3 * sizeof(__u64)];
6335         char rbuf[3 * sizeof(__u64)];
6336
6337         if (!uuid || !stat)
6338                 return -EINVAL;
6339
6340         switch (rc) {
6341         case 0:
6342                 if (flags & MNTDF_INODES) {
6343                         avail = stat->os_ffree;
6344                         used = stat->os_files - stat->os_ffree;
6345                         total = stat->os_files;
6346                 } else {
6347                         int shift = flags & MNTDF_COOKED ? 0 : 10;
6348
6349                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
6350                         used  = ((stat->os_blocks - stat->os_bfree) *
6351                                  stat->os_bsize) >> shift;
6352                         total = (stat->os_blocks * stat->os_bsize) >> shift;
6353                 }
6354
6355                 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6356
6357                 if (flags & MNTDF_COOKED) {
6358                         int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6359                         double cook_val;
6360                         int i;
6361
6362                         cook_val = (double)total;
6363                         i = COOK(cook_val, base);
6364                         if (i > 0)
6365                                 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6366                                          suffix[i - 1]);
6367                         else
6368                                 snprintf(tbuf, sizeof(tbuf), CDF, total);
6369
6370                         cook_val = (double)used;
6371                         i = COOK(cook_val, base);
6372                         if (i > 0)
6373                                 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6374                                          suffix[i - 1]);
6375                         else
6376                                 snprintf(ubuf, sizeof(ubuf), CDF, used);
6377
6378                         cook_val = (double)avail;
6379                         i = COOK(cook_val, base);
6380                         if (i > 0)
6381                                 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6382                                          suffix[i - 1]);
6383                         else
6384                                 snprintf(abuf, sizeof(abuf), CDF, avail);
6385                 } else {
6386                         snprintf(tbuf, sizeof(tbuf), CDF, total);
6387                         snprintf(ubuf, sizeof(tbuf), CDF, used);
6388                         snprintf(abuf, sizeof(tbuf), CDF, avail);
6389                 }
6390
6391                 sprintf(rbuf, RDF, ratio);
6392                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6393                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6394                 if (type)
6395                         printf("[%s:%d]", type, index);
6396
6397                 if (stat->os_state) {
6398                         uint32_t i;
6399
6400                         printf(" ");
6401                         for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6402                                 if (oss_names[i].osn_state & stat->os_state &&
6403                                     (oss_names[i].osn_err ||
6404                                      flags & MNTDF_VERBOSE))
6405                                         printf("%c", oss_names[i].osn_name);
6406                         }
6407                 }
6408
6409                 printf("\n");
6410                 break;
6411         case -ENODATA:
6412                 printf(UUF": inactive device\n", uuid);
6413                 break;
6414         default:
6415                 printf(UUF": %s\n", uuid, strerror(-rc));
6416                 break;
6417         }
6418
6419         return 0;
6420 }
6421
6422 struct ll_stat_type {
6423         int   st_op;
6424         char *st_name;
6425 };
6426
6427 #define LL_STATFS_MAX   LOV_MAX_STRIPE_COUNT
6428
6429 struct ll_statfs_data {
6430         int                     sd_index;
6431         struct obd_statfs       sd_st;
6432 };
6433
6434 struct ll_statfs_buf {
6435         int                     sb_count;
6436         struct ll_statfs_data   sb_buf[LL_STATFS_MAX];
6437 };
6438
6439 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6440                  int ops, struct ll_statfs_buf *lsb)
6441 {
6442         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6443         struct obd_uuid uuid_buf;
6444         char *poolname = NULL;
6445         struct ll_stat_type types[] = {
6446                 { .st_op = LL_STATFS_LMV,       .st_name = "MDT" },
6447                 { .st_op = LL_STATFS_LOV,       .st_name = "OST" },
6448                 { .st_name = NULL } };
6449         struct ll_stat_type *tp;
6450         __u64 ost_files = 0;
6451         __u64 ost_ffree = 0;
6452         __u32 index;
6453         __u32 type;
6454         int fd;
6455         int rc = 0;
6456         int rc2;
6457
6458         if (pool) {
6459                 poolname = strchr(pool, '.');
6460                 if (poolname) {
6461                         if (strncmp(fsname, pool, strlen(fsname))) {
6462                                 fprintf(stderr, "filesystem name incorrect\n");
6463                                 return -ENODEV;
6464                         }
6465                         poolname++;
6466                 } else
6467                         poolname = pool;
6468         }
6469
6470         fd = open(mntdir, O_RDONLY);
6471         if (fd < 0) {
6472                 rc = -errno;
6473                 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6474                         strerror(errno));
6475                 return rc;
6476         }
6477
6478         if (flags & MNTDF_SHOW) {
6479                 if (flags & MNTDF_INODES)
6480                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6481                                "UUID", "Inodes", "IUsed", "IFree",
6482                                "IUse%", "Mounted on");
6483                 else
6484                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6485                                "UUID",
6486                                flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6487                                "Used", "Available", "Use%", "Mounted on");
6488         }
6489
6490         for (tp = types; tp->st_name != NULL; tp++) {
6491                 bool have_ost = false;
6492
6493                 if (!(tp->st_op & ops))
6494                         continue;
6495
6496                 for (index = 0; ; index++) {
6497                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
6498                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6499                         type = flags & MNTDF_LAZY ?
6500                                 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6501                         rc2 = llapi_obd_fstatfs(fd, type, index,
6502                                                &stat_buf, &uuid_buf);
6503                         if (rc2 == -ENODEV)
6504                                 break;
6505                         if (rc2 == -EAGAIN)
6506                                 continue;
6507                         if (rc2 == -ENODATA) { /* Inactive device, OK. */
6508                                 if (!(flags & MNTDF_VERBOSE))
6509                                         continue;
6510                         } else if (rc2 < 0 && rc == 0) {
6511                                 rc = rc2;
6512                         }
6513
6514                         /*
6515                          * If we have OSTs then don't report MDT block counts.
6516                          * For MDT-only filesystems the expectation is that all
6517                          * layouts have a DoM component.  For filesystems with
6518                          * OSTs, files are not necessarily going to store data
6519                          * on MDTs, and MDT space is limited to a fraction of
6520                          * OST space, so don't include it in the summary.
6521                          */
6522                         if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6523                                 have_ost = true;
6524                                 sum.os_blocks = 0;
6525                                 sum.os_bfree = 0;
6526                                 sum.os_bavail = 0;
6527                         }
6528
6529                         if (poolname && tp->st_op == LL_STATFS_LOV &&
6530                             llapi_search_ost(fsname, poolname,
6531                                              obd_uuid2str(&uuid_buf)) != 1)
6532                                 continue;
6533
6534                         /*
6535                          * the llapi_obd_fstatfs() call may have returned with
6536                          * an error, but if it filled in uuid_buf we will at
6537                          * lease use that to print out a message for that OBD.
6538                          * If we didn't get anything in the uuid_buf, then fill
6539                          * it in so that we can print an error message.
6540                          */
6541                         if (uuid_buf.uuid[0] == '\0')
6542                                 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6543                                          "%s%04x", tp->st_name, index);
6544                         if (!rc && lsb) {
6545                                 lsb->sb_buf[lsb->sb_count].sd_index = index;
6546                                 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6547                                 lsb->sb_count++;
6548                         }
6549                         if (flags & MNTDF_SHOW)
6550                                 showdf(mntdir, &stat_buf,
6551                                        obd_uuid2str(&uuid_buf), flags,
6552                                        tp->st_name, index, rc2);
6553
6554                         if (rc2)
6555                                 continue;
6556
6557                         if (tp->st_op == LL_STATFS_LMV) {
6558                                 sum.os_ffree += stat_buf.os_ffree;
6559                                 sum.os_files += stat_buf.os_files;
6560                         } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6561                                 ost_files += stat_buf.os_files;
6562                                 ost_ffree += stat_buf.os_ffree;
6563                         }
6564                         sum.os_blocks += stat_buf.os_blocks *
6565                                          stat_buf.os_bsize;
6566                         sum.os_bfree  += stat_buf.os_bfree *
6567                                          stat_buf.os_bsize;
6568                         sum.os_bavail += stat_buf.os_bavail *
6569                                          stat_buf.os_bsize;
6570                 }
6571         }
6572
6573         close(fd);
6574
6575         /*
6576          * If we have _some_ OSTs, but don't have as many free objects on the
6577          * OST as inodes on the MDTs, reduce the reported number of inodes
6578          * to compensate, so that the "inodes in use" number is correct.
6579          * This should be kept in sync with ll_statfs_internal().
6580          */
6581         if (ost_files && ost_ffree < sum.os_ffree) {
6582                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6583                 sum.os_ffree = ost_ffree;
6584         }
6585         if (flags & MNTDF_SHOW) {
6586                 printf("\n");
6587                 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6588                 printf("\n");
6589         }
6590
6591         return rc;
6592 }
6593
6594 enum {
6595         LAYOUT_INHERIT_UNSET    = -2,
6596 };
6597
6598 /* functions */
6599 static int lfs_setdirstripe(int argc, char **argv)
6600 {
6601         char *dname;
6602         struct lfs_setstripe_args lsa = { 0 };
6603         struct llapi_stripe_param *param = NULL;
6604         __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6605         char *end;
6606         int c;
6607         char *mode_opt = NULL;
6608         bool default_stripe = false;
6609         bool delete = false;
6610         bool foreign_mode = false;
6611         mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6612         mode_t previous_mode = 0;
6613         char *xattr = NULL;
6614         __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6615         int max_inherit = LAYOUT_INHERIT_UNSET;
6616         int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6617         struct option long_opts[] = {
6618         { .val = 'c',   .name = "count",        .has_arg = required_argument },
6619         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument },
6620         { .val = 'd',   .name = "delete",       .has_arg = no_argument },
6621         { .val = 'D',   .name = "default",      .has_arg = no_argument },
6622         { .val = 'D',   .name = "default_stripe", .has_arg = no_argument },
6623         { .val = LFS_LAYOUT_FLAGS_OPT,
6624                         .name = "flags",        .has_arg = required_argument },
6625         { .val = LFS_LAYOUT_FOREIGN_OPT,
6626                         .name = "foreign",      .has_arg = optional_argument},
6627         { .val = 'h',   .name = "help",         .has_arg = no_argument },
6628         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
6629 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6630         { .val = 'i',   .name = "mdt-index",    .has_arg = required_argument },
6631         { .val = 'i',   .name = "mdt",          .has_arg = required_argument },
6632 #else
6633 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
6634         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
6635         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
6636 #endif
6637 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6638         { .val = 'i',   .name = "index",        .has_arg = required_argument },
6639 #endif
6640         { .val = 'o',   .name = "mode",         .has_arg = required_argument },
6641 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6642         { .val = 't',   .name = "hash-type",    .has_arg = required_argument },
6643 #endif
6644         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
6645         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
6646         { .val = 'X',   .name = "max-inherit",  .has_arg = required_argument },
6647         { .val = LFS_INHERIT_RR_OPT,
6648                         .name = "max-inherit-rr", .has_arg = required_argument},
6649 /* setstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
6650         { .name = NULL } };
6651         int result = 0;
6652
6653         setstripe_args_init(&lsa);
6654
6655         while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6656                                 long_opts, NULL)) >= 0) {
6657                 switch (c) {
6658                 case 0:
6659                         /* Long options. */
6660                         break;
6661                 case 'c':
6662                 case 'T':
6663                         errno = 0;
6664                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6665                         if (errno != 0 || *end != '\0' ||
6666                             lsa.lsa_stripe_count < -1 ||
6667                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6668                                 fprintf(stderr,
6669                                         "%s: invalid stripe count '%s'\n",
6670                                         progname, optarg);
6671                                 return CMD_HELP;
6672                         }
6673                         break;
6674                 case 'd':
6675                         delete = true;
6676                         default_stripe = true;
6677                         break;
6678                 case 'D':
6679                         default_stripe = true;
6680                         break;
6681                 case LFS_LAYOUT_FOREIGN_OPT:
6682                         if (optarg) {
6683                                 /* check pure numeric */
6684                                 type = strtoul(optarg, &end, 0);
6685                                 if (*end) {
6686                                         /* check name */
6687                                         type = check_foreign_type_name(optarg);
6688                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6689                                                 fprintf(stderr,
6690                                                         "%s %s: unknown foreign type '%s'\n",
6691                                                         progname, argv[0],
6692                                                         optarg);
6693                                                 return CMD_HELP;
6694                                         }
6695                                 } else if (type >= UINT32_MAX) {
6696                                         fprintf(stderr,
6697                                                 "%s %s: invalid foreign type '%s'\n",
6698                                                 progname, argv[0], optarg);
6699                                         return CMD_HELP;
6700                                 }
6701                         }
6702                         foreign_mode = true;
6703                         break;
6704                 case LFS_LAYOUT_FLAGS_OPT:
6705                         errno = 0;
6706                         flags = strtoul(optarg, &end, 16);
6707                         if (errno != 0 || *end != '\0' ||
6708                             flags >= UINT32_MAX) {
6709                                 fprintf(stderr,
6710                                         "%s %s: invalid hex flags '%s'\n",
6711                                         progname, argv[0], optarg);
6712                                 return CMD_HELP;
6713                         }
6714                         if (!foreign_mode) {
6715                                 fprintf(stderr,
6716                                         "%s %s: hex flags must be specified with --foreign option\n",
6717                                         progname, argv[0]);
6718                                 return CMD_HELP;
6719                         }
6720                         break;
6721 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6722                 case 't':
6723                         fprintf(stderr,
6724                                 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6725                         /* fallthrough */
6726 #endif
6727                 case 'H':
6728                         lsa.lsa_pattern = check_hashtype(optarg);
6729                         if (lsa.lsa_pattern == 0) {
6730                                 fprintf(stderr,
6731                                         "%s %s: bad stripe hash type '%s'\n",
6732                                         progname, argv[0], optarg);
6733                                 return CMD_HELP;
6734                         }
6735                         break;
6736                 case 'i':
6737 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6738                 case 'm':
6739 #endif
6740 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6741                         if (strcmp(argv[optind - 1], "--index") == 0)
6742                                 fprintf(stderr,
6743                                         "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6744                                         progname, argv[0]);
6745 #endif
6746                         lsa.lsa_nr_tgts = parse_targets(mdts,
6747                                                 sizeof(mdts) / sizeof(__u32),
6748                                                 lsa.lsa_nr_tgts, optarg, NULL);
6749                         if (lsa.lsa_nr_tgts < 0) {
6750                                 fprintf(stderr,
6751                                         "%s %s: invalid MDT target(s) '%s'\n",
6752                                         progname, argv[0], optarg);
6753                                 return CMD_HELP;
6754                         }
6755
6756                         lsa.lsa_tgts = mdts;
6757                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6758                                 lsa.lsa_stripe_off = mdts[0];
6759                         break;
6760 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6761                 case 'm':
6762                         fprintf(stderr,
6763                                 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6764 #endif
6765                 case 'o':
6766                         mode_opt = optarg;
6767                         break;
6768                 case 'x':
6769                         xattr = optarg;
6770                         break;
6771                 case 'X':
6772                         if (!default_stripe) {
6773                                 fprintf(stderr,
6774                                         "%s %s: '--max-inherit' must be specified with '-D'\n",
6775                                         progname, argv[0]);
6776                                 return CMD_HELP;
6777                         }
6778                         errno = 0;
6779                         max_inherit = strtol(optarg, &end, 10);
6780                         if (errno != 0 || *end != '\0' || max_inherit < -2) {
6781                                 fprintf(stderr,
6782                                         "%s %s: invalid max-inherit '%s'\n",
6783                                         progname, argv[0], optarg);
6784                                 return CMD_HELP;
6785                         }
6786                         if (max_inherit == 0) {
6787                                 max_inherit = LMV_INHERIT_NONE;
6788                         } else if (max_inherit == -1) {
6789                                 max_inherit = LMV_INHERIT_UNLIMITED;
6790                         } else if (max_inherit > LMV_INHERIT_MAX) {
6791                                 fprintf(stderr,
6792                                         "%s %s: max-inherit %d exceeds maximum %u\n",
6793                                         progname, argv[0], max_inherit,
6794                                         LMV_INHERIT_MAX);
6795                                 return CMD_HELP;
6796                         }
6797                         break;
6798                 case LFS_INHERIT_RR_OPT:
6799                         if (!default_stripe) {
6800                                 fprintf(stderr,
6801                                         "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6802                                         progname, argv[0]);
6803                                 return CMD_HELP;
6804                         }
6805                         errno = 0;
6806                         max_inherit_rr = strtol(optarg, &end, 10);
6807                         if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6808                                 fprintf(stderr,
6809                                         "%s %s: invalid max-inherit-rr '%s'\n",
6810                                         progname, argv[0], optarg);
6811                                 return CMD_HELP;
6812                         }
6813                         if (max_inherit_rr == 0) {
6814                                 max_inherit_rr = LMV_INHERIT_RR_NONE;
6815                         } else if (max_inherit_rr == -1) {
6816                                 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6817                         } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6818                                 fprintf(stderr,
6819                                         "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6820                                         progname, argv[0], max_inherit_rr,
6821                                         LMV_INHERIT_RR_MAX);
6822                                 return CMD_HELP;
6823                         }
6824                         break;
6825                 default:
6826                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6827                                 progname, argv[optind - 1]);
6828                         /* fallthrough */
6829                 case 'h':
6830                         return CMD_HELP;
6831                 }
6832         }
6833
6834         if (optind == argc) {
6835                 fprintf(stderr, "%s %s: DIR must be specified\n",
6836                         progname, argv[0]);
6837                 return CMD_HELP;
6838         }
6839
6840         if (xattr && !foreign_mode) {
6841                 /*
6842                  * only print a warning as this is armless and will be
6843                  * ignored
6844                  */
6845                 fprintf(stderr,
6846                         "%s %s: xattr has been specified for non-foreign layout\n",
6847                         progname, argv[0]);
6848         } else if (foreign_mode && !xattr) {
6849                 fprintf(stderr,
6850                         "%s %s: xattr must be provided in foreign mode\n",
6851                         progname, argv[0]);
6852                 return CMD_HELP;
6853         }
6854
6855         if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6856             lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6857                 fprintf(stderr,
6858                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6859                         progname, argv[0]);
6860                 return CMD_HELP;
6861         }
6862
6863         if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6864             lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6865                 /* if no parameters set, create directory on least-used MDTs */
6866                 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
6867                 lsa.lsa_stripe_count = 1;
6868         }
6869
6870         if (delete &&
6871             (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6872              lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6873                 fprintf(stderr,
6874                         "%s %s: cannot specify -d with -c or -i options\n",
6875                         progname, argv[0]);
6876                 return CMD_HELP;
6877         }
6878
6879         if (mode_opt) {
6880                 mode = strtoul(mode_opt, &end, 8);
6881                 if (*end != '\0') {
6882                         fprintf(stderr,
6883                                 "%s %s: bad MODE '%s'\n",
6884                                 progname, argv[0], mode_opt);
6885                         return CMD_HELP;
6886                 }
6887                 previous_mode = umask(0);
6888         }
6889
6890         if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6891             lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6892             lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
6893                 fprintf(stderr,
6894                         "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
6895                         progname, argv[0], lsa.lsa_stripe_off);
6896                 return CMD_HELP;
6897         }
6898
6899         /* foreign LMV/dir case */
6900         if (foreign_mode) {
6901                 if (argc > optind + 1) {
6902                         fprintf(stderr,
6903                                 "%s %s: cannot specify multiple foreign dirs\n",
6904                                 progname, argv[0]);
6905                         return CMD_HELP;
6906                 }
6907
6908                 dname = argv[optind];
6909                 result = llapi_dir_create_foreign(dname, mode, type, flags,
6910                                                   xattr);
6911                 if (result != 0)
6912                         fprintf(stderr,
6913                                 "%s mkdir: can't create foreign dir '%s': %s\n",
6914                                 progname, dname, strerror(-result));
6915                 return result;
6916         }
6917
6918         /*
6919          * initialize stripe parameters, in case param is converted to specific,
6920          * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6921          */
6922         param = calloc(1, offsetof(typeof(*param),
6923                        lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6924                                 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6925         if (!param) {
6926                 fprintf(stderr,
6927                         "%s %s: cannot allocate memory for parameters: %s\n",
6928                         progname, argv[0], strerror(ENOMEM));
6929                 return CMD_HELP;
6930         }
6931
6932         /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6933         if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
6934             (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6935              lsa.lsa_stripe_count == 0))
6936                 lsa.lsa_stripe_count = 1;
6937         if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6938                 param->lsp_stripe_count = lsa.lsa_stripe_count;
6939         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6940                 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
6941         else
6942                 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6943         if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6944                 param->lsp_stripe_pattern = lsa.lsa_pattern;
6945         else
6946                 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6947         param->lsp_pool = lsa.lsa_pool_name;
6948         param->lsp_is_specific = false;
6949         if (default_stripe) {
6950                 if (max_inherit == LAYOUT_INHERIT_UNSET)
6951                         max_inherit = LMV_INHERIT_DEFAULT;
6952                 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6953                         max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6954                 param->lsp_max_inherit = max_inherit;
6955                 param->lsp_max_inherit_rr = max_inherit_rr;
6956         }
6957         if (strcmp(argv[0], "mkdir") == 0)
6958                 param->lsp_is_create = true;
6959         if (lsa.lsa_nr_tgts > 1) {
6960                 if (lsa.lsa_stripe_count > 0 &&
6961                     lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6962                     lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6963                         fprintf(stderr,
6964                                 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6965                                 argv[0], lsa.lsa_stripe_count,
6966                                 lsa.lsa_nr_tgts);
6967                         free(param);
6968                         return CMD_HELP;
6969                 }
6970
6971                 param->lsp_is_specific = true;
6972                 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6973                 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6974         }
6975
6976         dname = argv[optind];
6977         do {
6978                 if (default_stripe) {
6979                         result = llapi_dir_set_default_lmv(dname, param);
6980                         if (result)
6981                                 fprintf(stderr,
6982                                         "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6983                                         progname, dname, strerror(-result));
6984                         continue;
6985                 }
6986
6987                 result = llapi_dir_create(dname, mode, param);
6988                 if (result)
6989                         fprintf(stderr,
6990                                 "%s setdirstripe: cannot create dir '%s': %s\n",
6991                                 progname, dname, strerror(-result));
6992         } while (!result && (dname = argv[++optind]));
6993
6994         if (mode_opt)
6995                 umask(previous_mode);
6996
6997         free(param);
6998         return result;
6999 }
7000
7001 /* functions */
7002 static int lfs_rmentry(int argc, char **argv)
7003 {
7004         char *dname;
7005         int   index;
7006         int   result = 0;
7007
7008         if (argc <= 1) {
7009                 fprintf(stderr, "error: %s: missing dirname\n",
7010                         argv[0]);
7011                 return CMD_HELP;
7012         }
7013
7014         index = 1;
7015         dname = argv[index];
7016         while (dname) {
7017                 result = llapi_direntry_remove(dname);
7018                 if (result) {
7019                         fprintf(stderr,
7020                                 "error: %s: remove dir entry '%s' failed\n",
7021                                 argv[0], dname);
7022                         break;
7023                 }
7024                 dname = argv[++index];
7025         }
7026         return result;
7027 }
7028
7029 static int lfs_unlink_foreign(int argc, char **argv)
7030 {
7031         char *name;
7032         int   index;
7033         int   result = 0;
7034
7035         if (argc <= 1) {
7036                 fprintf(stderr, "error: %s: missing pathname\n",
7037                         argv[0]);
7038                 return CMD_HELP;
7039         }
7040
7041         index = 1;
7042         name = argv[index];
7043         while (name != NULL) {
7044                 result = llapi_unlink_foreign(name);
7045                 if (result) {
7046                         fprintf(stderr,
7047                                 "error: %s: unlink foreign entry '%s' failed\n",
7048                                 argv[0], name);
7049                         break;
7050                 }
7051                 name = argv[++index];
7052         }
7053         return result;
7054 }
7055
7056 static int lfs_mv(int argc, char **argv)
7057 {
7058         struct lmv_user_md lmu = { LMV_USER_MAGIC };
7059         struct find_param param = {
7060                 .fp_max_depth = -1,
7061                 .fp_mdt_index = -1,
7062         };
7063         char *end;
7064         int c;
7065         int rc = 0;
7066         struct option long_opts[] = {
7067         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
7068         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
7069         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7070         { .name = NULL } };
7071
7072         while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7073                 switch (c) {
7074 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7075                 case 'M':
7076                         fprintf(stderr,
7077                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7078 #endif
7079                 case 'm':
7080                         errno = 0;
7081                         lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7082                         if (errno != 0 || *end != '\0' ||
7083                             lmu.lum_stripe_offset >= UINT32_MAX) {
7084                                 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7085                                         progname, optarg);
7086                                 return CMD_HELP;
7087                         }
7088                         break;
7089                 case 'v':
7090                         param.fp_verbose = VERBOSE_DETAIL;
7091                         break;
7092                 default:
7093                         fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7094                                 progname, argv[optind - 1]);
7095                         return CMD_HELP;
7096                 }
7097         }
7098
7099         if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7100                 fprintf(stderr, "%s mv: MDT index must be specified\n",
7101                         progname);
7102                 return CMD_HELP;
7103         }
7104
7105         if (optind >= argc) {
7106                 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7107                 return CMD_HELP;
7108         }
7109
7110         lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7111
7112         /* initialize migrate mdt parameters */
7113         param.fp_lmv_md = &lmu;
7114         param.fp_migrate = 1;
7115         rc = llapi_migrate_mdt(argv[optind], &param);
7116         if (rc != 0)
7117                 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7118                         progname, argv[optind], lmu.lum_stripe_offset,
7119                         strerror(-rc));
7120         return rc;
7121 }
7122
7123 static int lfs_osts(int argc, char **argv)
7124 {
7125         return lfs_tgts(argc, argv);
7126 }
7127
7128 static int lfs_mdts(int argc, char **argv)
7129 {
7130         return lfs_tgts(argc, argv);
7131 }
7132
7133 static int lfs_df(int argc, char **argv)
7134 {
7135         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7136         enum mntdf_flags flags = MNTDF_SHOW;
7137         int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7138         int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7139         char fsname[PATH_MAX] = "", *pool_name = NULL;
7140         struct option long_opts[] = {
7141         { .val = 'h',   .name = "human-readable", .has_arg = no_argument },
7142         { .val = 'H',   .name = "si",           .has_arg = no_argument },
7143         { .val = 'i',   .name = "inodes",       .has_arg = no_argument },
7144         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
7145         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
7146         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7147         { .name = NULL} };
7148
7149         while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7150                 switch (c) {
7151                 case 'h':
7152                         flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7153                         break;
7154                 case 'H':
7155                         flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7156                         break;
7157                 case 'i':
7158                         flags |= MNTDF_INODES;
7159                         break;
7160                 case 'l':
7161                         flags |= MNTDF_LAZY;
7162                         break;
7163                 case 'p':
7164                         pool_name = optarg;
7165                         break;
7166                 case 'v':
7167                         flags |= MNTDF_VERBOSE;
7168                         break;
7169                 default:
7170                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7171                                 progname, argv[optind - 1]);
7172                         return CMD_HELP;
7173                 }
7174         }
7175
7176         /* Handle case where path is not specified */
7177         if (optind == argc) {
7178                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7179                         /* Check if we have a mount point */
7180                         if (mntdir[0] == '\0')
7181                                 continue;
7182
7183                         rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7184                         if (rc || path[0] != '\0')
7185                                 break;
7186
7187                         fsname[0] = '\0'; /* avoid matching in next loop */
7188                         mntdir[0] = '\0'; /* avoid matching in next loop */
7189                         path[0] = '\0'; /* clean for next loop */
7190                 }
7191                 return rc;
7192         }
7193
7194         /* Loop through all the remaining arguments. These are Lustre FS
7195          * paths.
7196          */
7197         for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7198                 bool valid = false;
7199
7200                 fsname[0] = '\0'; /* start clean */
7201                 mntdir[0] = '\0'; /* start clean */
7202                 path[0] = '\0';   /* start clean */
7203
7204                 /* path does not exists at all */
7205                 if (!realpath(argv[arg_idx], path)) {
7206                         rc = -errno;
7207                         fprintf(stderr, "error: invalid path '%s': %s\n",
7208                                 argv[arg_idx], strerror(-rc));
7209                         /* save first seen error */
7210                         if (!rc1)
7211                                 rc1 = rc;
7212
7213                         continue;
7214                 }
7215
7216                 /* path exists but may not be a Lustre filesystem */
7217                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7218                         /* Check if we have a mount point */
7219                         if (mntdir[0] == '\0')
7220                                 continue;
7221
7222                         rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7223                         if (rc || path[0] != '\0') {
7224                                 valid = true;
7225
7226                                 /* save first seen error */
7227                                 if (!rc1)
7228                                         rc1 = rc;
7229                                 break;
7230                         }
7231                 }
7232
7233                 if (!valid) {
7234                         llapi_printf(LLAPI_MSG_ERROR,
7235                                      "%s:%s Not a Lustre filesystem\n",
7236                                      argv[0], argv[arg_idx]);
7237                         /* save first seen error */
7238                         if (!rc1)
7239                                 rc1 = -EOPNOTSUPP;
7240                 }
7241         }
7242
7243         return rc1;
7244 }
7245
7246 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7247                           bool opt_instance, bool opt_fsname, bool opt_mntdir)
7248 {
7249         int rc = 0;
7250
7251         if (opt_fsname == opt_instance) { /* both true or both false */
7252                 rc = llapi_getname(mntdir, buf, buflen);
7253         } else if (opt_fsname) {
7254                 /*
7255                  * llapi_search_mounts() fills @buf with fsname, but that is not
7256                  * called if explicit paths are specified on the command-line
7257                  */
7258                 if (buf[0] == '\0')
7259                         rc = llapi_get_fsname(mntdir, buf, buflen);
7260         } else /* if (opt_instance) */ {
7261                 rc = llapi_get_instance(mntdir, buf, buflen);
7262         }
7263
7264         if (rc < 0) {
7265                 fprintf(stderr, "cannot get instance for '%s': %s\n",
7266                         mntdir, strerror(-rc));
7267                 return rc;
7268         }
7269
7270         if (opt_mntdir)
7271                 printf("%s %s\n", buf, mntdir);
7272         else
7273                 printf("%s\n", buf);
7274
7275         return 0;
7276 }
7277
7278 static int lfs_getname(int argc, char **argv)
7279 {
7280         struct option long_opts[] = {
7281         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7282         { .val = 'i',   .name = "instance",     .has_arg = no_argument },
7283         { .val = 'n',   .name = "fsname",       .has_arg = no_argument },
7284         { .name = NULL} };
7285         bool opt_instance = false, opt_fsname = false;
7286         char fsname[PATH_MAX] = "";
7287         int rc = 0, rc2, c;
7288
7289         while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7290                 switch (c) {
7291                 case 'i':
7292                         opt_instance = true;
7293                         break;
7294                 case 'n':
7295                         opt_fsname = true;
7296                         break;
7297                 default:
7298                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7299                                 progname, argv[optind - 1]);
7300                         /* fallthrough */
7301                 case 'h':
7302                         return CMD_HELP;
7303                 }
7304         }
7305
7306         if (optind == argc) { /* no paths specified, get all paths. */
7307                 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7308                 int index = 0;
7309
7310                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7311                         rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7312                                              opt_instance, opt_fsname, true);
7313                         if (!rc)
7314                                 rc = rc2;
7315                         path[0] = fsname[0] = mntdir[0] = '\0';
7316                 }
7317         } else { /* paths specified, only attempt to search these. */
7318                 bool opt_mntdir;
7319
7320                 /* if only one path is given, print only requested info */
7321                 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7322
7323                 for (; optind < argc; optind++) {
7324                         rc2 = print_instance(argv[optind], fsname,
7325                                              sizeof(fsname), opt_instance,
7326                                              opt_fsname, opt_mntdir);
7327                         if (!rc)
7328                                 rc = rc2;
7329                         fsname[0] = '\0';
7330                 }
7331         }
7332
7333         return rc;
7334 }
7335
7336 static int lfs_check(int argc, char **argv)
7337 {
7338         char mntdir[PATH_MAX] = {'\0'};
7339         int num_types = 1;
7340         char *obd_types[3];
7341         char obd_type1[4];
7342         char obd_type2[4];
7343         char obd_type3[4];
7344         int rc;
7345
7346         if (argc != 2) {
7347                 fprintf(stderr, "%s check: server type must be specified\n",
7348                         progname);
7349                 return CMD_HELP;
7350         }
7351
7352         obd_types[0] = obd_type1;
7353         obd_types[1] = obd_type2;
7354         obd_types[2] = obd_type3;
7355
7356         if (strcmp(argv[1], "osts") == 0) {
7357                 strcpy(obd_types[0], "osc");
7358         } else if (strcmp(argv[1], "mdts") == 0 ||
7359                    strcmp(argv[1], "mds") == 0) {
7360                 strcpy(obd_types[0], "mdc");
7361         } else if (strcmp(argv[1], "mgts") == 0) {
7362                 strcpy(obd_types[0], "mgc");
7363         } else if (strcmp(argv[1], "all") == 0 ||
7364                    strcmp(argv[1], "servers") == 0) {
7365                 num_types = 3;
7366                 strcpy(obd_types[0], "osc");
7367                 strcpy(obd_types[1], "mdc");
7368                 strcpy(obd_types[2], "mgc");
7369         } else {
7370                 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7371                         progname, argv[1]);
7372                 return CMD_HELP;
7373         }
7374
7375         rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7376         if (rc < 0 || mntdir[0] == '\0') {
7377                 fprintf(stderr,
7378                         "%s check: cannot find mounted Lustre filesystem: %s\n",
7379                         progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7380                 return rc;
7381         }
7382
7383         rc = llapi_target_check(num_types, obd_types, mntdir);
7384         if (rc)
7385                 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7386                         progname, argv[1], strerror(-rc));
7387
7388         return rc;
7389 }
7390
7391 #ifdef HAVE_SYS_QUOTA_H
7392 #define ADD_OVERFLOW(a, b) \
7393                      ((((a) + (b)) < (a)) ? \
7394                       ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7395
7396 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7397  * returns the value or ULONG_MAX on integer overflow or incorrect format
7398  * Notes:
7399  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7400  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7401  *        3. empty integer value is interpreted as 0
7402  */
7403 static unsigned long str2sec(const char *timestr)
7404 {
7405         const char spec[] = "smhdw";
7406         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7407         unsigned long val = 0;
7408         char *tail;
7409
7410         if (strpbrk(timestr, spec) == NULL) {
7411                 /*
7412                  * no specifiers inside the time string,
7413                  * should treat it as an integer value
7414                  */
7415                 val = strtoul(timestr, &tail, 10);
7416                 return *tail ? ULONG_MAX : val;
7417         }
7418
7419         /* format string is XXwXXdXXhXXmXXs */
7420         while (*timestr) {
7421                 unsigned long v;
7422                 int ind;
7423                 char *ptr;
7424
7425                 v = strtoul(timestr, &tail, 10);
7426                 if (v == ULONG_MAX || *tail == '\0')
7427                         /*
7428                          * value too large (ULONG_MAX or more)
7429                          * or missing specifier
7430                          */
7431                         goto error;
7432
7433                 ptr = strchr(spec, *tail);
7434                 if (!ptr)
7435                         /* unknown specifier */
7436                         goto error;
7437
7438                 ind = ptr - spec;
7439
7440                 /* check if product will overflow the type */
7441                 if (!(v < ULONG_MAX / mult[ind]))
7442                         goto error;
7443
7444                 ADD_OVERFLOW(val, mult[ind] * v);
7445                 if (val == ULONG_MAX)
7446                         goto error;
7447
7448                 timestr = tail + 1;
7449         }
7450
7451         return val;
7452
7453 error:
7454         return ULONG_MAX;
7455 }
7456
7457 #define ARG2ULL(nr, str, def_units)                                     \
7458 do {                                                                    \
7459         unsigned long long limit, units = def_units;                    \
7460         int rc;                                                         \
7461                                                                         \
7462         rc = llapi_parse_size(str, &limit, &units, 1);                  \
7463         if (rc < 0) {                                                   \
7464                 fprintf(stderr, "%s: invalid limit '%s'\n",             \
7465                         progname, str);                                 \
7466                 return CMD_HELP;                                        \
7467         }                                                               \
7468         nr = limit;                                                     \
7469 } while (0)
7470
7471 static inline int has_times_option(int argc, char **argv)
7472 {
7473         int i;
7474
7475         for (i = 1; i < argc; i++)
7476                 if (!strcmp(argv[i], "-t"))
7477                         return 1;
7478
7479         return 0;
7480 }
7481
7482 static inline int lfs_verify_poolarg(char *pool)
7483 {
7484         if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7485                 fprintf(stderr,
7486                         "Pool name '%.*s' is longer than %d\n",
7487                         LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7488                 return 1;
7489         }
7490         return 0;
7491 }
7492
7493 /* special grace time, only notify the user when its quota is over soft limit
7494  * but doesn't block new writes until the hard limit is reached.
7495  */
7496 #define NOTIFY_GRACE            "notify"
7497 #define NOTIFY_GRACE_TIME       LQUOTA_GRACE_MASK
7498
7499 #ifndef toqb
7500 static inline __u64 lustre_stoqb(size_t space)
7501 {
7502         return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7503 }
7504 #else
7505 #define lustre_stoqb   toqb
7506 #endif
7507
7508 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7509 {
7510         int c, rc;
7511         char *mnt, *obd_type = (char *)qctl->obd_type;
7512         struct obd_dqblk *dqb = &qctl->qc_dqblk;
7513         struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7514         struct option long_opts[] = {
7515         { .val = 'b',   .name = "block-grace",  .has_arg = required_argument },
7516         { .val = 'g',   .name = "group",        .has_arg = no_argument },
7517         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7518         { .val = 'i',   .name = "inode-grace",  .has_arg = required_argument },
7519         { .val = 'p',   .name = "projid",       .has_arg = no_argument },
7520         { .val = 't',   .name = "times",        .has_arg = no_argument },
7521         { .val = 'u',   .name = "user",         .has_arg = no_argument },
7522         { .val = LFS_POOL_OPT,
7523                         .name = "pool",         .has_arg = required_argument },
7524         { .name = NULL } };
7525         int qtype;
7526
7527         qctl->qc_cmd  = LUSTRE_Q_SETINFO;
7528         qctl->qc_type = ALLQUOTA;
7529
7530         while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7531                                 long_opts, NULL)) != -1) {
7532                 switch (c) {
7533                 case 'u':
7534                         qtype = USRQUOTA;
7535                         goto quota_type;
7536                 case 'g':
7537                         qtype = GRPQUOTA;
7538                         goto quota_type;
7539                 case 'p':
7540                         qtype = PRJQUOTA;
7541 quota_type:
7542                         if (qctl->qc_type != ALLQUOTA) {
7543                                 fprintf(stderr,
7544                                         "%s: -u/g/p cannot be used more than once\n",
7545                                         progname);
7546                                 return CMD_HELP;
7547                         }
7548                         qctl->qc_type = qtype;
7549                         break;
7550                 case 'b':
7551                         if (strncmp(optarg, NOTIFY_GRACE,
7552                                     strlen(NOTIFY_GRACE)) == 0) {
7553                                 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7554                         } else {
7555                                 dqi->dqi_bgrace = str2sec(optarg);
7556                                 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7557                                         fprintf(stderr,
7558                                                 "%s: bad block-grace: %s\n",
7559                                                 progname, optarg);
7560                                         return CMD_HELP;
7561                                 }
7562                         }
7563                         dqb->dqb_valid |= QIF_BTIME;
7564                         break;
7565                 case 'i':
7566                         if (strncmp(optarg, NOTIFY_GRACE,
7567                                     strlen(NOTIFY_GRACE)) == 0) {
7568                                 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7569                         } else {
7570                                 dqi->dqi_igrace = str2sec(optarg);
7571                                 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7572                                         fprintf(stderr,
7573                                                 "%s: bad inode-grace: %s\n",
7574                                                 progname, optarg);
7575                                         return CMD_HELP;
7576                                 }
7577                         }
7578                         dqb->dqb_valid |= QIF_ITIME;
7579                         break;
7580                 case 't': /* Yes, of course! */
7581                         break;
7582                 case LFS_POOL_OPT:
7583                         if (lfs_verify_poolarg(optarg))
7584                                 return -1;
7585                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7586                         qctl->qc_cmd  = LUSTRE_Q_SETINFOPOOL;
7587                         break;
7588                 /* getopt prints error message for us when opterr != 0 */
7589                 default:
7590                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7591                                 progname, argv[optind - 1]);
7592                         /* fallthrough */
7593                 case 'h':
7594                         return CMD_HELP;
7595                 }
7596         }
7597
7598         if (qctl->qc_type == ALLQUOTA) {
7599                 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7600                         progname);
7601                 return CMD_HELP;
7602         }
7603
7604         if (optind != argc - 1) {
7605                 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7606                         progname, argv[optind + 1]);
7607                 return CMD_HELP;
7608         }
7609
7610         mnt = argv[optind];
7611         rc = llapi_quotactl(mnt, qctl);
7612         if (rc) {
7613                 if (*obd_type)
7614                         fprintf(stderr, "%s %s ", obd_type,
7615                                 obd_uuid2str(&qctl->obd_uuid));
7616                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7617                 return rc;
7618         }
7619
7620         return 0;
7621 }
7622
7623 #define BSLIMIT (1 << 0)
7624 #define BHLIMIT (1 << 1)
7625 #define ISLIMIT (1 << 2)
7626 #define IHLIMIT (1 << 3)
7627
7628 int lfs_setquota(int argc, char **argv)
7629 {
7630         int c, rc = 0;
7631         struct if_quotactl *qctl;
7632         char *mnt, *obd_type;
7633         struct obd_dqblk *dqb;
7634         struct option long_opts[] = {
7635         { .val = 'b',   .name = "block-softlimit",
7636                                                 .has_arg = required_argument },
7637         { .val = 'B',   .name = "block-hardlimit",
7638                                                 .has_arg = required_argument },
7639         { .val = 'd',   .name = "default",      .has_arg = no_argument },
7640         { .val = 'g',   .name = "group",        .has_arg = required_argument },
7641         { .val = 'G',   .name = "default-grp",  .has_arg = no_argument },
7642         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7643         { .val = 'i',   .name = "inode-softlimit",
7644                                                 .has_arg = required_argument },
7645         { .val = 'I',   .name = "inode-hardlimit",
7646                                                 .has_arg = required_argument },
7647         { .val = 'p',   .name = "projid",       .has_arg = required_argument },
7648         { .val = 'P',   .name = "default-prj",  .has_arg = no_argument },
7649         { .val = 'u',   .name = "user",         .has_arg = required_argument },
7650         { .val = 'U',   .name = "default-usr",  .has_arg = no_argument },
7651         { .val = LFS_POOL_OPT,
7652                         .name = "pool",         .has_arg = required_argument },
7653         { .name = NULL } };
7654         unsigned int limit_mask = 0;
7655         bool use_default = false;
7656         int qtype, qctl_len;
7657
7658         qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7659         qctl = malloc(qctl_len);
7660         if (!qctl)
7661                 return -ENOMEM;
7662
7663         memset(qctl, 0, qctl_len);
7664         obd_type = (char *)qctl->obd_type;
7665         dqb = &qctl->qc_dqblk;
7666
7667         if (has_times_option(argc, argv)) {
7668                 rc = lfs_setquota_times(argc, argv, qctl);
7669                 goto out;
7670         }
7671
7672         qctl->qc_cmd  = LUSTRE_Q_SETQUOTA;
7673         qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7674                                    * so it can be used as a marker that qc_type
7675                                    * isn't reinitialized from command line
7676                                    */
7677
7678         while ((c = getopt_long(argc, argv, "b:B:dg:Ghi:I:p:Pu:U",
7679                 long_opts, NULL)) != -1) {
7680                 switch (c) {
7681                 case 'U':
7682                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7683                         qtype = USRQUOTA;
7684                         qctl->qc_id = 0;
7685                         goto quota_type_def;
7686                 case 'u':
7687                         qtype = USRQUOTA;
7688                         rc = name2uid(&qctl->qc_id, optarg);
7689                         goto quota_type;
7690                 case 'G':
7691                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7692                         qtype = GRPQUOTA;
7693                         qctl->qc_id = 0;
7694                         goto quota_type_def;
7695                 case 'g':
7696                         qtype = GRPQUOTA;
7697                         rc = name2gid(&qctl->qc_id, optarg);
7698                         goto quota_type;
7699                 case 'P':
7700                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7701                         qtype = PRJQUOTA;
7702                         qctl->qc_id = 0;
7703                         goto quota_type_def;
7704                 case 'p':
7705                         qtype = PRJQUOTA;
7706                         rc = name2projid(&qctl->qc_id, optarg);
7707 quota_type:
7708                         if (rc) {
7709                                 if (str2quotaid(&qctl->qc_id, optarg)) {
7710                                         fprintf(stderr,
7711                                                 "%s setquota: invalid id '%s'\n",
7712                                                 progname, optarg);
7713                                         rc = -1;
7714                                         goto out;
7715                                 }
7716                         }
7717
7718                         if (qctl->qc_id == 0) {
7719                                 fprintf(stderr,
7720                                         "%s setquota: can't set quota for root usr/group/project.\n",
7721                                         progname);
7722                                 rc = -1;
7723                                 goto out;
7724                         }
7725
7726 quota_type_def:
7727                         if (qctl->qc_type != ALLQUOTA) {
7728                                 fprintf(stderr,
7729                                         "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7730                                         progname);
7731                                 rc = CMD_HELP;
7732                                 goto out;
7733                         }
7734                         qctl->qc_type = qtype;
7735                         break;
7736                 case 'd':
7737                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7738                         use_default = true;
7739                         break;
7740                 case 'b':
7741                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7742                         dqb->dqb_bsoftlimit >>= 10;
7743                         limit_mask |= BSLIMIT;
7744                         if (dqb->dqb_bsoftlimit &&
7745                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7746                                 fprintf(stderr,
7747                                         "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7748                                         progname,
7749                                         (unsigned long long)dqb->dqb_bsoftlimit,
7750                                         progname);
7751                         break;
7752                 case 'B':
7753                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7754                         dqb->dqb_bhardlimit >>= 10;
7755                         limit_mask |= BHLIMIT;
7756                         if (dqb->dqb_bhardlimit &&
7757                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7758                                 fprintf(stderr,
7759                                         "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7760                                         "See '%s help setquota' or Lustre manual for details\n",
7761                                         progname,
7762                                         (unsigned long long)dqb->dqb_bhardlimit,
7763                                         progname);
7764                         break;
7765                 case 'i':
7766                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7767                         limit_mask |= ISLIMIT;
7768                         if (dqb->dqb_isoftlimit &&
7769                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7770                                 fprintf(stderr,
7771                                         "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7772                                         progname,
7773                                         (unsigned long long)dqb->dqb_isoftlimit,
7774                                         progname);
7775                         break;
7776                 case 'I':
7777                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7778                         limit_mask |= IHLIMIT;
7779                         if (dqb->dqb_ihardlimit &&
7780                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7781                                 fprintf(stderr,
7782                                         "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7783                                         progname,
7784                                         (unsigned long long)dqb->dqb_ihardlimit,
7785                                         progname);
7786                         break;
7787                 case LFS_POOL_OPT:
7788                         if (lfs_verify_poolarg(optarg)) {
7789                                 rc = -1;
7790                                 goto out;
7791                         }
7792                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7793                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7794                                                 LUSTRE_Q_SETDEFAULT_POOL :
7795                                                 LUSTRE_Q_SETQUOTAPOOL;
7796                         break;
7797                 default:
7798                         fprintf(stderr,
7799                                 "%s setquota: unrecognized option '%s'\n",
7800                                 progname, argv[optind - 1]);
7801                         /* fallthrough */
7802                 case 'h':
7803                         rc = CMD_HELP;
7804                         goto out;
7805                 }
7806         }
7807
7808         if (qctl->qc_type == ALLQUOTA) {
7809                 fprintf(stderr,
7810                         "%s setquota: either -u or -g must be specified\n",
7811                         progname);
7812                 rc = CMD_HELP;
7813                 goto out;
7814         }
7815
7816         if (!use_default && limit_mask == 0) {
7817                 fprintf(stderr,
7818                         "%s setquota: at least one limit must be specified\n",
7819                         progname);
7820                 rc = CMD_HELP;
7821                 goto out;
7822         }
7823
7824         if (use_default && limit_mask != 0) {
7825                 fprintf(stderr,
7826                         "%s setquota: limits should not be specified when using default quota\n",
7827                         progname);
7828                 rc = CMD_HELP;
7829                 goto out;
7830         }
7831
7832         if (use_default && qctl->qc_id == 0) {
7833                 fprintf(stderr,
7834                         "%s setquota: can not set default quota for root user/group/project\n",
7835                         progname);
7836                 rc = CMD_HELP;
7837                 goto out;
7838         }
7839
7840         if (optind != argc - 1) {
7841                 fprintf(stderr,
7842                         "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7843                         progname, argv[optind]);
7844                 rc = CMD_HELP;
7845                 goto out;
7846         }
7847
7848         mnt = argv[optind];
7849
7850         if (use_default) {
7851                 dqb->dqb_bhardlimit = 0;
7852                 dqb->dqb_bsoftlimit = 0;
7853                 dqb->dqb_ihardlimit = 0;
7854                 dqb->dqb_isoftlimit = 0;
7855                 dqb->dqb_itime = 0;
7856                 dqb->dqb_btime = 0;
7857                 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7858                 /* do not set inode limits for Pool Quotas */
7859                 if (qctl->qc_cmd  == LUSTRE_Q_SETDEFAULT_POOL)
7860                         dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7861         } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7862                    (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7863                 /* sigh, we can't just set blimits/ilimits */
7864                 struct if_quotactl tmp_qctl = {.qc_cmd  = LUSTRE_Q_GETQUOTA,
7865                                                .qc_type = qctl->qc_type,
7866                                                .qc_id   = qctl->qc_id};
7867
7868                 rc = llapi_quotactl(mnt, &tmp_qctl);
7869                 if (rc < 0)
7870                         goto out;
7871
7872                 if (!(limit_mask & BHLIMIT))
7873                         dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7874                 if (!(limit_mask & BSLIMIT))
7875                         dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7876                 if (!(limit_mask & IHLIMIT))
7877                         dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7878                 if (!(limit_mask & ISLIMIT))
7879                         dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7880
7881                 /* Keep grace times if we have got no softlimit arguments */
7882                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7883                         dqb->dqb_valid |= QIF_BTIME;
7884                         dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7885                 }
7886
7887                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7888                         dqb->dqb_valid |= QIF_ITIME;
7889                         dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7890                 }
7891         }
7892
7893         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7894         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7895
7896         rc = llapi_quotactl(mnt, qctl);
7897         if (rc) {
7898                 if (*obd_type)
7899                         fprintf(stderr,
7900                                 "%s setquota: cannot quotactl '%s' '%s': %s\n",
7901                                 progname, obd_type,
7902                                 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7903                 else
7904                         fprintf(stderr,
7905                                 "%s setquota: quotactl failed: %s\n",
7906                                 progname, strerror(-rc));
7907         }
7908 out:
7909         if (rc)
7910                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7911
7912         free(qctl);
7913         return rc;
7914 }
7915
7916 /* Converts seconds value into format string
7917  * result is returned in buf
7918  * Notes:
7919  *        1. result is in descenting order: 1w2d3h4m5s
7920  *        2. zero fields are not filled (except for p. 3): 5d1s
7921  *        3. zero seconds value is presented as "0s"
7922  */
7923 static char *__sec2str(time_t seconds, char *buf)
7924 {
7925         const char spec[] = "smhdw";
7926         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7927         unsigned long c;
7928         char *tail = buf;
7929         int i;
7930
7931         for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7932                 c = seconds / mult[i];
7933
7934                 if (c > 0 || (i == 0 && buf == tail))
7935                         tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7936                                           spec[i]);
7937
7938                 seconds %= mult[i];
7939         }
7940
7941         return tail;
7942 }
7943
7944 static void sec2str(time_t seconds, char *buf, int rc)
7945 {
7946         char *tail = buf;
7947
7948         if (rc)
7949                 *tail++ = '[';
7950
7951         tail = __sec2str(seconds, tail);
7952
7953         if (rc && tail - buf < 39) {
7954                 *tail++ = ']';
7955                 *tail++ = 0;
7956         }
7957 }
7958
7959 static void diff2str(time_t seconds, char *buf, time_t now)
7960 {
7961         buf[0] = 0;
7962         if (!seconds)
7963                 return;
7964         if (seconds <= now) {
7965                 strcpy(buf, "none");
7966                 return;
7967         }
7968         __sec2str(seconds - now, buf);
7969 }
7970
7971 static void print_quota_title(char *name, struct if_quotactl *qctl,
7972                               bool human_readable, bool show_default)
7973 {
7974         if (show_default) {
7975                 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7976                 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7977                        "Filesystem", "bquota", "blimit", "bgrace",
7978                        "iquota", "ilimit", "igrace");
7979         } else {
7980                 printf("Disk quotas for %s %s (%cid %u):\n",
7981                        qtype_name(qctl->qc_type), name,
7982                        *qtype_name(qctl->qc_type), qctl->qc_id);
7983                 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7984                        "Filesystem", human_readable ? "used" : "kbytes",
7985                        "quota", "limit", "grace",
7986                        "files", "quota", "limit", "grace");
7987         }
7988 }
7989
7990 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7991 {
7992         if (!h) {
7993                 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7994         } else {
7995                 if (num >> 40)
7996                         snprintf(buf, buflen, "%5.4gP",
7997                                  (double)num / ((__u64)1 << 40));
7998                 else if (num >> 30)
7999                         snprintf(buf, buflen, "%5.4gT",
8000                                  (double)num / (1 << 30));
8001                 else if (num >> 20)
8002                         snprintf(buf, buflen, "%5.4gG",
8003                                  (double)num / (1 << 20));
8004                 else if (num >> 10)
8005                         snprintf(buf, buflen, "%5.4gM",
8006                                  (double)num / (1 << 10));
8007                 else
8008                         snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8009         }
8010 }
8011
8012 #ifdef HAVE_NATIVE_CLIENT
8013 /* In the current Lustre implementation, the grace time is either the time
8014  * or the timestamp to be used after some quota ID exceeds the soft limt,
8015  * 48 bits should be enough, its high 16 bits can be used as quota flags.
8016  */
8017 #define LQUOTA_GRACE_BITS       48
8018 #define LQUOTA_GRACE_MASK       ((1ULL << LQUOTA_GRACE_BITS) - 1)
8019 #define LQUOTA_GRACE_MAX        LQUOTA_GRACE_MASK
8020 #define LQUOTA_GRACE(t)         (t & LQUOTA_GRACE_MASK)
8021 #define LQUOTA_FLAG(t)          (t >> LQUOTA_GRACE_BITS)
8022 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8023 #endif
8024
8025 #define STRBUF_LEN      24
8026 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8027                         int rc, bool h, bool show_default)
8028 {
8029         time_t now;
8030
8031         time(&now);
8032
8033         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8034             qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8035             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8036             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8037                 int bover = 0, iover = 0;
8038                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8039                 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8040                 char timebuf[40];
8041                 char strbuf[STRBUF_LEN];
8042
8043                 if (dqb->dqb_bhardlimit &&
8044                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8045                         bover = 1;
8046                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8047                         if (dqb->dqb_btime > now)
8048                                 bover = 2;
8049                         else
8050                                 bover = 3;
8051                 }
8052
8053                 if (dqb->dqb_ihardlimit &&
8054                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8055                         iover = 1;
8056                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8057                         if (dqb->dqb_itime > now)
8058                                 iover = 2;
8059                         else
8060                                 iover = 3;
8061                 }
8062
8063                 if (strlen(mnt) > 15)
8064                         printf("%s\n%15s", mnt, "");
8065                 else
8066                         printf("%15s", mnt);
8067
8068                 if (bover)
8069                         diff2str(dqb->dqb_btime, timebuf, now);
8070                 else if (show_default)
8071                         snprintf(timebuf, sizeof(timebuf), "%llu",
8072                                  (unsigned long long)dqb->dqb_btime);
8073
8074                 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8075                            strbuf, sizeof(strbuf), h);
8076                 if (rc == -EREMOTEIO)
8077                         sprintf(numbuf[0], "%s*", strbuf);
8078                 else
8079                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8080                                 "%s" : "[%s]", strbuf);
8081
8082                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8083                 if (type == QC_GENERAL)
8084                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8085                                 "%s" : "[%s]", strbuf);
8086                 else
8087                         sprintf(numbuf[1], "%s", "-");
8088
8089                 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8090                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8091                         "%s" : "[%s]", strbuf);
8092
8093                 if (show_default)
8094                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8095                 else
8096                         printf(" %7s%c %6s %7s %7s",
8097                                numbuf[0], bover ? '*' : ' ', numbuf[1],
8098                                numbuf[2], bover > 1 ? timebuf : "-");
8099
8100                 if (iover)
8101                         diff2str(dqb->dqb_itime, timebuf, now);
8102                 else if (show_default)
8103                         snprintf(timebuf, sizeof(timebuf), "%llu",
8104                                  (unsigned long long)dqb->dqb_itime);
8105
8106                 snprintf(numbuf[0], sizeof(numbuf),
8107                          (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8108                          (uintmax_t)dqb->dqb_curinodes);
8109
8110                 if (type == QC_GENERAL)
8111                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8112                                 "%ju" : "[%ju]",
8113                                 (uintmax_t)dqb->dqb_isoftlimit);
8114                 else
8115                         sprintf(numbuf[1], "%s", "-");
8116
8117                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8118                         "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8119
8120                 if (show_default)
8121                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8122                 else if (type != QC_OSTIDX)
8123                         printf(" %7s%c %6s %7s %7s",
8124                                numbuf[0], iover ? '*' : ' ', numbuf[1],
8125                                numbuf[2], iover > 1 ? timebuf : "-");
8126                 else
8127                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8128                 printf("\n");
8129         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8130                    qctl->qc_cmd == Q_GETOINFO) {
8131                 char bgtimebuf[40];
8132                 char igtimebuf[40];
8133
8134                 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8135                         strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8136                 else
8137                         sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8138                 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8139                         strncpy(igtimebuf, NOTIFY_GRACE, 40);
8140                 else
8141                         sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8142
8143                 printf("Block grace time: %s; Inode grace time: %s\n",
8144                        bgtimebuf, igtimebuf);
8145         }
8146 }
8147
8148 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8149 {
8150         char *dash, *endp;
8151
8152         /* format is "lustre-OST0001" */
8153         dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8154         if (!dash) {
8155                 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8156                 return -EINVAL;
8157         }
8158         dash += 4;
8159
8160         *idx = strtoul(dash, &endp, 16);
8161         if (*idx > 0xffff) {
8162                 fprintf(stderr, "wrong index %s\n", tgtname);
8163                 return -ERANGE;
8164         }
8165
8166         return 0;
8167 }
8168
8169 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8170                            bool h, __u64 *total)
8171 {
8172         int rc = 0, rc1 = 0, count = 0, i = 0;
8173         char **list = NULL, *buffer = NULL;
8174         __u32 valid = qctl->qc_valid;
8175
8176         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8177                 return 0;
8178
8179         /* Is it correct for the case OST0000, OST0002, OST0003 -
8180          * we will ask OST0001 that is absent and won't ask OST0003? */
8181         rc = llapi_get_obd_count(mnt, &count, is_mdt);
8182         if (rc) {
8183                 fprintf(stderr, "can not get %s count: %s\n",
8184                         is_mdt ? "mdt" : "ost", strerror(-rc));
8185                 return rc;
8186         }
8187
8188         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8189                 char fname[PATH_MAX];
8190                 char fsname[LUSTRE_MAXFSNAME + 1];
8191                 int bufsize = sizeof(struct obd_uuid) * count;
8192
8193                 rc = llapi_search_fsname(mnt, fsname);
8194                 if (rc) {
8195                         fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8196                                 mnt);
8197                         goto out;
8198                 }
8199                 buffer = malloc(bufsize + sizeof(*list) * count);
8200                 if (!buffer)
8201                         return -ENOMEM;
8202                 list = (char **)(buffer + bufsize);
8203                 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8204                 count = llapi_get_poolmembers(fname, list, count,
8205                                               buffer, bufsize);
8206                 if (count <= 0)
8207                         goto out;
8208         }
8209
8210         for (i = 0; i < count; i++) {
8211                 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8212                         unsigned int index;
8213
8214                         if (tgt_name2index(list[i], &index))
8215                                 continue;
8216                         qctl->qc_idx = index;
8217                 } else {
8218                         qctl->qc_idx = i;
8219                 }
8220
8221                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8222                 rc = llapi_quotactl(mnt, qctl);
8223                 if (rc) {
8224                         /* It is remote client case. */
8225                         if (rc == -EOPNOTSUPP) {
8226                                 rc = 0;
8227                                 goto out;
8228                         }
8229
8230                         if (!rc1)
8231                                 rc1 = rc;
8232                         fprintf(stderr, "quotactl %s%d failed.\n",
8233                                 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8234                         continue;
8235                 }
8236
8237                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8238                             qctl->qc_valid, 0, h, false);
8239                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8240                                    qctl->qc_dqblk.dqb_bhardlimit;
8241         }
8242 out:
8243         if (buffer)
8244                 free(buffer);
8245         qctl->qc_valid = valid;
8246         return rc ? : rc1;
8247 }
8248
8249 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8250                            int verbose, int quiet, bool human_readable,
8251                            bool show_default)
8252 {
8253         int rc1 = 0, rc2 = 0, rc3 = 0;
8254         char *obd_type = (char *)qctl->obd_type;
8255         char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8256         __u64 total_ialloc = 0, total_balloc = 0;
8257         bool use_default_for_blk = false;
8258         bool use_default_for_file = false;
8259         int inacc;
8260
8261         rc1 = llapi_quotactl(mnt, qctl);
8262         if (rc1 < 0) {
8263                 switch (rc1) {
8264                 case -ESRCH:
8265                         fprintf(stderr, "%s quotas are not enabled.\n",
8266                                 qtype_name(qctl->qc_type));
8267                         goto out;
8268                 case -EPERM:
8269                         fprintf(stderr, "Permission denied.\n");
8270                 case -ENODEV:
8271                 case -ENOENT:
8272                         /* We already got error message. */
8273                         goto out;
8274                 default:
8275                         fprintf(stderr, "Unexpected quotactl error: %s\n",
8276                                 strerror(-rc1));
8277                 }
8278         }
8279
8280         if (!show_default && qctl->qc_id == 0) {
8281                 qctl->qc_dqblk.dqb_bhardlimit = 0;
8282                 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8283                 qctl->qc_dqblk.dqb_ihardlimit = 0;
8284                 qctl->qc_dqblk.dqb_isoftlimit = 0;
8285                 qctl->qc_dqblk.dqb_btime = 0;
8286                 qctl->qc_dqblk.dqb_itime = 0;
8287                 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8288         }
8289
8290         if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8291             LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8292                 use_default_for_blk = true;
8293                 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8294         }
8295
8296         if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8297             LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8298                 use_default_for_file = true;
8299                 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8300         }
8301
8302         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8303              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8304              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8305              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8306                 print_quota_title(name, qctl, human_readable, show_default);
8307
8308         if (rc1 && *obd_type)
8309                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8310
8311         if (qctl->qc_valid != QC_GENERAL)
8312                 mnt = "";
8313
8314         inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8315                  qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8316                 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8317                  (QIF_LIMITS|QIF_USAGE));
8318
8319         print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8320
8321         if (!show_default && verbose &&
8322             qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8323             qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8324                 char strbuf[STRBUF_LEN];
8325
8326                 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8327                                       &total_ialloc);
8328                 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8329                                       &total_balloc);
8330                 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8331                            human_readable);
8332                 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8333                        (uintmax_t)total_ialloc, strbuf);
8334         }
8335
8336         if (use_default_for_blk)
8337                 printf("%cid %u is using default block quota setting\n",
8338                        *qtype_name(qctl->qc_type), qctl->qc_id);
8339
8340         if (use_default_for_file)
8341                 printf("%cid %u is using default file quota setting\n",
8342                        *qtype_name(qctl->qc_type), qctl->qc_id);
8343
8344         if (rc1 || rc2 || rc3 || inacc)
8345                 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8346 out:
8347         if (rc1)
8348                 return rc1;
8349         if (rc2)
8350                 return rc2;
8351         if (rc3)
8352                 return rc3;
8353         if (inacc)
8354                 return -EIO;
8355
8356         return 0;
8357 }
8358
8359 static int lfs_project(int argc, char **argv)
8360 {
8361         int ret = 0, err = 0, c, i;
8362         struct project_handle_control phc = { 0 };
8363         enum lfs_project_ops_t op;
8364
8365         phc.newline = true;
8366         phc.assign_projid = false;
8367         /* default action */
8368         op = LFS_PROJECT_LIST;
8369
8370         while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8371                 switch (c) {
8372                 case 'c':
8373                         if (op != LFS_PROJECT_LIST) {
8374                                 fprintf(stderr,
8375                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8376                                         progname);
8377                                 return CMD_HELP;
8378                         }
8379
8380                         op = LFS_PROJECT_CHECK;
8381                         break;
8382                 case 'C':
8383                         if (op != LFS_PROJECT_LIST) {
8384                                 fprintf(stderr,
8385                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8386                                         progname);
8387                                 return CMD_HELP;
8388                         }
8389
8390                         op = LFS_PROJECT_CLEAR;
8391                         break;
8392                 case 's':
8393                         if (op != LFS_PROJECT_LIST) {
8394                                 fprintf(stderr,
8395                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8396                                         progname);
8397                                 return CMD_HELP;
8398                         }
8399
8400                         phc.set_inherit = true;
8401                         op = LFS_PROJECT_SET;
8402                         break;
8403                 case 'd':
8404                         phc.dironly = true;
8405                         break;
8406                 case 'k':
8407                         phc.keep_projid = true;
8408                         break;
8409                 case 'r':
8410                         phc.recursive = true;
8411                         break;
8412                 case 'p':
8413                         if (str2quotaid(&phc.projid, optarg)) {
8414                                 fprintf(stderr,
8415                                         "Invalid project ID: %s\n",
8416                                         optarg);
8417                                 return CMD_HELP;
8418                         }
8419
8420                         phc.assign_projid = true;
8421
8422                         break;
8423                 case '0':
8424                         phc.newline = false;
8425                         break;
8426                 default:
8427                         fprintf(stderr, "%s: invalid option '%c'\n",
8428                                 progname, optopt);
8429                         return CMD_HELP;
8430                 }
8431         }
8432
8433         if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8434                 op = LFS_PROJECT_SET;
8435                 phc.set_projid = true;
8436         } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8437                 phc.set_projid = true;
8438         }
8439
8440         switch (op) {
8441         case LFS_PROJECT_CHECK:
8442                 if (phc.keep_projid) {
8443                         fprintf(stderr,
8444                                 "%s: '-k' is useless together with '-c'\n",
8445                                 progname);
8446                         return CMD_HELP;
8447                 }
8448                 break;
8449         case LFS_PROJECT_CLEAR:
8450                 if (!phc.newline) {
8451                         fprintf(stderr,
8452                                 "%s: '-0' is useless together with '-C'\n",
8453                                 progname);
8454                         return CMD_HELP;
8455                 }
8456                 if (phc.assign_projid) {
8457                         fprintf(stderr,
8458                                 "%s: '-p' is useless together with '-C'\n",
8459                                 progname);
8460                         return CMD_HELP;
8461                 }
8462                 break;
8463         case LFS_PROJECT_SET:
8464                 if (!phc.newline) {
8465                         fprintf(stderr,
8466                                 "%s: '-0' is useless together with '-s'\n",
8467                                 progname);
8468                         return CMD_HELP;
8469                 }
8470                 if (phc.keep_projid) {
8471                         fprintf(stderr,
8472                                 "%s: '-k' is useless together with '-s'\n",
8473                                 progname);
8474                         return CMD_HELP;
8475                 }
8476                 break;
8477         default:
8478                 if (!phc.newline) {
8479                         fprintf(stderr,
8480                                 "%s: '-0' is useless for list operations\n",
8481                                 progname);
8482                         return CMD_HELP;
8483                 }
8484                 break;
8485         }
8486
8487         argv += optind;
8488         argc -= optind;
8489         if (argc == 0) {
8490                 fprintf(stderr, "%s: missing file or directory target(s)\n",
8491                         progname);
8492                 return CMD_HELP;
8493         }
8494
8495         for (i = 0; i < argc; i++) {
8496                 switch (op) {
8497                 case LFS_PROJECT_CHECK:
8498                         err = lfs_project_check(argv[i], &phc);
8499                         break;
8500                 case LFS_PROJECT_LIST:
8501                         err = lfs_project_list(argv[i], &phc);
8502                         break;
8503                 case LFS_PROJECT_CLEAR:
8504                         err = lfs_project_clear(argv[i], &phc);
8505                         break;
8506                 case LFS_PROJECT_SET:
8507                         err = lfs_project_set(argv[i], &phc);
8508                         break;
8509                 default:
8510                         break;
8511                 }
8512                 if (err && !ret)
8513                         ret = err;
8514         }
8515
8516         return ret;
8517 }
8518
8519 static int lfs_quota(int argc, char **argv)
8520 {
8521         int c;
8522         char *mnt, *name = NULL;
8523         struct if_quotactl *qctl;
8524         char *obd_uuid;
8525         int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8526         __u32 valid = QC_GENERAL, idx = 0;
8527         bool human_readable = false;
8528         bool show_default = false;
8529         int qtype;
8530         bool show_pools = false;
8531         struct option long_opts[] = {
8532         { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8533         { .name = NULL } };
8534         char **poollist = NULL;
8535         char *buf = NULL;
8536         int poolcount, i;
8537
8538         qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8539         if (!qctl)
8540                 return -ENOMEM;
8541
8542         qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8543         qctl->qc_type = ALLQUOTA;
8544         obd_uuid = (char *)qctl->obd_uuid.uuid;
8545
8546         while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8547                 long_opts, NULL)) != -1) {
8548                 switch (c) {
8549                 case 'U':
8550                         show_default = true;
8551                 case 'u':
8552                         qtype = USRQUOTA;
8553                         goto quota_type;
8554                 case 'G':
8555                         show_default = true;
8556                 case 'g':
8557                         qtype = GRPQUOTA;
8558                         goto quota_type;
8559                 case 'P':
8560                         show_default = true;
8561                 case 'p':
8562                         qtype = PRJQUOTA;
8563 quota_type:
8564                         if (qctl->qc_type != ALLQUOTA) {
8565                                 fprintf(stderr,
8566                                         "%s quota: only one of -u, -g, or -p may be specified\n",
8567                                         progname);
8568                                 rc = CMD_HELP;
8569                                 goto out;
8570                         }
8571                         qctl->qc_type = qtype;
8572                         break;
8573                 case 't':
8574                         qctl->qc_cmd = LUSTRE_Q_GETINFO;
8575                         break;
8576                 case 'o':
8577                         valid = qctl->qc_valid = QC_UUID;
8578                         snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8579                         break;
8580                 case 'i':
8581                         valid = qctl->qc_valid = QC_MDTIDX;
8582                         idx = qctl->qc_idx = atoi(optarg);
8583                         if (idx == 0 && *optarg != '0') {
8584                                 fprintf(stderr,
8585                                         "%s quota: invalid MDT index '%s'\n",
8586                                         progname, optarg);
8587                                 rc = CMD_HELP;
8588                                 goto out;
8589                         }
8590                         break;
8591                 case 'I':
8592                         valid = qctl->qc_valid = QC_OSTIDX;
8593                         idx = qctl->qc_idx = atoi(optarg);
8594                         if (idx == 0 && *optarg != '0') {
8595                                 fprintf(stderr,
8596                                         "%s quota: invalid OST index '%s'\n",
8597                                         progname, optarg);
8598                                 rc = CMD_HELP;
8599                                 goto out;
8600                         }
8601                         break;
8602                 case 'v':
8603                         verbose = 1;
8604                         break;
8605                 case 'q':
8606                         quiet = 1;
8607                         break;
8608                 case 'h':
8609                         human_readable = true;
8610                         break;
8611                 case LFS_POOL_OPT:
8612                         if ((!optarg) && (argv[optind] != NULL) &&
8613                                 (argv[optind][0] != '-') &&
8614                                 (argv[optind][0] != '/')) {
8615                                 optarg = argv[optind++];
8616                                 if (lfs_verify_poolarg(optarg)) {
8617                                         rc = -EINVAL;
8618                                         goto out;
8619                                 }
8620                                 strncpy(qctl->qc_poolname, optarg,
8621                                         LOV_MAXPOOLNAME);
8622                                 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8623                                         qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8624                                 else
8625                                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8626                                 break;
8627                         }
8628
8629                         /* optarg is NULL */
8630                         show_pools = true;
8631                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8632                         break;
8633                 default:
8634                         fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8635                                 progname, argv[optind - 1]);
8636                         rc = CMD_HELP;
8637                         goto out;
8638                 }
8639         }
8640
8641         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8642         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8643              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8644              qctl->qc_type == ALLQUOTA &&
8645              optind == argc - 1 && !show_default) {
8646                 qctl->qc_idx = idx;
8647
8648                 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8649                         qctl->qc_type = qtype;
8650                         qctl->qc_valid = valid;
8651                         if (qtype == USRQUOTA) {
8652                                 qctl->qc_id = geteuid();
8653                                 rc = uid2name(&name, qctl->qc_id);
8654                         } else {
8655                                 qctl->qc_id = getegid();
8656                                 rc = gid2name(&name, qctl->qc_id);
8657                                 memset(&qctl->qc_dqblk, 0,
8658                                        sizeof(qctl->qc_dqblk));
8659                         }
8660                         if (rc)
8661                                 name = "<unknown>";
8662                         mnt = argv[optind];
8663                         rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8664                                               human_readable, show_default);
8665                         if (rc1 && !rc)
8666                                 rc = rc1;
8667                 }
8668                 goto out;
8669         /* lfs quota -u username /path/to/lustre/mount */
8670         } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8671                    qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8672                 /* options should be followed by u/g-name and mntpoint */
8673                 if ((!show_default && optind + 2 != argc) ||
8674                     (show_default && optind + 1 != argc) ||
8675                     qctl->qc_type == ALLQUOTA) {
8676                         fprintf(stderr,
8677                                 "%s quota: name and mount point must be specified\n",
8678                                 progname);
8679                         rc = CMD_HELP;
8680                         goto out;
8681                 }
8682
8683                 if (!show_default) {
8684                         name = argv[optind++];
8685                         switch (qctl->qc_type) {
8686                         case USRQUOTA:
8687                                 rc = name2uid(&qctl->qc_id, name);
8688                                 break;
8689                         case GRPQUOTA:
8690                                 rc = name2gid(&qctl->qc_id, name);
8691                                 break;
8692                         case PRJQUOTA:
8693                                 rc = name2projid(&qctl->qc_id, name);
8694                                 break;
8695                         default:
8696                                 rc = -ENOTSUP;
8697                                 break;
8698                         }
8699                 } else {
8700                         qctl->qc_valid = QC_GENERAL;
8701                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8702                                         LUSTRE_Q_GETDEFAULT_POOL :
8703                                         LUSTRE_Q_GETDEFAULT;
8704                         qctl->qc_id = 0;
8705                 }
8706
8707                 if (rc) {
8708                         if (str2quotaid(&qctl->qc_id, name)) {
8709                                 fprintf(stderr, "%s quota: invalid id '%s'\n",
8710                                         progname, name);
8711                                 rc = CMD_HELP;
8712                                 goto out;
8713                         }
8714                 }
8715         } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8716                 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8717                         progname);
8718                 rc = CMD_HELP;
8719                 goto out;
8720         }
8721
8722         mnt = argv[optind];
8723         if (show_pools) {
8724                 char *p;
8725
8726                 i = 0;
8727                 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8728                 if (rc)
8729                         goto out;
8730
8731                 for (i = 0; i < poolcount; i++) {
8732                         p = memchr(poollist[i], '.', MAXNAMLEN);
8733                         if (!p) {
8734                                 fprintf(stderr, "bad string format %.*s\n",
8735                                         MAXNAMLEN, poollist[i]);
8736                                 rc = -EINVAL;
8737                                 goto out;
8738                         }
8739                         p++;
8740                         printf("Quotas for pool: %s\n", p);
8741                         strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8742                         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8743                                              human_readable, show_default);
8744                         if (rc)
8745                                 break;
8746                 }
8747                 goto out;
8748         }
8749
8750         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8751                              human_readable, show_default);
8752 out:
8753         free(buf);
8754         free(qctl);
8755         return rc;
8756 }
8757 #endif /* HAVE_SYS_QUOTA_H! */
8758
8759 static int flushctx_ioctl(char *mp)
8760 {
8761         int fd, rc;
8762
8763         fd = open(mp, O_RDONLY);
8764         if (fd == -1) {
8765                 fprintf(stderr, "flushctx: error open %s: %s\n",
8766                         mp, strerror(errno));
8767                 return -1;
8768         }
8769
8770         rc = ioctl(fd, LL_IOC_FLUSHCTX);
8771         if (rc == -1)
8772                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8773                         mp, strerror(errno));
8774
8775         close(fd);
8776         return rc;
8777 }
8778
8779 static int lfs_flushctx(int argc, char **argv)
8780 {
8781         int     kdestroy = 0, reap = 0, c;
8782         char    mntdir[PATH_MAX] = {'\0'};
8783         int     index = 0;
8784         int     rc = 0;
8785
8786         while ((c = getopt(argc, argv, "kr")) != -1) {
8787                 switch (c) {
8788                 case 'k':
8789                         kdestroy = 1;
8790                         break;
8791                 case 'r':
8792                         reap = 1;
8793                         break;
8794                 default:
8795                         fprintf(stderr,
8796                                 "error: %s: option '-%c' unrecognized\n",
8797                                 argv[0], c);
8798                         return CMD_HELP;
8799                 }
8800         }
8801
8802         if (kdestroy) {
8803                 rc = system("kdestroy > /dev/null");
8804                 if (rc) {
8805                         rc = WEXITSTATUS(rc);
8806                         fprintf(stderr,
8807                                 "error destroying tickets: %d, continuing\n",
8808                                 rc);
8809                 }
8810         }
8811
8812         if (optind >= argc) {
8813                 /* flush for all mounted lustre fs. */
8814                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8815                         /* Check if we have a mount point */
8816                         if (mntdir[0] == '\0')
8817                                 continue;
8818
8819                         if (flushctx_ioctl(mntdir))
8820                                 rc = -1;
8821
8822                         mntdir[0] = '\0'; /* avoid matching in next loop */
8823                 }
8824         } else {
8825                 /* flush fs as specified */
8826                 while (optind < argc) {
8827                         if (flushctx_ioctl(argv[optind++]))
8828                                 rc = -1;
8829                 }
8830         }
8831
8832         if (reap) {
8833                 rc = system("keyctl reap > /dev/null");
8834                 if (rc != 0) {
8835                         rc = WEXITSTATUS(rc);
8836                         fprintf(stderr, "error reaping keyring: %d\n", rc);
8837                 }
8838         }
8839
8840         return rc;
8841 }
8842
8843 static int lfs_changelog(int argc, char **argv)
8844 {
8845         void *changelog_priv;
8846         struct changelog_rec *rec;
8847         long long startrec = 0, endrec = 0;
8848         char *mdd;
8849         struct option long_opts[] = {
8850                 { .val = 'f', .name = "follow", .has_arg = no_argument },
8851                 { .name = NULL } };
8852         char short_opts[] = "f";
8853         int rc, follow = 0;
8854
8855         while ((rc = getopt_long(argc, argv, short_opts,
8856                 long_opts, NULL)) != -1) {
8857                 switch (rc) {
8858                 case 'f':
8859                         follow++;
8860                         break;
8861                 default:
8862                         fprintf(stderr,
8863                                 "%s changelog: unrecognized option '%s'\n",
8864                                 progname, argv[optind - 1]);
8865                         return CMD_HELP;
8866                 }
8867         }
8868         if (optind >= argc) {
8869                 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8870                         progname);
8871                 return CMD_HELP;
8872         }
8873
8874         mdd = argv[optind++];
8875         if (argc > optind) {
8876                 errno = 0;
8877                 startrec = strtoll(argv[optind++], NULL, 10);
8878                 if (errno != 0 || startrec < 0) {
8879                         fprintf(stderr,
8880                                 "%s changelog: bad startrec\n",
8881                                 progname);
8882                         return CMD_HELP;
8883                 }
8884         }
8885
8886         if (argc > optind) {
8887                 errno = 0;
8888                 endrec = strtoll(argv[optind++], NULL, 10);
8889                 if (errno != 0 || endrec < 0) {
8890                         fprintf(stderr,
8891                                 "%s changelog: bad endrec\n",
8892                                 progname);
8893                         return CMD_HELP;
8894                 }
8895         }
8896
8897         rc = llapi_changelog_start(&changelog_priv,
8898                                    CHANGELOG_FLAG_BLOCK |
8899                                    CHANGELOG_FLAG_JOBID |
8900                                    CHANGELOG_FLAG_EXTRA_FLAGS |
8901                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8902                                    mdd, startrec);
8903         if (rc < 0) {
8904                 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8905                         progname, strerror(errno = -rc));
8906                 return rc;
8907         }
8908
8909         rc = llapi_changelog_set_xflags(changelog_priv,
8910                                         CHANGELOG_EXTRA_FLAG_UIDGID |
8911                                         CHANGELOG_EXTRA_FLAG_NID |
8912                                         CHANGELOG_EXTRA_FLAG_OMODE |
8913                                         CHANGELOG_EXTRA_FLAG_XATTR);
8914         if (rc < 0) {
8915                 fprintf(stderr,
8916                         "%s changelog: cannot set xflags for changelog: %s\n",
8917                         progname, strerror(errno = -rc));
8918                 return rc;
8919         }
8920
8921         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8922                 time_t secs;
8923                 struct tm ts;
8924
8925                 if (endrec && rec->cr_index > endrec) {
8926                         llapi_changelog_free(&rec);
8927                         break;
8928                 }
8929                 if (rec->cr_index < startrec) {
8930                         llapi_changelog_free(&rec);
8931                         continue;
8932                 }
8933
8934                 secs = rec->cr_time >> 30;
8935                 gmtime_r(&secs, &ts);
8936                 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8937                        "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8938                        changelog_type2str(rec->cr_type),
8939                        ts.tm_hour, ts.tm_min, ts.tm_sec,
8940                        (int)(rec->cr_time & ((1 << 30) - 1)),
8941                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8942                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8943
8944                 if (rec->cr_flags & CLF_JOBID) {
8945                         struct changelog_ext_jobid *jid =
8946                                 changelog_rec_jobid(rec);
8947
8948                         if (jid->cr_jobid[0] != '\0')
8949                                 printf(" j=%s", jid->cr_jobid);
8950                 }
8951
8952                 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8953                         struct changelog_ext_extra_flags *ef =
8954                                 changelog_rec_extra_flags(rec);
8955
8956                         printf(" ef=0x%llx",
8957                                (unsigned long long)ef->cr_extra_flags);
8958
8959                         if (ef->cr_extra_flags & CLFE_UIDGID) {
8960                                 struct changelog_ext_uidgid *uidgid =
8961                                         changelog_rec_uidgid(rec);
8962
8963                                 printf(" u=%llu:%llu",
8964                                        (unsigned long long)uidgid->cr_uid,
8965                                        (unsigned long long)uidgid->cr_gid);
8966                         }
8967                         if (ef->cr_extra_flags & CLFE_NID) {
8968                                 struct changelog_ext_nid *nid =
8969                                         changelog_rec_nid(rec);
8970
8971                                 printf(" nid=%s",
8972                                        libcfs_nid2str(nid->cr_nid));
8973                         }
8974
8975                         if (ef->cr_extra_flags & CLFE_OPEN) {
8976                                 struct changelog_ext_openmode *omd =
8977                                         changelog_rec_openmode(rec);
8978                                 char mode[] = "---";
8979
8980                                 /* exec mode must be exclusive */
8981                                 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8982                                         mode[2] = 'x';
8983                                 } else {
8984                                         if (omd->cr_openflags & MDS_FMODE_READ)
8985                                                 mode[0] = 'r';
8986                                         if (omd->cr_openflags &
8987                                             (MDS_FMODE_WRITE |
8988                                              MDS_OPEN_TRUNC |
8989                                              MDS_OPEN_APPEND))
8990                                                 mode[1] = 'w';
8991                                 }
8992
8993                                 if (strcmp(mode, "---") != 0)
8994                                         printf(" m=%s", mode);
8995                         }
8996
8997                         if (ef->cr_extra_flags & CLFE_XATTR) {
8998                                 struct changelog_ext_xattr *xattr =
8999                                         changelog_rec_xattr(rec);
9000
9001                                 if (xattr->cr_xattr[0] != '\0')
9002                                         printf(" x=%s", xattr->cr_xattr);
9003                         }
9004                 }
9005
9006                 if (!fid_is_zero(&rec->cr_pfid))
9007                         printf(" p="DFID, PFID(&rec->cr_pfid));
9008                 if (rec->cr_namelen)
9009                         printf(" %.*s", rec->cr_namelen,
9010                                changelog_rec_name(rec));
9011
9012                 if (rec->cr_flags & CLF_RENAME) {
9013                         struct changelog_ext_rename *rnm =
9014                                 changelog_rec_rename(rec);
9015
9016                         if (!fid_is_zero(&rnm->cr_sfid))
9017                                 printf(" s="DFID" sp="DFID" %.*s",
9018                                        PFID(&rnm->cr_sfid),
9019                                        PFID(&rnm->cr_spfid),
9020                                        (int)changelog_rec_snamelen(rec),
9021                                        changelog_rec_sname(rec));
9022                 }
9023                 printf("\n");
9024
9025                 llapi_changelog_free(&rec);
9026         }
9027
9028         llapi_changelog_fini(&changelog_priv);
9029
9030         if (rc < 0)
9031                 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9032                         progname, strerror(errno = -rc));
9033
9034         return (rc == 1 ? 0 : rc);
9035 }
9036
9037 static int lfs_changelog_clear(int argc, char **argv)
9038 {
9039         long long endrec;
9040         int rc;
9041
9042         if (argc != 4)
9043                 return CMD_HELP;
9044
9045         errno = 0;
9046         endrec = strtoll(argv[3], NULL, 10);
9047         if (errno != 0 || endrec < 0) {
9048                 fprintf(stderr,
9049                         "%s: bad endrec '%s'\n",
9050                         argv[0], argv[3]);
9051                 return CMD_HELP;
9052         }
9053
9054         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9055
9056         if (rc == -EINVAL)
9057                 fprintf(stderr, "%s: record out of range: %llu\n",
9058                         argv[0], endrec);
9059         else if (rc == -ENOENT)
9060                 fprintf(stderr, "%s: no changelog user: %s\n",
9061                         argv[0], argv[2]);
9062         else if (rc)
9063                 fprintf(stderr, "%s error: %s\n", argv[0],
9064                         strerror(-rc));
9065
9066         if (rc)
9067                 errno = -rc;
9068
9069         return rc;
9070 }
9071
9072 static void rstripc(char *str, int c)
9073 {
9074         char *end = str + strlen(str);
9075
9076         for (; str < end && end[-1] == c; --end)
9077                 end[-1] = '\0';
9078 }
9079
9080 static int lfs_fid2path(int argc, char **argv)
9081 {
9082         struct option long_opts[] = {
9083                 { .val = 'c',   .name = "cur",  .has_arg = no_argument },
9084                 { .val = 'c',   .name = "current",      .has_arg = no_argument },
9085                 { .val = 'c',   .name = "print-link",   .has_arg = no_argument },
9086                 { .val = 'f',   .name = "print-fid",    .has_arg = no_argument },
9087                 { .val = 'l',   .name = "link", .has_arg = required_argument },
9088                 { .name = NULL } };
9089         char short_opts[] = "cfl:pr:";
9090         bool print_link = false;
9091         bool print_fid = false;
9092         bool print_mnt_dir;
9093         char mnt_dir[PATH_MAX] = "";
9094         int mnt_fd = -1;
9095         char *path_or_fsname;
9096         long long recno = -1;
9097         int linkno = -1;
9098         char *endptr = NULL;
9099         int rc = 0;
9100         int c;
9101         int i;
9102
9103         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9104                 switch (c) {
9105                 case 'c':
9106                         print_link = true;
9107                         break;
9108                 case 'f':
9109                         print_fid = true;
9110                         break;
9111                 case 'l':
9112                         errno = 0;
9113                         linkno = strtol(optarg, &endptr, 10);
9114                         if (errno != 0 || *endptr != '\0' || linkno < 0) {
9115                                 fprintf(stderr,
9116                                         "%s fid2path: invalid linkno '%s'\n",
9117                                         progname, optarg);
9118                                 return CMD_HELP;
9119                         }
9120                         break;
9121                 case 'r':
9122                         /* recno is something to do with changelogs
9123                          * that was never implemented. We just pass it
9124                          * through for the MDT to ignore.
9125                          */
9126                         errno = 0;
9127                         recno = strtoll(optarg, &endptr, 10);
9128                         if (errno != 0 || *endptr != '\0' || recno < 0) {
9129                                 fprintf(stderr,
9130                                         "%s fid2path: invalid recno '%s'\n",
9131                                         progname, optarg);
9132                                 return CMD_HELP;
9133                         }
9134                         break;
9135                 default:
9136                         fprintf(stderr,
9137                                 "%s fid2path: unrecognized option '%s'\n",
9138                                 progname, argv[optind - 1]);
9139                         return CMD_HELP;
9140                 }
9141         }
9142
9143         if (argc - optind < 2) {
9144                 fprintf(stderr,
9145                         "Usage: %s fid2path FSNAME|ROOT FID...\n",
9146                         progname);
9147                 return CMD_HELP;
9148         }
9149
9150         path_or_fsname = argv[optind];
9151
9152         if (*path_or_fsname == '/') {
9153                 print_mnt_dir = true;
9154                 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9155         } else {
9156                 print_mnt_dir = false;
9157                 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9158         }
9159
9160         if (rc < 0) {
9161                 fprintf(stderr,
9162                         "%s fid2path: cannot resolve mount point for '%s': %s\n",
9163                         progname, path_or_fsname, strerror(-rc));
9164                 goto out;
9165         }
9166
9167         mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9168         if (mnt_fd < 0) {
9169                 fprintf(stderr,
9170                         "%s fid2path: cannot open mount point for '%s': %s\n",
9171                         progname, path_or_fsname, strerror(-rc));
9172                 goto out;
9173         }
9174
9175         /* Strip trailing slashes from mnt_dir. */
9176         rstripc(mnt_dir + 1, '/');
9177
9178         for (i = optind + 1; i < argc; i++) {
9179                 const char *fid_str = argv[i];
9180                 struct lu_fid fid;
9181                 int rc2;
9182
9183                 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9184                 if (rc2 < 0) {
9185                         fprintf(stderr,
9186                                 "%s fid2path: invalid FID '%s'\n",
9187                                 progname, fid_str);
9188                         if (rc == 0)
9189                                 rc = rc2;
9190
9191                         continue;
9192                 }
9193
9194                 int linktmp = (linkno >= 0) ? linkno : 0;
9195                 while (1) {
9196                         int oldtmp = linktmp;
9197                         long long rectmp = recno;
9198                         char path_buf[PATH_MAX];
9199
9200                         rc2 = llapi_fid2path_at(mnt_fd, &fid,
9201                                 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9202                         if (rc2 < 0) {
9203                                 fprintf(stderr,
9204                                         "%s fid2path: cannot find %s %s: %s\n",
9205                                         progname, path_or_fsname, fid_str,
9206                                         strerror(-rc2));
9207                                 if (rc == 0)
9208                                         rc = rc2;
9209                                 break;
9210                         }
9211
9212                         if (print_fid)
9213                                 printf("%s ", fid_str);
9214
9215                         if (print_link)
9216                                 printf("%d ", linktmp);
9217
9218                         /* You may think this looks wrong or weird (and it is!)
9219                          * but we are actually trying to preserve the old quirky
9220                          * behaviors (enforced by our old quirky tests!) that
9221                          * make lfs so much fun to work on:
9222                          *
9223                          *   lustre 0x200000007:0x1:0x0 => "/"
9224                          *   /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9225                          *
9226                          * Note that llapi_fid2path() returns "" for the root
9227                          * FID. */
9228
9229                         printf("%s%s%s\n",
9230                                print_mnt_dir ? mnt_dir : "",
9231                                (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9232                                path_buf);
9233
9234                         if (linkno >= 0)
9235                                 /* specified linkno */
9236                                 break;
9237
9238                         if (oldtmp == linktmp)
9239                                 /* no more links */
9240                                 break;
9241                 }
9242         }
9243 out:
9244         if (!(mnt_fd < 0))
9245                 close(mnt_fd);
9246
9247         return rc;
9248 }
9249
9250 static int lfs_path2fid(int argc, char **argv)
9251 {
9252         struct option long_opts[] = {
9253                 { .val = 'p', .name = "parents", .has_arg = no_argument },
9254                 { .name = NULL } };
9255         char            **path;
9256         const char        short_opts[] = "p";
9257         const char       *sep = "";
9258         struct lu_fid     fid;
9259         int               rc = 0;
9260         bool              show_parents = false;
9261
9262         while ((rc = getopt_long(argc, argv, short_opts,
9263                                  long_opts, NULL)) != -1) {
9264                 switch (rc) {
9265                 case 'p':
9266                         show_parents = true;
9267                         break;
9268                 default:
9269                         fprintf(stderr,
9270                                 "%s path2fid: unrecognized option '%s'\n",
9271                                 progname, argv[optind - 1]);
9272                         return CMD_HELP;
9273                 }
9274         }
9275
9276         if (optind > argc - 1) {
9277                 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9278                         progname);
9279                 return CMD_HELP;
9280         } else if (optind < argc - 1) {
9281                 sep = ": ";
9282         }
9283
9284         rc = 0;
9285         for (path = argv + optind; *path != NULL; path++) {
9286                 int err = 0;
9287
9288                 if (!show_parents) {
9289                         err = llapi_path2fid(*path, &fid);
9290                         if (!err)
9291                                 printf("%s%s"DFID"\n",
9292                                        *sep != '\0' ? *path : "", sep,
9293                                        PFID(&fid));
9294                 } else {
9295                         char            name[NAME_MAX + 1];
9296                         unsigned int    linkno = 0;
9297
9298                         while ((err = llapi_path2parent(*path, linkno, &fid,
9299                                                 name, sizeof(name))) == 0) {
9300                                 if (*sep != '\0' && linkno == 0)
9301                                         printf("%s%s", *path, sep);
9302
9303                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9304                                        PFID(&fid), name);
9305                                 linkno++;
9306                         }
9307
9308                         /* err == -ENODATA is end-of-loop */
9309                         if (linkno > 0 && err == -ENODATA) {
9310                                 printf("\n");
9311                                 err = 0;
9312                         }
9313                 }
9314
9315                 if (err) {
9316                         fprintf(stderr,
9317                                 "%s path2fid: cannot get %sfid for '%s': %s\n",
9318                                 progname, show_parents ? "parent " : "", *path,
9319                                 strerror(-err));
9320                         if (rc == 0) {
9321                                 rc = err;
9322                                 errno = -err;
9323                         }
9324                 }
9325         }
9326
9327         return rc;
9328 }
9329
9330 #define MAX_ERRNO       4095
9331 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9332
9333 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9334 {
9335         int rc, rc2, k;
9336
9337         rc = llapi_rmfid(device, fa);
9338         if (rc < 0) {
9339                 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9340                         progname, strerror(-rc));
9341                 return rc;
9342         }
9343
9344         for (k = 0; k < fa->fa_nr; k++) {
9345                 rc2 = (__s32)fa->fa_fids[k].f_ver;
9346                 if (!IS_ERR_VALUE(rc2))
9347                         continue;
9348
9349                 if (rc == 0)
9350                         rc = rc2;
9351
9352                 fa->fa_fids[k].f_ver = 0;
9353                 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9354                         progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9355         }
9356
9357         return rc;
9358 }
9359
9360 static int lfs_rmfid(int argc, char **argv)
9361 {
9362         char *fidstr, *device;
9363         int rc = 0, rc2, nr;
9364         struct fid_array *fa;
9365
9366         if (optind > argc - 1) {
9367                 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9368                 return CMD_HELP;
9369         }
9370
9371         device = argv[optind++];
9372
9373         nr = argc - optind;
9374         fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9375         if (!fa)
9376                 return -ENOMEM;
9377
9378         fa->fa_nr = 0;
9379         rc = 0;
9380         while (optind < argc) {
9381                 int found;
9382
9383                 fidstr = argv[optind++];
9384                 while (*fidstr == '[')
9385                         fidstr++;
9386                 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9387                 if (found != 3) {
9388                         fprintf(stderr, "unrecognized FID: %s\n",
9389                                 argv[optind - 1]);
9390                         exit(1);
9391                 }
9392                 fa->fa_nr++;
9393                 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9394                         /* start another batch */
9395                         rc2 = lfs_rmfid_and_show_errors(device, fa);
9396                         if (rc2 && !rc)
9397                                 rc = rc2;
9398                         fa->fa_nr = 0;
9399                 }
9400         }
9401         if (fa->fa_nr) {
9402                 rc2 = lfs_rmfid_and_show_errors(device, fa);
9403                 if (rc2 && !rc)
9404                         rc = rc2;
9405         }
9406
9407         return rc;
9408 }
9409
9410 static int lfs_data_version(int argc, char **argv)
9411 {
9412         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9413         __u64 data_version;
9414         char *path;
9415         int fd;
9416         int rc;
9417         int c;
9418
9419         if (argc < 2) {
9420                 fprintf(stderr, "%s: FILE must be specified\n",
9421                         progname);
9422                 return CMD_HELP;
9423         }
9424
9425         while ((c = getopt(argc, argv, "hnrw")) != -1) {
9426                 switch (c) {
9427                 case 'n':
9428                         data_version_flags = 0;
9429                         break;
9430                 case 'r':
9431                         data_version_flags |= LL_DV_RD_FLUSH;
9432                         break;
9433                 case 'w':
9434                         data_version_flags |= LL_DV_WR_FLUSH;
9435                         break;
9436                 default:
9437                         fprintf(stderr,
9438                                 "%s data_version: unrecognized option '%s'\n",
9439                                 progname, argv[optind - 1]);
9440                         /* fallthrough */
9441                 case 'h':
9442                         return CMD_HELP;
9443                 }
9444         }
9445         if (optind == argc) {
9446                 fprintf(stderr, "%s data_version: FILE must be specified\n",
9447                         progname);
9448                 return CMD_HELP;
9449         }
9450
9451         path = argv[optind];
9452         fd = open(path, O_RDONLY);
9453         if (fd < 0) {
9454                 rc = -errno;
9455                 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9456                         progname, path, strerror(-rc));
9457                 return rc;
9458         }
9459
9460         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9461         if (rc < 0)
9462                 fprintf(stderr,
9463                         "%s data_version: cannot get version for '%s': %s\n",
9464                         progname, path, strerror(-rc));
9465         else
9466                 printf("%ju" "\n", (uintmax_t)data_version);
9467
9468         close(fd);
9469         return rc;
9470 }
9471
9472 static int lfs_hsm_state(int argc, char **argv)
9473 {
9474         int rc;
9475         int i = 1;
9476         char *path;
9477         struct hsm_user_state hus;
9478
9479         if (argc < 2)
9480                 return CMD_HELP;
9481
9482         do {
9483                 path = argv[i];
9484
9485                 rc = llapi_hsm_state_get(path, &hus);
9486                 if (rc) {
9487                         fprintf(stderr, "can't get hsm state for %s: %s\n",
9488                                 path, strerror(errno = -rc));
9489                         return rc;
9490                 }
9491
9492                 /* Display path name and status flags */
9493                 printf("%s: (0x%08x)", path, hus.hus_states);
9494
9495                 if (hus.hus_states & HS_RELEASED)
9496                         printf(" released");
9497                 if (hus.hus_states & HS_EXISTS)
9498                         printf(" exists");
9499                 if (hus.hus_states & HS_DIRTY)
9500                         printf(" dirty");
9501                 if (hus.hus_states & HS_ARCHIVED)
9502                         printf(" archived");
9503                 /* Display user-settable flags */
9504                 if (hus.hus_states & HS_NORELEASE)
9505                         printf(" never_release");
9506                 if (hus.hus_states & HS_NOARCHIVE)
9507                         printf(" never_archive");
9508                 if (hus.hus_states & HS_LOST)
9509                         printf(" lost_from_hsm");
9510
9511                 if (hus.hus_archive_id != 0)
9512                         printf(", archive_id:%d", hus.hus_archive_id);
9513                 printf("\n");
9514
9515         } while (++i < argc);
9516
9517         return 0;
9518 }
9519
9520 #define LFS_HSM_SET   0
9521 #define LFS_HSM_CLEAR 1
9522
9523 /**
9524  * Generic function to set or clear HSM flags.
9525  * Used by hsm_set and hsm_clear.
9526  *
9527  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9528  */
9529 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9530 {
9531         struct option long_opts[] = {
9532         { .val = 'A',   .name = "archived",     .has_arg = no_argument },
9533         { .val = 'a',   .name = "noarchive",    .has_arg = no_argument },
9534         { .val = 'd',   .name = "dirty",        .has_arg = no_argument },
9535         { .val = 'e',   .name = "exists",       .has_arg = no_argument },
9536         { .val = 'h',   .name = "help",         .has_arg = no_argument },
9537         { .val = 'i',   .name = "archive-id",   .has_arg = required_argument },
9538         { .val = 'l',   .name = "lost",         .has_arg = no_argument },
9539         { .val = 'r',   .name = "norelease",    .has_arg = no_argument },
9540         { .name = NULL } };
9541         __u64 mask = 0;
9542         int c, rc;
9543         char *path;
9544         __u32 archive_id = 0;
9545         char *end = NULL;
9546
9547         if (argc < 3)
9548                 return CMD_HELP;
9549
9550         while ((c = getopt_long(argc, argv, "aAdehi:lr",
9551                                 long_opts, NULL)) != -1) {
9552                 switch (c) {
9553                 case 'l':
9554                         mask |= HS_LOST;
9555                         break;
9556                 case 'a':
9557                         mask |= HS_NOARCHIVE;
9558                         break;
9559                 case 'A':
9560                         mask |= HS_ARCHIVED;
9561                         break;
9562                 case 'r':
9563                         mask |= HS_NORELEASE;
9564                         break;
9565                 case 'd':
9566                         mask |= HS_DIRTY;
9567                         break;
9568                 case 'e':
9569                         mask |= HS_EXISTS;
9570                         break;
9571                 case 'i':
9572                         errno = 0;
9573                         archive_id = strtol(optarg, &end, 10);
9574                         if (errno != 0 || *end != '\0' || archive_id < 0) {
9575                                 fprintf(stderr,
9576                                         "%s: invalid archive_id: '%s'\n",
9577                                         progname, end);
9578                                 return CMD_HELP;
9579                         }
9580                         break;
9581                 default:
9582                         fprintf(stderr, "%s: unrecognized option '%s'\n",
9583                                 progname, argv[optind - 1]);
9584                         /* fallthrough */
9585                 case 'h':
9586                         return CMD_HELP;
9587                 }
9588         }
9589
9590         /* User should have specified a flag */
9591         if (mask == 0)
9592                 return CMD_HELP;
9593
9594         while (optind < argc) {
9595                 path = argv[optind];
9596
9597                 /* If mode == 0, this means we apply the mask. */
9598                 if (mode == LFS_HSM_SET)
9599                         rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9600                 else
9601                         rc = llapi_hsm_state_set(path, 0, mask, 0);
9602
9603                 if (rc != 0) {
9604                         fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9605                                 path, strerror(errno = -rc));
9606                         return rc;
9607                 }
9608                 optind++;
9609         }
9610
9611         return 0;
9612 }
9613
9614 static int lfs_hsm_action(int argc, char **argv)
9615 {
9616         int                              rc;
9617         int                              i = 1;
9618         char                            *path;
9619         struct hsm_current_action        hca;
9620         struct hsm_extent                he;
9621         enum hsm_user_action             hua;
9622         enum hsm_progress_states         hps;
9623
9624         if (argc < 2)
9625                 return CMD_HELP;
9626
9627         do {
9628                 path = argv[i];
9629
9630                 rc = llapi_hsm_current_action(path, &hca);
9631                 if (rc) {
9632                         fprintf(stderr, "can't get hsm action for %s: %s\n",
9633                                 path, strerror(errno = -rc));
9634                         return rc;
9635                 }
9636                 he = hca.hca_location;
9637                 hua = hca.hca_action;
9638                 hps = hca.hca_state;
9639
9640                 printf("%s: %s", path, hsm_user_action2name(hua));
9641
9642                 /* Skip file without action */
9643                 if (hca.hca_action == HUA_NONE) {
9644                         printf("\n");
9645                         continue;
9646                 }
9647
9648                 printf(" %s ", hsm_progress_state2name(hps));
9649
9650                 if ((hps == HPS_RUNNING) &&
9651                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9652                         printf("(%llu bytes moved)\n",
9653                                (unsigned long long)he.length);
9654                 else if ((he.offset + he.length) == LUSTRE_EOF)
9655                         printf("(from %llu to EOF)\n",
9656                                (unsigned long long)he.offset);
9657                 else
9658                         printf("(from %llu to %llu)\n",
9659                                (unsigned long long)he.offset,
9660                                (unsigned long long)(he.offset + he.length));
9661
9662         } while (++i < argc);
9663
9664         return 0;
9665 }
9666
9667 static int lfs_hsm_set(int argc, char **argv)
9668 {
9669         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9670 }
9671
9672 static int lfs_hsm_clear(int argc, char **argv)
9673 {
9674         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9675 }
9676
9677 /**
9678  * Check file state and return its fid, to be used by lfs_hsm_request().
9679  *
9680  * \param[in]     file      Path to file to check
9681  * \param[in,out] fid       Pointer to allocated lu_fid struct.
9682  * \param[in,out] last_dev  Pointer to last device id used.
9683  *
9684  * \return 0 on success.
9685  */
9686 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9687                                 dev_t *last_dev)
9688 {
9689         struct stat     st;
9690         int             rc;
9691
9692         rc = lstat(file, &st);
9693         if (rc) {
9694                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9695                 return -errno;
9696         }
9697         /*
9698          * Checking for regular file as archiving as posix copytool
9699          * rejects archiving files other than regular files
9700          */
9701         if (!S_ISREG(st.st_mode)) {
9702                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9703                 return CMD_HELP;
9704         }
9705         /* A request should be ... */
9706         if (*last_dev != st.st_dev && *last_dev != 0) {
9707                 fprintf(stderr,
9708                         "All files should be on the same filesystem: %s\n",
9709                         file);
9710                 return -EINVAL;
9711         }
9712         *last_dev = st.st_dev;
9713
9714         rc = llapi_path2fid(file, fid);
9715         if (rc) {
9716                 fprintf(stderr, "Cannot read FID of %s: %s\n",
9717                         file, strerror(-rc));
9718                 return rc;
9719         }
9720         return 0;
9721 }
9722
9723 /* Fill an HSM HUR item with a given file name.
9724  *
9725  * If mntpath is set, then the filename is actually a FID, and no
9726  * lookup on the filesystem will be performed.
9727  *
9728  * \param[in]  hur         the user request to fill
9729  * \param[in]  idx         index of the item inside the HUR to fill
9730  * \param[in]  mntpath     mountpoint of Lustre
9731  * \param[in]  fname       filename (if mtnpath is NULL)
9732  *                         or FID (if mntpath is set)
9733  * \param[in]  last_dev    pointer to last device id used
9734  *
9735  * \retval 0 on success
9736  * \retval CMD_HELP or a negative errno on error
9737  */
9738 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9739                          const char *mntpath, const char *fname,
9740                          dev_t *last_dev)
9741 {
9742         struct hsm_user_item *hui = &hur->hur_user_item[idx];
9743         int rc;
9744
9745         hui->hui_extent.length = -1;
9746
9747         if (mntpath) {
9748                 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9749                 if (rc)
9750                         fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9751                                 fname);
9752         } else {
9753                 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9754         }
9755
9756         if (rc == 0)
9757                 hur->hur_request.hr_itemcount++;
9758
9759         return rc;
9760 }
9761
9762 static int lfs_hsm_request(int argc, char **argv, int action)
9763 {
9764         struct option long_opts[] = {
9765         { .val = 'a',   .name = "archive",      .has_arg = required_argument },
9766         { .val = 'D',   .name = "data",         .has_arg = required_argument },
9767         { .val = 'h',   .name = "help",         .has_arg = no_argument },
9768         { .val = 'l',   .name = "filelist",     .has_arg = required_argument },
9769         { .val = 'm',   .name = "mntpath",      .has_arg = required_argument },
9770         { .name = NULL } };
9771         dev_t last_dev = 0;
9772         struct hsm_user_request *hur, *oldhur;
9773         int c, i;
9774         size_t len;
9775         int nbfile;
9776         char *line = NULL;
9777         char *filelist = NULL;
9778         char fullpath[PATH_MAX];
9779         char *opaque = NULL;
9780         int opaque_len = 0;
9781         int archive_id = 0;
9782         FILE *fp;
9783         int nbfile_alloc = 0;
9784         char *some_file = NULL;
9785         char *mntpath = NULL;
9786         int rc;
9787
9788         if (argc < 2)
9789                 return CMD_HELP;
9790
9791         while ((c = getopt_long(argc, argv, "a:D:hl:m:",
9792                                 long_opts, NULL)) != -1) {
9793                 switch (c) {
9794                 case 'l':
9795                         filelist = optarg;
9796                         break;
9797                 case 'D':
9798                         opaque = optarg;
9799                         break;
9800                 case 'a':
9801                         if (action != HUA_ARCHIVE &&
9802                             action != HUA_REMOVE) {
9803                                 fprintf(stderr,
9804                                         "error: -a is supported only when archiving or removing\n");
9805                                 return CMD_HELP;
9806                         }
9807                         archive_id = atoi(optarg);
9808                         break;
9809                 case 'm':
9810                         if (!some_file) {
9811                                 mntpath = optarg;
9812                                 some_file = strdup(optarg);
9813                         }
9814                         break;
9815                 default:
9816                         fprintf(stderr, "%s: unrecognized option '%s'\n",
9817                                 progname, argv[optind - 1]);
9818                         /* fallthrough */
9819                 case 'h':
9820                         return CMD_HELP;
9821                 }
9822         }
9823
9824         /* All remaining args are files, so we have at least nbfile */
9825         nbfile = argc - optind;
9826
9827         if ((nbfile == 0) && (!filelist))
9828                 return CMD_HELP;
9829
9830         if (opaque)
9831                 opaque_len = strlen(opaque);
9832
9833         /*
9834          * Alloc the request structure with enough place to store all files
9835          * from command line.
9836          */
9837         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9838         if (!hur) {
9839                 fprintf(stderr, "Cannot create the request: %s\n",
9840                         strerror(errno));
9841                 return errno;
9842         }
9843         nbfile_alloc = nbfile;
9844
9845         hur->hur_request.hr_action = action;
9846         hur->hur_request.hr_archive_id = archive_id;
9847         hur->hur_request.hr_flags = 0;
9848
9849         /* All remaining args are files, add them */
9850         if (nbfile != 0 && some_file == NULL)
9851                 some_file = strdup(argv[optind]);
9852
9853         for (i = 0; i < nbfile; i++) {
9854                 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9855                                    &last_dev);
9856                 if (rc)
9857                         goto out_free;
9858         }
9859
9860         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9861
9862         /* If a filelist was specified, read the filelist from it. */
9863         if (filelist) {
9864                 fp = fopen(filelist, "r");
9865                 if (!fp) {
9866                         fprintf(stderr, "Cannot read the file list %s: %s\n",
9867                                 filelist, strerror(errno));
9868                         rc = -errno;
9869                         goto out_free;
9870                 }
9871
9872                 while ((rc = getline(&line, &len, fp)) != -1) {
9873                         /*
9874                          * If allocated buffer was too small, get something
9875                          * larger
9876                          */
9877                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9878                                 ssize_t size;
9879
9880                                 nbfile_alloc = nbfile_alloc * 2 + 1;
9881                                 oldhur = hur;
9882                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9883                                                                    opaque_len);
9884                                 if (!hur) {
9885                                         fprintf(stderr,
9886                                                 "hsm: cannot allocate the request: %s\n",
9887                                                 strerror(errno));
9888                                         hur = oldhur;
9889                                         rc = -errno;
9890                                         fclose(fp);
9891                                         goto out_free;
9892                                 }
9893                                 size = hur_len(oldhur);
9894                                 if (size < 0) {
9895                                         fprintf(stderr,
9896                                                 "hsm: cannot allocate %u files + %u bytes data\n",
9897                                                 oldhur->hur_request.hr_itemcount,
9898                                                 oldhur->hur_request.hr_data_len);
9899                                         free(hur);
9900                                         hur = oldhur;
9901                                         rc = -E2BIG;
9902                                         fclose(fp);
9903                                         goto out_free;
9904                                 }
9905                                 memcpy(hur, oldhur, size);
9906                                 free(oldhur);
9907                         }
9908
9909                         /* Chop CR */
9910                         if (line[strlen(line) - 1] == '\n')
9911                                 line[strlen(line) - 1] = '\0';
9912
9913                         rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9914                                            mntpath, line, &last_dev);
9915                         if (rc) {
9916                                 fclose(fp);
9917                                 goto out_free;
9918                         }
9919
9920                         if (!some_file) {
9921                                 some_file = line;
9922                                 line = NULL;
9923                         }
9924                 }
9925
9926                 rc = fclose(fp);
9927                 free(line);
9928         }
9929
9930         /* If a --data was used, add it to the request */
9931         hur->hur_request.hr_data_len = opaque_len;
9932         if (opaque)
9933                 memcpy(hur_data(hur), opaque, opaque_len);
9934
9935         /* Send the HSM request */
9936         if (realpath(some_file, fullpath) == NULL) {
9937                 fprintf(stderr, "Could not find path '%s': %s\n",
9938                         some_file, strerror(errno));
9939         }
9940         rc = llapi_hsm_request(fullpath, hur);
9941         if (rc) {
9942                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9943                         some_file, strerror(-rc));
9944                 goto out_free;
9945         }
9946
9947 out_free:
9948         free(some_file);
9949         free(hur);
9950         return rc;
9951 }
9952
9953 static int lfs_hsm_archive(int argc, char **argv)
9954 {
9955         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9956 }
9957
9958 static int lfs_hsm_restore(int argc, char **argv)
9959 {
9960         return lfs_hsm_request(argc, argv, HUA_RESTORE);
9961 }
9962
9963 static int lfs_hsm_release(int argc, char **argv)
9964 {
9965         return lfs_hsm_request(argc, argv, HUA_RELEASE);
9966 }
9967
9968 static int lfs_hsm_remove(int argc, char **argv)
9969 {
9970         return lfs_hsm_request(argc, argv, HUA_REMOVE);
9971 }
9972
9973 static int lfs_hsm_cancel(int argc, char **argv)
9974 {
9975         return lfs_hsm_request(argc, argv, HUA_CANCEL);
9976 }
9977
9978 static int lfs_swap_layouts(int argc, char **argv)
9979 {
9980         if (argc != 3)
9981                 return CMD_HELP;
9982
9983         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9984                                   SWAP_LAYOUTS_KEEP_MTIME |
9985                                   SWAP_LAYOUTS_KEEP_ATIME);
9986 }
9987
9988 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9989
9990 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9991
9992 int lfs_get_mode(const char *string)
9993 {
9994         enum lock_mode_user mode;
9995
9996         for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9997                 if (lock_mode_names[mode] == NULL)
9998                         continue;
9999                 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10000                         return mode;
10001         }
10002
10003         return -EINVAL;
10004 }
10005
10006 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10007 {
10008         enum lu_ladvise_type advice;
10009
10010         for (advice = 0;
10011              advice < ARRAY_SIZE(ladvise_names); advice++) {
10012                 if (ladvise_names[advice] == NULL)
10013                         continue;
10014                 if (strcmp(string, ladvise_names[advice]) == 0)
10015                         return advice;
10016         }
10017
10018         return LU_LADVISE_INVALID;
10019 }
10020
10021 static int lfs_ladvise(int argc, char **argv)
10022 {
10023         struct option long_opts[] = {
10024         { .val = 'a',   .name = "advice",       .has_arg = required_argument },
10025         { .val = 'b',   .name = "background",   .has_arg = no_argument },
10026         { .val = 'e',   .name = "end",          .has_arg = required_argument },
10027         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10028         { .val = 'l',   .name = "length",       .has_arg = required_argument },
10029         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
10030         { .val = 's',   .name = "start",        .has_arg = required_argument },
10031         { .val = 'u',   .name = "unset",        .has_arg = no_argument },
10032         { .name = NULL } };
10033         struct llapi_lu_ladvise advice;
10034         enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10035         unsigned long long start = 0;
10036         unsigned long long end = LUSTRE_EOF;
10037         unsigned long long length = 0;
10038         unsigned long long size_units;
10039         unsigned long long flags = 0;
10040         int c, fd, rc = 0;
10041         const char *path;
10042         int mode = 0;
10043
10044         optind = 0;
10045         while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10046                                 long_opts, NULL)) != -1) {
10047                 switch (c) {
10048                 case 'a':
10049                         advice_type = lfs_get_ladvice(optarg);
10050                         if (advice_type == LU_LADVISE_INVALID) {
10051                                 fprintf(stderr,
10052                                         "%s: invalid advice type '%s'\n",
10053                                         progname, optarg);
10054                                 fprintf(stderr, "Valid types:");
10055
10056                                 for (advice_type = 0;
10057                                      advice_type < ARRAY_SIZE(ladvise_names);
10058                                      advice_type++) {
10059                                         if (ladvise_names[advice_type] == NULL)
10060                                                 continue;
10061                                         fprintf(stderr, " %s",
10062                                                 ladvise_names[advice_type]);
10063                                 }
10064                                 fprintf(stderr, "\n");
10065
10066                                 return CMD_HELP;
10067                         }
10068                         break;
10069                 case 'b':
10070                         flags |= LF_ASYNC;
10071                         break;
10072                 case 'u':
10073                         flags |= LF_UNSET;
10074                         break;
10075                 case 'e':
10076                         size_units = 1;
10077                         rc = llapi_parse_size(optarg, &end,
10078                                               &size_units, 0);
10079                         if (rc) {
10080                                 fprintf(stderr, "%s: bad end offset '%s'\n",
10081                                         argv[0], optarg);
10082                                 return CMD_HELP;
10083                         }
10084                         break;
10085                 case 's':
10086                         size_units = 1;
10087                         rc = llapi_parse_size(optarg, &start,
10088                                               &size_units, 0);
10089                         if (rc) {
10090                                 fprintf(stderr,
10091                                         "%s: bad start offset '%s'\n",
10092                                         argv[0], optarg);
10093                                 return CMD_HELP;
10094                         }
10095                         break;
10096                 case 'l':
10097                         size_units = 1;
10098                         rc = llapi_parse_size(optarg, &length,
10099                                               &size_units, 0);
10100                         if (rc) {
10101                                 fprintf(stderr, "%s: bad length '%s'\n",
10102                                         argv[0], optarg);
10103                                 return CMD_HELP;
10104                         }
10105                         break;
10106                 case 'm':
10107                         mode = lfs_get_mode(optarg);
10108                         if (mode < 0) {
10109                                 fprintf(stderr,
10110                                         "%s: bad mode '%s', valid modes are READ or WRITE\n",
10111                                         argv[0], optarg);
10112                                 return CMD_HELP;
10113                         }
10114                         break;
10115                 default:
10116                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10117                                 progname, argv[optind - 1]);
10118                         /* fallthrough */
10119                 case 'h':
10120                         return CMD_HELP;
10121                 }
10122         }
10123
10124         if (advice_type == LU_LADVISE_INVALID) {
10125                 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10126                 fprintf(stderr, "Valid types:");
10127                 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10128                      advice_type++) {
10129                         if (ladvise_names[advice_type] == NULL)
10130                                 continue;
10131                         fprintf(stderr, " %s", ladvise_names[advice_type]);
10132                 }
10133                 fprintf(stderr, "\n");
10134                 return CMD_HELP;
10135         }
10136
10137         if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10138                 fprintf(stderr,
10139                         "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10140                         argv[0]);
10141                 return CMD_HELP;
10142         }
10143
10144         if (argc <= optind) {
10145                 fprintf(stderr, "%s: please give one or more file names\n",
10146                         argv[0]);
10147                 return CMD_HELP;
10148         }
10149
10150         if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10151                 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10152                         argv[0]);
10153                 return CMD_HELP;
10154         }
10155
10156         if (end == LUSTRE_EOF && length != 0)
10157                 end = start + length;
10158
10159         if (end <= start) {
10160                 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10161                         argv[0], start, end);
10162                 return CMD_HELP;
10163         }
10164
10165         if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10166                 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10167                         argv[0]);
10168                 return CMD_HELP;
10169         }
10170
10171         if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10172                 fprintf(stderr, "%s: mode is required with lockahead\n",
10173                         argv[0]);
10174                 return CMD_HELP;
10175         }
10176
10177         while (optind < argc) {
10178                 int rc2;
10179
10180                 path = argv[optind++];
10181
10182                 fd = open(path, O_RDONLY);
10183                 if (fd < 0) {
10184                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10185                                 argv[0], path, strerror(errno));
10186                         rc2 = -errno;
10187                         goto next;
10188                 }
10189
10190                 advice.lla_start = start;
10191                 advice.lla_end = end;
10192                 advice.lla_advice = advice_type;
10193                 advice.lla_value1 = 0;
10194                 advice.lla_value2 = 0;
10195                 advice.lla_value3 = 0;
10196                 advice.lla_value4 = 0;
10197                 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10198                         advice.lla_lockahead_mode = mode;
10199                         advice.lla_peradvice_flags = flags;
10200                 }
10201
10202                 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10203                 close(fd);
10204                 if (rc2 < 0) {
10205                         fprintf(stderr,
10206                                 "%s: cannot give advice '%s' to file '%s': %s\n",
10207                                 argv[0], ladvise_names[advice_type],
10208                                 path, strerror(errno));
10209
10210                         goto next;
10211                 }
10212
10213 next:
10214                 if (rc == 0 && rc2 < 0)
10215                         rc = rc2;
10216         }
10217         return rc;
10218 }
10219
10220 static const char *const heat_names[] = LU_HEAT_NAMES;
10221
10222 static int lfs_heat_get(int argc, char **argv)
10223 {
10224         struct lu_heat *heat;
10225         int rc = 0, rc2;
10226         char *path;
10227         int fd;
10228         int i;
10229
10230         if (argc <= 1)
10231                 return CMD_HELP;
10232
10233         heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10234         if (!heat) {
10235                 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10236                 return -ENOMEM;
10237         }
10238
10239         optind = 1;
10240         while (optind < argc) {
10241                 path = argv[optind++];
10242
10243                 fd = open(path, O_RDONLY);
10244                 if (fd < 0) {
10245                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10246                                 argv[0], path, strerror(errno));
10247                         rc2 = -errno;
10248                         goto next;
10249                 }
10250
10251                 heat->lh_count = OBD_HEAT_COUNT;
10252                 rc2 = llapi_heat_get(fd, heat);
10253                 close(fd);
10254                 if (rc2 < 0) {
10255                         fprintf(stderr,
10256                                 "%s: cannot get heat of file '%s': %s\n",
10257                                 argv[0], path, strerror(errno));
10258                         goto next;
10259                 }
10260
10261                 printf("flags: %x\n", heat->lh_flags);
10262                 for (i = 0; i < heat->lh_count; i++)
10263                         printf("%s: %llu\n", heat_names[i],
10264                                (unsigned long long)heat->lh_heat[i]);
10265 next:
10266                 if (rc == 0 && rc2 < 0)
10267                         rc = rc2;
10268         }
10269
10270         free(heat);
10271         return rc;
10272 }
10273
10274 static int lfs_heat_set(int argc, char **argv)
10275 {
10276         struct option long_opts[] = {
10277         { .val = 'c',   .name = "clear",        .has_arg = no_argument },
10278         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10279         { .val = 'o',   .name = "off",          .has_arg = no_argument },
10280         { .val = 'O',   .name = "on",           .has_arg = no_argument },
10281         { .name = NULL } };
10282         enum lu_heat_flag flags = 0;
10283         int rc = 0, rc2;
10284         char *path;
10285         int fd;
10286         int c;
10287
10288         if (argc <= 1)
10289                 return CMD_HELP;
10290
10291         optind = 0;
10292         while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10293                 switch (c) {
10294                 case 'c':
10295                         flags |= LU_HEAT_FLAG_CLEAR;
10296                         break;
10297                 case 'o':
10298                         flags |= LU_HEAT_FLAG_CLEAR;
10299                         flags |= LU_HEAT_FLAG_OFF;
10300                         break;
10301                 case 'O':
10302                         flags &= ~LU_HEAT_FLAG_OFF;
10303                         break;
10304                 default:
10305                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10306                                 progname, argv[optind - 1]);
10307                         /* fallthrough */
10308                 case 'h':
10309                         return CMD_HELP;
10310                 }
10311         }
10312
10313         if (argc <= optind) {
10314                 fprintf(stderr, "%s: please give one or more file names\n",
10315                         argv[0]);
10316                 return CMD_HELP;
10317         }
10318
10319         while (optind < argc) {
10320                 path = argv[optind++];
10321
10322                 fd = open(path, O_RDONLY);
10323                 if (fd < 0) {
10324                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10325                                 argv[0], path, strerror(errno));
10326                         rc2 = -errno;
10327                         goto next;
10328                 }
10329
10330                 rc2 = llapi_heat_set(fd, flags);
10331                 close(fd);
10332                 if (rc2 < 0) {
10333                         fprintf(stderr,
10334                                 "%s: cannot setflags heat of file '%s': %s\n",
10335                                 argv[0], path, strerror(errno));
10336                         goto next;
10337                 }
10338 next:
10339                 if (rc == 0 && rc2 < 0)
10340                         rc = rc2;
10341         }
10342         return rc;
10343 }
10344
10345 /**
10346  * The input string contains a comma delimited list of component ids and
10347  * ranges, for example "1,2-4,7".
10348  */
10349 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10350 {
10351         bool end_of_loop = false;
10352         char *ptr = NULL;
10353         int nr = 0;
10354         int rc;
10355
10356         if (!arg)
10357                 return -EINVAL;
10358
10359         while (!end_of_loop) {
10360                 int start_index;
10361                 int end_index;
10362                 int i;
10363                 char *endptr = NULL;
10364
10365                 rc = -EINVAL;
10366                 ptr = strchrnul(arg, ',');
10367                 end_of_loop = *ptr == '\0';
10368                 *ptr = '\0';
10369
10370                 start_index = strtol(arg, &endptr, 0);
10371                 if (endptr == arg) /* no data at all */
10372                         break;
10373                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10374                         break;
10375                 if (start_index < 0)
10376                         break;
10377
10378                 end_index = start_index;
10379                 if (*endptr == '-') {
10380                         end_index = strtol(endptr + 1, &endptr, 0);
10381                         if (*endptr != '\0')
10382                                 break;
10383                         if (end_index < start_index)
10384                                 break;
10385                 }
10386
10387                 for (i = start_index; i <= end_index && size > 0; i++) {
10388                         int j;
10389
10390                         /* remove duplicate */
10391                         for (j = 0; j < nr; j++) {
10392                                 if (ids[j] == i)
10393                                         break;
10394                         }
10395                         if (j == nr) { /* no duplicate */
10396                                 ids[nr++] = i;
10397                                 --size;
10398                         }
10399                 }
10400
10401                 if (size == 0 && i < end_index)
10402                         break;
10403
10404                 *ptr = ',';
10405                 arg = ++ptr;
10406                 rc = 0;
10407         }
10408         if (!end_of_loop && ptr)
10409                 *ptr = ',';
10410
10411         return rc < 0 ? rc : nr;
10412 }
10413
10414 /**
10415  * struct verify_mirror_id - Mirror id to be verified.
10416  * @mirror_id:   A specified mirror id.
10417  * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10418  */
10419 struct verify_mirror_id {
10420         __u16 mirror_id;
10421         bool is_valid_id;
10422 };
10423
10424 /**
10425  * compare_mirror_ids() - Compare mirror ids.
10426  * @layout: Mirror component list.
10427  * @cbdata: Callback data in verify_mirror_id structure.
10428  *
10429  * This is a callback function called by llapi_layout_comp_iterate()
10430  * to compare the specified mirror id with the one in the current
10431  * component of @layout. If they are the same, then the specified
10432  * mirror id is valid.
10433  *
10434  * Return: a negative error code on failure or
10435  *         LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10436  *         LLAPI_LAYOUT_ITER_STOP: Stop iteration
10437  */
10438 static inline
10439 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10440 {
10441         struct verify_mirror_id *mirror_id_cbdata =
10442                                  (struct verify_mirror_id *)cbdata;
10443         uint32_t mirror_id;
10444         int rc = 0;
10445
10446         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10447         if (rc < 0) {
10448                 rc = -errno;
10449                 fprintf(stderr,
10450                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
10451                         progname, strerror(errno));
10452                 return rc;
10453         }
10454
10455         if (mirror_id_cbdata->mirror_id == mirror_id) {
10456                 mirror_id_cbdata->is_valid_id = true;
10457                 return LLAPI_LAYOUT_ITER_STOP;
10458         }
10459
10460         return LLAPI_LAYOUT_ITER_CONT;
10461 }
10462
10463 /**
10464  * verify_mirror_ids() - Verify specified mirror ids.
10465  * @fname:      Mirrored file name.
10466  * @mirror_ids: Specified mirror ids to be verified.
10467  * @ids_nr:     Number of specified mirror ids.
10468  *
10469  * This function verifies that specified @mirror_ids are valid
10470  * in the mirrored file @fname.
10471  *
10472  * Return: 0 on success or a negative error code on failure.
10473  */
10474 static inline
10475 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10476 {
10477         struct llapi_layout *layout = NULL;
10478         struct verify_mirror_id mirror_id_cbdata = { 0 };
10479         struct stat stbuf;
10480         uint32_t flr_state;
10481         int i;
10482         int fd;
10483         int rc = 0;
10484         int rc2 = 0;
10485
10486         if (ids_nr <= 0)
10487                 return -EINVAL;
10488
10489         if (stat(fname, &stbuf) < 0) {
10490                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10491                         progname, fname, strerror(errno));
10492                 rc = -errno;
10493                 goto error;
10494         }
10495
10496         if (!S_ISREG(stbuf.st_mode)) {
10497                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10498                         progname, fname);
10499                 rc = -EINVAL;
10500                 goto error;
10501         }
10502
10503         fd = open(fname, O_DIRECT | O_RDONLY);
10504         if (fd < 0) {
10505                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10506                         progname, fname, strerror(errno));
10507                 rc = -errno;
10508                 goto error;
10509         }
10510
10511         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10512         if (rc < 0) {
10513                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10514                         progname, fname, strerror(errno));
10515                 goto close_fd;
10516         }
10517
10518         layout = llapi_layout_get_by_fd(fd, 0);
10519         if (!layout) {
10520                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10521                         progname, fname, strerror(errno));
10522                 rc = -errno;
10523                 llapi_lease_release(fd);
10524                 goto close_fd;
10525         }
10526
10527         rc = llapi_layout_flags_get(layout, &flr_state);
10528         if (rc < 0) {
10529                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10530                         progname, fname, strerror(errno));
10531                 rc = -errno;
10532                 goto free_layout;
10533         }
10534
10535         flr_state &= LCM_FL_FLR_MASK;
10536         switch (flr_state) {
10537         case LCM_FL_NONE:
10538                 rc = -EINVAL;
10539                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10540                         progname, fname, llapi_layout_flags_string(flr_state));
10541                 goto free_layout;
10542         default:
10543                 break;
10544         }
10545
10546         rc2 = 0;
10547         for (i = 0; i < ids_nr; i++) {
10548                 mirror_id_cbdata.mirror_id = mirror_ids[i];
10549                 mirror_id_cbdata.is_valid_id = false;
10550
10551                 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10552                                                &mirror_id_cbdata);
10553                 if (rc < 0) {
10554                         rc = -errno;
10555                         fprintf(stderr,
10556                                 "%s: '%s' failed to verify mirror id: %u.\n",
10557                                 progname, fname, mirror_ids[i]);
10558                         goto free_layout;
10559                 }
10560
10561                 if (!mirror_id_cbdata.is_valid_id) {
10562                         rc2 = -EINVAL;
10563                         fprintf(stderr,
10564                                 "%s: '%s' invalid specified mirror id: %u.\n",
10565                                 progname, fname, mirror_ids[i]);
10566                 }
10567         }
10568         rc = rc2;
10569
10570 free_layout:
10571         llapi_layout_free(layout);
10572         llapi_lease_release(fd);
10573 close_fd:
10574         close(fd);
10575 error:
10576         return rc;
10577 }
10578
10579 static inline
10580 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10581                            __u16 *mirror_ids, int ids_nr)
10582 {
10583         struct llapi_resync_comp comp_array[1024] = { { 0 } };
10584         struct llapi_layout *layout;
10585         struct stat stbuf;
10586         uint32_t flr_state;
10587         uint64_t start;
10588         uint64_t end;
10589         int comp_size = 0;
10590         int idx;
10591         int fd;
10592         int rc;
10593         int rc2;
10594
10595         if (stat(fname, &stbuf) < 0) {
10596                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10597                         progname, fname, strerror(errno));
10598                 rc = -errno;
10599                 goto error;
10600         }
10601         if (!S_ISREG(stbuf.st_mode)) {
10602                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10603                         progname, fname);
10604                 rc = -EINVAL;
10605                 goto error;
10606         }
10607
10608         /* Allow mirror resync even without the key on encrypted files */
10609         fd = open(fname, O_DIRECT | O_RDWR | O_FILE_ENC);
10610         if (fd < 0) {
10611                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10612                         progname, fname, strerror(errno));
10613                 rc = -errno;
10614                 goto error;
10615         }
10616
10617         layout = llapi_layout_get_by_fd(fd, 0);
10618         if (!layout) {
10619                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10620                         progname, fname, strerror(errno));
10621                 rc = -errno;
10622                 goto close_fd;
10623         }
10624
10625         rc = llapi_layout_flags_get(layout, &flr_state);
10626         if (rc) {
10627                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10628                         progname, fname, strerror(errno));
10629                 rc = -errno;
10630                 goto free_layout;
10631         }
10632
10633         flr_state &= LCM_FL_FLR_MASK;
10634         if (flr_state == LCM_FL_NONE) {
10635                 rc = -EINVAL;
10636                 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10637                         progname, fname);
10638                 goto free_layout;
10639         }
10640
10641         /* get stale component info */
10642         comp_size = llapi_mirror_find_stale(layout, comp_array,
10643                                             ARRAY_SIZE(comp_array),
10644                                             mirror_ids, ids_nr);
10645         if (comp_size <= 0) {
10646                 rc = comp_size;
10647                 goto free_layout;
10648         }
10649
10650         ioc->lil_mode = LL_LEASE_WRLCK;
10651         ioc->lil_flags = LL_LEASE_RESYNC;
10652         rc = llapi_lease_set(fd, ioc);
10653         if (rc < 0) {
10654                 if (rc == -EALREADY)
10655                         rc = 0;
10656                 else
10657                         fprintf(stderr,
10658                             "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10659                                 progname, fname, strerror(-rc));
10660                 goto free_layout;
10661         }
10662
10663         /* get the read range [start, end) */
10664         start = comp_array[0].lrc_start;
10665         end = comp_array[0].lrc_end;
10666         for (idx = 1; idx < comp_size; idx++) {
10667                 if (comp_array[idx].lrc_start < start)
10668                         start = comp_array[idx].lrc_start;
10669                 if (end < comp_array[idx].lrc_end)
10670                         end = comp_array[idx].lrc_end;
10671         }
10672
10673         rc = llapi_lease_check(fd);
10674         if (rc != LL_LEASE_WRLCK) {
10675                 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10676                         progname, fname);
10677                 goto free_layout;
10678         }
10679
10680         rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10681                                       start, end);
10682         if (rc < 0)
10683                 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10684                         progname, fname, strerror(-rc));
10685
10686         rc = migrate_set_timestamps(fd, &stbuf);
10687         if (rc < 0) {
10688                 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
10689                         progname, fname, strerror(-rc));
10690                 goto free_layout;
10691         }
10692
10693         /* need to do the lease unlock even resync fails */
10694         ioc->lil_mode = LL_LEASE_UNLCK;
10695         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10696         ioc->lil_count = 0;
10697         for (idx = 0; idx < comp_size; idx++) {
10698                 if (comp_array[idx].lrc_synced) {
10699                         ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10700                         ioc->lil_count++;
10701                 }
10702         }
10703
10704         rc2 = llapi_lease_set(fd, ioc);
10705         /**
10706          * llapi_lease_set returns lease mode when it request to unlock
10707          * the lease lock.
10708          */
10709         if (rc2 <= 0) {
10710                 /* rc2 == 0 means lost lease lock */
10711                 if (rc2 == 0 && rc == 0)
10712                         rc = -EBUSY;
10713                 else
10714                         rc = rc2;
10715                 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10716                         progname, fname,
10717                         rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10718
10719                 llapi_lease_release(fd);
10720                 goto free_layout;
10721         }
10722
10723 free_layout:
10724         llapi_layout_free(layout);
10725 close_fd:
10726         close(fd);
10727 error:
10728         return rc;
10729 }
10730
10731 static inline int lfs_mirror_resync(int argc, char **argv)
10732 {
10733         struct option long_opts[] = {
10734         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10735         { .val = 'o',   .name = "only",         .has_arg = required_argument },
10736         { .name = NULL } };
10737         struct ll_ioc_lease *ioc = NULL;
10738         __u16 mirror_ids[128] = { 0 };
10739         int ids_nr = 0;
10740         int c;
10741         int rc = 0;
10742
10743         while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
10744                 switch (c) {
10745                 case 'o':
10746                         rc = parse_mirror_ids(mirror_ids,
10747                                         sizeof(mirror_ids) / sizeof(__u16),
10748                                         optarg);
10749                         if (rc < 0) {
10750                                 fprintf(stderr,
10751                                         "%s: bad mirror ids '%s'.\n",
10752                                         argv[0], optarg);
10753                                 goto error;
10754                         }
10755                         ids_nr = rc;
10756                         break;
10757                 default:
10758                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10759                                 progname, argv[optind - 1]);
10760                         /* fallthrough */
10761                 case 'h':
10762                         rc = CMD_HELP;
10763                         goto error;
10764                 }
10765         }
10766
10767         if (argc == optind) {
10768                 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10769                 rc = CMD_HELP;
10770                 goto error;
10771         }
10772
10773         if (ids_nr > 0 && argc > optind + 1) {
10774                 fprintf(stderr,
10775                     "%s: option '--only' cannot be used upon multiple files.\n",
10776                         argv[0]);
10777                 rc = CMD_HELP;
10778                 goto error;
10779         }
10780
10781         if (ids_nr > 0) {
10782                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10783                 if (rc < 0)
10784                         goto error;
10785         }
10786
10787         /* set the lease on the file */
10788         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10789         if (!ioc) {
10790                 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10791                         argv[0], strerror(errno));
10792                 rc = -errno;
10793                 goto error;
10794         }
10795
10796         for (; optind < argc; optind++) {
10797                 rc = lfs_mirror_resync_file(argv[optind], ioc,
10798                                             mirror_ids, ids_nr);
10799                 /* ignore previous file's error, continue with next file */
10800
10801                 /* reset ioc */
10802                 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10803         }
10804
10805         free(ioc);
10806 error:
10807         return rc;
10808 }
10809
10810 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10811 {
10812         struct llapi_layout *layout;
10813         int rc;
10814
10815         layout = llapi_layout_get_by_fd(fd, 0);
10816         if (!layout) {
10817                 fprintf(stderr, "could not get layout.\n");
10818                 return  -EINVAL;
10819         }
10820
10821         rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10822         if (rc < 0) {
10823                 fprintf(stderr, "failed to iterate layout\n");
10824                 llapi_layout_free(layout);
10825
10826                 return rc;
10827         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10828                 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10829                 llapi_layout_free(layout);
10830
10831                 return -EINVAL;
10832         }
10833         llapi_layout_free(layout);
10834
10835         return 0;
10836 }
10837
10838 /**
10839  * Check whether two files are the same file
10840  * \retval      0  same file
10841  * \retval      1  not the same file
10842  * \retval      <0 error code
10843  */
10844 static inline int check_same_file(int fd, const char *f2)
10845 {
10846         struct stat stbuf1;
10847         struct stat stbuf2;
10848
10849         if (fstat(fd, &stbuf1) < 0)
10850                 return -errno;
10851
10852         if (stat(f2, &stbuf2) < 0)
10853                 return 1;
10854
10855         if (stbuf1.st_rdev == stbuf2.st_rdev &&
10856             stbuf1.st_ino == stbuf2.st_ino)
10857                 return 0;
10858
10859         return 1;
10860 }
10861
10862 static inline int lfs_mirror_read(int argc, char **argv)
10863 {
10864         int rc = CMD_HELP;
10865         __u16 mirror_id = 0;
10866         const char *outfile = NULL;
10867         char *fname;
10868         int fd = 0;
10869         int outfd;
10870         int c;
10871         void *buf;
10872         const size_t buflen = 4 << 20;
10873         off_t pos;
10874         struct option long_opts[] = {
10875         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10876         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
10877         { .val = 'o',   .name = "outfile",      .has_arg = required_argument },
10878         { .name = NULL } };
10879
10880         while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
10881                 char *end;
10882
10883                 switch (c) {
10884                 case 'N': {
10885                         unsigned long int id;
10886
10887                         errno = 0;
10888                         id = strtoul(optarg, &end, 0);
10889                         if (errno != 0 || *end != '\0' || id == 0 ||
10890                             id > UINT16_MAX) {
10891                                 fprintf(stderr,
10892                                         "%s %s: invalid mirror ID '%s'\n",
10893                                         progname, argv[0], optarg);
10894                                 return rc;
10895                         }
10896
10897                         mirror_id = (__u16)id;
10898                         break;
10899                 }
10900                 case 'o':
10901                         outfile = optarg;
10902                         break;
10903                 default:
10904                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10905                                 progname, argv[optind - 1]);
10906                         /* fallthrough */
10907                 case 'h':
10908                         return CMD_HELP;
10909                 }
10910         }
10911
10912         if (argc == optind) {
10913                 fprintf(stderr, "%s %s: no mirrored file provided\n",
10914                         progname, argv[0]);
10915                 return rc;
10916         } else if (argc > optind + 1) {
10917                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10918                 return rc;
10919         }
10920
10921         if (mirror_id == 0) {
10922                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10923                         progname, argv[0]);
10924                 return rc;
10925         }
10926
10927         /* open mirror file */
10928         fname = argv[optind];
10929         fd = open(fname, O_DIRECT | O_RDONLY);
10930         if (fd < 0) {
10931                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10932                         progname, argv[0], fname, strerror(errno));
10933                 return rc;
10934         }
10935
10936         /* verify mirror id */
10937         rc = verify_mirror_id_by_fd(fd, mirror_id);
10938         if (rc) {
10939                 fprintf(stderr,
10940                         "%s %s: cannot find mirror with ID %u in '%s'\n",
10941                         progname, argv[0], mirror_id, fname);
10942                 goto close_fd;
10943         }
10944
10945         /* open output file - O_EXCL ensures output is not the same as input */
10946         if (outfile) {
10947                 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10948                 if (outfd < 0) {
10949                         fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10950                                 progname, argv[0], outfile, strerror(errno));
10951                         rc = -errno;
10952                         goto close_fd;
10953                 }
10954         } else {
10955                 outfd = STDOUT_FILENO;
10956         }
10957
10958         /* allocate buffer */
10959         rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10960         if (rc) {
10961                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10962                                 progname, argv[0], rc);
10963                 goto close_outfd;
10964         }
10965
10966         pos = 0;
10967         while (1) {
10968                 ssize_t bytes_read;
10969                 ssize_t written = 0;
10970
10971                 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10972                 if (bytes_read < 0) {
10973                         rc = bytes_read;
10974                         fprintf(stderr,
10975                                 "%s %s: fail to read data from mirror %u: %s\n",
10976                                 progname, argv[0], mirror_id, strerror(-rc));
10977                         goto free_buf;
10978                 }
10979
10980                 /* EOF reached */
10981                 if (bytes_read == 0)
10982                         break;
10983
10984                 while (written < bytes_read) {
10985                         ssize_t written2;
10986
10987                         written2 = write(outfd, buf + written,
10988                                          bytes_read - written);
10989                         if (written2 < 0) {
10990                                 fprintf(stderr,
10991                                         "%s %s: fail to write %s: %s\n",
10992                                         progname, argv[0], outfile ? : "STDOUT",
10993                                         strerror(errno));
10994                                 rc = -errno;
10995                                 goto free_buf;
10996                         }
10997                         written += written2;
10998                 }
10999
11000                 if (written != bytes_read) {
11001                         fprintf(stderr,
11002                 "%s %s: written %ld bytes does not match with %ld read.\n",
11003                                 progname, argv[0], written, bytes_read);
11004                         rc = -EIO;
11005                         goto free_buf;
11006                 }
11007
11008                 pos += bytes_read;
11009         }
11010
11011         fsync(outfd);
11012         rc = 0;
11013
11014 free_buf:
11015         free(buf);
11016 close_outfd:
11017         if (outfile)
11018                 close(outfd);
11019 close_fd:
11020         close(fd);
11021
11022         return rc;
11023 }
11024
11025 static inline int lfs_mirror_write(int argc, char **argv)
11026 {
11027         int rc = CMD_HELP;
11028         __u16 mirror_id = 0;
11029         const char *inputfile = NULL;
11030         char *fname;
11031         int fd = 0;
11032         int inputfd;
11033         int c;
11034         void *buf;
11035         const size_t buflen = 4 << 20;
11036         off_t pos;
11037         size_t page_size = sysconf(_SC_PAGESIZE);
11038         struct ll_ioc_lease_id ioc;
11039         struct option long_opts[] = {
11040         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11041         { .val = 'i',   .name = "inputfile",    .has_arg = required_argument },
11042         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
11043         { .name = NULL } };
11044
11045         while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11046                 char *end;
11047
11048                 switch (c) {
11049                 case 'N': {
11050                         unsigned long int id;
11051
11052                         errno = 0;
11053                         id = strtoul(optarg, &end, 0);
11054                         if (errno != 0 || *end != '\0' || id == 0 ||
11055                             id > UINT16_MAX) {
11056                                 fprintf(stderr,
11057                                         "%s %s: invalid mirror ID '%s'\n",
11058                                         progname, argv[0], optarg);
11059                                 return rc;
11060                         }
11061
11062                         mirror_id = (__u16)id;
11063                         break;
11064                 }
11065                 case 'i':
11066                         inputfile = optarg;
11067                         break;
11068                 default:
11069                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11070                                 progname, argv[optind - 1]);
11071                         /* fallthrough */
11072                 case 'h':
11073                         return CMD_HELP;
11074                 }
11075         }
11076
11077         if (argc == optind) {
11078                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11079                         progname, argv[0]);
11080                 return rc;
11081         } else if (argc > optind + 1) {
11082                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11083                 return rc;
11084         }
11085
11086         if (mirror_id == 0) {
11087                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11088                         progname, argv[0]);
11089                 return rc;
11090         }
11091
11092         /* open mirror file */
11093         fname = argv[optind];
11094         fd = open(fname, O_DIRECT | O_WRONLY);
11095         if (fd < 0) {
11096                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11097                         progname, argv[0], fname, strerror(errno));
11098                 return rc;
11099         }
11100
11101         /* verify mirror id */
11102         rc = verify_mirror_id_by_fd(fd, mirror_id);
11103         if (rc) {
11104                 fprintf(stderr,
11105                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11106                         progname, argv[0], mirror_id, fname);
11107                 goto close_fd;
11108         }
11109
11110         /* open input file */
11111         if (inputfile) {
11112                 rc = check_same_file(fd, inputfile);
11113                 if (rc == 0) {
11114                         fprintf(stderr,
11115                         "%s %s: input file cannot be the mirrored file\n",
11116                                 progname, argv[0]);
11117                         goto close_fd;
11118                 }
11119                 if (rc < 0)
11120                         goto close_fd;
11121
11122                 inputfd = open(inputfile, O_RDONLY, 0644);
11123                 if (inputfd < 0) {
11124                         fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11125                                 progname, argv[0], inputfile, strerror(errno));
11126                         rc = -errno;
11127                         goto close_fd;
11128                 }
11129         } else {
11130                 inputfd = STDIN_FILENO;
11131         }
11132
11133         /* allocate buffer */
11134         rc = posix_memalign(&buf, page_size, buflen);
11135         if (rc) {
11136                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11137                         progname, argv[0], rc);
11138                 goto close_inputfd;
11139         }
11140
11141         /* prepare target mirror components instantiation */
11142         ioc.lil_mode = LL_LEASE_WRLCK;
11143         ioc.lil_flags = LL_LEASE_RESYNC;
11144         ioc.lil_mirror_id = mirror_id;
11145         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11146         if (rc < 0) {
11147                 fprintf(stderr,
11148                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11149                         progname, argv[0], fname, strerror(errno));
11150                 goto free_buf;
11151         }
11152
11153         pos = 0;
11154         while (1) {
11155                 ssize_t bytes_read;
11156                 ssize_t written;
11157                 size_t to_write;
11158
11159                 rc = llapi_lease_check(fd);
11160                 if (rc != LL_LEASE_WRLCK) {
11161                         fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11162                                 progname, argv[0], fname);
11163                         goto free_buf;
11164                 }
11165
11166                 bytes_read = read(inputfd, buf, buflen);
11167                 if (bytes_read < 0) {
11168                         rc = bytes_read;
11169                         fprintf(stderr,
11170                                 "%s %s: fail to read data from '%s': %s\n",
11171                                 progname, argv[0], inputfile ? : "STDIN",
11172                                 strerror(errno));
11173                         rc = -errno;
11174                         goto free_buf;
11175                 }
11176
11177                 /* EOF reached */
11178                 if (bytes_read == 0)
11179                         break;
11180
11181                 /* round up to page align to make direct IO happy. */
11182                 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11183
11184                 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11185                                              pos);
11186                 if (written < 0) {
11187                         rc = written;
11188                         fprintf(stderr,
11189                               "%s %s: fail to write to mirror %u: %s\n",
11190                                 progname, argv[0], mirror_id,
11191                                 strerror(-rc));
11192                         goto free_buf;
11193                 }
11194
11195                 pos += bytes_read;
11196         }
11197
11198         if (pos & (page_size - 1)) {
11199                 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11200                 if (rc < 0)
11201                         goto free_buf;
11202         }
11203
11204         ioc.lil_mode = LL_LEASE_UNLCK;
11205         ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11206         ioc.lil_count = 0;
11207         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11208         if (rc <= 0) {
11209                 if (rc == 0)
11210                         rc = -EBUSY;
11211                 fprintf(stderr,
11212                         "%s %s: release lease lock of '%s' failed: %s\n",
11213                         progname, argv[0], fname, strerror(errno));
11214                 goto free_buf;
11215         }
11216
11217         rc = 0;
11218
11219 free_buf:
11220         free(buf);
11221 close_inputfd:
11222         if (inputfile)
11223                 close(inputfd);
11224 close_fd:
11225         close(fd);
11226
11227         return rc;
11228 }
11229
11230 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11231 {
11232         struct llapi_layout *layout;
11233         struct collect_ids_data cid = { .cid_ids = ids,
11234                                         .cid_count = 0,
11235                                         .cid_exclude = exclude_id, };
11236         int rc;
11237
11238         layout = llapi_layout_get_by_fd(fd, 0);
11239         if (!layout) {
11240                 fprintf(stderr, "could not get layout\n");
11241                 return -EINVAL;
11242         }
11243
11244         rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11245         if (rc < 0) {
11246                 fprintf(stderr, "failed to iterate layout\n");
11247                 llapi_layout_free(layout);
11248
11249                 return rc;
11250         }
11251         llapi_layout_free(layout);
11252
11253         return cid.cid_count;
11254 }
11255
11256 #ifndef MIRROR_ID_NEG
11257 #define MIRROR_ID_NEG         0x8000
11258 #endif
11259
11260 static inline int lfs_mirror_copy(int argc, char **argv)
11261 {
11262         int rc = CMD_HELP;
11263         __u16 read_mirror_id = 0;
11264         __u16 ids[128] = { 0 };
11265         int count = 0;
11266         struct llapi_layout *layout = NULL;
11267         struct llapi_resync_comp comp_array[1024] = { { 0 } };
11268         int comp_size = 0;
11269         char *fname;
11270         int fd = 0;
11271         int c;
11272         int i;
11273         ssize_t copied;
11274         struct ll_ioc_lease *ioc = NULL;
11275         struct ll_ioc_lease_id *resync_ioc;
11276         struct option long_opts[] = {
11277         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11278         { .val = 'i',   .name = "read-mirror",  .has_arg = required_argument },
11279         { .val = 'o',   .name = "write-mirror", .has_arg = required_argument },
11280         { .name = NULL } };
11281         char cmd[PATH_MAX];
11282
11283         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11284         progname = cmd;
11285         while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11286                 char *end;
11287
11288                 switch (c) {
11289                 case 'i': {
11290                         unsigned long int id;
11291
11292                         errno = 0;
11293                         id = strtoul(optarg, &end, 0);
11294                         if (errno != 0 || *end != '\0' || id == 0 ||
11295                             id > UINT16_MAX) {
11296                                 fprintf(stderr,
11297                                         "%s: invalid read mirror ID '%s'\n",
11298                                         progname, optarg);
11299                                 return rc;
11300                         }
11301
11302                         read_mirror_id = (__u16)id;
11303                         break;
11304                 }
11305                 case 'o':
11306                         if (!strcmp(optarg, "-1")) {
11307                                 /* specify all other mirrors */
11308                                 ids[0] = (__u16)-1;
11309                                 count = 1;
11310                         } else {
11311                                 count = parse_mirror_ids((__u16 *)ids,
11312                                                          ARRAY_SIZE(ids),
11313                                                          optarg);
11314                                 if (count < 0)
11315                                         return rc;
11316                         }
11317                         break;
11318                 default:
11319                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11320                                 progname, argv[optind - 1]);
11321                         /* fallthrough */
11322                 case 'h':
11323                         return CMD_HELP;
11324                 }
11325         }
11326
11327         if (argc == optind) {
11328                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11329                         progname, argv[0]);
11330                 return rc;
11331         } else if (argc > optind + 1) {
11332                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11333                 return rc;
11334         }
11335
11336         if (read_mirror_id == 0) {
11337                 fprintf(stderr,
11338                         "%s %s: no valid read mirror ID %d is provided\n",
11339                         progname, argv[0], read_mirror_id);
11340                 return rc;
11341         }
11342
11343         if (count == 0) {
11344                 fprintf(stderr,
11345                         "%s %s: no write mirror ID is provided\n",
11346                         progname, argv[0]);
11347                 return rc;
11348         }
11349
11350         for (i = 0; i < count; i++) {
11351                 if (read_mirror_id == ids[i]) {
11352                         fprintf(stderr,
11353                         "%s %s: read and write mirror ID cannot be the same\n",
11354                                 progname, argv[0]);
11355                         return rc;
11356                 }
11357         }
11358
11359         /* open mirror file */
11360         fname = argv[optind];
11361
11362         fd = open(fname, O_DIRECT | O_RDWR);
11363         if (fd < 0) {
11364                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11365                         progname, argv[0], fname, strerror(errno));
11366                 return rc;
11367         }
11368
11369         /* write to all other mirrors */
11370         if (ids[0] == (__u16)-1) {
11371                 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11372                 if (count <= 0) {
11373                         rc = count;
11374                         fprintf(stderr,
11375                         "%s %s: failed to get other mirror ids in '%s': %d\n",
11376                                 progname, argv[0], fname, rc);
11377                         goto close_fd;
11378                 }
11379         }
11380
11381         /* verify mirror id */
11382         rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11383         if (rc) {
11384                 fprintf(stderr,
11385                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11386                         progname, argv[0], read_mirror_id, fname);
11387                 goto close_fd;
11388         }
11389
11390         for (i = 0; i < count; i++) {
11391                 rc = verify_mirror_id_by_fd(fd, ids[i]);
11392                 if (rc) {
11393                         fprintf(stderr,
11394                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11395                                 progname, argv[0], ids[i], fname);
11396                         goto close_fd;
11397                 }
11398         }
11399
11400         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11401         if (!ioc) {
11402                 fprintf(stderr,
11403                         "%s %s: cannot alloc comp id array for ioc: %s\n",
11404                         progname, argv[0], strerror(errno));
11405                 rc = -errno;
11406                 goto close_fd;
11407         }
11408
11409         /* get stale component info */
11410         layout = llapi_layout_get_by_fd(fd, 0);
11411         if (!layout) {
11412                 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11413                         progname, argv[0], fname, strerror(errno));
11414                 rc = -errno;
11415                 goto free_ioc;
11416         }
11417         comp_size = llapi_mirror_find_stale(layout, comp_array,
11418                                             ARRAY_SIZE(comp_array),
11419                                             ids, count);
11420         llapi_layout_free(layout);
11421         if (comp_size < 0) {
11422                 rc = comp_size;
11423                 goto free_ioc;
11424         }
11425
11426         /* prepare target mirror components instantiation */
11427         resync_ioc = (struct ll_ioc_lease_id *)ioc;
11428         resync_ioc->lil_mode = LL_LEASE_WRLCK;
11429         resync_ioc->lil_flags = LL_LEASE_RESYNC;
11430         if (count == 1)
11431                 resync_ioc->lil_mirror_id = ids[0];
11432         else
11433                 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11434         rc = llapi_lease_set(fd, ioc);
11435         if (rc < 0) {
11436                 fprintf(stderr,
11437                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11438                         progname, argv[0], fname, strerror(errno));
11439                 goto free_ioc;
11440         }
11441
11442         copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11443         if (copied < 0) {
11444                 rc = copied;
11445                 fprintf(stderr, "%s %s: copy error: %d\n",
11446                         progname, argv[0], rc);
11447                 goto free_ioc;
11448         }
11449
11450         fprintf(stdout, "mirror copied successfully: ");
11451         for (i = 0; i < copied; i++)
11452                 fprintf(stdout, "%d ", ids[i]);
11453         fprintf(stdout, "\n");
11454
11455         ioc->lil_mode = LL_LEASE_UNLCK;
11456         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11457         ioc->lil_count = 0;
11458         for (i = 0; i < comp_size; i++) {
11459                 int j;
11460
11461                 for (j = 0; j < copied; j++) {
11462                         if (comp_array[i].lrc_mirror_id != ids[j])
11463                                 continue;
11464
11465                         ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11466                         ioc->lil_count++;
11467                 }
11468         }
11469         rc = llapi_lease_set(fd, ioc);
11470         if (rc <= 0) {
11471                 if (rc == 0)
11472                         rc = -EBUSY;
11473                 fprintf(stderr,
11474                         "%s %s: release lease lock of '%s' failed: %s\n",
11475                         progname, argv[0], fname, strerror(errno));
11476                 goto free_ioc;
11477         }
11478
11479         rc = 0;
11480
11481 free_ioc:
11482         free(ioc);
11483 close_fd:
11484         close(fd);
11485
11486         return rc;
11487 }
11488
11489 /**
11490  * struct verify_chunk - Mirror chunk to be verified.
11491  * @chunk:        [start, end) of the chunk.
11492  * @mirror_count: Number of mirror ids in @mirror_id array.
11493  * @mirror_id:    Array of valid mirror ids that cover the chunk.
11494  */
11495 struct verify_chunk {
11496         struct lu_extent chunk;
11497         unsigned int mirror_count;
11498         __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11499 };
11500
11501 /**
11502  * print_chunks() - Print chunk information.
11503  * @fname:       Mirrored file name.
11504  * @chunks:      Array of chunks.
11505  * @chunk_count: Number of chunks in @chunks array.
11506  *
11507  * This function prints [start, end) of each chunk in @chunks
11508  * for mirrored file @fname, and also prints the valid mirror ids
11509  * that cover the chunk.
11510  *
11511  * Return: void.
11512  */
11513 static inline
11514 void print_chunks(const char *fname, struct verify_chunk *chunks,
11515                   int chunk_count)
11516 {
11517         int i;
11518         int j;
11519
11520         fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11521         for (i = 0; i < chunk_count; i++) {
11522                 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11523
11524                 if (chunks[i].mirror_count == 0)
11525                         fprintf(stdout, "\t[");
11526                 else {
11527                         fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11528                         for (j = 1; j < chunks[i].mirror_count; j++)
11529                                 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11530                 }
11531                 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11532         }
11533         fprintf(stdout, "\n");
11534 }
11535
11536 /**
11537  * print_checksums() - Print CRC-32 checksum values.
11538  * @chunk: A chunk and its corresponding valid mirror ids.
11539  * @crc:   CRC-32 checksum values on the chunk for each valid mirror.
11540  *
11541  * This function prints CRC-32 checksum values on @chunk for
11542  * each valid mirror that covers it.
11543  *
11544  * Return: void.
11545  */
11546 static inline
11547 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11548 {
11549         int i;
11550
11551         fprintf(stdout,
11552                 "CRC-32 checksum value for chunk "DEXT":\n",
11553                 PEXT(&chunk->chunk));
11554         for (i = 0; i < chunk->mirror_count; i++)
11555                 fprintf(stdout, "Mirror %u:\t%#lx\n",
11556                         chunk->mirror_id[i], crc[i]);
11557         fprintf(stdout, "\n");
11558 }
11559
11560 /**
11561  * filter_mirror_id() - Filter specified mirror ids.
11562  * @chunks:      Array of chunks.
11563  * @chunk_count: Number of chunks in @chunks array.
11564  * @mirror_ids:  Specified mirror ids to be verified.
11565  * @ids_nr:      Number of specified mirror ids.
11566  *
11567  * This function scans valid mirror ids that cover each chunk in @chunks
11568  * and filters specified mirror ids.
11569  *
11570  * Return: void.
11571  */
11572 static inline
11573 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11574                       __u16 *mirror_ids, int ids_nr)
11575 {
11576         int i;
11577         int j;
11578         int k;
11579         __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11580         unsigned int valid_count = 0;
11581
11582         for (i = 0; i < chunk_count; i++) {
11583                 if (chunks[i].mirror_count == 0)
11584                         continue;
11585
11586                 valid_count = 0;
11587                 for (j = 0; j < ids_nr; j++) {
11588                         for (k = 0; k < chunks[i].mirror_count; k++) {
11589                                 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11590                                         valid_id[valid_count] = mirror_ids[j];
11591                                         valid_count++;
11592                                         break;
11593                                 }
11594                         }
11595                 }
11596
11597                 memcpy(chunks[i].mirror_id, valid_id,
11598                        sizeof(__u16) * valid_count);
11599                 chunks[i].mirror_count = valid_count;
11600         }
11601 }
11602
11603 /**
11604  * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11605  * @layout:      Mirror component list.
11606  * @chunks:      Array of chunks.
11607  * @chunks_size: Array size of @chunks.
11608  *
11609  * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11610  * to find out chunk segments and store them in @chunks array.
11611  *
11612  * The @mirror_id array in each element of @chunks will store the valid
11613  * mirror ids that cover the chunk. If a mirror component covering the
11614  * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11615  * will not be stored into the @mirror_id array, and the chunk for that
11616  * mirror will not be verified.
11617  *
11618  * The @mirror_count in each element of @chunks will store the number of
11619  * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11620  * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11621  * indicates the chunk is valid in only one mirror. In both cases, the
11622  * chunk will not be verified.
11623  *
11624  * Here is an example:
11625  *
11626  *  0      1M     2M     3M     4M           EOF
11627  *  +------+-------------+--------------------+
11628  *  |      |             |      S             |       mirror1
11629  *  +------+------+------+------+-------------+
11630  *  |             |   S  |   S  |             |       mirror2
11631  *  +-------------+------+------+-------------+
11632  *
11633  * prepared @chunks array will contain 5 elements:
11634  * (([0, 1M), [1, 2], 2),
11635  *  ([1M, 2M), [1, 2], 2),
11636  *  ([2M, 3M), [1], 1),
11637  *  ([3M, 4M], [], 0),
11638  *  ([4M, EOF), [2], 1))
11639  *
11640  * Return: the actual array size of @chunks on success
11641  *         or a negative error code on failure.
11642  */
11643 static inline
11644 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11645                              struct verify_chunk *chunks,
11646                              size_t chunks_size)
11647 {
11648         uint64_t start;
11649         uint64_t end;
11650         uint32_t mirror_id;
11651         uint32_t flags;
11652         int idx = 0;
11653         int i = 0;
11654         int rc = 0;
11655
11656         memset(chunks, 0, sizeof(*chunks) * chunks_size);
11657
11658         while (1) {
11659                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11660                 if (rc < 0) {
11661                         fprintf(stderr,
11662                                 "%s: move to the first layout component: %s.\n",
11663                                 progname, strerror(errno));
11664                         goto error;
11665                 }
11666
11667                 i = 0;
11668                 rc = 0;
11669                 chunks[idx].chunk.e_end = LUSTRE_EOF;
11670                 while (rc == 0) {
11671                         rc = llapi_layout_comp_extent_get(layout, &start, &end);
11672                         if (rc < 0) {
11673                                 fprintf(stderr,
11674                                         "%s: llapi_layout_comp_extent_get failed: %s.\n",
11675                                         progname, strerror(errno));
11676                                 goto error;
11677                         }
11678
11679                         if (start > chunks[idx].chunk.e_start ||
11680                             end <= chunks[idx].chunk.e_start)
11681                                 goto next;
11682
11683                         if (end < chunks[idx].chunk.e_end)
11684                                 chunks[idx].chunk.e_end = end;
11685
11686                         rc = llapi_layout_comp_flags_get(layout, &flags);
11687                         if (rc < 0) {
11688                                 fprintf(stderr,
11689                                         "%s: llapi_layout_comp_flags_get failed: %s.\n",
11690                                         progname, strerror(errno));
11691                                 goto error;
11692                         }
11693
11694                         if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11695                                 goto next;
11696
11697                         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11698                         if (rc < 0) {
11699                                 fprintf(stderr,
11700                                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
11701                                         progname, strerror(errno));
11702                                 goto error;
11703                         }
11704
11705                         chunks[idx].mirror_id[i] = mirror_id;
11706                         i++;
11707                         if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11708                                 fprintf(stderr,
11709                                         "%s: mirror_id array is too small.\n",
11710                                         progname);
11711                                 rc = -EINVAL;
11712                                 goto error;
11713                         }
11714
11715 next:
11716                         rc = llapi_layout_comp_use(layout,
11717                                                    LLAPI_LAYOUT_COMP_USE_NEXT);
11718                         if (rc < 0) {
11719                                 fprintf(stderr,
11720                                         "%s: move to the next layout component: %s.\n",
11721                                         progname, strerror(errno));
11722                                 goto error;
11723                         }
11724                 } /* loop through all components */
11725
11726                 chunks[idx].mirror_count = i;
11727
11728                 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11729                         break;
11730
11731                 idx++;
11732                 if (idx >= chunks_size) {
11733                         fprintf(stderr, "%s: chunks array is too small.\n",
11734                                 progname);
11735                         rc = -EINVAL;
11736                         goto error;
11737                 }
11738
11739                 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11740         }
11741
11742 error:
11743         return rc < 0 ? rc : idx + 1;
11744 }
11745
11746 /**
11747  * lfs_mirror_verify_chunk() - Verify a chunk.
11748  * @fd:        File descriptor of the mirrored file.
11749  * @file_size: Size of the mirrored file.
11750  * @chunk:     A chunk and its corresponding valid mirror ids.
11751  * @verbose:   Verbose mode.
11752  *
11753  * This function verifies a @chunk contains exactly the same data
11754  * ammong the mirrors that cover it.
11755  *
11756  * If @verbose is specified, then the function will print where the
11757  * differences are if the data do not match. Otherwise, it will
11758  * just return an error in that case.
11759  *
11760  * Return: 0 on success or a negative error code on failure.
11761  */
11762 static inline
11763 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11764                             struct verify_chunk *chunk, int verbose)
11765 {
11766         const size_t buflen = 4 * 1024 * 1024; /* 4M */
11767         void *buf;
11768         size_t page_size = sysconf(_SC_PAGESIZE);
11769         ssize_t bytes_read;
11770         ssize_t bytes_done;
11771         size_t count;
11772         off_t pos;
11773         unsigned long crc;
11774         unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11775         int i;
11776         int rc = 0;
11777
11778         if (file_size == 0)
11779                 return 0;
11780
11781         rc = posix_memalign(&buf, page_size, buflen);
11782         if (rc) /* error code is returned directly */
11783                 return -rc;
11784
11785         if (verbose > 1) {
11786                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11787                         PEXT(&chunk->chunk));
11788                 for (i = 0; i < chunk->mirror_count; i++)
11789                         fprintf(stdout, " %u", chunk->mirror_id[i]);
11790                 fprintf(stdout, "\n");
11791         }
11792
11793         bytes_done = 0;
11794         count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11795         pos = chunk->chunk.e_start;
11796         while (bytes_done < count) {
11797                 /* compute initial CRC-32 checksum */
11798                 crc = crc32(0L, Z_NULL, 0);
11799                 memset(crc_array, 0, sizeof(crc_array));
11800
11801                 bytes_read = 0;
11802                 for (i = 0; i < chunk->mirror_count; i++) {
11803                         bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11804                                                        buf, buflen, pos);
11805                         if (bytes_read < 0) {
11806                                 rc = bytes_read;
11807                                 fprintf(stderr,
11808                                         "%s: failed to read data from mirror %u: %s.\n",
11809                                         progname, chunk->mirror_id[i],
11810                                         strerror(-rc));
11811                                 goto error;
11812                         }
11813
11814                         /* compute new CRC-32 checksum */
11815                         crc_array[i] = crc32(crc, buf, bytes_read);
11816                 }
11817
11818                 if (verbose)
11819                         print_checksums(chunk, crc_array);
11820
11821                 /* compare CRC-32 checksum values */
11822                 for (i = 1; i < chunk->mirror_count; i++) {
11823                         if (crc_array[i] != crc_array[0]) {
11824                                 rc = -EINVAL;
11825
11826                                 fprintf(stderr,
11827                                         "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11828                                         progname, PEXT(&chunk->chunk),
11829                                         chunk->mirror_id[0],
11830                                         chunk->mirror_id[i]);
11831                         }
11832                 }
11833
11834                 pos += bytes_read;
11835                 bytes_done += bytes_read;
11836         }
11837
11838         if (verbose > 1 && rc == 0) {
11839                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11840                         PEXT(&chunk->chunk));
11841                 for (i = 0; i < chunk->mirror_count; i++)
11842                         fprintf(stdout, " %u", chunk->mirror_id[i]);
11843                 fprintf(stdout, " PASS\n\n");
11844         }
11845
11846 error:
11847         free(buf);
11848         return rc;
11849 }
11850
11851 /**
11852  * lfs_mirror_verify_file() - Verify a mirrored file.
11853  * @fname:      Mirrored file name.
11854  * @mirror_ids: Specified mirror ids to be verified.
11855  * @ids_nr:     Number of specified mirror ids.
11856  * @verbose:    Verbose mode.
11857  *
11858  * This function verifies that each SYNC mirror of a mirrored file
11859  * specified by @fname contains exactly the same data.
11860  *
11861  * If @mirror_ids is specified, then the function will verify the
11862  * mirrors specified by @mirror_ids contain exactly the same data.
11863  *
11864  * If @verbose is specified, then the function will print where the
11865  * differences are if the data do not match. Otherwise, it will
11866  * just return an error in that case.
11867  *
11868  * Return: 0 on success or a negative error code on failure.
11869  */
11870 static inline
11871 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11872                            int verbose)
11873 {
11874         struct verify_chunk chunks_array[1024] = { };
11875         struct llapi_layout *layout = NULL;
11876         struct stat stbuf;
11877         uint32_t flr_state;
11878         int fd;
11879         int chunk_count = 0;
11880         int idx = 0;
11881         int rc = 0;
11882         int rc1 = 0;
11883         int rc2 = 0;
11884
11885         if (stat(fname, &stbuf) < 0) {
11886                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11887                         progname, fname, strerror(errno));
11888                 rc = -errno;
11889                 goto error;
11890         }
11891
11892         if (!S_ISREG(stbuf.st_mode)) {
11893                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11894                         progname, fname);
11895                 rc = -EINVAL;
11896                 goto error;
11897         }
11898
11899         if (stbuf.st_size == 0) {
11900                 if (verbose)
11901                         fprintf(stdout, "%s: '%s' file size is 0.\n",
11902                                 progname, fname);
11903                 rc = 0;
11904                 goto error;
11905         }
11906
11907         /* Allow mirror verify even without the key on encrypted files */
11908         fd = open(fname, O_DIRECT | O_RDONLY | O_FILE_ENC);
11909         if (fd < 0) {
11910                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11911                         progname, fname, strerror(errno));
11912                 rc = -errno;
11913                 goto error;
11914         }
11915
11916         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11917         if (rc < 0) {
11918                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11919                         progname, fname, strerror(errno));
11920                 goto close_fd;
11921         }
11922
11923         layout = llapi_layout_get_by_fd(fd, 0);
11924         if (!layout) {
11925                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11926                         progname, fname, strerror(errno));
11927                 rc = -errno;
11928                 llapi_lease_release(fd);
11929                 goto close_fd;
11930         }
11931
11932         rc = llapi_layout_flags_get(layout, &flr_state);
11933         if (rc < 0) {
11934                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11935                         progname, fname, strerror(errno));
11936                 rc = -errno;
11937                 goto free_layout;
11938         }
11939
11940         flr_state &= LCM_FL_FLR_MASK;
11941         switch (flr_state) {
11942         case LCM_FL_NONE:
11943                 rc = -EINVAL;
11944                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11945                         progname, fname, llapi_layout_flags_string(flr_state));
11946                 goto free_layout;
11947         default:
11948                 break;
11949         }
11950
11951         /* find out mirror chunks to be verified */
11952         chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11953                                                ARRAY_SIZE(chunks_array));
11954         if (chunk_count < 0) {
11955                 rc = chunk_count;
11956                 goto free_layout;
11957         }
11958
11959         if (ids_nr > 0)
11960                 /* filter specified mirror ids */
11961                 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11962
11963         if (verbose > 2)
11964                 print_chunks(fname, chunks_array, chunk_count);
11965
11966         for (idx = 0; idx < chunk_count; idx++) {
11967                 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11968                         if (verbose)
11969                                 fprintf(stdout,
11970                                         "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11971                                         progname, fname,
11972                                         PEXT(&chunks_array[idx].chunk),
11973                                         (unsigned long long)stbuf.st_size);
11974                         break;
11975                 }
11976
11977                 if (chunks_array[idx].mirror_count == 0) {
11978                         fprintf(stderr,
11979                                 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11980                                 progname, fname,
11981                                 PEXT(&chunks_array[idx].chunk));
11982                         if (verbose) {
11983                                 fprintf(stderr, "skipped\n");
11984                                 continue;
11985                         }
11986                         rc = -EINVAL;
11987                         fprintf(stderr, "failed\n");
11988                         goto free_layout;
11989                 }
11990
11991                 if (chunks_array[idx].mirror_count == 1) {
11992                         if (verbose)
11993                                 fprintf(stdout,
11994                                         "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11995                                         progname, fname,
11996                                         PEXT(&chunks_array[idx].chunk),
11997                                         chunks_array[idx].mirror_id[0]);
11998                         continue;
11999                 }
12000
12001                 rc = llapi_lease_check(fd);
12002                 if (rc != LL_LEASE_RDLCK) {
12003                         fprintf(stderr, "%s: '%s' lost lease lock.\n",
12004                                 progname, fname);
12005                         goto free_layout;
12006                 }
12007
12008                 /* verify one chunk */
12009                 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12010                                               &chunks_array[idx], verbose);
12011                 if (rc1 < 0) {
12012                         rc2 = rc1;
12013                         if (!verbose) {
12014                                 rc = rc1;
12015                                 goto free_layout;
12016                         }
12017                 }
12018         }
12019
12020         if (rc2 < 0)
12021                 rc = rc2;
12022
12023 free_layout:
12024         llapi_layout_free(layout);
12025         llapi_lease_release(fd);
12026 close_fd:
12027         close(fd);
12028 error:
12029         return rc;
12030 }
12031
12032 /**
12033  * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12034  * @argc: The count of lfs mirror verify command line arguments.
12035  * @argv: Array of strings for lfs mirror verify command line arguments.
12036  *
12037  * This function parses lfs mirror verify command and verifies the
12038  * specified mirrored file(s).
12039  *
12040  * Return: 0 on success or a negative error code on failure.
12041  */
12042 static inline int lfs_mirror_verify(int argc, char **argv)
12043 {
12044         __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12045         int ids_nr = 0;
12046         int c;
12047         int verbose = 0;
12048         int rc = 0;
12049         int rc1 = 0;
12050         char cmd[PATH_MAX];
12051
12052         struct option long_opts[] = {
12053         { .val = 'h',   .name = "help",         .has_arg = no_argument },
12054         { .val = 'o',   .name = "only",         .has_arg = required_argument },
12055         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
12056         { .name = NULL } };
12057
12058         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12059         progname = cmd;
12060         while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12061                 switch (c) {
12062                 case 'o':
12063                         rc = parse_mirror_ids(mirror_ids,
12064                                               ARRAY_SIZE(mirror_ids),
12065                                               optarg);
12066                         if (rc < 0) {
12067                                 fprintf(stderr,
12068                                         "%s: bad mirror ids '%s'.\n",
12069                                         progname, optarg);
12070                                 goto error;
12071                         }
12072                         ids_nr = rc;
12073                         if (ids_nr < 2) {
12074                                 fprintf(stderr,
12075                                         "%s: at least 2 mirror ids needed with '--only' option.\n",
12076                                         progname);
12077                                 rc = CMD_HELP;
12078                                 goto error;
12079                         }
12080                         break;
12081                 case 'v':
12082                         verbose++;
12083                         break;
12084                 default:
12085                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12086                                 progname, argv[optind - 1]);
12087                         /* fallthrough */
12088                 case 'h':
12089                         rc = CMD_HELP;
12090                         goto error;
12091                 }
12092         }
12093
12094         if (argc == optind) {
12095                 fprintf(stderr, "%s: no file name given.\n", progname);
12096                 rc = CMD_HELP;
12097                 goto error;
12098         }
12099
12100         if (ids_nr > 0 && argc > optind + 1) {
12101                 fprintf(stderr,
12102                         "%s: '--only' cannot be used upon multiple files.\n",
12103                         progname);
12104                 rc = CMD_HELP;
12105                 goto error;
12106         }
12107
12108         if (ids_nr > 0) {
12109                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12110                 if (rc < 0)
12111                         goto error;
12112         }
12113
12114         rc = 0;
12115         for (; optind < argc; optind++) {
12116                 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12117                                              verbose);
12118                 if (rc1 < 0)
12119                         rc = rc1;
12120         }
12121 error:
12122         return rc;
12123 }
12124
12125 /**
12126  * lfs_mirror() - Parse and execute lfs mirror commands.
12127  * @argc: The count of lfs mirror command line arguments.
12128  * @argv: Array of strings for lfs mirror command line arguments.
12129  *
12130  * This function parses lfs mirror commands and performs the
12131  * corresponding functions specified in mirror_cmdlist[].
12132  *
12133  * Return: 0 on success or an error code on failure.
12134  */
12135 static int lfs_mirror(int argc, char **argv)
12136 {
12137         char cmd[PATH_MAX];
12138         int rc = 0;
12139
12140         setlinebuf(stdout);
12141
12142         Parser_init("lfs-mirror > ", mirror_cmdlist);
12143
12144         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12145         progname = cmd;
12146         program_invocation_short_name = cmd;
12147         if (argc > 1)
12148                 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12149         else
12150                 rc = Parser_commands();
12151
12152         return rc < 0 ? -rc : rc;
12153 }
12154
12155 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12156 {
12157 #if __BYTE_ORDER == __BIG_ENDIAN
12158         __swab16s(&attrs->lsa_valid);
12159         __swab64s(&attrs->lsa_size);
12160         __swab64s(&attrs->lsa_blocks);
12161 #endif
12162 }
12163
12164 enum lfs_som_type {
12165         LFS_SOM_SIZE = 0x1,
12166         LFS_SOM_BLOCKS = 0x2,
12167         LFS_SOM_FLAGS = 0x4,
12168         LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12169                            LFS_SOM_FLAGS,
12170 };
12171
12172 static int lfs_getsom(int argc, char **argv)
12173 {
12174         const char *path;
12175         struct lustre_som_attrs *attrs;
12176         char buf[sizeof(*attrs) + 64];
12177         enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12178         int rc = 0, c;
12179
12180         while ((c = getopt(argc, argv, "bfhs")) != -1) {
12181                 switch (c) {
12182                 case 'b':
12183                         type = LFS_SOM_BLOCKS;
12184                         break;
12185                 case 'f':
12186                         type = LFS_SOM_FLAGS;
12187                         break;
12188                 case 's':
12189                         type = LFS_SOM_SIZE;
12190                         break;
12191                 default:
12192                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12193                                 progname, argv[optind - 1]);
12194                         /* fallthrough */
12195                 case 'h':
12196                         return CMD_HELP;
12197                 }
12198         }
12199
12200         argc -= optind;
12201         argv += optind;
12202
12203         if (argc != 1) {
12204                 fprintf(stderr, "%s: %s\n",
12205                         progname, argc == 0 ? "miss file target" :
12206                         "input more than 2 files");
12207                 return CMD_HELP;
12208         }
12209
12210         path = argv[0];
12211         attrs = (void *)buf;
12212         rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12213         if (rc < 0) {
12214                 rc = -errno;
12215                 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12216                         argv[0], strerror(errno), errno);
12217                 return rc;
12218         }
12219
12220         lustre_som_swab(attrs);
12221
12222         switch (type) {
12223         case LFS_SOM_ATTR_ALL:
12224                 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12225                        path, (unsigned long long)attrs->lsa_size,
12226                        (unsigned long long)attrs->lsa_blocks,
12227                        attrs->lsa_valid);
12228                 break;
12229         case LFS_SOM_SIZE:
12230                 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12231                 break;
12232         case LFS_SOM_BLOCKS:
12233                 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12234                 break;
12235         case LFS_SOM_FLAGS:
12236                 printf("%x\n", attrs->lsa_valid);
12237                 break;
12238         default:
12239                 fprintf(stderr, "%s: unknown option\n", progname);
12240                 return CMD_HELP;
12241         }
12242
12243         return 0;
12244 }
12245
12246 /**
12247  * lfs_mirror_list_commands() - List lfs mirror commands.
12248  * @argc: The count of command line arguments.
12249  * @argv: Array of strings for command line arguments.
12250  *
12251  * This function lists lfs mirror commands defined in mirror_cmdlist[].
12252  *
12253  * Return: 0 on success.
12254  */
12255 static int lfs_mirror_list_commands(int argc, char **argv)
12256 {
12257         char buffer[81] = "";
12258
12259         Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12260                              NULL, 0, 4);
12261
12262         return 0;
12263 }
12264
12265 static int lfs_pcc_attach(int argc, char **argv)
12266 {
12267         struct option long_opts[] = {
12268         { .val = 'h',   .name = "help", .has_arg = no_argument },
12269         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12270         { .name = NULL } };
12271         int c;
12272         int rc = 0;
12273         __u32 archive_id = 0;
12274         const char *path;
12275         char *end;
12276         char fullpath[PATH_MAX];
12277         enum lu_pcc_type type = LU_PCC_READWRITE;
12278
12279         optind = 0;
12280         while ((c = getopt_long(argc, argv, "hi:",
12281                                 long_opts, NULL)) != -1) {
12282                 switch (c) {
12283                 case 'i':
12284                         errno = 0;
12285                         archive_id = strtoul(optarg, &end, 0);
12286                         if (errno != 0 || *end != '\0' ||
12287                             archive_id == 0 || archive_id > UINT32_MAX) {
12288                                 fprintf(stderr,
12289                                         "error: %s: bad archive ID '%s'\n",
12290                                         progname, optarg);
12291                                 return CMD_HELP;
12292                         }
12293                         break;
12294                 default:
12295                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12296                                 progname, argv[optind - 1]);
12297                         /* fallthrough */
12298                 case 'h':
12299                         return CMD_HELP;
12300                 }
12301         }
12302
12303         if (archive_id == 0) {
12304                 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12305                 return CMD_HELP;
12306         }
12307
12308         if (argc <= optind) {
12309                 fprintf(stderr, "%s: must specify one or more file names\n",
12310                         argv[0]);
12311                 return CMD_HELP;
12312         }
12313
12314         while (optind < argc) {
12315                 int rc2;
12316
12317                 path = argv[optind++];
12318                 if (!realpath(path, fullpath)) {
12319                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12320                                 argv[0], path, strerror(errno));
12321                         if (rc == 0)
12322                                 rc = -EINVAL;
12323                         continue;
12324                 }
12325
12326                 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12327                 if (rc2 < 0) {
12328                         fprintf(stderr,
12329                                 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12330                                 argv[0], path, archive_id, strerror(-rc2));
12331                         if (rc == 0)
12332                                 rc = rc2;
12333                 }
12334         }
12335         return rc;
12336 }
12337
12338 static int lfs_pcc_attach_fid(int argc, char **argv)
12339 {
12340         struct option long_opts[] = {
12341         { .val = 'h',   .name = "help", .has_arg = no_argument },
12342         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12343         { .val = 'm',   .name = "mnt",  .has_arg = required_argument },
12344         { .name = NULL } };
12345         int c;
12346         int rc = 0;
12347         __u32 archive_id = 0;
12348         char *end;
12349         const char *mntpath = NULL;
12350         const char *fidstr;
12351         enum lu_pcc_type type = LU_PCC_READWRITE;
12352
12353         optind = 0;
12354         while ((c = getopt_long(argc, argv, "hi:m:",
12355                                 long_opts, NULL)) != -1) {
12356                 switch (c) {
12357                 case 'i':
12358                         errno = 0;
12359                         archive_id = strtoul(optarg, &end, 0);
12360                         if (errno != 0 || *end != '\0' ||
12361                             archive_id > UINT32_MAX) {
12362                                 fprintf(stderr,
12363                                         "error: %s: bad archive ID '%s'\n",
12364                                         argv[0], optarg);
12365                                 return CMD_HELP;
12366                         }
12367                         break;
12368                 case 'm':
12369                         mntpath = optarg;
12370                         break;
12371                 default:
12372                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12373                                 progname, argv[optind - 1]);
12374                         /* fallthrough */
12375                 case 'h':
12376                         return CMD_HELP;
12377                 }
12378         }
12379
12380         if (archive_id == 0) {
12381                 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12382                 return CMD_HELP;
12383         }
12384
12385         if (!mntpath) {
12386                 fprintf(stderr, "%s: must specify Lustre mount point\n",
12387                         argv[0]);
12388                 return CMD_HELP;
12389         }
12390
12391         if (argc <= optind) {
12392                 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12393                 return CMD_HELP;
12394         }
12395
12396         while (optind < argc) {
12397                 int rc2;
12398
12399                 fidstr = argv[optind++];
12400
12401                 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12402                                                archive_id, type);
12403                 if (rc2 < 0) {
12404                         fprintf(stderr,
12405                                 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12406                                 argv[0], fidstr, mntpath, archive_id,
12407                                 strerror(rc2));
12408                 }
12409                 if (rc == 0 && rc2 < 0)
12410                         rc = rc2;
12411         }
12412         return rc;
12413 }
12414
12415 static int lfs_pcc_detach(int argc, char **argv)
12416 {
12417         struct option long_opts[] = {
12418         { .val = 'h',   .name = "help", .has_arg = no_argument },
12419         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12420         { .name = NULL } };
12421         int c;
12422         int rc = 0;
12423         const char *path;
12424         char fullpath[PATH_MAX];
12425         __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12426
12427         optind = 0;
12428         while ((c = getopt_long(argc, argv, "hk",
12429                                 long_opts, NULL)) != -1) {
12430                 switch (c) {
12431                 case 'k':
12432                         detach_opt = PCC_DETACH_OPT_NONE;
12433                         break;
12434                 default:
12435                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12436                                 progname, argv[optind - 1]);
12437                         /* fallthrough */
12438                 case 'h':
12439                         return CMD_HELP;
12440                 }
12441         }
12442
12443         while (optind < argc) {
12444                 int rc2;
12445
12446                 path = argv[optind++];
12447                 if (!realpath(path, fullpath)) {
12448                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12449                                 argv[0], path, strerror(errno));
12450                         if (rc == 0)
12451                                 rc = -EINVAL;
12452                         continue;
12453                 }
12454
12455                 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12456                 if (rc2 < 0) {
12457                         rc2 = -errno;
12458                         fprintf(stderr,
12459                                 "%s: cannot detach '%s' from PCC: %s\n",
12460                                 argv[0], path, strerror(errno));
12461                         if (rc == 0)
12462                                 rc = rc2;
12463                 }
12464         }
12465         return rc;
12466 }
12467
12468 static int lfs_pcc_detach_fid(int argc, char **argv)
12469 {
12470         struct option long_opts[] = {
12471         { .val = 'h',   .name = "help", .has_arg = no_argument },
12472         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12473         { .name = NULL } };
12474         int c;
12475         int rc = 0;
12476         const char *fid;
12477         const char *mntpath;
12478         __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12479
12480         optind = 0;
12481         while ((c = getopt_long(argc, argv, "hk",
12482                                 long_opts, NULL)) != -1) {
12483                 switch (c) {
12484                 case 'k':
12485                         detach_opt = PCC_DETACH_OPT_NONE;
12486                         break;
12487                 default:
12488                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12489                                 progname, argv[optind - 1]);
12490                         /* fallthrough */
12491                 case 'h':
12492                         return CMD_HELP;
12493                 }
12494         }
12495
12496         mntpath = argv[optind++];
12497
12498         while (optind < argc) {
12499                 int rc2;
12500
12501                 fid = argv[optind++];
12502
12503                 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12504                 if (rc2 < 0) {
12505                         fprintf(stderr,
12506                                 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12507                                 argv[0], fid, mntpath, strerror(-rc2));
12508                         if (rc == 0)
12509                                 rc = rc2;
12510                 }
12511         }
12512         return rc;
12513 }
12514
12515 static int lfs_pcc_state(int argc, char **argv)
12516 {
12517         int rc = 0;
12518         const char *path;
12519         char fullpath[PATH_MAX];
12520         struct lu_pcc_state state;
12521
12522         optind = 1;
12523
12524         if (argc <= 1) {
12525                 fprintf(stderr, "%s: must specify one or more file names\n",
12526                         progname);
12527                 return CMD_HELP;
12528         }
12529
12530         while (optind < argc) {
12531                 int rc2;
12532
12533                 path = argv[optind++];
12534                 if (!realpath(path, fullpath)) {
12535                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12536                                 argv[0], path, strerror(errno));
12537                         if (rc == 0)
12538                                 rc = -EINVAL;
12539                         continue;
12540                 }
12541
12542                 rc2 = llapi_pcc_state_get(fullpath, &state);
12543                 if (rc2 < 0) {
12544                         if (rc == 0)
12545                                 rc = rc2;
12546                         fprintf(stderr,
12547                                 "%s: cannot get PCC state of '%s': %s\n",
12548                                 argv[0], path, strerror(-rc2));
12549                         continue;
12550                 }
12551
12552                 printf("file: %s", path);
12553                 printf(", type: %s", pcc_type2string(state.pccs_type));
12554                 if (state.pccs_type == LU_PCC_NONE &&
12555                     state.pccs_open_count == 0) {
12556                         printf("\n");
12557                         continue;
12558                 }
12559
12560                 printf(", PCC file: %s", state.pccs_path);
12561                 printf(", user number: %u", state.pccs_open_count);
12562                 printf(", flags: %x", state.pccs_flags);
12563                 printf("\n");
12564         }
12565         return rc;
12566 }
12567
12568 /**
12569  * lfs_pcc_list_commands() - List lfs pcc commands.
12570  * @argc: The count of command line arguments.
12571  * @argv: Array of strings for command line arguments.
12572  *
12573  * This function lists lfs pcc commands defined in pcc_cmdlist[].
12574  *
12575  * Return: 0 on success.
12576  */
12577 static int lfs_pcc_list_commands(int argc, char **argv)
12578 {
12579         char buffer[81] = "";
12580
12581         Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12582                              NULL, 0, 4);
12583
12584         return 0;
12585 }
12586
12587 /**
12588  * lfs_pcc() - Parse and execute lfs pcc commands.
12589  * @argc: The count of lfs pcc command line arguments.
12590  * @argv: Array of strings for lfs pcc command line arguments.
12591  *
12592  * This function parses lfs pcc commands and performs the
12593  * corresponding functions specified in pcc_cmdlist[].
12594  *
12595  * Return: 0 on success or an error code on failure.
12596  */
12597 static int lfs_pcc(int argc, char **argv)
12598 {
12599         char cmd[PATH_MAX];
12600         int rc = 0;
12601
12602         setlinebuf(stdout);
12603
12604         Parser_init("lfs-pcc > ", pcc_cmdlist);
12605
12606         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12607         progname = cmd;
12608         program_invocation_short_name = cmd;
12609         if (argc > 1)
12610                 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12611         else
12612                 rc = Parser_commands();
12613
12614         return rc < 0 ? -rc : rc;
12615 }
12616
12617 static int lfs_list_commands(int argc, char **argv)
12618 {
12619         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12620
12621         Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12622
12623         return 0;
12624 }
12625
12626 int main(int argc, char **argv)
12627 {
12628         int rc;
12629
12630         /* Ensure that liblustreapi constructor has run */
12631         if (!llapi_liblustreapi_initialized())
12632                 fprintf(stderr, "liblustreapi was not properly initialized\n");
12633
12634         setlinebuf(stdout);
12635         opterr = 0;
12636
12637         Parser_init("lfs > ", cmdlist);
12638
12639         progname = program_invocation_short_name; /* Used in error messages */
12640         if (argc > 1) {
12641                 llapi_set_command_name(argv[1]);
12642                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12643                 llapi_clear_command_name();
12644         } else {
12645                 rc = Parser_commands();
12646         }
12647
12648         return rc < 0 ? -rc : rc;
12649 }
12650
12651 #ifdef _LUSTRE_IDL_H_
12652 /* Everything we need here should be included by lustreapi.h. */
12653 # error "lfs should not depend on lustre_idl.h"
12654 #endif /* _LUSTRE_IDL_H_ */