4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
57 #include <sys/types.h>
64 #include <libcfs/util/string.h>
65 #include <libcfs/util/ioctl.h>
66 #include <libcfs/util/parser.h>
67 #include <lustre/lustreapi.h>
68 #include <lustre_ver.h>
69 #include <linux/lustre_param.h>
72 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
73 #endif /* !ARRAY_SIZE */
76 static int lfs_setstripe(int argc, char **argv);
77 static int lfs_find(int argc, char **argv);
78 static int lfs_getstripe(int argc, char **argv);
79 static int lfs_getdirstripe(int argc, char **argv);
80 static int lfs_setdirstripe(int argc, char **argv);
81 static int lfs_rmentry(int argc, char **argv);
82 static int lfs_osts(int argc, char **argv);
83 static int lfs_mdts(int argc, char **argv);
84 static int lfs_df(int argc, char **argv);
85 static int lfs_getname(int argc, char **argv);
86 static int lfs_check(int argc, char **argv);
87 #ifdef HAVE_SYS_QUOTA_H
88 static int lfs_setquota(int argc, char **argv);
89 static int lfs_quota(int argc, char **argv);
91 static int lfs_flushctx(int argc, char **argv);
92 static int lfs_cp(int argc, char **argv);
93 static int lfs_ls(int argc, char **argv);
94 static int lfs_poollist(int argc, char **argv);
95 static int lfs_changelog(int argc, char **argv);
96 static int lfs_changelog_clear(int argc, char **argv);
97 static int lfs_fid2path(int argc, char **argv);
98 static int lfs_path2fid(int argc, char **argv);
99 static int lfs_data_version(int argc, char **argv);
100 static int lfs_hsm_state(int argc, char **argv);
101 static int lfs_hsm_set(int argc, char **argv);
102 static int lfs_hsm_clear(int argc, char **argv);
103 static int lfs_hsm_action(int argc, char **argv);
104 static int lfs_hsm_archive(int argc, char **argv);
105 static int lfs_hsm_restore(int argc, char **argv);
106 static int lfs_hsm_release(int argc, char **argv);
107 static int lfs_hsm_remove(int argc, char **argv);
108 static int lfs_hsm_cancel(int argc, char **argv);
109 static int lfs_swap_layouts(int argc, char **argv);
110 static int lfs_mv(int argc, char **argv);
111 static int lfs_ladvise(int argc, char **argv);
112 static int lfs_list_commands(int argc, char **argv);
114 /* Setstripe and migrate share mostly the same parameters */
115 #define SSM_CMD_COMMON(cmd) \
116 "usage: "cmd" [--stripe-count|-c <stripe_count>]\n" \
117 " [--stripe-index|-i <start_ost_idx>]\n" \
118 " [--stripe-size|-S <stripe_size>]\n" \
119 " [--pool|-p <pool_name>]\n" \
120 " [--ost|-o <ost_indices>]\n" \
121 " [--component-end|-E <comp_end>]\n"
123 #define SSM_HELP_COMMON \
124 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n" \
125 "\t Can be specified with k, m or g (in KB, MB and GB\n" \
126 "\t respectively)\n" \
127 "\tstart_ost_idx: OST index of first stripe (-1 default)\n" \
128 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n" \
129 "\tpool_name: Name of OST pool to use (default none)\n" \
130 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
131 "\t Indices be specified in a format of:\n" \
132 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
134 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
135 "\t If --pool is set with --ost, then the OSTs\n" \
136 "\t must be the members of the pool." \
137 "\tcomp_end: Extent end of the component\n" \
138 "\t Can be specified with k, m or g (in KB, MB and GB\n" \
139 "\t respectively, -1 for EOF), it must be aligned with\n"\
140 "\t the stripe_size\n"
142 #define SETSTRIPE_USAGE \
143 SSM_CMD_COMMON("setstripe") \
144 " <directory|filename>\n" \
147 #define MIGRATE_USAGE \
148 SSM_CMD_COMMON("migrate ") \
150 " [--non-block|-n]\n" \
154 "\tblock: Block file access during data migration (default)\n" \
155 "\tnon-block: Abort migrations if concurrent access is detected\n" \
157 #define SETDIRSTRIPE_USAGE \
158 " [--mdt-count|-c stripe_count>\n" \
159 " [--mdt-index|-i mdt_index]\n" \
160 " [--mdt-hash|-H mdt_hash]\n" \
161 " [--default|-D] [--mode|-m mode] <dir>\n" \
162 "\tstripe_count: stripe count of the striped directory\n" \
163 "\tmdt_index: MDT index of first stripe\n" \
164 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
165 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
166 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
167 "\tdefault_stripe: set default dirstripe of the directory\n" \
168 "\tmode: the mode of the directory\n"
170 static const char *progname;
171 static bool file_lease_supported = true;
173 /* all available commands */
174 command_t cmdlist[] = {
175 {"setstripe", lfs_setstripe, 0,
176 "Create a new file with a specific striping pattern or\n"
177 "set the default striping pattern on an existing directory or\n"
178 "delete the default striping pattern from an existing directory or\n"
179 "add layout component(s) to an existing composite file or\n"
180 "delete specified component(s) from an existing composite file\n\n"
181 "To delete default striping from an existing directory:\n"
182 "usage: setstripe -d <directory>\n"
184 "To delete component(s) from an existing composite file:\n"
185 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
186 " [--component-flags|-F <comp_flags>]\n"
188 "\tcomp_id: Unique component ID\n"
189 "\tcomp_flags: 'init' indicating all instantiated components\n"
190 "\t '^init' indicating all uninstantiated components\n"
191 "\t-I and -F can't be specified at the same time\n"
193 "To add component(s) to an existing composite file:\n"
194 SSM_CMD_COMMON("setstripe --component-add")
196 "To create a file with specified striping/composite layout:\n"
198 {"getstripe", lfs_getstripe, 0,
199 "To list the striping info for a given file or files in a\n"
200 "directory or recursively for all files in a directory tree.\n"
201 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
202 " [--stripe-count|-c] [--stripe-index|-i]\n"
203 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
204 " [--mdt|-m] [--recursive|-r] [--raw|-R] [--yaml|-y]\n"
205 " [--layout|-L] [--fid|-F] [--generation|-g]\n"
206 " [--component-id[=comp_id]|-I[comp_id]]\n"
207 " [--component-flags[=comp_flags]]\n"
208 " [--component-count]\n"
209 " [--component-start[=[+-]comp_start]]\n"
210 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
211 " <directory|filename> ..."},
212 {"setdirstripe", lfs_setdirstripe, 0,
213 "To create a striped directory on a specified MDT. This can only\n"
214 "be done on MDT0 with the right of administrator.\n"
215 "usage: setdirstripe [OPTION] <directory>\n"
217 {"getdirstripe", lfs_getdirstripe, 0,
218 "To list the striping info for a given directory\n"
219 "or recursively for all directories in a directory tree.\n"
220 "usage: getdirstripe [--obd|-O <uuid>] [--mdt-count|-c]\n"
221 " [--mdt-index|-i] [--mdt-hash|-t]\n"
222 " [--recursive|-r] [--yaml|-y]\n"
223 " [--default|-D] <dir> ..."},
224 {"mkdir", lfs_setdirstripe, 0,
225 "To create a striped directory on a specified MDT. This can only\n"
226 "be done on MDT0 with the right of administrator.\n"
227 "usage: mkdir [OPTION] <directory>\n"
229 {"rm_entry", lfs_rmentry, 0,
230 "To remove the name entry of the remote directory. Note: This\n"
231 "command will only delete the name entry, i.e. the remote directory\n"
232 "will become inaccessable after this command. This can only be done\n"
233 "by the administrator\n"
234 "usage: rm_entry <dir>\n"},
235 {"pool_list", lfs_poollist, 0,
236 "List pools or pool OSTs\n"
237 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
238 {"find", lfs_find, 0,
239 "find files matching given attributes recursively in directory tree.\n"
240 "usage: find <directory|filename> ...\n"
241 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
242 " [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
243 " [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
244 " [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
245 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
246 " [[!] --stripe-count|-c [+-]<stripes>]\n"
247 " [[!] --stripe-index|-i <index,...>]\n"
248 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
249 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
250 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
251 " [[!] --projid <projid>]\n"
252 " [[!] --layout|-L released,raid0]\n"
253 " [[!] --component-count [+-]<comp_cnt>]\n"
254 " [[!] --component-start [+-]N[kMGTPE]]\n"
255 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
256 " [[!] --component-flags <comp_flags>]\n"
257 " [[!] --mdt-count|-T [+-]<stripes>]\n"
258 " [[!] --mdt-hash|-H <hashtype>\n"
259 "\t !: used before an option indicates 'NOT' requested attribute\n"
260 "\t -: used before a value indicates less than requested value\n"
261 "\t +: used before a value indicates more than requested value\n"
262 "\tmdt-hash: hash type of the striped directory.\n"
263 "\t fnv_1a_64 FNV-1a hash algorithm\n"
264 "\t all_char sum of characters % MDT_COUNT\n"},
265 {"check", lfs_check, 0,
266 "Display the status of MDS or OSTs (as specified in the command)\n"
267 "or all the servers (MDS and OSTs).\n"
268 "usage: check <osts|mds|servers>"},
269 {"osts", lfs_osts, 0, "list OSTs connected to client "
270 "[for specified path only]\n" "usage: osts [path]"},
271 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
272 "[for specified path only]\n" "usage: mdts [path]"},
274 "report filesystem disk space usage or inodes usage"
275 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
276 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
277 {"getname", lfs_getname, 0, "list instances and specified mount points "
278 "[for specified path only]\n"
279 "Usage: getname [-h]|[path ...] "},
280 #ifdef HAVE_SYS_QUOTA_H
281 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
282 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
283 " -b <block-softlimit> -B <block-hardlimit>\n"
284 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
285 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
286 " [--block-softlimit <block-softlimit>]\n"
287 " [--block-hardlimit <block-hardlimit>]\n"
288 " [--inode-softlimit <inode-softlimit>]\n"
289 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
290 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
291 " [--block-grace <block-grace>]\n"
292 " [--inode-grace <inode-grace>] <filesystem>\n"
293 " -b can be used instead of --block-softlimit/--block-grace\n"
294 " -B can be used instead of --block-hardlimit\n"
295 " -i can be used instead of --inode-softlimit/--inode-grace\n"
296 " -I can be used instead of --inode-hardlimit\n\n"
297 "Note: The total quota space will be split into many qunits and\n"
298 " balanced over all server targets, the minimal qunit size is\n"
299 " 1M bytes for block space and 1K inodes for inode space.\n\n"
300 " Quota space rebalancing process will stop when this mininum\n"
301 " value is reached. As a result, quota exceeded can be returned\n"
302 " while many targets still have 1MB or 1K inodes of spare\n"
304 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
305 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
307 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
308 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
310 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
311 "usage: flushctx [-k] [mountpoint...]"},
313 "Remote user copy files and directories.\n"
314 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
316 "Remote user list directory contents.\n"
317 "usage: ls [OPTION]... [FILE]..."},
318 {"changelog", lfs_changelog, 0,
319 "Show the metadata changes on an MDT."
320 "\nusage: changelog <mdtname> [startrec [endrec]]"},
321 {"changelog_clear", lfs_changelog_clear, 0,
322 "Indicate that old changelog records up to <endrec> are no longer of "
323 "interest to consumer <id>, allowing the system to free up space.\n"
324 "An <endrec> of 0 means all records.\n"
325 "usage: changelog_clear <mdtname> <id> <endrec>"},
326 {"fid2path", lfs_fid2path, 0,
327 "Resolve the full path(s) for given FID(s). For a specific hardlink "
328 "specify link number <linkno>.\n"
329 /* "For a historical link name, specify changelog record <recno>.\n" */
330 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
331 /* [ --rec <recno> ] */ },
332 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
333 "usage: path2fid [--parents] <path> ..."},
334 {"data_version", lfs_data_version, 0, "Display file data version for "
335 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
336 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
337 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
338 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
339 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
340 "[--archived] [--lost] <file> ..."},
341 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
343 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
344 "[--archived] [--lost] <file> ..."},
345 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
346 "given files.\n" "usage: hsm_action <file> ..."},
347 {"hsm_archive", lfs_hsm_archive, 0,
348 "Archive file to external storage.\n"
349 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
351 {"hsm_restore", lfs_hsm_restore, 0,
352 "Restore file from external storage.\n"
353 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
354 {"hsm_release", lfs_hsm_release, 0,
355 "Release files from Lustre.\n"
356 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
357 {"hsm_remove", lfs_hsm_remove, 0,
358 "Remove file copy from external storage.\n"
359 "usage: hsm_remove [--filelist FILELIST] [--data DATA]\n"
360 " [--mntpath MOUNTPATH] [--archive NUM] <file|FID> ...\n"
362 "Note: To remove files from the archive that have been deleted on\n"
363 "Lustre, set mntpath and optionally archive. In that case, all the\n"
364 "positional arguments and entries in the file list must be FIDs."
366 {"hsm_cancel", lfs_hsm_cancel, 0,
367 "Cancel requests related to specified files.\n"
368 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
369 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
370 "usage: swap_layouts <path1> <path2>"},
371 {"migrate", lfs_setstripe, 0,
372 "migrate a directory between MDTs.\n"
373 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
375 "\tmdt_idx: index of the destination MDT\n"
377 "migrate file objects from one OST "
378 "layout\nto another (may be not safe with concurent writes).\n"
380 "[--stripe-count|-c] <stripe_count>\n"
381 " [--stripe-index|-i] <start_ost_index>\n"
382 " [--stripe-size|-S] <stripe_size>\n"
383 " [--pool|-p] <pool_name>\n"
384 " [--ost-list|-o] <ost_indices>\n"
386 " [--non-block|-n]\n"
387 " <file|directory>\n"
388 "\tstripe_count: number of OSTs to stripe a file over\n"
389 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
390 "\tstripe_size: number of bytes to store before moving to the next OST\n"
391 "\tpool_name: name of the predefined pool of OSTs\n"
392 "\tost_indices: OSTs to stripe over, in order\n"
393 "\tblock: wait for the operation to return before continuing\n"
394 "\tnon-block: do not wait for the operation to return.\n"},
396 "To move directories between MDTs. This command is deprecated, "
397 "use \"migrate\" instead.\n"
398 "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
400 {"ladvise", lfs_ladvise, 0,
401 "Provide servers with advice about access patterns for a file.\n"
402 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
403 " [--background|-b]\n"
404 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
406 {"help", Parser_help, 0, "help"},
407 {"exit", Parser_quit, 0, "quit"},
408 {"quit", Parser_quit, 0, "quit"},
409 {"--version", Parser_version, 0,
410 "output build version of the utility and exit"},
411 {"--list-commands", lfs_list_commands, 0,
412 "list commands supported by the utility and exit"},
417 #define MIGRATION_NONBLOCK 1
419 static int check_hashtype(const char *hashtype)
423 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
424 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
431 * Internal helper for migrate_copy_data(). Check lease and report error if
434 * \param[in] fd File descriptor on which to check the lease.
435 * \param[out] lease_broken Set to true if the lease was broken.
436 * \param[in] group_locked Whether a group lock was taken or not.
437 * \param[in] path Name of the file being processed, for error
440 * \retval 0 Migration can keep on going.
441 * \retval -errno Error occurred, abort migration.
443 static int check_lease(int fd, bool *lease_broken, bool group_locked,
448 if (!file_lease_supported)
451 rc = llapi_lease_check(fd);
453 return 0; /* llapi_check_lease returns > 0 on success. */
456 fprintf(stderr, "%s: cannot migrate '%s': file busy\n",
458 rc = rc ? rc : -EAGAIN;
460 fprintf(stderr, "%s: external attempt to access file '%s' "
461 "blocked until migration ends.\n", progname, path);
464 *lease_broken = true;
468 static int migrate_copy_data(int fd_src, int fd_dst, size_t buf_size,
469 bool group_locked, const char *fname)
478 bool lease_broken = false;
480 /* Use a page-aligned buffer for direct I/O */
481 rc = posix_memalign(&buf, getpagesize(), buf_size);
486 /* read new data only if we have written all
487 * previously read data */
490 rc = check_lease(fd_src, &lease_broken,
491 group_locked, fname);
495 rsize = read(fd_src, buf, buf_size);
498 fprintf(stderr, "%s: %s: read failed: %s\n",
499 progname, fname, strerror(-rc));
509 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
513 "%s: %s: write failed on volatile: %s\n",
514 progname, fname, strerror(-rc));
524 fprintf(stderr, "%s: %s: fsync failed: %s\n",
525 progname, fname, strerror(-rc));
533 static int migrate_copy_timestamps(int fdv, const struct stat *st)
535 struct timeval tv[2] = {
536 {.tv_sec = st->st_atime},
537 {.tv_sec = st->st_mtime}
540 return futimes(fdv, tv);
543 static int migrate_block(int fd, int fdv, const struct stat *st,
544 size_t buf_size, const char *name)
551 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
553 fprintf(stderr, "%s: %s: cannot get dataversion: %s\n",
554 progname, name, strerror(-rc));
562 /* The grouplock blocks all concurrent accesses to the file.
563 * It has to be taken after llapi_get_data_version as it would
565 rc = llapi_group_lock(fd, gid);
567 fprintf(stderr, "%s: %s: cannot get group lock: %s\n",
568 progname, name, strerror(-rc));
572 rc = migrate_copy_data(fd, fdv, buf_size, true, name);
574 fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
578 /* Make sure we keep original atime/mtime values */
579 rc = migrate_copy_timestamps(fdv, st);
581 fprintf(stderr, "%s: %s: timestamp copy failed\n",
587 * for a migration we need to check data version on file did
590 * Pass in gid=0 since we already own grouplock. */
591 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
592 SWAP_LAYOUTS_CHECK_DV1);
594 fprintf(stderr, "%s: %s: dataversion changed during copy, "
595 "migration aborted\n", progname, name);
598 fprintf(stderr, "%s: %s: cannot swap layouts: %s\n", progname,
599 name, strerror(-rc));
604 rc2 = llapi_group_unlock(fd, gid);
605 if (rc2 < 0 && rc == 0) {
606 fprintf(stderr, "%s: %s: putting group lock failed: %s\n",
607 progname, name, strerror(-rc2));
614 static int migrate_nonblock(int fd, int fdv, const struct stat *st,
615 size_t buf_size, const char *name)
621 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
623 fprintf(stderr, "%s: %s: cannot get data version: %s\n",
624 progname, name, strerror(-rc));
628 rc = migrate_copy_data(fd, fdv, buf_size, false, name);
630 fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
634 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
636 fprintf(stderr, "%s: %s: cannot get data version: %s\n",
637 progname, name, strerror(-rc));
643 fprintf(stderr, "%s: %s: data version changed during "
649 /* Make sure we keep original atime/mtime values */
650 rc = migrate_copy_timestamps(fdv, st);
652 fprintf(stderr, "%s: %s: timestamp copy failed\n",
657 /* Atomically put lease, swap layouts and close.
658 * for a migration we need to check data version on file did
660 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
662 fprintf(stderr, "%s: %s: cannot swap layouts: %s\n",
663 progname, name, strerror(-rc));
670 static int lfs_component_set(char *fname, int comp_id, __u32 flags)
675 static int lfs_component_del(char *fname, __u32 comp_id, __u32 flags)
679 if (flags != 0 && comp_id != 0)
682 /* LCME_FL_INIT is the only supported flag in PFL */
684 if (flags & ~LCME_KNOWN_FLAGS) {
685 fprintf(stderr, "Invalid component flags %#x\n", flags);
688 } else if (comp_id > LCME_ID_MAX) {
689 fprintf(stderr, "Invalid component id %u\n", comp_id);
693 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
695 fprintf(stderr, "Delete component %#x from %s failed. %s\n",
696 comp_id, fname, strerror(errno));
700 static int lfs_component_add(char *fname, struct llapi_layout *layout)
707 rc = llapi_layout_file_comp_add(fname, layout);
709 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
710 fname, strerror(errno));
714 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
715 struct llapi_layout *layout)
723 fd = lstat(fname, &st);
724 if (fd == 0 && S_ISDIR(st.st_mode))
725 open_flags = O_DIRECTORY | O_RDONLY;
727 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
729 fprintf(stderr, "%s %s failed. %s\n",
730 S_ISDIR(st.st_mode) ?
731 "Set default composite layout to " :
732 "Create composite file",
733 fname, strerror(errno));
737 static int lfs_migrate(char *name, __u64 migration_flags,
738 struct llapi_stripe_param *param,
739 struct llapi_layout *layout)
743 char parent[PATH_MAX];
746 char volatile_file[sizeof(parent) +
747 LUSTRE_VOLATILE_HDR_LEN +
748 2 * sizeof(mdt_index) +
749 2 * sizeof(random_value) + 4];
752 struct lov_user_md *lum = NULL;
754 int buf_size = 1024 * 1024 * 4;
755 bool have_lease_rdlck = false;
759 /* find the right size for the IO and allocate the buffer */
760 lum_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
761 lum = malloc(lum_size);
767 rc = llapi_file_get_stripe(name, lum);
768 /* failure can happen for many reasons and some may be not real errors
770 * in case of a real error, a later call will fail with better
771 * error management */
773 if ((lum->lmm_magic == LOV_USER_MAGIC_V1 ||
774 lum->lmm_magic == LOV_USER_MAGIC_V3) &&
775 lum->lmm_stripe_size != 0)
776 buf_size = lum->lmm_stripe_size;
779 /* open file, direct io */
780 /* even if the file is only read, WR mode is nedeed to allow
781 * layout swap on fd */
782 fd = open(name, O_RDWR | O_DIRECT);
785 fprintf(stderr, "%s: %s: cannot open: %s\n", progname, name,
790 if (file_lease_supported) {
791 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
792 if (rc == -EOPNOTSUPP) {
793 /* Older servers do not support file lease.
794 * Disable related checks. This opens race conditions
795 * as explained in LU-4840 */
796 file_lease_supported = false;
798 fprintf(stderr, "%s: %s: cannot get open lease: %s\n",
799 progname, name, strerror(-rc));
802 have_lease_rdlck = true;
806 /* search for file directory pathname */
807 if (strlen(name) > sizeof(parent)-1) {
811 strncpy(parent, name, sizeof(parent));
812 ptr = strrchr(parent, '/');
814 if (getcwd(parent, sizeof(parent)) == NULL) {
825 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
827 fprintf(stderr, "%s: %s: cannot get MDT index: %s\n",
828 progname, name, strerror(-rc));
833 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
834 mode_t open_mode = S_IRUSR | S_IWUSR;
836 random_value = random();
837 rc = snprintf(volatile_file, sizeof(volatile_file),
838 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
839 mdt_index, random_value);
840 if (rc >= sizeof(volatile_file)) {
845 /* create, open a volatile file, use caching (ie no directio) */
847 fdv = llapi_file_open_param(volatile_file, open_flags,
849 else if (layout != NULL)
850 fdv = lfs_component_create(volatile_file, open_flags,
854 } while (fdv == -EEXIST);
858 fprintf(stderr, "%s: %s: cannot create volatile file in"
860 progname, parent, strerror(-rc));
864 /* In case the MDT does not support creation of volatile files
865 * we should try to unlink it. */
866 (void)unlink(volatile_file);
868 /* Not-owner (root?) special case.
869 * Need to set owner/group of volatile file like original.
870 * This will allow to pass related check during layout_swap.
875 fprintf(stderr, "%s: %s: cannot stat: %s\n", progname, name,
879 rc = fstat(fdv, &stv);
882 fprintf(stderr, "%s: %s: cannot stat: %s\n", progname,
883 volatile_file, strerror(errno));
886 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
887 rc = fchown(fdv, st.st_uid, st.st_gid);
890 fprintf(stderr, "%s: %s: cannot chown: %s\n", progname,
891 name, strerror(errno));
896 if (migration_flags & MIGRATION_NONBLOCK && file_lease_supported) {
897 rc = migrate_nonblock(fd, fdv, &st, buf_size, name);
899 have_lease_rdlck = false;
900 fdv = -1; /* The volatile file is closed as we put the
901 * lease in non-blocking mode. */
904 /* Blocking mode (forced if servers do not support file lease).
905 * It is also the default mode, since we cannot distinguish
906 * between a broken lease and a server that does not support
907 * atomic swap/close (LU-6785) */
908 rc = migrate_block(fd, fdv, &st, buf_size, name);
912 if (have_lease_rdlck)
929 * Parse a string containing an OST index list into an array of integers.
931 * The input string contains a comma delimited list of individual
932 * indices and ranges, for example "1,2-4,7". Add the indices into the
933 * \a osts array and remove duplicates.
935 * \param[out] osts array to store indices in
936 * \param[in] size size of \a osts array
937 * \param[in] offset starting index in \a osts
938 * \param[in] arg string containing OST index list
940 * \retval positive number of indices in \a osts
941 * \retval -EINVAL unable to parse \a arg
943 static int parse_targets(__u32 *osts, int size, int offset, char *arg)
947 int slots = size - offset;
955 while (!end_of_loop) {
963 ptr = strchrnul(arg, ',');
965 end_of_loop = *ptr == '\0';
968 start_index = strtol(arg, &endptr, 0);
969 if (endptr == arg) /* no data at all */
971 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
976 end_index = start_index;
977 if (*endptr == '-') {
978 end_index = strtol(endptr + 1, &endptr, 0);
981 if (end_index < start_index)
985 for (i = start_index; i <= end_index && slots > 0; i++) {
988 /* remove duplicate */
989 for (j = 0; j < offset; j++) {
993 if (j == offset) { /* no duplicate */
998 if (slots == 0 && i < end_index)
1006 if (!end_of_loop && ptr != NULL)
1009 return rc < 0 ? rc : nr;
1012 static int verify_pool_name(char *prog_name, char *pool_name)
1016 if (pool_name == NULL)
1019 ptr = strchr(pool_name, '.');
1020 if (ptr != NULL && ptr == pool_name) {
1021 fprintf(stderr, "error: %s: fsname is empty in pool name '%s'\n",
1022 prog_name, pool_name);
1029 struct lfs_setstripe_args {
1030 unsigned long long lsa_comp_end;
1031 unsigned long long lsa_stripe_size;
1032 int lsa_stripe_count;
1034 __u32 lsa_comp_flags;
1037 char *lsa_pool_name;
1040 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1042 memset(lsa, 0, sizeof(*lsa));
1043 lsa->lsa_stripe_off = -1;
1046 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1048 return (lsa->lsa_stripe_size != 0 || lsa->lsa_stripe_count != 0 ||
1049 lsa->lsa_stripe_off != -1 || lsa->lsa_pool_name != NULL ||
1050 lsa->lsa_comp_end != 0);
1053 static int comp_args_to_layout(struct llapi_layout **composite,
1054 struct lfs_setstripe_args *lsa)
1056 struct llapi_layout *layout = *composite;
1057 uint64_t prev_end = 0;
1060 if (layout == NULL) {
1061 layout = llapi_layout_alloc();
1062 if (layout == NULL) {
1063 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1067 *composite = layout;
1071 /* Get current component extent, current component
1072 * must be the tail component. */
1073 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
1075 fprintf(stderr, "Get comp extent failed. %s\n",
1080 rc = llapi_layout_comp_add(layout);
1082 fprintf(stderr, "Add component failed. %s\n",
1088 rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
1090 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
1091 prev_end, lsa->lsa_comp_end, strerror(errno));
1095 if (lsa->lsa_stripe_size != 0) {
1096 rc = llapi_layout_stripe_size_set(layout,
1097 lsa->lsa_stripe_size);
1099 fprintf(stderr, "Set stripe size %llu failed. %s\n",
1100 lsa->lsa_stripe_size, strerror(errno));
1105 if (lsa->lsa_stripe_count != 0) {
1106 rc = llapi_layout_stripe_count_set(layout,
1107 lsa->lsa_stripe_count == -1 ?
1109 lsa->lsa_stripe_count);
1111 fprintf(stderr, "Set stripe count %d failed. %s\n",
1112 lsa->lsa_stripe_count, strerror(errno));
1117 if (lsa->lsa_pool_name != NULL) {
1118 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
1120 fprintf(stderr, "Set pool name: %s failed. %s\n",
1121 lsa->lsa_pool_name, strerror(errno));
1126 if (lsa->lsa_nr_osts > 0) {
1127 if (lsa->lsa_stripe_count > 0 &&
1128 lsa->lsa_nr_osts != lsa->lsa_stripe_count) {
1129 fprintf(stderr, "stripe_count(%d) != nr_osts(%d)\n",
1130 lsa->lsa_stripe_count, lsa->lsa_nr_osts);
1133 for (i = 0; i < lsa->lsa_nr_osts; i++) {
1134 rc = llapi_layout_ost_index_set(layout, i,
1139 } else if (lsa->lsa_stripe_off != -1) {
1140 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
1143 fprintf(stderr, "Set ost index %d failed. %s\n",
1144 i, strerror(errno));
1151 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
1152 * end of the last component in the existing file, and adjust the
1153 * first extent start of the components to be added accordingly. */
1154 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
1156 struct llapi_layout *head;
1157 uint64_t start, end, stripe_size, prev_end = 0;
1164 head = llapi_layout_get_by_path(fname, 0);
1166 fprintf(stderr, "Read layout from %s failed. %s\n",
1167 fname, strerror(errno));
1169 } else if (errno == ENODATA) {
1170 /* file without LOVEA, this component-add will be turned
1171 * into a component-create. */
1172 llapi_layout_free(head);
1174 } else if (!llapi_layout_is_composite(head)) {
1175 fprintf(stderr, "'%s' isn't a composite file.\n",
1177 llapi_layout_free(head);
1181 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
1183 fprintf(stderr, "Get prev extent failed. %s\n",
1185 llapi_layout_free(head);
1189 llapi_layout_free(head);
1191 /* Make sure we use the first component of the layout to be added. */
1192 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1194 fprintf(stderr, "Move component cursor failed. %s\n",
1199 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1201 fprintf(stderr, "Get extent failed. %s\n", strerror(errno));
1205 if (start > prev_end || end <= prev_end) {
1206 fprintf(stderr, "First extent to be set [%lu, %lu) isn't "
1207 "adjacent with the existing file extent end: %lu\n",
1208 start, end, prev_end);
1212 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
1214 fprintf(stderr, "Get stripe size failed. %s\n",
1219 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
1220 (prev_end & (stripe_size - 1))) {
1221 fprintf(stderr, "Stripe size %lu not aligned with %lu\n",
1222 stripe_size, prev_end);
1226 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
1228 fprintf(stderr, "Set component extent [%lu, %lu) failed. %s\n",
1229 prev_end, end, strerror(errno));
1236 static inline bool comp_flags_is_neg(__u32 flags)
1238 return flags & LCME_FL_NEG;
1241 static inline void comp_flags_set_neg(__u32 *flags)
1243 *flags |= LCME_FL_NEG;
1246 static inline void comp_flags_clear_neg(__u32 *flags)
1248 *flags &= ~LCME_FL_NEG;
1251 static int comp_str2flags(__u32 *flags, char *string)
1254 __u32 neg_flags = 0;
1260 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1264 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1265 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1266 const char *comp_name = comp_flags_table[i].cfn_name;
1268 if (strcmp(name, comp_name) == 0) {
1269 *flags |= comp_flag;
1271 } else if (strncmp(name, "^", 1) == 0 &&
1272 strcmp(name + 1, comp_name) == 0) {
1273 neg_flags |= comp_flag;
1278 llapi_printf(LLAPI_MSG_ERROR, "Component flag "
1279 "'%s' is not supported.\n", name);
1284 if (*flags == 0 && neg_flags == 0)
1286 /* don't support mixed flags for now */
1287 if (*flags && neg_flags)
1292 comp_flags_set_neg(flags);
1298 static inline bool arg_is_eof(char *arg)
1300 return !strncmp(arg, "-1", strlen("-1")) ||
1301 !strncmp(arg, "EOF", strlen("EOF")) ||
1302 !strncmp(arg, "eof", strlen("eof"));
1317 static int lfs_setstripe(int argc, char **argv)
1319 struct lfs_setstripe_args lsa;
1320 struct llapi_stripe_param *param = NULL;
1321 struct find_param migrate_mdt_param = {
1331 char *mdt_idx_arg = NULL;
1332 unsigned long long size_units = 1;
1333 bool migrate_mode = false;
1334 bool migration_block = false;
1335 __u64 migration_flags = 0;
1336 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
1337 int comp_del = 0, comp_set = 0;
1340 struct llapi_layout *layout = NULL;
1342 struct option long_opts[] = {
1343 /* --block is only valid in migrate mode */
1344 { .val = 'b', .name = "block", .has_arg = no_argument},
1345 { .val = LFS_COMP_ADD_OPT,
1346 .name = "comp-add", .has_arg = no_argument},
1347 { .val = LFS_COMP_ADD_OPT,
1348 .name = "component-add",
1349 .has_arg = no_argument},
1350 { .val = LFS_COMP_DEL_OPT,
1351 .name = "comp-del", .has_arg = no_argument},
1352 { .val = LFS_COMP_DEL_OPT,
1353 .name = "component-del",
1354 .has_arg = no_argument},
1355 { .val = LFS_COMP_FLAGS_OPT,
1356 .name = "comp-flags", .has_arg = required_argument},
1357 { .val = LFS_COMP_FLAGS_OPT,
1358 .name = "component-flags",
1359 .has_arg = required_argument},
1360 { .val = LFS_COMP_SET_OPT,
1361 .name = "comp-set", .has_arg = no_argument},
1362 { .val = LFS_COMP_SET_OPT,
1363 .name = "component-set",
1364 .has_arg = no_argument},
1365 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
1366 /* This formerly implied "stripe-count", but was explicitly
1367 * made "stripe-count" for consistency with other options,
1368 * and to separate it from "mdt-count" when DNE arrives. */
1369 { .val = 'c', .name = "count", .has_arg = required_argument },
1371 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
1372 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
1373 { .val = 'd', .name = "delete", .has_arg = no_argument},
1374 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
1375 { .val = 'E', .name = "component-end",
1376 .has_arg = required_argument},
1377 /* dirstripe {"mdt-hash", required_argument, 0, 'H'}, */
1378 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
1379 /* This formerly implied "stripe-index", but was explicitly
1380 * made "stripe-index" for consistency with other options,
1381 * and to separate it from "mdt-index" when DNE arrives. */
1382 { .val = 'i', .name = "index", .has_arg = required_argument },
1384 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
1385 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
1386 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
1387 { .val = 'I', .name = "component-id", .has_arg = required_argument},
1388 { .val = 'm', .name = "mdt", .has_arg = required_argument},
1389 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
1390 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
1391 /* --non-block is only valid in migrate mode */
1392 { .val = 'n', .name = "non-block", .has_arg = no_argument},
1393 { .val = 'o', .name = "ost", .has_arg = required_argument},
1394 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1395 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
1396 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
1398 { .val = 'p', .name = "pool", .has_arg = required_argument },
1399 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
1400 /* This formerly implied "--stripe-size", but was confusing
1401 * with "lfs find --size|-s", which means "file size", so use
1402 * the consistent "--stripe-size|-S" for all commands. */
1403 { .val = 's', .name = "size", .has_arg = required_argument },
1405 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
1406 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
1407 /* dirstripe {"mdt-count", required_argument, 0, 'T'}, */
1408 /* --verbose is only valid in migrate mode */
1409 { .val = 'v', .name = "verbose", .has_arg = no_argument },
1410 { .val = LFS_COMP_ADD_OPT,
1411 .name = "component-add",
1412 .has_arg = no_argument },
1413 { .val = LFS_COMP_DEL_OPT,
1414 .name = "component-del",
1415 .has_arg = no_argument },
1416 { .val = LFS_COMP_FLAGS_OPT,
1417 .name = "component-flags",
1418 .has_arg = required_argument },
1419 { .val = LFS_COMP_SET_OPT,
1420 .name = "component-set",
1421 .has_arg = no_argument },
1424 setstripe_args_init(&lsa);
1426 if (strcmp(argv[0], "migrate") == 0)
1427 migrate_mode = true;
1429 while ((c = getopt_long(argc, argv, "bc:dE:i:I:m:no:p:s:S:v",
1430 long_opts, NULL)) >= 0) {
1435 case LFS_COMP_ADD_OPT:
1438 case LFS_COMP_DEL_OPT:
1441 case LFS_COMP_FLAGS_OPT:
1442 result = comp_str2flags(&lsa.lsa_comp_flags, optarg);
1444 fprintf(stderr, "error: %s: bad comp flags "
1445 "'%s'\n", argv[0], optarg);
1449 case LFS_COMP_SET_OPT:
1453 if (!migrate_mode) {
1454 fprintf(stderr, "--block is valid only for"
1458 migration_block = true;
1461 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1462 if (strcmp(argv[optind - 1], "--count") == 0)
1463 fprintf(stderr, "warning: '--count' deprecated"
1464 ", use '--stripe-count' instead\n");
1466 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
1468 fprintf(stderr, "error: %s: bad stripe count "
1469 "'%s'\n", argv[0], optarg);
1474 /* delete the default striping pattern */
1478 if (lsa.lsa_comp_end != 0) {
1479 result = comp_args_to_layout(&layout, &lsa);
1483 setstripe_args_init(&lsa);
1486 if (arg_is_eof(optarg)) {
1487 lsa.lsa_comp_end = LUSTRE_EOF;
1489 result = llapi_parse_size(optarg,
1493 fprintf(stderr, "error: %s: "
1494 "bad component end '%s'\n",
1501 if (strcmp(argv[optind - 1], "--index") == 0)
1502 fprintf(stderr, "warning: '--index' deprecated"
1503 ", use '--stripe-index' instead\n");
1504 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
1506 fprintf(stderr, "error: %s: bad stripe offset "
1507 "'%s'\n", argv[0], optarg);
1512 comp_id = strtoul(optarg, &end, 0);
1513 if (*end != '\0' || comp_id == 0 ||
1514 comp_id > LCME_ID_MAX) {
1515 fprintf(stderr, "error: %s: bad comp ID "
1516 "'%s'\n", argv[0], optarg);
1521 if (!migrate_mode) {
1522 fprintf(stderr, "--mdt-index is valid only for"
1526 mdt_idx_arg = optarg;
1529 if (!migrate_mode) {
1530 fprintf(stderr, "--non-block is valid only for"
1534 migration_flags |= MIGRATION_NONBLOCK;
1537 lsa.lsa_nr_osts = parse_targets(osts,
1538 sizeof(osts) / sizeof(__u32),
1539 lsa.lsa_nr_osts, optarg);
1540 if (lsa.lsa_nr_osts < 0) {
1542 "error: %s: bad OST indices '%s'\n",
1547 lsa.lsa_osts = osts;
1548 if (lsa.lsa_stripe_off == -1)
1549 lsa.lsa_stripe_off = osts[0];
1552 result = verify_pool_name(argv[0], optarg);
1555 lsa.lsa_pool_name = optarg;
1557 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
1559 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1560 fprintf(stderr, "warning: '--size|-s' deprecated, "
1561 "use '--stripe-size|-S' instead\n");
1563 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0) */
1565 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
1568 fprintf(stderr, "error: %s: bad stripe size "
1569 "'%s'\n", argv[0], optarg);
1574 if (!migrate_mode) {
1575 fprintf(stderr, "--verbose is valid only for"
1579 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
1586 fname = argv[optind];
1588 if (lsa.lsa_comp_end != 0) {
1589 result = comp_args_to_layout(&layout, &lsa);
1594 if (optind == argc) {
1595 fprintf(stderr, "error: %s: missing filename|dirname\n",
1600 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
1601 * altered by user space tool, so we don't need to support the
1602 * --component-set for this moment. */
1603 if (comp_set != 0) {
1604 fprintf(stderr, "error: %s: --component-set isn't supported.\n",
1609 if ((delete + comp_set + comp_del + comp_add) > 1) {
1610 fprintf(stderr, "error: %s: can't specify --component-set, "
1611 "--component-del, --component-add or -d together\n",
1616 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
1617 lsa.lsa_comp_flags != 0 || layout != NULL)) {
1618 fprintf(stderr, "error: %s: can't specify -d with "
1619 "-s, -c, -o, -p, -I, -F or -E options\n",
1624 if ((comp_set || comp_del) &&
1625 (setstripe_args_specified(&lsa) || layout != NULL)) {
1626 fprintf(stderr, "error: %s: can't specify --component-del or "
1627 "--component-set with -s, -c, -o, -p or -E options.\n",
1632 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
1633 fprintf(stderr, "error: %s: can't specify both -I and -F for "
1634 "--component-del option.\n", argv[0]);
1638 if (comp_add || comp_del) {
1641 result = lstat(fname, &st);
1642 if (result == 0 && S_ISDIR(st.st_mode)) {
1643 fprintf(stderr, "error: %s: can't use --component-add "
1644 "or --component-del for directory.\n",
1651 if (layout == NULL) {
1652 fprintf(stderr, "error: %s: -E option must be present"
1653 "in --component-add mode.\n", argv[0]);
1656 result = adjust_first_extent(fname, layout);
1657 if (result == -ENODATA)
1659 else if (result != 0)
1663 if (mdt_idx_arg != NULL && optind > 3) {
1664 fprintf(stderr, "error: %s: cannot specify -m with other "
1665 "options\n", argv[0]);
1669 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
1671 "error: %s: cannot specify --non-block and --block\n",
1676 if (!comp_del && !comp_set && comp_id != 0) {
1677 fprintf(stderr, "error: %s: -I can only be used with "
1678 "--component-del.\n", argv[0]);
1682 if (mdt_idx_arg != NULL) {
1683 /* initialize migrate mdt parameters */
1684 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
1686 fprintf(stderr, "error: %s: bad MDT index '%s'\n",
1687 argv[0], mdt_idx_arg);
1690 migrate_mdt_param.fp_migrate = 1;
1691 } else if (layout == NULL) {
1692 /* initialize stripe parameters */
1693 param = calloc(1, offsetof(typeof(*param),
1694 lsp_osts[lsa.lsa_nr_osts]));
1695 if (param == NULL) {
1696 fprintf(stderr, "error: %s: %s\n", argv[0],
1701 param->lsp_stripe_size = lsa.lsa_stripe_size;
1702 param->lsp_stripe_offset = lsa.lsa_stripe_off;
1703 param->lsp_stripe_count = lsa.lsa_stripe_count;
1704 param->lsp_stripe_pattern = 0;
1705 param->lsp_pool = lsa.lsa_pool_name;
1706 param->lsp_is_specific = false;
1707 if (lsa.lsa_nr_osts > 0) {
1708 if (lsa.lsa_stripe_count > 0 &&
1709 lsa.lsa_nr_osts != lsa.lsa_stripe_count) {
1710 fprintf(stderr, "error: %s: stripe count '%d' "
1711 "doesn't match the number of OSTs: %d\n"
1712 , argv[0], lsa.lsa_stripe_count,
1718 param->lsp_is_specific = true;
1719 param->lsp_stripe_count = lsa.lsa_nr_osts;
1720 memcpy(param->lsp_osts, osts,
1721 sizeof(*osts) * lsa.lsa_nr_osts);
1725 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
1727 if (mdt_idx_arg != NULL) {
1728 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
1729 op = "migrate mdt objects of";
1730 } else if (migrate_mode) {
1731 result = lfs_migrate(fname, migration_flags, param,
1733 op = "migrate ost objects of";
1734 } else if (comp_set != 0) {
1735 result = lfs_component_set(fname, comp_id,
1736 lsa.lsa_comp_flags);
1737 op = "modify component flags of";
1738 } else if (comp_del != 0) {
1739 result = lfs_component_del(fname, comp_id,
1740 lsa.lsa_comp_flags);
1741 op = "delete component of";
1742 } else if (comp_add != 0) {
1743 result = lfs_component_add(fname, layout);
1744 op = "add component to";
1745 } else if (layout != NULL) {
1746 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
1752 op = "create composite";
1754 result = llapi_file_open_param(fname,
1761 op = "create striped";
1764 /* Save the first error encountered. */
1767 fprintf(stderr, "error: %s: %s file '%s' failed: %s\n",
1769 lsa.lsa_pool_name != NULL && result == EINVAL ?
1770 "OST not in pool?" : strerror(errno));
1776 llapi_layout_free(layout);
1779 llapi_layout_free(layout);
1783 static int lfs_poollist(int argc, char **argv)
1788 return llapi_poollist(argv[1]);
1791 static int set_time(time_t *time, time_t *set, char *str)
1798 else if (str[0] == '-')
1804 t = strtol(str, NULL, 0);
1805 if (*time < t * 24 * 60 * 60) {
1808 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
1812 *set = *time - t * 24 * 60 * 60;
1815 static int name2uid(unsigned int *id, const char *name)
1817 struct passwd *passwd;
1819 passwd = getpwnam(name);
1822 *id = passwd->pw_uid;
1827 static int name2gid(unsigned int *id, const char *name)
1829 struct group *group;
1831 group = getgrnam(name);
1834 *id = group->gr_gid;
1839 static inline int name2projid(unsigned int *id, const char *name)
1844 static int uid2name(char **name, unsigned int id)
1846 struct passwd *passwd;
1848 passwd = getpwuid(id);
1851 *name = passwd->pw_name;
1856 static inline int gid2name(char **name, unsigned int id)
1858 struct group *group;
1860 group = getgrgid(id);
1863 *name = group->gr_name;
1868 static int name2layout(__u32 *layout, char *name)
1873 for (ptr = name; ; ptr = NULL) {
1874 lyt = strtok(ptr, ",");
1877 if (strcmp(lyt, "released") == 0)
1878 *layout |= LOV_PATTERN_F_RELEASED;
1879 else if (strcmp(lyt, "raid0") == 0)
1880 *layout |= LOV_PATTERN_RAID0;
1887 static int lfs_find(int argc, char **argv)
1892 struct find_param param = {
1896 struct option long_opts[] = {
1897 { .val = 'A', .name = "atime", .has_arg = required_argument },
1898 { .val = LFS_COMP_COUNT_OPT,
1899 .name = "comp-count", .has_arg = required_argument },
1900 { .val = LFS_COMP_COUNT_OPT,
1901 .name = "component-count",
1902 .has_arg = required_argument },
1903 { .val = LFS_COMP_FLAGS_OPT,
1904 .name = "comp-flags", .has_arg = required_argument },
1905 { .val = LFS_COMP_FLAGS_OPT,
1906 .name = "component-flags",
1907 .has_arg = required_argument },
1908 { .val = LFS_COMP_START_OPT,
1909 .name = "comp-start", .has_arg = required_argument },
1910 { .val = LFS_COMP_START_OPT,
1911 .name = "component-start",
1912 .has_arg = required_argument },
1913 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
1914 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
1915 { .val = 'C', .name = "ctime", .has_arg = required_argument },
1916 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
1917 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
1918 { .val = 'E', .name = "component-end",
1919 .has_arg = required_argument },
1920 { .val = 'g', .name = "gid", .has_arg = required_argument },
1921 { .val = 'G', .name = "group", .has_arg = required_argument },
1922 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
1923 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
1924 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
1925 /*{"component-id", required_argument, 0, 'I'},*/
1926 { .val = 'L', .name = "layout", .has_arg = required_argument },
1927 { .val = 'm', .name = "mdt", .has_arg = required_argument },
1928 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
1929 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
1930 { .val = 'M', .name = "mtime", .has_arg = required_argument },
1931 { .val = 'n', .name = "name", .has_arg = required_argument },
1932 /* reserve {"or", no_argument, , 0, 'o'}, to match find(1) */
1933 { .val = 'O', .name = "obd", .has_arg = required_argument },
1934 { .val = 'O', .name = "ost", .has_arg = required_argument },
1935 /* no short option for pool, p/P already used */
1936 { .val = LFS_POOL_OPT,
1937 .name = "pool", .has_arg = required_argument },
1938 { .val = 'p', .name = "print0", .has_arg = no_argument },
1939 { .val = 'P', .name = "print", .has_arg = no_argument },
1940 { .val = LFS_PROJID_OPT,
1941 .name = "projid", .has_arg = required_argument },
1942 { .val = 's', .name = "size", .has_arg = required_argument },
1943 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
1944 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
1945 { .val = 't', .name = "type", .has_arg = required_argument },
1946 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
1947 { .val = 'u', .name = "uid", .has_arg = required_argument },
1948 { .val = 'U', .name = "user", .has_arg = required_argument },
1960 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
1961 while ((c = getopt_long_only(argc, argv,
1962 "-A:c:C:D:E:g:G:H:i:L:m:M:n:O:Ppqrs:S:t:T:u:U:v",
1963 long_opts, NULL)) >= 0) {
1968 /* '!' is part of option */
1969 /* when getopt_long_only() finds a string which is not
1970 * an option nor a known option argument it returns 1
1971 * in that case if we already have found pathstart and pathend
1972 * (i.e. we have the list of pathnames),
1973 * the only supported value is "!"
1975 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
1976 if (!isoption && pathend != -1) {
1977 fprintf(stderr, "err: %s: filename|dirname must either "
1978 "precede options or follow options\n",
1983 if (!isoption && pathstart == -1)
1984 pathstart = optind - 1;
1985 if (isoption && pathstart != -1 && pathend == -1)
1986 pathend = optind - 2;
1992 /* unknown; opt is "!" or path component,
1993 * checking done above.
1995 if (strcmp(optarg, "!") == 0)
1999 xtime = ¶m.fp_atime;
2000 xsign = ¶m.fp_asign;
2001 param.fp_exclude_atime = !!neg_opt;
2002 /* no break, this falls through to 'C' for ctime */
2005 xtime = ¶m.fp_ctime;
2006 xsign = ¶m.fp_csign;
2007 param.fp_exclude_ctime = !!neg_opt;
2009 /* no break, this falls through to 'M' for mtime */
2012 xtime = ¶m.fp_mtime;
2013 xsign = ¶m.fp_msign;
2014 param.fp_exclude_mtime = !!neg_opt;
2016 rc = set_time(&t, xtime, optarg);
2017 if (rc == INT_MAX) {
2024 case LFS_COMP_COUNT_OPT:
2025 if (optarg[0] == '+') {
2026 param.fp_comp_count_sign = -1;
2028 } else if (optarg[0] == '-') {
2029 param.fp_comp_count_sign = 1;
2033 param.fp_comp_count = strtoul(optarg, &endptr, 0);
2034 if (*endptr != '\0') {
2035 fprintf(stderr, "error: bad component count "
2039 param.fp_check_comp_count = 1;
2040 param.fp_exclude_comp_count = !!neg_opt;
2042 case LFS_COMP_FLAGS_OPT:
2043 rc = comp_str2flags(¶m.fp_comp_flags, optarg);
2044 if (rc || comp_flags_is_neg(param.fp_comp_flags)) {
2045 fprintf(stderr, "error: bad component flags "
2049 param.fp_check_comp_flags = 1;
2050 param.fp_exclude_comp_flags = !!neg_opt;
2052 case LFS_COMP_START_OPT:
2053 if (optarg[0] == '+') {
2054 param.fp_comp_start_sign = -1;
2056 } else if (optarg[0] == '-') {
2057 param.fp_comp_start_sign = 1;
2061 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
2062 ¶m.fp_comp_start_units, 0);
2064 fprintf(stderr, "error: bad component start "
2068 param.fp_check_comp_start = 1;
2069 param.fp_exclude_comp_start = !!neg_opt;
2072 if (optarg[0] == '+') {
2073 param.fp_stripe_count_sign = -1;
2075 } else if (optarg[0] == '-') {
2076 param.fp_stripe_count_sign = 1;
2080 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
2081 if (*endptr != '\0') {
2082 fprintf(stderr,"error: bad stripe_count '%s'\n",
2087 param.fp_check_stripe_count = 1;
2088 param.fp_exclude_stripe_count = !!neg_opt;
2091 param.fp_max_depth = strtol(optarg, 0, 0);
2094 if (optarg[0] == '+') {
2095 param.fp_comp_end_sign = -1;
2097 } else if (optarg[0] == '-') {
2098 param.fp_comp_end_sign = 1;
2102 if (arg_is_eof(optarg)) {
2103 param.fp_comp_end = LUSTRE_EOF;
2104 param.fp_comp_end_units = 1;
2107 rc = llapi_parse_size(optarg,
2109 ¶m.fp_comp_end_units, 0);
2112 fprintf(stderr, "error: bad component end "
2116 param.fp_check_comp_end = 1;
2117 param.fp_exclude_comp_end = !!neg_opt;
2121 rc = name2gid(¶m.fp_gid, optarg);
2123 param.fp_gid = strtoul(optarg, &endptr, 10);
2124 if (*endptr != '\0') {
2125 fprintf(stderr, "Group/GID: %s cannot "
2126 "be found.\n", optarg);
2131 param.fp_exclude_gid = !!neg_opt;
2132 param.fp_check_gid = 1;
2135 param.fp_hash_type = check_hashtype(optarg);
2136 if (param.fp_hash_type == 0) {
2137 fprintf(stderr, "error: bad hash_type '%s'\n",
2142 param.fp_check_hash_type = 1;
2143 param.fp_exclude_hash_type = !!neg_opt;
2146 ret = name2layout(¶m.fp_layout, optarg);
2149 param.fp_exclude_layout = !!neg_opt;
2150 param.fp_check_layout = 1;
2154 rc = name2uid(¶m.fp_uid, optarg);
2156 param.fp_uid = strtoul(optarg, &endptr, 10);
2157 if (*endptr != '\0') {
2158 fprintf(stderr, "User/UID: %s cannot "
2159 "be found.\n", optarg);
2164 param.fp_exclude_uid = !!neg_opt;
2165 param.fp_check_uid = 1;
2168 if (strlen(optarg) > LOV_MAXPOOLNAME) {
2170 "Pool name %s is too long"
2171 " (max is %d)\n", optarg,
2176 /* we do check for empty pool because empty pool
2177 * is used to find V1 lov attributes */
2178 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
2179 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
2180 param.fp_exclude_pool = !!neg_opt;
2181 param.fp_check_pool = 1;
2184 param.fp_pattern = (char *)optarg;
2185 param.fp_exclude_pattern = !!neg_opt;
2190 char *buf, *token, *next, *p;
2194 buf = strdup(optarg);
2200 param.fp_exclude_obd = !!neg_opt;
2203 while (token && *token) {
2204 token = strchr(token, ',');
2211 param.fp_exclude_mdt = !!neg_opt;
2212 param.fp_num_alloc_mdts += len;
2213 tmp = realloc(param.fp_mdt_uuid,
2214 param.fp_num_alloc_mdts *
2215 sizeof(*param.fp_mdt_uuid));
2221 param.fp_mdt_uuid = tmp;
2223 param.fp_exclude_obd = !!neg_opt;
2224 param.fp_num_alloc_obds += len;
2225 tmp = realloc(param.fp_obd_uuid,
2226 param.fp_num_alloc_obds *
2227 sizeof(*param.fp_obd_uuid));
2233 param.fp_obd_uuid = tmp;
2235 for (token = buf; token && *token; token = next) {
2236 struct obd_uuid *puuid;
2239 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
2242 ¶m.fp_obd_uuid[param.fp_num_obds++];
2244 p = strchr(token, ',');
2251 if (strlen(token) > sizeof(puuid->uuid) - 1) {
2256 strncpy(puuid->uuid, token,
2257 sizeof(puuid->uuid));
2265 param.fp_zero_end = 1;
2269 case LFS_PROJID_OPT:
2270 rc = name2projid(¶m.fp_projid, optarg);
2272 param.fp_projid = strtoul(optarg, &endptr, 10);
2273 if (*endptr != '\0') {
2275 "Invalid project ID: %s",
2281 param.fp_exclude_projid = !!neg_opt;
2282 param.fp_check_projid = 1;
2285 if (optarg[0] == '+') {
2286 param.fp_size_sign = -1;
2288 } else if (optarg[0] == '-') {
2289 param.fp_size_sign = 1;
2293 ret = llapi_parse_size(optarg, ¶m.fp_size,
2294 ¶m.fp_size_units, 0);
2296 fprintf(stderr, "error: bad file size '%s'\n",
2300 param.fp_check_size = 1;
2301 param.fp_exclude_size = !!neg_opt;
2304 if (optarg[0] == '+') {
2305 param.fp_stripe_size_sign = -1;
2307 } else if (optarg[0] == '-') {
2308 param.fp_stripe_size_sign = 1;
2312 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
2313 ¶m.fp_stripe_size_units, 0);
2315 fprintf(stderr, "error: bad stripe_size '%s'\n",
2319 param.fp_check_stripe_size = 1;
2320 param.fp_exclude_stripe_size = !!neg_opt;
2323 param.fp_exclude_type = !!neg_opt;
2324 switch (optarg[0]) {
2326 param.fp_type = S_IFBLK;
2329 param.fp_type = S_IFCHR;
2332 param.fp_type = S_IFDIR;
2335 param.fp_type = S_IFREG;
2338 param.fp_type = S_IFLNK;
2341 param.fp_type = S_IFIFO;
2344 param.fp_type = S_IFSOCK;
2347 fprintf(stderr, "error: %s: bad type '%s'\n",
2354 if (optarg[0] == '+') {
2355 param.fp_mdt_count_sign = -1;
2357 } else if (optarg[0] == '-') {
2358 param.fp_mdt_count_sign = 1;
2362 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
2363 if (*endptr != '\0') {
2364 fprintf(stderr, "error: bad mdt_count '%s'\n",
2369 param.fp_check_mdt_count = 1;
2370 param.fp_exclude_mdt_count = !!neg_opt;
2378 if (pathstart == -1) {
2379 fprintf(stderr, "error: %s: no filename|pathname\n",
2383 } else if (pathend == -1) {
2389 rc = llapi_find(argv[pathstart], ¶m);
2390 if (rc != 0 && ret == 0)
2392 } while (++pathstart < pathend);
2395 fprintf(stderr, "error: %s failed for %s.\n",
2396 argv[0], argv[optind - 1]);
2398 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
2399 free(param.fp_obd_uuid);
2401 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
2402 free(param.fp_mdt_uuid);
2407 static int lfs_getstripe_internal(int argc, char **argv,
2408 struct find_param *param)
2410 struct option long_opts[] = {
2411 {"comp-count", no_argument, 0, LFS_COMP_COUNT_OPT},
2412 {"component-count", no_argument, 0, LFS_COMP_COUNT_OPT},
2413 {"comp-flags", optional_argument, 0, LFS_COMP_FLAGS_OPT},
2414 {"component-flags", optional_argument, 0, LFS_COMP_FLAGS_OPT},
2415 {"comp-start", optional_argument, 0, LFS_COMP_START_OPT},
2416 {"component-start", optional_argument, 0, LFS_COMP_START_OPT},
2417 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2418 /* This formerly implied "stripe-count", but was explicitly
2419 * made "stripe-count" for consistency with other options,
2420 * and to separate it from "mdt-count" when DNE arrives. */
2421 {"count", no_argument, 0, 'c'},
2423 {"stripe-count", no_argument, 0, 'c'},
2424 {"stripe_count", no_argument, 0, 'c'},
2425 {"directory", no_argument, 0, 'd'},
2426 {"default", no_argument, 0, 'D'},
2427 {"comp-end", optional_argument, 0, 'E'},
2428 {"component-end", optional_argument, 0, 'E'},
2429 {"fid", no_argument, 0, 'F'},
2430 {"generation", no_argument, 0, 'g'},
2431 /* dirstripe {"mdt-hash", required_argument, 0, 'H'}, */
2432 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2433 /* This formerly implied "stripe-index", but was explicitly
2434 * made "stripe-index" for consistency with other options,
2435 * and to separate it from "mdt-index" when DNE arrives. */
2436 {"index", no_argument, 0, 'i'},
2438 {"stripe-index", no_argument, 0, 'i'},
2439 {"stripe_index", no_argument, 0, 'i'},
2440 {"comp-id", optional_argument, 0, 'I'},
2441 {"component-id", optional_argument, 0, 'I'},
2442 {"layout", no_argument, 0, 'L'},
2443 {"mdt", no_argument, 0, 'm'},
2444 {"mdt-index", no_argument, 0, 'm'},
2445 {"mdt_index", no_argument, 0, 'm'},
2446 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2447 {"mdt-index", no_argument, 0, 'M'},
2448 {"mdt_index", no_argument, 0, 'M'},
2450 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2451 /* This formerly implied "stripe-index", but was confusing
2452 * with "file offset" (which will eventually be needed for
2453 * with different layouts by offset), so deprecate it. */
2454 {"offset", no_argument, 0, 'o'},
2456 {"obd", required_argument, 0, 'O'},
2457 {"ost", required_argument, 0, 'O'},
2458 {"pool", no_argument, 0, 'p'},
2459 {"quiet", no_argument, 0, 'q'},
2460 {"recursive", no_argument, 0, 'r'},
2461 {"raw", no_argument, 0, 'R'},
2462 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2463 /* This formerly implied "--stripe-size", but was confusing
2464 * with "lfs find --size|-s", which means "file size", so use
2465 * the consistent "--stripe-size|-S" for all commands. */
2466 {"size", no_argument, 0, 's'},
2468 {"stripe-size", no_argument, 0, 'S'},
2469 {"stripe_size", no_argument, 0, 'S'},
2470 /* dirstripe {"mdt-count", required_argument, 0, 'T'}, */
2471 {"verbose", no_argument, 0, 'v'},
2472 {"yaml", no_argument, 0, 'y'},
2478 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
2479 long_opts, NULL)) != -1) {
2482 if (strcmp(argv[optind - 1], "--count") == 0)
2483 fprintf(stderr, "warning: '--count' deprecated,"
2484 " use '--stripe-count' instead\n");
2485 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2486 param->fp_verbose |= VERBOSE_COUNT;
2487 param->fp_max_depth = 0;
2490 case LFS_COMP_COUNT_OPT:
2491 param->fp_verbose |= VERBOSE_COMP_COUNT;
2492 param->fp_max_depth = 0;
2494 case LFS_COMP_FLAGS_OPT:
2495 if (optarg != NULL) {
2496 __u32 *flags = ¶m->fp_comp_flags;
2497 rc = comp_str2flags(flags, optarg);
2499 fprintf(stderr, "error: %s bad "
2500 "component flags '%s'.\n",
2504 param->fp_check_comp_flags = 1;
2505 param->fp_exclude_comp_flags =
2506 comp_flags_is_neg(*flags);
2507 comp_flags_clear_neg(flags);
2510 param->fp_verbose |= VERBOSE_COMP_FLAGS;
2511 param->fp_max_depth = 0;
2514 case LFS_COMP_START_OPT:
2515 if (optarg != NULL) {
2517 if (tmp[0] == '+') {
2518 param->fp_comp_start_sign = -1;
2520 } else if (tmp[0] == '-') {
2521 param->fp_comp_start_sign = 1;
2524 rc = llapi_parse_size(tmp,
2525 ¶m->fp_comp_start,
2526 ¶m->fp_comp_start_units, 0);
2528 fprintf(stderr, "error: %s bad "
2529 "component start '%s'.\n",
2533 param->fp_check_comp_start = 1;
2536 param->fp_verbose |= VERBOSE_COMP_START;
2537 param->fp_max_depth = 0;
2541 param->fp_max_depth = 0;
2544 param->fp_get_default_lmv = 1;
2547 if (optarg != NULL) {
2549 if (tmp[0] == '+') {
2550 param->fp_comp_end_sign = -1;
2552 } else if (tmp[0] == '-') {
2553 param->fp_comp_end_sign = 1;
2557 if (arg_is_eof(tmp)) {
2558 param->fp_comp_end = LUSTRE_EOF;
2559 param->fp_comp_end_units = 1;
2562 rc = llapi_parse_size(tmp,
2563 ¶m->fp_comp_end,
2564 ¶m->fp_comp_end_units, 0);
2567 fprintf(stderr, "error: %s bad "
2568 "component end '%s'.\n",
2572 param->fp_check_comp_end = 1;
2574 param->fp_verbose |= VERBOSE_COMP_END;
2575 param->fp_max_depth = 0;
2579 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2580 param->fp_verbose |= VERBOSE_DFID;
2581 param->fp_max_depth = 0;
2585 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2586 param->fp_verbose |= VERBOSE_GENERATION;
2587 param->fp_max_depth = 0;
2590 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2592 fprintf(stderr, "warning: '--offset|-o' deprecated, "
2593 "use '--stripe-index|-i' instead\n");
2596 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
2597 if (strcmp(argv[optind - 1], "--index") == 0)
2598 fprintf(stderr, "warning: '--index' deprecated"
2599 ", use '--stripe-index' instead\n");
2601 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2602 param->fp_verbose |= VERBOSE_OFFSET;
2603 param->fp_max_depth = 0;
2607 if (optarg != NULL) {
2608 param->fp_comp_id = strtoul(optarg, &end, 0);
2609 if (*end != '\0' || param->fp_comp_id == 0 ||
2610 param->fp_comp_id > LCME_ID_MAX) {
2611 fprintf(stderr, "error: %s bad "
2612 "component id '%s'\n",
2616 param->fp_check_comp_id = 1;
2619 param->fp_max_depth = 0;
2620 param->fp_verbose |= VERBOSE_COMP_ID;
2624 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2625 param->fp_verbose |= VERBOSE_LAYOUT;
2626 param->fp_max_depth = 0;
2629 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2631 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
2632 fprintf(stderr, "warning: '-M' deprecated"
2633 ", use '-m' instead\n");
2637 if (!(param->fp_verbose & VERBOSE_DETAIL))
2638 param->fp_max_depth = 0;
2639 param->fp_verbose |= VERBOSE_MDTINDEX;
2642 if (param->fp_obd_uuid) {
2644 "error: %s: only one obduuid allowed",
2648 param->fp_obd_uuid = (struct obd_uuid *)optarg;
2651 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2652 param->fp_verbose |= VERBOSE_POOL;
2653 param->fp_max_depth = 0;
2660 param->fp_recursive = 1;
2665 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
2667 fprintf(stderr, "warning: '--size|-s' deprecated, "
2668 "use '--stripe-size|-S' instead\n");
2669 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0) */
2671 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
2672 param->fp_verbose |= VERBOSE_SIZE;
2673 param->fp_max_depth = 0;
2677 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
2690 if (param->fp_recursive)
2691 param->fp_max_depth = -1;
2692 else if (param->fp_verbose & VERBOSE_DETAIL)
2693 param->fp_max_depth = 1;
2695 if (!param->fp_verbose)
2696 param->fp_verbose = VERBOSE_DEFAULT;
2697 if (param->fp_quiet)
2698 param->fp_verbose = VERBOSE_OBJID;
2701 rc = llapi_getstripe(argv[optind], param);
2702 } while (++optind < argc && !rc);
2705 fprintf(stderr, "error: %s failed for %s.\n",
2706 argv[0], argv[optind - 1]);
2710 static int lfs_tgts(int argc, char **argv)
2712 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
2713 struct find_param param;
2714 int index = 0, rc=0;
2719 if (argc == 2 && !realpath(argv[1], path)) {
2721 fprintf(stderr, "error: invalid path '%s': %s\n",
2722 argv[1], strerror(-rc));
2726 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
2727 /* Check if we have a mount point */
2728 if (mntdir[0] == '\0')
2731 memset(¶m, 0, sizeof(param));
2732 if (!strcmp(argv[0], "mdts"))
2733 param.fp_get_lmv = 1;
2735 rc = llapi_ostlist(mntdir, ¶m);
2737 fprintf(stderr, "error: %s: failed on %s\n",
2740 if (path[0] != '\0')
2742 memset(mntdir, 0, PATH_MAX);
2748 static int lfs_getstripe(int argc, char **argv)
2750 struct find_param param = { 0 };
2752 param.fp_max_depth = 1;
2753 return lfs_getstripe_internal(argc, argv, ¶m);
2757 static int lfs_getdirstripe(int argc, char **argv)
2759 struct find_param param = { 0 };
2760 struct option long_opts[] = {
2761 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2762 {"mdt-count", no_argument, 0, 'c'},
2764 {"mdt-hash", no_argument, 0, 'H'},
2765 {"mdt-index", no_argument, 0, 'i'},
2766 {"recursive", no_argument, 0, 'r'},
2767 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2768 {"mdt-hash", no_argument, 0, 't'},
2770 {"default", no_argument, 0, 'D'},
2771 {"obd", required_argument, 0, 'O'},
2772 {"mdt-count", no_argument, 0, 'T'},
2773 {"yaml", no_argument, 0, 'y'},
2778 param.fp_get_lmv = 1;
2780 while ((c = getopt_long(argc, argv,
2781 "cDHiO:rtTy", long_opts, NULL)) != -1)
2785 if (param.fp_obd_uuid) {
2787 "error: %s: only one obduuid allowed",
2791 param.fp_obd_uuid = (struct obd_uuid *)optarg;
2793 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2795 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 10, 50, 0)
2796 fprintf(stderr, "warning: '-c' deprecated"
2797 ", use '-T' instead\n");
2801 param.fp_verbose |= VERBOSE_COUNT;
2804 param.fp_verbose |= VERBOSE_OFFSET;
2806 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2810 param.fp_verbose |= VERBOSE_HASH_TYPE;
2813 param.fp_get_default_lmv = 1;
2816 param.fp_recursive = 1;
2829 if (param.fp_recursive)
2830 param.fp_max_depth = -1;
2832 if (!param.fp_verbose)
2833 param.fp_verbose = VERBOSE_DEFAULT;
2836 rc = llapi_getstripe(argv[optind], ¶m);
2837 } while (++optind < argc && !rc);
2840 fprintf(stderr, "error: %s failed for %s.\n",
2841 argv[0], argv[optind - 1]);
2846 static int lfs_setdirstripe(int argc, char **argv)
2850 unsigned int stripe_offset = -1;
2851 unsigned int stripe_count = 1;
2852 enum lmv_hash_type hash_type;
2855 char *stripe_offset_opt = NULL;
2856 char *stripe_count_opt = NULL;
2857 char *stripe_hash_opt = NULL;
2858 char *mode_opt = NULL;
2859 bool default_stripe = false;
2860 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
2861 mode_t previous_mode = 0;
2862 bool delete = false;
2864 struct option long_opts[] = {
2865 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2866 { .val = 'c', .name = "count", .has_arg = required_argument },
2868 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
2869 { .val = 'd', .name = "delete", .has_arg = no_argument },
2870 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2871 { .val = 'i', .name = "index", .has_arg = required_argument },
2873 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
2874 { .val = 'm', .name = "mode", .has_arg = required_argument },
2875 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2876 { .val = 't', .name = "hash-type", .has_arg = required_argument },
2877 { .val = 't', .name = "mdt-hash", .has_arg = required_argument },
2879 {"mdt-hash", required_argument, 0, 'H'},
2880 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2881 { .val = 'D', .name = "default_stripe",
2882 .has_arg = no_argument },
2884 { .val = 'D', .name = "default", .has_arg = no_argument },
2887 while ((c = getopt_long(argc, argv, "c:dDi:H:m:t:", long_opts,
2894 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
2895 if (strcmp(argv[optind - 1], "--count") == 0)
2896 fprintf(stderr, "warning: '--count' deprecated"
2897 ", use '--mdt-count' instead\n");
2899 stripe_count_opt = optarg;
2903 default_stripe = true;
2906 default_stripe = true;
2909 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
2910 if (strcmp(argv[optind - 1], "--index") == 0)
2911 fprintf(stderr, "warning: '--index' deprecated"
2912 ", use '--mdt-index' instead\n");
2914 stripe_offset_opt = optarg;
2919 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2923 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
2924 if (strcmp(argv[optind - 1], "--hash-type") == 0)
2925 fprintf(stderr, "warning: '--hash-type' "
2926 "deprecated, use '--mdt-hash' "
2929 stripe_hash_opt = optarg;
2932 fprintf(stderr, "error: %s: option '%s' "
2934 argv[0], argv[optind - 1]);
2939 if (optind == argc) {
2940 fprintf(stderr, "error: %s: missing dirname\n",
2945 if (!delete && stripe_offset_opt == NULL && stripe_count_opt == NULL) {
2946 fprintf(stderr, "error: %s: missing stripe offset and count.\n",
2951 if (stripe_offset_opt != NULL) {
2952 /* get the stripe offset */
2953 stripe_offset = strtoul(stripe_offset_opt, &end, 0);
2955 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
2956 argv[0], stripe_offset_opt);
2962 if (stripe_offset_opt != NULL || stripe_count_opt != NULL) {
2963 fprintf(stderr, "error: %s: cannot specify -d with -s,"
2964 " or -i options.\n", argv[0]);
2972 if (mode_opt != NULL) {
2973 mode = strtoul(mode_opt, &end, 8);
2975 fprintf(stderr, "error: %s: bad mode '%s'\n",
2979 previous_mode = umask(0);
2982 if (stripe_hash_opt == NULL) {
2983 hash_type = LMV_HASH_TYPE_FNV_1A_64;
2985 hash_type = check_hashtype(stripe_hash_opt);
2986 if (hash_type == 0) {
2988 "error: %s: bad stripe hash type '%s'\n",
2989 argv[0], stripe_hash_opt);
2994 /* get the stripe count */
2995 if (stripe_count_opt != NULL) {
2996 stripe_count = strtoul(stripe_count_opt, &end, 0);
2998 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
2999 argv[0], stripe_count_opt);
3004 dname = argv[optind];
3006 if (default_stripe) {
3007 result = llapi_dir_set_default_lmv_stripe(dname,
3008 stripe_offset, stripe_count,
3011 result = llapi_dir_create_pool(dname, mode,
3013 stripe_count, hash_type,
3018 fprintf(stderr, "error: %s: create stripe dir '%s' "
3019 "failed\n", argv[0], dname);
3022 dname = argv[++optind];
3023 } while (dname != NULL);
3025 if (mode_opt != NULL)
3026 umask(previous_mode);
3032 static int lfs_rmentry(int argc, char **argv)
3039 fprintf(stderr, "error: %s: missing dirname\n",
3045 dname = argv[index];
3046 while (dname != NULL) {
3047 result = llapi_direntry_remove(dname);
3049 fprintf(stderr, "error: %s: remove dir entry '%s' "
3050 "failed\n", argv[0], dname);
3053 dname = argv[++index];
3058 static int lfs_mv(int argc, char **argv)
3060 struct find_param param = {
3067 struct option long_opts[] = {
3068 { .val = 'M', .name = "mdt-index", .has_arg = required_argument },
3069 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3072 while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
3075 param.fp_mdt_index = strtoul(optarg, &end, 0);
3077 fprintf(stderr, "%s: invalid MDT index'%s'\n",
3084 param.fp_verbose = VERBOSE_DETAIL;
3088 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
3089 argv[0], argv[optind - 1]);
3094 if (param.fp_mdt_index == -1) {
3095 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
3099 if (optind >= argc) {
3100 fprintf(stderr, "%s: missing operand path\n", argv[0]);
3104 param.fp_migrate = 1;
3105 rc = llapi_migrate_mdt(argv[optind], ¶m);
3107 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
3108 argv[0], argv[optind], param.fp_mdt_index,
3113 static int lfs_osts(int argc, char **argv)
3115 return lfs_tgts(argc, argv);
3118 static int lfs_mdts(int argc, char **argv)
3120 return lfs_tgts(argc, argv);
3123 #define COOK(value) \
3126 while (value > 1024) { \
3134 #define CDF "%11llu"
3135 #define HDF "%8.1f%c"
3140 MNTDF_INODES = 0x0001,
3141 MNTDF_COOKED = 0x0002,
3142 MNTDF_LAZY = 0x0004,
3143 MNTDF_VERBOSE = 0x0008,
3146 static int showdf(char *mntdir, struct obd_statfs *stat,
3147 char *uuid, enum mntdf_flags flags,
3148 char *type, int index, int rc)
3150 long long avail, used, total;
3152 char *suffix = "KMGTPEZY";
3153 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
3154 char tbuf[3 * sizeof(__u64)];
3155 char ubuf[3 * sizeof(__u64)];
3156 char abuf[3 * sizeof(__u64)];
3157 char rbuf[3 * sizeof(__u64)];
3164 if (flags & MNTDF_INODES) {
3165 avail = stat->os_ffree;
3166 used = stat->os_files - stat->os_ffree;
3167 total = stat->os_files;
3169 int shift = flags & MNTDF_COOKED ? 0 : 10;
3171 avail = (stat->os_bavail * stat->os_bsize) >> shift;
3172 used = ((stat->os_blocks - stat->os_bfree) *
3173 stat->os_bsize) >> shift;
3174 total = (stat->os_blocks * stat->os_bsize) >> shift;
3177 if ((used + avail) > 0)
3178 ratio = (double)used / (double)(used + avail);
3180 if (flags & MNTDF_COOKED) {
3184 cook_val = (double)total;
3187 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
3190 snprintf(tbuf, sizeof(tbuf), CDF, total);
3192 cook_val = (double)used;
3195 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
3198 snprintf(ubuf, sizeof(ubuf), CDF, used);
3200 cook_val = (double)avail;
3203 snprintf(abuf, sizeof(abuf), HDF, cook_val,
3206 snprintf(abuf, sizeof(abuf), CDF, avail);
3208 snprintf(tbuf, sizeof(tbuf), CDF, total);
3209 snprintf(ubuf, sizeof(tbuf), CDF, used);
3210 snprintf(abuf, sizeof(tbuf), CDF, avail);
3213 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
3214 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
3215 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
3217 printf("[%s:%d]", type, index);
3219 if (stat->os_state) {
3221 * Each character represents the matching
3224 const char state_names[] = "DRSI";
3229 for (i = 0, state = stat->os_state;
3230 state && i < sizeof(state_names); i++) {
3231 if (!(state & (1 << i)))
3233 printf("%c", state_names[i]);
3241 printf(UUF": inactive device\n", uuid);
3244 printf(UUF": %s\n", uuid, strerror(-rc));
3251 struct ll_stat_type {
3256 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags)
3258 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
3259 struct obd_uuid uuid_buf;
3260 char *poolname = NULL;
3261 struct ll_stat_type types[] = {
3262 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
3263 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
3264 { .st_name = NULL } };
3265 struct ll_stat_type *tp;
3266 __u64 ost_ffree = 0;
3274 poolname = strchr(pool, '.');
3275 if (poolname != NULL) {
3276 if (strncmp(fsname, pool, strlen(fsname))) {
3277 fprintf(stderr, "filesystem name incorrect\n");
3285 fd = open(mntdir, O_RDONLY);
3288 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
3293 if (flags & MNTDF_INODES)
3294 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
3295 "UUID", "Inodes", "IUsed", "IFree",
3296 "IUse%", "Mounted on");
3298 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
3299 "UUID", flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
3300 "Used", "Available", "Use%", "Mounted on");
3302 for (tp = types; tp->st_name != NULL; tp++) {
3303 for (index = 0; ; index++) {
3304 memset(&stat_buf, 0, sizeof(struct obd_statfs));
3305 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
3306 type = flags & MNTDF_LAZY ?
3307 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
3308 rc2 = llapi_obd_fstatfs(fd, type, index,
3309 &stat_buf, &uuid_buf);
3314 if (rc2 == -ENODATA) { /* Inactive device, OK. */
3315 if (!(flags & MNTDF_VERBOSE))
3317 } else if (rc2 < 0 && rc == 0) {
3321 if (poolname && tp->st_op == LL_STATFS_LOV &&
3322 llapi_search_ost(fsname, poolname,
3323 obd_uuid2str(&uuid_buf)) != 1)
3326 /* the llapi_obd_statfs() call may have returned with
3327 * an error, but if it filled in uuid_buf we will at
3328 * lease use that to print out a message for that OBD.
3329 * If we didn't get anything in the uuid_buf, then fill
3330 * it in so that we can print an error message. */
3331 if (uuid_buf.uuid[0] == '\0')
3332 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
3333 "%s%04x", tp->st_name, index);
3334 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
3335 flags, tp->st_name, index, rc2);
3338 if (tp->st_op == LL_STATFS_LMV) {
3339 sum.os_ffree += stat_buf.os_ffree;
3340 sum.os_files += stat_buf.os_files;
3341 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
3342 sum.os_blocks += stat_buf.os_blocks *
3344 sum.os_bfree += stat_buf.os_bfree *
3346 sum.os_bavail += stat_buf.os_bavail *
3348 ost_ffree += stat_buf.os_ffree;
3356 /* If we don't have as many objects free on the OST as inodes
3357 * on the MDS, we reduce the total number of inodes to
3358 * compensate, so that the "inodes in use" number is correct.
3359 * Matches ll_statfs_internal() so the results are consistent. */
3360 if (ost_ffree < sum.os_ffree) {
3361 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
3362 sum.os_ffree = ost_ffree;
3365 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
3371 static int lfs_df(int argc, char **argv)
3373 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3374 enum mntdf_flags flags = 0;
3375 int c, rc = 0, index = 0;
3376 char fsname[PATH_MAX] = "", *pool_name = NULL;
3377 struct option long_opts[] = {
3378 { .val = 'h', .name = "human-readable",
3379 .has_arg = no_argument },
3380 { .val = 'i', .name = "inodes", .has_arg = no_argument },
3381 { .val = 'l', .name = "lazy", .has_arg = no_argument },
3382 { .val = 'p', .name = "pool", .has_arg = required_argument },
3383 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3386 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
3389 flags |= MNTDF_COOKED;
3392 flags |= MNTDF_INODES;
3395 flags |= MNTDF_LAZY;
3401 flags |= MNTDF_VERBOSE;
3407 if (optind < argc && !realpath(argv[optind], path)) {
3409 fprintf(stderr, "error: invalid path '%s': %s\n",
3410 argv[optind], strerror(-rc));
3414 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
3415 /* Check if we have a mount point */
3416 if (mntdir[0] == '\0')
3419 rc = mntdf(mntdir, fsname, pool_name, flags);
3420 if (rc || path[0] != '\0')
3422 fsname[0] = '\0'; /* avoid matching in next loop */
3423 mntdir[0] = '\0'; /* avoid matching in next loop */
3429 static int lfs_getname(int argc, char **argv)
3431 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
3432 int rc = 0, index = 0, c;
3433 char buf[sizeof(struct obd_uuid)];
3435 while ((c = getopt(argc, argv, "h")) != -1)
3438 if (optind == argc) { /* no paths specified, get all paths. */
3439 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
3440 rc = llapi_getname(mntdir, buf, sizeof(buf));
3443 "cannot get name for `%s': %s\n",
3444 mntdir, strerror(-rc));
3448 printf("%s %s\n", buf, mntdir);
3450 path[0] = fsname[0] = mntdir[0] = 0;
3452 } else { /* paths specified, only attempt to search these. */
3453 for (; optind < argc; optind++) {
3454 rc = llapi_getname(argv[optind], buf, sizeof(buf));
3457 "cannot get name for `%s': %s\n",
3458 argv[optind], strerror(-rc));
3462 printf("%s %s\n", buf, argv[optind]);
3468 static int lfs_check(int argc, char **argv)
3471 char mntdir[PATH_MAX] = {'\0'};
3480 obd_types[0] = obd_type1;
3481 obd_types[1] = obd_type2;
3483 if (strcmp(argv[1], "osts") == 0) {
3484 strcpy(obd_types[0], "osc");
3485 } else if (strcmp(argv[1], "mds") == 0) {
3486 strcpy(obd_types[0], "mdc");
3487 } else if (strcmp(argv[1], "servers") == 0) {
3489 strcpy(obd_types[0], "osc");
3490 strcpy(obd_types[1], "mdc");
3492 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3497 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
3498 if (rc < 0 || mntdir[0] == '\0') {
3499 fprintf(stderr, "No suitable Lustre mount found\n");
3503 rc = llapi_target_check(num_types, obd_types, mntdir);
3505 fprintf(stderr, "error: %s: %s status failed\n",
3512 #ifdef HAVE_SYS_QUOTA_H
3513 #define ARG2INT(nr, str, msg) \
3516 nr = strtol(str, &endp, 0); \
3518 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
3523 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
3525 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
3526 * returns the value or ULONG_MAX on integer overflow or incorrect format
3528 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
3529 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
3530 * 3. empty integer value is interpreted as 0
3532 static unsigned long str2sec(const char* timestr)
3534 const char spec[] = "smhdw";
3535 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
3536 unsigned long val = 0;
3539 if (strpbrk(timestr, spec) == NULL) {
3540 /* no specifiers inside the time string,
3541 should treat it as an integer value */
3542 val = strtoul(timestr, &tail, 10);
3543 return *tail ? ULONG_MAX : val;
3546 /* format string is XXwXXdXXhXXmXXs */
3552 v = strtoul(timestr, &tail, 10);
3553 if (v == ULONG_MAX || *tail == '\0')
3554 /* value too large (ULONG_MAX or more)
3555 or missing specifier */
3558 ptr = strchr(spec, *tail);
3560 /* unknown specifier */
3565 /* check if product will overflow the type */
3566 if (!(v < ULONG_MAX / mult[ind]))
3569 ADD_OVERFLOW(val, mult[ind] * v);
3570 if (val == ULONG_MAX)
3582 #define ARG2ULL(nr, str, def_units) \
3584 unsigned long long limit, units = def_units; \
3587 rc = llapi_parse_size(str, &limit, &units, 1); \
3589 fprintf(stderr, "error: bad limit value %s\n", str); \
3595 static inline int has_times_option(int argc, char **argv)
3599 for (i = 1; i < argc; i++)
3600 if (!strcmp(argv[i], "-t"))
3606 int lfs_setquota_times(int argc, char **argv)
3609 struct if_quotactl qctl;
3610 char *mnt, *obd_type = (char *)qctl.obd_type;
3611 struct obd_dqblk *dqb = &qctl.qc_dqblk;
3612 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
3613 struct option long_opts[] = {
3614 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
3615 { .val = 'g', .name = "group", .has_arg = no_argument },
3616 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
3617 { .val = 'p', .name = "projid", .has_arg = no_argument },
3618 { .val = 't', .name = "times", .has_arg = no_argument },
3619 { .val = 'u', .name = "user", .has_arg = no_argument },
3623 memset(&qctl, 0, sizeof(qctl));
3624 qctl.qc_cmd = LUSTRE_Q_SETINFO;
3625 qctl.qc_type = ALLQUOTA;
3627 while ((c = getopt_long(argc, argv, "b:gi:ptu",
3628 long_opts, NULL)) != -1) {
3639 if (qctl.qc_type != ALLQUOTA) {
3640 fprintf(stderr, "error: -u/g/p can't be used "
3641 "more than once\n");
3644 qctl.qc_type = qtype;
3647 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
3648 fprintf(stderr, "error: bad block-grace: %s\n",
3652 dqb->dqb_valid |= QIF_BTIME;
3655 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
3656 fprintf(stderr, "error: bad inode-grace: %s\n",
3660 dqb->dqb_valid |= QIF_ITIME;
3662 case 't': /* Yes, of course! */
3664 default: /* getopt prints error message for us when opterr != 0 */
3669 if (qctl.qc_type == ALLQUOTA) {
3670 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
3674 if (optind != argc - 1) {
3675 fprintf(stderr, "error: unexpected parameters encountered\n");
3680 rc = llapi_quotactl(mnt, &qctl);
3683 fprintf(stderr, "%s %s ", obd_type,
3684 obd_uuid2str(&qctl.obd_uuid));
3685 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
3692 #define BSLIMIT (1 << 0)
3693 #define BHLIMIT (1 << 1)
3694 #define ISLIMIT (1 << 2)
3695 #define IHLIMIT (1 << 3)
3697 int lfs_setquota(int argc, char **argv)
3700 struct if_quotactl qctl;
3701 char *mnt, *obd_type = (char *)qctl.obd_type;
3702 struct obd_dqblk *dqb = &qctl.qc_dqblk;
3703 struct option long_opts[] = {
3704 {"block-softlimit", required_argument, 0, 'b'},
3705 {"block-hardlimit", required_argument, 0, 'B'},
3706 {"group", required_argument, 0, 'g'},
3707 {"inode-softlimit", required_argument, 0, 'i'},
3708 {"inode-hardlimit", required_argument, 0, 'I'},
3709 {"user", required_argument, 0, 'u'},
3710 {"projid", required_argument, 0, 'p'},
3713 unsigned limit_mask = 0;
3717 if (has_times_option(argc, argv))
3718 return lfs_setquota_times(argc, argv);
3720 memset(&qctl, 0, sizeof(qctl));
3721 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
3722 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
3723 * so it can be used as a marker that qc_type
3724 * isn't reinitialized from command line */
3726 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
3727 long_opts, NULL)) != -1) {
3731 rc = name2uid(&qctl.qc_id, optarg);
3735 rc = name2gid(&qctl.qc_id, optarg);
3739 rc = name2projid(&qctl.qc_id, optarg);
3741 if (qctl.qc_type != ALLQUOTA) {
3742 fprintf(stderr, "error: -u and -g can't be used"
3743 " more than once\n");
3746 qctl.qc_type = qtype;
3748 qctl.qc_id = strtoul(optarg, &endptr, 10);
3749 if (*endptr != '\0') {
3750 fprintf(stderr, "error: can't find id "
3751 "for name %s\n", optarg);
3757 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
3758 dqb->dqb_bsoftlimit >>= 10;
3759 limit_mask |= BSLIMIT;
3760 if (dqb->dqb_bsoftlimit &&
3761 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
3762 fprintf(stderr, "warning: block softlimit is "
3763 "smaller than the miminal qunit size, "
3764 "please see the help of setquota or "
3765 "Lustre manual for details.\n");
3768 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
3769 dqb->dqb_bhardlimit >>= 10;
3770 limit_mask |= BHLIMIT;
3771 if (dqb->dqb_bhardlimit &&
3772 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
3773 fprintf(stderr, "warning: block hardlimit is "
3774 "smaller than the miminal qunit size, "
3775 "please see the help of setquota or "
3776 "Lustre manual for details.\n");
3779 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
3780 limit_mask |= ISLIMIT;
3781 if (dqb->dqb_isoftlimit &&
3782 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
3783 fprintf(stderr, "warning: inode softlimit is "
3784 "smaller than the miminal qunit size, "
3785 "please see the help of setquota or "
3786 "Lustre manual for details.\n");
3789 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
3790 limit_mask |= IHLIMIT;
3791 if (dqb->dqb_ihardlimit &&
3792 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
3793 fprintf(stderr, "warning: inode hardlimit is "
3794 "smaller than the miminal qunit size, "
3795 "please see the help of setquota or "
3796 "Lustre manual for details.\n");
3798 default: /* getopt prints error message for us when opterr != 0 */
3803 if (qctl.qc_type == ALLQUOTA) {
3804 fprintf(stderr, "error: neither -u, -g nor -p was specified\n");
3808 if (limit_mask == 0) {
3809 fprintf(stderr, "error: at least one limit must be specified\n");
3813 if (optind != argc - 1) {
3814 fprintf(stderr, "error: unexpected parameters encountered\n");
3820 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
3821 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
3822 /* sigh, we can't just set blimits/ilimits */
3823 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
3824 .qc_type = qctl.qc_type,
3825 .qc_id = qctl.qc_id};
3827 rc = llapi_quotactl(mnt, &tmp_qctl);
3829 fprintf(stderr, "error: setquota failed while retrieving"
3830 " current quota settings (%s)\n",
3835 if (!(limit_mask & BHLIMIT))
3836 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
3837 if (!(limit_mask & BSLIMIT))
3838 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
3839 if (!(limit_mask & IHLIMIT))
3840 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
3841 if (!(limit_mask & ISLIMIT))
3842 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
3844 /* Keep grace times if we have got no softlimit arguments */
3845 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
3846 dqb->dqb_valid |= QIF_BTIME;
3847 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
3850 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
3851 dqb->dqb_valid |= QIF_ITIME;
3852 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
3856 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
3857 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
3859 rc = llapi_quotactl(mnt, &qctl);
3862 fprintf(stderr, "%s %s ", obd_type,
3863 obd_uuid2str(&qctl.obd_uuid));
3864 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
3871 /* Converts seconds value into format string
3872 * result is returned in buf
3874 * 1. result is in descenting order: 1w2d3h4m5s
3875 * 2. zero fields are not filled (except for p. 3): 5d1s
3876 * 3. zero seconds value is presented as "0s"
3878 static char * __sec2str(time_t seconds, char *buf)
3880 const char spec[] = "smhdw";
3881 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
3886 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
3887 c = seconds / mult[i];
3889 if (c > 0 || (i == 0 && buf == tail))
3890 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
3898 static void sec2str(time_t seconds, char *buf, int rc)
3905 tail = __sec2str(seconds, tail);
3907 if (rc && tail - buf < 39) {
3913 static void diff2str(time_t seconds, char *buf, time_t now)
3919 if (seconds <= now) {
3920 strcpy(buf, "none");
3923 __sec2str(seconds - now, buf);
3926 static void print_quota_title(char *name, struct if_quotactl *qctl,
3927 bool human_readable)
3929 printf("Disk quotas for %s %s (%cid %u):\n",
3930 qtype_name(qctl->qc_type), name,
3931 *qtype_name(qctl->qc_type), qctl->qc_id);
3932 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
3933 "Filesystem", human_readable ? "used" : "kbytes",
3934 "quota", "limit", "grace",
3935 "files", "quota", "limit", "grace");
3938 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
3941 snprintf(buf, buflen, "%ju", (uintmax_t)num);
3944 snprintf(buf, buflen, "%5.4gP",
3945 (double)num / ((__u64)1 << 40));
3947 snprintf(buf, buflen, "%5.4gT",
3948 (double)num / (1 << 30));
3950 snprintf(buf, buflen, "%5.4gG",
3951 (double)num / (1 << 20));
3953 snprintf(buf, buflen, "%5.4gM",
3954 (double)num / (1 << 10));
3956 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
3960 #define STRBUF_LEN 32
3961 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
3968 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
3969 int bover = 0, iover = 0;
3970 struct obd_dqblk *dqb = &qctl->qc_dqblk;
3971 char numbuf[3][STRBUF_LEN];
3973 char strbuf[STRBUF_LEN];
3975 if (dqb->dqb_bhardlimit &&
3976 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
3978 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
3979 if (dqb->dqb_btime > now) {
3986 if (dqb->dqb_ihardlimit &&
3987 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
3989 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
3990 if (dqb->dqb_itime > now) {
3998 if (strlen(mnt) > 15)
3999 printf("%s\n%15s", mnt, "");
4001 printf("%15s", mnt);
4004 diff2str(dqb->dqb_btime, timebuf, now);
4006 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
4007 strbuf, sizeof(strbuf), h);
4008 if (rc == -EREMOTEIO)
4009 sprintf(numbuf[0], "%s*", strbuf);
4011 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
4012 "%s" : "[%s]", strbuf);
4014 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
4015 if (type == QC_GENERAL)
4016 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
4017 "%s" : "[%s]", strbuf);
4019 sprintf(numbuf[1], "%s", "-");
4021 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
4022 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
4023 "%s" : "[%s]", strbuf);
4025 printf(" %7s%c %6s %7s %7s",
4026 numbuf[0], bover ? '*' : ' ', numbuf[1],
4027 numbuf[2], bover > 1 ? timebuf : "-");
4030 diff2str(dqb->dqb_itime, timebuf, now);
4032 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
4033 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
4035 if (type == QC_GENERAL)
4036 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
4038 (uintmax_t)dqb->dqb_isoftlimit);
4040 sprintf(numbuf[1], "%s", "-");
4042 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
4043 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
4045 if (type != QC_OSTIDX)
4046 printf(" %7s%c %6s %7s %7s",
4047 numbuf[0], iover ? '*' : ' ', numbuf[1],
4048 numbuf[2], iover > 1 ? timebuf : "-");
4050 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
4053 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
4054 qctl->qc_cmd == Q_GETOINFO) {
4058 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
4059 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
4060 printf("Block grace time: %s; Inode grace time: %s\n",
4061 bgtimebuf, igtimebuf);
4065 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
4066 bool h, __u64 *total)
4068 int rc = 0, rc1 = 0, count = 0;
4069 __u32 valid = qctl->qc_valid;
4071 rc = llapi_get_obd_count(mnt, &count, is_mdt);
4073 fprintf(stderr, "can not get %s count: %s\n",
4074 is_mdt ? "mdt": "ost", strerror(-rc));
4078 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
4079 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
4080 rc = llapi_quotactl(mnt, qctl);
4082 /* It is remote client case. */
4083 if (rc == -EOPNOTSUPP) {
4090 fprintf(stderr, "quotactl %s%d failed.\n",
4091 is_mdt ? "mdt": "ost", qctl->qc_idx);
4095 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
4096 qctl->qc_valid, 0, h);
4097 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
4098 qctl->qc_dqblk.dqb_bhardlimit;
4101 qctl->qc_valid = valid;
4105 static int lfs_quota(int argc, char **argv)
4108 char *mnt, *name = NULL;
4109 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
4110 .qc_type = ALLQUOTA };
4111 char *obd_type = (char *)qctl.obd_type;
4112 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
4113 int rc = 0, rc1 = 0, rc2 = 0, rc3 = 0,
4114 verbose = 0, pass = 0, quiet = 0, inacc;
4116 __u32 valid = QC_GENERAL, idx = 0;
4117 __u64 total_ialloc = 0, total_balloc = 0;
4118 bool human_readable = false;
4121 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
4132 if (qctl.qc_type != ALLQUOTA) {
4133 fprintf(stderr, "error: use either -u or -g\n");
4136 qctl.qc_type = qtype;
4139 qctl.qc_cmd = LUSTRE_Q_GETINFO;
4142 valid = qctl.qc_valid = QC_UUID;
4143 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
4146 valid = qctl.qc_valid = QC_MDTIDX;
4147 idx = qctl.qc_idx = atoi(optarg);
4150 valid = qctl.qc_valid = QC_OSTIDX;
4151 idx = qctl.qc_idx = atoi(optarg);
4160 human_readable = true;
4163 fprintf(stderr, "error: %s: option '-%c' "
4164 "unrecognized\n", argv[0], c);
4169 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
4170 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
4171 optind == argc - 1) {
4173 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
4174 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
4175 qctl.qc_valid = valid;
4177 qctl.qc_type = pass;
4178 switch (qctl.qc_type) {
4180 qctl.qc_id = geteuid();
4181 rc = uid2name(&name, qctl.qc_id);
4184 qctl.qc_id = getegid();
4185 rc = gid2name(&name, qctl.qc_id);
4194 /* lfs quota -u username /path/to/lustre/mount */
4195 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
4196 /* options should be followed by u/g-name and mntpoint */
4197 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
4198 fprintf(stderr, "error: missing quota argument(s)\n");
4202 name = argv[optind++];
4203 switch (qctl.qc_type) {
4205 rc = name2uid(&qctl.qc_id, name);
4208 rc = name2gid(&qctl.qc_id, name);
4211 rc = name2projid(&qctl.qc_id, name);
4218 qctl.qc_id = strtoul(name, &endptr, 10);
4219 if (*endptr != '\0') {
4220 fprintf(stderr, "error: can't find id for name: %s\n",
4225 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
4226 fprintf(stderr, "error: missing quota info argument(s)\n");
4231 rc1 = llapi_quotactl(mnt, &qctl);
4235 fprintf(stderr, "%s quotas are not enabled.\n",
4236 qtype_name(qctl.qc_type));
4239 fprintf(stderr, "Permission denied.\n");
4242 /* We already got error message. */
4245 fprintf(stderr, "Unexpected quotactl error: %s\n",
4250 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
4251 print_quota_title(name, &qctl, human_readable);
4253 if (rc1 && *obd_type)
4254 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
4256 if (qctl.qc_valid != QC_GENERAL)
4259 inacc = (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) &&
4260 ((qctl.qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
4261 (QIF_LIMITS|QIF_USAGE));
4263 print_quota(mnt, &qctl, QC_GENERAL, rc1, human_readable);
4265 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO &&
4267 char strbuf[STRBUF_LEN];
4269 rc2 = print_obd_quota(mnt, &qctl, 1, human_readable,
4271 rc3 = print_obd_quota(mnt, &qctl, 0, human_readable,
4273 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
4275 printf("Total allocated inode limit: %ju, total "
4276 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
4280 if (rc1 || rc2 || rc3 || inacc)
4281 printf("Some errors happened when getting quota info. "
4282 "Some devices may be not working or deactivated. "
4283 "The data in \"[]\" is inaccurate.\n");
4286 if (pass > 0 && pass < LL_MAXQUOTAS)
4291 #endif /* HAVE_SYS_QUOTA_H! */
4293 static int flushctx_ioctl(char *mp)
4297 fd = open(mp, O_RDONLY);
4299 fprintf(stderr, "flushctx: error open %s: %s\n",
4300 mp, strerror(errno));
4304 rc = ioctl(fd, LL_IOC_FLUSHCTX);
4306 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
4307 mp, strerror(errno));
4313 static int lfs_flushctx(int argc, char **argv)
4315 int kdestroy = 0, c;
4316 char mntdir[PATH_MAX] = {'\0'};
4320 while ((c = getopt(argc, argv, "k")) != -1) {
4326 fprintf(stderr, "error: %s: option '-%c' "
4327 "unrecognized\n", argv[0], c);
4333 if ((rc = system("kdestroy > /dev/null")) != 0) {
4334 rc = WEXITSTATUS(rc);
4335 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
4339 if (optind >= argc) {
4340 /* flush for all mounted lustre fs. */
4341 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
4342 /* Check if we have a mount point */
4343 if (mntdir[0] == '\0')
4346 if (flushctx_ioctl(mntdir))
4349 mntdir[0] = '\0'; /* avoid matching in next loop */
4352 /* flush fs as specified */
4353 while (optind < argc) {
4354 if (flushctx_ioctl(argv[optind++]))
4361 static int lfs_cp(int argc, char **argv)
4363 fprintf(stderr, "remote client copy file(s).\n"
4364 "obsolete, does not support it anymore.\n");
4368 static int lfs_ls(int argc, char **argv)
4370 fprintf(stderr, "remote client lists directory contents.\n"
4371 "obsolete, does not support it anymore.\n");
4375 static int lfs_changelog(int argc, char **argv)
4377 void *changelog_priv;
4378 struct changelog_rec *rec;
4379 long long startrec = 0, endrec = 0;
4381 struct option long_opts[] = {
4382 {"follow", no_argument, 0, 'f'},
4385 char short_opts[] = "f";
4388 while ((rc = getopt_long(argc, argv, short_opts,
4389 long_opts, NULL)) != -1) {
4397 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4398 argv[0], argv[optind - 1]);
4405 mdd = argv[optind++];
4407 startrec = strtoll(argv[optind++], NULL, 10);
4409 endrec = strtoll(argv[optind++], NULL, 10);
4411 rc = llapi_changelog_start(&changelog_priv,
4412 CHANGELOG_FLAG_BLOCK |
4413 CHANGELOG_FLAG_JOBID |
4414 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
4417 fprintf(stderr, "Can't start changelog: %s\n",
4418 strerror(errno = -rc));
4422 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
4426 if (endrec && rec->cr_index > endrec) {
4427 llapi_changelog_free(&rec);
4430 if (rec->cr_index < startrec) {
4431 llapi_changelog_free(&rec);
4435 secs = rec->cr_time >> 30;
4436 gmtime_r(&secs, &ts);
4437 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
4438 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
4439 changelog_type2str(rec->cr_type),
4440 ts.tm_hour, ts.tm_min, ts.tm_sec,
4441 (int)(rec->cr_time & ((1 << 30) - 1)),
4442 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
4443 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
4445 if (rec->cr_flags & CLF_JOBID) {
4446 struct changelog_ext_jobid *jid =
4447 changelog_rec_jobid(rec);
4449 if (jid->cr_jobid[0] != '\0')
4450 printf(" j=%s", jid->cr_jobid);
4453 if (rec->cr_namelen)
4454 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
4455 rec->cr_namelen, changelog_rec_name(rec));
4457 if (rec->cr_flags & CLF_RENAME) {
4458 struct changelog_ext_rename *rnm =
4459 changelog_rec_rename(rec);
4461 if (!fid_is_zero(&rnm->cr_sfid))
4462 printf(" s="DFID" sp="DFID" %.*s",
4463 PFID(&rnm->cr_sfid),
4464 PFID(&rnm->cr_spfid),
4465 (int)changelog_rec_snamelen(rec),
4466 changelog_rec_sname(rec));
4470 llapi_changelog_free(&rec);
4473 llapi_changelog_fini(&changelog_priv);
4476 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
4478 return (rc == 1 ? 0 : rc);
4481 static int lfs_changelog_clear(int argc, char **argv)
4489 endrec = strtoll(argv[3], NULL, 10);
4491 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
4494 fprintf(stderr, "%s: record out of range: %llu\n",
4496 else if (rc == -ENOENT)
4497 fprintf(stderr, "%s: no changelog user: %s\n",
4500 fprintf(stderr, "%s error: %s\n", argv[0],
4509 static int lfs_fid2path(int argc, char **argv)
4511 struct option long_opts[] = {
4512 { .val = 'c', .name = "cur", .has_arg = no_argument },
4513 { .val = 'l', .name = "link", .has_arg = required_argument },
4514 { .val = 'r', .name = "rec", .has_arg = required_argument },
4516 char short_opts[] = "cl:r:";
4517 char *device, *fid, *path;
4518 long long recno = -1;
4524 while ((rc = getopt_long(argc, argv, short_opts,
4525 long_opts, NULL)) != -1) {
4531 linkno = strtol(optarg, NULL, 10);
4534 recno = strtoll(optarg, NULL, 10);
4539 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4540 argv[0], argv[optind - 1]);
4548 device = argv[optind++];
4549 path = calloc(1, PATH_MAX);
4551 fprintf(stderr, "error: Not enough memory\n");
4556 while (optind < argc) {
4557 fid = argv[optind++];
4559 lnktmp = (linkno >= 0) ? linkno : 0;
4561 int oldtmp = lnktmp;
4562 long long rectmp = recno;
4564 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
4567 fprintf(stderr, "%s: error on FID %s: %s\n",
4568 argv[0], fid, strerror(errno = -rc2));
4575 fprintf(stdout, "%lld ", rectmp);
4576 if (device[0] == '/') {
4577 fprintf(stdout, "%s", device);
4578 if (device[strlen(device) - 1] != '/')
4579 fprintf(stdout, "/");
4580 } else if (path[0] == '\0') {
4581 fprintf(stdout, "/");
4583 fprintf(stdout, "%s\n", path);
4586 /* specified linkno */
4588 if (oldtmp == lnktmp)
4598 static int lfs_path2fid(int argc, char **argv)
4600 struct option long_opts[] = {
4601 { .val = 'p', .name = "parents", .has_arg = no_argument },
4604 const char short_opts[] = "p";
4605 const char *sep = "";
4608 bool show_parents = false;
4610 while ((rc = getopt_long(argc, argv, short_opts,
4611 long_opts, NULL)) != -1) {
4614 show_parents = true;
4617 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4618 argv[0], argv[optind - 1]);
4623 if (optind > argc - 1)
4625 else if (optind < argc - 1)
4629 for (path = argv + optind; *path != NULL; path++) {
4631 if (!show_parents) {
4632 err = llapi_path2fid(*path, &fid);
4634 printf("%s%s"DFID"\n",
4635 *sep != '\0' ? *path : "", sep,
4638 char name[NAME_MAX + 1];
4639 unsigned int linkno = 0;
4641 while ((err = llapi_path2parent(*path, linkno, &fid,
4642 name, sizeof(name))) == 0) {
4643 if (*sep != '\0' && linkno == 0)
4644 printf("%s%s", *path, sep);
4646 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
4651 /* err == -ENODATA is end-of-loop */
4652 if (linkno > 0 && err == -ENODATA) {
4659 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
4660 argv[0], show_parents ? "parent " : "", *path,
4672 static int lfs_data_version(int argc, char **argv)
4679 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
4684 while ((c = getopt(argc, argv, "nrw")) != -1) {
4687 data_version_flags = 0;
4690 data_version_flags |= LL_DV_RD_FLUSH;
4693 data_version_flags |= LL_DV_WR_FLUSH;
4702 path = argv[optind];
4703 fd = open(path, O_RDONLY);
4705 err(errno, "cannot open file %s", path);
4707 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
4709 err(errno, "cannot get version for %s", path);
4711 printf("%ju" "\n", (uintmax_t)data_version);
4717 static int lfs_hsm_state(int argc, char **argv)
4722 struct hsm_user_state hus;
4730 rc = llapi_hsm_state_get(path, &hus);
4732 fprintf(stderr, "can't get hsm state for %s: %s\n",
4733 path, strerror(errno = -rc));
4737 /* Display path name and status flags */
4738 printf("%s: (0x%08x)", path, hus.hus_states);
4740 if (hus.hus_states & HS_RELEASED)
4741 printf(" released");
4742 if (hus.hus_states & HS_EXISTS)
4744 if (hus.hus_states & HS_DIRTY)
4746 if (hus.hus_states & HS_ARCHIVED)
4747 printf(" archived");
4748 /* Display user-settable flags */
4749 if (hus.hus_states & HS_NORELEASE)
4750 printf(" never_release");
4751 if (hus.hus_states & HS_NOARCHIVE)
4752 printf(" never_archive");
4753 if (hus.hus_states & HS_LOST)
4754 printf(" lost_from_hsm");
4756 if (hus.hus_archive_id != 0)
4757 printf(", archive_id:%d", hus.hus_archive_id);
4760 } while (++i < argc);
4765 #define LFS_HSM_SET 0
4766 #define LFS_HSM_CLEAR 1
4769 * Generic function to set or clear HSM flags.
4770 * Used by hsm_set and hsm_clear.
4772 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
4774 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
4776 struct option long_opts[] = {
4777 { .val = 'A', .name = "archived", .has_arg = no_argument },
4778 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
4779 { .val = 'd', .name = "dirty", .has_arg = no_argument },
4780 { .val = 'e', .name = "exists", .has_arg = no_argument },
4781 { .val = 'l', .name = "lost", .has_arg = no_argument },
4782 { .val = 'r', .name = "norelease", .has_arg = no_argument },
4784 char short_opts[] = "lraAde";
4792 while ((c = getopt_long(argc, argv, short_opts,
4793 long_opts, NULL)) != -1) {
4799 mask |= HS_NOARCHIVE;
4802 mask |= HS_ARCHIVED;
4805 mask |= HS_NORELEASE;
4816 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4817 argv[0], argv[optind - 1]);
4822 /* User should have specified a flag */
4826 while (optind < argc) {
4828 path = argv[optind];
4830 /* If mode == 0, this means we apply the mask. */
4831 if (mode == LFS_HSM_SET)
4832 rc = llapi_hsm_state_set(path, mask, 0, 0);
4834 rc = llapi_hsm_state_set(path, 0, mask, 0);
4837 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
4838 path, strerror(errno = -rc));
4847 static int lfs_hsm_action(int argc, char **argv)
4852 struct hsm_current_action hca;
4853 struct hsm_extent he;
4854 enum hsm_user_action hua;
4855 enum hsm_progress_states hps;
4863 rc = llapi_hsm_current_action(path, &hca);
4865 fprintf(stderr, "can't get hsm action for %s: %s\n",
4866 path, strerror(errno = -rc));
4869 he = hca.hca_location;
4870 hua = hca.hca_action;
4871 hps = hca.hca_state;
4873 printf("%s: %s", path, hsm_user_action2name(hua));
4875 /* Skip file without action */
4876 if (hca.hca_action == HUA_NONE) {
4881 printf(" %s ", hsm_progress_state2name(hps));
4883 if ((hps == HPS_RUNNING) &&
4884 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
4885 printf("(%llu bytes moved)\n",
4886 (unsigned long long)he.length);
4887 else if ((he.offset + he.length) == LUSTRE_EOF)
4888 printf("(from %llu to EOF)\n",
4889 (unsigned long long)he.offset);
4891 printf("(from %llu to %llu)\n",
4892 (unsigned long long)he.offset,
4893 (unsigned long long)(he.offset + he.length));
4895 } while (++i < argc);
4900 static int lfs_hsm_set(int argc, char **argv)
4902 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
4905 static int lfs_hsm_clear(int argc, char **argv)
4907 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
4911 * Check file state and return its fid, to be used by lfs_hsm_request().
4913 * \param[in] file Path to file to check
4914 * \param[in,out] fid Pointer to allocated lu_fid struct.
4915 * \param[in,out] last_dev Pointer to last device id used.
4917 * \return 0 on success.
4919 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
4925 rc = lstat(file, &st);
4927 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
4930 /* Checking for regular file as archiving as posix copytool
4931 * rejects archiving files other than regular files
4933 if (!S_ISREG(st.st_mode)) {
4934 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
4937 /* A request should be ... */
4938 if (*last_dev != st.st_dev && *last_dev != 0) {
4939 fprintf(stderr, "All files should be "
4940 "on the same filesystem: %s\n", file);
4943 *last_dev = st.st_dev;
4945 rc = llapi_path2fid(file, fid);
4947 fprintf(stderr, "Cannot read FID of %s: %s\n",
4948 file, strerror(-rc));
4954 /* Fill an HSM HUR item with a given file name.
4956 * If mntpath is set, then the filename is actually a FID, and no
4957 * lookup on the filesystem will be performed.
4959 * \param[in] hur the user request to fill
4960 * \param[in] idx index of the item inside the HUR to fill
4961 * \param[in] mntpath mountpoint of Lustre
4962 * \param[in] fname filename (if mtnpath is NULL)
4963 * or FID (if mntpath is set)
4964 * \param[in] last_dev pointer to last device id used
4966 * \retval 0 on success
4967 * \retval CMD_HELP or a negative errno on error
4969 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
4970 const char *mntpath, const char *fname,
4973 struct hsm_user_item *hui = &hur->hur_user_item[idx];
4976 hui->hui_extent.length = -1;
4978 if (mntpath != NULL) {
4981 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
4985 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
4990 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
4994 hur->hur_request.hr_itemcount++;
4999 static int lfs_hsm_request(int argc, char **argv, int action)
5001 struct option long_opts[] = {
5002 {"filelist", 1, 0, 'l'},
5003 {"data", 1, 0, 'D'},
5004 {"archive", 1, 0, 'a'},
5005 {"mntpath", 1, 0, 'm'},
5009 char short_opts[] = "l:D:a:m:";
5010 struct hsm_user_request *hur, *oldhur;
5015 char *filelist = NULL;
5016 char fullpath[PATH_MAX];
5017 char *opaque = NULL;
5021 int nbfile_alloc = 0;
5022 char *some_file = NULL;
5023 char *mntpath = NULL;
5029 while ((c = getopt_long(argc, argv, short_opts,
5030 long_opts, NULL)) != -1) {
5039 if (action != HUA_ARCHIVE &&
5040 action != HUA_REMOVE) {
5042 "error: -a is supported only "
5043 "when archiving or removing\n");
5046 archive_id = atoi(optarg);
5049 if (some_file == NULL) {
5051 some_file = strdup(optarg);
5057 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5058 argv[0], argv[optind - 1]);
5063 /* All remaining args are files, so we have at least nbfile */
5064 nbfile = argc - optind;
5066 if ((nbfile == 0) && (filelist == NULL))
5070 opaque_len = strlen(opaque);
5072 /* Alloc the request structure with enough place to store all files
5073 * from command line. */
5074 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
5076 fprintf(stderr, "Cannot create the request: %s\n",
5080 nbfile_alloc = nbfile;
5082 hur->hur_request.hr_action = action;
5083 hur->hur_request.hr_archive_id = archive_id;
5084 hur->hur_request.hr_flags = 0;
5086 /* All remaining args are files, add them */
5087 if (nbfile != 0 && some_file == NULL)
5088 some_file = strdup(argv[optind]);
5090 for (i = 0; i < nbfile; i++) {
5091 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
5097 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
5099 /* If a filelist was specified, read the filelist from it. */
5100 if (filelist != NULL) {
5101 fp = fopen(filelist, "r");
5103 fprintf(stderr, "Cannot read the file list %s: %s\n",
5104 filelist, strerror(errno));
5109 while ((rc = getline(&line, &len, fp)) != -1) {
5110 /* If allocated buffer was too small, get something
5112 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
5115 nbfile_alloc = nbfile_alloc * 2 + 1;
5117 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
5120 fprintf(stderr, "hsm: cannot allocate "
5121 "the request: %s\n",
5128 size = hur_len(oldhur);
5130 fprintf(stderr, "hsm: cannot allocate "
5131 "%u files + %u bytes data\n",
5132 oldhur->hur_request.hr_itemcount,
5133 oldhur->hur_request.hr_data_len);
5140 memcpy(hur, oldhur, size);
5145 if (line[strlen(line) - 1] == '\n')
5146 line[strlen(line) - 1] = '\0';
5148 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
5149 mntpath, line, &last_dev);
5155 if (some_file == NULL) {
5165 /* If a --data was used, add it to the request */
5166 hur->hur_request.hr_data_len = opaque_len;
5168 memcpy(hur_data(hur), opaque, opaque_len);
5170 /* Send the HSM request */
5171 if (realpath(some_file, fullpath) == NULL) {
5172 fprintf(stderr, "Could not find path '%s': %s\n",
5173 some_file, strerror(errno));
5175 rc = llapi_hsm_request(fullpath, hur);
5177 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
5178 some_file, strerror(-rc));
5188 static int lfs_hsm_archive(int argc, char **argv)
5190 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
5193 static int lfs_hsm_restore(int argc, char **argv)
5195 return lfs_hsm_request(argc, argv, HUA_RESTORE);
5198 static int lfs_hsm_release(int argc, char **argv)
5200 return lfs_hsm_request(argc, argv, HUA_RELEASE);
5203 static int lfs_hsm_remove(int argc, char **argv)
5205 return lfs_hsm_request(argc, argv, HUA_REMOVE);
5208 static int lfs_hsm_cancel(int argc, char **argv)
5210 return lfs_hsm_request(argc, argv, HUA_CANCEL);
5213 static int lfs_swap_layouts(int argc, char **argv)
5218 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
5219 SWAP_LAYOUTS_KEEP_MTIME |
5220 SWAP_LAYOUTS_KEEP_ATIME);
5223 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
5225 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
5227 enum lu_ladvise_type advice;
5230 advice < ARRAY_SIZE(ladvise_names); advice++) {
5231 if (ladvise_names[advice] == NULL)
5233 if (strcmp(string, ladvise_names[advice]) == 0)
5237 return LU_LADVISE_INVALID;
5240 static int lfs_ladvise(int argc, char **argv)
5242 struct option long_opts[] = {
5243 { .val = 'a', .name = "advice", .has_arg = required_argument },
5244 { .val = 'b', .name = "background", .has_arg = no_argument },
5245 { .val = 'e', .name = "end", .has_arg = required_argument },
5246 { .val = 'l', .name = "length", .has_arg = required_argument },
5247 { .val = 's', .name = "start", .has_arg = required_argument },
5249 char short_opts[] = "a:be:l:s:";
5254 struct llapi_lu_ladvise advice;
5255 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
5256 unsigned long long start = 0;
5257 unsigned long long end = LUSTRE_EOF;
5258 unsigned long long length = 0;
5259 unsigned long long size_units;
5260 unsigned long long flags = 0;
5263 while ((c = getopt_long(argc, argv, short_opts,
5264 long_opts, NULL)) != -1) {
5267 advice_type = lfs_get_ladvice(optarg);
5268 if (advice_type == LU_LADVISE_INVALID) {
5269 fprintf(stderr, "%s: invalid advice type "
5270 "'%s'\n", argv[0], optarg);
5271 fprintf(stderr, "Valid types:");
5273 for (advice_type = 0;
5274 advice_type < ARRAY_SIZE(ladvise_names);
5276 if (ladvise_names[advice_type] == NULL)
5278 fprintf(stderr, " %s",
5279 ladvise_names[advice_type]);
5281 fprintf(stderr, "\n");
5291 rc = llapi_parse_size(optarg, &end,
5294 fprintf(stderr, "%s: bad end offset '%s'\n",
5301 rc = llapi_parse_size(optarg, &start,
5304 fprintf(stderr, "%s: bad start offset "
5305 "'%s'\n", argv[0], optarg);
5311 rc = llapi_parse_size(optarg, &length,
5314 fprintf(stderr, "%s: bad length '%s'\n",
5322 fprintf(stderr, "%s: option '%s' unrecognized\n",
5323 argv[0], argv[optind - 1]);
5328 if (advice_type == LU_LADVISE_INVALID) {
5329 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
5330 fprintf(stderr, "Valid types:");
5331 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
5333 if (ladvise_names[advice_type] == NULL)
5335 fprintf(stderr, " %s", ladvise_names[advice_type]);
5337 fprintf(stderr, "\n");
5341 if (argc <= optind) {
5342 fprintf(stderr, "%s: please give one or more file names\n",
5347 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
5348 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
5353 if (end == LUSTRE_EOF && length != 0)
5354 end = start + length;
5357 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
5358 argv[0], start, end);
5362 while (optind < argc) {
5365 path = argv[optind++];
5367 fd = open(path, O_RDONLY);
5369 fprintf(stderr, "%s: cannot open file '%s': %s\n",
5370 argv[0], path, strerror(errno));
5375 advice.lla_start = start;
5376 advice.lla_end = end;
5377 advice.lla_advice = advice_type;
5378 advice.lla_value1 = 0;
5379 advice.lla_value2 = 0;
5380 advice.lla_value3 = 0;
5381 advice.lla_value4 = 0;
5382 rc2 = llapi_ladvise(fd, flags, 1, &advice);
5385 fprintf(stderr, "%s: cannot give advice '%s' to file "
5386 "'%s': %s\n", argv[0],
5387 ladvise_names[advice_type],
5388 path, strerror(errno));
5391 if (rc == 0 && rc2 < 0)
5397 static int lfs_list_commands(int argc, char **argv)
5399 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
5401 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
5406 int main(int argc, char **argv)
5410 /* Ensure that liblustreapi constructor has run */
5411 if (!liblustreapi_initialized)
5412 fprintf(stderr, "liblustreapi was not properly initialized\n");
5416 Parser_init("lfs > ", cmdlist);
5418 progname = argv[0]; /* Used in error messages */
5420 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
5422 rc = Parser_commands();
5425 return rc < 0 ? -rc : rc;
5428 #ifdef _LUSTRE_IDL_H_
5429 /* Everything we need here should be included by lustreapi.h. */
5430 # error "lfs should not depend on lustre_idl.h"
5431 #endif /* _LUSTRE_IDL_H_ */