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 <linux/lustre/lustre_ver.h>
69 #include <linux/lustre/lustre_param.h>
72 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
73 #endif /* !ARRAY_SIZE */
76 static int lfs_find(int argc, char **argv);
77 static int lfs_getstripe(int argc, char **argv);
78 static int lfs_getdirstripe(int argc, char **argv);
79 static int lfs_setdirstripe(int argc, char **argv);
80 static int lfs_rmentry(int argc, char **argv);
81 static int lfs_osts(int argc, char **argv);
82 static int lfs_mdts(int argc, char **argv);
83 static int lfs_df(int argc, char **argv);
84 static int lfs_getname(int argc, char **argv);
85 static int lfs_check(int argc, char **argv);
86 #ifdef HAVE_SYS_QUOTA_H
87 static int lfs_setquota(int argc, char **argv);
88 static int lfs_quota(int argc, char **argv);
90 static int lfs_flushctx(int argc, char **argv);
91 static int lfs_cp(int argc, char **argv);
92 static int lfs_ls(int argc, char **argv);
93 static int lfs_poollist(int argc, char **argv);
94 static int lfs_changelog(int argc, char **argv);
95 static int lfs_changelog_clear(int argc, char **argv);
96 static int lfs_fid2path(int argc, char **argv);
97 static int lfs_path2fid(int argc, char **argv);
98 static int lfs_data_version(int argc, char **argv);
99 static int lfs_hsm_state(int argc, char **argv);
100 static int lfs_hsm_set(int argc, char **argv);
101 static int lfs_hsm_clear(int argc, char **argv);
102 static int lfs_hsm_action(int argc, char **argv);
103 static int lfs_hsm_archive(int argc, char **argv);
104 static int lfs_hsm_restore(int argc, char **argv);
105 static int lfs_hsm_release(int argc, char **argv);
106 static int lfs_hsm_remove(int argc, char **argv);
107 static int lfs_hsm_cancel(int argc, char **argv);
108 static int lfs_swap_layouts(int argc, char **argv);
109 static int lfs_mv(int argc, char **argv);
110 static int lfs_ladvise(int argc, char **argv);
111 static int lfs_mirror(int argc, char **argv);
112 static int lfs_mirror_list_commands(int argc, char **argv);
113 static int lfs_list_commands(int argc, char **argv);
114 static inline int lfs_mirror_resync(int argc, char **argv);
116 enum setstripe_origin {
122 static int lfs_setstripe0(int argc, char **argv, enum setstripe_origin opc);
124 static inline int lfs_setstripe(int argc, char **argv)
126 return lfs_setstripe0(argc, argv, SO_SETSTRIPE);
128 static inline int lfs_setstripe_migrate(int argc, char **argv)
130 return lfs_setstripe0(argc, argv, SO_MIGRATE);
132 static inline int lfs_mirror_create(int argc, char **argv)
134 return lfs_setstripe0(argc, argv, SO_MIRROR_CREATE);
136 static inline int lfs_mirror_extend(int argc, char **argv)
138 return lfs_setstripe0(argc, argv, SO_MIRROR_EXTEND);
141 /* Setstripe and migrate share mostly the same parameters */
142 #define SSM_CMD_COMMON(cmd) \
143 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
144 " [--stripe-count|-c <stripe_count>]\n" \
145 " [--stripe-index|-i <start_ost_idx>]\n" \
146 " [--stripe-size|-S <stripe_size>]\n" \
147 " [--layout|-L <pattern>]\n" \
148 " [--pool|-p <pool_name>]\n" \
149 " [--ost|-o <ost_indices>]\n"
151 #define SSM_HELP_COMMON \
152 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
153 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
154 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
155 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
156 "\t respectively)\n" \
157 "\tpool_name: Name of OST pool to use (default none)\n" \
158 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
159 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
160 "\t Indices be specified in a format of:\n" \
161 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
163 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
164 "\t If --pool is set with --ost, then the OSTs\n" \
165 "\t must be the members of the pool.\n" \
166 "\tcomp_end: Extent end of component, start after previous end.\n"\
167 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
168 "\t respectively, -1 for EOF). Must be a multiple of\n"\
171 #define MIRROR_CREATE_HELP \
172 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
173 "\t setstripe layout options\n" \
174 "\t It defaults to 1 if not specified; if specified,\n" \
175 "\t it must follow the option without a space.\n" \
176 "\t The option can also be repeated multiple times to\n" \
177 "\t separate mirrors that have different layouts.\n" \
178 "\tsetstripe options: Mirror layout\n" \
179 "\t It can be a plain layout or a composite layout.\n" \
180 "\t If not specified, the stripe options inherited\n" \
181 "\t from the previous component will be used.\n" \
182 "\tparent: Use default stripe options from parent directory\n"
184 #define MIRROR_EXTEND_HELP \
186 "\tvictim_file: The layout of victim_file will be split and used\n" \
187 "\t as a mirror added to the mirrored file.\n" \
188 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
189 "\t from victim file(s) in case the victim file(s)\n" \
190 "\t contains the same data as the original mirrored\n" \
193 #define MIRROR_EXTEND_USAGE \
194 " <--mirror-count|-N[mirror_count]>\n" \
195 " [setstripe options|--parent|-f <victim_file>]\n" \
198 #define SETSTRIPE_USAGE \
199 SSM_CMD_COMMON("setstripe") \
200 MIRROR_EXTEND_USAGE \
201 " <directory|filename>\n" \
205 #define MIGRATE_USAGE \
206 SSM_CMD_COMMON("migrate ") \
208 " [--non-block|-n]\n" \
212 "\tblock: Block file access during data migration (default)\n" \
213 "\tnon-block: Abort migrations if concurrent access is detected\n" \
215 #define SETDIRSTRIPE_USAGE \
216 " [--mdt-count|-c stripe_count>\n" \
217 " [--mdt-index|-i mdt_index]\n" \
218 " [--mdt-hash|-H mdt_hash]\n" \
219 " [--default|-D] [--mode|-m mode] <dir>\n" \
220 "\tstripe_count: stripe count of the striped directory\n" \
221 "\tmdt_index: MDT index of first stripe\n" \
222 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
223 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
224 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
225 "\tdefault_stripe: set default dirstripe of the directory\n" \
226 "\tmode: the mode of the directory\n"
228 static const char *progname;
231 * command_t mirror_cmdlist - lfs mirror commands.
233 command_t mirror_cmdlist[] = {
234 { .pc_name = "create", .pc_func = lfs_mirror_create,
235 .pc_help = "Create a mirrored file.\n"
236 "usage: lfs mirror create "
237 "<--mirror-count|-N[mirror_count]> "
238 "[setstripe options|--parent] ... <filename|directory>\n"
239 MIRROR_CREATE_HELP },
240 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
241 .pc_help = "Extend a mirrored file.\n"
242 "usage: lfs mirror extend "
243 "<--mirror-count|-N[mirror_count]> [--no-verify] "
244 "[setstripe options|--parent|-f <victim_file>] ... <filename>\n"
245 MIRROR_EXTEND_HELP },
246 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
247 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
248 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
249 "<mirrored file> [<mirrored file2>...]\n"},
250 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
251 .pc_help = "list commands supported by lfs mirror"},
252 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
253 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
254 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
258 /* all available commands */
259 command_t cmdlist[] = {
260 {"setstripe", lfs_setstripe, 0,
261 "To create a file with specified striping/composite layout, or\n"
262 "create/replace the default layout on an existing directory:\n"
263 SSM_CMD_COMMON("setstripe")
264 " <directory|filename>\n"
266 "To add component(s) to an existing composite file:\n"
267 SSM_CMD_COMMON("setstripe --component-add")
269 "To totally delete the default striping from an existing directory:\n"
270 "usage: setstripe -d <directory>\n"
272 "To delete the last component(s) from an existing composite file\n"
273 "(note that this will also delete any data in those components):\n"
274 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
275 " [--component-flags|-F <comp_flags>]\n"
277 "\tcomp_id: Unique component ID to delete\n"
278 "\tcomp_flags: 'init' indicating all instantiated components\n"
279 "\t '^init' indicating all uninstantiated components\n"
280 "\t-I and -F cannot be specified at the same time\n"},
281 {"getstripe", lfs_getstripe, 0,
282 "To list the striping info for a given file or files in a\n"
283 "directory or recursively for all files in a directory tree.\n"
284 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
285 " [--stripe-count|-c] [--stripe-index|-i]\n"
286 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
287 " [--mdt|-m] [--recursive|-r] [--raw|-R] [--yaml|-y]\n"
288 " [--layout|-L] [--fid|-F] [--generation|-g]\n"
289 " [--component-id[=comp_id]|-I[comp_id]]\n"
290 " [--component-flags[=comp_flags]]\n"
291 " [--component-count]\n"
292 " [--component-start[=[+-]comp_start]]\n"
293 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
294 " <directory|filename> ..."},
295 {"setdirstripe", lfs_setdirstripe, 0,
296 "To create a striped directory on a specified MDT. This can only\n"
297 "be done on MDT0 with the right of administrator.\n"
298 "usage: setdirstripe [OPTION] <directory>\n"
300 {"getdirstripe", lfs_getdirstripe, 0,
301 "To list the striping info for a given directory\n"
302 "or recursively for all directories in a directory tree.\n"
303 "usage: getdirstripe [--obd|-O <uuid>] [--mdt-count|-c]\n"
304 " [--mdt-index|-i] [--mdt-hash|-t]\n"
305 " [--recursive|-r] [--yaml|-y]\n"
306 " [--default|-D] <dir> ..."},
307 {"mkdir", lfs_setdirstripe, 0,
308 "To create a striped directory on a specified MDT. This can only\n"
309 "be done on MDT0 with the right of administrator.\n"
310 "usage: mkdir [OPTION] <directory>\n"
312 {"rm_entry", lfs_rmentry, 0,
313 "To remove the name entry of the remote directory. Note: This\n"
314 "command will only delete the name entry, i.e. the remote directory\n"
315 "will become inaccessable after this command. This can only be done\n"
316 "by the administrator\n"
317 "usage: rm_entry <dir>\n"},
318 {"pool_list", lfs_poollist, 0,
319 "List pools or pool OSTs\n"
320 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
321 {"find", lfs_find, 0,
322 "find files matching given attributes recursively in directory tree.\n"
323 "usage: find <directory|filename> ...\n"
324 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
325 " [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
326 " [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
327 " [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
328 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
329 " [[!] --stripe-count|-c [+-]<stripes>]\n"
330 " [[!] --stripe-index|-i <index,...>]\n"
331 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
332 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
333 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
334 " [[!] --projid <projid>]\n"
335 " [[!] --layout|-L released,raid0,mdt]\n"
336 " [[!] --component-count [+-]<comp_cnt>]\n"
337 " [[!] --component-start [+-]N[kMGTPE]]\n"
338 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
339 " [[!] --component-flags <comp_flags>]\n"
340 " [[!] --mdt-count|-T [+-]<stripes>]\n"
341 " [[!] --mdt-hash|-H <hashtype>\n"
342 "\t !: used before an option indicates 'NOT' requested attribute\n"
343 "\t -: used before a value indicates less than requested value\n"
344 "\t +: used before a value indicates more than requested value\n"
345 "\tmdt-hash: hash type of the striped directory.\n"
346 "\t fnv_1a_64 FNV-1a hash algorithm\n"
347 "\t all_char sum of characters % MDT_COUNT\n"},
348 {"check", lfs_check, 0,
349 "Display the status of MDS or OSTs (as specified in the command)\n"
350 "or all the servers (MDS and OSTs).\n"
351 "usage: check <osts|mds|servers>"},
352 {"osts", lfs_osts, 0, "list OSTs connected to client "
353 "[for specified path only]\n" "usage: osts [path]"},
354 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
355 "[for specified path only]\n" "usage: mdts [path]"},
357 "report filesystem disk space usage or inodes usage"
358 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
359 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
360 {"getname", lfs_getname, 0, "list instances and specified mount points "
361 "[for specified path only]\n"
362 "Usage: getname [-h]|[path ...] "},
363 #ifdef HAVE_SYS_QUOTA_H
364 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
365 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
366 " -b <block-softlimit> -B <block-hardlimit>\n"
367 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
368 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
369 " [--block-softlimit <block-softlimit>]\n"
370 " [--block-hardlimit <block-hardlimit>]\n"
371 " [--inode-softlimit <inode-softlimit>]\n"
372 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
373 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
374 " [--block-grace <block-grace>]\n"
375 " [--inode-grace <inode-grace>] <filesystem>\n"
376 " -b can be used instead of --block-softlimit/--block-grace\n"
377 " -B can be used instead of --block-hardlimit\n"
378 " -i can be used instead of --inode-softlimit/--inode-grace\n"
379 " -I can be used instead of --inode-hardlimit\n\n"
380 "Note: The total quota space will be split into many qunits and\n"
381 " balanced over all server targets, the minimal qunit size is\n"
382 " 1M bytes for block space and 1K inodes for inode space.\n\n"
383 " Quota space rebalancing process will stop when this mininum\n"
384 " value is reached. As a result, quota exceeded can be returned\n"
385 " while many targets still have 1MB or 1K inodes of spare\n"
387 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
388 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
390 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
391 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
393 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
394 "usage: flushctx [-k] [mountpoint...]"},
396 "Remote user copy files and directories.\n"
397 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
399 "Remote user list directory contents.\n"
400 "usage: ls [OPTION]... [FILE]..."},
401 {"changelog", lfs_changelog, 0,
402 "Show the metadata changes on an MDT."
403 "\nusage: changelog <mdtname> [startrec [endrec]]"},
404 {"changelog_clear", lfs_changelog_clear, 0,
405 "Indicate that old changelog records up to <endrec> are no longer of "
406 "interest to consumer <id>, allowing the system to free up space.\n"
407 "An <endrec> of 0 means all records.\n"
408 "usage: changelog_clear <mdtname> <id> <endrec>"},
409 {"fid2path", lfs_fid2path, 0,
410 "Resolve the full path(s) for given FID(s). For a specific hardlink "
411 "specify link number <linkno>.\n"
412 /* "For a historical link name, specify changelog record <recno>.\n" */
413 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
414 /* [ --rec <recno> ] */ },
415 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
416 "usage: path2fid [--parents] <path> ..."},
417 {"data_version", lfs_data_version, 0, "Display file data version for "
418 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
419 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
420 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
421 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
422 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
423 "[--archived] [--lost] <file> ..."},
424 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
426 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
427 "[--archived] [--lost] <file> ..."},
428 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
429 "given files.\n" "usage: hsm_action <file> ..."},
430 {"hsm_archive", lfs_hsm_archive, 0,
431 "Archive file to external storage.\n"
432 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
434 {"hsm_restore", lfs_hsm_restore, 0,
435 "Restore file from external storage.\n"
436 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
437 {"hsm_release", lfs_hsm_release, 0,
438 "Release files from Lustre.\n"
439 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
440 {"hsm_remove", lfs_hsm_remove, 0,
441 "Remove file copy from external storage.\n"
442 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
444 " (FILE [FILE ...] | "
445 "--mntpath MOUNTPATH FID [FID ...])\n"
447 "Note: To remove an archived copy of a file already deleted from a "
449 "--mntpath option and a list of FIDs must be specified"
451 {"hsm_cancel", lfs_hsm_cancel, 0,
452 "Cancel requests related to specified files.\n"
453 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
454 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
455 "usage: swap_layouts <path1> <path2>"},
456 {"migrate", lfs_setstripe_migrate, 0,
457 "migrate a directory between MDTs.\n"
458 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
460 "\tmdt_idx: index of the destination MDT\n"
462 "migrate file objects from one OST "
463 "layout\nto another (may be not safe with concurent writes).\n"
465 "[--stripe-count|-c] <stripe_count>\n"
466 " [--stripe-index|-i] <start_ost_index>\n"
467 " [--stripe-size|-S] <stripe_size>\n"
468 " [--pool|-p] <pool_name>\n"
469 " [--ost-list|-o] <ost_indices>\n"
471 " [--non-block|-n]\n"
472 " <file|directory>\n"
473 "\tstripe_count: number of OSTs to stripe a file over\n"
474 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
475 "\tstripe_size: number of bytes to store before moving to the next OST\n"
476 "\tpool_name: name of the predefined pool of OSTs\n"
477 "\tost_indices: OSTs to stripe over, in order\n"
478 "\tblock: wait for the operation to return before continuing\n"
479 "\tnon-block: do not wait for the operation to return.\n"},
481 "To move directories between MDTs. This command is deprecated, "
482 "use \"migrate\" instead.\n"
483 "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
485 {"ladvise", lfs_ladvise, 0,
486 "Provide servers with advice about access patterns for a file.\n"
487 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
488 " [--background|-b] [--unset|-u]\n\n"
489 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
490 " {[--mode|-m [READ,WRITE]}\n"
492 {"mirror", lfs_mirror, mirror_cmdlist,
493 "lfs commands used to manage files with mirrored components:\n"
494 "lfs mirror create - create a mirrored file or directory\n"
495 "lfs mirror extend - add mirror(s) to an existing file\n"
496 "lfs mirror split - split a mirror from an existing mirrored file\n"
497 "lfs mirror resync - resynchronize an out-of-sync mirrored file\n"
498 "lfs mirror verify - verify a mirrored file\n"},
499 {"help", Parser_help, 0, "help"},
500 {"exit", Parser_quit, 0, "quit"},
501 {"quit", Parser_quit, 0, "quit"},
502 {"--version", Parser_version, 0,
503 "output build version of the utility and exit"},
504 {"--list-commands", lfs_list_commands, 0,
505 "list commands supported by the utility and exit"},
510 static int check_hashtype(const char *hashtype)
514 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
515 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
522 static const char *error_loc = "syserror";
525 MIGRATION_NONBLOCK = 1 << 0,
526 MIGRATION_MIRROR = 1 << 1,
529 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
530 struct llapi_layout *layout);
533 migrate_open_files(const char *name, const struct llapi_stripe_param *param,
534 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
540 char parent[PATH_MAX];
541 char volatile_file[PATH_MAX];
547 if (param == NULL && layout == NULL) {
548 error_loc = "layout information";
552 /* search for file directory pathname */
553 if (strlen(name) > sizeof(parent) - 1) {
554 error_loc = "source file name";
558 strncpy(parent, name, sizeof(parent));
559 ptr = strrchr(parent, '/');
561 if (getcwd(parent, sizeof(parent)) == NULL) {
562 error_loc = "getcwd";
566 if (ptr == parent) /* leading '/' */
571 /* open file, direct io */
572 /* even if the file is only read, WR mode is nedeed to allow
573 * layout swap on fd */
574 fd = open(name, O_RDWR | O_DIRECT);
577 error_loc = "cannot open source file";
581 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
583 error_loc = "cannot get MDT index";
588 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
589 mode_t open_mode = S_IRUSR | S_IWUSR;
591 random_value = random();
592 rc = snprintf(volatile_file, sizeof(volatile_file),
593 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
594 mdt_index, random_value);
595 if (rc >= sizeof(volatile_file)) {
600 /* create, open a volatile file, use caching (ie no directio) */
602 fdv = llapi_file_open_param(volatile_file, open_flags,
605 fdv = lfs_component_create(volatile_file, open_flags,
607 } while (fdv < 0 && (rc = fdv) == -EEXIST);
610 error_loc = "cannot create volatile file";
614 /* In case the MDT does not support creation of volatile files
615 * we should try to unlink it. */
616 (void)unlink(volatile_file);
618 /* Not-owner (root?) special case.
619 * Need to set owner/group of volatile file like original.
620 * This will allow to pass related check during layout_swap.
625 error_loc = "cannot stat source file";
629 rc = fstat(fdv, &stv);
632 error_loc = "cannot stat volatile";
636 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
637 rc = fchown(fdv, st.st_uid, st.st_gid);
640 error_loc = "cannot change ownwership of volatile";
659 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
661 struct llapi_layout *layout;
662 size_t buf_size = 4 * 1024 * 1024;
671 layout = llapi_layout_get_by_fd(fd_src, 0);
672 if (layout != NULL) {
673 uint64_t stripe_size;
675 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
677 buf_size = stripe_size;
679 llapi_layout_free(layout);
682 /* Use a page-aligned buffer for direct I/O */
683 rc = posix_memalign(&buf, getpagesize(), buf_size);
688 /* read new data only if we have written all
689 * previously read data */
692 rc = check_file(fd_src);
697 rsize = read(fd_src, buf, buf_size);
709 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
728 static int migrate_copy_timestamps(int fd, int fdv)
732 if (fstat(fd, &st) == 0) {
733 struct timeval tv[2] = {
734 {.tv_sec = st.st_atime},
735 {.tv_sec = st.st_mtime}
738 return futimes(fdv, tv);
744 static int migrate_block(int fd, int fdv)
751 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
753 error_loc = "cannot get dataversion";
761 /* The grouplock blocks all concurrent accesses to the file.
762 * It has to be taken after llapi_get_data_version as it would
764 rc = llapi_group_lock(fd, gid);
766 error_loc = "cannot get group lock";
770 rc = migrate_copy_data(fd, fdv, NULL);
772 error_loc = "data copy failed";
776 /* Make sure we keep original atime/mtime values */
777 rc = migrate_copy_timestamps(fd, fdv);
779 error_loc = "timestamp copy failed";
784 * for a migration we need to check data version on file did
787 * Pass in gid=0 since we already own grouplock. */
788 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
789 SWAP_LAYOUTS_CHECK_DV1);
791 error_loc = "file changed";
794 error_loc = "cannot swap layout";
799 rc2 = llapi_group_unlock(fd, gid);
800 if (rc2 < 0 && rc == 0) {
801 error_loc = "unlock group lock";
809 * Internal helper for migrate_copy_data(). Check lease and report error if
812 * \param[in] fd File descriptor on which to check the lease.
814 * \retval 0 Migration can keep on going.
815 * \retval -errno Error occurred, abort migration.
817 static int check_lease(int fd)
821 rc = llapi_lease_check(fd);
823 return 0; /* llapi_check_lease returns > 0 on success. */
828 static int migrate_nonblock(int fd, int fdv)
834 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
836 error_loc = "cannot get data version";
840 rc = migrate_copy_data(fd, fdv, check_lease);
842 error_loc = "data copy failed";
846 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
848 error_loc = "cannot get data version";
854 error_loc = "source file changed";
858 /* Make sure we keep original atime/mtime values */
859 rc = migrate_copy_timestamps(fd, fdv);
861 error_loc = "timestamp copy failed";
868 static int lfs_component_set(char *fname, int comp_id, __u32 flags)
873 static int lfs_component_del(char *fname, __u32 comp_id, __u32 flags)
877 if (flags != 0 && comp_id != 0)
880 /* LCME_FL_INIT is the only supported flag in PFL */
882 if (flags & ~LCME_KNOWN_FLAGS) {
884 "%s setstripe: bad component flags %#x\n",
888 } else if (comp_id > LCME_ID_MAX) {
889 fprintf(stderr, "%s setstripe: bad component id %u\n",
894 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
897 "%s setstripe: cannot delete component %#x from '%s': %s\n",
898 progname, comp_id, fname, strerror(errno));
902 static int lfs_component_add(char *fname, struct llapi_layout *layout)
909 rc = llapi_layout_file_comp_add(fname, layout);
911 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
912 fname, strerror(errno));
916 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
917 struct llapi_layout *layout)
925 fd = lstat(fname, &st);
926 if (fd == 0 && S_ISDIR(st.st_mode))
927 open_flags = O_DIRECTORY | O_RDONLY;
929 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
931 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
932 S_ISDIR(st.st_mode) ?
933 "set default composite layout for" :
934 "create composite file",
935 fname, strerror(errno));
939 static int lfs_migrate(char *name, __u64 migration_flags,
940 struct llapi_stripe_param *param,
941 struct llapi_layout *layout)
947 rc = migrate_open_files(name, param, layout, &fd, &fdv);
951 if (!(migration_flags & MIGRATION_NONBLOCK)) {
952 /* Blocking mode (forced if servers do not support file lease).
953 * It is also the default mode, since we cannot distinguish
954 * between a broken lease and a server that does not support
955 * atomic swap/close (LU-6785) */
956 rc = migrate_block(fd, fdv);
960 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
962 error_loc = "cannot get lease";
966 rc = migrate_nonblock(fd, fdv);
972 /* Atomically put lease, swap layouts and close.
973 * for a migration we need to check data version on file did
975 rc = llapi_fswap_layouts(fd, fdv, 0, 0,
976 migration_flags & MIGRATION_MIRROR ?
977 MERGE_LAYOUTS_CLOSE : SWAP_LAYOUTS_CLOSE);
979 error_loc = "cannot swap layout";
991 fprintf(stderr, "error: %s: %s: %s: %s\n",
992 progname, name, error_loc, strerror(-rc));
997 * struct mirror_args - Command-line arguments for mirror(s).
998 * @m_count: Number of mirrors to be created with this layout.
999 * @m_layout: Mirror layout.
1000 * @m_file: A victim file. Its layout will be split and used as a mirror.
1001 * @m_next: Point to the next node of the list.
1003 * Command-line arguments for mirror(s) will be parsed and stored in
1004 * a linked list that consists of this structure.
1006 struct mirror_args {
1008 struct llapi_layout *m_layout;
1010 struct mirror_args *m_next;
1013 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1015 uint64_t start, end;
1019 /* LU-10112: do not support dom+flr in phase 1 */
1020 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1024 rc = llapi_layout_pattern_get(layout, &pattern);
1028 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1029 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1034 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1038 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1042 if (end != LUSTRE_EOF) {
1043 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1052 * enum mirror_flags - Flags for extending a mirrored file.
1053 * @NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1054 * in case the victim file(s) contains the same data as the
1055 * original mirrored file.
1057 * Flags for extending a mirrored file.
1064 * mirror_create_sanity_check() - Check mirror list.
1065 * @list: A linked list that stores the mirror arguments.
1067 * This function does a sanity check on @list for creating
1070 * Return: 0 on success or a negative error code on failure.
1072 static int mirror_create_sanity_check(const char *fname,
1073 struct mirror_args *list)
1076 bool has_m_file = false;
1077 bool has_m_layout = false;
1083 struct llapi_layout *layout;
1085 layout = llapi_layout_get_by_path(fname, 0);
1088 "error: %s: file '%s' couldn't get layout\n",
1093 rc = mirror_sanity_check_one(layout);
1094 llapi_layout_free(layout);
1100 while (list != NULL) {
1101 if (list->m_file != NULL) {
1103 llapi_layout_free(list->m_layout);
1106 llapi_layout_get_by_path(list->m_file, 0);
1107 if (list->m_layout == NULL) {
1109 "error: %s: file '%s' has no layout\n",
1110 progname, list->m_file);
1114 if (list->m_layout != NULL)
1115 has_m_layout = true;
1117 fprintf(stderr, "error: %s: no mirror layout\n",
1123 rc = mirror_sanity_check_one(list->m_layout);
1127 list = list->m_next;
1130 if (has_m_file && has_m_layout) {
1131 fprintf(stderr, "error: %s: -f <victim_file> option should not "
1132 "be specified with setstripe options or "
1133 "--parent option\n", progname);
1141 * mirror_create() - Create a mirrored file.
1142 * @fname: The file to be created.
1143 * @mirror_list: A linked list that stores the mirror arguments.
1145 * This function creates a mirrored file @fname with the mirror(s)
1146 * from @mirror_list.
1148 * Return: 0 on success or a negative error code on failure.
1150 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1152 struct llapi_layout *layout = NULL;
1153 struct mirror_args *cur_mirror = NULL;
1154 uint16_t mirror_count = 0;
1158 rc = mirror_create_sanity_check(NULL, mirror_list);
1162 cur_mirror = mirror_list;
1163 while (cur_mirror != NULL) {
1164 for (i = 0; i < cur_mirror->m_count; i++) {
1165 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1168 fprintf(stderr, "error: %s: "
1169 "merge layout failed: %s\n",
1170 progname, strerror(errno));
1174 mirror_count += cur_mirror->m_count;
1175 cur_mirror = cur_mirror->m_next;
1178 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1181 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1182 progname, strerror(errno));
1186 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0644,
1194 llapi_layout_free(layout);
1199 * Compare files and check lease on @fd.
1201 * \retval bytes number of bytes are the same
1203 static ssize_t mirror_file_compare(int fd, int fdv)
1205 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1207 ssize_t bytes_done = 0;
1208 ssize_t bytes_read = 0;
1210 buf = malloc(buflen * 2);
1215 if (!llapi_lease_check(fd)) {
1216 bytes_done = -EBUSY;
1220 bytes_read = read(fd, buf, buflen);
1221 if (bytes_read <= 0)
1224 if (bytes_read != read(fdv, buf + buflen, buflen))
1227 /* XXX: should compute the checksum on each buffer and then
1228 * compare checksum to avoid cache collision */
1229 if (memcmp(buf, buf + buflen, bytes_read))
1232 bytes_done += bytes_read;
1240 static int mirror_extend_file(const char *fname, const char *victim_file,
1241 enum mirror_flags mirror_flags)
1246 struct stat stbuf_v;
1250 fd = open(fname, O_RDWR);
1252 error_loc = "open source file";
1257 fdv = open(victim_file, O_RDWR);
1259 error_loc = "open target file";
1264 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1265 error_loc = "stat source or target file";
1270 if (stbuf.st_dev != stbuf_v.st_dev) {
1271 error_loc = "stat source and target file";
1276 /* mirrors should be of the same size */
1277 if (stbuf.st_size != stbuf_v.st_size) {
1278 error_loc = "file sizes don't match";
1283 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
1285 error_loc = "cannot get lease";
1289 if (!(mirror_flags & NO_VERIFY)) {
1291 /* mirrors should have the same contents */
1292 ret = mirror_file_compare(fd, fdv);
1293 if (ret != stbuf.st_size) {
1294 error_loc = "file busy or contents don't match";
1295 rc = ret < 0 ? ret : -EINVAL;
1300 /* Get rid of caching pages from clients */
1301 rc = llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
1303 error_loc = "cannot get data version";
1307 rc = llapi_get_data_version(fdv, &dv, LL_DV_WR_FLUSH);
1309 error_loc = "cannot get data version";
1314 /* Make sure we keep original atime/mtime values */
1315 rc = migrate_copy_timestamps(fd, fdv);
1317 /* Atomically put lease, swap layouts and close.
1318 * for a migration we need to check data version on file did
1320 rc = llapi_fswap_layouts(fd, fdv, 0, 0, MERGE_LAYOUTS_CLOSE);
1322 error_loc = "cannot swap layout";
1334 (void) unlink(victim_file);
1337 fprintf(stderr, "error: %s: %s: %s: %s\n",
1338 progname, fname, error_loc, strerror(-rc));
1342 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1343 enum mirror_flags mirror_flags)
1347 rc = mirror_create_sanity_check(fname, mirror_list);
1351 while (mirror_list) {
1352 if (mirror_list->m_file != NULL) {
1353 rc = mirror_extend_file(fname, mirror_list->m_file,
1356 __u32 mirror_count = mirror_list->m_count;
1358 while (mirror_count > 0) {
1359 rc = lfs_migrate(fname,
1360 MIGRATION_NONBLOCK | MIGRATION_MIRROR,
1361 NULL, mirror_list->m_layout);
1371 mirror_list = mirror_list->m_next;
1378 * Parse a string containing an OST index list into an array of integers.
1380 * The input string contains a comma delimited list of individual
1381 * indices and ranges, for example "1,2-4,7". Add the indices into the
1382 * \a osts array and remove duplicates.
1384 * \param[out] osts array to store indices in
1385 * \param[in] size size of \a osts array
1386 * \param[in] offset starting index in \a osts
1387 * \param[in] arg string containing OST index list
1389 * \retval positive number of indices in \a osts
1390 * \retval -EINVAL unable to parse \a arg
1392 static int parse_targets(__u32 *osts, int size, int offset, char *arg)
1396 int slots = size - offset;
1403 end_of_loop = false;
1404 while (!end_of_loop) {
1408 char *endptr = NULL;
1412 ptr = strchrnul(arg, ',');
1414 end_of_loop = *ptr == '\0';
1417 start_index = strtol(arg, &endptr, 0);
1418 if (endptr == arg) /* no data at all */
1420 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1422 if (start_index < 0)
1425 end_index = start_index;
1426 if (*endptr == '-') {
1427 end_index = strtol(endptr + 1, &endptr, 0);
1428 if (*endptr != '\0')
1430 if (end_index < start_index)
1434 for (i = start_index; i <= end_index && slots > 0; i++) {
1437 /* remove duplicate */
1438 for (j = 0; j < offset; j++) {
1442 if (j == offset) { /* no duplicate */
1447 if (slots == 0 && i < end_index)
1455 if (!end_of_loop && ptr != NULL)
1458 return rc < 0 ? rc : nr;
1461 struct lfs_setstripe_args {
1462 unsigned long long lsa_comp_end;
1463 unsigned long long lsa_stripe_size;
1464 long long lsa_stripe_count;
1465 long long lsa_stripe_off;
1466 __u32 lsa_comp_flags;
1468 unsigned long long lsa_pattern;
1470 char *lsa_pool_name;
1473 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1475 memset(lsa, 0, sizeof(*lsa));
1477 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1478 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1479 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1480 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1481 lsa->lsa_pool_name = NULL;
1485 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1486 * @lsa: Stripe options to be initialized and inherited.
1488 * This function initializes stripe options in @lsa and inherit
1489 * stripe_size, stripe_count and OST pool_name options.
1493 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1495 unsigned long long stripe_size;
1496 long long stripe_count;
1497 char *pool_name = NULL;
1499 stripe_size = lsa->lsa_stripe_size;
1500 stripe_count = lsa->lsa_stripe_count;
1501 pool_name = lsa->lsa_pool_name;
1503 setstripe_args_init(lsa);
1505 lsa->lsa_stripe_size = stripe_size;
1506 lsa->lsa_stripe_count = stripe_count;
1507 lsa->lsa_pool_name = pool_name;
1510 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1512 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1513 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1514 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1515 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1516 lsa->lsa_pool_name != NULL ||
1517 lsa->lsa_comp_end != 0);
1521 * comp_args_to_layout() - Create or extend a composite layout.
1522 * @composite: Pointer to the composite layout.
1523 * @lsa: Stripe options for the new component.
1525 * This function creates or extends a composite layout by adding a new
1526 * component with stripe options from @lsa.
1528 * Return: 0 on success or an error code on failure.
1530 static int comp_args_to_layout(struct llapi_layout **composite,
1531 struct lfs_setstripe_args *lsa)
1533 struct llapi_layout *layout = *composite;
1534 uint64_t prev_end = 0;
1537 if (layout == NULL) {
1538 layout = llapi_layout_alloc();
1539 if (layout == NULL) {
1540 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1544 *composite = layout;
1548 /* Get current component extent, current component
1549 * must be the tail component. */
1550 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
1552 fprintf(stderr, "Get comp extent failed. %s\n",
1557 rc = llapi_layout_comp_add(layout);
1559 fprintf(stderr, "Add component failed. %s\n",
1565 rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
1567 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
1568 prev_end, lsa->lsa_comp_end, strerror(errno));
1572 /* Data-on-MDT component setting */
1573 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
1574 /* In case of Data-on-MDT patterns the only extra option
1575 * applicable is stripe size option. */
1576 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
1577 fprintf(stderr, "Option 'stripe-count' can't be "
1578 "specified with Data-on-MDT component: %lld\n",
1579 lsa->lsa_stripe_count);
1582 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
1583 fprintf(stderr, "Option 'stripe-size' can't be "
1584 "specified with Data-on-MDT component: %llu\n",
1585 lsa->lsa_stripe_size);
1588 if (lsa->lsa_nr_osts != 0) {
1589 fprintf(stderr, "Option 'ost-list' can't be specified "
1590 "with Data-on-MDT component: '%i'\n",
1594 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
1595 fprintf(stderr, "Option 'stripe-offset' can't be "
1596 "specified with Data-on-MDT component: %lld\n",
1597 lsa->lsa_stripe_off);
1600 if (lsa->lsa_pool_name != 0) {
1601 fprintf(stderr, "Option 'pool' can't be specified "
1602 "with Data-on-MDT component: '%s'\n",
1603 lsa->lsa_pool_name);
1607 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
1609 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
1610 lsa->lsa_pattern, strerror(errno));
1613 /* Data-on-MDT component has always single stripe up to end */
1614 lsa->lsa_stripe_size = lsa->lsa_comp_end;
1617 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
1619 fprintf(stderr, "Set stripe size %llu failed: %s\n",
1620 lsa->lsa_stripe_size, strerror(errno));
1624 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
1626 fprintf(stderr, "Set stripe count %lld failed: %s\n",
1627 lsa->lsa_stripe_count, strerror(errno));
1631 if (lsa->lsa_pool_name != NULL) {
1632 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
1634 fprintf(stderr, "Set pool name: %s failed. %s\n",
1635 lsa->lsa_pool_name, strerror(errno));
1639 rc = llapi_layout_pool_name_set(layout, "");
1641 fprintf(stderr, "Clear pool name failed: %s\n",
1647 if (lsa->lsa_nr_osts > 0) {
1648 if (lsa->lsa_stripe_count > 0 &&
1649 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
1650 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
1651 lsa->lsa_nr_osts != lsa->lsa_stripe_count) {
1652 fprintf(stderr, "stripe_count(%lld) != nr_osts(%d)\n",
1653 lsa->lsa_stripe_count, lsa->lsa_nr_osts);
1656 for (i = 0; i < lsa->lsa_nr_osts; i++) {
1657 rc = llapi_layout_ost_index_set(layout, i,
1662 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
1663 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
1666 fprintf(stderr, "Set ost index %d failed. %s\n",
1667 i, strerror(errno));
1674 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
1675 * end of the last component in the existing file, and adjust the
1676 * first extent start of the components to be added accordingly. */
1677 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
1679 struct llapi_layout *head;
1680 uint64_t start, end, stripe_size, prev_end = 0;
1683 if (layout == NULL) {
1685 "%s setstripe: layout must be specified\n",
1691 head = llapi_layout_get_by_path(fname, 0);
1694 "%s setstripe: cannot read layout from '%s': %s\n",
1695 progname, fname, strerror(errno));
1697 } else if (errno == ENODATA) {
1698 /* file without LOVEA, this component-add will be turned
1699 * into a component-create. */
1700 llapi_layout_free(head);
1702 } else if (!llapi_layout_is_composite(head)) {
1703 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
1705 llapi_layout_free(head);
1709 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
1711 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
1712 progname, strerror(errno));
1713 llapi_layout_free(head);
1717 llapi_layout_free(head);
1719 /* Make sure we use the first component of the layout to be added. */
1720 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1723 "%s setstripe: cannot move component cursor: %s\n",
1724 progname, strerror(errno));
1728 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1730 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
1731 progname, strerror(errno));
1735 if (start > prev_end || end <= prev_end) {
1737 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
1738 progname, start, end, prev_end);
1742 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
1744 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
1745 progname, strerror(errno));
1749 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
1750 (prev_end & (stripe_size - 1))) {
1752 "%s setstripe: stripe size %lu not aligned with %lu\n",
1753 progname, stripe_size, prev_end);
1757 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
1760 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
1761 progname, prev_end, end, strerror(errno));
1768 static inline bool comp_flags_is_neg(__u32 flags)
1770 return flags & LCME_FL_NEG;
1773 static inline void comp_flags_set_neg(__u32 *flags)
1775 *flags |= LCME_FL_NEG;
1778 static inline void comp_flags_clear_neg(__u32 *flags)
1780 *flags &= ~LCME_FL_NEG;
1783 static int comp_str2flags(__u32 *flags, char *string)
1786 __u32 neg_flags = 0;
1792 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1796 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1797 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1798 const char *comp_name = comp_flags_table[i].cfn_name;
1800 if (strcmp(name, comp_name) == 0) {
1801 *flags |= comp_flag;
1803 } else if (strncmp(name, "^", 1) == 0 &&
1804 strcmp(name + 1, comp_name) == 0) {
1805 neg_flags |= comp_flag;
1810 llapi_printf(LLAPI_MSG_ERROR,
1811 "%s: component flag '%s' not supported\n",
1817 if (*flags == 0 && neg_flags == 0)
1819 /* don't support mixed flags for now */
1820 if (*flags && neg_flags)
1825 comp_flags_set_neg(flags);
1831 static inline bool arg_is_eof(char *arg)
1833 return !strncmp(arg, "-1", strlen("-1")) ||
1834 !strncmp(arg, "EOF", strlen("EOF")) ||
1835 !strncmp(arg, "eof", strlen("eof"));
1839 * lfs_mirror_alloc() - Allocate a mirror argument structure.
1841 * Return: Valid mirror_args pointer on success and
1842 * NULL if memory allocation fails.
1844 static struct mirror_args *lfs_mirror_alloc(void)
1846 struct mirror_args *mirror = NULL;
1849 mirror = calloc(1, sizeof(*mirror));
1860 * lfs_mirror_free() - Free memory allocated for a mirror argument
1862 * @mirror: Previously allocated mirror argument structure by
1863 * lfs_mirror_alloc().
1865 * Free memory allocated for @mirror.
1869 static void lfs_mirror_free(struct mirror_args *mirror)
1871 if (mirror->m_layout != NULL)
1872 llapi_layout_free(mirror->m_layout);
1877 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
1878 * @mirror_list: Previously allocated mirror list.
1880 * Free memory allocated for @mirror_list.
1884 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
1886 struct mirror_args *next_mirror = NULL;
1888 while (mirror_list != NULL) {
1889 next_mirror = mirror_list->m_next;
1890 lfs_mirror_free(mirror_list);
1891 mirror_list = next_mirror;
1903 LFS_COMP_USE_PARENT_OPT,
1904 LFS_COMP_NO_VERIFY_OPT,
1909 static int lfs_setstripe0(int argc, char **argv, enum setstripe_origin opc)
1911 struct lfs_setstripe_args lsa;
1912 struct llapi_stripe_param *param = NULL;
1913 struct find_param migrate_mdt_param = {
1923 char *mdt_idx_arg = NULL;
1924 unsigned long long size_units = 1;
1925 bool migrate_mode = false;
1926 bool migration_block = false;
1927 __u64 migration_flags = 0;
1928 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
1929 int comp_del = 0, comp_set = 0;
1932 struct llapi_layout *layout = NULL;
1933 struct llapi_layout **lpp = &layout;
1934 bool mirror_mode = false;
1935 bool has_m_file = false;
1936 __u32 mirror_count = 0;
1937 enum mirror_flags mirror_flags = 0;
1938 struct mirror_args *mirror_list = NULL;
1939 struct mirror_args *new_mirror = NULL;
1940 struct mirror_args *last_mirror = NULL;
1943 struct option long_opts[] = {
1944 /* --block is only valid in migrate mode */
1945 { .val = 'b', .name = "block", .has_arg = no_argument},
1946 { .val = LFS_COMP_ADD_OPT,
1947 .name = "comp-add", .has_arg = no_argument},
1948 { .val = LFS_COMP_ADD_OPT,
1949 .name = "component-add",
1950 .has_arg = no_argument},
1951 { .val = LFS_COMP_DEL_OPT,
1952 .name = "comp-del", .has_arg = no_argument},
1953 { .val = LFS_COMP_DEL_OPT,
1954 .name = "component-del",
1955 .has_arg = no_argument},
1956 { .val = LFS_COMP_FLAGS_OPT,
1957 .name = "comp-flags", .has_arg = required_argument},
1958 { .val = LFS_COMP_FLAGS_OPT,
1959 .name = "component-flags",
1960 .has_arg = required_argument},
1961 { .val = LFS_COMP_SET_OPT,
1962 .name = "comp-set", .has_arg = no_argument},
1963 { .val = LFS_COMP_SET_OPT,
1964 .name = "component-set",
1965 .has_arg = no_argument},
1966 { .val = LFS_COMP_USE_PARENT_OPT,
1967 .name = "parent", .has_arg = no_argument},
1968 { .val = LFS_COMP_NO_VERIFY_OPT,
1969 .name = "no-verify", .has_arg = no_argument},
1970 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
1971 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
1972 { .val = 'd', .name = "delete", .has_arg = no_argument},
1973 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
1974 { .val = 'E', .name = "component-end",
1975 .has_arg = required_argument},
1976 { .val = 'f', .name = "file", .has_arg = required_argument },
1977 /* dirstripe {"mdt-hash", required_argument, 0, 'H'}, */
1978 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
1979 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
1980 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
1981 { .val = 'I', .name = "component-id", .has_arg = required_argument},
1982 { .val = 'L', .name = "layout", .has_arg = required_argument },
1983 { .val = 'm', .name = "mdt", .has_arg = required_argument},
1984 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
1985 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
1986 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
1987 /* --non-block is only valid in migrate mode */
1988 { .val = 'n', .name = "non-block", .has_arg = no_argument},
1989 { .val = 'o', .name = "ost", .has_arg = required_argument},
1990 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1991 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
1992 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
1994 { .val = 'p', .name = "pool", .has_arg = required_argument },
1995 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
1996 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
1997 /* dirstripe {"mdt-count", required_argument, 0, 'T'}, */
1998 /* --verbose is only valid in migrate mode */
1999 { .val = 'v', .name = "verbose", .has_arg = no_argument },
2002 setstripe_args_init(&lsa);
2004 migrate_mode = (opc == SO_MIGRATE);
2005 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2007 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2009 while ((c = getopt_long(argc, argv, "bc:dE:f:i:I:m:N::no:p:L:s:S:v",
2010 long_opts, NULL)) >= 0) {
2015 case LFS_COMP_ADD_OPT:
2018 case LFS_COMP_DEL_OPT:
2021 case LFS_COMP_FLAGS_OPT:
2022 result = comp_str2flags(&lsa.lsa_comp_flags, optarg);
2026 case LFS_COMP_SET_OPT:
2029 case LFS_COMP_USE_PARENT_OPT:
2031 fprintf(stderr, "error: %s: --parent must be "
2032 "specified with --mirror-count|-N "
2033 "option\n", progname);
2036 setstripe_args_init(&lsa);
2038 case LFS_COMP_NO_VERIFY_OPT:
2039 mirror_flags |= NO_VERIFY;
2042 if (!migrate_mode) {
2044 "%s %s: -b|--block valid only for migrate command\n",
2048 migration_block = true;
2051 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2054 "%s %s: invalid stripe count '%s'\n",
2055 progname, argv[0], optarg);
2059 if (lsa.lsa_stripe_count == -1)
2060 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2063 /* delete the default striping pattern */
2067 if (lsa.lsa_comp_end != 0) {
2068 result = comp_args_to_layout(lpp, &lsa);
2071 "%s %s: invalid layout\n",
2076 setstripe_args_init_inherit(&lsa);
2079 if (arg_is_eof(optarg)) {
2080 lsa.lsa_comp_end = LUSTRE_EOF;
2082 result = llapi_parse_size(optarg,
2087 "%s %s: invalid component end '%s'\n",
2088 progname, argv[0], optarg);
2094 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2097 "%s %s: invalid stripe offset '%s'\n",
2098 progname, argv[0], optarg);
2101 if (lsa.lsa_stripe_off == -1)
2102 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2105 comp_id = strtoul(optarg, &end, 0);
2106 if (*end != '\0' || comp_id == 0 ||
2107 comp_id > LCME_ID_MAX) {
2109 "%s %s: invalid component ID '%s'\n",
2110 progname, argv[0], optarg);
2115 if (opc != SO_MIRROR_EXTEND) {
2117 "error: %s: invalid option: %s\n",
2118 progname, argv[optopt + 1]);
2121 if (last_mirror == NULL) {
2122 fprintf(stderr, "error: %s: '-N' must exist "
2123 "in front of '%s'\n",
2124 progname, argv[optopt + 1]);
2128 last_mirror->m_file = optarg;
2129 last_mirror->m_count = 1;
2133 if (strcmp(argv[optind - 1], "mdt") == 0) {
2134 /* Can be only the first component */
2135 if (layout != NULL) {
2137 fprintf(stderr, "error: 'mdt' layout "
2138 "can be only the first one\n");
2141 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2143 fprintf(stderr, "error: 'mdt' layout "
2144 "size is too big\n");
2147 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2148 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2150 fprintf(stderr, "error: layout '%s' is "
2151 "unknown, supported layouts are: "
2152 "'mdt', 'raid0'\n", argv[optind]);
2157 if (!migrate_mode) {
2159 "%s %s: -m|--mdt-index valid only for migrate command\n",
2163 mdt_idx_arg = optarg;
2166 if (!migrate_mode) {
2168 "%s %s: -n|--non-block valid only for migrate command\n",
2172 migration_flags |= MIGRATION_NONBLOCK;
2175 if (opc == SO_SETSTRIPE) {
2176 opc = SO_MIRROR_CREATE;
2180 if (optarg != NULL) {
2181 mirror_count = strtoul(optarg, &end, 0);
2182 if (*end != '\0' || mirror_count == 0) {
2184 "error: %s: bad mirror count: %s\n",
2191 new_mirror = lfs_mirror_alloc();
2192 new_mirror->m_count = mirror_count;
2194 if (mirror_list == NULL)
2195 mirror_list = new_mirror;
2197 if (last_mirror != NULL) {
2198 /* wrap up last mirror */
2199 if (lsa.lsa_comp_end == 0)
2200 lsa.lsa_comp_end = LUSTRE_EOF;
2202 result = comp_args_to_layout(lpp, &lsa);
2204 lfs_mirror_free(new_mirror);
2208 setstripe_args_init_inherit(&lsa);
2210 last_mirror->m_next = new_mirror;
2213 last_mirror = new_mirror;
2214 lpp = &last_mirror->m_layout;
2217 lsa.lsa_nr_osts = parse_targets(osts,
2218 sizeof(osts) / sizeof(__u32),
2219 lsa.lsa_nr_osts, optarg);
2220 if (lsa.lsa_nr_osts < 0) {
2222 "%s %s: invalid OST target(s) '%s'\n",
2223 progname, argv[0], optarg);
2227 lsa.lsa_osts = osts;
2228 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2229 lsa.lsa_stripe_off = osts[0];
2234 lsa.lsa_pool_name = optarg;
2237 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2241 "%s %s: invalid stripe size '%s'\n",
2242 progname, argv[0], optarg);
2247 if (!migrate_mode) {
2249 "%s %s: -v|--verbose valid only for migrate command\n",
2253 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2256 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2257 progname, argv[0], argv[optind - 1]);
2262 fname = argv[optind];
2264 if (optind == argc) {
2265 fprintf(stderr, "%s %s: FILE must be specified\n",
2270 if (mirror_mode && mirror_count == 0) {
2272 "error: %s: --mirror-count|-N option is required\n",
2279 if (lsa.lsa_comp_end == 0)
2280 lsa.lsa_comp_end = LUSTRE_EOF;
2283 if (lsa.lsa_comp_end != 0) {
2284 result = comp_args_to_layout(lpp, &lsa);
2289 if (mirror_flags & NO_VERIFY) {
2290 if (opc != SO_MIRROR_EXTEND) {
2292 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2296 } else if (!has_m_file) {
2298 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2305 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2306 * altered by user space tool, so we don't need to support the
2307 * --component-set for this moment. */
2308 if (comp_set != 0) {
2309 fprintf(stderr, "%s %s: --component-set not supported\n",
2314 if ((delete + comp_set + comp_del + comp_add) > 1) {
2316 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2321 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2322 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2324 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2329 if ((comp_set || comp_del) &&
2330 (setstripe_args_specified(&lsa) || layout != NULL)) {
2332 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
2337 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
2339 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
2344 if (comp_add || comp_del) {
2347 result = lstat(fname, &st);
2348 if (result == 0 && S_ISDIR(st.st_mode)) {
2350 "%s setstripe: cannot use --component-add or --component-del for directory\n",
2356 fprintf(stderr, "error: %s: can't use --component-add "
2357 "or --component-del for mirror operation\n",
2364 if (layout == NULL) {
2366 "%s %s: option -E must be specified with --component-add\n",
2371 result = adjust_first_extent(fname, layout);
2372 if (result == -ENODATA)
2374 else if (result != 0)
2378 if (mdt_idx_arg != NULL && optind > 3) {
2380 "%s %s: option -m cannot be used with other options\n",
2385 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
2387 "%s %s: options --non-block and --block are mutually exclusive\n",
2392 if (!comp_del && !comp_set && comp_id != 0) {
2394 "%s %s: option -I can only be used with --component-del\n",
2399 if (mdt_idx_arg != NULL) {
2400 /* initialize migrate mdt parameters */
2401 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
2403 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
2404 progname, argv[0], mdt_idx_arg);
2407 migrate_mdt_param.fp_migrate = 1;
2408 } else if (layout == NULL) {
2409 /* initialize stripe parameters */
2410 param = calloc(1, offsetof(typeof(*param),
2411 lsp_osts[lsa.lsa_nr_osts]));
2412 if (param == NULL) {
2414 "%s %s: cannot allocate memory for parameters: %s\n",
2415 progname, argv[0], strerror(ENOMEM));
2420 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
2421 param->lsp_stripe_size = lsa.lsa_stripe_size;
2422 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2423 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
2424 param->lsp_stripe_count = -1;
2426 param->lsp_stripe_count = lsa.lsa_stripe_count;
2428 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2429 param->lsp_stripe_offset = -1;
2431 param->lsp_stripe_offset = lsa.lsa_stripe_off;
2432 param->lsp_pool = lsa.lsa_pool_name;
2433 param->lsp_is_specific = false;
2434 if (lsa.lsa_nr_osts > 0) {
2435 if (lsa.lsa_stripe_count > 0 &&
2436 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2437 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2438 lsa.lsa_nr_osts != lsa.lsa_stripe_count) {
2439 fprintf(stderr, "error: %s: stripe count %lld "
2440 "doesn't match the number of OSTs: %d\n"
2441 , argv[0], lsa.lsa_stripe_count,
2447 param->lsp_is_specific = true;
2448 param->lsp_stripe_count = lsa.lsa_nr_osts;
2449 memcpy(param->lsp_osts, osts,
2450 sizeof(*osts) * lsa.lsa_nr_osts);
2454 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
2455 if (mdt_idx_arg != NULL) {
2456 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
2457 } else if (migrate_mode) {
2458 result = lfs_migrate(fname, migration_flags, param,
2460 } else if (comp_set != 0) {
2461 result = lfs_component_set(fname, comp_id,
2462 lsa.lsa_comp_flags);
2463 } else if (comp_del != 0) {
2464 result = lfs_component_del(fname, comp_id,
2465 lsa.lsa_comp_flags);
2466 } else if (comp_add != 0) {
2467 result = lfs_component_add(fname, layout);
2468 } else if (opc == SO_MIRROR_CREATE) {
2469 result = mirror_create(fname, mirror_list);
2470 } else if (opc == SO_MIRROR_EXTEND) {
2471 result = mirror_extend(fname, mirror_list,
2473 } else if (layout != NULL) {
2474 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
2481 result = llapi_file_open_param(fname,
2490 /* Save the first error encountered. */
2498 llapi_layout_free(layout);
2499 lfs_mirror_list_free(mirror_list);
2504 llapi_layout_free(layout);
2505 lfs_mirror_list_free(mirror_list);
2509 static int lfs_poollist(int argc, char **argv)
2514 return llapi_poollist(argv[1]);
2517 static int set_time(time_t *time, time_t *set, char *str)
2524 else if (str[0] == '-')
2530 t = strtol(str, NULL, 0);
2531 if (*time < t * 24 * 60 * 60) {
2534 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
2538 *set = *time - t * 24 * 60 * 60;
2541 static int name2uid(unsigned int *id, const char *name)
2543 struct passwd *passwd;
2545 passwd = getpwnam(name);
2548 *id = passwd->pw_uid;
2553 static int name2gid(unsigned int *id, const char *name)
2555 struct group *group;
2557 group = getgrnam(name);
2560 *id = group->gr_gid;
2565 static inline int name2projid(unsigned int *id, const char *name)
2570 static int uid2name(char **name, unsigned int id)
2572 struct passwd *passwd;
2574 passwd = getpwuid(id);
2577 *name = passwd->pw_name;
2582 static inline int gid2name(char **name, unsigned int id)
2584 struct group *group;
2586 group = getgrgid(id);
2589 *name = group->gr_name;
2594 static int name2layout(__u32 *layout, char *name)
2596 char *ptr, *layout_name;
2599 for (ptr = name; ; ptr = NULL) {
2600 layout_name = strtok(ptr, ",");
2601 if (layout_name == NULL)
2603 if (strcmp(layout_name, "released") == 0)
2604 *layout |= LOV_PATTERN_F_RELEASED;
2605 else if (strcmp(layout_name, "raid0") == 0)
2606 *layout |= LOV_PATTERN_RAID0;
2607 else if (strcmp(layout_name, "mdt") == 0)
2608 *layout |= LOV_PATTERN_MDT;
2615 static int lfs_find(int argc, char **argv)
2620 struct find_param param = {
2624 struct option long_opts[] = {
2625 { .val = 'A', .name = "atime", .has_arg = required_argument },
2626 { .val = LFS_COMP_COUNT_OPT,
2627 .name = "comp-count", .has_arg = required_argument },
2628 { .val = LFS_COMP_COUNT_OPT,
2629 .name = "component-count",
2630 .has_arg = required_argument },
2631 { .val = LFS_COMP_FLAGS_OPT,
2632 .name = "comp-flags", .has_arg = required_argument },
2633 { .val = LFS_COMP_FLAGS_OPT,
2634 .name = "component-flags",
2635 .has_arg = required_argument },
2636 { .val = LFS_COMP_START_OPT,
2637 .name = "comp-start", .has_arg = required_argument },
2638 { .val = LFS_COMP_START_OPT,
2639 .name = "component-start",
2640 .has_arg = required_argument },
2641 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
2642 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
2643 { .val = 'C', .name = "ctime", .has_arg = required_argument },
2644 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
2645 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
2646 { .val = 'E', .name = "component-end",
2647 .has_arg = required_argument },
2648 { .val = 'g', .name = "gid", .has_arg = required_argument },
2649 { .val = 'G', .name = "group", .has_arg = required_argument },
2650 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
2651 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
2652 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
2653 /*{"component-id", required_argument, 0, 'I'},*/
2654 { .val = 'L', .name = "layout", .has_arg = required_argument },
2655 { .val = 'm', .name = "mdt", .has_arg = required_argument },
2656 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
2657 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
2658 { .val = 'M', .name = "mtime", .has_arg = required_argument },
2659 { .val = 'n', .name = "name", .has_arg = required_argument },
2660 /* reserve {"or", no_argument, , 0, 'o'}, to match find(1) */
2661 { .val = 'O', .name = "obd", .has_arg = required_argument },
2662 { .val = 'O', .name = "ost", .has_arg = required_argument },
2663 /* no short option for pool, p/P already used */
2664 { .val = LFS_POOL_OPT,
2665 .name = "pool", .has_arg = required_argument },
2666 { .val = 'p', .name = "print0", .has_arg = no_argument },
2667 { .val = 'P', .name = "print", .has_arg = no_argument },
2668 { .val = LFS_PROJID_OPT,
2669 .name = "projid", .has_arg = required_argument },
2670 { .val = 's', .name = "size", .has_arg = required_argument },
2671 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2672 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2673 { .val = 't', .name = "type", .has_arg = required_argument },
2674 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
2675 { .val = 'u', .name = "uid", .has_arg = required_argument },
2676 { .val = 'U', .name = "user", .has_arg = required_argument },
2688 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
2689 while ((c = getopt_long_only(argc, argv,
2690 "-A:c:C:D:E:g:G:H:i:L:m:M:n:O:Ppqrs:S:t:T:u:U:v",
2691 long_opts, NULL)) >= 0) {
2696 /* '!' is part of option */
2697 /* when getopt_long_only() finds a string which is not
2698 * an option nor a known option argument it returns 1
2699 * in that case if we already have found pathstart and pathend
2700 * (i.e. we have the list of pathnames),
2701 * the only supported value is "!"
2703 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
2704 if (!isoption && pathend != -1) {
2705 fprintf(stderr, "err: %s: filename|dirname must either "
2706 "precede options or follow options\n",
2711 if (!isoption && pathstart == -1)
2712 pathstart = optind - 1;
2713 if (isoption && pathstart != -1 && pathend == -1)
2714 pathend = optind - 2;
2720 /* unknown; opt is "!" or path component,
2721 * checking done above.
2723 if (strcmp(optarg, "!") == 0)
2727 xtime = ¶m.fp_atime;
2728 xsign = ¶m.fp_asign;
2729 param.fp_exclude_atime = !!neg_opt;
2730 /* no break, this falls through to 'C' for ctime */
2733 xtime = ¶m.fp_ctime;
2734 xsign = ¶m.fp_csign;
2735 param.fp_exclude_ctime = !!neg_opt;
2737 /* no break, this falls through to 'M' for mtime */
2740 xtime = ¶m.fp_mtime;
2741 xsign = ¶m.fp_msign;
2742 param.fp_exclude_mtime = !!neg_opt;
2744 rc = set_time(&t, xtime, optarg);
2745 if (rc == INT_MAX) {
2752 case LFS_COMP_COUNT_OPT:
2753 if (optarg[0] == '+') {
2754 param.fp_comp_count_sign = -1;
2756 } else if (optarg[0] == '-') {
2757 param.fp_comp_count_sign = 1;
2761 param.fp_comp_count = strtoul(optarg, &endptr, 0);
2762 if (*endptr != '\0') {
2763 fprintf(stderr, "error: bad component count "
2767 param.fp_check_comp_count = 1;
2768 param.fp_exclude_comp_count = !!neg_opt;
2770 case LFS_COMP_FLAGS_OPT:
2771 rc = comp_str2flags(¶m.fp_comp_flags, optarg);
2772 if (rc || comp_flags_is_neg(param.fp_comp_flags)) {
2773 fprintf(stderr, "error: bad component flags "
2777 param.fp_check_comp_flags = 1;
2778 param.fp_exclude_comp_flags = !!neg_opt;
2780 case LFS_COMP_START_OPT:
2781 if (optarg[0] == '+') {
2782 param.fp_comp_start_sign = -1;
2784 } else if (optarg[0] == '-') {
2785 param.fp_comp_start_sign = 1;
2789 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
2790 ¶m.fp_comp_start_units, 0);
2792 fprintf(stderr, "error: bad component start "
2796 param.fp_check_comp_start = 1;
2797 param.fp_exclude_comp_start = !!neg_opt;
2800 if (optarg[0] == '+') {
2801 param.fp_stripe_count_sign = -1;
2803 } else if (optarg[0] == '-') {
2804 param.fp_stripe_count_sign = 1;
2808 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
2809 if (*endptr != '\0') {
2810 fprintf(stderr,"error: bad stripe_count '%s'\n",
2815 param.fp_check_stripe_count = 1;
2816 param.fp_exclude_stripe_count = !!neg_opt;
2819 param.fp_max_depth = strtol(optarg, 0, 0);
2822 if (optarg[0] == '+') {
2823 param.fp_comp_end_sign = -1;
2825 } else if (optarg[0] == '-') {
2826 param.fp_comp_end_sign = 1;
2830 if (arg_is_eof(optarg)) {
2831 param.fp_comp_end = LUSTRE_EOF;
2832 param.fp_comp_end_units = 1;
2835 rc = llapi_parse_size(optarg,
2837 ¶m.fp_comp_end_units, 0);
2840 fprintf(stderr, "error: bad component end "
2844 param.fp_check_comp_end = 1;
2845 param.fp_exclude_comp_end = !!neg_opt;
2849 rc = name2gid(¶m.fp_gid, optarg);
2851 param.fp_gid = strtoul(optarg, &endptr, 10);
2852 if (*endptr != '\0') {
2853 fprintf(stderr, "Group/GID: %s cannot "
2854 "be found.\n", optarg);
2859 param.fp_exclude_gid = !!neg_opt;
2860 param.fp_check_gid = 1;
2863 param.fp_hash_type = check_hashtype(optarg);
2864 if (param.fp_hash_type == 0) {
2865 fprintf(stderr, "error: bad hash_type '%s'\n",
2870 param.fp_check_hash_type = 1;
2871 param.fp_exclude_hash_type = !!neg_opt;
2874 ret = name2layout(¶m.fp_layout, optarg);
2877 param.fp_exclude_layout = !!neg_opt;
2878 param.fp_check_layout = 1;
2882 rc = name2uid(¶m.fp_uid, optarg);
2884 param.fp_uid = strtoul(optarg, &endptr, 10);
2885 if (*endptr != '\0') {
2886 fprintf(stderr, "User/UID: %s cannot "
2887 "be found.\n", optarg);
2892 param.fp_exclude_uid = !!neg_opt;
2893 param.fp_check_uid = 1;
2896 if (strlen(optarg) > LOV_MAXPOOLNAME) {
2898 "Pool name %s is too long"
2899 " (max is %d)\n", optarg,
2904 /* we do check for empty pool because empty pool
2905 * is used to find V1 lov attributes */
2906 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
2907 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
2908 param.fp_exclude_pool = !!neg_opt;
2909 param.fp_check_pool = 1;
2912 param.fp_pattern = (char *)optarg;
2913 param.fp_exclude_pattern = !!neg_opt;
2918 char *buf, *token, *next, *p;
2922 buf = strdup(optarg);
2928 param.fp_exclude_obd = !!neg_opt;
2931 while (token && *token) {
2932 token = strchr(token, ',');
2939 param.fp_exclude_mdt = !!neg_opt;
2940 param.fp_num_alloc_mdts += len;
2941 tmp = realloc(param.fp_mdt_uuid,
2942 param.fp_num_alloc_mdts *
2943 sizeof(*param.fp_mdt_uuid));
2949 param.fp_mdt_uuid = tmp;
2951 param.fp_exclude_obd = !!neg_opt;
2952 param.fp_num_alloc_obds += len;
2953 tmp = realloc(param.fp_obd_uuid,
2954 param.fp_num_alloc_obds *
2955 sizeof(*param.fp_obd_uuid));
2961 param.fp_obd_uuid = tmp;
2963 for (token = buf; token && *token; token = next) {
2964 struct obd_uuid *puuid;
2967 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
2970 ¶m.fp_obd_uuid[param.fp_num_obds++];
2972 p = strchr(token, ',');
2979 if (strlen(token) > sizeof(puuid->uuid) - 1) {
2984 strncpy(puuid->uuid, token,
2985 sizeof(puuid->uuid));
2993 param.fp_zero_end = 1;
2997 case LFS_PROJID_OPT:
2998 rc = name2projid(¶m.fp_projid, optarg);
3000 param.fp_projid = strtoul(optarg, &endptr, 10);
3001 if (*endptr != '\0') {
3003 "Invalid project ID: %s",
3009 param.fp_exclude_projid = !!neg_opt;
3010 param.fp_check_projid = 1;
3013 if (optarg[0] == '+') {
3014 param.fp_size_sign = -1;
3016 } else if (optarg[0] == '-') {
3017 param.fp_size_sign = 1;
3021 ret = llapi_parse_size(optarg, ¶m.fp_size,
3022 ¶m.fp_size_units, 0);
3024 fprintf(stderr, "error: bad file size '%s'\n",
3028 param.fp_check_size = 1;
3029 param.fp_exclude_size = !!neg_opt;
3032 if (optarg[0] == '+') {
3033 param.fp_stripe_size_sign = -1;
3035 } else if (optarg[0] == '-') {
3036 param.fp_stripe_size_sign = 1;
3040 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3041 ¶m.fp_stripe_size_units, 0);
3043 fprintf(stderr, "error: bad stripe_size '%s'\n",
3047 param.fp_check_stripe_size = 1;
3048 param.fp_exclude_stripe_size = !!neg_opt;
3051 param.fp_exclude_type = !!neg_opt;
3052 switch (optarg[0]) {
3054 param.fp_type = S_IFBLK;
3057 param.fp_type = S_IFCHR;
3060 param.fp_type = S_IFDIR;
3063 param.fp_type = S_IFREG;
3066 param.fp_type = S_IFLNK;
3069 param.fp_type = S_IFIFO;
3072 param.fp_type = S_IFSOCK;
3075 fprintf(stderr, "error: %s: bad type '%s'\n",
3082 if (optarg[0] == '+') {
3083 param.fp_mdt_count_sign = -1;
3085 } else if (optarg[0] == '-') {
3086 param.fp_mdt_count_sign = 1;
3090 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3091 if (*endptr != '\0') {
3092 fprintf(stderr, "error: bad mdt_count '%s'\n",
3097 param.fp_check_mdt_count = 1;
3098 param.fp_exclude_mdt_count = !!neg_opt;
3106 if (pathstart == -1) {
3107 fprintf(stderr, "error: %s: no filename|pathname\n",
3111 } else if (pathend == -1) {
3117 rc = llapi_find(argv[pathstart], ¶m);
3118 if (rc != 0 && ret == 0)
3120 } while (++pathstart < pathend);
3123 fprintf(stderr, "error: %s failed for %s.\n",
3124 argv[0], argv[optind - 1]);
3126 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3127 free(param.fp_obd_uuid);
3129 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3130 free(param.fp_mdt_uuid);
3135 static int lfs_getstripe_internal(int argc, char **argv,
3136 struct find_param *param)
3138 struct option long_opts[] = {
3139 { .val = LFS_COMP_COUNT_OPT,
3140 .name = "comp-count", .has_arg = no_argument },
3141 { .val = LFS_COMP_COUNT_OPT,
3142 .name = "component-count", .has_arg = no_argument },
3143 { .val = LFS_COMP_FLAGS_OPT,
3144 .name = "comp-flags", .has_arg = optional_argument },
3145 { .val = LFS_COMP_FLAGS_OPT,
3146 .name = "component-flags", .has_arg = optional_argument },
3147 { .val = LFS_COMP_START_OPT,
3148 .name = "comp-start", .has_arg = optional_argument },
3149 { .val = LFS_COMP_START_OPT,
3150 .name = "component-start", .has_arg = optional_argument },
3151 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3152 /* This formerly implied "stripe-count", but was explicitly
3153 * made "stripe-count" for consistency with other options,
3154 * and to separate it from "mdt-count" when DNE arrives. */
3155 { .val = 'c', .name = "count", .has_arg = no_argument },
3157 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3158 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3159 { .val = 'd', .name = "directory", .has_arg = no_argument },
3160 { .val = 'D', .name = "default", .has_arg = no_argument },
3161 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3162 { .val = 'E', .name = "component-end",
3163 .has_arg = optional_argument },
3164 { .val = 'F', .name = "fid", .has_arg = no_argument },
3165 { .val = 'g', .name = "generation", .has_arg = no_argument },
3166 /* dirstripe { .val = 'H', .name = "mdt-hash",
3167 * .has_arg = required_argument }, */
3168 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3169 /* This formerly implied "stripe-index", but was explicitly
3170 * made "stripe-index" for consistency with other options,
3171 * and to separate it from "mdt-index" when DNE arrives. */
3172 { .val = 'i', .name = "index", .has_arg = no_argument },
3174 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3175 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3176 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3177 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3178 { .val = 'L', .name = "layout", .has_arg = no_argument },
3179 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3180 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3181 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3182 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3183 { .val = 'M', .name = "mdt-index", .has_arg = no_argument },
3184 { .val = 'M', .name = "mdt_index", .has_arg = no_argument },
3186 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3187 /* This formerly implied "stripe-index", but was confusing
3188 * with "file offset" (which will eventually be needed for
3189 * with different layouts by offset), so deprecate it. */
3190 { .val = 'o', .name = "offset", .has_arg = no_argument },
3192 { .val = 'O', .name = "obd", .has_arg = required_argument },
3193 { .val = 'O', .name = "ost", .has_arg = required_argument },
3194 { .val = 'p', .name = "pool", .has_arg = no_argument },
3195 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3196 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3197 { .val = 'R', .name = "raw", .has_arg = no_argument },
3198 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3199 /* This formerly implied "--stripe-size", but was confusing
3200 * with "lfs find --size|-s", which means "file size", so use
3201 * the consistent "--stripe-size|-S" for all commands. */
3202 { .val = 's', .name = "size", .has_arg = no_argument },
3204 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3205 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3206 /* dirstripe { .val = 'T', .name = "mdt-count",
3207 * .has_arg = required_argument }, */
3208 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3209 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3214 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
3215 long_opts, NULL)) != -1) {
3218 if (strcmp(argv[optind - 1], "--count") == 0)
3219 fprintf(stderr, "warning: '--count' deprecated,"
3220 " use '--stripe-count' instead\n");
3221 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3222 param->fp_verbose |= VERBOSE_COUNT;
3223 param->fp_max_depth = 0;
3226 case LFS_COMP_COUNT_OPT:
3227 param->fp_verbose |= VERBOSE_COMP_COUNT;
3228 param->fp_max_depth = 0;
3230 case LFS_COMP_FLAGS_OPT:
3231 if (optarg != NULL) {
3232 __u32 *flags = ¶m->fp_comp_flags;
3233 rc = comp_str2flags(flags, optarg);
3235 fprintf(stderr, "error: %s bad "
3236 "component flags '%s'.\n",
3240 param->fp_check_comp_flags = 1;
3241 param->fp_exclude_comp_flags =
3242 comp_flags_is_neg(*flags);
3243 comp_flags_clear_neg(flags);
3246 param->fp_verbose |= VERBOSE_COMP_FLAGS;
3247 param->fp_max_depth = 0;
3250 case LFS_COMP_START_OPT:
3251 if (optarg != NULL) {
3253 if (tmp[0] == '+') {
3254 param->fp_comp_start_sign = -1;
3256 } else if (tmp[0] == '-') {
3257 param->fp_comp_start_sign = 1;
3260 rc = llapi_parse_size(tmp,
3261 ¶m->fp_comp_start,
3262 ¶m->fp_comp_start_units, 0);
3264 fprintf(stderr, "error: %s bad "
3265 "component start '%s'.\n",
3269 param->fp_check_comp_start = 1;
3272 param->fp_verbose |= VERBOSE_COMP_START;
3273 param->fp_max_depth = 0;
3277 param->fp_max_depth = 0;
3280 param->fp_get_default_lmv = 1;
3283 if (optarg != NULL) {
3285 if (tmp[0] == '+') {
3286 param->fp_comp_end_sign = -1;
3288 } else if (tmp[0] == '-') {
3289 param->fp_comp_end_sign = 1;
3293 if (arg_is_eof(tmp)) {
3294 param->fp_comp_end = LUSTRE_EOF;
3295 param->fp_comp_end_units = 1;
3298 rc = llapi_parse_size(tmp,
3299 ¶m->fp_comp_end,
3300 ¶m->fp_comp_end_units, 0);
3303 fprintf(stderr, "error: %s bad "
3304 "component end '%s'.\n",
3308 param->fp_check_comp_end = 1;
3310 param->fp_verbose |= VERBOSE_COMP_END;
3311 param->fp_max_depth = 0;
3315 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3316 param->fp_verbose |= VERBOSE_DFID;
3317 param->fp_max_depth = 0;
3321 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3322 param->fp_verbose |= VERBOSE_GENERATION;
3323 param->fp_max_depth = 0;
3326 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3328 fprintf(stderr, "warning: '--offset|-o' deprecated, "
3329 "use '--stripe-index|-i' instead\n");
3332 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
3333 if (strcmp(argv[optind - 1], "--index") == 0)
3334 fprintf(stderr, "warning: '--index' deprecated"
3335 ", use '--stripe-index' instead\n");
3337 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3338 param->fp_verbose |= VERBOSE_OFFSET;
3339 param->fp_max_depth = 0;
3343 if (optarg != NULL) {
3344 param->fp_comp_id = strtoul(optarg, &end, 0);
3345 if (*end != '\0' || param->fp_comp_id == 0 ||
3346 param->fp_comp_id > LCME_ID_MAX) {
3347 fprintf(stderr, "error: %s bad "
3348 "component id '%s'\n",
3352 param->fp_check_comp_id = 1;
3355 param->fp_max_depth = 0;
3356 param->fp_verbose |= VERBOSE_COMP_ID;
3360 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3361 param->fp_verbose |= VERBOSE_LAYOUT;
3362 param->fp_max_depth = 0;
3365 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3367 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3368 fprintf(stderr, "warning: '-M' deprecated"
3369 ", use '-m' instead\n");
3373 if (!(param->fp_verbose & VERBOSE_DETAIL))
3374 param->fp_max_depth = 0;
3375 param->fp_verbose |= VERBOSE_MDTINDEX;
3378 if (param->fp_obd_uuid) {
3380 "error: %s: only one obduuid allowed",
3384 param->fp_obd_uuid = (struct obd_uuid *)optarg;
3387 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3388 param->fp_verbose |= VERBOSE_POOL;
3389 param->fp_max_depth = 0;
3396 param->fp_recursive = 1;
3401 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0)
3403 fprintf(stderr, "warning: '--size|-s' deprecated, "
3404 "use '--stripe-size|-S' instead\n");
3405 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 59, 0) */
3407 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3408 param->fp_verbose |= VERBOSE_SIZE;
3409 param->fp_max_depth = 0;
3413 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
3426 if (param->fp_recursive)
3427 param->fp_max_depth = -1;
3428 else if (param->fp_verbose & VERBOSE_DETAIL)
3429 param->fp_max_depth = 1;
3431 if (!param->fp_verbose)
3432 param->fp_verbose = VERBOSE_DEFAULT;
3433 if (param->fp_quiet)
3434 param->fp_verbose = VERBOSE_OBJID;
3437 rc = llapi_getstripe(argv[optind], param);
3438 } while (++optind < argc && !rc);
3441 fprintf(stderr, "error: %s failed for %s.\n",
3442 argv[0], argv[optind - 1]);
3446 static int lfs_tgts(int argc, char **argv)
3448 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3449 struct find_param param;
3450 int index = 0, rc=0;
3455 if (argc == 2 && !realpath(argv[1], path)) {
3457 fprintf(stderr, "error: invalid path '%s': %s\n",
3458 argv[1], strerror(-rc));
3462 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
3463 /* Check if we have a mount point */
3464 if (mntdir[0] == '\0')
3467 memset(¶m, 0, sizeof(param));
3468 if (!strcmp(argv[0], "mdts"))
3469 param.fp_get_lmv = 1;
3471 rc = llapi_ostlist(mntdir, ¶m);
3473 fprintf(stderr, "error: %s: failed on %s\n",
3476 if (path[0] != '\0')
3478 memset(mntdir, 0, PATH_MAX);
3484 static int lfs_getstripe(int argc, char **argv)
3486 struct find_param param = { 0 };
3488 param.fp_max_depth = 1;
3489 return lfs_getstripe_internal(argc, argv, ¶m);
3493 static int lfs_getdirstripe(int argc, char **argv)
3495 struct find_param param = { 0 };
3496 struct option long_opts[] = {
3497 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3498 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
3500 { .val = 'D', .name = "default", .has_arg = no_argument },
3501 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
3502 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
3503 { .val = 'O', .name = "obd", .has_arg = required_argument },
3504 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3505 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3506 { .val = 't', .name = "mdt-hash", .has_arg = no_argument },
3508 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
3509 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3513 param.fp_get_lmv = 1;
3515 while ((c = getopt_long(argc, argv,
3516 "cDHiO:rtTy", long_opts, NULL)) != -1)
3520 if (param.fp_obd_uuid) {
3522 "error: %s: only one obduuid allowed",
3526 param.fp_obd_uuid = (struct obd_uuid *)optarg;
3528 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3530 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 10, 50, 0)
3531 fprintf(stderr, "warning: '-c' deprecated"
3532 ", use '-T' instead\n");
3536 param.fp_verbose |= VERBOSE_COUNT;
3539 param.fp_verbose |= VERBOSE_OFFSET;
3541 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3545 param.fp_verbose |= VERBOSE_HASH_TYPE;
3548 param.fp_get_default_lmv = 1;
3551 param.fp_recursive = 1;
3564 if (param.fp_recursive)
3565 param.fp_max_depth = -1;
3567 if (!param.fp_verbose)
3568 param.fp_verbose = VERBOSE_DEFAULT;
3571 rc = llapi_getstripe(argv[optind], ¶m);
3572 } while (++optind < argc && !rc);
3575 fprintf(stderr, "error: %s failed for %s.\n",
3576 argv[0], argv[optind - 1]);
3581 static int lfs_setdirstripe(int argc, char **argv)
3585 unsigned int stripe_offset = -1;
3586 unsigned int stripe_count = 1;
3587 enum lmv_hash_type hash_type;
3590 char *stripe_offset_opt = NULL;
3591 char *stripe_count_opt = NULL;
3592 char *stripe_hash_opt = NULL;
3593 char *mode_opt = NULL;
3594 bool default_stripe = false;
3595 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
3596 mode_t previous_mode = 0;
3597 bool delete = false;
3599 struct option long_opts[] = {
3600 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3601 { .val = 'c', .name = "count", .has_arg = required_argument },
3603 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
3604 { .val = 'd', .name = "delete", .has_arg = no_argument },
3605 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3606 { .val = 'i', .name = "index", .has_arg = required_argument },
3608 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
3609 { .val = 'm', .name = "mode", .has_arg = required_argument },
3610 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3611 { .val = 't', .name = "hash-type", .has_arg = required_argument },
3612 { .val = 't', .name = "mdt-hash", .has_arg = required_argument },
3614 {"mdt-hash", required_argument, 0, 'H'},
3615 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3616 { .val = 'D', .name = "default_stripe",
3617 .has_arg = no_argument },
3619 { .val = 'D', .name = "default", .has_arg = no_argument },
3622 while ((c = getopt_long(argc, argv, "c:dDi:H:m:t:", long_opts,
3629 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3630 if (strcmp(argv[optind - 1], "--count") == 0)
3632 "%s %s: warning: '--count' deprecated, use '--mdt-count' instead\n",
3635 stripe_count_opt = optarg;
3639 default_stripe = true;
3642 default_stripe = true;
3645 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3646 if (strcmp(argv[optind - 1], "--index") == 0)
3648 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
3651 stripe_offset_opt = optarg;
3656 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3660 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
3661 if (strcmp(argv[optind - 1], "--hash-type") == 0)
3663 "%s %s: warning: '--hash-type' deprecated, use '--mdt-hash' instead\n",
3666 stripe_hash_opt = optarg;
3669 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3670 progname, argv[0], argv[optind - 1]);
3675 if (optind == argc) {
3676 fprintf(stderr, "%s %s: DIR must be specified\n",
3681 if (!delete && stripe_offset_opt == NULL && stripe_count_opt == NULL) {
3683 "%s %s: stripe offset and count must be specified\n",
3688 if (stripe_offset_opt != NULL) {
3689 /* get the stripe offset */
3690 stripe_offset = strtoul(stripe_offset_opt, &end, 0);
3693 "%s %s: bad stripe offset '%s'\n",
3694 progname, argv[0], stripe_offset_opt);
3700 if (stripe_offset_opt != NULL || stripe_count_opt != NULL) {
3702 "%s %s: cannot specify -d with -c or -i options\n",
3711 if (mode_opt != NULL) {
3712 mode = strtoul(mode_opt, &end, 8);
3715 "%s %s: bad MODE '%s'\n",
3716 progname, argv[0], mode_opt);
3719 previous_mode = umask(0);
3722 if (stripe_hash_opt == NULL) {
3723 hash_type = LMV_HASH_TYPE_FNV_1A_64;
3725 hash_type = check_hashtype(stripe_hash_opt);
3726 if (hash_type == 0) {
3727 fprintf(stderr, "%s %s: bad stripe hash type '%s'\n",
3728 progname, argv[0], stripe_hash_opt);
3733 /* get the stripe count */
3734 if (stripe_count_opt != NULL) {
3735 stripe_count = strtoul(stripe_count_opt, &end, 0);
3738 "%s %s: bad stripe count '%s'\n",
3739 progname, argv[0], stripe_count_opt);
3744 dname = argv[optind];
3746 if (default_stripe) {
3747 result = llapi_dir_set_default_lmv_stripe(dname,
3748 stripe_offset, stripe_count,
3751 result = llapi_dir_create_pool(dname, mode,
3753 stripe_count, hash_type,
3759 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
3760 progname, dname, strerror(-result));
3763 dname = argv[++optind];
3764 } while (dname != NULL);
3766 if (mode_opt != NULL)
3767 umask(previous_mode);
3773 static int lfs_rmentry(int argc, char **argv)
3780 fprintf(stderr, "error: %s: missing dirname\n",
3786 dname = argv[index];
3787 while (dname != NULL) {
3788 result = llapi_direntry_remove(dname);
3790 fprintf(stderr, "error: %s: remove dir entry '%s' "
3791 "failed\n", argv[0], dname);
3794 dname = argv[++index];
3799 static int lfs_mv(int argc, char **argv)
3801 struct find_param param = {
3808 struct option long_opts[] = {
3809 { .val = 'M', .name = "mdt-index", .has_arg = required_argument },
3810 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3813 while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
3816 param.fp_mdt_index = strtoul(optarg, &end, 0);
3818 fprintf(stderr, "%s: invalid MDT index'%s'\n",
3825 param.fp_verbose = VERBOSE_DETAIL;
3829 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
3830 argv[0], argv[optind - 1]);
3835 if (param.fp_mdt_index == -1) {
3836 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
3840 if (optind >= argc) {
3841 fprintf(stderr, "%s: missing operand path\n", argv[0]);
3845 param.fp_migrate = 1;
3846 rc = llapi_migrate_mdt(argv[optind], ¶m);
3848 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
3849 argv[0], argv[optind], param.fp_mdt_index,
3854 static int lfs_osts(int argc, char **argv)
3856 return lfs_tgts(argc, argv);
3859 static int lfs_mdts(int argc, char **argv)
3861 return lfs_tgts(argc, argv);
3864 #define COOK(value) \
3867 while (value > 1024) { \
3875 #define CDF "%11llu"
3876 #define HDF "%8.1f%c"
3881 MNTDF_INODES = 0x0001,
3882 MNTDF_COOKED = 0x0002,
3883 MNTDF_LAZY = 0x0004,
3884 MNTDF_VERBOSE = 0x0008,
3887 static int showdf(char *mntdir, struct obd_statfs *stat,
3888 char *uuid, enum mntdf_flags flags,
3889 char *type, int index, int rc)
3891 long long avail, used, total;
3893 char *suffix = "KMGTPEZY";
3894 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
3895 char tbuf[3 * sizeof(__u64)];
3896 char ubuf[3 * sizeof(__u64)];
3897 char abuf[3 * sizeof(__u64)];
3898 char rbuf[3 * sizeof(__u64)];
3905 if (flags & MNTDF_INODES) {
3906 avail = stat->os_ffree;
3907 used = stat->os_files - stat->os_ffree;
3908 total = stat->os_files;
3910 int shift = flags & MNTDF_COOKED ? 0 : 10;
3912 avail = (stat->os_bavail * stat->os_bsize) >> shift;
3913 used = ((stat->os_blocks - stat->os_bfree) *
3914 stat->os_bsize) >> shift;
3915 total = (stat->os_blocks * stat->os_bsize) >> shift;
3918 if ((used + avail) > 0)
3919 ratio = (double)used / (double)(used + avail);
3921 if (flags & MNTDF_COOKED) {
3925 cook_val = (double)total;
3928 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
3931 snprintf(tbuf, sizeof(tbuf), CDF, total);
3933 cook_val = (double)used;
3936 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
3939 snprintf(ubuf, sizeof(ubuf), CDF, used);
3941 cook_val = (double)avail;
3944 snprintf(abuf, sizeof(abuf), HDF, cook_val,
3947 snprintf(abuf, sizeof(abuf), CDF, avail);
3949 snprintf(tbuf, sizeof(tbuf), CDF, total);
3950 snprintf(ubuf, sizeof(tbuf), CDF, used);
3951 snprintf(abuf, sizeof(tbuf), CDF, avail);
3954 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
3955 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
3956 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
3958 printf("[%s:%d]", type, index);
3960 if (stat->os_state) {
3962 * Each character represents the matching
3965 const char state_names[] = "DRSI";
3970 for (i = 0, state = stat->os_state;
3971 state && i < sizeof(state_names); i++) {
3972 if (!(state & (1 << i)))
3974 printf("%c", state_names[i]);
3982 printf(UUF": inactive device\n", uuid);
3985 printf(UUF": %s\n", uuid, strerror(-rc));
3992 struct ll_stat_type {
3997 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags)
3999 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4000 struct obd_uuid uuid_buf;
4001 char *poolname = NULL;
4002 struct ll_stat_type types[] = {
4003 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4004 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4005 { .st_name = NULL } };
4006 struct ll_stat_type *tp;
4007 __u64 ost_ffree = 0;
4015 poolname = strchr(pool, '.');
4016 if (poolname != NULL) {
4017 if (strncmp(fsname, pool, strlen(fsname))) {
4018 fprintf(stderr, "filesystem name incorrect\n");
4026 fd = open(mntdir, O_RDONLY);
4029 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4034 if (flags & MNTDF_INODES)
4035 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4036 "UUID", "Inodes", "IUsed", "IFree",
4037 "IUse%", "Mounted on");
4039 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4040 "UUID", flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4041 "Used", "Available", "Use%", "Mounted on");
4043 for (tp = types; tp->st_name != NULL; tp++) {
4044 for (index = 0; ; index++) {
4045 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4046 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4047 type = flags & MNTDF_LAZY ?
4048 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4049 rc2 = llapi_obd_fstatfs(fd, type, index,
4050 &stat_buf, &uuid_buf);
4055 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4056 if (!(flags & MNTDF_VERBOSE))
4058 } else if (rc2 < 0 && rc == 0) {
4062 if (poolname && tp->st_op == LL_STATFS_LOV &&
4063 llapi_search_ost(fsname, poolname,
4064 obd_uuid2str(&uuid_buf)) != 1)
4067 /* the llapi_obd_statfs() call may have returned with
4068 * an error, but if it filled in uuid_buf we will at
4069 * lease use that to print out a message for that OBD.
4070 * If we didn't get anything in the uuid_buf, then fill
4071 * it in so that we can print an error message. */
4072 if (uuid_buf.uuid[0] == '\0')
4073 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4074 "%s%04x", tp->st_name, index);
4075 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
4076 flags, tp->st_name, index, rc2);
4079 if (tp->st_op == LL_STATFS_LMV) {
4080 sum.os_ffree += stat_buf.os_ffree;
4081 sum.os_files += stat_buf.os_files;
4082 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4083 sum.os_blocks += stat_buf.os_blocks *
4085 sum.os_bfree += stat_buf.os_bfree *
4087 sum.os_bavail += stat_buf.os_bavail *
4089 ost_ffree += stat_buf.os_ffree;
4097 /* If we don't have as many objects free on the OST as inodes
4098 * on the MDS, we reduce the total number of inodes to
4099 * compensate, so that the "inodes in use" number is correct.
4100 * Matches ll_statfs_internal() so the results are consistent. */
4101 if (ost_ffree < sum.os_ffree) {
4102 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4103 sum.os_ffree = ost_ffree;
4106 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4112 static int lfs_df(int argc, char **argv)
4114 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4115 enum mntdf_flags flags = 0;
4116 int c, rc = 0, index = 0;
4117 char fsname[PATH_MAX] = "", *pool_name = NULL;
4118 struct option long_opts[] = {
4119 { .val = 'h', .name = "human-readable",
4120 .has_arg = no_argument },
4121 { .val = 'i', .name = "inodes", .has_arg = no_argument },
4122 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4123 { .val = 'p', .name = "pool", .has_arg = required_argument },
4124 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4127 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
4130 flags |= MNTDF_COOKED;
4133 flags |= MNTDF_INODES;
4136 flags |= MNTDF_LAZY;
4142 flags |= MNTDF_VERBOSE;
4148 if (optind < argc && !realpath(argv[optind], path)) {
4150 fprintf(stderr, "error: invalid path '%s': %s\n",
4151 argv[optind], strerror(-rc));
4155 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4156 /* Check if we have a mount point */
4157 if (mntdir[0] == '\0')
4160 rc = mntdf(mntdir, fsname, pool_name, flags);
4161 if (rc || path[0] != '\0')
4163 fsname[0] = '\0'; /* avoid matching in next loop */
4164 mntdir[0] = '\0'; /* avoid matching in next loop */
4170 static int lfs_getname(int argc, char **argv)
4172 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
4173 int rc = 0, index = 0, c;
4174 char buf[sizeof(struct obd_uuid)];
4176 while ((c = getopt(argc, argv, "h")) != -1)
4179 if (optind == argc) { /* no paths specified, get all paths. */
4180 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4181 rc = llapi_getname(mntdir, buf, sizeof(buf));
4184 "cannot get name for `%s': %s\n",
4185 mntdir, strerror(-rc));
4189 printf("%s %s\n", buf, mntdir);
4191 path[0] = fsname[0] = mntdir[0] = 0;
4193 } else { /* paths specified, only attempt to search these. */
4194 for (; optind < argc; optind++) {
4195 rc = llapi_getname(argv[optind], buf, sizeof(buf));
4198 "cannot get name for `%s': %s\n",
4199 argv[optind], strerror(-rc));
4203 printf("%s %s\n", buf, argv[optind]);
4209 static int lfs_check(int argc, char **argv)
4212 char mntdir[PATH_MAX] = {'\0'};
4221 obd_types[0] = obd_type1;
4222 obd_types[1] = obd_type2;
4224 if (strcmp(argv[1], "osts") == 0) {
4225 strcpy(obd_types[0], "osc");
4226 } else if (strcmp(argv[1], "mds") == 0) {
4227 strcpy(obd_types[0], "mdc");
4228 } else if (strcmp(argv[1], "servers") == 0) {
4230 strcpy(obd_types[0], "osc");
4231 strcpy(obd_types[1], "mdc");
4233 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
4238 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
4239 if (rc < 0 || mntdir[0] == '\0') {
4240 fprintf(stderr, "No suitable Lustre mount found\n");
4244 rc = llapi_target_check(num_types, obd_types, mntdir);
4246 fprintf(stderr, "error: %s: %s status failed\n",
4253 #ifdef HAVE_SYS_QUOTA_H
4254 #define ARG2INT(nr, str, msg) \
4257 nr = strtol(str, &endp, 0); \
4259 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
4264 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
4266 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
4267 * returns the value or ULONG_MAX on integer overflow or incorrect format
4269 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
4270 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
4271 * 3. empty integer value is interpreted as 0
4273 static unsigned long str2sec(const char* timestr)
4275 const char spec[] = "smhdw";
4276 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4277 unsigned long val = 0;
4280 if (strpbrk(timestr, spec) == NULL) {
4281 /* no specifiers inside the time string,
4282 should treat it as an integer value */
4283 val = strtoul(timestr, &tail, 10);
4284 return *tail ? ULONG_MAX : val;
4287 /* format string is XXwXXdXXhXXmXXs */
4293 v = strtoul(timestr, &tail, 10);
4294 if (v == ULONG_MAX || *tail == '\0')
4295 /* value too large (ULONG_MAX or more)
4296 or missing specifier */
4299 ptr = strchr(spec, *tail);
4301 /* unknown specifier */
4306 /* check if product will overflow the type */
4307 if (!(v < ULONG_MAX / mult[ind]))
4310 ADD_OVERFLOW(val, mult[ind] * v);
4311 if (val == ULONG_MAX)
4323 #define ARG2ULL(nr, str, def_units) \
4325 unsigned long long limit, units = def_units; \
4328 rc = llapi_parse_size(str, &limit, &units, 1); \
4330 fprintf(stderr, "error: bad limit value %s\n", str); \
4336 static inline int has_times_option(int argc, char **argv)
4340 for (i = 1; i < argc; i++)
4341 if (!strcmp(argv[i], "-t"))
4347 int lfs_setquota_times(int argc, char **argv)
4350 struct if_quotactl qctl;
4351 char *mnt, *obd_type = (char *)qctl.obd_type;
4352 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4353 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
4354 struct option long_opts[] = {
4355 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
4356 { .val = 'g', .name = "group", .has_arg = no_argument },
4357 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
4358 { .val = 'p', .name = "projid", .has_arg = no_argument },
4359 { .val = 't', .name = "times", .has_arg = no_argument },
4360 { .val = 'u', .name = "user", .has_arg = no_argument },
4364 memset(&qctl, 0, sizeof(qctl));
4365 qctl.qc_cmd = LUSTRE_Q_SETINFO;
4366 qctl.qc_type = ALLQUOTA;
4368 while ((c = getopt_long(argc, argv, "b:gi:ptu",
4369 long_opts, NULL)) != -1) {
4380 if (qctl.qc_type != ALLQUOTA) {
4381 fprintf(stderr, "error: -u/g/p can't be used "
4382 "more than once\n");
4385 qctl.qc_type = qtype;
4388 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
4389 fprintf(stderr, "error: bad block-grace: %s\n",
4393 dqb->dqb_valid |= QIF_BTIME;
4396 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
4397 fprintf(stderr, "error: bad inode-grace: %s\n",
4401 dqb->dqb_valid |= QIF_ITIME;
4403 case 't': /* Yes, of course! */
4405 default: /* getopt prints error message for us when opterr != 0 */
4410 if (qctl.qc_type == ALLQUOTA) {
4411 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
4415 if (optind != argc - 1) {
4416 fprintf(stderr, "error: unexpected parameters encountered\n");
4421 rc = llapi_quotactl(mnt, &qctl);
4424 fprintf(stderr, "%s %s ", obd_type,
4425 obd_uuid2str(&qctl.obd_uuid));
4426 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
4433 #define BSLIMIT (1 << 0)
4434 #define BHLIMIT (1 << 1)
4435 #define ISLIMIT (1 << 2)
4436 #define IHLIMIT (1 << 3)
4438 int lfs_setquota(int argc, char **argv)
4441 struct if_quotactl qctl;
4442 char *mnt, *obd_type = (char *)qctl.obd_type;
4443 struct obd_dqblk *dqb = &qctl.qc_dqblk;
4444 struct option long_opts[] = {
4445 { .val = 'b', .name = "block-softlimit",
4446 .has_arg = required_argument },
4447 { .val = 'B', .name = "block-hardlimit",
4448 .has_arg = required_argument },
4449 { .val = 'g', .name = "group", .has_arg = required_argument },
4450 { .val = 'i', .name = "inode-softlimit",
4451 .has_arg = required_argument },
4452 { .val = 'I', .name = "inode-hardlimit",
4453 .has_arg = required_argument },
4454 { .val = 'p', .name = "projid", .has_arg = required_argument },
4455 { .val = 'u', .name = "user", .has_arg = required_argument },
4457 unsigned limit_mask = 0;
4461 if (has_times_option(argc, argv))
4462 return lfs_setquota_times(argc, argv);
4464 memset(&qctl, 0, sizeof(qctl));
4465 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
4466 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
4467 * so it can be used as a marker that qc_type
4468 * isn't reinitialized from command line */
4470 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
4471 long_opts, NULL)) != -1) {
4475 rc = name2uid(&qctl.qc_id, optarg);
4479 rc = name2gid(&qctl.qc_id, optarg);
4483 rc = name2projid(&qctl.qc_id, optarg);
4485 if (qctl.qc_type != ALLQUOTA) {
4486 fprintf(stderr, "error: -u and -g can't be used"
4487 " more than once\n");
4490 qctl.qc_type = qtype;
4492 qctl.qc_id = strtoul(optarg, &endptr, 10);
4493 if (*endptr != '\0') {
4494 fprintf(stderr, "error: can't find id "
4495 "for name %s\n", optarg);
4501 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
4502 dqb->dqb_bsoftlimit >>= 10;
4503 limit_mask |= BSLIMIT;
4504 if (dqb->dqb_bsoftlimit &&
4505 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
4506 fprintf(stderr, "warning: block softlimit is "
4507 "smaller than the miminal qunit size, "
4508 "please see the help of setquota or "
4509 "Lustre manual for details.\n");
4512 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
4513 dqb->dqb_bhardlimit >>= 10;
4514 limit_mask |= BHLIMIT;
4515 if (dqb->dqb_bhardlimit &&
4516 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
4517 fprintf(stderr, "warning: block hardlimit is "
4518 "smaller than the miminal qunit size, "
4519 "please see the help of setquota or "
4520 "Lustre manual for details.\n");
4523 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
4524 limit_mask |= ISLIMIT;
4525 if (dqb->dqb_isoftlimit &&
4526 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
4527 fprintf(stderr, "warning: inode softlimit is "
4528 "smaller than the miminal qunit size, "
4529 "please see the help of setquota or "
4530 "Lustre manual for details.\n");
4533 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
4534 limit_mask |= IHLIMIT;
4535 if (dqb->dqb_ihardlimit &&
4536 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
4537 fprintf(stderr, "warning: inode hardlimit is "
4538 "smaller than the miminal qunit size, "
4539 "please see the help of setquota or "
4540 "Lustre manual for details.\n");
4542 default: /* getopt prints error message for us when opterr != 0 */
4547 if (qctl.qc_type == ALLQUOTA) {
4548 fprintf(stderr, "error: neither -u, -g nor -p was specified\n");
4552 if (limit_mask == 0) {
4553 fprintf(stderr, "error: at least one limit must be specified\n");
4557 if (optind != argc - 1) {
4558 fprintf(stderr, "error: unexpected parameters encountered\n");
4564 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
4565 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
4566 /* sigh, we can't just set blimits/ilimits */
4567 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
4568 .qc_type = qctl.qc_type,
4569 .qc_id = qctl.qc_id};
4571 rc = llapi_quotactl(mnt, &tmp_qctl);
4573 fprintf(stderr, "error: setquota failed while retrieving"
4574 " current quota settings (%s)\n",
4579 if (!(limit_mask & BHLIMIT))
4580 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
4581 if (!(limit_mask & BSLIMIT))
4582 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
4583 if (!(limit_mask & IHLIMIT))
4584 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
4585 if (!(limit_mask & ISLIMIT))
4586 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
4588 /* Keep grace times if we have got no softlimit arguments */
4589 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
4590 dqb->dqb_valid |= QIF_BTIME;
4591 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
4594 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
4595 dqb->dqb_valid |= QIF_ITIME;
4596 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
4600 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
4601 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
4603 rc = llapi_quotactl(mnt, &qctl);
4606 fprintf(stderr, "%s %s ", obd_type,
4607 obd_uuid2str(&qctl.obd_uuid));
4608 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
4615 /* Converts seconds value into format string
4616 * result is returned in buf
4618 * 1. result is in descenting order: 1w2d3h4m5s
4619 * 2. zero fields are not filled (except for p. 3): 5d1s
4620 * 3. zero seconds value is presented as "0s"
4622 static char * __sec2str(time_t seconds, char *buf)
4624 const char spec[] = "smhdw";
4625 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4630 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
4631 c = seconds / mult[i];
4633 if (c > 0 || (i == 0 && buf == tail))
4634 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
4642 static void sec2str(time_t seconds, char *buf, int rc)
4649 tail = __sec2str(seconds, tail);
4651 if (rc && tail - buf < 39) {
4657 static void diff2str(time_t seconds, char *buf, time_t now)
4663 if (seconds <= now) {
4664 strcpy(buf, "none");
4667 __sec2str(seconds - now, buf);
4670 static void print_quota_title(char *name, struct if_quotactl *qctl,
4671 bool human_readable)
4673 printf("Disk quotas for %s %s (%cid %u):\n",
4674 qtype_name(qctl->qc_type), name,
4675 *qtype_name(qctl->qc_type), qctl->qc_id);
4676 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
4677 "Filesystem", human_readable ? "used" : "kbytes",
4678 "quota", "limit", "grace",
4679 "files", "quota", "limit", "grace");
4682 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
4685 snprintf(buf, buflen, "%ju", (uintmax_t)num);
4688 snprintf(buf, buflen, "%5.4gP",
4689 (double)num / ((__u64)1 << 40));
4691 snprintf(buf, buflen, "%5.4gT",
4692 (double)num / (1 << 30));
4694 snprintf(buf, buflen, "%5.4gG",
4695 (double)num / (1 << 20));
4697 snprintf(buf, buflen, "%5.4gM",
4698 (double)num / (1 << 10));
4700 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
4704 #define STRBUF_LEN 32
4705 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
4712 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
4713 int bover = 0, iover = 0;
4714 struct obd_dqblk *dqb = &qctl->qc_dqblk;
4715 char numbuf[3][STRBUF_LEN];
4717 char strbuf[STRBUF_LEN];
4719 if (dqb->dqb_bhardlimit &&
4720 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
4722 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
4723 if (dqb->dqb_btime > now) {
4730 if (dqb->dqb_ihardlimit &&
4731 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
4733 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
4734 if (dqb->dqb_itime > now) {
4742 if (strlen(mnt) > 15)
4743 printf("%s\n%15s", mnt, "");
4745 printf("%15s", mnt);
4748 diff2str(dqb->dqb_btime, timebuf, now);
4750 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
4751 strbuf, sizeof(strbuf), h);
4752 if (rc == -EREMOTEIO)
4753 sprintf(numbuf[0], "%s*", strbuf);
4755 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
4756 "%s" : "[%s]", strbuf);
4758 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
4759 if (type == QC_GENERAL)
4760 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
4761 "%s" : "[%s]", strbuf);
4763 sprintf(numbuf[1], "%s", "-");
4765 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
4766 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
4767 "%s" : "[%s]", strbuf);
4769 printf(" %7s%c %6s %7s %7s",
4770 numbuf[0], bover ? '*' : ' ', numbuf[1],
4771 numbuf[2], bover > 1 ? timebuf : "-");
4774 diff2str(dqb->dqb_itime, timebuf, now);
4776 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
4777 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
4779 if (type == QC_GENERAL)
4780 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
4782 (uintmax_t)dqb->dqb_isoftlimit);
4784 sprintf(numbuf[1], "%s", "-");
4786 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
4787 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
4789 if (type != QC_OSTIDX)
4790 printf(" %7s%c %6s %7s %7s",
4791 numbuf[0], iover ? '*' : ' ', numbuf[1],
4792 numbuf[2], iover > 1 ? timebuf : "-");
4794 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
4797 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
4798 qctl->qc_cmd == Q_GETOINFO) {
4802 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
4803 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
4804 printf("Block grace time: %s; Inode grace time: %s\n",
4805 bgtimebuf, igtimebuf);
4809 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
4810 bool h, __u64 *total)
4812 int rc = 0, rc1 = 0, count = 0;
4813 __u32 valid = qctl->qc_valid;
4815 rc = llapi_get_obd_count(mnt, &count, is_mdt);
4817 fprintf(stderr, "can not get %s count: %s\n",
4818 is_mdt ? "mdt": "ost", strerror(-rc));
4822 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
4823 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
4824 rc = llapi_quotactl(mnt, qctl);
4826 /* It is remote client case. */
4827 if (rc == -EOPNOTSUPP) {
4834 fprintf(stderr, "quotactl %s%d failed.\n",
4835 is_mdt ? "mdt": "ost", qctl->qc_idx);
4839 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
4840 qctl->qc_valid, 0, h);
4841 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
4842 qctl->qc_dqblk.dqb_bhardlimit;
4845 qctl->qc_valid = valid;
4849 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
4850 int verbose, int quiet, bool human_readable)
4852 int rc1 = 0, rc2 = 0, rc3 = 0;
4853 char *obd_type = (char *)qctl->obd_type;
4854 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
4855 __u64 total_ialloc = 0, total_balloc = 0;
4858 rc1 = llapi_quotactl(mnt, qctl);
4862 fprintf(stderr, "%s quotas are not enabled.\n",
4863 qtype_name(qctl->qc_type));
4866 fprintf(stderr, "Permission denied.\n");
4869 /* We already got error message. */
4872 fprintf(stderr, "Unexpected quotactl error: %s\n",
4877 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
4878 print_quota_title(name, qctl, human_readable);
4880 if (rc1 && *obd_type)
4881 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
4883 if (qctl->qc_valid != QC_GENERAL)
4886 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
4887 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
4888 (QIF_LIMITS|QIF_USAGE));
4890 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
4892 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
4894 char strbuf[STRBUF_LEN];
4896 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
4898 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
4900 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
4902 printf("Total allocated inode limit: %ju, total "
4903 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
4907 if (rc1 || rc2 || rc3 || inacc)
4908 printf("Some errors happened when getting quota info. "
4909 "Some devices may be not working or deactivated. "
4910 "The data in \"[]\" is inaccurate.\n");
4916 static int lfs_quota(int argc, char **argv)
4919 char *mnt, *name = NULL;
4920 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
4921 .qc_type = ALLQUOTA };
4922 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
4923 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
4925 __u32 valid = QC_GENERAL, idx = 0;
4926 bool human_readable = false;
4929 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
4940 if (qctl.qc_type != ALLQUOTA) {
4941 fprintf(stderr, "error: use either -u or -g\n");
4944 qctl.qc_type = qtype;
4947 qctl.qc_cmd = LUSTRE_Q_GETINFO;
4950 valid = qctl.qc_valid = QC_UUID;
4951 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
4954 valid = qctl.qc_valid = QC_MDTIDX;
4955 idx = qctl.qc_idx = atoi(optarg);
4958 valid = qctl.qc_valid = QC_OSTIDX;
4959 idx = qctl.qc_idx = atoi(optarg);
4968 human_readable = true;
4971 fprintf(stderr, "error: %s: option '-%c' "
4972 "unrecognized\n", argv[0], c);
4977 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
4978 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
4979 optind == argc - 1) {
4981 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
4982 qctl.qc_valid = valid;
4985 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
4986 qctl.qc_type = qtype;
4987 if (qtype == USRQUOTA) {
4988 qctl.qc_id = geteuid();
4989 rc = uid2name(&name, qctl.qc_id);
4991 qctl.qc_id = getegid();
4992 rc = gid2name(&name, qctl.qc_id);
4997 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
5002 /* lfs quota -u username /path/to/lustre/mount */
5003 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
5004 /* options should be followed by u/g-name and mntpoint */
5005 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
5006 fprintf(stderr, "error: missing quota argument(s)\n");
5010 name = argv[optind++];
5011 switch (qctl.qc_type) {
5013 rc = name2uid(&qctl.qc_id, name);
5016 rc = name2gid(&qctl.qc_id, name);
5019 rc = name2projid(&qctl.qc_id, name);
5026 qctl.qc_id = strtoul(name, &endptr, 10);
5027 if (*endptr != '\0') {
5028 fprintf(stderr, "error: can't find id for name: %s\n",
5034 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
5036 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
5037 fprintf(stderr, "error: missing quota info argument(s)\n");
5043 #endif /* HAVE_SYS_QUOTA_H! */
5045 static int flushctx_ioctl(char *mp)
5049 fd = open(mp, O_RDONLY);
5051 fprintf(stderr, "flushctx: error open %s: %s\n",
5052 mp, strerror(errno));
5056 rc = ioctl(fd, LL_IOC_FLUSHCTX);
5058 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
5059 mp, strerror(errno));
5065 static int lfs_flushctx(int argc, char **argv)
5067 int kdestroy = 0, c;
5068 char mntdir[PATH_MAX] = {'\0'};
5072 while ((c = getopt(argc, argv, "k")) != -1) {
5078 fprintf(stderr, "error: %s: option '-%c' "
5079 "unrecognized\n", argv[0], c);
5085 if ((rc = system("kdestroy > /dev/null")) != 0) {
5086 rc = WEXITSTATUS(rc);
5087 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
5091 if (optind >= argc) {
5092 /* flush for all mounted lustre fs. */
5093 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
5094 /* Check if we have a mount point */
5095 if (mntdir[0] == '\0')
5098 if (flushctx_ioctl(mntdir))
5101 mntdir[0] = '\0'; /* avoid matching in next loop */
5104 /* flush fs as specified */
5105 while (optind < argc) {
5106 if (flushctx_ioctl(argv[optind++]))
5113 static int lfs_cp(int argc, char **argv)
5115 fprintf(stderr, "remote client copy file(s).\n"
5116 "obsolete, does not support it anymore.\n");
5120 static int lfs_ls(int argc, char **argv)
5122 fprintf(stderr, "remote client lists directory contents.\n"
5123 "obsolete, does not support it anymore.\n");
5127 static int lfs_changelog(int argc, char **argv)
5129 void *changelog_priv;
5130 struct changelog_rec *rec;
5131 long long startrec = 0, endrec = 0;
5133 struct option long_opts[] = {
5134 { .val = 'f', .name = "follow", .has_arg = no_argument },
5136 char short_opts[] = "f";
5139 while ((rc = getopt_long(argc, argv, short_opts,
5140 long_opts, NULL)) != -1) {
5148 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5149 argv[0], argv[optind - 1]);
5156 mdd = argv[optind++];
5158 startrec = strtoll(argv[optind++], NULL, 10);
5160 endrec = strtoll(argv[optind++], NULL, 10);
5162 rc = llapi_changelog_start(&changelog_priv,
5163 CHANGELOG_FLAG_BLOCK |
5164 CHANGELOG_FLAG_JOBID |
5165 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
5168 fprintf(stderr, "Can't start changelog: %s\n",
5169 strerror(errno = -rc));
5173 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
5177 if (endrec && rec->cr_index > endrec) {
5178 llapi_changelog_free(&rec);
5181 if (rec->cr_index < startrec) {
5182 llapi_changelog_free(&rec);
5186 secs = rec->cr_time >> 30;
5187 gmtime_r(&secs, &ts);
5188 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
5189 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
5190 changelog_type2str(rec->cr_type),
5191 ts.tm_hour, ts.tm_min, ts.tm_sec,
5192 (int)(rec->cr_time & ((1 << 30) - 1)),
5193 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
5194 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
5196 if (rec->cr_flags & CLF_JOBID) {
5197 struct changelog_ext_jobid *jid =
5198 changelog_rec_jobid(rec);
5200 if (jid->cr_jobid[0] != '\0')
5201 printf(" j=%s", jid->cr_jobid);
5204 if (rec->cr_namelen)
5205 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
5206 rec->cr_namelen, changelog_rec_name(rec));
5208 if (rec->cr_flags & CLF_RENAME) {
5209 struct changelog_ext_rename *rnm =
5210 changelog_rec_rename(rec);
5212 if (!fid_is_zero(&rnm->cr_sfid))
5213 printf(" s="DFID" sp="DFID" %.*s",
5214 PFID(&rnm->cr_sfid),
5215 PFID(&rnm->cr_spfid),
5216 (int)changelog_rec_snamelen(rec),
5217 changelog_rec_sname(rec));
5221 llapi_changelog_free(&rec);
5224 llapi_changelog_fini(&changelog_priv);
5227 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
5229 return (rc == 1 ? 0 : rc);
5232 static int lfs_changelog_clear(int argc, char **argv)
5240 endrec = strtoll(argv[3], NULL, 10);
5242 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
5245 fprintf(stderr, "%s: record out of range: %llu\n",
5247 else if (rc == -ENOENT)
5248 fprintf(stderr, "%s: no changelog user: %s\n",
5251 fprintf(stderr, "%s error: %s\n", argv[0],
5260 static int lfs_fid2path(int argc, char **argv)
5262 struct option long_opts[] = {
5263 { .val = 'c', .name = "cur", .has_arg = no_argument },
5264 { .val = 'l', .name = "link", .has_arg = required_argument },
5265 { .val = 'r', .name = "rec", .has_arg = required_argument },
5267 char short_opts[] = "cl:r:";
5268 char *device, *fid, *path;
5269 long long recno = -1;
5275 while ((rc = getopt_long(argc, argv, short_opts,
5276 long_opts, NULL)) != -1) {
5282 linkno = strtol(optarg, NULL, 10);
5285 recno = strtoll(optarg, NULL, 10);
5290 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5291 argv[0], argv[optind - 1]);
5299 device = argv[optind++];
5300 path = calloc(1, PATH_MAX);
5302 fprintf(stderr, "error: Not enough memory\n");
5307 while (optind < argc) {
5308 fid = argv[optind++];
5310 lnktmp = (linkno >= 0) ? linkno : 0;
5312 int oldtmp = lnktmp;
5313 long long rectmp = recno;
5315 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
5318 fprintf(stderr, "%s: error on FID %s: %s\n",
5319 argv[0], fid, strerror(errno = -rc2));
5326 fprintf(stdout, "%lld ", rectmp);
5327 if (device[0] == '/') {
5328 fprintf(stdout, "%s", device);
5329 if (device[strlen(device) - 1] != '/')
5330 fprintf(stdout, "/");
5331 } else if (path[0] == '\0') {
5332 fprintf(stdout, "/");
5334 fprintf(stdout, "%s\n", path);
5337 /* specified linkno */
5339 if (oldtmp == lnktmp)
5349 static int lfs_path2fid(int argc, char **argv)
5351 struct option long_opts[] = {
5352 { .val = 'p', .name = "parents", .has_arg = no_argument },
5355 const char short_opts[] = "p";
5356 const char *sep = "";
5359 bool show_parents = false;
5361 while ((rc = getopt_long(argc, argv, short_opts,
5362 long_opts, NULL)) != -1) {
5365 show_parents = true;
5368 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5369 argv[0], argv[optind - 1]);
5374 if (optind > argc - 1)
5376 else if (optind < argc - 1)
5380 for (path = argv + optind; *path != NULL; path++) {
5382 if (!show_parents) {
5383 err = llapi_path2fid(*path, &fid);
5385 printf("%s%s"DFID"\n",
5386 *sep != '\0' ? *path : "", sep,
5389 char name[NAME_MAX + 1];
5390 unsigned int linkno = 0;
5392 while ((err = llapi_path2parent(*path, linkno, &fid,
5393 name, sizeof(name))) == 0) {
5394 if (*sep != '\0' && linkno == 0)
5395 printf("%s%s", *path, sep);
5397 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
5402 /* err == -ENODATA is end-of-loop */
5403 if (linkno > 0 && err == -ENODATA) {
5410 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
5411 argv[0], show_parents ? "parent " : "", *path,
5423 static int lfs_data_version(int argc, char **argv)
5430 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
5435 while ((c = getopt(argc, argv, "nrw")) != -1) {
5438 data_version_flags = 0;
5441 data_version_flags |= LL_DV_RD_FLUSH;
5444 data_version_flags |= LL_DV_WR_FLUSH;
5453 path = argv[optind];
5454 fd = open(path, O_RDONLY);
5456 err(errno, "cannot open file %s", path);
5458 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
5460 err(errno, "cannot get version for %s", path);
5462 printf("%ju" "\n", (uintmax_t)data_version);
5468 static int lfs_hsm_state(int argc, char **argv)
5473 struct hsm_user_state hus;
5481 rc = llapi_hsm_state_get(path, &hus);
5483 fprintf(stderr, "can't get hsm state for %s: %s\n",
5484 path, strerror(errno = -rc));
5488 /* Display path name and status flags */
5489 printf("%s: (0x%08x)", path, hus.hus_states);
5491 if (hus.hus_states & HS_RELEASED)
5492 printf(" released");
5493 if (hus.hus_states & HS_EXISTS)
5495 if (hus.hus_states & HS_DIRTY)
5497 if (hus.hus_states & HS_ARCHIVED)
5498 printf(" archived");
5499 /* Display user-settable flags */
5500 if (hus.hus_states & HS_NORELEASE)
5501 printf(" never_release");
5502 if (hus.hus_states & HS_NOARCHIVE)
5503 printf(" never_archive");
5504 if (hus.hus_states & HS_LOST)
5505 printf(" lost_from_hsm");
5507 if (hus.hus_archive_id != 0)
5508 printf(", archive_id:%d", hus.hus_archive_id);
5511 } while (++i < argc);
5516 #define LFS_HSM_SET 0
5517 #define LFS_HSM_CLEAR 1
5520 * Generic function to set or clear HSM flags.
5521 * Used by hsm_set and hsm_clear.
5523 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
5525 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
5527 struct option long_opts[] = {
5528 { .val = 'A', .name = "archived", .has_arg = no_argument },
5529 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
5530 { .val = 'd', .name = "dirty", .has_arg = no_argument },
5531 { .val = 'e', .name = "exists", .has_arg = no_argument },
5532 { .val = 'l', .name = "lost", .has_arg = no_argument },
5533 { .val = 'r', .name = "norelease", .has_arg = no_argument },
5535 char short_opts[] = "lraAde";
5543 while ((c = getopt_long(argc, argv, short_opts,
5544 long_opts, NULL)) != -1) {
5550 mask |= HS_NOARCHIVE;
5553 mask |= HS_ARCHIVED;
5556 mask |= HS_NORELEASE;
5567 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5568 argv[0], argv[optind - 1]);
5573 /* User should have specified a flag */
5577 while (optind < argc) {
5579 path = argv[optind];
5581 /* If mode == 0, this means we apply the mask. */
5582 if (mode == LFS_HSM_SET)
5583 rc = llapi_hsm_state_set(path, mask, 0, 0);
5585 rc = llapi_hsm_state_set(path, 0, mask, 0);
5588 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
5589 path, strerror(errno = -rc));
5598 static int lfs_hsm_action(int argc, char **argv)
5603 struct hsm_current_action hca;
5604 struct hsm_extent he;
5605 enum hsm_user_action hua;
5606 enum hsm_progress_states hps;
5614 rc = llapi_hsm_current_action(path, &hca);
5616 fprintf(stderr, "can't get hsm action for %s: %s\n",
5617 path, strerror(errno = -rc));
5620 he = hca.hca_location;
5621 hua = hca.hca_action;
5622 hps = hca.hca_state;
5624 printf("%s: %s", path, hsm_user_action2name(hua));
5626 /* Skip file without action */
5627 if (hca.hca_action == HUA_NONE) {
5632 printf(" %s ", hsm_progress_state2name(hps));
5634 if ((hps == HPS_RUNNING) &&
5635 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
5636 printf("(%llu bytes moved)\n",
5637 (unsigned long long)he.length);
5638 else if ((he.offset + he.length) == LUSTRE_EOF)
5639 printf("(from %llu to EOF)\n",
5640 (unsigned long long)he.offset);
5642 printf("(from %llu to %llu)\n",
5643 (unsigned long long)he.offset,
5644 (unsigned long long)(he.offset + he.length));
5646 } while (++i < argc);
5651 static int lfs_hsm_set(int argc, char **argv)
5653 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
5656 static int lfs_hsm_clear(int argc, char **argv)
5658 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
5662 * Check file state and return its fid, to be used by lfs_hsm_request().
5664 * \param[in] file Path to file to check
5665 * \param[in,out] fid Pointer to allocated lu_fid struct.
5666 * \param[in,out] last_dev Pointer to last device id used.
5668 * \return 0 on success.
5670 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
5676 rc = lstat(file, &st);
5678 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
5681 /* Checking for regular file as archiving as posix copytool
5682 * rejects archiving files other than regular files
5684 if (!S_ISREG(st.st_mode)) {
5685 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
5688 /* A request should be ... */
5689 if (*last_dev != st.st_dev && *last_dev != 0) {
5690 fprintf(stderr, "All files should be "
5691 "on the same filesystem: %s\n", file);
5694 *last_dev = st.st_dev;
5696 rc = llapi_path2fid(file, fid);
5698 fprintf(stderr, "Cannot read FID of %s: %s\n",
5699 file, strerror(-rc));
5705 /* Fill an HSM HUR item with a given file name.
5707 * If mntpath is set, then the filename is actually a FID, and no
5708 * lookup on the filesystem will be performed.
5710 * \param[in] hur the user request to fill
5711 * \param[in] idx index of the item inside the HUR to fill
5712 * \param[in] mntpath mountpoint of Lustre
5713 * \param[in] fname filename (if mtnpath is NULL)
5714 * or FID (if mntpath is set)
5715 * \param[in] last_dev pointer to last device id used
5717 * \retval 0 on success
5718 * \retval CMD_HELP or a negative errno on error
5720 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
5721 const char *mntpath, const char *fname,
5724 struct hsm_user_item *hui = &hur->hur_user_item[idx];
5727 hui->hui_extent.length = -1;
5729 if (mntpath != NULL) {
5732 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
5736 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
5741 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
5745 hur->hur_request.hr_itemcount++;
5750 static int lfs_hsm_request(int argc, char **argv, int action)
5752 struct option long_opts[] = {
5753 { .val = 'a', .name = "archive", .has_arg = required_argument },
5754 { .val = 'D', .name = "data", .has_arg = required_argument },
5755 { .val = 'l', .name = "filelist", .has_arg = required_argument },
5756 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
5759 char short_opts[] = "l:D:a:m:";
5760 struct hsm_user_request *hur, *oldhur;
5765 char *filelist = NULL;
5766 char fullpath[PATH_MAX];
5767 char *opaque = NULL;
5771 int nbfile_alloc = 0;
5772 char *some_file = NULL;
5773 char *mntpath = NULL;
5779 while ((c = getopt_long(argc, argv, short_opts,
5780 long_opts, NULL)) != -1) {
5789 if (action != HUA_ARCHIVE &&
5790 action != HUA_REMOVE) {
5792 "error: -a is supported only "
5793 "when archiving or removing\n");
5796 archive_id = atoi(optarg);
5799 if (some_file == NULL) {
5801 some_file = strdup(optarg);
5807 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
5808 argv[0], argv[optind - 1]);
5813 /* All remaining args are files, so we have at least nbfile */
5814 nbfile = argc - optind;
5816 if ((nbfile == 0) && (filelist == NULL))
5820 opaque_len = strlen(opaque);
5822 /* Alloc the request structure with enough place to store all files
5823 * from command line. */
5824 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
5826 fprintf(stderr, "Cannot create the request: %s\n",
5830 nbfile_alloc = nbfile;
5832 hur->hur_request.hr_action = action;
5833 hur->hur_request.hr_archive_id = archive_id;
5834 hur->hur_request.hr_flags = 0;
5836 /* All remaining args are files, add them */
5837 if (nbfile != 0 && some_file == NULL)
5838 some_file = strdup(argv[optind]);
5840 for (i = 0; i < nbfile; i++) {
5841 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
5847 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
5849 /* If a filelist was specified, read the filelist from it. */
5850 if (filelist != NULL) {
5851 fp = fopen(filelist, "r");
5853 fprintf(stderr, "Cannot read the file list %s: %s\n",
5854 filelist, strerror(errno));
5859 while ((rc = getline(&line, &len, fp)) != -1) {
5860 /* If allocated buffer was too small, get something
5862 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
5865 nbfile_alloc = nbfile_alloc * 2 + 1;
5867 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
5870 fprintf(stderr, "hsm: cannot allocate "
5871 "the request: %s\n",
5878 size = hur_len(oldhur);
5880 fprintf(stderr, "hsm: cannot allocate "
5881 "%u files + %u bytes data\n",
5882 oldhur->hur_request.hr_itemcount,
5883 oldhur->hur_request.hr_data_len);
5890 memcpy(hur, oldhur, size);
5895 if (line[strlen(line) - 1] == '\n')
5896 line[strlen(line) - 1] = '\0';
5898 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
5899 mntpath, line, &last_dev);
5905 if (some_file == NULL) {
5915 /* If a --data was used, add it to the request */
5916 hur->hur_request.hr_data_len = opaque_len;
5918 memcpy(hur_data(hur), opaque, opaque_len);
5920 /* Send the HSM request */
5921 if (realpath(some_file, fullpath) == NULL) {
5922 fprintf(stderr, "Could not find path '%s': %s\n",
5923 some_file, strerror(errno));
5925 rc = llapi_hsm_request(fullpath, hur);
5927 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
5928 some_file, strerror(-rc));
5938 static int lfs_hsm_archive(int argc, char **argv)
5940 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
5943 static int lfs_hsm_restore(int argc, char **argv)
5945 return lfs_hsm_request(argc, argv, HUA_RESTORE);
5948 static int lfs_hsm_release(int argc, char **argv)
5950 return lfs_hsm_request(argc, argv, HUA_RELEASE);
5953 static int lfs_hsm_remove(int argc, char **argv)
5955 return lfs_hsm_request(argc, argv, HUA_REMOVE);
5958 static int lfs_hsm_cancel(int argc, char **argv)
5960 return lfs_hsm_request(argc, argv, HUA_CANCEL);
5963 static int lfs_swap_layouts(int argc, char **argv)
5968 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
5969 SWAP_LAYOUTS_KEEP_MTIME |
5970 SWAP_LAYOUTS_KEEP_ATIME);
5973 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
5975 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
5977 static const char *const lockahead_results[] = {
5978 [LLA_RESULT_SENT] = "Lock request sent",
5979 [LLA_RESULT_DIFFERENT] = "Different matching lock found",
5980 [LLA_RESULT_SAME] = "Matching lock on identical extent found",
5983 int lfs_get_mode(const char *string)
5985 enum lock_mode_user mode;
5987 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
5988 if (lock_mode_names[mode] == NULL)
5990 if (strcmp(string, lock_mode_names[mode]) == 0)
5997 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
5999 enum lu_ladvise_type advice;
6002 advice < ARRAY_SIZE(ladvise_names); advice++) {
6003 if (ladvise_names[advice] == NULL)
6005 if (strcmp(string, ladvise_names[advice]) == 0)
6009 return LU_LADVISE_INVALID;
6012 static int lfs_ladvise(int argc, char **argv)
6014 struct option long_opts[] = {
6015 { .val = 'a', .name = "advice", .has_arg = required_argument },
6016 { .val = 'b', .name = "background", .has_arg = no_argument },
6017 { .val = 'e', .name = "end", .has_arg = required_argument },
6018 { .val = 'l', .name = "length", .has_arg = required_argument },
6019 { .val = 'm', .name = "mode", .has_arg = required_argument },
6020 { .val = 's', .name = "start", .has_arg = required_argument },
6021 { .val = 'u', .name = "unset", .has_arg = no_argument },
6023 char short_opts[] = "a:be:l:m:s:u";
6028 struct llapi_lu_ladvise advice;
6029 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
6030 unsigned long long start = 0;
6031 unsigned long long end = LUSTRE_EOF;
6032 unsigned long long length = 0;
6033 unsigned long long size_units;
6034 unsigned long long flags = 0;
6038 while ((c = getopt_long(argc, argv, short_opts,
6039 long_opts, NULL)) != -1) {
6042 advice_type = lfs_get_ladvice(optarg);
6043 if (advice_type == LU_LADVISE_INVALID) {
6044 fprintf(stderr, "%s: invalid advice type "
6045 "'%s'\n", argv[0], optarg);
6046 fprintf(stderr, "Valid types:");
6048 for (advice_type = 0;
6049 advice_type < ARRAY_SIZE(ladvise_names);
6051 if (ladvise_names[advice_type] == NULL)
6053 fprintf(stderr, " %s",
6054 ladvise_names[advice_type]);
6056 fprintf(stderr, "\n");
6069 rc = llapi_parse_size(optarg, &end,
6072 fprintf(stderr, "%s: bad end offset '%s'\n",
6079 rc = llapi_parse_size(optarg, &start,
6082 fprintf(stderr, "%s: bad start offset "
6083 "'%s'\n", argv[0], optarg);
6089 rc = llapi_parse_size(optarg, &length,
6092 fprintf(stderr, "%s: bad length '%s'\n",
6098 mode = lfs_get_mode(optarg);
6100 fprintf(stderr, "%s: bad mode '%s', valid "
6101 "modes are READ or WRITE\n",
6109 fprintf(stderr, "%s: option '%s' unrecognized\n",
6110 argv[0], argv[optind - 1]);
6115 if (advice_type == LU_LADVISE_INVALID) {
6116 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
6117 fprintf(stderr, "Valid types:");
6118 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
6120 if (ladvise_names[advice_type] == NULL)
6122 fprintf(stderr, " %s", ladvise_names[advice_type]);
6124 fprintf(stderr, "\n");
6128 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
6129 fprintf(stderr, "%s: Lock no expand advice is a per file "
6130 "descriptor advice, so when called from lfs, "
6131 "it does nothing.\n", argv[0]);
6135 if (argc <= optind) {
6136 fprintf(stderr, "%s: please give one or more file names\n",
6141 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
6142 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
6147 if (end == LUSTRE_EOF && length != 0)
6148 end = start + length;
6151 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
6152 argv[0], start, end);
6156 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
6157 fprintf(stderr, "%s: mode is only valid with lockahead\n",
6162 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
6163 fprintf(stderr, "%s: mode is required with lockahead\n",
6168 while (optind < argc) {
6171 path = argv[optind++];
6173 fd = open(path, O_RDONLY);
6175 fprintf(stderr, "%s: cannot open file '%s': %s\n",
6176 argv[0], path, strerror(errno));
6181 advice.lla_start = start;
6182 advice.lla_end = end;
6183 advice.lla_advice = advice_type;
6184 advice.lla_value1 = 0;
6185 advice.lla_value2 = 0;
6186 advice.lla_value3 = 0;
6187 advice.lla_value4 = 0;
6188 if (advice_type == LU_LADVISE_LOCKAHEAD) {
6189 advice.lla_lockahead_mode = mode;
6190 advice.lla_peradvice_flags = flags;
6193 rc2 = llapi_ladvise(fd, flags, 1, &advice);
6196 fprintf(stderr, "%s: cannot give advice '%s' to file "
6197 "'%s': %s\n", argv[0],
6198 ladvise_names[advice_type],
6199 path, strerror(errno));
6205 if (rc == 0 && rc2 < 0)
6211 /** The input string contains a comma delimited list of component ids and
6212 * ranges, for example "1,2-4,7".
6214 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
6216 bool end_of_loop = false;
6224 while (!end_of_loop) {
6228 char *endptr = NULL;
6231 ptr = strchrnul(arg, ',');
6232 end_of_loop = *ptr == '\0';
6235 start_index = strtol(arg, &endptr, 0);
6236 if (endptr == arg) /* no data at all */
6238 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
6240 if (start_index < 0)
6243 end_index = start_index;
6244 if (*endptr == '-') {
6245 end_index = strtol(endptr + 1, &endptr, 0);
6246 if (*endptr != '\0')
6248 if (end_index < start_index)
6252 for (i = start_index; i <= end_index && size > 0; i++) {
6255 /* remove duplicate */
6256 for (j = 0; j < nr; j++) {
6260 if (j == nr) { /* no duplicate */
6266 if (size == 0 && i < end_index)
6273 if (!end_of_loop && ptr != NULL)
6276 return rc < 0 ? rc : nr;
6280 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
6281 __u16 *mirror_ids, int ids_nr)
6283 const char *progname = "lfs mirror resync";
6284 struct llapi_resync_comp comp_array[1024] = { { 0 } };
6285 struct llapi_layout *layout;
6293 if (stat(fname, &stbuf) < 0) {
6294 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
6295 progname, fname, strerror(errno));
6299 if (!S_ISREG(stbuf.st_mode)) {
6300 fprintf(stderr, "%s: '%s' is not a regular file.\n",
6306 fd = open(fname, O_DIRECT | O_RDWR);
6308 fprintf(stderr, "%s: cannot open '%s': %s.\n",
6309 progname, fname, strerror(errno));
6314 ioc->lil_mode = LL_LEASE_WRLCK;
6315 ioc->lil_flags = LL_LEASE_RESYNC;
6316 rc = llapi_lease_get_ext(fd, ioc);
6318 fprintf(stderr, "%s: '%s' llapi_lease_get_ext resync failed: "
6319 "%s.\n", progname, fname, strerror(errno));
6323 layout = llapi_layout_get_by_fd(fd, 0);
6324 if (layout == NULL) {
6325 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
6326 progname, fname, strerror(errno));
6331 rc = llapi_layout_flags_get(layout, &flr_state);
6333 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
6334 progname, fname, strerror(errno));
6339 flr_state &= LCM_FL_FLR_MASK;
6340 switch (flr_state) {
6341 case LCM_FL_NOT_FLR:
6344 fprintf(stderr, "%s: '%s' file state error: %s.\n",
6345 progname, fname, lcm_flags_string(flr_state));
6351 /* get stale component info */
6352 comp_size = llapi_mirror_find_stale(layout, comp_array,
6353 ARRAY_SIZE(comp_array),
6354 mirror_ids, ids_nr);
6355 if (comp_size < 0) {
6361 while (idx < comp_size) {
6367 rc = llapi_lease_check(fd);
6368 if (rc != LL_LEASE_WRLCK) {
6369 fprintf(stderr, "%s: '%s' lost lease lock.\n",
6374 mirror_id = comp_array[idx].lrc_mirror_id;
6375 end = comp_array[idx].lrc_end;
6377 /* try to combine adjacent component */
6378 for (i = idx + 1; i < comp_size; i++) {
6379 if (mirror_id != comp_array[i].lrc_mirror_id ||
6380 end != comp_array[i].lrc_start)
6382 end = comp_array[i].lrc_end;
6385 result = llapi_mirror_resync_one(fd, layout, mirror_id,
6386 comp_array[idx].lrc_start,
6389 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
6390 "%ld.\n", progname, fname, result);
6393 } else if (result > 0) {
6396 /* mark synced components */
6397 for (j = idx; j < i; j++)
6398 comp_array[j].lrc_synced = true;
6404 /* prepare ioc for lease put */
6405 ioc->lil_mode = LL_LEASE_UNLCK;
6406 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
6408 for (idx = 0; idx < comp_size; idx++) {
6409 if (comp_array[idx].lrc_synced) {
6410 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
6415 llapi_layout_free(layout);
6417 rc = llapi_lease_get_ext(fd, ioc);
6419 if (rc == 0) /* lost lease lock */
6421 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
6422 progname, fname, strerror(errno));
6426 * llapi_lease_get_ext returns lease mode when it request to unlock
6437 static inline int lfs_mirror_resync(int argc, char **argv)
6439 struct ll_ioc_lease *ioc = NULL;
6440 __u16 mirror_ids[128] = { 0 };
6445 struct option long_opts[] = {
6446 { .val = 'o', .name = "only", .has_arg = required_argument },
6449 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
6452 rc = parse_mirror_ids(mirror_ids,
6453 sizeof(mirror_ids) / sizeof(__u16),
6457 "%s: bad mirror ids '%s'.\n",
6464 fprintf(stderr, "%s: options '%s' unrecognized.\n",
6465 argv[0], argv[optind - 1]);
6471 if (argc == optind) {
6472 fprintf(stderr, "%s: no file name given.\n", argv[0]);
6477 if (ids_nr > 0 && argc > optind + 1) {
6478 fprintf(stderr, "%s: option '--only' cannot be used upon "
6479 "multiple files.\n", argv[0]);
6485 /* set the lease on the file */
6486 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
6488 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
6489 argv[0], strerror(errno));
6494 for (; optind < argc; optind++) {
6495 rc = lfs_mirror_resync_file(argv[optind], ioc,
6496 mirror_ids, ids_nr);
6498 fprintf(stderr, "%s: resync file '%s' failed: %d\n",
6499 argv[0], argv[optind], rc);
6500 /* ignore previous file's error, continue with next file */
6503 memset(ioc, 0, sizeof(__u32) * 4096);
6512 * lfs_mirror() - Parse and execute lfs mirror commands.
6513 * @argc: The count of lfs mirror command line arguments.
6514 * @argv: Array of strings for lfs mirror command line arguments.
6516 * This function parses lfs mirror commands and performs the
6517 * corresponding functions specified in mirror_cmdlist[].
6519 * Return: 0 on success or an error code on failure.
6521 static int lfs_mirror(int argc, char **argv)
6528 Parser_init("lfs-mirror > ", mirror_cmdlist);
6530 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
6532 program_invocation_short_name = cmd;
6534 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
6536 rc = Parser_commands();
6538 return rc < 0 ? -rc : rc;
6542 * lfs_mirror_list_commands() - List lfs mirror commands.
6543 * @argc: The count of command line arguments.
6544 * @argv: Array of strings for command line arguments.
6546 * This function lists lfs mirror commands defined in mirror_cmdlist[].
6548 * Return: 0 on success.
6550 static int lfs_mirror_list_commands(int argc, char **argv)
6552 char buffer[81] = "";
6554 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
6560 static int lfs_list_commands(int argc, char **argv)
6562 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
6564 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
6569 int main(int argc, char **argv)
6573 /* Ensure that liblustreapi constructor has run */
6574 if (!liblustreapi_initialized)
6575 fprintf(stderr, "liblustreapi was not properly initialized\n");
6580 Parser_init("lfs > ", cmdlist);
6582 progname = argv[0]; /* Used in error messages */
6584 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
6586 rc = Parser_commands();
6588 return rc < 0 ? -rc : rc;
6591 #ifdef _LUSTRE_IDL_H_
6592 /* Everything we need here should be included by lustreapi.h. */
6593 # error "lfs should not depend on lustre_idl.h"
6594 #endif /* _LUSTRE_IDL_H_ */