Whamcloud - gitweb
LU-4840 lfs: Use file lease to implement migration
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/lfs.c
37  *
38  * Author: Peter J. Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Robert Read <rread@clusterfs.com>
41  */
42
43 /* for O_DIRECTORY */
44 #ifndef _GNU_SOURCE
45 #define _GNU_SOURCE
46 #endif
47
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <getopt.h>
51 #include <string.h>
52 #include <mntent.h>
53 #include <unistd.h>
54 #include <errno.h>
55 #include <err.h>
56 #include <pwd.h>
57 #include <grp.h>
58 #include <sys/quota.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <fcntl.h>
62 #include <dirent.h>
63 #include <time.h>
64 #include <ctype.h>
65 #ifdef HAVE_SYS_QUOTA_H
66 # include <sys/quota.h>
67 #endif
68
69 #include <libcfs/util/string.h>
70 #include <libcfs/libcfs.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <lustre/lustreapi.h>
74 #include <lustre_ver.h>
75
76 /* all functions */
77 static int lfs_setstripe(int argc, char **argv);
78 static int lfs_find(int argc, char **argv);
79 static int lfs_getstripe(int argc, char **argv);
80 static int lfs_getdirstripe(int argc, char **argv);
81 static int lfs_setdirstripe(int argc, char **argv);
82 static int lfs_rmentry(int argc, char **argv);
83 static int lfs_osts(int argc, char **argv);
84 static int lfs_mdts(int argc, char **argv);
85 static int lfs_df(int argc, char **argv);
86 static int lfs_getname(int argc, char **argv);
87 static int lfs_check(int argc, char **argv);
88 #ifdef HAVE_SYS_QUOTA_H
89 static int lfs_setquota(int argc, char **argv);
90 static int lfs_quota(int argc, char **argv);
91 #endif
92 static int lfs_flushctx(int argc, char **argv);
93 static int lfs_join(int argc, char **argv);
94 static int lfs_lsetfacl(int argc, char **argv);
95 static int lfs_lgetfacl(int argc, char **argv);
96 static int lfs_rsetfacl(int argc, char **argv);
97 static int lfs_rgetfacl(int argc, char **argv);
98 static int lfs_cp(int argc, char **argv);
99 static int lfs_ls(int argc, char **argv);
100 static int lfs_poollist(int argc, char **argv);
101 static int lfs_changelog(int argc, char **argv);
102 static int lfs_changelog_clear(int argc, char **argv);
103 static int lfs_fid2path(int argc, char **argv);
104 static int lfs_path2fid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117
118 /* Setstripe and migrate share mostly the same parameters */
119 #define SSM_CMD_COMMON(cmd) \
120         "usage: "cmd" [--stripe-count|-c <stripe_count>]\n"             \
121         "                 [--stripe-index|-i <start_ost_idx>]\n"        \
122         "                 [--stripe-size|-S <stripe_size>]\n"           \
123         "                 [--pool|-p <pool_name>]\n"                    \
124         "                 [--ost-list|-o <ost_indices>]\n"
125
126 #define SSM_HELP_COMMON \
127         "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n" \
128         "\t              Can be specified with k, m or g (in KB, MB and GB\n" \
129         "\t              respectively)\n"                               \
130         "\tstart_ost_idx: OST index of first stripe (-1 default)\n"     \
131         "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n" \
132         "\tpool_name:    Name of OST pool to use (default none)\n"      \
133         "\tost_indices:  List of OST indices, can be repeated multiple times\n"\
134         "\t              Indices be specified in a format of:\n"        \
135         "\t                -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n"        \
136         "\t              Or:\n"                                         \
137         "\t                -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n"  \
138         "\t              If --pool is set with --ost-list, then the OSTs\n" \
139         "\t              must be the members of the pool."
140
141 #define SETSTRIPE_USAGE                                         \
142         SSM_CMD_COMMON("setstripe")                             \
143         "                 <directory|filename>\n"               \
144         SSM_HELP_COMMON
145
146 #define MIGRATE_USAGE                                                   \
147         SSM_CMD_COMMON("migrate  ")                                     \
148         "                 [--block|-b]\n"                               \
149         "                 <filename>\n"                                 \
150         SSM_HELP_COMMON                                                 \
151         "\n"                                                            \
152         "\tblock:        Block file access during data migration\n"     \
153
154 static const char       *progname;
155 static bool              file_lease_supported = true;
156
157 /* all available commands */
158 command_t cmdlist[] = {
159         {"setstripe", lfs_setstripe, 0,
160          "Create a new file with a specific striping pattern or\n"
161          "set the default striping pattern on an existing directory or\n"
162          "delete the default striping pattern from an existing directory\n"
163          "usage: setstripe -d <directory>   (to delete default striping)\n"\
164          " or\n"
165          SETSTRIPE_USAGE},
166         {"getstripe", lfs_getstripe, 0,
167          "To list the striping info for a given file or files in a\n"
168          "directory or recursively for all files in a directory tree.\n"
169          "usage: getstripe [--ost|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
170          "                 [--stripe-count|-c] [--stripe-index|-i]\n"
171          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
172          "                 [--mdt-index|-M] [--recursive|-r] [--raw|-R]\n"
173          "                 [--layout|-L]\n"
174          "                 <directory|filename> ..."},
175         {"setdirstripe", lfs_setdirstripe, 0,
176          "To create a striped directory on a specified MDT. This can only\n"
177          "be done on MDT0 with the right of administrator.\n"
178          "usage: setdirstripe <--count|-c stripe_count>\n"
179          "              [--index|-i mdt_index] [--hash-type|-t hash_type]\n"
180          "              [--default_stripe|-D ] [--mode|-m mode] <dir>\n"
181          "\tstripe_count: stripe count of the striped directory\n"
182          "\tmdt_index:  MDT index of first stripe\n"
183          "\thash_type:  hash type of the striped directory. Hash types:\n"
184          "      fnv_1a_64 FNV-1a hash algorithm (default)\n"
185          "      all_char  sum of characters % MDT_COUNT (not recommended)\n"
186          "\tdefault_stripe: set default dirstripe of the directory\n"
187          "\tmode: the mode of the directory\n"},
188         {"getdirstripe", lfs_getdirstripe, 0,
189          "To list the striping info for a given directory\n"
190          "or recursively for all directories in a directory tree.\n"
191          "usage: getdirstripe [--obd|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
192          "               [--count|-c ] [--index|-i ] [--raw|-R]\n"
193          "               [--recursive | -r] [ --default_stripe | -D ] <dir> "},
194         {"mkdir", lfs_setdirstripe, 0,
195          "To create a striped directory on a specified MDT. This can only\n"
196          "be done on MDT0 with the right of administrator.\n"
197          "usage: mkdir <--count|-c stripe_count>\n"
198          "              [--index|-i mdt_index] [--hash-type|-t hash_type]\n"
199          "              [--default_stripe|-D ] [--mode|-m mode] <dir>\n"
200          "\tstripe_count: stripe count of the striped directory\n"
201          "\tmdt_index:  MDT index of first stripe\n"
202          "\thash_type:  hash type of the striped directory. Hash types:\n"
203          "      fnv_1a_64 FNV-1a hash algorithm (default)\n"
204          "      all_char  sum of characters % MDT_COUNT (not recommended)\n"
205          "\tdefault_stripe: set default dirstripe of the directory\n"
206          "\tmode: the mode of the directory\n"},
207         {"rm_entry", lfs_rmentry, 0,
208          "To remove the name entry of the remote directory. Note: This\n"
209          "command will only delete the name entry, i.e. the remote directory\n"
210          "will become inaccessable after this command. This can only be done\n"
211          "by the administrator\n"
212          "usage: rm_entry <dir>\n"},
213         {"pool_list", lfs_poollist, 0,
214          "List pools or pool OSTs\n"
215          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
216         {"find", lfs_find, 0,
217          "find files matching given attributes recursively in directory tree.\n"
218          "usage: find <directory|filename> ...\n"
219          "     [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
220          "     [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
221          "     [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
222          "     [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
223          "     [[!] --size|-s [+-]N[bkMGTPE]]\n"
224          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
225          "     [[!] --stripe-index|-i <index,...>]\n"
226          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
227          "     [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
228          "     [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
229          "     [[!] --layout|-L released,raid0]\n"
230          "\t !: used before an option indicates 'NOT' requested attribute\n"
231          "\t -: used before a value indicates 'AT MOST' requested value\n"
232          "\t +: used before a value indicates 'AT LEAST' requested value\n"},
233         {"check", lfs_check, 0,
234          "Display the status of MDS or OSTs (as specified in the command)\n"
235          "or all the servers (MDS and OSTs).\n"
236          "usage: check <osts|mds|servers>"},
237         {"join", lfs_join, 0,
238          "join two lustre files into one.\n"
239          "obsolete, HEAD does not support it anymore.\n"},
240         {"osts", lfs_osts, 0, "list OSTs connected to client "
241          "[for specified path only]\n" "usage: osts [path]"},
242         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
243          "[for specified path only]\n" "usage: mdts [path]"},
244         {"df", lfs_df, 0,
245          "report filesystem disk space usage or inodes usage"
246          "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
247          "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
248         {"getname", lfs_getname, 0, "list instances and specified mount points "
249          "[for specified path only]\n"
250          "Usage: getname [-h]|[path ...] "},
251 #ifdef HAVE_SYS_QUOTA_H
252         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
253          "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
254          "                -b <block-softlimit> -B <block-hardlimit>\n"
255          "                -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
256          "       setquota <-u|--user|-g|--group> <uname>|<uid>|<gname>|<gid>\n"
257          "                [--block-softlimit <block-softlimit>]\n"
258          "                [--block-hardlimit <block-hardlimit>]\n"
259          "                [--inode-softlimit <inode-softlimit>]\n"
260          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
261          "       setquota [-t] <-u|--user|-g|--group>\n"
262          "                [--block-grace <block-grace>]\n"
263          "                [--inode-grace <inode-grace>] <filesystem>\n"
264          "       -b can be used instead of --block-softlimit/--block-grace\n"
265          "       -B can be used instead of --block-hardlimit\n"
266          "       -i can be used instead of --inode-softlimit/--inode-grace\n"
267          "       -I can be used instead of --inode-hardlimit\n\n"
268          "Note: The total quota space will be split into many qunits and\n"
269          "      balanced over all server targets, the minimal qunit size is\n"
270          "      1M bytes for block space and 1K inodes for inode space.\n\n"
271          "      Quota space rebalancing process will stop when this mininum\n"
272          "      value is reached. As a result, quota exceeded can be returned\n"
273          "      while many targets still have 1MB or 1K inodes of spare\n"
274          "      quota space."},
275         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
276          "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
277                        "<ost_idx>]\n"
278          "             [<-u|-g> <uname>|<uid>|<gname>|<gid>] <filesystem>\n"
279          "       quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g> <filesystem>"},
280 #endif
281         {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
282          "usage: flushctx [-k] [mountpoint...]"},
283         {"lsetfacl", lfs_lsetfacl, 0,
284          "Remote user setfacl for user/group on the same remote client.\n"
285          "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
286         {"lgetfacl", lfs_lgetfacl, 0,
287          "Remote user getfacl for user/group on the same remote client.\n"
288          "usage: lgetfacl [-dRLPvh] file ..."},
289         {"rsetfacl", lfs_rsetfacl, 0,
290          "Remote user setfacl for user/group on other clients.\n"
291          "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
292         {"rgetfacl", lfs_rgetfacl, 0,
293          "Remote user getfacl for user/group on other clients.\n"
294          "usage: rgetfacl [-dRLPvh] file ..."},
295         {"cp", lfs_cp, 0,
296          "Remote user copy files and directories.\n"
297          "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
298         {"ls", lfs_ls, 0,
299          "Remote user list directory contents.\n"
300          "usage: ls [OPTION]... [FILE]..."},
301         {"changelog", lfs_changelog, 0,
302          "Show the metadata changes on an MDT."
303          "\nusage: changelog <mdtname> [startrec [endrec]]"},
304         {"changelog_clear", lfs_changelog_clear, 0,
305          "Indicate that old changelog records up to <endrec> are no longer of "
306          "interest to consumer <id>, allowing the system to free up space.\n"
307          "An <endrec> of 0 means all records.\n"
308          "usage: changelog_clear <mdtname> <id> <endrec>"},
309         {"fid2path", lfs_fid2path, 0,
310          "Resolve the full path(s) for given FID(s). For a specific hardlink "
311          "specify link number <linkno>.\n"
312         /* "For a historical link name, specify changelog record <recno>.\n" */
313          "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
314                 /* [ --rec <recno> ] */ },
315         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
316          "usage: path2fid [--parents] <path> ..."},
317         {"data_version", lfs_data_version, 0, "Display file data version for "
318          "a given path.\n" "usage: data_version -[n|r|w] <path>"},
319         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
320          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
321         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
322          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
323          "[--archived] [--lost] <file> ..."},
324         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
325          "files.\n"
326          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
327          "[--archived] [--lost] <file> ..."},
328         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
329          "given files.\n" "usage: hsm_action <file> ..."},
330         {"hsm_archive", lfs_hsm_archive, 0,
331          "Archive file to external storage.\n"
332          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
333          "<file> ..."},
334         {"hsm_restore", lfs_hsm_restore, 0,
335          "Restore file from external storage.\n"
336          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
337         {"hsm_release", lfs_hsm_release, 0,
338          "Release files from Lustre.\n"
339          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
340         {"hsm_remove", lfs_hsm_remove, 0,
341          "Remove file copy from external storage.\n"
342          "usage: hsm_remove [--filelist FILELIST] [--data DATA] <file> ..."},
343         {"hsm_cancel", lfs_hsm_cancel, 0,
344          "Cancel requests related to specified files.\n"
345          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
346         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
347          "usage: swap_layouts <path1> <path2>"},
348         {"migrate", lfs_setstripe, 0, "migrate file from one OST layout to "
349          "another.\n" MIGRATE_USAGE},
350         {"mv", lfs_mv, 0,
351          "To move directories between MDTs.\n"
352          "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
353          "[--verbose|-v]\n"},
354         {"help", Parser_help, 0, "help"},
355         {"exit", Parser_quit, 0, "quit"},
356         {"quit", Parser_quit, 0, "quit"},
357         {"--version", Parser_version, 0,
358          "output build version of the utility and exit"},
359         { 0, 0, 0, NULL }
360 };
361
362
363 #define MIGRATION_BLOCKS 1
364
365 /**
366  * Internal helper for migrate_copy_data(). Check lease and report error if
367  * need be.
368  *
369  * \param[in]  fd           File descriptor on which to check the lease.
370  * \param[out] lease_broken Set to true if the lease was broken.
371  * \param[in]  group_locked Whether a group lock was taken or not.
372  * \param[in]  path         Name of the file being processed, for error
373  *                          reporting
374  *
375  * \retval 0       Migration can keep on going.
376  * \retval -errno  Error occurred, abort migration.
377  */
378 static int check_lease(int fd, bool *lease_broken, bool group_locked,
379                        const char *path)
380 {
381         int rc;
382
383         if (!file_lease_supported)
384                 return 0;
385
386         rc = llapi_lease_check(fd);
387         if (rc > 0)
388                 return 0; /* llapi_check_lease returns > 0 on success. */
389
390         if (!group_locked) {
391                 fprintf(stderr, "%s: cannot migrate '%s': file busy\n",
392                         progname, path);
393                 rc = rc ? rc : -EAGAIN;
394         } else {
395                 fprintf(stderr, "%s: external attempt to access file '%s' "
396                         "blocked until migration ends.\n", progname, path);
397                 rc = 0;
398         }
399         *lease_broken = true;
400         return rc;
401 }
402
403 static int migrate_copy_data(int fd_src, int fd_dst, size_t buf_size,
404                              bool group_locked, const char *fname)
405 {
406         void    *buf = NULL;
407         ssize_t  rsize = -1;
408         ssize_t  wsize = 0;
409         size_t   rpos = 0;
410         size_t   wpos = 0;
411         off_t    bufoff = 0;
412         int      rc;
413         bool     lease_broken = false;
414
415         /* Use a page-aligned buffer for direct I/O */
416         rc = posix_memalign(&buf, getpagesize(), buf_size);
417         if (rc != 0)
418                 return -rc;
419
420         while (1) {
421                 /* read new data only if we have written all
422                  * previously read data */
423                 if (wpos == rpos) {
424                         if (!lease_broken) {
425                                 rc = check_lease(fd_src, &lease_broken,
426                                                  group_locked, fname);
427                                 if (rc < 0)
428                                         goto out;
429                         }
430                         rsize = read(fd_src, buf, buf_size);
431                         if (rsize < 0) {
432                                 rc = -errno;
433                                 fprintf(stderr, "%s: %s: read failed: %s\n",
434                                         progname, fname, strerror(-rc));
435                                 goto out;
436                         }
437                         rpos += rsize;
438                         bufoff = 0;
439                 }
440                 /* eof ? */
441                 if (rsize == 0)
442                         break;
443
444                 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
445                 if (wsize < 0) {
446                         rc = -errno;
447                         fprintf(stderr,
448                                 "%s: %s: write failed on volatile: %s\n",
449                                 progname, fname, strerror(-rc));
450                         goto out;
451                 }
452                 wpos += wsize;
453                 bufoff += wsize;
454         }
455
456         rc = fsync(fd_dst);
457         if (rc < 0) {
458                 rc = -errno;
459                 fprintf(stderr, "%s: %s: fsync failed: %s\n",
460                         progname, fname, strerror(-rc));
461         }
462
463 out:
464         free(buf);
465         return rc;
466 }
467
468 static int migrate_copy_timestamps(int fdv, const struct stat *st)
469 {
470         struct timeval  tv[2] = {
471                 {.tv_sec = st->st_atime},
472                 {.tv_sec = st->st_mtime}
473         };
474
475         return futimes(fdv, tv);
476 }
477
478 static int migrate_block(int fd, int fdv, const struct stat *st,
479                          size_t buf_size, const char *name)
480 {
481         __u64   dv1;
482         int     gid;
483         int     rc;
484         int     rc2;
485
486         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
487         if (rc < 0) {
488                 fprintf(stderr, "%s: %s: cannot get dataversion: %s\n",
489                         progname, name, strerror(-rc));
490                 return rc;
491         }
492
493         do
494                 gid = random();
495         while (gid == 0);
496
497         /* The grouplock blocks all concurrent accesses to the file.
498          * It has to be taken after llapi_get_data_version as it would
499          * block it too. */
500         rc = llapi_group_lock(fd, gid);
501         if (rc < 0) {
502                 fprintf(stderr, "%s: %s: cannot get group lock: %s\n",
503                         progname, name, strerror(-rc));
504                 return rc;
505         }
506
507         rc = migrate_copy_data(fd, fdv, buf_size, true, name);
508         if (rc < 0) {
509                 fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
510                 goto out_unlock;
511         }
512
513         /* Make sure we keep original atime/mtime values */
514         rc = migrate_copy_timestamps(fdv, st);
515         if (rc < 0) {
516                 fprintf(stderr, "%s: %s: timestamp copy failed\n",
517                         progname, name);
518                 goto out_unlock;
519         }
520
521         /* swap layouts
522          * for a migration we need to check data version on file did
523          * not change.
524          *
525          * Pass in gid=0 since we already own grouplock. */
526         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
527                                            SWAP_LAYOUTS_CHECK_DV1);
528         if (rc == -EAGAIN) {
529                 fprintf(stderr, "%s: %s: dataversion changed during copy, "
530                         "migration aborted\n", progname, name);
531                 goto out_unlock;
532         } else if (rc < 0) {
533                 fprintf(stderr, "%s: %s: cannot swap layouts: %s\n", progname,
534                         name, strerror(-rc));
535                 goto out_unlock;
536         }
537
538 out_unlock:
539         rc2 = llapi_group_unlock(fd, gid);
540         if (rc2 < 0 && rc == 0) {
541                 fprintf(stderr, "%s: %s: putting group lock failed: %s\n",
542                         progname, name, strerror(-rc2));
543                 rc = rc2;
544         }
545
546         return rc;
547 }
548
549 static int migrate_nonblock(int fd, int fdv, const struct stat *st,
550                             size_t buf_size, const char *name)
551 {
552         __u64   dv1;
553         __u64   dv2;
554         int     rc;
555
556         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
557         if (rc < 0) {
558                 fprintf(stderr, "%s: %s: cannot get data version: %s\n",
559                         progname, name, strerror(-rc));
560                 return rc;
561         }
562
563         rc = migrate_copy_data(fd, fdv, buf_size, false, name);
564         if (rc < 0) {
565                 fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
566                 return rc;
567         }
568
569         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
570         if (rc != 0) {
571                 fprintf(stderr, "%s: %s: cannot get data version: %s\n",
572                         progname, name, strerror(-rc));
573                 return rc;
574         }
575
576         if (dv1 != dv2) {
577                 rc = -EAGAIN;
578                 fprintf(stderr, "%s: %s: data version changed during "
579                                 "migration\n",
580                         progname, name);
581                 return rc;
582         }
583
584         /* Make sure we keep original atime/mtime values */
585         rc = migrate_copy_timestamps(fdv, st);
586         if (rc < 0) {
587                 fprintf(stderr, "%s: %s: timestamp copy failed\n",
588                         progname, name);
589                 return rc;
590         }
591
592         /* Atomically put lease, swap layouts and close.
593          * for a migration we need to check data version on file did
594          * not change. */
595         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
596         if (rc < 0) {
597                 fprintf(stderr, "%s: %s: cannot swap layouts: %s\n",
598                         progname, name, strerror(-rc));
599                 return rc;
600         }
601
602         return 0;
603 }
604
605 static int lfs_migrate(char *name, __u64 migration_flags,
606                        struct llapi_stripe_param *param)
607 {
608         int                      fd = -1;
609         int                      fdv = -1;
610         char                     volatile_file[PATH_MAX +
611                                                 LUSTRE_VOLATILE_HDR_LEN + 4];
612         char                     parent[PATH_MAX];
613         char                    *ptr;
614         int                      rc;
615         struct lov_user_md      *lum = NULL;
616         int                      lum_size;
617         int                      buf_size;
618         bool                     have_lease_rdlck = false;
619         struct stat              st;
620         struct stat              stv;
621
622         /* find the right size for the IO and allocate the buffer */
623         lum_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
624         lum = malloc(lum_size);
625         if (lum == NULL) {
626                 rc = -ENOMEM;
627                 goto free;
628         }
629
630         rc = llapi_file_get_stripe(name, lum);
631         /* failure can happen for many reasons and some may be not real errors
632          * (eg: no stripe)
633          * in case of a real error, a later call will fail with better
634          * error management */
635         if (rc < 0)
636                 buf_size = 1024 * 1024;
637         else
638                 buf_size = lum->lmm_stripe_size;
639
640         /* open file, direct io */
641         /* even if the file is only read, WR mode is nedeed to allow
642          * layout swap on fd */
643         fd = open(name, O_RDWR | O_DIRECT);
644         if (fd == -1) {
645                 rc = -errno;
646                 fprintf(stderr, "%s: %s: cannot open: %s\n", progname, name,
647                         strerror(-rc));
648                 goto free;
649         }
650
651         if (file_lease_supported) {
652                 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
653                 if (rc == -EOPNOTSUPP) {
654                         /* Older servers do not support file lease.
655                          * Disable related checks. This opens race conditions
656                          * as explained in LU-4840 */
657                         file_lease_supported = false;
658                 } else if (rc < 0) {
659                         fprintf(stderr, "%s: %s: cannot get open lease: %s\n",
660                                 progname, name, strerror(-rc));
661                         goto error;
662                 } else {
663                         have_lease_rdlck = true;
664                 }
665         }
666
667         /* search for file directory pathname */
668         if (strlen(name) > sizeof(parent)-1) {
669                 rc = -E2BIG;
670                 goto error;
671         }
672         strncpy(parent, name, sizeof(parent));
673         ptr = strrchr(parent, '/');
674         if (ptr == NULL) {
675                 if (getcwd(parent, sizeof(parent)) == NULL) {
676                         rc = -errno;
677                         goto error;
678                 }
679         } else {
680                 if (ptr == parent)
681                         strcpy(parent, "/");
682                 else
683                         *ptr = '\0';
684         }
685
686         rc = snprintf(volatile_file, sizeof(volatile_file), "%s/%s::", parent,
687                       LUSTRE_VOLATILE_HDR);
688         if (rc >= sizeof(volatile_file)) {
689                 rc = -E2BIG;
690                 goto error;
691         }
692
693         /* create, open a volatile file, use caching (ie no directio) */
694         /* exclusive create is not needed because volatile files cannot
695          * conflict on name by construction */
696         fdv = llapi_file_open_param(volatile_file, O_CREAT | O_WRONLY, 0644,
697                                     param);
698         if (fdv < 0) {
699                 rc = fdv;
700                 fprintf(stderr, "%s: %s: cannot create volatile file in"
701                                 " directory: %s\n",
702                         progname, parent, strerror(-rc));
703                 goto error;
704         }
705
706         /* Not-owner (root?) special case.
707          * Need to set owner/group of volatile file like original.
708          * This will allow to pass related check during layout_swap.
709          */
710         rc = fstat(fd, &st);
711         if (rc != 0) {
712                 rc = -errno;
713                 fprintf(stderr, "%s: %s: cannot stat: %s\n", progname, name,
714                         strerror(errno));
715                 goto error;
716         }
717         rc = fstat(fdv, &stv);
718         if (rc != 0) {
719                 rc = -errno;
720                 fprintf(stderr, "%s: %s: cannot stat: %s\n", progname,
721                         volatile_file, strerror(errno));
722                 goto error;
723         }
724         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
725                 rc = fchown(fdv, st.st_uid, st.st_gid);
726                 if (rc != 0) {
727                         rc = -errno;
728                         fprintf(stderr, "%s: %s: cannot chown: %s\n", progname,
729                                 name, strerror(errno));
730                         goto error;
731                 }
732         }
733
734         if (migration_flags & MIGRATION_BLOCKS || !file_lease_supported) {
735                 /* Blocking mode, forced if servers do not support file lease */
736                 rc = migrate_block(fd, fdv, &st, buf_size, name);
737         } else {
738                 rc = migrate_nonblock(fd, fdv, &st, buf_size, name);
739                 if (rc == 0) {
740                         have_lease_rdlck = false;
741                         fdv = -1; /* The volatile file is closed as we put the
742                                    * lease in non-blocking mode. */
743                 }
744         }
745
746 error:
747         if (have_lease_rdlck)
748                 llapi_lease_put(fd);
749
750         if (fd >= 0)
751                 close(fd);
752
753         if (fdv >= 0)
754                 close(fdv);
755
756 free:
757         if (lum)
758                 free(lum);
759
760         return rc;
761 }
762
763 /**
764  * Parse a string containing an OST index list into an array of integers.
765  *
766  * The input string contains a comma delimited list of individual
767  * indices and ranges, for example "1,2-4,7". Add the indices into the
768  * \a osts array and remove duplicates.
769  *
770  * \param[out] osts    array to store indices in
771  * \param[in] size     size of \a osts array
772  * \param[in] offset   starting index in \a osts
773  * \param[in] arg      string containing OST index list
774  *
775  * \retval positive    number of indices in \a osts
776  * \retval -EINVAL     unable to parse \a arg
777  */
778 static int parse_targets(__u32 *osts, int size, int offset, char *arg)
779 {
780         int rc;
781         int nr = offset;
782         int slots = size - offset;
783         char *ptr = NULL;
784         bool end_of_loop;
785
786         if (arg == NULL)
787                 return -EINVAL;
788
789         end_of_loop = false;
790         while (!end_of_loop) {
791                 int start_index;
792                 int end_index;
793                 int i;
794                 char *endptr = NULL;
795
796                 rc = -EINVAL;
797
798                 ptr = strchrnul(arg, ',');
799
800                 end_of_loop = *ptr == '\0';
801                 *ptr = '\0';
802
803                 start_index = strtol(arg, &endptr, 0);
804                 if (endptr == arg) /* no data at all */
805                         break;
806                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
807                         break;
808                 if (start_index < 0)
809                         break;
810
811                 end_index = start_index;
812                 if (*endptr == '-') {
813                         end_index = strtol(endptr + 1, &endptr, 0);
814                         if (*endptr != '\0')
815                                 break;
816                         if (end_index < start_index)
817                                 break;
818                 }
819
820                 for (i = start_index; i <= end_index && slots > 0; i++) {
821                         int j;
822
823                         /* remove duplicate */
824                         for (j = 0; j < offset; j++) {
825                                 if (osts[j] == i)
826                                         break;
827                         }
828                         if (j == offset) { /* no duplicate */
829                                 osts[nr++] = i;
830                                 --slots;
831                         }
832                 }
833                 if (slots == 0 && i < end_index)
834                         break;
835
836                 *ptr = ',';
837                 arg = ++ptr;
838                 offset = nr;
839                 rc = 0;
840         }
841         if (!end_of_loop && ptr != NULL)
842                 *ptr = ',';
843
844         return rc < 0 ? rc : nr;
845 }
846
847 /* functions */
848 static int lfs_setstripe(int argc, char **argv)
849 {
850         struct llapi_stripe_param       *param;
851         char                            *fname;
852         int                              result;
853         int                              result2 = 0;
854         unsigned long long               st_size;
855         int                              st_offset, st_count;
856         char                            *end;
857         int                              c;
858         int                              delete = 0;
859         char                            *stripe_size_arg = NULL;
860         char                            *stripe_off_arg = NULL;
861         char                            *stripe_count_arg = NULL;
862         char                            *pool_name_arg = NULL;
863         unsigned long long               size_units = 1;
864         bool                             migrate_mode = false;
865         __u64                            migration_flags = 0;
866         __u32                            osts[LOV_MAX_STRIPE_COUNT] = { 0 };
867         int                              nr_osts = 0;
868
869         struct option            long_opts[] = {
870                 /* valid only in migrate mode */
871                 {"block",        no_argument,       0, 'b'},
872 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
873                 /* This formerly implied "stripe-count", but was explicitly
874                  * made "stripe-count" for consistency with other options,
875                  * and to separate it from "mdt-count" when DNE arrives. */
876                 {"count",        required_argument, 0, 'c'},
877 #endif
878                 {"stripe-count", required_argument, 0, 'c'},
879                 {"stripe_count", required_argument, 0, 'c'},
880                 {"delete",       no_argument,       0, 'd'},
881 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
882                 /* This formerly implied "stripe-index", but was explicitly
883                  * made "stripe-index" for consistency with other options,
884                  * and to separate it from "mdt-index" when DNE arrives. */
885                 {"index",        required_argument, 0, 'i'},
886 #endif
887                 {"stripe-index", required_argument, 0, 'i'},
888                 {"stripe_index", required_argument, 0, 'i'},
889                 {"ost-list",     required_argument, 0, 'o'},
890                 {"ost_list",     required_argument, 0, 'o'},
891                 {"pool",         required_argument, 0, 'p'},
892 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
893                 /* This formerly implied "--stripe-size", but was confusing
894                  * with "lfs find --size|-s", which means "file size", so use
895                  * the consistent "--stripe-size|-S" for all commands. */
896                 {"size",         required_argument, 0, 's'},
897 #endif
898                 {"stripe-size",  required_argument, 0, 'S'},
899                 {"stripe_size",  required_argument, 0, 'S'},
900                 {0, 0, 0, 0}
901         };
902
903         st_size = 0;
904         st_offset = -1;
905         st_count = 0;
906
907         if (strcmp(argv[0], "migrate") == 0)
908                 migrate_mode = true;
909
910         while ((c = getopt_long(argc, argv, "bc:di:o:p:s:S:",
911                                 long_opts, NULL)) >= 0) {
912                 switch (c) {
913                 case 0:
914                         /* Long options. */
915                         break;
916                 case 'b':
917                         if (!migrate_mode) {
918                                 fprintf(stderr, "--block is valid only for"
919                                                 " migrate mode\n");
920                                 return CMD_HELP;
921                         }
922                         migration_flags |= MIGRATION_BLOCKS;
923                         break;
924                 case 'c':
925 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
926                         if (strcmp(argv[optind - 1], "--count") == 0)
927                                 fprintf(stderr, "warning: '--count' deprecated"
928                                         ", use '--stripe-count' instead\n");
929 #endif
930                         stripe_count_arg = optarg;
931                         break;
932                 case 'd':
933                         /* delete the default striping pattern */
934                         delete = 1;
935                         break;
936                 case 'o':
937                         nr_osts = parse_targets(osts, ARRAY_SIZE(osts), nr_osts,
938                                                 optarg);
939                         if (nr_osts < 0) {
940                                 fprintf(stderr,
941                                         "error: %s: bad OST indices '%s'\n",
942                                         argv[0], optarg);
943                                 return CMD_HELP;
944                         }
945
946                         if (st_offset == -1) /* first in the command line */
947                                 st_offset = osts[0];
948                         break;
949                 case 'i':
950 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
951                         if (strcmp(argv[optind - 1], "--index") == 0)
952                                 fprintf(stderr, "warning: '--index' deprecated"
953                                         ", use '--stripe-index' instead\n");
954 #endif
955                         stripe_off_arg = optarg;
956                         break;
957 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
958                 case 's':
959 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
960                         fprintf(stderr, "warning: '--size|-s' deprecated, "
961                                 "use '--stripe-size|-S' instead\n");
962 #endif
963 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
964                 case 'S':
965                         stripe_size_arg = optarg;
966                         break;
967                 case 'p':
968                         pool_name_arg = optarg;
969                         break;
970                 default:
971                         return CMD_HELP;
972                 }
973         }
974
975         fname = argv[optind];
976
977         if (delete &&
978             (stripe_size_arg != NULL || stripe_off_arg != NULL ||
979              stripe_count_arg != NULL || pool_name_arg != NULL)) {
980                 fprintf(stderr, "error: %s: cannot specify -d with "
981                         "-s, -c, -o, or -p options\n",
982                         argv[0]);
983                 return CMD_HELP;
984         }
985
986         if (optind == argc) {
987                 fprintf(stderr, "error: %s: missing filename|dirname\n",
988                         argv[0]);
989                 return CMD_HELP;
990         }
991
992         if (pool_name_arg && strlen(pool_name_arg) > LOV_MAXPOOLNAME) {
993                 fprintf(stderr,
994                         "error: %s: pool name '%s' is too long (max is %d characters)\n",
995                         argv[0], pool_name_arg, LOV_MAXPOOLNAME);
996                 return CMD_HELP;
997         }
998
999         /* get the stripe size */
1000         if (stripe_size_arg != NULL) {
1001                 result = llapi_parse_size(stripe_size_arg, &st_size,
1002                                           &size_units, 0);
1003                 if (result) {
1004                         fprintf(stderr, "error: %s: bad stripe size '%s'\n",
1005                                 argv[0], stripe_size_arg);
1006                         return CMD_HELP;
1007                 }
1008         }
1009         /* get the stripe offset */
1010         if (stripe_off_arg != NULL) {
1011                 st_offset = strtol(stripe_off_arg, &end, 0);
1012                 if (*end != '\0') {
1013                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
1014                                 argv[0], stripe_off_arg);
1015                         return CMD_HELP;
1016                 }
1017         }
1018         /* get the stripe count */
1019         if (stripe_count_arg != NULL) {
1020                 st_count = strtoul(stripe_count_arg, &end, 0);
1021                 if (*end != '\0') {
1022                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
1023                                 argv[0], stripe_count_arg);
1024                         return CMD_HELP;
1025                 }
1026         }
1027
1028         /* initialize stripe parameters */
1029         param = calloc(1, offsetof(typeof(*param), lsp_osts[nr_osts]));
1030         if (param == NULL) {
1031                 fprintf(stderr, "error: %s: run out of memory\n", argv[0]);
1032                 return CMD_HELP;
1033         }
1034
1035         param->lsp_stripe_size = st_size;
1036         param->lsp_stripe_offset = st_offset;
1037         param->lsp_stripe_count = st_count;
1038         param->lsp_stripe_pattern = 0;
1039         param->lsp_pool = pool_name_arg;
1040         param->lsp_is_specific = false;
1041         if (nr_osts > 0) {
1042                 if (st_count > 0 && nr_osts != st_count) {
1043                         fprintf(stderr, "error: %s: stripe count '%d' doesn't "
1044                                 "match the number of OSTs: %d\n",
1045                                 argv[0], st_count, nr_osts);
1046                         return CMD_HELP;
1047                 }
1048
1049                 param->lsp_is_specific = true;
1050                 param->lsp_stripe_count = nr_osts;
1051                 memcpy(param->lsp_osts, osts, sizeof(*osts) * nr_osts);
1052         }
1053
1054         for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
1055                 if (migrate_mode) {
1056                         result = lfs_migrate(fname, migration_flags, param);
1057                 } else {
1058                         result = llapi_file_open_param(fname,
1059                                                        O_CREAT | O_WRONLY,
1060                                                        0644, param);
1061                         if (result >= 0) {
1062                                 close(result);
1063                                 result = 0;
1064                         }
1065                 }
1066                 if (result) {
1067                         /* Save the first error encountered. */
1068                         if (result2 == 0)
1069                                 result2 = result;
1070                         fprintf(stderr,
1071                                 "error: %s: %s stripe file '%s' failed\n",
1072                                 argv[0], migrate_mode ? "migrate" : "create",
1073                                 fname);
1074                         continue;
1075                 }
1076         }
1077
1078         free(param);
1079         return result2;
1080 }
1081
1082 static int lfs_poollist(int argc, char **argv)
1083 {
1084         if (argc != 2)
1085                 return CMD_HELP;
1086
1087         return llapi_poollist(argv[1]);
1088 }
1089
1090 static int set_time(time_t *time, time_t *set, char *str)
1091 {
1092         time_t t;
1093         int res = 0;
1094
1095         if (str[0] == '+')
1096                 res = 1;
1097         else if (str[0] == '-')
1098                 res = -1;
1099
1100         if (res)
1101                 str++;
1102
1103         t = strtol(str, NULL, 0);
1104         if (*time < t * 24 * 60 * 60) {
1105                 if (res)
1106                         str--;
1107                 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
1108                 return INT_MAX;
1109         }
1110
1111         *set = *time - t * 24 * 60 * 60;
1112         return res;
1113 }
1114
1115 #define USER 0
1116 #define GROUP 1
1117
1118 static int name2id(unsigned int *id, char *name, int type)
1119 {
1120         if (type == USER) {
1121                 struct passwd *entry;
1122
1123                 if (!(entry = getpwnam(name))) {
1124                         if (!errno)
1125                                 errno = ENOENT;
1126                         return -1;
1127                 }
1128
1129                 *id = entry->pw_uid;
1130         } else {
1131                 struct group *entry;
1132
1133                 if (!(entry = getgrnam(name))) {
1134                         if (!errno)
1135                                 errno = ENOENT;
1136                         return -1;
1137                 }
1138
1139                 *id = entry->gr_gid;
1140         }
1141
1142         return 0;
1143 }
1144
1145 static int id2name(char **name, unsigned int id, int type)
1146 {
1147         if (type == USER) {
1148                 struct passwd *entry;
1149
1150                 if (!(entry = getpwuid(id))) {
1151                         if (!errno)
1152                                 errno = ENOENT;
1153                         return -1;
1154                 }
1155
1156                 *name = entry->pw_name;
1157         } else {
1158                 struct group *entry;
1159
1160                 if (!(entry = getgrgid(id))) {
1161                         if (!errno)
1162                                 errno = ENOENT;
1163                         return -1;
1164                 }
1165
1166                 *name = entry->gr_name;
1167         }
1168
1169         return 0;
1170 }
1171
1172 static int name2layout(__u32 *layout, char *name)
1173 {
1174         char *ptr, *lyt;
1175
1176         *layout = 0;
1177         for (ptr = name; ; ptr = NULL) {
1178                 lyt = strtok(ptr, ",");
1179                 if (lyt == NULL)
1180                         break;
1181                 if (strcmp(lyt, "released") == 0)
1182                         *layout |= LOV_PATTERN_F_RELEASED;
1183                 else if (strcmp(lyt, "raid0") == 0)
1184                         *layout |= LOV_PATTERN_RAID0;
1185                 else
1186                         return -1;
1187         }
1188         return 0;
1189 }
1190
1191 #define FIND_POOL_OPT 3
1192 static int lfs_find(int argc, char **argv)
1193 {
1194         int c, rc;
1195         int ret = 0;
1196         time_t t;
1197         struct find_param param = {
1198                 .fp_max_depth = -1,
1199                 .fp_quiet = 1,
1200         };
1201         struct option long_opts[] = {
1202                 {"atime",        required_argument, 0, 'A'},
1203                 {"stripe-count", required_argument, 0, 'c'},
1204                 {"stripe_count", required_argument, 0, 'c'},
1205                 {"ctime",        required_argument, 0, 'C'},
1206                 {"maxdepth",     required_argument, 0, 'D'},
1207                 {"gid",          required_argument, 0, 'g'},
1208                 {"group",        required_argument, 0, 'G'},
1209                 {"stripe-index", required_argument, 0, 'i'},
1210                 {"stripe_index", required_argument, 0, 'i'},
1211                 {"layout",       required_argument, 0, 'L'},
1212                 {"mdt",          required_argument, 0, 'm'},
1213                 {"mtime",        required_argument, 0, 'M'},
1214                 {"name",         required_argument, 0, 'n'},
1215      /* reserve {"or",           no_argument,     , 0, 'o'}, to match find(1) */
1216                 {"obd",          required_argument, 0, 'O'},
1217                 {"ost",          required_argument, 0, 'O'},
1218                 /* no short option for pool, p/P already used */
1219                 {"pool",         required_argument, 0, FIND_POOL_OPT},
1220                 {"print0",       no_argument,       0, 'p'},
1221                 {"print",        no_argument,       0, 'P'},
1222                 {"size",         required_argument, 0, 's'},
1223                 {"stripe-size",  required_argument, 0, 'S'},
1224                 {"stripe_size",  required_argument, 0, 'S'},
1225                 {"type",         required_argument, 0, 't'},
1226                 {"uid",          required_argument, 0, 'u'},
1227                 {"user",         required_argument, 0, 'U'},
1228                 {0, 0, 0, 0}
1229         };
1230         int pathstart = -1;
1231         int pathend = -1;
1232         int neg_opt = 0;
1233         time_t *xtime;
1234         int *xsign;
1235         int isoption;
1236         char *endptr;
1237
1238         time(&t);
1239
1240         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
1241         while ((c = getopt_long_only(argc, argv,
1242                                      "-A:c:C:D:g:G:i:L:m:M:n:O:Ppqrs:S:t:u:U:v",
1243                                      long_opts, NULL)) >= 0) {
1244                 xtime = NULL;
1245                 xsign = NULL;
1246                 if (neg_opt)
1247                         --neg_opt;
1248                 /* '!' is part of option */
1249                 /* when getopt_long_only() finds a string which is not
1250                  * an option nor a known option argument it returns 1
1251                  * in that case if we already have found pathstart and pathend
1252                  * (i.e. we have the list of pathnames),
1253                  * the only supported value is "!"
1254                  */
1255                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
1256                 if (!isoption && pathend != -1) {
1257                         fprintf(stderr, "err: %s: filename|dirname must either "
1258                                         "precede options or follow options\n",
1259                                         argv[0]);
1260                         ret = CMD_HELP;
1261                         goto err;
1262                 }
1263                 if (!isoption && pathstart == -1)
1264                         pathstart = optind - 1;
1265                 if (isoption && pathstart != -1 && pathend == -1)
1266                         pathend = optind - 2;
1267                 switch (c) {
1268                 case 0:
1269                         /* Long options. */
1270                         break;
1271                 case 1:
1272                         /* unknown; opt is "!" or path component,
1273                          * checking done above.
1274                          */
1275                         if (strcmp(optarg, "!") == 0)
1276                                 neg_opt = 2;
1277                         break;
1278                 case 'A':
1279                         xtime = &param.fp_atime;
1280                         xsign = &param.fp_asign;
1281                         param.fp_exclude_atime = !!neg_opt;
1282                         /* no break, this falls through to 'C' for ctime */
1283                 case 'C':
1284                         if (c == 'C') {
1285                                 xtime = &param.fp_ctime;
1286                                 xsign = &param.fp_csign;
1287                                 param.fp_exclude_ctime = !!neg_opt;
1288                         }
1289                         /* no break, this falls through to 'M' for mtime */
1290                 case 'M':
1291                         if (c == 'M') {
1292                                 xtime = &param.fp_mtime;
1293                                 xsign = &param.fp_msign;
1294                                 param.fp_exclude_mtime = !!neg_opt;
1295                         }
1296                         rc = set_time(&t, xtime, optarg);
1297                         if (rc == INT_MAX) {
1298                                 ret = -1;
1299                                 goto err;
1300                         }
1301                         if (rc)
1302                                 *xsign = rc;
1303                         break;
1304                 case 'c':
1305                         if (optarg[0] == '+') {
1306                                 param.fp_stripe_count_sign = -1;
1307                                 optarg++;
1308                         } else if (optarg[0] == '-') {
1309                                 param.fp_stripe_count_sign =  1;
1310                                 optarg++;
1311                         }
1312
1313                         param.fp_stripe_count = strtoul(optarg, &endptr, 0);
1314                         if (*endptr != '\0') {
1315                                 fprintf(stderr,"error: bad stripe_count '%s'\n",
1316                                         optarg);
1317                                 ret = -1;
1318                                 goto err;
1319                         }
1320                         param.fp_check_stripe_count = 1;
1321                         param.fp_exclude_stripe_count = !!neg_opt;
1322                         break;
1323                 case 'D':
1324                         param.fp_max_depth = strtol(optarg, 0, 0);
1325                         break;
1326                 case 'g':
1327                 case 'G':
1328                         rc = name2id(&param.fp_gid, optarg, GROUP);
1329                         if (rc) {
1330                                 param.fp_gid = strtoul(optarg, &endptr, 10);
1331                                 if (*endptr != '\0') {
1332                                         fprintf(stderr, "Group/GID: %s cannot "
1333                                                 "be found.\n", optarg);
1334                                         ret = -1;
1335                                         goto err;
1336                                 }
1337                         }
1338                         param.fp_exclude_gid = !!neg_opt;
1339                         param.fp_check_gid = 1;
1340                         break;
1341                 case 'L':
1342                         ret = name2layout(&param.fp_layout, optarg);
1343                         if (ret)
1344                                 goto err;
1345                         param.fp_exclude_layout = !!neg_opt;
1346                         param.fp_check_layout = 1;
1347                         break;
1348                 case 'u':
1349                 case 'U':
1350                         rc = name2id(&param.fp_uid, optarg, USER);
1351                         if (rc) {
1352                                 param.fp_uid = strtoul(optarg, &endptr, 10);
1353                                 if (*endptr != '\0') {
1354                                         fprintf(stderr, "User/UID: %s cannot "
1355                                                 "be found.\n", optarg);
1356                                         ret = -1;
1357                                         goto err;
1358                                 }
1359                         }
1360                         param.fp_exclude_uid = !!neg_opt;
1361                         param.fp_check_uid = 1;
1362                         break;
1363                 case FIND_POOL_OPT:
1364                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
1365                                 fprintf(stderr,
1366                                         "Pool name %s is too long"
1367                                         " (max is %d)\n", optarg,
1368                                         LOV_MAXPOOLNAME);
1369                                 ret = -1;
1370                                 goto err;
1371                         }
1372                         /* we do check for empty pool because empty pool
1373                          * is used to find V1 lov attributes */
1374                         strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
1375                         param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
1376                         param.fp_exclude_pool = !!neg_opt;
1377                         param.fp_check_pool = 1;
1378                         break;
1379                 case 'n':
1380                         param.fp_pattern = (char *)optarg;
1381                         param.fp_exclude_pattern = !!neg_opt;
1382                         break;
1383                 case 'm':
1384                 case 'i':
1385                 case 'O': {
1386                         char *buf, *token, *next, *p;
1387                         int len = 1;
1388                         void *tmp;
1389
1390                         buf = strdup(optarg);
1391                         if (buf == NULL) {
1392                                 ret = -ENOMEM;
1393                                 goto err;
1394                         }
1395
1396                         param.fp_exclude_obd = !!neg_opt;
1397
1398                         token = buf;
1399                         while (token && *token) {
1400                                 token = strchr(token, ',');
1401                                 if (token) {
1402                                         len++;
1403                                         token++;
1404                                 }
1405                         }
1406                         if (c == 'm') {
1407                                 param.fp_exclude_mdt = !!neg_opt;
1408                                 param.fp_num_alloc_mdts += len;
1409                                 tmp = realloc(param.fp_mdt_uuid,
1410                                               param.fp_num_alloc_mdts *
1411                                               sizeof(*param.fp_mdt_uuid));
1412                                 if (tmp == NULL) {
1413                                         ret = -ENOMEM;
1414                                         goto err_free;
1415                                 }
1416
1417                                 param.fp_mdt_uuid = tmp;
1418                         } else {
1419                                 param.fp_exclude_obd = !!neg_opt;
1420                                 param.fp_num_alloc_obds += len;
1421                                 tmp = realloc(param.fp_obd_uuid,
1422                                               param.fp_num_alloc_obds *
1423                                               sizeof(*param.fp_obd_uuid));
1424                                 if (tmp == NULL) {
1425                                         ret = -ENOMEM;
1426                                         goto err_free;
1427                                 }
1428
1429                                 param.fp_obd_uuid = tmp;
1430                         }
1431                         for (token = buf; token && *token; token = next) {
1432                                 struct obd_uuid *puuid;
1433                                 if (c == 'm') {
1434                                         puuid =
1435                                         &param.fp_mdt_uuid[param.fp_num_mdts++];
1436                                 } else {
1437                                         puuid =
1438                                         &param.fp_obd_uuid[param.fp_num_obds++];
1439                                 }
1440                                 p = strchr(token, ',');
1441                                 next = 0;
1442                                 if (p) {
1443                                         *p = 0;
1444                                         next = p+1;
1445                                 }
1446
1447                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
1448                                         ret = -E2BIG;
1449                                         goto err_free;
1450                                 }
1451
1452                                 strncpy(puuid->uuid, token,
1453                                         sizeof(puuid->uuid));
1454                         }
1455 err_free:
1456                         if (buf)
1457                                 free(buf);
1458                         break;
1459                 }
1460                 case 'p':
1461                         param.fp_zero_end = 1;
1462                         break;
1463                 case 'P':
1464                         break;
1465                 case 's':
1466                         if (optarg[0] == '+') {
1467                                 param.fp_size_sign = -1;
1468                                 optarg++;
1469                         } else if (optarg[0] == '-') {
1470                                 param.fp_size_sign =  1;
1471                                 optarg++;
1472                         }
1473
1474                         ret = llapi_parse_size(optarg, &param.fp_size,
1475                                                &param.fp_size_units, 0);
1476                         if (ret) {
1477                                 fprintf(stderr, "error: bad file size '%s'\n",
1478                                         optarg);
1479                                 goto err;
1480                         }
1481                         param.fp_check_size = 1;
1482                         param.fp_exclude_size = !!neg_opt;
1483                         break;
1484                 case 'S':
1485                         if (optarg[0] == '+') {
1486                                 param.fp_stripe_size_sign = -1;
1487                                 optarg++;
1488                         } else if (optarg[0] == '-') {
1489                                 param.fp_stripe_size_sign =  1;
1490                                 optarg++;
1491                         }
1492
1493                         ret = llapi_parse_size(optarg, &param.fp_stripe_size,
1494                                                &param.fp_stripe_size_units, 0);
1495                         if (ret) {
1496                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
1497                                         optarg);
1498                                 goto err;
1499                         }
1500                         param.fp_check_stripe_size = 1;
1501                         param.fp_exclude_stripe_size = !!neg_opt;
1502                         break;
1503                 case 't':
1504                         param.fp_exclude_type = !!neg_opt;
1505                         switch (optarg[0]) {
1506                         case 'b':
1507                                 param.fp_type = S_IFBLK;
1508                                 break;
1509                         case 'c':
1510                                 param.fp_type = S_IFCHR;
1511                                 break;
1512                         case 'd':
1513                                 param.fp_type = S_IFDIR;
1514                                 break;
1515                         case 'f':
1516                                 param.fp_type = S_IFREG;
1517                                 break;
1518                         case 'l':
1519                                 param.fp_type = S_IFLNK;
1520                                 break;
1521                         case 'p':
1522                                 param.fp_type = S_IFIFO;
1523                                 break;
1524                         case 's':
1525                                 param.fp_type = S_IFSOCK;
1526                                 break;
1527                         default:
1528                                 fprintf(stderr, "error: %s: bad type '%s'\n",
1529                                         argv[0], optarg);
1530                                 ret = CMD_HELP;
1531                                 goto err;
1532                         };
1533                         break;
1534                 default:
1535                         ret = CMD_HELP;
1536                         goto err;
1537                 };
1538         }
1539
1540         if (pathstart == -1) {
1541                 fprintf(stderr, "error: %s: no filename|pathname\n",
1542                         argv[0]);
1543                 ret = CMD_HELP;
1544                 goto err;
1545         } else if (pathend == -1) {
1546                 /* no options */
1547                 pathend = argc;
1548         }
1549
1550         do {
1551                 rc = llapi_find(argv[pathstart], &param);
1552                 if (rc != 0 && ret == 0)
1553                         ret = rc;
1554         } while (++pathstart < pathend);
1555
1556         if (ret)
1557                 fprintf(stderr, "error: %s failed for %s.\n",
1558                         argv[0], argv[optind - 1]);
1559 err:
1560         if (param.fp_obd_uuid && param.fp_num_alloc_obds)
1561                 free(param.fp_obd_uuid);
1562
1563         if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
1564                 free(param.fp_mdt_uuid);
1565
1566         return ret;
1567 }
1568
1569 static int lfs_getstripe_internal(int argc, char **argv,
1570                                   struct find_param *param)
1571 {
1572         struct option long_opts[] = {
1573 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1574                 /* This formerly implied "stripe-count", but was explicitly
1575                  * made "stripe-count" for consistency with other options,
1576                  * and to separate it from "mdt-count" when DNE arrives. */
1577                 {"count",               no_argument,            0, 'c'},
1578 #endif
1579                 {"stripe-count",        no_argument,            0, 'c'},
1580                 {"stripe_count",        no_argument,            0, 'c'},
1581                 {"directory",           no_argument,            0, 'd'},
1582                 {"default",             no_argument,            0, 'D'},
1583                 {"generation",          no_argument,            0, 'g'},
1584 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1585                 /* This formerly implied "stripe-index", but was explicitly
1586                  * made "stripe-index" for consistency with other options,
1587                  * and to separate it from "mdt-index" when DNE arrives. */
1588                 {"index",               no_argument,            0, 'i'},
1589 #endif
1590                 {"stripe-index",        no_argument,            0, 'i'},
1591                 {"stripe_index",        no_argument,            0, 'i'},
1592                 {"layout",              no_argument,            0, 'L'},
1593                 {"mdt-index",           no_argument,            0, 'M'},
1594                 {"mdt_index",           no_argument,            0, 'M'},
1595 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1596                 /* This formerly implied "stripe-index", but was confusing
1597                  * with "file offset" (which will eventually be needed for
1598                  * with different layouts by offset), so deprecate it. */
1599                 {"offset",              no_argument,            0, 'o'},
1600 #endif
1601                 {"obd",                 required_argument,      0, 'O'},
1602                 {"ost",                 required_argument,      0, 'O'},
1603                 {"pool",                no_argument,            0, 'p'},
1604                 {"quiet",               no_argument,            0, 'q'},
1605                 {"recursive",           no_argument,            0, 'r'},
1606                 {"raw",                 no_argument,            0, 'R'},
1607 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1608                 /* This formerly implied "--stripe-size", but was confusing
1609                  * with "lfs find --size|-s", which means "file size", so use
1610                  * the consistent "--stripe-size|-S" for all commands. */
1611                 {"size",                no_argument,            0, 's'},
1612 #endif
1613                 {"stripe-size",         no_argument,            0, 'S'},
1614                 {"stripe_size",         no_argument,            0, 'S'},
1615                 {"verbose",             no_argument,            0, 'v'},
1616                 {0, 0, 0, 0}
1617         };
1618         int c, rc;
1619
1620         param->fp_max_depth = 1;
1621         while ((c = getopt_long(argc, argv, "cdDghiLMoO:pqrRsSv",
1622                                 long_opts, NULL)) != -1) {
1623                 switch (c) {
1624                 case 'O':
1625                         if (param->fp_obd_uuid) {
1626                                 fprintf(stderr,
1627                                         "error: %s: only one obduuid allowed",
1628                                         argv[0]);
1629                                 return CMD_HELP;
1630                         }
1631                         param->fp_obd_uuid = (struct obd_uuid *)optarg;
1632                         break;
1633                 case 'q':
1634                         param->fp_quiet++;
1635                         break;
1636                 case 'd':
1637                         param->fp_max_depth = 0;
1638                         break;
1639                 case 'D':
1640                         param->fp_get_default_lmv = 1;
1641                         break;
1642                 case 'r':
1643                         param->fp_recursive = 1;
1644                         break;
1645                 case 'v':
1646                         param->fp_verbose = VERBOSE_ALL | VERBOSE_DETAIL;
1647                         break;
1648                 case 'c':
1649 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1650                         if (strcmp(argv[optind - 1], "--count") == 0)
1651                                 fprintf(stderr, "warning: '--count' deprecated,"
1652                                         " use '--stripe-count' instead\n");
1653 #endif
1654                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
1655                                 param->fp_verbose |= VERBOSE_COUNT;
1656                                 param->fp_max_depth = 0;
1657                         }
1658                         break;
1659 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1660                 case 's':
1661 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1662                         fprintf(stderr, "warning: '--size|-s' deprecated, "
1663                                 "use '--stripe-size|-S' instead\n");
1664 #endif
1665 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
1666                 case 'S':
1667                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
1668                                 param->fp_verbose |= VERBOSE_SIZE;
1669                                 param->fp_max_depth = 0;
1670                         }
1671                         break;
1672 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1673                 case 'o':
1674                         fprintf(stderr, "warning: '--offset|-o' deprecated, "
1675                                 "use '--stripe-index|-i' instead\n");
1676 #endif
1677                 case 'i':
1678 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1679                         if (strcmp(argv[optind - 1], "--index") == 0)
1680                                 fprintf(stderr, "warning: '--index' deprecated"
1681                                         ", use '--stripe-index' instead\n");
1682 #endif
1683                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
1684                                 param->fp_verbose |= VERBOSE_OFFSET;
1685                                 param->fp_max_depth = 0;
1686                         }
1687                         break;
1688                 case 'p':
1689                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
1690                                 param->fp_verbose |= VERBOSE_POOL;
1691                                 param->fp_max_depth = 0;
1692                         }
1693                         break;
1694                 case 'g':
1695                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
1696                                 param->fp_verbose |= VERBOSE_GENERATION;
1697                                 param->fp_max_depth = 0;
1698                         }
1699                         break;
1700                 case 'L':
1701                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
1702                                 param->fp_verbose |= VERBOSE_LAYOUT;
1703                                 param->fp_max_depth = 0;
1704                         }
1705                         break;
1706                 case 'M':
1707                         if (!(param->fp_verbose & VERBOSE_DETAIL))
1708                                 param->fp_max_depth = 0;
1709                         param->fp_verbose |= VERBOSE_MDTINDEX;
1710                         break;
1711                 case 'R':
1712                         param->fp_raw = 1;
1713                         break;
1714                 default:
1715                         return CMD_HELP;
1716                 }
1717         }
1718
1719         if (optind >= argc)
1720                 return CMD_HELP;
1721
1722         if (param->fp_recursive)
1723                 param->fp_max_depth = -1;
1724
1725         if (!param->fp_verbose)
1726                 param->fp_verbose = VERBOSE_ALL;
1727         if (param->fp_quiet)
1728                 param->fp_verbose = VERBOSE_OBJID;
1729
1730         do {
1731                 rc = llapi_getstripe(argv[optind], param);
1732         } while (++optind < argc && !rc);
1733
1734         if (rc)
1735                 fprintf(stderr, "error: %s failed for %s.\n",
1736                         argv[0], argv[optind - 1]);
1737         return rc;
1738 }
1739
1740 static int lfs_tgts(int argc, char **argv)
1741 {
1742         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1743         struct find_param param;
1744         int index = 0, rc=0;
1745
1746         if (argc > 2)
1747                 return CMD_HELP;
1748
1749         if (argc == 2 && !realpath(argv[1], path)) {
1750                 rc = -errno;
1751                 fprintf(stderr, "error: invalid path '%s': %s\n",
1752                         argv[1], strerror(-rc));
1753                 return rc;
1754         }
1755
1756         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
1757                 /* Check if we have a mount point */
1758                 if (mntdir[0] == '\0')
1759                         continue;
1760
1761                 memset(&param, 0, sizeof(param));
1762                 if (!strcmp(argv[0], "mdts"))
1763                         param.fp_get_lmv = 1;
1764
1765                 rc = llapi_ostlist(mntdir, &param);
1766                 if (rc) {
1767                         fprintf(stderr, "error: %s: failed on %s\n",
1768                                 argv[0], mntdir);
1769                 }
1770                 if (path[0] != '\0')
1771                         break;
1772                 memset(mntdir, 0, PATH_MAX);
1773         }
1774
1775         return rc;
1776 }
1777
1778 static int lfs_getstripe(int argc, char **argv)
1779 {
1780         struct find_param param = { 0 };
1781         return lfs_getstripe_internal(argc, argv, &param);
1782 }
1783
1784 /* functions */
1785 static int lfs_getdirstripe(int argc, char **argv)
1786 {
1787         struct find_param param = { 0 };
1788
1789         param.fp_get_lmv = 1;
1790         return lfs_getstripe_internal(argc, argv, &param);
1791 }
1792
1793 /* functions */
1794 static int lfs_setdirstripe(int argc, char **argv)
1795 {
1796         char                    *dname;
1797         int                     result;
1798         unsigned int            stripe_offset = -1;
1799         unsigned int            stripe_count = 1;
1800         enum lmv_hash_type      hash_type;
1801         char                    *end;
1802         int                     c;
1803         char                    *stripe_offset_opt = NULL;
1804         char                    *stripe_count_opt = NULL;
1805         char                    *stripe_hash_opt = NULL;
1806         char                    *mode_opt = NULL;
1807         bool                    default_stripe = false;
1808         mode_t                  mode = S_IRWXU | S_IRWXG | S_IRWXO;
1809         mode_t                  previous_mode = 0;
1810         bool                    delete = false;
1811
1812         struct option long_opts[] = {
1813                 {"count",       required_argument, 0, 'c'},
1814                 {"delete",      no_argument, 0, 'd'},
1815                 {"index",       required_argument, 0, 'i'},
1816                 {"mode",        required_argument, 0, 'm'},
1817                 {"hash-type",   required_argument, 0, 't'},
1818                 {"default_stripe", no_argument, 0, 'D'},
1819                 {0, 0, 0, 0}
1820         };
1821
1822         while ((c = getopt_long(argc, argv, "c:dDi:m:t:", long_opts,
1823                                 NULL)) >= 0) {
1824                 switch (c) {
1825                 case 0:
1826                         /* Long options. */
1827                         break;
1828                 case 'c':
1829                         stripe_count_opt = optarg;
1830                         break;
1831                 case 'd':
1832                         delete = true;
1833                         default_stripe = true;
1834                         break;
1835                 case 'D':
1836                         default_stripe = true;
1837                         break;
1838                 case 'i':
1839                         stripe_offset_opt = optarg;
1840                         break;
1841                 case 'm':
1842                         mode_opt = optarg;
1843                         break;
1844                 case 't':
1845                         stripe_hash_opt = optarg;
1846                         break;
1847                 default:
1848                         fprintf(stderr, "error: %s: option '%s' "
1849                                         "unrecognized\n",
1850                                         argv[0], argv[optind - 1]);
1851                         return CMD_HELP;
1852                 }
1853         }
1854
1855         if (optind == argc) {
1856                 fprintf(stderr, "error: %s: missing dirname\n",
1857                         argv[0]);
1858                 return CMD_HELP;
1859         }
1860
1861         if (!delete && stripe_offset_opt == NULL && stripe_count_opt == NULL) {
1862                 fprintf(stderr, "error: %s: missing stripe offset and count.\n",
1863                         argv[0]);
1864                 return CMD_HELP;
1865         }
1866
1867         if (stripe_offset_opt != NULL) {
1868                 /* get the stripe offset */
1869                 stripe_offset = strtoul(stripe_offset_opt, &end, 0);
1870                 if (*end != '\0') {
1871                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
1872                                 argv[0], stripe_offset_opt);
1873                         return CMD_HELP;
1874                 }
1875         }
1876
1877         if (delete) {
1878                 if (stripe_offset_opt != NULL || stripe_count_opt != NULL) {
1879                         fprintf(stderr, "error: %s: cannot specify -d with -s,"
1880                                 " or -i options.\n", argv[0]);
1881                         return CMD_HELP;
1882                 } else {
1883                         stripe_count = 0;
1884                 }
1885         }
1886
1887
1888         if (mode_opt != NULL) {
1889                 mode = strtoul(mode_opt, &end, 8);
1890                 if (*end != '\0') {
1891                         fprintf(stderr, "error: %s: bad mode '%s'\n",
1892                                 argv[0], mode_opt);
1893                         return CMD_HELP;
1894                 }
1895                 previous_mode = umask(0);
1896         }
1897
1898         if (stripe_hash_opt == NULL ||
1899             strcmp(stripe_hash_opt, LMV_HASH_NAME_FNV_1A_64) == 0) {
1900                 hash_type = LMV_HASH_TYPE_FNV_1A_64;
1901         } else if (strcmp(stripe_hash_opt, LMV_HASH_NAME_ALL_CHARS) == 0) {
1902                 hash_type = LMV_HASH_TYPE_ALL_CHARS;
1903         } else {
1904                 fprintf(stderr, "error: %s: bad stripe hash type '%s'\n",
1905                         argv[0], stripe_hash_opt);
1906                 return CMD_HELP;
1907         }
1908
1909         /* get the stripe count */
1910         if (stripe_count_opt != NULL) {
1911                 stripe_count = strtoul(stripe_count_opt, &end, 0);
1912                 if (*end != '\0') {
1913                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
1914                                 argv[0], stripe_count_opt);
1915                         return CMD_HELP;
1916                 }
1917         }
1918
1919         dname = argv[optind];
1920         do {
1921                 if (default_stripe) {
1922                         result = llapi_dir_set_default_lmv_stripe(dname,
1923                                                     stripe_offset, stripe_count,
1924                                                     hash_type, NULL);
1925                 } else {
1926                         result = llapi_dir_create_pool(dname, mode,
1927                                                        stripe_offset,
1928                                                        stripe_count, hash_type,
1929                                                        NULL);
1930                 }
1931
1932                 if (result) {
1933                         fprintf(stderr, "error: %s: create stripe dir '%s' "
1934                                 "failed\n", argv[0], dname);
1935                         break;
1936                 }
1937                 dname = argv[++optind];
1938         } while (dname != NULL);
1939
1940         if (mode_opt != NULL)
1941                 umask(previous_mode);
1942
1943         return result;
1944 }
1945
1946 /* functions */
1947 static int lfs_rmentry(int argc, char **argv)
1948 {
1949         char *dname;
1950         int   index;
1951         int   result = 0;
1952
1953         if (argc <= 1) {
1954                 fprintf(stderr, "error: %s: missing dirname\n",
1955                         argv[0]);
1956                 return CMD_HELP;
1957         }
1958
1959         index = 1;
1960         dname = argv[index];
1961         while (dname != NULL) {
1962                 result = llapi_direntry_remove(dname);
1963                 if (result) {
1964                         fprintf(stderr, "error: %s: remove dir entry '%s' "
1965                                 "failed\n", argv[0], dname);
1966                         break;
1967                 }
1968                 dname = argv[++index];
1969         }
1970         return result;
1971 }
1972
1973 static int lfs_mv(int argc, char **argv)
1974 {
1975         struct  find_param param = {
1976                 .fp_max_depth = -1,
1977                 .fp_mdt_index = -1,
1978         };
1979         char   *end;
1980         int     c;
1981         int     rc = 0;
1982         struct option long_opts[] = {
1983                 {"mdt-index", required_argument, 0, 'M'},
1984                 {"verbose",     no_argument,       0, 'v'},
1985                 {0, 0, 0, 0}
1986         };
1987
1988         while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
1989                 switch (c) {
1990                 case 'M': {
1991                         param.fp_mdt_index = strtoul(optarg, &end, 0);
1992                         if (*end != '\0') {
1993                                 fprintf(stderr, "%s: invalid MDT index'%s'\n",
1994                                         argv[0], optarg);
1995                                 return CMD_HELP;
1996                         }
1997                         break;
1998                 }
1999                 case 'v': {
2000                         param.fp_verbose = VERBOSE_DETAIL;
2001                         break;
2002                 }
2003                 default:
2004                         fprintf(stderr, "error: %s: unrecognized option '%s'\n",
2005                                 argv[0], argv[optind - 1]);
2006                         return CMD_HELP;
2007                 }
2008         }
2009
2010         if (param.fp_mdt_index == -1) {
2011                 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
2012                 return CMD_HELP;
2013         }
2014
2015         if (optind >= argc) {
2016                 fprintf(stderr, "%s: missing operand path\n", argv[0]);
2017                 return CMD_HELP;
2018         }
2019
2020         param.fp_migrate = 1;
2021         rc = llapi_mv(argv[optind], &param);
2022         if (rc != 0)
2023                 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
2024                         argv[0], argv[optind], param.fp_mdt_index,
2025                         strerror(-rc));
2026         return rc;
2027 }
2028
2029 static int lfs_osts(int argc, char **argv)
2030 {
2031         return lfs_tgts(argc, argv);
2032 }
2033
2034 static int lfs_mdts(int argc, char **argv)
2035 {
2036         return lfs_tgts(argc, argv);
2037 }
2038
2039 #define COOK(value)                                                     \
2040 ({                                                                      \
2041         int radix = 0;                                                  \
2042         while (value > 1024) {                                          \
2043                 value /= 1024;                                          \
2044                 radix++;                                                \
2045         }                                                               \
2046         radix;                                                          \
2047 })
2048 #define UUF     "%-20s"
2049 #define CSF     "%11s"
2050 #define CDF     "%11llu"
2051 #define HDF     "%8.1f%c"
2052 #define RSF     "%4s"
2053 #define RDF     "%3d%%"
2054
2055 static int showdf(char *mntdir, struct obd_statfs *stat,
2056                   char *uuid, int ishow, int cooked,
2057                   char *type, int index, int rc)
2058 {
2059         long long avail, used, total;
2060         double ratio = 0;
2061         char *suffix = "KMGTPEZY";
2062         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
2063         char tbuf[3 * sizeof(__u64)];
2064         char ubuf[3 * sizeof(__u64)];
2065         char abuf[3 * sizeof(__u64)];
2066         char rbuf[3 * sizeof(__u64)];
2067
2068         if (!uuid || !stat)
2069                 return -EINVAL;
2070
2071         switch (rc) {
2072         case 0:
2073                 if (ishow) {
2074                         avail = stat->os_ffree;
2075                         used = stat->os_files - stat->os_ffree;
2076                         total = stat->os_files;
2077                 } else {
2078                         int shift = cooked ? 0 : 10;
2079
2080                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
2081                         used  = ((stat->os_blocks - stat->os_bfree) *
2082                                  stat->os_bsize) >> shift;
2083                         total = (stat->os_blocks * stat->os_bsize) >> shift;
2084                 }
2085
2086                 if ((used + avail) > 0)
2087                         ratio = (double)used / (double)(used + avail);
2088
2089                 if (cooked) {
2090                         int i;
2091                         double cook_val;
2092
2093                         cook_val = (double)total;
2094                         i = COOK(cook_val);
2095                         if (i > 0)
2096                                 sprintf(tbuf, HDF, cook_val, suffix[i - 1]);
2097                         else
2098                                 sprintf(tbuf, CDF, total);
2099
2100                         cook_val = (double)used;
2101                         i = COOK(cook_val);
2102                         if (i > 0)
2103                                 sprintf(ubuf, HDF, cook_val, suffix[i - 1]);
2104                         else
2105                                 sprintf(ubuf, CDF, used);
2106
2107                         cook_val = (double)avail;
2108                         i = COOK(cook_val);
2109                         if (i > 0)
2110                                 sprintf(abuf, HDF, cook_val, suffix[i - 1]);
2111                         else
2112                                 sprintf(abuf, CDF, avail);
2113                 } else {
2114                         sprintf(tbuf, CDF, total);
2115                         sprintf(ubuf, CDF, used);
2116                         sprintf(abuf, CDF, avail);
2117                 }
2118
2119                 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
2120                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
2121                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
2122                 if (type)
2123                         printf("[%s:%d]\n", type, index);
2124                 else
2125                         printf("\n");
2126
2127                 break;
2128         case -ENODATA:
2129                 printf(UUF": inactive device\n", uuid);
2130                 break;
2131         default:
2132                 printf(UUF": %s\n", uuid, strerror(-rc));
2133                 break;
2134         }
2135
2136         return 0;
2137 }
2138
2139 struct ll_stat_type {
2140         int   st_op;
2141         char *st_name;
2142 };
2143
2144 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow,
2145                 int cooked, int lazy)
2146 {
2147         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
2148         struct obd_uuid uuid_buf;
2149         char *poolname = NULL;
2150         struct ll_stat_type types[] = { { LL_STATFS_LMV, "MDT" },
2151                                         { LL_STATFS_LOV, "OST" },
2152                                         { 0, NULL } };
2153         struct ll_stat_type *tp;
2154         __u64 ost_ffree = 0;
2155         __u32 index;
2156         __u32 type;
2157         int rc;
2158
2159         if (pool) {
2160                 poolname = strchr(pool, '.');
2161                 if (poolname != NULL) {
2162                         if (strncmp(fsname, pool, strlen(fsname))) {
2163                                 fprintf(stderr, "filesystem name incorrect\n");
2164                                 return -ENODEV;
2165                         }
2166                         poolname++;
2167                 } else
2168                         poolname = pool;
2169         }
2170
2171         if (ishow)
2172                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
2173                        "UUID", "Inodes", "IUsed", "IFree",
2174                        "IUse%", "Mounted on");
2175         else
2176                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
2177                        "UUID", cooked ? "bytes" : "1K-blocks",
2178                        "Used", "Available", "Use%", "Mounted on");
2179
2180         for (tp = types; tp->st_name != NULL; tp++) {
2181                 for (index = 0; ; index++) {
2182                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
2183                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2184                         type = lazy ? tp->st_op | LL_STATFS_NODELAY : tp->st_op;
2185                         rc = llapi_obd_statfs(mntdir, type, index,
2186                                               &stat_buf, &uuid_buf);
2187                         if (rc == -ENODEV)
2188                                 break;
2189
2190                         if (rc == -EAGAIN)
2191                                 continue;
2192
2193                         if (poolname && tp->st_op == LL_STATFS_LOV &&
2194                             llapi_search_ost(fsname, poolname,
2195                                              obd_uuid2str(&uuid_buf)) != 1)
2196                                 continue;
2197
2198                         /* the llapi_obd_statfs() call may have returned with
2199                          * an error, but if it filled in uuid_buf we will at
2200                          * lease use that to print out a message for that OBD.
2201                          * If we didn't get anything in the uuid_buf, then fill
2202                          * it in so that we can print an error message. */
2203                         if (uuid_buf.uuid[0] == '\0')
2204                                 sprintf(uuid_buf.uuid, "%s%04x",
2205                                         tp->st_name, index);
2206                         showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
2207                                ishow, cooked, tp->st_name, index, rc);
2208
2209                         if (rc == 0) {
2210                                 if (tp->st_op == LL_STATFS_LMV) {
2211                                         sum.os_ffree += stat_buf.os_ffree;
2212                                         sum.os_files += stat_buf.os_files;
2213                                 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
2214                                         sum.os_blocks += stat_buf.os_blocks *
2215                                                 stat_buf.os_bsize;
2216                                         sum.os_bfree  += stat_buf.os_bfree *
2217                                                 stat_buf.os_bsize;
2218                                         sum.os_bavail += stat_buf.os_bavail *
2219                                                 stat_buf.os_bsize;
2220                                         ost_ffree += stat_buf.os_ffree;
2221                                 }
2222                         } else if (rc == -EINVAL || rc == -EFAULT) {
2223                                 break;
2224                         }
2225                 }
2226         }
2227
2228         /* If we don't have as many objects free on the OST as inodes
2229          * on the MDS, we reduce the total number of inodes to
2230          * compensate, so that the "inodes in use" number is correct.
2231          * Matches ll_statfs_internal() so the results are consistent. */
2232         if (ost_ffree < sum.os_ffree) {
2233                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
2234                 sum.os_ffree = ost_ffree;
2235         }
2236         printf("\n");
2237         showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0, 0);
2238         printf("\n");
2239         return 0;
2240 }
2241
2242 static int lfs_df(int argc, char **argv)
2243 {
2244         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
2245         int ishow = 0, cooked = 0;
2246         int lazy = 0;
2247         int c, rc = 0, index = 0;
2248         char fsname[PATH_MAX] = "", *pool_name = NULL;
2249         struct option long_opts[] = {
2250                 {"pool", required_argument, 0, 'p'},
2251                 {"lazy", 0, 0, 'l'},
2252                 {0, 0, 0, 0}
2253         };
2254
2255         while ((c = getopt_long(argc, argv, "hilp:", long_opts, NULL)) != -1) {
2256                 switch (c) {
2257                 case 'i':
2258                         ishow = 1;
2259                         break;
2260                 case 'h':
2261                         cooked = 1;
2262                         break;
2263                 case 'l':
2264                         lazy = 1;
2265                         break;
2266                 case 'p':
2267                         pool_name = optarg;
2268                         break;
2269                 default:
2270                         return CMD_HELP;
2271                 }
2272         }
2273         if (optind < argc && !realpath(argv[optind], path)) {
2274                 rc = -errno;
2275                 fprintf(stderr, "error: invalid path '%s': %s\n",
2276                         argv[optind], strerror(-rc));
2277                 return rc;
2278         }
2279
2280         while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
2281                 /* Check if we have a mount point */
2282                 if (mntdir[0] == '\0')
2283                         continue;
2284
2285                 rc = mntdf(mntdir, fsname, pool_name, ishow, cooked, lazy);
2286                 if (rc || path[0] != '\0')
2287                         break;
2288                 fsname[0] = '\0'; /* avoid matching in next loop */
2289                 mntdir[0] = '\0'; /* avoid matching in next loop */
2290         }
2291
2292         return rc;
2293 }
2294
2295 static int lfs_getname(int argc, char **argv)
2296 {
2297         char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
2298         int rc = 0, index = 0, c;
2299         char buf[sizeof(struct obd_uuid)];
2300
2301         while ((c = getopt(argc, argv, "h")) != -1)
2302                 return CMD_HELP;
2303
2304         if (optind == argc) { /* no paths specified, get all paths. */
2305                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
2306                         rc = llapi_getname(mntdir, buf, sizeof(buf));
2307                         if (rc < 0) {
2308                                 fprintf(stderr,
2309                                         "cannot get name for `%s': %s\n",
2310                                         mntdir, strerror(-rc));
2311                                 break;
2312                         }
2313
2314                         printf("%s %s\n", buf, mntdir);
2315
2316                         path[0] = fsname[0] = mntdir[0] = 0;
2317                 }
2318         } else { /* paths specified, only attempt to search these. */
2319                 for (; optind < argc; optind++) {
2320                         rc = llapi_getname(argv[optind], buf, sizeof(buf));
2321                         if (rc < 0) {
2322                                 fprintf(stderr,
2323                                         "cannot get name for `%s': %s\n",
2324                                         argv[optind], strerror(-rc));
2325                                 break;
2326                         }
2327
2328                         printf("%s %s\n", buf, argv[optind]);
2329                 }
2330         }
2331         return rc;
2332 }
2333
2334 static int lfs_check(int argc, char **argv)
2335 {
2336         int rc;
2337         char mntdir[PATH_MAX] = {'\0'};
2338         int num_types = 1;
2339         char *obd_types[2];
2340         char obd_type1[4];
2341         char obd_type2[4];
2342
2343         if (argc != 2)
2344                 return CMD_HELP;
2345
2346         obd_types[0] = obd_type1;
2347         obd_types[1] = obd_type2;
2348
2349         if (strcmp(argv[1], "osts") == 0) {
2350                 strcpy(obd_types[0], "osc");
2351         } else if (strcmp(argv[1], "mds") == 0) {
2352                 strcpy(obd_types[0], "mdc");
2353         } else if (strcmp(argv[1], "servers") == 0) {
2354                 num_types = 2;
2355                 strcpy(obd_types[0], "osc");
2356                 strcpy(obd_types[1], "mdc");
2357         } else {
2358                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2359                                 argv[0], argv[1]);
2360                         return CMD_HELP;
2361         }
2362
2363         rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
2364         if (rc < 0 || mntdir[0] == '\0') {
2365                 fprintf(stderr, "No suitable Lustre mount found\n");
2366                 return rc;
2367         }
2368
2369         rc = llapi_target_check(num_types, obd_types, mntdir);
2370         if (rc)
2371                 fprintf(stderr, "error: %s: %s status failed\n",
2372                                 argv[0],argv[1]);
2373
2374         return rc;
2375
2376 }
2377
2378 static int lfs_join(int argc, char **argv)
2379 {
2380         fprintf(stderr, "join two lustre files into one.\n"
2381                         "obsolete, HEAD does not support it anymore.\n");
2382         return 0;
2383 }
2384
2385 #ifdef HAVE_SYS_QUOTA_H
2386 #define ARG2INT(nr, str, msg)                                           \
2387 do {                                                                    \
2388         char *endp;                                                     \
2389         nr = strtol(str, &endp, 0);                                     \
2390         if (*endp) {                                                    \
2391                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
2392                 return CMD_HELP;                                        \
2393         }                                                               \
2394 } while (0)
2395
2396 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
2397
2398 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
2399  * returns the value or ULONG_MAX on integer overflow or incorrect format
2400  * Notes:
2401  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
2402  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
2403  *        3. empty integer value is interpreted as 0
2404  */
2405 static unsigned long str2sec(const char* timestr)
2406 {
2407         const char spec[] = "smhdw";
2408         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
2409         unsigned long val = 0;
2410         char *tail;
2411
2412         if (strpbrk(timestr, spec) == NULL) {
2413                 /* no specifiers inside the time string,
2414                    should treat it as an integer value */
2415                 val = strtoul(timestr, &tail, 10);
2416                 return *tail ? ULONG_MAX : val;
2417         }
2418
2419         /* format string is XXwXXdXXhXXmXXs */
2420         while (*timestr) {
2421                 unsigned long v;
2422                 int ind;
2423                 char* ptr;
2424
2425                 v = strtoul(timestr, &tail, 10);
2426                 if (v == ULONG_MAX || *tail == '\0')
2427                         /* value too large (ULONG_MAX or more)
2428                            or missing specifier */
2429                         goto error;
2430
2431                 ptr = strchr(spec, *tail);
2432                 if (ptr == NULL)
2433                         /* unknown specifier */
2434                         goto error;
2435
2436                 ind = ptr - spec;
2437
2438                 /* check if product will overflow the type */
2439                 if (!(v < ULONG_MAX / mult[ind]))
2440                         goto error;
2441
2442                 ADD_OVERFLOW(val, mult[ind] * v);
2443                 if (val == ULONG_MAX)
2444                         goto error;
2445
2446                 timestr = tail + 1;
2447         }
2448
2449         return val;
2450
2451 error:
2452         return ULONG_MAX;
2453 }
2454
2455 #define ARG2ULL(nr, str, def_units)                                     \
2456 do {                                                                    \
2457         unsigned long long limit, units = def_units;                    \
2458         int rc;                                                         \
2459                                                                         \
2460         rc = llapi_parse_size(str, &limit, &units, 1);                  \
2461         if (rc < 0) {                                                   \
2462                 fprintf(stderr, "error: bad limit value %s\n", str);    \
2463                 return CMD_HELP;                                        \
2464         }                                                               \
2465         nr = limit;                                                     \
2466 } while (0)
2467
2468 static inline int has_times_option(int argc, char **argv)
2469 {
2470         int i;
2471
2472         for (i = 1; i < argc; i++)
2473                 if (!strcmp(argv[i], "-t"))
2474                         return 1;
2475
2476         return 0;
2477 }
2478
2479 int lfs_setquota_times(int argc, char **argv)
2480 {
2481         int c, rc;
2482         struct if_quotactl qctl;
2483         char *mnt, *obd_type = (char *)qctl.obd_type;
2484         struct obd_dqblk *dqb = &qctl.qc_dqblk;
2485         struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
2486         struct option long_opts[] = {
2487                 {"block-grace",     required_argument, 0, 'b'},
2488                 {"group",           no_argument,       0, 'g'},
2489                 {"inode-grace",     required_argument, 0, 'i'},
2490                 {"times",           no_argument,       0, 't'},
2491                 {"user",            no_argument,       0, 'u'},
2492                 {0, 0, 0, 0}
2493         };
2494
2495         memset(&qctl, 0, sizeof(qctl));
2496         qctl.qc_cmd  = LUSTRE_Q_SETINFO;
2497         qctl.qc_type = UGQUOTA;
2498
2499         while ((c = getopt_long(argc, argv, "b:gi:tu", long_opts, NULL)) != -1) {
2500                 switch (c) {
2501                 case 'u':
2502                 case 'g':
2503                         if (qctl.qc_type != UGQUOTA) {
2504                                 fprintf(stderr, "error: -u and -g can't be used "
2505                                                 "more than once\n");
2506                                 return CMD_HELP;
2507                         }
2508                         qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
2509                         break;
2510                 case 'b':
2511                         if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
2512                                 fprintf(stderr, "error: bad block-grace: %s\n",
2513                                         optarg);
2514                                 return CMD_HELP;
2515                         }
2516                         dqb->dqb_valid |= QIF_BTIME;
2517                         break;
2518                 case 'i':
2519                         if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
2520                                 fprintf(stderr, "error: bad inode-grace: %s\n",
2521                                         optarg);
2522                                 return CMD_HELP;
2523                         }
2524                         dqb->dqb_valid |= QIF_ITIME;
2525                         break;
2526                 case 't': /* Yes, of course! */
2527                         break;
2528                 default: /* getopt prints error message for us when opterr != 0 */
2529                         return CMD_HELP;
2530                 }
2531         }
2532
2533         if (qctl.qc_type == UGQUOTA) {
2534                 fprintf(stderr, "error: neither -u nor -g specified\n");
2535                 return CMD_HELP;
2536         }
2537
2538         if (optind != argc - 1) {
2539                 fprintf(stderr, "error: unexpected parameters encountered\n");
2540                 return CMD_HELP;
2541         }
2542
2543         mnt = argv[optind];
2544         rc = llapi_quotactl(mnt, &qctl);
2545         if (rc) {
2546                 if (*obd_type)
2547                         fprintf(stderr, "%s %s ", obd_type,
2548                                 obd_uuid2str(&qctl.obd_uuid));
2549                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
2550                 return rc;
2551         }
2552
2553         return 0;
2554 }
2555
2556 #define BSLIMIT (1 << 0)
2557 #define BHLIMIT (1 << 1)
2558 #define ISLIMIT (1 << 2)
2559 #define IHLIMIT (1 << 3)
2560
2561 int lfs_setquota(int argc, char **argv)
2562 {
2563         int c, rc;
2564         struct if_quotactl qctl;
2565         char *mnt, *obd_type = (char *)qctl.obd_type;
2566         struct obd_dqblk *dqb = &qctl.qc_dqblk;
2567         struct option long_opts[] = {
2568                 {"block-softlimit", required_argument, 0, 'b'},
2569                 {"block-hardlimit", required_argument, 0, 'B'},
2570                 {"group",           required_argument, 0, 'g'},
2571                 {"inode-softlimit", required_argument, 0, 'i'},
2572                 {"inode-hardlimit", required_argument, 0, 'I'},
2573                 {"user",            required_argument, 0, 'u'},
2574                 {0, 0, 0, 0}
2575         };
2576         unsigned limit_mask = 0;
2577         char *endptr;
2578
2579         if (has_times_option(argc, argv))
2580                 return lfs_setquota_times(argc, argv);
2581
2582         memset(&qctl, 0, sizeof(qctl));
2583         qctl.qc_cmd  = LUSTRE_Q_SETQUOTA;
2584         qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
2585                                  * so it can be used as a marker that qc_type
2586                                  * isn't reinitialized from command line */
2587
2588         while ((c = getopt_long(argc, argv, "b:B:g:i:I:u:", long_opts, NULL)) != -1) {
2589                 switch (c) {
2590                 case 'u':
2591                 case 'g':
2592                         if (qctl.qc_type != UGQUOTA) {
2593                                 fprintf(stderr, "error: -u and -g can't be used"
2594                                                 " more than once\n");
2595                                 return CMD_HELP;
2596                         }
2597                         qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
2598                         rc = name2id(&qctl.qc_id, optarg,
2599                                      (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2600                         if (rc) {
2601                                 qctl.qc_id = strtoul(optarg, &endptr, 10);
2602                                 if (*endptr != '\0') {
2603                                         fprintf(stderr, "error: can't find id "
2604                                                 "for name %s\n", optarg);
2605                                         return CMD_HELP;
2606                                 }
2607                         }
2608                         break;
2609                 case 'b':
2610                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
2611                         dqb->dqb_bsoftlimit >>= 10;
2612                         limit_mask |= BSLIMIT;
2613                         if (dqb->dqb_bsoftlimit &&
2614                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
2615                                 fprintf(stderr, "warning: block softlimit is "
2616                                         "smaller than the miminal qunit size, "
2617                                         "please see the help of setquota or "
2618                                         "Lustre manual for details.\n");
2619                         break;
2620                 case 'B':
2621                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
2622                         dqb->dqb_bhardlimit >>= 10;
2623                         limit_mask |= BHLIMIT;
2624                         if (dqb->dqb_bhardlimit &&
2625                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
2626                                 fprintf(stderr, "warning: block hardlimit is "
2627                                         "smaller than the miminal qunit size, "
2628                                         "please see the help of setquota or "
2629                                         "Lustre manual for details.\n");
2630                         break;
2631                 case 'i':
2632                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
2633                         limit_mask |= ISLIMIT;
2634                         if (dqb->dqb_isoftlimit &&
2635                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
2636                                 fprintf(stderr, "warning: inode softlimit is "
2637                                         "smaller than the miminal qunit size, "
2638                                         "please see the help of setquota or "
2639                                         "Lustre manual for details.\n");
2640                         break;
2641                 case 'I':
2642                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
2643                         limit_mask |= IHLIMIT;
2644                         if (dqb->dqb_ihardlimit &&
2645                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
2646                                 fprintf(stderr, "warning: inode hardlimit is "
2647                                         "smaller than the miminal qunit size, "
2648                                         "please see the help of setquota or "
2649                                         "Lustre manual for details.\n");
2650                         break;
2651                 default: /* getopt prints error message for us when opterr != 0 */
2652                         return CMD_HELP;
2653                 }
2654         }
2655
2656         if (qctl.qc_type == UGQUOTA) {
2657                 fprintf(stderr, "error: neither -u nor -g was specified\n");
2658                 return CMD_HELP;
2659         }
2660
2661         if (limit_mask == 0) {
2662                 fprintf(stderr, "error: at least one limit must be specified\n");
2663                 return CMD_HELP;
2664         }
2665
2666         if (optind != argc - 1) {
2667                 fprintf(stderr, "error: unexpected parameters encountered\n");
2668                 return CMD_HELP;
2669         }
2670
2671         mnt = argv[optind];
2672
2673         if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
2674             (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
2675                 /* sigh, we can't just set blimits/ilimits */
2676                 struct if_quotactl tmp_qctl = {.qc_cmd  = LUSTRE_Q_GETQUOTA,
2677                                                .qc_type = qctl.qc_type,
2678                                                .qc_id   = qctl.qc_id};
2679
2680                 rc = llapi_quotactl(mnt, &tmp_qctl);
2681                 if (rc < 0) {
2682                         fprintf(stderr, "error: setquota failed while retrieving"
2683                                         " current quota settings (%s)\n",
2684                                         strerror(-rc));
2685                         return rc;
2686                 }
2687
2688                 if (!(limit_mask & BHLIMIT))
2689                         dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
2690                 if (!(limit_mask & BSLIMIT))
2691                         dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
2692                 if (!(limit_mask & IHLIMIT))
2693                         dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
2694                 if (!(limit_mask & ISLIMIT))
2695                         dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
2696
2697                 /* Keep grace times if we have got no softlimit arguments */
2698                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
2699                         dqb->dqb_valid |= QIF_BTIME;
2700                         dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
2701                 }
2702
2703                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
2704                         dqb->dqb_valid |= QIF_ITIME;
2705                         dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
2706                 }
2707         }
2708
2709         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
2710         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
2711
2712         rc = llapi_quotactl(mnt, &qctl);
2713         if (rc) {
2714                 if (*obd_type)
2715                         fprintf(stderr, "%s %s ", obd_type,
2716                                 obd_uuid2str(&qctl.obd_uuid));
2717                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
2718                 return rc;
2719         }
2720
2721         return 0;
2722 }
2723
2724 static inline char *type2name(int check_type)
2725 {
2726         if (check_type == USRQUOTA)
2727                 return "user";
2728         else if (check_type == GRPQUOTA)
2729                 return "group";
2730         else
2731                 return "unknown";
2732 }
2733
2734 /* Converts seconds value into format string
2735  * result is returned in buf
2736  * Notes:
2737  *        1. result is in descenting order: 1w2d3h4m5s
2738  *        2. zero fields are not filled (except for p. 3): 5d1s
2739  *        3. zero seconds value is presented as "0s"
2740  */
2741 static char * __sec2str(time_t seconds, char *buf)
2742 {
2743         const char spec[] = "smhdw";
2744         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
2745         unsigned long c;
2746         char *tail = buf;
2747         int i;
2748
2749         for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
2750                 c = seconds / mult[i];
2751
2752                 if (c > 0 || (i == 0 && buf == tail))
2753                         tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
2754
2755                 seconds %= mult[i];
2756         }
2757
2758         return tail;
2759 }
2760
2761 static void sec2str(time_t seconds, char *buf, int rc)
2762 {
2763         char *tail = buf;
2764
2765         if (rc)
2766                 *tail++ = '[';
2767
2768         tail = __sec2str(seconds, tail);
2769
2770         if (rc && tail - buf < 39) {
2771                 *tail++ = ']';
2772                 *tail++ = 0;
2773         }
2774 }
2775
2776 static void diff2str(time_t seconds, char *buf, time_t now)
2777 {
2778
2779         buf[0] = 0;
2780         if (!seconds)
2781                 return;
2782         if (seconds <= now) {
2783                 strcpy(buf, "none");
2784                 return;
2785         }
2786         __sec2str(seconds - now, buf);
2787 }
2788
2789 static void print_quota_title(char *name, struct if_quotactl *qctl,
2790                               bool human_readable)
2791 {
2792         printf("Disk quotas for %s %s (%cid %u):\n",
2793                type2name(qctl->qc_type), name,
2794                *type2name(qctl->qc_type), qctl->qc_id);
2795         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
2796                "Filesystem", human_readable ? "used" : "kbytes",
2797                "quota", "limit", "grace",
2798                "files", "quota", "limit", "grace");
2799 }
2800
2801 static void kbytes2str(__u64 num, char *buf, bool h)
2802 {
2803         if (!h) {
2804                 sprintf(buf, LPU64, num);
2805         } else {
2806                 if (num >> 30)
2807                         sprintf(buf, "%5.4gT", (double)num / (1 << 30));
2808                 else if (num >> 20)
2809                         sprintf(buf, "%5.4gG", (double)num / (1 << 20));
2810                 else if (num >> 10)
2811                         sprintf(buf, "%5.4gM", (double)num / (1 << 10));
2812                 else
2813                         sprintf(buf, LPU64"%s", num, "k");
2814         }
2815 }
2816
2817 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
2818                         int rc, bool h)
2819 {
2820         time_t now;
2821
2822         time(&now);
2823
2824         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
2825                 int bover = 0, iover = 0;
2826                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
2827                 char numbuf[3][32];
2828                 char timebuf[40];
2829                 char strbuf[32];
2830
2831                 if (dqb->dqb_bhardlimit &&
2832                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
2833                         bover = 1;
2834                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
2835                         if (dqb->dqb_btime > now) {
2836                                 bover = 2;
2837                         } else {
2838                                 bover = 3;
2839                         }
2840                 }
2841
2842                 if (dqb->dqb_ihardlimit &&
2843                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
2844                         iover = 1;
2845                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
2846                         if (dqb->dqb_itime > now) {
2847                                 iover = 2;
2848                         } else {
2849                                 iover = 3;
2850                         }
2851                 }
2852
2853
2854                 if (strlen(mnt) > 15)
2855                         printf("%s\n%15s", mnt, "");
2856                 else
2857                         printf("%15s", mnt);
2858
2859                 if (bover)
2860                         diff2str(dqb->dqb_btime, timebuf, now);
2861
2862                 kbytes2str(lustre_stoqb(dqb->dqb_curspace), strbuf, h);
2863                 if (rc == -EREMOTEIO)
2864                         sprintf(numbuf[0], "%s*", strbuf);
2865                 else
2866                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
2867                                 "%s" : "[%s]", strbuf);
2868
2869                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, h);
2870                 if (type == QC_GENERAL)
2871                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
2872                                 "%s" : "[%s]", strbuf);
2873                 else
2874                         sprintf(numbuf[1], "%s", "-");
2875
2876                 kbytes2str(dqb->dqb_bhardlimit, strbuf, h);
2877                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
2878                         "%s" : "[%s]", strbuf);
2879
2880                 printf(" %7s%c %6s %7s %7s",
2881                        numbuf[0], bover ? '*' : ' ', numbuf[1],
2882                        numbuf[2], bover > 1 ? timebuf : "-");
2883
2884                 if (iover)
2885                         diff2str(dqb->dqb_itime, timebuf, now);
2886
2887                 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2888                         LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2889
2890                 if (type == QC_GENERAL)
2891                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
2892                                 LPU64 : "["LPU64"]", dqb->dqb_isoftlimit);
2893                 else
2894                         sprintf(numbuf[1], "%s", "-");
2895
2896                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2897                         LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2898
2899                 if (type != QC_OSTIDX)
2900                         printf(" %7s%c %6s %7s %7s",
2901                                numbuf[0], iover ? '*' : ' ', numbuf[1],
2902                                numbuf[2], iover > 1 ? timebuf : "-");
2903                 else
2904                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
2905                 printf("\n");
2906
2907         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2908                    qctl->qc_cmd == Q_GETOINFO) {
2909                 char bgtimebuf[40];
2910                 char igtimebuf[40];
2911
2912                 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
2913                 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
2914                 printf("Block grace time: %s; Inode grace time: %s\n",
2915                        bgtimebuf, igtimebuf);
2916         }
2917 }
2918
2919 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
2920                            bool h, __u64 *total)
2921 {
2922         int rc = 0, rc1 = 0, count = 0;
2923         __u32 valid = qctl->qc_valid;
2924
2925         rc = llapi_get_obd_count(mnt, &count, is_mdt);
2926         if (rc) {
2927                 fprintf(stderr, "can not get %s count: %s\n",
2928                         is_mdt ? "mdt": "ost", strerror(-rc));
2929                 return rc;
2930         }
2931
2932         for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2933                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2934                 rc = llapi_quotactl(mnt, qctl);
2935                 if (rc) {
2936                         /* It is remote client case. */
2937                         if (-rc == EOPNOTSUPP) {
2938                                 rc = 0;
2939                                 goto out;
2940                         }
2941
2942                         if (!rc1)
2943                                 rc1 = rc;
2944                         fprintf(stderr, "quotactl %s%d failed.\n",
2945                                 is_mdt ? "mdt": "ost", qctl->qc_idx);
2946                         continue;
2947                 }
2948
2949                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
2950                             qctl->qc_valid, 0, h);
2951                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
2952                                    qctl->qc_dqblk.dqb_bhardlimit;
2953         }
2954 out:
2955         qctl->qc_valid = valid;
2956         return rc ? : rc1;
2957 }
2958
2959 static int lfs_quota(int argc, char **argv)
2960 {
2961         int c;
2962         char *mnt, *name = NULL;
2963         struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2964                                     .qc_type = UGQUOTA };
2965         char *obd_type = (char *)qctl.obd_type;
2966         char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2967         int rc, rc1 = 0, rc2 = 0, rc3 = 0,
2968             verbose = 0, pass = 0, quiet = 0, inacc;
2969         char *endptr;
2970         __u32 valid = QC_GENERAL, idx = 0;
2971         __u64 total_ialloc = 0, total_balloc = 0;
2972         bool human_readable = false;
2973
2974         while ((c = getopt(argc, argv, "gi:I:o:qtuvh")) != -1) {
2975                 switch (c) {
2976                 case 'u':
2977                         if (qctl.qc_type != UGQUOTA) {
2978                                 fprintf(stderr, "error: use either -u or -g\n");
2979                                 return CMD_HELP;
2980                         }
2981                         qctl.qc_type = USRQUOTA;
2982                         break;
2983                 case 'g':
2984                         if (qctl.qc_type != UGQUOTA) {
2985                                 fprintf(stderr, "error: use either -u or -g\n");
2986                                 return CMD_HELP;
2987                         }
2988                         qctl.qc_type = GRPQUOTA;
2989                         break;
2990                 case 't':
2991                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
2992                         break;
2993                 case 'o':
2994                         valid = qctl.qc_valid = QC_UUID;
2995                         strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2996                         break;
2997                 case 'i':
2998                         valid = qctl.qc_valid = QC_MDTIDX;
2999                         idx = qctl.qc_idx = atoi(optarg);