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, 2017, 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>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
67 #include "lfs_project.h"
69 #include <libcfs/util/string.h>
70 #include <libcfs/util/ioctl.h>
71 #include <libcfs/util/parser.h>
72 #include <lustre/lustreapi.h>
73 #include <linux/lustre/lustre_ver.h>
74 #include <linux/lustre/lustre_param.h>
75 #include <linux/lnet/nidstr.h>
78 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
79 #endif /* !ARRAY_SIZE */
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_osts(int argc, char **argv);
88 static int lfs_mdts(int argc, char **argv);
89 static int lfs_df(int argc, char **argv);
90 static int lfs_getname(int argc, char **argv);
91 static int lfs_check(int argc, char **argv);
92 #ifdef HAVE_SYS_QUOTA_H
93 static int lfs_setquota(int argc, char **argv);
94 static int lfs_quota(int argc, char **argv);
95 static int lfs_project(int argc, char **argv);
97 static int lfs_flushctx(int argc, char **argv);
98 static int lfs_cp(int argc, char **argv);
99 static int lfs_ls(int argc, char **argv);
100 static int lfs_poollist(int argc, char **argv);
101 static int lfs_changelog(int argc, char **argv);
102 static int lfs_changelog_clear(int argc, char **argv);
103 static int lfs_fid2path(int argc, char **argv);
104 static int lfs_path2fid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_mirror(int argc, char **argv);
119 static int lfs_mirror_list_commands(int argc, char **argv);
120 static int lfs_list_commands(int argc, char **argv);
121 static inline int lfs_mirror_resync(int argc, char **argv);
122 static inline int lfs_mirror_verify(int argc, char **argv);
124 enum setstripe_origin {
131 static int lfs_setstripe_internal(int argc, char **argv,
132 enum setstripe_origin opc);
134 static inline int lfs_setstripe(int argc, char **argv)
136 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
138 static inline int lfs_setstripe_migrate(int argc, char **argv)
140 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
142 static inline int lfs_mirror_create(int argc, char **argv)
144 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
146 static inline int lfs_mirror_extend(int argc, char **argv)
148 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
150 static inline int lfs_mirror_split(int argc, char **argv)
152 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
155 /* Setstripe and migrate share mostly the same parameters */
156 #define SSM_CMD_COMMON(cmd) \
157 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
158 " [--stripe-count|-c <stripe_count>]\n" \
159 " [--stripe-index|-i <start_ost_idx>]\n" \
160 " [--stripe-size|-S <stripe_size>]\n" \
161 " [--layout|-L <pattern>]\n" \
162 " [--pool|-p <pool_name>]\n" \
163 " [--ost|-o <ost_indices>]\n"
165 #define SSM_HELP_COMMON \
166 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
167 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
168 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
169 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
170 "\t respectively)\n" \
171 "\tpool_name: Name of OST pool to use (default none)\n" \
172 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
173 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
174 "\t Indices be specified in a format of:\n" \
175 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
177 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
178 "\t If --pool is set with --ost, then the OSTs\n" \
179 "\t must be the members of the pool.\n" \
180 "\tcomp_end: Extent end of component, start after previous end.\n"\
181 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
182 "\t respectively, -1 for EOF). Must be a multiple of\n"\
185 #define MIRROR_CREATE_HELP \
186 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
187 "\t setstripe layout options\n" \
188 "\t It defaults to 1 if not specified; if specified,\n" \
189 "\t it must follow the option without a space.\n" \
190 "\t The option can also be repeated multiple times to\n" \
191 "\t separate mirrors that have different layouts.\n" \
192 "\tsetstripe options: Mirror layout\n" \
193 "\t It can be a plain layout or a composite layout.\n" \
194 "\t If not specified, the stripe options inherited\n" \
195 "\t from the previous component will be used.\n" \
196 "\tflags: set flags to the component of the current mirror.\n" \
197 "\t Only \"prefer\" flag is supported so far.\n"
199 #define MIRROR_EXTEND_HELP \
201 "\tvictim_file: The layout of victim_file will be split and used\n" \
202 "\t as a mirror added to the mirrored file.\n" \
203 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
204 "\t from victim file(s) in case the victim file(s)\n" \
205 "\t contains the same data as the original mirrored\n" \
208 #define MIRROR_EXTEND_USAGE \
209 " <--mirror-count|-N[mirror_count]>\n" \
210 " [setstripe options|-f <victim_file>]\n" \
213 #define SETSTRIPE_USAGE \
214 SSM_CMD_COMMON("setstripe") \
215 MIRROR_EXTEND_USAGE \
216 " <directory|filename>\n" \
220 #define MIGRATE_USAGE \
221 SSM_CMD_COMMON("migrate ") \
223 " [--non-block|-n]\n" \
224 " [--non-direct|-D]\n" \
228 "\tblock: Block file access during data migration (default)\n" \
229 "\tnon-block: Abort migrations if concurrent access is detected\n" \
230 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
232 #define SETDIRSTRIPE_USAGE \
233 " [--mdt-count|-c stripe_count>\n" \
234 " [--mdt-index|-i mdt_index]\n" \
235 " [--mdt-hash|-H mdt_hash]\n" \
236 " [--default|-D] [--mode|-m mode] <dir>\n" \
237 "\tstripe_count: stripe count of the striped directory\n" \
238 "\tmdt_index: MDT index of first stripe\n" \
239 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
240 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
241 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
242 "\tdefault_stripe: set default dirstripe of the directory\n" \
243 "\tmode: the mode of the directory\n"
246 * command_t mirror_cmdlist - lfs mirror commands.
248 command_t mirror_cmdlist[] = {
249 { .pc_name = "create", .pc_func = lfs_mirror_create,
250 .pc_help = "Create a mirrored file.\n"
251 "usage: lfs mirror create "
252 "<--mirror-count|-N[mirror_count]> "
253 "[setstripe options] ... <filename|directory>\n"
254 MIRROR_CREATE_HELP },
255 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
256 .pc_help = "Extend a mirrored file.\n"
257 "usage: lfs mirror extend "
258 "<--mirror-count|-N[mirror_count]> [--no-verify] "
259 "[setstripe options|-f <victim_file>] ... <filename>\n"
260 MIRROR_EXTEND_HELP },
261 { .pc_name = "split", .pc_func = lfs_mirror_split,
262 .pc_help = "Split a mirrored file.\n"
263 "usage: lfs mirror split <--mirror-id <mirror_id>> [--destroy|-d] "
264 "[-f <new_file>] <mirrored file>\n"
265 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
266 "\t can be fetched by lfs getstripe command.\n"
267 "\tnew_file: This option indicates the layout of the split\n"
268 "\t mirror will be stored into. If not specified,\n"
269 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
270 "\t will be used.\n" },
271 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
272 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
273 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
274 "<mirrored file> [<mirrored file2>...]\n"},
275 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
276 .pc_help = "Verify mirrored file(s).\n"
277 "usage: lfs mirror verify "
278 "[--only <mirror_id,mirror_id2[,...]>] "
279 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
280 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
281 .pc_help = "list commands supported by lfs mirror"},
282 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
283 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
284 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
288 /* all available commands */
289 command_t cmdlist[] = {
290 {"setstripe", lfs_setstripe, 0,
291 "To create a file with specified striping/composite layout, or\n"
292 "create/replace the default layout on an existing directory:\n"
293 SSM_CMD_COMMON("setstripe")
294 " <directory|filename>\n"
296 "To add component(s) to an existing composite file:\n"
297 SSM_CMD_COMMON("setstripe --component-add")
299 "To totally delete the default striping from an existing directory:\n"
300 "usage: setstripe -d <directory>\n"
302 "To delete the last component(s) from an existing composite file\n"
303 "(note that this will also delete any data in those components):\n"
304 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
305 " [--component-flags|-F <comp_flags>]\n"
307 "\tcomp_id: Unique component ID to delete\n"
308 "\tcomp_flags: 'init' indicating all instantiated components\n"
309 "\t '^init' indicating all uninstantiated components\n"
310 "\t-I and -F cannot be specified at the same time\n"},
311 {"getstripe", lfs_getstripe, 0,
312 "To list the layout pattern for a given file or files in a\n"
313 "directory or recursively for all files in a directory tree.\n"
314 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
315 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
316 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
317 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
318 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
319 " [--component-id[=comp_id]|-I[comp_id]]\n"
320 " [--component-flags[=comp_flags]]\n"
321 " [--component-count]\n"
322 " [--component-start[=[+-]comp_start]]\n"
323 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
324 " <directory|filename> ..."},
325 {"setdirstripe", lfs_setdirstripe, 0,
326 "To create a striped directory on a specified MDT. This can only\n"
327 "be done on MDT0 with the right of administrator.\n"
328 "usage: setdirstripe [OPTION] <directory>\n"
330 {"getdirstripe", lfs_getdirstripe, 0,
331 "To list the layout pattern info for a given directory\n"
332 "or recursively for all directories in a directory tree.\n"
333 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
334 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
335 " [--recursive|-r] [--yaml|-y]\n"
336 " [--default|-D] <dir> ..."},
337 {"mkdir", lfs_setdirstripe, 0,
338 "To create a striped directory on a specified MDT. This can only\n"
339 "be done on MDT0 with the right of administrator.\n"
340 "usage: mkdir [OPTION] <directory>\n"
342 {"rm_entry", lfs_rmentry, 0,
343 "To remove the name entry of the remote directory. Note: This\n"
344 "command will only delete the name entry, i.e. the remote directory\n"
345 "will become inaccessable after this command. This can only be done\n"
346 "by the administrator\n"
347 "usage: rm_entry <dir>\n"},
348 {"pool_list", lfs_poollist, 0,
349 "List pools or pool OSTs\n"
350 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
351 {"find", lfs_find, 0,
352 "find files matching given attributes recursively in directory tree.\n"
353 "usage: find <directory|filename> ...\n"
354 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
355 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N]\n"
356 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
357 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
358 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
359 " [[!] --stripe-count|-c [+-]<stripes>]\n"
360 " [[!] --stripe-index|-i <index,...>]\n"
361 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
362 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
363 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
364 " [[!] --projid <projid>]\n"
365 " [[!] --layout|-L released,raid0,mdt]\n"
366 " [[!] --component-count [+-]<comp_cnt>]\n"
367 " [[!] --component-start [+-]N[kMGTPE]]\n"
368 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
369 " [[!] --component-flags <comp_flags>]\n"
370 " [[!] --mirror-count|-N [+-]<n>]\n"
371 " [[!] --mirror-state <[^]state>]\n"
372 " [[!] --mdt-count|-T [+-]<stripes>]\n"
373 " [[!] --mdt-hash|-H <hashtype>\n"
374 "\t !: used before an option indicates 'NOT' requested attribute\n"
375 "\t -: used before a value indicates less than requested value\n"
376 "\t +: used before a value indicates more than requested value\n"
377 "\thashtype: hash type of the striped directory.\n"
378 "\t fnv_1a_64 FNV-1a hash algorithm\n"
379 "\t all_char sum of characters % MDT_COUNT\n"},
380 {"check", lfs_check, 0,
381 "Display the status of MDS or OSTs (as specified in the command)\n"
382 "or all the servers (MDS and OSTs).\n"
383 "usage: check <osts|mds|servers>"},
384 {"osts", lfs_osts, 0, "list OSTs connected to client "
385 "[for specified path only]\n" "usage: osts [path]"},
386 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
387 "[for specified path only]\n" "usage: mdts [path]"},
389 "report filesystem disk space usage or inodes usage"
390 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
391 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
392 {"getname", lfs_getname, 0, "list instances and specified mount points "
393 "[for specified path only]\n"
394 "Usage: getname [-h]|[path ...] "},
395 #ifdef HAVE_SYS_QUOTA_H
396 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
397 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
398 " -b <block-softlimit> -B <block-hardlimit>\n"
399 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
400 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
401 " [--block-softlimit <block-softlimit>]\n"
402 " [--block-hardlimit <block-hardlimit>]\n"
403 " [--inode-softlimit <inode-softlimit>]\n"
404 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
405 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
406 " [--block-grace <block-grace>]\n"
407 " [--inode-grace <inode-grace>] <filesystem>\n"
408 " -b can be used instead of --block-softlimit/--block-grace\n"
409 " -B can be used instead of --block-hardlimit\n"
410 " -i can be used instead of --inode-softlimit/--inode-grace\n"
411 " -I can be used instead of --inode-hardlimit\n\n"
412 "Note: The total quota space will be split into many qunits and\n"
413 " balanced over all server targets, the minimal qunit size is\n"
414 " 1M bytes for block space and 1K inodes for inode space.\n\n"
415 " Quota space rebalancing process will stop when this mininum\n"
416 " value is reached. As a result, quota exceeded can be returned\n"
417 " while many targets still have 1MB or 1K inodes of spare\n"
419 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
420 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
422 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
423 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
424 {"project", lfs_project, 0,
425 "Change or list project attribute for specified file or directory.\n"
426 "usage: project [-d|-r] <file|directory...>\n"
427 " list project ID and flags on file(s) or directories\n"
428 " project [-p id] [-s] [-r] <file|directory...>\n"
429 " set project ID and/or inherit flag for specified file(s) or directories\n"
430 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
431 " check project ID and flags on file(s) or directories, print outliers\n"
432 " project -C [-r] [-k] <file|directory...>\n"
433 " clear the project inherit flag and ID on the file or directory\n"
436 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
437 "usage: flushctx [-k] [mountpoint...]"},
439 "Remote user copy files and directories.\n"
440 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
442 "Remote user list directory contents.\n"
443 "usage: ls [OPTION]... [FILE]..."},
444 {"changelog", lfs_changelog, 0,
445 "Show the metadata changes on an MDT."
446 "\nusage: changelog <mdtname> [startrec [endrec]]"},
447 {"changelog_clear", lfs_changelog_clear, 0,
448 "Indicate that old changelog records up to <endrec> are no longer of "
449 "interest to consumer <id>, allowing the system to free up space.\n"
450 "An <endrec> of 0 means all records.\n"
451 "usage: changelog_clear <mdtname> <id> <endrec>"},
452 {"fid2path", lfs_fid2path, 0,
453 "Resolve the full path(s) for given FID(s). For a specific hardlink "
454 "specify link number <linkno>.\n"
455 /* "For a historical link name, specify changelog record <recno>.\n" */
456 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
457 /* [ --rec <recno> ] */ },
458 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
459 "usage: path2fid [--parents] <path> ..."},
460 {"data_version", lfs_data_version, 0, "Display file data version for "
461 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
462 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
463 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
464 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
465 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
466 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
467 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
469 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
470 "[--archived] [--lost] <file> ..."},
471 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
472 "given files.\n" "usage: hsm_action <file> ..."},
473 {"hsm_archive", lfs_hsm_archive, 0,
474 "Archive file to external storage.\n"
475 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
477 {"hsm_restore", lfs_hsm_restore, 0,
478 "Restore file from external storage.\n"
479 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
480 {"hsm_release", lfs_hsm_release, 0,
481 "Release files from Lustre.\n"
482 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
483 {"hsm_remove", lfs_hsm_remove, 0,
484 "Remove file copy from external storage.\n"
485 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
487 " (FILE [FILE ...] | "
488 "--mntpath MOUNTPATH FID [FID ...])\n"
490 "Note: To remove an archived copy of a file already deleted from a "
492 "--mntpath option and a list of FIDs must be specified"
494 {"hsm_cancel", lfs_hsm_cancel, 0,
495 "Cancel requests related to specified files.\n"
496 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
497 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
498 "usage: swap_layouts <path1> <path2>"},
499 {"migrate", lfs_setstripe_migrate, 0,
500 "migrate a directory between MDTs.\n"
501 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
503 "\tmdt_idx: index of the destination MDT\n"
505 "migrate file objects from one OST "
506 "layout\nto another (may be not safe with concurent writes).\n"
508 "[--stripe-count|-c] <stripe_count>\n"
509 " [--stripe-index|-i] <start_ost_index>\n"
510 " [--stripe-size|-S] <stripe_size>\n"
511 " [--pool|-p] <pool_name>\n"
512 " [--ost|-o] <ost_indices>\n"
514 " [--non-block|-n]\n"
515 " [--non-direct|-D]\n"
516 " <file|directory>\n"
517 "\tstripe_count: number of OSTs to stripe a file over\n"
518 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
519 "\tstripe_size: number of bytes to store before moving to the next OST\n"
520 "\tpool_name: name of the predefined pool of OSTs\n"
521 "\tost_indices: OSTs to stripe over, in order\n"
522 "\tblock: wait for the operation to return before continuing\n"
523 "\tnon-block: do not wait for the operation to return\n"
524 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
526 "To move directories between MDTs. This command is deprecated, "
527 "use \"migrate\" instead.\n"
528 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
530 {"ladvise", lfs_ladvise, 0,
531 "Provide servers with advice about access patterns for a file.\n"
532 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
533 " [--background|-b] [--unset|-u]\n\n"
534 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
535 " {[--mode|-m [READ,WRITE]}\n"
537 {"mirror", lfs_mirror, mirror_cmdlist,
538 "lfs commands used to manage files with mirrored components:\n"
539 "lfs mirror create - create a mirrored file or directory\n"
540 "lfs mirror extend - add mirror(s) to an existing file\n"
541 "lfs mirror split - split a mirror from an existing mirrored file\n"
542 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
543 "lfs mirror verify - verify mirrored file(s)\n"},
544 {"help", Parser_help, 0, "help"},
545 {"exit", Parser_quit, 0, "quit"},
546 {"quit", Parser_quit, 0, "quit"},
547 {"--version", Parser_version, 0,
548 "output build version of the utility and exit"},
549 {"--list-commands", lfs_list_commands, 0,
550 "list commands supported by the utility and exit"},
555 static int check_hashtype(const char *hashtype)
559 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
560 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
567 static const char *error_loc = "syserror";
570 MIGRATION_NONBLOCK = 1 << 0,
571 MIGRATION_MIRROR = 1 << 1,
572 MIGRATION_NONDIRECT = 1 << 2,
575 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
576 struct llapi_layout *layout);
579 migrate_open_files(const char *name, __u64 migration_flags,
580 const struct llapi_stripe_param *param,
581 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
588 char parent[PATH_MAX];
589 char volatile_file[PATH_MAX];
595 if (param == NULL && layout == NULL) {
596 error_loc = "layout information";
600 /* search for file directory pathname */
601 if (strlen(name) > sizeof(parent) - 1) {
602 error_loc = "source file name";
606 strncpy(parent, name, sizeof(parent));
607 ptr = strrchr(parent, '/');
609 if (getcwd(parent, sizeof(parent)) == NULL) {
610 error_loc = "getcwd";
614 if (ptr == parent) /* leading '/' */
619 /* open file, direct io */
620 /* even if the file is only read, WR mode is nedeed to allow
621 * layout swap on fd */
623 if (!(migration_flags & MIGRATION_NONDIRECT))
625 fd = open(name, rflags);
628 error_loc = "cannot open source file";
632 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
634 error_loc = "cannot get MDT index";
639 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
640 mode_t open_mode = S_IRUSR | S_IWUSR;
642 random_value = random();
643 rc = snprintf(volatile_file, sizeof(volatile_file),
644 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
645 mdt_index, random_value);
646 if (rc >= sizeof(volatile_file)) {
651 /* create, open a volatile file, use caching (ie no directio) */
653 fdv = llapi_file_open_param(volatile_file, open_flags,
656 fdv = lfs_component_create(volatile_file, open_flags,
658 } while (fdv < 0 && (rc = fdv) == -EEXIST);
661 error_loc = "cannot create volatile file";
665 /* In case the MDT does not support creation of volatile files
666 * we should try to unlink it. */
667 (void)unlink(volatile_file);
669 /* Not-owner (root?) special case.
670 * Need to set owner/group of volatile file like original.
671 * This will allow to pass related check during layout_swap.
676 error_loc = "cannot stat source file";
680 rc = fstat(fdv, &stv);
683 error_loc = "cannot stat volatile";
687 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
688 rc = fchown(fdv, st.st_uid, st.st_gid);
691 error_loc = "cannot change ownwership of volatile";
710 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
712 struct llapi_layout *layout;
713 size_t buf_size = 4 * 1024 * 1024;
722 layout = llapi_layout_get_by_fd(fd_src, 0);
723 if (layout != NULL) {
724 uint64_t stripe_size;
726 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
728 buf_size = stripe_size;
730 llapi_layout_free(layout);
733 /* Use a page-aligned buffer for direct I/O */
734 rc = posix_memalign(&buf, getpagesize(), buf_size);
739 /* read new data only if we have written all
740 * previously read data */
743 rc = check_file(fd_src);
748 rsize = read(fd_src, buf, buf_size);
760 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
779 static int migrate_copy_timestamps(int fd, int fdv)
783 if (fstat(fd, &st) == 0) {
784 struct timeval tv[2] = {
785 {.tv_sec = st.st_atime},
786 {.tv_sec = st.st_mtime}
789 return futimes(fdv, tv);
795 static int migrate_block(int fd, int fdv)
802 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
804 error_loc = "cannot get dataversion";
812 /* The grouplock blocks all concurrent accesses to the file.
813 * It has to be taken after llapi_get_data_version as it would
815 rc = llapi_group_lock(fd, gid);
817 error_loc = "cannot get group lock";
821 rc = migrate_copy_data(fd, fdv, NULL);
823 error_loc = "data copy failed";
827 /* Make sure we keep original atime/mtime values */
828 rc = migrate_copy_timestamps(fd, fdv);
830 error_loc = "timestamp copy failed";
835 * for a migration we need to check data version on file did
838 * Pass in gid=0 since we already own grouplock. */
839 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
840 SWAP_LAYOUTS_CHECK_DV1);
842 error_loc = "file changed";
845 error_loc = "cannot swap layout";
850 rc2 = llapi_group_unlock(fd, gid);
851 if (rc2 < 0 && rc == 0) {
852 error_loc = "unlock group lock";
860 * Internal helper for migrate_copy_data(). Check lease and report error if
863 * \param[in] fd File descriptor on which to check the lease.
865 * \retval 0 Migration can keep on going.
866 * \retval -errno Error occurred, abort migration.
868 static int check_lease(int fd)
872 rc = llapi_lease_check(fd);
874 return 0; /* llapi_check_lease returns > 0 on success. */
879 static int migrate_nonblock(int fd, int fdv)
885 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
887 error_loc = "cannot get data version";
891 rc = migrate_copy_data(fd, fdv, check_lease);
893 error_loc = "data copy failed";
897 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
899 error_loc = "cannot get data version";
905 error_loc = "source file changed";
909 /* Make sure we keep original atime/mtime values */
910 rc = migrate_copy_timestamps(fd, fdv);
912 error_loc = "timestamp copy failed";
919 static int lfs_component_set(char *fname, int comp_id,
920 __u32 flags, __u32 neg_flags)
923 __u32 flags_array[2];
928 ids[count] = comp_id;
929 flags_array[count] = flags;
934 ids[count] = comp_id;
935 flags_array[count] = neg_flags | LCME_FL_NEG;
939 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
942 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
943 progname, comp_id, fname, flags, neg_flags);
948 static int lfs_component_del(char *fname, __u32 comp_id,
949 __u32 flags, __u32 neg_flags)
953 if (flags && neg_flags)
956 if (!flags && neg_flags)
957 flags = neg_flags | LCME_FL_NEG;
959 if ((flags && comp_id) || (!flags && !comp_id))
962 /* LCME_FL_INIT is the only supported flag in PFL */
964 if (flags & ~LCME_KNOWN_FLAGS) {
966 "%s setstripe: unknown flags %#x\n",
970 } else if (comp_id > LCME_ID_MAX) {
971 fprintf(stderr, "%s setstripe: invalid component id %u\n",
976 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
979 "%s setstripe: cannot delete component %#x from '%s': %s\n",
980 progname, comp_id, fname, strerror(errno));
984 static int lfs_component_add(char *fname, struct llapi_layout *layout)
991 rc = llapi_layout_file_comp_add(fname, layout);
993 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
994 fname, strerror(errno));
998 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
999 struct llapi_layout *layout)
1007 fd = lstat(fname, &st);
1008 if (fd == 0 && S_ISDIR(st.st_mode))
1009 open_flags = O_DIRECTORY | O_RDONLY;
1011 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1013 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1014 S_ISDIR(st.st_mode) ?
1015 "set default composite layout for" :
1016 "create composite file",
1017 fname, strerror(errno));
1021 static int lfs_migrate(char *name, __u64 migration_flags,
1022 struct llapi_stripe_param *param,
1023 struct llapi_layout *layout)
1029 rc = migrate_open_files(name, migration_flags, param, layout,
1034 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1035 /* Blocking mode (forced if servers do not support file lease).
1036 * It is also the default mode, since we cannot distinguish
1037 * between a broken lease and a server that does not support
1038 * atomic swap/close (LU-6785) */
1039 rc = migrate_block(fd, fdv);
1043 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1045 error_loc = "cannot get lease";
1049 rc = migrate_nonblock(fd, fdv);
1051 llapi_lease_release(fd);
1055 /* Atomically put lease, swap layouts and close.
1056 * for a migration we need to check data version on file did
1058 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1060 error_loc = "cannot swap layout";
1072 fprintf(stderr, "error: %s: %s: %s: %s\n",
1073 progname, name, error_loc, strerror(-rc));
1077 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1086 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1090 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1091 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1092 const char *comp_name = comp_flags_table[i].cfn_name;
1094 if (strcmp(name, comp_name) == 0) {
1095 *flags |= comp_flag;
1097 } else if (strncmp(name, "^", 1) == 0 &&
1098 strcmp(name + 1, comp_name) == 0) {
1099 *neg_flags |= comp_flag;
1104 llapi_printf(LLAPI_MSG_ERROR,
1105 "%s: component flag '%s' not supported\n",
1111 if (!*flags && !*neg_flags)
1114 /* don't allow to set and exclude the same flag */
1115 if (*flags & *neg_flags)
1121 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1129 if (strncmp(string, "^", 1) == 0) {
1130 *neg_state = llapi_layout_string_flags(string + 1);
1131 if (*neg_state != 0)
1134 *state = llapi_layout_string_flags(string);
1139 llapi_printf(LLAPI_MSG_ERROR,
1140 "%s: mirrored file state '%s' not supported\n",
1146 * struct mirror_args - Command-line arguments for mirror(s).
1147 * @m_count: Number of mirrors to be created with this layout.
1148 * @m_flags: Mirror level flags, only 'prefer' is supported.
1149 * @m_layout: Mirror layout.
1150 * @m_file: A victim file. Its layout will be split and used as a mirror.
1151 * @m_next: Point to the next node of the list.
1153 * Command-line arguments for mirror(s) will be parsed and stored in
1154 * a linked list that consists of this structure.
1156 struct mirror_args {
1159 struct llapi_layout *m_layout;
1161 struct mirror_args *m_next;
1164 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1169 rc = llapi_layout_comp_flags_get(layout, &flags);
1173 if (flags & LCME_FL_NEG) {
1174 fprintf(stderr, "error: %s: negative flags are not supported\n",
1179 if (flags & LCME_FL_STALE) {
1180 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1181 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1185 return LLAPI_LAYOUT_ITER_CONT;
1188 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1190 uint64_t start, end;
1194 /* LU-10112: do not support dom+flr in phase 1 */
1195 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1199 rc = llapi_layout_pattern_get(layout, &pattern);
1203 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1204 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1209 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1213 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1217 if (end != LUSTRE_EOF) {
1218 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1223 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1228 * enum mirror_flags - Flags for extending a mirrored file.
1229 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1230 * in case the victim file(s) contains the same data as the
1231 * original mirrored file.
1232 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1234 * Flags for extending a mirrored file.
1242 * mirror_create_sanity_check() - Check mirror list.
1243 * @list: A linked list that stores the mirror arguments.
1245 * This function does a sanity check on @list for creating
1248 * Return: 0 on success or a negative error code on failure.
1250 static int mirror_create_sanity_check(const char *fname,
1251 struct mirror_args *list)
1254 bool has_m_file = false;
1255 bool has_m_layout = false;
1261 struct llapi_layout *layout;
1263 layout = llapi_layout_get_by_path(fname, 0);
1266 "error: %s: file '%s' couldn't get layout\n",
1271 rc = mirror_sanity_check_one(layout);
1272 llapi_layout_free(layout);
1278 while (list != NULL) {
1279 if (list->m_file != NULL) {
1281 llapi_layout_free(list->m_layout);
1284 llapi_layout_get_by_path(list->m_file, 0);
1285 if (list->m_layout == NULL) {
1287 "error: %s: file '%s' has no layout\n",
1288 progname, list->m_file);
1292 has_m_layout = true;
1293 if (list->m_layout == NULL) {
1294 fprintf(stderr, "error: %s: no mirror layout\n",
1300 rc = mirror_sanity_check_one(list->m_layout);
1304 list = list->m_next;
1307 if (has_m_file && has_m_layout) {
1309 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1317 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1319 __u32 mirror_flags = *(__u32 *)cbdata;
1323 rc = llapi_layout_comp_flags_get(layout, &flags);
1328 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1333 return LLAPI_LAYOUT_ITER_CONT;
1337 * mirror_create() - Create a mirrored file.
1338 * @fname: The file to be created.
1339 * @mirror_list: A linked list that stores the mirror arguments.
1341 * This function creates a mirrored file @fname with the mirror(s)
1342 * from @mirror_list.
1344 * Return: 0 on success or a negative error code on failure.
1346 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1348 struct llapi_layout *layout = NULL;
1349 struct mirror_args *cur_mirror = NULL;
1350 uint16_t mirror_count = 0;
1354 rc = mirror_create_sanity_check(NULL, mirror_list);
1358 cur_mirror = mirror_list;
1359 while (cur_mirror != NULL) {
1360 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1362 &cur_mirror->m_flags);
1365 fprintf(stderr, "%s: failed to set mirror flags\n",
1370 for (i = 0; i < cur_mirror->m_count; i++) {
1371 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1374 fprintf(stderr, "error: %s: "
1375 "merge layout failed: %s\n",
1376 progname, strerror(errno));
1380 mirror_count += cur_mirror->m_count;
1381 cur_mirror = cur_mirror->m_next;
1384 if (layout == NULL) {
1385 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1389 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1392 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1393 progname, strerror(errno));
1397 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0644,
1405 llapi_layout_free(layout);
1410 * Compare files and check lease on @fd.
1412 * \retval bytes number of bytes are the same
1414 static ssize_t mirror_file_compare(int fd, int fdv)
1416 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1418 ssize_t bytes_done = 0;
1419 ssize_t bytes_read = 0;
1421 buf = malloc(buflen * 2);
1426 if (!llapi_lease_check(fd)) {
1427 bytes_done = -EBUSY;
1431 bytes_read = read(fd, buf, buflen);
1432 if (bytes_read <= 0)
1435 if (bytes_read != read(fdv, buf + buflen, buflen))
1438 /* XXX: should compute the checksum on each buffer and then
1439 * compare checksum to avoid cache collision */
1440 if (memcmp(buf, buf + buflen, bytes_read))
1443 bytes_done += bytes_read;
1451 static int mirror_extend_file(const char *fname, const char *victim_file,
1452 enum mirror_flags mirror_flags)
1457 struct stat stbuf_v;
1458 struct ll_ioc_lease *data = NULL;
1461 fd = open(fname, O_RDWR);
1463 error_loc = "open source file";
1468 fdv = open(victim_file, O_RDWR);
1470 error_loc = "open target file";
1475 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1476 error_loc = "stat source or target file";
1481 if (stbuf.st_dev != stbuf_v.st_dev) {
1482 error_loc = "stat source and target file";
1487 /* mirrors should be of the same size */
1488 if (stbuf.st_size != stbuf_v.st_size) {
1489 error_loc = "file sizes don't match";
1494 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1496 error_loc = "cannot get lease";
1500 if (!(mirror_flags & MF_NO_VERIFY)) {
1502 /* mirrors should have the same contents */
1503 ret = mirror_file_compare(fd, fdv);
1504 if (ret != stbuf.st_size) {
1505 error_loc = "file busy or contents don't match";
1506 rc = ret < 0 ? ret : -EINVAL;
1511 /* Get rid of caching pages from clients */
1512 rc = llapi_file_flush(fd);
1514 error_loc = "cannot get data version";
1518 rc = llapi_file_flush(fdv);
1520 error_loc = "cannot get data version";
1525 /* Make sure we keep original atime/mtime values */
1526 rc = migrate_copy_timestamps(fd, fdv);
1528 error_loc = "cannot copy timestamp";
1532 /* Atomically put lease, merge layouts and close. */
1533 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1535 error_loc = "memory allocation";
1538 data->lil_mode = LL_LEASE_UNLCK;
1539 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1540 data->lil_count = 1;
1541 data->lil_ids[0] = fdv;
1542 rc = llapi_lease_set(fd, data);
1544 error_loc = "cannot merge layout";
1546 } else if (rc == 0) {
1548 error_loc = "lost lease lock";
1561 (void) unlink(victim_file);
1563 fprintf(stderr, "error: %s: %s: %s: %s\n",
1564 progname, fname, error_loc, strerror(-rc));
1568 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1570 struct ll_ioc_lease *data = NULL;
1575 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1579 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1581 error_loc = "cannot get lease";
1585 rc = migrate_nonblock(fd, fdv);
1587 llapi_lease_release(fd);
1591 /* Atomically put lease, merge layouts and close. */
1592 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1594 error_loc = "memory allocation";
1597 data->lil_mode = LL_LEASE_UNLCK;
1598 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1599 data->lil_count = 1;
1600 data->lil_ids[0] = fdv;
1601 rc = llapi_lease_set(fd, data);
1603 error_loc = "cannot merge layout";
1605 } else if (rc == 0) {
1607 error_loc = "lost lease lock";
1620 fprintf(stderr, "error: %s: %s: %s: %s\n",
1621 progname, name, error_loc, strerror(-rc));
1625 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1626 enum mirror_flags mirror_flags)
1630 rc = mirror_create_sanity_check(fname, mirror_list);
1634 while (mirror_list) {
1635 if (mirror_list->m_file != NULL) {
1636 rc = mirror_extend_file(fname, mirror_list->m_file,
1639 __u32 mirror_count = mirror_list->m_count;
1641 while (mirror_count > 0) {
1642 rc = mirror_extend_layout(fname,
1643 mirror_list->m_layout);
1653 mirror_list = mirror_list->m_next;
1659 static int verify_id(struct llapi_layout *layout, void *cbdata)
1664 rc = llapi_layout_mirror_id_get(layout, &id);
1668 if ((__u16)id == *(__u16 *)cbdata)
1669 return LLAPI_LAYOUT_ITER_STOP;
1671 return LLAPI_LAYOUT_ITER_CONT;
1674 static int mirror_split(const char *fname, __u16 mirror_id,
1675 enum mirror_flags mflags, const char *victim_file)
1677 struct llapi_layout *layout;
1678 char parent[PATH_MAX];
1679 char victim[PATH_MAX];
1680 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1682 struct ll_ioc_lease *data;
1683 uint16_t mirror_count;
1688 /* check fname contains mirror with mirror_id */
1689 layout = llapi_layout_get_by_path(fname, 0);
1692 "error %s: file '%s' couldn't get layout\n",
1697 rc = mirror_sanity_check_one(layout);
1701 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1704 "error %s: file '%s' couldn't get mirror count\n",
1708 if (mirror_count < 2) {
1710 "error %s: file '%s' has %d component, cannot split\n",
1711 progname, fname, mirror_count);
1715 rc = llapi_layout_comp_iterate(layout, verify_id, &mirror_id);
1717 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1720 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1722 "error %s: file '%s' does not contain mirror with id %u\n",
1723 progname, fname, mirror_id);
1727 fd = open(fname, O_RDWR);
1730 "error %s: open file '%s' failed: %s\n",
1731 progname, fname, strerror(errno));
1735 /* get victim file directory pathname */
1736 if (strlen(fname) > sizeof(parent) - 1) {
1737 fprintf(stderr, "error %s: file name of '%s' too long\n",
1742 strncpy(parent, fname, sizeof(parent));
1743 ptr = strrchr(parent, '/');
1745 if (getcwd(parent, sizeof(parent)) == NULL) {
1746 fprintf(stderr, "error %s: getcwd failed: %s\n",
1747 progname, strerror(errno));
1757 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1759 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1764 if (victim_file == NULL) {
1765 /* use a temp file to store the splitted layout */
1766 if (mflags & MF_DESTROY) {
1767 fdv = llapi_create_volatile_idx(parent, mdt_index,
1768 O_LOV_DELAY_CREATE);
1770 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1772 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1775 /* user specified victim file */
1776 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1781 "error %s: create victim file failed: %s\n",
1782 progname, strerror(errno));
1786 /* get lease lock of fname */
1787 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1790 "error %s: cannot get lease of file '%s': %d\n",
1791 progname, fname, rc);
1795 /* Atomatically put lease, split layouts and close. */
1796 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1802 data->lil_mode = LL_LEASE_UNLCK;
1803 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1804 data->lil_count = 2;
1805 data->lil_ids[0] = fdv;
1806 data->lil_ids[1] = mirror_id;
1807 rc = llapi_lease_set(fd, data);
1809 if (rc == 0) /* lost lease lock */
1812 "error %s: cannot split '%s': %s\n",
1813 progname, fname, strerror(-rc));
1824 llapi_layout_free(layout);
1829 * Parse a string containing an target index list into an array of integers.
1831 * The input string contains a comma delimited list of individual
1832 * indices and ranges, for example "1,2-4,7". Add the indices into the
1833 * \a tgts array and remove duplicates.
1835 * \param[out] tgts array to store indices in
1836 * \param[in] size size of \a tgts array
1837 * \param[in] offset starting index in \a tgts
1838 * \param[in] arg string containing OST index list
1840 * \retval positive number of indices in \a tgts
1841 * \retval -EINVAL unable to parse \a arg
1843 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1847 int slots = size - offset;
1854 end_of_loop = false;
1855 while (!end_of_loop) {
1859 char *endptr = NULL;
1863 ptr = strchrnul(arg, ',');
1865 end_of_loop = *ptr == '\0';
1868 start_index = strtol(arg, &endptr, 0);
1869 if (endptr == arg) /* no data at all */
1871 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1874 end_index = start_index;
1875 if (*endptr == '-') {
1876 end_index = strtol(endptr + 1, &endptr, 0);
1877 if (*endptr != '\0')
1879 if (end_index < start_index)
1883 for (i = start_index; i <= end_index && slots > 0; i++) {
1886 /* remove duplicate */
1887 for (j = 0; j < offset; j++) {
1891 if (j == offset) { /* no duplicate */
1896 if (slots == 0 && i < end_index)
1904 if (!end_of_loop && ptr != NULL)
1907 return rc < 0 ? rc : nr;
1910 struct lfs_setstripe_args {
1911 unsigned long long lsa_comp_end;
1912 unsigned long long lsa_stripe_size;
1913 long long lsa_stripe_count;
1914 long long lsa_stripe_off;
1915 __u32 lsa_comp_flags;
1916 __u32 lsa_comp_neg_flags;
1917 unsigned long long lsa_pattern;
1920 char *lsa_pool_name;
1923 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1925 memset(lsa, 0, sizeof(*lsa));
1927 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1928 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1929 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1930 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1931 lsa->lsa_pool_name = NULL;
1935 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1936 * @lsa: Stripe options to be initialized and inherited.
1938 * This function initializes stripe options in @lsa and inherit
1939 * stripe_size, stripe_count and OST pool_name options.
1943 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1945 unsigned long long stripe_size;
1946 long long stripe_count;
1947 char *pool_name = NULL;
1949 stripe_size = lsa->lsa_stripe_size;
1950 stripe_count = lsa->lsa_stripe_count;
1951 pool_name = lsa->lsa_pool_name;
1953 setstripe_args_init(lsa);
1955 lsa->lsa_stripe_size = stripe_size;
1956 lsa->lsa_stripe_count = stripe_count;
1957 lsa->lsa_pool_name = pool_name;
1960 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1962 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1963 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1964 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1965 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1966 lsa->lsa_pool_name != NULL ||
1967 lsa->lsa_comp_end != 0);
1971 * comp_args_to_layout() - Create or extend a composite layout.
1972 * @composite: Pointer to the composite layout.
1973 * @lsa: Stripe options for the new component.
1975 * This function creates or extends a composite layout by adding a new
1976 * component with stripe options from @lsa.
1978 * Return: 0 on success or an error code on failure.
1980 static int comp_args_to_layout(struct llapi_layout **composite,
1981 struct lfs_setstripe_args *lsa)
1983 struct llapi_layout *layout = *composite;
1984 uint64_t prev_end = 0;
1987 if (layout == NULL) {
1988 layout = llapi_layout_alloc();
1989 if (layout == NULL) {
1990 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1994 *composite = layout;
1998 /* Get current component extent, current component
1999 * must be the tail component. */
2000 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2002 fprintf(stderr, "Get comp extent failed. %s\n",
2007 rc = llapi_layout_comp_add(layout);
2009 fprintf(stderr, "Add component failed. %s\n",
2015 rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
2017 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2018 prev_end, lsa->lsa_comp_end, strerror(errno));
2022 /* Data-on-MDT component setting */
2023 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2024 /* In case of Data-on-MDT patterns the only extra option
2025 * applicable is stripe size option. */
2026 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2027 fprintf(stderr, "Option 'stripe-count' can't be "
2028 "specified with Data-on-MDT component: %lld\n",
2029 lsa->lsa_stripe_count);
2032 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2033 fprintf(stderr, "Option 'stripe-size' can't be "
2034 "specified with Data-on-MDT component: %llu\n",
2035 lsa->lsa_stripe_size);
2038 if (lsa->lsa_nr_tgts != 0) {
2039 fprintf(stderr, "Option 'ost-list' can't be specified "
2040 "with Data-on-MDT component: '%i'\n",
2044 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2045 fprintf(stderr, "Option 'stripe-offset' can't be "
2046 "specified with Data-on-MDT component: %lld\n",
2047 lsa->lsa_stripe_off);
2050 if (lsa->lsa_pool_name != 0) {
2051 fprintf(stderr, "Option 'pool' can't be specified "
2052 "with Data-on-MDT component: '%s'\n",
2053 lsa->lsa_pool_name);
2057 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2059 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2060 lsa->lsa_pattern, strerror(errno));
2063 /* Data-on-MDT component has always single stripe up to end */
2064 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2067 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2069 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2070 lsa->lsa_stripe_size, strerror(errno));
2074 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2076 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2077 lsa->lsa_stripe_count, strerror(errno));
2081 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2083 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2084 lsa->lsa_comp_flags, strerror(errno));
2088 if (lsa->lsa_pool_name != NULL) {
2089 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2091 fprintf(stderr, "Set pool name: %s failed. %s\n",
2092 lsa->lsa_pool_name, strerror(errno));
2096 rc = llapi_layout_pool_name_set(layout, "");
2098 fprintf(stderr, "Clear pool name failed: %s\n",
2104 if (lsa->lsa_nr_tgts > 0) {
2105 if (lsa->lsa_stripe_count > 0 &&
2106 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2107 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2108 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2109 fprintf(stderr, "stripe_count(%lld) != nr_osts(%d)\n",
2110 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2113 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2114 rc = llapi_layout_ost_index_set(layout, i,
2119 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2120 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2123 fprintf(stderr, "Set ost index %d failed. %s\n",
2124 i, strerror(errno));
2131 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2132 * end of the last component in the existing file, and adjust the
2133 * first extent start of the components to be added accordingly. */
2134 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2136 struct llapi_layout *head;
2137 uint64_t start, end, stripe_size, prev_end = 0;
2140 if (layout == NULL) {
2142 "%s setstripe: layout must be specified\n",
2148 head = llapi_layout_get_by_path(fname, 0);
2151 "%s setstripe: cannot read layout from '%s': %s\n",
2152 progname, fname, strerror(errno));
2154 } else if (errno == ENODATA) {
2155 /* file without LOVEA, this component-add will be turned
2156 * into a component-create. */
2157 llapi_layout_free(head);
2159 } else if (!llapi_layout_is_composite(head)) {
2160 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2162 llapi_layout_free(head);
2166 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2168 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2169 progname, strerror(errno));
2170 llapi_layout_free(head);
2174 llapi_layout_free(head);
2176 /* Make sure we use the first component of the layout to be added. */
2177 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2180 "%s setstripe: cannot move component cursor: %s\n",
2181 progname, strerror(errno));
2185 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2187 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2188 progname, strerror(errno));
2192 if (start > prev_end || end <= prev_end) {
2194 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2195 progname, start, end, prev_end);
2199 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2201 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2202 progname, strerror(errno));
2206 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2207 (prev_end & (stripe_size - 1))) {
2209 "%s setstripe: stripe size %lu not aligned with %lu\n",
2210 progname, stripe_size, prev_end);
2214 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2217 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2218 progname, prev_end, end, strerror(errno));
2225 static inline bool arg_is_eof(char *arg)
2227 return !strncmp(arg, "-1", strlen("-1")) ||
2228 !strncmp(arg, "EOF", strlen("EOF")) ||
2229 !strncmp(arg, "eof", strlen("eof"));
2233 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2235 * Return: Valid mirror_args pointer on success and
2236 * NULL if memory allocation fails.
2238 static struct mirror_args *lfs_mirror_alloc(void)
2240 struct mirror_args *mirror = NULL;
2243 mirror = calloc(1, sizeof(*mirror));
2254 * lfs_mirror_free() - Free memory allocated for a mirror argument
2256 * @mirror: Previously allocated mirror argument structure by
2257 * lfs_mirror_alloc().
2259 * Free memory allocated for @mirror.
2263 static void lfs_mirror_free(struct mirror_args *mirror)
2265 if (mirror->m_layout != NULL)
2266 llapi_layout_free(mirror->m_layout);
2271 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2272 * @mirror_list: Previously allocated mirror list.
2274 * Free memory allocated for @mirror_list.
2278 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2280 struct mirror_args *next_mirror = NULL;
2282 while (mirror_list != NULL) {
2283 next_mirror = mirror_list->m_next;
2284 lfs_mirror_free(mirror_list);
2285 mirror_list = next_mirror;
2297 LFS_COMP_NO_VERIFY_OPT,
2299 LFS_MIRROR_FLAGS_OPT,
2301 LFS_MIRROR_STATE_OPT,
2305 static int lfs_setstripe_internal(int argc, char **argv,
2306 enum setstripe_origin opc)
2308 struct lfs_setstripe_args lsa;
2309 struct llapi_stripe_param *param = NULL;
2310 struct find_param migrate_mdt_param = {
2320 char *mdt_idx_arg = NULL;
2321 unsigned long long size_units = 1;
2322 bool migrate_mode = false;
2323 bool migration_block = false;
2324 __u64 migration_flags = 0;
2325 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2326 int comp_del = 0, comp_set = 0;
2329 struct llapi_layout *layout = NULL;
2330 struct llapi_layout **lpp = &layout;
2331 bool mirror_mode = false;
2332 bool has_m_file = false;
2333 __u32 mirror_count = 0;
2334 enum mirror_flags mirror_flags = 0;
2335 struct mirror_args *mirror_list = NULL;
2336 struct mirror_args *new_mirror = NULL;
2337 struct mirror_args *last_mirror = NULL;
2338 __u16 mirror_id = 0;
2341 struct option long_opts[] = {
2342 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2343 /* --block is only valid in migrate mode */
2344 { .val = 'b', .name = "block", .has_arg = no_argument },
2345 { .val = LFS_COMP_ADD_OPT,
2346 .name = "comp-add", .has_arg = no_argument },
2347 { .val = LFS_COMP_ADD_OPT,
2348 .name = "component-add", .has_arg = no_argument },
2349 { .val = LFS_COMP_DEL_OPT,
2350 .name = "comp-del", .has_arg = no_argument },
2351 { .val = LFS_COMP_DEL_OPT,
2352 .name = "component-del", .has_arg = no_argument },
2353 { .val = LFS_COMP_FLAGS_OPT,
2354 .name = "comp-flags", .has_arg = required_argument },
2355 { .val = LFS_COMP_FLAGS_OPT,
2356 .name = "component-flags",
2357 .has_arg = required_argument },
2358 { .val = LFS_COMP_SET_OPT,
2359 .name = "comp-set", .has_arg = no_argument },
2360 { .val = LFS_COMP_SET_OPT,
2361 .name = "component-set",
2362 .has_arg = no_argument},
2363 { .val = LFS_COMP_NO_VERIFY_OPT,
2364 .name = "no-verify", .has_arg = no_argument},
2365 { .val = LFS_MIRROR_FLAGS_OPT,
2366 .name = "flags", .has_arg = required_argument},
2367 { .val = LFS_MIRROR_ID_OPT,
2368 .name = "mirror-id", .has_arg = required_argument},
2369 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2370 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2371 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2372 { .val = 'd', .name = "delete", .has_arg = no_argument},
2373 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2374 /* --non-direct is only valid in migrate mode */
2375 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2376 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2377 { .val = 'E', .name = "component-end",
2378 .has_arg = required_argument},
2379 { .val = 'f', .name = "file", .has_arg = required_argument },
2380 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2381 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2382 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2383 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
2384 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2385 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2386 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2387 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2388 { .val = 'L', .name = "layout", .has_arg = required_argument },
2389 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2390 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2391 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2392 /* --non-block is only valid in migrate mode */
2393 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2394 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2395 { .val = 'o', .name = "ost", .has_arg = required_argument },
2396 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2397 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2398 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2400 { .val = 'p', .name = "pool", .has_arg = required_argument },
2401 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2402 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2403 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2404 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2405 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2406 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2407 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2408 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2409 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2410 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2411 /* --verbose is only valid in migrate mode */
2412 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2413 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
2416 setstripe_args_init(&lsa);
2418 migrate_mode = (opc == SO_MIGRATE);
2419 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2421 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2423 while ((c = getopt_long(argc, argv, "bc:dDE:f:i:I:m:N::no:p:L:s:S:v",
2424 long_opts, NULL)) >= 0) {
2429 case LFS_COMP_ADD_OPT:
2432 case LFS_COMP_DEL_OPT:
2435 case LFS_COMP_FLAGS_OPT:
2436 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2437 &lsa.lsa_comp_neg_flags);
2440 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2441 fprintf(stderr, "%s: inverted flags are not supported\n",
2445 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2447 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2454 case LFS_COMP_SET_OPT:
2457 case LFS_COMP_NO_VERIFY_OPT:
2458 mirror_flags |= MF_NO_VERIFY;
2460 case LFS_MIRROR_ID_OPT:
2461 mirror_id = strtoul(optarg, &end, 0);
2462 if (*end != '\0' || mirror_id == 0) {
2464 "%s %s: invalid mirror ID '%s'\n",
2465 progname, argv[0], optarg);
2469 case LFS_MIRROR_FLAGS_OPT: {
2472 if (!mirror_mode || !last_mirror) {
2473 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2478 result = comp_str2flags(optarg, &last_mirror->m_flags,
2484 fprintf(stderr, "%s: inverted flags are not supported\n",
2489 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2491 "%s: unsupported mirror flags: %s\n",
2499 if (!migrate_mode) {
2501 "%s %s: -b|--block valid only for migrate command\n",
2505 migration_block = true;
2508 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2511 "%s %s: invalid stripe count '%s'\n",
2512 progname, argv[0], optarg);
2516 if (lsa.lsa_stripe_count == -1)
2517 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2520 /* delete the default striping pattern */
2522 if (opc == SO_MIRROR_SPLIT) {
2525 "%s %s: -d cannot used with -f\n",
2529 mirror_flags |= MF_DESTROY;
2533 if (!migrate_mode) {
2535 "%s %s: -D|--non-direct is valid "
2536 "only for migrate command\n",
2540 migration_flags |= MIGRATION_NONDIRECT;
2543 if (lsa.lsa_comp_end != 0) {
2544 result = comp_args_to_layout(lpp, &lsa);
2547 "%s %s: invalid layout\n",
2552 setstripe_args_init_inherit(&lsa);
2555 if (arg_is_eof(optarg)) {
2556 lsa.lsa_comp_end = LUSTRE_EOF;
2558 result = llapi_parse_size(optarg,
2563 "%s %s: invalid component end '%s'\n",
2564 progname, argv[0], optarg);
2570 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2573 "%s %s: invalid stripe offset '%s'\n",
2574 progname, argv[0], optarg);
2577 if (lsa.lsa_stripe_off == -1)
2578 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2581 comp_id = strtoul(optarg, &end, 0);
2582 if (*end != '\0' || comp_id == 0 ||
2583 comp_id > LCME_ID_MAX) {
2585 "%s %s: invalid component ID '%s'\n",
2586 progname, argv[0], optarg);
2591 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2593 "error: %s: invalid option: %s\n",
2594 progname, argv[optopt + 1]);
2597 if (opc == SO_MIRROR_EXTEND) {
2598 if (last_mirror == NULL) {
2600 "error: %s: '-N' must exist in front of '%s'\n",
2601 progname, argv[optopt + 1]);
2604 last_mirror->m_file = optarg;
2605 last_mirror->m_count = 1;
2608 if (mirror_list == NULL)
2609 mirror_list = lfs_mirror_alloc();
2610 mirror_list->m_file = optarg;
2615 if (strcmp(argv[optind - 1], "mdt") == 0) {
2616 /* Can be only the first component */
2617 if (layout != NULL) {
2619 fprintf(stderr, "error: 'mdt' layout "
2620 "can be only the first one\n");
2623 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2625 fprintf(stderr, "error: 'mdt' layout "
2626 "size is too big\n");
2629 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2630 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2632 fprintf(stderr, "error: layout '%s' is "
2633 "unknown, supported layouts are: "
2634 "'mdt', 'raid0'\n", argv[optind]);
2639 if (!migrate_mode) {
2641 "%s %s: -m|--mdt-index valid only for migrate command\n",
2645 mdt_idx_arg = optarg;
2648 if (!migrate_mode) {
2650 "%s %s: -n|--non-block valid only for migrate command\n",
2654 migration_flags |= MIGRATION_NONBLOCK;
2657 if (opc == SO_SETSTRIPE) {
2658 opc = SO_MIRROR_CREATE;
2662 if (optarg != NULL) {
2663 mirror_count = strtoul(optarg, &end, 0);
2664 if (*end != '\0' || mirror_count == 0) {
2666 "error: %s: bad mirror count: %s\n",
2673 new_mirror = lfs_mirror_alloc();
2674 new_mirror->m_count = mirror_count;
2676 if (mirror_list == NULL)
2677 mirror_list = new_mirror;
2679 if (last_mirror != NULL) {
2680 /* wrap up last mirror */
2681 if (lsa.lsa_comp_end == 0)
2682 lsa.lsa_comp_end = LUSTRE_EOF;
2684 result = comp_args_to_layout(lpp, &lsa);
2686 lfs_mirror_free(new_mirror);
2690 setstripe_args_init_inherit(&lsa);
2692 last_mirror->m_next = new_mirror;
2695 last_mirror = new_mirror;
2696 lpp = &last_mirror->m_layout;
2699 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2700 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2701 fprintf(stderr, "warning: '--ost-list' is "
2702 "deprecated, use '--ost' instead\n");
2704 lsa.lsa_nr_tgts = parse_targets(osts,
2705 sizeof(osts) / sizeof(__u32),
2706 lsa.lsa_nr_tgts, optarg);
2707 if (lsa.lsa_nr_tgts < 0) {
2709 "%s %s: invalid OST target(s) '%s'\n",
2710 progname, argv[0], optarg);
2714 lsa.lsa_tgts = osts;
2715 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2716 lsa.lsa_stripe_off = osts[0];
2721 lsa.lsa_pool_name = optarg;
2723 if (strlen(lsa.lsa_pool_name) == 0 ||
2724 strncmp(lsa.lsa_pool_name, "none",
2725 LOV_MAXPOOLNAME) == 0)
2726 lsa.lsa_pool_name = NULL;
2729 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2733 "%s %s: invalid stripe size '%s'\n",
2734 progname, argv[0], optarg);
2739 if (!migrate_mode) {
2741 "%s %s: -v|--verbose valid only for migrate command\n",
2745 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2748 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2749 progname, argv[0], argv[optind - 1]);
2754 fname = argv[optind];
2756 if (optind == argc) {
2757 fprintf(stderr, "%s %s: FILE must be specified\n",
2762 if (mirror_mode && mirror_count == 0) {
2764 "error: %s: --mirror-count|-N option is required\n",
2771 if (lsa.lsa_comp_end == 0)
2772 lsa.lsa_comp_end = LUSTRE_EOF;
2775 if (lsa.lsa_comp_end != 0) {
2776 result = comp_args_to_layout(lpp, &lsa);
2781 if (mirror_flags & MF_NO_VERIFY) {
2782 if (opc != SO_MIRROR_EXTEND) {
2784 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2788 } else if (!has_m_file) {
2790 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2797 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2798 * altered by user space tool, so we don't need to support the
2799 * --component-set for this moment. */
2800 if (comp_set && !comp_id) {
2801 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2806 if ((delete + comp_set + comp_del + comp_add) > 1) {
2808 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2813 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2814 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2816 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2821 if ((comp_set || comp_del) &&
2822 (setstripe_args_specified(&lsa) || layout != NULL)) {
2824 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
2829 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
2831 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
2836 if (comp_add || comp_del) {
2839 result = lstat(fname, &st);
2840 if (result == 0 && S_ISDIR(st.st_mode)) {
2842 "%s setstripe: cannot use --component-add or --component-del for directory\n",
2848 fprintf(stderr, "error: %s: can't use --component-add "
2849 "or --component-del for mirror operation\n",
2856 if (layout == NULL) {
2858 "%s %s: option -E must be specified with --component-add\n",
2863 result = adjust_first_extent(fname, layout);
2864 if (result == -ENODATA)
2866 else if (result != 0)
2870 if (mdt_idx_arg != NULL && optind > 3) {
2872 "%s %s: option -m cannot be used with other options\n",
2877 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
2879 "%s %s: options --non-block and --block are mutually exclusive\n",
2884 if (!comp_del && !comp_set && comp_id != 0) {
2886 "%s %s: option -I can only be used with --component-del\n",
2891 if (mdt_idx_arg != NULL) {
2892 /* initialize migrate mdt parameters */
2893 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
2895 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
2896 progname, argv[0], mdt_idx_arg);
2899 migrate_mdt_param.fp_migrate = 1;
2900 } else if (layout == NULL) {
2901 /* initialize stripe parameters */
2902 param = calloc(1, offsetof(typeof(*param),
2903 lsp_osts[lsa.lsa_nr_tgts]));
2904 if (param == NULL) {
2906 "%s %s: cannot allocate memory for parameters: %s\n",
2907 progname, argv[0], strerror(ENOMEM));
2912 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
2913 param->lsp_stripe_size = lsa.lsa_stripe_size;
2914 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2915 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
2916 param->lsp_stripe_count = -1;
2918 param->lsp_stripe_count = lsa.lsa_stripe_count;
2920 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2921 param->lsp_stripe_offset = -1;
2923 param->lsp_stripe_offset = lsa.lsa_stripe_off;
2924 param->lsp_pool = lsa.lsa_pool_name;
2925 param->lsp_is_specific = false;
2926 if (lsa.lsa_nr_tgts > 0) {
2927 if (lsa.lsa_stripe_count > 0 &&
2928 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2929 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2930 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
2931 fprintf(stderr, "error: %s: stripe count %lld "
2932 "doesn't match the number of OSTs: %d\n"
2933 , argv[0], lsa.lsa_stripe_count,
2939 param->lsp_is_specific = true;
2940 param->lsp_stripe_count = lsa.lsa_nr_tgts;
2941 memcpy(param->lsp_osts, osts,
2942 sizeof(*osts) * lsa.lsa_nr_tgts);
2946 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
2947 if (mdt_idx_arg != NULL) {
2948 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
2949 } else if (migrate_mode) {
2950 result = lfs_migrate(fname, migration_flags, param,
2952 } else if (comp_set != 0) {
2953 result = lfs_component_set(fname, comp_id,
2955 lsa.lsa_comp_neg_flags);
2956 } else if (comp_del != 0) {
2957 result = lfs_component_del(fname, comp_id,
2959 lsa.lsa_comp_neg_flags);
2960 } else if (comp_add != 0) {
2961 result = lfs_component_add(fname, layout);
2962 } else if (opc == SO_MIRROR_CREATE) {
2963 result = mirror_create(fname, mirror_list);
2964 } else if (opc == SO_MIRROR_EXTEND) {
2965 result = mirror_extend(fname, mirror_list,
2967 } else if (opc == SO_MIRROR_SPLIT) {
2968 if (mirror_id == 0) {
2970 "%s %s: no mirror id is specified\n",
2974 result = mirror_split(fname, mirror_id, mirror_flags,
2975 has_m_file ? mirror_list->m_file :
2977 } else if (layout != NULL) {
2978 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
2985 result = llapi_file_open_param(fname,
2994 /* Save the first error encountered. */
3002 llapi_layout_free(layout);
3003 lfs_mirror_list_free(mirror_list);
3008 llapi_layout_free(layout);
3009 lfs_mirror_list_free(mirror_list);
3013 static int lfs_poollist(int argc, char **argv)
3018 return llapi_poollist(argv[1]);
3021 static int set_time(time_t *time, time_t *set, char *str)
3028 else if (str[0] == '-')
3034 t = strtol(str, NULL, 0);
3035 if (*time < t * 24 * 60 * 60) {
3038 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
3042 *set = *time - t * 24 * 60 * 60;
3045 static int name2uid(unsigned int *id, const char *name)
3047 struct passwd *passwd;
3049 passwd = getpwnam(name);
3052 *id = passwd->pw_uid;
3057 static int name2gid(unsigned int *id, const char *name)
3059 struct group *group;
3061 group = getgrnam(name);
3064 *id = group->gr_gid;
3069 static inline int name2projid(unsigned int *id, const char *name)
3074 static int uid2name(char **name, unsigned int id)
3076 struct passwd *passwd;
3078 passwd = getpwuid(id);
3081 *name = passwd->pw_name;
3086 static inline int gid2name(char **name, unsigned int id)
3088 struct group *group;
3090 group = getgrgid(id);
3093 *name = group->gr_name;
3098 static int name2layout(__u32 *layout, char *name)
3100 char *ptr, *layout_name;
3103 for (ptr = name; ; ptr = NULL) {
3104 layout_name = strtok(ptr, ",");
3105 if (layout_name == NULL)
3107 if (strcmp(layout_name, "released") == 0)
3108 *layout |= LOV_PATTERN_F_RELEASED;
3109 else if (strcmp(layout_name, "raid0") == 0)
3110 *layout |= LOV_PATTERN_RAID0;
3111 else if (strcmp(layout_name, "mdt") == 0)
3112 *layout |= LOV_PATTERN_MDT;
3119 static int lfs_find(int argc, char **argv)
3124 struct find_param param = {
3128 struct option long_opts[] = {
3129 { .val = 'A', .name = "atime", .has_arg = required_argument },
3130 { .val = LFS_COMP_COUNT_OPT,
3131 .name = "comp-count", .has_arg = required_argument },
3132 { .val = LFS_COMP_COUNT_OPT,
3133 .name = "component-count",
3134 .has_arg = required_argument },
3135 { .val = LFS_COMP_FLAGS_OPT,
3136 .name = "comp-flags", .has_arg = required_argument },
3137 { .val = LFS_COMP_FLAGS_OPT,
3138 .name = "component-flags",
3139 .has_arg = required_argument },
3140 { .val = LFS_COMP_START_OPT,
3141 .name = "comp-start", .has_arg = required_argument },
3142 { .val = LFS_COMP_START_OPT,
3143 .name = "component-start",
3144 .has_arg = required_argument },
3145 { .val = LFS_MIRROR_STATE_OPT,
3146 .name = "mirror-state", .has_arg = required_argument },
3147 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3148 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3149 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3150 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3151 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3152 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3153 { .val = 'E', .name = "component-end",
3154 .has_arg = required_argument },
3155 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3156 { .val = 'g', .name = "gid", .has_arg = required_argument },
3157 { .val = 'G', .name = "group", .has_arg = required_argument },
3158 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3159 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3160 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3161 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3162 { .val = 'L', .name = "layout", .has_arg = required_argument },
3163 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3164 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3165 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3166 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3167 { .val = 'n', .name = "name", .has_arg = required_argument },
3168 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3169 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3170 { .val = 'O', .name = "obd", .has_arg = required_argument },
3171 { .val = 'O', .name = "ost", .has_arg = required_argument },
3172 /* no short option for pool yet, can be 'p' after 2.18 */
3173 { .val = LFS_POOL_OPT,
3174 .name = "pool", .has_arg = required_argument },
3175 { .val = '0', .name = "print0", .has_arg = no_argument },
3176 { .val = 'P', .name = "print", .has_arg = no_argument },
3177 { .val = LFS_PROJID_OPT,
3178 .name = "projid", .has_arg = required_argument },
3179 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3180 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3181 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3182 { .val = 's', .name = "size", .has_arg = required_argument },
3183 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3184 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3185 { .val = 't', .name = "type", .has_arg = required_argument },
3186 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3187 { .val = 'u', .name = "uid", .has_arg = required_argument },
3188 { .val = 'U', .name = "user", .has_arg = required_argument },
3189 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3190 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3202 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3203 while ((c = getopt_long_only(argc, argv,
3204 "-0A:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3205 long_opts, NULL)) >= 0) {
3210 /* '!' is part of option */
3211 /* when getopt_long_only() finds a string which is not
3212 * an option nor a known option argument it returns 1
3213 * in that case if we already have found pathstart and pathend
3214 * (i.e. we have the list of pathnames),
3215 * the only supported value is "!"
3217 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3218 if (!isoption && pathend != -1) {
3219 fprintf(stderr, "err: %s: filename|dirname must either "
3220 "precede options or follow options\n",
3225 if (!isoption && pathstart == -1)
3226 pathstart = optind - 1;
3227 if (isoption && pathstart != -1 && pathend == -1)
3228 pathend = optind - 2;
3234 /* unknown; opt is "!" or path component,
3235 * checking done above.
3237 if (strcmp(optarg, "!") == 0)
3241 xtime = ¶m.fp_atime;
3242 xsign = ¶m.fp_asign;
3243 param.fp_exclude_atime = !!neg_opt;
3244 /* no break, this falls through to 'C' for ctime */
3247 xtime = ¶m.fp_ctime;
3248 xsign = ¶m.fp_csign;
3249 param.fp_exclude_ctime = !!neg_opt;
3251 /* no break, this falls through to 'M' for mtime */
3254 xtime = ¶m.fp_mtime;
3255 xsign = ¶m.fp_msign;
3256 param.fp_exclude_mtime = !!neg_opt;
3258 rc = set_time(&t, xtime, optarg);
3259 if (rc == INT_MAX) {
3266 case LFS_COMP_COUNT_OPT:
3267 if (optarg[0] == '+') {
3268 param.fp_comp_count_sign = -1;
3270 } else if (optarg[0] == '-') {
3271 param.fp_comp_count_sign = 1;
3275 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3276 if (*endptr != '\0') {
3277 fprintf(stderr, "error: bad component count "
3281 param.fp_check_comp_count = 1;
3282 param.fp_exclude_comp_count = !!neg_opt;
3284 case LFS_COMP_FLAGS_OPT:
3285 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3286 ¶m.fp_comp_neg_flags);
3288 fprintf(stderr, "error: bad component flags "
3292 param.fp_check_comp_flags = 1;
3294 __u32 flags = param.fp_comp_neg_flags;
3295 param.fp_comp_neg_flags = param.fp_comp_flags;
3296 param.fp_comp_flags = flags;
3299 case LFS_COMP_START_OPT:
3300 if (optarg[0] == '+') {
3301 param.fp_comp_start_sign = -1;
3303 } else if (optarg[0] == '-') {
3304 param.fp_comp_start_sign = 1;
3308 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3309 ¶m.fp_comp_start_units, 0);
3311 fprintf(stderr, "error: bad component start "
3315 param.fp_check_comp_start = 1;
3316 param.fp_exclude_comp_start = !!neg_opt;
3318 case LFS_MIRROR_STATE_OPT:
3319 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3320 ¶m.fp_mirror_neg_state);
3323 "error: bad mirrored file state '%s'\n",
3327 param.fp_check_mirror_state = 1;
3329 __u16 state = param.fp_mirror_neg_state;
3330 param.fp_mirror_neg_state =
3331 param.fp_mirror_state;
3332 param.fp_mirror_state = state;
3336 if (optarg[0] == '+') {
3337 param.fp_stripe_count_sign = -1;
3339 } else if (optarg[0] == '-') {
3340 param.fp_stripe_count_sign = 1;
3344 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3345 if (*endptr != '\0') {
3346 fprintf(stderr,"error: bad stripe_count '%s'\n",
3351 param.fp_check_stripe_count = 1;
3352 param.fp_exclude_stripe_count = !!neg_opt;
3355 param.fp_max_depth = strtol(optarg, 0, 0);
3358 if (optarg[0] == '+') {
3359 param.fp_comp_end_sign = -1;
3361 } else if (optarg[0] == '-') {
3362 param.fp_comp_end_sign = 1;
3366 if (arg_is_eof(optarg)) {
3367 param.fp_comp_end = LUSTRE_EOF;
3368 param.fp_comp_end_units = 1;
3371 rc = llapi_parse_size(optarg,
3373 ¶m.fp_comp_end_units, 0);
3376 fprintf(stderr, "error: bad component end "
3380 param.fp_check_comp_end = 1;
3381 param.fp_exclude_comp_end = !!neg_opt;
3385 rc = name2gid(¶m.fp_gid, optarg);
3387 param.fp_gid = strtoul(optarg, &endptr, 10);
3388 if (*endptr != '\0') {
3389 fprintf(stderr, "Group/GID: %s cannot "
3390 "be found.\n", optarg);
3395 param.fp_exclude_gid = !!neg_opt;
3396 param.fp_check_gid = 1;
3399 param.fp_hash_type = check_hashtype(optarg);
3400 if (param.fp_hash_type == 0) {
3401 fprintf(stderr, "error: bad hash_type '%s'\n",
3406 param.fp_check_hash_type = 1;
3407 param.fp_exclude_hash_type = !!neg_opt;
3410 ret = name2layout(¶m.fp_layout, optarg);
3413 param.fp_exclude_layout = !!neg_opt;
3414 param.fp_check_layout = 1;
3418 rc = name2uid(¶m.fp_uid, optarg);
3420 param.fp_uid = strtoul(optarg, &endptr, 10);
3421 if (*endptr != '\0') {
3422 fprintf(stderr, "User/UID: %s cannot "
3423 "be found.\n", optarg);
3428 param.fp_exclude_uid = !!neg_opt;
3429 param.fp_check_uid = 1;
3432 param.fp_pattern = (char *)optarg;
3433 param.fp_exclude_pattern = !!neg_opt;
3436 if (optarg[0] == '+') {
3437 param.fp_mirror_count_sign = -1;
3439 } else if (optarg[0] == '-') {
3440 param.fp_mirror_count_sign = 1;
3444 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3445 if (*endptr != '\0') {
3447 "error: bad mirror count '%s'\n",
3451 param.fp_check_mirror_count = 1;
3452 param.fp_exclude_mirror_count = !!neg_opt;
3457 char *buf, *token, *next, *p;
3461 buf = strdup(optarg);
3467 param.fp_exclude_obd = !!neg_opt;
3470 while (token && *token) {
3471 token = strchr(token, ',');
3478 param.fp_exclude_mdt = !!neg_opt;
3479 param.fp_num_alloc_mdts += len;
3480 tmp = realloc(param.fp_mdt_uuid,
3481 param.fp_num_alloc_mdts *
3482 sizeof(*param.fp_mdt_uuid));
3488 param.fp_mdt_uuid = tmp;
3490 param.fp_exclude_obd = !!neg_opt;
3491 param.fp_num_alloc_obds += len;
3492 tmp = realloc(param.fp_obd_uuid,
3493 param.fp_num_alloc_obds *
3494 sizeof(*param.fp_obd_uuid));
3500 param.fp_obd_uuid = tmp;
3502 for (token = buf; token && *token; token = next) {
3503 struct obd_uuid *puuid;
3506 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3509 ¶m.fp_obd_uuid[param.fp_num_obds++];
3511 p = strchr(token, ',');
3518 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3523 strncpy(puuid->uuid, token,
3524 sizeof(puuid->uuid));
3531 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3535 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3537 "Pool name %s is too long (max %d)\n",
3538 optarg, LOV_MAXPOOLNAME);
3543 * We do check for empty pool because empty pool
3544 * is used to find V1 LOV attributes
3546 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3547 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3548 param.fp_exclude_pool = !!neg_opt;
3549 param.fp_check_pool = 1;
3551 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3552 case 'p': /* want this for --pool, to match getstripe/find */
3554 "warning: -p deprecated, use --print0 or -0\n");
3557 param.fp_zero_end = 1;
3559 case 'P': /* we always print, this option is a no-op */
3561 case LFS_PROJID_OPT:
3562 rc = name2projid(¶m.fp_projid, optarg);
3564 param.fp_projid = strtoul(optarg, &endptr, 10);
3565 if (*endptr != '\0') {
3567 "Invalid project ID: %s",
3573 param.fp_exclude_projid = !!neg_opt;
3574 param.fp_check_projid = 1;
3577 if (optarg[0] == '+') {
3578 param.fp_size_sign = -1;
3580 } else if (optarg[0] == '-') {
3581 param.fp_size_sign = 1;
3585 ret = llapi_parse_size(optarg, ¶m.fp_size,
3586 ¶m.fp_size_units, 0);
3588 fprintf(stderr, "error: bad file size '%s'\n",
3592 param.fp_check_size = 1;
3593 param.fp_exclude_size = !!neg_opt;
3596 if (optarg[0] == '+') {
3597 param.fp_stripe_size_sign = -1;
3599 } else if (optarg[0] == '-') {
3600 param.fp_stripe_size_sign = 1;
3604 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3605 ¶m.fp_stripe_size_units, 0);
3607 fprintf(stderr, "error: bad stripe_size '%s'\n",
3611 param.fp_check_stripe_size = 1;
3612 param.fp_exclude_stripe_size = !!neg_opt;
3615 param.fp_exclude_type = !!neg_opt;
3616 switch (optarg[0]) {
3618 param.fp_type = S_IFBLK;
3621 param.fp_type = S_IFCHR;
3624 param.fp_type = S_IFDIR;
3627 param.fp_type = S_IFREG;
3630 param.fp_type = S_IFLNK;
3633 param.fp_type = S_IFIFO;
3636 param.fp_type = S_IFSOCK;
3639 fprintf(stderr, "error: %s: bad type '%s'\n",
3646 if (optarg[0] == '+') {
3647 param.fp_mdt_count_sign = -1;
3649 } else if (optarg[0] == '-') {
3650 param.fp_mdt_count_sign = 1;
3654 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3655 if (*endptr != '\0') {
3656 fprintf(stderr, "error: bad mdt_count '%s'\n",
3661 param.fp_check_mdt_count = 1;
3662 param.fp_exclude_mdt_count = !!neg_opt;
3670 if (pathstart == -1) {
3671 fprintf(stderr, "error: %s: no filename|pathname\n",
3675 } else if (pathend == -1) {
3681 rc = llapi_find(argv[pathstart], ¶m);
3682 if (rc != 0 && ret == 0)
3684 } while (++pathstart < pathend);
3687 fprintf(stderr, "error: %s failed for %s.\n",
3688 argv[0], argv[optind - 1]);
3690 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3691 free(param.fp_obd_uuid);
3693 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3694 free(param.fp_mdt_uuid);
3699 static int lfs_getstripe_internal(int argc, char **argv,
3700 struct find_param *param)
3702 struct option long_opts[] = {
3703 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3704 { .val = LFS_COMP_COUNT_OPT,
3705 .name = "comp-count", .has_arg = no_argument },
3706 { .val = LFS_COMP_COUNT_OPT,
3707 .name = "component-count", .has_arg = no_argument },
3708 { .val = LFS_COMP_FLAGS_OPT,
3709 .name = "comp-flags", .has_arg = optional_argument },
3710 { .val = LFS_COMP_FLAGS_OPT,
3711 .name = "component-flags", .has_arg = optional_argument },
3712 { .val = LFS_COMP_START_OPT,
3713 .name = "comp-start", .has_arg = optional_argument },
3714 { .val = LFS_COMP_START_OPT,
3715 .name = "component-start", .has_arg = optional_argument },
3716 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3717 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3718 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
3719 { .val = 'd', .name = "directory", .has_arg = no_argument },
3720 { .val = 'D', .name = "default", .has_arg = no_argument },
3721 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3722 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
3723 { .val = 'F', .name = "fid", .has_arg = no_argument },
3724 { .val = 'g', .name = "generation", .has_arg = no_argument },
3725 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3726 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
3727 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3728 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3729 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3730 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3731 { .val = 'L', .name = "layout", .has_arg = no_argument },
3732 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3733 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3734 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3735 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
3736 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
3737 { .val = 'O', .name = "obd", .has_arg = required_argument },
3738 { .val = 'O', .name = "ost", .has_arg = required_argument },
3739 { .val = 'p', .name = "pool", .has_arg = no_argument },
3740 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3741 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3742 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3743 { .val = 'R', .name = "raw", .has_arg = no_argument },
3744 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3745 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3746 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3747 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3748 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3749 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3750 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3751 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3756 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
3757 long_opts, NULL)) != -1) {
3760 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3761 param->fp_verbose |= VERBOSE_COUNT;
3762 param->fp_max_depth = 0;
3765 case LFS_COMP_COUNT_OPT:
3766 param->fp_verbose |= VERBOSE_COMP_COUNT;
3767 param->fp_max_depth = 0;
3769 case LFS_COMP_FLAGS_OPT:
3770 if (optarg != NULL) {
3771 rc = comp_str2flags(optarg,
3772 ¶m->fp_comp_flags,
3773 ¶m->fp_comp_neg_flags);
3775 fprintf(stderr, "error: %s bad "
3776 "component flags '%s'.\n",
3780 param->fp_check_comp_flags = 1;
3782 param->fp_verbose |= VERBOSE_COMP_FLAGS;
3783 param->fp_max_depth = 0;
3786 case LFS_COMP_START_OPT:
3787 if (optarg != NULL) {
3789 if (tmp[0] == '+') {
3790 param->fp_comp_start_sign = -1;
3792 } else if (tmp[0] == '-') {
3793 param->fp_comp_start_sign = 1;
3796 rc = llapi_parse_size(tmp,
3797 ¶m->fp_comp_start,
3798 ¶m->fp_comp_start_units, 0);
3800 fprintf(stderr, "error: %s bad "
3801 "component start '%s'.\n",
3805 param->fp_check_comp_start = 1;
3808 param->fp_verbose |= VERBOSE_COMP_START;
3809 param->fp_max_depth = 0;
3813 param->fp_max_depth = 0;
3816 param->fp_get_default_lmv = 1;
3819 if (optarg != NULL) {
3821 if (tmp[0] == '+') {
3822 param->fp_comp_end_sign = -1;
3824 } else if (tmp[0] == '-') {
3825 param->fp_comp_end_sign = 1;
3829 if (arg_is_eof(tmp)) {
3830 param->fp_comp_end = LUSTRE_EOF;
3831 param->fp_comp_end_units = 1;
3834 rc = llapi_parse_size(tmp,
3835 ¶m->fp_comp_end,
3836 ¶m->fp_comp_end_units, 0);
3839 fprintf(stderr, "error: %s bad "
3840 "component end '%s'.\n",
3844 param->fp_check_comp_end = 1;
3846 param->fp_verbose |= VERBOSE_COMP_END;
3847 param->fp_max_depth = 0;
3851 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3852 param->fp_verbose |= VERBOSE_DFID;
3853 param->fp_max_depth = 0;
3857 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3858 param->fp_verbose |= VERBOSE_GENERATION;
3859 param->fp_max_depth = 0;
3863 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3864 param->fp_verbose |= VERBOSE_OFFSET;
3865 param->fp_max_depth = 0;
3869 if (optarg != NULL) {
3870 param->fp_comp_id = strtoul(optarg, &end, 0);
3871 if (*end != '\0' || param->fp_comp_id == 0 ||
3872 param->fp_comp_id > LCME_ID_MAX) {
3873 fprintf(stderr, "error: %s bad "
3874 "component id '%s'\n",
3878 param->fp_check_comp_id = 1;
3881 param->fp_max_depth = 0;
3882 param->fp_verbose |= VERBOSE_COMP_ID;
3886 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3887 param->fp_verbose |= VERBOSE_LAYOUT;
3888 param->fp_max_depth = 0;
3891 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3893 fprintf(stderr, "warning: '-M' deprecated"
3894 ", use '--mdt-index' or '-m' instead\n");
3897 if (!(param->fp_verbose & VERBOSE_DETAIL))
3898 param->fp_max_depth = 0;
3899 param->fp_verbose |= VERBOSE_MDTINDEX;
3902 if (param->fp_obd_uuid) {
3904 "error: %s: only one obduuid allowed",
3908 param->fp_obd_uuid = (struct obd_uuid *)optarg;
3911 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3912 param->fp_verbose |= VERBOSE_POOL;
3913 param->fp_max_depth = 0;
3920 param->fp_recursive = 1;
3926 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3927 param->fp_verbose |= VERBOSE_SIZE;
3928 param->fp_max_depth = 0;
3932 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
3945 if (param->fp_recursive)
3946 param->fp_max_depth = -1;
3947 else if (param->fp_verbose & VERBOSE_DETAIL)
3948 param->fp_max_depth = 1;
3950 if (!param->fp_verbose)
3951 param->fp_verbose = VERBOSE_DEFAULT;
3952 if (param->fp_quiet)
3953 param->fp_verbose = VERBOSE_OBJID;
3956 rc = llapi_getstripe(argv[optind], param);
3957 } while (++optind < argc && !rc);
3960 fprintf(stderr, "error: %s failed for %s.\n",
3961 argv[0], argv[optind - 1]);
3965 static int lfs_tgts(int argc, char **argv)
3967 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3968 struct find_param param;
3969 int index = 0, rc=0;
3974 if (argc == 2 && !realpath(argv[1], path)) {
3976 fprintf(stderr, "error: invalid path '%s': %s\n",
3977 argv[1], strerror(-rc));
3981 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
3982 /* Check if we have a mount point */
3983 if (mntdir[0] == '\0')
3986 memset(¶m, 0, sizeof(param));
3987 if (!strcmp(argv[0], "mdts"))
3988 param.fp_get_lmv = 1;
3990 rc = llapi_ostlist(mntdir, ¶m);
3992 fprintf(stderr, "error: %s: failed on %s\n",
3995 if (path[0] != '\0')
3997 memset(mntdir, 0, PATH_MAX);
4003 static int lfs_getstripe(int argc, char **argv)
4005 struct find_param param = { 0 };
4007 param.fp_max_depth = 1;
4008 return lfs_getstripe_internal(argc, argv, ¶m);
4012 static int lfs_getdirstripe(int argc, char **argv)
4014 struct find_param param = { 0 };
4015 struct option long_opts[] = {
4016 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4017 { .val = 'D', .name = "default", .has_arg = no_argument },
4018 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4019 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4020 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4021 { .val = 'O', .name = "obd", .has_arg = required_argument },
4022 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4023 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4024 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4028 param.fp_get_lmv = 1;
4030 while ((c = getopt_long(argc, argv,
4031 "cDHimO:rtTy", long_opts, NULL)) != -1)
4036 param.fp_verbose |= VERBOSE_COUNT;
4039 param.fp_get_default_lmv = 1;
4043 param.fp_verbose |= VERBOSE_OFFSET;
4045 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4047 fprintf(stderr, "warning: '-t' deprecated, "
4048 "use '--mdt-hash' or '-H' instead\n");
4051 param.fp_verbose |= VERBOSE_HASH_TYPE;
4054 if (param.fp_obd_uuid) {
4056 "error: %s: only one obduuid allowed",
4060 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4063 param.fp_recursive = 1;
4076 if (param.fp_recursive)
4077 param.fp_max_depth = -1;
4079 if (!param.fp_verbose)
4080 param.fp_verbose = VERBOSE_DEFAULT;
4083 rc = llapi_getstripe(argv[optind], ¶m);
4084 } while (++optind < argc && !rc);
4087 fprintf(stderr, "error: %s failed for %s.\n",
4088 argv[0], argv[optind - 1]);
4093 MNTDF_INODES = 0x0001,
4094 MNTDF_COOKED = 0x0002,
4095 MNTDF_LAZY = 0x0004,
4096 MNTDF_VERBOSE = 0x0008,
4097 MNTDF_SHOW = 0x0010,
4100 #define COOK(value) \
4103 while (value > 1024) { \
4111 #define CDF "%11llu"
4112 #define HDF "%8.1f%c"
4116 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4118 double avail, used, ratio = 0;
4120 avail = st->os_bavail;
4121 used = st->os_blocks - st->os_bfree;
4122 if (avail + used > 0)
4123 ratio = used / (used + avail) * 100 + 0.5;
4128 static int showdf(char *mntdir, struct obd_statfs *stat,
4129 char *uuid, enum mntdf_flags flags,
4130 char *type, int index, int rc)
4132 long long avail, used, total;
4134 char *suffix = "KMGTPEZY";
4135 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4136 char tbuf[3 * sizeof(__u64)];
4137 char ubuf[3 * sizeof(__u64)];
4138 char abuf[3 * sizeof(__u64)];
4139 char rbuf[3 * sizeof(__u64)];
4146 if (flags & MNTDF_INODES) {
4147 avail = stat->os_ffree;
4148 used = stat->os_files - stat->os_ffree;
4149 total = stat->os_files;
4151 int shift = flags & MNTDF_COOKED ? 0 : 10;
4153 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4154 used = ((stat->os_blocks - stat->os_bfree) *
4155 stat->os_bsize) >> shift;
4156 total = (stat->os_blocks * stat->os_bsize) >> shift;
4159 ratio = obd_statfs_ratio(stat);
4161 if (flags & MNTDF_COOKED) {
4165 cook_val = (double)total;
4168 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4171 snprintf(tbuf, sizeof(tbuf), CDF, total);
4173 cook_val = (double)used;
4176 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4179 snprintf(ubuf, sizeof(ubuf), CDF, used);
4181 cook_val = (double)avail;
4184 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4187 snprintf(abuf, sizeof(abuf), CDF, avail);
4189 snprintf(tbuf, sizeof(tbuf), CDF, total);
4190 snprintf(ubuf, sizeof(tbuf), CDF, used);
4191 snprintf(abuf, sizeof(tbuf), CDF, avail);
4194 sprintf(rbuf, RDF, ratio);
4195 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4196 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4198 printf("[%s:%d]", type, index);
4200 if (stat->os_state) {
4202 * Each character represents the matching
4205 const char state_names[] = "DRSI";
4210 for (i = 0, state = stat->os_state;
4211 state && i < sizeof(state_names); i++) {
4212 if (!(state & (1 << i)))
4214 printf("%c", state_names[i]);
4222 printf(UUF": inactive device\n", uuid);
4225 printf(UUF": %s\n", uuid, strerror(-rc));
4232 struct ll_stat_type {
4237 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4239 struct ll_statfs_data {
4241 struct obd_statfs sd_st;
4244 struct ll_statfs_buf {
4246 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4249 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4250 int ops, struct ll_statfs_buf *lsb)
4252 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4253 struct obd_uuid uuid_buf;
4254 char *poolname = NULL;
4255 struct ll_stat_type types[] = {
4256 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4257 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4258 { .st_name = NULL } };
4259 struct ll_stat_type *tp;
4260 __u64 ost_ffree = 0;
4268 poolname = strchr(pool, '.');
4269 if (poolname != NULL) {
4270 if (strncmp(fsname, pool, strlen(fsname))) {
4271 fprintf(stderr, "filesystem name incorrect\n");
4279 fd = open(mntdir, O_RDONLY);
4282 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4287 if (flags & MNTDF_SHOW) {
4288 if (flags & MNTDF_INODES)
4289 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4290 "UUID", "Inodes", "IUsed", "IFree",
4291 "IUse%", "Mounted on");
4293 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4295 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4296 "Used", "Available", "Use%", "Mounted on");
4299 for (tp = types; tp->st_name != NULL; tp++) {
4300 if (!(tp->st_op & ops))
4303 for (index = 0; ; index++) {
4304 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4305 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4306 type = flags & MNTDF_LAZY ?
4307 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4308 rc2 = llapi_obd_fstatfs(fd, type, index,
4309 &stat_buf, &uuid_buf);
4314 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4315 if (!(flags & MNTDF_VERBOSE))
4317 } else if (rc2 < 0 && rc == 0) {
4321 if (poolname && tp->st_op == LL_STATFS_LOV &&
4322 llapi_search_ost(fsname, poolname,
4323 obd_uuid2str(&uuid_buf)) != 1)
4326 /* the llapi_obd_statfs() call may have returned with
4327 * an error, but if it filled in uuid_buf we will at
4328 * lease use that to print out a message for that OBD.
4329 * If we didn't get anything in the uuid_buf, then fill
4330 * it in so that we can print an error message. */
4331 if (uuid_buf.uuid[0] == '\0')
4332 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4333 "%s%04x", tp->st_name, index);
4335 lsb->sb_buf[lsb->sb_count].sd_index = index;
4336 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4339 if (flags & MNTDF_SHOW)
4340 showdf(mntdir, &stat_buf,
4341 obd_uuid2str(&uuid_buf), flags,
4342 tp->st_name, index, rc2);
4345 if (tp->st_op == LL_STATFS_LMV) {
4346 sum.os_ffree += stat_buf.os_ffree;
4347 sum.os_files += stat_buf.os_files;
4348 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4349 sum.os_blocks += stat_buf.os_blocks *
4351 sum.os_bfree += stat_buf.os_bfree *
4353 sum.os_bavail += stat_buf.os_bavail *
4355 ost_ffree += stat_buf.os_ffree;
4363 /* If we don't have as many objects free on the OST as inodes
4364 * on the MDS, we reduce the total number of inodes to
4365 * compensate, so that the "inodes in use" number is correct.
4366 * Matches ll_statfs_internal() so the results are consistent. */
4367 if (ost_ffree < sum.os_ffree) {
4368 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4369 sum.os_ffree = ost_ffree;
4371 if (flags & MNTDF_SHOW) {
4373 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4380 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4382 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4384 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4386 int r1 = obd_statfs_ratio(st1);
4387 int r2 = obd_statfs_ratio(st2);
4388 int64_t result = r1 - r2;
4390 /* if both space usage are above 90, compare free inodes */
4391 if (r1 > 90 && r2 > 90)
4392 result = st2->os_ffree - st1->os_ffree;
4396 else if (result == 0)
4403 static int lfs_setdirstripe(int argc, char **argv)
4407 struct lfs_setstripe_args lsa;
4408 struct llapi_stripe_param *param = NULL;
4409 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4412 char *mode_opt = NULL;
4413 bool default_stripe = false;
4414 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4415 mode_t previous_mode = 0;
4416 bool delete = false;
4417 struct ll_statfs_buf *lsb = NULL;
4418 char mntdir[PATH_MAX] = "";
4419 bool auto_distributed = false;
4421 struct option long_opts[] = {
4422 { .val = 'c', .name = "count", .has_arg = required_argument },
4423 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4424 { .val = 'd', .name = "delete", .has_arg = no_argument },
4425 { .val = 'D', .name = "default", .has_arg = no_argument },
4426 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4427 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4428 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4429 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4430 { .val = 'i', .name = "index", .has_arg = required_argument },
4432 { .val = 'o', .name = "mode", .has_arg = required_argument },
4433 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4434 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4436 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4439 setstripe_args_init(&lsa);
4441 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4449 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4452 "%s %s: invalid stripe count '%s'\n",
4453 progname, argv[0], optarg);
4459 default_stripe = true;
4462 default_stripe = true;
4464 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4466 fprintf(stderr, "warning: '--hash-type' and '-t' "
4467 "deprecated, use '--mdt-hash' or '-H' instead\n");
4470 lsa.lsa_pattern = check_hashtype(optarg);
4471 if (lsa.lsa_pattern == 0) {
4473 "%s %s: bad stripe hash type '%s'\n",
4474 progname, argv[0], optarg);
4479 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4480 if (strcmp(argv[optind - 1], "--index") == 0)
4482 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4485 lsa.lsa_nr_tgts = parse_targets(mdts,
4486 sizeof(mdts) / sizeof(__u32),
4487 lsa.lsa_nr_tgts, optarg);
4488 if (lsa.lsa_nr_tgts < 0) {
4490 "%s %s: invalid MDT target(s) '%s'\n",
4491 progname, argv[0], optarg);
4495 lsa.lsa_tgts = mdts;
4496 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4497 lsa.lsa_stripe_off = mdts[0];
4499 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4501 fprintf(stderr, "warning: '-m' is deprecated, "
4502 "use '--mode' or '-o' instead\n");
4508 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4509 progname, argv[0], argv[optind - 1]);
4514 if (optind == argc) {
4515 fprintf(stderr, "%s %s: DIR must be specified\n",
4520 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
4521 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
4523 "%s %s: stripe offset and count must be specified\n",
4529 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
4530 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
4532 "%s %s: cannot specify -d with -c or -i options\n",
4537 if (mode_opt != NULL) {
4538 mode = strtoul(mode_opt, &end, 8);
4541 "%s %s: bad MODE '%s'\n",
4542 progname, argv[0], mode_opt);
4545 previous_mode = umask(0);
4549 * initialize stripe parameters, in case param is converted to specific,
4550 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
4552 param = calloc(1, offsetof(typeof(*param),
4553 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
4554 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
4555 if (param == NULL) {
4557 "%s %s: cannot allocate memory for parameters: %s\n",
4558 progname, argv[0], strerror(ENOMEM));
4562 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4563 param->lsp_stripe_count = lsa.lsa_stripe_count;
4564 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4565 param->lsp_stripe_offset = -1;
4567 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4568 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4569 param->lsp_stripe_pattern = lsa.lsa_pattern;
4571 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
4572 param->lsp_pool = lsa.lsa_pool_name;
4573 param->lsp_is_specific = false;
4574 if (lsa.lsa_nr_tgts > 1) {
4575 if (lsa.lsa_stripe_count > 0 &&
4576 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4577 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4578 fprintf(stderr, "error: %s: stripe count %lld doesn't "
4579 "match the number of MDTs: %d\n",
4580 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
4585 param->lsp_is_specific = true;
4586 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4587 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
4590 dname = argv[optind];
4592 if (default_stripe) {
4593 result = llapi_dir_set_default_lmv(dname, param);
4595 /* if current \a dname isn't under the same \a mntdir
4596 * as the last one, and the last one was
4597 * auto-distributed, restore \a param.
4599 if (mntdir[0] != '\0' &&
4600 strncmp(dname, mntdir, strlen(mntdir)) &&
4602 param->lsp_is_specific = false;
4603 param->lsp_stripe_offset = -1;
4604 auto_distributed = false;
4607 if (!param->lsp_is_specific &&
4608 param->lsp_stripe_offset == -1) {
4609 char path[PATH_MAX] = "";
4612 lsb = malloc(sizeof(*lsb));
4620 /* use mntdir for dirname() temporarily */
4621 strncpy(mntdir, dname, sizeof(mntdir));
4622 if (!realpath(dirname(mntdir), path)) {
4625 "error: invalid path '%s': %s\n",
4626 argv[optind], strerror(errno));
4631 result = llapi_search_mounts(path, 0, mntdir,
4633 if (result < 0 || mntdir[0] == '\0') {
4635 "No suitable Lustre mount found\n");
4639 result = mntdf(mntdir, NULL, NULL, 0,
4640 LL_STATFS_LMV, lsb);
4644 if (param->lsp_stripe_count > lsb->sb_count) {
4646 "error: stripe count %d is too big\n",
4647 param->lsp_stripe_count);
4652 qsort(lsb->sb_buf, lsb->sb_count,
4653 sizeof(struct ll_statfs_data),
4654 ll_statfs_data_comp);
4656 auto_distributed = true;
4659 if (auto_distributed) {
4661 int nr = MAX(param->lsp_stripe_count,
4664 /* don't use server whose usage is above 90% */
4665 while (nr != param->lsp_stripe_count &&
4666 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
4668 nr = MAX(param->lsp_stripe_count,
4671 /* get \a r between [0, nr) */
4674 param->lsp_stripe_offset =
4675 lsb->sb_buf[r].sd_index;
4676 if (param->lsp_stripe_count > 1) {
4679 param->lsp_is_specific = true;
4680 for (; i < param->lsp_stripe_count; i++)
4681 param->lsp_tgts[(i + r) % nr] =
4682 lsb->sb_buf[i].sd_index;
4686 result = llapi_dir_create(dname, mode, param);
4691 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
4692 progname, dname, strerror(-result));
4695 dname = argv[++optind];
4696 } while (dname != NULL);
4698 if (mode_opt != NULL)
4699 umask(previous_mode);
4707 static int lfs_rmentry(int argc, char **argv)
4714 fprintf(stderr, "error: %s: missing dirname\n",
4720 dname = argv[index];
4721 while (dname != NULL) {
4722 result = llapi_direntry_remove(dname);
4724 fprintf(stderr, "error: %s: remove dir entry '%s' "
4725 "failed\n", argv[0], dname);
4728 dname = argv[++index];
4733 static int lfs_mv(int argc, char **argv)
4735 struct find_param param = {
4742 struct option long_opts[] = {
4743 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4744 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4747 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
4749 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4751 fprintf(stderr, "warning: '-M' deprecated"
4752 ", use '--mdt-index' or '-m' instead\n");
4755 param.fp_mdt_index = strtoul(optarg, &end, 0);
4757 fprintf(stderr, "%s: invalid MDT index'%s'\n",
4763 param.fp_verbose = VERBOSE_DETAIL;
4766 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
4767 argv[0], argv[optind - 1]);
4772 if (param.fp_mdt_index == -1) {
4773 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
4777 if (optind >= argc) {
4778 fprintf(stderr, "%s: missing operand path\n", argv[0]);
4782 param.fp_migrate = 1;
4783 rc = llapi_migrate_mdt(argv[optind], ¶m);
4785 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
4786 argv[0], argv[optind], param.fp_mdt_index,
4791 static int lfs_osts(int argc, char **argv)
4793 return lfs_tgts(argc, argv);
4796 static int lfs_mdts(int argc, char **argv)
4798 return lfs_tgts(argc, argv);
4801 static int lfs_df(int argc, char **argv)
4803 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4804 enum mntdf_flags flags = MNTDF_SHOW;
4805 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
4806 int c, rc = 0, index = 0;
4807 char fsname[PATH_MAX] = "", *pool_name = NULL;
4808 struct option long_opts[] = {
4809 { .val = 'h', .name = "human-readable",
4810 .has_arg = no_argument },
4811 { .val = 'i', .name = "inodes", .has_arg = no_argument },
4812 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4813 { .val = 'p', .name = "pool", .has_arg = required_argument },
4814 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4817 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
4820 flags |= MNTDF_COOKED;
4823 flags |= MNTDF_INODES;
4826 flags |= MNTDF_LAZY;
4832 flags |= MNTDF_VERBOSE;
4838 if (optind < argc && !realpath(argv[optind], path)) {
4840 fprintf(stderr, "error: invalid path '%s': %s\n",
4841 argv[optind], strerror(-rc));
4845 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4846 /* Check if we have a mount point */
4847 if (mntdir[0] == '\0')
4850 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
4851 if (rc || path[0] != '\0')
4853 fsname[0] = '\0'; /* avoid matching in next loop */
4854 mntdir[0] = '\0'; /* avoid matching in next loop */
4860 static int lfs_getname(int argc, char **argv)
4862 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
4863 int rc = 0, index = 0, c;
4864 char buf[sizeof(struct obd_uuid)];
4866 while ((c = getopt(argc, argv, "h")) != -1)
4869 if (optind == argc) { /* no paths specified, get all paths. */
4870 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4871 rc = llapi_getname(mntdir, buf, sizeof(buf));
4874 "cannot get name for `%s': %s\n",
4875 mntdir, strerror(-rc));
4879 printf("%s %s\n", buf, mntdir);
4881 path[0] = fsname[0] = mntdir[0] = 0;
4883 } else { /* paths specified, only attempt to search these. */
4884 for (; optind < argc; optind++) {
4885 rc = llapi_getname(argv[optind], buf, sizeof(buf));
4888 "cannot get name for `%s': %s\n",
4889 argv[optind], strerror(-rc));
4893 printf("%s %s\n", buf, argv[optind]);
4899 static int lfs_check(int argc, char **argv)
4902 char mntdir[PATH_MAX] = {'\0'};
4909 fprintf(stderr, "%s check: server type must be specified\n",
4914 obd_types[0] = obd_type1;
4915 obd_types[1] = obd_type2;
4917 if (strcmp(argv[1], "osts") == 0) {
4918 strcpy(obd_types[0], "osc");
4919 } else if (strcmp(argv[1], "mds") == 0) {
4920 strcpy(obd_types[0], "mdc");
4921 } else if (strcmp(argv[1], "servers") == 0) {
4923 strcpy(obd_types[0], "osc");
4924 strcpy(obd_types[1], "mdc");
4926 fprintf(stderr, "%s check: unrecognized option '%s'\n",
4931 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
4932 if (rc < 0 || mntdir[0] == '\0') {
4934 "%s check: cannot find mounted Lustre filesystem: %s\n",
4935 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
4939 rc = llapi_target_check(num_types, obd_types, mntdir);
4941 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
4942 progname, argv[1], strerror(-rc));
4948 #ifdef HAVE_SYS_QUOTA_H
4949 #define ARG2INT(nr, str, msg) \
4952 nr = strtol(str, &endp, 0); \
4953 if (*endp != '\0') { \
4954 fprintf(stderr, "%s: bad %s '%s'\n", \
4955 progname, msg, str); \
4960 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
4962 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
4963 * returns the value or ULONG_MAX on integer overflow or incorrect format
4965 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
4966 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
4967 * 3. empty integer value is interpreted as 0
4969 static unsigned long str2sec(const char* timestr)
4971 const char spec[] = "smhdw";
4972 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4973 unsigned long val = 0;
4976 if (strpbrk(timestr, spec) == NULL) {
4977 /* no specifiers inside the time string,
4978 should treat it as an integer value */
4979 val = strtoul(timestr, &tail, 10);
4980 return *tail ? ULONG_MAX : val;
4983 /* format string is XXwXXdXXhXXmXXs */
4989 v = strtoul(timestr, &tail, 10);
4990 if (v == ULONG_MAX || *tail == '\0')
4991 /* value too large (ULONG_MAX or more)
4992 or missing specifier */
4995 ptr = strchr(spec, *tail);
4997 /* unknown specifier */
5002 /* check if product will overflow the type */
5003 if (!(v < ULONG_MAX / mult[ind]))
5006 ADD_OVERFLOW(val, mult[ind] * v);
5007 if (val == ULONG_MAX)
5019 #define ARG2ULL(nr, str, def_units) \
5021 unsigned long long limit, units = def_units; \
5024 rc = llapi_parse_size(str, &limit, &units, 1); \
5026 fprintf(stderr, "%s: invalid limit '%s'\n", \
5033 static inline int has_times_option(int argc, char **argv)
5037 for (i = 1; i < argc; i++)
5038 if (!strcmp(argv[i], "-t"))
5044 int lfs_setquota_times(int argc, char **argv)
5047 struct if_quotactl qctl;
5048 char *mnt, *obd_type = (char *)qctl.obd_type;
5049 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5050 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5051 struct option long_opts[] = {
5052 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5053 { .val = 'g', .name = "group", .has_arg = no_argument },
5054 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5055 { .val = 'p', .name = "projid", .has_arg = no_argument },
5056 { .val = 't', .name = "times", .has_arg = no_argument },
5057 { .val = 'u', .name = "user", .has_arg = no_argument },
5061 memset(&qctl, 0, sizeof(qctl));
5062 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5063 qctl.qc_type = ALLQUOTA;
5065 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5066 long_opts, NULL)) != -1) {
5077 if (qctl.qc_type != ALLQUOTA) {
5078 fprintf(stderr, "error: -u/g/p can't be used "
5079 "more than once\n");
5082 qctl.qc_type = qtype;
5085 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5086 fprintf(stderr, "error: bad block-grace: %s\n",
5090 dqb->dqb_valid |= QIF_BTIME;
5093 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5094 fprintf(stderr, "error: bad inode-grace: %s\n",
5098 dqb->dqb_valid |= QIF_ITIME;
5100 case 't': /* Yes, of course! */
5102 default: /* getopt prints error message for us when opterr != 0 */
5107 if (qctl.qc_type == ALLQUOTA) {
5108 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5112 if (optind != argc - 1) {
5113 fprintf(stderr, "error: unexpected parameters encountered\n");
5118 rc = llapi_quotactl(mnt, &qctl);
5121 fprintf(stderr, "%s %s ", obd_type,
5122 obd_uuid2str(&qctl.obd_uuid));
5123 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5130 #define BSLIMIT (1 << 0)
5131 #define BHLIMIT (1 << 1)
5132 #define ISLIMIT (1 << 2)
5133 #define IHLIMIT (1 << 3)
5135 int lfs_setquota(int argc, char **argv)
5138 struct if_quotactl qctl;
5139 char *mnt, *obd_type = (char *)qctl.obd_type;
5140 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5141 struct option long_opts[] = {
5142 { .val = 'b', .name = "block-softlimit",
5143 .has_arg = required_argument },
5144 { .val = 'B', .name = "block-hardlimit",
5145 .has_arg = required_argument },
5146 { .val = 'g', .name = "group", .has_arg = required_argument },
5147 { .val = 'i', .name = "inode-softlimit",
5148 .has_arg = required_argument },
5149 { .val = 'I', .name = "inode-hardlimit",
5150 .has_arg = required_argument },
5151 { .val = 'p', .name = "projid", .has_arg = required_argument },
5152 { .val = 'u', .name = "user", .has_arg = required_argument },
5154 unsigned limit_mask = 0;
5158 if (has_times_option(argc, argv))
5159 return lfs_setquota_times(argc, argv);
5161 memset(&qctl, 0, sizeof(qctl));
5162 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5163 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5164 * so it can be used as a marker that qc_type
5165 * isn't reinitialized from command line */
5167 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
5168 long_opts, NULL)) != -1) {
5172 rc = name2uid(&qctl.qc_id, optarg);
5176 rc = name2gid(&qctl.qc_id, optarg);
5180 rc = name2projid(&qctl.qc_id, optarg);
5182 if (qctl.qc_type != ALLQUOTA) {
5184 "%s setquota: only one of -u, -g or -p may be specified\n",
5188 qctl.qc_type = qtype;
5190 qctl.qc_id = strtoul(optarg, &endptr, 10);
5191 if (*endptr != '\0') {
5193 "%s setquota: invalid id '%s'\n",
5200 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5201 dqb->dqb_bsoftlimit >>= 10;
5202 limit_mask |= BSLIMIT;
5203 if (dqb->dqb_bsoftlimit &&
5204 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5206 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5207 "See '%s help setquota' or Lustre manual for details\n",
5208 progname, dqb->dqb_bsoftlimit,
5212 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5213 dqb->dqb_bhardlimit >>= 10;
5214 limit_mask |= BHLIMIT;
5215 if (dqb->dqb_bhardlimit &&
5216 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5218 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5219 "See '%s help setquota' or Lustre manual for details\n",
5220 progname, dqb->dqb_bhardlimit,
5224 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5225 limit_mask |= ISLIMIT;
5226 if (dqb->dqb_isoftlimit &&
5227 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5229 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5230 "See '%s help setquota' or Lustre manual for details\n",
5231 progname, dqb->dqb_isoftlimit,
5235 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5236 limit_mask |= IHLIMIT;
5237 if (dqb->dqb_ihardlimit &&
5238 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5240 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5241 "See '%s help setquota' or Lustre manual for details\n",
5242 progname, dqb->dqb_ihardlimit,
5247 "%s setquota: unrecognized option '%s'\n",
5248 progname, argv[optind - 1]);
5253 if (qctl.qc_type == ALLQUOTA) {
5255 "%s setquota: either -u or -g must be specified\n",
5260 if (limit_mask == 0) {
5262 "%s setquota: at least one limit must be specified\n",
5267 if (optind != argc - 1) {
5269 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5270 progname, argv[optind]);
5276 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5277 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5278 /* sigh, we can't just set blimits/ilimits */
5279 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5280 .qc_type = qctl.qc_type,
5281 .qc_id = qctl.qc_id};
5283 rc = llapi_quotactl(mnt, &tmp_qctl);
5287 if (!(limit_mask & BHLIMIT))
5288 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5289 if (!(limit_mask & BSLIMIT))
5290 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5291 if (!(limit_mask & IHLIMIT))
5292 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5293 if (!(limit_mask & ISLIMIT))
5294 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5296 /* Keep grace times if we have got no softlimit arguments */
5297 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5298 dqb->dqb_valid |= QIF_BTIME;
5299 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5302 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5303 dqb->dqb_valid |= QIF_ITIME;
5304 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5308 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5309 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5311 rc = llapi_quotactl(mnt, &qctl);
5315 "%s setquota: cannot quotactl '%s' '%s': %s",
5317 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5324 /* Converts seconds value into format string
5325 * result is returned in buf
5327 * 1. result is in descenting order: 1w2d3h4m5s
5328 * 2. zero fields are not filled (except for p. 3): 5d1s
5329 * 3. zero seconds value is presented as "0s"
5331 static char * __sec2str(time_t seconds, char *buf)
5333 const char spec[] = "smhdw";
5334 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5339 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5340 c = seconds / mult[i];
5342 if (c > 0 || (i == 0 && buf == tail))
5343 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5351 static void sec2str(time_t seconds, char *buf, int rc)
5358 tail = __sec2str(seconds, tail);
5360 if (rc && tail - buf < 39) {
5366 static void diff2str(time_t seconds, char *buf, time_t now)
5372 if (seconds <= now) {
5373 strcpy(buf, "none");
5376 __sec2str(seconds - now, buf);
5379 static void print_quota_title(char *name, struct if_quotactl *qctl,
5380 bool human_readable)
5382 printf("Disk quotas for %s %s (%cid %u):\n",
5383 qtype_name(qctl->qc_type), name,
5384 *qtype_name(qctl->qc_type), qctl->qc_id);
5385 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5386 "Filesystem", human_readable ? "used" : "kbytes",
5387 "quota", "limit", "grace",
5388 "files", "quota", "limit", "grace");
5391 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5394 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5397 snprintf(buf, buflen, "%5.4gP",
5398 (double)num / ((__u64)1 << 40));
5400 snprintf(buf, buflen, "%5.4gT",
5401 (double)num / (1 << 30));
5403 snprintf(buf, buflen, "%5.4gG",
5404 (double)num / (1 << 20));
5406 snprintf(buf, buflen, "%5.4gM",
5407 (double)num / (1 << 10));
5409 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5413 #define STRBUF_LEN 32
5414 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5421 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
5422 int bover = 0, iover = 0;
5423 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5424 char numbuf[3][STRBUF_LEN];
5426 char strbuf[STRBUF_LEN];
5428 if (dqb->dqb_bhardlimit &&
5429 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5431 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5432 if (dqb->dqb_btime > now) {
5439 if (dqb->dqb_ihardlimit &&
5440 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5442 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5443 if (dqb->dqb_itime > now) {
5451 if (strlen(mnt) > 15)
5452 printf("%s\n%15s", mnt, "");
5454 printf("%15s", mnt);
5457 diff2str(dqb->dqb_btime, timebuf, now);
5459 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5460 strbuf, sizeof(strbuf), h);
5461 if (rc == -EREMOTEIO)
5462 sprintf(numbuf[0], "%s*", strbuf);
5464 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5465 "%s" : "[%s]", strbuf);
5467 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5468 if (type == QC_GENERAL)
5469 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5470 "%s" : "[%s]", strbuf);
5472 sprintf(numbuf[1], "%s", "-");
5474 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5475 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5476 "%s" : "[%s]", strbuf);
5478 printf(" %7s%c %6s %7s %7s",
5479 numbuf[0], bover ? '*' : ' ', numbuf[1],
5480 numbuf[2], bover > 1 ? timebuf : "-");
5483 diff2str(dqb->dqb_itime, timebuf, now);
5485 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
5486 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
5488 if (type == QC_GENERAL)
5489 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5491 (uintmax_t)dqb->dqb_isoftlimit);
5493 sprintf(numbuf[1], "%s", "-");
5495 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5496 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5498 if (type != QC_OSTIDX)
5499 printf(" %7s%c %6s %7s %7s",
5500 numbuf[0], iover ? '*' : ' ', numbuf[1],
5501 numbuf[2], iover > 1 ? timebuf : "-");
5503 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5506 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5507 qctl->qc_cmd == Q_GETOINFO) {
5511 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5512 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5513 printf("Block grace time: %s; Inode grace time: %s\n",
5514 bgtimebuf, igtimebuf);
5518 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5519 bool h, __u64 *total)
5521 int rc = 0, rc1 = 0, count = 0;
5522 __u32 valid = qctl->qc_valid;
5524 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5526 fprintf(stderr, "can not get %s count: %s\n",
5527 is_mdt ? "mdt": "ost", strerror(-rc));
5531 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5532 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5533 rc = llapi_quotactl(mnt, qctl);
5535 /* It is remote client case. */
5536 if (rc == -EOPNOTSUPP) {
5543 fprintf(stderr, "quotactl %s%d failed.\n",
5544 is_mdt ? "mdt": "ost", qctl->qc_idx);
5548 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5549 qctl->qc_valid, 0, h);
5550 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5551 qctl->qc_dqblk.dqb_bhardlimit;
5554 qctl->qc_valid = valid;
5558 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5559 int verbose, int quiet, bool human_readable)
5561 int rc1 = 0, rc2 = 0, rc3 = 0;
5562 char *obd_type = (char *)qctl->obd_type;
5563 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5564 __u64 total_ialloc = 0, total_balloc = 0;
5567 rc1 = llapi_quotactl(mnt, qctl);
5571 fprintf(stderr, "%s quotas are not enabled.\n",
5572 qtype_name(qctl->qc_type));
5575 fprintf(stderr, "Permission denied.\n");
5578 /* We already got error message. */
5581 fprintf(stderr, "Unexpected quotactl error: %s\n",
5586 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
5587 print_quota_title(name, qctl, human_readable);
5589 if (rc1 && *obd_type)
5590 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5592 if (qctl->qc_valid != QC_GENERAL)
5595 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5596 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5597 (QIF_LIMITS|QIF_USAGE));
5599 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
5601 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
5603 char strbuf[STRBUF_LEN];
5605 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5607 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5609 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5611 printf("Total allocated inode limit: %ju, total "
5612 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5616 if (rc1 || rc2 || rc3 || inacc)
5617 printf("Some errors happened when getting quota info. "
5618 "Some devices may be not working or deactivated. "
5619 "The data in \"[]\" is inaccurate.\n");
5625 static int lfs_project(int argc, char **argv)
5627 int ret = 0, err = 0, c, i;
5628 struct project_handle_control phc = { 0 };
5629 enum lfs_project_ops_t op;
5632 phc.assign_projid = false;
5633 /* default action */
5634 op = LFS_PROJECT_LIST;
5636 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
5639 if (op != LFS_PROJECT_LIST) {
5641 "%s: cannot specify '-c' '-C' '-s' together\n",
5646 op = LFS_PROJECT_CHECK;
5649 if (op != LFS_PROJECT_LIST) {
5651 "%s: cannot specify '-c' '-C' '-s' together\n",
5656 op = LFS_PROJECT_CLEAR;
5659 if (op != LFS_PROJECT_LIST) {
5661 "%s: cannot specify '-c' '-C' '-s' together\n",
5666 phc.set_inherit = true;
5667 op = LFS_PROJECT_SET;
5673 phc.keep_projid = true;
5676 phc.recursive = true;
5679 phc.projid = strtoul(optarg, NULL, 0);
5680 phc.assign_projid = true;
5684 phc.newline = false;
5687 fprintf(stderr, "%s: invalid option '%c'\n",
5693 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
5694 op = LFS_PROJECT_SET;
5695 phc.set_projid = true;
5696 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
5697 phc.set_projid = true;
5701 case LFS_PROJECT_CHECK:
5702 if (phc.keep_projid) {
5704 "%s: '-k' is useless together with '-c'\n",
5709 case LFS_PROJECT_CLEAR:
5712 "%s: '-0' is useless together with '-C'\n",
5716 if (phc.assign_projid) {
5718 "%s: '-p' is useless together with '-C'\n",
5723 case LFS_PROJECT_SET:
5726 "%s: '-0' is useless together with '-s'\n",
5730 if (phc.keep_projid) {
5732 "%s: '-k' is useless together with '-s'\n",
5740 "%s: '-0' is useless for list operations\n",
5750 fprintf(stderr, "%s: missing file or directory target(s)\n",
5755 for (i = 0; i < argc; i++) {
5757 case LFS_PROJECT_CHECK:
5758 err = lfs_project_check(argv[i], &phc);
5760 case LFS_PROJECT_LIST:
5761 err = lfs_project_list(argv[i], &phc);
5763 case LFS_PROJECT_CLEAR:
5764 err = lfs_project_clear(argv[i], &phc);
5766 case LFS_PROJECT_SET:
5767 err = lfs_project_set(argv[i], &phc);
5779 static int lfs_quota(int argc, char **argv)
5782 char *mnt, *name = NULL;
5783 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
5784 .qc_type = ALLQUOTA };
5785 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
5786 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
5788 __u32 valid = QC_GENERAL, idx = 0;
5789 bool human_readable = false;
5792 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
5803 if (qctl.qc_type != ALLQUOTA) {
5805 "%s quota: only one of -u, -g, or -p may be specified\n",
5809 qctl.qc_type = qtype;
5812 qctl.qc_cmd = LUSTRE_Q_GETINFO;
5815 valid = qctl.qc_valid = QC_UUID;
5816 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
5819 valid = qctl.qc_valid = QC_MDTIDX;
5820 idx = qctl.qc_idx = atoi(optarg);
5821 if (idx == 0 && *optarg != '0') {
5823 "%s quota: invalid MDT index '%s'\n",
5829 valid = qctl.qc_valid = QC_OSTIDX;
5830 idx = qctl.qc_idx = atoi(optarg);
5831 if (idx == 0 && *optarg != '0') {
5833 "%s quota: invalid OST index '%s'\n",
5845 human_readable = true;
5848 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
5849 progname, argv[optind - 1]);
5854 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
5855 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
5856 optind == argc - 1) {
5858 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
5859 qctl.qc_valid = valid;
5862 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
5863 qctl.qc_type = qtype;
5864 if (qtype == USRQUOTA) {
5865 qctl.qc_id = geteuid();
5866 rc = uid2name(&name, qctl.qc_id);
5868 qctl.qc_id = getegid();
5869 rc = gid2name(&name, qctl.qc_id);
5874 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
5879 /* lfs quota -u username /path/to/lustre/mount */
5880 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
5881 /* options should be followed by u/g-name and mntpoint */
5882 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
5884 "%s quota: name and mount point must be specified\n",
5889 name = argv[optind++];
5890 switch (qctl.qc_type) {
5892 rc = name2uid(&qctl.qc_id, name);
5895 rc = name2gid(&qctl.qc_id, name);
5898 rc = name2projid(&qctl.qc_id, name);
5905 qctl.qc_id = strtoul(name, &endptr, 10);
5906 if (*endptr != '\0') {
5907 fprintf(stderr, "%s quota: invalid id '%s'\n",
5913 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
5915 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
5916 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
5923 #endif /* HAVE_SYS_QUOTA_H! */
5925 static int flushctx_ioctl(char *mp)
5929 fd = open(mp, O_RDONLY);
5931 fprintf(stderr, "flushctx: error open %s: %s\n",
5932 mp, strerror(errno));
5936 rc = ioctl(fd, LL_IOC_FLUSHCTX);
5938 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
5939 mp, strerror(errno));
5945 static int lfs_flushctx(int argc, char **argv)
5947 int kdestroy = 0, c;
5948 char mntdir[PATH_MAX] = {'\0'};
5952 while ((c = getopt(argc, argv, "k")) != -1) {
5958 fprintf(stderr, "error: %s: option '-%c' "
5959 "unrecognized\n", argv[0], c);
5965 if ((rc = system("kdestroy > /dev/null")) != 0) {
5966 rc = WEXITSTATUS(rc);
5967 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
5971 if (optind >= argc) {
5972 /* flush for all mounted lustre fs. */
5973 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
5974 /* Check if we have a mount point */
5975 if (mntdir[0] == '\0')
5978 if (flushctx_ioctl(mntdir))
5981 mntdir[0] = '\0'; /* avoid matching in next loop */
5984 /* flush fs as specified */
5985 while (optind < argc) {
5986 if (flushctx_ioctl(argv[optind++]))
5993 static int lfs_cp(int argc, char **argv)
5995 fprintf(stderr, "remote client copy file(s).\n"
5996 "obsolete, does not support it anymore.\n");
6000 static int lfs_ls(int argc, char **argv)
6002 fprintf(stderr, "remote client lists directory contents.\n"
6003 "obsolete, does not support it anymore.\n");
6007 static int lfs_changelog(int argc, char **argv)
6009 void *changelog_priv;
6010 struct changelog_rec *rec;
6011 long long startrec = 0, endrec = 0;
6013 struct option long_opts[] = {
6014 { .val = 'f', .name = "follow", .has_arg = no_argument },
6016 char short_opts[] = "f";
6019 while ((rc = getopt_long(argc, argv, short_opts,
6020 long_opts, NULL)) != -1) {
6028 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6029 argv[0], argv[optind - 1]);
6036 mdd = argv[optind++];
6038 startrec = strtoll(argv[optind++], NULL, 10);
6040 endrec = strtoll(argv[optind++], NULL, 10);
6042 rc = llapi_changelog_start(&changelog_priv,
6043 CHANGELOG_FLAG_BLOCK |
6044 CHANGELOG_FLAG_JOBID |
6045 CHANGELOG_FLAG_EXTRA_FLAGS |
6046 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6049 fprintf(stderr, "Can't start changelog: %s\n",
6050 strerror(errno = -rc));
6054 rc = llapi_changelog_set_xflags(changelog_priv,
6055 CHANGELOG_EXTRA_FLAG_UIDGID |
6056 CHANGELOG_EXTRA_FLAG_NID |
6057 CHANGELOG_EXTRA_FLAG_OMODE |
6058 CHANGELOG_EXTRA_FLAG_XATTR);
6060 fprintf(stderr, "Can't set xflags for changelog: %s\n",
6061 strerror(errno = -rc));
6065 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6069 if (endrec && rec->cr_index > endrec) {
6070 llapi_changelog_free(&rec);
6073 if (rec->cr_index < startrec) {
6074 llapi_changelog_free(&rec);
6078 secs = rec->cr_time >> 30;
6079 gmtime_r(&secs, &ts);
6080 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6081 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6082 changelog_type2str(rec->cr_type),
6083 ts.tm_hour, ts.tm_min, ts.tm_sec,
6084 (int)(rec->cr_time & ((1 << 30) - 1)),
6085 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6086 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6088 if (rec->cr_flags & CLF_JOBID) {
6089 struct changelog_ext_jobid *jid =
6090 changelog_rec_jobid(rec);
6092 if (jid->cr_jobid[0] != '\0')
6093 printf(" j=%s", jid->cr_jobid);
6096 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6097 struct changelog_ext_extra_flags *ef =
6098 changelog_rec_extra_flags(rec);
6100 printf(" ef=0x%llx", ef->cr_extra_flags);
6102 if (ef->cr_extra_flags & CLFE_UIDGID) {
6103 struct changelog_ext_uidgid *uidgid =
6104 changelog_rec_uidgid(rec);
6106 printf(" u=%llu:%llu",
6107 uidgid->cr_uid, uidgid->cr_gid);
6109 if (ef->cr_extra_flags & CLFE_NID) {
6110 struct changelog_ext_nid *nid =
6111 changelog_rec_nid(rec);
6114 libcfs_nid2str(nid->cr_nid));
6117 if (ef->cr_extra_flags & CLFE_OPEN) {
6118 struct changelog_ext_openmode *omd =
6119 changelog_rec_openmode(rec);
6120 char mode[] = "---";
6122 /* exec mode must be exclusive */
6123 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6126 if (omd->cr_openflags & FMODE_READ)
6128 if (omd->cr_openflags &
6135 if (strcmp(mode, "---") != 0)
6136 printf(" m=%s", mode);
6140 if (ef->cr_extra_flags & CLFE_XATTR) {
6141 struct changelog_ext_xattr *xattr =
6142 changelog_rec_xattr(rec);
6144 if (xattr->cr_xattr[0] != '\0')
6145 printf(" x=%s", xattr->cr_xattr);
6149 if (rec->cr_namelen)
6150 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6151 rec->cr_namelen, changelog_rec_name(rec));
6153 if (rec->cr_flags & CLF_RENAME) {
6154 struct changelog_ext_rename *rnm =
6155 changelog_rec_rename(rec);
6157 if (!fid_is_zero(&rnm->cr_sfid))
6158 printf(" s="DFID" sp="DFID" %.*s",
6159 PFID(&rnm->cr_sfid),
6160 PFID(&rnm->cr_spfid),
6161 (int)changelog_rec_snamelen(rec),
6162 changelog_rec_sname(rec));
6166 llapi_changelog_free(&rec);
6169 llapi_changelog_fini(&changelog_priv);
6172 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
6174 return (rc == 1 ? 0 : rc);
6177 static int lfs_changelog_clear(int argc, char **argv)
6185 endrec = strtoll(argv[3], NULL, 10);
6187 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6190 fprintf(stderr, "%s: record out of range: %llu\n",
6192 else if (rc == -ENOENT)
6193 fprintf(stderr, "%s: no changelog user: %s\n",
6196 fprintf(stderr, "%s error: %s\n", argv[0],
6205 static int lfs_fid2path(int argc, char **argv)
6207 struct option long_opts[] = {
6208 { .val = 'c', .name = "cur", .has_arg = no_argument },
6209 { .val = 'l', .name = "link", .has_arg = required_argument },
6210 { .val = 'r', .name = "rec", .has_arg = required_argument },
6212 char short_opts[] = "cl:r:";
6213 char *device, *fid, *path;
6214 long long recno = -1;
6220 while ((rc = getopt_long(argc, argv, short_opts,
6221 long_opts, NULL)) != -1) {
6227 linkno = strtol(optarg, NULL, 10);
6230 recno = strtoll(optarg, NULL, 10);
6235 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6236 argv[0], argv[optind - 1]);
6244 device = argv[optind++];
6245 path = calloc(1, PATH_MAX);
6247 fprintf(stderr, "error: Not enough memory\n");
6252 while (optind < argc) {
6253 fid = argv[optind++];
6255 lnktmp = (linkno >= 0) ? linkno : 0;
6257 int oldtmp = lnktmp;
6258 long long rectmp = recno;
6260 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6263 fprintf(stderr, "%s: error on FID %s: %s\n",
6264 argv[0], fid, strerror(errno = -rc2));
6271 fprintf(stdout, "%lld ", rectmp);
6272 if (device[0] == '/') {
6273 fprintf(stdout, "%s", device);
6274 if (device[strlen(device) - 1] != '/')
6275 fprintf(stdout, "/");
6276 } else if (path[0] == '\0') {
6277 fprintf(stdout, "/");
6279 fprintf(stdout, "%s\n", path);
6282 /* specified linkno */
6284 if (oldtmp == lnktmp)
6294 static int lfs_path2fid(int argc, char **argv)
6296 struct option long_opts[] = {
6297 { .val = 'p', .name = "parents", .has_arg = no_argument },
6300 const char short_opts[] = "p";
6301 const char *sep = "";
6304 bool show_parents = false;
6306 while ((rc = getopt_long(argc, argv, short_opts,
6307 long_opts, NULL)) != -1) {
6310 show_parents = true;
6313 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6314 argv[0], argv[optind - 1]);
6319 if (optind > argc - 1)
6321 else if (optind < argc - 1)
6325 for (path = argv + optind; *path != NULL; path++) {
6327 if (!show_parents) {
6328 err = llapi_path2fid(*path, &fid);
6330 printf("%s%s"DFID"\n",
6331 *sep != '\0' ? *path : "", sep,
6334 char name[NAME_MAX + 1];
6335 unsigned int linkno = 0;
6337 while ((err = llapi_path2parent(*path, linkno, &fid,
6338 name, sizeof(name))) == 0) {
6339 if (*sep != '\0' && linkno == 0)
6340 printf("%s%s", *path, sep);
6342 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6347 /* err == -ENODATA is end-of-loop */
6348 if (linkno > 0 && err == -ENODATA) {
6355 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
6356 argv[0], show_parents ? "parent " : "", *path,
6368 static int lfs_data_version(int argc, char **argv)
6375 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
6380 while ((c = getopt(argc, argv, "nrw")) != -1) {
6383 data_version_flags = 0;
6386 data_version_flags |= LL_DV_RD_FLUSH;
6389 data_version_flags |= LL_DV_WR_FLUSH;
6398 path = argv[optind];
6399 fd = open(path, O_RDONLY);
6401 err(errno, "cannot open file %s", path);
6403 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
6405 err(errno, "cannot get version for %s", path);
6407 printf("%ju" "\n", (uintmax_t)data_version);
6413 static int lfs_hsm_state(int argc, char **argv)
6418 struct hsm_user_state hus;
6426 rc = llapi_hsm_state_get(path, &hus);
6428 fprintf(stderr, "can't get hsm state for %s: %s\n",
6429 path, strerror(errno = -rc));
6433 /* Display path name and status flags */
6434 printf("%s: (0x%08x)", path, hus.hus_states);
6436 if (hus.hus_states & HS_RELEASED)
6437 printf(" released");
6438 if (hus.hus_states & HS_EXISTS)
6440 if (hus.hus_states & HS_DIRTY)
6442 if (hus.hus_states & HS_ARCHIVED)
6443 printf(" archived");
6444 /* Display user-settable flags */
6445 if (hus.hus_states & HS_NORELEASE)
6446 printf(" never_release");
6447 if (hus.hus_states & HS_NOARCHIVE)
6448 printf(" never_archive");
6449 if (hus.hus_states & HS_LOST)
6450 printf(" lost_from_hsm");
6452 if (hus.hus_archive_id != 0)
6453 printf(", archive_id:%d", hus.hus_archive_id);
6456 } while (++i < argc);
6461 #define LFS_HSM_SET 0
6462 #define LFS_HSM_CLEAR 1
6465 * Generic function to set or clear HSM flags.
6466 * Used by hsm_set and hsm_clear.
6468 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
6470 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
6472 struct option long_opts[] = {
6473 { .val = 'A', .name = "archived", .has_arg = no_argument },
6474 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
6475 { .val = 'd', .name = "dirty", .has_arg = no_argument },
6476 { .val = 'e', .name = "exists", .has_arg = no_argument },
6477 { .val = 'l', .name = "lost", .has_arg = no_argument },
6478 { .val = 'r', .name = "norelease", .has_arg = no_argument },
6479 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
6481 char short_opts[] = "lraAdei:";
6485 __u32 archive_id = 0;
6491 while ((c = getopt_long(argc, argv, short_opts,
6492 long_opts, NULL)) != -1) {
6498 mask |= HS_NOARCHIVE;
6501 mask |= HS_ARCHIVED;
6504 mask |= HS_NORELEASE;
6513 archive_id = strtol(optarg, &end, 10);
6515 fprintf(stderr, "invalid archive_id: '%s'\n",
6523 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6524 argv[0], argv[optind - 1]);
6529 /* User should have specified a flag */
6533 while (optind < argc) {
6535 path = argv[optind];
6537 /* If mode == 0, this means we apply the mask. */
6538 if (mode == LFS_HSM_SET)
6539 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6541 rc = llapi_hsm_state_set(path, 0, mask, 0);
6544 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6545 path, strerror(errno = -rc));
6554 static int lfs_hsm_action(int argc, char **argv)
6559 struct hsm_current_action hca;
6560 struct hsm_extent he;
6561 enum hsm_user_action hua;
6562 enum hsm_progress_states hps;
6570 rc = llapi_hsm_current_action(path, &hca);
6572 fprintf(stderr, "can't get hsm action for %s: %s\n",
6573 path, strerror(errno = -rc));
6576 he = hca.hca_location;
6577 hua = hca.hca_action;
6578 hps = hca.hca_state;
6580 printf("%s: %s", path, hsm_user_action2name(hua));
6582 /* Skip file without action */
6583 if (hca.hca_action == HUA_NONE) {
6588 printf(" %s ", hsm_progress_state2name(hps));
6590 if ((hps == HPS_RUNNING) &&
6591 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
6592 printf("(%llu bytes moved)\n",
6593 (unsigned long long)he.length);
6594 else if ((he.offset + he.length) == LUSTRE_EOF)
6595 printf("(from %llu to EOF)\n",
6596 (unsigned long long)he.offset);
6598 printf("(from %llu to %llu)\n",
6599 (unsigned long long)he.offset,
6600 (unsigned long long)(he.offset + he.length));
6602 } while (++i < argc);
6607 static int lfs_hsm_set(int argc, char **argv)
6609 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
6612 static int lfs_hsm_clear(int argc, char **argv)
6614 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
6618 * Check file state and return its fid, to be used by lfs_hsm_request().
6620 * \param[in] file Path to file to check
6621 * \param[in,out] fid Pointer to allocated lu_fid struct.
6622 * \param[in,out] last_dev Pointer to last device id used.
6624 * \return 0 on success.
6626 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
6632 rc = lstat(file, &st);
6634 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
6637 /* Checking for regular file as archiving as posix copytool
6638 * rejects archiving files other than regular files
6640 if (!S_ISREG(st.st_mode)) {
6641 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
6644 /* A request should be ... */
6645 if (*last_dev != st.st_dev && *last_dev != 0) {
6646 fprintf(stderr, "All files should be "
6647 "on the same filesystem: %s\n", file);
6650 *last_dev = st.st_dev;
6652 rc = llapi_path2fid(file, fid);
6654 fprintf(stderr, "Cannot read FID of %s: %s\n",
6655 file, strerror(-rc));
6661 /* Fill an HSM HUR item with a given file name.
6663 * If mntpath is set, then the filename is actually a FID, and no
6664 * lookup on the filesystem will be performed.
6666 * \param[in] hur the user request to fill
6667 * \param[in] idx index of the item inside the HUR to fill
6668 * \param[in] mntpath mountpoint of Lustre
6669 * \param[in] fname filename (if mtnpath is NULL)
6670 * or FID (if mntpath is set)
6671 * \param[in] last_dev pointer to last device id used
6673 * \retval 0 on success
6674 * \retval CMD_HELP or a negative errno on error
6676 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
6677 const char *mntpath, const char *fname,
6680 struct hsm_user_item *hui = &hur->hur_user_item[idx];
6683 hui->hui_extent.length = -1;
6685 if (mntpath != NULL) {
6688 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
6692 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
6697 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
6701 hur->hur_request.hr_itemcount++;
6706 static int lfs_hsm_request(int argc, char **argv, int action)
6708 struct option long_opts[] = {
6709 { .val = 'a', .name = "archive", .has_arg = required_argument },
6710 { .val = 'D', .name = "data", .has_arg = required_argument },
6711 { .val = 'l', .name = "filelist", .has_arg = required_argument },
6712 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
6715 char short_opts[] = "l:D:a:m:";
6716 struct hsm_user_request *hur, *oldhur;
6721 char *filelist = NULL;
6722 char fullpath[PATH_MAX];
6723 char *opaque = NULL;
6727 int nbfile_alloc = 0;
6728 char *some_file = NULL;
6729 char *mntpath = NULL;
6735 while ((c = getopt_long(argc, argv, short_opts,
6736 long_opts, NULL)) != -1) {
6745 if (action != HUA_ARCHIVE &&
6746 action != HUA_REMOVE) {
6748 "error: -a is supported only "
6749 "when archiving or removing\n");
6752 archive_id = atoi(optarg);
6755 if (some_file == NULL) {
6757 some_file = strdup(optarg);
6763 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6764 argv[0], argv[optind - 1]);
6769 /* All remaining args are files, so we have at least nbfile */
6770 nbfile = argc - optind;
6772 if ((nbfile == 0) && (filelist == NULL))
6776 opaque_len = strlen(opaque);
6778 /* Alloc the request structure with enough place to store all files
6779 * from command line. */
6780 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
6782 fprintf(stderr, "Cannot create the request: %s\n",
6786 nbfile_alloc = nbfile;
6788 hur->hur_request.hr_action = action;
6789 hur->hur_request.hr_archive_id = archive_id;
6790 hur->hur_request.hr_flags = 0;
6792 /* All remaining args are files, add them */
6793 if (nbfile != 0 && some_file == NULL)
6794 some_file = strdup(argv[optind]);
6796 for (i = 0; i < nbfile; i++) {
6797 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
6803 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
6805 /* If a filelist was specified, read the filelist from it. */
6806 if (filelist != NULL) {
6807 fp = fopen(filelist, "r");
6809 fprintf(stderr, "Cannot read the file list %s: %s\n",
6810 filelist, strerror(errno));
6815 while ((rc = getline(&line, &len, fp)) != -1) {
6816 /* If allocated buffer was too small, get something
6818 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
6821 nbfile_alloc = nbfile_alloc * 2 + 1;
6823 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
6826 fprintf(stderr, "hsm: cannot allocate "
6827 "the request: %s\n",
6834 size = hur_len(oldhur);
6836 fprintf(stderr, "hsm: cannot allocate "
6837 "%u files + %u bytes data\n",
6838 oldhur->hur_request.hr_itemcount,
6839 oldhur->hur_request.hr_data_len);
6846 memcpy(hur, oldhur, size);
6851 if (line[strlen(line) - 1] == '\n')
6852 line[strlen(line) - 1] = '\0';
6854 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
6855 mntpath, line, &last_dev);
6861 if (some_file == NULL) {
6871 /* If a --data was used, add it to the request */
6872 hur->hur_request.hr_data_len = opaque_len;
6874 memcpy(hur_data(hur), opaque, opaque_len);
6876 /* Send the HSM request */
6877 if (realpath(some_file, fullpath) == NULL) {
6878 fprintf(stderr, "Could not find path '%s': %s\n",
6879 some_file, strerror(errno));
6881 rc = llapi_hsm_request(fullpath, hur);
6883 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
6884 some_file, strerror(-rc));
6894 static int lfs_hsm_archive(int argc, char **argv)
6896 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
6899 static int lfs_hsm_restore(int argc, char **argv)
6901 return lfs_hsm_request(argc, argv, HUA_RESTORE);
6904 static int lfs_hsm_release(int argc, char **argv)
6906 return lfs_hsm_request(argc, argv, HUA_RELEASE);
6909 static int lfs_hsm_remove(int argc, char **argv)
6911 return lfs_hsm_request(argc, argv, HUA_REMOVE);
6914 static int lfs_hsm_cancel(int argc, char **argv)
6916 return lfs_hsm_request(argc, argv, HUA_CANCEL);
6919 static int lfs_swap_layouts(int argc, char **argv)
6924 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
6925 SWAP_LAYOUTS_KEEP_MTIME |
6926 SWAP_LAYOUTS_KEEP_ATIME);
6929 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
6931 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
6933 int lfs_get_mode(const char *string)
6935 enum lock_mode_user mode;
6937 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
6938 if (lock_mode_names[mode] == NULL)
6940 if (strcmp(string, lock_mode_names[mode]) == 0)
6947 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
6949 enum lu_ladvise_type advice;
6952 advice < ARRAY_SIZE(ladvise_names); advice++) {
6953 if (ladvise_names[advice] == NULL)
6955 if (strcmp(string, ladvise_names[advice]) == 0)
6959 return LU_LADVISE_INVALID;
6962 static int lfs_ladvise(int argc, char **argv)
6964 struct option long_opts[] = {
6965 { .val = 'a', .name = "advice", .has_arg = required_argument },
6966 { .val = 'b', .name = "background", .has_arg = no_argument },
6967 { .val = 'e', .name = "end", .has_arg = required_argument },
6968 { .val = 'l', .name = "length", .has_arg = required_argument },
6969 { .val = 'm', .name = "mode", .has_arg = required_argument },
6970 { .val = 's', .name = "start", .has_arg = required_argument },
6971 { .val = 'u', .name = "unset", .has_arg = no_argument },
6973 char short_opts[] = "a:be:l:m:s:u";
6978 struct llapi_lu_ladvise advice;
6979 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
6980 unsigned long long start = 0;
6981 unsigned long long end = LUSTRE_EOF;
6982 unsigned long long length = 0;
6983 unsigned long long size_units;
6984 unsigned long long flags = 0;
6988 while ((c = getopt_long(argc, argv, short_opts,
6989 long_opts, NULL)) != -1) {
6992 advice_type = lfs_get_ladvice(optarg);
6993 if (advice_type == LU_LADVISE_INVALID) {
6994 fprintf(stderr, "%s: invalid advice type "
6995 "'%s'\n", argv[0], optarg);
6996 fprintf(stderr, "Valid types:");
6998 for (advice_type = 0;
6999 advice_type < ARRAY_SIZE(ladvise_names);
7001 if (ladvise_names[advice_type] == NULL)
7003 fprintf(stderr, " %s",
7004 ladvise_names[advice_type]);
7006 fprintf(stderr, "\n");
7019 rc = llapi_parse_size(optarg, &end,
7022 fprintf(stderr, "%s: bad end offset '%s'\n",
7029 rc = llapi_parse_size(optarg, &start,
7032 fprintf(stderr, "%s: bad start offset "
7033 "'%s'\n", argv[0], optarg);
7039 rc = llapi_parse_size(optarg, &length,
7042 fprintf(stderr, "%s: bad length '%s'\n",
7048 mode = lfs_get_mode(optarg);
7050 fprintf(stderr, "%s: bad mode '%s', valid "
7051 "modes are READ or WRITE\n",
7059 fprintf(stderr, "%s: option '%s' unrecognized\n",
7060 argv[0], argv[optind - 1]);
7065 if (advice_type == LU_LADVISE_INVALID) {
7066 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7067 fprintf(stderr, "Valid types:");
7068 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7070 if (ladvise_names[advice_type] == NULL)
7072 fprintf(stderr, " %s", ladvise_names[advice_type]);
7074 fprintf(stderr, "\n");
7078 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7079 fprintf(stderr, "%s: Lock no expand advice is a per file "
7080 "descriptor advice, so when called from lfs, "
7081 "it does nothing.\n", argv[0]);
7085 if (argc <= optind) {
7086 fprintf(stderr, "%s: please give one or more file names\n",
7091 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7092 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7097 if (end == LUSTRE_EOF && length != 0)
7098 end = start + length;
7101 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7102 argv[0], start, end);
7106 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7107 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7112 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7113 fprintf(stderr, "%s: mode is required with lockahead\n",
7118 while (optind < argc) {
7121 path = argv[optind++];
7123 fd = open(path, O_RDONLY);
7125 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7126 argv[0], path, strerror(errno));
7131 advice.lla_start = start;
7132 advice.lla_end = end;
7133 advice.lla_advice = advice_type;
7134 advice.lla_value1 = 0;
7135 advice.lla_value2 = 0;
7136 advice.lla_value3 = 0;
7137 advice.lla_value4 = 0;
7138 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7139 advice.lla_lockahead_mode = mode;
7140 advice.lla_peradvice_flags = flags;
7143 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7146 fprintf(stderr, "%s: cannot give advice '%s' to file "
7147 "'%s': %s\n", argv[0],
7148 ladvise_names[advice_type],
7149 path, strerror(errno));
7155 if (rc == 0 && rc2 < 0)
7161 /** The input string contains a comma delimited list of component ids and
7162 * ranges, for example "1,2-4,7".
7164 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7166 bool end_of_loop = false;
7174 while (!end_of_loop) {
7178 char *endptr = NULL;
7181 ptr = strchrnul(arg, ',');
7182 end_of_loop = *ptr == '\0';
7185 start_index = strtol(arg, &endptr, 0);
7186 if (endptr == arg) /* no data at all */
7188 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7190 if (start_index < 0)
7193 end_index = start_index;
7194 if (*endptr == '-') {
7195 end_index = strtol(endptr + 1, &endptr, 0);
7196 if (*endptr != '\0')
7198 if (end_index < start_index)
7202 for (i = start_index; i <= end_index && size > 0; i++) {
7205 /* remove duplicate */
7206 for (j = 0; j < nr; j++) {
7210 if (j == nr) { /* no duplicate */
7216 if (size == 0 && i < end_index)
7223 if (!end_of_loop && ptr != NULL)
7226 return rc < 0 ? rc : nr;
7230 * struct verify_mirror_id - Mirror id to be verified.
7231 * @mirror_id: A specified mirror id.
7232 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7234 struct verify_mirror_id {
7240 * compare_mirror_ids() - Compare mirror ids.
7241 * @layout: Mirror component list.
7242 * @cbdata: Callback data in verify_mirror_id structure.
7244 * This is a callback function called by llapi_layout_comp_iterate()
7245 * to compare the specified mirror id with the one in the current
7246 * component of @layout. If they are the same, then the specified
7247 * mirror id is valid.
7249 * Return: a negative error code on failure or
7250 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7251 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7254 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7256 struct verify_mirror_id *mirror_id_cbdata =
7257 (struct verify_mirror_id *)cbdata;
7261 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7265 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7266 progname, strerror(errno));
7270 if (mirror_id_cbdata->mirror_id == mirror_id) {
7271 mirror_id_cbdata->is_valid_id = true;
7272 return LLAPI_LAYOUT_ITER_STOP;
7275 return LLAPI_LAYOUT_ITER_CONT;
7279 * verify_mirror_ids() - Verify specified mirror ids.
7280 * @fname: Mirrored file name.
7281 * @mirror_ids: Specified mirror ids to be verified.
7282 * @ids_nr: Number of specified mirror ids.
7284 * This function verifies that specified @mirror_ids are valid
7285 * in the mirrored file @fname.
7287 * Return: 0 on success or a negative error code on failure.
7290 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7292 struct llapi_layout *layout = NULL;
7293 struct verify_mirror_id mirror_id_cbdata = { 0 };
7304 if (stat(fname, &stbuf) < 0) {
7305 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7306 progname, fname, strerror(errno));
7311 if (!S_ISREG(stbuf.st_mode)) {
7312 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7318 fd = open(fname, O_DIRECT | O_RDONLY);
7320 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7321 progname, fname, strerror(errno));
7326 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
7328 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
7329 progname, fname, strerror(errno));
7333 layout = llapi_layout_get_by_fd(fd, 0);
7334 if (layout == NULL) {
7335 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7336 progname, fname, strerror(errno));
7338 llapi_lease_release(fd);
7342 rc = llapi_layout_flags_get(layout, &flr_state);
7344 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7345 progname, fname, strerror(errno));
7350 flr_state &= LCM_FL_FLR_MASK;
7351 switch (flr_state) {
7354 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7355 progname, fname, llapi_layout_flags_string(flr_state));
7362 for (i = 0; i < ids_nr; i++) {
7363 mirror_id_cbdata.mirror_id = mirror_ids[i];
7364 mirror_id_cbdata.is_valid_id = false;
7366 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
7371 "%s: '%s' failed to verify mirror id: %u.\n",
7372 progname, fname, mirror_ids[i]);
7376 if (!mirror_id_cbdata.is_valid_id) {
7379 "%s: '%s' invalid specified mirror id: %u.\n",
7380 progname, fname, mirror_ids[i]);
7386 llapi_layout_free(layout);
7387 llapi_lease_release(fd);
7395 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
7396 __u16 *mirror_ids, int ids_nr)
7398 struct llapi_resync_comp comp_array[1024] = { { 0 } };
7399 struct llapi_layout *layout;
7407 if (stat(fname, &stbuf) < 0) {
7408 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7409 progname, fname, strerror(errno));
7413 if (!S_ISREG(stbuf.st_mode)) {
7414 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7420 fd = open(fname, O_DIRECT | O_RDWR);
7422 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7423 progname, fname, strerror(errno));
7428 layout = llapi_layout_get_by_fd(fd, 0);
7429 if (layout == NULL) {
7430 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7431 progname, fname, strerror(errno));
7436 rc = llapi_layout_flags_get(layout, &flr_state);
7438 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7439 progname, fname, strerror(errno));
7444 flr_state &= LCM_FL_FLR_MASK;
7445 if (flr_state == LCM_FL_NONE) {
7447 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
7452 /* get stale component info */
7453 comp_size = llapi_mirror_find_stale(layout, comp_array,
7454 ARRAY_SIZE(comp_array),
7455 mirror_ids, ids_nr);
7456 if (comp_size <= 0) {
7461 ioc->lil_mode = LL_LEASE_WRLCK;
7462 ioc->lil_flags = LL_LEASE_RESYNC;
7463 rc = llapi_lease_set(fd, ioc);
7465 if (rc == -EALREADY)
7469 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
7470 progname, fname, strerror(errno));
7475 while (idx < comp_size) {
7481 rc = llapi_lease_check(fd);
7482 if (rc != LL_LEASE_WRLCK) {
7483 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7488 mirror_id = comp_array[idx].lrc_mirror_id;
7489 end = comp_array[idx].lrc_end;
7491 /* try to combine adjacent component */
7492 for (i = idx + 1; i < comp_size; i++) {
7493 if (mirror_id != comp_array[i].lrc_mirror_id ||
7494 end != comp_array[i].lrc_start)
7496 end = comp_array[i].lrc_end;
7499 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7500 comp_array[idx].lrc_start,
7503 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7504 "%ld.\n", progname, fname, result);
7507 } else if (result > 0) {
7510 /* mark synced components */
7511 for (j = idx; j < i; j++)
7512 comp_array[j].lrc_synced = true;
7519 /* prepare ioc for lease put */
7520 ioc->lil_mode = LL_LEASE_UNLCK;
7521 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7523 for (idx = 0; idx < comp_size; idx++) {
7524 if (comp_array[idx].lrc_synced) {
7525 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7530 rc = llapi_lease_set(fd, ioc);
7532 if (rc == 0) /* lost lease lock */
7534 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7535 progname, fname, strerror(errno));
7539 * llapi_lease_set returns lease mode when it request to unlock
7545 llapi_layout_free(layout);
7552 static inline int lfs_mirror_resync(int argc, char **argv)
7554 struct ll_ioc_lease *ioc = NULL;
7555 __u16 mirror_ids[128] = { 0 };
7560 struct option long_opts[] = {
7561 { .val = 'o', .name = "only", .has_arg = required_argument },
7564 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
7567 rc = parse_mirror_ids(mirror_ids,
7568 sizeof(mirror_ids) / sizeof(__u16),
7572 "%s: bad mirror ids '%s'.\n",
7579 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7580 argv[0], argv[optind - 1]);
7586 if (argc == optind) {
7587 fprintf(stderr, "%s: no file name given.\n", argv[0]);
7592 if (ids_nr > 0 && argc > optind + 1) {
7594 "%s: option '--only' cannot be used upon multiple files.\n",
7602 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7607 /* set the lease on the file */
7608 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
7610 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
7611 argv[0], strerror(errno));
7616 for (; optind < argc; optind++) {
7617 rc = lfs_mirror_resync_file(argv[optind], ioc,
7618 mirror_ids, ids_nr);
7619 /* ignore previous file's error, continue with next file */
7622 memset(ioc, 0, sizeof(__u32) * 4096);
7631 * struct verify_chunk - Mirror chunk to be verified.
7632 * @chunk: [start, end) of the chunk.
7633 * @mirror_count: Number of mirror ids in @mirror_id array.
7634 * @mirror_id: Array of valid mirror ids that cover the chunk.
7636 struct verify_chunk {
7637 struct lu_extent chunk;
7638 unsigned int mirror_count;
7639 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
7643 * print_chunks() - Print chunk information.
7644 * @fname: Mirrored file name.
7645 * @chunks: Array of chunks.
7646 * @chunk_count: Number of chunks in @chunks array.
7648 * This function prints [start, end) of each chunk in @chunks
7649 * for mirrored file @fname, and also prints the valid mirror ids
7650 * that cover the chunk.
7655 void print_chunks(const char *fname, struct verify_chunk *chunks,
7661 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
7662 for (i = 0; i < chunk_count; i++) {
7663 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
7665 if (chunks[i].mirror_count == 0)
7666 fprintf(stdout, "\t[");
7668 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
7669 for (j = 1; j < chunks[i].mirror_count; j++)
7670 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
7672 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
7674 fprintf(stdout, "\n");
7678 * print_checksums() - Print CRC-32 checksum values.
7679 * @chunk: A chunk and its corresponding valid mirror ids.
7680 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
7682 * This function prints CRC-32 checksum values on @chunk for
7683 * each valid mirror that covers it.
7688 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
7693 "CRC-32 checksum value for chunk "DEXT":\n",
7694 PEXT(&chunk->chunk));
7695 for (i = 0; i < chunk->mirror_count; i++)
7696 fprintf(stdout, "Mirror %u:\t%#lx\n",
7697 chunk->mirror_id[i], crc[i]);
7698 fprintf(stdout, "\n");
7702 * filter_mirror_id() - Filter specified mirror ids.
7703 * @chunks: Array of chunks.
7704 * @chunk_count: Number of chunks in @chunks array.
7705 * @mirror_ids: Specified mirror ids to be verified.
7706 * @ids_nr: Number of specified mirror ids.
7708 * This function scans valid mirror ids that cover each chunk in @chunks
7709 * and filters specified mirror ids.
7714 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
7715 __u16 *mirror_ids, int ids_nr)
7720 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7721 unsigned int valid_count = 0;
7723 for (i = 0; i < chunk_count; i++) {
7724 if (chunks[i].mirror_count == 0)
7728 for (j = 0; j < ids_nr; j++) {
7729 for (k = 0; k < chunks[i].mirror_count; k++) {
7730 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
7731 valid_id[valid_count] = mirror_ids[j];
7738 memcpy(chunks[i].mirror_id, valid_id,
7739 sizeof(__u16) * valid_count);
7740 chunks[i].mirror_count = valid_count;
7745 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
7746 * @layout: Mirror component list.
7747 * @chunks: Array of chunks.
7748 * @chunks_size: Array size of @chunks.
7750 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
7751 * to find out chunk segments and store them in @chunks array.
7753 * The @mirror_id array in each element of @chunks will store the valid
7754 * mirror ids that cover the chunk. If a mirror component covering the
7755 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
7756 * will not be stored into the @mirror_id array, and the chunk for that
7757 * mirror will not be verified.
7759 * The @mirror_count in each element of @chunks will store the number of
7760 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
7761 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
7762 * indicates the chunk is valid in only one mirror. In both cases, the
7763 * chunk will not be verified.
7765 * Here is an example:
7768 * +------+-------------+--------------------+
7770 * +------+------+------+------+-------------+
7771 * | | S | S | | mirror2
7772 * +-------------+------+------+-------------+
7774 * prepared @chunks array will contain 5 elements:
7775 * (([0, 1M), [1, 2], 2),
7776 * ([1M, 2M), [1, 2], 2),
7777 * ([2M, 3M), [1], 1),
7778 * ([3M, 4M], [], 0),
7779 * ([4M, EOF), [2], 1))
7781 * Return: the actual array size of @chunks on success
7782 * or a negative error code on failure.
7785 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
7786 struct verify_chunk *chunks,
7797 memset(chunks, 0, sizeof(*chunks) * chunks_size);
7800 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
7803 "%s: move to the first layout component: %s.\n",
7804 progname, strerror(errno));
7810 chunks[idx].chunk.e_end = LUSTRE_EOF;
7812 rc = llapi_layout_comp_extent_get(layout, &start, &end);
7815 "%s: llapi_layout_comp_extent_get failed: %s.\n",
7816 progname, strerror(errno));
7820 if (start > chunks[idx].chunk.e_start ||
7821 end <= chunks[idx].chunk.e_start)
7824 if (end < chunks[idx].chunk.e_end)
7825 chunks[idx].chunk.e_end = end;
7827 rc = llapi_layout_comp_flags_get(layout, &flags);
7830 "%s: llapi_layout_comp_flags_get failed: %s.\n",
7831 progname, strerror(errno));
7835 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
7838 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7841 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7842 progname, strerror(errno));
7846 chunks[idx].mirror_id[i] = mirror_id;
7848 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
7850 "%s: mirror_id array is too small.\n",
7857 rc = llapi_layout_comp_use(layout,
7858 LLAPI_LAYOUT_COMP_USE_NEXT);
7861 "%s: move to the next layout component: %s.\n",
7862 progname, strerror(errno));
7865 } /* loop through all components */
7867 chunks[idx].mirror_count = i;
7869 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
7873 if (idx >= chunks_size) {
7874 fprintf(stderr, "%s: chunks array is too small.\n",
7880 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
7884 return rc < 0 ? rc : idx + 1;
7888 * lfs_mirror_verify_chunk() - Verify a chunk.
7889 * @fd: File descriptor of the mirrored file.
7890 * @file_size: Size of the mirrored file.
7891 * @chunk: A chunk and its corresponding valid mirror ids.
7892 * @verbose: Verbose mode.
7894 * This function verifies a @chunk contains exactly the same data
7895 * ammong the mirrors that cover it.
7897 * If @verbose is specified, then the function will print where the
7898 * differences are if the data do not match. Otherwise, it will
7899 * just return an error in that case.
7901 * Return: 0 on success or a negative error code on failure.
7904 int lfs_mirror_verify_chunk(int fd, size_t file_size,
7905 struct verify_chunk *chunk, int verbose)
7907 const size_t buflen = 4 * 1024 * 1024; /* 4M */
7909 size_t page_size = sysconf(_SC_PAGESIZE);
7915 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7922 rc = posix_memalign(&buf, page_size, buflen);
7923 if (rc) /* error code is returned directly */
7927 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7928 PEXT(&chunk->chunk));
7929 for (i = 0; i < chunk->mirror_count; i++)
7930 fprintf(stdout, " %u", chunk->mirror_id[i]);
7931 fprintf(stdout, "\n");
7935 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
7936 pos = chunk->chunk.e_start;
7937 while (bytes_done < count) {
7938 /* compute initial CRC-32 checksum */
7939 crc = crc32(0L, Z_NULL, 0);
7940 memset(crc_array, 0, sizeof(crc_array));
7943 for (i = 0; i < chunk->mirror_count; i++) {
7944 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
7946 if (bytes_read < 0) {
7949 "%s: failed to read data from mirror %u: %s.\n",
7950 progname, chunk->mirror_id[i],
7955 /* compute new CRC-32 checksum */
7956 crc_array[i] = crc32(crc, buf, bytes_read);
7960 print_checksums(chunk, crc_array);
7962 /* compare CRC-32 checksum values */
7963 for (i = 1; i < chunk->mirror_count; i++) {
7964 if (crc_array[i] != crc_array[0]) {
7970 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
7971 progname, PEXT(&chunk->chunk),
7972 chunk->mirror_id[0],
7973 chunk->mirror_id[i]);
7978 bytes_done += bytes_read;
7981 if (verbose > 1 && rc == 0) {
7982 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7983 PEXT(&chunk->chunk));
7984 for (i = 0; i < chunk->mirror_count; i++)
7985 fprintf(stdout, " %u", chunk->mirror_id[i]);
7986 fprintf(stdout, " PASS\n\n");
7995 * lfs_mirror_verify_file() - Verify a mirrored file.
7996 * @fname: Mirrored file name.
7997 * @mirror_ids: Specified mirror ids to be verified.
7998 * @ids_nr: Number of specified mirror ids.
7999 * @verbose: Verbose mode.
8001 * This function verifies that each SYNC mirror of a mirrored file
8002 * specified by @fname contains exactly the same data.
8004 * If @mirror_ids is specified, then the function will verify the
8005 * mirrors specified by @mirror_ids contain exactly the same data.
8007 * If @verbose is specified, then the function will print where the
8008 * differences are if the data do not match. Otherwise, it will
8009 * just return an error in that case.
8011 * Return: 0 on success or a negative error code on failure.
8014 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8017 struct verify_chunk chunks_array[1024] = { };
8018 struct llapi_layout *layout = NULL;
8022 int chunk_count = 0;
8028 if (stat(fname, &stbuf) < 0) {
8029 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8030 progname, fname, strerror(errno));
8035 if (!S_ISREG(stbuf.st_mode)) {
8036 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8042 if (stbuf.st_size == 0) {
8044 fprintf(stdout, "%s: '%s' file size is 0.\n",
8050 fd = open(fname, O_DIRECT | O_RDONLY);
8052 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8053 progname, fname, strerror(errno));
8058 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8060 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8061 progname, fname, strerror(errno));
8065 layout = llapi_layout_get_by_fd(fd, 0);
8066 if (layout == NULL) {
8067 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8068 progname, fname, strerror(errno));
8070 llapi_lease_release(fd);
8074 rc = llapi_layout_flags_get(layout, &flr_state);
8076 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8077 progname, fname, strerror(errno));
8082 flr_state &= LCM_FL_FLR_MASK;
8083 switch (flr_state) {
8086 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8087 progname, fname, llapi_layout_flags_string(flr_state));
8093 /* find out mirror chunks to be verified */
8094 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8095 ARRAY_SIZE(chunks_array));
8096 if (chunk_count < 0) {
8102 /* filter specified mirror ids */
8103 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8106 print_chunks(fname, chunks_array, chunk_count);
8108 for (idx = 0; idx < chunk_count; idx++) {
8109 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8112 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8114 PEXT(&chunks_array[idx].chunk),
8115 (unsigned long long)stbuf.st_size);
8119 if (chunks_array[idx].mirror_count == 0) {
8121 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8123 PEXT(&chunks_array[idx].chunk));
8125 fprintf(stderr, "skipped\n");
8129 fprintf(stderr, "failed\n");
8133 if (chunks_array[idx].mirror_count == 1) {
8136 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8138 PEXT(&chunks_array[idx].chunk),
8139 chunks_array[idx].mirror_id[0]);
8143 rc = llapi_lease_check(fd);
8144 if (rc != LL_LEASE_RDLCK) {
8145 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8150 /* verify one chunk */
8151 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8152 &chunks_array[idx], verbose);
8166 llapi_layout_free(layout);
8167 llapi_lease_release(fd);
8175 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
8176 * @argc: The count of lfs mirror verify command line arguments.
8177 * @argv: Array of strings for lfs mirror verify command line arguments.
8179 * This function parses lfs mirror verify command and verifies the
8180 * specified mirrored file(s).
8182 * Return: 0 on success or a negative error code on failure.
8184 static inline int lfs_mirror_verify(int argc, char **argv)
8186 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8194 struct option long_opts[] = {
8195 { .val = 'o', .name = "only", .has_arg = required_argument },
8196 { .val = 'v', .name = "verbose", .has_arg = no_argument },
8199 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8201 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
8204 rc = parse_mirror_ids(mirror_ids,
8205 ARRAY_SIZE(mirror_ids),
8209 "%s: bad mirror ids '%s'.\n",
8216 "%s: at least 2 mirror ids needed with '--only' option.\n",
8226 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8227 progname, argv[optind - 1]);
8233 if (argc == optind) {
8234 fprintf(stderr, "%s: no file name given.\n", progname);
8239 if (ids_nr > 0 && argc > optind + 1) {
8241 "%s: '--only' cannot be used upon multiple files.\n",
8249 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8255 for (; optind < argc; optind++) {
8256 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
8266 * lfs_mirror() - Parse and execute lfs mirror commands.
8267 * @argc: The count of lfs mirror command line arguments.
8268 * @argv: Array of strings for lfs mirror command line arguments.
8270 * This function parses lfs mirror commands and performs the
8271 * corresponding functions specified in mirror_cmdlist[].
8273 * Return: 0 on success or an error code on failure.
8275 static int lfs_mirror(int argc, char **argv)
8282 Parser_init("lfs-mirror > ", mirror_cmdlist);
8284 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8286 program_invocation_short_name = cmd;
8288 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
8290 rc = Parser_commands();
8292 return rc < 0 ? -rc : rc;
8296 * lfs_mirror_list_commands() - List lfs mirror commands.
8297 * @argc: The count of command line arguments.
8298 * @argv: Array of strings for command line arguments.
8300 * This function lists lfs mirror commands defined in mirror_cmdlist[].
8302 * Return: 0 on success.
8304 static int lfs_mirror_list_commands(int argc, char **argv)
8306 char buffer[81] = "";
8308 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
8314 static int lfs_list_commands(int argc, char **argv)
8316 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
8318 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
8323 int main(int argc, char **argv)
8327 /* Ensure that liblustreapi constructor has run */
8328 if (!llapi_liblustreapi_initialized())
8329 fprintf(stderr, "liblustreapi was not properly initialized\n");
8334 Parser_init("lfs > ", cmdlist);
8336 progname = program_invocation_short_name; /* Used in error messages */
8338 llapi_set_command_name(argv[1]);
8339 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
8340 llapi_clear_command_name();
8342 rc = Parser_commands();
8345 return rc < 0 ? -rc : rc;
8348 #ifdef _LUSTRE_IDL_H_
8349 /* Everything we need here should be included by lustreapi.h. */
8350 # error "lfs should not depend on lustre_idl.h"
8351 #endif /* _LUSTRE_IDL_H_ */