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