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