Whamcloud - gitweb
LU-4665 utils: lfs setstripe to specify OSTs
[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         "                 [--ost-list|-o <ost_indices>]\n"\
125         "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n"\
126         "\t              Can be specified with k, m or g (in KB, MB and GB\n"\
127         "\t              respectively)\n"\
128         "\tstart_ost_idx: OST index of first stripe (-1 default)\n"\
129         "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"\
130         "\tpool_name:    Name of OST pool to use (default none)\n"\
131         "\tblock:        Block file access during data migration\n"\
132         "\tost_indices:  List of OST indices, can be repeated multiple times\n"\
133         "\t              Indices be specified in a format of:\n"\
134         "\t                -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n"\
135         "\t              Or:\n"\
136         "\t                -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n"\
137         "\t              If --pool is set with --ost-list, then the OSTs\n"\
138         "\t              must be the members of the pool."
139
140 /* all avaialable commands */
141 command_t cmdlist[] = {
142         {"setstripe", lfs_setstripe, 0,
143          "Create a new file with a specific striping pattern or\n"
144          "set the default striping pattern on an existing directory or\n"
145          "delete the default striping pattern from an existing directory\n"
146          "usage: setstripe -d <directory>   (to delete default striping)\n"\
147          " or\n"
148          SETSTRIPE_USAGE("setstripe", "<directory|filename>")},
149         {"getstripe", lfs_getstripe, 0,
150          "To list the striping info for a given file or files in a\n"
151          "directory or recursively for all files in a directory tree.\n"
152          "usage: getstripe [--ost|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
153          "                 [--stripe-count|-c] [--stripe-index|-i]\n"
154          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
155          "                 [--mdt-index|-M] [--recursive|-r] [--raw|-R]\n"
156          "                 [--layout|-L]\n"
157          "                 <directory|filename> ..."},
158         {"setdirstripe", lfs_setdirstripe, 0,
159          "To create a striped directory on a specified MDT. This can only\n"
160          "be done on MDT0 with the right of administrator.\n"
161          "usage: setdirstripe <--count|-c stripe_count>\n"
162          "              [--index|-i mdt_index] [--hash-type|-t hash_type]\n"
163          "              [--default_stripe|-D ] [--mode|-m mode] <dir>\n"
164          "\tstripe_count: stripe count of the striped directory\n"
165          "\tmdt_index:  MDT index of first stripe\n"
166          "\thash_type:  hash type of the striped directory. Hash types:\n"
167          "      fnv_1a_64 FNV-1a hash algorithm (default)\n"
168          "      all_char  sum of characters % MDT_COUNT (not recommended)\n"
169          "\tdefault_stripe: set default dirstripe of the directory\n"
170          "\tmode: the mode of the directory\n"},
171         {"getdirstripe", lfs_getdirstripe, 0,
172          "To list the striping info for a given directory\n"
173          "or recursively for all directories in a directory tree.\n"
174          "usage: getdirstripe [--obd|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
175          "               [--count|-c ] [--index|-i ] [--raw|-R]\n"
176          "               [--recursive | -r] [ --default_stripe | -D ] <dir> "},
177         {"mkdir", lfs_setdirstripe, 0,
178          "To create a striped directory on a specified MDT. This can only\n"
179          "be done on MDT0 with the right of administrator.\n"
180          "usage: mkdir <--count|-c stripe_count>\n"
181          "              [--index|-i mdt_index] [--hash-type|-t hash_type]\n"
182          "              [--default_stripe|-D ] [--mode|-m mode] <dir>\n"
183          "\tstripe_count: stripe count of the striped directory\n"
184          "\tmdt_index:  MDT index of first stripe\n"
185          "\thash_type:  hash type of the striped directory. Hash types:\n"
186          "      fnv_1a_64 FNV-1a hash algorithm (default)\n"
187          "      all_char  sum of characters % MDT_COUNT (not recommended)\n"
188          "\tdefault_stripe: set default dirstripe of the directory\n"
189          "\tmode: the mode of the directory\n"},
190         {"rm_entry", lfs_rmentry, 0,
191          "To remove the name entry of the remote directory. Note: This\n"
192          "command will only delete the name entry, i.e. the remote directory\n"
193          "will become inaccessable after this command. This can only be done\n"
194          "by the administrator\n"
195          "usage: rm_entry <dir>\n"},
196         {"pool_list", lfs_poollist, 0,
197          "List pools or pool OSTs\n"
198          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
199         {"find", lfs_find, 0,
200          "find files matching given attributes recursively in directory tree.\n"
201          "usage: find <directory|filename> ...\n"
202          "     [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
203          "     [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
204          "     [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
205          "     [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
206          "     [[!] --size|-s [+-]N[bkMGTPE]]\n"
207          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
208          "     [[!] --stripe-index|-i <index,...>]\n"
209          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
210          "     [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
211          "     [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
212          "     [[!] --layout|-L released,raid0]\n"
213          "\t !: used before an option indicates 'NOT' requested attribute\n"
214          "\t -: used before a value indicates 'AT MOST' requested value\n"
215          "\t +: used before a value indicates 'AT LEAST' requested value\n"},
216         {"check", lfs_check, 0,
217          "Display the status of MDS or OSTs (as specified in the command)\n"
218          "or all the servers (MDS and OSTs).\n"
219          "usage: check <osts|mds|servers>"},
220         {"join", lfs_join, 0,
221          "join two lustre files into one.\n"
222          "obsolete, HEAD does not support it anymore.\n"},
223         {"osts", lfs_osts, 0, "list OSTs connected to client "
224          "[for specified path only]\n" "usage: osts [path]"},
225         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
226          "[for specified path only]\n" "usage: mdts [path]"},
227         {"df", lfs_df, 0,
228          "report filesystem disk space usage or inodes usage"
229          "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
230          "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
231         {"getname", lfs_getname, 0, "list instances and specified mount points "
232          "[for specified path only]\n"
233          "Usage: getname [-h]|[path ...] "},
234 #ifdef HAVE_SYS_QUOTA_H
235         {"quotacheck", lfs_quotacheck, 0,
236          "Scan the specified filesystem for disk usage, and create,\n"
237          "or update quota files. Deprecated as of 2.4.0.\n"
238          "usage: quotacheck [ -ug ] <filesystem>"},
239         {"quotaon", lfs_quotaon, 0, "Turn filesystem"
240          " quotas on. Deprecated as of 2.4.0.\n"
241          "usage: quotaon [ -ugf ] <filesystem>"},
242         {"quotaoff", lfs_quotaoff, 0, "Turn filesystem"
243          " quotas off. Deprecated as of 2.4.0.\n"
244          "usage: quotaoff [ -ug ] <filesystem>"},
245         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
246          "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
247          "                -b <block-softlimit> -B <block-hardlimit>\n"
248          "                -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
249          "       setquota <-u|--user|-g|--group> <uname>|<uid>|<gname>|<gid>\n"
250          "                [--block-softlimit <block-softlimit>]\n"
251          "                [--block-hardlimit <block-hardlimit>]\n"
252          "                [--inode-softlimit <inode-softlimit>]\n"
253          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
254          "       setquota [-t] <-u|--user|-g|--group>\n"
255          "                [--block-grace <block-grace>]\n"
256          "                [--inode-grace <inode-grace>] <filesystem>\n"
257          "       -b can be used instead of --block-softlimit/--block-grace\n"
258          "       -B can be used instead of --block-hardlimit\n"
259          "       -i can be used instead of --inode-softlimit/--inode-grace\n"
260          "       -I can be used instead of --inode-hardlimit\n\n"
261          "Note: The total quota space will be split into many qunits and\n"
262          "      balanced over all server targets, the minimal qunit size is\n"
263          "      1M bytes for block space and 1K inodes for inode space.\n\n"
264          "      Quota space rebalancing process will stop when this mininum\n"
265          "      value is reached. As a result, quota exceeded can be returned\n"
266          "      while many targets still have 1MB or 1K inodes of spare\n"
267          "      quota space."},
268         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
269          "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
270                        "<ost_idx>]\n"
271          "             [<-u|-g> <uname>|<uid>|<gname>|<gid>] <filesystem>\n"
272          "       quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g> <filesystem>"},
273 #endif
274         {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
275          "usage: flushctx [-k] [mountpoint...]"},
276         {"lsetfacl", lfs_lsetfacl, 0,
277          "Remote user setfacl for user/group on the same remote client.\n"
278          "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
279         {"lgetfacl", lfs_lgetfacl, 0,
280          "Remote user getfacl for user/group on the same remote client.\n"
281          "usage: lgetfacl [-dRLPvh] file ..."},
282         {"rsetfacl", lfs_rsetfacl, 0,
283          "Remote user setfacl for user/group on other clients.\n"
284          "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
285         {"rgetfacl", lfs_rgetfacl, 0,
286          "Remote user getfacl for user/group on other clients.\n"
287          "usage: rgetfacl [-dRLPvh] file ..."},
288         {"cp", lfs_cp, 0,
289          "Remote user copy files and directories.\n"
290          "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
291         {"ls", lfs_ls, 0,
292          "Remote user list directory contents.\n"
293          "usage: ls [OPTION]... [FILE]..."},
294         {"changelog", lfs_changelog, 0,
295          "Show the metadata changes on an MDT."
296          "\nusage: changelog <mdtname> [startrec [endrec]]"},
297         {"changelog_clear", lfs_changelog_clear, 0,
298          "Indicate that old changelog records up to <endrec> are no longer of "
299          "interest to consumer <id>, allowing the system to free up space.\n"
300          "An <endrec> of 0 means all records.\n"
301          "usage: changelog_clear <mdtname> <id> <endrec>"},
302         {"fid2path", lfs_fid2path, 0,
303          "Resolve the full path(s) for given FID(s). For a specific hardlink "
304          "specify link number <linkno>.\n"
305         /* "For a historical link name, specify changelog record <recno>.\n" */
306          "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
307                 /* [ --rec <recno> ] */ },
308         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
309          "usage: path2fid [--parents] <path> ..."},
310         {"data_version", lfs_data_version, 0, "Display file data version for "
311          "a given path.\n" "usage: data_version -[n|r|w] <path>"},
312         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
313          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
314         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
315          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
316          "[--archived] [--lost] <file> ..."},
317         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
318          "files.\n"
319          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
320          "[--archived] [--lost] <file> ..."},
321         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
322          "given files.\n" "usage: hsm_action <file> ..."},
323         {"hsm_archive", lfs_hsm_archive, 0,
324          "Archive file to external storage.\n"
325          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
326          "<file> ..."},
327         {"hsm_restore", lfs_hsm_restore, 0,
328          "Restore file from external storage.\n"
329          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
330         {"hsm_release", lfs_hsm_release, 0,
331          "Release files from Lustre.\n"
332          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
333         {"hsm_remove", lfs_hsm_remove, 0,
334          "Remove file copy from external storage.\n"
335          "usage: hsm_remove [--filelist FILELIST] [--data DATA] <file> ..."},
336         {"hsm_cancel", lfs_hsm_cancel, 0,
337          "Cancel requests related to specified files.\n"
338          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
339         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
340          "usage: swap_layouts <path1> <path2>"},
341         {"migrate", lfs_setstripe, 0, "migrate file from one OST layout to "
342          "another (may be not safe with concurent writes).\n"
343          SETSTRIPE_USAGE("migrate  ", "<filename>")},
344         {"mv", lfs_mv, 0,
345          "To move directories between MDTs.\n"
346          "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
347          "[--verbose|-v]\n"},
348         {"help", Parser_help, 0, "help"},
349         {"exit", Parser_quit, 0, "quit"},
350         {"quit", Parser_quit, 0, "quit"},
351         {"--version", Parser_version, 0,
352          "output build version of the utility and exit"},
353         { 0, 0, 0, NULL }
354 };
355
356 /* Generate a random id for the grouplock */
357 static int random_group_id(int *gid)
358 {
359         int     fd;
360         int     rc;
361         size_t  sz = sizeof(*gid);
362
363         fd = open("/dev/urandom", O_RDONLY);
364         if (fd < 0) {
365                 rc = -errno;
366                 fprintf(stderr, "cannot open /dev/urandom: %s\n",
367                         strerror(-rc));
368                 goto out;
369         }
370
371         rc = read(fd, gid, sz);
372         if (rc < sz) {
373                 rc = -errno;
374                 fprintf(stderr, "cannot read %zu bytes from /dev/urandom: %s\n",
375                         sz, strerror(-rc));
376                 goto out;
377         }
378
379 out:
380         if (fd >= 0)
381                 close(fd);
382
383         return rc;
384 }
385
386 #define MIGRATION_BLOCKS 1
387
388 static int lfs_migrate(char *name, __u64 migration_flags,
389                        struct llapi_stripe_param *param)
390 {
391         int                      fd, fdv;
392         char                     volatile_file[PATH_MAX +
393                                                 LUSTRE_VOLATILE_HDR_LEN + 4];
394         char                     parent[PATH_MAX];
395         char                    *ptr;
396         int                      rc;
397         __u64                    dv1;
398         struct lov_user_md      *lum = NULL;
399         int                      lumsz;
400         int                      bufsz;
401         void                    *buf = NULL;
402         int                      rsize, wsize;
403         __u64                    rpos, wpos, bufoff;
404         int                      gid;
405         int                      have_gl = 0;
406         struct stat              st, stv;
407
408         /* find the right size for the IO and allocate the buffer */
409         lumsz = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
410         lum = malloc(lumsz);
411         if (lum == NULL) {
412                 rc = -ENOMEM;
413                 goto free;
414         }
415
416         rc = llapi_file_get_stripe(name, lum);
417         /* failure can come from may case and some may be not real error
418          * (eg: no stripe)
419          * in case of a real error, a later call will failed with a better
420          * error management */
421         if (rc < 0)
422                 bufsz = 1024*1024;
423         else
424                 bufsz = lum->lmm_stripe_size;
425         rc = posix_memalign(&buf, getpagesize(), bufsz);
426         if (rc != 0) {
427                 rc = -rc;
428                 goto free;
429         }
430
431         if (migration_flags & MIGRATION_BLOCKS) {
432                 rc = random_group_id(&gid);
433                 if (rc < 0) {
434                         fprintf(stderr, "%s: cannot get random group ID: %s\n",
435                                 name, strerror(-rc));
436                         goto free;
437                 }
438         }
439
440         /* search for file directory pathname */
441         if (strlen(name) > sizeof(parent)-1) {
442                 rc = -E2BIG;
443                 goto free;
444         }
445         strncpy(parent, name, sizeof(parent));
446         ptr = strrchr(parent, '/');
447         if (ptr == NULL) {
448                 if (getcwd(parent, sizeof(parent)) == NULL) {
449                         rc = -errno;
450                         goto free;
451                 }
452         } else {
453                 if (ptr == parent)
454                         strcpy(parent, "/");
455                 else
456                         *ptr = '\0';
457         }
458         rc = snprintf(volatile_file, sizeof(volatile_file), "%s/%s::", parent,
459                       LUSTRE_VOLATILE_HDR);
460         if (rc >= sizeof(volatile_file)) {
461                 rc = -E2BIG;
462                 goto free;
463         }
464
465         /* create, open a volatile file, use caching (ie no directio) */
466         /* exclusive create is not needed because volatile files cannot
467          * conflict on name by construction */
468         fdv = llapi_file_open_param(volatile_file, O_CREAT | O_WRONLY, 0644,
469                                     param);
470         if (fdv < 0) {
471                 rc = fdv;
472                 fprintf(stderr, "cannot create volatile file in %s (%s)\n",
473                         parent, strerror(-rc));
474                 goto free;
475         }
476
477         /* open file, direct io */
478         /* even if the file is only read, WR mode is nedeed to allow
479          * layout swap on fd */
480         fd = open(name, O_RDWR | O_DIRECT);
481         if (fd == -1) {
482                 rc = -errno;
483                 fprintf(stderr, "cannot open %s (%s)\n", name, strerror(-rc));
484                 close(fdv);
485                 goto free;
486         }
487
488         /* Not-owner (root?) special case.
489          * Need to set owner/group of volatile file like original.
490          * This will allow to pass related check during layout_swap.
491          */
492         rc = fstat(fd, &st);
493         if (rc != 0) {
494                 rc = -errno;
495                 fprintf(stderr, "cannot stat %s (%s)\n", name,
496                         strerror(errno));
497                 goto error;
498         }
499         rc = fstat(fdv, &stv);
500         if (rc != 0) {
501                 rc = -errno;
502                 fprintf(stderr, "cannot stat %s (%s)\n", volatile_file,
503                         strerror(errno));
504                 goto error;
505         }
506         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
507                 rc = fchown(fdv, st.st_uid, st.st_gid);
508                 if (rc != 0) {
509                         rc = -errno;
510                         fprintf(stderr, "cannot chown %s (%s)\n", name,
511                                 strerror(errno));
512                         goto error;
513                 }
514         }
515
516         /* get file data version */
517         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
518         if (rc != 0) {
519                 fprintf(stderr, "cannot get dataversion on %s (%s)\n",
520                         name, strerror(-rc));
521                 goto error;
522         }
523
524         if (migration_flags & MIGRATION_BLOCKS) {
525                 /* take group lock to limit concurent access
526                  * this will be no more needed when exclusive access will
527                  * be implemented (see LU-2919) */
528                 /* group lock is taken after data version read because it
529                  * blocks data version call */
530                 rc = llapi_group_lock(fd, gid);
531                 if (rc < 0) {
532                         fprintf(stderr, "cannot get group lock on %s (%s)\n",
533                                 name, strerror(-rc));
534                         goto error;
535                 }
536                 have_gl = 1;
537         }
538
539         /* copy data */
540         rpos = 0;
541         wpos = 0;
542         bufoff = 0;
543         rsize = -1;
544         do {
545                 /* read new data only if we have written all
546                  * previously read data */
547                 if (wpos == rpos) {
548                         rsize = read(fd, buf, bufsz);
549                         if (rsize < 0) {
550                                 rc = -errno;
551                                 fprintf(stderr, "read failed on %s"
552                                         " (%s)\n", name,
553                                         strerror(-rc));
554                                 goto error;
555                         }
556                         rpos += rsize;
557                         bufoff = 0;
558                 }
559                 /* eof ? */
560                 if (rsize == 0)
561                         break;
562                 wsize = write(fdv, buf + bufoff, rpos - wpos);
563                 if (wsize < 0) {
564                         rc = -errno;
565                         fprintf(stderr, "write failed on volatile"
566                                 " for %s (%s)\n", name, strerror(-rc));
567                         goto error;
568                 }
569                 wpos += wsize;
570                 bufoff += wsize;
571         } while (1);
572
573         /* flush data */
574         fsync(fdv);
575
576         if (migration_flags & MIGRATION_BLOCKS) {
577                 /* give back group lock */
578                 rc = llapi_group_unlock(fd, gid);
579                 if (rc < 0)
580                         fprintf(stderr, "cannot put group lock on %s (%s)\n",
581                                 name, strerror(-rc));
582                 have_gl = 0;
583         }
584
585         /* swap layouts
586          * for a migration we need to:
587          * - check data version on file did not change
588          * - keep file mtime
589          * - keep file atime
590          */
591         rc = llapi_fswap_layouts(fd, fdv, dv1, 0,
592                                  SWAP_LAYOUTS_CHECK_DV1 |
593                                  SWAP_LAYOUTS_KEEP_MTIME |
594                                  SWAP_LAYOUTS_KEEP_ATIME);
595         if (rc == -EAGAIN) {
596                 fprintf(stderr, "%s: dataversion changed during copy, "
597                         "migration aborted\n", name);
598                 goto error;
599         }
600         if (rc != 0)
601                 fprintf(stderr, "%s: swap layout to new file failed: %s\n",
602                         name, strerror(-rc));
603
604 error:
605         /* give back group lock */
606         if ((migration_flags & MIGRATION_BLOCKS) && have_gl) {
607                 int rc2;
608
609                 /* we keep the original error in rc */
610                 rc2 = llapi_group_unlock(fd, gid);
611                 if (rc2 < 0)
612                         fprintf(stderr, "cannot put group lock on %s (%s)\n",
613                                 name, strerror(-rc2));
614         }
615
616         close(fdv);
617         close(fd);
618 free:
619         if (lum)
620                 free(lum);
621         if (buf)
622                 free(buf);
623         return rc;
624 }
625
626 /**
627  * Parse a string containing an OST index list into an array of integers.
628  *
629  * The input string contains a comma delimited list of individual
630  * indices and ranges, for example "1,2-4,7". Add the indices into the
631  * \a osts array and remove duplicates.
632  *
633  * \param[out] osts    array to store indices in
634  * \param[in] size     size of \a osts array
635  * \param[in] offset   starting index in \a osts
636  * \param[in] arg      string containing OST index list
637  *
638  * \retval positive    number of indices in \a osts
639  * \retval -EINVAL     unable to parse \a arg
640  */
641 static int parse_targets(__u32 *osts, int size, int offset, char *arg)
642 {
643         int rc;
644         int nr = offset;
645         int slots = size - offset;
646         char *ptr = NULL;
647         bool end_of_loop;
648
649         if (arg == NULL)
650                 return -EINVAL;
651
652         end_of_loop = false;
653         while (!end_of_loop) {
654                 int start_index;
655                 int end_index;
656                 int i;
657                 char *endptr = NULL;
658
659                 rc = -EINVAL;
660
661                 ptr = strchrnul(arg, ',');
662
663                 end_of_loop = *ptr == '\0';
664                 *ptr = '\0';
665
666                 start_index = strtol(arg, &endptr, 0);
667                 if (endptr == arg) /* no data at all */
668                         break;
669                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
670                         break;
671                 if (start_index < 0)
672                         break;
673
674                 end_index = start_index;
675                 if (*endptr == '-') {
676                         end_index = strtol(endptr + 1, &endptr, 0);
677                         if (*endptr != '\0')
678                                 break;
679                         if (end_index < start_index)
680                                 break;
681                 }
682
683                 for (i = start_index; i <= end_index && slots > 0; i++) {
684                         int j;
685
686                         /* remove duplicate */
687                         for (j = 0; j < offset; j++) {
688                                 if (osts[j] == i)
689                                         break;
690                         }
691                         if (j == offset) { /* no duplicate */
692                                 osts[nr++] = i;
693                                 --slots;
694                         }
695                 }
696                 if (slots == 0 && i < end_index)
697                         break;
698
699                 *ptr = ',';
700                 arg = ++ptr;
701                 offset = nr;
702                 rc = 0;
703         }
704         if (!end_of_loop && ptr != NULL)
705                 *ptr = ',';
706
707         return rc < 0 ? rc : nr;
708 }
709
710 /* functions */
711 static int lfs_setstripe(int argc, char **argv)
712 {
713         struct llapi_stripe_param       *param;
714         char                            *fname;
715         int                              result;
716         unsigned long long               st_size;
717         int                              st_offset, st_count;
718         char                            *end;
719         int                              c;
720         int                              delete = 0;
721         char                            *stripe_size_arg = NULL;
722         char                            *stripe_off_arg = NULL;
723         char                            *stripe_count_arg = NULL;
724         char                            *pool_name_arg = NULL;
725         unsigned long long               size_units = 1;
726         bool                             migrate_mode = false;
727         __u64                            migration_flags = 0;
728         __u32                            osts[LOV_MAX_STRIPE_COUNT] = { 0 };
729         int                              nr_osts = 0;
730
731         struct option            long_opts[] = {
732                 /* valid only in migrate mode */
733                 {"block",        no_argument,       0, 'b'},
734 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
735                 /* This formerly implied "stripe-count", but was explicitly
736                  * made "stripe-count" for consistency with other options,
737                  * and to separate it from "mdt-count" when DNE arrives. */
738                 {"count",        required_argument, 0, 'c'},
739 #endif
740                 {"stripe-count", required_argument, 0, 'c'},
741                 {"stripe_count", required_argument, 0, 'c'},
742                 {"delete",       no_argument,       0, 'd'},
743 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
744                 /* This formerly implied "stripe-index", but was explicitly
745                  * made "stripe-index" for consistency with other options,
746                  * and to separate it from "mdt-index" when DNE arrives. */
747                 {"index",        required_argument, 0, 'i'},
748 #endif
749                 {"stripe-index", required_argument, 0, 'i'},
750                 {"stripe_index", required_argument, 0, 'i'},
751                 {"ost-list",     required_argument, 0, 'o'},
752                 {"ost_list",     required_argument, 0, 'o'},
753                 {"pool",         required_argument, 0, 'p'},
754 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
755                 /* This formerly implied "--stripe-size", but was confusing
756                  * with "lfs find --size|-s", which means "file size", so use
757                  * the consistent "--stripe-size|-S" for all commands. */
758                 {"size",         required_argument, 0, 's'},
759 #endif
760                 {"stripe-size",  required_argument, 0, 'S'},
761                 {"stripe_size",  required_argument, 0, 'S'},
762                 {0, 0, 0, 0}
763         };
764
765         st_size = 0;
766         st_offset = -1;
767         st_count = 0;
768
769         if (strcmp(argv[0], "migrate") == 0)
770                 migrate_mode = true;
771
772         optind = 0;
773         while ((c = getopt_long(argc, argv, "c:di:o:p:s:S:",
774                                 long_opts, NULL)) >= 0) {
775                 switch (c) {
776                 case 0:
777                         /* Long options. */
778                         break;
779                 case 'b':
780                         if (!migrate_mode) {
781                                 fprintf(stderr, "--block is valid only for"
782                                                 " migrate mode");
783                                 return CMD_HELP;
784                         }
785                         migration_flags |= MIGRATION_BLOCKS;
786                         break;
787                 case 'c':
788 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
789                         if (strcmp(argv[optind - 1], "--count") == 0)
790                                 fprintf(stderr, "warning: '--count' deprecated"
791                                         ", use '--stripe-count' instead\n");
792 #endif
793                         stripe_count_arg = optarg;
794                         break;
795                 case 'd':
796                         /* delete the default striping pattern */
797                         delete = 1;
798                         break;
799                 case 'o':
800                         nr_osts = parse_targets(osts, ARRAY_SIZE(osts), nr_osts,
801                                                 optarg);
802                         if (nr_osts < 0) {
803                                 fprintf(stderr,
804                                         "error: %s: bad OST indices '%s'\n",
805                                         argv[0], optarg);
806                                 return CMD_HELP;
807                         }
808
809                         if (st_offset == -1) /* first in the command line */
810                                 st_offset = osts[0];
811                         break;
812                 case 'i':
813 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
814                         if (strcmp(argv[optind - 1], "--index") == 0)
815                                 fprintf(stderr, "warning: '--index' deprecated"
816                                         ", use '--stripe-index' instead\n");
817 #endif
818                         stripe_off_arg = optarg;
819                         break;
820 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
821                 case 's':
822 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
823                         fprintf(stderr, "warning: '--size|-s' deprecated, "
824                                 "use '--stripe-size|-S' instead\n");
825 #endif
826 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
827                 case 'S':
828                         stripe_size_arg = optarg;
829                         break;
830                 case 'p':
831                         pool_name_arg = optarg;
832                         break;
833                 default:
834                         return CMD_HELP;
835                 }
836         }
837
838         fname = argv[optind];
839
840         if (delete &&
841             (stripe_size_arg != NULL || stripe_off_arg != NULL ||
842              stripe_count_arg != NULL || pool_name_arg != NULL)) {
843                 fprintf(stderr, "error: %s: cannot specify -d with "
844                         "-s, -c, -o, or -p options\n",
845                         argv[0]);
846                 return CMD_HELP;
847         }
848
849         if (optind == argc) {
850                 fprintf(stderr, "error: %s: missing filename|dirname\n",
851                         argv[0]);
852                 return CMD_HELP;
853         }
854
855         /* get the stripe size */
856         if (stripe_size_arg != NULL) {
857                 result = llapi_parse_size(stripe_size_arg, &st_size,
858                                           &size_units, 0);
859                 if (result) {
860                         fprintf(stderr, "error: %s: bad stripe size '%s'\n",
861                                 argv[0], stripe_size_arg);
862                         return result;
863                 }
864         }
865         /* get the stripe offset */
866         if (stripe_off_arg != NULL) {
867                 st_offset = strtol(stripe_off_arg, &end, 0);
868                 if (*end != '\0') {
869                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
870                                 argv[0], stripe_off_arg);
871                         return CMD_HELP;
872                 }
873         }
874         /* get the stripe count */
875         if (stripe_count_arg != NULL) {
876                 st_count = strtoul(stripe_count_arg, &end, 0);
877                 if (*end != '\0') {
878                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
879                                 argv[0], stripe_count_arg);
880                         return CMD_HELP;
881                 }
882         }
883
884         /* initialize stripe parameters */
885         param = calloc(1, offsetof(typeof(*param), lsp_osts[nr_osts]));
886         if (param == NULL) {
887                 fprintf(stderr, "error: %s: run out of memory\n", argv[0]);
888                 return CMD_HELP;
889         }
890
891         param->lsp_stripe_size = st_size;
892         param->lsp_stripe_offset = st_offset;
893         param->lsp_stripe_count = st_count;
894         param->lsp_stripe_pattern = 0;
895         param->lsp_pool = pool_name_arg;
896         param->lsp_is_specific = false;
897         if (nr_osts > 0) {
898                 if (st_count > 0 && nr_osts != st_count) {
899                         fprintf(stderr, "error: %s: stripe count '%d' doesn't "
900                                 "match the number of OSTs: %d\n",
901                                 argv[0], st_count, nr_osts);
902                         return CMD_HELP;
903                 }
904
905                 param->lsp_is_specific = true;
906                 param->lsp_stripe_count = nr_osts;
907                 memcpy(param->lsp_osts, osts, sizeof(*osts) * nr_osts);
908         }
909
910         do {
911                 if (!migrate_mode) {
912                         result = llapi_file_open_param(fname,
913                                                        O_CREAT | O_WRONLY,
914                                                        0644, param);
915                         if (result >= 0) {
916                                 close(result);
917                                 result = 0;
918                         }
919                 } else {
920                         result = lfs_migrate(fname, migration_flags, param);
921                 }
922                 if (result) {
923                         fprintf(stderr,
924                                 "error: %s: %s stripe file '%s' failed\n",
925                                 argv[0], migrate_mode ? "migrate" : "create",
926                                 fname);
927                         break;
928                 }
929                 fname = argv[++optind];
930         } while (fname != NULL);
931
932         free(param);
933         return result;
934 }
935
936 static int lfs_poollist(int argc, char **argv)
937 {
938         if (argc != 2)
939                 return CMD_HELP;
940
941         return llapi_poollist(argv[1]);
942 }
943
944 static int set_time(time_t *time, time_t *set, char *str)
945 {
946         time_t t;
947         int res = 0;
948
949         if (str[0] == '+')
950                 res = 1;
951         else if (str[0] == '-')
952                 res = -1;
953
954         if (res)
955                 str++;
956
957         t = strtol(str, NULL, 0);
958         if (*time < t * 24 * 60 * 60) {
959                 if (res)
960                         str--;
961                 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
962                 return INT_MAX;
963         }
964
965         *set = *time - t * 24 * 60 * 60;
966         return res;
967 }
968
969 #define USER 0
970 #define GROUP 1
971
972 static int name2id(unsigned int *id, char *name, int type)
973 {
974         if (type == USER) {
975                 struct passwd *entry;
976
977                 if (!(entry = getpwnam(name))) {
978                         if (!errno)
979                                 errno = ENOENT;
980                         return -1;
981                 }
982
983                 *id = entry->pw_uid;
984         } else {
985                 struct group *entry;
986
987                 if (!(entry = getgrnam(name))) {
988                         if (!errno)
989                                 errno = ENOENT;
990                         return -1;
991                 }
992
993                 *id = entry->gr_gid;
994         }
995
996         return 0;
997 }
998
999 static int id2name(char **name, unsigned int id, int type)
1000 {
1001         if (type == USER) {
1002                 struct passwd *entry;
1003
1004                 if (!(entry = getpwuid(id))) {
1005                         if (!errno)
1006                                 errno = ENOENT;
1007                         return -1;
1008                 }
1009
1010                 *name = entry->pw_name;
1011         } else {
1012                 struct group *entry;
1013
1014                 if (!(entry = getgrgid(id))) {
1015                         if (!errno)
1016                                 errno = ENOENT;
1017                         return -1;
1018                 }
1019
1020                 *name = entry->gr_name;
1021         }
1022
1023         return 0;
1024 }
1025
1026 static int name2layout(__u32 *layout, char *name)
1027 {
1028         char *ptr, *lyt;
1029
1030         *layout = 0;
1031         for (ptr = name; ; ptr = NULL) {
1032                 lyt = strtok(ptr, ",");
1033                 if (lyt == NULL)
1034                         break;
1035                 if (strcmp(lyt, "released") == 0)
1036                         *layout |= LOV_PATTERN_F_RELEASED;
1037                 else if (strcmp(lyt, "raid0") == 0)
1038                         *layout |= LOV_PATTERN_RAID0;
1039                 else
1040                         return -1;
1041         }
1042         return 0;
1043 }
1044
1045 #define FIND_POOL_OPT 3
1046 static int lfs_find(int argc, char **argv)
1047 {
1048         int c, rc;
1049         int ret = 0;
1050         time_t t;
1051         struct find_param param = {
1052                 .fp_max_depth = -1,
1053                 .quiet = 1,
1054         };
1055         struct option long_opts[] = {
1056                 {"atime",        required_argument, 0, 'A'},
1057                 {"stripe-count", required_argument, 0, 'c'},
1058                 {"stripe_count", required_argument, 0, 'c'},
1059                 {"ctime",        required_argument, 0, 'C'},
1060                 {"maxdepth",     required_argument, 0, 'D'},
1061                 {"gid",          required_argument, 0, 'g'},
1062                 {"group",        required_argument, 0, 'G'},
1063                 {"stripe-index", required_argument, 0, 'i'},
1064                 {"stripe_index", required_argument, 0, 'i'},
1065                 {"layout",       required_argument, 0, 'L'},
1066                 {"mdt",          required_argument, 0, 'm'},
1067                 {"mtime",        required_argument, 0, 'M'},
1068                 {"name",         required_argument, 0, 'n'},
1069      /* reserve {"or",           no_argument,     , 0, 'o'}, to match find(1) */
1070                 {"obd",          required_argument, 0, 'O'},
1071                 {"ost",          required_argument, 0, 'O'},
1072                 /* no short option for pool, p/P already used */
1073                 {"pool",         required_argument, 0, FIND_POOL_OPT},
1074                 {"print0",       no_argument,       0, 'p'},
1075                 {"print",        no_argument,       0, 'P'},
1076                 {"size",         required_argument, 0, 's'},
1077                 {"stripe-size",  required_argument, 0, 'S'},
1078                 {"stripe_size",  required_argument, 0, 'S'},
1079                 {"type",         required_argument, 0, 't'},
1080                 {"uid",          required_argument, 0, 'u'},
1081                 {"user",         required_argument, 0, 'U'},
1082                 {0, 0, 0, 0}
1083         };
1084         int pathstart = -1;
1085         int pathend = -1;
1086         int neg_opt = 0;
1087         time_t *xtime;
1088         int *xsign;
1089         int isoption;
1090         char *endptr;
1091
1092         time(&t);
1093
1094         optind = 0;
1095         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
1096         while ((c = getopt_long_only(argc, argv,
1097                                      "-A:c:C:D:g:G:i:L:m:M:n:O:Ppqrs:S:t:u:U:v",
1098                                      long_opts, NULL)) >= 0) {
1099                 xtime = NULL;
1100                 xsign = NULL;
1101                 if (neg_opt)
1102                         --neg_opt;
1103                 /* '!' is part of option */
1104                 /* when getopt_long_only() finds a string which is not
1105                  * an option nor a known option argument it returns 1
1106                  * in that case if we already have found pathstart and pathend
1107                  * (i.e. we have the list of pathnames),
1108                  * the only supported value is "!"
1109                  */
1110                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
1111                 if (!isoption && pathend != -1) {
1112                         fprintf(stderr, "err: %s: filename|dirname must either "
1113                                         "precede options or follow options\n",
1114                                         argv[0]);
1115                         ret = CMD_HELP;
1116                         goto err;
1117                 }
1118                 if (!isoption && pathstart == -1)
1119                         pathstart = optind - 1;
1120                 if (isoption && pathstart != -1 && pathend == -1)
1121                         pathend = optind - 2;
1122                 switch (c) {
1123                 case 0:
1124                         /* Long options. */
1125                         break;
1126                 case 1:
1127                         /* unknown; opt is "!" or path component,
1128                          * checking done above.
1129                          */
1130                         if (strcmp(optarg, "!") == 0)
1131                                 neg_opt = 2;
1132                         break;
1133                 case 'A':
1134                         xtime = &param.fp_atime;
1135                         xsign = &param.fp_asign;
1136                         param.fp_exclude_atime = !!neg_opt;
1137                         /* no break, this falls through to 'C' for ctime */
1138                 case 'C':
1139                         if (c == 'C') {
1140                                 xtime = &param.fp_ctime;
1141                                 xsign = &param.fp_csign;
1142                                 param.fp_exclude_ctime = !!neg_opt;
1143                         }
1144                         /* no break, this falls through to 'M' for mtime */
1145                 case 'M':
1146                         if (c == 'M') {
1147                                 xtime = &param.fp_mtime;
1148                                 xsign = &param.fp_msign;
1149                                 param.fp_exclude_mtime = !!neg_opt;
1150                         }
1151                         rc = set_time(&t, xtime, optarg);
1152                         if (rc == INT_MAX) {
1153                                 ret = -1;
1154                                 goto err;
1155                         }
1156                         if (rc)
1157                                 *xsign = rc;
1158                         break;
1159                 case 'c':
1160                         if (optarg[0] == '+') {
1161                                 param.stripecount_sign = -1;
1162                                 optarg++;
1163                         } else if (optarg[0] == '-') {
1164                                 param.stripecount_sign =  1;
1165                                 optarg++;
1166                         }
1167
1168                         param.stripecount = strtoul(optarg, &endptr, 0);
1169                         if (*endptr != '\0') {
1170                                 fprintf(stderr,"error: bad stripe_count '%s'\n",
1171                                         optarg);
1172                                 ret = -1;
1173                                 goto err;
1174                         }
1175                         param.check_stripecount = 1;
1176                         param.exclude_stripecount = !!neg_opt;
1177                         break;
1178                 case 'D':
1179                         param.fp_max_depth = strtol(optarg, 0, 0);
1180                         break;
1181                 case 'g':
1182                 case 'G':
1183                         rc = name2id(&param.fp_gid, optarg, GROUP);
1184                         if (rc) {
1185                                 param.fp_gid = strtoul(optarg, &endptr, 10);
1186                                 if (*endptr != '\0') {
1187                                         fprintf(stderr, "Group/GID: %s cannot "
1188                                                 "be found.\n", optarg);
1189                                         ret = -1;
1190                                         goto err;
1191                                 }
1192                         }
1193                         param.fp_exclude_gid = !!neg_opt;
1194                         param.fp_check_gid = 1;
1195                         break;
1196                 case 'L':
1197                         ret = name2layout(&param.layout, optarg);
1198                         if (ret)
1199                                 goto err;
1200                         param.exclude_layout = !!neg_opt;
1201                         param.check_layout = 1;
1202                         break;
1203                 case 'u':
1204                 case 'U':
1205                         rc = name2id(&param.fp_uid, optarg, USER);
1206                         if (rc) {
1207                                 param.fp_uid = strtoul(optarg, &endptr, 10);
1208                                 if (*endptr != '\0') {
1209                                         fprintf(stderr, "User/UID: %s cannot "
1210                                                 "be found.\n", optarg);
1211                                         ret = -1;
1212                                         goto err;
1213                                 }
1214                         }
1215                         param.fp_exclude_uid = !!neg_opt;
1216                         param.fp_check_uid = 1;
1217                         break;
1218                 case FIND_POOL_OPT:
1219                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
1220                                 fprintf(stderr,
1221                                         "Pool name %s is too long"
1222                                         " (max is %d)\n", optarg,
1223                                         LOV_MAXPOOLNAME);
1224                                 ret = -1;
1225                                 goto err;
1226                         }
1227                         /* we do check for empty pool because empty pool
1228                          * is used to find V1 lov attributes */
1229                         strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
1230                         param.poolname[LOV_MAXPOOLNAME] = '\0';
1231                         param.exclude_pool = !!neg_opt;
1232                         param.check_pool = 1;
1233                         break;
1234                 case 'n':
1235                         param.pattern = (char *)optarg;
1236                         param.exclude_pattern = !!neg_opt;
1237                         break;
1238                 case 'm':
1239                 case 'i':
1240                 case 'O': {
1241                         char *buf, *token, *next, *p;
1242                         int len = 1;
1243                         void *tmp;
1244
1245                         buf = strdup(optarg);
1246                         if (buf == NULL) {
1247                                 ret = -ENOMEM;
1248                                 goto err;
1249                         }
1250
1251                         param.exclude_obd = !!neg_opt;
1252
1253                         token = buf;
1254                         while (token && *token) {
1255                                 token = strchr(token, ',');
1256                                 if (token) {
1257                                         len++;
1258                                         token++;
1259                                 }
1260                         }
1261                         if (c == 'm') {
1262                                 param.exclude_mdt = !!neg_opt;
1263                                 param.num_alloc_mdts += len;
1264                                 tmp = realloc(param.mdtuuid,
1265                                               param.num_alloc_mdts *
1266                                               sizeof(*param.mdtuuid));
1267                                 if (tmp == NULL) {
1268                                         ret = -ENOMEM;
1269                                         goto err_free;
1270                                 }
1271
1272                                 param.mdtuuid = tmp;
1273                         } else {
1274                                 param.exclude_obd = !!neg_opt;
1275                                 param.num_alloc_obds += len;
1276                                 tmp = realloc(param.obduuid,
1277                                               param.num_alloc_obds *
1278                                               sizeof(*param.obduuid));
1279                                 if (tmp == NULL) {
1280                                         ret = -ENOMEM;
1281                                         goto err_free;
1282                                 }
1283
1284                                 param.obduuid = tmp;
1285                         }
1286                         for (token = buf; token && *token; token = next) {
1287                                 struct obd_uuid *puuid;
1288                                 if (c == 'm') {
1289                                         puuid =
1290                                           &param.mdtuuid[param.num_mdts++];
1291                                 } else {
1292                                         puuid =
1293                                           &param.obduuid[param.num_obds++];
1294                                 }
1295                                 p = strchr(token, ',');
1296                                 next = 0;
1297                                 if (p) {
1298                                         *p = 0;
1299                                         next = p+1;
1300                                 }
1301
1302                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
1303                                         ret = -E2BIG;
1304                                         goto err_free;
1305                                 }
1306
1307                                 strncpy(puuid->uuid, token,
1308                                         sizeof(puuid->uuid));
1309                         }
1310 err_free:
1311                         if (buf)
1312                                 free(buf);
1313                         break;
1314                 }
1315                 case 'p':
1316                         param.zeroend = 1;
1317                         break;
1318                 case 'P':
1319                         break;
1320                 case 's':
1321                         if (optarg[0] == '+') {
1322                                 param.size_sign = -1;
1323                                 optarg++;
1324                         } else if (optarg[0] == '-') {
1325                                 param.size_sign =  1;
1326                                 optarg++;
1327                         }
1328
1329                         ret = llapi_parse_size(optarg, &param.size,
1330                                                &param.size_units, 0);
1331                         if (ret) {
1332                                 fprintf(stderr, "error: bad file size '%s'\n",
1333                                         optarg);
1334                                 goto err;
1335                         }
1336                         param.check_size = 1;
1337                         param.exclude_size = !!neg_opt;
1338                         break;
1339                 case 'S':
1340                         if (optarg[0] == '+') {
1341                                 param.stripesize_sign = -1;
1342                                 optarg++;
1343                         } else if (optarg[0] == '-') {
1344                                 param.stripesize_sign =  1;
1345                                 optarg++;
1346                         }
1347
1348                         ret = llapi_parse_size(optarg, &param.stripesize,
1349                                                &param.stripesize_units, 0);
1350                         if (ret) {
1351                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
1352                                         optarg);
1353                                 goto err;
1354                         }
1355                         param.check_stripesize = 1;
1356                         param.exclude_stripesize = !!neg_opt;
1357                         break;
1358                 case 't':
1359                         param.fp_exclude_type = !!neg_opt;
1360                         switch (optarg[0]) {
1361                         case 'b':
1362                                 param.fp_type = S_IFBLK;
1363                                 break;
1364                         case 'c':
1365                                 param.fp_type = S_IFCHR;
1366                                 break;
1367                         case 'd':
1368                                 param.fp_type = S_IFDIR;
1369                                 break;
1370                         case 'f':
1371                                 param.fp_type = S_IFREG;
1372                                 break;
1373                         case 'l':
1374                                 param.fp_type = S_IFLNK;
1375                                 break;
1376                         case 'p':
1377                                 param.fp_type = S_IFIFO;
1378                                 break;
1379                         case 's':
1380                                 param.fp_type = S_IFSOCK;
1381                                 break;
1382                         default:
1383                                 fprintf(stderr, "error: %s: bad type '%s'\n",
1384                                         argv[0], optarg);
1385                                 ret = CMD_HELP;
1386                                 goto err;
1387                         };
1388                         break;
1389                 default:
1390                         ret = CMD_HELP;
1391                         goto err;
1392                 };
1393         }
1394
1395         if (pathstart == -1) {
1396                 fprintf(stderr, "error: %s: no filename|pathname\n",
1397                         argv[0]);
1398                 ret = CMD_HELP;
1399                 goto err;
1400         } else if (pathend == -1) {
1401                 /* no options */
1402                 pathend = argc;
1403         }
1404
1405         do {
1406                 rc = llapi_find(argv[pathstart], &param);
1407                 if (rc != 0 && ret == 0)
1408                         ret = rc;
1409         } while (++pathstart < pathend);
1410
1411         if (ret)
1412                 fprintf(stderr, "error: %s failed for %s.\n",
1413                         argv[0], argv[optind - 1]);
1414 err:
1415         if (param.obduuid && param.num_alloc_obds)
1416                 free(param.obduuid);
1417
1418         if (param.mdtuuid && param.num_alloc_mdts)
1419                 free(param.mdtuuid);
1420
1421         return ret;
1422 }
1423
1424 static int lfs_getstripe_internal(int argc, char **argv,
1425                                   struct find_param *param)
1426 {
1427         struct option long_opts[] = {
1428 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1429                 /* This formerly implied "stripe-count", but was explicitly
1430                  * made "stripe-count" for consistency with other options,
1431                  * and to separate it from "mdt-count" when DNE arrives. */
1432                 {"count",               no_argument,            0, 'c'},
1433 #endif
1434                 {"stripe-count",        no_argument,            0, 'c'},
1435                 {"stripe_count",        no_argument,            0, 'c'},
1436                 {"directory",           no_argument,            0, 'd'},
1437                 {"default",             no_argument,            0, 'D'},
1438                 {"generation",          no_argument,            0, 'g'},
1439 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1440                 /* This formerly implied "stripe-index", but was explicitly
1441                  * made "stripe-index" for consistency with other options,
1442                  * and to separate it from "mdt-index" when DNE arrives. */
1443                 {"index",               no_argument,            0, 'i'},
1444 #endif
1445                 {"stripe-index",        no_argument,            0, 'i'},
1446                 {"stripe_index",        no_argument,            0, 'i'},
1447                 {"layout",              no_argument,            0, 'L'},
1448                 {"mdt-index",           no_argument,            0, 'M'},
1449                 {"mdt_index",           no_argument,            0, 'M'},
1450 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1451                 /* This formerly implied "stripe-index", but was confusing
1452                  * with "file offset" (which will eventually be needed for
1453                  * with different layouts by offset), so deprecate it. */
1454                 {"offset",              no_argument,            0, 'o'},
1455 #endif
1456                 {"obd",                 required_argument,      0, 'O'},
1457                 {"ost",                 required_argument,      0, 'O'},
1458                 {"pool",                no_argument,            0, 'p'},
1459                 {"quiet",               no_argument,            0, 'q'},
1460                 {"recursive",           no_argument,            0, 'r'},
1461                 {"raw",                 no_argument,            0, 'R'},
1462 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1463                 /* This formerly implied "--stripe-size", but was confusing
1464                  * with "lfs find --size|-s", which means "file size", so use
1465                  * the consistent "--stripe-size|-S" for all commands. */
1466                 {"size",                no_argument,            0, 's'},
1467 #endif
1468                 {"stripe-size",         no_argument,            0, 'S'},
1469                 {"stripe_size",         no_argument,            0, 'S'},
1470                 {"verbose",             no_argument,            0, 'v'},
1471                 {0, 0, 0, 0}
1472         };
1473         int c, rc;
1474
1475         param->fp_max_depth = 1;
1476         optind = 0;
1477         while ((c = getopt_long(argc, argv, "cdDghiLMoO:pqrRsSv",
1478                                 long_opts, NULL)) != -1) {
1479                 switch (c) {
1480                 case 'O':
1481                         if (param->obduuid) {
1482                                 fprintf(stderr,
1483                                         "error: %s: only one obduuid allowed",
1484                                         argv[0]);
1485                                 return CMD_HELP;
1486                         }
1487                         param->obduuid = (struct obd_uuid *)optarg;
1488                         break;
1489                 case 'q':
1490                         param->quiet++;
1491                         break;
1492                 case 'd':
1493                         param->fp_max_depth = 0;
1494                         break;
1495                 case 'D':
1496                         param->get_default_lmv = 1;
1497                         break;
1498                 case 'r':
1499                         param->recursive = 1;
1500                         break;
1501                 case 'v':
1502                         param->verbose = VERBOSE_ALL | VERBOSE_DETAIL;
1503                         break;
1504                 case 'c':
1505 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1506                         if (strcmp(argv[optind - 1], "--count") == 0)
1507                                 fprintf(stderr, "warning: '--count' deprecated,"
1508                                         " use '--stripe-count' instead\n");
1509 #endif
1510                         if (!(param->verbose & VERBOSE_DETAIL)) {
1511                                 param->verbose |= VERBOSE_COUNT;
1512                                 param->fp_max_depth = 0;
1513                         }
1514                         break;
1515 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1516                 case 's':
1517 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1518                         fprintf(stderr, "warning: '--size|-s' deprecated, "
1519                                 "use '--stripe-size|-S' instead\n");
1520 #endif
1521 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
1522                 case 'S':
1523                         if (!(param->verbose & VERBOSE_DETAIL)) {
1524                                 param->verbose |= VERBOSE_SIZE;
1525                                 param->fp_max_depth = 0;
1526                         }
1527                         break;
1528 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1529                 case 'o':
1530                         fprintf(stderr, "warning: '--offset|-o' deprecated, "
1531                                 "use '--stripe-index|-i' instead\n");
1532 #endif
1533                 case 'i':
1534 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1535                         if (strcmp(argv[optind - 1], "--index") == 0)
1536                                 fprintf(stderr, "warning: '--index' deprecated"
1537                                         ", use '--stripe-index' instead\n");
1538 #endif
1539                         if (!(param->verbose & VERBOSE_DETAIL)) {
1540                                 param->verbose |= VERBOSE_OFFSET;
1541                                 param->fp_max_depth = 0;
1542                         }
1543                         break;
1544                 case 'p':
1545                         if (!(param->verbose & VERBOSE_DETAIL)) {
1546                                 param->verbose |= VERBOSE_POOL;
1547                                 param->fp_max_depth = 0;
1548                         }
1549                         break;
1550                 case 'g':
1551                         if (!(param->verbose & VERBOSE_DETAIL)) {
1552                                 param->verbose |= VERBOSE_GENERATION;
1553                                 param->fp_max_depth = 0;
1554                         }
1555                         break;
1556                 case 'L':
1557                         if (!(param->verbose & VERBOSE_DETAIL)) {
1558                                 param->verbose |= VERBOSE_LAYOUT;
1559                                 param->fp_max_depth = 0;
1560                         }
1561                         break;
1562                 case 'M':
1563                         if (!(param->verbose & VERBOSE_DETAIL))
1564                                 param->fp_max_depth = 0;
1565                         param->verbose |= VERBOSE_MDTINDEX;
1566                         break;
1567                 case 'R':
1568                         param->raw = 1;
1569                         break;
1570                 default:
1571                         return CMD_HELP;
1572                 }
1573         }
1574
1575         if (optind >= argc)
1576                 return CMD_HELP;
1577
1578         if (param->recursive)
1579                 param->fp_max_depth = -1;
1580
1581         if (!param->verbose)
1582                 param->verbose = VERBOSE_ALL;
1583         if (param->quiet)
1584                 param->verbose = VERBOSE_OBJID;
1585
1586         do {
1587                 rc = llapi_getstripe(argv[optind], param);
1588         } while (++optind < argc && !rc);
1589
1590         if (rc)
1591                 fprintf(stderr, "error: %s failed for %s.\n",
1592                         argv[0], argv[optind - 1]);
1593         return rc;
1594 }
1595
1596 static int lfs_tgts(int argc, char **argv)
1597 {
1598         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1599         struct find_param param;
1600         int index = 0, rc=0;
1601
1602         if (argc > 2)
1603                 return CMD_HELP;
1604
1605         if (argc == 2 && !realpath(argv[1], path)) {
1606                 rc = -errno;
1607                 fprintf(stderr, "error: invalid path '%s': %s\n",
1608                         argv[1], strerror(-rc));
1609                 return rc;
1610         }
1611
1612         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
1613                 /* Check if we have a mount point */
1614                 if (mntdir[0] == '\0')
1615                         continue;
1616
1617                 memset(&param, 0, sizeof(param));
1618                 if (!strcmp(argv[0], "mdts"))
1619                         param.get_lmv = 1;
1620
1621                 rc = llapi_ostlist(mntdir, &param);
1622                 if (rc) {
1623                         fprintf(stderr, "error: %s: failed on %s\n",
1624                                 argv[0], mntdir);
1625                 }
1626                 if (path[0] != '\0')
1627                         break;
1628                 memset(mntdir, 0, PATH_MAX);
1629         }
1630
1631         return rc;
1632 }
1633
1634 static int lfs_getstripe(int argc, char **argv)
1635 {
1636         struct find_param param = { 0 };
1637         return lfs_getstripe_internal(argc, argv, &param);
1638 }
1639
1640 /* functions */
1641 static int lfs_getdirstripe(int argc, char **argv)
1642 {
1643         struct find_param param = { 0 };
1644
1645         param.get_lmv = 1;
1646         return lfs_getstripe_internal(argc, argv, &param);
1647 }
1648
1649 /* functions */
1650 static int lfs_setdirstripe(int argc, char **argv)
1651 {
1652         char                    *dname;
1653         int                     result;
1654         unsigned int            stripe_offset = -1;
1655         unsigned int            stripe_count = 1;
1656         enum lmv_hash_type      hash_type;
1657         char                    *end;
1658         int                     c;
1659         char                    *stripe_offset_opt = NULL;
1660         char                    *stripe_count_opt = NULL;
1661         char                    *stripe_hash_opt = NULL;
1662         char                    *mode_opt = NULL;
1663         int                     default_stripe = 0;
1664         mode_t                  mode = S_IRWXU | S_IRWXG | S_IRWXO;
1665         mode_t                  previous_mode = 0;
1666
1667         struct option long_opts[] = {
1668                 {"count",       required_argument, 0, 'c'},
1669                 {"index",       required_argument, 0, 'i'},
1670                 {"mode",        required_argument, 0, 'm'},
1671                 {"hash-type",   required_argument, 0, 't'},
1672                 {"default_stripe", no_argument, 0, 'D'},
1673                 {0, 0, 0, 0}
1674         };
1675
1676         optind = 0;
1677
1678         while ((c = getopt_long(argc, argv, "c:Di:m:t:", long_opts,
1679                                 NULL)) >= 0) {
1680                 switch (c) {
1681                 case 0:
1682                         /* Long options. */
1683                         break;
1684                 case 'c':
1685                         stripe_count_opt = optarg;
1686                         break;
1687                 case 'D':
1688                         default_stripe = 1;
1689                         break;
1690                 case 'i':
1691                         stripe_offset_opt = optarg;
1692                         break;
1693                 case 'm':
1694                         mode_opt = optarg;
1695                         break;
1696                 case 't':
1697                         stripe_hash_opt = optarg;
1698                         break;
1699                 default:
1700                         fprintf(stderr, "error: %s: option '%s' "
1701                                         "unrecognized\n",
1702                                         argv[0], argv[optind - 1]);
1703                         return CMD_HELP;
1704                 }
1705         }
1706
1707         if (optind == argc) {
1708                 fprintf(stderr, "error: %s: missing dirname\n",
1709                         argv[0]);
1710                 return CMD_HELP;
1711         }
1712
1713         if (stripe_offset_opt == NULL && stripe_count_opt == NULL) {
1714                 fprintf(stderr, "error: %s: missing stripe offset and count.\n",
1715                         argv[0]);
1716                 return CMD_HELP;
1717         }
1718
1719         if (stripe_offset_opt != NULL) {
1720                 /* get the stripe offset */
1721                 stripe_offset = strtoul(stripe_offset_opt, &end, 0);
1722                 if (*end != '\0') {
1723                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
1724                                 argv[0], stripe_offset_opt);
1725                         return CMD_HELP;
1726                 }
1727         }
1728
1729         if (mode_opt != NULL) {
1730                 mode = strtoul(mode_opt, &end, 8);
1731                 if (*end != '\0') {
1732                         fprintf(stderr, "error: %s: bad mode '%s'\n",
1733                                 argv[0], mode_opt);
1734                         return CMD_HELP;
1735                 }
1736                 previous_mode = umask(0);
1737         }
1738
1739         if (stripe_hash_opt == NULL ||
1740             strcmp(stripe_hash_opt, LMV_HASH_NAME_FNV_1A_64) == 0) {
1741                 hash_type = LMV_HASH_TYPE_FNV_1A_64;
1742         } else if (strcmp(stripe_hash_opt, LMV_HASH_NAME_ALL_CHARS) == 0) {
1743                 hash_type = LMV_HASH_TYPE_ALL_CHARS;
1744         } else {
1745                 fprintf(stderr, "error: %s: bad stripe hash type '%s'\n",
1746                         argv[0], stripe_hash_opt);
1747                 return CMD_HELP;
1748         }
1749
1750         /* get the stripe count */
1751         if (stripe_count_opt != NULL) {
1752                 stripe_count = strtoul(stripe_count_opt, &end, 0);
1753                 if (*end != '\0') {
1754                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
1755                                 argv[0], stripe_count_opt);
1756                         return CMD_HELP;
1757                 }
1758         }
1759
1760         dname = argv[optind];
1761         do {
1762                 if (default_stripe == 1) {
1763                         result = llapi_dir_set_default_lmv_stripe(dname,
1764                                                     stripe_offset, stripe_count,
1765                                                     hash_type, NULL);
1766                 } else {
1767                         result = llapi_dir_create_pool(dname, mode,
1768                                                        stripe_offset,
1769                                                        stripe_count, hash_type,
1770                                                        NULL);
1771                 }
1772
1773                 if (result) {
1774                         fprintf(stderr, "error: %s: create stripe dir '%s' "
1775                                 "failed\n", argv[0], dname);
1776                         break;
1777                 }
1778                 dname = argv[++optind];
1779         } while (dname != NULL);
1780
1781         if (mode_opt != NULL)
1782                 umask(previous_mode);
1783
1784         return result;
1785 }
1786
1787 /* functions */
1788 static int lfs_rmentry(int argc, char **argv)
1789 {
1790         char *dname;
1791         int   index;
1792         int   result = 0;
1793
1794         if (argc <= 1) {
1795                 fprintf(stderr, "error: %s: missing dirname\n",
1796                         argv[0]);
1797                 return CMD_HELP;
1798         }
1799
1800         index = 1;
1801         dname = argv[index];
1802         while (dname != NULL) {
1803                 result = llapi_direntry_remove(dname);
1804                 if (result) {
1805                         fprintf(stderr, "error: %s: remove dir entry '%s' "
1806                                 "failed\n", argv[0], dname);
1807                         break;
1808                 }
1809                 dname = argv[++index];
1810         }
1811         return result;
1812 }
1813
1814 static int lfs_mv(int argc, char **argv)
1815 {
1816         struct  find_param param = {
1817                 .fp_max_depth = -1,
1818                 .mdtindex = -1,
1819         };
1820         char   *end;
1821         int     c;
1822         int     rc = 0;
1823         struct option long_opts[] = {
1824                 {"--mdt-index", required_argument, 0, 'M'},
1825                 {"verbose",     no_argument,       0, 'v'},
1826                 {0, 0, 0, 0}
1827         };
1828
1829         while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
1830                 switch (c) {
1831                 case 'M': {
1832                         param.mdtindex = strtoul(optarg, &end, 0);
1833                         if (*end != '\0') {
1834                                 fprintf(stderr, "%s: invalid MDT index'%s'\n",
1835                                         argv[0], optarg);
1836                                 return CMD_HELP;
1837                         }
1838                         break;
1839                 }
1840                 case 'v': {
1841                         param.verbose = VERBOSE_DETAIL;
1842                         break;
1843                 }
1844                 default:
1845                         fprintf(stderr, "error: %s: unrecognized option '%s'\n",
1846                                 argv[0], argv[optind - 1]);
1847                         return CMD_HELP;
1848                 }
1849         }
1850
1851         if (param.mdtindex == -1) {
1852                 fprintf(stderr, "%s MDT index must be indicated\n", argv[0]);
1853                 return CMD_HELP;
1854         }
1855
1856         if (optind >= argc) {
1857                 fprintf(stderr, "%s missing operand path\n", argv[0]);
1858                 return CMD_HELP;
1859         }
1860
1861         param.migrate = 1;
1862         rc = llapi_mv(argv[optind], &param);
1863         if (rc != 0)
1864                 fprintf(stderr, "cannot migrate '%s' to MDT%04x: %s\n",
1865                         argv[optind], param.mdtindex, strerror(-rc));
1866         return rc;
1867 }
1868
1869 static int lfs_osts(int argc, char **argv)
1870 {
1871         return lfs_tgts(argc, argv);
1872 }
1873
1874 static int lfs_mdts(int argc, char **argv)
1875 {
1876         return lfs_tgts(argc, argv);
1877 }
1878
1879 #define COOK(value)                                                     \
1880 ({                                                                      \
1881         int radix = 0;                                                  \
1882         while (value > 1024) {                                          \
1883                 value /= 1024;                                          \
1884                 radix++;                                                \
1885         }                                                               \
1886         radix;                                                          \
1887 })
1888 #define UUF     "%-20s"
1889 #define CSF     "%11s"
1890 #define CDF     "%11llu"
1891 #define HDF     "%8.1f%c"
1892 #define RSF     "%4s"
1893 #define RDF     "%3d%%"
1894
1895 static int showdf(char *mntdir, struct obd_statfs *stat,
1896                   char *uuid, int ishow, int cooked,
1897                   char *type, int index, int rc)
1898 {
1899         long long avail, used, total;
1900         double ratio = 0;
1901         char *suffix = "KMGTPEZY";
1902         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
1903         char tbuf[3 * sizeof(__u64)];
1904         char ubuf[3 * sizeof(__u64)];
1905         char abuf[3 * sizeof(__u64)];
1906         char rbuf[3 * sizeof(__u64)];
1907
1908         if (!uuid || !stat)
1909                 return -EINVAL;
1910
1911         switch (rc) {
1912         case 0:
1913                 if (ishow) {
1914                         avail = stat->os_ffree;
1915                         used = stat->os_files - stat->os_ffree;
1916                         total = stat->os_files;
1917                 } else {
1918                         int shift = cooked ? 0 : 10;
1919
1920                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
1921                         used  = ((stat->os_blocks - stat->os_bfree) *
1922                                  stat->os_bsize) >> shift;
1923                         total = (stat->os_blocks * stat->os_bsize) >> shift;
1924                 }
1925
1926                 if ((used + avail) > 0)
1927                         ratio = (double)used / (double)(used + avail);
1928
1929                 if (cooked) {
1930                         int i;
1931                         double cook_val;
1932
1933                         cook_val = (double)total;
1934                         i = COOK(cook_val);
1935                         if (i > 0)
1936                                 sprintf(tbuf, HDF, cook_val, suffix[i - 1]);
1937                         else
1938                                 sprintf(tbuf, CDF, total);
1939
1940                         cook_val = (double)used;
1941                         i = COOK(cook_val);
1942                         if (i > 0)
1943                                 sprintf(ubuf, HDF, cook_val, suffix[i - 1]);
1944                         else
1945                                 sprintf(ubuf, CDF, used);
1946
1947                         cook_val = (double)avail;
1948                         i = COOK(cook_val);
1949                         if (i > 0)
1950                                 sprintf(abuf, HDF, cook_val, suffix[i - 1]);
1951                         else
1952                                 sprintf(abuf, CDF, avail);
1953                 } else {
1954                         sprintf(tbuf, CDF, total);
1955                         sprintf(ubuf, CDF, used);
1956                         sprintf(abuf, CDF, avail);
1957                 }
1958
1959                 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
1960                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1961                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1962                 if (type)
1963                         printf("[%s:%d]\n", type, index);
1964                 else
1965                         printf("\n");
1966
1967                 break;
1968         case -ENODATA:
1969                 printf(UUF": inactive device\n", uuid);
1970                 break;
1971         default:
1972                 printf(UUF": %s\n", uuid, strerror(-rc));
1973                 break;
1974         }
1975
1976         return 0;
1977 }
1978
1979 struct ll_stat_type {
1980         int   st_op;
1981         char *st_name;
1982 };
1983
1984 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow,
1985                 int cooked, int lazy)
1986 {
1987         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1988         struct obd_uuid uuid_buf;
1989         char *poolname = NULL;
1990         struct ll_stat_type types[] = { { LL_STATFS_LMV, "MDT" },
1991                                         { LL_STATFS_LOV, "OST" },
1992                                         { 0, NULL } };
1993         struct ll_stat_type *tp;
1994         __u64 ost_ffree = 0;
1995         __u32 index;
1996         __u32 type;
1997         int rc;
1998
1999         if (pool) {
2000                 poolname = strchr(pool, '.');
2001                 if (poolname != NULL) {
2002                         if (strncmp(fsname, pool, strlen(fsname))) {
2003                                 fprintf(stderr, "filesystem name incorrect\n");
2004                                 return -ENODEV;
2005                         }
2006                         poolname++;
2007                 } else
2008                         poolname = pool;
2009         }
2010
2011         if (ishow)
2012                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
2013                        "UUID", "Inodes", "IUsed", "IFree",
2014                        "IUse%", "Mounted on");
2015         else
2016                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
2017                        "UUID", cooked ? "bytes" : "1K-blocks",
2018                        "Used", "Available", "Use%", "Mounted on");
2019
2020         for (tp = types; tp->st_name != NULL; tp++) {
2021                 for (index = 0; ; index++) {
2022                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
2023                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2024                         type = lazy ? tp->st_op | LL_STATFS_NODELAY : tp->st_op;
2025                         rc = llapi_obd_statfs(mntdir, type, index,
2026                                               &stat_buf, &uuid_buf);
2027                         if (rc == -ENODEV)
2028                                 break;
2029
2030                         if (rc == -EAGAIN)
2031                                 continue;
2032
2033                         if (poolname && tp->st_op == LL_STATFS_LOV &&
2034                             llapi_search_ost(fsname, poolname,
2035                                              obd_uuid2str(&uuid_buf)) != 1)
2036                                 continue;
2037
2038                         /* the llapi_obd_statfs() call may have returned with
2039                          * an error, but if it filled in uuid_buf we will at
2040                          * lease use that to print out a message for that OBD.
2041                          * If we didn't get anything in the uuid_buf, then fill
2042                          * it in so that we can print an error message. */
2043                         if (uuid_buf.uuid[0] == '\0')
2044                                 sprintf(uuid_buf.uuid, "%s%04x",
2045                                         tp->st_name, index);
2046                         showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
2047                                ishow, cooked, tp->st_name, index, rc);
2048
2049                         if (rc == 0) {
2050                                 if (tp->st_op == LL_STATFS_LMV) {
2051                                         sum.os_ffree += stat_buf.os_ffree;
2052                                         sum.os_files += stat_buf.os_files;
2053                                 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
2054                                         sum.os_blocks += stat_buf.os_blocks *
2055                                                 stat_buf.os_bsize;
2056                                         sum.os_bfree  += stat_buf.os_bfree *
2057                                                 stat_buf.os_bsize;
2058                                         sum.os_bavail += stat_buf.os_bavail *
2059                                                 stat_buf.os_bsize;
2060                                         ost_ffree += stat_buf.os_ffree;
2061                                 }
2062                         } else if (rc == -EINVAL || rc == -EFAULT) {
2063                                 break;
2064                         }
2065                 }
2066         }
2067
2068         /* If we don't have as many objects free on the OST as inodes
2069          * on the MDS, we reduce the total number of inodes to
2070          * compensate, so that the "inodes in use" number is correct.
2071          * Matches ll_statfs_internal() so the results are consistent. */
2072         if (ost_ffree < sum.os_ffree) {
2073                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
2074                 sum.os_ffree = ost_ffree;
2075         }
2076         printf("\n");
2077         showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0, 0);
2078         printf("\n");
2079         return 0;
2080 }
2081
2082 static int lfs_df(int argc, char **argv)
2083 {
2084         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
2085         int ishow = 0, cooked = 0;
2086         int lazy = 0;
2087         int c, rc = 0, index = 0;
2088         char fsname[PATH_MAX] = "", *pool_name = NULL;
2089         struct option long_opts[] = {
2090                 {"pool", required_argument, 0, 'p'},
2091                 {"lazy", 0, 0, 'l'},
2092                 {0, 0, 0, 0}
2093         };
2094
2095         optind = 0;
2096         while ((c = getopt_long(argc, argv, "hilp:", long_opts, NULL)) != -1) {
2097                 switch (c) {
2098                 case 'i':
2099                         ishow = 1;
2100                         break;
2101                 case 'h':
2102                         cooked = 1;
2103                         break;
2104                 case 'l':
2105                         lazy = 1;
2106                         break;
2107                 case 'p':
2108                         pool_name = optarg;
2109                         break;
2110                 default:
2111                         return CMD_HELP;
2112                 }
2113         }
2114         if (optind < argc && !realpath(argv[optind], path)) {
2115                 rc = -errno;
2116                 fprintf(stderr, "error: invalid path '%s': %s\n",
2117                         argv[optind], strerror(-rc));
2118                 return rc;
2119         }
2120
2121         while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
2122                 /* Check if we have a mount point */
2123                 if (mntdir[0] == '\0')
2124                         continue;
2125
2126                 rc = mntdf(mntdir, fsname, pool_name, ishow, cooked, lazy);
2127                 if (rc || path[0] != '\0')
2128                         break;
2129                 fsname[0] = '\0'; /* avoid matching in next loop */
2130                 mntdir[0] = '\0'; /* avoid matching in next loop */
2131         }
2132
2133         return rc;
2134 }
2135
2136 static int lfs_getname(int argc, char **argv)
2137 {
2138         char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
2139         int rc = 0, index = 0, c;
2140         char buf[sizeof(struct obd_uuid)];
2141
2142         optind = 0;
2143         while ((c = getopt(argc, argv, "h")) != -1)
2144                 return CMD_HELP;
2145
2146         if (optind == argc) { /* no paths specified, get all paths. */
2147                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
2148                         rc = llapi_getname(mntdir, buf, sizeof(buf));
2149                         if (rc < 0) {
2150                                 fprintf(stderr,
2151                                         "cannot get name for `%s': %s\n",
2152                                         mntdir, strerror(-rc));
2153                                 break;
2154                         }
2155
2156                         printf("%s %s\n", buf, mntdir);
2157
2158                         path[0] = fsname[0] = mntdir[0] = 0;
2159                 }
2160         } else { /* paths specified, only attempt to search these. */
2161                 for (; optind < argc; optind++) {
2162                         rc = llapi_getname(argv[optind], buf, sizeof(buf));
2163                         if (rc < 0) {
2164                                 fprintf(stderr,
2165                                         "cannot get name for `%s': %s\n",
2166                                         argv[optind], strerror(-rc));
2167                                 break;
2168                         }
2169
2170                         printf("%s %s\n", buf, argv[optind]);
2171                 }
2172         }
2173         return rc;
2174 }
2175
2176 static int lfs_check(int argc, char **argv)
2177 {
2178         int rc;
2179         char mntdir[PATH_MAX] = {'\0'};
2180         int num_types = 1;
2181         char *obd_types[2];
2182         char obd_type1[4];
2183         char obd_type2[4];
2184
2185         if (argc != 2)
2186                 return CMD_HELP;
2187
2188         obd_types[0] = obd_type1;
2189         obd_types[1] = obd_type2;
2190
2191         if (strcmp(argv[1], "osts") == 0) {
2192                 strcpy(obd_types[0], "osc");
2193         } else if (strcmp(argv[1], "mds") == 0) {
2194                 strcpy(obd_types[0], "mdc");
2195         } else if (strcmp(argv[1], "servers") == 0) {
2196                 num_types = 2;
2197                 strcpy(obd_types[0], "osc");
2198                 strcpy(obd_types[1], "mdc");
2199         } else {
2200                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2201                                 argv[0], argv[1]);
2202                         return CMD_HELP;
2203         }
2204
2205         rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
2206         if (rc < 0 || mntdir[0] == '\0') {
2207                 fprintf(stderr, "No suitable Lustre mount found\n");
2208                 return rc;
2209         }
2210
2211         rc = llapi_target_check(num_types, obd_types, mntdir);
2212         if (rc)
2213                 fprintf(stderr, "error: %s: %s status failed\n",
2214                                 argv[0],argv[1]);
2215
2216         return rc;
2217
2218 }
2219
2220 static int lfs_join(int argc, char **argv)
2221 {
2222         fprintf(stderr, "join two lustre files into one.\n"
2223                         "obsolete, HEAD does not support it anymore.\n");
2224         return 0;
2225 }
2226
2227 #ifdef HAVE_SYS_QUOTA_H
2228 static int lfs_quotacheck(int argc, char **argv)
2229 {
2230         int c, check_type = 0;
2231         char *mnt;
2232         struct if_quotacheck qchk;
2233         struct if_quotactl qctl;
2234         char *obd_type = (char *)qchk.obd_type;
2235         int rc;
2236
2237         memset(&qchk, 0, sizeof(qchk));
2238
2239         optind = 0;
2240         while ((c = getopt(argc, argv, "gu")) != -1) {
2241                 switch (c) {
2242                 case 'u':
2243                         check_type |= 0x01;
2244                         break;
2245                 case 'g':
2246                         check_type |= 0x02;
2247                         break;
2248                 default:
2249                         fprintf(stderr, "error: %s: option '-%c' "
2250                                         "unrecognized\n", argv[0], c);
2251                         return CMD_HELP;
2252                 }
2253         }
2254
2255         if (check_type)
2256                 check_type--;
2257         else    /* do quotacheck for both user & group quota by default */
2258                 check_type = 0x02;
2259
2260         if (argc == optind)
2261                 return CMD_HELP;
2262
2263         mnt = argv[optind];
2264
2265         rc = llapi_quotacheck(mnt, check_type);
2266         if (rc == -EOPNOTSUPP) {
2267                 fprintf(stderr, "error: quotacheck not supported by the quota "
2268                         "master.\nPlease note that quotacheck is deprecated as "
2269                         "of lustre 2.4.0 since space accounting is always "
2270                         "enabled.\nFilesystems not formatted with 2.4 utils or "
2271                         "beyond can be upgraded with tunefs.lustre --quota.\n");
2272                 return rc;
2273         } else if (rc) {
2274                 fprintf(stderr, "quotacheck failed: %s\n", strerror(-rc));
2275                 return rc;
2276         }
2277
2278         rc = llapi_poll_quotacheck(mnt, &qchk);
2279         if (rc) {
2280                 if (*obd_type)
2281                         fprintf(stderr, "%s %s ", obd_type,
2282                                 obd_uuid2str(&qchk.obd_uuid));
2283                 fprintf(stderr, "quota check failed: %s\n", strerror(-rc));
2284                 return rc;
2285         }
2286
2287         memset(&qctl, 0, sizeof(qctl));
2288         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
2289         qctl.qc_type = check_type;
2290         rc = llapi_quotactl(mnt, &qctl);
2291         if (rc && rc != -EALREADY) {
2292                 if (*obd_type)
2293                         fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
2294                                 obd_uuid2str(&qctl.obd_uuid));
2295                 fprintf(stderr, "%s turn on quota failed: %s\n",
2296                         argv[0], strerror(-rc));
2297                 return rc;
2298         }
2299
2300         return 0;
2301 }
2302
2303 static int lfs_quotaon(int argc, char **argv)
2304 {
2305         int c;
2306         char *mnt;
2307         struct if_quotactl qctl;
2308         char *obd_type = (char *)qctl.obd_type;
2309         int rc;
2310
2311         memset(&qctl, 0, sizeof(qctl));
2312         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
2313
2314         optind = 0;
2315         while ((c = getopt(argc, argv, "fgu")) != -1) {
2316                 switch (c) {
2317                 case 'u':
2318                         qctl.qc_type |= 0x01;
2319                         break;
2320                 case 'g':
2321                         qctl.qc_type |= 0x02;
2322                         break;
2323                 case 'f':
2324                         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
2325                         break;
2326                 default:
2327                         fprintf(stderr, "error: %s: option '-%c' "
2328                                         "unrecognized\n", argv[0], c);
2329                         return CMD_HELP;
2330                 }
2331         }
2332
2333         if (qctl.qc_type)
2334                 qctl.qc_type--;
2335         else /* by default, enable quota for both user & group */
2336                 qctl.qc_type = 0x02;
2337
2338         if (argc == optind)
2339                 return CMD_HELP;
2340
2341         mnt = argv[optind];
2342
2343         rc = llapi_quotactl(mnt, &qctl);
2344         if (rc) {
2345                 if (rc == -EOPNOTSUPP) {
2346                         fprintf(stderr, "error: quotaon not supported by the "
2347                                 "quota master.\nPlease note that quotaon/off is"
2348                                 " deprecated as of lustre 2.4.0.\nQuota "
2349                                 "enforcement should now be enabled on the MGS "
2350                                 "via:\nmgs# lctl conf_param ${FSNAME}.quota."
2351                                 "<ost|mdt>=<u|g|ug>\n(ost for block quota, mdt "
2352                                 "for inode quota, u for user and g for group"
2353                                 "\n");
2354                 } else if (rc == -EALREADY) {
2355                         rc = 0;
2356                 } else if (rc == -ENOENT) {
2357                         fprintf(stderr, "error: cannot find quota database, "
2358                                         "make sure you have run quotacheck\n");
2359                 } else {
2360                         if (*obd_type)
2361                                 fprintf(stderr, "%s %s ", obd_type,
2362                                         obd_uuid2str(&qctl.obd_uuid));
2363                         fprintf(stderr, "%s failed: %s\n", argv[0],
2364                                 strerror(-rc));
2365                 }
2366         }
2367
2368         return rc;
2369 }
2370
2371 static int lfs_quotaoff(int argc, char **argv)
2372 {
2373         int c;
2374         char *mnt;
2375         struct if_quotactl qctl;
2376         char *obd_type = (char *)qctl.obd_type;
2377         int rc;
2378
2379         memset(&qctl, 0, sizeof(qctl));
2380         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
2381
2382         optind = 0;
2383         while ((c = getopt(argc, argv, "gu")) != -1) {
2384                 switch (c) {
2385                 case 'u':
2386                         qctl.qc_type |= 0x01;
2387                         break;
2388                 case 'g':
2389                         qctl.qc_type |= 0x02;
2390                         break;
2391                 default:
2392                         fprintf(stderr, "error: %s: option '-%c' "
2393                                         "unrecognized\n", argv[0], c);
2394                         return CMD_HELP;
2395                 }
2396         }
2397
2398         if (qctl.qc_type)
2399                 qctl.qc_type--;
2400         else /* by default, disable quota for both user & group */
2401                 qctl.qc_type = 0x02;
2402
2403         if (argc == optind)
2404                 return CMD_HELP;
2405
2406         mnt = argv[optind];
2407
2408         rc = llapi_quotactl(mnt, &qctl);
2409         if (rc) {
2410                 if (rc == -EOPNOTSUPP) {
2411                         fprintf(stderr, "error: quotaoff not supported by the "
2412                                 "quota master.\nPlease note that quotaon/off is"
2413                                 " deprecated as of lustre 2.4.0.\nQuota "
2414                                 "enforcement can be disabled on the MGS via:\n"
2415                                 "mgs# lctl conf_param ${FSNAME}.quota.<ost|mdt>"
2416                                 "=\"\"\n");
2417                 } else if (rc == -EALREADY) {
2418                         rc = 0;
2419                 } else {
2420                         if (*obd_type)
2421                                 fprintf(stderr, "%s %s ", obd_type,
2422                                         obd_uuid2str(&qctl.obd_uuid));
2423                         fprintf(stderr, "quotaoff failed: %s\n",
2424                                 strerror(-rc));
2425                 }
2426         }
2427
2428         return rc;
2429 }
2430
2431 #define ARG2INT(nr, str, msg)                                           \
2432 do {                                                                    \
2433         char *endp;                                                     \
2434         nr = strtol(str, &endp, 0);                                     \
2435         if (*endp) {                                                    \
2436                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
2437                 return CMD_HELP;                                        \
2438         }                                                               \
2439 } while (0)
2440
2441 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
2442
2443 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
2444  * returns the value or ULONG_MAX on integer overflow or incorrect format
2445  * Notes:
2446  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
2447  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
2448  *        3. empty integer value is interpreted as 0
2449  */
2450 static unsigned long str2sec(const char* timestr)
2451 {
2452         const char spec[] = "smhdw";
2453         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
2454         unsigned long val = 0;
2455         char *tail;
2456
2457         if (strpbrk(timestr, spec) == NULL) {
2458                 /* no specifiers inside the time string,
2459                    should treat it as an integer value */
2460                 val = strtoul(timestr, &tail, 10);
2461                 return *tail ? ULONG_MAX : val;
2462         }
2463
2464         /* format string is XXwXXdXXhXXmXXs */
2465         while (*timestr) {
2466                 unsigned long v;
2467                 int ind;
2468                 char* ptr;
2469
2470                 v = strtoul(timestr, &tail, 10);
2471                 if (v == ULONG_MAX || *tail == '\0')
2472                         /* value too large (ULONG_MAX or more)
2473                            or missing specifier */
2474                         goto error;
2475
2476                 ptr = strchr(spec, *tail);
2477                 if (ptr == NULL)
2478                         /* unknown specifier */
2479                         goto error;
2480
2481                 ind = ptr - spec;
2482
2483                 /* check if product will overflow the type */
2484                 if (!(v < ULONG_MAX / mult[ind]))
2485                         goto error;
2486
2487                 ADD_OVERFLOW(val, mult[ind] * v);
2488                 if (val == ULONG_MAX)
2489                         goto error;
2490
2491                 timestr = tail + 1;
2492         }
2493
2494         return val;
2495
2496 error:
2497         return ULONG_MAX;
2498 }
2499
2500 #define ARG2ULL(nr, str, def_units)                                     \
2501 do {                                                                    \
2502         unsigned long long limit, units = def_units;                    \
2503         int rc;                                                         \
2504                                                                         \
2505         rc = llapi_parse_size(str, &limit, &units, 1);                  \
2506         if (rc < 0) {                                                   \
2507                 fprintf(stderr, "error: bad limit value %s\n", str);    \
2508                 return CMD_HELP;                                        \
2509         }                                                               \
2510         nr = limit;                                                     \
2511 } while (0)
2512
2513 static inline int has_times_option(int argc, char **argv)
2514 {
2515         int i;
2516
2517         for (i = 1; i < argc; i++)
2518                 if (!strcmp(argv[i], "-t"))
2519                         return 1;
2520
2521         return 0;
2522 }
2523
2524 int lfs_setquota_times(int argc, char **argv)
2525 {
2526         int c, rc;
2527         struct if_quotactl qctl;
2528         char *mnt, *obd_type = (char *)qctl.obd_type;
2529         struct obd_dqblk *dqb = &qctl.qc_dqblk;
2530         struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
2531         struct option long_opts[] = {
2532                 {"block-grace",     required_argument, 0, 'b'},
2533                 {"group",           no_argument,       0, 'g'},
2534                 {"inode-grace",     required_argument, 0, 'i'},
2535                 {"times",           no_argument,       0, 't'},
2536                 {"user",            no_argument,       0, 'u'},
2537                 {0, 0, 0, 0}
2538         };
2539
2540         memset(&qctl, 0, sizeof(qctl));
2541         qctl.qc_cmd  = LUSTRE_Q_SETINFO;
2542         qctl.qc_type = UGQUOTA;
2543
2544         optind = 0;
2545         while ((c = getopt_long(argc, argv, "b:gi:tu", long_opts, NULL)) != -1) {
2546                 switch (c) {
2547                 case 'u':
2548                 case 'g':
2549                         if (qctl.qc_type != UGQUOTA) {
2550                                 fprintf(stderr, "error: -u and -g can't be used "
2551                                                 "more than once\n");
2552                                 return CMD_HELP;
2553                         }
2554                         qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
2555                         break;
2556                 case 'b':
2557                         if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
2558                                 fprintf(stderr, "error: bad block-grace: %s\n",
2559                                         optarg);
2560                                 return CMD_HELP;
2561                         }
2562                         dqb->dqb_valid |= QIF_BTIME;
2563                         break;
2564                 case 'i':
2565                         if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
2566                                 fprintf(stderr, "error: bad inode-grace: %s\n",
2567                                         optarg);
2568                                 return CMD_HELP;
2569                         }
2570                         dqb->dqb_valid |= QIF_ITIME;
2571                         break;
2572                 case 't': /* Yes, of course! */
2573                         break;
2574                 default: /* getopt prints error message for us when opterr != 0 */
2575                         return CMD_HELP;
2576                 }
2577         }
2578
2579         if (qctl.qc_type == UGQUOTA) {
2580                 fprintf(stderr, "error: neither -u nor -g 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         rc = llapi_quotactl(mnt, &qctl);
2591         if (rc) {
2592                 if (*obd_type)
2593                         fprintf(stderr, "%s %s ", obd_type,
2594                                 obd_uuid2str(&qctl.obd_uuid));
2595                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
2596                 return rc;
2597         }
2598
2599         return 0;
2600 }
2601
2602 #define BSLIMIT (1 << 0)
2603 #define BHLIMIT (1 << 1)
2604 #define ISLIMIT (1 << 2)
2605 #define IHLIMIT (1 << 3)
2606
2607 int lfs_setquota(int argc, char **argv)
2608 {
2609         int c, rc;
2610         struct if_quotactl qctl;
2611         char *mnt, *obd_type = (char *)qctl.obd_type;
2612         struct obd_dqblk *dqb = &qctl.qc_dqblk;
2613         struct option long_opts[] = {
2614                 {"block-softlimit", required_argument, 0, 'b'},
2615                 {"block-hardlimit", required_argument, 0, 'B'},
2616                 {"group",           required_argument, 0, 'g'},
2617                 {"inode-softlimit", required_argument, 0, 'i'},
2618                 {"inode-hardlimit", required_argument, 0, 'I'},
2619                 {"user",            required_argument, 0, 'u'},
2620                 {0, 0, 0, 0}
2621         };
2622         unsigned limit_mask = 0;
2623         char *endptr;
2624
2625         if (has_times_option(argc, argv))
2626                 return lfs_setquota_times(argc, argv);
2627
2628         memset(&qctl, 0, sizeof(qctl));
2629         qctl.qc_cmd  = LUSTRE_Q_SETQUOTA;
2630         qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
2631                                  * so it can be used as a marker that qc_type
2632                                  * isn't reinitialized from command line */
2633
2634         optind = 0;
2635         while ((c = getopt_long(argc, argv, "b:B:g:i:I:u:", long_opts, NULL)) != -1) {
2636                 switch (c) {
2637                 case 'u':
2638                 case 'g':
2639                         if (qctl.qc_type != UGQUOTA) {
2640                                 fprintf(stderr, "error: -u and -g can't be used"
2641                                                 " more than once\n");
2642                                 return CMD_HELP;
2643                         }
2644                         qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
2645                         rc = name2id(&qctl.qc_id, optarg,
2646                                      (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2647                         if (rc) {
2648                                 qctl.qc_id = strtoul(optarg, &endptr, 10);
2649                                 if (*endptr != '\0') {
2650                                         fprintf(stderr, "error: can't find id "
2651                                                 "for name %s\n", optarg);
2652                                         return CMD_HELP;
2653                                 }
2654                         }
2655                         break;
2656                 case 'b':
2657                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
2658                         dqb->dqb_bsoftlimit >>= 10;
2659                         limit_mask |= BSLIMIT;
2660                         if (dqb->dqb_bsoftlimit &&
2661                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
2662                                 fprintf(stderr, "warning: block softlimit is "
2663                                         "smaller than the miminal qunit size, "
2664                                         "please see the help of setquota or "
2665                                         "Lustre manual for details.\n");
2666                         break;
2667                 case 'B':
2668                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
2669                         dqb->dqb_bhardlimit >>= 10;
2670                         limit_mask |= BHLIMIT;
2671                         if (dqb->dqb_bhardlimit &&
2672                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
2673                                 fprintf(stderr, "warning: block hardlimit is "
2674                                         "smaller than the miminal qunit size, "
2675                                         "please see the help of setquota or "
2676                                         "Lustre manual for details.\n");
2677                         break;
2678                 case 'i':
2679                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
2680                         limit_mask |= ISLIMIT;
2681                         if (dqb->dqb_isoftlimit &&
2682                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
2683                                 fprintf(stderr, "warning: inode softlimit is "
2684                                         "smaller than the miminal qunit size, "
2685                                         "please see the help of setquota or "
2686                                         "Lustre manual for details.\n");
2687                         break;
2688                 case 'I':
2689                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
2690                         limit_mask |= IHLIMIT;
2691                         if (dqb->dqb_ihardlimit &&
2692                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
2693                                 fprintf(stderr, "warning: inode hardlimit is "
2694                                         "smaller than the miminal qunit size, "
2695                                         "please see the help of setquota or "
2696                                         "Lustre manual for details.\n");
2697                         break;
2698                 default: /* getopt prints error message for us when opterr != 0 */
2699                         return CMD_HELP;
2700                 }
2701         }
2702
2703         if (qctl.qc_type == UGQUOTA) {
2704                 fprintf(stderr, "error: neither -u nor -g was specified\n");
2705                 return CMD_HELP;
2706         }
2707
2708         if (limit_mask == 0) {
2709                 fprintf(stderr, "error: at least one limit must be specified\n");
2710                 return CMD_HELP;
2711         }
2712
2713         if (optind != argc - 1) {
2714                 fprintf(stderr, "error: unexpected parameters encountered\n");
2715                 return CMD_HELP;
2716         }
2717
2718         mnt = argv[optind];
2719
2720         if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
2721             (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
2722                 /* sigh, we can't just set blimits/ilimits */
2723                 struct if_quotactl tmp_qctl = {.qc_cmd  = LUSTRE_Q_GETQUOTA,
2724                                                .qc_type = qctl.qc_type,
2725                                                .qc_id   = qctl.qc_id};
2726
2727                 rc = llapi_quotactl(mnt, &tmp_qctl);
2728                 if (rc < 0) {
2729                         fprintf(stderr, "error: setquota failed while retrieving"
2730                                         " current quota settings (%s)\n",
2731                                         strerror(-rc));
2732                         return rc;
2733                 }
2734
2735                 if (!(limit_mask & BHLIMIT))
2736                         dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
2737                 if (!(limit_mask & BSLIMIT))
2738                         dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
2739                 if (!(limit_mask & IHLIMIT))
2740                         dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
2741                 if (!(limit_mask & ISLIMIT))
2742                         dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
2743
2744                 /* Keep grace times if we have got no softlimit arguments */
2745                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
2746                         dqb->dqb_valid |= QIF_BTIME;
2747                         dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
2748                 }
2749
2750                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
2751                         dqb->dqb_valid |= QIF_ITIME;
2752                         dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
2753                 }
2754         }
2755
2756         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
2757         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
2758
2759         rc = llapi_quotactl(mnt, &qctl);
2760         if (rc) {
2761                 if (*obd_type)
2762                         fprintf(stderr, "%s %s ", obd_type,
2763                                 obd_uuid2str(&qctl.obd_uuid));
2764                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
2765                 return rc;
2766         }
2767
2768         return 0;
2769 }
2770
2771 static inline char *type2name(int check_type)
2772 {
2773         if (check_type == USRQUOTA)
2774                 return "user";
2775         else if (check_type == GRPQUOTA)
2776                 return "group";
2777         else
2778                 return "unknown";
2779 }
2780
2781 /* Converts seconds value into format string
2782  * result is returned in buf
2783  * Notes:
2784  *        1. result is in descenting order: 1w2d3h4m5s
2785  *        2. zero fields are not filled (except for p. 3): 5d1s
2786  *        3. zero seconds value is presented as "0s"
2787  */
2788 static char * __sec2str(time_t seconds, char *buf)
2789 {
2790         const char spec[] = "smhdw";
2791         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
2792         unsigned long c;
2793         char *tail = buf;
2794         int i;
2795
2796         for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
2797                 c = seconds / mult[i];
2798
2799                 if (c > 0 || (i == 0 && buf == tail))
2800                         tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
2801
2802                 seconds %= mult[i];
2803         }
2804
2805         return tail;
2806 }
2807
2808 static void sec2str(time_t seconds, char *buf, int rc)
2809 {
2810         char *tail = buf;
2811
2812         if (rc)
2813                 *tail++ = '[';
2814
2815         tail = __sec2str(seconds, tail);
2816
2817         if (rc && tail - buf < 39) {
2818                 *tail++ = ']';
2819                 *tail++ = 0;
2820         }
2821 }
2822
2823 static void diff2str(time_t seconds, char *buf, time_t now)
2824 {
2825
2826         buf[0] = 0;
2827         if (!seconds)
2828                 return;
2829         if (seconds <= now) {
2830                 strcpy(buf, "none");
2831                 return;
2832         }
2833         __sec2str(seconds - now, buf);
2834 }
2835
2836 static void print_quota_title(char *name, struct if_quotactl *qctl,
2837                               bool human_readable)
2838 {
2839         printf("Disk quotas for %s %s (%cid %u):\n",
2840                type2name(qctl->qc_type), name,
2841                *type2name(qctl->qc_type), qctl->qc_id);
2842         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
2843                "Filesystem", human_readable ? "used" : "kbytes",
2844                "quota", "limit", "grace",
2845                "files", "quota", "limit", "grace");
2846 }
2847
2848 static void kbytes2str(__u64 num, char *buf, bool h)
2849 {
2850         if (!h) {
2851                 sprintf(buf, LPU64, num);
2852         } else {
2853                 if (num >> 30)
2854                         sprintf(buf, "%5.4gT", (double)num / (1 << 30));
2855                 else if (num >> 20)
2856                         sprintf(buf, "%5.4gG", (double)num / (1 << 20));
2857                 else if (num >> 10)
2858                         sprintf(buf, "%5.4gM", (double)num / (1 << 10));
2859                 else
2860                         sprintf(buf, LPU64"%s", num, "k");
2861         }
2862 }
2863
2864 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
2865                         int rc, bool h)
2866 {
2867         time_t now;
2868
2869         time(&now);
2870
2871         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
2872                 int bover = 0, iover = 0;
2873                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
2874                 char numbuf[3][32];
2875                 char timebuf[40];
2876                 char strbuf[32];
2877
2878                 if (dqb->dqb_bhardlimit &&
2879                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
2880                         bover = 1;
2881                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
2882                         if (dqb->dqb_btime > now) {
2883                                 bover = 2;
2884                         } else {
2885                                 bover = 3;
2886                         }
2887                 }
2888
2889                 if (dqb->dqb_ihardlimit &&
2890                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
2891                         iover = 1;
2892                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
2893                         if (dqb->dqb_itime > now) {
2894                                 iover = 2;
2895                         } else {
2896                                 iover = 3;
2897                         }
2898                 }
2899
2900
2901                 if (strlen(mnt) > 15)
2902                         printf("%s\n%15s", mnt, "");
2903                 else
2904                         printf("%15s", mnt);
2905
2906                 if (bover)
2907                         diff2str(dqb->dqb_btime, timebuf, now);
2908
2909                 kbytes2str(lustre_stoqb(dqb->dqb_curspace), strbuf, h);
2910                 if (rc == -EREMOTEIO)
2911                         sprintf(numbuf[0], "%s*", strbuf);
2912                 else
2913                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
2914                                 "%s" : "[%s]", strbuf);
2915
2916                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, h);
2917                 if (type == QC_GENERAL)
2918                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
2919                                 "%s" : "[%s]", strbuf);
2920                 else
2921                         sprintf(numbuf[1], "%s", "-");
2922
2923                 kbytes2str(dqb->dqb_bhardlimit, strbuf, h);
2924                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
2925                         "%s" : "[%s]", strbuf);
2926
2927                 printf(" %7s%c %6s %7s %7s",
2928                        numbuf[0], bover ? '*' : ' ', numbuf[1],
2929                        numbuf[2], bover > 1 ? timebuf : "-");
2930
2931                 if (iover)
2932                         diff2str(dqb->dqb_itime, timebuf, now);
2933
2934                 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2935                         LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2936
2937                 if (type == QC_GENERAL)
2938                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
2939                                 LPU64 : "["LPU64"]", dqb->dqb_isoftlimit);
2940                 else
2941                         sprintf(numbuf[1], "%s", "-");
2942
2943                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2944                         LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2945
2946                 if (type != QC_OSTIDX)
2947                         printf(" %7s%c %6s %7s %7s",
2948                                numbuf[0], iover ? '*' : ' ', numbuf[1],
2949                                numbuf[2], iover > 1 ? timebuf : "-");
2950                 else
2951                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
2952                 printf("\n");
2953
2954         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2955                    qctl->qc_cmd == Q_GETOINFO) {
2956                 char bgtimebuf[40];
2957                 char igtimebuf[40];
2958
2959                 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
2960                 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
2961                 printf("Block grace time: %s; Inode grace time: %s\n",
2962                        bgtimebuf, igtimebuf);
2963         }
2964 }
2965
2966 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
2967                            bool h, __u64 *total)
2968 {
2969         int rc = 0, rc1 = 0, count = 0;
2970         __u32 valid = qctl->qc_valid;
2971
2972         rc = llapi_get_obd_count(mnt, &count, is_mdt);
2973         if (rc) {
2974                 fprintf(stderr, "can not get %s count: %s\n",
2975                         is_mdt ? "mdt": "ost", strerror(-rc));
2976                 return rc;
2977         }
2978
2979         for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2980                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2981                 rc = llapi_quotactl(mnt, qctl);
2982                 if (rc) {
2983                         /* It is remote client case. */
2984                         if (-rc == EOPNOTSUPP) {
2985                                 rc = 0;
2986                                 goto out;
2987                         }
2988
2989                         if (!rc1)
2990                                 rc1 = rc;
2991                         fprintf(stderr, "quotactl %s%d failed.\n",
2992                                 is_mdt ? "mdt": "ost", qctl->qc_idx);
2993                         continue;
2994                 }
2995
2996                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
2997                             qctl->qc_valid, 0, h);
2998                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
2999                                    qctl->qc_dqblk.dqb_bhardlimit;
3000         }
3001 out:
3002         qctl->qc_valid = valid;
3003         return rc ? : rc1;
3004 }
3005
3006 static int lfs_quota(int argc, char **argv)
3007 {
3008         int c;
3009         char *mnt, *name = NULL;
3010         struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
3011                                     .qc_type = UGQUOTA };
3012         char *obd_type = (char *)qctl.obd_type;
3013         char *obd_uuid = (char *)qctl.obd_uuid.uuid;
3014         int rc, rc1 = 0, rc2 = 0, rc3 = 0,
3015             verbose = 0, pass = 0, quiet = 0, inacc;
3016         char *endptr;
3017         __u32 valid = QC_GENERAL, idx = 0;
3018         __u64 total_ialloc = 0, total_balloc = 0;
3019         bool human_readable = false;
3020
3021         optind = 0;
3022         while ((c = getopt(argc, argv, "gi:I:o:qtuvh")) != -1) {
3023                 switch (c) {
3024                 case 'u':
3025                         if (qctl.qc_type != UGQUOTA) {
3026                                 fprintf(stderr, "error: use either -u or -g\n");
3027                                 return CMD_HELP;
3028                         }
3029                         qctl.qc_type = USRQUOTA;
3030                         break;
3031                 case 'g':
3032                         if (qctl.qc_type != UGQUOTA) {
3033                                 fprintf(stderr, "error: use either -u or -g\n");
3034                                 return CMD_HELP;
3035                         }
3036                         qctl.qc_type = GRPQUOTA;
3037                         break;
3038                 case 't':
3039                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
3040                         break;
3041                 case 'o':
3042                         valid = qctl.qc_valid = QC_UUID;
3043                         strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
3044                         break;
3045                 case 'i':
3046                         valid = qctl.qc_valid = QC_MDTIDX;
3047                         idx = qctl.qc_idx = atoi(optarg);
3048                         break;
3049                 case 'I':
3050                         valid = qctl.qc_valid = QC_OSTIDX;
3051                         idx = qctl.qc_idx = atoi(optarg);
3052                         break;
3053                 case 'v':
3054                         verbose = 1;
3055                         break;
3056                 case 'q':
3057                         quiet = 1;
3058                         break;
3059                 case 'h':
3060                         human_readable = true;
3061                         break;
3062                 default:
3063                         fprintf(stderr, "error: %s: option '-%c' "
3064                                         "unrecognized\n", argv[0], c);
3065                         return CMD_HELP;
3066                 }
3067         }
3068
3069         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
3070         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
3071             optind == argc - 1) {
3072 ug_output:
3073                 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
3074                 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
3075                 qctl.qc_valid = valid;
3076                 qctl.qc_idx = idx;
3077                 if (pass++ == 0) {
3078                         qctl.qc_type = USRQUOTA;
3079                         qctl.qc_id = geteuid();
3080                 } else {
3081                         qctl.qc_type = GRPQUOTA;
3082                         qctl.qc_id = getegid();
3083                 }
3084                 rc = id2name(&name, qctl.qc_id,
3085                              (qctl.qc_type == USRQUOTA) ? USER : GROUP);
3086                 if (rc)
3087                         name = "<unknown>";
3088         /* lfs quota -u username /path/to/lustre/mount */
3089         } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
3090                 /* options should be followed by u/g-name and mntpoint */
3091                 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
3092                         fprintf(stderr, "error: missing quota argument(s)\n");
3093                         return CMD_HELP;
3094                 }
3095
3096                 name = argv[optind++];
3097                 rc = name2id(&qctl.qc_id, name,
3098                              (qctl.qc_type == USRQUOTA) ? USER : GROUP);
3099                 if (rc) {
3100                         qctl.qc_id = strtoul(name, &endptr, 10);
3101                         if (*endptr != '\0') {
3102                                 fprintf(stderr, "error: can't find id for name "
3103                                         "%s\n", name);
3104                                 return CMD_HELP;
3105                         }
3106                 }
3107         } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
3108                 fprintf(stderr, "error: missing quota info argument(s)\n");
3109                 return CMD_HELP;
3110         }
3111
3112         mnt = argv[optind];
3113
3114         rc1 = llapi_quotactl(mnt, &qctl);
3115         if (rc1 < 0) {
3116                 switch (rc1) {
3117                 case -ESRCH:
3118                         fprintf(stderr, "%s quotas are not enabled.\n",
3119                                 qctl.qc_type == USRQUOTA ? "user" : "group");
3120                         goto out;
3121                 case -EPERM:
3122                         fprintf(stderr, "Permission denied.\n");
3123                 case -ENOENT:
3124                         /* We already got a "No such file..." message. */
3125                         goto out;
3126                 default:
3127                         fprintf(stderr, "Unexpected quotactl error: %s\n",
3128                                 strerror(-rc1));
3129                 }
3130         }
3131
3132         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
3133                 print_quota_title(name, &qctl, human_readable);
3134
3135         if (rc1 && *obd_type)
3136                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
3137
3138         if (qctl.qc_valid != QC_GENERAL)
3139                 mnt = "";
3140
3141         inacc = (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) &&
3142                 ((qctl.qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
3143                  (QIF_LIMITS|QIF_USAGE));
3144
3145         print_quota(mnt, &qctl, QC_GENERAL, rc1, human_readable);
3146
3147         if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO &&
3148             verbose) {
3149                 char strbuf[32];
3150
3151                 rc2 = print_obd_quota(mnt, &qctl, 1, human_readable,
3152                                       &total_ialloc);
3153                 rc3 = print_obd_quota(mnt, &qctl, 0, human_readable,
3154                                       &total_balloc);
3155                 kbytes2str(total_balloc, strbuf, human_readable);
3156                 printf("Total allocated inode limit: "LPU64", total "
3157                        "allocated block limit: %s\n", total_ialloc, strbuf);
3158         }
3159
3160         if (rc1 || rc2 || rc3 || inacc)
3161                 printf("Some errors happened when getting quota info. "
3162                        "Some devices may be not working or deactivated. "
3163                        "The data in \"[]\" is inaccurate.\n");
3164
3165 out:
3166         if (pass == 1)
3167                 goto ug_output;
3168
3169         return rc1;
3170 }
3171 #endif /* HAVE_SYS_QUOTA_H! */
3172
3173 static int flushctx_ioctl(char *mp)
3174 {
3175         int fd, rc;
3176
3177         fd = open(mp, O_RDONLY);
3178         if (fd == -1) {
3179                 fprintf(stderr, "flushctx: error open %s: %s\n",
3180                         mp, strerror(errno));
3181                 return -1;
3182         }
3183
3184         rc = ioctl(fd, LL_IOC_FLUSHCTX);
3185         if (rc == -1)
3186                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
3187                         mp, strerror(errno));
3188
3189         close(fd);
3190         return rc;
3191 }
3192
3193 static int lfs_flushctx(int argc, char **argv)
3194 {
3195         int     kdestroy = 0, c;
3196         char    mntdir[PATH_MAX] = {'\0'};
3197         int     index = 0;
3198         int     rc = 0;
3199
3200         optind = 0;
3201         while ((c = getopt(argc, argv, "k")) != -1) {
3202                 switch (c) {
3203                 case 'k':
3204                         kdestroy = 1;
3205                         break;
3206                 default:
3207                         fprintf(stderr, "error: %s: option '-%c' "
3208                                         "unrecognized\n", argv[0], c);
3209                         return CMD_HELP;
3210                 }
3211         }
3212
3213         if (kdestroy) {
3214             if ((rc = system("kdestroy > /dev/null")) != 0) {
3215                 rc = WEXITSTATUS(rc);
3216                 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
3217             }
3218         }
3219
3220         if (optind >= argc) {
3221                 /* flush for all mounted lustre fs. */
3222                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
3223                         /* Check if we have a mount point */
3224                         if (mntdir[0] == '\0')
3225                                 continue;
3226
3227                         if (flushctx_ioctl(mntdir))
3228                                 rc = -1;
3229
3230                         mntdir[0] = '\0'; /* avoid matching in next loop */
3231                 }
3232         } else {
3233                 /* flush fs as specified */
3234                 while (optind < argc) {
3235                         if (flushctx_ioctl(argv[optind++]))
3236                                 rc = -1;
3237                 }
3238         }
3239         return rc;
3240 }
3241
3242 static int lfs_lsetfacl(int argc, char **argv)
3243 {
3244         argv[0]++;
3245         return(llapi_lsetfacl(argc, argv));
3246 }
3247
3248 static int lfs_lgetfacl(int argc, char **argv)
3249 {
3250         argv[0]++;
3251         return(llapi_lgetfacl(argc, argv));
3252 }
3253
3254 static int lfs_rsetfacl(int argc, char **argv)
3255 {
3256         argv[0]++;
3257         return(llapi_rsetfacl(argc, argv));
3258 }
3259
3260 static int lfs_rgetfacl(int argc, char **argv)
3261 {
3262         argv[0]++;
3263         return(llapi_rgetfacl(argc, argv));
3264 }
3265
3266 static int lfs_cp(int argc, char **argv)
3267 {
3268         return(llapi_cp(argc, argv));
3269 }
3270
3271 static int lfs_ls(int argc, char **argv)
3272 {
3273         return(llapi_ls(argc, argv));
3274 }
3275
3276 static int lfs_changelog(int argc, char **argv)
3277 {
3278         void *changelog_priv;
3279         struct changelog_rec *rec;
3280         long long startrec = 0, endrec = 0;
3281         char *mdd;
3282         struct option long_opts[] = {
3283                 {"follow", no_argument, 0, 'f'},
3284                 {0, 0, 0, 0}
3285         };
3286         char short_opts[] = "f";
3287         int rc, follow = 0;
3288
3289         optind = 0;
3290         while ((rc = getopt_long(argc, argv, short_opts,
3291                                 long_opts, NULL)) != -1) {
3292                 switch (rc) {
3293                 case 'f':
3294                         follow++;
3295                         break;
3296                 case '?':
3297                         return CMD_HELP;
3298                 default:
3299                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3300                                 argv[0], argv[optind - 1]);
3301                         return CMD_HELP;
3302                 }
3303         }
3304         if (optind >= argc)
3305                 return CMD_HELP;
3306
3307         mdd = argv[optind++];
3308         if (argc > optind)
3309                 startrec = strtoll(argv[optind++], NULL, 10);
3310         if (argc > optind)
3311                 endrec = strtoll(argv[optind++], NULL, 10);
3312
3313         rc = llapi_changelog_start(&changelog_priv,
3314                                    CHANGELOG_FLAG_BLOCK |
3315                                    CHANGELOG_FLAG_JOBID |
3316                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
3317                                    mdd, startrec);
3318         if (rc < 0) {
3319                 fprintf(stderr, "Can't start changelog: %s\n",
3320                         strerror(errno = -rc));
3321                 return rc;
3322         }
3323
3324         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
3325                 time_t secs;
3326                 struct tm ts;
3327
3328                 if (endrec && rec->cr_index > endrec) {
3329                         llapi_changelog_free(&rec);
3330                         break;
3331                 }
3332                 if (rec->cr_index < startrec) {
3333                         llapi_changelog_free(&rec);
3334                         continue;
3335                 }
3336
3337                 secs = rec->cr_time >> 30;
3338                 gmtime_r(&secs, &ts);
3339                 printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
3340                        "0x%x t="DFID, rec->cr_index, rec->cr_type,
3341                        changelog_type2str(rec->cr_type),
3342                        ts.tm_hour, ts.tm_min, ts.tm_sec,
3343                        (int)(rec->cr_time & ((1<<30) - 1)),
3344                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
3345                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
3346
3347                 if (rec->cr_flags & CLF_JOBID) {
3348                         struct changelog_ext_jobid *jid =
3349                                 changelog_rec_jobid(rec);
3350
3351                         if (jid->cr_jobid[0] != '\0')
3352                                 printf(" j=%s", jid->cr_jobid);
3353                 }
3354
3355                 if (rec->cr_namelen)
3356                         printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
3357                                rec->cr_namelen, changelog_rec_name(rec));
3358
3359                 if (rec->cr_flags & CLF_RENAME) {
3360                         struct changelog_ext_rename *rnm =
3361                                 changelog_rec_rename(rec);
3362
3363                         if (!fid_is_zero(&rnm->cr_sfid))
3364                                 printf(" s="DFID" sp="DFID" %.*s",
3365                                        PFID(&rnm->cr_sfid),
3366                                        PFID(&rnm->cr_spfid),
3367                                        changelog_rec_snamelen(rec),
3368                                        changelog_rec_sname(rec));
3369                 }
3370                 printf("\n");
3371
3372                 llapi_changelog_free(&rec);
3373         }
3374
3375         llapi_changelog_fini(&changelog_priv);
3376
3377         if (rc < 0)
3378                 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
3379
3380         return (rc == 1 ? 0 : rc);
3381 }
3382
3383 static int lfs_changelog_clear(int argc, char **argv)
3384 {
3385         long long endrec;
3386         int rc;
3387
3388         if (argc != 4)
3389                 return CMD_HELP;
3390
3391         endrec = strtoll(argv[3], NULL, 10);
3392
3393         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
3394         if (rc)
3395                 fprintf(stderr, "%s error: %s\n", argv[0],
3396                         strerror(errno = -rc));
3397         return rc;
3398 }
3399
3400 static int lfs_fid2path(int argc, char **argv)
3401 {
3402         struct option long_opts[] = {
3403                 {"cur", no_argument, 0, 'c'},
3404                 {"link", required_argument, 0, 'l'},
3405                 {"rec", required_argument, 0, 'r'},
3406                 {0, 0, 0, 0}
3407         };
3408         char  short_opts[] = "cl:r:";
3409         char *device, *fid, *path;
3410         long long recno = -1;
3411         int linkno = -1;
3412         int lnktmp;
3413         int printcur = 0;
3414         int rc = 0;
3415
3416         optind = 0;
3417
3418         while ((rc = getopt_long(argc, argv, short_opts,
3419                                 long_opts, NULL)) != -1) {
3420                 switch (rc) {
3421                 case 'c':
3422                         printcur++;
3423                         break;
3424                 case 'l':
3425                         linkno = strtol(optarg, NULL, 10);
3426                         break;
3427                 case 'r':
3428                         recno = strtoll(optarg, NULL, 10);
3429                         break;
3430                 case '?':
3431                         return CMD_HELP;
3432                 default:
3433                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3434                                 argv[0], argv[optind - 1]);
3435                         return CMD_HELP;
3436                 }
3437         }
3438
3439         if (argc < 3)
3440                 return CMD_HELP;
3441
3442         device = argv[optind++];
3443         path = calloc(1, PATH_MAX);
3444         if (path == NULL) {
3445                 fprintf(stderr, "error: Not enough memory\n");
3446                 return -errno;
3447         }
3448
3449         rc = 0;
3450         while (optind < argc) {
3451                 fid = argv[optind++];
3452
3453                 lnktmp = (linkno >= 0) ? linkno : 0;
3454                 while (1) {
3455                         int oldtmp = lnktmp;
3456                         long long rectmp = recno;
3457                         int rc2;
3458                         rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
3459                                              &rectmp, &lnktmp);
3460                         if (rc2 < 0) {
3461                                 fprintf(stderr, "%s: error on FID %s: %s\n",
3462                                         argv[0], fid, strerror(errno = -rc2));
3463                                 if (rc == 0)
3464                                         rc = rc2;
3465                                 break;
3466                         }
3467
3468                         if (printcur)
3469                                 fprintf(stdout, "%lld ", rectmp);
3470                         if (device[0] == '/') {
3471                                 fprintf(stdout, "%s", device);
3472                                 if (device[strlen(device) - 1] != '/')
3473                                         fprintf(stdout, "/");
3474                         } else if (path[0] == '\0') {
3475                                 fprintf(stdout, "/");
3476                         }
3477                         fprintf(stdout, "%s\n", path);
3478
3479                         if (linkno >= 0)
3480                                 /* specified linkno */
3481                                 break;
3482                         if (oldtmp == lnktmp)
3483                                 /* no more links */
3484                                 break;
3485                 }
3486         }
3487
3488         free(path);
3489         return rc;
3490 }
3491
3492 static int lfs_path2fid(int argc, char **argv)
3493 {
3494         struct option     long_opts[] = {
3495                 {"parents", no_argument, 0, 'p'},
3496                 {0, 0, 0, 0}
3497         };
3498         char            **path;
3499         const char        short_opts[] = "p";
3500         const char       *sep = "";
3501         lustre_fid        fid;
3502         int               rc = 0;
3503         bool              show_parents = false;
3504
3505         optind = 0;
3506         while ((rc = getopt_long(argc, argv, short_opts,
3507                                  long_opts, NULL)) != -1) {
3508                 switch (rc) {
3509                 case 'p':
3510                         show_parents = true;
3511                         break;
3512                 default:
3513                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3514                                 argv[0], argv[optind - 1]);
3515                         return CMD_HELP;
3516                 }
3517         }
3518
3519         if (optind > argc - 1)
3520                 return CMD_HELP;
3521         else if (optind < argc - 1)
3522                 sep = ": ";
3523
3524         rc = 0;
3525         for (path = argv + optind; *path != NULL; path++) {
3526                 int err = 0;
3527                 if (!show_parents) {
3528                         err = llapi_path2fid(*path, &fid);
3529                         if (!err)
3530                                 printf("%s%s"DFID"\n",
3531                                        *sep != '\0' ? *path : "", sep,
3532                                        PFID(&fid));
3533                 } else {
3534                         char            name[NAME_MAX + 1];
3535                         unsigned int    linkno = 0;
3536
3537                         while ((err = llapi_path2parent(*path, linkno, &fid,
3538                                                 name, sizeof(name))) == 0) {
3539                                 if (*sep != '\0' && linkno == 0)
3540                                         printf("%s%s", *path, sep);
3541
3542                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
3543                                        PFID(&fid), name);
3544                                 linkno++;
3545                         }
3546
3547                         /* err == -ENODATA is end-of-loop */
3548                         if (linkno > 0 && err == -ENODATA) {
3549                                 printf("\n");
3550                                 err = 0;
3551                         }
3552                 }
3553
3554                 if (err) {
3555                         fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
3556                                 argv[0], show_parents ? "parent " : "", *path,
3557                                 strerror(-err));
3558                         if (rc == 0) {
3559                                 rc = err;
3560                                 errno = -err;
3561                         }
3562                 }
3563         }
3564
3565         return rc;
3566 }
3567
3568 static int lfs_data_version(int argc, char **argv)
3569 {
3570         char *path;
3571         __u64 data_version;
3572         int fd;
3573         int rc;
3574         int c;
3575         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
3576
3577         if (argc < 2)
3578                 return CMD_HELP;
3579
3580         optind = 0;
3581         while ((c = getopt(argc, argv, "nrw")) != -1) {
3582                 switch (c) {
3583                 case 'n':
3584                         data_version_flags = 0;
3585                         break;
3586                 case 'r':
3587                         data_version_flags |= LL_DV_RD_FLUSH;
3588                         break;
3589                 case 'w':
3590                         data_version_flags |= LL_DV_WR_FLUSH;
3591                         break;
3592                 default:
3593                         return CMD_HELP;
3594                 }
3595         }
3596         if (optind == argc)
3597                 return CMD_HELP;
3598
3599         path = argv[optind];
3600         fd = open(path, O_RDONLY);
3601         if (fd < 0)
3602                 err(errno, "cannot open file %s", path);
3603
3604         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
3605         if (rc < 0)
3606                 err(errno, "cannot get version for %s", path);
3607         else
3608                 printf(LPU64 "\n", data_version);
3609
3610         close(fd);
3611         return rc;
3612 }
3613
3614 static int lfs_hsm_state(int argc, char **argv)
3615 {
3616         int rc;
3617         int i = 1;
3618         char *path;
3619         struct hsm_user_state hus;
3620
3621         if (argc < 2)
3622                 return CMD_HELP;
3623
3624         do {
3625                 path = argv[i];
3626
3627                 rc = llapi_hsm_state_get(path, &hus);
3628                 if (rc) {
3629                         fprintf(stderr, "can't get hsm state for %s: %s\n",
3630                                 path, strerror(errno = -rc));
3631                         return rc;
3632                 }
3633
3634                 /* Display path name and status flags */
3635                 printf("%s: (0x%08x)", path, hus.hus_states);
3636
3637                 if (hus.hus_states & HS_RELEASED)
3638                         printf(" released");
3639                 if (hus.hus_states & HS_EXISTS)
3640                         printf(" exists");
3641                 if (hus.hus_states & HS_DIRTY)
3642                         printf(" dirty");
3643                 if (hus.hus_states & HS_ARCHIVED)
3644                         printf(" archived");
3645                 /* Display user-settable flags */
3646                 if (hus.hus_states & HS_NORELEASE)
3647                         printf(" never_release");
3648                 if (hus.hus_states & HS_NOARCHIVE)
3649                         printf(" never_archive");
3650                 if (hus.hus_states & HS_LOST)
3651                         printf(" lost_from_hsm");
3652
3653                 if (hus.hus_archive_id != 0)
3654                         printf(", archive_id:%d", hus.hus_archive_id);
3655                 printf("\n");
3656
3657         } while (++i < argc);
3658
3659         return 0;
3660 }
3661
3662 #define LFS_HSM_SET   0
3663 #define LFS_HSM_CLEAR 1
3664
3665 /**
3666  * Generic function to set or clear HSM flags.
3667  * Used by hsm_set and hsm_clear.
3668  *
3669  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
3670  */
3671 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
3672 {
3673         struct option long_opts[] = {
3674                 {"lost", 0, 0, 'l'},
3675                 {"norelease", 0, 0, 'r'},
3676                 {"noarchive", 0, 0, 'a'},
3677                 {"archived", 0, 0, 'A'},
3678                 {"dirty", 0, 0, 'd'},
3679                 {"exists", 0, 0, 'e'},
3680                 {0, 0, 0, 0}
3681         };
3682         char short_opts[] = "lraAde";
3683         __u64 mask = 0;
3684         int c, rc;
3685         char *path;
3686
3687         if (argc < 3)
3688                 return CMD_HELP;
3689
3690         optind = 0;
3691         while ((c = getopt_long(argc, argv, short_opts,
3692                                 long_opts, NULL)) != -1) {
3693                 switch (c) {
3694                 case 'l':
3695                         mask |= HS_LOST;
3696                         break;
3697                 case 'a':
3698                         mask |= HS_NOARCHIVE;
3699                         break;
3700                 case 'A':
3701                         mask |= HS_ARCHIVED;
3702                         break;
3703                 case 'r':
3704                         mask |= HS_NORELEASE;
3705                         break;
3706                 case 'd':
3707                         mask |= HS_DIRTY;
3708                         break;
3709                 case 'e':
3710                         mask |= HS_EXISTS;
3711                         break;
3712                 case '?':
3713                         return CMD_HELP;
3714                 default:
3715                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3716                                 argv[0], argv[optind - 1]);
3717                         return CMD_HELP;
3718                 }
3719         }
3720
3721         /* User should have specified a flag */
3722         if (mask == 0)
3723                 return CMD_HELP;
3724
3725         while (optind < argc) {
3726
3727                 path = argv[optind];
3728
3729                 /* If mode == 0, this means we apply the mask. */
3730                 if (mode == LFS_HSM_SET)
3731                         rc = llapi_hsm_state_set(path, mask, 0, 0);
3732                 else
3733                         rc = llapi_hsm_state_set(path, 0, mask, 0);
3734
3735                 if (rc != 0) {
3736                         fprintf(stderr, "Can't change hsm flags for %s: %s\n",
3737                                 path, strerror(errno = -rc));
3738                         return rc;
3739                 }
3740                 optind++;
3741         }
3742
3743         return 0;
3744 }
3745
3746 static int lfs_hsm_action(int argc, char **argv)
3747 {
3748         int                              rc;
3749         int                              i = 1;
3750         char                            *path;
3751         struct hsm_current_action        hca;
3752         struct hsm_extent                he;
3753         enum hsm_user_action             hua;
3754         enum hsm_progress_states         hps;
3755
3756         if (argc < 2)
3757                 return CMD_HELP;
3758
3759         do {
3760                 path = argv[i];
3761
3762                 rc = llapi_hsm_current_action(path, &hca);
3763                 if (rc) {
3764                         fprintf(stderr, "can't get hsm action for %s: %s\n",
3765                                 path, strerror(errno = -rc));
3766                         return rc;
3767                 }
3768                 he = hca.hca_location;
3769                 hua = hca.hca_action;
3770                 hps = hca.hca_state;
3771
3772                 printf("%s: %s", path, hsm_user_action2name(hua));
3773
3774                 /* Skip file without action */
3775                 if (hca.hca_action == HUA_NONE) {
3776                         printf("\n");
3777                         continue;
3778                 }
3779
3780                 printf(" %s ", hsm_progress_state2name(hps));
3781
3782                 if ((hps == HPS_RUNNING) &&
3783                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
3784                         printf("("LPX64 " bytes moved)\n", he.length);
3785                 else if ((he.offset + he.length) == LUSTRE_EOF)
3786                         printf("(from "LPX64 " to EOF)\n", he.offset);
3787                 else
3788                         printf("(from "LPX64 " to "LPX64")\n",
3789                                he.offset, he.offset + he.length);
3790
3791         } while (++i < argc);
3792
3793         return 0;
3794 }
3795
3796 static int lfs_hsm_set(int argc, char **argv)
3797 {
3798         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
3799 }
3800
3801 static int lfs_hsm_clear(int argc, char **argv)
3802 {
3803         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
3804 }
3805
3806 /**
3807  * Check file state and return its fid, to be used by lfs_hsm_request().
3808  *
3809  * \param[in]     file      Path to file to check
3810  * \param[in,out] fid       Pointer to allocated lu_fid struct.
3811  * \param[in,out] last_dev  Pointer to last device id used.
3812  *
3813  * \return 0 on success.
3814  */
3815 static int lfs_hsm_prepare_file(char *file, struct lu_fid *fid,
3816                                 dev_t *last_dev)
3817 {
3818         struct stat     st;
3819         int             rc;
3820
3821         rc = lstat(file, &st);
3822         if (rc) {
3823                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
3824                 return -errno;
3825         }
3826         /* Checking for regular file as archiving as posix copytool
3827          * rejects archiving files other than regular files
3828          */
3829         if (!S_ISREG(st.st_mode)) {
3830                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
3831                 return CMD_HELP;
3832         }
3833         /* A request should be ... */
3834         if (*last_dev != st.st_dev && *last_dev != 0) {
3835                 fprintf(stderr, "All files should be "
3836                         "on the same filesystem: %s\n", file);
3837                 return -EINVAL;
3838         }
3839         *last_dev = st.st_dev;
3840
3841         rc = llapi_path2fid(file, fid);
3842         if (rc) {
3843                 fprintf(stderr, "Cannot read FID of %s: %s\n",
3844                         file, strerror(-rc));
3845                 return rc;
3846         }
3847         return 0;
3848 }
3849
3850 static int lfs_hsm_request(int argc, char **argv, int action)
3851 {
3852         struct option            long_opts[] = {
3853                 {"filelist", 1, 0, 'l'},
3854                 {"data", 1, 0, 'D'},
3855                 {"archive", 1, 0, 'a'},
3856                 {0, 0, 0, 0}
3857         };
3858         dev_t                    last_dev = 0;
3859         char                     short_opts[] = "l:D:a:";
3860         struct hsm_user_request *hur, *oldhur;
3861         int                      c, i;
3862         size_t                   len;
3863         int                      nbfile;
3864         char                    *line = NULL;
3865         char                    *filelist = NULL;
3866         char                     fullpath[PATH_MAX];
3867         char                    *opaque = NULL;
3868         int                      opaque_len = 0;
3869         int                      archive_id = 0;
3870         FILE                    *fp;
3871         int                      nbfile_alloc = 0;
3872         char                     some_file[PATH_MAX+1] = "";
3873         int                      rc;
3874
3875         if (argc < 2)
3876                 return CMD_HELP;
3877
3878         optind = 0;
3879         while ((c = getopt_long(argc, argv, short_opts,
3880                                 long_opts, NULL)) != -1) {
3881                 switch (c) {
3882                 case 'l':
3883                         filelist = optarg;
3884                         break;
3885                 case 'D':
3886                         opaque = optarg;
3887                         break;
3888                 case 'a':
3889                         if (action != HUA_ARCHIVE) {
3890                                 fprintf(stderr,
3891                                         "error: -a is supported only "
3892                                         "when archiving\n");
3893                                 return CMD_HELP;
3894                         }
3895                         archive_id = atoi(optarg);
3896                         break;
3897                 case '?':
3898                         return CMD_HELP;
3899                 default:
3900                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3901                                 argv[0], argv[optind - 1]);
3902                         return CMD_HELP;
3903                 }
3904         }
3905
3906         /* All remaining args are files, so we have at least nbfile */
3907         nbfile = argc - optind;
3908
3909         if ((nbfile == 0) && (filelist == NULL))
3910                 return CMD_HELP;
3911
3912         if (opaque != NULL)
3913                 opaque_len = strlen(opaque);
3914
3915         /* Alloc the request structure with enough place to store all files
3916          * from command line. */
3917         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
3918         if (hur == NULL) {
3919                 fprintf(stderr, "Cannot create the request: %s\n",
3920                         strerror(errno));
3921                 return errno;
3922         }
3923         nbfile_alloc = nbfile;
3924
3925         hur->hur_request.hr_action = action;
3926         hur->hur_request.hr_archive_id = archive_id;
3927         hur->hur_request.hr_flags = 0;
3928
3929         /* All remaining args are files, add them */
3930         if (nbfile != 0) {
3931                 if (strlen(argv[optind]) > sizeof(some_file)-1) {
3932                         free(hur);
3933                         return -E2BIG;
3934                 }
3935                 strncpy(some_file, argv[optind], sizeof(some_file));
3936         }
3937
3938         for (i = 0; i < nbfile; i++) {
3939                 hur->hur_user_item[i].hui_extent.length = -1;
3940                 rc = lfs_hsm_prepare_file(argv[optind + i],
3941                                           &hur->hur_user_item[i].hui_fid,
3942                                           &last_dev);
3943                 hur->hur_request.hr_itemcount++;
3944                 if (rc)
3945                         goto out_free;
3946         }
3947
3948         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
3949
3950         /* If a filelist was specified, read the filelist from it. */
3951         if (filelist != NULL) {
3952                 fp = fopen(filelist, "r");
3953                 if (fp == NULL) {
3954                         fprintf(stderr, "Cannot read the file list %s: %s\n",
3955                                 filelist, strerror(errno));
3956                         rc = -errno;
3957                         goto out_free;
3958                 }
3959
3960                 while ((rc = getline(&line, &len, fp)) != -1) {
3961                         struct hsm_user_item *hui;
3962
3963                         /* If allocated buffer was too small, gets something
3964                          * bigger */
3965                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
3966                                 ssize_t size;
3967                                 nbfile_alloc = nbfile_alloc * 2 + 1;
3968                                 oldhur = hur;
3969                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
3970                                                                    opaque_len);
3971                                 if (hur == NULL) {
3972                                         fprintf(stderr, "hsm: cannot allocate "
3973                                                 "the request: %s\n",
3974                                                 strerror(errno));
3975                                         hur = oldhur;
3976                                         rc = -errno;
3977                                         fclose(fp);
3978                                         goto out_free;
3979                                 }
3980                                 size = hur_len(oldhur);
3981                                 if (size < 0) {
3982                                         fprintf(stderr, "hsm: cannot allocate "
3983                                                 "%u files + %u bytes data\n",
3984                                             oldhur->hur_request.hr_itemcount,
3985                                             oldhur->hur_request.hr_data_len);
3986                                         free(hur);
3987                                         hur = oldhur;
3988                                         rc = -E2BIG;
3989                                         fclose(fp);
3990                                         goto out_free;
3991                                 }
3992                                 memcpy(hur, oldhur, size);
3993                                 free(oldhur);
3994                         }
3995
3996                         /* Chop CR */
3997                         if (line[strlen(line) - 1] == '\n')
3998                                 line[strlen(line) - 1] = '\0';
3999
4000                         hui =
4001                              &hur->hur_user_item[hur->hur_request.hr_itemcount];
4002                         hui->hui_extent.length = -1;
4003                         rc = lfs_hsm_prepare_file(line, &hui->hui_fid,
4004                                                   &last_dev);
4005                         hur->hur_request.hr_itemcount++;
4006                         if (rc) {
4007                                 fclose(fp);
4008                                 goto out_free;
4009                         }
4010
4011                         if ((some_file[0] == '\0') &&
4012                             (strlen(line) < sizeof(some_file)))
4013                                 strcpy(some_file, line);
4014                 }
4015
4016                 rc = fclose(fp);
4017                 if (line)
4018                         free(line);
4019         }
4020
4021         /* If a --data was used, add it to the request */
4022         hur->hur_request.hr_data_len = opaque_len;
4023         if (opaque != NULL)
4024                 memcpy(hur_data(hur), opaque, opaque_len);
4025
4026         /* Send the HSM request */
4027         if (realpath(some_file, fullpath) == NULL) {
4028                 fprintf(stderr, "Could not find path '%s': %s\n",
4029                         some_file, strerror(errno));
4030         }
4031         rc = llapi_hsm_request(fullpath, hur);
4032         if (rc) {
4033                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
4034                         some_file, strerror(-rc));
4035                 goto out_free;
4036         }
4037
4038 out_free:
4039         free(hur);
4040         return rc;
4041 }
4042
4043 static int lfs_hsm_archive(int argc, char **argv)
4044 {
4045         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
4046 }
4047
4048 static int lfs_hsm_restore(int argc, char **argv)
4049 {
4050         return lfs_hsm_request(argc, argv, HUA_RESTORE);
4051 }
4052
4053 static int lfs_hsm_release(int argc, char **argv)
4054 {
4055         return lfs_hsm_request(argc, argv, HUA_RELEASE);
4056 }
4057
4058 static int lfs_hsm_remove(int argc, char **argv)
4059 {
4060         return lfs_hsm_request(argc, argv, HUA_REMOVE);
4061 }
4062
4063 static int lfs_hsm_cancel(int argc, char **argv)
4064 {
4065         return lfs_hsm_request(argc, argv, HUA_CANCEL);
4066 }
4067
4068 static int lfs_swap_layouts(int argc, char **argv)
4069 {
4070         if (argc != 3)
4071                 return CMD_HELP;
4072
4073         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
4074                                   SWAP_LAYOUTS_KEEP_MTIME |
4075                                   SWAP_LAYOUTS_KEEP_ATIME);
4076 }
4077
4078 int main(int argc, char **argv)
4079 {
4080         int rc;
4081
4082         setlinebuf(stdout);
4083
4084         Parser_init("lfs > ", cmdlist);
4085
4086         if (argc > 1) {
4087                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
4088         } else {
4089                 rc = Parser_commands();
4090         }
4091
4092         return rc < 0 ? -rc : rc;
4093 }
4094
4095 #ifdef _LUSTRE_IDL_H_
4096 /* Everything we need here should be included by lustreapi.h. */
4097 # error "lfs should not depend on lustre_idl.h"
4098 #endif /* _LUSTRE_IDL_H_ */