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 "\tparent: Use default stripe options from parent directory\n" \
197 "\tflags: set flags to the component of the current mirror.\n" \
198 "\t Only \"prefer\" flag is supported so far.\n"
200 #define MIRROR_EXTEND_HELP \
202 "\tvictim_file: The layout of victim_file will be split and used\n" \
203 "\t as a mirror added to the mirrored file.\n" \
204 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
205 "\t from victim file(s) in case the victim file(s)\n" \
206 "\t contains the same data as the original mirrored\n" \
209 #define MIRROR_EXTEND_USAGE \
210 " <--mirror-count|-N[mirror_count]>\n" \
211 " [setstripe options|--parent|-f <victim_file>]\n" \
214 #define SETSTRIPE_USAGE \
215 SSM_CMD_COMMON("setstripe") \
216 MIRROR_EXTEND_USAGE \
217 " <directory|filename>\n" \
221 #define MIGRATE_USAGE \
222 SSM_CMD_COMMON("migrate ") \
224 " [--non-block|-n]\n" \
225 " [--non-direct|-D]\n" \
229 "\tblock: Block file access during data migration (default)\n" \
230 "\tnon-block: Abort migrations if concurrent access is detected\n" \
231 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
233 #define SETDIRSTRIPE_USAGE \
234 " [--mdt-count|-c stripe_count>\n" \
235 " [--mdt-index|-i mdt_index]\n" \
236 " [--mdt-hash|-H mdt_hash]\n" \
237 " [--default|-D] [--mode|-m mode] <dir>\n" \
238 "\tstripe_count: stripe count of the striped directory\n" \
239 "\tmdt_index: MDT index of first stripe\n" \
240 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
241 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
242 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
243 "\tdefault_stripe: set default dirstripe of the directory\n" \
244 "\tmode: the mode of the directory\n"
247 * command_t mirror_cmdlist - lfs mirror commands.
249 command_t mirror_cmdlist[] = {
250 { .pc_name = "create", .pc_func = lfs_mirror_create,
251 .pc_help = "Create a mirrored file.\n"
252 "usage: lfs mirror create "
253 "<--mirror-count|-N[mirror_count]> "
254 "[setstripe options|--parent] ... <filename|directory>\n"
255 MIRROR_CREATE_HELP },
256 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
257 .pc_help = "Extend a mirrored file.\n"
258 "usage: lfs mirror extend "
259 "<--mirror-count|-N[mirror_count]> [--no-verify] "
260 "[setstripe options|--parent|-f <victim_file>] ... <filename>\n"
261 MIRROR_EXTEND_HELP },
262 { .pc_name = "split", .pc_func = lfs_mirror_split,
263 .pc_help = "Split a mirrored file.\n"
264 "usage: lfs mirror split <--mirror-id <mirror_id>> [--destroy|-d] "
265 "[-f <new_file>] <mirrored file>\n"
266 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
267 "\t can be fetched by lfs getstripe command.\n"
268 "\tnew_file: This option indicates the layout of the split\n"
269 "\t mirror will be stored into. If not specified,\n"
270 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
271 "\t will be used.\n" },
272 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
273 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
274 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
275 "<mirrored file> [<mirrored file2>...]\n"},
276 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
277 .pc_help = "Verify mirrored file(s).\n"
278 "usage: lfs mirror verify "
279 "[--only <mirror_id,mirror_id2[,...]>] "
280 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
281 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
282 .pc_help = "list commands supported by lfs mirror"},
283 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
284 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
285 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
289 /* all available commands */
290 command_t cmdlist[] = {
291 {"setstripe", lfs_setstripe, 0,
292 "To create a file with specified striping/composite layout, or\n"
293 "create/replace the default layout on an existing directory:\n"
294 SSM_CMD_COMMON("setstripe")
295 " <directory|filename>\n"
297 "To add component(s) to an existing composite file:\n"
298 SSM_CMD_COMMON("setstripe --component-add")
300 "To totally delete the default striping from an existing directory:\n"
301 "usage: setstripe -d <directory>\n"
303 "To delete the last component(s) from an existing composite file\n"
304 "(note that this will also delete any data in those components):\n"
305 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
306 " [--component-flags|-F <comp_flags>]\n"
308 "\tcomp_id: Unique component ID to delete\n"
309 "\tcomp_flags: 'init' indicating all instantiated components\n"
310 "\t '^init' indicating all uninstantiated components\n"
311 "\t-I and -F cannot be specified at the same time\n"},
312 {"getstripe", lfs_getstripe, 0,
313 "To list the layout pattern for a given file or files in a\n"
314 "directory or recursively for all files in a directory tree.\n"
315 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
316 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
317 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
318 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
319 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
320 " [--component-id[=comp_id]|-I[comp_id]]\n"
321 " [--component-flags[=comp_flags]]\n"
322 " [--component-count]\n"
323 " [--component-start[=[+-]comp_start]]\n"
324 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
325 " <directory|filename> ..."},
326 {"setdirstripe", lfs_setdirstripe, 0,
327 "To create a striped directory on a specified MDT. This can only\n"
328 "be done on MDT0 with the right of administrator.\n"
329 "usage: setdirstripe [OPTION] <directory>\n"
331 {"getdirstripe", lfs_getdirstripe, 0,
332 "To list the layout pattern info for a given directory\n"
333 "or recursively for all directories in a directory tree.\n"
334 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
335 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
336 " [--recursive|-r] [--yaml|-y]\n"
337 " [--default|-D] <dir> ..."},
338 {"mkdir", lfs_setdirstripe, 0,
339 "To create a striped directory on a specified MDT. This can only\n"
340 "be done on MDT0 with the right of administrator.\n"
341 "usage: mkdir [OPTION] <directory>\n"
343 {"rm_entry", lfs_rmentry, 0,
344 "To remove the name entry of the remote directory. Note: This\n"
345 "command will only delete the name entry, i.e. the remote directory\n"
346 "will become inaccessable after this command. This can only be done\n"
347 "by the administrator\n"
348 "usage: rm_entry <dir>\n"},
349 {"pool_list", lfs_poollist, 0,
350 "List pools or pool OSTs\n"
351 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
352 {"find", lfs_find, 0,
353 "find files matching given attributes recursively in directory tree.\n"
354 "usage: find <directory|filename> ...\n"
355 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
356 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N]\n"
357 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
358 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
359 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
360 " [[!] --stripe-count|-c [+-]<stripes>]\n"
361 " [[!] --stripe-index|-i <index,...>]\n"
362 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
363 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
364 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
365 " [[!] --projid <projid>]\n"
366 " [[!] --layout|-L released,raid0,mdt]\n"
367 " [[!] --component-count [+-]<comp_cnt>]\n"
368 " [[!] --component-start [+-]N[kMGTPE]]\n"
369 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
370 " [[!] --component-flags <comp_flags>]\n"
371 " [[!] --mirror-count|-N [+-]<n>]\n"
372 " [[!] --mirror-state <[^]state>]\n"
373 " [[!] --mdt-count|-T [+-]<stripes>]\n"
374 " [[!] --mdt-hash|-H <hashtype>\n"
375 "\t !: used before an option indicates 'NOT' requested attribute\n"
376 "\t -: used before a value indicates less than requested value\n"
377 "\t +: used before a value indicates more than requested value\n"
378 "\thashtype: hash type of the striped directory.\n"
379 "\t fnv_1a_64 FNV-1a hash algorithm\n"
380 "\t all_char sum of characters % MDT_COUNT\n"},
381 {"check", lfs_check, 0,
382 "Display the status of MDS or OSTs (as specified in the command)\n"
383 "or all the servers (MDS and OSTs).\n"
384 "usage: check <osts|mds|servers>"},
385 {"osts", lfs_osts, 0, "list OSTs connected to client "
386 "[for specified path only]\n" "usage: osts [path]"},
387 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
388 "[for specified path only]\n" "usage: mdts [path]"},
390 "report filesystem disk space usage or inodes usage"
391 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
392 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
393 {"getname", lfs_getname, 0, "list instances and specified mount points "
394 "[for specified path only]\n"
395 "Usage: getname [-h]|[path ...] "},
396 #ifdef HAVE_SYS_QUOTA_H
397 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
398 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
399 " -b <block-softlimit> -B <block-hardlimit>\n"
400 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
401 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
402 " [--block-softlimit <block-softlimit>]\n"
403 " [--block-hardlimit <block-hardlimit>]\n"
404 " [--inode-softlimit <inode-softlimit>]\n"
405 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
406 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
407 " [--block-grace <block-grace>]\n"
408 " [--inode-grace <inode-grace>] <filesystem>\n"
409 " -b can be used instead of --block-softlimit/--block-grace\n"
410 " -B can be used instead of --block-hardlimit\n"
411 " -i can be used instead of --inode-softlimit/--inode-grace\n"
412 " -I can be used instead of --inode-hardlimit\n\n"
413 "Note: The total quota space will be split into many qunits and\n"
414 " balanced over all server targets, the minimal qunit size is\n"
415 " 1M bytes for block space and 1K inodes for inode space.\n\n"
416 " Quota space rebalancing process will stop when this mininum\n"
417 " value is reached. As a result, quota exceeded can be returned\n"
418 " while many targets still have 1MB or 1K inodes of spare\n"
420 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
421 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
423 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
424 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
425 {"project", lfs_project, 0,
426 "Change or list project attribute for specified file or directory.\n"
427 "usage: project [-d|-r] <file|directory...>\n"
428 " list project ID and flags on file(s) or directories\n"
429 " project [-p id] [-s] [-r] <file|directory...>\n"
430 " set project ID and/or inherit flag for specified file(s) or directories\n"
431 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
432 " check project ID and flags on file(s) or directories, print outliers\n"
433 " project -C [-r] [-k] <file|directory...>\n"
434 " clear the project inherit flag and ID on the file or directory\n"
437 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
438 "usage: flushctx [-k] [mountpoint...]"},
440 "Remote user copy files and directories.\n"
441 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
443 "Remote user list directory contents.\n"
444 "usage: ls [OPTION]... [FILE]..."},
445 {"changelog", lfs_changelog, 0,
446 "Show the metadata changes on an MDT."
447 "\nusage: changelog <mdtname> [startrec [endrec]]"},
448 {"changelog_clear", lfs_changelog_clear, 0,
449 "Indicate that old changelog records up to <endrec> are no longer of "
450 "interest to consumer <id>, allowing the system to free up space.\n"
451 "An <endrec> of 0 means all records.\n"
452 "usage: changelog_clear <mdtname> <id> <endrec>"},
453 {"fid2path", lfs_fid2path, 0,
454 "Resolve the full path(s) for given FID(s). For a specific hardlink "
455 "specify link number <linkno>.\n"
456 /* "For a historical link name, specify changelog record <recno>.\n" */
457 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
458 /* [ --rec <recno> ] */ },
459 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
460 "usage: path2fid [--parents] <path> ..."},
461 {"data_version", lfs_data_version, 0, "Display file data version for "
462 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
463 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
464 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
465 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
466 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
467 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
468 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
470 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
471 "[--archived] [--lost] <file> ..."},
472 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
473 "given files.\n" "usage: hsm_action <file> ..."},
474 {"hsm_archive", lfs_hsm_archive, 0,
475 "Archive file to external storage.\n"
476 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
478 {"hsm_restore", lfs_hsm_restore, 0,
479 "Restore file from external storage.\n"
480 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
481 {"hsm_release", lfs_hsm_release, 0,
482 "Release files from Lustre.\n"
483 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
484 {"hsm_remove", lfs_hsm_remove, 0,
485 "Remove file copy from external storage.\n"
486 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
488 " (FILE [FILE ...] | "
489 "--mntpath MOUNTPATH FID [FID ...])\n"
491 "Note: To remove an archived copy of a file already deleted from a "
493 "--mntpath option and a list of FIDs must be specified"
495 {"hsm_cancel", lfs_hsm_cancel, 0,
496 "Cancel requests related to specified files.\n"
497 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
498 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
499 "usage: swap_layouts <path1> <path2>"},
500 {"migrate", lfs_setstripe_migrate, 0,
501 "migrate a directory between MDTs.\n"
502 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
504 "\tmdt_idx: index of the destination MDT\n"
506 "migrate file objects from one OST "
507 "layout\nto another (may be not safe with concurent writes).\n"
509 "[--stripe-count|-c] <stripe_count>\n"
510 " [--stripe-index|-i] <start_ost_index>\n"
511 " [--stripe-size|-S] <stripe_size>\n"
512 " [--pool|-p] <pool_name>\n"
513 " [--ost|-o] <ost_indices>\n"
515 " [--non-block|-n]\n"
516 " [--non-direct|-D]\n"
517 " <file|directory>\n"
518 "\tstripe_count: number of OSTs to stripe a file over\n"
519 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
520 "\tstripe_size: number of bytes to store before moving to the next OST\n"
521 "\tpool_name: name of the predefined pool of OSTs\n"
522 "\tost_indices: OSTs to stripe over, in order\n"
523 "\tblock: wait for the operation to return before continuing\n"
524 "\tnon-block: do not wait for the operation to return\n"
525 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
527 "To move directories between MDTs. This command is deprecated, "
528 "use \"migrate\" instead.\n"
529 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
531 {"ladvise", lfs_ladvise, 0,
532 "Provide servers with advice about access patterns for a file.\n"
533 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
534 " [--background|-b] [--unset|-u]\n\n"
535 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
536 " {[--mode|-m [READ,WRITE]}\n"
538 {"mirror", lfs_mirror, mirror_cmdlist,
539 "lfs commands used to manage files with mirrored components:\n"
540 "lfs mirror create - create a mirrored file or directory\n"
541 "lfs mirror extend - add mirror(s) to an existing file\n"
542 "lfs mirror split - split a mirror from an existing mirrored file\n"
543 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
544 "lfs mirror verify - verify mirrored file(s)\n"},
545 {"help", Parser_help, 0, "help"},
546 {"exit", Parser_quit, 0, "quit"},
547 {"quit", Parser_quit, 0, "quit"},
548 {"--version", Parser_version, 0,
549 "output build version of the utility and exit"},
550 {"--list-commands", lfs_list_commands, 0,
551 "list commands supported by the utility and exit"},
556 static int check_hashtype(const char *hashtype)
560 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
561 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
568 static const char *error_loc = "syserror";
571 MIGRATION_NONBLOCK = 1 << 0,
572 MIGRATION_MIRROR = 1 << 1,
573 MIGRATION_NONDIRECT = 1 << 2,
576 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
577 struct llapi_layout *layout);
580 migrate_open_files(const char *name, __u64 migration_flags,
581 const struct llapi_stripe_param *param,
582 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
589 char parent[PATH_MAX];
590 char volatile_file[PATH_MAX];
596 if (param == NULL && layout == NULL) {
597 error_loc = "layout information";
601 /* search for file directory pathname */
602 if (strlen(name) > sizeof(parent) - 1) {
603 error_loc = "source file name";
607 strncpy(parent, name, sizeof(parent));
608 ptr = strrchr(parent, '/');
610 if (getcwd(parent, sizeof(parent)) == NULL) {
611 error_loc = "getcwd";
615 if (ptr == parent) /* leading '/' */
620 /* open file, direct io */
621 /* even if the file is only read, WR mode is nedeed to allow
622 * layout swap on fd */
624 if (!(migration_flags & MIGRATION_NONDIRECT))
626 fd = open(name, rflags);
629 error_loc = "cannot open source file";
633 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
635 error_loc = "cannot get MDT index";
640 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
641 mode_t open_mode = S_IRUSR | S_IWUSR;
643 random_value = random();
644 rc = snprintf(volatile_file, sizeof(volatile_file),
645 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
646 mdt_index, random_value);
647 if (rc >= sizeof(volatile_file)) {
652 /* create, open a volatile file, use caching (ie no directio) */
654 fdv = llapi_file_open_param(volatile_file, open_flags,
657 fdv = lfs_component_create(volatile_file, open_flags,
659 } while (fdv < 0 && (rc = fdv) == -EEXIST);
662 error_loc = "cannot create volatile file";
666 /* In case the MDT does not support creation of volatile files
667 * we should try to unlink it. */
668 (void)unlink(volatile_file);
670 /* Not-owner (root?) special case.
671 * Need to set owner/group of volatile file like original.
672 * This will allow to pass related check during layout_swap.
677 error_loc = "cannot stat source file";
681 rc = fstat(fdv, &stv);
684 error_loc = "cannot stat volatile";
688 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
689 rc = fchown(fdv, st.st_uid, st.st_gid);
692 error_loc = "cannot change ownwership of volatile";
711 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
713 struct llapi_layout *layout;
714 size_t buf_size = 4 * 1024 * 1024;
723 layout = llapi_layout_get_by_fd(fd_src, 0);
724 if (layout != NULL) {
725 uint64_t stripe_size;
727 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
729 buf_size = stripe_size;
731 llapi_layout_free(layout);
734 /* Use a page-aligned buffer for direct I/O */
735 rc = posix_memalign(&buf, getpagesize(), buf_size);
740 /* read new data only if we have written all
741 * previously read data */
744 rc = check_file(fd_src);
749 rsize = read(fd_src, buf, buf_size);
761 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
780 static int migrate_copy_timestamps(int fd, int fdv)
784 if (fstat(fd, &st) == 0) {
785 struct timeval tv[2] = {
786 {.tv_sec = st.st_atime},
787 {.tv_sec = st.st_mtime}
790 return futimes(fdv, tv);
796 static int migrate_block(int fd, int fdv)
803 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
805 error_loc = "cannot get dataversion";
813 /* The grouplock blocks all concurrent accesses to the file.
814 * It has to be taken after llapi_get_data_version as it would
816 rc = llapi_group_lock(fd, gid);
818 error_loc = "cannot get group lock";
822 rc = migrate_copy_data(fd, fdv, NULL);
824 error_loc = "data copy failed";
828 /* Make sure we keep original atime/mtime values */
829 rc = migrate_copy_timestamps(fd, fdv);
831 error_loc = "timestamp copy failed";
836 * for a migration we need to check data version on file did
839 * Pass in gid=0 since we already own grouplock. */
840 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
841 SWAP_LAYOUTS_CHECK_DV1);
843 error_loc = "file changed";
846 error_loc = "cannot swap layout";
851 rc2 = llapi_group_unlock(fd, gid);
852 if (rc2 < 0 && rc == 0) {
853 error_loc = "unlock group lock";
861 * Internal helper for migrate_copy_data(). Check lease and report error if
864 * \param[in] fd File descriptor on which to check the lease.
866 * \retval 0 Migration can keep on going.
867 * \retval -errno Error occurred, abort migration.
869 static int check_lease(int fd)
873 rc = llapi_lease_check(fd);
875 return 0; /* llapi_check_lease returns > 0 on success. */
880 static int migrate_nonblock(int fd, int fdv)
886 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
888 error_loc = "cannot get data version";
892 rc = migrate_copy_data(fd, fdv, check_lease);
894 error_loc = "data copy failed";
898 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
900 error_loc = "cannot get data version";
906 error_loc = "source file changed";
910 /* Make sure we keep original atime/mtime values */
911 rc = migrate_copy_timestamps(fd, fdv);
913 error_loc = "timestamp copy failed";
920 static int lfs_component_set(char *fname, int comp_id,
921 __u32 flags, __u32 neg_flags)
924 __u32 flags_array[2];
929 ids[count] = comp_id;
930 flags_array[count] = flags;
935 ids[count] = comp_id;
936 flags_array[count] = neg_flags | LCME_FL_NEG;
940 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
943 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
944 progname, comp_id, fname, flags, neg_flags);
949 static int lfs_component_del(char *fname, __u32 comp_id,
950 __u32 flags, __u32 neg_flags)
954 if (flags && neg_flags)
957 if (!flags && neg_flags)
958 flags = neg_flags | LCME_FL_NEG;
960 if ((flags && comp_id) || (!flags && !comp_id))
963 /* LCME_FL_INIT is the only supported flag in PFL */
965 if (flags & ~LCME_KNOWN_FLAGS) {
967 "%s setstripe: unknown flags %#x\n",
971 } else if (comp_id > LCME_ID_MAX) {
972 fprintf(stderr, "%s setstripe: invalid component id %u\n",
977 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
980 "%s setstripe: cannot delete component %#x from '%s': %s\n",
981 progname, comp_id, fname, strerror(errno));
985 static int lfs_component_add(char *fname, struct llapi_layout *layout)
992 rc = llapi_layout_file_comp_add(fname, layout);
994 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
995 fname, strerror(errno));
999 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1000 struct llapi_layout *layout)
1008 fd = lstat(fname, &st);
1009 if (fd == 0 && S_ISDIR(st.st_mode))
1010 open_flags = O_DIRECTORY | O_RDONLY;
1012 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1014 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1015 S_ISDIR(st.st_mode) ?
1016 "set default composite layout for" :
1017 "create composite file",
1018 fname, strerror(errno));
1022 static int lfs_migrate(char *name, __u64 migration_flags,
1023 struct llapi_stripe_param *param,
1024 struct llapi_layout *layout)
1030 rc = migrate_open_files(name, migration_flags, param, layout,
1035 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1036 /* Blocking mode (forced if servers do not support file lease).
1037 * It is also the default mode, since we cannot distinguish
1038 * between a broken lease and a server that does not support
1039 * atomic swap/close (LU-6785) */
1040 rc = migrate_block(fd, fdv);
1044 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1046 error_loc = "cannot get lease";
1050 rc = migrate_nonblock(fd, fdv);
1052 llapi_lease_release(fd);
1056 /* Atomically put lease, swap layouts and close.
1057 * for a migration we need to check data version on file did
1059 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1061 error_loc = "cannot swap layout";
1073 fprintf(stderr, "error: %s: %s: %s: %s\n",
1074 progname, name, error_loc, strerror(-rc));
1078 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1087 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1091 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1092 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1093 const char *comp_name = comp_flags_table[i].cfn_name;
1095 if (strcmp(name, comp_name) == 0) {
1096 *flags |= comp_flag;
1098 } else if (strncmp(name, "^", 1) == 0 &&
1099 strcmp(name + 1, comp_name) == 0) {
1100 *neg_flags |= comp_flag;
1105 llapi_printf(LLAPI_MSG_ERROR,
1106 "%s: component flag '%s' not supported\n",
1112 if (!*flags && !*neg_flags)
1115 /* don't allow to set and exclude the same flag */
1116 if (*flags & *neg_flags)
1122 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1130 if (strncmp(string, "^", 1) == 0) {
1131 *neg_state = llapi_layout_string_flags(string + 1);
1132 if (*neg_state != 0)
1135 *state = llapi_layout_string_flags(string);
1140 llapi_printf(LLAPI_MSG_ERROR,
1141 "%s: mirrored file state '%s' not supported\n",
1147 * struct mirror_args - Command-line arguments for mirror(s).
1148 * @m_count: Number of mirrors to be created with this layout.
1149 * @m_flags: Mirror level flags, only 'prefer' is supported.
1150 * @m_layout: Mirror layout.
1151 * @m_file: A victim file. Its layout will be split and used as a mirror.
1152 * @m_next: Point to the next node of the list.
1154 * Command-line arguments for mirror(s) will be parsed and stored in
1155 * a linked list that consists of this structure.
1157 struct mirror_args {
1160 struct llapi_layout *m_layout;
1162 struct mirror_args *m_next;
1165 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1170 rc = llapi_layout_comp_flags_get(layout, &flags);
1174 if (flags & LCME_FL_NEG) {
1175 fprintf(stderr, "error: %s: negative flags are not supported\n",
1180 if (flags & LCME_FL_STALE) {
1181 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1182 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1186 return LLAPI_LAYOUT_ITER_CONT;
1189 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1191 uint64_t start, end;
1195 /* LU-10112: do not support dom+flr in phase 1 */
1196 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1200 rc = llapi_layout_pattern_get(layout, &pattern);
1204 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1205 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1210 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1214 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1218 if (end != LUSTRE_EOF) {
1219 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1224 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1229 * enum mirror_flags - Flags for extending a mirrored file.
1230 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1231 * in case the victim file(s) contains the same data as the
1232 * original mirrored file.
1233 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1235 * Flags for extending a mirrored file.
1243 * mirror_create_sanity_check() - Check mirror list.
1244 * @list: A linked list that stores the mirror arguments.
1246 * This function does a sanity check on @list for creating
1249 * Return: 0 on success or a negative error code on failure.
1251 static int mirror_create_sanity_check(const char *fname,
1252 struct mirror_args *list)
1255 bool has_m_file = false;
1256 bool has_m_layout = false;
1262 struct llapi_layout *layout;
1264 layout = llapi_layout_get_by_path(fname, 0);
1267 "error: %s: file '%s' couldn't get layout\n",
1272 rc = mirror_sanity_check_one(layout);
1273 llapi_layout_free(layout);
1279 while (list != NULL) {
1280 if (list->m_file != NULL) {
1282 llapi_layout_free(list->m_layout);
1285 llapi_layout_get_by_path(list->m_file, 0);
1286 if (list->m_layout == NULL) {
1288 "error: %s: file '%s' has no layout\n",
1289 progname, list->m_file);
1293 has_m_layout = true;
1294 if (list->m_layout == NULL) {
1295 fprintf(stderr, "error: %s: no mirror layout\n",
1301 rc = mirror_sanity_check_one(list->m_layout);
1305 list = list->m_next;
1308 if (has_m_file && has_m_layout) {
1309 fprintf(stderr, "error: %s: -f <victim_file> option should not "
1310 "be specified with setstripe options or "
1311 "--parent option\n", progname);
1318 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1320 __u32 mirror_flags = *(__u32 *)cbdata;
1324 rc = llapi_layout_comp_flags_get(layout, &flags);
1329 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1334 return LLAPI_LAYOUT_ITER_CONT;
1338 * mirror_create() - Create a mirrored file.
1339 * @fname: The file to be created.
1340 * @mirror_list: A linked list that stores the mirror arguments.
1342 * This function creates a mirrored file @fname with the mirror(s)
1343 * from @mirror_list.
1345 * Return: 0 on success or a negative error code on failure.
1347 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1349 struct llapi_layout *layout = NULL;
1350 struct mirror_args *cur_mirror = NULL;
1351 uint16_t mirror_count = 0;
1355 rc = mirror_create_sanity_check(NULL, mirror_list);
1359 cur_mirror = mirror_list;
1360 while (cur_mirror != NULL) {
1361 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1363 &cur_mirror->m_flags);
1366 fprintf(stderr, "%s: failed to set mirror flags\n",
1371 for (i = 0; i < cur_mirror->m_count; i++) {
1372 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1375 fprintf(stderr, "error: %s: "
1376 "merge layout failed: %s\n",
1377 progname, strerror(errno));
1381 mirror_count += cur_mirror->m_count;
1382 cur_mirror = cur_mirror->m_next;
1385 if (layout == NULL) {
1386 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1390 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1393 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1394 progname, strerror(errno));
1398 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0644,
1406 llapi_layout_free(layout);
1411 * Compare files and check lease on @fd.
1413 * \retval bytes number of bytes are the same
1415 static ssize_t mirror_file_compare(int fd, int fdv)
1417 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1419 ssize_t bytes_done = 0;
1420 ssize_t bytes_read = 0;
1422 buf = malloc(buflen * 2);
1427 if (!llapi_lease_check(fd)) {
1428 bytes_done = -EBUSY;
1432 bytes_read = read(fd, buf, buflen);
1433 if (bytes_read <= 0)
1436 if (bytes_read != read(fdv, buf + buflen, buflen))
1439 /* XXX: should compute the checksum on each buffer and then
1440 * compare checksum to avoid cache collision */
1441 if (memcmp(buf, buf + buflen, bytes_read))
1444 bytes_done += bytes_read;
1452 static int mirror_extend_file(const char *fname, const char *victim_file,
1453 enum mirror_flags mirror_flags)
1458 struct stat stbuf_v;
1459 struct ll_ioc_lease *data = NULL;
1462 fd = open(fname, O_RDWR);
1464 error_loc = "open source file";
1469 fdv = open(victim_file, O_RDWR);
1471 error_loc = "open target file";
1476 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1477 error_loc = "stat source or target file";
1482 if (stbuf.st_dev != stbuf_v.st_dev) {
1483 error_loc = "stat source and target file";
1488 /* mirrors should be of the same size */
1489 if (stbuf.st_size != stbuf_v.st_size) {
1490 error_loc = "file sizes don't match";
1495 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1497 error_loc = "cannot get lease";
1501 if (!(mirror_flags & MF_NO_VERIFY)) {
1503 /* mirrors should have the same contents */
1504 ret = mirror_file_compare(fd, fdv);
1505 if (ret != stbuf.st_size) {
1506 error_loc = "file busy or contents don't match";
1507 rc = ret < 0 ? ret : -EINVAL;
1512 /* Get rid of caching pages from clients */
1513 rc = llapi_file_flush(fd);
1515 error_loc = "cannot get data version";
1519 rc = llapi_file_flush(fdv);
1521 error_loc = "cannot get data version";
1526 /* Make sure we keep original atime/mtime values */
1527 rc = migrate_copy_timestamps(fd, fdv);
1529 error_loc = "cannot copy timestamp";
1533 /* Atomically put lease, merge layouts and close. */
1534 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1536 error_loc = "memory allocation";
1539 data->lil_mode = LL_LEASE_UNLCK;
1540 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1541 data->lil_count = 1;
1542 data->lil_ids[0] = fdv;
1543 rc = llapi_lease_set(fd, data);
1545 error_loc = "cannot merge layout";
1547 } else if (rc == 0) {
1549 error_loc = "lost lease lock";
1562 (void) unlink(victim_file);
1564 fprintf(stderr, "error: %s: %s: %s: %s\n",
1565 progname, fname, error_loc, strerror(-rc));
1569 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1571 struct ll_ioc_lease *data = NULL;
1576 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1580 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1582 error_loc = "cannot get lease";
1586 rc = migrate_nonblock(fd, fdv);
1588 llapi_lease_release(fd);
1592 /* Atomically put lease, merge layouts and close. */
1593 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1595 error_loc = "memory allocation";
1598 data->lil_mode = LL_LEASE_UNLCK;
1599 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1600 data->lil_count = 1;
1601 data->lil_ids[0] = fdv;
1602 rc = llapi_lease_set(fd, data);
1604 error_loc = "cannot merge layout";
1606 } else if (rc == 0) {
1608 error_loc = "lost lease lock";
1621 fprintf(stderr, "error: %s: %s: %s: %s\n",
1622 progname, name, error_loc, strerror(-rc));
1626 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1627 enum mirror_flags mirror_flags)
1631 rc = mirror_create_sanity_check(fname, mirror_list);
1635 while (mirror_list) {
1636 if (mirror_list->m_file != NULL) {
1637 rc = mirror_extend_file(fname, mirror_list->m_file,
1640 __u32 mirror_count = mirror_list->m_count;
1642 while (mirror_count > 0) {
1643 rc = mirror_extend_layout(fname,
1644 mirror_list->m_layout);
1654 mirror_list = mirror_list->m_next;
1660 static int verify_id(struct llapi_layout *layout, void *cbdata)
1665 rc = llapi_layout_mirror_id_get(layout, &id);
1669 if ((__u16)id == *(__u16 *)cbdata)
1670 return LLAPI_LAYOUT_ITER_STOP;
1672 return LLAPI_LAYOUT_ITER_CONT;
1675 static int mirror_split(const char *fname, __u16 mirror_id,
1676 enum mirror_flags mflags, const char *victim_file)
1678 struct llapi_layout *layout;
1679 char parent[PATH_MAX];
1680 char victim[PATH_MAX];
1681 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1683 struct ll_ioc_lease *data;
1684 uint16_t mirror_count;
1689 /* check fname contains mirror with mirror_id */
1690 layout = llapi_layout_get_by_path(fname, 0);
1693 "error %s: file '%s' couldn't get layout\n",
1698 rc = mirror_sanity_check_one(layout);
1702 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1705 "error %s: file '%s' couldn't get mirror count\n",
1709 if (mirror_count < 2) {
1711 "error %s: file '%s' has %d component, cannot split\n",
1712 progname, fname, mirror_count);
1716 rc = llapi_layout_comp_iterate(layout, verify_id, &mirror_id);
1718 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1721 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1723 "error %s: file '%s' does not contain mirror with id %u\n",
1724 progname, fname, mirror_id);
1728 fd = open(fname, O_RDWR);
1731 "error %s: open file '%s' failed: %s\n",
1732 progname, fname, strerror(errno));
1736 /* get victim file directory pathname */
1737 if (strlen(fname) > sizeof(parent) - 1) {
1738 fprintf(stderr, "error %s: file name of '%s' too long\n",
1743 strncpy(parent, fname, sizeof(parent));
1744 ptr = strrchr(parent, '/');
1746 if (getcwd(parent, sizeof(parent)) == NULL) {
1747 fprintf(stderr, "error %s: getcwd failed: %s\n",
1748 progname, strerror(errno));
1758 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1760 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1765 if (victim_file == NULL) {
1766 /* use a temp file to store the splitted layout */
1767 if (mflags & MF_DESTROY) {
1768 fdv = llapi_create_volatile_idx(parent, mdt_index,
1769 O_LOV_DELAY_CREATE);
1771 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1773 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1776 /* user specified victim file */
1777 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1782 "error %s: create victim file failed: %s\n",
1783 progname, strerror(errno));
1787 /* get lease lock of fname */
1788 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1791 "error %s: cannot get lease of file '%s': %d\n",
1792 progname, fname, rc);
1796 /* Atomatically put lease, split layouts and close. */
1797 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1803 data->lil_mode = LL_LEASE_UNLCK;
1804 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1805 data->lil_count = 2;
1806 data->lil_ids[0] = fdv;
1807 data->lil_ids[1] = mirror_id;
1808 rc = llapi_lease_set(fd, data);
1810 if (rc == 0) /* lost lease lock */
1813 "error %s: cannot split '%s': %s\n",
1814 progname, fname, strerror(-rc));
1825 llapi_layout_free(layout);
1830 * Parse a string containing an target index list into an array of integers.
1832 * The input string contains a comma delimited list of individual
1833 * indices and ranges, for example "1,2-4,7". Add the indices into the
1834 * \a tgts array and remove duplicates.
1836 * \param[out] tgts array to store indices in
1837 * \param[in] size size of \a tgts array
1838 * \param[in] offset starting index in \a tgts
1839 * \param[in] arg string containing OST index list
1841 * \retval positive number of indices in \a tgts
1842 * \retval -EINVAL unable to parse \a arg
1844 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1848 int slots = size - offset;
1855 end_of_loop = false;
1856 while (!end_of_loop) {
1860 char *endptr = NULL;
1864 ptr = strchrnul(arg, ',');
1866 end_of_loop = *ptr == '\0';
1869 start_index = strtol(arg, &endptr, 0);
1870 if (endptr == arg) /* no data at all */
1872 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1875 end_index = start_index;
1876 if (*endptr == '-') {
1877 end_index = strtol(endptr + 1, &endptr, 0);
1878 if (*endptr != '\0')
1880 if (end_index < start_index)
1884 for (i = start_index; i <= end_index && slots > 0; i++) {
1887 /* remove duplicate */
1888 for (j = 0; j < offset; j++) {
1892 if (j == offset) { /* no duplicate */
1897 if (slots == 0 && i < end_index)
1905 if (!end_of_loop && ptr != NULL)
1908 return rc < 0 ? rc : nr;
1911 struct lfs_setstripe_args {
1912 unsigned long long lsa_comp_end;
1913 unsigned long long lsa_stripe_size;
1914 long long lsa_stripe_count;
1915 long long lsa_stripe_off;
1916 __u32 lsa_comp_flags;
1917 __u32 lsa_comp_neg_flags;
1918 unsigned long long lsa_pattern;
1921 char *lsa_pool_name;
1924 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1926 memset(lsa, 0, sizeof(*lsa));
1928 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1929 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1930 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1931 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1932 lsa->lsa_pool_name = NULL;
1936 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1937 * @lsa: Stripe options to be initialized and inherited.
1939 * This function initializes stripe options in @lsa and inherit
1940 * stripe_size, stripe_count and OST pool_name options.
1944 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1946 unsigned long long stripe_size;
1947 long long stripe_count;
1948 char *pool_name = NULL;
1950 stripe_size = lsa->lsa_stripe_size;
1951 stripe_count = lsa->lsa_stripe_count;
1952 pool_name = lsa->lsa_pool_name;
1954 setstripe_args_init(lsa);
1956 lsa->lsa_stripe_size = stripe_size;
1957 lsa->lsa_stripe_count = stripe_count;
1958 lsa->lsa_pool_name = pool_name;
1961 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1963 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1964 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1965 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1966 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1967 lsa->lsa_pool_name != NULL ||
1968 lsa->lsa_comp_end != 0);
1972 * comp_args_to_layout() - Create or extend a composite layout.
1973 * @composite: Pointer to the composite layout.
1974 * @lsa: Stripe options for the new component.
1976 * This function creates or extends a composite layout by adding a new
1977 * component with stripe options from @lsa.
1979 * Return: 0 on success or an error code on failure.
1981 static int comp_args_to_layout(struct llapi_layout **composite,
1982 struct lfs_setstripe_args *lsa)
1984 struct llapi_layout *layout = *composite;
1985 uint64_t prev_end = 0;
1988 if (layout == NULL) {
1989 layout = llapi_layout_alloc();
1990 if (layout == NULL) {
1991 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
1995 *composite = layout;
1999 /* Get current component extent, current component
2000 * must be the tail component. */
2001 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2003 fprintf(stderr, "Get comp extent failed. %s\n",
2008 rc = llapi_layout_comp_add(layout);
2010 fprintf(stderr, "Add component failed. %s\n",
2016 rc = llapi_layout_comp_extent_set(layout, prev_end, lsa->lsa_comp_end);
2018 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2019 prev_end, lsa->lsa_comp_end, strerror(errno));
2023 /* Data-on-MDT component setting */
2024 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2025 /* In case of Data-on-MDT patterns the only extra option
2026 * applicable is stripe size option. */
2027 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2028 fprintf(stderr, "Option 'stripe-count' can't be "
2029 "specified with Data-on-MDT component: %lld\n",
2030 lsa->lsa_stripe_count);
2033 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2034 fprintf(stderr, "Option 'stripe-size' can't be "
2035 "specified with Data-on-MDT component: %llu\n",
2036 lsa->lsa_stripe_size);
2039 if (lsa->lsa_nr_tgts != 0) {
2040 fprintf(stderr, "Option 'ost-list' can't be specified "
2041 "with Data-on-MDT component: '%i'\n",
2045 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2046 fprintf(stderr, "Option 'stripe-offset' can't be "
2047 "specified with Data-on-MDT component: %lld\n",
2048 lsa->lsa_stripe_off);
2051 if (lsa->lsa_pool_name != 0) {
2052 fprintf(stderr, "Option 'pool' can't be specified "
2053 "with Data-on-MDT component: '%s'\n",
2054 lsa->lsa_pool_name);
2058 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2060 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2061 lsa->lsa_pattern, strerror(errno));
2064 /* Data-on-MDT component has always single stripe up to end */
2065 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2068 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2070 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2071 lsa->lsa_stripe_size, strerror(errno));
2075 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2077 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2078 lsa->lsa_stripe_count, strerror(errno));
2082 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2084 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2085 lsa->lsa_comp_flags, strerror(errno));
2089 if (lsa->lsa_pool_name != NULL) {
2090 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2092 fprintf(stderr, "Set pool name: %s failed. %s\n",
2093 lsa->lsa_pool_name, strerror(errno));
2097 rc = llapi_layout_pool_name_set(layout, "");
2099 fprintf(stderr, "Clear pool name failed: %s\n",
2105 if (lsa->lsa_nr_tgts > 0) {
2106 if (lsa->lsa_stripe_count > 0 &&
2107 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2108 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2109 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2110 fprintf(stderr, "stripe_count(%lld) != nr_osts(%d)\n",
2111 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2114 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2115 rc = llapi_layout_ost_index_set(layout, i,
2120 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2121 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2124 fprintf(stderr, "Set ost index %d failed. %s\n",
2125 i, strerror(errno));
2132 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2133 * end of the last component in the existing file, and adjust the
2134 * first extent start of the components to be added accordingly. */
2135 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2137 struct llapi_layout *head;
2138 uint64_t start, end, stripe_size, prev_end = 0;
2141 if (layout == NULL) {
2143 "%s setstripe: layout must be specified\n",
2149 head = llapi_layout_get_by_path(fname, 0);
2152 "%s setstripe: cannot read layout from '%s': %s\n",
2153 progname, fname, strerror(errno));
2155 } else if (errno == ENODATA) {
2156 /* file without LOVEA, this component-add will be turned
2157 * into a component-create. */
2158 llapi_layout_free(head);
2160 } else if (!llapi_layout_is_composite(head)) {
2161 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2163 llapi_layout_free(head);
2167 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2169 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2170 progname, strerror(errno));
2171 llapi_layout_free(head);
2175 llapi_layout_free(head);
2177 /* Make sure we use the first component of the layout to be added. */
2178 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2181 "%s setstripe: cannot move component cursor: %s\n",
2182 progname, strerror(errno));
2186 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2188 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2189 progname, strerror(errno));
2193 if (start > prev_end || end <= prev_end) {
2195 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2196 progname, start, end, prev_end);
2200 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2202 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2203 progname, strerror(errno));
2207 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2208 (prev_end & (stripe_size - 1))) {
2210 "%s setstripe: stripe size %lu not aligned with %lu\n",
2211 progname, stripe_size, prev_end);
2215 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2218 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2219 progname, prev_end, end, strerror(errno));
2226 static inline bool arg_is_eof(char *arg)
2228 return !strncmp(arg, "-1", strlen("-1")) ||
2229 !strncmp(arg, "EOF", strlen("EOF")) ||
2230 !strncmp(arg, "eof", strlen("eof"));
2234 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2236 * Return: Valid mirror_args pointer on success and
2237 * NULL if memory allocation fails.
2239 static struct mirror_args *lfs_mirror_alloc(void)
2241 struct mirror_args *mirror = NULL;
2244 mirror = calloc(1, sizeof(*mirror));
2255 * lfs_mirror_free() - Free memory allocated for a mirror argument
2257 * @mirror: Previously allocated mirror argument structure by
2258 * lfs_mirror_alloc().
2260 * Free memory allocated for @mirror.
2264 static void lfs_mirror_free(struct mirror_args *mirror)
2266 if (mirror->m_layout != NULL)
2267 llapi_layout_free(mirror->m_layout);
2272 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2273 * @mirror_list: Previously allocated mirror list.
2275 * Free memory allocated for @mirror_list.
2279 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2281 struct mirror_args *next_mirror = NULL;
2283 while (mirror_list != NULL) {
2284 next_mirror = mirror_list->m_next;
2285 lfs_mirror_free(mirror_list);
2286 mirror_list = next_mirror;
2298 LFS_COMP_USE_PARENT_OPT,
2299 LFS_COMP_NO_VERIFY_OPT,
2301 LFS_MIRROR_FLAGS_OPT,
2303 LFS_MIRROR_STATE_OPT,
2307 static int lfs_setstripe_internal(int argc, char **argv,
2308 enum setstripe_origin opc)
2310 struct lfs_setstripe_args lsa;
2311 struct llapi_stripe_param *param = NULL;
2312 struct find_param migrate_mdt_param = {
2322 char *mdt_idx_arg = NULL;
2323 unsigned long long size_units = 1;
2324 bool migrate_mode = false;
2325 bool migration_block = false;
2326 __u64 migration_flags = 0;
2327 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2328 int comp_del = 0, comp_set = 0;
2331 struct llapi_layout *layout = NULL;
2332 struct llapi_layout **lpp = &layout;
2333 bool mirror_mode = false;
2334 bool has_m_file = false;
2335 __u32 mirror_count = 0;
2336 enum mirror_flags mirror_flags = 0;
2337 struct mirror_args *mirror_list = NULL;
2338 struct mirror_args *new_mirror = NULL;
2339 struct mirror_args *last_mirror = NULL;
2340 __u16 mirror_id = 0;
2343 struct option long_opts[] = {
2344 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2345 /* --block is only valid in migrate mode */
2346 { .val = 'b', .name = "block", .has_arg = no_argument },
2347 { .val = LFS_COMP_ADD_OPT,
2348 .name = "comp-add", .has_arg = no_argument },
2349 { .val = LFS_COMP_ADD_OPT,
2350 .name = "component-add", .has_arg = no_argument },
2351 { .val = LFS_COMP_DEL_OPT,
2352 .name = "comp-del", .has_arg = no_argument },
2353 { .val = LFS_COMP_DEL_OPT,
2354 .name = "component-del", .has_arg = no_argument },
2355 { .val = LFS_COMP_FLAGS_OPT,
2356 .name = "comp-flags", .has_arg = required_argument },
2357 { .val = LFS_COMP_FLAGS_OPT,
2358 .name = "component-flags",
2359 .has_arg = required_argument },
2360 { .val = LFS_COMP_SET_OPT,
2361 .name = "comp-set", .has_arg = no_argument },
2362 { .val = LFS_COMP_SET_OPT,
2363 .name = "component-set",
2364 .has_arg = no_argument},
2365 { .val = LFS_COMP_USE_PARENT_OPT,
2366 .name = "parent", .has_arg = no_argument},
2367 { .val = LFS_COMP_NO_VERIFY_OPT,
2368 .name = "no-verify", .has_arg = no_argument},
2369 { .val = LFS_MIRROR_FLAGS_OPT,
2370 .name = "flags", .has_arg = required_argument},
2371 { .val = LFS_MIRROR_ID_OPT,
2372 .name = "mirror-id", .has_arg = required_argument},
2373 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2374 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2375 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2376 { .val = 'd', .name = "delete", .has_arg = no_argument},
2377 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2378 /* --non-direct is only valid in migrate mode */
2379 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2380 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2381 { .val = 'E', .name = "component-end",
2382 .has_arg = required_argument},
2383 { .val = 'f', .name = "file", .has_arg = required_argument },
2384 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2385 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2386 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2387 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
2388 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2389 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2390 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2391 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2392 { .val = 'L', .name = "layout", .has_arg = required_argument },
2393 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2394 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2395 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2396 /* --non-block is only valid in migrate mode */
2397 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2398 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2399 { .val = 'o', .name = "ost", .has_arg = required_argument },
2400 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2401 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2402 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2404 { .val = 'p', .name = "pool", .has_arg = required_argument },
2405 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2406 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2407 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2408 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2409 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2410 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2411 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2412 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2413 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2414 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2415 /* --verbose is only valid in migrate mode */
2416 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2417 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
2420 setstripe_args_init(&lsa);
2422 migrate_mode = (opc == SO_MIGRATE);
2423 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2425 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2427 while ((c = getopt_long(argc, argv, "bc:dDE:f:i:I:m:N::no:p:L:s:S:v",
2428 long_opts, NULL)) >= 0) {
2433 case LFS_COMP_ADD_OPT:
2436 case LFS_COMP_DEL_OPT:
2439 case LFS_COMP_FLAGS_OPT:
2440 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2441 &lsa.lsa_comp_neg_flags);
2444 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2445 fprintf(stderr, "%s: inverted flags are not supported\n",
2449 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2451 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2458 case LFS_COMP_SET_OPT:
2461 case LFS_COMP_USE_PARENT_OPT:
2463 fprintf(stderr, "error: %s: --parent must be "
2464 "specified with --mirror-count|-N "
2465 "option\n", progname);
2468 setstripe_args_init(&lsa);
2470 case LFS_COMP_NO_VERIFY_OPT:
2471 mirror_flags |= MF_NO_VERIFY;
2473 case LFS_MIRROR_ID_OPT:
2474 mirror_id = strtoul(optarg, &end, 0);
2475 if (*end != '\0' || mirror_id == 0) {
2477 "%s %s: invalid mirror ID '%s'\n",
2478 progname, argv[0], optarg);
2482 case LFS_MIRROR_FLAGS_OPT: {
2485 if (!mirror_mode || !last_mirror) {
2486 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2491 result = comp_str2flags(optarg, &last_mirror->m_flags,
2497 fprintf(stderr, "%s: inverted flags are not supported\n",
2502 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2504 "%s: unsupported mirror flags: %s\n",
2512 if (!migrate_mode) {
2514 "%s %s: -b|--block valid only for migrate command\n",
2518 migration_block = true;
2521 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2524 "%s %s: invalid stripe count '%s'\n",
2525 progname, argv[0], optarg);
2529 if (lsa.lsa_stripe_count == -1)
2530 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2533 /* delete the default striping pattern */
2535 if (opc == SO_MIRROR_SPLIT) {
2538 "%s %s: -d cannot used with -f\n",
2542 mirror_flags |= MF_DESTROY;
2546 if (!migrate_mode) {
2548 "%s %s: -D|--non-direct is valid "
2549 "only for migrate command\n",
2553 migration_flags |= MIGRATION_NONDIRECT;
2556 if (lsa.lsa_comp_end != 0) {
2557 result = comp_args_to_layout(lpp, &lsa);
2560 "%s %s: invalid layout\n",
2565 setstripe_args_init_inherit(&lsa);
2568 if (arg_is_eof(optarg)) {
2569 lsa.lsa_comp_end = LUSTRE_EOF;
2571 result = llapi_parse_size(optarg,
2576 "%s %s: invalid component end '%s'\n",
2577 progname, argv[0], optarg);
2583 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2586 "%s %s: invalid stripe offset '%s'\n",
2587 progname, argv[0], optarg);
2590 if (lsa.lsa_stripe_off == -1)
2591 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2594 comp_id = strtoul(optarg, &end, 0);
2595 if (*end != '\0' || comp_id == 0 ||
2596 comp_id > LCME_ID_MAX) {
2598 "%s %s: invalid component ID '%s'\n",
2599 progname, argv[0], optarg);
2604 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2606 "error: %s: invalid option: %s\n",
2607 progname, argv[optopt + 1]);
2610 if (opc == SO_MIRROR_EXTEND) {
2611 if (last_mirror == NULL) {
2613 "error: %s: '-N' must exist in front of '%s'\n",
2614 progname, argv[optopt + 1]);
2617 last_mirror->m_file = optarg;
2618 last_mirror->m_count = 1;
2621 if (mirror_list == NULL)
2622 mirror_list = lfs_mirror_alloc();
2623 mirror_list->m_file = optarg;
2628 if (strcmp(argv[optind - 1], "mdt") == 0) {
2629 /* Can be only the first component */
2630 if (layout != NULL) {
2632 fprintf(stderr, "error: 'mdt' layout "
2633 "can be only the first one\n");
2636 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2638 fprintf(stderr, "error: 'mdt' layout "
2639 "size is too big\n");
2642 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2643 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2645 fprintf(stderr, "error: layout '%s' is "
2646 "unknown, supported layouts are: "
2647 "'mdt', 'raid0'\n", argv[optind]);
2652 if (!migrate_mode) {
2654 "%s %s: -m|--mdt-index valid only for migrate command\n",
2658 mdt_idx_arg = optarg;
2661 if (!migrate_mode) {
2663 "%s %s: -n|--non-block valid only for migrate command\n",
2667 migration_flags |= MIGRATION_NONBLOCK;
2670 if (opc == SO_SETSTRIPE) {
2671 opc = SO_MIRROR_CREATE;
2675 if (optarg != NULL) {
2676 mirror_count = strtoul(optarg, &end, 0);
2677 if (*end != '\0' || mirror_count == 0) {
2679 "error: %s: bad mirror count: %s\n",
2686 new_mirror = lfs_mirror_alloc();
2687 new_mirror->m_count = mirror_count;
2689 if (mirror_list == NULL)
2690 mirror_list = new_mirror;
2692 if (last_mirror != NULL) {
2693 /* wrap up last mirror */
2694 if (lsa.lsa_comp_end == 0)
2695 lsa.lsa_comp_end = LUSTRE_EOF;
2697 result = comp_args_to_layout(lpp, &lsa);
2699 lfs_mirror_free(new_mirror);
2703 setstripe_args_init_inherit(&lsa);
2705 last_mirror->m_next = new_mirror;
2708 last_mirror = new_mirror;
2709 lpp = &last_mirror->m_layout;
2712 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2713 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2714 fprintf(stderr, "warning: '--ost-list' is "
2715 "deprecated, use '--ost' instead\n");
2717 lsa.lsa_nr_tgts = parse_targets(osts,
2718 sizeof(osts) / sizeof(__u32),
2719 lsa.lsa_nr_tgts, optarg);
2720 if (lsa.lsa_nr_tgts < 0) {
2722 "%s %s: invalid OST target(s) '%s'\n",
2723 progname, argv[0], optarg);
2727 lsa.lsa_tgts = osts;
2728 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2729 lsa.lsa_stripe_off = osts[0];
2734 lsa.lsa_pool_name = optarg;
2737 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2741 "%s %s: invalid stripe size '%s'\n",
2742 progname, argv[0], optarg);
2747 if (!migrate_mode) {
2749 "%s %s: -v|--verbose valid only for migrate command\n",
2753 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2756 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2757 progname, argv[0], argv[optind - 1]);
2762 fname = argv[optind];
2764 if (optind == argc) {
2765 fprintf(stderr, "%s %s: FILE must be specified\n",
2770 if (mirror_mode && mirror_count == 0) {
2772 "error: %s: --mirror-count|-N option is required\n",
2779 if (lsa.lsa_comp_end == 0)
2780 lsa.lsa_comp_end = LUSTRE_EOF;
2783 if (lsa.lsa_comp_end != 0) {
2784 result = comp_args_to_layout(lpp, &lsa);
2789 if (mirror_flags & MF_NO_VERIFY) {
2790 if (opc != SO_MIRROR_EXTEND) {
2792 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2796 } else if (!has_m_file) {
2798 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2805 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2806 * altered by user space tool, so we don't need to support the
2807 * --component-set for this moment. */
2808 if (comp_set && !comp_id) {
2809 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2814 if ((delete + comp_set + comp_del + comp_add) > 1) {
2816 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2821 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2822 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2824 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2829 if ((comp_set || comp_del) &&
2830 (setstripe_args_specified(&lsa) || layout != NULL)) {
2832 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
2837 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
2839 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
2844 if (comp_add || comp_del) {
2847 result = lstat(fname, &st);
2848 if (result == 0 && S_ISDIR(st.st_mode)) {
2850 "%s setstripe: cannot use --component-add or --component-del for directory\n",
2856 fprintf(stderr, "error: %s: can't use --component-add "
2857 "or --component-del for mirror operation\n",
2864 if (layout == NULL) {
2866 "%s %s: option -E must be specified with --component-add\n",
2871 result = adjust_first_extent(fname, layout);
2872 if (result == -ENODATA)
2874 else if (result != 0)
2878 if (mdt_idx_arg != NULL && optind > 3) {
2880 "%s %s: option -m cannot be used with other options\n",
2885 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
2887 "%s %s: options --non-block and --block are mutually exclusive\n",
2892 if (!comp_del && !comp_set && comp_id != 0) {
2894 "%s %s: option -I can only be used with --component-del\n",
2899 if (mdt_idx_arg != NULL) {
2900 /* initialize migrate mdt parameters */
2901 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
2903 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
2904 progname, argv[0], mdt_idx_arg);
2907 migrate_mdt_param.fp_migrate = 1;
2908 } else if (layout == NULL) {
2909 /* initialize stripe parameters */
2910 param = calloc(1, offsetof(typeof(*param),
2911 lsp_osts[lsa.lsa_nr_tgts]));
2912 if (param == NULL) {
2914 "%s %s: cannot allocate memory for parameters: %s\n",
2915 progname, argv[0], strerror(ENOMEM));
2920 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
2921 param->lsp_stripe_size = lsa.lsa_stripe_size;
2922 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2923 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
2924 param->lsp_stripe_count = -1;
2926 param->lsp_stripe_count = lsa.lsa_stripe_count;
2928 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2929 param->lsp_stripe_offset = -1;
2931 param->lsp_stripe_offset = lsa.lsa_stripe_off;
2932 param->lsp_pool = lsa.lsa_pool_name;
2933 param->lsp_is_specific = false;
2934 if (lsa.lsa_nr_tgts > 0) {
2935 if (lsa.lsa_stripe_count > 0 &&
2936 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2937 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2938 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
2939 fprintf(stderr, "error: %s: stripe count %lld "
2940 "doesn't match the number of OSTs: %d\n"
2941 , argv[0], lsa.lsa_stripe_count,
2947 param->lsp_is_specific = true;
2948 param->lsp_stripe_count = lsa.lsa_nr_tgts;
2949 memcpy(param->lsp_osts, osts,
2950 sizeof(*osts) * lsa.lsa_nr_tgts);
2954 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
2955 if (mdt_idx_arg != NULL) {
2956 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
2957 } else if (migrate_mode) {
2958 result = lfs_migrate(fname, migration_flags, param,
2960 } else if (comp_set != 0) {
2961 result = lfs_component_set(fname, comp_id,
2963 lsa.lsa_comp_neg_flags);
2964 } else if (comp_del != 0) {
2965 result = lfs_component_del(fname, comp_id,
2967 lsa.lsa_comp_neg_flags);
2968 } else if (comp_add != 0) {
2969 result = lfs_component_add(fname, layout);
2970 } else if (opc == SO_MIRROR_CREATE) {
2971 result = mirror_create(fname, mirror_list);
2972 } else if (opc == SO_MIRROR_EXTEND) {
2973 result = mirror_extend(fname, mirror_list,
2975 } else if (opc == SO_MIRROR_SPLIT) {
2976 if (mirror_id == 0) {
2978 "%s %s: no mirror id is specified\n",
2982 result = mirror_split(fname, mirror_id, mirror_flags,
2983 has_m_file ? mirror_list->m_file :
2985 } else if (layout != NULL) {
2986 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
2993 result = llapi_file_open_param(fname,
3002 /* Save the first error encountered. */
3010 llapi_layout_free(layout);
3011 lfs_mirror_list_free(mirror_list);
3016 llapi_layout_free(layout);
3017 lfs_mirror_list_free(mirror_list);
3021 static int lfs_poollist(int argc, char **argv)
3026 return llapi_poollist(argv[1]);
3029 static int set_time(time_t *time, time_t *set, char *str)
3036 else if (str[0] == '-')
3042 t = strtol(str, NULL, 0);
3043 if (*time < t * 24 * 60 * 60) {
3046 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
3050 *set = *time - t * 24 * 60 * 60;
3053 static int name2uid(unsigned int *id, const char *name)
3055 struct passwd *passwd;
3057 passwd = getpwnam(name);
3060 *id = passwd->pw_uid;
3065 static int name2gid(unsigned int *id, const char *name)
3067 struct group *group;
3069 group = getgrnam(name);
3072 *id = group->gr_gid;
3077 static inline int name2projid(unsigned int *id, const char *name)
3082 static int uid2name(char **name, unsigned int id)
3084 struct passwd *passwd;
3086 passwd = getpwuid(id);
3089 *name = passwd->pw_name;
3094 static inline int gid2name(char **name, unsigned int id)
3096 struct group *group;
3098 group = getgrgid(id);
3101 *name = group->gr_name;
3106 static int name2layout(__u32 *layout, char *name)
3108 char *ptr, *layout_name;
3111 for (ptr = name; ; ptr = NULL) {
3112 layout_name = strtok(ptr, ",");
3113 if (layout_name == NULL)
3115 if (strcmp(layout_name, "released") == 0)
3116 *layout |= LOV_PATTERN_F_RELEASED;
3117 else if (strcmp(layout_name, "raid0") == 0)
3118 *layout |= LOV_PATTERN_RAID0;
3119 else if (strcmp(layout_name, "mdt") == 0)
3120 *layout |= LOV_PATTERN_MDT;
3127 static int lfs_find(int argc, char **argv)
3132 struct find_param param = {
3136 struct option long_opts[] = {
3137 { .val = 'A', .name = "atime", .has_arg = required_argument },
3138 { .val = LFS_COMP_COUNT_OPT,
3139 .name = "comp-count", .has_arg = required_argument },
3140 { .val = LFS_COMP_COUNT_OPT,
3141 .name = "component-count",
3142 .has_arg = required_argument },
3143 { .val = LFS_COMP_FLAGS_OPT,
3144 .name = "comp-flags", .has_arg = required_argument },
3145 { .val = LFS_COMP_FLAGS_OPT,
3146 .name = "component-flags",
3147 .has_arg = required_argument },
3148 { .val = LFS_COMP_START_OPT,
3149 .name = "comp-start", .has_arg = required_argument },
3150 { .val = LFS_COMP_START_OPT,
3151 .name = "component-start",
3152 .has_arg = required_argument },
3153 { .val = LFS_MIRROR_STATE_OPT,
3154 .name = "mirror-state", .has_arg = required_argument },
3155 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3156 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3157 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3158 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3159 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3160 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3161 { .val = 'E', .name = "component-end",
3162 .has_arg = required_argument },
3163 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3164 { .val = 'g', .name = "gid", .has_arg = required_argument },
3165 { .val = 'G', .name = "group", .has_arg = required_argument },
3166 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3167 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3168 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3169 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3170 { .val = 'L', .name = "layout", .has_arg = required_argument },
3171 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3172 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3173 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3174 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3175 { .val = 'n', .name = "name", .has_arg = required_argument },
3176 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3177 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3178 { .val = 'O', .name = "obd", .has_arg = required_argument },
3179 { .val = 'O', .name = "ost", .has_arg = required_argument },
3180 /* no short option for pool yet, can be 'p' after 2.18 */
3181 { .val = LFS_POOL_OPT,
3182 .name = "pool", .has_arg = required_argument },
3183 { .val = '0', .name = "print0", .has_arg = no_argument },
3184 { .val = 'P', .name = "print", .has_arg = no_argument },
3185 { .val = LFS_PROJID_OPT,
3186 .name = "projid", .has_arg = required_argument },
3187 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3188 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3189 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3190 { .val = 's', .name = "size", .has_arg = required_argument },
3191 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3192 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3193 { .val = 't', .name = "type", .has_arg = required_argument },
3194 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3195 { .val = 'u', .name = "uid", .has_arg = required_argument },
3196 { .val = 'U', .name = "user", .has_arg = required_argument },
3197 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3198 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3210 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3211 while ((c = getopt_long_only(argc, argv,
3212 "-0A:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3213 long_opts, NULL)) >= 0) {
3218 /* '!' is part of option */
3219 /* when getopt_long_only() finds a string which is not
3220 * an option nor a known option argument it returns 1
3221 * in that case if we already have found pathstart and pathend
3222 * (i.e. we have the list of pathnames),
3223 * the only supported value is "!"
3225 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3226 if (!isoption && pathend != -1) {
3227 fprintf(stderr, "err: %s: filename|dirname must either "
3228 "precede options or follow options\n",
3233 if (!isoption && pathstart == -1)
3234 pathstart = optind - 1;
3235 if (isoption && pathstart != -1 && pathend == -1)
3236 pathend = optind - 2;
3242 /* unknown; opt is "!" or path component,
3243 * checking done above.
3245 if (strcmp(optarg, "!") == 0)
3249 xtime = ¶m.fp_atime;
3250 xsign = ¶m.fp_asign;
3251 param.fp_exclude_atime = !!neg_opt;
3252 /* no break, this falls through to 'C' for ctime */
3255 xtime = ¶m.fp_ctime;
3256 xsign = ¶m.fp_csign;
3257 param.fp_exclude_ctime = !!neg_opt;
3259 /* no break, this falls through to 'M' for mtime */
3262 xtime = ¶m.fp_mtime;
3263 xsign = ¶m.fp_msign;
3264 param.fp_exclude_mtime = !!neg_opt;
3266 rc = set_time(&t, xtime, optarg);
3267 if (rc == INT_MAX) {
3274 case LFS_COMP_COUNT_OPT:
3275 if (optarg[0] == '+') {
3276 param.fp_comp_count_sign = -1;
3278 } else if (optarg[0] == '-') {
3279 param.fp_comp_count_sign = 1;
3283 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3284 if (*endptr != '\0') {
3285 fprintf(stderr, "error: bad component count "
3289 param.fp_check_comp_count = 1;
3290 param.fp_exclude_comp_count = !!neg_opt;
3292 case LFS_COMP_FLAGS_OPT:
3293 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3294 ¶m.fp_comp_neg_flags);
3296 fprintf(stderr, "error: bad component flags "
3300 param.fp_check_comp_flags = 1;
3302 __u32 flags = param.fp_comp_neg_flags;
3303 param.fp_comp_neg_flags = param.fp_comp_flags;
3304 param.fp_comp_flags = flags;
3307 case LFS_COMP_START_OPT:
3308 if (optarg[0] == '+') {
3309 param.fp_comp_start_sign = -1;
3311 } else if (optarg[0] == '-') {
3312 param.fp_comp_start_sign = 1;
3316 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3317 ¶m.fp_comp_start_units, 0);
3319 fprintf(stderr, "error: bad component start "
3323 param.fp_check_comp_start = 1;
3324 param.fp_exclude_comp_start = !!neg_opt;
3326 case LFS_MIRROR_STATE_OPT:
3327 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3328 ¶m.fp_mirror_neg_state);
3331 "error: bad mirrored file state '%s'\n",
3335 param.fp_check_mirror_state = 1;
3337 __u16 state = param.fp_mirror_neg_state;
3338 param.fp_mirror_neg_state =
3339 param.fp_mirror_state;
3340 param.fp_mirror_state = state;
3344 if (optarg[0] == '+') {
3345 param.fp_stripe_count_sign = -1;
3347 } else if (optarg[0] == '-') {
3348 param.fp_stripe_count_sign = 1;
3352 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3353 if (*endptr != '\0') {
3354 fprintf(stderr,"error: bad stripe_count '%s'\n",
3359 param.fp_check_stripe_count = 1;
3360 param.fp_exclude_stripe_count = !!neg_opt;
3363 param.fp_max_depth = strtol(optarg, 0, 0);
3366 if (optarg[0] == '+') {
3367 param.fp_comp_end_sign = -1;
3369 } else if (optarg[0] == '-') {
3370 param.fp_comp_end_sign = 1;
3374 if (arg_is_eof(optarg)) {
3375 param.fp_comp_end = LUSTRE_EOF;
3376 param.fp_comp_end_units = 1;
3379 rc = llapi_parse_size(optarg,
3381 ¶m.fp_comp_end_units, 0);
3384 fprintf(stderr, "error: bad component end "
3388 param.fp_check_comp_end = 1;
3389 param.fp_exclude_comp_end = !!neg_opt;
3393 rc = name2gid(¶m.fp_gid, optarg);
3395 param.fp_gid = strtoul(optarg, &endptr, 10);
3396 if (*endptr != '\0') {
3397 fprintf(stderr, "Group/GID: %s cannot "
3398 "be found.\n", optarg);
3403 param.fp_exclude_gid = !!neg_opt;
3404 param.fp_check_gid = 1;
3407 param.fp_hash_type = check_hashtype(optarg);
3408 if (param.fp_hash_type == 0) {
3409 fprintf(stderr, "error: bad hash_type '%s'\n",
3414 param.fp_check_hash_type = 1;
3415 param.fp_exclude_hash_type = !!neg_opt;
3418 ret = name2layout(¶m.fp_layout, optarg);
3421 param.fp_exclude_layout = !!neg_opt;
3422 param.fp_check_layout = 1;
3426 rc = name2uid(¶m.fp_uid, optarg);
3428 param.fp_uid = strtoul(optarg, &endptr, 10);
3429 if (*endptr != '\0') {
3430 fprintf(stderr, "User/UID: %s cannot "
3431 "be found.\n", optarg);
3436 param.fp_exclude_uid = !!neg_opt;
3437 param.fp_check_uid = 1;
3440 param.fp_pattern = (char *)optarg;
3441 param.fp_exclude_pattern = !!neg_opt;
3444 if (optarg[0] == '+') {
3445 param.fp_mirror_count_sign = -1;
3447 } else if (optarg[0] == '-') {
3448 param.fp_mirror_count_sign = 1;
3452 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3453 if (*endptr != '\0') {
3455 "error: bad mirror count '%s'\n",
3459 param.fp_check_mirror_count = 1;
3460 param.fp_exclude_mirror_count = !!neg_opt;
3465 char *buf, *token, *next, *p;
3469 buf = strdup(optarg);
3475 param.fp_exclude_obd = !!neg_opt;
3478 while (token && *token) {
3479 token = strchr(token, ',');
3486 param.fp_exclude_mdt = !!neg_opt;
3487 param.fp_num_alloc_mdts += len;
3488 tmp = realloc(param.fp_mdt_uuid,
3489 param.fp_num_alloc_mdts *
3490 sizeof(*param.fp_mdt_uuid));
3496 param.fp_mdt_uuid = tmp;
3498 param.fp_exclude_obd = !!neg_opt;
3499 param.fp_num_alloc_obds += len;
3500 tmp = realloc(param.fp_obd_uuid,
3501 param.fp_num_alloc_obds *
3502 sizeof(*param.fp_obd_uuid));
3508 param.fp_obd_uuid = tmp;
3510 for (token = buf; token && *token; token = next) {
3511 struct obd_uuid *puuid;
3514 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3517 ¶m.fp_obd_uuid[param.fp_num_obds++];
3519 p = strchr(token, ',');
3526 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3531 strncpy(puuid->uuid, token,
3532 sizeof(puuid->uuid));
3539 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3543 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3545 "Pool name %s is too long (max %d)\n",
3546 optarg, LOV_MAXPOOLNAME);
3551 * We do check for empty pool because empty pool
3552 * is used to find V1 LOV attributes
3554 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3555 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3556 param.fp_exclude_pool = !!neg_opt;
3557 param.fp_check_pool = 1;
3559 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3560 case 'p': /* want this for --pool, to match getstripe/find */
3562 "warning: -p deprecated, use --print0 or -0\n");
3565 param.fp_zero_end = 1;
3567 case 'P': /* we always print, this option is a no-op */
3569 case LFS_PROJID_OPT:
3570 rc = name2projid(¶m.fp_projid, optarg);
3572 param.fp_projid = strtoul(optarg, &endptr, 10);
3573 if (*endptr != '\0') {
3575 "Invalid project ID: %s",
3581 param.fp_exclude_projid = !!neg_opt;
3582 param.fp_check_projid = 1;
3585 if (optarg[0] == '+') {
3586 param.fp_size_sign = -1;
3588 } else if (optarg[0] == '-') {
3589 param.fp_size_sign = 1;
3593 ret = llapi_parse_size(optarg, ¶m.fp_size,
3594 ¶m.fp_size_units, 0);
3596 fprintf(stderr, "error: bad file size '%s'\n",
3600 param.fp_check_size = 1;
3601 param.fp_exclude_size = !!neg_opt;
3604 if (optarg[0] == '+') {
3605 param.fp_stripe_size_sign = -1;
3607 } else if (optarg[0] == '-') {
3608 param.fp_stripe_size_sign = 1;
3612 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3613 ¶m.fp_stripe_size_units, 0);
3615 fprintf(stderr, "error: bad stripe_size '%s'\n",
3619 param.fp_check_stripe_size = 1;
3620 param.fp_exclude_stripe_size = !!neg_opt;
3623 param.fp_exclude_type = !!neg_opt;
3624 switch (optarg[0]) {
3626 param.fp_type = S_IFBLK;
3629 param.fp_type = S_IFCHR;
3632 param.fp_type = S_IFDIR;
3635 param.fp_type = S_IFREG;
3638 param.fp_type = S_IFLNK;
3641 param.fp_type = S_IFIFO;
3644 param.fp_type = S_IFSOCK;
3647 fprintf(stderr, "error: %s: bad type '%s'\n",
3654 if (optarg[0] == '+') {
3655 param.fp_mdt_count_sign = -1;
3657 } else if (optarg[0] == '-') {
3658 param.fp_mdt_count_sign = 1;
3662 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3663 if (*endptr != '\0') {
3664 fprintf(stderr, "error: bad mdt_count '%s'\n",
3669 param.fp_check_mdt_count = 1;
3670 param.fp_exclude_mdt_count = !!neg_opt;
3678 if (pathstart == -1) {
3679 fprintf(stderr, "error: %s: no filename|pathname\n",
3683 } else if (pathend == -1) {
3689 rc = llapi_find(argv[pathstart], ¶m);
3690 if (rc != 0 && ret == 0)
3692 } while (++pathstart < pathend);
3695 fprintf(stderr, "error: %s failed for %s.\n",
3696 argv[0], argv[optind - 1]);
3698 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3699 free(param.fp_obd_uuid);
3701 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3702 free(param.fp_mdt_uuid);
3707 static int lfs_getstripe_internal(int argc, char **argv,
3708 struct find_param *param)
3710 struct option long_opts[] = {
3711 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3712 { .val = LFS_COMP_COUNT_OPT,
3713 .name = "comp-count", .has_arg = no_argument },
3714 { .val = LFS_COMP_COUNT_OPT,
3715 .name = "component-count", .has_arg = no_argument },
3716 { .val = LFS_COMP_FLAGS_OPT,
3717 .name = "comp-flags", .has_arg = optional_argument },
3718 { .val = LFS_COMP_FLAGS_OPT,
3719 .name = "component-flags", .has_arg = optional_argument },
3720 { .val = LFS_COMP_START_OPT,
3721 .name = "comp-start", .has_arg = optional_argument },
3722 { .val = LFS_COMP_START_OPT,
3723 .name = "component-start", .has_arg = optional_argument },
3724 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3725 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3726 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
3727 { .val = 'd', .name = "directory", .has_arg = no_argument },
3728 { .val = 'D', .name = "default", .has_arg = no_argument },
3729 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3730 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
3731 { .val = 'F', .name = "fid", .has_arg = no_argument },
3732 { .val = 'g', .name = "generation", .has_arg = no_argument },
3733 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3734 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
3735 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3736 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3737 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3738 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3739 { .val = 'L', .name = "layout", .has_arg = no_argument },
3740 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3741 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3742 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3743 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
3744 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
3745 { .val = 'O', .name = "obd", .has_arg = required_argument },
3746 { .val = 'O', .name = "ost", .has_arg = required_argument },
3747 { .val = 'p', .name = "pool", .has_arg = no_argument },
3748 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3749 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3750 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3751 { .val = 'R', .name = "raw", .has_arg = no_argument },
3752 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3753 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3754 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3755 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3756 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3757 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3758 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3759 { .val = 'y', .name = "yaml", .has_arg = no_argument },
3764 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
3765 long_opts, NULL)) != -1) {
3768 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3769 param->fp_verbose |= VERBOSE_COUNT;
3770 param->fp_max_depth = 0;
3773 case LFS_COMP_COUNT_OPT:
3774 param->fp_verbose |= VERBOSE_COMP_COUNT;
3775 param->fp_max_depth = 0;
3777 case LFS_COMP_FLAGS_OPT:
3778 if (optarg != NULL) {
3779 rc = comp_str2flags(optarg,
3780 ¶m->fp_comp_flags,
3781 ¶m->fp_comp_neg_flags);
3783 fprintf(stderr, "error: %s bad "
3784 "component flags '%s'.\n",
3788 param->fp_check_comp_flags = 1;
3790 param->fp_verbose |= VERBOSE_COMP_FLAGS;
3791 param->fp_max_depth = 0;
3794 case LFS_COMP_START_OPT:
3795 if (optarg != NULL) {
3797 if (tmp[0] == '+') {
3798 param->fp_comp_start_sign = -1;
3800 } else if (tmp[0] == '-') {
3801 param->fp_comp_start_sign = 1;
3804 rc = llapi_parse_size(tmp,
3805 ¶m->fp_comp_start,
3806 ¶m->fp_comp_start_units, 0);
3808 fprintf(stderr, "error: %s bad "
3809 "component start '%s'.\n",
3813 param->fp_check_comp_start = 1;
3816 param->fp_verbose |= VERBOSE_COMP_START;
3817 param->fp_max_depth = 0;
3821 param->fp_max_depth = 0;
3824 param->fp_get_default_lmv = 1;
3827 if (optarg != NULL) {
3829 if (tmp[0] == '+') {
3830 param->fp_comp_end_sign = -1;
3832 } else if (tmp[0] == '-') {
3833 param->fp_comp_end_sign = 1;
3837 if (arg_is_eof(tmp)) {
3838 param->fp_comp_end = LUSTRE_EOF;
3839 param->fp_comp_end_units = 1;
3842 rc = llapi_parse_size(tmp,
3843 ¶m->fp_comp_end,
3844 ¶m->fp_comp_end_units, 0);
3847 fprintf(stderr, "error: %s bad "
3848 "component end '%s'.\n",
3852 param->fp_check_comp_end = 1;
3854 param->fp_verbose |= VERBOSE_COMP_END;
3855 param->fp_max_depth = 0;
3859 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3860 param->fp_verbose |= VERBOSE_DFID;
3861 param->fp_max_depth = 0;
3865 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3866 param->fp_verbose |= VERBOSE_GENERATION;
3867 param->fp_max_depth = 0;
3871 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3872 param->fp_verbose |= VERBOSE_OFFSET;
3873 param->fp_max_depth = 0;
3877 if (optarg != NULL) {
3878 param->fp_comp_id = strtoul(optarg, &end, 0);
3879 if (*end != '\0' || param->fp_comp_id == 0 ||
3880 param->fp_comp_id > LCME_ID_MAX) {
3881 fprintf(stderr, "error: %s bad "
3882 "component id '%s'\n",
3886 param->fp_check_comp_id = 1;
3889 param->fp_max_depth = 0;
3890 param->fp_verbose |= VERBOSE_COMP_ID;
3894 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3895 param->fp_verbose |= VERBOSE_LAYOUT;
3896 param->fp_max_depth = 0;
3899 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3901 fprintf(stderr, "warning: '-M' deprecated"
3902 ", use '--mdt-index' or '-m' instead\n");
3905 if (!(param->fp_verbose & VERBOSE_DETAIL))
3906 param->fp_max_depth = 0;
3907 param->fp_verbose |= VERBOSE_MDTINDEX;
3910 if (param->fp_obd_uuid) {
3912 "error: %s: only one obduuid allowed",
3916 param->fp_obd_uuid = (struct obd_uuid *)optarg;
3919 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3920 param->fp_verbose |= VERBOSE_POOL;
3921 param->fp_max_depth = 0;
3928 param->fp_recursive = 1;
3934 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
3935 param->fp_verbose |= VERBOSE_SIZE;
3936 param->fp_max_depth = 0;
3940 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
3953 if (param->fp_recursive)
3954 param->fp_max_depth = -1;
3955 else if (param->fp_verbose & VERBOSE_DETAIL)
3956 param->fp_max_depth = 1;
3958 if (!param->fp_verbose)
3959 param->fp_verbose = VERBOSE_DEFAULT;
3960 if (param->fp_quiet)
3961 param->fp_verbose = VERBOSE_OBJID;
3964 rc = llapi_getstripe(argv[optind], param);
3965 } while (++optind < argc && !rc);
3968 fprintf(stderr, "error: %s failed for %s.\n",
3969 argv[0], argv[optind - 1]);
3973 static int lfs_tgts(int argc, char **argv)
3975 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
3976 struct find_param param;
3977 int index = 0, rc=0;
3982 if (argc == 2 && !realpath(argv[1], path)) {
3984 fprintf(stderr, "error: invalid path '%s': %s\n",
3985 argv[1], strerror(-rc));
3989 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
3990 /* Check if we have a mount point */
3991 if (mntdir[0] == '\0')
3994 memset(¶m, 0, sizeof(param));
3995 if (!strcmp(argv[0], "mdts"))
3996 param.fp_get_lmv = 1;
3998 rc = llapi_ostlist(mntdir, ¶m);
4000 fprintf(stderr, "error: %s: failed on %s\n",
4003 if (path[0] != '\0')
4005 memset(mntdir, 0, PATH_MAX);
4011 static int lfs_getstripe(int argc, char **argv)
4013 struct find_param param = { 0 };
4015 param.fp_max_depth = 1;
4016 return lfs_getstripe_internal(argc, argv, ¶m);
4020 static int lfs_getdirstripe(int argc, char **argv)
4022 struct find_param param = { 0 };
4023 struct option long_opts[] = {
4024 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4025 { .val = 'D', .name = "default", .has_arg = no_argument },
4026 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4027 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4028 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4029 { .val = 'O', .name = "obd", .has_arg = required_argument },
4030 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4031 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4032 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4036 param.fp_get_lmv = 1;
4038 while ((c = getopt_long(argc, argv,
4039 "cDHimO:rtTy", long_opts, NULL)) != -1)
4044 param.fp_verbose |= VERBOSE_COUNT;
4047 param.fp_get_default_lmv = 1;
4051 param.fp_verbose |= VERBOSE_OFFSET;
4053 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4055 fprintf(stderr, "warning: '-t' deprecated, "
4056 "use '--mdt-hash' or '-H' instead\n");
4059 param.fp_verbose |= VERBOSE_HASH_TYPE;
4062 if (param.fp_obd_uuid) {
4064 "error: %s: only one obduuid allowed",
4068 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4071 param.fp_recursive = 1;
4084 if (param.fp_recursive)
4085 param.fp_max_depth = -1;
4087 if (!param.fp_verbose)
4088 param.fp_verbose = VERBOSE_DEFAULT;
4091 rc = llapi_getstripe(argv[optind], ¶m);
4092 } while (++optind < argc && !rc);
4095 fprintf(stderr, "error: %s failed for %s.\n",
4096 argv[0], argv[optind - 1]);
4101 MNTDF_INODES = 0x0001,
4102 MNTDF_COOKED = 0x0002,
4103 MNTDF_LAZY = 0x0004,
4104 MNTDF_VERBOSE = 0x0008,
4105 MNTDF_SHOW = 0x0010,
4108 #define COOK(value) \
4111 while (value > 1024) { \
4119 #define CDF "%11llu"
4120 #define HDF "%8.1f%c"
4124 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4126 double avail, used, ratio = 0;
4128 avail = st->os_bavail;
4129 used = st->os_blocks - st->os_bfree;
4130 if (avail + used > 0)
4131 ratio = used / (used + avail) * 100 + 0.5;
4136 static int showdf(char *mntdir, struct obd_statfs *stat,
4137 char *uuid, enum mntdf_flags flags,
4138 char *type, int index, int rc)
4140 long long avail, used, total;
4142 char *suffix = "KMGTPEZY";
4143 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4144 char tbuf[3 * sizeof(__u64)];
4145 char ubuf[3 * sizeof(__u64)];
4146 char abuf[3 * sizeof(__u64)];
4147 char rbuf[3 * sizeof(__u64)];
4154 if (flags & MNTDF_INODES) {
4155 avail = stat->os_ffree;
4156 used = stat->os_files - stat->os_ffree;
4157 total = stat->os_files;
4159 int shift = flags & MNTDF_COOKED ? 0 : 10;
4161 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4162 used = ((stat->os_blocks - stat->os_bfree) *
4163 stat->os_bsize) >> shift;
4164 total = (stat->os_blocks * stat->os_bsize) >> shift;
4167 ratio = obd_statfs_ratio(stat);
4169 if (flags & MNTDF_COOKED) {
4173 cook_val = (double)total;
4176 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4179 snprintf(tbuf, sizeof(tbuf), CDF, total);
4181 cook_val = (double)used;
4184 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4187 snprintf(ubuf, sizeof(ubuf), CDF, used);
4189 cook_val = (double)avail;
4192 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4195 snprintf(abuf, sizeof(abuf), CDF, avail);
4197 snprintf(tbuf, sizeof(tbuf), CDF, total);
4198 snprintf(ubuf, sizeof(tbuf), CDF, used);
4199 snprintf(abuf, sizeof(tbuf), CDF, avail);
4202 sprintf(rbuf, RDF, ratio);
4203 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4204 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4206 printf("[%s:%d]", type, index);
4208 if (stat->os_state) {
4210 * Each character represents the matching
4213 const char state_names[] = "DRSI";
4218 for (i = 0, state = stat->os_state;
4219 state && i < sizeof(state_names); i++) {
4220 if (!(state & (1 << i)))
4222 printf("%c", state_names[i]);
4230 printf(UUF": inactive device\n", uuid);
4233 printf(UUF": %s\n", uuid, strerror(-rc));
4240 struct ll_stat_type {
4245 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4247 struct ll_statfs_data {
4249 struct obd_statfs sd_st;
4252 struct ll_statfs_buf {
4254 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4257 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4258 int ops, struct ll_statfs_buf *lsb)
4260 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4261 struct obd_uuid uuid_buf;
4262 char *poolname = NULL;
4263 struct ll_stat_type types[] = {
4264 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4265 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4266 { .st_name = NULL } };
4267 struct ll_stat_type *tp;
4268 __u64 ost_ffree = 0;
4276 poolname = strchr(pool, '.');
4277 if (poolname != NULL) {
4278 if (strncmp(fsname, pool, strlen(fsname))) {
4279 fprintf(stderr, "filesystem name incorrect\n");
4287 fd = open(mntdir, O_RDONLY);
4290 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4295 if (flags & MNTDF_SHOW) {
4296 if (flags & MNTDF_INODES)
4297 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4298 "UUID", "Inodes", "IUsed", "IFree",
4299 "IUse%", "Mounted on");
4301 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4303 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4304 "Used", "Available", "Use%", "Mounted on");
4307 for (tp = types; tp->st_name != NULL; tp++) {
4308 if (!(tp->st_op & ops))
4311 for (index = 0; ; index++) {
4312 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4313 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4314 type = flags & MNTDF_LAZY ?
4315 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4316 rc2 = llapi_obd_fstatfs(fd, type, index,
4317 &stat_buf, &uuid_buf);
4322 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4323 if (!(flags & MNTDF_VERBOSE))
4325 } else if (rc2 < 0 && rc == 0) {
4329 if (poolname && tp->st_op == LL_STATFS_LOV &&
4330 llapi_search_ost(fsname, poolname,
4331 obd_uuid2str(&uuid_buf)) != 1)
4334 /* the llapi_obd_statfs() call may have returned with
4335 * an error, but if it filled in uuid_buf we will at
4336 * lease use that to print out a message for that OBD.
4337 * If we didn't get anything in the uuid_buf, then fill
4338 * it in so that we can print an error message. */
4339 if (uuid_buf.uuid[0] == '\0')
4340 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4341 "%s%04x", tp->st_name, index);
4343 lsb->sb_buf[lsb->sb_count].sd_index = index;
4344 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4347 if (flags & MNTDF_SHOW)
4348 showdf(mntdir, &stat_buf,
4349 obd_uuid2str(&uuid_buf), flags,
4350 tp->st_name, index, rc2);
4353 if (tp->st_op == LL_STATFS_LMV) {
4354 sum.os_ffree += stat_buf.os_ffree;
4355 sum.os_files += stat_buf.os_files;
4356 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4357 sum.os_blocks += stat_buf.os_blocks *
4359 sum.os_bfree += stat_buf.os_bfree *
4361 sum.os_bavail += stat_buf.os_bavail *
4363 ost_ffree += stat_buf.os_ffree;
4371 /* If we don't have as many objects free on the OST as inodes
4372 * on the MDS, we reduce the total number of inodes to
4373 * compensate, so that the "inodes in use" number is correct.
4374 * Matches ll_statfs_internal() so the results are consistent. */
4375 if (ost_ffree < sum.os_ffree) {
4376 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4377 sum.os_ffree = ost_ffree;
4379 if (flags & MNTDF_SHOW) {
4381 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4388 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4390 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4392 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4394 int r1 = obd_statfs_ratio(st1);
4395 int r2 = obd_statfs_ratio(st2);
4396 int64_t result = r1 - r2;
4398 /* if both space usage are above 90, compare free inodes */
4399 if (r1 > 90 && r2 > 90)
4400 result = st2->os_ffree - st1->os_ffree;
4404 else if (result == 0)
4411 static int lfs_setdirstripe(int argc, char **argv)
4415 struct lfs_setstripe_args lsa;
4416 struct llapi_stripe_param *param = NULL;
4417 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4420 char *mode_opt = NULL;
4421 bool default_stripe = false;
4422 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4423 mode_t previous_mode = 0;
4424 bool delete = false;
4425 struct ll_statfs_buf *lsb = NULL;
4426 char mntdir[PATH_MAX] = "";
4427 bool auto_distributed = false;
4429 struct option long_opts[] = {
4430 { .val = 'c', .name = "count", .has_arg = required_argument },
4431 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4432 { .val = 'd', .name = "delete", .has_arg = no_argument },
4433 { .val = 'D', .name = "default", .has_arg = no_argument },
4434 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4435 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4436 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4437 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4438 { .val = 'i', .name = "index", .has_arg = required_argument },
4440 { .val = 'o', .name = "mode", .has_arg = required_argument },
4441 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4442 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4444 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4447 setstripe_args_init(&lsa);
4449 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4457 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4460 "%s %s: invalid stripe count '%s'\n",
4461 progname, argv[0], optarg);
4467 default_stripe = true;
4470 default_stripe = true;
4472 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4474 fprintf(stderr, "warning: '--hash-type' and '-t' "
4475 "deprecated, use '--mdt-hash' or '-H' instead\n");
4478 lsa.lsa_pattern = check_hashtype(optarg);
4479 if (lsa.lsa_pattern == 0) {
4481 "%s %s: bad stripe hash type '%s'\n",
4482 progname, argv[0], optarg);
4487 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4488 if (strcmp(argv[optind - 1], "--index") == 0)
4490 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4493 lsa.lsa_nr_tgts = parse_targets(mdts,
4494 sizeof(mdts) / sizeof(__u32),
4495 lsa.lsa_nr_tgts, optarg);
4496 if (lsa.lsa_nr_tgts < 0) {
4498 "%s %s: invalid MDT target(s) '%s'\n",
4499 progname, argv[0], optarg);
4503 lsa.lsa_tgts = mdts;
4504 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4505 lsa.lsa_stripe_off = mdts[0];
4507 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4509 fprintf(stderr, "warning: '-m' is deprecated, "
4510 "use '--mode' or '-o' instead\n");
4516 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4517 progname, argv[0], argv[optind - 1]);
4522 if (optind == argc) {
4523 fprintf(stderr, "%s %s: DIR must be specified\n",
4528 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
4529 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
4531 "%s %s: stripe offset and count must be specified\n",
4537 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
4538 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
4540 "%s %s: cannot specify -d with -c or -i options\n",
4545 if (mode_opt != NULL) {
4546 mode = strtoul(mode_opt, &end, 8);
4549 "%s %s: bad MODE '%s'\n",
4550 progname, argv[0], mode_opt);
4553 previous_mode = umask(0);
4557 * initialize stripe parameters, in case param is converted to specific,
4558 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
4560 param = calloc(1, offsetof(typeof(*param),
4561 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
4562 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
4563 if (param == NULL) {
4565 "%s %s: cannot allocate memory for parameters: %s\n",
4566 progname, argv[0], strerror(ENOMEM));
4570 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4571 param->lsp_stripe_count = lsa.lsa_stripe_count;
4572 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4573 param->lsp_stripe_offset = -1;
4575 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4576 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4577 param->lsp_stripe_pattern = lsa.lsa_pattern;
4579 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
4580 param->lsp_pool = lsa.lsa_pool_name;
4581 param->lsp_is_specific = false;
4582 if (lsa.lsa_nr_tgts > 1) {
4583 if (lsa.lsa_stripe_count > 0 &&
4584 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4585 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4586 fprintf(stderr, "error: %s: stripe count %lld doesn't "
4587 "match the number of MDTs: %d\n",
4588 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
4593 param->lsp_is_specific = true;
4594 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4595 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
4598 dname = argv[optind];
4600 if (default_stripe) {
4601 result = llapi_dir_set_default_lmv(dname, param);
4603 /* if current \a dname isn't under the same \a mntdir
4604 * as the last one, and the last one was
4605 * auto-distributed, restore \a param.
4607 if (mntdir[0] != '\0' &&
4608 strncmp(dname, mntdir, strlen(mntdir)) &&
4610 param->lsp_is_specific = false;
4611 param->lsp_stripe_offset = -1;
4612 auto_distributed = false;
4615 if (!param->lsp_is_specific &&
4616 param->lsp_stripe_offset == -1) {
4617 char path[PATH_MAX] = "";
4620 lsb = malloc(sizeof(*lsb));
4628 /* use mntdir for dirname() temporarily */
4629 strncpy(mntdir, dname, sizeof(mntdir));
4630 if (!realpath(dirname(mntdir), path)) {
4633 "error: invalid path '%s': %s\n",
4634 argv[optind], strerror(errno));
4639 result = llapi_search_mounts(path, 0, mntdir,
4641 if (result < 0 || mntdir[0] == '\0') {
4643 "No suitable Lustre mount found\n");
4647 result = mntdf(mntdir, NULL, NULL, 0,
4648 LL_STATFS_LMV, lsb);
4652 if (param->lsp_stripe_count > lsb->sb_count) {
4654 "error: stripe count %d is too big\n",
4655 param->lsp_stripe_count);
4660 qsort(lsb->sb_buf, lsb->sb_count,
4661 sizeof(struct ll_statfs_data),
4662 ll_statfs_data_comp);
4664 auto_distributed = true;
4667 if (auto_distributed) {
4669 int nr = MAX(param->lsp_stripe_count,
4672 /* don't use server whose usage is above 90% */
4673 while (nr != param->lsp_stripe_count &&
4674 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
4676 nr = MAX(param->lsp_stripe_count,
4679 /* get \a r between [0, nr) */
4682 param->lsp_stripe_offset =
4683 lsb->sb_buf[r].sd_index;
4684 if (param->lsp_stripe_count > 1) {
4687 param->lsp_is_specific = true;
4688 for (; i < param->lsp_stripe_count; i++)
4689 param->lsp_tgts[(i + r) % nr] =
4690 lsb->sb_buf[i].sd_index;
4694 result = llapi_dir_create(dname, mode, param);
4699 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
4700 progname, dname, strerror(-result));
4703 dname = argv[++optind];
4704 } while (dname != NULL);
4706 if (mode_opt != NULL)
4707 umask(previous_mode);
4715 static int lfs_rmentry(int argc, char **argv)
4722 fprintf(stderr, "error: %s: missing dirname\n",
4728 dname = argv[index];
4729 while (dname != NULL) {
4730 result = llapi_direntry_remove(dname);
4732 fprintf(stderr, "error: %s: remove dir entry '%s' "
4733 "failed\n", argv[0], dname);
4736 dname = argv[++index];
4741 static int lfs_mv(int argc, char **argv)
4743 struct find_param param = {
4750 struct option long_opts[] = {
4751 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4752 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4755 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
4757 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4759 fprintf(stderr, "warning: '-M' deprecated"
4760 ", use '--mdt-index' or '-m' instead\n");
4763 param.fp_mdt_index = strtoul(optarg, &end, 0);
4765 fprintf(stderr, "%s: invalid MDT index'%s'\n",
4771 param.fp_verbose = VERBOSE_DETAIL;
4774 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
4775 argv[0], argv[optind - 1]);
4780 if (param.fp_mdt_index == -1) {
4781 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
4785 if (optind >= argc) {
4786 fprintf(stderr, "%s: missing operand path\n", argv[0]);
4790 param.fp_migrate = 1;
4791 rc = llapi_migrate_mdt(argv[optind], ¶m);
4793 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
4794 argv[0], argv[optind], param.fp_mdt_index,
4799 static int lfs_osts(int argc, char **argv)
4801 return lfs_tgts(argc, argv);
4804 static int lfs_mdts(int argc, char **argv)
4806 return lfs_tgts(argc, argv);
4809 static int lfs_df(int argc, char **argv)
4811 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4812 enum mntdf_flags flags = MNTDF_SHOW;
4813 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
4814 int c, rc = 0, index = 0;
4815 char fsname[PATH_MAX] = "", *pool_name = NULL;
4816 struct option long_opts[] = {
4817 { .val = 'h', .name = "human-readable",
4818 .has_arg = no_argument },
4819 { .val = 'i', .name = "inodes", .has_arg = no_argument },
4820 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4821 { .val = 'p', .name = "pool", .has_arg = required_argument },
4822 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4825 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
4828 flags |= MNTDF_COOKED;
4831 flags |= MNTDF_INODES;
4834 flags |= MNTDF_LAZY;
4840 flags |= MNTDF_VERBOSE;
4846 if (optind < argc && !realpath(argv[optind], path)) {
4848 fprintf(stderr, "error: invalid path '%s': %s\n",
4849 argv[optind], strerror(-rc));
4853 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4854 /* Check if we have a mount point */
4855 if (mntdir[0] == '\0')
4858 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
4859 if (rc || path[0] != '\0')
4861 fsname[0] = '\0'; /* avoid matching in next loop */
4862 mntdir[0] = '\0'; /* avoid matching in next loop */
4868 static int lfs_getname(int argc, char **argv)
4870 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
4871 int rc = 0, index = 0, c;
4872 char buf[sizeof(struct obd_uuid)];
4874 while ((c = getopt(argc, argv, "h")) != -1)
4877 if (optind == argc) { /* no paths specified, get all paths. */
4878 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
4879 rc = llapi_getname(mntdir, buf, sizeof(buf));
4882 "cannot get name for `%s': %s\n",
4883 mntdir, strerror(-rc));
4887 printf("%s %s\n", buf, mntdir);
4889 path[0] = fsname[0] = mntdir[0] = 0;
4891 } else { /* paths specified, only attempt to search these. */
4892 for (; optind < argc; optind++) {
4893 rc = llapi_getname(argv[optind], buf, sizeof(buf));
4896 "cannot get name for `%s': %s\n",
4897 argv[optind], strerror(-rc));
4901 printf("%s %s\n", buf, argv[optind]);
4907 static int lfs_check(int argc, char **argv)
4910 char mntdir[PATH_MAX] = {'\0'};
4917 fprintf(stderr, "%s check: server type must be specified\n",
4922 obd_types[0] = obd_type1;
4923 obd_types[1] = obd_type2;
4925 if (strcmp(argv[1], "osts") == 0) {
4926 strcpy(obd_types[0], "osc");
4927 } else if (strcmp(argv[1], "mds") == 0) {
4928 strcpy(obd_types[0], "mdc");
4929 } else if (strcmp(argv[1], "servers") == 0) {
4931 strcpy(obd_types[0], "osc");
4932 strcpy(obd_types[1], "mdc");
4934 fprintf(stderr, "%s check: unrecognized option '%s'\n",
4939 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
4940 if (rc < 0 || mntdir[0] == '\0') {
4942 "%s check: cannot find mounted Lustre filesystem: %s\n",
4943 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
4947 rc = llapi_target_check(num_types, obd_types, mntdir);
4949 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
4950 progname, argv[1], strerror(-rc));
4956 #ifdef HAVE_SYS_QUOTA_H
4957 #define ARG2INT(nr, str, msg) \
4960 nr = strtol(str, &endp, 0); \
4961 if (*endp != '\0') { \
4962 fprintf(stderr, "%s: bad %s '%s'\n", \
4963 progname, msg, str); \
4968 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
4970 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
4971 * returns the value or ULONG_MAX on integer overflow or incorrect format
4973 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
4974 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
4975 * 3. empty integer value is interpreted as 0
4977 static unsigned long str2sec(const char* timestr)
4979 const char spec[] = "smhdw";
4980 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
4981 unsigned long val = 0;
4984 if (strpbrk(timestr, spec) == NULL) {
4985 /* no specifiers inside the time string,
4986 should treat it as an integer value */
4987 val = strtoul(timestr, &tail, 10);
4988 return *tail ? ULONG_MAX : val;
4991 /* format string is XXwXXdXXhXXmXXs */
4997 v = strtoul(timestr, &tail, 10);
4998 if (v == ULONG_MAX || *tail == '\0')
4999 /* value too large (ULONG_MAX or more)
5000 or missing specifier */
5003 ptr = strchr(spec, *tail);
5005 /* unknown specifier */
5010 /* check if product will overflow the type */
5011 if (!(v < ULONG_MAX / mult[ind]))
5014 ADD_OVERFLOW(val, mult[ind] * v);
5015 if (val == ULONG_MAX)
5027 #define ARG2ULL(nr, str, def_units) \
5029 unsigned long long limit, units = def_units; \
5032 rc = llapi_parse_size(str, &limit, &units, 1); \
5034 fprintf(stderr, "%s: invalid limit '%s'\n", \
5041 static inline int has_times_option(int argc, char **argv)
5045 for (i = 1; i < argc; i++)
5046 if (!strcmp(argv[i], "-t"))
5052 int lfs_setquota_times(int argc, char **argv)
5055 struct if_quotactl qctl;
5056 char *mnt, *obd_type = (char *)qctl.obd_type;
5057 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5058 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5059 struct option long_opts[] = {
5060 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5061 { .val = 'g', .name = "group", .has_arg = no_argument },
5062 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5063 { .val = 'p', .name = "projid", .has_arg = no_argument },
5064 { .val = 't', .name = "times", .has_arg = no_argument },
5065 { .val = 'u', .name = "user", .has_arg = no_argument },
5069 memset(&qctl, 0, sizeof(qctl));
5070 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5071 qctl.qc_type = ALLQUOTA;
5073 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5074 long_opts, NULL)) != -1) {
5085 if (qctl.qc_type != ALLQUOTA) {
5086 fprintf(stderr, "error: -u/g/p can't be used "
5087 "more than once\n");
5090 qctl.qc_type = qtype;
5093 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5094 fprintf(stderr, "error: bad block-grace: %s\n",
5098 dqb->dqb_valid |= QIF_BTIME;
5101 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5102 fprintf(stderr, "error: bad inode-grace: %s\n",
5106 dqb->dqb_valid |= QIF_ITIME;
5108 case 't': /* Yes, of course! */
5110 default: /* getopt prints error message for us when opterr != 0 */
5115 if (qctl.qc_type == ALLQUOTA) {
5116 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5120 if (optind != argc - 1) {
5121 fprintf(stderr, "error: unexpected parameters encountered\n");
5126 rc = llapi_quotactl(mnt, &qctl);
5129 fprintf(stderr, "%s %s ", obd_type,
5130 obd_uuid2str(&qctl.obd_uuid));
5131 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5138 #define BSLIMIT (1 << 0)
5139 #define BHLIMIT (1 << 1)
5140 #define ISLIMIT (1 << 2)
5141 #define IHLIMIT (1 << 3)
5143 int lfs_setquota(int argc, char **argv)
5146 struct if_quotactl qctl;
5147 char *mnt, *obd_type = (char *)qctl.obd_type;
5148 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5149 struct option long_opts[] = {
5150 { .val = 'b', .name = "block-softlimit",
5151 .has_arg = required_argument },
5152 { .val = 'B', .name = "block-hardlimit",
5153 .has_arg = required_argument },
5154 { .val = 'g', .name = "group", .has_arg = required_argument },
5155 { .val = 'i', .name = "inode-softlimit",
5156 .has_arg = required_argument },
5157 { .val = 'I', .name = "inode-hardlimit",
5158 .has_arg = required_argument },
5159 { .val = 'p', .name = "projid", .has_arg = required_argument },
5160 { .val = 'u', .name = "user", .has_arg = required_argument },
5162 unsigned limit_mask = 0;
5166 if (has_times_option(argc, argv))
5167 return lfs_setquota_times(argc, argv);
5169 memset(&qctl, 0, sizeof(qctl));
5170 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5171 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5172 * so it can be used as a marker that qc_type
5173 * isn't reinitialized from command line */
5175 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
5176 long_opts, NULL)) != -1) {
5180 rc = name2uid(&qctl.qc_id, optarg);
5184 rc = name2gid(&qctl.qc_id, optarg);
5188 rc = name2projid(&qctl.qc_id, optarg);
5190 if (qctl.qc_type != ALLQUOTA) {
5192 "%s setquota: only one of -u, -g or -p may be specified\n",
5196 qctl.qc_type = qtype;
5198 qctl.qc_id = strtoul(optarg, &endptr, 10);
5199 if (*endptr != '\0') {
5201 "%s setquota: invalid id '%s'\n",
5208 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5209 dqb->dqb_bsoftlimit >>= 10;
5210 limit_mask |= BSLIMIT;
5211 if (dqb->dqb_bsoftlimit &&
5212 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5214 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5215 "See '%s help setquota' or Lustre manual for details\n",
5216 progname, dqb->dqb_bsoftlimit,
5220 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5221 dqb->dqb_bhardlimit >>= 10;
5222 limit_mask |= BHLIMIT;
5223 if (dqb->dqb_bhardlimit &&
5224 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5226 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5227 "See '%s help setquota' or Lustre manual for details\n",
5228 progname, dqb->dqb_bhardlimit,
5232 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5233 limit_mask |= ISLIMIT;
5234 if (dqb->dqb_isoftlimit &&
5235 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5237 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5238 "See '%s help setquota' or Lustre manual for details\n",
5239 progname, dqb->dqb_isoftlimit,
5243 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5244 limit_mask |= IHLIMIT;
5245 if (dqb->dqb_ihardlimit &&
5246 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5248 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5249 "See '%s help setquota' or Lustre manual for details\n",
5250 progname, dqb->dqb_ihardlimit,
5255 "%s setquota: unrecognized option '%s'\n",
5256 progname, argv[optind - 1]);
5261 if (qctl.qc_type == ALLQUOTA) {
5263 "%s setquota: either -u or -g must be specified\n",
5268 if (limit_mask == 0) {
5270 "%s setquota: at least one limit must be specified\n",
5275 if (optind != argc - 1) {
5277 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5278 progname, argv[optind]);
5284 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5285 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5286 /* sigh, we can't just set blimits/ilimits */
5287 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5288 .qc_type = qctl.qc_type,
5289 .qc_id = qctl.qc_id};
5291 rc = llapi_quotactl(mnt, &tmp_qctl);
5295 if (!(limit_mask & BHLIMIT))
5296 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5297 if (!(limit_mask & BSLIMIT))
5298 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5299 if (!(limit_mask & IHLIMIT))
5300 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5301 if (!(limit_mask & ISLIMIT))
5302 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5304 /* Keep grace times if we have got no softlimit arguments */
5305 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5306 dqb->dqb_valid |= QIF_BTIME;
5307 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5310 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5311 dqb->dqb_valid |= QIF_ITIME;
5312 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5316 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5317 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5319 rc = llapi_quotactl(mnt, &qctl);
5323 "%s setquota: cannot quotactl '%s' '%s': %s",
5325 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5332 /* Converts seconds value into format string
5333 * result is returned in buf
5335 * 1. result is in descenting order: 1w2d3h4m5s
5336 * 2. zero fields are not filled (except for p. 3): 5d1s
5337 * 3. zero seconds value is presented as "0s"
5339 static char * __sec2str(time_t seconds, char *buf)
5341 const char spec[] = "smhdw";
5342 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5347 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5348 c = seconds / mult[i];
5350 if (c > 0 || (i == 0 && buf == tail))
5351 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5359 static void sec2str(time_t seconds, char *buf, int rc)
5366 tail = __sec2str(seconds, tail);
5368 if (rc && tail - buf < 39) {
5374 static void diff2str(time_t seconds, char *buf, time_t now)
5380 if (seconds <= now) {
5381 strcpy(buf, "none");
5384 __sec2str(seconds - now, buf);
5387 static void print_quota_title(char *name, struct if_quotactl *qctl,
5388 bool human_readable)
5390 printf("Disk quotas for %s %s (%cid %u):\n",
5391 qtype_name(qctl->qc_type), name,
5392 *qtype_name(qctl->qc_type), qctl->qc_id);
5393 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5394 "Filesystem", human_readable ? "used" : "kbytes",
5395 "quota", "limit", "grace",
5396 "files", "quota", "limit", "grace");
5399 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5402 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5405 snprintf(buf, buflen, "%5.4gP",
5406 (double)num / ((__u64)1 << 40));
5408 snprintf(buf, buflen, "%5.4gT",
5409 (double)num / (1 << 30));
5411 snprintf(buf, buflen, "%5.4gG",
5412 (double)num / (1 << 20));
5414 snprintf(buf, buflen, "%5.4gM",
5415 (double)num / (1 << 10));
5417 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5421 #define STRBUF_LEN 32
5422 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5429 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
5430 int bover = 0, iover = 0;
5431 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5432 char numbuf[3][STRBUF_LEN];
5434 char strbuf[STRBUF_LEN];
5436 if (dqb->dqb_bhardlimit &&
5437 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5439 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5440 if (dqb->dqb_btime > now) {
5447 if (dqb->dqb_ihardlimit &&
5448 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5450 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5451 if (dqb->dqb_itime > now) {
5459 if (strlen(mnt) > 15)
5460 printf("%s\n%15s", mnt, "");
5462 printf("%15s", mnt);
5465 diff2str(dqb->dqb_btime, timebuf, now);
5467 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5468 strbuf, sizeof(strbuf), h);
5469 if (rc == -EREMOTEIO)
5470 sprintf(numbuf[0], "%s*", strbuf);
5472 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5473 "%s" : "[%s]", strbuf);
5475 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5476 if (type == QC_GENERAL)
5477 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5478 "%s" : "[%s]", strbuf);
5480 sprintf(numbuf[1], "%s", "-");
5482 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5483 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5484 "%s" : "[%s]", strbuf);
5486 printf(" %7s%c %6s %7s %7s",
5487 numbuf[0], bover ? '*' : ' ', numbuf[1],
5488 numbuf[2], bover > 1 ? timebuf : "-");
5491 diff2str(dqb->dqb_itime, timebuf, now);
5493 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
5494 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
5496 if (type == QC_GENERAL)
5497 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5499 (uintmax_t)dqb->dqb_isoftlimit);
5501 sprintf(numbuf[1], "%s", "-");
5503 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5504 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5506 if (type != QC_OSTIDX)
5507 printf(" %7s%c %6s %7s %7s",
5508 numbuf[0], iover ? '*' : ' ', numbuf[1],
5509 numbuf[2], iover > 1 ? timebuf : "-");
5511 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5514 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5515 qctl->qc_cmd == Q_GETOINFO) {
5519 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5520 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5521 printf("Block grace time: %s; Inode grace time: %s\n",
5522 bgtimebuf, igtimebuf);
5526 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5527 bool h, __u64 *total)
5529 int rc = 0, rc1 = 0, count = 0;
5530 __u32 valid = qctl->qc_valid;
5532 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5534 fprintf(stderr, "can not get %s count: %s\n",
5535 is_mdt ? "mdt": "ost", strerror(-rc));
5539 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5540 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5541 rc = llapi_quotactl(mnt, qctl);
5543 /* It is remote client case. */
5544 if (rc == -EOPNOTSUPP) {
5551 fprintf(stderr, "quotactl %s%d failed.\n",
5552 is_mdt ? "mdt": "ost", qctl->qc_idx);
5556 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5557 qctl->qc_valid, 0, h);
5558 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5559 qctl->qc_dqblk.dqb_bhardlimit;
5562 qctl->qc_valid = valid;
5566 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5567 int verbose, int quiet, bool human_readable)
5569 int rc1 = 0, rc2 = 0, rc3 = 0;
5570 char *obd_type = (char *)qctl->obd_type;
5571 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5572 __u64 total_ialloc = 0, total_balloc = 0;
5575 rc1 = llapi_quotactl(mnt, qctl);
5579 fprintf(stderr, "%s quotas are not enabled.\n",
5580 qtype_name(qctl->qc_type));
5583 fprintf(stderr, "Permission denied.\n");
5586 /* We already got error message. */
5589 fprintf(stderr, "Unexpected quotactl error: %s\n",
5594 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
5595 print_quota_title(name, qctl, human_readable);
5597 if (rc1 && *obd_type)
5598 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5600 if (qctl->qc_valid != QC_GENERAL)
5603 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5604 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5605 (QIF_LIMITS|QIF_USAGE));
5607 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
5609 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
5611 char strbuf[STRBUF_LEN];
5613 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5615 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5617 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5619 printf("Total allocated inode limit: %ju, total "
5620 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5624 if (rc1 || rc2 || rc3 || inacc)
5625 printf("Some errors happened when getting quota info. "
5626 "Some devices may be not working or deactivated. "
5627 "The data in \"[]\" is inaccurate.\n");
5633 static int lfs_project(int argc, char **argv)
5635 int ret = 0, err = 0, c, i;
5636 struct project_handle_control phc = { 0 };
5637 enum lfs_project_ops_t op;
5640 phc.assign_projid = false;
5641 /* default action */
5642 op = LFS_PROJECT_LIST;
5644 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
5647 if (op != LFS_PROJECT_LIST) {
5649 "%s: cannot specify '-c' '-C' '-s' together\n",
5654 op = LFS_PROJECT_CHECK;
5657 if (op != LFS_PROJECT_LIST) {
5659 "%s: cannot specify '-c' '-C' '-s' together\n",
5664 op = LFS_PROJECT_CLEAR;
5667 if (op != LFS_PROJECT_LIST) {
5669 "%s: cannot specify '-c' '-C' '-s' together\n",
5674 phc.set_inherit = true;
5675 op = LFS_PROJECT_SET;
5681 phc.keep_projid = true;
5684 phc.recursive = true;
5687 phc.projid = strtoul(optarg, NULL, 0);
5688 phc.assign_projid = true;
5692 phc.newline = false;
5695 fprintf(stderr, "%s: invalid option '%c'\n",
5701 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
5702 op = LFS_PROJECT_SET;
5703 phc.set_projid = true;
5704 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
5705 phc.set_projid = true;
5709 case LFS_PROJECT_CHECK:
5710 if (phc.keep_projid) {
5712 "%s: '-k' is useless together with '-c'\n",
5717 case LFS_PROJECT_CLEAR:
5720 "%s: '-0' is useless together with '-C'\n",
5724 if (phc.assign_projid) {
5726 "%s: '-p' is useless together with '-C'\n",
5731 case LFS_PROJECT_SET:
5734 "%s: '-0' is useless together with '-s'\n",
5738 if (phc.keep_projid) {
5740 "%s: '-k' is useless together with '-s'\n",
5748 "%s: '-0' is useless for list operations\n",
5758 fprintf(stderr, "%s: missing file or directory target(s)\n",
5763 for (i = 0; i < argc; i++) {
5765 case LFS_PROJECT_CHECK:
5766 err = lfs_project_check(argv[i], &phc);
5768 case LFS_PROJECT_LIST:
5769 err = lfs_project_list(argv[i], &phc);
5771 case LFS_PROJECT_CLEAR:
5772 err = lfs_project_clear(argv[i], &phc);
5774 case LFS_PROJECT_SET:
5775 err = lfs_project_set(argv[i], &phc);
5787 static int lfs_quota(int argc, char **argv)
5790 char *mnt, *name = NULL;
5791 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
5792 .qc_type = ALLQUOTA };
5793 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
5794 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
5796 __u32 valid = QC_GENERAL, idx = 0;
5797 bool human_readable = false;
5800 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
5811 if (qctl.qc_type != ALLQUOTA) {
5813 "%s quota: only one of -u, -g, or -p may be specified\n",
5817 qctl.qc_type = qtype;
5820 qctl.qc_cmd = LUSTRE_Q_GETINFO;
5823 valid = qctl.qc_valid = QC_UUID;
5824 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
5827 valid = qctl.qc_valid = QC_MDTIDX;
5828 idx = qctl.qc_idx = atoi(optarg);
5829 if (idx == 0 && *optarg != '0') {
5831 "%s quota: invalid MDT index '%s'\n",
5837 valid = qctl.qc_valid = QC_OSTIDX;
5838 idx = qctl.qc_idx = atoi(optarg);
5839 if (idx == 0 && *optarg != '0') {
5841 "%s quota: invalid OST index '%s'\n",
5853 human_readable = true;
5856 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
5857 progname, argv[optind - 1]);
5862 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
5863 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
5864 optind == argc - 1) {
5866 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
5867 qctl.qc_valid = valid;
5870 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
5871 qctl.qc_type = qtype;
5872 if (qtype == USRQUOTA) {
5873 qctl.qc_id = geteuid();
5874 rc = uid2name(&name, qctl.qc_id);
5876 qctl.qc_id = getegid();
5877 rc = gid2name(&name, qctl.qc_id);
5882 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
5887 /* lfs quota -u username /path/to/lustre/mount */
5888 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
5889 /* options should be followed by u/g-name and mntpoint */
5890 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
5892 "%s quota: name and mount point must be specified\n",
5897 name = argv[optind++];
5898 switch (qctl.qc_type) {
5900 rc = name2uid(&qctl.qc_id, name);
5903 rc = name2gid(&qctl.qc_id, name);
5906 rc = name2projid(&qctl.qc_id, name);
5913 qctl.qc_id = strtoul(name, &endptr, 10);
5914 if (*endptr != '\0') {
5915 fprintf(stderr, "%s quota: invalid id '%s'\n",
5921 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
5923 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
5924 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
5931 #endif /* HAVE_SYS_QUOTA_H! */
5933 static int flushctx_ioctl(char *mp)
5937 fd = open(mp, O_RDONLY);
5939 fprintf(stderr, "flushctx: error open %s: %s\n",
5940 mp, strerror(errno));
5944 rc = ioctl(fd, LL_IOC_FLUSHCTX);
5946 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
5947 mp, strerror(errno));
5953 static int lfs_flushctx(int argc, char **argv)
5955 int kdestroy = 0, c;
5956 char mntdir[PATH_MAX] = {'\0'};
5960 while ((c = getopt(argc, argv, "k")) != -1) {
5966 fprintf(stderr, "error: %s: option '-%c' "
5967 "unrecognized\n", argv[0], c);
5973 if ((rc = system("kdestroy > /dev/null")) != 0) {
5974 rc = WEXITSTATUS(rc);
5975 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
5979 if (optind >= argc) {
5980 /* flush for all mounted lustre fs. */
5981 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
5982 /* Check if we have a mount point */
5983 if (mntdir[0] == '\0')
5986 if (flushctx_ioctl(mntdir))
5989 mntdir[0] = '\0'; /* avoid matching in next loop */
5992 /* flush fs as specified */
5993 while (optind < argc) {
5994 if (flushctx_ioctl(argv[optind++]))
6001 static int lfs_cp(int argc, char **argv)
6003 fprintf(stderr, "remote client copy file(s).\n"
6004 "obsolete, does not support it anymore.\n");
6008 static int lfs_ls(int argc, char **argv)
6010 fprintf(stderr, "remote client lists directory contents.\n"
6011 "obsolete, does not support it anymore.\n");
6015 static int lfs_changelog(int argc, char **argv)
6017 void *changelog_priv;
6018 struct changelog_rec *rec;
6019 long long startrec = 0, endrec = 0;
6021 struct option long_opts[] = {
6022 { .val = 'f', .name = "follow", .has_arg = no_argument },
6024 char short_opts[] = "f";
6027 while ((rc = getopt_long(argc, argv, short_opts,
6028 long_opts, NULL)) != -1) {
6036 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6037 argv[0], argv[optind - 1]);
6044 mdd = argv[optind++];
6046 startrec = strtoll(argv[optind++], NULL, 10);
6048 endrec = strtoll(argv[optind++], NULL, 10);
6050 rc = llapi_changelog_start(&changelog_priv,
6051 CHANGELOG_FLAG_BLOCK |
6052 CHANGELOG_FLAG_JOBID |
6053 CHANGELOG_FLAG_EXTRA_FLAGS |
6054 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6057 fprintf(stderr, "Can't start changelog: %s\n",
6058 strerror(errno = -rc));
6062 rc = llapi_changelog_set_xflags(changelog_priv,
6063 CHANGELOG_EXTRA_FLAG_UIDGID |
6064 CHANGELOG_EXTRA_FLAG_NID |
6065 CHANGELOG_EXTRA_FLAG_OMODE |
6066 CHANGELOG_EXTRA_FLAG_XATTR);
6068 fprintf(stderr, "Can't set xflags for changelog: %s\n",
6069 strerror(errno = -rc));
6073 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6077 if (endrec && rec->cr_index > endrec) {
6078 llapi_changelog_free(&rec);
6081 if (rec->cr_index < startrec) {
6082 llapi_changelog_free(&rec);
6086 secs = rec->cr_time >> 30;
6087 gmtime_r(&secs, &ts);
6088 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6089 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6090 changelog_type2str(rec->cr_type),
6091 ts.tm_hour, ts.tm_min, ts.tm_sec,
6092 (int)(rec->cr_time & ((1 << 30) - 1)),
6093 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6094 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6096 if (rec->cr_flags & CLF_JOBID) {
6097 struct changelog_ext_jobid *jid =
6098 changelog_rec_jobid(rec);
6100 if (jid->cr_jobid[0] != '\0')
6101 printf(" j=%s", jid->cr_jobid);
6104 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6105 struct changelog_ext_extra_flags *ef =
6106 changelog_rec_extra_flags(rec);
6108 printf(" ef=0x%llx", ef->cr_extra_flags);
6110 if (ef->cr_extra_flags & CLFE_UIDGID) {
6111 struct changelog_ext_uidgid *uidgid =
6112 changelog_rec_uidgid(rec);
6114 printf(" u=%llu:%llu",
6115 uidgid->cr_uid, uidgid->cr_gid);
6117 if (ef->cr_extra_flags & CLFE_NID) {
6118 struct changelog_ext_nid *nid =
6119 changelog_rec_nid(rec);
6122 libcfs_nid2str(nid->cr_nid));
6125 if (ef->cr_extra_flags & CLFE_OPEN) {
6126 struct changelog_ext_openmode *omd =
6127 changelog_rec_openmode(rec);
6128 char mode[] = "---";
6130 /* exec mode must be exclusive */
6131 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6134 if (omd->cr_openflags & FMODE_READ)
6136 if (omd->cr_openflags &
6143 if (strcmp(mode, "---") != 0)
6144 printf(" m=%s", mode);
6148 if (ef->cr_extra_flags & CLFE_XATTR) {
6149 struct changelog_ext_xattr *xattr =
6150 changelog_rec_xattr(rec);
6152 if (xattr->cr_xattr[0] != '\0')
6153 printf(" x=%s", xattr->cr_xattr);
6157 if (rec->cr_namelen)
6158 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6159 rec->cr_namelen, changelog_rec_name(rec));
6161 if (rec->cr_flags & CLF_RENAME) {
6162 struct changelog_ext_rename *rnm =
6163 changelog_rec_rename(rec);
6165 if (!fid_is_zero(&rnm->cr_sfid))
6166 printf(" s="DFID" sp="DFID" %.*s",
6167 PFID(&rnm->cr_sfid),
6168 PFID(&rnm->cr_spfid),
6169 (int)changelog_rec_snamelen(rec),
6170 changelog_rec_sname(rec));
6174 llapi_changelog_free(&rec);
6177 llapi_changelog_fini(&changelog_priv);
6180 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
6182 return (rc == 1 ? 0 : rc);
6185 static int lfs_changelog_clear(int argc, char **argv)
6193 endrec = strtoll(argv[3], NULL, 10);
6195 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6198 fprintf(stderr, "%s: record out of range: %llu\n",
6200 else if (rc == -ENOENT)
6201 fprintf(stderr, "%s: no changelog user: %s\n",
6204 fprintf(stderr, "%s error: %s\n", argv[0],
6213 static int lfs_fid2path(int argc, char **argv)
6215 struct option long_opts[] = {
6216 { .val = 'c', .name = "cur", .has_arg = no_argument },
6217 { .val = 'l', .name = "link", .has_arg = required_argument },
6218 { .val = 'r', .name = "rec", .has_arg = required_argument },
6220 char short_opts[] = "cl:r:";
6221 char *device, *fid, *path;
6222 long long recno = -1;
6228 while ((rc = getopt_long(argc, argv, short_opts,
6229 long_opts, NULL)) != -1) {
6235 linkno = strtol(optarg, NULL, 10);
6238 recno = strtoll(optarg, NULL, 10);
6243 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6244 argv[0], argv[optind - 1]);
6252 device = argv[optind++];
6253 path = calloc(1, PATH_MAX);
6255 fprintf(stderr, "error: Not enough memory\n");
6260 while (optind < argc) {
6261 fid = argv[optind++];
6263 lnktmp = (linkno >= 0) ? linkno : 0;
6265 int oldtmp = lnktmp;
6266 long long rectmp = recno;
6268 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6271 fprintf(stderr, "%s: error on FID %s: %s\n",
6272 argv[0], fid, strerror(errno = -rc2));
6279 fprintf(stdout, "%lld ", rectmp);
6280 if (device[0] == '/') {
6281 fprintf(stdout, "%s", device);
6282 if (device[strlen(device) - 1] != '/')
6283 fprintf(stdout, "/");
6284 } else if (path[0] == '\0') {
6285 fprintf(stdout, "/");
6287 fprintf(stdout, "%s\n", path);
6290 /* specified linkno */
6292 if (oldtmp == lnktmp)
6302 static int lfs_path2fid(int argc, char **argv)
6304 struct option long_opts[] = {
6305 { .val = 'p', .name = "parents", .has_arg = no_argument },
6308 const char short_opts[] = "p";
6309 const char *sep = "";
6312 bool show_parents = false;
6314 while ((rc = getopt_long(argc, argv, short_opts,
6315 long_opts, NULL)) != -1) {
6318 show_parents = true;
6321 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6322 argv[0], argv[optind - 1]);
6327 if (optind > argc - 1)
6329 else if (optind < argc - 1)
6333 for (path = argv + optind; *path != NULL; path++) {
6335 if (!show_parents) {
6336 err = llapi_path2fid(*path, &fid);
6338 printf("%s%s"DFID"\n",
6339 *sep != '\0' ? *path : "", sep,
6342 char name[NAME_MAX + 1];
6343 unsigned int linkno = 0;
6345 while ((err = llapi_path2parent(*path, linkno, &fid,
6346 name, sizeof(name))) == 0) {
6347 if (*sep != '\0' && linkno == 0)
6348 printf("%s%s", *path, sep);
6350 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6355 /* err == -ENODATA is end-of-loop */
6356 if (linkno > 0 && err == -ENODATA) {
6363 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
6364 argv[0], show_parents ? "parent " : "", *path,
6376 static int lfs_data_version(int argc, char **argv)
6383 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
6388 while ((c = getopt(argc, argv, "nrw")) != -1) {
6391 data_version_flags = 0;
6394 data_version_flags |= LL_DV_RD_FLUSH;
6397 data_version_flags |= LL_DV_WR_FLUSH;
6406 path = argv[optind];
6407 fd = open(path, O_RDONLY);
6409 err(errno, "cannot open file %s", path);
6411 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
6413 err(errno, "cannot get version for %s", path);
6415 printf("%ju" "\n", (uintmax_t)data_version);
6421 static int lfs_hsm_state(int argc, char **argv)
6426 struct hsm_user_state hus;
6434 rc = llapi_hsm_state_get(path, &hus);
6436 fprintf(stderr, "can't get hsm state for %s: %s\n",
6437 path, strerror(errno = -rc));
6441 /* Display path name and status flags */
6442 printf("%s: (0x%08x)", path, hus.hus_states);
6444 if (hus.hus_states & HS_RELEASED)
6445 printf(" released");
6446 if (hus.hus_states & HS_EXISTS)
6448 if (hus.hus_states & HS_DIRTY)
6450 if (hus.hus_states & HS_ARCHIVED)
6451 printf(" archived");
6452 /* Display user-settable flags */
6453 if (hus.hus_states & HS_NORELEASE)
6454 printf(" never_release");
6455 if (hus.hus_states & HS_NOARCHIVE)
6456 printf(" never_archive");
6457 if (hus.hus_states & HS_LOST)
6458 printf(" lost_from_hsm");
6460 if (hus.hus_archive_id != 0)
6461 printf(", archive_id:%d", hus.hus_archive_id);
6464 } while (++i < argc);
6469 #define LFS_HSM_SET 0
6470 #define LFS_HSM_CLEAR 1
6473 * Generic function to set or clear HSM flags.
6474 * Used by hsm_set and hsm_clear.
6476 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
6478 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
6480 struct option long_opts[] = {
6481 { .val = 'A', .name = "archived", .has_arg = no_argument },
6482 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
6483 { .val = 'd', .name = "dirty", .has_arg = no_argument },
6484 { .val = 'e', .name = "exists", .has_arg = no_argument },
6485 { .val = 'l', .name = "lost", .has_arg = no_argument },
6486 { .val = 'r', .name = "norelease", .has_arg = no_argument },
6487 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
6489 char short_opts[] = "lraAdei:";
6493 __u32 archive_id = 0;
6499 while ((c = getopt_long(argc, argv, short_opts,
6500 long_opts, NULL)) != -1) {
6506 mask |= HS_NOARCHIVE;
6509 mask |= HS_ARCHIVED;
6512 mask |= HS_NORELEASE;
6521 archive_id = strtol(optarg, &end, 10);
6523 fprintf(stderr, "invalid archive_id: '%s'\n",
6531 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6532 argv[0], argv[optind - 1]);
6537 /* User should have specified a flag */
6541 while (optind < argc) {
6543 path = argv[optind];
6545 /* If mode == 0, this means we apply the mask. */
6546 if (mode == LFS_HSM_SET)
6547 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6549 rc = llapi_hsm_state_set(path, 0, mask, 0);
6552 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6553 path, strerror(errno = -rc));
6562 static int lfs_hsm_action(int argc, char **argv)
6567 struct hsm_current_action hca;
6568 struct hsm_extent he;
6569 enum hsm_user_action hua;
6570 enum hsm_progress_states hps;
6578 rc = llapi_hsm_current_action(path, &hca);
6580 fprintf(stderr, "can't get hsm action for %s: %s\n",
6581 path, strerror(errno = -rc));
6584 he = hca.hca_location;
6585 hua = hca.hca_action;
6586 hps = hca.hca_state;
6588 printf("%s: %s", path, hsm_user_action2name(hua));
6590 /* Skip file without action */
6591 if (hca.hca_action == HUA_NONE) {
6596 printf(" %s ", hsm_progress_state2name(hps));
6598 if ((hps == HPS_RUNNING) &&
6599 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
6600 printf("(%llu bytes moved)\n",
6601 (unsigned long long)he.length);
6602 else if ((he.offset + he.length) == LUSTRE_EOF)
6603 printf("(from %llu to EOF)\n",
6604 (unsigned long long)he.offset);
6606 printf("(from %llu to %llu)\n",
6607 (unsigned long long)he.offset,
6608 (unsigned long long)(he.offset + he.length));
6610 } while (++i < argc);
6615 static int lfs_hsm_set(int argc, char **argv)
6617 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
6620 static int lfs_hsm_clear(int argc, char **argv)
6622 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
6626 * Check file state and return its fid, to be used by lfs_hsm_request().
6628 * \param[in] file Path to file to check
6629 * \param[in,out] fid Pointer to allocated lu_fid struct.
6630 * \param[in,out] last_dev Pointer to last device id used.
6632 * \return 0 on success.
6634 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
6640 rc = lstat(file, &st);
6642 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
6645 /* Checking for regular file as archiving as posix copytool
6646 * rejects archiving files other than regular files
6648 if (!S_ISREG(st.st_mode)) {
6649 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
6652 /* A request should be ... */
6653 if (*last_dev != st.st_dev && *last_dev != 0) {
6654 fprintf(stderr, "All files should be "
6655 "on the same filesystem: %s\n", file);
6658 *last_dev = st.st_dev;
6660 rc = llapi_path2fid(file, fid);
6662 fprintf(stderr, "Cannot read FID of %s: %s\n",
6663 file, strerror(-rc));
6669 /* Fill an HSM HUR item with a given file name.
6671 * If mntpath is set, then the filename is actually a FID, and no
6672 * lookup on the filesystem will be performed.
6674 * \param[in] hur the user request to fill
6675 * \param[in] idx index of the item inside the HUR to fill
6676 * \param[in] mntpath mountpoint of Lustre
6677 * \param[in] fname filename (if mtnpath is NULL)
6678 * or FID (if mntpath is set)
6679 * \param[in] last_dev pointer to last device id used
6681 * \retval 0 on success
6682 * \retval CMD_HELP or a negative errno on error
6684 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
6685 const char *mntpath, const char *fname,
6688 struct hsm_user_item *hui = &hur->hur_user_item[idx];
6691 hui->hui_extent.length = -1;
6693 if (mntpath != NULL) {
6696 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
6700 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
6705 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
6709 hur->hur_request.hr_itemcount++;
6714 static int lfs_hsm_request(int argc, char **argv, int action)
6716 struct option long_opts[] = {
6717 { .val = 'a', .name = "archive", .has_arg = required_argument },
6718 { .val = 'D', .name = "data", .has_arg = required_argument },
6719 { .val = 'l', .name = "filelist", .has_arg = required_argument },
6720 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
6723 char short_opts[] = "l:D:a:m:";
6724 struct hsm_user_request *hur, *oldhur;
6729 char *filelist = NULL;
6730 char fullpath[PATH_MAX];
6731 char *opaque = NULL;
6735 int nbfile_alloc = 0;
6736 char *some_file = NULL;
6737 char *mntpath = NULL;
6743 while ((c = getopt_long(argc, argv, short_opts,
6744 long_opts, NULL)) != -1) {
6753 if (action != HUA_ARCHIVE &&
6754 action != HUA_REMOVE) {
6756 "error: -a is supported only "
6757 "when archiving or removing\n");
6760 archive_id = atoi(optarg);
6763 if (some_file == NULL) {
6765 some_file = strdup(optarg);
6771 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6772 argv[0], argv[optind - 1]);
6777 /* All remaining args are files, so we have at least nbfile */
6778 nbfile = argc - optind;
6780 if ((nbfile == 0) && (filelist == NULL))
6784 opaque_len = strlen(opaque);
6786 /* Alloc the request structure with enough place to store all files
6787 * from command line. */
6788 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
6790 fprintf(stderr, "Cannot create the request: %s\n",
6794 nbfile_alloc = nbfile;
6796 hur->hur_request.hr_action = action;
6797 hur->hur_request.hr_archive_id = archive_id;
6798 hur->hur_request.hr_flags = 0;
6800 /* All remaining args are files, add them */
6801 if (nbfile != 0 && some_file == NULL)
6802 some_file = strdup(argv[optind]);
6804 for (i = 0; i < nbfile; i++) {
6805 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
6811 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
6813 /* If a filelist was specified, read the filelist from it. */
6814 if (filelist != NULL) {
6815 fp = fopen(filelist, "r");
6817 fprintf(stderr, "Cannot read the file list %s: %s\n",
6818 filelist, strerror(errno));
6823 while ((rc = getline(&line, &len, fp)) != -1) {
6824 /* If allocated buffer was too small, get something
6826 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
6829 nbfile_alloc = nbfile_alloc * 2 + 1;
6831 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
6834 fprintf(stderr, "hsm: cannot allocate "
6835 "the request: %s\n",
6842 size = hur_len(oldhur);
6844 fprintf(stderr, "hsm: cannot allocate "
6845 "%u files + %u bytes data\n",
6846 oldhur->hur_request.hr_itemcount,
6847 oldhur->hur_request.hr_data_len);
6854 memcpy(hur, oldhur, size);
6859 if (line[strlen(line) - 1] == '\n')
6860 line[strlen(line) - 1] = '\0';
6862 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
6863 mntpath, line, &last_dev);
6869 if (some_file == NULL) {
6879 /* If a --data was used, add it to the request */
6880 hur->hur_request.hr_data_len = opaque_len;
6882 memcpy(hur_data(hur), opaque, opaque_len);
6884 /* Send the HSM request */
6885 if (realpath(some_file, fullpath) == NULL) {
6886 fprintf(stderr, "Could not find path '%s': %s\n",
6887 some_file, strerror(errno));
6889 rc = llapi_hsm_request(fullpath, hur);
6891 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
6892 some_file, strerror(-rc));
6902 static int lfs_hsm_archive(int argc, char **argv)
6904 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
6907 static int lfs_hsm_restore(int argc, char **argv)
6909 return lfs_hsm_request(argc, argv, HUA_RESTORE);
6912 static int lfs_hsm_release(int argc, char **argv)
6914 return lfs_hsm_request(argc, argv, HUA_RELEASE);
6917 static int lfs_hsm_remove(int argc, char **argv)
6919 return lfs_hsm_request(argc, argv, HUA_REMOVE);
6922 static int lfs_hsm_cancel(int argc, char **argv)
6924 return lfs_hsm_request(argc, argv, HUA_CANCEL);
6927 static int lfs_swap_layouts(int argc, char **argv)
6932 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
6933 SWAP_LAYOUTS_KEEP_MTIME |
6934 SWAP_LAYOUTS_KEEP_ATIME);
6937 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
6939 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
6941 int lfs_get_mode(const char *string)
6943 enum lock_mode_user mode;
6945 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
6946 if (lock_mode_names[mode] == NULL)
6948 if (strcmp(string, lock_mode_names[mode]) == 0)
6955 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
6957 enum lu_ladvise_type advice;
6960 advice < ARRAY_SIZE(ladvise_names); advice++) {
6961 if (ladvise_names[advice] == NULL)
6963 if (strcmp(string, ladvise_names[advice]) == 0)
6967 return LU_LADVISE_INVALID;
6970 static int lfs_ladvise(int argc, char **argv)
6972 struct option long_opts[] = {
6973 { .val = 'a', .name = "advice", .has_arg = required_argument },
6974 { .val = 'b', .name = "background", .has_arg = no_argument },
6975 { .val = 'e', .name = "end", .has_arg = required_argument },
6976 { .val = 'l', .name = "length", .has_arg = required_argument },
6977 { .val = 'm', .name = "mode", .has_arg = required_argument },
6978 { .val = 's', .name = "start", .has_arg = required_argument },
6979 { .val = 'u', .name = "unset", .has_arg = no_argument },
6981 char short_opts[] = "a:be:l:m:s:u";
6986 struct llapi_lu_ladvise advice;
6987 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
6988 unsigned long long start = 0;
6989 unsigned long long end = LUSTRE_EOF;
6990 unsigned long long length = 0;
6991 unsigned long long size_units;
6992 unsigned long long flags = 0;
6996 while ((c = getopt_long(argc, argv, short_opts,
6997 long_opts, NULL)) != -1) {
7000 advice_type = lfs_get_ladvice(optarg);
7001 if (advice_type == LU_LADVISE_INVALID) {
7002 fprintf(stderr, "%s: invalid advice type "
7003 "'%s'\n", argv[0], optarg);
7004 fprintf(stderr, "Valid types:");
7006 for (advice_type = 0;
7007 advice_type < ARRAY_SIZE(ladvise_names);
7009 if (ladvise_names[advice_type] == NULL)
7011 fprintf(stderr, " %s",
7012 ladvise_names[advice_type]);
7014 fprintf(stderr, "\n");
7027 rc = llapi_parse_size(optarg, &end,
7030 fprintf(stderr, "%s: bad end offset '%s'\n",
7037 rc = llapi_parse_size(optarg, &start,
7040 fprintf(stderr, "%s: bad start offset "
7041 "'%s'\n", argv[0], optarg);
7047 rc = llapi_parse_size(optarg, &length,
7050 fprintf(stderr, "%s: bad length '%s'\n",
7056 mode = lfs_get_mode(optarg);
7058 fprintf(stderr, "%s: bad mode '%s', valid "
7059 "modes are READ or WRITE\n",
7067 fprintf(stderr, "%s: option '%s' unrecognized\n",
7068 argv[0], argv[optind - 1]);
7073 if (advice_type == LU_LADVISE_INVALID) {
7074 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7075 fprintf(stderr, "Valid types:");
7076 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7078 if (ladvise_names[advice_type] == NULL)
7080 fprintf(stderr, " %s", ladvise_names[advice_type]);
7082 fprintf(stderr, "\n");
7086 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7087 fprintf(stderr, "%s: Lock no expand advice is a per file "
7088 "descriptor advice, so when called from lfs, "
7089 "it does nothing.\n", argv[0]);
7093 if (argc <= optind) {
7094 fprintf(stderr, "%s: please give one or more file names\n",
7099 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7100 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7105 if (end == LUSTRE_EOF && length != 0)
7106 end = start + length;
7109 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7110 argv[0], start, end);
7114 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7115 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7120 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7121 fprintf(stderr, "%s: mode is required with lockahead\n",
7126 while (optind < argc) {
7129 path = argv[optind++];
7131 fd = open(path, O_RDONLY);
7133 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7134 argv[0], path, strerror(errno));
7139 advice.lla_start = start;
7140 advice.lla_end = end;
7141 advice.lla_advice = advice_type;
7142 advice.lla_value1 = 0;
7143 advice.lla_value2 = 0;
7144 advice.lla_value3 = 0;
7145 advice.lla_value4 = 0;
7146 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7147 advice.lla_lockahead_mode = mode;
7148 advice.lla_peradvice_flags = flags;
7151 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7154 fprintf(stderr, "%s: cannot give advice '%s' to file "
7155 "'%s': %s\n", argv[0],
7156 ladvise_names[advice_type],
7157 path, strerror(errno));
7163 if (rc == 0 && rc2 < 0)
7169 /** The input string contains a comma delimited list of component ids and
7170 * ranges, for example "1,2-4,7".
7172 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7174 bool end_of_loop = false;
7182 while (!end_of_loop) {
7186 char *endptr = NULL;
7189 ptr = strchrnul(arg, ',');
7190 end_of_loop = *ptr == '\0';
7193 start_index = strtol(arg, &endptr, 0);
7194 if (endptr == arg) /* no data at all */
7196 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7198 if (start_index < 0)
7201 end_index = start_index;
7202 if (*endptr == '-') {
7203 end_index = strtol(endptr + 1, &endptr, 0);
7204 if (*endptr != '\0')
7206 if (end_index < start_index)
7210 for (i = start_index; i <= end_index && size > 0; i++) {
7213 /* remove duplicate */
7214 for (j = 0; j < nr; j++) {
7218 if (j == nr) { /* no duplicate */
7224 if (size == 0 && i < end_index)
7231 if (!end_of_loop && ptr != NULL)
7234 return rc < 0 ? rc : nr;
7238 * struct verify_mirror_id - Mirror id to be verified.
7239 * @mirror_id: A specified mirror id.
7240 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7242 struct verify_mirror_id {
7248 * compare_mirror_ids() - Compare mirror ids.
7249 * @layout: Mirror component list.
7250 * @cbdata: Callback data in verify_mirror_id structure.
7252 * This is a callback function called by llapi_layout_comp_iterate()
7253 * to compare the specified mirror id with the one in the current
7254 * component of @layout. If they are the same, then the specified
7255 * mirror id is valid.
7257 * Return: a negative error code on failure or
7258 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7259 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7262 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7264 struct verify_mirror_id *mirror_id_cbdata =
7265 (struct verify_mirror_id *)cbdata;
7269 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7273 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7274 progname, strerror(errno));
7278 if (mirror_id_cbdata->mirror_id == mirror_id) {
7279 mirror_id_cbdata->is_valid_id = true;
7280 return LLAPI_LAYOUT_ITER_STOP;
7283 return LLAPI_LAYOUT_ITER_CONT;
7287 * verify_mirror_ids() - Verify specified mirror ids.
7288 * @fname: Mirrored file name.
7289 * @mirror_ids: Specified mirror ids to be verified.
7290 * @ids_nr: Number of specified mirror ids.
7292 * This function verifies that specified @mirror_ids are valid
7293 * in the mirrored file @fname.
7295 * Return: 0 on success or a negative error code on failure.
7298 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7300 struct llapi_layout *layout = NULL;
7301 struct verify_mirror_id mirror_id_cbdata = { 0 };
7312 if (stat(fname, &stbuf) < 0) {
7313 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7314 progname, fname, strerror(errno));
7319 if (!S_ISREG(stbuf.st_mode)) {
7320 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7326 fd = open(fname, O_DIRECT | O_RDONLY);
7328 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7329 progname, fname, strerror(errno));
7334 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
7336 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
7337 progname, fname, strerror(errno));
7341 layout = llapi_layout_get_by_fd(fd, 0);
7342 if (layout == NULL) {
7343 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7344 progname, fname, strerror(errno));
7346 llapi_lease_release(fd);
7350 rc = llapi_layout_flags_get(layout, &flr_state);
7352 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7353 progname, fname, strerror(errno));
7358 flr_state &= LCM_FL_FLR_MASK;
7359 switch (flr_state) {
7362 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7363 progname, fname, llapi_layout_flags_string(flr_state));
7370 for (i = 0; i < ids_nr; i++) {
7371 mirror_id_cbdata.mirror_id = mirror_ids[i];
7372 mirror_id_cbdata.is_valid_id = false;
7374 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
7379 "%s: '%s' failed to verify mirror id: %u.\n",
7380 progname, fname, mirror_ids[i]);
7384 if (!mirror_id_cbdata.is_valid_id) {
7387 "%s: '%s' invalid specified mirror id: %u.\n",
7388 progname, fname, mirror_ids[i]);
7394 llapi_layout_free(layout);
7395 llapi_lease_release(fd);
7403 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
7404 __u16 *mirror_ids, int ids_nr)
7406 struct llapi_resync_comp comp_array[1024] = { { 0 } };
7407 struct llapi_layout *layout;
7415 if (stat(fname, &stbuf) < 0) {
7416 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7417 progname, fname, strerror(errno));
7421 if (!S_ISREG(stbuf.st_mode)) {
7422 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7428 fd = open(fname, O_DIRECT | O_RDWR);
7430 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7431 progname, fname, strerror(errno));
7436 layout = llapi_layout_get_by_fd(fd, 0);
7437 if (layout == NULL) {
7438 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7439 progname, fname, strerror(errno));
7444 rc = llapi_layout_flags_get(layout, &flr_state);
7446 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7447 progname, fname, strerror(errno));
7452 flr_state &= LCM_FL_FLR_MASK;
7453 if (flr_state == LCM_FL_NONE) {
7455 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
7460 /* get stale component info */
7461 comp_size = llapi_mirror_find_stale(layout, comp_array,
7462 ARRAY_SIZE(comp_array),
7463 mirror_ids, ids_nr);
7464 if (comp_size <= 0) {
7469 ioc->lil_mode = LL_LEASE_WRLCK;
7470 ioc->lil_flags = LL_LEASE_RESYNC;
7471 rc = llapi_lease_set(fd, ioc);
7473 if (rc == -EALREADY)
7477 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
7478 progname, fname, strerror(errno));
7483 while (idx < comp_size) {
7489 rc = llapi_lease_check(fd);
7490 if (rc != LL_LEASE_WRLCK) {
7491 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7496 mirror_id = comp_array[idx].lrc_mirror_id;
7497 end = comp_array[idx].lrc_end;
7499 /* try to combine adjacent component */
7500 for (i = idx + 1; i < comp_size; i++) {
7501 if (mirror_id != comp_array[i].lrc_mirror_id ||
7502 end != comp_array[i].lrc_start)
7504 end = comp_array[i].lrc_end;
7507 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7508 comp_array[idx].lrc_start,
7511 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7512 "%ld.\n", progname, fname, result);
7515 } else if (result > 0) {
7518 /* mark synced components */
7519 for (j = idx; j < i; j++)
7520 comp_array[j].lrc_synced = true;
7527 /* prepare ioc for lease put */
7528 ioc->lil_mode = LL_LEASE_UNLCK;
7529 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7531 for (idx = 0; idx < comp_size; idx++) {
7532 if (comp_array[idx].lrc_synced) {
7533 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7538 rc = llapi_lease_set(fd, ioc);
7540 if (rc == 0) /* lost lease lock */
7542 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7543 progname, fname, strerror(errno));
7547 * llapi_lease_set returns lease mode when it request to unlock
7553 llapi_layout_free(layout);
7560 static inline int lfs_mirror_resync(int argc, char **argv)
7562 struct ll_ioc_lease *ioc = NULL;
7563 __u16 mirror_ids[128] = { 0 };
7568 struct option long_opts[] = {
7569 { .val = 'o', .name = "only", .has_arg = required_argument },
7572 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
7575 rc = parse_mirror_ids(mirror_ids,
7576 sizeof(mirror_ids) / sizeof(__u16),
7580 "%s: bad mirror ids '%s'.\n",
7587 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7588 argv[0], argv[optind - 1]);
7594 if (argc == optind) {
7595 fprintf(stderr, "%s: no file name given.\n", argv[0]);
7600 if (ids_nr > 0 && argc > optind + 1) {
7602 "%s: option '--only' cannot be used upon multiple files.\n",
7610 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7615 /* set the lease on the file */
7616 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
7618 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
7619 argv[0], strerror(errno));
7624 for (; optind < argc; optind++) {
7625 rc = lfs_mirror_resync_file(argv[optind], ioc,
7626 mirror_ids, ids_nr);
7627 /* ignore previous file's error, continue with next file */
7630 memset(ioc, 0, sizeof(__u32) * 4096);
7639 * struct verify_chunk - Mirror chunk to be verified.
7640 * @chunk: [start, end) of the chunk.
7641 * @mirror_count: Number of mirror ids in @mirror_id array.
7642 * @mirror_id: Array of valid mirror ids that cover the chunk.
7644 struct verify_chunk {
7645 struct lu_extent chunk;
7646 unsigned int mirror_count;
7647 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
7651 * print_chunks() - Print chunk information.
7652 * @fname: Mirrored file name.
7653 * @chunks: Array of chunks.
7654 * @chunk_count: Number of chunks in @chunks array.
7656 * This function prints [start, end) of each chunk in @chunks
7657 * for mirrored file @fname, and also prints the valid mirror ids
7658 * that cover the chunk.
7663 void print_chunks(const char *fname, struct verify_chunk *chunks,
7669 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
7670 for (i = 0; i < chunk_count; i++) {
7671 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
7673 if (chunks[i].mirror_count == 0)
7674 fprintf(stdout, "\t[");
7676 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
7677 for (j = 1; j < chunks[i].mirror_count; j++)
7678 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
7680 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
7682 fprintf(stdout, "\n");
7686 * print_checksums() - Print CRC-32 checksum values.
7687 * @chunk: A chunk and its corresponding valid mirror ids.
7688 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
7690 * This function prints CRC-32 checksum values on @chunk for
7691 * each valid mirror that covers it.
7696 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
7701 "CRC-32 checksum value for chunk "DEXT":\n",
7702 PEXT(&chunk->chunk));
7703 for (i = 0; i < chunk->mirror_count; i++)
7704 fprintf(stdout, "Mirror %u:\t%#lx\n",
7705 chunk->mirror_id[i], crc[i]);
7706 fprintf(stdout, "\n");
7710 * filter_mirror_id() - Filter specified mirror ids.
7711 * @chunks: Array of chunks.
7712 * @chunk_count: Number of chunks in @chunks array.
7713 * @mirror_ids: Specified mirror ids to be verified.
7714 * @ids_nr: Number of specified mirror ids.
7716 * This function scans valid mirror ids that cover each chunk in @chunks
7717 * and filters specified mirror ids.
7722 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
7723 __u16 *mirror_ids, int ids_nr)
7728 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7729 unsigned int valid_count = 0;
7731 for (i = 0; i < chunk_count; i++) {
7732 if (chunks[i].mirror_count == 0)
7736 for (j = 0; j < ids_nr; j++) {
7737 for (k = 0; k < chunks[i].mirror_count; k++) {
7738 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
7739 valid_id[valid_count] = mirror_ids[j];
7746 memcpy(chunks[i].mirror_id, valid_id,
7747 sizeof(__u16) * valid_count);
7748 chunks[i].mirror_count = valid_count;
7753 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
7754 * @layout: Mirror component list.
7755 * @chunks: Array of chunks.
7756 * @chunks_size: Array size of @chunks.
7758 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
7759 * to find out chunk segments and store them in @chunks array.
7761 * The @mirror_id array in each element of @chunks will store the valid
7762 * mirror ids that cover the chunk. If a mirror component covering the
7763 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
7764 * will not be stored into the @mirror_id array, and the chunk for that
7765 * mirror will not be verified.
7767 * The @mirror_count in each element of @chunks will store the number of
7768 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
7769 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
7770 * indicates the chunk is valid in only one mirror. In both cases, the
7771 * chunk will not be verified.
7773 * Here is an example:
7776 * +------+-------------+--------------------+
7778 * +------+------+------+------+-------------+
7779 * | | S | S | | mirror2
7780 * +-------------+------+------+-------------+
7782 * prepared @chunks array will contain 5 elements:
7783 * (([0, 1M), [1, 2], 2),
7784 * ([1M, 2M), [1, 2], 2),
7785 * ([2M, 3M), [1], 1),
7786 * ([3M, 4M], [], 0),
7787 * ([4M, EOF), [2], 1))
7789 * Return: the actual array size of @chunks on success
7790 * or a negative error code on failure.
7793 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
7794 struct verify_chunk *chunks,
7805 memset(chunks, 0, sizeof(*chunks) * chunks_size);
7808 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
7811 "%s: move to the first layout component: %s.\n",
7812 progname, strerror(errno));
7818 chunks[idx].chunk.e_end = LUSTRE_EOF;
7820 rc = llapi_layout_comp_extent_get(layout, &start, &end);
7823 "%s: llapi_layout_comp_extent_get failed: %s.\n",
7824 progname, strerror(errno));
7828 if (start > chunks[idx].chunk.e_start ||
7829 end <= chunks[idx].chunk.e_start)
7832 if (end < chunks[idx].chunk.e_end)
7833 chunks[idx].chunk.e_end = end;
7835 rc = llapi_layout_comp_flags_get(layout, &flags);
7838 "%s: llapi_layout_comp_flags_get failed: %s.\n",
7839 progname, strerror(errno));
7843 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
7846 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7849 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7850 progname, strerror(errno));
7854 chunks[idx].mirror_id[i] = mirror_id;
7856 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
7858 "%s: mirror_id array is too small.\n",
7865 rc = llapi_layout_comp_use(layout,
7866 LLAPI_LAYOUT_COMP_USE_NEXT);
7869 "%s: move to the next layout component: %s.\n",
7870 progname, strerror(errno));
7873 } /* loop through all components */
7875 chunks[idx].mirror_count = i;
7877 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
7881 if (idx >= chunks_size) {
7882 fprintf(stderr, "%s: chunks array is too small.\n",
7888 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
7892 return rc < 0 ? rc : idx + 1;
7896 * lfs_mirror_verify_chunk() - Verify a chunk.
7897 * @fd: File descriptor of the mirrored file.
7898 * @file_size: Size of the mirrored file.
7899 * @chunk: A chunk and its corresponding valid mirror ids.
7900 * @verbose: Verbose mode.
7902 * This function verifies a @chunk contains exactly the same data
7903 * ammong the mirrors that cover it.
7905 * If @verbose is specified, then the function will print where the
7906 * differences are if the data do not match. Otherwise, it will
7907 * just return an error in that case.
7909 * Return: 0 on success or a negative error code on failure.
7912 int lfs_mirror_verify_chunk(int fd, size_t file_size,
7913 struct verify_chunk *chunk, int verbose)
7915 const size_t buflen = 4 * 1024 * 1024; /* 4M */
7917 size_t page_size = sysconf(_SC_PAGESIZE);
7923 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7930 rc = posix_memalign(&buf, page_size, buflen);
7931 if (rc) /* error code is returned directly */
7935 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7936 PEXT(&chunk->chunk));
7937 for (i = 0; i < chunk->mirror_count; i++)
7938 fprintf(stdout, " %u", chunk->mirror_id[i]);
7939 fprintf(stdout, "\n");
7943 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
7944 pos = chunk->chunk.e_start;
7945 while (bytes_done < count) {
7946 /* compute initial CRC-32 checksum */
7947 crc = crc32(0L, Z_NULL, 0);
7948 memset(crc_array, 0, sizeof(crc_array));
7951 for (i = 0; i < chunk->mirror_count; i++) {
7952 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
7954 if (bytes_read < 0) {
7957 "%s: failed to read data from mirror %u: %s.\n",
7958 progname, chunk->mirror_id[i],
7963 /* compute new CRC-32 checksum */
7964 crc_array[i] = crc32(crc, buf, bytes_read);
7968 print_checksums(chunk, crc_array);
7970 /* compare CRC-32 checksum values */
7971 for (i = 1; i < chunk->mirror_count; i++) {
7972 if (crc_array[i] != crc_array[0]) {
7978 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
7979 progname, PEXT(&chunk->chunk),
7980 chunk->mirror_id[0],
7981 chunk->mirror_id[i]);
7986 bytes_done += bytes_read;
7989 if (verbose > 1 && rc == 0) {
7990 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
7991 PEXT(&chunk->chunk));
7992 for (i = 0; i < chunk->mirror_count; i++)
7993 fprintf(stdout, " %u", chunk->mirror_id[i]);
7994 fprintf(stdout, " PASS\n\n");
8003 * lfs_mirror_verify_file() - Verify a mirrored file.
8004 * @fname: Mirrored file name.
8005 * @mirror_ids: Specified mirror ids to be verified.
8006 * @ids_nr: Number of specified mirror ids.
8007 * @verbose: Verbose mode.
8009 * This function verifies that each SYNC mirror of a mirrored file
8010 * specified by @fname contains exactly the same data.
8012 * If @mirror_ids is specified, then the function will verify the
8013 * mirrors specified by @mirror_ids contain exactly the same data.
8015 * If @verbose is specified, then the function will print where the
8016 * differences are if the data do not match. Otherwise, it will
8017 * just return an error in that case.
8019 * Return: 0 on success or a negative error code on failure.
8022 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8025 struct verify_chunk chunks_array[1024] = { };
8026 struct llapi_layout *layout = NULL;
8030 int chunk_count = 0;
8036 if (stat(fname, &stbuf) < 0) {
8037 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8038 progname, fname, strerror(errno));
8043 if (!S_ISREG(stbuf.st_mode)) {
8044 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8050 if (stbuf.st_size == 0) {
8052 fprintf(stdout, "%s: '%s' file size is 0.\n",
8058 fd = open(fname, O_DIRECT | O_RDONLY);
8060 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8061 progname, fname, strerror(errno));
8066 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8068 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8069 progname, fname, strerror(errno));
8073 layout = llapi_layout_get_by_fd(fd, 0);
8074 if (layout == NULL) {
8075 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8076 progname, fname, strerror(errno));
8078 llapi_lease_release(fd);
8082 rc = llapi_layout_flags_get(layout, &flr_state);
8084 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8085 progname, fname, strerror(errno));
8090 flr_state &= LCM_FL_FLR_MASK;
8091 switch (flr_state) {
8094 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8095 progname, fname, llapi_layout_flags_string(flr_state));
8101 /* find out mirror chunks to be verified */
8102 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8103 ARRAY_SIZE(chunks_array));
8104 if (chunk_count < 0) {
8110 /* filter specified mirror ids */
8111 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8114 print_chunks(fname, chunks_array, chunk_count);
8116 for (idx = 0; idx < chunk_count; idx++) {
8117 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8120 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8122 PEXT(&chunks_array[idx].chunk),
8123 (unsigned long long)stbuf.st_size);
8127 if (chunks_array[idx].mirror_count == 0) {
8129 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8131 PEXT(&chunks_array[idx].chunk));
8133 fprintf(stderr, "skipped\n");
8137 fprintf(stderr, "failed\n");
8141 if (chunks_array[idx].mirror_count == 1) {
8144 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8146 PEXT(&chunks_array[idx].chunk),
8147 chunks_array[idx].mirror_id[0]);
8151 rc = llapi_lease_check(fd);
8152 if (rc != LL_LEASE_RDLCK) {
8153 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8158 /* verify one chunk */
8159 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8160 &chunks_array[idx], verbose);
8174 llapi_layout_free(layout);
8175 llapi_lease_release(fd);
8183 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
8184 * @argc: The count of lfs mirror verify command line arguments.
8185 * @argv: Array of strings for lfs mirror verify command line arguments.
8187 * This function parses lfs mirror verify command and verifies the
8188 * specified mirrored file(s).
8190 * Return: 0 on success or a negative error code on failure.
8192 static inline int lfs_mirror_verify(int argc, char **argv)
8194 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8202 struct option long_opts[] = {
8203 { .val = 'o', .name = "only", .has_arg = required_argument },
8204 { .val = 'v', .name = "verbose", .has_arg = no_argument },
8207 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8209 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
8212 rc = parse_mirror_ids(mirror_ids,
8213 ARRAY_SIZE(mirror_ids),
8217 "%s: bad mirror ids '%s'.\n",
8224 "%s: at least 2 mirror ids needed with '--only' option.\n",
8234 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8235 progname, argv[optind - 1]);
8241 if (argc == optind) {
8242 fprintf(stderr, "%s: no file name given.\n", progname);
8247 if (ids_nr > 0 && argc > optind + 1) {
8249 "%s: '--only' cannot be used upon multiple files.\n",
8257 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8263 for (; optind < argc; optind++) {
8264 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
8274 * lfs_mirror() - Parse and execute lfs mirror commands.
8275 * @argc: The count of lfs mirror command line arguments.
8276 * @argv: Array of strings for lfs mirror command line arguments.
8278 * This function parses lfs mirror commands and performs the
8279 * corresponding functions specified in mirror_cmdlist[].
8281 * Return: 0 on success or an error code on failure.
8283 static int lfs_mirror(int argc, char **argv)
8290 Parser_init("lfs-mirror > ", mirror_cmdlist);
8292 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8294 program_invocation_short_name = cmd;
8296 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
8298 rc = Parser_commands();
8300 return rc < 0 ? -rc : rc;
8304 * lfs_mirror_list_commands() - List lfs mirror commands.
8305 * @argc: The count of command line arguments.
8306 * @argv: Array of strings for command line arguments.
8308 * This function lists lfs mirror commands defined in mirror_cmdlist[].
8310 * Return: 0 on success.
8312 static int lfs_mirror_list_commands(int argc, char **argv)
8314 char buffer[81] = "";
8316 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
8322 static int lfs_list_commands(int argc, char **argv)
8324 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
8326 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
8331 int main(int argc, char **argv)
8335 /* Ensure that liblustreapi constructor has run */
8336 if (!llapi_liblustreapi_initialized())
8337 fprintf(stderr, "liblustreapi was not properly initialized\n");
8342 Parser_init("lfs > ", cmdlist);
8344 progname = program_invocation_short_name; /* Used in error messages */
8346 llapi_set_command_name(argv[1]);
8347 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
8348 llapi_clear_command_name();
8350 rc = Parser_commands();
8353 return rc < 0 ? -rc : rc;
8356 #ifdef _LUSTRE_IDL_H_
8357 /* Everything we need here should be included by lustreapi.h. */
8358 # error "lfs should not depend on lustre_idl.h"
8359 #endif /* _LUSTRE_IDL_H_ */