Whamcloud - gitweb
1c775603d6501f8a98b4b7e61d7fc0b147e3258e
[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, 2016, 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 <getopt.h>
47 #include <string.h>
48 #include <mntent.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60 #include <dirent.h>
61 #include <time.h>
62 #include <ctype.h>
63
64 #include <libcfs/util/string.h>
65 #include <libcfs/util/ioctl.h>
66 #include <libcfs/util/parser.h>
67 #include <lustre/lustreapi.h>
68 #include <lustre_ver.h>
69 #include <linux/lustre_param.h>
70
71 #ifndef ARRAY_SIZE
72 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
73 #endif /* !ARRAY_SIZE */
74
75 /* all functions */
76 static int lfs_setstripe(int argc, char **argv);
77 static int lfs_find(int argc, char **argv);
78 static int lfs_getstripe(int argc, char **argv);
79 static int lfs_getdirstripe(int argc, char **argv);
80 static int lfs_setdirstripe(int argc, char **argv);
81 static int lfs_rmentry(int argc, char **argv);
82 static int lfs_osts(int argc, char **argv);
83 static int lfs_mdts(int argc, char **argv);
84 static int lfs_df(int argc, char **argv);
85 static int lfs_getname(int argc, char **argv);
86 static int lfs_check(int argc, char **argv);
87 #ifdef HAVE_SYS_QUOTA_H
88 static int lfs_setquota(int argc, char **argv);
89 static int lfs_quota(int argc, char **argv);
90 #endif
91 static int lfs_flushctx(int argc, char **argv);
92 static int lfs_cp(int argc, char **argv);
93 static int lfs_ls(int argc, char **argv);
94 static int lfs_poollist(int argc, char **argv);
95 static int lfs_changelog(int argc, char **argv);
96 static int lfs_changelog_clear(int argc, char **argv);
97 static int lfs_fid2path(int argc, char **argv);
98 static int lfs_path2fid(int argc, char **argv);
99 static int lfs_data_version(int argc, char **argv);
100 static int lfs_hsm_state(int argc, char **argv);
101 static int lfs_hsm_set(int argc, char **argv);
102 static int lfs_hsm_clear(int argc, char **argv);
103 static int lfs_hsm_action(int argc, char **argv);
104 static int lfs_hsm_archive(int argc, char **argv);
105 static int lfs_hsm_restore(int argc, char **argv);
106 static int lfs_hsm_release(int argc, char **argv);
107 static int lfs_hsm_remove(int argc, char **argv);
108 static int lfs_hsm_cancel(int argc, char **argv);
109 static int lfs_swap_layouts(int argc, char **argv);
110 static int lfs_mv(int argc, char **argv);
111 static int lfs_ladvise(int argc, char **argv);
112 static int lfs_list_commands(int argc, char **argv);
113
114 /* Setstripe and migrate share mostly the same parameters */
115 #define SSM_CMD_COMMON(cmd) \
116         "usage: "cmd" [--stripe-count|-c <stripe_count>]\n"             \
117         "                 [--stripe-index|-i <start_ost_idx>]\n"        \
118         "                 [--stripe-size|-S <stripe_size>]\n"           \
119         "                 [--pool|-p <pool_name>]\n"                    \
120         "                 [--ost|-o <ost_indices>]\n"                   \
121         "                 [--component-end|-E <comp_end>]\n"
122
123 #define SSM_HELP_COMMON \
124         "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n" \
125         "\t              Can be specified with k, m or g (in KB, MB and GB\n" \
126         "\t              respectively)\n"                               \
127         "\tstart_ost_idx: OST index of first stripe (-1 default)\n"     \
128         "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n" \
129         "\tpool_name:    Name of OST pool to use (default none)\n"      \
130         "\tost_indices:  List of OST indices, can be repeated multiple times\n"\
131         "\t              Indices be specified in a format of:\n"        \
132         "\t                -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n"        \
133         "\t              Or:\n"                                         \
134         "\t                -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n"  \
135         "\t              If --pool is set with --ost, then the OSTs\n"  \
136         "\t              must be the members of the pool."              \
137         "\tcomp_end:     Extent end of the component\n"                 \
138         "\t              Can be specified with k, m or g (in KB, MB and GB\n" \
139         "\t              respectively, -1 for EOF), it must be aligned with\n"\
140         "\t              the stripe_size\n"
141
142 #define SETSTRIPE_USAGE                                         \
143         SSM_CMD_COMMON("setstripe")                             \
144         "                 <directory|filename>\n"               \
145         SSM_HELP_COMMON                                         \
146
147 #define MIGRATE_USAGE                                                   \
148         SSM_CMD_COMMON("migrate  ")                                     \
149         "                 [--block|-b]\n"                               \
150         "                 [--non-block|-n]\n"                           \
151         "                 <filename>\n"                                 \
152         SSM_HELP_COMMON                                                 \
153         "\n"                                                            \
154         "\tblock:        Block file access during data migration (default)\n" \
155         "\tnon-block:    Abort migrations if concurrent access is detected\n" \
156
157 #define SETDIRSTRIPE_USAGE                                      \
158         "               [--mdt-count|-c stripe_count>\n"        \
159         "               [--mdt-index|-i mdt_index]\n"           \
160         "               [--mdt-hash|-H mdt_hash]\n"             \
161         "               [--default|-D] [--mode|-m mode] <dir>\n"        \
162         "\tstripe_count: stripe count of the striped directory\n"       \
163         "\tmdt_index: MDT index of first stripe\n"                      \
164         "\tmdt_hash:  hash type of the striped directory. mdt types:\n" \
165         "       fnv_1a_64 FNV-1a hash algorithm (default)\n"            \
166         "       all_char  sum of characters % MDT_COUNT (not recommended)\n" \
167         "\tdefault_stripe: set default dirstripe of the directory\n"    \
168         "\tmode: the mode of the directory\n"
169
170 static const char       *progname;
171 static bool              file_lease_supported = true;
172
173 /* all available commands */
174 command_t cmdlist[] = {
175         {"setstripe", lfs_setstripe, 0,
176          "Create a new file with a specific striping pattern or\n"
177          "set the default striping pattern on an existing directory or\n"
178          "delete the default striping pattern from an existing directory or\n"
179          "add layout component(s) to an existing composite file or\n"
180          "delete specified component(s) from an existing composite file\n\n"
181          "To delete default striping from an existing directory:\n"
182          "usage: setstripe -d <directory>\n"
183          " or\n"
184          "To delete component(s) from an existing composite file:\n"
185          "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
186          "                               [--component-flags|-F <comp_flags>]\n"
187          "                               <filename>\n"
188          "\tcomp_id:     Unique component ID\n"
189          "\tcomp_flags:  'init' indicating all instantiated components\n"
190          "\t             '^init' indicating all uninstantiated components\n"
191          "\t-I and -F can't be specified at the same time\n"
192          " or\n"
193          "To add component(s) to an existing composite file:\n"
194          SSM_CMD_COMMON("setstripe --component-add")
195          " or\n"
196          "To create a file with specified striping/composite layout:\n"
197          SETSTRIPE_USAGE},
198         {"getstripe", lfs_getstripe, 0,
199          "To list the striping info for a given file or files in a\n"
200          "directory or recursively for all files in a directory tree.\n"
201          "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
202          "                 [--stripe-count|-c] [--stripe-index|-i]\n"
203          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
204          "                 [--mdt|-m] [--recursive|-r] [--raw|-R] [--yaml|-y]\n"
205          "                 [--layout|-L] [--fid|-F] [--generation|-g]\n"
206          "                 [--component-id[=comp_id]|-I[comp_id]]\n"
207          "                 [--component-flags[=comp_flags]]\n"
208          "                 [--component-count]\n"
209          "                 [--component-start[=[+-]comp_start]]\n"
210          "                 [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
211          "                 <directory|filename> ..."},
212         {"setdirstripe", lfs_setdirstripe, 0,
213          "To create a striped directory on a specified MDT. This can only\n"
214          "be done on MDT0 with the right of administrator.\n"
215          "usage: setdirstripe [OPTION] <directory>\n"
216          SETDIRSTRIPE_USAGE},
217         {"getdirstripe", lfs_getdirstripe, 0,
218          "To list the striping info for a given directory\n"
219          "or recursively for all directories in a directory tree.\n"
220          "usage: getdirstripe [--obd|-O <uuid>] [--mdt-count|-c]\n"
221          "                    [--mdt-index|-i] [--mdt-hash|-t]\n"
222          "                    [--recursive|-r] [--yaml|-y]\n"
223          "                    [--default|-D] <dir> ..."},
224         {"mkdir", lfs_setdirstripe, 0,
225          "To create a striped directory on a specified MDT. This can only\n"
226          "be done on MDT0 with the right of administrator.\n"
227          "usage: mkdir [OPTION] <directory>\n"
228          SETDIRSTRIPE_USAGE},
229         {"rm_entry", lfs_rmentry, 0,
230          "To remove the name entry of the remote directory. Note: This\n"
231          "command will only delete the name entry, i.e. the remote directory\n"
232          "will become inaccessable after this command. This can only be done\n"
233          "by the administrator\n"
234          "usage: rm_entry <dir>\n"},
235         {"pool_list", lfs_poollist, 0,
236          "List pools or pool OSTs\n"
237          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
238         {"find", lfs_find, 0,
239          "find files matching given attributes recursively in directory tree.\n"
240          "usage: find <directory|filename> ...\n"
241          "     [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
242          "     [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
243          "     [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
244          "     [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
245          "     [[!] --size|-s [+-]N[bkMGTPE]]\n"
246          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
247          "     [[!] --stripe-index|-i <index,...>]\n"
248          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
249          "     [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
250          "     [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
251          "     [[!] --projid <projid>]\n"
252          "     [[!] --layout|-L released,raid0]\n"
253          "     [[!] --component-count [+-]<comp_cnt>]\n"
254          "     [[!] --component-start [+-]N[kMGTPE]]\n"
255          "     [[!] --component-end|-E [+-]N[kMGTPE]]\n"
256          "     [[!] --component-flags <comp_flags>]\n"
257          "     [[!] --mdt-count|-T [+-]<stripes>]\n"
258          "     [[!] --mdt-hash|-H <hashtype>\n"
259          "\t !: used before an option indicates 'NOT' requested attribute\n"
260          "\t -: used before a value indicates less than requested value\n"
261          "\t +: used before a value indicates more than requested value\n"
262          "\tmdt-hash:   hash type of the striped directory.\n"
263          "\t            fnv_1a_64 FNV-1a hash algorithm\n"
264          "\t            all_char  sum of characters % MDT_COUNT\n"},
265         {"check", lfs_check, 0,
266          "Display the status of MDS or OSTs (as specified in the command)\n"
267          "or all the servers (MDS and OSTs).\n"
268          "usage: check <osts|mds|servers>"},
269         {"osts", lfs_osts, 0, "list OSTs connected to client "
270          "[for specified path only]\n" "usage: osts [path]"},
271         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
272          "[for specified path only]\n" "usage: mdts [path]"},
273         {"df", lfs_df, 0,
274          "report filesystem disk space usage or inodes usage"
275          "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
276          "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
277         {"getname", lfs_getname, 0, "list instances and specified mount points "
278          "[for specified path only]\n"
279          "Usage: getname [-h]|[path ...] "},
280 #ifdef HAVE_SYS_QUOTA_H
281         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
282          "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
283          "                -b <block-softlimit> -B <block-hardlimit>\n"
284          "                -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
285          "       setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
286          "                [--block-softlimit <block-softlimit>]\n"
287          "                [--block-hardlimit <block-hardlimit>]\n"
288          "                [--inode-softlimit <inode-softlimit>]\n"
289          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
290          "       setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
291          "                [--block-grace <block-grace>]\n"
292          "                [--inode-grace <inode-grace>] <filesystem>\n"
293          "       -b can be used instead of --block-softlimit/--block-grace\n"
294          "       -B can be used instead of --block-hardlimit\n"
295          "       -i can be used instead of --inode-softlimit/--inode-grace\n"
296          "       -I can be used instead of --inode-hardlimit\n\n"
297          "Note: The total quota space will be split into many qunits and\n"
298          "      balanced over all server targets, the minimal qunit size is\n"
299          "      1M bytes for block space and 1K inodes for inode space.\n\n"
300          "      Quota space rebalancing process will stop when this mininum\n"
301          "      value is reached. As a result, quota exceeded can be returned\n"
302          "      while many targets still have 1MB or 1K inodes of spare\n"
303          "      quota space."},
304         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
305          "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
306                        "<ost_idx>]\n"
307          "             [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
308          "       quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
309 #endif
310         {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
311          "usage: flushctx [-k] [mountpoint...]"},
312         {"cp", lfs_cp, 0,
313          "Remote user copy files and directories.\n"
314          "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
315         {"ls", lfs_ls, 0,
316          "Remote user list directory contents.\n"
317          "usage: ls [OPTION]... [FILE]..."},
318         {"changelog", lfs_changelog, 0,
319          "Show the metadata changes on an MDT."
320          "\nusage: changelog <mdtname> [startrec [endrec]]"},
321         {"changelog_clear", lfs_changelog_clear, 0,
322          "Indicate that old changelog records up to <endrec> are no longer of "
323          "interest to consumer <id>, allowing the system to free up space.\n"
324          "An <endrec> of 0 means all records.\n"
325          "usage: changelog_clear <mdtname> <id> <endrec>"},
326         {"fid2path", lfs_fid2path, 0,
327          "Resolve the full path(s) for given FID(s). For a specific hardlink "
328          "specify link number <linkno>.\n"
329         /* "For a historical link name, specify changelog record <recno>.\n" */
330          "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
331                 /* [ --rec <recno> ] */ },
332         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
333          "usage: path2fid [--parents] <path> ..."},
334         {"data_version", lfs_data_version, 0, "Display file data version for "
335          "a given path.\n" "usage: data_version -[n|r|w] <path>"},
336         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
337          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
338         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
339          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
340          "[--archived] [--lost] <file> ..."},
341         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
342          "files.\n"
343          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
344          "[--archived] [--lost] <file> ..."},
345         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
346          "given files.\n" "usage: hsm_action <file> ..."},
347         {"hsm_archive", lfs_hsm_archive, 0,
348          "Archive file to external storage.\n"
349          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
350          "<file> ..."},
351         {"hsm_restore", lfs_hsm_restore, 0,
352          "Restore file from external storage.\n"
353          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
354         {"hsm_release", lfs_hsm_release, 0,
355          "Release files from Lustre.\n"
356          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
357         {"hsm_remove", lfs_hsm_remove, 0,
358          "Remove file copy from external storage.\n"
359          "usage: hsm_remove [--filelist FILELIST] [--data DATA]\n"
360          "                  [--mntpath MOUNTPATH] [--archive NUM] <file|FID> ...\n"
361          "\n"
362          "Note: To remove files from the archive that have been deleted on\n"
363          "Lustre, set mntpath and optionally archive. In that case, all the\n"
364          "positional arguments and entries in the file list must be FIDs."
365         },
366         {"hsm_cancel", lfs_hsm_cancel, 0,
367          "Cancel requests related to specified files.\n"
368          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
369         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
370          "usage: swap_layouts <path1> <path2>"},
371         {"migrate", lfs_setstripe, 0,
372          "migrate a directory between MDTs.\n"
373          "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
374          "<directory>\n"
375          "\tmdt_idx:      index of the destination MDT\n"
376          "\n"
377          "migrate file objects from one OST "
378          "layout\nto another (may be not safe with concurent writes).\n"
379          "usage: migrate  "
380          "[--stripe-count|-c] <stripe_count>\n"
381          "              [--stripe-index|-i] <start_ost_index>\n"
382          "              [--stripe-size|-S] <stripe_size>\n"
383          "              [--pool|-p] <pool_name>\n"
384          "              [--ost-list|-o] <ost_indices>\n"
385          "              [--block|-b]\n"
386          "              [--non-block|-n]\n"
387          "              <file|directory>\n"
388          "\tstripe_count:     number of OSTs to stripe a file over\n"
389          "\tstripe_ost_index: index of the first OST to stripe a file over\n"
390          "\tstripe_size:      number of bytes to store before moving to the next OST\n"
391          "\tpool_name:        name of the predefined pool of OSTs\n"
392          "\tost_indices:      OSTs to stripe over, in order\n"
393          "\tblock:            wait for the operation to return before continuing\n"
394          "\tnon-block:        do not wait for the operation to return.\n"},
395         {"mv", lfs_mv, 0,
396          "To move directories between MDTs. This command is deprecated, "
397          "use \"migrate\" instead.\n"
398          "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
399          "[--verbose|-v]\n"},
400         {"ladvise", lfs_ladvise, 0,
401          "Provide servers with advice about access patterns for a file.\n"
402          "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
403          "               [--background|-b]\n"
404          "               {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
405          "               <file> ...\n"},
406         {"help", Parser_help, 0, "help"},
407         {"exit", Parser_quit, 0, "quit"},
408         {"quit", Parser_quit, 0, "quit"},
409         {"--version", Parser_version, 0,
410          "output build version of the utility and exit"},
411         {"--list-commands", lfs_list_commands, 0,
412          "list commands supported by the utility and exit"},
413         { 0, 0, 0, NULL }
414 };
415
416
417 #define MIGRATION_NONBLOCK      1
418
419 static int check_hashtype(const char *hashtype)
420 {
421         int i;
422
423         for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
424                 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
425                         return i;
426
427         return 0;
428 }
429
430 /**
431  * Internal helper for migrate_copy_data(). Check lease and report error if
432  * need be.
433  *
434  * \param[in]  fd           File descriptor on which to check the lease.
435  * \param[out] lease_broken Set to true if the lease was broken.
436  * \param[in]  group_locked Whether a group lock was taken or not.
437  * \param[in]  path         Name of the file being processed, for error
438  *                          reporting
439  *
440  * \retval 0       Migration can keep on going.
441  * \retval -errno  Error occurred, abort migration.
442  */
443 static int check_lease(int fd, bool *lease_broken, bool group_locked,
444                        const char *path)
445 {
446         int rc;
447
448         if (!file_lease_supported)
449                 return 0;
450
451         rc = llapi_lease_check(fd);
452         if (rc > 0)
453                 return 0; /* llapi_check_lease returns > 0 on success. */
454
455         if (!group_locked) {
456                 fprintf(stderr, "%s: cannot migrate '%s': file busy\n",
457                         progname, path);
458                 rc = rc ? rc : -EAGAIN;
459         } else {
460                 fprintf(stderr, "%s: external attempt to access file '%s' "
461                         "blocked until migration ends.\n", progname, path);
462                 rc = 0;
463         }
464         *lease_broken = true;
465         return rc;
466 }
467
468 static int migrate_copy_data(int fd_src, int fd_dst, size_t buf_size,
469                              bool group_locked, const char *fname)
470 {
471         void    *buf = NULL;
472         ssize_t  rsize = -1;
473         ssize_t  wsize = 0;
474         size_t   rpos = 0;
475         size_t   wpos = 0;
476         off_t    bufoff = 0;
477         int      rc;
478         bool     lease_broken = false;
479
480         /* Use a page-aligned buffer for direct I/O */
481         rc = posix_memalign(&buf, getpagesize(), buf_size);
482         if (rc != 0)
483                 return -rc;
484
485         while (1) {
486                 /* read new data only if we have written all
487                  * previously read data */
488                 if (wpos == rpos) {
489                         if (!lease_broken) {
490                                 rc = check_lease(fd_src, &lease_broken,
491                                                  group_locked, fname);
492                                 if (rc < 0)
493                                         goto out;
494                         }
495                         rsize = read(fd_src, buf, buf_size);
496                         if (rsize < 0) {
497                                 rc = -errno;
498                                 fprintf(stderr, "%s: %s: read failed: %s\n",
499                                         progname, fname, strerror(-rc));
500                                 goto out;
501                         }
502                         rpos += rsize;
503                         bufoff = 0;
504                 }
505                 /* eof ? */
506                 if (rsize == 0)
507                         break;
508
509                 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
510                 if (wsize < 0) {
511                         rc = -errno;
512                         fprintf(stderr,
513                                 "%s: %s: write failed on volatile: %s\n",
514                                 progname, fname, strerror(-rc));
515                         goto out;
516                 }
517                 wpos += wsize;
518                 bufoff += wsize;
519         }
520
521         rc = fsync(fd_dst);
522         if (rc < 0) {
523                 rc = -errno;
524                 fprintf(stderr, "%s: %s: fsync failed: %s\n",
525                         progname, fname, strerror(-rc));
526         }
527
528 out:
529         free(buf);
530         return rc;
531 }
532
533 static int migrate_copy_timestamps(int fdv, const struct stat *st)
534 {
535         struct timeval  tv[2] = {
536                 {.tv_sec = st->st_atime},
537                 {.tv_sec = st->st_mtime}
538         };
539
540         return futimes(fdv, tv);
541 }
542
543 static int migrate_block(int fd, int fdv, const struct stat *st,
544                          size_t buf_size, const char *name)
545 {
546         __u64   dv1;
547         int     gid;
548         int     rc;
549         int     rc2;
550
551         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
552         if (rc < 0) {
553                 fprintf(stderr, "%s: %s: cannot get dataversion: %s\n",
554                         progname, name, strerror(-rc));
555                 return rc;
556         }
557
558         do
559                 gid = random();
560         while (gid == 0);
561
562         /* The grouplock blocks all concurrent accesses to the file.
563          * It has to be taken after llapi_get_data_version as it would
564          * block it too. */
565         rc = llapi_group_lock(fd, gid);
566         if (rc < 0) {
567                 fprintf(stderr, "%s: %s: cannot get group lock: %s\n",
568                         progname, name, strerror(-rc));
569                 return rc;
570         }
571
572         rc = migrate_copy_data(fd, fdv, buf_size, true, name);
573         if (rc < 0) {
574                 fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
575                 goto out_unlock;
576         }
577
578         /* Make sure we keep original atime/mtime values */
579         rc = migrate_copy_timestamps(fdv, st);
580         if (rc < 0) {
581                 fprintf(stderr, "%s: %s: timestamp copy failed\n",
582                         progname, name);
583                 goto out_unlock;
584         }
585
586         /* swap layouts
587          * for a migration we need to check data version on file did
588          * not change.
589          *
590          * Pass in gid=0 since we already own grouplock. */
591         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
592                                            SWAP_LAYOUTS_CHECK_DV1);
593         if (rc == -EAGAIN) {
594                 fprintf(stderr, "%s: %s: dataversion changed during copy, "
595                         "migration aborted\n", progname, name);
596                 goto out_unlock;
597         } else if (rc < 0) {
598                 fprintf(stderr, "%s: %s: cannot swap layouts: %s\n", progname,
599                         name, strerror(-rc));
600                 goto out_unlock;
601         }
602
603 out_unlock:
604         rc2 = llapi_group_unlock(fd, gid);
605         if (rc2 < 0 && rc == 0) {
606                 fprintf(stderr, "%s: %s: putting group lock failed: %s\n",
607                         progname, name, strerror(-rc2));
608                 rc = rc2;
609         }
610
611         return rc;
612 }
613
614 static int migrate_nonblock(int fd, int fdv, const struct stat *st,
615                             size_t buf_size, const char *name)
616 {
617         __u64   dv1;
618         __u64   dv2;
619         int     rc;
620
621         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
622         if (rc < 0) {
623                 fprintf(stderr, "%s: %s: cannot get data version: %s\n",
624                         progname, name, strerror(-rc));
625                 return rc;
626         }
627
628         rc = migrate_copy_data(fd, fdv, buf_size, false, name);
629         if (rc < 0) {
630                 fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
631                 return rc;
632         }
633
634         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
635         if (rc != 0) {
636                 fprintf(stderr, "%s: %s: cannot get data version: %s\n",
637                         progname, name, strerror(-rc));
638                 return rc;
639         }
640
641         if (dv1 != dv2) {
642                 rc = -EAGAIN;
643                 fprintf(stderr, "%s: %s: data version changed during "
644                                 "migration\n",
645                         progname, name);
646                 return rc;
647         }
648
649         /* Make sure we keep original atime/mtime values */
650         rc = migrate_copy_timestamps(fdv, st);
651         if (rc < 0) {
652                 fprintf(stderr, "%s: %s: timestamp copy failed\n",
653                         progname, name);
654                 return rc;
655         }
656
657         /* Atomically put lease, swap layouts and close.
658          * for a migration we need to check data version on file did
659          * not change. */
660         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
661         if (rc < 0) {
662                 fprintf(stderr, "%s: %s: cannot swap layouts: %s\n",
663                         progname, name, strerror(-rc));
664                 return rc;
665         }
666
667         return 0;
668 }
669
670 static int lfs_component_set(char *fname, int comp_id, __u32 flags)
671 {
672         return -ENOTSUP;
673 }
674
675 static int lfs_component_del(char *fname, __u32 comp_id, __u32 flags)
676 {
677         int     rc = 0;
678
679         if (flags != 0 && comp_id != 0)
680                 return -EINVAL;
681
682         /* LCME_FL_INIT is the only supported flag in PFL */
683         if (flags != 0) {
684                 if (flags & ~LCME_KNOWN_FLAGS) {
685                         fprintf(stderr, "Invalid component flags %#x\n", flags);
686                         return -EINVAL;
687                 }
688         } else if (comp_id > LCME_ID_MAX) {
689                 fprintf(stderr, "Invalid component id %u\n", comp_id);
690                 return -EINVAL;
691         }
692
693         rc = llapi_layout_file_comp_del(fname, comp_id, flags);
694         if (rc)
695                 fprintf(stderr, "Delete component %#x from %s failed. %s\n",
696                         comp_id, fname, strerror(errno));
697         return rc;
698 }
699
700 static int lfs_component_add(char *fname, struct llapi_layout *layout)
701 {
702         int     rc;
703
704         if (layout == NULL)
705                 return -EINVAL;
706
707         rc = llapi_layout_file_comp_add(fname, layout);
708         if (rc)
709                 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
710                         fname, strerror(errno));
711         return rc;
712 }
713
714 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
715                                 struct llapi_layout *layout)
716 {
717         struct stat     st;
718         int     fd;
719
720         if (layout == NULL)
721                 return -EINVAL;
722
723         fd = lstat(fname, &st);
724         if (fd == 0 && S_ISDIR(st.st_mode))
725                 open_flags = O_DIRECTORY | O_RDONLY;
726
727         fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
728         if (fd < 0)
729                 fprintf(stderr, "%s %s failed. %s\n",
730                         S_ISDIR(st.st_mode) ?
731                                 "Set default composite layout to " :
732                                 "Create composite file",
733                         fname, strerror(errno));
734         return fd;
735 }
736
737 static int lfs_migrate(char *name, __u64 migration_flags,
738                        struct llapi_stripe_param *param,
739                        struct llapi_layout *layout)
740 {
741         int                      fd = -1;
742         int                      fdv = -1;
743         char                     parent[PATH_MAX];
744         int                      mdt_index;
745         int                      random_value;
746         char                     volatile_file[sizeof(parent) +
747                                                LUSTRE_VOLATILE_HDR_LEN +
748                                                2 * sizeof(mdt_index) +
749                                                2 * sizeof(random_value) + 4];
750         char                    *ptr;
751         int                      rc;
752         struct lov_user_md      *lum = NULL;
753         int                      lum_size;
754         int                      buf_size = 1024 * 1024 * 4;
755         bool                     have_lease_rdlck = false;
756         struct stat              st;
757         struct stat              stv;
758
759         /* find the right size for the IO and allocate the buffer */
760         lum_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
761         lum = malloc(lum_size);
762         if (lum == NULL) {
763                 rc = -ENOMEM;
764                 goto free;
765         }
766
767         rc = llapi_file_get_stripe(name, lum);
768         /* failure can happen for many reasons and some may be not real errors
769          * (eg: no stripe)
770          * in case of a real error, a later call will fail with better
771          * error management */
772         if (rc == 0) {
773                 if ((lum->lmm_magic == LOV_USER_MAGIC_V1 ||
774                      lum->lmm_magic == LOV_USER_MAGIC_V3) &&
775                     lum->lmm_stripe_size != 0)
776                         buf_size = lum->lmm_stripe_size;
777         }
778
779         /* open file, direct io */
780         /* even if the file is only read, WR mode is nedeed to allow
781          * layout swap on fd */
782         fd = open(name, O_RDWR | O_DIRECT);
783         if (fd == -1) {
784                 rc = -errno;
785                 fprintf(stderr, "%s: %s: cannot open: %s\n", progname, name,
786                         strerror(-rc));
787                 goto free;
788         }
789
790         if (file_lease_supported) {
791                 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
792                 if (rc == -EOPNOTSUPP) {
793                         /* Older servers do not support file lease.
794                          * Disable related checks. This opens race conditions
795                          * as explained in LU-4840 */
796                         file_lease_supported = false;
797                 } else if (rc < 0) {
798                         fprintf(stderr, "%s: %s: cannot get open lease: %s\n",
799                                 progname, name, strerror(-rc));
800                         goto error;
801                 } else {
802                         have_lease_rdlck = true;
803                 }
804         }
805
806         /* search for file directory pathname */
807         if (strlen(name) > sizeof(parent)-1) {
808                 rc = -E2BIG;
809                 goto error;
810         }
811         strncpy(parent, name, sizeof(parent));
812         ptr = strrchr(parent, '/');
813         if (ptr == NULL) {
814                 if (getcwd(parent, sizeof(parent)) == NULL) {
815                         rc = -errno;
816                         goto error;
817                 }
818         } else {
819                 if (ptr == parent)
820                         strcpy(parent, "/");
821                 else
822                         *ptr = '\0';
823         }
824
825         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
826         if (rc < 0) {
827                 fprintf(stderr, "%s: %s: cannot get MDT index: %s\n",
828                         progname, name, strerror(-rc));
829                 goto error;
830         }
831
832         do {
833                 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
834                 mode_t open_mode = S_IRUSR | S_IWUSR;
835
836                 random_value = random();
837                 rc = snprintf(volatile_file, sizeof(volatile_file),
838                               "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
839                               mdt_index, random_value);
840                 if (rc >= sizeof(volatile_file)) {
841                         rc = -E2BIG;
842                         goto error;
843                 }
844
845                 /* create, open a volatile file, use caching (ie no directio) */
846                 if (param != NULL)
847                         fdv = llapi_file_open_param(volatile_file, open_flags,
848                                                     open_mode, param);
849                 else if (layout != NULL)
850                         fdv = lfs_component_create(volatile_file, open_flags,
851                                                    open_mode, layout);
852                 else
853                         fdv = -EINVAL;
854         } while (fdv == -EEXIST);
855
856         if (fdv < 0) {
857                 rc = fdv;
858                 fprintf(stderr, "%s: %s: cannot create volatile file in"
859                                 " directory: %s\n",
860                         progname, parent, strerror(-rc));
861                 goto error;
862         }
863
864         /* In case the MDT does not support creation of volatile files
865          * we should try to unlink it. */
866         (void)unlink(volatile_file);
867
868         /* Not-owner (root?) special case.
869          * Need to set owner/group of volatile file like original.
870          * This will allow to pass related check during layout_swap.
871          */
872         rc = fstat(fd, &st);
873         if (rc != 0) {
874                 rc = -errno;
875                 fprintf(stderr, "%s: %s: cannot stat: %s\n", progname, name,
876                         strerror(errno));
877                 goto error;
878         }
879         rc = fstat(fdv, &stv);
880         if (rc != 0) {
881                 rc = -errno;
882                 fprintf(stderr, "%s: %s: cannot stat: %s\n", progname,
883                         volatile_file, strerror(errno));
884                 goto error;
885         }
886         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
887                 rc = fchown(fdv, st.st_uid, st.st_gid);
888                 if (rc != 0) {
889                         rc = -errno;
890                         fprintf(stderr, "%s: %s: cannot chown: %s\n", progname,
891                                 name, strerror(errno));
892                         goto error;
893                 }
894         }
895
896         if (migration_flags & MIGRATION_NONBLOCK && file_lease_supported) {
897                 rc = migrate_nonblock(fd, fdv, &st, buf_size, name);
898                 if (rc == 0) {
899                         have_lease_rdlck = false;
900                         fdv = -1; /* The volatile file is closed as we put the
901                                    * lease in non-blocking mode. */
902                 }
903         } else {
904                 /* Blocking mode (forced if servers do not support file lease).
905                  * It is also the default mode, since we cannot distinguish
906                  * between a broken lease and a server that does not support
907                  * atomic swap/close (LU-6785) */
908                 rc = migrate_block(fd, fdv, &st, buf_size, name);
909         }
910
911 error:
912         if (have_lease_rdlck)
913                 llapi_lease_put(fd);
914
915         if (fd >= 0)
916                 close(fd);
917
918         if (fdv >= 0)
919                 close(fdv);
920
921 free:
922         if (lum)
923                 free(lum);
924
925         return rc;
926 }
927
928 /**
929  * Parse a string containing an OST index list into an array of integers.
930  *
931  * The input string contains a comma delimited list of individual
932  * indices and ranges, for example "1,2-4,7". Add the indices into the
933  * \a osts array and remove duplicates.
934  *
935  * \param[out] osts    array to store indices in
936  * \param[in] size     size of \a osts array
937  * \param[in] offset   starting index in \a osts
938  * \param[in] arg      string containing OST index list
939  *
940  * \retval positive    number of indices in \a osts
941  * \retval -EINVAL     unable to parse \a arg
942  */
943 static int parse_targets(__u32 *osts, int size, int offset, char *arg)
944 {
945         int rc;
946         int nr = offset;
947         int slots = size - offset;
948         char *ptr = NULL;
949         bool end_of_loop;
950
951         if (arg == NULL)
952                 return -EINVAL;
953
954         end_of_loop = false;
955         while (!end_of_loop) {
956                 int start_index;
957                 int end_index;
958                 int i;
959                 char *endptr = NULL;
960
961                 rc = -EINVAL;
962
963                 ptr = strchrnul(arg, ',');
964
965                 end_of_loop = *ptr == '\0';
966                 *ptr = '\0';
967
968                 start_index = strtol(arg, &endptr, 0);
969                 if (endptr == arg) /* no data at all */
970                         break;
971                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
972                         break;
973                 if (start_index < 0)
974                         break;
975
976                 end_index = start_index;
977                 if (*endptr == '-') {
978                         end_index = strtol(endptr + 1, &endptr, 0);
979                         if (*endptr != '\0')
980                                 break;
981                         if (end_index < start_index)
982                                 break;
983                 }
984
985                 for (i = start_index; i <= end_index && slots > 0; i++) {
986                         int j;
987
988                         /* remove duplicate */
989                         for (j = 0; j < offset; j++) {
990                                 if (osts[j] == i)
991                                         break;
992                         }
993                         if (j == offset) { /* no duplicate */
994                                 osts[nr++] = i;
995                                 --slots;
996                         }
997                 }
998                 if (slots == 0 && i < end_index)
999                         break;
1000
1001                 *ptr = ',';
1002                 arg = ++ptr;
1003                 offset = nr;
1004                 rc = 0;
1005         }
1006         if (!end_of_loop && ptr != NULL)
1007                 *ptr = ',';
1008
1009         return rc < 0 ? rc : nr;
1010 }
1011
1012 static int verify_pool_name(char *prog_name, char *pool_name)
1013 {
1014         char *ptr;
1015
1016         if (pool_name == NULL)
1017                 return 0;
1018
1019         ptr = strchr(pool_name, '.');
1020         if (ptr != NULL && ptr == pool_name) {
1021                 fprintf(stderr, "error: %s: fsname is empty in pool name '%s'\n",
1022                         prog_name, pool_name);
1023                 return -EINVAL;
1024         }
1025
1026         return 0;
1027 }
1028
1029 struct lfs_setstripe_args {
1030         unsigned long long       lsa_comp_end;
1031         unsigned long long       lsa_stripe_size;
1032         int                      lsa_stripe_count;
1033         int                      lsa_stripe_off;
1034         __u32                    lsa_comp_flags;
1035         int                      lsa_nr_osts;
1036         __u32                   *lsa_osts;
1037         char                    *lsa_pool_name;
1038 };
1039
1040 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1041 {
1042         memset(lsa, 0, sizeof(*lsa));
1043         lsa->lsa_stripe_off = -1;
1044 }
1045
1046 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1047 {
1048         return (lsa->lsa_stripe_size != 0 || lsa->lsa_stripe_count != 0 ||
1049                 lsa->lsa_stripe_off != -1 || lsa->lsa_pool_name != NULL ||
1050                 lsa->lsa_comp_end != 0);
1051 }
1052
1053 static int comp_args_to_layout(struct llapi_layout **composite,
1054                                struct lfs_setstripe_args *lsa)
1055 {
1056         struct llapi_layout *layout = *composite;
1057         uint64_t prev_end = 0;
1058         int i = 0, rc;
1059
1060         if (layout == NULL) {
1061                 layout = llapi_layout_alloc();
1062                 if (layout == NULL) {
1063                         fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1064                                 strerror(errno));
1065                         return -ENOMEM;
1066                 }
1067                 *composite = layout;
1068         } else {
1069                 uint64_t start;
1070
1071                 /* Get current component extent, current component
1072                  * must be the tail component. */
1073                 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
1074                 if (rc) {
1075                         fprintf(stderr, "Get comp extent failed. %s\n",
1076                                 strerror(errno));
1077                         return rc;
1078                 }
1079
1080                 rc = llapi_layout_comp_add(layout);
1081                 if (rc) {
1082                         fprintf(stderr, "Add component failed. %s\n",
1083                                 strerror(errno));
1084                         return rc;
1085                 }
1086         }
1087
1088         rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
1089         if (rc) {
1090                 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
1091                         prev_end, lsa->lsa_comp_end, strerror(errno));
1092                 return rc;
1093         }
1094
1095         if (lsa->lsa_stripe_size != 0) {
1096                 rc = llapi_layout_stripe_size_set(layout,
1097                                                   lsa->lsa_stripe_size);
1098                 if (rc) {
1099                         fprintf(stderr, "Set stripe size %llu failed. %s\n",
1100                                 lsa->lsa_stripe_size, strerror(errno));
1101                         return rc;
1102                 }
1103         }
1104
1105         if (lsa->lsa_stripe_count != 0) {
1106                 rc = llapi_layout_stripe_count_set(layout,
1107                                                    lsa->lsa_stripe_count == -1 ?
1108                                                    LLAPI_LAYOUT_WIDE :
1109                                                    lsa->lsa_stripe_count);
1110                 if (rc) {
1111                         fprintf(stderr, "Set stripe count %d failed. %s\n",
1112                                 lsa->lsa_stripe_count, strerror(errno));
1113                         return rc;
1114                 }
1115         }
1116
1117         if (lsa->lsa_pool_name != NULL) {
1118                 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
1119                 if (rc) {
1120                         fprintf(stderr, "Set pool name: %s failed. %s\n",
1121                                 lsa->lsa_pool_name, strerror(errno));
1122                         return rc;
1123                 }
1124         }
1125
1126         if (lsa->lsa_nr_osts > 0) {
1127                 if (lsa->lsa_stripe_count > 0 &&
1128                     lsa->lsa_nr_osts != lsa->lsa_stripe_count) {
1129                         fprintf(stderr, "stripe_count(%d) != nr_osts(%d)\n",
1130                                 lsa->lsa_stripe_count, lsa->lsa_nr_osts);
1131                         return -EINVAL;
1132                 }
1133                 for (i = 0; i < lsa->lsa_nr_osts; i++) {
1134                         rc = llapi_layout_ost_index_set(layout, i,
1135                                                         lsa->lsa_osts[i]);
1136                         if (rc)
1137                                 break;
1138                 }
1139         } else if (lsa->lsa_stripe_off != -1) {
1140                 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
1141         }
1142         if (rc) {
1143                 fprintf(stderr, "Set ost index %d failed. %s\n",
1144                         i, strerror(errno));
1145                 return rc;
1146         }
1147
1148         return 0;
1149 }
1150
1151 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
1152  * end of the last component in the existing file, and adjust the
1153  * first extent start of the components to be added accordingly. */
1154 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
1155 {
1156         struct llapi_layout *head;
1157         uint64_t start, end, stripe_size, prev_end = 0;
1158         int rc;
1159
1160         if (layout == NULL)
1161                 return -EINVAL;
1162
1163         errno = 0;
1164         head = llapi_layout_get_by_path(fname, 0);
1165         if (head == NULL) {
1166                 fprintf(stderr, "Read layout from %s failed. %s\n",
1167                         fname, strerror(errno));
1168                 return -EINVAL;
1169         } else if (errno == ENODATA) {
1170                 /* file without LOVEA, this component-add will be turned
1171                  * into a component-create. */
1172                 llapi_layout_free(head);
1173                 return -ENODATA;
1174         } else if (!llapi_layout_is_composite(head)) {
1175                 fprintf(stderr, "'%s' isn't a composite file.\n",
1176                         fname);
1177                 llapi_layout_free(head);
1178                 return -EINVAL;
1179         }
1180
1181         rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
1182         if (rc) {
1183                 fprintf(stderr, "Get prev extent failed. %s\n",
1184                         strerror(errno));
1185                 llapi_layout_free(head);
1186                 return rc;
1187         }
1188
1189         llapi_layout_free(head);
1190
1191         /* Make sure we use the first component of the layout to be added. */
1192         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1193         if (rc < 0) {
1194                 fprintf(stderr, "Move component cursor failed. %s\n",
1195                         strerror(errno));
1196                 return rc;
1197         }
1198
1199         rc = llapi_layout_comp_extent_get(layout, &start, &end);
1200         if (rc) {
1201                 fprintf(stderr, "Get extent failed. %s\n", strerror(errno));
1202                 return rc;
1203         }
1204
1205         if (start > prev_end || end <= prev_end) {
1206                 fprintf(stderr, "First extent to be set [%lu, %lu) isn't "
1207                         "adjacent with the existing file extent end: %lu\n",
1208                         start, end, prev_end);
1209                 return -EINVAL;
1210         }
1211
1212         rc = llapi_layout_stripe_size_get(layout, &stripe_size);
1213         if (rc) {
1214                 fprintf(stderr, "Get stripe size failed. %s\n",
1215                         strerror(errno));
1216                 return rc;
1217         }
1218
1219         if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
1220             (prev_end & (stripe_size - 1))) {
1221                 fprintf(stderr, "Stripe size %lu not aligned with %lu\n",
1222                         stripe_size, prev_end);
1223                 return -EINVAL;
1224         }
1225
1226         rc = llapi_layout_comp_extent_set(layout, prev_end, end);
1227         if (rc) {
1228                 fprintf(stderr, "Set component extent [%lu, %lu) failed. %s\n",
1229                         prev_end, end, strerror(errno));
1230                 return rc;
1231         }
1232
1233         return 0;
1234 }
1235
1236 static inline bool comp_flags_is_neg(__u32 flags)
1237 {
1238         return flags & LCME_FL_NEG;
1239 }
1240
1241 static inline void comp_flags_set_neg(__u32 *flags)
1242 {
1243         *flags |= LCME_FL_NEG;
1244 }
1245
1246 static inline void comp_flags_clear_neg(__u32 *flags)
1247 {
1248         *flags &= ~LCME_FL_NEG;
1249 }
1250
1251 static int comp_str2flags(__u32 *flags, char *string)
1252 {
1253         char *name;
1254         __u32 neg_flags = 0;
1255
1256         if (string == NULL)
1257                 return -EINVAL;
1258
1259         *flags = 0;
1260         for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1261                 bool found = false;
1262                 int i;
1263
1264                 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1265                         __u32 comp_flag = comp_flags_table[i].cfn_flag;
1266                         const char *comp_name = comp_flags_table[i].cfn_name;
1267
1268                         if (strcmp(name, comp_name) == 0) {
1269                                 *flags |= comp_flag;
1270                                 found = true;
1271                         } else if (strncmp(name, "^", 1) == 0 &&
1272                                    strcmp(name + 1, comp_name) == 0) {
1273                                 neg_flags |= comp_flag;
1274                                 found = true;
1275                         }
1276                 }
1277                 if (!found) {
1278                         llapi_printf(LLAPI_MSG_ERROR, "Component flag "
1279                                      "'%s' is not supported.\n", name);
1280                         return -EINVAL;
1281                 }
1282         }
1283
1284         if (*flags == 0 && neg_flags == 0)
1285                 return -EINVAL;
1286         /* don't support mixed flags for now */
1287         if (*flags && neg_flags)
1288                 return -EINVAL;
1289
1290         if (neg_flags) {
1291                 *flags = neg_flags;
1292                 comp_flags_set_neg(flags);
1293         }
1294
1295         return 0;
1296 }
1297
1298 static inline bool arg_is_eof(char *arg)
1299 {
1300         return !strncmp(arg, "-1", strlen("-1")) ||
1301                !strncmp(arg, "EOF", strlen("EOF")) ||
1302                !strncmp(arg, "eof", strlen("eof"));
1303 }
1304
1305 enum {
1306         LFS_POOL_OPT = 3,
1307         LFS_COMP_COUNT_OPT,
1308         LFS_COMP_START_OPT,
1309         LFS_COMP_FLAGS_OPT,
1310         LFS_COMP_DEL_OPT,
1311         LFS_COMP_SET_OPT,
1312         LFS_COMP_ADD_OPT,
1313         LFS_PROJID_OPT,
1314 };
1315
1316 /* functions */
1317 static int lfs_setstripe(int argc, char **argv)
1318 {
1319         struct lfs_setstripe_args        lsa;
1320         struct llapi_stripe_param       *param = NULL;
1321         struct find_param                migrate_mdt_param = {
1322                 .fp_max_depth = -1,
1323                 .fp_mdt_index = -1,
1324         };
1325         char                            *fname;
1326         int                              result;
1327         int                              result2 = 0;
1328         char                            *end;
1329         int                              c;
1330         int                              delete = 0;
1331         char                            *mdt_idx_arg = NULL;
1332         unsigned long long               size_units = 1;
1333         bool                             migrate_mode = false;
1334         bool                             migration_block = false;
1335         __u64                            migration_flags = 0;
1336         __u32                            osts[LOV_MAX_STRIPE_COUNT] = { 0 };
1337         int                              comp_del = 0, comp_set = 0;
1338         int                              comp_add = 0;
1339         __u32                            comp_id = 0;
1340         struct llapi_layout             *layout = NULL;
1341
1342         struct option long_opts[] = {
1343                 /* --block is only valid in migrate mode */
1344         { .val = 'b',   .name = "block",        .has_arg = no_argument},
1345         { .val = LFS_COMP_ADD_OPT,
1346                         .name = "comp-add",     .has_arg = no_argument},
1347         { .val = LFS_COMP_ADD_OPT,
1348                         .name = "component-add",
1349                                                 .has_arg = no_argument},
1350         { .val = LFS_COMP_DEL_OPT,
1351                         .name = "comp-del",     .has_arg = no_argument},
1352         { .val = LFS_COMP_DEL_OPT,
1353                         .name = "component-del",
1354                                                 .has_arg = no_argument},
1355         { .val = LFS_COMP_FLAGS_OPT,
1356                         .name = "comp-flags",   .has_arg = required_argument},
1357         { .val = LFS_COMP_FLAGS_OPT,
1358                         .name = "component-flags",
1359                                                 .has_arg = required_argument},
1360         { .val = LFS_COMP_SET_OPT,
1361                         .name = "comp-set",     .has_arg = no_argument},
1362         { .val = LFS_COMP_SET_OPT,
1363                         .name = "component-set",
1364                                                 .has_arg = no_argument},
1365 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
1366         /* This formerly implied "stripe-count", but was explicitly
1367          * made "stripe-count" for consistency with other options,
1368          * and to separate it from "mdt-count" when DNE arrives. */
1369         { .val = 'c',   .name = "count",        .has_arg = required_argument },
1370 #endif
1371         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument},
1372         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument},
1373         { .val = 'd',   .name = "delete",       .has_arg = no_argument},
1374         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument},
1375         { .val = 'E',   .name = "component-end",
1376                                                 .has_arg = required_argument},
1377         /* dirstripe {"mdt-hash",     required_argument, 0, 'H'}, */
1378 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
1379         /* This formerly implied "stripe-index", but was explicitly
1380          * made "stripe-index" for consistency with other options,
1381          * and to separate it from "mdt-index" when DNE arrives. */
1382         { .val = 'i',   .name = "index",        .has_arg = required_argument },
1383 #endif
1384         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument},
1385         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument},
1386         { .val = 'I',   .name = "comp-id",      .has_arg = required_argument},
1387         { .val = 'I',   .name = "component-id", .has_arg = required_argument},
1388         { .val = 'm',   .name = "mdt",          .has_arg = required_argument},
1389         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument},
1390         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument},
1391         /* --non-block is only valid in migrate mode */
1392         { .val = 'n',   .name = "non-block",    .has_arg = no_argument},
1393         { .val = 'o',   .name = "ost",          .has_arg = required_argument},
1394 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1395         { .val = 'o',   .name = "ost-list",     .has_arg = required_argument },
1396         { .val = 'o',   .name = "ost_list",     .has_arg = required_argument },
1397 #endif
1398         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
1399 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
1400         /* This formerly implied "--stripe-size", but was confusing
1401          * with "lfs find --size|-s", which means "file size", so use
1402          * the consistent "--stripe-size|-S" for all commands. */
1403         { .val = 's',   .name = "size",         .has_arg = required_argument },
1404 #endif
1405         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
1406         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
1407         /* dirstripe {"mdt-count",    required_argument, 0, 'T'}, */
1408         /* --verbose is only valid in migrate mode */
1409         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
1410         { .val = LFS_COMP_ADD_OPT,
1411                         .name = "component-add",
1412                                                 .has_arg = no_argument },
1413         { .val = LFS_COMP_DEL_OPT,
1414                         .name = "component-del",
1415                                                 .has_arg = no_argument },
1416         { .val = LFS_COMP_FLAGS_OPT,
1417                         .name = "component-flags",
1418                                                 .has_arg = required_argument },
1419         { .val = LFS_COMP_SET_OPT,
1420                         .name = "component-set",
1421                                                 .has_arg = no_argument },
1422         { .name = NULL } };
1423
1424         setstripe_args_init(&lsa);
1425
1426         if (strcmp(argv[0], "migrate") == 0)
1427                 migrate_mode = true;
1428
1429         while ((c = getopt_long(argc, argv, "bc:dE:i:I:m:no:p:s:S:v",
1430                                 long_opts, NULL)) >= 0) {
1431                 switch (c) {
1432                 case 0:
1433                         /* Long options. */
1434                         break;
1435                 case LFS_COMP_ADD_OPT:
1436                         comp_add = 1;
1437                         break;
1438                 case LFS_COMP_DEL_OPT:
1439                         comp_del = 1;
1440                         break;
1441                 case LFS_COMP_FLAGS_OPT:
1442                         result = comp_str2flags(&lsa.lsa_comp_flags, optarg);
1443                         if (result != 0) {
1444                                 fprintf(stderr, "error: %s: bad comp flags "
1445                                         "'%s'\n", argv[0], optarg);
1446                                 goto error;
1447                         }
1448                         break;
1449                 case LFS_COMP_SET_OPT:
1450                         comp_set = 1;
1451                         break;
1452                 case 'b':
1453                         if (!migrate_mode) {
1454                                 fprintf(stderr, "--block is valid only for"
1455                                                 " migrate mode\n");
1456                                 goto error;
1457                         }
1458                         migration_block = true;
1459                         break;
1460                 case 'c':
1461 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1462                         if (strcmp(argv[optind - 1], "--count") == 0)
1463                                 fprintf(stderr, "warning: '--count' deprecated"
1464                                         ", use '--stripe-count' instead\n");
1465 #endif
1466                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
1467                         if (*end != '\0') {
1468                                 fprintf(stderr, "error: %s: bad stripe count "
1469                                         "'%s'\n", argv[0], optarg);
1470                                 goto error;
1471                         }
1472                         break;
1473                 case 'd':
1474                         /* delete the default striping pattern */
1475                         delete = 1;
1476                         break;
1477                 case 'E':
1478                         if (lsa.lsa_comp_end != 0) {
1479                                 result = comp_args_to_layout(&layout, &lsa);
1480                                 if (result)
1481                                         goto error;
1482
1483                                 setstripe_args_init(&lsa);
1484                         }
1485
1486                         if (arg_is_eof(optarg)) {
1487                                 lsa.lsa_comp_end = LUSTRE_EOF;
1488                         } else {
1489                                 result = llapi_parse_size(optarg,
1490                                                         &lsa.lsa_comp_end,
1491                                                         &size_units, 0);
1492                                 if (result) {
1493                                         fprintf(stderr, "error: %s: "
1494                                                 "bad component end '%s'\n",
1495                                                 argv[0], optarg);
1496                                         goto error;
1497                                 }
1498                         }
1499                         break;
1500                 case 'i':
1501                         if (strcmp(argv[optind - 1], "--index") == 0)
1502                                 fprintf(stderr, "warning: '--index' deprecated"
1503                                         ", use '--stripe-index' instead\n");
1504                         lsa.lsa_stripe_off = strtol(optarg, &end, 0);
1505                         if (*end != '\0') {
1506                                 fprintf(stderr, "error: %s: bad stripe offset "
1507                                         "'%s'\n", argv[0], optarg);
1508                                 goto error;
1509                         }
1510                         break;
1511                 case 'I':
1512                         comp_id = strtoul(optarg, &end, 0);
1513                         if (*end != '\0' || comp_id == 0 ||
1514                             comp_id > LCME_ID_MAX) {
1515                                 fprintf(stderr, "error: %s: bad comp ID "
1516                                         "'%s'\n", argv[0], optarg);
1517                                 goto error;
1518                         }
1519                         break;
1520                 case 'm':
1521                         if (!migrate_mode) {
1522                                 fprintf(stderr, "--mdt-index is valid only for"
1523                                                 " migrate mode\n");
1524                                 goto error;
1525                         }
1526                         mdt_idx_arg = optarg;
1527                         break;
1528                 case 'n':
1529                         if (!migrate_mode) {
1530                                 fprintf(stderr, "--non-block is valid only for"
1531                                                 " migrate mode\n");
1532                                 goto error;
1533                         }
1534                         migration_flags |= MIGRATION_NONBLOCK;
1535                         break;
1536                 case 'o':
1537                         lsa.lsa_nr_osts = parse_targets(osts,
1538                                                 sizeof(osts) / sizeof(__u32),
1539                                                 lsa.lsa_nr_osts, optarg);
1540                         if (lsa.lsa_nr_osts < 0) {
1541                                 fprintf(stderr,
1542                                         "error: %s: bad OST indices '%s'\n",
1543                                         argv[0], optarg);
1544                                 goto error;
1545                         }
1546
1547                         lsa.lsa_osts = osts;
1548                         if (lsa.lsa_stripe_off == -1)
1549                                 lsa.lsa_stripe_off = osts[0];
1550                         break;
1551                 case 'p':
1552                         result = verify_pool_name(argv[0], optarg);
1553                         if (result)
1554                                 goto error;
1555                         lsa.lsa_pool_name = optarg;
1556                         break;
1557 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
1558                 case 's':
1559 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1560                         fprintf(stderr, "warning: '--size|-s' deprecated, "
1561                                 "use '--stripe-size|-S' instead\n");
1562 #endif
1563 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0) */
1564                 case 'S':
1565                         result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
1566                                                   &size_units, 0);
1567                         if (result) {
1568                                 fprintf(stderr, "error: %s: bad stripe size "
1569                                         "'%s'\n", argv[0], optarg);
1570                                 goto error;
1571                         }
1572                         break;
1573                 case 'v':
1574                         if (!migrate_mode) {
1575                                 fprintf(stderr, "--verbose is valid only for"
1576                                                 " migrate mode\n");
1577                                 goto error;
1578                         }
1579                         migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
1580                         break;
1581                 default:
1582                         goto error;
1583                 }
1584         }
1585
1586         fname = argv[optind];
1587
1588         if (lsa.lsa_comp_end != 0) {
1589                 result = comp_args_to_layout(&layout, &lsa);
1590                 if (result)
1591                         goto error;
1592         }
1593
1594         if (optind == argc) {
1595                 fprintf(stderr, "error: %s: missing filename|dirname\n",
1596                         argv[0]);
1597                 goto error;
1598         }
1599
1600         /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
1601          * altered by user space tool, so we don't need to support the
1602          * --component-set for this moment. */
1603         if (comp_set != 0) {
1604                 fprintf(stderr, "error: %s: --component-set isn't supported.\n",
1605                         argv[0]);
1606                 goto error;
1607         }
1608
1609         if ((delete + comp_set + comp_del + comp_add) > 1) {
1610                 fprintf(stderr, "error: %s: can't specify --component-set, "
1611                         "--component-del, --component-add or -d together\n",
1612                         argv[0]);
1613                 goto error;
1614         }
1615
1616         if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
1617                        lsa.lsa_comp_flags != 0 || layout != NULL)) {
1618                 fprintf(stderr, "error: %s: can't specify -d with "
1619                         "-s, -c, -o, -p, -I, -F or -E options\n",
1620                         argv[0]);
1621                 goto error;
1622         }
1623
1624         if ((comp_set || comp_del) &&
1625             (setstripe_args_specified(&lsa) || layout != NULL)) {
1626                 fprintf(stderr, "error: %s: can't specify --component-del or "
1627                         "--component-set with -s, -c, -o, -p or -E options.\n",
1628                         argv[0]);
1629                 goto error;
1630         }
1631
1632         if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
1633                 fprintf(stderr, "error: %s: can't specify both -I and -F for "
1634                         "--component-del option.\n", argv[0]);
1635                 goto error;
1636         }
1637
1638         if (comp_add || comp_del) {
1639                 struct stat st;
1640
1641                 result = lstat(fname, &st);
1642                 if (result == 0 && S_ISDIR(st.st_mode)) {
1643                         fprintf(stderr, "error: %s: can't use --component-add "
1644                                 "or --component-del for directory.\n",
1645                                 argv[0]);
1646                         goto error;
1647                 }
1648         }
1649
1650         if (comp_add) {
1651                 if (layout == NULL) {
1652                         fprintf(stderr, "error: %s: -E option must be present"
1653                                 "in --component-add mode.\n", argv[0]);
1654                         goto error;
1655                 }
1656                 result = adjust_first_extent(fname, layout);
1657                 if (result == -ENODATA)
1658                         comp_add = 0;
1659                 else if (result != 0)
1660                         goto error;
1661         }
1662
1663         if (mdt_idx_arg != NULL && optind > 3) {
1664                 fprintf(stderr, "error: %s: cannot specify -m with other "
1665                         "options\n", argv[0]);
1666                 goto error;
1667         }
1668
1669         if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
1670                 fprintf(stderr,
1671                         "error: %s: cannot specify --non-block and --block\n",
1672                         argv[0]);
1673                 goto error;
1674         }
1675
1676         if (!comp_del && !comp_set && comp_id != 0) {
1677                 fprintf(stderr, "error: %s: -I can only be used with "
1678                         "--component-del.\n", argv[0]);
1679                 goto error;
1680         }
1681
1682         if (mdt_idx_arg != NULL) {
1683                 /* initialize migrate mdt parameters */
1684                 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
1685                 if (*end != '\0') {
1686                         fprintf(stderr, "error: %s: bad MDT index '%s'\n",
1687                                 argv[0], mdt_idx_arg);
1688                         goto error;
1689                 }
1690                 migrate_mdt_param.fp_migrate = 1;
1691         } else if (layout == NULL) {
1692                 /* initialize stripe parameters */
1693                 param = calloc(1, offsetof(typeof(*param),
1694                                lsp_osts[lsa.lsa_nr_osts]));
1695                 if (param == NULL) {
1696                         fprintf(stderr, "error: %s: %s\n", argv[0],
1697                                 strerror(ENOMEM));
1698                         goto error;
1699                 }
1700
1701                 param->lsp_stripe_size = lsa.lsa_stripe_size;
1702                 param->lsp_stripe_offset = lsa.lsa_stripe_off;
1703                 param->lsp_stripe_count = lsa.lsa_stripe_count;
1704                 param->lsp_stripe_pattern = 0;
1705                 param->lsp_pool = lsa.lsa_pool_name;
1706                 param->lsp_is_specific = false;
1707                 if (lsa.lsa_nr_osts > 0) {
1708                         if (lsa.lsa_stripe_count > 0 &&
1709                             lsa.lsa_nr_osts != lsa.lsa_stripe_count) {
1710                                 fprintf(stderr, "error: %s: stripe count '%d' "
1711                                         "doesn't match the number of OSTs: %d\n"
1712                                         , argv[0], lsa.lsa_stripe_count,
1713                                         lsa.lsa_nr_osts);
1714                                 free(param);
1715                                 goto error;
1716                         }
1717
1718                         param->lsp_is_specific = true;
1719                         param->lsp_stripe_count = lsa.lsa_nr_osts;
1720                         memcpy(param->lsp_osts, osts,
1721                                sizeof(*osts) * lsa.lsa_nr_osts);
1722                 }
1723         }
1724
1725         for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
1726                 char *op;
1727                 if (mdt_idx_arg != NULL) {
1728                         result = llapi_migrate_mdt(fname, &migrate_mdt_param);
1729                         op = "migrate mdt objects of";
1730                 } else if (migrate_mode) {
1731                         result = lfs_migrate(fname, migration_flags, param,
1732                                              layout);
1733                         op = "migrate ost objects of";
1734                 } else if (comp_set != 0) {
1735                         result = lfs_component_set(fname, comp_id,
1736                                                    lsa.lsa_comp_flags);
1737                         op = "modify component flags of";
1738                 } else if (comp_del != 0) {
1739                         result = lfs_component_del(fname, comp_id,
1740                                                    lsa.lsa_comp_flags);
1741                         op = "delete component of";
1742                 } else if (comp_add != 0) {
1743                         result = lfs_component_add(fname, layout);
1744                         op = "add component to";
1745                 } else if (layout != NULL) {
1746                         result = lfs_component_create(fname, O_CREAT | O_WRONLY,
1747                                                       0644, layout);
1748                         if (result >= 0) {
1749                                 close(result);
1750                                 result = 0;
1751                         }
1752                         op = "create composite";
1753                 } else {
1754                         result = llapi_file_open_param(fname,
1755                                                        O_CREAT | O_WRONLY,
1756                                                        0644, param);
1757                         if (result >= 0) {
1758                                 close(result);
1759                                 result = 0;
1760                         }
1761                         op = "create striped";
1762                 }
1763                 if (result) {
1764                         /* Save the first error encountered. */
1765                         if (result2 == 0)
1766                                 result2 = result;
1767                         fprintf(stderr, "error: %s: %s file '%s' failed: %s\n",
1768                                 argv[0], op, fname,
1769                                 lsa.lsa_pool_name != NULL && result == EINVAL ?
1770                                 "OST not in pool?" : strerror(errno));
1771                         continue;
1772                 }
1773         }
1774
1775         free(param);
1776         llapi_layout_free(layout);
1777         return result2;
1778 error:
1779         llapi_layout_free(layout);
1780         return CMD_HELP;
1781 }
1782
1783 static int lfs_poollist(int argc, char **argv)
1784 {
1785         if (argc != 2)
1786                 return CMD_HELP;
1787
1788         return llapi_poollist(argv[1]);
1789 }
1790
1791 static int set_time(time_t *time, time_t *set, char *str)
1792 {
1793         time_t t;
1794         int res = 0;
1795
1796         if (str[0] == '+')
1797                 res = 1;
1798         else if (str[0] == '-')
1799                 res = -1;
1800
1801         if (res)
1802                 str++;
1803
1804         t = strtol(str, NULL, 0);
1805         if (*time < t * 24 * 60 * 60) {
1806                 if (res)
1807                         str--;
1808                 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
1809                 return INT_MAX;
1810         }
1811
1812         *set = *time - t * 24 * 60 * 60;
1813         return res;
1814 }
1815 static int name2uid(unsigned int *id, const char *name)
1816 {
1817         struct passwd *passwd;
1818
1819         passwd = getpwnam(name);
1820         if (passwd == NULL)
1821                 return -ENOENT;
1822         *id = passwd->pw_uid;
1823
1824         return 0;
1825 }
1826
1827 static int name2gid(unsigned int *id, const char *name)
1828 {
1829         struct group *group;
1830
1831         group = getgrnam(name);
1832         if (group == NULL)
1833                 return -ENOENT;
1834         *id = group->gr_gid;
1835
1836         return 0;
1837 }
1838
1839 static inline int name2projid(unsigned int *id, const char *name)
1840 {
1841         return -ENOTSUP;
1842 }
1843
1844 static int uid2name(char **name, unsigned int id)
1845 {
1846         struct passwd *passwd;
1847
1848         passwd = getpwuid(id);
1849         if (passwd == NULL)
1850                 return -ENOENT;
1851         *name = passwd->pw_name;
1852
1853         return 0;
1854 }
1855
1856 static inline int gid2name(char **name, unsigned int id)
1857 {
1858         struct group *group;
1859
1860         group = getgrgid(id);
1861         if (group == NULL)
1862                 return -ENOENT;
1863         *name = group->gr_name;
1864
1865         return 0;
1866 }
1867
1868 static int name2layout(__u32 *layout, char *name)
1869 {
1870         char *ptr, *lyt;
1871
1872         *layout = 0;
1873         for (ptr = name; ; ptr = NULL) {
1874                 lyt = strtok(ptr, ",");
1875                 if (lyt == NULL)
1876                         break;
1877                 if (strcmp(lyt, "released") == 0)
1878                         *layout |= LOV_PATTERN_F_RELEASED;
1879                 else if (strcmp(lyt, "raid0") == 0)
1880                         *layout |= LOV_PATTERN_RAID0;
1881                 else
1882                         return -1;
1883         }
1884         return 0;
1885 }
1886
1887 static int lfs_find(int argc, char **argv)
1888 {
1889         int c, rc;
1890         int ret = 0;
1891         time_t t;
1892         struct find_param param = {
1893                 .fp_max_depth = -1,
1894                 .fp_quiet = 1,
1895         };
1896         struct option long_opts[] = {
1897         { .val = 'A',   .name = "atime",        .has_arg = required_argument },
1898         { .val = LFS_COMP_COUNT_OPT,
1899                         .name = "comp-count",   .has_arg = required_argument },
1900         { .val = LFS_COMP_COUNT_OPT,
1901                         .name = "component-count",
1902                                                 .has_arg = required_argument },
1903         { .val = LFS_COMP_FLAGS_OPT,
1904                         .name = "comp-flags",   .has_arg = required_argument },
1905         { .val = LFS_COMP_FLAGS_OPT,
1906                         .name = "component-flags",
1907                                                 .has_arg = required_argument },
1908         { .val = LFS_COMP_START_OPT,
1909                         .name = "comp-start",   .has_arg = required_argument },
1910         { .val = LFS_COMP_START_OPT,
1911                         .name = "component-start",
1912                                                 .has_arg = required_argument },
1913         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument },
1914         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument },
1915         { .val = 'C',   .name = "ctime",        .has_arg = required_argument },
1916         { .val = 'D',   .name = "maxdepth",     .has_arg = required_argument },
1917         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument },
1918         { .val = 'E',   .name = "component-end",
1919                                                 .has_arg = required_argument },
1920         { .val = 'g',   .name = "gid",          .has_arg = required_argument },
1921         { .val = 'G',   .name = "group",        .has_arg = required_argument },
1922         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
1923         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument },
1924         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
1925         /*{"component-id", required_argument, 0, 'I'},*/
1926         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
1927         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
1928         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
1929         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument },
1930         { .val = 'M',   .name = "mtime",        .has_arg = required_argument },
1931         { .val = 'n',   .name = "name",         .has_arg = required_argument },
1932      /* reserve {"or",           no_argument,     , 0, 'o'}, to match find(1) */
1933         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
1934         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
1935         /* no short option for pool, p/P already used */
1936         { .val = LFS_POOL_OPT,
1937                         .name = "pool",         .has_arg = required_argument },
1938         { .val = 'p',   .name = "print0",       .has_arg = no_argument },
1939         { .val = 'P',   .name = "print",        .has_arg = no_argument },
1940         { .val = LFS_PROJID_OPT,
1941                         .name = "projid",       .has_arg = required_argument },
1942         { .val = 's',   .name = "size",         .has_arg = required_argument },
1943         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
1944         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
1945         { .val = 't',   .name = "type",         .has_arg = required_argument },
1946         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
1947         { .val = 'u',   .name = "uid",          .has_arg = required_argument },
1948         { .val = 'U',   .name = "user",         .has_arg = required_argument },
1949         { .name = NULL } };
1950         int pathstart = -1;
1951         int pathend = -1;
1952         int neg_opt = 0;
1953         time_t *xtime;
1954         int *xsign;
1955         int isoption;
1956         char *endptr;
1957
1958         time(&t);
1959
1960         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
1961         while ((c = getopt_long_only(argc, argv,
1962                         "-A:c:C:D:E:g:G:H:i:L:m:M:n:O:Ppqrs:S:t:T:u:U:v",
1963                         long_opts, NULL)) >= 0) {
1964                 xtime = NULL;
1965                 xsign = NULL;
1966                 if (neg_opt)
1967                         --neg_opt;
1968                 /* '!' is part of option */
1969                 /* when getopt_long_only() finds a string which is not
1970                  * an option nor a known option argument it returns 1
1971                  * in that case if we already have found pathstart and pathend
1972                  * (i.e. we have the list of pathnames),
1973                  * the only supported value is "!"
1974                  */
1975                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
1976                 if (!isoption && pathend != -1) {
1977                         fprintf(stderr, "err: %s: filename|dirname must either "
1978                                         "precede options or follow options\n",
1979                                         argv[0]);
1980                         ret = CMD_HELP;
1981                         goto err;
1982                 }
1983                 if (!isoption && pathstart == -1)
1984                         pathstart = optind - 1;
1985                 if (isoption && pathstart != -1 && pathend == -1)
1986                         pathend = optind - 2;
1987                 switch (c) {
1988                 case 0:
1989                         /* Long options. */
1990                         break;
1991                 case 1:
1992                         /* unknown; opt is "!" or path component,
1993                          * checking done above.
1994                          */
1995                         if (strcmp(optarg, "!") == 0)
1996                                 neg_opt = 2;
1997                         break;
1998                 case 'A':
1999                         xtime = &param.fp_atime;
2000                         xsign = &param.fp_asign;
2001                         param.fp_exclude_atime = !!neg_opt;
2002                         /* no break, this falls through to 'C' for ctime */
2003                 case 'C':
2004                         if (c == 'C') {
2005                                 xtime = &param.fp_ctime;
2006                                 xsign = &param.fp_csign;
2007                                 param.fp_exclude_ctime = !!neg_opt;
2008                         }
2009                         /* no break, this falls through to 'M' for mtime */
2010                 case 'M':
2011                         if (c == 'M') {
2012                                 xtime = &param.fp_mtime;
2013                                 xsign = &param.fp_msign;
2014                                 param.fp_exclude_mtime = !!neg_opt;
2015                         }
2016                         rc = set_time(&t, xtime, optarg);
2017                         if (rc == INT_MAX) {
2018                                 ret = -1;
2019                                 goto err;
2020                         }
2021                         if (rc)
2022                                 *xsign = rc;
2023                         break;
2024                 case LFS_COMP_COUNT_OPT:
2025                         if (optarg[0] == '+') {
2026                                 param.fp_comp_count_sign = -1;
2027                                 optarg++;
2028                         } else if (optarg[0] == '-') {
2029                                 param.fp_comp_count_sign =  1;
2030                                 optarg++;
2031                         }
2032
2033                         param.fp_comp_count = strtoul(optarg, &endptr, 0);
2034                         if (*endptr != '\0') {
2035                                 fprintf(stderr, "error: bad component count "
2036                                         "'%s'\n", optarg);
2037                                 goto err;
2038                         }
2039                         param.fp_check_comp_count = 1;
2040                         param.fp_exclude_comp_count = !!neg_opt;
2041                         break;
2042                 case LFS_COMP_FLAGS_OPT:
2043                         rc = comp_str2flags(&param.fp_comp_flags, optarg);
2044                         if (rc || comp_flags_is_neg(param.fp_comp_flags)) {
2045                                 fprintf(stderr, "error: bad component flags "
2046                                         "'%s'\n", optarg);
2047                                 goto err;
2048                         }
2049                         param.fp_check_comp_flags = 1;
2050                         param.fp_exclude_comp_flags = !!neg_opt;
2051                         break;
2052                 case LFS_COMP_START_OPT:
2053                         if (optarg[0] == '+') {
2054                                 param.fp_comp_start_sign = -1;
2055                                 optarg++;
2056                         } else if (optarg[0] == '-') {
2057                                 param.fp_comp_start_sign =  1;
2058                                 optarg++;
2059                         }
2060
2061                         rc = llapi_parse_size(optarg, &param.fp_comp_start,
2062                                               &param.fp_comp_start_units, 0);
2063                         if (rc) {
2064                                 fprintf(stderr, "error: bad component start "
2065                                         "'%s'\n", optarg);
2066                                 goto err;
2067                         }
2068                         param.fp_check_comp_start = 1;
2069                         param.fp_exclude_comp_start = !!neg_opt;
2070                         break;
2071                 case 'c':
2072                         if (optarg[0] == '+') {
2073                                 param.fp_stripe_count_sign = -1;
2074                                 optarg++;
2075                         } else if (optarg[0] == '-') {
2076                                 param.fp_stripe_count_sign =  1;
2077                                 optarg++;
2078                         }
2079
2080                         param.fp_stripe_count = strtoul(optarg, &endptr, 0);
2081                         if (*endptr != '\0') {
2082                                 fprintf(stderr,"error: bad stripe_count '%s'\n",
2083                                         optarg);
2084                                 ret = -1;
2085                                 goto err;
2086                         }
2087                         param.fp_check_stripe_count = 1;
2088                         param.fp_exclude_stripe_count = !!neg_opt;
2089                         break;
2090                 case 'D':
2091                         param.fp_max_depth = strtol(optarg, 0, 0);
2092                         break;
2093                 case 'E':
2094                         if (optarg[0] == '+') {
2095                                 param.fp_comp_end_sign = -1;
2096                                 optarg++;
2097                         } else if (optarg[0] == '-') {
2098                                 param.fp_comp_end_sign =  1;
2099                                 optarg++;
2100                         }
2101
2102                         if (arg_is_eof(optarg)) {
2103                                 param.fp_comp_end = LUSTRE_EOF;
2104                                 param.fp_comp_end_units = 1;
2105                                 rc = 0;
2106                         } else {
2107                                 rc = llapi_parse_size(optarg,
2108                                                 &param.fp_comp_end,
2109                                                 &param.fp_comp_end_units, 0);
2110                         }
2111                         if (rc) {
2112                                 fprintf(stderr, "error: bad component end "
2113                                         "'%s'\n", optarg);
2114                                 goto err;
2115                         }
2116                         param.fp_check_comp_end = 1;
2117                         param.fp_exclude_comp_end = !!neg_opt;
2118                         break;
2119                 case 'g':
2120                 case 'G':
2121                         rc = name2gid(&param.fp_gid, optarg);
2122                         if (rc) {
2123                                 param.fp_gid = strtoul(optarg, &endptr, 10);
2124                                 if (*endptr != '\0') {
2125                                         fprintf(stderr, "Group/GID: %s cannot "
2126                                                 "be found.\n", optarg);
2127                                         ret = -1;
2128                                         goto err;
2129                                 }
2130                         }
2131                         param.fp_exclude_gid = !!neg_opt;
2132                         param.fp_check_gid = 1;
2133                         break;
2134                 case 'H':
2135                         param.fp_hash_type = check_hashtype(optarg);
2136                         if (param.fp_hash_type == 0) {
2137                                 fprintf(stderr, "error: bad hash_type '%s'\n",
2138                                         optarg);
2139                                 ret = -1;
2140                                 goto err;
2141                         }
2142                         param.fp_check_hash_type = 1;
2143                         param.fp_exclude_hash_type = !!neg_opt;
2144                         break;
2145                 case 'L':
2146                         ret = name2layout(&param.fp_layout, optarg);
2147                         if (ret)
2148                                 goto err;
2149                         param.fp_exclude_layout = !!neg_opt;
2150                         param.fp_check_layout = 1;
2151                         break;
2152                 case 'u':
2153                 case 'U':
2154                         rc = name2uid(&param.fp_uid, optarg);
2155                         if (rc) {
2156                                 param.fp_uid = strtoul(optarg, &endptr, 10);
2157                                 if (*endptr != '\0') {
2158                                         fprintf(stderr, "User/UID: %s cannot "
2159                                                 "be found.\n", optarg);
2160                                         ret = -1;
2161                                         goto err;
2162                                 }
2163                         }
2164                         param.fp_exclude_uid = !!neg_opt;
2165                         param.fp_check_uid = 1;
2166                         break;
2167                 case LFS_POOL_OPT:
2168                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
2169                                 fprintf(stderr,
2170                                         "Pool name %s is too long"
2171                                         " (max is %d)\n", optarg,
2172                                         LOV_MAXPOOLNAME);
2173                                 ret = -1;
2174                                 goto err;
2175                         }
2176                         /* we do check for empty pool because empty pool
2177                          * is used to find V1 lov attributes */
2178                         strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
2179                         param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
2180                         param.fp_exclude_pool = !!neg_opt;
2181                         param.fp_check_pool = 1;
2182                         break;
2183                 case 'n':
2184                         param.fp_pattern = (char *)optarg;
2185                         param.fp_exclude_pattern = !!neg_opt;
2186                         break;
2187                 case 'm':
2188                 case 'i':
2189                 case 'O': {
2190                         char *buf, *token, *next, *p;
2191                         int len = 1;
2192                         void *tmp;
2193
2194                         buf = strdup(optarg);
2195                         if (buf == NULL) {
2196                                 ret = -ENOMEM;
2197                                 goto err;
2198                         }
2199
2200                         param.fp_exclude_obd = !!neg_opt;
2201
2202                         token = buf;
2203                         while (token && *token) {
2204                                 token = strchr(token, ',');
2205                                 if (token) {
2206                                         len++;
2207                                         token++;
2208                                 }
2209                         }
2210                         if (c == 'm') {
2211                                 param.fp_exclude_mdt = !!neg_opt;
2212                                 param.fp_num_alloc_mdts += len;
2213                                 tmp = realloc(param.fp_mdt_uuid,
2214                                               param.fp_num_alloc_mdts *
2215                                               sizeof(*param.fp_mdt_uuid));
2216                                 if (tmp == NULL) {
2217                                         ret = -ENOMEM;
2218                                         goto err_free;
2219                                 }
2220
2221                                 param.fp_mdt_uuid = tmp;
2222                         } else {
2223                                 param.fp_exclude_obd = !!neg_opt;
2224                                 param.fp_num_alloc_obds += len;
2225                                 tmp = realloc(param.fp_obd_uuid,
2226                                               param.fp_num_alloc_obds *
2227                                               sizeof(*param.fp_obd_uuid));
2228                                 if (tmp == NULL) {
2229                                         ret = -ENOMEM;
2230                                         goto err_free;
2231                                 }
2232
2233                                 param.fp_obd_uuid = tmp;
2234                         }
2235                         for (token = buf; token && *token; token = next) {
2236                                 struct obd_uuid *puuid;
2237                                 if (c == 'm') {
2238                                         puuid =
2239                                         &param.fp_mdt_uuid[param.fp_num_mdts++];
2240                                 } else {
2241                                         puuid =
2242                                         &param.fp_obd_uuid[param.fp_num_obds++];
2243                                 }
2244                                 p = strchr(token, ',');
2245                                 next = 0;
2246                                 if (p) {
2247                                         *p = 0;
2248                                         next = p+1;
2249                                 }
2250
2251                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
2252                                         ret = -E2BIG;
2253                                         goto err_free;
2254                                 }
2255
2256                                 strncpy(puuid->uuid, token,
2257                                         sizeof(puuid->uuid));
2258                         }
2259 err_free:
2260                         if (buf)
2261                                 free(buf);
2262                         break;
2263                 }
2264                 case 'p':
2265                         param.fp_zero_end = 1;
2266                         break;
2267                 case 'P':
2268                         break;
2269                 case LFS_PROJID_OPT:
2270                         rc = name2projid(&param.fp_projid, optarg);
2271                         if (rc) {
2272                                 param.fp_projid = strtoul(optarg, &endptr, 10);
2273                                 if (*endptr != '\0') {
2274                                         fprintf(stderr,
2275                                                 "Invalid project ID: %s",
2276                                                 optarg);
2277                                         ret = -1;
2278                                         goto err;
2279                                 }
2280                         }
2281                         param.fp_exclude_projid = !!neg_opt;
2282                         param.fp_check_projid = 1;
2283                         break;
2284                 case 's':
2285                         if (optarg[0] == '+') {
2286                                 param.fp_size_sign = -1;
2287                                 optarg++;
2288                         } else if (optarg[0] == '-') {
2289                                 param.fp_size_sign =  1;
2290                                 optarg++;
2291                         }
2292
2293                         ret = llapi_parse_size(optarg, &param.fp_size,
2294                                                &param.fp_size_units, 0);
2295                         if (ret) {
2296                                 fprintf(stderr, "error: bad file size '%s'\n",
2297                                         optarg);
2298                                 goto err;
2299                         }
2300                         param.fp_check_size = 1;
2301                         param.fp_exclude_size = !!neg_opt;
2302                         break;
2303                 case 'S':
2304                         if (optarg[0] == '+') {
2305                                 param.fp_stripe_size_sign = -1;
2306                                 optarg++;
2307                         } else if (optarg[0] == '-') {
2308                                 param.fp_stripe_size_sign =  1;
2309                                 optarg++;
2310                         }
2311
2312                         ret = llapi_parse_size(optarg, &param.fp_stripe_size,
2313                                                &param.fp_stripe_size_units, 0);
2314                         if (ret) {
2315                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
2316                                         optarg);
2317                                 goto err;
2318                         }
2319                         param.fp_check_stripe_size = 1;
2320                         param.fp_exclude_stripe_size = !!neg_opt;
2321                         break;
2322                 case 't':
2323                         param.fp_exclude_type = !!neg_opt;
2324                         switch (optarg[0]) {
2325                         case 'b':
2326                                 param.fp_type = S_IFBLK;
2327                                 break;
2328                         case 'c':
2329                                 param.fp_type = S_IFCHR;
2330                                 break;
2331                         case 'd':
2332                                 param.fp_type = S_IFDIR;
2333                                 break;
2334                         case 'f':
2335                                 param.fp_type = S_IFREG;
2336                                 break;
2337                         case 'l':
2338                                 param.fp_type = S_IFLNK;
2339                                 break;
2340                         case 'p':
2341                                 param.fp_type = S_IFIFO;
2342                                 break;
2343                         case 's':
2344                                 param.fp_type = S_IFSOCK;
2345                                 break;
2346                         default:
2347                                 fprintf(stderr, "error: %s: bad type '%s'\n",
2348                                         argv[0], optarg);
2349                                 ret = CMD_HELP;
2350                                 goto err;
2351                         };
2352                         break;
2353                 case 'T':
2354                         if (optarg[0] == '+') {
2355                                 param.fp_mdt_count_sign = -1;
2356                                 optarg++;
2357                         } else if (optarg[0] == '-') {
2358                                 param.fp_mdt_count_sign =  1;
2359                                 optarg++;
2360                         }
2361
2362                         param.fp_mdt_count = strtoul(optarg, &endptr, 0);
2363                         if (*endptr != '\0') {
2364                                 fprintf(stderr, "error: bad mdt_count '%s'\n",
2365                                         optarg);
2366                                 ret = -1;
2367                                 goto err;
2368                         }
2369                         param.fp_check_mdt_count = 1;
2370                         param.fp_exclude_mdt_count = !!neg_opt;
2371                         break;
2372                 default:
2373                         ret = CMD_HELP;
2374                         goto err;
2375                 };
2376         }
2377
2378         if (pathstart == -1) {
2379                 fprintf(stderr, "error: %s: no filename|pathname\n",
2380                         argv[0]);
2381                 ret = CMD_HELP;
2382                 goto err;
2383         } else if (pathend == -1) {
2384                 /* no options */
2385                 pathend = argc;
2386         }
2387
2388         do {
2389                 rc = llapi_find(argv[pathstart], &param);
2390                 if (rc != 0 && ret == 0)
2391                         ret = rc;
2392         } while (++pathstart < pathend);
2393
2394         if (ret)
2395                 fprintf(stderr, "error: %s failed for %s.\n",
2396                         argv[0], argv[optind - 1]);
2397 err:
2398         if (param.fp_obd_uuid && param.fp_num_alloc_obds)
2399                 free(param.fp_obd_uuid);
2400
2401         if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
2402                 free(param.fp_mdt_uuid);
2403
2404         return ret;
2405 }
2406
2407 static int lfs_getstripe_internal(int argc, char **argv,
2408                                   struct find_param *param)
2409 {
2410         struct option long_opts[] = {
2411         { .val = LFS_COMP_COUNT_OPT,
2412                         .name = "comp-count",   .has_arg = no_argument },
2413         { .val = LFS_COMP_COUNT_OPT,
2414                 .name = "component-count",      .has_arg = no_argument },
2415         { .val = LFS_COMP_FLAGS_OPT,
2416                         .name = "comp-flags",   .has_arg = optional_argument },
2417         { .val = LFS_COMP_FLAGS_OPT,
2418                 .name = "component-flags",      .has_arg = optional_argument },
2419         { .val = LFS_COMP_START_OPT,
2420                         .name = "comp-start",   .has_arg = optional_argument },
2421         { .val = LFS_COMP_START_OPT,
2422                 .name = "component-start",      .has_arg = optional_argument },
2423 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2424         /* This formerly implied "stripe-count", but was explicitly
2425          * made "stripe-count" for consistency with other options,
2426          * and to separate it from "mdt-count" when DNE arrives. */
2427         { .val = 'c',   .name = "count",        .has_arg = no_argument },
2428 #endif
2429         { .val = 'c',   .name = "stripe-count", .has_arg = no_argument },
2430         { .val = 'c',   .name = "stripe_count", .has_arg = no_argument },
2431         { .val = 'd',   .name = "directory",    .has_arg = no_argument },
2432         { .val = 'D',   .name = "default",      .has_arg = no_argument },
2433         { .val = 'E',   .name = "comp-end",     .has_arg = optional_argument },
2434         { .val = 'E',   .name = "component-end",
2435                                                 .has_arg = optional_argument },
2436         { .val = 'F',   .name = "fid",          .has_arg = no_argument },
2437         { .val = 'g',   .name = "generation",   .has_arg = no_argument },
2438         /* dirstripe { .val = 'H',      .name = "mdt-hash",
2439          *             .has_arg = required_argument }, */
2440 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2441         /* This formerly implied "stripe-index", but was explicitly
2442          * made "stripe-index" for consistency with other options,
2443          * and to separate it from "mdt-index" when DNE arrives. */
2444         { .val = 'i',   .name = "index",        .has_arg = no_argument },
2445 #endif
2446         { .val = 'i',   .name = "stripe-index", .has_arg = no_argument },
2447         { .val = 'i',   .name = "stripe_index", .has_arg = no_argument },
2448         { .val = 'I',   .name = "comp-id",      .has_arg = optional_argument },
2449         { .val = 'I',   .name = "component-id", .has_arg = optional_argument },
2450         { .val = 'L',   .name = "layout",       .has_arg = no_argument },
2451         { .val = 'm',   .name = "mdt",          .has_arg = no_argument },
2452         { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
2453         { .val = 'm',   .name = "mdt_index",    .has_arg = no_argument },
2454 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2455         { .val = 'M',   .name = "mdt-index",    .has_arg = no_argument },
2456         { .val = 'M',   .name = "mdt_index",    .has_arg = no_argument },
2457 #endif
2458 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2459         /* This formerly implied "stripe-index", but was confusing
2460          * with "file offset" (which will eventually be needed for
2461          * with different layouts by offset), so deprecate it. */
2462         { .val = 'o',   .name = "offset",       .has_arg = no_argument },
2463 #endif
2464         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
2465         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
2466         { .val = 'p',   .name = "pool",         .has_arg = no_argument },
2467         { .val = 'q',   .name = "quiet",        .has_arg = no_argument },
2468         { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
2469         { .val = 'R',   .name = "raw",          .has_arg = no_argument },
2470 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2471         /* This formerly implied "--stripe-size", but was confusing
2472          * with "lfs find --size|-s", which means "file size", so use
2473          * the consistent "--stripe-size|-S" for all commands. */
2474         { .val = 's',   .name = "size",         .has_arg = no_argument },
2475 #endif
2476         { .val = 'S',   .name = "stripe-size",  .has_arg = no_argument },
2477         { .val = 'S',   .name = "stripe_size",  .has_arg = no_argument },
2478         /* dirstripe { .val = 'T',      .name = "mdt-count",
2479          *             .has_arg = required_argument }, */
2480         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
2481         { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
2482         { .name = NULL } };
2483         int c, rc;
2484         char *end, *tmp;
2485
2486         while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
2487                                 long_opts, NULL)) != -1) {
2488                 switch (c) {
2489                 case 'c':
2490                         if (strcmp(argv[optind - 1], "--count") == 0)
2491                                 fprintf(stderr, "warning: '--count' deprecated,"
2492                                         " use '--stripe-count' instead\n");
2493                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2494                                 param->fp_verbose |= VERBOSE_COUNT;
2495                                 param->fp_max_depth = 0;
2496                         }
2497                         break;
2498                 case LFS_COMP_COUNT_OPT:
2499                         param->fp_verbose |= VERBOSE_COMP_COUNT;
2500                         param->fp_max_depth = 0;
2501                         break;
2502                 case LFS_COMP_FLAGS_OPT:
2503                         if (optarg != NULL) {
2504                                 __u32 *flags = &param->fp_comp_flags;
2505                                 rc = comp_str2flags(flags, optarg);
2506                                 if (rc != 0) {
2507                                         fprintf(stderr, "error: %s bad "
2508                                                 "component flags '%s'.\n",
2509                                                 argv[0], optarg);
2510                                         return CMD_HELP;
2511                                 } else {
2512                                         param->fp_check_comp_flags = 1;
2513                                         param->fp_exclude_comp_flags =
2514                                                 comp_flags_is_neg(*flags);
2515                                         comp_flags_clear_neg(flags);
2516                                 }
2517                         } else {
2518                                 param->fp_verbose |= VERBOSE_COMP_FLAGS;
2519                                 param->fp_max_depth = 0;
2520                         }
2521                         break;
2522                 case LFS_COMP_START_OPT:
2523                         if (optarg != NULL) {
2524                                 tmp = optarg;
2525                                 if (tmp[0] == '+') {
2526                                         param->fp_comp_start_sign = -1;
2527                                         tmp++;
2528                                 } else if (tmp[0] == '-') {
2529                                         param->fp_comp_start_sign = 1;
2530                                         tmp++;
2531                                 }
2532                                 rc = llapi_parse_size(tmp,
2533                                                 &param->fp_comp_start,
2534                                                 &param->fp_comp_start_units, 0);
2535                                 if (rc != 0) {
2536                                         fprintf(stderr, "error: %s bad "
2537                                                 "component start '%s'.\n",
2538                                                 argv[0], tmp);
2539                                         return CMD_HELP;
2540                                 } else {
2541                                         param->fp_check_comp_start = 1;
2542                                 }
2543                         } else {
2544                                 param->fp_verbose |= VERBOSE_COMP_START;
2545                                 param->fp_max_depth = 0;
2546                         }
2547                         break;
2548                 case 'd':
2549                         param->fp_max_depth = 0;
2550                         break;
2551                 case 'D':
2552                         param->fp_get_default_lmv = 1;
2553                         break;
2554                 case 'E':
2555                         if (optarg != NULL) {
2556                                 tmp = optarg;
2557                                 if (tmp[0] == '+') {
2558                                         param->fp_comp_end_sign = -1;
2559                                         tmp++;
2560                                 } else if (tmp[0] == '-') {
2561                                         param->fp_comp_end_sign = 1;
2562                                         tmp++;
2563                                 }
2564
2565                                 if (arg_is_eof(tmp)) {
2566                                         param->fp_comp_end = LUSTRE_EOF;
2567                                         param->fp_comp_end_units = 1;
2568                                         rc = 0;
2569                                 } else {
2570                                         rc = llapi_parse_size(tmp,
2571                                                 &param->fp_comp_end,
2572                                                 &param->fp_comp_end_units, 0);
2573                                 }
2574                                 if (rc != 0) {
2575                                         fprintf(stderr, "error: %s bad "
2576                                                 "component end '%s'.\n",
2577                                                 argv[0], tmp);
2578                                         return CMD_HELP;
2579                                 }
2580                                 param->fp_check_comp_end = 1;
2581                         } else {
2582                                 param->fp_verbose |= VERBOSE_COMP_END;
2583                                 param->fp_max_depth = 0;
2584                         }
2585                         break;
2586                 case 'F':
2587                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2588                                 param->fp_verbose |= VERBOSE_DFID;
2589                                 param->fp_max_depth = 0;
2590                         }
2591                         break;
2592                 case 'g':
2593                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2594                                 param->fp_verbose |= VERBOSE_GENERATION;
2595                                 param->fp_max_depth = 0;
2596                         }
2597                         break;
2598 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2599                 case 'o':
2600                         fprintf(stderr, "warning: '--offset|-o' deprecated, "
2601                                 "use '--stripe-index|-i' instead\n");
2602 #endif
2603                 case 'i':
2604 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
2605                         if (strcmp(argv[optind - 1], "--index") == 0)
2606                                 fprintf(stderr, "warning: '--index' deprecated"
2607                                         ", use '--stripe-index' instead\n");
2608 #endif
2609                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2610                                 param->fp_verbose |= VERBOSE_OFFSET;
2611                                 param->fp_max_depth = 0;
2612                         }
2613                         break;
2614                 case 'I':
2615                         if (optarg != NULL) {
2616                                 param->fp_comp_id = strtoul(optarg, &end, 0);
2617                                 if (*end != '\0' || param->fp_comp_id == 0 ||
2618                                     param->fp_comp_id > LCME_ID_MAX) {
2619                                         fprintf(stderr, "error: %s bad "
2620                                                 "component id '%s'\n",
2621                                                 argv[0], optarg);
2622                                         return CMD_HELP;
2623                                 } else {
2624                                         param->fp_check_comp_id = 1;
2625                                 }
2626                         } else {
2627                                 param->fp_max_depth = 0;
2628                                 param->fp_verbose |= VERBOSE_COMP_ID;
2629                         }
2630                         break;
2631                 case 'L':
2632                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2633                                 param->fp_verbose |= VERBOSE_LAYOUT;
2634                                 param->fp_max_depth = 0;
2635                         }
2636                         break;
2637 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2638                 case 'M':
2639 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
2640                         fprintf(stderr, "warning: '-M' deprecated"
2641                                 ", use '-m' instead\n");
2642 #endif
2643 #endif
2644                 case 'm':
2645                         if (!(param->fp_verbose & VERBOSE_DETAIL))
2646                                 param->fp_max_depth = 0;
2647                         param->fp_verbose |= VERBOSE_MDTINDEX;
2648                         break;
2649                 case 'O':
2650                         if (param->fp_obd_uuid) {
2651                                 fprintf(stderr,
2652                                         "error: %s: only one obduuid allowed",
2653                                         argv[0]);
2654                                 return CMD_HELP;
2655                         }
2656                         param->fp_obd_uuid = (struct obd_uuid *)optarg;
2657                         break;
2658                 case 'p':
2659                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2660                                 param->fp_verbose |= VERBOSE_POOL;
2661                                 param->fp_max_depth = 0;
2662                         }
2663                         break;
2664                 case 'q':
2665                         param->fp_quiet++;
2666                         break;
2667                 case 'r':
2668                         param->fp_recursive = 1;
2669                         break;
2670                 case 'R':
2671                         param->fp_raw = 1;
2672                         break;
2673 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2674                 case 's':
2675                         fprintf(stderr, "warning: '--size|-s' deprecated, "
2676                                 "use '--stripe-size|-S' instead\n");
2677 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0) */
2678                 case 'S':
2679                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2680                                 param->fp_verbose |= VERBOSE_SIZE;
2681                                 param->fp_max_depth = 0;
2682                         }
2683                         break;
2684                 case 'v':
2685                         param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
2686                         break;
2687                 case 'y':
2688                         param->fp_yaml = 1;
2689                         break;
2690                 default:
2691                         return CMD_HELP;
2692                 }
2693         }
2694
2695         if (optind >= argc)
2696                 return CMD_HELP;
2697
2698         if (param->fp_recursive)
2699                 param->fp_max_depth = -1;
2700         else if (param->fp_verbose & VERBOSE_DETAIL)
2701                 param->fp_max_depth = 1;
2702
2703         if (!param->fp_verbose)
2704                 param->fp_verbose = VERBOSE_DEFAULT;
2705         if (param->fp_quiet)
2706                 param->fp_verbose = VERBOSE_OBJID;
2707
2708         do {
2709                 rc = llapi_getstripe(argv[optind], param);
2710         } while (++optind < argc && !rc);
2711
2712         if (rc)
2713                 fprintf(stderr, "error: %s failed for %s.\n",
2714                         argv[0], argv[optind - 1]);
2715         return rc;
2716 }
2717
2718 static int lfs_tgts(int argc, char **argv)
2719 {
2720         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
2721         struct find_param param;
2722         int index = 0, rc=0;
2723
2724         if (argc > 2)
2725                 return CMD_HELP;
2726
2727         if (argc == 2 && !realpath(argv[1], path)) {
2728                 rc = -errno;
2729                 fprintf(stderr, "error: invalid path '%s': %s\n",
2730                         argv[1], strerror(-rc));
2731                 return rc;
2732         }
2733
2734         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
2735                 /* Check if we have a mount point */
2736                 if (mntdir[0] == '\0')
2737                         continue;
2738
2739                 memset(&param, 0, sizeof(param));
2740                 if (!strcmp(argv[0], "mdts"))
2741                         param.fp_get_lmv = 1;
2742
2743                 rc = llapi_ostlist(mntdir, &param);
2744                 if (rc) {
2745                         fprintf(stderr, "error: %s: failed on %s\n",
2746                                 argv[0], mntdir);
2747                 }
2748                 if (path[0] != '\0')
2749                         break;
2750                 memset(mntdir, 0, PATH_MAX);
2751         }
2752
2753         return rc;
2754 }
2755
2756 static int lfs_getstripe(int argc, char **argv)
2757 {
2758         struct find_param param = { 0 };
2759
2760         param.fp_max_depth = 1;
2761         return lfs_getstripe_internal(argc, argv, &param);
2762 }
2763
2764 /* functions */
2765 static int lfs_getdirstripe(int argc, char **argv)
2766 {
2767         struct find_param param = { 0 };
2768         struct option long_opts[] = {
2769 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2770                 {"mdt-count",   no_argument,            0, 'c'},
2771 #endif
2772                 {"mdt-hash",    no_argument,            0, 'H'},
2773                 {"mdt-index",   no_argument,            0, 'i'},
2774                 {"recursive",   no_argument,            0, 'r'},
2775 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2776                 {"mdt-hash",    no_argument,            0, 't'},
2777 #endif
2778                 {"default",     no_argument,            0, 'D'},
2779                 {"obd",         required_argument,      0, 'O'},
2780                 {"mdt-count",   no_argument,            0, 'T'},
2781                 {"yaml",        no_argument,            0, 'y'},
2782                 {0, 0, 0, 0}
2783         };
2784         int c, rc;
2785
2786         param.fp_get_lmv = 1;
2787
2788         while ((c = getopt_long(argc, argv,
2789                                 "cDHiO:rtTy", long_opts, NULL)) != -1)
2790         {
2791                 switch (c) {
2792                 case 'O':
2793                         if (param.fp_obd_uuid) {
2794                                 fprintf(stderr,
2795                                         "error: %s: only one obduuid allowed",
2796                                         argv[0]);
2797                                 return CMD_HELP;
2798                         }
2799                         param.fp_obd_uuid = (struct obd_uuid *)optarg;
2800                         break;
2801 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2802                 case 'c':
2803 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 10, 50, 0)
2804                         fprintf(stderr, "warning: '-c' deprecated"
2805                                 ", use '-T' instead\n");
2806 #endif
2807 #endif
2808                 case 'T':
2809                         param.fp_verbose |= VERBOSE_COUNT;
2810                         break;
2811                 case 'i':
2812                         param.fp_verbose |= VERBOSE_OFFSET;
2813                         break;
2814 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2815                 case 't':
2816 #endif
2817                 case 'H':
2818                         param.fp_verbose |= VERBOSE_HASH_TYPE;
2819                         break;
2820                 case 'D':
2821                         param.fp_get_default_lmv = 1;
2822                         break;
2823                 case 'r':
2824                         param.fp_recursive = 1;
2825                         break;
2826                 case 'y':
2827                         param.fp_yaml = 1;
2828                         break;
2829                 default:
2830                         return CMD_HELP;
2831                 }
2832         }
2833
2834         if (optind >= argc)
2835                 return CMD_HELP;
2836
2837         if (param.fp_recursive)
2838                 param.fp_max_depth = -1;
2839
2840         if (!param.fp_verbose)
2841                 param.fp_verbose = VERBOSE_DEFAULT;
2842
2843         do {
2844                 rc = llapi_getstripe(argv[optind], &param);
2845         } while (++optind < argc && !rc);
2846
2847         if (rc)
2848                 fprintf(stderr, "error: %s failed for %s.\n",
2849                         argv[0], argv[optind - 1]);
2850         return rc;
2851 }
2852
2853 /* functions */
2854 static int lfs_setdirstripe(int argc, char **argv)
2855 {
2856         char                    *dname;
2857         int                     result;
2858         unsigned int            stripe_offset = -1;
2859         unsigned int            stripe_count = 1;
2860         enum lmv_hash_type      hash_type;
2861         char                    *end;
2862         int                     c;
2863         char                    *stripe_offset_opt = NULL;
2864         char                    *stripe_count_opt = NULL;
2865         char                    *stripe_hash_opt = NULL;
2866         char                    *mode_opt = NULL;
2867         bool                    default_stripe = false;
2868         mode_t                  mode = S_IRWXU | S_IRWXG | S_IRWXO;
2869         mode_t                  previous_mode = 0;
2870         bool                    delete = false;
2871
2872         struct option long_opts[] = {
2873 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2874         { .val = 'c',   .name = "count",        .has_arg = required_argument },
2875 #endif
2876         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument },
2877         { .val = 'd',   .name = "delete",       .has_arg = no_argument },
2878 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2879         { .val = 'i',   .name = "index",        .has_arg = required_argument },
2880 #endif
2881         { .val = 'i',   .name = "mdt-index",    .has_arg = required_argument },
2882         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
2883 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2884         { .val = 't',   .name = "hash-type",    .has_arg = required_argument },
2885         { .val = 't',   .name = "mdt-hash",     .has_arg = required_argument },
2886 #endif
2887                 {"mdt-hash",    required_argument, 0, 'H'},
2888 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2889         { .val = 'D',   .name = "default_stripe",
2890                                                 .has_arg = no_argument },
2891 #endif
2892         { .val = 'D',   .name = "default",      .has_arg = no_argument },
2893         { .name = NULL } };
2894
2895         while ((c = getopt_long(argc, argv, "c:dDi:H:m:t:", long_opts,
2896                                 NULL)) >= 0) {
2897                 switch (c) {
2898                 case 0:
2899                         /* Long options. */
2900                         break;
2901                 case 'c':
2902 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
2903                         if (strcmp(argv[optind - 1], "--count") == 0)
2904                                 fprintf(stderr, "warning: '--count' deprecated"
2905                                         ", use '--mdt-count' instead\n");
2906 #endif
2907                         stripe_count_opt = optarg;
2908                         break;
2909                 case 'd':
2910                         delete = true;
2911                         default_stripe = true;
2912                         break;
2913                 case 'D':
2914                         default_stripe = true;
2915                         break;
2916                 case 'i':
2917 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
2918                         if (strcmp(argv[optind - 1], "--index") == 0)
2919                                 fprintf(stderr, "warning: '--index' deprecated"
2920                                         ", use '--mdt-index' instead\n");
2921 #endif
2922                         stripe_offset_opt = optarg;
2923                         break;
2924                 case 'm':
2925                         mode_opt = optarg;
2926                         break;
2927 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2928                 case 't':
2929 #endif
2930                 case 'H':
2931 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
2932                         if (strcmp(argv[optind - 1], "--hash-type") == 0)
2933                                 fprintf(stderr, "warning: '--hash-type' "
2934                                         "deprecated, use '--mdt-hash' "
2935                                         "instead\n");
2936 #endif
2937                         stripe_hash_opt = optarg;
2938                         break;
2939                 default:
2940                         fprintf(stderr, "error: %s: option '%s' "
2941                                         "unrecognized\n",
2942                                         argv[0], argv[optind - 1]);
2943                         return CMD_HELP;
2944                 }
2945         }
2946
2947         if (optind == argc) {
2948                 fprintf(stderr, "error: %s: missing dirname\n",
2949                         argv[0]);
2950                 return CMD_HELP;
2951         }
2952
2953         if (!delete && stripe_offset_opt == NULL && stripe_count_opt == NULL) {
2954                 fprintf(stderr, "error: %s: missing stripe offset and count.\n",
2955                         argv[0]);
2956                 return CMD_HELP;
2957         }
2958
2959         if (stripe_offset_opt != NULL) {
2960                 /* get the stripe offset */
2961                 stripe_offset = strtoul(stripe_offset_opt, &end, 0);
2962                 if (*end != '\0') {
2963                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
2964                                 argv[0], stripe_offset_opt);
2965                         return CMD_HELP;
2966                 }
2967         }
2968
2969         if (delete) {
2970                 if (stripe_offset_opt != NULL || stripe_count_opt != NULL) {
2971                         fprintf(stderr, "error: %s: cannot specify -d with -s,"
2972                                 " or -i options.\n", argv[0]);
2973                         return CMD_HELP;
2974                 } else {
2975                         stripe_count = 0;
2976                 }
2977         }
2978
2979
2980         if (mode_opt != NULL) {
2981                 mode = strtoul(mode_opt, &end, 8);
2982                 if (*end != '\0') {
2983                         fprintf(stderr, "error: %s: bad mode '%s'\n",
2984                                 argv[0], mode_opt);
2985                         return CMD_HELP;
2986                 }
2987                 previous_mode = umask(0);
2988         }
2989
2990         if (stripe_hash_opt == NULL) {
2991                 hash_type = LMV_HASH_TYPE_FNV_1A_64;
2992         } else {
2993                 hash_type = check_hashtype(stripe_hash_opt);
2994                 if (hash_type == 0) {
2995                         fprintf(stderr,
2996                                 "error: %s: bad stripe hash type '%s'\n",
2997                                 argv[0], stripe_hash_opt);
2998                         return CMD_HELP;
2999                 }
3000         }
3001
3002         /* get the stripe count */
3003         if (stripe_count_opt != NULL) {
3004                 stripe_count = strtoul(stripe_count_opt, &end, 0);
3005                 if (*end != '\0') {
3006                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
3007                                 argv[0], stripe_count_opt);
3008                         return CMD_HELP;
3009                 }
3010         }
3011
3012         dname = argv[optind];
3013         do {
3014                 if (default_stripe) {
3015                         result = llapi_dir_set_default_lmv_stripe(dname,
3016                                                     stripe_offset, stripe_count,
3017                                                     hash_type, NULL);
3018                 } else {
3019                         result = llapi_dir_create_pool(dname, mode,
3020                                                        stripe_offset,
3021                                                        stripe_count, hash_type,
3022                                                        NULL);
3023                 }
3024
3025                 if (result) {
3026                         fprintf(stderr, "error: %s: create stripe dir '%s' "
3027                                 "failed\n", argv[0], dname);
3028                         break;
3029                 }
3030                 dname = argv[++optind];
3031         } while (dname != NULL);
3032
3033         if (mode_opt != NULL)
3034                 umask(previous_mode);
3035
3036         return result;
3037 }
3038
3039 /* functions */
3040 static int lfs_rmentry(int argc, char **argv)
3041 {
3042         char *dname;
3043         int   index;
3044         int   result = 0;
3045
3046         if (argc <= 1) {
3047                 fprintf(stderr, "error: %s: missing dirname\n",
3048                         argv[0]);
3049                 return CMD_HELP;
3050         }
3051
3052         index = 1;
3053         dname = argv[index];
3054         while (dname != NULL) {
3055                 result = llapi_direntry_remove(dname);
3056                 if (result) {
3057                         fprintf(stderr, "error: %s: remove dir entry '%s' "
3058                                 "failed\n", argv[0], dname);
3059                         break;
3060                 }
3061                 dname = argv[++index];
3062         }
3063         return result;
3064 }
3065
3066 static int lfs_mv(int argc, char **argv)
3067 {
3068         struct  find_param param = {
3069                 .fp_max_depth = -1,
3070                 .fp_mdt_index = -1,
3071         };
3072         char   *end;
3073         int     c;
3074         int     rc = 0;
3075         struct option long_opts[] = {
3076         { .val = 'M',   .name = "mdt-index",    .has_arg = required_argument },
3077         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
3078         { .name = NULL } };
3079
3080         while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
3081                 switch (c) {
3082                 case 'M': {
3083                         param.fp_mdt_index = strtoul(optarg, &end, 0);
3084                         if (*end != '\0') {
3085                                 fprintf(stderr, "%s: invalid MDT index'%s'\n",
3086                                         argv[0], optarg);
3087                                 return CMD_HELP;
3088                         }
3089                         break;
3090                 }
3091                 case 'v': {
3092                         param.fp_verbose = VERBOSE_DETAIL;
3093                         break;
3094                 }
3095                 default:
3096                         fprintf(stderr, "error: %s: unrecognized option '%s'\n",
3097                                 argv[0], argv[optind - 1]);
3098                         return CMD_HELP;
3099                 }
3100         }
3101
3102         if (param.fp_mdt_index == -1) {
3103                 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
3104                 return CMD_HELP;
3105         }
3106
3107         if (optind >= argc) {
3108                 fprintf(stderr, "%s: missing operand path\n", argv[0]);
3109                 return CMD_HELP;
3110         }
3111
3112         param.fp_migrate = 1;
3113         rc = llapi_migrate_mdt(argv[optind], &param);
3114         if (rc != 0)
3115                 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
3116                         argv[0], argv[optind], param.fp_mdt_index,
3117                         strerror(-rc));
3118         return rc;
3119 }
3120
3121 static int lfs_osts(int argc, char **argv)
3122 {
3123         return lfs_tgts(argc, argv);
3124 }
3125
3126 static int lfs_mdts(int argc, char **argv)
3127 {
3128         return lfs_tgts(argc, argv);
3129 }
3130
3131 #define COOK(value)                                                     \
3132 ({                                                                      \
3133         int radix = 0;                                                  \
3134         while (value > 1024) {                                          \
3135                 value /= 1024;                                          \
3136                 radix++;                                                \
3137         }                                                               \
3138         radix;                                                          \
3139 })
3140 #define UUF     "%-20s"
3141 #define CSF     "%11s"
3142 #define CDF     "%11llu"
3143 #define HDF     "%8.1f%c"
3144 #define RSF     "%4s"
3145 #define RDF     "%3d%%"
3146
3147 enum mntdf_flags {
3148         MNTDF_INODES    = 0x0001,
3149         MNTDF_COOKED    = 0x0002,
3150         MNTDF_LAZY      = 0x0004,
3151         MNTDF_VERBOSE   = 0x0008,
3152 };
3153
3154 static int showdf(char *mntdir, struct obd_statfs *stat,
3155                   char *uuid, enum mntdf_flags flags,
3156                   char *type, int index, int rc)
3157 {
3158         long long avail, used, total;
3159         double ratio = 0;
3160         char *suffix = "KMGTPEZY";
3161         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
3162         char tbuf[3 * sizeof(__u64)];
3163         char ubuf[3 * sizeof(__u64)];
3164         char abuf[3 * sizeof(__u64)];
3165         char rbuf[3 * sizeof(__u64)];
3166
3167         if (!uuid || !stat)
3168                 return -EINVAL;
3169
3170         switch (rc) {
3171         case 0:
3172                 if (flags & MNTDF_INODES) {
3173                         avail = stat->os_ffree;
3174                         used = stat->os_files - stat->os_ffree;
3175                         total = stat->os_files;
3176                 } else {
3177                         int shift = flags & MNTDF_COOKED ? 0 : 10;
3178
3179                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
3180                         used  = ((stat->os_blocks - stat->os_bfree) *
3181                                  stat->os_bsize) >> shift;
3182                         total = (stat->os_blocks * stat->os_bsize) >> shift;
3183                 }
3184
3185                 if ((used + avail) > 0)
3186                         ratio = (double)used / (double)(used + avail);
3187
3188                 if (flags & MNTDF_COOKED) {
3189                         int i;
3190                         double cook_val;
3191
3192                         cook_val = (double)total;
3193                         i = COOK(cook_val);
3194                         if (i > 0)
3195                                 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
3196                                          suffix[i - 1]);
3197                         else
3198                                 snprintf(tbuf, sizeof(tbuf), CDF, total);
3199
3200                         cook_val = (double)used;
3201                         i = COOK(cook_val);
3202                         if (i > 0)
3203                                 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
3204                                          suffix[i - 1]);
3205                         else
3206                                 snprintf(ubuf, sizeof(ubuf), CDF, used);
3207
3208                         cook_val = (double)avail;
3209                         i = COOK(cook_val);
3210                         if (i > 0)
3211                                 snprintf(abuf, sizeof(abuf), HDF, cook_val,
3212                                          suffix[i - 1]);
3213                         else
3214                                 snprintf(abuf, sizeof(abuf), CDF, avail);
3215                 } else {
3216                         snprintf(tbuf, sizeof(tbuf), CDF, total);
3217                         snprintf(ubuf, sizeof(tbuf), CDF, used);
3218                         snprintf(abuf, sizeof(tbuf), CDF, avail);
3219                 }
3220
3221                 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
3222                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
3223                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
3224                 if (type)
3225                         printf("[%s:%d]", type, index);
3226
3227                 if (stat->os_state) {
3228                         /*
3229                          * Each character represents the matching
3230                          * OS_STATE_* bit.
3231                          */
3232                         const char state_names[] = "DRSI";
3233                         __u32      state;
3234                         __u32      i;
3235
3236                         printf(" ");
3237                         for (i = 0, state = stat->os_state;
3238                              state && i < sizeof(state_names); i++) {
3239                                 if (!(state & (1 << i)))
3240                                         continue;
3241                                 printf("%c", state_names[i]);
3242                                 state ^= 1 << i;
3243                         }
3244                 }
3245
3246                 printf("\n");
3247                 break;
3248         case -ENODATA:
3249                 printf(UUF": inactive device\n", uuid);
3250                 break;
3251         default:
3252                 printf(UUF": %s\n", uuid, strerror(-rc));
3253                 break;
3254         }
3255
3256         return 0;
3257 }
3258
3259 struct ll_stat_type {
3260         int   st_op;
3261         char *st_name;
3262 };
3263
3264 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags)
3265 {
3266         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
3267         struct obd_uuid uuid_buf;
3268         char *poolname = NULL;
3269         struct ll_stat_type types[] = {
3270                 { .st_op = LL_STATFS_LMV,       .st_name = "MDT" },
3271                 { .st_op = LL_STATFS_LOV,       .st_name = "OST" },
3272                 { .st_name = NULL } };
3273         struct ll_stat_type *tp;
3274         __u64 ost_ffree = 0;
3275         __u32 index;
3276         __u32 type;
3277         int fd;
3278         int rc = 0;
3279         int rc2;
3280
3281         if (pool) {
3282                 poolname = strchr(pool, '.');
3283                 if (poolname != NULL) {
3284                         if (strncmp(fsname, pool, strlen(fsname))) {
3285                                 fprintf(stderr, "filesystem name incorrect\n");
3286                                 return -ENODEV;
3287                         }
3288                         poolname++;
3289                 } else
3290                         poolname = pool;
3291         }
3292
3293         fd = open(mntdir, O_RDONLY);
3294         if (fd < 0) {
3295                 rc = -errno;
3296                 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
3297                         strerror(errno));
3298                 return rc;
3299         }
3300
3301         if (flags & MNTDF_INODES)
3302                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
3303                        "UUID", "Inodes", "IUsed", "IFree",
3304                        "IUse%", "Mounted on");
3305         else
3306                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
3307                        "UUID", flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
3308                        "Used", "Available", "Use%", "Mounted on");
3309
3310         for (tp = types; tp->st_name != NULL; tp++) {
3311                 for (index = 0; ; index++) {
3312                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
3313                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
3314                         type = flags & MNTDF_LAZY ?
3315                                 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
3316                         rc2 = llapi_obd_fstatfs(fd, type, index,
3317                                                &stat_buf, &uuid_buf);
3318                         if (rc2 == -ENODEV)
3319                                 break;
3320                         if (rc2 == -EAGAIN)
3321                                 continue;
3322                         if (rc2 == -ENODATA) { /* Inactive device, OK. */
3323                                 if (!(flags & MNTDF_VERBOSE))
3324                                         continue;
3325                         } else if (rc2 < 0 && rc == 0) {
3326                                 rc = rc2;
3327                         }
3328
3329                         if (poolname && tp->st_op == LL_STATFS_LOV &&
3330                             llapi_search_ost(fsname, poolname,
3331                                              obd_uuid2str(&uuid_buf)) != 1)
3332                                 continue;
3333
3334                         /* the llapi_obd_statfs() call may have returned with
3335                          * an error, but if it filled in uuid_buf we will at
3336                          * lease use that to print out a message for that OBD.
3337                          * If we didn't get anything in the uuid_buf, then fill
3338                          * it in so that we can print an error message. */
3339                         if (uuid_buf.uuid[0] == '\0')
3340                                 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
3341                                          "%s%04x", tp->st_name, index);
3342                         showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
3343                                flags, tp->st_name, index, rc2);
3344
3345                         if (rc2 == 0) {
3346                                 if (tp->st_op == LL_STATFS_LMV) {
3347                                         sum.os_ffree += stat_buf.os_ffree;
3348                                         sum.os_files += stat_buf.os_files;
3349                                 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
3350                                         sum.os_blocks += stat_buf.os_blocks *
3351                                                 stat_buf.os_bsize;
3352                                         sum.os_bfree  += stat_buf.os_bfree *
3353                                                 stat_buf.os_bsize;
3354                                         sum.os_bavail += stat_buf.os_bavail *
3355                                                 stat_buf.os_bsize;
3356                                         ost_ffree += stat_buf.os_ffree;
3357                                 }
3358                         }
3359                 }
3360         }
3361
3362         close(fd);
3363
3364         /* If we don't have as many objects free on the OST as inodes
3365          * on the MDS, we reduce the total number of inodes to
3366          * compensate, so that the "inodes in use" number is correct.
3367          * Matches ll_statfs_internal() so the results are consistent. */
3368         if (ost_ffree < sum.os_ffree) {
3369                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
3370                 sum.os_ffree = ost_ffree;
3371         }
3372         printf("\n");
3373         showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
3374         printf("\n");
3375
3376         return rc;
3377 }
3378
3379 static int lfs_df(int argc, char **argv)
3380 {
3381         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3382         enum mntdf_flags flags = 0;
3383         int c, rc = 0, index = 0;
3384         char fsname[PATH_MAX] = "", *pool_name = NULL;
3385         struct option long_opts[] = {
3386         { .val = 'h',   .name = "human-readable",
3387                                                 .has_arg = no_argument },
3388         { .val = 'i',   .name = "inodes",       .has_arg = no_argument },
3389         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
3390         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
3391         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
3392         { .name = NULL} };
3393
3394         while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
3395                 switch (c) {
3396                 case 'h':
3397                         flags |= MNTDF_COOKED;
3398                         break;
3399                 case 'i':
3400                         flags |= MNTDF_INODES;
3401                         break;
3402                 case 'l':
3403                         flags |= MNTDF_LAZY;
3404                         break;
3405                 case 'p':
3406                         pool_name = optarg;
3407                         break;
3408                 case 'v':
3409                         flags |= MNTDF_VERBOSE;
3410                         break;
3411                 default:
3412                         return CMD_HELP;
3413                 }
3414         }
3415         if (optind < argc && !realpath(argv[optind], path)) {
3416                 rc = -errno;
3417                 fprintf(stderr, "error: invalid path '%s': %s\n",
3418                         argv[optind], strerror(-rc));
3419                 return rc;
3420         }
3421
3422         while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
3423                 /* Check if we have a mount point */
3424                 if (mntdir[0] == '\0')
3425                         continue;
3426
3427                 rc = mntdf(mntdir, fsname, pool_name, flags);
3428                 if (rc || path[0] != '\0')
3429                         break;
3430                 fsname[0] = '\0'; /* avoid matching in next loop */
3431                 mntdir[0] = '\0'; /* avoid matching in next loop */
3432         }
3433
3434         return rc;
3435 }
3436
3437 static int lfs_getname(int argc, char **argv)
3438 {
3439         char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
3440         int rc = 0, index = 0, c;
3441         char buf[sizeof(struct obd_uuid)];
3442
3443         while ((c = getopt(argc, argv, "h")) != -1)
3444                 return CMD_HELP;
3445
3446         if (optind == argc) { /* no paths specified, get all paths. */
3447                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
3448                         rc = llapi_getname(mntdir, buf, sizeof(buf));
3449                         if (rc < 0) {
3450                                 fprintf(stderr,
3451                                         "cannot get name for `%s': %s\n",
3452                                         mntdir, strerror(-rc));
3453                                 break;
3454                         }
3455
3456                         printf("%s %s\n", buf, mntdir);
3457
3458                         path[0] = fsname[0] = mntdir[0] = 0;
3459                 }
3460         } else { /* paths specified, only attempt to search these. */
3461                 for (; optind < argc; optind++) {
3462                         rc = llapi_getname(argv[optind], buf, sizeof(buf));
3463                         if (rc < 0) {
3464                                 fprintf(stderr,
3465                                         "cannot get name for `%s': %s\n",
3466                                         argv[optind], strerror(-rc));
3467                                 break;
3468                         }
3469
3470                         printf("%s %s\n", buf, argv[optind]);
3471                 }
3472         }
3473         return rc;
3474 }
3475
3476 static int lfs_check(int argc, char **argv)
3477 {
3478         int rc;
3479         char mntdir[PATH_MAX] = {'\0'};
3480         int num_types = 1;
3481         char *obd_types[2];
3482         char obd_type1[4];
3483         char obd_type2[4];
3484
3485         if (argc != 2)
3486                 return CMD_HELP;
3487
3488         obd_types[0] = obd_type1;
3489         obd_types[1] = obd_type2;
3490
3491         if (strcmp(argv[1], "osts") == 0) {
3492                 strcpy(obd_types[0], "osc");
3493         } else if (strcmp(argv[1], "mds") == 0) {
3494                 strcpy(obd_types[0], "mdc");
3495         } else if (strcmp(argv[1], "servers") == 0) {
3496                 num_types = 2;
3497                 strcpy(obd_types[0], "osc");
3498                 strcpy(obd_types[1], "mdc");
3499         } else {
3500                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3501                                 argv[0], argv[1]);
3502                         return CMD_HELP;
3503         }
3504
3505         rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
3506         if (rc < 0 || mntdir[0] == '\0') {
3507                 fprintf(stderr, "No suitable Lustre mount found\n");
3508                 return rc;
3509         }
3510
3511         rc = llapi_target_check(num_types, obd_types, mntdir);
3512         if (rc)
3513                 fprintf(stderr, "error: %s: %s status failed\n",
3514                                 argv[0],argv[1]);
3515
3516         return rc;
3517
3518 }
3519
3520 #ifdef HAVE_SYS_QUOTA_H
3521 #define ARG2INT(nr, str, msg)                                           \
3522 do {                                                                    \
3523         char *endp;                                                     \
3524         nr = strtol(str, &endp, 0);                                     \
3525         if (*endp) {                                                    \
3526                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
3527                 return CMD_HELP;                                        \
3528         }                                                               \
3529 } while (0)
3530
3531 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
3532
3533 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
3534  * returns the value or ULONG_MAX on integer overflow or incorrect format
3535  * Notes:
3536  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
3537  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
3538  *        3. empty integer value is interpreted as 0
3539  */
3540 static unsigned long str2sec(const char* timestr)
3541 {
3542         const char spec[] = "smhdw";
3543         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
3544         unsigned long val = 0;
3545         char *tail;
3546
3547         if (strpbrk(timestr, spec) == NULL) {
3548                 /* no specifiers inside the time string,
3549                    should treat it as an integer value */
3550                 val = strtoul(timestr, &tail, 10);
3551                 return *tail ? ULONG_MAX : val;
3552         }
3553
3554         /* format string is XXwXXdXXhXXmXXs */
3555         while (*timestr) {
3556                 unsigned long v;
3557                 int ind;
3558                 char* ptr;
3559
3560                 v = strtoul(timestr, &tail, 10);
3561                 if (v == ULONG_MAX || *tail == '\0')
3562                         /* value too large (ULONG_MAX or more)
3563                            or missing specifier */
3564                         goto error;
3565
3566                 ptr = strchr(spec, *tail);
3567                 if (ptr == NULL)
3568                         /* unknown specifier */
3569                         goto error;
3570
3571                 ind = ptr - spec;
3572
3573                 /* check if product will overflow the type */
3574                 if (!(v < ULONG_MAX / mult[ind]))
3575                         goto error;
3576
3577                 ADD_OVERFLOW(val, mult[ind] * v);
3578                 if (val == ULONG_MAX)
3579                         goto error;
3580
3581                 timestr = tail + 1;
3582         }
3583
3584         return val;
3585
3586 error:
3587         return ULONG_MAX;
3588 }
3589
3590 #define ARG2ULL(nr, str, def_units)                                     \
3591 do {                                                                    \
3592         unsigned long long limit, units = def_units;                    \
3593         int rc;                                                         \
3594                                                                         \
3595         rc = llapi_parse_size(str, &limit, &units, 1);                  \
3596         if (rc < 0) {                                                   \
3597                 fprintf(stderr, "error: bad limit value %s\n", str);    \
3598                 return CMD_HELP;                                        \
3599         }                                                               \
3600         nr = limit;                                                     \
3601 } while (0)
3602
3603 static inline int has_times_option(int argc, char **argv)
3604 {
3605         int i;
3606
3607         for (i = 1; i < argc; i++)
3608                 if (!strcmp(argv[i], "-t"))
3609                         return 1;
3610
3611         return 0;
3612 }
3613
3614 int lfs_setquota_times(int argc, char **argv)
3615 {
3616         int c, rc;
3617         struct if_quotactl qctl;
3618         char *mnt, *obd_type = (char *)qctl.obd_type;
3619         struct obd_dqblk *dqb = &qctl.qc_dqblk;
3620         struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
3621         struct option long_opts[] = {
3622         { .val = 'b',   .name = "block-grace",  .has_arg = required_argument },
3623         { .val = 'g',   .name = "group",        .has_arg = no_argument },
3624         { .val = 'i',   .name = "inode-grace",  .has_arg = required_argument },
3625         { .val = 'p',   .name = "projid",       .has_arg = no_argument },
3626         { .val = 't',   .name = "times",        .has_arg = no_argument },
3627         { .val = 'u',   .name = "user",         .has_arg = no_argument },
3628         { .name = NULL } };
3629         int qtype;
3630
3631         memset(&qctl, 0, sizeof(qctl));
3632         qctl.qc_cmd  = LUSTRE_Q_SETINFO;
3633         qctl.qc_type = ALLQUOTA;
3634
3635         while ((c = getopt_long(argc, argv, "b:gi:ptu",
3636                                 long_opts, NULL)) != -1) {
3637                 switch (c) {
3638                 case 'u':
3639                         qtype = USRQUOTA;
3640                         goto quota_type;
3641                 case 'g':
3642                         qtype = GRPQUOTA;
3643                         goto quota_type;
3644                 case 'p':
3645                         qtype = PRJQUOTA;
3646 quota_type:
3647                         if (qctl.qc_type != ALLQUOTA) {
3648                                 fprintf(stderr, "error: -u/g/p can't be used "
3649                                                 "more than once\n");
3650                                 return CMD_HELP;
3651                         }
3652                         qctl.qc_type = qtype;
3653                         break;
3654                 case 'b':
3655                         if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
3656                                 fprintf(stderr, "error: bad block-grace: %s\n",
3657                                         optarg);
3658                                 return CMD_HELP;
3659                         }
3660                         dqb->dqb_valid |= QIF_BTIME;
3661                         break;
3662                 case 'i':
3663                         if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
3664                                 fprintf(stderr, "error: bad inode-grace: %s\n",
3665                                         optarg);
3666                                 return CMD_HELP;
3667                         }
3668                         dqb->dqb_valid |= QIF_ITIME;
3669                         break;
3670                 case 't': /* Yes, of course! */
3671                         break;
3672                 default: /* getopt prints error message for us when opterr != 0 */
3673                         return CMD_HELP;
3674                 }
3675         }
3676
3677         if (qctl.qc_type == ALLQUOTA) {
3678                 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
3679                 return CMD_HELP;
3680         }
3681
3682         if (optind != argc - 1) {
3683                 fprintf(stderr, "error: unexpected parameters encountered\n");
3684                 return CMD_HELP;
3685         }
3686
3687         mnt = argv[optind];
3688         rc = llapi_quotactl(mnt, &qctl);
3689         if (rc) {
3690                 if (*obd_type)
3691                         fprintf(stderr, "%s %s ", obd_type,
3692                                 obd_uuid2str(&qctl.obd_uuid));
3693                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
3694                 return rc;
3695         }
3696
3697         return 0;
3698 }
3699
3700 #define BSLIMIT (1 << 0)
3701 #define BHLIMIT (1 << 1)
3702 #define ISLIMIT (1 << 2)
3703 #define IHLIMIT (1 << 3)
3704
3705 int lfs_setquota(int argc, char **argv)
3706 {
3707         int c, rc;
3708         struct if_quotactl qctl;
3709         char *mnt, *obd_type = (char *)qctl.obd_type;
3710         struct obd_dqblk *dqb = &qctl.qc_dqblk;
3711         struct option long_opts[] = {
3712                 {"block-softlimit", required_argument, 0, 'b'},
3713                 {"block-hardlimit", required_argument, 0, 'B'},
3714                 {"group",           required_argument, 0, 'g'},
3715                 {"inode-softlimit", required_argument, 0, 'i'},
3716                 {"inode-hardlimit", required_argument, 0, 'I'},
3717                 {"user",            required_argument, 0, 'u'},
3718                 {"projid",         required_argument, 0, 'p'},
3719                 {0, 0, 0, 0}
3720         };
3721         unsigned limit_mask = 0;
3722         char *endptr;
3723         int qtype;
3724
3725         if (has_times_option(argc, argv))
3726                 return lfs_setquota_times(argc, argv);
3727
3728         memset(&qctl, 0, sizeof(qctl));
3729         qctl.qc_cmd  = LUSTRE_Q_SETQUOTA;
3730         qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
3731                                   * so it can be used as a marker that qc_type
3732                                   * isn't reinitialized from command line */
3733
3734         while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
3735                 long_opts, NULL)) != -1) {
3736                 switch (c) {
3737                 case 'u':
3738                         qtype = USRQUOTA;
3739                         rc = name2uid(&qctl.qc_id, optarg);
3740                         goto quota_type;
3741                 case 'g':
3742                         qtype = GRPQUOTA;
3743                         rc = name2gid(&qctl.qc_id, optarg);
3744                         goto quota_type;
3745                 case 'p':
3746                         qtype = PRJQUOTA;
3747                         rc = name2projid(&qctl.qc_id, optarg);
3748 quota_type:
3749                         if (qctl.qc_type != ALLQUOTA) {
3750                                 fprintf(stderr, "error: -u and -g can't be used"
3751                                                 " more than once\n");
3752                                 return CMD_HELP;
3753                         }
3754                         qctl.qc_type = qtype;
3755                         if (rc) {
3756                                 qctl.qc_id = strtoul(optarg, &endptr, 10);
3757                                 if (*endptr != '\0') {
3758                                         fprintf(stderr, "error: can't find id "
3759                                                 "for name %s\n", optarg);
3760                                         return CMD_HELP;
3761                                 }
3762                         }
3763                         break;
3764                 case 'b':
3765                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
3766                         dqb->dqb_bsoftlimit >>= 10;
3767                         limit_mask |= BSLIMIT;
3768                         if (dqb->dqb_bsoftlimit &&
3769                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
3770                                 fprintf(stderr, "warning: block softlimit is "
3771                                         "smaller than the miminal qunit size, "
3772                                         "please see the help of setquota or "
3773                                         "Lustre manual for details.\n");
3774                         break;
3775                 case 'B':
3776                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
3777                         dqb->dqb_bhardlimit >>= 10;
3778                         limit_mask |= BHLIMIT;
3779                         if (dqb->dqb_bhardlimit &&
3780                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
3781                                 fprintf(stderr, "warning: block hardlimit is "
3782                                         "smaller than the miminal qunit size, "
3783                                         "please see the help of setquota or "
3784                                         "Lustre manual for details.\n");
3785                         break;
3786                 case 'i':
3787                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
3788                         limit_mask |= ISLIMIT;
3789                         if (dqb->dqb_isoftlimit &&
3790                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
3791                                 fprintf(stderr, "warning: inode softlimit is "
3792                                         "smaller than the miminal qunit size, "
3793                                         "please see the help of setquota or "
3794                                         "Lustre manual for details.\n");
3795                         break;
3796                 case 'I':
3797                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
3798                         limit_mask |= IHLIMIT;
3799                         if (dqb->dqb_ihardlimit &&
3800                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
3801                                 fprintf(stderr, "warning: inode hardlimit is "
3802                                         "smaller than the miminal qunit size, "
3803                                         "please see the help of setquota or "
3804                                         "Lustre manual for details.\n");
3805                         break;
3806                 default: /* getopt prints error message for us when opterr != 0 */
3807                         return CMD_HELP;
3808                 }
3809         }
3810
3811         if (qctl.qc_type == ALLQUOTA) {
3812                 fprintf(stderr, "error: neither -u, -g nor -p was specified\n");
3813                 return CMD_HELP;
3814         }
3815
3816         if (limit_mask == 0) {
3817                 fprintf(stderr, "error: at least one limit must be specified\n");
3818                 return CMD_HELP;
3819         }
3820
3821         if (optind != argc - 1) {
3822                 fprintf(stderr, "error: unexpected parameters encountered\n");
3823                 return CMD_HELP;
3824         }
3825
3826         mnt = argv[optind];
3827
3828         if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
3829             (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
3830                 /* sigh, we can't just set blimits/ilimits */
3831                 struct if_quotactl tmp_qctl = {.qc_cmd  = LUSTRE_Q_GETQUOTA,
3832                                                .qc_type = qctl.qc_type,
3833                                                .qc_id   = qctl.qc_id};
3834
3835                 rc = llapi_quotactl(mnt, &tmp_qctl);
3836                 if (rc < 0) {
3837                         fprintf(stderr, "error: setquota failed while retrieving"
3838                                         " current quota settings (%s)\n",
3839                                         strerror(-rc));
3840                         return rc;
3841                 }
3842
3843                 if (!(limit_mask & BHLIMIT))
3844                         dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
3845                 if (!(limit_mask & BSLIMIT))
3846                         dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
3847                 if (!(limit_mask & IHLIMIT))
3848                         dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
3849                 if (!(limit_mask & ISLIMIT))
3850                         dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
3851
3852                 /* Keep grace times if we have got no softlimit arguments */
3853                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
3854                         dqb->dqb_valid |= QIF_BTIME;
3855                         dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
3856                 }
3857
3858                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
3859                         dqb->dqb_valid |= QIF_ITIME;
3860                         dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
3861                 }
3862         }
3863
3864         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
3865         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
3866
3867         rc = llapi_quotactl(mnt, &qctl);
3868         if (rc) {
3869                 if (*obd_type)
3870                         fprintf(stderr, "%s %s ", obd_type,
3871                                 obd_uuid2str(&qctl.obd_uuid));
3872                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
3873                 return rc;
3874         }
3875
3876         return 0;
3877 }
3878
3879 /* Converts seconds value into format string
3880  * result is returned in buf
3881  * Notes:
3882  *        1. result is in descenting order: 1w2d3h4m5s
3883  *        2. zero fields are not filled (except for p. 3): 5d1s
3884  *        3. zero seconds value is presented as "0s"
3885  */
3886 static char * __sec2str(time_t seconds, char *buf)
3887 {
3888         const char spec[] = "smhdw";
3889         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
3890         unsigned long c;
3891         char *tail = buf;
3892         int i;
3893
3894         for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
3895                 c = seconds / mult[i];
3896
3897                 if (c > 0 || (i == 0 && buf == tail))
3898                         tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
3899
3900                 seconds %= mult[i];
3901         }
3902
3903         return tail;
3904 }
3905
3906 static void sec2str(time_t seconds, char *buf, int rc)
3907 {
3908         char *tail = buf;
3909
3910         if (rc)
3911                 *tail++ = '[';
3912
3913         tail = __sec2str(seconds, tail);
3914
3915         if (rc && tail - buf < 39) {
3916                 *tail++ = ']';
3917                 *tail++ = 0;
3918         }
3919 }
3920
3921 static void diff2str(time_t seconds, char *buf, time_t now)
3922 {
3923
3924         buf[0] = 0;
3925         if (!seconds)
3926                 return;
3927         if (seconds <= now) {
3928                 strcpy(buf, "none");
3929                 return;
3930         }
3931         __sec2str(seconds - now, buf);
3932 }
3933
3934 static void print_quota_title(char *name, struct if_quotactl *qctl,
3935                               bool human_readable)
3936 {
3937         printf("Disk quotas for %s %s (%cid %u):\n",
3938                qtype_name(qctl->qc_type), name,
3939                *qtype_name(qctl->qc_type), qctl->qc_id);
3940         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
3941                "Filesystem", human_readable ? "used" : "kbytes",
3942                "quota", "limit", "grace",
3943                "files", "quota", "limit", "grace");
3944 }
3945
3946 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
3947 {
3948         if (!h) {
3949                 snprintf(buf, buflen, "%ju", (uintmax_t)num);
3950         } else {
3951                 if (num >> 40)
3952                         snprintf(buf, buflen, "%5.4gP",
3953                                  (double)num / ((__u64)1 << 40));
3954                 else if (num >> 30)
3955                         snprintf(buf, buflen, "%5.4gT",
3956                                  (double)num / (1 << 30));
3957                 else if (num >> 20)
3958                         snprintf(buf, buflen, "%5.4gG",
3959                                  (double)num / (1 << 20));
3960                 else if (num >> 10)
3961                         snprintf(buf, buflen, "%5.4gM",
3962                                  (double)num / (1 << 10));
3963                 else
3964                         snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
3965         }
3966 }
3967
3968 #define STRBUF_LEN      32
3969 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
3970                         int rc, bool h)
3971 {
3972         time_t now;
3973
3974         time(&now);
3975
3976         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
3977                 int bover = 0, iover = 0;
3978                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
3979                 char numbuf[3][STRBUF_LEN];
3980                 char timebuf[40];
3981                 char strbuf[STRBUF_LEN];
3982
3983                 if (dqb->dqb_bhardlimit &&
3984                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
3985                         bover = 1;
3986                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
3987                         if (dqb->dqb_btime > now) {
3988                                 bover = 2;
3989                         } else {
3990                                 bover = 3;
3991                         }
3992                 }
3993
3994                 if (dqb->dqb_ihardlimit &&
3995                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
3996                         iover = 1;
3997                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
3998                         if (dqb->dqb_itime > now) {
3999                                 iover = 2;
4000                         } else {
4001                                 iover = 3;
4002                         }
4003                 }
4004
4005
4006                 if (strlen(mnt) > 15)
4007                         printf("%s\n%15s", mnt, "");
4008                 else
4009                         printf("%15s", mnt);
4010
4011                 if (bover)
4012                         diff2str(dqb->dqb_btime, timebuf, now);
4013
4014                 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
4015                            strbuf, sizeof(strbuf), h);
4016                 if (rc == -EREMOTEIO)
4017                         sprintf(numbuf[0], "%s*", strbuf);
4018                 else
4019                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
4020                                 "%s" : "[%s]", strbuf);
4021
4022                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
4023                 if (type == QC_GENERAL)
4024                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
4025                                 "%s" : "[%s]", strbuf);
4026                 else
4027                         sprintf(numbuf[1], "%s", "-");
4028
4029                 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
4030                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
4031                         "%s" : "[%s]", strbuf);
4032
4033                 printf(" %7s%c %6s %7s %7s",
4034                        numbuf[0], bover ? '*' : ' ', numbuf[1],
4035                        numbuf[2], bover > 1 ? timebuf : "-");
4036
4037                 if (iover)
4038                         diff2str(dqb->dqb_itime, timebuf, now);
4039
4040                 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
4041                         "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
4042
4043                 if (type == QC_GENERAL)
4044                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
4045                                 "%ju" : "[%ju]",
4046                                 (uintmax_t)dqb->dqb_isoftlimit);
4047                 else
4048                         sprintf(numbuf[1], "%s", "-");
4049
4050                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
4051                         "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
4052
4053                 if (type != QC_OSTIDX)
4054                         printf(" %7s%c %6s %7s %7s",
4055                                numbuf[0], iover ? '*' : ' ', numbuf[1],
4056                                numbuf[2], iover > 1 ? timebuf : "-");
4057                 else
4058                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
4059                 printf("\n");
4060
4061         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
4062                    qctl->qc_cmd == Q_GETOINFO) {
4063                 char bgtimebuf[40];
4064                 char igtimebuf[40];
4065
4066                 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
4067                 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
4068                 printf("Block grace time: %s; Inode grace time: %s\n",
4069                        bgtimebuf, igtimebuf);
4070         }
4071 }
4072
4073 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
4074                            bool h, __u64 *total)
4075 {
4076         int rc = 0, rc1 = 0, count = 0;
4077         __u32 valid = qctl->qc_valid;
4078
4079         rc = llapi_get_obd_count(mnt, &count, is_mdt);
4080         if (rc) {
4081                 fprintf(stderr, "can not get %s count: %s\n",
4082                         is_mdt ? "mdt": "ost", strerror(-rc));
4083                 return rc;
4084         }
4085
4086         for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
4087                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
4088                 rc = llapi_quotactl(mnt, qctl);
4089                 if (rc) {
4090                         /* It is remote client case. */
4091                         if (rc == -EOPNOTSUPP) {
4092                                 rc = 0;
4093                                 goto out;
4094                         }
4095
4096                         if (!rc1)
4097                                 rc1 = rc;
4098                         fprintf(stderr, "quotactl %s%d failed.\n",
4099                                 is_mdt ? "mdt": "ost", qctl->qc_idx);
4100                         continue;
4101                 }
4102
4103                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
4104                             qctl->qc_valid, 0, h);
4105                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
4106                                    qctl->qc_dqblk.dqb_bhardlimit;
4107         }
4108 out:
4109         qctl->qc_valid = valid;
4110         return rc ? : rc1;
4111 }
4112
4113 static int lfs_quota(int argc, char **argv)
4114 {
4115         int c;
4116         char *mnt, *name = NULL;
4117         struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
4118                                     .qc_type = ALLQUOTA };
4119         char *obd_type = (char *)qctl.obd_type;
4120         char *obd_uuid = (char *)qctl.obd_uuid.uuid;
4121         int rc = 0, rc1 = 0, rc2 = 0, rc3 = 0,
4122             verbose = 0, pass = 0, quiet = 0, inacc;
4123         char *endptr;
4124         __u32 valid = QC_GENERAL, idx = 0;
4125         __u64 total_ialloc = 0, total_balloc = 0;
4126         bool human_readable = false;
4127         int qtype;
4128
4129         while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
4130                 switch (c) {
4131                 case 'u':
4132                         qtype = USRQUOTA;
4133                         goto quota_type;
4134                 case 'g':
4135                         qtype = GRPQUOTA;
4136                         goto quota_type;
4137                 case 'p':
4138                         qtype = PRJQUOTA;
4139 quota_type:
4140                         if (qctl.qc_type != ALLQUOTA) {
4141                                 fprintf(stderr, "error: use either -u or -g\n");
4142                                 return CMD_HELP;
4143                         }
4144                         qctl.qc_type = qtype;
4145                         break;
4146                 case 't':
4147                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
4148                         break;
4149                 case 'o':
4150                         valid = qctl.qc_valid = QC_UUID;
4151                         strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
4152                         break;
4153                 case 'i':
4154                         valid = qctl.qc_valid = QC_MDTIDX;
4155                         idx = qctl.qc_idx = atoi(optarg);
4156                         break;
4157                 case 'I':
4158                         valid = qctl.qc_valid = QC_OSTIDX;
4159                         idx = qctl.qc_idx = atoi(optarg);
4160                         break;
4161                 case 'v':
4162                         verbose = 1;
4163                         break;
4164                 case 'q':
4165                         quiet = 1;
4166                         break;
4167                 case 'h':
4168                         human_readable = true;
4169                         break;
4170                 default:
4171                         fprintf(stderr, "error: %s: option '-%c' "
4172                                         "unrecognized\n", argv[0], c);
4173                         return CMD_HELP;
4174                 }
4175         }
4176
4177         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
4178         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
4179             optind == argc - 1) {
4180 all_output:
4181                 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
4182                 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
4183                 qctl.qc_valid = valid;
4184                 qctl.qc_idx = idx;
4185                 qctl.qc_type = pass;
4186                 switch (qctl.qc_type) {
4187                 case USRQUOTA:
4188                         qctl.qc_id = geteuid();
4189                         rc = uid2name(&name, qctl.qc_id);
4190                         break;
4191                 case GRPQUOTA:
4192                         qctl.qc_id = getegid();
4193                         rc = gid2name(&name, qctl.qc_id);
4194                         break;
4195                 default:
4196                         rc = -ENOTSUP;
4197                         break;
4198                 }
4199                 if (rc)
4200                         name = "<unknown>";
4201                 pass++;
4202         /* lfs quota -u username /path/to/lustre/mount */
4203         } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
4204                 /* options should be followed by u/g-name and mntpoint */
4205                 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
4206                         fprintf(stderr, "error: missing quota argument(s)\n");
4207                         return CMD_HELP;
4208                 }
4209
4210                 name = argv[optind++];
4211                 switch (qctl.qc_type) {
4212                 case USRQUOTA:
4213                         rc = name2uid(&qctl.qc_id, name);
4214                         break;
4215                 case GRPQUOTA:
4216                         rc = name2gid(&qctl.qc_id, name);
4217                         break;
4218                 case PRJQUOTA:
4219                         rc = name2projid(&qctl.qc_id, name);
4220                         break;
4221                 default:
4222                         rc = -ENOTSUP;
4223                         break;
4224                 }
4225                 if (rc) {
4226                         qctl.qc_id = strtoul(name, &endptr, 10);
4227                         if (*endptr != '\0') {
4228                                 fprintf(stderr, "error: can't find id for name: %s\n",
4229                                                 name);
4230                                 return CMD_HELP;
4231                         }
4232                 }
4233         } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
4234                 fprintf(stderr, "error: missing quota info argument(s)\n");
4235                 return CMD_HELP;
4236         }
4237
4238         mnt = argv[optind];
4239         rc1 = llapi_quotactl(mnt, &qctl);
4240         if (rc1 < 0) {
4241                 switch (rc1) {
4242                 case -ESRCH:
4243                         fprintf(stderr, "%s quotas are not enabled.\n",
4244                                 qtype_name(qctl.qc_type));
4245                         goto out;
4246                 case -EPERM:
4247                         fprintf(stderr, "Permission denied.\n");
4248                 case -ENODEV:
4249                 case -ENOENT:
4250                         /* We already got error message. */
4251                         goto out;
4252                 default:
4253                         fprintf(stderr, "Unexpected quotactl error: %s\n",
4254                                 strerror(-rc1));
4255                 }
4256         }
4257
4258         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
4259                 print_quota_title(name, &qctl, human_readable);
4260
4261         if (rc1 && *obd_type)
4262                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
4263
4264         if (qctl.qc_valid != QC_GENERAL)
4265                 mnt = "";
4266
4267         inacc = (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) &&
4268                 ((qctl.qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
4269                  (QIF_LIMITS|QIF_USAGE));
4270
4271         print_quota(mnt, &qctl, QC_GENERAL, rc1, human_readable);
4272
4273         if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO &&
4274             verbose) {
4275                 char strbuf[STRBUF_LEN];
4276
4277                 rc2 = print_obd_quota(mnt, &qctl, 1, human_readable,
4278                                       &total_ialloc);
4279                 rc3 = print_obd_quota(mnt, &qctl, 0, human_readable,
4280                                       &total_balloc);
4281                 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
4282                            human_readable);
4283                 printf("Total allocated inode limit: %ju, total "
4284                        "allocated block limit: %s\n", (uintmax_t)total_ialloc,
4285                        strbuf);
4286         }
4287
4288         if (rc1 || rc2 || rc3 || inacc)
4289                 printf("Some errors happened when getting quota info. "
4290                        "Some devices may be not working or deactivated. "
4291                        "The data in \"[]\" is inaccurate.\n");
4292
4293 out:
4294         if (pass > 0 && pass < LL_MAXQUOTAS)
4295                 goto all_output;
4296
4297         return rc1;
4298 }
4299 #endif /* HAVE_SYS_QUOTA_H! */
4300
4301 static int flushctx_ioctl(char *mp)
4302 {
4303         int fd, rc;
4304
4305         fd = open(mp, O_RDONLY);
4306         if (fd == -1) {
4307                 fprintf(stderr, "flushctx: error open %s: %s\n",
4308                         mp, strerror(errno));
4309                 return -1;
4310         }
4311
4312         rc = ioctl(fd, LL_IOC_FLUSHCTX);
4313         if (rc == -1)
4314                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
4315                         mp, strerror(errno));
4316
4317         close(fd);
4318         return rc;
4319 }
4320
4321 static int lfs_flushctx(int argc, char **argv)
4322 {
4323         int     kdestroy = 0, c;
4324         char    mntdir[PATH_MAX] = {'\0'};
4325         int     index = 0;
4326         int     rc = 0;
4327
4328         while ((c = getopt(argc, argv, "k")) != -1) {
4329                 switch (c) {
4330                 case 'k':
4331                         kdestroy = 1;
4332                         break;
4333                 default:
4334                         fprintf(stderr, "error: %s: option '-%c' "
4335                                         "unrecognized\n", argv[0], c);
4336                         return CMD_HELP;
4337                 }
4338         }
4339
4340         if (kdestroy) {
4341             if ((rc = system("kdestroy > /dev/null")) != 0) {
4342                 rc = WEXITSTATUS(rc);
4343                 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
4344             }
4345         }
4346
4347         if (optind >= argc) {
4348                 /* flush for all mounted lustre fs. */
4349                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
4350                         /* Check if we have a mount point */
4351                         if (mntdir[0] == '\0')
4352                                 continue;
4353
4354                         if (flushctx_ioctl(mntdir))
4355                                 rc = -1;
4356
4357                         mntdir[0] = '\0'; /* avoid matching in next loop */
4358                 }
4359         } else {
4360                 /* flush fs as specified */
4361                 while (optind < argc) {
4362                         if (flushctx_ioctl(argv[optind++]))
4363                                 rc = -1;
4364                 }
4365         }
4366         return rc;
4367 }
4368
4369 static int lfs_cp(int argc, char **argv)
4370 {
4371         fprintf(stderr, "remote client copy file(s).\n"
4372                 "obsolete, does not support it anymore.\n");
4373         return 0;
4374 }
4375
4376 static int lfs_ls(int argc, char **argv)
4377 {
4378         fprintf(stderr, "remote client lists directory contents.\n"
4379                 "obsolete, does not support it anymore.\n");
4380         return 0;
4381 }
4382
4383 static int lfs_changelog(int argc, char **argv)
4384 {
4385         void *changelog_priv;
4386         struct changelog_rec *rec;
4387         long long startrec = 0, endrec = 0;
4388         char *mdd;
4389         struct option long_opts[] = {
4390                 {"follow", no_argument, 0, 'f'},
4391                 {0, 0, 0, 0}
4392         };
4393         char short_opts[] = "f";
4394         int rc, follow = 0;
4395
4396         while ((rc = getopt_long(argc, argv, short_opts,
4397                                 long_opts, NULL)) != -1) {
4398                 switch (rc) {
4399                 case 'f':
4400                         follow++;
4401                         break;
4402                 case '?':
4403                         return CMD_HELP;
4404                 default:
4405                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4406                                 argv[0], argv[optind - 1]);
4407                         return CMD_HELP;
4408                 }
4409         }
4410         if (optind >= argc)
4411                 return CMD_HELP;
4412
4413         mdd = argv[optind++];
4414         if (argc > optind)
4415                 startrec = strtoll(argv[optind++], NULL, 10);
4416         if (argc > optind)
4417                 endrec = strtoll(argv[optind++], NULL, 10);
4418
4419         rc = llapi_changelog_start(&changelog_priv,
4420                                    CHANGELOG_FLAG_BLOCK |
4421                                    CHANGELOG_FLAG_JOBID |
4422                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
4423                                    mdd, startrec);
4424         if (rc < 0) {
4425                 fprintf(stderr, "Can't start changelog: %s\n",
4426                         strerror(errno = -rc));
4427                 return rc;
4428         }
4429
4430         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
4431                 time_t secs;
4432                 struct tm ts;
4433
4434                 if (endrec && rec->cr_index > endrec) {
4435                         llapi_changelog_free(&rec);
4436                         break;
4437                 }
4438                 if (rec->cr_index < startrec) {
4439                         llapi_changelog_free(&rec);
4440                         continue;
4441                 }
4442
4443                 secs = rec->cr_time >> 30;
4444                 gmtime_r(&secs, &ts);
4445                 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
4446                        "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
4447                        changelog_type2str(rec->cr_type),
4448                        ts.tm_hour, ts.tm_min, ts.tm_sec,
4449                        (int)(rec->cr_time & ((1 << 30) - 1)),
4450                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
4451                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
4452
4453                 if (rec->cr_flags & CLF_JOBID) {
4454                         struct changelog_ext_jobid *jid =
4455                                 changelog_rec_jobid(rec);
4456
4457                         if (jid->cr_jobid[0] != '\0')
4458                                 printf(" j=%s", jid->cr_jobid);
4459                 }
4460
4461                 if (rec->cr_namelen)
4462                         printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
4463                                rec->cr_namelen, changelog_rec_name(rec));
4464
4465                 if (rec->cr_flags & CLF_RENAME) {
4466                         struct changelog_ext_rename *rnm =
4467                                 changelog_rec_rename(rec);
4468
4469                         if (!fid_is_zero(&rnm->cr_sfid))
4470                                 printf(" s="DFID" sp="DFID" %.*s",
4471                                        PFID(&rnm->cr_sfid),
4472                                        PFID(&rnm->cr_spfid),
4473                                        (int)changelog_rec_snamelen(rec),
4474                                        changelog_rec_sname(rec));
4475                 }
4476                 printf("\n");
4477
4478                 llapi_changelog_free(&rec);
4479         }
4480
4481         llapi_changelog_fini(&changelog_priv);
4482
4483         if (rc < 0)
4484                 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
4485
4486         return (rc == 1 ? 0 : rc);
4487 }
4488
4489 static int lfs_changelog_clear(int argc, char **argv)
4490 {
4491         long long endrec;
4492         int rc;
4493
4494         if (argc != 4)
4495                 return CMD_HELP;
4496
4497         endrec = strtoll(argv[3], NULL, 10);
4498
4499         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
4500
4501         if (rc == -EINVAL)
4502                 fprintf(stderr, "%s: record out of range: %llu\n",
4503                         argv[0], endrec);
4504         else if (rc == -ENOENT)
4505                 fprintf(stderr, "%s: no changelog user: %s\n",
4506                         argv[0], argv[2]);
4507         else if (rc)
4508                 fprintf(stderr, "%s error: %s\n", argv[0],
4509                         strerror(-rc));
4510
4511         if (rc)
4512                 errno = -rc;
4513
4514         return rc;
4515 }
4516
4517 static int lfs_fid2path(int argc, char **argv)
4518 {
4519         struct option long_opts[] = {
4520                 { .val = 'c',   .name = "cur",  .has_arg = no_argument },
4521                 { .val = 'l',   .name = "link", .has_arg = required_argument },
4522                 { .val = 'r',   .name = "rec",  .has_arg = required_argument },
4523                 { .name = NULL } };
4524         char  short_opts[] = "cl:r:";
4525         char *device, *fid, *path;
4526         long long recno = -1;
4527         int linkno = -1;
4528         int lnktmp;
4529         int printcur = 0;
4530         int rc = 0;
4531
4532         while ((rc = getopt_long(argc, argv, short_opts,
4533                 long_opts, NULL)) != -1) {
4534                 switch (rc) {
4535                 case 'c':
4536                         printcur++;
4537                         break;
4538                 case 'l':
4539                         linkno = strtol(optarg, NULL, 10);
4540                         break;
4541                 case 'r':
4542                         recno = strtoll(optarg, NULL, 10);
4543                         break;
4544                 case '?':
4545                         return CMD_HELP;
4546                 default:
4547                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4548                                 argv[0], argv[optind - 1]);
4549                         return CMD_HELP;
4550                 }
4551         }
4552
4553         if (argc < 3)
4554                 return CMD_HELP;
4555
4556         device = argv[optind++];
4557         path = calloc(1, PATH_MAX);
4558         if (path == NULL) {
4559                 fprintf(stderr, "error: Not enough memory\n");
4560                 return -errno;
4561         }
4562
4563         rc = 0;
4564         while (optind < argc) {
4565                 fid = argv[optind++];
4566
4567                 lnktmp = (linkno >= 0) ? linkno : 0;
4568                 while (1) {
4569                         int oldtmp = lnktmp;
4570                         long long rectmp = recno;
4571                         int rc2;
4572                         rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
4573                                              &rectmp, &lnktmp);
4574                         if (rc2 < 0) {
4575                                 fprintf(stderr, "%s: error on FID %s: %s\n",
4576                                         argv[0], fid, strerror(errno = -rc2));
4577                                 if (rc == 0)
4578                                         rc = rc2;
4579                                 break;
4580                         }
4581
4582                         if (printcur)
4583                                 fprintf(stdout, "%lld ", rectmp);
4584                         if (device[0] == '/') {
4585                                 fprintf(stdout, "%s", device);
4586                                 if (device[strlen(device) - 1] != '/')
4587                                         fprintf(stdout, "/");
4588                         } else if (path[0] == '\0') {
4589                                 fprintf(stdout, "/");
4590                         }
4591                         fprintf(stdout, "%s\n", path);
4592
4593                         if (linkno >= 0)
4594                                 /* specified linkno */
4595                                 break;
4596                         if (oldtmp == lnktmp)
4597                                 /* no more links */
4598                                 break;
4599                 }
4600         }
4601
4602         free(path);
4603         return rc;
4604 }
4605
4606 static int lfs_path2fid(int argc, char **argv)
4607 {
4608         struct option long_opts[] = {
4609                 { .val = 'p', .name = "parents", .has_arg = no_argument },
4610                 { .name = NULL } };
4611         char            **path;
4612         const char        short_opts[] = "p";
4613         const char       *sep = "";
4614         lustre_fid        fid;
4615         int               rc = 0;
4616         bool              show_parents = false;
4617
4618         while ((rc = getopt_long(argc, argv, short_opts,
4619                                  long_opts, NULL)) != -1) {
4620                 switch (rc) {
4621                 case 'p':
4622                         show_parents = true;
4623                         break;
4624                 default:
4625                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4626                                 argv[0], argv[optind - 1]);
4627                         return CMD_HELP;
4628                 }
4629         }
4630
4631         if (optind > argc - 1)
4632                 return CMD_HELP;
4633         else if (optind < argc - 1)
4634                 sep = ": ";
4635
4636         rc = 0;
4637         for (path = argv + optind; *path != NULL; path++) {
4638                 int err = 0;
4639                 if (!show_parents) {
4640                         err = llapi_path2fid(*path, &fid);
4641                         if (!err)
4642                                 printf("%s%s"DFID"\n",
4643                                        *sep != '\0' ? *path : "", sep,
4644                                        PFID(&fid));
4645                 } else {
4646                         char            name[NAME_MAX + 1];
4647                         unsigned int    linkno = 0;
4648
4649                         while ((err = llapi_path2parent(*path, linkno, &fid,
4650                                                 name, sizeof(name))) == 0) {
4651                                 if (*sep != '\0' && linkno == 0)
4652                                         printf("%s%s", *path, sep);
4653
4654                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
4655                                        PFID(&fid), name);
4656                                 linkno++;
4657                         }
4658
4659                         /* err == -ENODATA is end-of-loop */
4660                         if (linkno > 0 && err == -ENODATA) {
4661                                 printf("\n");
4662                                 err = 0;
4663                         }
4664                 }
4665
4666                 if (err) {
4667                         fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
4668                                 argv[0], show_parents ? "parent " : "", *path,
4669                                 strerror(-err));
4670                         if (rc == 0) {
4671                                 rc = err;
4672                                 errno = -err;
4673                         }
4674                 }
4675         }
4676
4677         return rc;
4678 }
4679
4680 static int lfs_data_version(int argc, char **argv)
4681 {
4682         char *path;
4683         __u64 data_version;
4684         int fd;
4685         int rc;
4686         int c;
4687         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
4688
4689         if (argc < 2)
4690                 return CMD_HELP;
4691
4692         while ((c = getopt(argc, argv, "nrw")) != -1) {
4693                 switch (c) {
4694                 case 'n':
4695                         data_version_flags = 0;
4696                         break;
4697                 case 'r':
4698                         data_version_flags |= LL_DV_RD_FLUSH;
4699                         break;
4700                 case 'w':
4701                         data_version_flags |= LL_DV_WR_FLUSH;
4702                         break;
4703                 default:
4704                         return CMD_HELP;
4705                 }
4706         }
4707         if (optind == argc)
4708                 return CMD_HELP;
4709
4710         path = argv[optind];
4711         fd = open(path, O_RDONLY);
4712         if (fd < 0)
4713                 err(errno, "cannot open file %s", path);
4714
4715         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
4716         if (rc < 0)
4717                 err(errno, "cannot get version for %s", path);
4718         else
4719                 printf("%ju" "\n", (uintmax_t)data_version);
4720
4721         close(fd);
4722         return rc;
4723 }
4724
4725 static int lfs_hsm_state(int argc, char **argv)
4726 {
4727         int rc;
4728         int i = 1;
4729         char *path;
4730         struct hsm_user_state hus;
4731
4732         if (argc < 2)
4733                 return CMD_HELP;
4734
4735         do {
4736                 path = argv[i];
4737
4738                 rc = llapi_hsm_state_get(path, &hus);
4739                 if (rc) {
4740                         fprintf(stderr, "can't get hsm state for %s: %s\n",
4741                                 path, strerror(errno = -rc));
4742                         return rc;
4743                 }
4744
4745                 /* Display path name and status flags */
4746                 printf("%s: (0x%08x)", path, hus.hus_states);
4747
4748                 if (hus.hus_states & HS_RELEASED)
4749                         printf(" released");
4750                 if (hus.hus_states & HS_EXISTS)
4751                         printf(" exists");
4752                 if (hus.hus_states & HS_DIRTY)
4753                         printf(" dirty");
4754                 if (hus.hus_states & HS_ARCHIVED)
4755                         printf(" archived");
4756                 /* Display user-settable flags */
4757                 if (hus.hus_states & HS_NORELEASE)
4758                         printf(" never_release");
4759                 if (hus.hus_states & HS_NOARCHIVE)
4760                         printf(" never_archive");
4761                 if (hus.hus_states & HS_LOST)
4762                         printf(" lost_from_hsm");
4763
4764                 if (hus.hus_archive_id != 0)
4765                         printf(", archive_id:%d", hus.hus_archive_id);
4766                 printf("\n");
4767
4768         } while (++i < argc);
4769
4770         return 0;
4771 }
4772
4773 #define LFS_HSM_SET   0
4774 #define LFS_HSM_CLEAR 1
4775
4776 /**
4777  * Generic function to set or clear HSM flags.
4778  * Used by hsm_set and hsm_clear.
4779  *
4780  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
4781  */
4782 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
4783 {
4784         struct option long_opts[] = {
4785         { .val = 'A',   .name = "archived",     .has_arg = no_argument },
4786         { .val = 'a',   .name = "noarchive",    .has_arg = no_argument },
4787         { .val = 'd',   .name = "dirty",        .has_arg = no_argument },
4788         { .val = 'e',   .name = "exists",       .has_arg = no_argument },
4789         { .val = 'l',   .name = "lost",         .has_arg = no_argument },
4790         { .val = 'r',   .name = "norelease",    .has_arg = no_argument },
4791         { .name = NULL } };
4792         char short_opts[] = "lraAde";
4793         __u64 mask = 0;
4794         int c, rc;
4795         char *path;
4796
4797         if (argc < 3)
4798                 return CMD_HELP;
4799
4800         while ((c = getopt_long(argc, argv, short_opts,
4801                                 long_opts, NULL)) != -1) {
4802                 switch (c) {
4803                 case 'l':
4804                         mask |= HS_LOST;
4805                         break;
4806                 case 'a':
4807                         mask |= HS_NOARCHIVE;
4808                         break;
4809                 case 'A':
4810                         mask |= HS_ARCHIVED;
4811                         break;
4812                 case 'r':
4813                         mask |= HS_NORELEASE;
4814                         break;
4815                 case 'd':
4816                         mask |= HS_DIRTY;
4817                         break;
4818                 case 'e':
4819                         mask |= HS_EXISTS;
4820                         break;
4821                 case '?':
4822                         return CMD_HELP;
4823                 default:
4824                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4825                                 argv[0], argv[optind - 1]);
4826                         return CMD_HELP;
4827                 }
4828         }
4829
4830         /* User should have specified a flag */
4831         if (mask == 0)
4832                 return CMD_HELP;
4833
4834         while (optind < argc) {
4835
4836                 path = argv[optind];
4837
4838                 /* If mode == 0, this means we apply the mask. */
4839                 if (mode == LFS_HSM_SET)
4840                         rc = llapi_hsm_state_set(path, mask, 0, 0);
4841                 else
4842                         rc = llapi_hsm_state_set(path, 0, mask, 0);
4843
4844                 if (rc != 0) {
4845                         fprintf(stderr, "Can't change hsm flags for %s: %s\n",
4846                                 path, strerror(errno = -rc));
4847                         return rc;
4848                 }
4849                 optind++;
4850         }
4851
4852         return 0;
4853 }
4854
4855 static int lfs_hsm_action(int argc, char **argv)
4856 {
4857         int                              rc;
4858         int                              i = 1;
4859         char                            *path;
4860         struct hsm_current_action        hca;
4861         struct hsm_extent                he;
4862         enum hsm_user_action             hua;
4863         enum hsm_progress_states         hps;
4864
4865         if (argc < 2)
4866                 return CMD_HELP;
4867
4868         do {
4869                 path = argv[i];
4870
4871                 rc = llapi_hsm_current_action(path, &hca);
4872                 if (rc) {
4873                         fprintf(stderr, "can't get hsm action for %s: %s\n",
4874                                 path, strerror(errno = -rc));
4875                         return rc;
4876                 }
4877                 he = hca.hca_location;
4878                 hua = hca.hca_action;
4879                 hps = hca.hca_state;
4880
4881                 printf("%s: %s", path, hsm_user_action2name(hua));
4882
4883                 /* Skip file without action */
4884                 if (hca.hca_action == HUA_NONE) {
4885                         printf("\n");
4886                         continue;
4887                 }
4888
4889                 printf(" %s ", hsm_progress_state2name(hps));
4890
4891                 if ((hps == HPS_RUNNING) &&
4892                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
4893                         printf("(%llu bytes moved)\n",
4894                                (unsigned long long)he.length);
4895                 else if ((he.offset + he.length) == LUSTRE_EOF)
4896                         printf("(from %llu to EOF)\n",
4897                                (unsigned long long)he.offset);
4898                 else
4899                         printf("(from %llu to %llu)\n",
4900                                (unsigned long long)he.offset,
4901                                (unsigned long long)(he.offset + he.length));
4902
4903         } while (++i < argc);
4904
4905         return 0;
4906 }
4907
4908 static int lfs_hsm_set(int argc, char **argv)
4909 {
4910         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
4911 }
4912
4913 static int lfs_hsm_clear(int argc, char **argv)
4914 {
4915         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
4916 }
4917
4918 /**
4919  * Check file state and return its fid, to be used by lfs_hsm_request().
4920  *
4921  * \param[in]     file      Path to file to check
4922  * \param[in,out] fid       Pointer to allocated lu_fid struct.
4923  * \param[in,out] last_dev  Pointer to last device id used.
4924  *
4925  * \return 0 on success.
4926  */
4927 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
4928                                 dev_t *last_dev)
4929 {
4930         struct stat     st;
4931         int             rc;
4932
4933         rc = lstat(file, &st);
4934         if (rc) {
4935                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
4936                 return -errno;
4937         }
4938         /* Checking for regular file as archiving as posix copytool
4939          * rejects archiving files other than regular files
4940          */
4941         if (!S_ISREG(st.st_mode)) {
4942                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
4943                 return CMD_HELP;
4944         }
4945         /* A request should be ... */
4946         if (*last_dev != st.st_dev && *last_dev != 0) {
4947                 fprintf(stderr, "All files should be "
4948                         "on the same filesystem: %s\n", file);
4949                 return -EINVAL;
4950         }
4951         *last_dev = st.st_dev;
4952
4953         rc = llapi_path2fid(file, fid);
4954         if (rc) {
4955                 fprintf(stderr, "Cannot read FID of %s: %s\n",
4956                         file, strerror(-rc));
4957                 return rc;
4958         }
4959         return 0;
4960 }
4961
4962 /* Fill an HSM HUR item with a given file name.
4963  *
4964  * If mntpath is set, then the filename is actually a FID, and no
4965  * lookup on the filesystem will be performed.
4966  *
4967  * \param[in]  hur         the user request to fill
4968  * \param[in]  idx         index of the item inside the HUR to fill
4969  * \param[in]  mntpath     mountpoint of Lustre
4970  * \param[in]  fname       filename (if mtnpath is NULL)
4971  *                         or FID (if mntpath is set)
4972  * \param[in]  last_dev    pointer to last device id used
4973  *
4974  * \retval 0 on success
4975  * \retval CMD_HELP or a negative errno on error
4976  */
4977 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
4978                          const char *mntpath, const char *fname,
4979                          dev_t *last_dev)
4980 {
4981         struct hsm_user_item *hui = &hur->hur_user_item[idx];
4982         int rc;
4983
4984         hui->hui_extent.length = -1;
4985
4986         if (mntpath != NULL) {
4987                 if (*fname == '[')
4988                         fname++;
4989                 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
4990                 if (rc == 3) {
4991                         rc = 0;
4992                 } else {
4993                         fprintf(stderr, "hsm: '%s' is not a valid FID\n",
4994                                 fname);
4995                         rc = -EINVAL;
4996                 }
4997         } else {
4998                 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
4999         }
5000
5001         if (rc == 0)
5002                 hur->hur_request.hr_itemcount++;
5003
5004         return rc;
5005 }
5006
5007 static int lfs_hsm_request(int argc, char **argv, int action)
5008 {
5009         struct option            long_opts[] = {
5010                 {"filelist", 1, 0, 'l'},
5011                 {"data", 1, 0, 'D'},
5012                 {"archive", 1, 0, 'a'},
5013                 {"mntpath", 1, 0, 'm'},
5014                 {0, 0, 0, 0}
5015         };
5016         dev_t                    last_dev = 0;
5017         char                     short_opts[] = "l:D:a:m:";
5018         struct hsm_user_request *hur, *oldhur;
5019         int                      c, i;
5020         size_t                   len;
5021         int                      nbfile;
5022         char                    *line = NULL;
5023         char                    *filelist = NULL;
5024         char                     fullpath[PATH_MAX];
5025         char                    *opaque = NULL;
5026         int                      opaque_len = 0;
5027         int                      archive_id = 0;
5028         FILE                    *fp;
5029         int                      nbfile_alloc = 0;
5030         char                    *some_file = NULL;
5031         char                    *mntpath = NULL;
5032         int                      rc;
5033
5034         if (argc < 2)
5035                 return CMD_HELP;
5036
5037         while ((c = getopt_long(argc, argv, short_opts,
5038                                 long_opts, NULL)) != -1) {
5039                 switch (c) {
5040                 case 'l':
5041                         filelist = optarg;
5042                         break;
5043                 case 'D':
5044                         opaque = optarg;
5045                         break;
5046                 case 'a':
5047                         if (action != HUA_ARCHIVE &&
5048                             action != HUA_REMOVE) {
5049                                 fprintf(stderr,
5050                                         "error: -a is supported only "
5051                                         "when archiving or removing\n");
5052                                 return CMD_HELP;
5053                         }
5054                         archive_id = atoi(optarg);
5055                         break;
5056                 case 'm':
5057                         if (some_file == NULL) {
5058                                 mntpath = optarg;
5059                                 some_file = strdup(optarg);
5060                         }
5061                         break;
5062                 case '?':
5063                         return CMD_HELP;
5064                 default:
5065                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5066                                 argv[0], argv[optind - 1]);
5067                         return CMD_HELP;
5068                 }
5069         }
5070
5071         /* All remaining args are files, so we have at least nbfile */
5072         nbfile = argc - optind;
5073
5074         if ((nbfile == 0) && (filelist == NULL))
5075                 return CMD_HELP;
5076
5077         if (opaque != NULL)
5078                 opaque_len = strlen(opaque);
5079
5080         /* Alloc the request structure with enough place to store all files
5081          * from command line. */
5082         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
5083         if (hur == NULL) {
5084                 fprintf(stderr, "Cannot create the request: %s\n",
5085                         strerror(errno));
5086                 return errno;
5087         }
5088         nbfile_alloc = nbfile;
5089
5090         hur->hur_request.hr_action = action;
5091         hur->hur_request.hr_archive_id = archive_id;
5092         hur->hur_request.hr_flags = 0;
5093
5094         /* All remaining args are files, add them */
5095         if (nbfile != 0 && some_file == NULL)
5096                 some_file = strdup(argv[optind]);
5097
5098         for (i = 0; i < nbfile; i++) {
5099                 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
5100                                    &last_dev);
5101                 if (rc)
5102                         goto out_free;
5103         }
5104
5105         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
5106
5107         /* If a filelist was specified, read the filelist from it. */
5108         if (filelist != NULL) {
5109                 fp = fopen(filelist, "r");
5110                 if (fp == NULL) {
5111                         fprintf(stderr, "Cannot read the file list %s: %s\n",
5112                                 filelist, strerror(errno));
5113                         rc = -errno;
5114                         goto out_free;
5115                 }
5116
5117                 while ((rc = getline(&line, &len, fp)) != -1) {
5118                         /* If allocated buffer was too small, get something
5119                          * larger */
5120                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
5121                                 ssize_t size;
5122
5123                                 nbfile_alloc = nbfile_alloc * 2 + 1;
5124                                 oldhur = hur;
5125                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
5126                                                                    opaque_len);
5127                                 if (hur == NULL) {
5128                                         fprintf(stderr, "hsm: cannot allocate "
5129                                                 "the request: %s\n",
5130                                                 strerror(errno));
5131                                         hur = oldhur;
5132                                         rc = -errno;
5133                                         fclose(fp);
5134                                         goto out_free;
5135                                 }
5136                                 size = hur_len(oldhur);
5137                                 if (size < 0) {
5138                                         fprintf(stderr, "hsm: cannot allocate "
5139                                                 "%u files + %u bytes data\n",
5140                                             oldhur->hur_request.hr_itemcount,
5141                                             oldhur->hur_request.hr_data_len);
5142                                         free(hur);
5143                                         hur = oldhur;
5144                                         rc = -E2BIG;
5145                                         fclose(fp);
5146                                         goto out_free;
5147                                 }
5148                                 memcpy(hur, oldhur, size);
5149                                 free(oldhur);
5150                         }
5151
5152                         /* Chop CR */
5153                         if (line[strlen(line) - 1] == '\n')
5154                                 line[strlen(line) - 1] = '\0';
5155
5156                         rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
5157                                            mntpath, line, &last_dev);
5158                         if (rc) {
5159                                 fclose(fp);
5160                                 goto out_free;
5161                         }
5162
5163                         if (some_file == NULL) {
5164                                 some_file = line;
5165                                 line = NULL;
5166                         }
5167                 }
5168
5169                 rc = fclose(fp);
5170                 free(line);
5171         }
5172
5173         /* If a --data was used, add it to the request */
5174         hur->hur_request.hr_data_len = opaque_len;
5175         if (opaque != NULL)
5176                 memcpy(hur_data(hur), opaque, opaque_len);
5177
5178         /* Send the HSM request */
5179         if (realpath(some_file, fullpath) == NULL) {
5180                 fprintf(stderr, "Could not find path '%s': %s\n",
5181                         some_file, strerror(errno));
5182         }
5183         rc = llapi_hsm_request(fullpath, hur);
5184         if (rc) {
5185                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
5186                         some_file, strerror(-rc));
5187                 goto out_free;
5188         }
5189
5190 out_free:
5191         free(some_file);
5192         free(hur);
5193         return rc;
5194 }
5195
5196 static int lfs_hsm_archive(int argc, char **argv)
5197 {
5198         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
5199 }
5200
5201 static int lfs_hsm_restore(int argc, char **argv)
5202 {
5203         return lfs_hsm_request(argc, argv, HUA_RESTORE);
5204 }
5205
5206 static int lfs_hsm_release(int argc, char **argv)
5207 {
5208         return lfs_hsm_request(argc, argv, HUA_RELEASE);
5209 }
5210
5211 static int lfs_hsm_remove(int argc, char **argv)
5212 {
5213         return lfs_hsm_request(argc, argv, HUA_REMOVE);
5214 }
5215
5216 static int lfs_hsm_cancel(int argc, char **argv)
5217 {
5218         return lfs_hsm_request(argc, argv, HUA_CANCEL);
5219 }
5220
5221 static int lfs_swap_layouts(int argc, char **argv)
5222 {
5223         if (argc != 3)
5224                 return CMD_HELP;
5225
5226         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
5227                                   SWAP_LAYOUTS_KEEP_MTIME |
5228                                   SWAP_LAYOUTS_KEEP_ATIME);
5229 }
5230
5231 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
5232
5233 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
5234 {
5235         enum lu_ladvise_type advice;
5236
5237         for (advice = 0;
5238              advice < ARRAY_SIZE(ladvise_names); advice++) {
5239                 if (ladvise_names[advice] == NULL)
5240                         continue;
5241                 if (strcmp(string, ladvise_names[advice]) == 0)
5242                         return advice;
5243         }
5244
5245         return LU_LADVISE_INVALID;
5246 }
5247
5248 static int lfs_ladvise(int argc, char **argv)
5249 {
5250         struct option long_opts[] = {
5251         { .val = 'a',   .name = "advice",       .has_arg = required_argument },
5252         { .val = 'b',   .name = "background",   .has_arg = no_argument },
5253         { .val = 'e',   .name = "end",          .has_arg = required_argument },
5254         { .val = 'l',   .name = "length",       .has_arg = required_argument },
5255         { .val = 's',   .name = "start",        .has_arg = required_argument },
5256         { .name = NULL } };
5257         char                     short_opts[] = "a:be:l:s:";
5258         int                      c;
5259         int                      rc = 0;
5260         const char              *path;
5261         int                      fd;
5262         struct llapi_lu_ladvise  advice;
5263         enum lu_ladvise_type     advice_type = LU_LADVISE_INVALID;
5264         unsigned long long       start = 0;
5265         unsigned long long       end = LUSTRE_EOF;
5266         unsigned long long       length = 0;
5267         unsigned long long       size_units;
5268         unsigned long long       flags = 0;
5269
5270         optind = 0;
5271         while ((c = getopt_long(argc, argv, short_opts,
5272                                 long_opts, NULL)) != -1) {
5273                 switch (c) {
5274                 case 'a':
5275                         advice_type = lfs_get_ladvice(optarg);
5276                         if (advice_type == LU_LADVISE_INVALID) {
5277                                 fprintf(stderr, "%s: invalid advice type "
5278                                         "'%s'\n", argv[0], optarg);
5279                                 fprintf(stderr, "Valid types:");
5280
5281                                 for (advice_type = 0;
5282                                      advice_type < ARRAY_SIZE(ladvise_names);
5283                                      advice_type++) {
5284                                         if (ladvise_names[advice_type] == NULL)
5285                                                 continue;
5286                                         fprintf(stderr, " %s",
5287                                                 ladvise_names[advice_type]);
5288                                 }
5289                                 fprintf(stderr, "\n");
5290
5291                                 return CMD_HELP;
5292                         }
5293                         break;
5294                 case 'b':
5295                         flags |= LF_ASYNC;
5296                         break;
5297                 case 'e':
5298                         size_units = 1;
5299                         rc = llapi_parse_size(optarg, &end,
5300                                               &size_units, 0);
5301                         if (rc) {
5302                                 fprintf(stderr, "%s: bad end offset '%s'\n",
5303                                         argv[0], optarg);
5304                                 return CMD_HELP;
5305                         }
5306                         break;
5307                 case 's':
5308                         size_units = 1;
5309                         rc = llapi_parse_size(optarg, &start,
5310                                               &size_units, 0);
5311                         if (rc) {
5312                                 fprintf(stderr, "%s: bad start offset "
5313                                         "'%s'\n", argv[0], optarg);
5314                                 return CMD_HELP;
5315                         }
5316                         break;
5317                 case 'l':
5318                         size_units = 1;
5319                         rc = llapi_parse_size(optarg, &length,
5320                                               &size_units, 0);
5321                         if (rc) {
5322                                 fprintf(stderr, "%s: bad length '%s'\n",
5323                                         argv[0], optarg);
5324                                 return CMD_HELP;
5325                         }
5326                         break;
5327                 case '?':
5328                         return CMD_HELP;
5329                 default:
5330                         fprintf(stderr, "%s: option '%s' unrecognized\n",
5331                                 argv[0], argv[optind - 1]);
5332                         return CMD_HELP;
5333                 }
5334         }
5335
5336         if (advice_type == LU_LADVISE_INVALID) {
5337                 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
5338                 fprintf(stderr, "Valid types:");
5339                 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
5340                      advice_type++) {
5341                         if (ladvise_names[advice_type] == NULL)
5342                                 continue;
5343                         fprintf(stderr, " %s", ladvise_names[advice_type]);
5344                 }
5345                 fprintf(stderr, "\n");
5346                 return CMD_HELP;
5347         }
5348
5349         if (argc <= optind) {
5350                 fprintf(stderr, "%s: please give one or more file names\n",
5351                         argv[0]);
5352                 return CMD_HELP;
5353         }
5354
5355         if (end != LUSTRE_EOF && length != 0 && end != start + length) {
5356                 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
5357                         argv[0]);
5358                 return CMD_HELP;
5359         }
5360
5361         if (end == LUSTRE_EOF && length != 0)
5362                 end = start + length;
5363
5364         if (end <= start) {
5365                 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
5366                         argv[0], start, end);
5367                 return CMD_HELP;
5368         }
5369
5370         while (optind < argc) {
5371                 int rc2;
5372
5373                 path = argv[optind++];
5374
5375                 fd = open(path, O_RDONLY);
5376                 if (fd < 0) {
5377                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
5378                                 argv[0], path, strerror(errno));
5379                         rc2 = -errno;
5380                         goto next;
5381                 }
5382
5383                 advice.lla_start = start;
5384                 advice.lla_end = end;
5385                 advice.lla_advice = advice_type;
5386                 advice.lla_value1 = 0;
5387                 advice.lla_value2 = 0;
5388                 advice.lla_value3 = 0;
5389                 advice.lla_value4 = 0;
5390                 rc2 = llapi_ladvise(fd, flags, 1, &advice);
5391                 close(fd);
5392                 if (rc2 < 0) {
5393                         fprintf(stderr, "%s: cannot give advice '%s' to file "
5394                                 "'%s': %s\n", argv[0],
5395                                 ladvise_names[advice_type],
5396                                 path, strerror(errno));
5397                 }
5398 next:
5399                 if (rc == 0 && rc2 < 0)
5400                         rc = rc2;
5401         }
5402         return rc;
5403 }
5404
5405 static int lfs_list_commands(int argc, char **argv)
5406 {
5407         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
5408
5409         Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
5410
5411         return 0;
5412 }
5413
5414 int main(int argc, char **argv)
5415 {
5416         int rc;
5417
5418         /* Ensure that liblustreapi constructor has run */
5419         if (!liblustreapi_initialized)
5420                 fprintf(stderr, "liblustreapi was not properly initialized\n");
5421
5422         setlinebuf(stdout);
5423
5424         Parser_init("lfs > ", cmdlist);
5425
5426         progname = argv[0]; /* Used in error messages */
5427         if (argc > 1) {
5428                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
5429         } else {
5430                 rc = Parser_commands();
5431         }
5432
5433         return rc < 0 ? -rc : rc;
5434 }
5435
5436 #ifdef _LUSTRE_IDL_H_
5437 /* Everything we need here should be included by lustreapi.h. */
5438 # error "lfs should not depend on lustre_idl.h"
5439 #endif /* _LUSTRE_IDL_H_ */