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>
79 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
80 #endif /* !ARRAY_SIZE */
83 static int lfs_find(int argc, char **argv);
84 static int lfs_getstripe(int argc, char **argv);
85 static int lfs_getdirstripe(int argc, char **argv);
86 static int lfs_setdirstripe(int argc, char **argv);
87 static int lfs_rmentry(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_cp(int argc, char **argv);
100 static int lfs_ls(int argc, char **argv);
101 static int lfs_poollist(int argc, char **argv);
102 static int lfs_changelog(int argc, char **argv);
103 static int lfs_changelog_clear(int argc, char **argv);
104 static int lfs_fid2path(int argc, char **argv);
105 static int lfs_path2fid(int argc, char **argv);
106 static int lfs_data_version(int argc, char **argv);
107 static int lfs_hsm_state(int argc, char **argv);
108 static int lfs_hsm_set(int argc, char **argv);
109 static int lfs_hsm_clear(int argc, char **argv);
110 static int lfs_hsm_action(int argc, char **argv);
111 static int lfs_hsm_archive(int argc, char **argv);
112 static int lfs_hsm_restore(int argc, char **argv);
113 static int lfs_hsm_release(int argc, char **argv);
114 static int lfs_hsm_remove(int argc, char **argv);
115 static int lfs_hsm_cancel(int argc, char **argv);
116 static int lfs_swap_layouts(int argc, char **argv);
117 static int lfs_mv(int argc, char **argv);
118 static int lfs_ladvise(int argc, char **argv);
119 static int lfs_mirror(int argc, char **argv);
120 static int lfs_mirror_list_commands(int argc, char **argv);
121 static int lfs_list_commands(int argc, char **argv);
122 static inline int lfs_mirror_resync(int argc, char **argv);
123 static inline int lfs_mirror_verify(int argc, char **argv);
125 enum setstripe_origin {
132 static int lfs_setstripe_internal(int argc, char **argv,
133 enum setstripe_origin opc);
135 static inline int lfs_setstripe(int argc, char **argv)
137 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
139 static inline int lfs_setstripe_migrate(int argc, char **argv)
141 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
143 static inline int lfs_mirror_create(int argc, char **argv)
145 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
147 static inline int lfs_mirror_extend(int argc, char **argv)
149 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
151 static inline int lfs_mirror_split(int argc, char **argv)
153 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
156 /* Setstripe and migrate share mostly the same parameters */
157 #define SSM_CMD_COMMON(cmd) \
158 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
159 " [--stripe-count|-c <stripe_count>]\n" \
160 " [--stripe-index|-i <start_ost_idx>]\n" \
161 " [--stripe-size|-S <stripe_size>]\n" \
162 " [--layout|-L <pattern>]\n" \
163 " [--pool|-p <pool_name>]\n" \
164 " [--ost|-o <ost_indices>]\n" \
165 " [--yaml|-y <yaml_template_file>]\n" \
166 " [--copy=<lustre_src>]\n"
168 #define SSM_HELP_COMMON \
169 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
170 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
171 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
172 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
173 "\t respectively)\n" \
174 "\tpool_name: Name of OST pool to use (default none)\n" \
175 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
176 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
177 "\t Indices be specified in a format of:\n" \
178 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
180 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
181 "\t If --pool is set with --ost, then the OSTs\n" \
182 "\t must be the members of the pool.\n" \
183 "\tcomp_end: Extent end of component, start after previous end.\n"\
184 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
185 "\t respectively, -1 for EOF). Must be a multiple of\n"\
186 "\t stripe_size.\n" \
187 "\tyaml_template_file:\n" \
188 "\t YAML layout template file, can't be used with -c,\n" \
189 "\t -i, -S, -p, -o, or -E arguments.\n" \
190 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
191 "\t another lustre file or directory, can't used with\n" \
192 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
194 #define MIRROR_CREATE_HELP \
195 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
196 "\t setstripe layout options\n" \
197 "\t It defaults to 1 if not specified; if specified,\n" \
198 "\t it must follow the option without a space.\n" \
199 "\t The option can also be repeated multiple times to\n" \
200 "\t separate mirrors that have different layouts.\n" \
201 "\tsetstripe options: Mirror layout\n" \
202 "\t It can be a plain layout or a composite layout.\n" \
203 "\t If not specified, the stripe options inherited\n" \
204 "\t from the previous component will be used.\n" \
205 "\tflags: set flags to the component of the current mirror.\n" \
206 "\t Only \"prefer\" flag is supported so far.\n"
208 #define MIRROR_EXTEND_HELP \
210 "\tvictim_file: The layout of victim_file will be split and used\n" \
211 "\t as a mirror added to the mirrored file.\n" \
212 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
213 "\t from victim file(s) in case the victim file(s)\n" \
214 "\t contains the same data as the original mirrored\n" \
217 #define MIRROR_EXTEND_USAGE \
218 " <--mirror-count|-N[mirror_count]>\n" \
219 " [setstripe options|-f <victim_file>]\n" \
222 #define SETSTRIPE_USAGE \
223 SSM_CMD_COMMON("setstripe") \
224 MIRROR_EXTEND_USAGE \
225 " <directory|filename>\n" \
229 #define MIGRATE_USAGE \
230 SSM_CMD_COMMON("migrate ") \
232 " [--non-block|-n]\n" \
233 " [--non-direct|-D]\n" \
237 "\tblock: Block file access during data migration (default)\n" \
238 "\tnon-block: Abort migrations if concurrent access is detected\n" \
239 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
241 #define SETDIRSTRIPE_USAGE \
242 " [--mdt-count|-c stripe_count>\n" \
243 " [--mdt-index|-i mdt_index]\n" \
244 " [--mdt-hash|-H mdt_hash]\n" \
245 " [--default|-D] [--mode|-m mode] <dir>\n" \
246 "\tstripe_count: stripe count of the striped directory\n" \
247 "\tmdt_index: MDT index of first stripe\n" \
248 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
249 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
250 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
251 "\tdefault_stripe: set default dirstripe of the directory\n" \
252 "\tmode: the mode of the directory\n"
255 * command_t mirror_cmdlist - lfs mirror commands.
257 command_t mirror_cmdlist[] = {
258 { .pc_name = "create", .pc_func = lfs_mirror_create,
259 .pc_help = "Create a mirrored file.\n"
260 "usage: lfs mirror create "
261 "<--mirror-count|-N[mirror_count]> "
262 "[setstripe options] ... <filename|directory>\n"
263 MIRROR_CREATE_HELP },
264 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
265 .pc_help = "Extend a mirrored file.\n"
266 "usage: lfs mirror extend "
267 "<--mirror-count|-N[mirror_count]> [--no-verify] "
268 "[setstripe options|-f <victim_file>] ... <filename>\n"
269 MIRROR_EXTEND_HELP },
270 { .pc_name = "split", .pc_func = lfs_mirror_split,
271 .pc_help = "Split a mirrored file.\n"
272 "usage: lfs mirror split <--mirror-id <mirror_id>> [--destroy|-d] "
273 "[-f <new_file>] <mirrored file>\n"
274 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
275 "\t can be fetched by lfs getstripe command.\n"
276 "\tnew_file: This option indicates the layout of the split\n"
277 "\t mirror will be stored into. If not specified,\n"
278 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
279 "\t will be used.\n" },
280 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
281 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
282 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
283 "<mirrored file> [<mirrored file2>...]\n"},
284 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
285 .pc_help = "Verify mirrored file(s).\n"
286 "usage: lfs mirror verify "
287 "[--only <mirror_id,mirror_id2[,...]>] "
288 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
289 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
290 .pc_help = "list commands supported by lfs mirror"},
291 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
292 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
293 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
297 /* all available commands */
298 command_t cmdlist[] = {
299 {"setstripe", lfs_setstripe, 0,
300 "To create a file with specified striping/composite layout, or\n"
301 "create/replace the default layout on an existing directory:\n"
302 SSM_CMD_COMMON("setstripe")
303 " <directory|filename>\n"
305 "To add component(s) to an existing composite file:\n"
306 SSM_CMD_COMMON("setstripe --component-add")
308 "To totally delete the default striping from an existing directory:\n"
309 "usage: setstripe -d <directory>\n"
311 "To delete the last component(s) from an existing composite file\n"
312 "(note that this will also delete any data in those components):\n"
313 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
314 " [--component-flags|-F <comp_flags>]\n"
316 "\tcomp_id: Unique component ID to delete\n"
317 "\tcomp_flags: 'init' indicating all instantiated components\n"
318 "\t '^init' indicating all uninstantiated components\n"
319 "\t-I and -F cannot be specified at the same time\n"},
320 {"getstripe", lfs_getstripe, 0,
321 "To list the layout pattern for a given file or files in a\n"
322 "directory or recursively for all files in a directory tree.\n"
323 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
324 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
325 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
326 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
327 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
328 " [--component-id[=comp_id]|-I[comp_id]]\n"
329 " [--component-flags[=comp_flags]]\n"
330 " [--component-count]\n"
331 " [--component-start[=[+-]comp_start]]\n"
332 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
333 " <directory|filename> ..."},
334 {"setdirstripe", lfs_setdirstripe, 0,
335 "To create a striped directory on a specified MDT. This can only\n"
336 "be done on MDT0 with the right of administrator.\n"
337 "usage: setdirstripe [OPTION] <directory>\n"
339 {"getdirstripe", lfs_getdirstripe, 0,
340 "To list the layout pattern info for a given directory\n"
341 "or recursively for all directories in a directory tree.\n"
342 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
343 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
344 " [--recursive|-r] [--yaml|-y]\n"
345 " [--default|-D] <dir> ..."},
346 {"mkdir", lfs_setdirstripe, 0,
347 "To create a striped directory on a specified MDT. This can only\n"
348 "be done on MDT0 with the right of administrator.\n"
349 "usage: mkdir [OPTION] <directory>\n"
351 {"rm_entry", lfs_rmentry, 0,
352 "To remove the name entry of the remote directory. Note: This\n"
353 "command will only delete the name entry, i.e. the remote directory\n"
354 "will become inaccessable after this command. This can only be done\n"
355 "by the administrator\n"
356 "usage: rm_entry <dir>\n"},
357 {"pool_list", lfs_poollist, 0,
358 "List pools or pool OSTs\n"
359 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
360 {"find", lfs_find, 0,
361 "find files matching given attributes recursively in directory tree.\n"
362 "usage: find <directory|filename> ...\n"
363 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
364 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
365 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
366 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
367 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
368 " [[!] --stripe-count|-c [+-]<stripes>]\n"
369 " [[!] --stripe-index|-i <index,...>]\n"
370 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
371 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
372 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
373 " [[!] --projid <projid>]\n"
374 " [[!] --layout|-L released,raid0,mdt]\n"
375 " [[!] --component-count [+-]<comp_cnt>]\n"
376 " [[!] --component-start [+-]N[kMGTPE]]\n"
377 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
378 " [[!] --component-flags <comp_flags>]\n"
379 " [[!] --mirror-count|-N [+-]<n>]\n"
380 " [[!] --mirror-state <[^]state>]\n"
381 " [[!] --mdt-count|-T [+-]<stripes>]\n"
382 " [[!] --mdt-hash|-H <hashtype>\n"
383 "\t !: used before an option indicates 'NOT' requested attribute\n"
384 "\t -: used before a value indicates less than requested value\n"
385 "\t +: used before a value indicates more than requested value\n"
386 "\thashtype: hash type of the striped directory.\n"
387 "\t fnv_1a_64 FNV-1a hash algorithm\n"
388 "\t all_char sum of characters % MDT_COUNT\n"},
389 {"check", lfs_check, 0,
390 "Display the status of MDS or OSTs (as specified in the command)\n"
391 "or all the servers (MDS and OSTs).\n"
392 "usage: check <osts|mds|servers>"},
393 {"osts", lfs_osts, 0, "list OSTs connected to client "
394 "[for specified path only]\n" "usage: osts [path]"},
395 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
396 "[for specified path only]\n" "usage: mdts [path]"},
398 "report filesystem disk space usage or inodes usage"
399 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
400 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
401 {"getname", lfs_getname, 0, "list instances and specified mount points "
402 "[for specified path only]\n"
403 "Usage: getname [-h]|[path ...] "},
404 #ifdef HAVE_SYS_QUOTA_H
405 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
406 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
407 " -b <block-softlimit> -B <block-hardlimit>\n"
408 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
409 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
410 " [--block-softlimit <block-softlimit>]\n"
411 " [--block-hardlimit <block-hardlimit>]\n"
412 " [--inode-softlimit <inode-softlimit>]\n"
413 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
414 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
415 " [--block-grace <block-grace>]\n"
416 " [--inode-grace <inode-grace>] <filesystem>\n"
417 " -b can be used instead of --block-softlimit/--block-grace\n"
418 " -B can be used instead of --block-hardlimit\n"
419 " -i can be used instead of --inode-softlimit/--inode-grace\n"
420 " -I can be used instead of --inode-hardlimit\n\n"
421 "Note: The total quota space will be split into many qunits and\n"
422 " balanced over all server targets, the minimal qunit size is\n"
423 " 1M bytes for block space and 1K inodes for inode space.\n\n"
424 " Quota space rebalancing process will stop when this mininum\n"
425 " value is reached. As a result, quota exceeded can be returned\n"
426 " while many targets still have 1MB or 1K inodes of spare\n"
428 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
429 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
431 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
432 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>"},
433 {"project", lfs_project, 0,
434 "Change or list project attribute for specified file or directory.\n"
435 "usage: project [-d|-r] <file|directory...>\n"
436 " list project ID and flags on file(s) or directories\n"
437 " project [-p id] [-s] [-r] <file|directory...>\n"
438 " set project ID and/or inherit flag for specified file(s) or directories\n"
439 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
440 " check project ID and flags on file(s) or directories, print outliers\n"
441 " project -C [-r] [-k] <file|directory...>\n"
442 " clear the project inherit flag and ID on the file or directory\n"
445 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
446 "usage: flushctx [-k] [mountpoint...]"},
448 "Remote user copy files and directories.\n"
449 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
451 "Remote user list directory contents.\n"
452 "usage: ls [OPTION]... [FILE]..."},
453 {"changelog", lfs_changelog, 0,
454 "Show the metadata changes on an MDT."
455 "\nusage: changelog <mdtname> [startrec [endrec]]"},
456 {"changelog_clear", lfs_changelog_clear, 0,
457 "Indicate that old changelog records up to <endrec> are no longer of "
458 "interest to consumer <id>, allowing the system to free up space.\n"
459 "An <endrec> of 0 means all records.\n"
460 "usage: changelog_clear <mdtname> <id> <endrec>"},
461 {"fid2path", lfs_fid2path, 0,
462 "Resolve the full path(s) for given FID(s). For a specific hardlink "
463 "specify link number <linkno>.\n"
464 /* "For a historical link name, specify changelog record <recno>.\n" */
465 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
466 /* [ --rec <recno> ] */ },
467 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
468 "usage: path2fid [--parents] <path> ..."},
469 {"data_version", lfs_data_version, 0, "Display file data version for "
470 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
471 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
472 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
473 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
474 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
475 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
476 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
478 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
479 "[--archived] [--lost] <file> ..."},
480 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
481 "given files.\n" "usage: hsm_action <file> ..."},
482 {"hsm_archive", lfs_hsm_archive, 0,
483 "Archive file to external storage.\n"
484 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
486 {"hsm_restore", lfs_hsm_restore, 0,
487 "Restore file from external storage.\n"
488 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
489 {"hsm_release", lfs_hsm_release, 0,
490 "Release files from Lustre.\n"
491 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
492 {"hsm_remove", lfs_hsm_remove, 0,
493 "Remove file copy from external storage.\n"
494 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
496 " (FILE [FILE ...] | "
497 "--mntpath MOUNTPATH FID [FID ...])\n"
499 "Note: To remove an archived copy of a file already deleted from a "
501 "--mntpath option and a list of FIDs must be specified"
503 {"hsm_cancel", lfs_hsm_cancel, 0,
504 "Cancel requests related to specified files.\n"
505 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
506 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
507 "usage: swap_layouts <path1> <path2>"},
508 {"migrate", lfs_setstripe_migrate, 0,
509 "migrate a directory between MDTs.\n"
510 "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
512 "\tmdt_idx: index of the destination MDT\n"
514 "migrate file objects from one OST "
515 "layout\nto another (may be not safe with concurent writes).\n"
517 "[--stripe-count|-c] <stripe_count>\n"
518 " [--stripe-index|-i] <start_ost_index>\n"
519 " [--stripe-size|-S] <stripe_size>\n"
520 " [--pool|-p] <pool_name>\n"
521 " [--ost|-o] <ost_indices>\n"
523 " [--non-block|-n]\n"
524 " [--non-direct|-D]\n"
525 " <file|directory>\n"
526 "\tstripe_count: number of OSTs to stripe a file over\n"
527 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
528 "\tstripe_size: number of bytes to store before moving to the next OST\n"
529 "\tpool_name: name of the predefined pool of OSTs\n"
530 "\tost_indices: OSTs to stripe over, in order\n"
531 "\tblock: wait for the operation to return before continuing\n"
532 "\tnon-block: do not wait for the operation to return\n"
533 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
535 "To move directories between MDTs. This command is deprecated, "
536 "use \"migrate\" instead.\n"
537 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
539 {"ladvise", lfs_ladvise, 0,
540 "Provide servers with advice about access patterns for a file.\n"
541 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
542 " [--background|-b] [--unset|-u]\n\n"
543 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
544 " {[--mode|-m [READ,WRITE]}\n"
546 {"mirror", lfs_mirror, mirror_cmdlist,
547 "lfs commands used to manage files with mirrored components:\n"
548 "lfs mirror create - create a mirrored file or directory\n"
549 "lfs mirror extend - add mirror(s) to an existing file\n"
550 "lfs mirror split - split a mirror from an existing mirrored file\n"
551 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
552 "lfs mirror verify - verify mirrored file(s)\n"},
553 {"help", Parser_help, 0, "help"},
554 {"exit", Parser_quit, 0, "quit"},
555 {"quit", Parser_quit, 0, "quit"},
556 {"--version", Parser_version, 0,
557 "output build version of the utility and exit"},
558 {"--list-commands", lfs_list_commands, 0,
559 "list commands supported by the utility and exit"},
564 static int check_hashtype(const char *hashtype)
568 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
569 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
576 static const char *error_loc = "syserror";
579 MIGRATION_NONBLOCK = 1 << 0,
580 MIGRATION_MIRROR = 1 << 1,
581 MIGRATION_NONDIRECT = 1 << 2,
584 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
585 struct llapi_layout *layout);
588 migrate_open_files(const char *name, __u64 migration_flags,
589 const struct llapi_stripe_param *param,
590 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
597 char parent[PATH_MAX];
598 char volatile_file[PATH_MAX];
604 if (param == NULL && layout == NULL) {
605 error_loc = "layout information";
609 /* search for file directory pathname */
610 if (strlen(name) > sizeof(parent) - 1) {
611 error_loc = "source file name";
615 strncpy(parent, name, sizeof(parent));
616 ptr = strrchr(parent, '/');
618 if (getcwd(parent, sizeof(parent)) == NULL) {
619 error_loc = "getcwd";
623 if (ptr == parent) /* leading '/' */
628 /* open file, direct io */
629 /* even if the file is only read, WR mode is nedeed to allow
630 * layout swap on fd */
632 if (!(migration_flags & MIGRATION_NONDIRECT))
634 fd = open(name, rflags);
637 error_loc = "cannot open source file";
641 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
643 error_loc = "cannot get MDT index";
648 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
649 mode_t open_mode = S_IRUSR | S_IWUSR;
651 random_value = random();
652 rc = snprintf(volatile_file, sizeof(volatile_file),
653 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
654 mdt_index, random_value);
655 if (rc >= sizeof(volatile_file)) {
660 /* create, open a volatile file, use caching (ie no directio) */
662 fdv = llapi_file_open_param(volatile_file, open_flags,
665 fdv = lfs_component_create(volatile_file, open_flags,
667 } while (fdv < 0 && (rc = fdv) == -EEXIST);
670 error_loc = "cannot create volatile file";
674 /* In case the MDT does not support creation of volatile files
675 * we should try to unlink it. */
676 (void)unlink(volatile_file);
678 /* Not-owner (root?) special case.
679 * Need to set owner/group of volatile file like original.
680 * This will allow to pass related check during layout_swap.
685 error_loc = "cannot stat source file";
689 rc = fstat(fdv, &stv);
692 error_loc = "cannot stat volatile";
696 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
697 rc = fchown(fdv, st.st_uid, st.st_gid);
700 error_loc = "cannot change ownwership of volatile";
719 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
721 struct llapi_layout *layout;
722 size_t buf_size = 4 * 1024 * 1024;
731 layout = llapi_layout_get_by_fd(fd_src, 0);
732 if (layout != NULL) {
733 uint64_t stripe_size;
735 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
737 buf_size = stripe_size;
739 llapi_layout_free(layout);
742 /* Use a page-aligned buffer for direct I/O */
743 rc = posix_memalign(&buf, getpagesize(), buf_size);
748 /* read new data only if we have written all
749 * previously read data */
752 rc = check_file(fd_src);
757 rsize = read(fd_src, buf, buf_size);
769 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
788 static int migrate_copy_timestamps(int fd, int fdv)
792 if (fstat(fd, &st) == 0) {
793 struct timeval tv[2] = {
794 {.tv_sec = st.st_atime},
795 {.tv_sec = st.st_mtime}
798 return futimes(fdv, tv);
804 static int migrate_block(int fd, int fdv)
811 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
813 error_loc = "cannot get dataversion";
821 /* The grouplock blocks all concurrent accesses to the file.
822 * It has to be taken after llapi_get_data_version as it would
824 rc = llapi_group_lock(fd, gid);
826 error_loc = "cannot get group lock";
830 rc = migrate_copy_data(fd, fdv, NULL);
832 error_loc = "data copy failed";
836 /* Make sure we keep original atime/mtime values */
837 rc = migrate_copy_timestamps(fd, fdv);
839 error_loc = "timestamp copy failed";
844 * for a migration we need to check data version on file did
847 * Pass in gid=0 since we already own grouplock. */
848 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
849 SWAP_LAYOUTS_CHECK_DV1);
851 error_loc = "file changed";
854 error_loc = "cannot swap layout";
859 rc2 = llapi_group_unlock(fd, gid);
860 if (rc2 < 0 && rc == 0) {
861 error_loc = "unlock group lock";
869 * Internal helper for migrate_copy_data(). Check lease and report error if
872 * \param[in] fd File descriptor on which to check the lease.
874 * \retval 0 Migration can keep on going.
875 * \retval -errno Error occurred, abort migration.
877 static int check_lease(int fd)
881 rc = llapi_lease_check(fd);
883 return 0; /* llapi_check_lease returns > 0 on success. */
888 static int migrate_nonblock(int fd, int fdv)
894 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
896 error_loc = "cannot get data version";
900 rc = migrate_copy_data(fd, fdv, check_lease);
902 error_loc = "data copy failed";
906 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
908 error_loc = "cannot get data version";
914 error_loc = "source file changed";
918 /* Make sure we keep original atime/mtime values */
919 rc = migrate_copy_timestamps(fd, fdv);
921 error_loc = "timestamp copy failed";
928 static int lfs_component_set(char *fname, int comp_id,
929 __u32 flags, __u32 neg_flags)
932 __u32 flags_array[2];
937 ids[count] = comp_id;
938 flags_array[count] = flags;
943 ids[count] = comp_id;
944 flags_array[count] = neg_flags | LCME_FL_NEG;
948 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
951 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
952 progname, comp_id, fname, flags, neg_flags);
957 static int lfs_component_del(char *fname, __u32 comp_id,
958 __u32 flags, __u32 neg_flags)
962 if (flags && neg_flags)
965 if (!flags && neg_flags)
966 flags = neg_flags | LCME_FL_NEG;
968 if ((flags && comp_id) || (!flags && !comp_id))
971 /* LCME_FL_INIT is the only supported flag in PFL */
973 if (flags & ~LCME_KNOWN_FLAGS) {
975 "%s setstripe: unknown flags %#x\n",
979 } else if (comp_id > LCME_ID_MAX) {
980 fprintf(stderr, "%s setstripe: invalid component id %u\n",
985 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
988 "%s setstripe: cannot delete component %#x from '%s': %s\n",
989 progname, comp_id, fname, strerror(errno));
993 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1000 rc = llapi_layout_file_comp_add(fname, layout);
1002 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1003 fname, strerror(errno));
1007 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1008 struct llapi_layout *layout)
1016 fd = lstat(fname, &st);
1017 if (fd == 0 && S_ISDIR(st.st_mode))
1018 open_flags = O_DIRECTORY | O_RDONLY;
1020 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1022 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1023 S_ISDIR(st.st_mode) ?
1024 "set default composite layout for" :
1025 "create composite file",
1026 fname, strerror(errno));
1030 static int lfs_migrate(char *name, __u64 migration_flags,
1031 struct llapi_stripe_param *param,
1032 struct llapi_layout *layout)
1038 rc = migrate_open_files(name, migration_flags, param, layout,
1043 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1044 /* Blocking mode (forced if servers do not support file lease).
1045 * It is also the default mode, since we cannot distinguish
1046 * between a broken lease and a server that does not support
1047 * atomic swap/close (LU-6785) */
1048 rc = migrate_block(fd, fdv);
1052 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1054 error_loc = "cannot get lease";
1058 rc = migrate_nonblock(fd, fdv);
1060 llapi_lease_release(fd);
1064 /* Atomically put lease, swap layouts and close.
1065 * for a migration we need to check data version on file did
1067 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1069 error_loc = "cannot swap layout";
1081 fprintf(stderr, "error: %s: %s: %s: %s\n",
1082 progname, name, error_loc, strerror(-rc));
1086 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1095 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1099 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1100 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1101 const char *comp_name = comp_flags_table[i].cfn_name;
1103 if (strcmp(name, comp_name) == 0) {
1104 *flags |= comp_flag;
1106 } else if (strncmp(name, "^", 1) == 0 &&
1107 strcmp(name + 1, comp_name) == 0) {
1108 *neg_flags |= comp_flag;
1113 llapi_printf(LLAPI_MSG_ERROR,
1114 "%s: component flag '%s' not supported\n",
1120 if (!*flags && !*neg_flags)
1123 /* don't allow to set and exclude the same flag */
1124 if (*flags & *neg_flags)
1130 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1138 if (strncmp(string, "^", 1) == 0) {
1139 *neg_state = llapi_layout_string_flags(string + 1);
1140 if (*neg_state != 0)
1143 *state = llapi_layout_string_flags(string);
1148 llapi_printf(LLAPI_MSG_ERROR,
1149 "%s: mirrored file state '%s' not supported\n",
1155 * struct mirror_args - Command-line arguments for mirror(s).
1156 * @m_count: Number of mirrors to be created with this layout.
1157 * @m_flags: Mirror level flags, only 'prefer' is supported.
1158 * @m_layout: Mirror layout.
1159 * @m_file: A victim file. Its layout will be split and used as a mirror.
1160 * @m_next: Point to the next node of the list.
1162 * Command-line arguments for mirror(s) will be parsed and stored in
1163 * a linked list that consists of this structure.
1165 struct mirror_args {
1168 struct llapi_layout *m_layout;
1170 struct mirror_args *m_next;
1173 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1178 rc = llapi_layout_comp_flags_get(layout, &flags);
1182 if (flags & LCME_FL_NEG) {
1183 fprintf(stderr, "error: %s: negative flags are not supported\n",
1188 if (flags & LCME_FL_STALE) {
1189 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1190 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1194 return LLAPI_LAYOUT_ITER_CONT;
1197 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1199 uint64_t start, end;
1203 /* LU-10112: do not support dom+flr in phase 1 */
1204 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1208 rc = llapi_layout_pattern_get(layout, &pattern);
1212 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1213 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1218 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1222 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1226 if (end != LUSTRE_EOF) {
1227 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1232 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1237 * enum mirror_flags - Flags for extending a mirrored file.
1238 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1239 * in case the victim file(s) contains the same data as the
1240 * original mirrored file.
1241 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1243 * Flags for extending a mirrored file.
1251 * mirror_create_sanity_check() - Check mirror list.
1252 * @list: A linked list that stores the mirror arguments.
1254 * This function does a sanity check on @list for creating
1257 * Return: 0 on success or a negative error code on failure.
1259 static int mirror_create_sanity_check(const char *fname,
1260 struct mirror_args *list)
1263 bool has_m_file = false;
1264 bool has_m_layout = false;
1270 struct llapi_layout *layout;
1272 layout = llapi_layout_get_by_path(fname, 0);
1275 "error: %s: file '%s' couldn't get layout\n",
1280 rc = mirror_sanity_check_one(layout);
1281 llapi_layout_free(layout);
1287 while (list != NULL) {
1288 if (list->m_file != NULL) {
1290 llapi_layout_free(list->m_layout);
1293 llapi_layout_get_by_path(list->m_file, 0);
1294 if (list->m_layout == NULL) {
1296 "error: %s: file '%s' has no layout\n",
1297 progname, list->m_file);
1301 has_m_layout = true;
1302 if (list->m_layout == NULL) {
1303 fprintf(stderr, "error: %s: no mirror layout\n",
1309 rc = mirror_sanity_check_one(list->m_layout);
1313 list = list->m_next;
1316 if (has_m_file && has_m_layout) {
1318 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1326 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1328 __u32 mirror_flags = *(__u32 *)cbdata;
1332 rc = llapi_layout_comp_flags_get(layout, &flags);
1337 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1342 return LLAPI_LAYOUT_ITER_CONT;
1346 * mirror_create() - Create a mirrored file.
1347 * @fname: The file to be created.
1348 * @mirror_list: A linked list that stores the mirror arguments.
1350 * This function creates a mirrored file @fname with the mirror(s)
1351 * from @mirror_list.
1353 * Return: 0 on success or a negative error code on failure.
1355 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1357 struct llapi_layout *layout = NULL;
1358 struct mirror_args *cur_mirror = NULL;
1359 uint16_t mirror_count = 0;
1363 rc = mirror_create_sanity_check(NULL, mirror_list);
1367 cur_mirror = mirror_list;
1368 while (cur_mirror != NULL) {
1369 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1371 &cur_mirror->m_flags);
1374 fprintf(stderr, "%s: failed to set mirror flags\n",
1379 for (i = 0; i < cur_mirror->m_count; i++) {
1380 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1383 fprintf(stderr, "error: %s: "
1384 "merge layout failed: %s\n",
1385 progname, strerror(errno));
1389 mirror_count += cur_mirror->m_count;
1390 cur_mirror = cur_mirror->m_next;
1393 if (layout == NULL) {
1394 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1398 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1401 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1402 progname, strerror(errno));
1406 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1414 llapi_layout_free(layout);
1419 * Compare files and check lease on @fd.
1421 * \retval bytes number of bytes are the same
1423 static ssize_t mirror_file_compare(int fd, int fdv)
1425 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1427 ssize_t bytes_done = 0;
1428 ssize_t bytes_read = 0;
1430 buf = malloc(buflen * 2);
1435 if (!llapi_lease_check(fd)) {
1436 bytes_done = -EBUSY;
1440 bytes_read = read(fd, buf, buflen);
1441 if (bytes_read <= 0)
1444 if (bytes_read != read(fdv, buf + buflen, buflen))
1447 /* XXX: should compute the checksum on each buffer and then
1448 * compare checksum to avoid cache collision */
1449 if (memcmp(buf, buf + buflen, bytes_read))
1452 bytes_done += bytes_read;
1460 static int mirror_extend_file(const char *fname, const char *victim_file,
1461 enum mirror_flags mirror_flags)
1466 struct stat stbuf_v;
1467 struct ll_ioc_lease *data = NULL;
1470 fd = open(fname, O_RDWR);
1472 error_loc = "open source file";
1477 fdv = open(victim_file, O_RDWR);
1479 error_loc = "open target file";
1484 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1485 error_loc = "stat source or target file";
1490 if (stbuf.st_dev != stbuf_v.st_dev) {
1491 error_loc = "stat source and target file";
1496 /* mirrors should be of the same size */
1497 if (stbuf.st_size != stbuf_v.st_size) {
1498 error_loc = "file sizes don't match";
1503 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1505 error_loc = "cannot get lease";
1509 if (!(mirror_flags & MF_NO_VERIFY)) {
1511 /* mirrors should have the same contents */
1512 ret = mirror_file_compare(fd, fdv);
1513 if (ret != stbuf.st_size) {
1514 error_loc = "file busy or contents don't match";
1515 rc = ret < 0 ? ret : -EINVAL;
1520 /* Get rid of caching pages from clients */
1521 rc = llapi_file_flush(fd);
1523 error_loc = "cannot get data version";
1527 rc = llapi_file_flush(fdv);
1529 error_loc = "cannot get data version";
1534 /* Make sure we keep original atime/mtime values */
1535 rc = migrate_copy_timestamps(fd, fdv);
1537 error_loc = "cannot copy timestamp";
1541 /* Atomically put lease, merge layouts and close. */
1542 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1544 error_loc = "memory allocation";
1547 data->lil_mode = LL_LEASE_UNLCK;
1548 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1549 data->lil_count = 1;
1550 data->lil_ids[0] = fdv;
1551 rc = llapi_lease_set(fd, data);
1553 error_loc = "cannot merge layout";
1555 } else if (rc == 0) {
1557 error_loc = "lost lease lock";
1570 (void) unlink(victim_file);
1572 fprintf(stderr, "error: %s: %s: %s: %s\n",
1573 progname, fname, error_loc, strerror(-rc));
1577 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1579 struct ll_ioc_lease *data = NULL;
1584 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1588 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1590 error_loc = "cannot get lease";
1594 rc = migrate_nonblock(fd, fdv);
1596 llapi_lease_release(fd);
1600 /* Atomically put lease, merge layouts and close. */
1601 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1603 error_loc = "memory allocation";
1606 data->lil_mode = LL_LEASE_UNLCK;
1607 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1608 data->lil_count = 1;
1609 data->lil_ids[0] = fdv;
1610 rc = llapi_lease_set(fd, data);
1612 error_loc = "cannot merge layout";
1614 } else if (rc == 0) {
1616 error_loc = "lost lease lock";
1629 fprintf(stderr, "error: %s: %s: %s: %s\n",
1630 progname, name, error_loc, strerror(-rc));
1634 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1635 enum mirror_flags mirror_flags)
1639 rc = mirror_create_sanity_check(fname, mirror_list);
1643 while (mirror_list) {
1644 if (mirror_list->m_file != NULL) {
1645 rc = mirror_extend_file(fname, mirror_list->m_file,
1648 __u32 mirror_count = mirror_list->m_count;
1650 while (mirror_count > 0) {
1651 rc = mirror_extend_layout(fname,
1652 mirror_list->m_layout);
1662 mirror_list = mirror_list->m_next;
1668 static int verify_id(struct llapi_layout *layout, void *cbdata)
1673 rc = llapi_layout_mirror_id_get(layout, &id);
1677 if ((__u16)id == *(__u16 *)cbdata)
1678 return LLAPI_LAYOUT_ITER_STOP;
1680 return LLAPI_LAYOUT_ITER_CONT;
1683 static int mirror_split(const char *fname, __u16 mirror_id,
1684 enum mirror_flags mflags, const char *victim_file)
1686 struct llapi_layout *layout;
1687 char parent[PATH_MAX];
1688 char victim[PATH_MAX];
1689 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1691 struct ll_ioc_lease *data;
1692 uint16_t mirror_count;
1697 /* check fname contains mirror with mirror_id */
1698 layout = llapi_layout_get_by_path(fname, 0);
1701 "error %s: file '%s' couldn't get layout\n",
1706 rc = mirror_sanity_check_one(layout);
1710 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1713 "error %s: file '%s' couldn't get mirror count\n",
1717 if (mirror_count < 2) {
1719 "error %s: file '%s' has %d component, cannot split\n",
1720 progname, fname, mirror_count);
1724 rc = llapi_layout_comp_iterate(layout, verify_id, &mirror_id);
1726 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1729 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1731 "error %s: file '%s' does not contain mirror with id %u\n",
1732 progname, fname, mirror_id);
1736 fd = open(fname, O_RDWR);
1739 "error %s: open file '%s' failed: %s\n",
1740 progname, fname, strerror(errno));
1744 /* get victim file directory pathname */
1745 if (strlen(fname) > sizeof(parent) - 1) {
1746 fprintf(stderr, "error %s: file name of '%s' too long\n",
1751 strncpy(parent, fname, sizeof(parent));
1752 ptr = strrchr(parent, '/');
1754 if (getcwd(parent, sizeof(parent)) == NULL) {
1755 fprintf(stderr, "error %s: getcwd failed: %s\n",
1756 progname, strerror(errno));
1766 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1768 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1773 if (victim_file == NULL) {
1774 /* use a temp file to store the splitted layout */
1775 if (mflags & MF_DESTROY) {
1776 fdv = llapi_create_volatile_idx(parent, mdt_index,
1777 O_LOV_DELAY_CREATE);
1779 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1781 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1784 /* user specified victim file */
1785 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1790 "error %s: create victim file failed: %s\n",
1791 progname, strerror(errno));
1795 /* get lease lock of fname */
1796 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1799 "error %s: cannot get lease of file '%s': %d\n",
1800 progname, fname, rc);
1804 /* Atomatically put lease, split layouts and close. */
1805 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1811 data->lil_mode = LL_LEASE_UNLCK;
1812 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1813 data->lil_count = 2;
1814 data->lil_ids[0] = fdv;
1815 data->lil_ids[1] = mirror_id;
1816 rc = llapi_lease_set(fd, data);
1818 if (rc == 0) /* lost lease lock */
1821 "error %s: cannot split '%s': %s\n",
1822 progname, fname, strerror(-rc));
1833 llapi_layout_free(layout);
1838 * Parse a string containing an target index list into an array of integers.
1840 * The input string contains a comma delimited list of individual
1841 * indices and ranges, for example "1,2-4,7". Add the indices into the
1842 * \a tgts array and remove duplicates.
1844 * \param[out] tgts array to store indices in
1845 * \param[in] size size of \a tgts array
1846 * \param[in] offset starting index in \a tgts
1847 * \param[in] arg string containing OST index list
1849 * \retval positive number of indices in \a tgts
1850 * \retval -EINVAL unable to parse \a arg
1852 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1856 int slots = size - offset;
1863 end_of_loop = false;
1864 while (!end_of_loop) {
1868 char *endptr = NULL;
1872 ptr = strchrnul(arg, ',');
1874 end_of_loop = *ptr == '\0';
1877 start_index = strtol(arg, &endptr, 0);
1878 if (endptr == arg) /* no data at all */
1880 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1883 end_index = start_index;
1884 if (*endptr == '-') {
1885 end_index = strtol(endptr + 1, &endptr, 0);
1886 if (*endptr != '\0')
1888 if (end_index < start_index)
1892 for (i = start_index; i <= end_index && slots > 0; i++) {
1895 /* remove duplicate */
1896 for (j = 0; j < offset; j++) {
1900 if (j == offset) { /* no duplicate */
1905 if (slots == 0 && i < end_index)
1913 if (!end_of_loop && ptr != NULL)
1916 return rc < 0 ? rc : nr;
1919 struct lfs_setstripe_args {
1920 unsigned long long lsa_comp_end;
1921 unsigned long long lsa_stripe_size;
1922 long long lsa_stripe_count;
1923 long long lsa_stripe_off;
1924 __u32 lsa_comp_flags;
1925 __u32 lsa_comp_neg_flags;
1926 unsigned long long lsa_pattern;
1927 unsigned int lsa_mirror_count;
1929 bool lsa_first_comp;
1931 char *lsa_pool_name;
1934 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1936 unsigned int mirror_count = lsa->lsa_mirror_count;
1937 bool first_comp = lsa->lsa_first_comp;
1939 memset(lsa, 0, sizeof(*lsa));
1941 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
1942 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
1943 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
1944 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
1945 lsa->lsa_pool_name = NULL;
1947 lsa->lsa_mirror_count = mirror_count;
1948 lsa->lsa_first_comp = first_comp;
1952 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
1953 * @lsa: Stripe options to be initialized and inherited.
1955 * This function initializes stripe options in @lsa and inherit
1956 * stripe_size, stripe_count and OST pool_name options.
1960 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
1962 unsigned long long stripe_size;
1963 long long stripe_count;
1964 char *pool_name = NULL;
1966 stripe_size = lsa->lsa_stripe_size;
1967 stripe_count = lsa->lsa_stripe_count;
1968 pool_name = lsa->lsa_pool_name;
1970 setstripe_args_init(lsa);
1972 lsa->lsa_stripe_size = stripe_size;
1973 lsa->lsa_stripe_count = stripe_count;
1974 lsa->lsa_pool_name = pool_name;
1977 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
1979 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
1980 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
1981 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
1982 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
1983 lsa->lsa_pool_name != NULL ||
1984 lsa->lsa_comp_end != 0);
1988 * comp_args_to_layout() - Create or extend a composite layout.
1989 * @composite: Pointer to the composite layout.
1990 * @lsa: Stripe options for the new component.
1992 * This function creates or extends a composite layout by adding a new
1993 * component with stripe options from @lsa.
1995 * Return: 0 on success or an error code on failure.
1997 static int comp_args_to_layout(struct llapi_layout **composite,
1998 struct lfs_setstripe_args *lsa,
2001 struct llapi_layout *layout = *composite;
2002 uint64_t prev_end = 0;
2005 if (layout == NULL) {
2006 layout = llapi_layout_alloc();
2007 if (layout == NULL) {
2008 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2012 *composite = layout;
2016 /* Get current component extent, current component
2017 * must be the tail component. */
2018 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2020 fprintf(stderr, "Get comp extent failed. %s\n",
2025 if (lsa->lsa_first_comp)
2028 if (lsa->lsa_first_comp)
2029 rc = llapi_layout_add_first_comp(layout);
2031 rc = llapi_layout_comp_add(layout);
2033 fprintf(stderr, "Add component failed. %s\n",
2038 /* reset lsa_first_comp */
2039 lsa->lsa_first_comp = false;
2042 rc = llapi_layout_comp_extent_set(layout, prev_end,
2045 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2046 prev_end, lsa->lsa_comp_end, strerror(errno));
2051 /* Data-on-MDT component setting */
2052 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2053 /* In case of Data-on-MDT patterns the only extra option
2054 * applicable is stripe size option. */
2055 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2056 fprintf(stderr, "Option 'stripe-count' can't be "
2057 "specified with Data-on-MDT component: %lld\n",
2058 lsa->lsa_stripe_count);
2061 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2062 fprintf(stderr, "Option 'stripe-size' can't be "
2063 "specified with Data-on-MDT component: %llu\n",
2064 lsa->lsa_stripe_size);
2067 if (lsa->lsa_nr_tgts != 0) {
2068 fprintf(stderr, "Option 'ost-list' can't be specified "
2069 "with Data-on-MDT component: '%i'\n",
2073 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2074 fprintf(stderr, "Option 'stripe-offset' can't be "
2075 "specified with Data-on-MDT component: %lld\n",
2076 lsa->lsa_stripe_off);
2079 if (lsa->lsa_pool_name != 0) {
2080 fprintf(stderr, "Option 'pool' can't be specified "
2081 "with Data-on-MDT component: '%s'\n",
2082 lsa->lsa_pool_name);
2086 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2088 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2089 lsa->lsa_pattern, strerror(errno));
2092 /* Data-on-MDT component has always single stripe up to end */
2093 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2096 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2098 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2099 lsa->lsa_stripe_size, strerror(errno));
2103 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2105 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2106 lsa->lsa_stripe_count, strerror(errno));
2110 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2112 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2113 lsa->lsa_comp_flags, strerror(errno));
2117 if (lsa->lsa_pool_name != NULL) {
2118 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2120 fprintf(stderr, "Set pool name: %s failed. %s\n",
2121 lsa->lsa_pool_name, strerror(errno));
2125 rc = llapi_layout_pool_name_set(layout, "");
2127 fprintf(stderr, "Clear pool name failed: %s\n",
2133 if (lsa->lsa_nr_tgts > 0) {
2134 if (lsa->lsa_stripe_count > 0 &&
2135 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2136 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2137 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2138 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2139 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2142 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2143 rc = llapi_layout_ost_index_set(layout, i,
2148 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2149 lsa->lsa_stripe_off != -1) {
2150 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2153 fprintf(stderr, "Set ost index %d failed. %s\n",
2154 i, strerror(errno));
2161 static int build_component(struct llapi_layout **layout,
2162 struct lfs_setstripe_args *lsa, bool set_extent)
2166 rc = comp_args_to_layout(layout, lsa, set_extent);
2170 if (lsa->lsa_mirror_count > 0) {
2171 rc = llapi_layout_mirror_count_set(*layout,
2172 lsa->lsa_mirror_count);
2176 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2179 lsa->lsa_mirror_count = 0;
2185 static int build_layout_from_yaml_node(struct cYAML *node,
2186 struct llapi_layout **layout,
2187 struct lfs_setstripe_args *lsa,
2194 string = node->cy_string;
2195 /* skip leading lmm_ if present, to simplify parsing */
2196 if (string != NULL && strncmp(string, "lmm_", 4) == 0)
2199 if (node->cy_type == CYAML_TYPE_STRING) {
2200 if (!strcmp(string, "lcme_extent.e_end")) {
2201 if (!strcmp(node->cy_valuestring, "EOF") ||
2202 !strcmp(node->cy_valuestring, "eof"))
2203 lsa->lsa_comp_end = LUSTRE_EOF;
2204 } else if (!strcmp(string, "pool")) {
2205 lsa->lsa_pool_name = node->cy_valuestring;
2206 } else if (!strcmp(string, "pattern")) {
2207 if (!strcmp(node->cy_valuestring, "mdt"))
2208 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2210 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2211 if (!strcmp(string, "lcm_mirror_count")) {
2212 lsa->lsa_mirror_count = node->cy_valueint;
2213 } else if (!strcmp(string, "lcme_extent.e_start")) {
2214 if (node->cy_valueint != 0 || *layout != NULL) {
2215 rc = build_component(layout, lsa, true);
2220 if (node->cy_valueint == 0)
2221 lsa->lsa_first_comp = true;
2223 /* initialize lsa */
2224 setstripe_args_init(lsa);
2225 lsa->lsa_tgts = osts;
2226 } else if (!strcmp(string, "lcme_extent.e_end")) {
2227 if (node->cy_valueint == -1)
2228 lsa->lsa_comp_end = LUSTRE_EOF;
2230 lsa->lsa_comp_end = node->cy_valueint;
2231 } else if (!strcmp(string, "stripe_count")) {
2232 lsa->lsa_stripe_count = node->cy_valueint;
2233 } else if (!strcmp(string, "stripe_size")) {
2234 lsa->lsa_stripe_size = node->cy_valueint;
2235 } else if (!strcmp(string, "stripe_offset")) {
2236 lsa->lsa_stripe_off = node->cy_valueint;
2237 } else if (!strcmp(string, "l_ost_idx")) {
2238 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2241 } else if (node->cy_type == CYAML_TYPE_OBJECT) {
2242 /* go deep to sub blocks */
2243 rc = build_layout_from_yaml_node(node->cy_child, layout,
2248 node = node->cy_next;
2254 static int lfs_comp_create_from_yaml(char *template,
2255 struct llapi_layout **layout,
2256 struct lfs_setstripe_args *lsa,
2259 struct cYAML *tree = NULL, *err_rc = NULL;
2262 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2264 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2265 progname, template);
2266 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2267 "can't parse", &err_rc);
2268 cYAML_print_tree2file(stderr, err_rc);
2269 cYAML_free_tree(err_rc);
2274 /* initialize lsa for plain file */
2275 setstripe_args_init(lsa);
2276 lsa->lsa_tgts = osts;
2278 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2280 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2281 progname, template);
2284 rc = build_component(layout, lsa, *layout != NULL);
2286 /* clean clean lsa */
2287 setstripe_args_init(lsa);
2291 cYAML_free_tree(tree);
2295 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2296 * end of the last component in the existing file, and adjust the
2297 * first extent start of the components to be added accordingly. */
2298 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2300 struct llapi_layout *head;
2301 uint64_t start, end, stripe_size, prev_end = 0;
2304 if (layout == NULL) {
2306 "%s setstripe: layout must be specified\n",
2312 head = llapi_layout_get_by_path(fname, 0);
2315 "%s setstripe: cannot read layout from '%s': %s\n",
2316 progname, fname, strerror(errno));
2318 } else if (errno == ENODATA) {
2319 /* file without LOVEA, this component-add will be turned
2320 * into a component-create. */
2321 llapi_layout_free(head);
2323 } else if (!llapi_layout_is_composite(head)) {
2324 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2326 llapi_layout_free(head);
2330 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2332 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2333 progname, strerror(errno));
2334 llapi_layout_free(head);
2338 llapi_layout_free(head);
2340 /* Make sure we use the first component of the layout to be added. */
2341 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2344 "%s setstripe: cannot move component cursor: %s\n",
2345 progname, strerror(errno));
2349 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2351 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2352 progname, strerror(errno));
2356 if (start > prev_end || end <= prev_end) {
2358 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2359 progname, start, end, prev_end);
2363 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2365 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2366 progname, strerror(errno));
2370 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2371 (prev_end & (stripe_size - 1))) {
2373 "%s setstripe: stripe size %lu not aligned with %lu\n",
2374 progname, stripe_size, prev_end);
2378 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2381 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2382 progname, prev_end, end, strerror(errno));
2389 static inline bool arg_is_eof(char *arg)
2391 return !strncmp(arg, "-1", strlen("-1")) ||
2392 !strncmp(arg, "EOF", strlen("EOF")) ||
2393 !strncmp(arg, "eof", strlen("eof"));
2397 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2399 * Return: Valid mirror_args pointer on success and
2400 * NULL if memory allocation fails.
2402 static struct mirror_args *lfs_mirror_alloc(void)
2404 struct mirror_args *mirror = NULL;
2407 mirror = calloc(1, sizeof(*mirror));
2418 * lfs_mirror_free() - Free memory allocated for a mirror argument
2420 * @mirror: Previously allocated mirror argument structure by
2421 * lfs_mirror_alloc().
2423 * Free memory allocated for @mirror.
2427 static void lfs_mirror_free(struct mirror_args *mirror)
2429 if (mirror->m_layout != NULL)
2430 llapi_layout_free(mirror->m_layout);
2435 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2436 * @mirror_list: Previously allocated mirror list.
2438 * Free memory allocated for @mirror_list.
2442 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2444 struct mirror_args *next_mirror = NULL;
2446 while (mirror_list != NULL) {
2447 next_mirror = mirror_list->m_next;
2448 lfs_mirror_free(mirror_list);
2449 mirror_list = next_mirror;
2461 LFS_COMP_NO_VERIFY_OPT,
2463 LFS_MIRROR_FLAGS_OPT,
2465 LFS_MIRROR_STATE_OPT,
2470 static int lfs_setstripe_internal(int argc, char **argv,
2471 enum setstripe_origin opc)
2473 struct lfs_setstripe_args lsa = { 0 };
2474 struct llapi_stripe_param *param = NULL;
2475 struct find_param migrate_mdt_param = {
2485 char *mdt_idx_arg = NULL;
2486 unsigned long long size_units = 1;
2487 bool migrate_mode = false;
2488 bool migration_block = false;
2489 __u64 migration_flags = 0;
2490 __u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
2491 int comp_del = 0, comp_set = 0;
2494 struct llapi_layout *layout = NULL;
2495 struct llapi_layout **lpp = &layout;
2496 bool mirror_mode = false;
2497 bool has_m_file = false;
2498 __u32 mirror_count = 0;
2499 enum mirror_flags mirror_flags = 0;
2500 struct mirror_args *mirror_list = NULL;
2501 struct mirror_args *new_mirror = NULL;
2502 struct mirror_args *last_mirror = NULL;
2503 __u16 mirror_id = 0;
2505 bool from_yaml = false;
2506 bool from_copy = false;
2507 char *template = NULL;
2509 struct option long_opts[] = {
2510 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2511 /* --block is only valid in migrate mode */
2512 { .val = 'b', .name = "block", .has_arg = no_argument },
2513 { .val = LFS_COMP_ADD_OPT,
2514 .name = "comp-add", .has_arg = no_argument },
2515 { .val = LFS_COMP_ADD_OPT,
2516 .name = "component-add", .has_arg = no_argument },
2517 { .val = LFS_COMP_DEL_OPT,
2518 .name = "comp-del", .has_arg = no_argument },
2519 { .val = LFS_COMP_DEL_OPT,
2520 .name = "component-del", .has_arg = no_argument },
2521 { .val = LFS_COMP_FLAGS_OPT,
2522 .name = "comp-flags", .has_arg = required_argument },
2523 { .val = LFS_COMP_FLAGS_OPT,
2524 .name = "component-flags",
2525 .has_arg = required_argument },
2526 { .val = LFS_COMP_SET_OPT,
2527 .name = "comp-set", .has_arg = no_argument },
2528 { .val = LFS_COMP_SET_OPT,
2529 .name = "component-set",
2530 .has_arg = no_argument},
2531 { .val = LFS_COMP_NO_VERIFY_OPT,
2532 .name = "no-verify", .has_arg = no_argument},
2533 { .val = LFS_MIRROR_FLAGS_OPT,
2534 .name = "flags", .has_arg = required_argument},
2535 { .val = LFS_MIRROR_ID_OPT,
2536 .name = "mirror-id", .has_arg = required_argument},
2537 { .val = LFS_LAYOUT_COPY,
2538 .name = "copy", .has_arg = required_argument},
2539 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2540 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2541 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2542 { .val = 'd', .name = "delete", .has_arg = no_argument},
2543 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2544 /* --non-direct is only valid in migrate mode */
2545 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2546 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2547 { .val = 'E', .name = "component-end",
2548 .has_arg = required_argument},
2549 { .val = 'f', .name = "file", .has_arg = required_argument },
2550 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2551 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2552 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2553 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
2554 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2555 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2556 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2557 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2558 { .val = 'L', .name = "layout", .has_arg = required_argument },
2559 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2560 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2561 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2562 /* --non-block is only valid in migrate mode */
2563 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2564 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2565 { .val = 'o', .name = "ost", .has_arg = required_argument },
2566 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2567 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2568 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2570 { .val = 'p', .name = "pool", .has_arg = required_argument },
2571 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2572 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2573 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2574 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2575 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2576 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2577 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2578 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2579 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2580 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2581 /* --verbose is only valid in migrate mode */
2582 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2583 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2586 setstripe_args_init(&lsa);
2588 migrate_mode = (opc == SO_MIGRATE);
2589 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2591 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2593 while ((c = getopt_long(argc, argv, "bc:dDE:f:i:I:m:N::no:p:L:s:S:vy:",
2594 long_opts, NULL)) >= 0) {
2599 case LFS_COMP_ADD_OPT:
2602 case LFS_COMP_DEL_OPT:
2605 case LFS_COMP_FLAGS_OPT:
2606 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2607 &lsa.lsa_comp_neg_flags);
2610 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2611 fprintf(stderr, "%s: inverted flags are not supported\n",
2615 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2617 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2624 case LFS_COMP_SET_OPT:
2627 case LFS_COMP_NO_VERIFY_OPT:
2628 mirror_flags |= MF_NO_VERIFY;
2630 case LFS_MIRROR_ID_OPT:
2631 mirror_id = strtoul(optarg, &end, 0);
2632 if (*end != '\0' || mirror_id == 0) {
2634 "%s %s: invalid mirror ID '%s'\n",
2635 progname, argv[0], optarg);
2639 case LFS_MIRROR_FLAGS_OPT: {
2642 if (!mirror_mode || !last_mirror) {
2643 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2648 result = comp_str2flags(optarg, &last_mirror->m_flags,
2654 fprintf(stderr, "%s: inverted flags are not supported\n",
2659 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2661 "%s: unsupported mirror flags: %s\n",
2668 case LFS_LAYOUT_COPY:
2673 if (!migrate_mode) {
2675 "%s %s: -b|--block valid only for migrate command\n",
2679 migration_block = true;
2682 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2685 "%s %s: invalid stripe count '%s'\n",
2686 progname, argv[0], optarg);
2690 if (lsa.lsa_stripe_count == -1)
2691 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2694 /* delete the default striping pattern */
2696 if (opc == SO_MIRROR_SPLIT) {
2699 "%s %s: -d cannot used with -f\n",
2703 mirror_flags |= MF_DESTROY;
2707 if (!migrate_mode) {
2709 "%s %s: -D|--non-direct is valid "
2710 "only for migrate command\n",
2714 migration_flags |= MIGRATION_NONDIRECT;
2717 if (lsa.lsa_comp_end != 0) {
2718 result = comp_args_to_layout(lpp, &lsa, true);
2721 "%s %s: invalid layout\n",
2726 setstripe_args_init_inherit(&lsa);
2729 if (arg_is_eof(optarg)) {
2730 lsa.lsa_comp_end = LUSTRE_EOF;
2732 result = llapi_parse_size(optarg,
2737 "%s %s: invalid component end '%s'\n",
2738 progname, argv[0], optarg);
2744 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2747 "%s %s: invalid stripe offset '%s'\n",
2748 progname, argv[0], optarg);
2751 if (lsa.lsa_stripe_off == -1)
2752 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2755 comp_id = strtoul(optarg, &end, 0);
2756 if (*end != '\0' || comp_id == 0 ||
2757 comp_id > LCME_ID_MAX) {
2759 "%s %s: invalid component ID '%s'\n",
2760 progname, argv[0], optarg);
2765 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2767 "error: %s: invalid option: %s\n",
2768 progname, argv[optopt + 1]);
2771 if (opc == SO_MIRROR_EXTEND) {
2772 if (last_mirror == NULL) {
2774 "error: %s: '-N' must exist in front of '%s'\n",
2775 progname, argv[optopt + 1]);
2778 last_mirror->m_file = optarg;
2779 last_mirror->m_count = 1;
2782 if (mirror_list == NULL)
2783 mirror_list = lfs_mirror_alloc();
2784 mirror_list->m_file = optarg;
2789 if (strcmp(argv[optind - 1], "mdt") == 0) {
2790 /* Can be only the first component */
2791 if (layout != NULL) {
2793 fprintf(stderr, "error: 'mdt' layout "
2794 "can be only the first one\n");
2797 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2799 fprintf(stderr, "error: 'mdt' layout "
2800 "size is too big\n");
2803 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2804 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2806 fprintf(stderr, "error: layout '%s' is "
2807 "unknown, supported layouts are: "
2808 "'mdt', 'raid0'\n", argv[optind]);
2813 if (!migrate_mode) {
2815 "%s %s: -m|--mdt-index valid only for migrate command\n",
2819 mdt_idx_arg = optarg;
2822 if (!migrate_mode) {
2824 "%s %s: -n|--non-block valid only for migrate command\n",
2828 migration_flags |= MIGRATION_NONBLOCK;
2831 if (opc == SO_SETSTRIPE) {
2832 opc = SO_MIRROR_CREATE;
2836 if (optarg != NULL) {
2837 mirror_count = strtoul(optarg, &end, 0);
2838 if (*end != '\0' || mirror_count == 0) {
2840 "error: %s: bad mirror count: %s\n",
2847 new_mirror = lfs_mirror_alloc();
2848 new_mirror->m_count = mirror_count;
2850 if (mirror_list == NULL)
2851 mirror_list = new_mirror;
2853 if (last_mirror != NULL) {
2854 /* wrap up last mirror */
2855 if (lsa.lsa_comp_end == 0)
2856 lsa.lsa_comp_end = LUSTRE_EOF;
2858 result = comp_args_to_layout(lpp, &lsa, true);
2860 lfs_mirror_free(new_mirror);
2864 setstripe_args_init_inherit(&lsa);
2866 last_mirror->m_next = new_mirror;
2869 last_mirror = new_mirror;
2870 lpp = &last_mirror->m_layout;
2873 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2874 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2875 fprintf(stderr, "warning: '--ost-list' is "
2876 "deprecated, use '--ost' instead\n");
2878 lsa.lsa_nr_tgts = parse_targets(osts,
2879 sizeof(osts) / sizeof(__u32),
2880 lsa.lsa_nr_tgts, optarg);
2881 if (lsa.lsa_nr_tgts < 0) {
2883 "%s %s: invalid OST target(s) '%s'\n",
2884 progname, argv[0], optarg);
2888 lsa.lsa_tgts = osts;
2889 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2890 lsa.lsa_stripe_off = osts[0];
2895 lsa.lsa_pool_name = optarg;
2897 if (strlen(lsa.lsa_pool_name) == 0 ||
2898 strncmp(lsa.lsa_pool_name, "none",
2899 LOV_MAXPOOLNAME) == 0)
2900 lsa.lsa_pool_name = NULL;
2903 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
2907 "%s %s: invalid stripe size '%s'\n",
2908 progname, argv[0], optarg);
2913 if (!migrate_mode) {
2915 "%s %s: -v|--verbose valid only for migrate command\n",
2919 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
2926 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
2927 progname, argv[0], argv[optind - 1]);
2932 fname = argv[optind];
2934 if (optind == argc) {
2935 fprintf(stderr, "%s %s: FILE must be specified\n",
2940 if (mirror_mode && mirror_count == 0) {
2942 "error: %s: --mirror-count|-N option is required\n",
2949 if (lsa.lsa_comp_end == 0)
2950 lsa.lsa_comp_end = LUSTRE_EOF;
2953 if (lsa.lsa_comp_end != 0) {
2954 result = comp_args_to_layout(lpp, &lsa, true);
2959 if (mirror_flags & MF_NO_VERIFY) {
2960 if (opc != SO_MIRROR_EXTEND) {
2962 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
2966 } else if (!has_m_file) {
2968 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
2975 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
2976 * altered by user space tool, so we don't need to support the
2977 * --component-set for this moment. */
2978 if (comp_set && !comp_id) {
2979 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
2984 if ((delete + comp_set + comp_del + comp_add) > 1) {
2986 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
2991 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
2992 lsa.lsa_comp_flags != 0 || layout != NULL)) {
2994 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
2999 if ((comp_set || comp_del) &&
3000 (setstripe_args_specified(&lsa) || layout != NULL)) {
3002 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3007 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3009 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3014 if (comp_add || comp_del) {
3017 result = lstat(fname, &st);
3018 if (result == 0 && S_ISDIR(st.st_mode)) {
3020 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3026 fprintf(stderr, "error: %s: can't use --component-add "
3027 "or --component-del for mirror operation\n",
3034 if (layout == NULL) {
3036 "%s %s: option -E must be specified with --component-add\n",
3041 result = adjust_first_extent(fname, layout);
3042 if (result == -ENODATA)
3044 else if (result != 0)
3048 if (from_yaml && from_copy) {
3050 "%s: can't specify --yaml and --copy together\n",
3055 if ((from_yaml || from_copy) &&
3056 (setstripe_args_specified(&lsa) || layout != NULL)) {
3057 fprintf(stderr, "error: %s: can't specify --yaml with "
3058 "-c, -S, -i, -o, -p or -E options.\n",
3063 if (mdt_idx_arg != NULL && optind > 3) {
3065 "%s %s: option -m cannot be used with other options\n",
3070 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3072 "%s %s: options --non-block and --block are mutually exclusive\n",
3077 if (!comp_del && !comp_set && comp_id != 0) {
3079 "%s %s: option -I can only be used with --component-del\n",
3084 if (mdt_idx_arg != NULL) {
3085 /* initialize migrate mdt parameters */
3086 migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
3088 fprintf(stderr, "%s %s: invalid MDT index '%s'\n",
3089 progname, argv[0], mdt_idx_arg);
3092 migrate_mdt_param.fp_migrate = 1;
3093 } else if (layout == NULL) {
3094 /* initialize stripe parameters */
3095 param = calloc(1, offsetof(typeof(*param),
3096 lsp_osts[lsa.lsa_nr_tgts]));
3097 if (param == NULL) {
3099 "%s %s: cannot allocate memory for parameters: %s\n",
3100 progname, argv[0], strerror(ENOMEM));
3105 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3106 param->lsp_stripe_size = lsa.lsa_stripe_size;
3107 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3108 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3109 param->lsp_stripe_count = -1;
3111 param->lsp_stripe_count = lsa.lsa_stripe_count;
3113 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3114 param->lsp_stripe_offset = -1;
3116 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3117 param->lsp_pool = lsa.lsa_pool_name;
3118 param->lsp_is_specific = false;
3119 if (lsa.lsa_nr_tgts > 0) {
3120 if (lsa.lsa_stripe_count > 0 &&
3121 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3122 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3123 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3124 fprintf(stderr, "error: %s: stripe count %lld "
3125 "doesn't match the number of OSTs: %d\n"
3126 , argv[0], lsa.lsa_stripe_count,
3132 param->lsp_is_specific = true;
3133 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3134 memcpy(param->lsp_osts, osts,
3135 sizeof(*osts) * lsa.lsa_nr_tgts);
3140 /* generate a layout from a YAML template */
3141 result = lfs_comp_create_from_yaml(template, &layout,
3144 fprintf(stderr, "error: %s: can't create composite "
3145 "layout from template file %s\n",
3149 } else if (from_copy) {
3150 layout = llapi_layout_get_by_path(template, 0);
3151 if (layout == NULL) {
3153 "%s: can't create composite layout from file %s.\n",
3154 progname, template);
3159 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3160 if (mdt_idx_arg != NULL) {
3161 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3162 } else if (migrate_mode) {
3163 result = lfs_migrate(fname, migration_flags, param,
3165 } else if (comp_set != 0) {
3166 result = lfs_component_set(fname, comp_id,
3168 lsa.lsa_comp_neg_flags);
3169 } else if (comp_del != 0) {
3170 result = lfs_component_del(fname, comp_id,
3172 lsa.lsa_comp_neg_flags);
3173 } else if (comp_add != 0) {
3174 result = lfs_component_add(fname, layout);
3175 } else if (opc == SO_MIRROR_CREATE) {
3176 result = mirror_create(fname, mirror_list);
3177 } else if (opc == SO_MIRROR_EXTEND) {
3178 result = mirror_extend(fname, mirror_list,
3180 } else if (opc == SO_MIRROR_SPLIT) {
3181 if (mirror_id == 0) {
3183 "%s %s: no mirror id is specified\n",
3187 result = mirror_split(fname, mirror_id, mirror_flags,
3188 has_m_file ? mirror_list->m_file :
3190 } else if (layout != NULL) {
3191 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3198 result = llapi_file_open_param(fname,
3207 /* Save the first error encountered. */
3215 llapi_layout_free(layout);
3216 lfs_mirror_list_free(mirror_list);
3221 llapi_layout_free(layout);
3222 lfs_mirror_list_free(mirror_list);
3226 static int lfs_poollist(int argc, char **argv)
3231 return llapi_poollist(argv[1]);
3234 static int set_time(time_t *time, time_t *set, char *str)
3242 else if (str[0] == '-')
3248 t = strtol(str, &endptr, 0);
3249 if (*endptr != '\0') {
3251 "%s find: bad time '%s': %s\n",
3252 progname, str, strerror(EINVAL));
3255 if (*time < t * 24 * 60 * 60) {
3259 "%s find: bad time '%s': too large\n",
3264 *set = *time - t * 24 * 60 * 60;
3268 static int name2uid(unsigned int *id, const char *name)
3270 struct passwd *passwd;
3272 passwd = getpwnam(name);
3275 *id = passwd->pw_uid;
3280 static int name2gid(unsigned int *id, const char *name)
3282 struct group *group;
3284 group = getgrnam(name);
3287 *id = group->gr_gid;
3292 static inline int name2projid(unsigned int *id, const char *name)
3297 static int uid2name(char **name, unsigned int id)
3299 struct passwd *passwd;
3301 passwd = getpwuid(id);
3304 *name = passwd->pw_name;
3309 static inline int gid2name(char **name, unsigned int id)
3311 struct group *group;
3313 group = getgrgid(id);
3316 *name = group->gr_name;
3321 static int name2layout(__u32 *layout, char *name)
3323 char *ptr, *layout_name;
3326 for (ptr = name; ; ptr = NULL) {
3327 layout_name = strtok(ptr, ",");
3328 if (layout_name == NULL)
3330 if (strcmp(layout_name, "released") == 0)
3331 *layout |= LOV_PATTERN_F_RELEASED;
3332 else if (strcmp(layout_name, "raid0") == 0)
3333 *layout |= LOV_PATTERN_RAID0;
3334 else if (strcmp(layout_name, "mdt") == 0)
3335 *layout |= LOV_PATTERN_MDT;
3342 static int lfs_find(int argc, char **argv)
3347 struct find_param param = {
3351 struct option long_opts[] = {
3352 { .val = 'A', .name = "atime", .has_arg = required_argument },
3353 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3354 { .val = LFS_COMP_COUNT_OPT,
3355 .name = "comp-count", .has_arg = required_argument },
3356 { .val = LFS_COMP_COUNT_OPT,
3357 .name = "component-count",
3358 .has_arg = required_argument },
3359 { .val = LFS_COMP_FLAGS_OPT,
3360 .name = "comp-flags", .has_arg = required_argument },
3361 { .val = LFS_COMP_FLAGS_OPT,
3362 .name = "component-flags",
3363 .has_arg = required_argument },
3364 { .val = LFS_COMP_START_OPT,
3365 .name = "comp-start", .has_arg = required_argument },
3366 { .val = LFS_COMP_START_OPT,
3367 .name = "component-start",
3368 .has_arg = required_argument },
3369 { .val = LFS_MIRROR_STATE_OPT,
3370 .name = "mirror-state", .has_arg = required_argument },
3371 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3372 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3373 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3374 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3375 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3376 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3377 { .val = 'E', .name = "component-end",
3378 .has_arg = required_argument },
3379 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3380 { .val = 'g', .name = "gid", .has_arg = required_argument },
3381 { .val = 'G', .name = "group", .has_arg = required_argument },
3382 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3383 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3384 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3385 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3386 { .val = 'L', .name = "layout", .has_arg = required_argument },
3387 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3388 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3389 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3390 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3391 { .val = 'n', .name = "name", .has_arg = required_argument },
3392 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3393 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3394 { .val = 'O', .name = "obd", .has_arg = required_argument },
3395 { .val = 'O', .name = "ost", .has_arg = required_argument },
3396 /* no short option for pool yet, can be 'p' after 2.18 */
3397 { .val = LFS_POOL_OPT,
3398 .name = "pool", .has_arg = required_argument },
3399 { .val = '0', .name = "print0", .has_arg = no_argument },
3400 { .val = 'P', .name = "print", .has_arg = no_argument },
3401 { .val = LFS_PROJID_OPT,
3402 .name = "projid", .has_arg = required_argument },
3403 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3404 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3405 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3406 { .val = 's', .name = "size", .has_arg = required_argument },
3407 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3408 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3409 { .val = 't', .name = "type", .has_arg = required_argument },
3410 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3411 { .val = 'u', .name = "uid", .has_arg = required_argument },
3412 { .val = 'U', .name = "user", .has_arg = required_argument },
3413 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3414 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3426 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3427 while ((c = getopt_long_only(argc, argv,
3428 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3429 long_opts, NULL)) >= 0) {
3434 /* '!' is part of option */
3435 /* when getopt_long_only() finds a string which is not
3436 * an option nor a known option argument it returns 1
3437 * in that case if we already have found pathstart and pathend
3438 * (i.e. we have the list of pathnames),
3439 * the only supported value is "!"
3441 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3442 if (!isoption && pathend != -1) {
3443 fprintf(stderr, "err: %s: filename|dirname must either "
3444 "precede options or follow options\n",
3449 if (!isoption && pathstart == -1)
3450 pathstart = optind - 1;
3451 if (isoption && pathstart != -1 && pathend == -1)
3452 pathend = optind - 2;
3458 /* unknown; opt is "!" or path component,
3459 * checking done above.
3461 if (strcmp(optarg, "!") == 0)
3465 xtime = ¶m.fp_atime;
3466 xsign = ¶m.fp_asign;
3467 param.fp_exclude_atime = !!neg_opt;
3468 /* no break, this falls through to 'C' for ctime */
3471 xtime = ¶m.fp_ctime;
3472 xsign = ¶m.fp_csign;
3473 param.fp_exclude_ctime = !!neg_opt;
3475 /* no break, this falls through to 'M' for mtime */
3478 xtime = ¶m.fp_mtime;
3479 xsign = ¶m.fp_msign;
3480 param.fp_exclude_mtime = !!neg_opt;
3482 rc = set_time(&t, xtime, optarg);
3483 if (rc == INT_MAX) {
3491 if (optarg[0] == '+') {
3492 param.fp_blocks_sign = -1;
3494 } else if (optarg[0] == '-') {
3495 param.fp_blocks_sign = 1;
3499 param.fp_blocks_units = 1024;
3500 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3501 ¶m.fp_blocks_units, 0);
3503 fprintf(stderr, "error: bad blocks '%s'\n",
3507 param.fp_check_blocks = 1;
3508 param.fp_exclude_blocks = !!neg_opt;
3510 case LFS_COMP_COUNT_OPT:
3511 if (optarg[0] == '+') {
3512 param.fp_comp_count_sign = -1;
3514 } else if (optarg[0] == '-') {
3515 param.fp_comp_count_sign = 1;
3519 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3520 if (*endptr != '\0') {
3521 fprintf(stderr, "error: bad component count "
3525 param.fp_check_comp_count = 1;
3526 param.fp_exclude_comp_count = !!neg_opt;
3528 case LFS_COMP_FLAGS_OPT:
3529 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3530 ¶m.fp_comp_neg_flags);
3532 fprintf(stderr, "error: bad component flags "
3536 param.fp_check_comp_flags = 1;
3538 __u32 flags = param.fp_comp_neg_flags;
3539 param.fp_comp_neg_flags = param.fp_comp_flags;
3540 param.fp_comp_flags = flags;
3543 case LFS_COMP_START_OPT:
3544 if (optarg[0] == '+') {
3545 param.fp_comp_start_sign = -1;
3547 } else if (optarg[0] == '-') {
3548 param.fp_comp_start_sign = 1;
3552 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3553 ¶m.fp_comp_start_units, 0);
3555 fprintf(stderr, "error: bad component start "
3559 param.fp_check_comp_start = 1;
3560 param.fp_exclude_comp_start = !!neg_opt;
3562 case LFS_MIRROR_STATE_OPT:
3563 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3564 ¶m.fp_mirror_neg_state);
3567 "error: bad mirrored file state '%s'\n",
3571 param.fp_check_mirror_state = 1;
3573 __u16 state = param.fp_mirror_neg_state;
3574 param.fp_mirror_neg_state =
3575 param.fp_mirror_state;
3576 param.fp_mirror_state = state;
3580 if (optarg[0] == '+') {
3581 param.fp_stripe_count_sign = -1;
3583 } else if (optarg[0] == '-') {
3584 param.fp_stripe_count_sign = 1;
3588 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3589 if (*endptr != '\0') {
3590 fprintf(stderr,"error: bad stripe_count '%s'\n",
3595 param.fp_check_stripe_count = 1;
3596 param.fp_exclude_stripe_count = !!neg_opt;
3599 param.fp_max_depth = strtol(optarg, 0, 0);
3602 if (optarg[0] == '+') {
3603 param.fp_comp_end_sign = -1;
3605 } else if (optarg[0] == '-') {
3606 param.fp_comp_end_sign = 1;
3610 if (arg_is_eof(optarg)) {
3611 param.fp_comp_end = LUSTRE_EOF;
3612 param.fp_comp_end_units = 1;
3615 rc = llapi_parse_size(optarg,
3617 ¶m.fp_comp_end_units, 0);
3620 fprintf(stderr, "error: bad component end "
3624 param.fp_check_comp_end = 1;
3625 param.fp_exclude_comp_end = !!neg_opt;
3629 rc = name2gid(¶m.fp_gid, optarg);
3631 param.fp_gid = strtoul(optarg, &endptr, 10);
3632 if (*endptr != '\0') {
3633 fprintf(stderr, "Group/GID: %s cannot "
3634 "be found.\n", optarg);
3639 param.fp_exclude_gid = !!neg_opt;
3640 param.fp_check_gid = 1;
3643 param.fp_hash_type = check_hashtype(optarg);
3644 if (param.fp_hash_type == 0) {
3645 fprintf(stderr, "error: bad hash_type '%s'\n",
3650 param.fp_check_hash_type = 1;
3651 param.fp_exclude_hash_type = !!neg_opt;
3654 ret = name2layout(¶m.fp_layout, optarg);
3657 param.fp_exclude_layout = !!neg_opt;
3658 param.fp_check_layout = 1;
3662 rc = name2uid(¶m.fp_uid, optarg);
3664 param.fp_uid = strtoul(optarg, &endptr, 10);
3665 if (*endptr != '\0') {
3666 fprintf(stderr, "User/UID: %s cannot "
3667 "be found.\n", optarg);
3672 param.fp_exclude_uid = !!neg_opt;
3673 param.fp_check_uid = 1;
3676 param.fp_pattern = (char *)optarg;
3677 param.fp_exclude_pattern = !!neg_opt;
3680 if (optarg[0] == '+') {
3681 param.fp_mirror_count_sign = -1;
3683 } else if (optarg[0] == '-') {
3684 param.fp_mirror_count_sign = 1;
3688 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3689 if (*endptr != '\0') {
3691 "error: bad mirror count '%s'\n",
3695 param.fp_check_mirror_count = 1;
3696 param.fp_exclude_mirror_count = !!neg_opt;
3701 char *buf, *token, *next, *p;
3705 buf = strdup(optarg);
3711 param.fp_exclude_obd = !!neg_opt;
3714 while (token && *token) {
3715 token = strchr(token, ',');
3722 param.fp_exclude_mdt = !!neg_opt;
3723 param.fp_num_alloc_mdts += len;
3724 tmp = realloc(param.fp_mdt_uuid,
3725 param.fp_num_alloc_mdts *
3726 sizeof(*param.fp_mdt_uuid));
3732 param.fp_mdt_uuid = tmp;
3734 param.fp_exclude_obd = !!neg_opt;
3735 param.fp_num_alloc_obds += len;
3736 tmp = realloc(param.fp_obd_uuid,
3737 param.fp_num_alloc_obds *
3738 sizeof(*param.fp_obd_uuid));
3744 param.fp_obd_uuid = tmp;
3746 for (token = buf; token && *token; token = next) {
3747 struct obd_uuid *puuid;
3750 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3753 ¶m.fp_obd_uuid[param.fp_num_obds++];
3755 p = strchr(token, ',');
3762 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3767 strncpy(puuid->uuid, token,
3768 sizeof(puuid->uuid));
3775 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3779 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3781 "Pool name %s is too long (max %d)\n",
3782 optarg, LOV_MAXPOOLNAME);
3787 * We do check for empty pool because empty pool
3788 * is used to find V1 LOV attributes
3790 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3791 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3792 param.fp_exclude_pool = !!neg_opt;
3793 param.fp_check_pool = 1;
3795 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3796 case 'p': /* want this for --pool, to match getstripe/find */
3798 "warning: -p deprecated, use --print0 or -0\n");
3801 param.fp_zero_end = 1;
3803 case 'P': /* we always print, this option is a no-op */
3805 case LFS_PROJID_OPT:
3806 rc = name2projid(¶m.fp_projid, optarg);
3808 param.fp_projid = strtoul(optarg, &endptr, 10);
3809 if (*endptr != '\0') {
3811 "Invalid project ID: %s",
3817 param.fp_exclude_projid = !!neg_opt;
3818 param.fp_check_projid = 1;
3821 if (optarg[0] == '+') {
3822 param.fp_size_sign = -1;
3824 } else if (optarg[0] == '-') {
3825 param.fp_size_sign = 1;
3829 ret = llapi_parse_size(optarg, ¶m.fp_size,
3830 ¶m.fp_size_units, 0);
3832 fprintf(stderr, "error: bad file size '%s'\n",
3836 param.fp_check_size = 1;
3837 param.fp_exclude_size = !!neg_opt;
3840 if (optarg[0] == '+') {
3841 param.fp_stripe_size_sign = -1;
3843 } else if (optarg[0] == '-') {
3844 param.fp_stripe_size_sign = 1;
3848 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3849 ¶m.fp_stripe_size_units, 0);
3851 fprintf(stderr, "error: bad stripe_size '%s'\n",
3855 param.fp_check_stripe_size = 1;
3856 param.fp_exclude_stripe_size = !!neg_opt;
3859 param.fp_exclude_type = !!neg_opt;
3860 switch (optarg[0]) {
3862 param.fp_type = S_IFBLK;
3865 param.fp_type = S_IFCHR;
3868 param.fp_type = S_IFDIR;
3871 param.fp_type = S_IFREG;
3874 param.fp_type = S_IFLNK;
3877 param.fp_type = S_IFIFO;
3880 param.fp_type = S_IFSOCK;
3883 fprintf(stderr, "error: %s: bad type '%s'\n",
3890 if (optarg[0] == '+') {
3891 param.fp_mdt_count_sign = -1;
3893 } else if (optarg[0] == '-') {
3894 param.fp_mdt_count_sign = 1;
3898 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
3899 if (*endptr != '\0') {
3900 fprintf(stderr, "error: bad mdt_count '%s'\n",
3905 param.fp_check_mdt_count = 1;
3906 param.fp_exclude_mdt_count = !!neg_opt;
3914 if (pathstart == -1) {
3915 fprintf(stderr, "error: %s: no filename|pathname\n",
3919 } else if (pathend == -1) {
3925 rc = llapi_find(argv[pathstart], ¶m);
3926 if (rc != 0 && ret == 0)
3928 } while (++pathstart < pathend);
3931 fprintf(stderr, "error: %s failed for %s.\n",
3932 argv[0], argv[optind - 1]);
3934 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
3935 free(param.fp_obd_uuid);
3937 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
3938 free(param.fp_mdt_uuid);
3943 static int lfs_getstripe_internal(int argc, char **argv,
3944 struct find_param *param)
3946 struct option long_opts[] = {
3947 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3948 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
3949 { .val = LFS_COMP_COUNT_OPT,
3950 .name = "comp-count", .has_arg = no_argument },
3951 { .val = LFS_COMP_COUNT_OPT,
3952 .name = "component-count", .has_arg = no_argument },
3953 { .val = LFS_COMP_FLAGS_OPT,
3954 .name = "comp-flags", .has_arg = optional_argument },
3955 { .val = LFS_COMP_FLAGS_OPT,
3956 .name = "component-flags", .has_arg = optional_argument },
3957 { .val = LFS_COMP_START_OPT,
3958 .name = "comp-start", .has_arg = optional_argument },
3959 { .val = LFS_COMP_START_OPT,
3960 .name = "component-start", .has_arg = optional_argument },
3961 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
3962 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
3963 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
3964 { .val = 'd', .name = "directory", .has_arg = no_argument },
3965 { .val = 'D', .name = "default", .has_arg = no_argument },
3966 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
3967 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
3968 { .val = 'F', .name = "fid", .has_arg = no_argument },
3969 { .val = 'g', .name = "generation", .has_arg = no_argument },
3970 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3971 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
3972 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
3973 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
3974 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
3975 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
3976 { .val = 'L', .name = "layout", .has_arg = no_argument },
3977 { .val = 'm', .name = "mdt", .has_arg = no_argument },
3978 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
3979 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
3980 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
3981 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
3982 { .val = 'O', .name = "obd", .has_arg = required_argument },
3983 { .val = 'O', .name = "ost", .has_arg = required_argument },
3984 { .val = 'p', .name = "pool", .has_arg = no_argument },
3985 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3986 { .val = 'q', .name = "quiet", .has_arg = no_argument },
3987 { .val = 'r', .name = "recursive", .has_arg = no_argument },
3988 { .val = 'R', .name = "raw", .has_arg = no_argument },
3989 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
3990 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
3991 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3992 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3993 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3994 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3995 { .val = 'v', .name = "verbose", .has_arg = no_argument },
3996 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4001 while ((c = getopt_long(argc, argv, "cdDE::FghiI::LmMoO:pqrRsSvy",
4002 long_opts, NULL)) != -1) {
4005 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4006 param->fp_verbose |= VERBOSE_COUNT;
4007 param->fp_max_depth = 0;
4010 case LFS_COMP_COUNT_OPT:
4011 param->fp_verbose |= VERBOSE_COMP_COUNT;
4012 param->fp_max_depth = 0;
4014 case LFS_COMP_FLAGS_OPT:
4015 if (optarg != NULL) {
4016 rc = comp_str2flags(optarg,
4017 ¶m->fp_comp_flags,
4018 ¶m->fp_comp_neg_flags);
4020 fprintf(stderr, "error: %s bad "
4021 "component flags '%s'.\n",
4025 param->fp_check_comp_flags = 1;
4027 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4028 param->fp_max_depth = 0;
4031 case LFS_COMP_START_OPT:
4032 if (optarg != NULL) {
4034 if (tmp[0] == '+') {
4035 param->fp_comp_start_sign = -1;
4037 } else if (tmp[0] == '-') {
4038 param->fp_comp_start_sign = 1;
4041 rc = llapi_parse_size(tmp,
4042 ¶m->fp_comp_start,
4043 ¶m->fp_comp_start_units, 0);
4045 fprintf(stderr, "error: %s bad "
4046 "component start '%s'.\n",
4050 param->fp_check_comp_start = 1;
4053 param->fp_verbose |= VERBOSE_COMP_START;
4054 param->fp_max_depth = 0;
4058 param->fp_max_depth = 0;
4061 param->fp_get_default_lmv = 1;
4064 if (optarg != NULL) {
4066 if (tmp[0] == '+') {
4067 param->fp_comp_end_sign = -1;
4069 } else if (tmp[0] == '-') {
4070 param->fp_comp_end_sign = 1;
4074 if (arg_is_eof(tmp)) {
4075 param->fp_comp_end = LUSTRE_EOF;
4076 param->fp_comp_end_units = 1;
4079 rc = llapi_parse_size(tmp,
4080 ¶m->fp_comp_end,
4081 ¶m->fp_comp_end_units, 0);
4084 fprintf(stderr, "error: %s bad "
4085 "component end '%s'.\n",
4089 param->fp_check_comp_end = 1;
4091 param->fp_verbose |= VERBOSE_COMP_END;
4092 param->fp_max_depth = 0;
4096 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4097 param->fp_verbose |= VERBOSE_DFID;
4098 param->fp_max_depth = 0;
4102 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4103 param->fp_verbose |= VERBOSE_GENERATION;
4104 param->fp_max_depth = 0;
4108 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4109 param->fp_verbose |= VERBOSE_OFFSET;
4110 param->fp_max_depth = 0;
4114 if (optarg != NULL) {
4115 param->fp_comp_id = strtoul(optarg, &end, 0);
4116 if (*end != '\0' || param->fp_comp_id == 0 ||
4117 param->fp_comp_id > LCME_ID_MAX) {
4118 fprintf(stderr, "error: %s bad "
4119 "component id '%s'\n",
4123 param->fp_check_comp_id = 1;
4126 param->fp_max_depth = 0;
4127 param->fp_verbose |= VERBOSE_COMP_ID;
4131 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4132 param->fp_verbose |= VERBOSE_LAYOUT;
4133 param->fp_max_depth = 0;
4136 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4138 fprintf(stderr, "warning: '-M' deprecated"
4139 ", use '--mdt-index' or '-m' instead\n");
4142 if (!(param->fp_verbose & VERBOSE_DETAIL))
4143 param->fp_max_depth = 0;
4144 param->fp_verbose |= VERBOSE_MDTINDEX;
4147 if (param->fp_obd_uuid) {
4149 "error: %s: only one obduuid allowed",
4153 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4156 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4157 param->fp_verbose |= VERBOSE_POOL;
4158 param->fp_max_depth = 0;
4165 param->fp_recursive = 1;
4171 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4172 param->fp_verbose |= VERBOSE_SIZE;
4173 param->fp_max_depth = 0;
4177 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4190 if (param->fp_recursive)
4191 param->fp_max_depth = -1;
4192 else if (param->fp_verbose & VERBOSE_DETAIL)
4193 param->fp_max_depth = 1;
4195 if (!param->fp_verbose)
4196 param->fp_verbose = VERBOSE_DEFAULT;
4197 if (param->fp_quiet)
4198 param->fp_verbose = VERBOSE_OBJID;
4201 rc = llapi_getstripe(argv[optind], param);
4202 } while (++optind < argc && !rc);
4205 fprintf(stderr, "error: %s failed for %s.\n",
4206 argv[0], argv[optind - 1]);
4210 static int lfs_tgts(int argc, char **argv)
4212 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4213 struct find_param param;
4214 int index = 0, rc=0;
4219 if (argc == 2 && !realpath(argv[1], path)) {
4221 fprintf(stderr, "error: invalid path '%s': %s\n",
4222 argv[1], strerror(-rc));
4226 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4227 /* Check if we have a mount point */
4228 if (mntdir[0] == '\0')
4231 memset(¶m, 0, sizeof(param));
4232 if (!strcmp(argv[0], "mdts"))
4233 param.fp_get_lmv = 1;
4235 rc = llapi_ostlist(mntdir, ¶m);
4237 fprintf(stderr, "error: %s: failed on %s\n",
4240 if (path[0] != '\0')
4242 memset(mntdir, 0, PATH_MAX);
4248 static int lfs_getstripe(int argc, char **argv)
4250 struct find_param param = { 0 };
4252 param.fp_max_depth = 1;
4253 return lfs_getstripe_internal(argc, argv, ¶m);
4257 static int lfs_getdirstripe(int argc, char **argv)
4259 struct find_param param = { 0 };
4260 struct option long_opts[] = {
4261 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4262 { .val = 'D', .name = "default", .has_arg = no_argument },
4263 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4264 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4265 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4266 { .val = 'O', .name = "obd", .has_arg = required_argument },
4267 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4268 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4269 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4273 param.fp_get_lmv = 1;
4275 while ((c = getopt_long(argc, argv,
4276 "cDHimO:rtTy", long_opts, NULL)) != -1)
4281 param.fp_verbose |= VERBOSE_COUNT;
4284 param.fp_get_default_lmv = 1;
4288 param.fp_verbose |= VERBOSE_OFFSET;
4290 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4292 fprintf(stderr, "warning: '-t' deprecated, "
4293 "use '--mdt-hash' or '-H' instead\n");
4296 param.fp_verbose |= VERBOSE_HASH_TYPE;
4299 if (param.fp_obd_uuid) {
4301 "error: %s: only one obduuid allowed",
4305 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4308 param.fp_recursive = 1;
4321 if (param.fp_recursive)
4322 param.fp_max_depth = -1;
4324 if (!param.fp_verbose)
4325 param.fp_verbose = VERBOSE_DEFAULT;
4328 rc = llapi_getstripe(argv[optind], ¶m);
4329 } while (++optind < argc && !rc);
4332 fprintf(stderr, "error: %s failed for %s.\n",
4333 argv[0], argv[optind - 1]);
4338 MNTDF_INODES = 0x0001,
4339 MNTDF_COOKED = 0x0002,
4340 MNTDF_LAZY = 0x0004,
4341 MNTDF_VERBOSE = 0x0008,
4342 MNTDF_SHOW = 0x0010,
4345 #define COOK(value) \
4348 while (value > 1024) { \
4356 #define CDF "%11llu"
4357 #define HDF "%8.1f%c"
4361 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4363 double avail, used, ratio = 0;
4365 avail = st->os_bavail;
4366 used = st->os_blocks - st->os_bfree;
4367 if (avail + used > 0)
4368 ratio = used / (used + avail) * 100 + 0.5;
4373 static int showdf(char *mntdir, struct obd_statfs *stat,
4374 char *uuid, enum mntdf_flags flags,
4375 char *type, int index, int rc)
4377 long long avail, used, total;
4379 char *suffix = "KMGTPEZY";
4380 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4381 char tbuf[3 * sizeof(__u64)];
4382 char ubuf[3 * sizeof(__u64)];
4383 char abuf[3 * sizeof(__u64)];
4384 char rbuf[3 * sizeof(__u64)];
4391 if (flags & MNTDF_INODES) {
4392 avail = stat->os_ffree;
4393 used = stat->os_files - stat->os_ffree;
4394 total = stat->os_files;
4396 int shift = flags & MNTDF_COOKED ? 0 : 10;
4398 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4399 used = ((stat->os_blocks - stat->os_bfree) *
4400 stat->os_bsize) >> shift;
4401 total = (stat->os_blocks * stat->os_bsize) >> shift;
4404 ratio = obd_statfs_ratio(stat);
4406 if (flags & MNTDF_COOKED) {
4410 cook_val = (double)total;
4413 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4416 snprintf(tbuf, sizeof(tbuf), CDF, total);
4418 cook_val = (double)used;
4421 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4424 snprintf(ubuf, sizeof(ubuf), CDF, used);
4426 cook_val = (double)avail;
4429 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4432 snprintf(abuf, sizeof(abuf), CDF, avail);
4434 snprintf(tbuf, sizeof(tbuf), CDF, total);
4435 snprintf(ubuf, sizeof(tbuf), CDF, used);
4436 snprintf(abuf, sizeof(tbuf), CDF, avail);
4439 sprintf(rbuf, RDF, ratio);
4440 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4441 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4443 printf("[%s:%d]", type, index);
4445 if (stat->os_state) {
4447 * Each character represents the matching
4450 const char state_names[] = "DRSI";
4455 for (i = 0, state = stat->os_state;
4456 state && i < sizeof(state_names); i++) {
4457 if (!(state & (1 << i)))
4459 printf("%c", state_names[i]);
4467 printf(UUF": inactive device\n", uuid);
4470 printf(UUF": %s\n", uuid, strerror(-rc));
4477 struct ll_stat_type {
4482 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4484 struct ll_statfs_data {
4486 struct obd_statfs sd_st;
4489 struct ll_statfs_buf {
4491 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4494 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4495 int ops, struct ll_statfs_buf *lsb)
4497 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4498 struct obd_uuid uuid_buf;
4499 char *poolname = NULL;
4500 struct ll_stat_type types[] = {
4501 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4502 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4503 { .st_name = NULL } };
4504 struct ll_stat_type *tp;
4505 __u64 ost_ffree = 0;
4513 poolname = strchr(pool, '.');
4514 if (poolname != NULL) {
4515 if (strncmp(fsname, pool, strlen(fsname))) {
4516 fprintf(stderr, "filesystem name incorrect\n");
4524 fd = open(mntdir, O_RDONLY);
4527 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4532 if (flags & MNTDF_SHOW) {
4533 if (flags & MNTDF_INODES)
4534 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4535 "UUID", "Inodes", "IUsed", "IFree",
4536 "IUse%", "Mounted on");
4538 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4540 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4541 "Used", "Available", "Use%", "Mounted on");
4544 for (tp = types; tp->st_name != NULL; tp++) {
4545 if (!(tp->st_op & ops))
4548 for (index = 0; ; index++) {
4549 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4550 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4551 type = flags & MNTDF_LAZY ?
4552 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4553 rc2 = llapi_obd_fstatfs(fd, type, index,
4554 &stat_buf, &uuid_buf);
4559 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4560 if (!(flags & MNTDF_VERBOSE))
4562 } else if (rc2 < 0 && rc == 0) {
4566 if (poolname && tp->st_op == LL_STATFS_LOV &&
4567 llapi_search_ost(fsname, poolname,
4568 obd_uuid2str(&uuid_buf)) != 1)
4571 /* the llapi_obd_statfs() call may have returned with
4572 * an error, but if it filled in uuid_buf we will at
4573 * lease use that to print out a message for that OBD.
4574 * If we didn't get anything in the uuid_buf, then fill
4575 * it in so that we can print an error message. */
4576 if (uuid_buf.uuid[0] == '\0')
4577 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4578 "%s%04x", tp->st_name, index);
4580 lsb->sb_buf[lsb->sb_count].sd_index = index;
4581 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4584 if (flags & MNTDF_SHOW)
4585 showdf(mntdir, &stat_buf,
4586 obd_uuid2str(&uuid_buf), flags,
4587 tp->st_name, index, rc2);
4590 if (tp->st_op == LL_STATFS_LMV) {
4591 sum.os_ffree += stat_buf.os_ffree;
4592 sum.os_files += stat_buf.os_files;
4593 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4594 sum.os_blocks += stat_buf.os_blocks *
4596 sum.os_bfree += stat_buf.os_bfree *
4598 sum.os_bavail += stat_buf.os_bavail *
4600 ost_ffree += stat_buf.os_ffree;
4608 /* If we don't have as many objects free on the OST as inodes
4609 * on the MDS, we reduce the total number of inodes to
4610 * compensate, so that the "inodes in use" number is correct.
4611 * Matches ll_statfs_internal() so the results are consistent. */
4612 if (ost_ffree < sum.os_ffree) {
4613 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4614 sum.os_ffree = ost_ffree;
4616 if (flags & MNTDF_SHOW) {
4618 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4625 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4627 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4629 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4631 int r1 = obd_statfs_ratio(st1);
4632 int r2 = obd_statfs_ratio(st2);
4633 int64_t result = r1 - r2;
4635 /* if both space usage are above 90, compare free inodes */
4636 if (r1 > 90 && r2 > 90)
4637 result = st2->os_ffree - st1->os_ffree;
4641 else if (result == 0)
4648 static int lfs_setdirstripe(int argc, char **argv)
4652 struct lfs_setstripe_args lsa = { 0 };
4653 struct llapi_stripe_param *param = NULL;
4654 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4657 char *mode_opt = NULL;
4658 bool default_stripe = false;
4659 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4660 mode_t previous_mode = 0;
4661 bool delete = false;
4662 struct ll_statfs_buf *lsb = NULL;
4663 char mntdir[PATH_MAX] = "";
4664 bool auto_distributed = false;
4666 struct option long_opts[] = {
4667 { .val = 'c', .name = "count", .has_arg = required_argument },
4668 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4669 { .val = 'd', .name = "delete", .has_arg = no_argument },
4670 { .val = 'D', .name = "default", .has_arg = no_argument },
4671 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4672 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4673 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4674 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4675 { .val = 'i', .name = "index", .has_arg = required_argument },
4677 { .val = 'o', .name = "mode", .has_arg = required_argument },
4678 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4679 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4681 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4682 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4685 setstripe_args_init(&lsa);
4687 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4695 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4698 "%s %s: invalid stripe count '%s'\n",
4699 progname, argv[0], optarg);
4705 default_stripe = true;
4708 default_stripe = true;
4710 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4712 fprintf(stderr, "warning: '--hash-type' and '-t' "
4713 "deprecated, use '--mdt-hash' or '-H' instead\n");
4716 lsa.lsa_pattern = check_hashtype(optarg);
4717 if (lsa.lsa_pattern == 0) {
4719 "%s %s: bad stripe hash type '%s'\n",
4720 progname, argv[0], optarg);
4725 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4726 if (strcmp(argv[optind - 1], "--index") == 0)
4728 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4731 lsa.lsa_nr_tgts = parse_targets(mdts,
4732 sizeof(mdts) / sizeof(__u32),
4733 lsa.lsa_nr_tgts, optarg);
4734 if (lsa.lsa_nr_tgts < 0) {
4736 "%s %s: invalid MDT target(s) '%s'\n",
4737 progname, argv[0], optarg);
4741 lsa.lsa_tgts = mdts;
4742 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4743 lsa.lsa_stripe_off = mdts[0];
4745 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4747 fprintf(stderr, "warning: '-m' is deprecated, "
4748 "use '--mode' or '-o' instead\n");
4754 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4755 progname, argv[0], argv[optind - 1]);
4760 if (optind == argc) {
4761 fprintf(stderr, "%s %s: DIR must be specified\n",
4766 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
4767 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
4769 "%s %s: stripe offset and count must be specified\n",
4775 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
4776 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
4778 "%s %s: cannot specify -d with -c or -i options\n",
4783 if (mode_opt != NULL) {
4784 mode = strtoul(mode_opt, &end, 8);
4787 "%s %s: bad MODE '%s'\n",
4788 progname, argv[0], mode_opt);
4791 previous_mode = umask(0);
4795 * initialize stripe parameters, in case param is converted to specific,
4796 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
4798 param = calloc(1, offsetof(typeof(*param),
4799 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
4800 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
4801 if (param == NULL) {
4803 "%s %s: cannot allocate memory for parameters: %s\n",
4804 progname, argv[0], strerror(ENOMEM));
4808 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4809 param->lsp_stripe_count = lsa.lsa_stripe_count;
4810 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4811 param->lsp_stripe_offset = -1;
4813 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4814 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4815 param->lsp_stripe_pattern = lsa.lsa_pattern;
4817 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
4818 param->lsp_pool = lsa.lsa_pool_name;
4819 param->lsp_is_specific = false;
4820 if (lsa.lsa_nr_tgts > 1) {
4821 if (lsa.lsa_stripe_count > 0 &&
4822 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4823 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4824 fprintf(stderr, "error: %s: stripe count %lld doesn't "
4825 "match the number of MDTs: %d\n",
4826 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
4831 param->lsp_is_specific = true;
4832 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4833 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
4836 dname = argv[optind];
4838 if (default_stripe) {
4839 result = llapi_dir_set_default_lmv(dname, param);
4841 /* if current \a dname isn't under the same \a mntdir
4842 * as the last one, and the last one was
4843 * auto-distributed, restore \a param.
4845 if (mntdir[0] != '\0' &&
4846 strncmp(dname, mntdir, strlen(mntdir)) &&
4848 param->lsp_is_specific = false;
4849 param->lsp_stripe_offset = -1;
4850 auto_distributed = false;
4853 if (!param->lsp_is_specific &&
4854 param->lsp_stripe_offset == -1) {
4855 char path[PATH_MAX] = "";
4858 lsb = malloc(sizeof(*lsb));
4866 /* use mntdir for dirname() temporarily */
4867 strncpy(mntdir, dname, sizeof(mntdir));
4868 if (!realpath(dirname(mntdir), path)) {
4871 "error: invalid path '%s': %s\n",
4872 argv[optind], strerror(errno));
4877 result = llapi_search_mounts(path, 0, mntdir,
4879 if (result < 0 || mntdir[0] == '\0') {
4881 "No suitable Lustre mount found\n");
4885 result = mntdf(mntdir, NULL, NULL, 0,
4886 LL_STATFS_LMV, lsb);
4890 if (param->lsp_stripe_count > lsb->sb_count) {
4892 "error: stripe count %d is too big\n",
4893 param->lsp_stripe_count);
4898 qsort(lsb->sb_buf, lsb->sb_count,
4899 sizeof(struct ll_statfs_data),
4900 ll_statfs_data_comp);
4902 auto_distributed = true;
4905 if (auto_distributed) {
4907 int nr = MAX(param->lsp_stripe_count,
4910 /* don't use server whose usage is above 90% */
4911 while (nr != param->lsp_stripe_count &&
4912 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
4914 nr = MAX(param->lsp_stripe_count,
4917 /* get \a r between [0, nr) */
4920 param->lsp_stripe_offset =
4921 lsb->sb_buf[r].sd_index;
4922 if (param->lsp_stripe_count > 1) {
4925 param->lsp_is_specific = true;
4926 for (; i < param->lsp_stripe_count; i++)
4927 param->lsp_tgts[(i + r) % nr] =
4928 lsb->sb_buf[i].sd_index;
4932 result = llapi_dir_create(dname, mode, param);
4937 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
4938 progname, dname, strerror(-result));
4941 dname = argv[++optind];
4942 } while (dname != NULL);
4944 if (mode_opt != NULL)
4945 umask(previous_mode);
4953 static int lfs_rmentry(int argc, char **argv)
4960 fprintf(stderr, "error: %s: missing dirname\n",
4966 dname = argv[index];
4967 while (dname != NULL) {
4968 result = llapi_direntry_remove(dname);
4970 fprintf(stderr, "error: %s: remove dir entry '%s' "
4971 "failed\n", argv[0], dname);
4974 dname = argv[++index];
4979 static int lfs_mv(int argc, char **argv)
4981 struct find_param param = {
4988 struct option long_opts[] = {
4989 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4990 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4993 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
4995 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4997 fprintf(stderr, "warning: '-M' deprecated"
4998 ", use '--mdt-index' or '-m' instead\n");
5001 param.fp_mdt_index = strtoul(optarg, &end, 0);
5003 fprintf(stderr, "%s: invalid MDT index'%s'\n",
5009 param.fp_verbose = VERBOSE_DETAIL;
5012 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
5013 argv[0], argv[optind - 1]);
5018 if (param.fp_mdt_index == -1) {
5019 fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
5023 if (optind >= argc) {
5024 fprintf(stderr, "%s: missing operand path\n", argv[0]);
5028 param.fp_migrate = 1;
5029 rc = llapi_migrate_mdt(argv[optind], ¶m);
5031 fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
5032 argv[0], argv[optind], param.fp_mdt_index,
5037 static int lfs_osts(int argc, char **argv)
5039 return lfs_tgts(argc, argv);
5042 static int lfs_mdts(int argc, char **argv)
5044 return lfs_tgts(argc, argv);
5047 static int lfs_df(int argc, char **argv)
5049 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5050 enum mntdf_flags flags = MNTDF_SHOW;
5051 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5052 int c, rc = 0, index = 0;
5053 char fsname[PATH_MAX] = "", *pool_name = NULL;
5054 struct option long_opts[] = {
5055 { .val = 'h', .name = "human-readable",
5056 .has_arg = no_argument },
5057 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5058 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5059 { .val = 'p', .name = "pool", .has_arg = required_argument },
5060 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5063 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5066 flags |= MNTDF_COOKED;
5069 flags |= MNTDF_INODES;
5072 flags |= MNTDF_LAZY;
5078 flags |= MNTDF_VERBOSE;
5084 if (optind < argc && !realpath(argv[optind], path)) {
5086 fprintf(stderr, "error: invalid path '%s': %s\n",
5087 argv[optind], strerror(-rc));
5091 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5092 /* Check if we have a mount point */
5093 if (mntdir[0] == '\0')
5096 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5097 if (rc || path[0] != '\0')
5099 fsname[0] = '\0'; /* avoid matching in next loop */
5100 mntdir[0] = '\0'; /* avoid matching in next loop */
5106 static int lfs_getname(int argc, char **argv)
5108 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5109 int rc = 0, index = 0, c;
5110 char buf[sizeof(struct obd_uuid)];
5112 while ((c = getopt(argc, argv, "h")) != -1)
5115 if (optind == argc) { /* no paths specified, get all paths. */
5116 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5117 rc = llapi_getname(mntdir, buf, sizeof(buf));
5120 "cannot get name for `%s': %s\n",
5121 mntdir, strerror(-rc));
5125 printf("%s %s\n", buf, mntdir);
5127 path[0] = fsname[0] = mntdir[0] = 0;
5129 } else { /* paths specified, only attempt to search these. */
5130 for (; optind < argc; optind++) {
5131 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5134 "cannot get name for `%s': %s\n",
5135 argv[optind], strerror(-rc));
5139 printf("%s %s\n", buf, argv[optind]);
5145 static int lfs_check(int argc, char **argv)
5148 char mntdir[PATH_MAX] = {'\0'};
5155 fprintf(stderr, "%s check: server type must be specified\n",
5160 obd_types[0] = obd_type1;
5161 obd_types[1] = obd_type2;
5163 if (strcmp(argv[1], "osts") == 0) {
5164 strcpy(obd_types[0], "osc");
5165 } else if (strcmp(argv[1], "mds") == 0) {
5166 strcpy(obd_types[0], "mdc");
5167 } else if (strcmp(argv[1], "servers") == 0) {
5169 strcpy(obd_types[0], "osc");
5170 strcpy(obd_types[1], "mdc");
5172 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5177 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5178 if (rc < 0 || mntdir[0] == '\0') {
5180 "%s check: cannot find mounted Lustre filesystem: %s\n",
5181 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5185 rc = llapi_target_check(num_types, obd_types, mntdir);
5187 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5188 progname, argv[1], strerror(-rc));
5194 #ifdef HAVE_SYS_QUOTA_H
5195 #define ARG2INT(nr, str, msg) \
5198 nr = strtol(str, &endp, 0); \
5199 if (*endp != '\0') { \
5200 fprintf(stderr, "%s: bad %s '%s'\n", \
5201 progname, msg, str); \
5206 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5208 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5209 * returns the value or ULONG_MAX on integer overflow or incorrect format
5211 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5212 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5213 * 3. empty integer value is interpreted as 0
5215 static unsigned long str2sec(const char* timestr)
5217 const char spec[] = "smhdw";
5218 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5219 unsigned long val = 0;
5222 if (strpbrk(timestr, spec) == NULL) {
5223 /* no specifiers inside the time string,
5224 should treat it as an integer value */
5225 val = strtoul(timestr, &tail, 10);
5226 return *tail ? ULONG_MAX : val;
5229 /* format string is XXwXXdXXhXXmXXs */
5235 v = strtoul(timestr, &tail, 10);
5236 if (v == ULONG_MAX || *tail == '\0')
5237 /* value too large (ULONG_MAX or more)
5238 or missing specifier */
5241 ptr = strchr(spec, *tail);
5243 /* unknown specifier */
5248 /* check if product will overflow the type */
5249 if (!(v < ULONG_MAX / mult[ind]))
5252 ADD_OVERFLOW(val, mult[ind] * v);
5253 if (val == ULONG_MAX)
5265 #define ARG2ULL(nr, str, def_units) \
5267 unsigned long long limit, units = def_units; \
5270 rc = llapi_parse_size(str, &limit, &units, 1); \
5272 fprintf(stderr, "%s: invalid limit '%s'\n", \
5279 static inline int has_times_option(int argc, char **argv)
5283 for (i = 1; i < argc; i++)
5284 if (!strcmp(argv[i], "-t"))
5290 int lfs_setquota_times(int argc, char **argv)
5293 struct if_quotactl qctl;
5294 char *mnt, *obd_type = (char *)qctl.obd_type;
5295 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5296 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5297 struct option long_opts[] = {
5298 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5299 { .val = 'g', .name = "group", .has_arg = no_argument },
5300 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5301 { .val = 'p', .name = "projid", .has_arg = no_argument },
5302 { .val = 't', .name = "times", .has_arg = no_argument },
5303 { .val = 'u', .name = "user", .has_arg = no_argument },
5307 memset(&qctl, 0, sizeof(qctl));
5308 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5309 qctl.qc_type = ALLQUOTA;
5311 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5312 long_opts, NULL)) != -1) {
5323 if (qctl.qc_type != ALLQUOTA) {
5324 fprintf(stderr, "error: -u/g/p can't be used "
5325 "more than once\n");
5328 qctl.qc_type = qtype;
5331 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5332 fprintf(stderr, "error: bad block-grace: %s\n",
5336 dqb->dqb_valid |= QIF_BTIME;
5339 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5340 fprintf(stderr, "error: bad inode-grace: %s\n",
5344 dqb->dqb_valid |= QIF_ITIME;
5346 case 't': /* Yes, of course! */
5348 default: /* getopt prints error message for us when opterr != 0 */
5353 if (qctl.qc_type == ALLQUOTA) {
5354 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5358 if (optind != argc - 1) {
5359 fprintf(stderr, "error: unexpected parameters encountered\n");
5364 rc = llapi_quotactl(mnt, &qctl);
5367 fprintf(stderr, "%s %s ", obd_type,
5368 obd_uuid2str(&qctl.obd_uuid));
5369 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5376 #define BSLIMIT (1 << 0)
5377 #define BHLIMIT (1 << 1)
5378 #define ISLIMIT (1 << 2)
5379 #define IHLIMIT (1 << 3)
5381 int lfs_setquota(int argc, char **argv)
5384 struct if_quotactl qctl;
5385 char *mnt, *obd_type = (char *)qctl.obd_type;
5386 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5387 struct option long_opts[] = {
5388 { .val = 'b', .name = "block-softlimit",
5389 .has_arg = required_argument },
5390 { .val = 'B', .name = "block-hardlimit",
5391 .has_arg = required_argument },
5392 { .val = 'g', .name = "group", .has_arg = required_argument },
5393 { .val = 'i', .name = "inode-softlimit",
5394 .has_arg = required_argument },
5395 { .val = 'I', .name = "inode-hardlimit",
5396 .has_arg = required_argument },
5397 { .val = 'p', .name = "projid", .has_arg = required_argument },
5398 { .val = 'u', .name = "user", .has_arg = required_argument },
5400 unsigned limit_mask = 0;
5404 if (has_times_option(argc, argv))
5405 return lfs_setquota_times(argc, argv);
5407 memset(&qctl, 0, sizeof(qctl));
5408 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5409 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5410 * so it can be used as a marker that qc_type
5411 * isn't reinitialized from command line */
5413 while ((c = getopt_long(argc, argv, "b:B:g:i:I:p:u:",
5414 long_opts, NULL)) != -1) {
5418 rc = name2uid(&qctl.qc_id, optarg);
5422 rc = name2gid(&qctl.qc_id, optarg);
5426 rc = name2projid(&qctl.qc_id, optarg);
5428 if (qctl.qc_type != ALLQUOTA) {
5430 "%s setquota: only one of -u, -g or -p may be specified\n",
5434 qctl.qc_type = qtype;
5436 qctl.qc_id = strtoul(optarg, &endptr, 10);
5437 if (*endptr != '\0') {
5439 "%s setquota: invalid id '%s'\n",
5446 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5447 dqb->dqb_bsoftlimit >>= 10;
5448 limit_mask |= BSLIMIT;
5449 if (dqb->dqb_bsoftlimit &&
5450 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5452 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5453 "See '%s help setquota' or Lustre manual for details\n",
5454 progname, dqb->dqb_bsoftlimit,
5458 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5459 dqb->dqb_bhardlimit >>= 10;
5460 limit_mask |= BHLIMIT;
5461 if (dqb->dqb_bhardlimit &&
5462 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5464 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5465 "See '%s help setquota' or Lustre manual for details\n",
5466 progname, dqb->dqb_bhardlimit,
5470 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5471 limit_mask |= ISLIMIT;
5472 if (dqb->dqb_isoftlimit &&
5473 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5475 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5476 "See '%s help setquota' or Lustre manual for details\n",
5477 progname, dqb->dqb_isoftlimit,
5481 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5482 limit_mask |= IHLIMIT;
5483 if (dqb->dqb_ihardlimit &&
5484 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5486 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5487 "See '%s help setquota' or Lustre manual for details\n",
5488 progname, dqb->dqb_ihardlimit,
5493 "%s setquota: unrecognized option '%s'\n",
5494 progname, argv[optind - 1]);
5499 if (qctl.qc_type == ALLQUOTA) {
5501 "%s setquota: either -u or -g must be specified\n",
5506 if (limit_mask == 0) {
5508 "%s setquota: at least one limit must be specified\n",
5513 if (optind != argc - 1) {
5515 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5516 progname, argv[optind]);
5522 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5523 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5524 /* sigh, we can't just set blimits/ilimits */
5525 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5526 .qc_type = qctl.qc_type,
5527 .qc_id = qctl.qc_id};
5529 rc = llapi_quotactl(mnt, &tmp_qctl);
5533 if (!(limit_mask & BHLIMIT))
5534 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5535 if (!(limit_mask & BSLIMIT))
5536 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5537 if (!(limit_mask & IHLIMIT))
5538 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5539 if (!(limit_mask & ISLIMIT))
5540 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5542 /* Keep grace times if we have got no softlimit arguments */
5543 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5544 dqb->dqb_valid |= QIF_BTIME;
5545 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5548 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5549 dqb->dqb_valid |= QIF_ITIME;
5550 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5554 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5555 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5557 rc = llapi_quotactl(mnt, &qctl);
5561 "%s setquota: cannot quotactl '%s' '%s': %s",
5563 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5570 /* Converts seconds value into format string
5571 * result is returned in buf
5573 * 1. result is in descenting order: 1w2d3h4m5s
5574 * 2. zero fields are not filled (except for p. 3): 5d1s
5575 * 3. zero seconds value is presented as "0s"
5577 static char * __sec2str(time_t seconds, char *buf)
5579 const char spec[] = "smhdw";
5580 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5585 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5586 c = seconds / mult[i];
5588 if (c > 0 || (i == 0 && buf == tail))
5589 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5597 static void sec2str(time_t seconds, char *buf, int rc)
5604 tail = __sec2str(seconds, tail);
5606 if (rc && tail - buf < 39) {
5612 static void diff2str(time_t seconds, char *buf, time_t now)
5618 if (seconds <= now) {
5619 strcpy(buf, "none");
5622 __sec2str(seconds - now, buf);
5625 static void print_quota_title(char *name, struct if_quotactl *qctl,
5626 bool human_readable)
5628 printf("Disk quotas for %s %s (%cid %u):\n",
5629 qtype_name(qctl->qc_type), name,
5630 *qtype_name(qctl->qc_type), qctl->qc_id);
5631 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5632 "Filesystem", human_readable ? "used" : "kbytes",
5633 "quota", "limit", "grace",
5634 "files", "quota", "limit", "grace");
5637 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5640 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5643 snprintf(buf, buflen, "%5.4gP",
5644 (double)num / ((__u64)1 << 40));
5646 snprintf(buf, buflen, "%5.4gT",
5647 (double)num / (1 << 30));
5649 snprintf(buf, buflen, "%5.4gG",
5650 (double)num / (1 << 20));
5652 snprintf(buf, buflen, "%5.4gM",
5653 (double)num / (1 << 10));
5655 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5659 #define STRBUF_LEN 32
5660 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5667 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
5668 int bover = 0, iover = 0;
5669 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5670 char numbuf[3][STRBUF_LEN];
5672 char strbuf[STRBUF_LEN];
5674 if (dqb->dqb_bhardlimit &&
5675 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5677 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5678 if (dqb->dqb_btime > now) {
5685 if (dqb->dqb_ihardlimit &&
5686 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5688 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5689 if (dqb->dqb_itime > now) {
5697 if (strlen(mnt) > 15)
5698 printf("%s\n%15s", mnt, "");
5700 printf("%15s", mnt);
5703 diff2str(dqb->dqb_btime, timebuf, now);
5705 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
5706 strbuf, sizeof(strbuf), h);
5707 if (rc == -EREMOTEIO)
5708 sprintf(numbuf[0], "%s*", strbuf);
5710 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
5711 "%s" : "[%s]", strbuf);
5713 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
5714 if (type == QC_GENERAL)
5715 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
5716 "%s" : "[%s]", strbuf);
5718 sprintf(numbuf[1], "%s", "-");
5720 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
5721 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
5722 "%s" : "[%s]", strbuf);
5724 printf(" %7s%c %6s %7s %7s",
5725 numbuf[0], bover ? '*' : ' ', numbuf[1],
5726 numbuf[2], bover > 1 ? timebuf : "-");
5729 diff2str(dqb->dqb_itime, timebuf, now);
5731 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
5732 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
5734 if (type == QC_GENERAL)
5735 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
5737 (uintmax_t)dqb->dqb_isoftlimit);
5739 sprintf(numbuf[1], "%s", "-");
5741 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
5742 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
5744 if (type != QC_OSTIDX)
5745 printf(" %7s%c %6s %7s %7s",
5746 numbuf[0], iover ? '*' : ' ', numbuf[1],
5747 numbuf[2], iover > 1 ? timebuf : "-");
5749 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
5752 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
5753 qctl->qc_cmd == Q_GETOINFO) {
5757 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
5758 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
5759 printf("Block grace time: %s; Inode grace time: %s\n",
5760 bgtimebuf, igtimebuf);
5764 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
5765 bool h, __u64 *total)
5767 int rc = 0, rc1 = 0, count = 0;
5768 __u32 valid = qctl->qc_valid;
5770 rc = llapi_get_obd_count(mnt, &count, is_mdt);
5772 fprintf(stderr, "can not get %s count: %s\n",
5773 is_mdt ? "mdt": "ost", strerror(-rc));
5777 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
5778 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
5779 rc = llapi_quotactl(mnt, qctl);
5781 /* It is remote client case. */
5782 if (rc == -EOPNOTSUPP) {
5789 fprintf(stderr, "quotactl %s%d failed.\n",
5790 is_mdt ? "mdt": "ost", qctl->qc_idx);
5794 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
5795 qctl->qc_valid, 0, h);
5796 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
5797 qctl->qc_dqblk.dqb_bhardlimit;
5800 qctl->qc_valid = valid;
5804 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
5805 int verbose, int quiet, bool human_readable)
5807 int rc1 = 0, rc2 = 0, rc3 = 0;
5808 char *obd_type = (char *)qctl->obd_type;
5809 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
5810 __u64 total_ialloc = 0, total_balloc = 0;
5813 rc1 = llapi_quotactl(mnt, qctl);
5817 fprintf(stderr, "%s quotas are not enabled.\n",
5818 qtype_name(qctl->qc_type));
5821 fprintf(stderr, "Permission denied.\n");
5824 /* We already got error message. */
5827 fprintf(stderr, "Unexpected quotactl error: %s\n",
5832 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
5833 print_quota_title(name, qctl, human_readable);
5835 if (rc1 && *obd_type)
5836 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
5838 if (qctl->qc_valid != QC_GENERAL)
5841 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
5842 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
5843 (QIF_LIMITS|QIF_USAGE));
5845 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable);
5847 if (qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
5849 char strbuf[STRBUF_LEN];
5851 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
5853 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
5855 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
5857 printf("Total allocated inode limit: %ju, total "
5858 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
5862 if (rc1 || rc2 || rc3 || inacc)
5863 printf("Some errors happened when getting quota info. "
5864 "Some devices may be not working or deactivated. "
5865 "The data in \"[]\" is inaccurate.\n");
5871 static int lfs_project(int argc, char **argv)
5873 int ret = 0, err = 0, c, i;
5874 struct project_handle_control phc = { 0 };
5875 enum lfs_project_ops_t op;
5878 phc.assign_projid = false;
5879 /* default action */
5880 op = LFS_PROJECT_LIST;
5882 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
5885 if (op != LFS_PROJECT_LIST) {
5887 "%s: cannot specify '-c' '-C' '-s' together\n",
5892 op = LFS_PROJECT_CHECK;
5895 if (op != LFS_PROJECT_LIST) {
5897 "%s: cannot specify '-c' '-C' '-s' together\n",
5902 op = LFS_PROJECT_CLEAR;
5905 if (op != LFS_PROJECT_LIST) {
5907 "%s: cannot specify '-c' '-C' '-s' together\n",
5912 phc.set_inherit = true;
5913 op = LFS_PROJECT_SET;
5919 phc.keep_projid = true;
5922 phc.recursive = true;
5925 phc.projid = strtoul(optarg, NULL, 0);
5926 phc.assign_projid = true;
5930 phc.newline = false;
5933 fprintf(stderr, "%s: invalid option '%c'\n",
5939 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
5940 op = LFS_PROJECT_SET;
5941 phc.set_projid = true;
5942 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
5943 phc.set_projid = true;
5947 case LFS_PROJECT_CHECK:
5948 if (phc.keep_projid) {
5950 "%s: '-k' is useless together with '-c'\n",
5955 case LFS_PROJECT_CLEAR:
5958 "%s: '-0' is useless together with '-C'\n",
5962 if (phc.assign_projid) {
5964 "%s: '-p' is useless together with '-C'\n",
5969 case LFS_PROJECT_SET:
5972 "%s: '-0' is useless together with '-s'\n",
5976 if (phc.keep_projid) {
5978 "%s: '-k' is useless together with '-s'\n",
5986 "%s: '-0' is useless for list operations\n",
5996 fprintf(stderr, "%s: missing file or directory target(s)\n",
6001 for (i = 0; i < argc; i++) {
6003 case LFS_PROJECT_CHECK:
6004 err = lfs_project_check(argv[i], &phc);
6006 case LFS_PROJECT_LIST:
6007 err = lfs_project_list(argv[i], &phc);
6009 case LFS_PROJECT_CLEAR:
6010 err = lfs_project_clear(argv[i], &phc);
6012 case LFS_PROJECT_SET:
6013 err = lfs_project_set(argv[i], &phc);
6025 static int lfs_quota(int argc, char **argv)
6028 char *mnt, *name = NULL;
6029 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6030 .qc_type = ALLQUOTA };
6031 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6032 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6034 __u32 valid = QC_GENERAL, idx = 0;
6035 bool human_readable = false;
6038 while ((c = getopt(argc, argv, "gi:I:o:pqtuvh")) != -1) {
6049 if (qctl.qc_type != ALLQUOTA) {
6051 "%s quota: only one of -u, -g, or -p may be specified\n",
6055 qctl.qc_type = qtype;
6058 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6061 valid = qctl.qc_valid = QC_UUID;
6062 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6065 valid = qctl.qc_valid = QC_MDTIDX;
6066 idx = qctl.qc_idx = atoi(optarg);
6067 if (idx == 0 && *optarg != '0') {
6069 "%s quota: invalid MDT index '%s'\n",
6075 valid = qctl.qc_valid = QC_OSTIDX;
6076 idx = qctl.qc_idx = atoi(optarg);
6077 if (idx == 0 && *optarg != '0') {
6079 "%s quota: invalid OST index '%s'\n",
6091 human_readable = true;
6094 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6095 progname, argv[optind - 1]);
6100 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6101 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6102 optind == argc - 1) {
6104 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6105 qctl.qc_valid = valid;
6108 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6109 qctl.qc_type = qtype;
6110 if (qtype == USRQUOTA) {
6111 qctl.qc_id = geteuid();
6112 rc = uid2name(&name, qctl.qc_id);
6114 qctl.qc_id = getegid();
6115 rc = gid2name(&name, qctl.qc_id);
6120 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6126 /* lfs quota -u username /path/to/lustre/mount */
6127 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6128 /* options should be followed by u/g-name and mntpoint */
6129 if (optind + 2 != argc || qctl.qc_type == ALLQUOTA) {
6131 "%s quota: name and mount point must be specified\n",
6136 name = argv[optind++];
6137 switch (qctl.qc_type) {
6139 rc = name2uid(&qctl.qc_id, name);
6142 rc = name2gid(&qctl.qc_id, name);
6145 rc = name2projid(&qctl.qc_id, name);
6152 qctl.qc_id = strtoul(name, &endptr, 10);
6153 if (*endptr != '\0') {
6154 fprintf(stderr, "%s quota: invalid id '%s'\n",
6159 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6160 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6166 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6170 #endif /* HAVE_SYS_QUOTA_H! */
6172 static int flushctx_ioctl(char *mp)
6176 fd = open(mp, O_RDONLY);
6178 fprintf(stderr, "flushctx: error open %s: %s\n",
6179 mp, strerror(errno));
6183 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6185 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6186 mp, strerror(errno));
6192 static int lfs_flushctx(int argc, char **argv)
6194 int kdestroy = 0, c;
6195 char mntdir[PATH_MAX] = {'\0'};
6199 while ((c = getopt(argc, argv, "k")) != -1) {
6205 fprintf(stderr, "error: %s: option '-%c' "
6206 "unrecognized\n", argv[0], c);
6212 if ((rc = system("kdestroy > /dev/null")) != 0) {
6213 rc = WEXITSTATUS(rc);
6214 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6218 if (optind >= argc) {
6219 /* flush for all mounted lustre fs. */
6220 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6221 /* Check if we have a mount point */
6222 if (mntdir[0] == '\0')
6225 if (flushctx_ioctl(mntdir))
6228 mntdir[0] = '\0'; /* avoid matching in next loop */
6231 /* flush fs as specified */
6232 while (optind < argc) {
6233 if (flushctx_ioctl(argv[optind++]))
6240 static int lfs_cp(int argc, char **argv)
6242 fprintf(stderr, "remote client copy file(s).\n"
6243 "obsolete, does not support it anymore.\n");
6247 static int lfs_ls(int argc, char **argv)
6249 fprintf(stderr, "remote client lists directory contents.\n"
6250 "obsolete, does not support it anymore.\n");
6254 static int lfs_changelog(int argc, char **argv)
6256 void *changelog_priv;
6257 struct changelog_rec *rec;
6258 long long startrec = 0, endrec = 0;
6260 struct option long_opts[] = {
6261 { .val = 'f', .name = "follow", .has_arg = no_argument },
6263 char short_opts[] = "f";
6266 while ((rc = getopt_long(argc, argv, short_opts,
6267 long_opts, NULL)) != -1) {
6274 "%s changelog: unrecognized option '%s'\n",
6275 progname, argv[optind - 1]);
6279 if (optind >= argc) {
6280 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6285 mdd = argv[optind++];
6287 startrec = strtoll(argv[optind++], NULL, 10);
6289 endrec = strtoll(argv[optind++], NULL, 10);
6291 rc = llapi_changelog_start(&changelog_priv,
6292 CHANGELOG_FLAG_BLOCK |
6293 CHANGELOG_FLAG_JOBID |
6294 CHANGELOG_FLAG_EXTRA_FLAGS |
6295 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6298 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6299 progname, strerror(errno = -rc));
6303 rc = llapi_changelog_set_xflags(changelog_priv,
6304 CHANGELOG_EXTRA_FLAG_UIDGID |
6305 CHANGELOG_EXTRA_FLAG_NID |
6306 CHANGELOG_EXTRA_FLAG_OMODE |
6307 CHANGELOG_EXTRA_FLAG_XATTR);
6310 "%s changelog: cannot set xflags for changelog: %s\n",
6311 progname, strerror(errno = -rc));
6315 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6319 if (endrec && rec->cr_index > endrec) {
6320 llapi_changelog_free(&rec);
6323 if (rec->cr_index < startrec) {
6324 llapi_changelog_free(&rec);
6328 secs = rec->cr_time >> 30;
6329 gmtime_r(&secs, &ts);
6330 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6331 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6332 changelog_type2str(rec->cr_type),
6333 ts.tm_hour, ts.tm_min, ts.tm_sec,
6334 (int)(rec->cr_time & ((1 << 30) - 1)),
6335 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6336 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6338 if (rec->cr_flags & CLF_JOBID) {
6339 struct changelog_ext_jobid *jid =
6340 changelog_rec_jobid(rec);
6342 if (jid->cr_jobid[0] != '\0')
6343 printf(" j=%s", jid->cr_jobid);
6346 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6347 struct changelog_ext_extra_flags *ef =
6348 changelog_rec_extra_flags(rec);
6350 printf(" ef=0x%llx", ef->cr_extra_flags);
6352 if (ef->cr_extra_flags & CLFE_UIDGID) {
6353 struct changelog_ext_uidgid *uidgid =
6354 changelog_rec_uidgid(rec);
6356 printf(" u=%llu:%llu",
6357 uidgid->cr_uid, uidgid->cr_gid);
6359 if (ef->cr_extra_flags & CLFE_NID) {
6360 struct changelog_ext_nid *nid =
6361 changelog_rec_nid(rec);
6364 libcfs_nid2str(nid->cr_nid));
6367 if (ef->cr_extra_flags & CLFE_OPEN) {
6368 struct changelog_ext_openmode *omd =
6369 changelog_rec_openmode(rec);
6370 char mode[] = "---";
6372 /* exec mode must be exclusive */
6373 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6376 if (omd->cr_openflags & MDS_FMODE_READ)
6378 if (omd->cr_openflags &
6385 if (strcmp(mode, "---") != 0)
6386 printf(" m=%s", mode);
6390 if (ef->cr_extra_flags & CLFE_XATTR) {
6391 struct changelog_ext_xattr *xattr =
6392 changelog_rec_xattr(rec);
6394 if (xattr->cr_xattr[0] != '\0')
6395 printf(" x=%s", xattr->cr_xattr);
6399 if (rec->cr_namelen)
6400 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6401 rec->cr_namelen, changelog_rec_name(rec));
6403 if (rec->cr_flags & CLF_RENAME) {
6404 struct changelog_ext_rename *rnm =
6405 changelog_rec_rename(rec);
6407 if (!fid_is_zero(&rnm->cr_sfid))
6408 printf(" s="DFID" sp="DFID" %.*s",
6409 PFID(&rnm->cr_sfid),
6410 PFID(&rnm->cr_spfid),
6411 (int)changelog_rec_snamelen(rec),
6412 changelog_rec_sname(rec));
6416 llapi_changelog_free(&rec);
6419 llapi_changelog_fini(&changelog_priv);
6422 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6423 progname, strerror(errno = -rc));
6425 return (rc == 1 ? 0 : rc);
6428 static int lfs_changelog_clear(int argc, char **argv)
6436 endrec = strtoll(argv[3], NULL, 10);
6438 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6441 fprintf(stderr, "%s: record out of range: %llu\n",
6443 else if (rc == -ENOENT)
6444 fprintf(stderr, "%s: no changelog user: %s\n",
6447 fprintf(stderr, "%s error: %s\n", argv[0],
6456 static int lfs_fid2path(int argc, char **argv)
6458 struct option long_opts[] = {
6459 { .val = 'c', .name = "cur", .has_arg = no_argument },
6460 { .val = 'l', .name = "link", .has_arg = required_argument },
6461 { .val = 'r', .name = "rec", .has_arg = required_argument },
6463 char short_opts[] = "cl:r:";
6464 char *device, *fid, *path;
6465 long long recno = -1;
6471 while ((rc = getopt_long(argc, argv, short_opts,
6472 long_opts, NULL)) != -1) {
6478 linkno = strtol(optarg, NULL, 10);
6481 recno = strtoll(optarg, NULL, 10);
6486 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6487 argv[0], argv[optind - 1]);
6495 device = argv[optind++];
6496 path = calloc(1, PATH_MAX);
6498 fprintf(stderr, "error: Not enough memory\n");
6503 while (optind < argc) {
6504 fid = argv[optind++];
6506 lnktmp = (linkno >= 0) ? linkno : 0;
6508 int oldtmp = lnktmp;
6509 long long rectmp = recno;
6511 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6514 fprintf(stderr, "%s: error on FID %s: %s\n",
6515 argv[0], fid, strerror(errno = -rc2));
6522 fprintf(stdout, "%lld ", rectmp);
6523 if (device[0] == '/') {
6524 fprintf(stdout, "%s", device);
6525 if (device[strlen(device) - 1] != '/')
6526 fprintf(stdout, "/");
6527 } else if (path[0] == '\0') {
6528 fprintf(stdout, "/");
6530 fprintf(stdout, "%s\n", path);
6533 /* specified linkno */
6535 if (oldtmp == lnktmp)
6545 static int lfs_path2fid(int argc, char **argv)
6547 struct option long_opts[] = {
6548 { .val = 'p', .name = "parents", .has_arg = no_argument },
6551 const char short_opts[] = "p";
6552 const char *sep = "";
6555 bool show_parents = false;
6557 while ((rc = getopt_long(argc, argv, short_opts,
6558 long_opts, NULL)) != -1) {
6561 show_parents = true;
6564 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6565 argv[0], argv[optind - 1]);
6570 if (optind > argc - 1)
6572 else if (optind < argc - 1)
6576 for (path = argv + optind; *path != NULL; path++) {
6578 if (!show_parents) {
6579 err = llapi_path2fid(*path, &fid);
6581 printf("%s%s"DFID"\n",
6582 *sep != '\0' ? *path : "", sep,
6585 char name[NAME_MAX + 1];
6586 unsigned int linkno = 0;
6588 while ((err = llapi_path2parent(*path, linkno, &fid,
6589 name, sizeof(name))) == 0) {
6590 if (*sep != '\0' && linkno == 0)
6591 printf("%s%s", *path, sep);
6593 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6598 /* err == -ENODATA is end-of-loop */
6599 if (linkno > 0 && err == -ENODATA) {
6606 fprintf(stderr, "%s: can't get %sfid for %s: %s\n",
6607 argv[0], show_parents ? "parent " : "", *path,
6619 static int lfs_data_version(int argc, char **argv)
6626 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
6631 while ((c = getopt(argc, argv, "nrw")) != -1) {
6634 data_version_flags = 0;
6637 data_version_flags |= LL_DV_RD_FLUSH;
6640 data_version_flags |= LL_DV_WR_FLUSH;
6649 path = argv[optind];
6650 fd = open(path, O_RDONLY);
6652 err(errno, "cannot open file %s", path);
6654 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
6656 err(errno, "cannot get version for %s", path);
6658 printf("%ju" "\n", (uintmax_t)data_version);
6664 static int lfs_hsm_state(int argc, char **argv)
6669 struct hsm_user_state hus;
6677 rc = llapi_hsm_state_get(path, &hus);
6679 fprintf(stderr, "can't get hsm state for %s: %s\n",
6680 path, strerror(errno = -rc));
6684 /* Display path name and status flags */
6685 printf("%s: (0x%08x)", path, hus.hus_states);
6687 if (hus.hus_states & HS_RELEASED)
6688 printf(" released");
6689 if (hus.hus_states & HS_EXISTS)
6691 if (hus.hus_states & HS_DIRTY)
6693 if (hus.hus_states & HS_ARCHIVED)
6694 printf(" archived");
6695 /* Display user-settable flags */
6696 if (hus.hus_states & HS_NORELEASE)
6697 printf(" never_release");
6698 if (hus.hus_states & HS_NOARCHIVE)
6699 printf(" never_archive");
6700 if (hus.hus_states & HS_LOST)
6701 printf(" lost_from_hsm");
6703 if (hus.hus_archive_id != 0)
6704 printf(", archive_id:%d", hus.hus_archive_id);
6707 } while (++i < argc);
6712 #define LFS_HSM_SET 0
6713 #define LFS_HSM_CLEAR 1
6716 * Generic function to set or clear HSM flags.
6717 * Used by hsm_set and hsm_clear.
6719 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
6721 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
6723 struct option long_opts[] = {
6724 { .val = 'A', .name = "archived", .has_arg = no_argument },
6725 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
6726 { .val = 'd', .name = "dirty", .has_arg = no_argument },
6727 { .val = 'e', .name = "exists", .has_arg = no_argument },
6728 { .val = 'l', .name = "lost", .has_arg = no_argument },
6729 { .val = 'r', .name = "norelease", .has_arg = no_argument },
6730 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
6732 char short_opts[] = "lraAdei:";
6736 __u32 archive_id = 0;
6742 while ((c = getopt_long(argc, argv, short_opts,
6743 long_opts, NULL)) != -1) {
6749 mask |= HS_NOARCHIVE;
6752 mask |= HS_ARCHIVED;
6755 mask |= HS_NORELEASE;
6764 archive_id = strtol(optarg, &end, 10);
6766 fprintf(stderr, "invalid archive_id: '%s'\n",
6774 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
6775 argv[0], argv[optind - 1]);
6780 /* User should have specified a flag */
6784 while (optind < argc) {
6786 path = argv[optind];
6788 /* If mode == 0, this means we apply the mask. */
6789 if (mode == LFS_HSM_SET)
6790 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
6792 rc = llapi_hsm_state_set(path, 0, mask, 0);
6795 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
6796 path, strerror(errno = -rc));
6805 static int lfs_hsm_action(int argc, char **argv)
6810 struct hsm_current_action hca;
6811 struct hsm_extent he;
6812 enum hsm_user_action hua;
6813 enum hsm_progress_states hps;
6821 rc = llapi_hsm_current_action(path, &hca);
6823 fprintf(stderr, "can't get hsm action for %s: %s\n",
6824 path, strerror(errno = -rc));
6827 he = hca.hca_location;
6828 hua = hca.hca_action;
6829 hps = hca.hca_state;
6831 printf("%s: %s", path, hsm_user_action2name(hua));
6833 /* Skip file without action */
6834 if (hca.hca_action == HUA_NONE) {
6839 printf(" %s ", hsm_progress_state2name(hps));
6841 if ((hps == HPS_RUNNING) &&
6842 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
6843 printf("(%llu bytes moved)\n",
6844 (unsigned long long)he.length);
6845 else if ((he.offset + he.length) == LUSTRE_EOF)
6846 printf("(from %llu to EOF)\n",
6847 (unsigned long long)he.offset);
6849 printf("(from %llu to %llu)\n",
6850 (unsigned long long)he.offset,
6851 (unsigned long long)(he.offset + he.length));
6853 } while (++i < argc);
6858 static int lfs_hsm_set(int argc, char **argv)
6860 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
6863 static int lfs_hsm_clear(int argc, char **argv)
6865 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
6869 * Check file state and return its fid, to be used by lfs_hsm_request().
6871 * \param[in] file Path to file to check
6872 * \param[in,out] fid Pointer to allocated lu_fid struct.
6873 * \param[in,out] last_dev Pointer to last device id used.
6875 * \return 0 on success.
6877 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
6883 rc = lstat(file, &st);
6885 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
6888 /* Checking for regular file as archiving as posix copytool
6889 * rejects archiving files other than regular files
6891 if (!S_ISREG(st.st_mode)) {
6892 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
6895 /* A request should be ... */
6896 if (*last_dev != st.st_dev && *last_dev != 0) {
6897 fprintf(stderr, "All files should be "
6898 "on the same filesystem: %s\n", file);
6901 *last_dev = st.st_dev;
6903 rc = llapi_path2fid(file, fid);
6905 fprintf(stderr, "Cannot read FID of %s: %s\n",
6906 file, strerror(-rc));
6912 /* Fill an HSM HUR item with a given file name.
6914 * If mntpath is set, then the filename is actually a FID, and no
6915 * lookup on the filesystem will be performed.
6917 * \param[in] hur the user request to fill
6918 * \param[in] idx index of the item inside the HUR to fill
6919 * \param[in] mntpath mountpoint of Lustre
6920 * \param[in] fname filename (if mtnpath is NULL)
6921 * or FID (if mntpath is set)
6922 * \param[in] last_dev pointer to last device id used
6924 * \retval 0 on success
6925 * \retval CMD_HELP or a negative errno on error
6927 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
6928 const char *mntpath, const char *fname,
6931 struct hsm_user_item *hui = &hur->hur_user_item[idx];
6934 hui->hui_extent.length = -1;
6936 if (mntpath != NULL) {
6939 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
6943 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
6948 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
6952 hur->hur_request.hr_itemcount++;
6957 static int lfs_hsm_request(int argc, char **argv, int action)
6959 struct option long_opts[] = {
6960 { .val = 'a', .name = "archive", .has_arg = required_argument },
6961 { .val = 'D', .name = "data", .has_arg = required_argument },
6962 { .val = 'l', .name = "filelist", .has_arg = required_argument },
6963 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
6966 char short_opts[] = "l:D:a:m:";
6967 struct hsm_user_request *hur, *oldhur;
6972 char *filelist = NULL;
6973 char fullpath[PATH_MAX];
6974 char *opaque = NULL;
6978 int nbfile_alloc = 0;
6979 char *some_file = NULL;
6980 char *mntpath = NULL;
6986 while ((c = getopt_long(argc, argv, short_opts,
6987 long_opts, NULL)) != -1) {
6996 if (action != HUA_ARCHIVE &&
6997 action != HUA_REMOVE) {
6999 "error: -a is supported only "
7000 "when archiving or removing\n");
7003 archive_id = atoi(optarg);
7006 if (some_file == NULL) {
7008 some_file = strdup(optarg);
7014 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7015 argv[0], argv[optind - 1]);
7020 /* All remaining args are files, so we have at least nbfile */
7021 nbfile = argc - optind;
7023 if ((nbfile == 0) && (filelist == NULL))
7027 opaque_len = strlen(opaque);
7029 /* Alloc the request structure with enough place to store all files
7030 * from command line. */
7031 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7033 fprintf(stderr, "Cannot create the request: %s\n",
7037 nbfile_alloc = nbfile;
7039 hur->hur_request.hr_action = action;
7040 hur->hur_request.hr_archive_id = archive_id;
7041 hur->hur_request.hr_flags = 0;
7043 /* All remaining args are files, add them */
7044 if (nbfile != 0 && some_file == NULL)
7045 some_file = strdup(argv[optind]);
7047 for (i = 0; i < nbfile; i++) {
7048 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7054 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7056 /* If a filelist was specified, read the filelist from it. */
7057 if (filelist != NULL) {
7058 fp = fopen(filelist, "r");
7060 fprintf(stderr, "Cannot read the file list %s: %s\n",
7061 filelist, strerror(errno));
7066 while ((rc = getline(&line, &len, fp)) != -1) {
7067 /* If allocated buffer was too small, get something
7069 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7072 nbfile_alloc = nbfile_alloc * 2 + 1;
7074 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7077 fprintf(stderr, "hsm: cannot allocate "
7078 "the request: %s\n",
7085 size = hur_len(oldhur);
7087 fprintf(stderr, "hsm: cannot allocate "
7088 "%u files + %u bytes data\n",
7089 oldhur->hur_request.hr_itemcount,
7090 oldhur->hur_request.hr_data_len);
7097 memcpy(hur, oldhur, size);
7102 if (line[strlen(line) - 1] == '\n')
7103 line[strlen(line) - 1] = '\0';
7105 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7106 mntpath, line, &last_dev);
7112 if (some_file == NULL) {
7122 /* If a --data was used, add it to the request */
7123 hur->hur_request.hr_data_len = opaque_len;
7125 memcpy(hur_data(hur), opaque, opaque_len);
7127 /* Send the HSM request */
7128 if (realpath(some_file, fullpath) == NULL) {
7129 fprintf(stderr, "Could not find path '%s': %s\n",
7130 some_file, strerror(errno));
7132 rc = llapi_hsm_request(fullpath, hur);
7134 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7135 some_file, strerror(-rc));
7145 static int lfs_hsm_archive(int argc, char **argv)
7147 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7150 static int lfs_hsm_restore(int argc, char **argv)
7152 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7155 static int lfs_hsm_release(int argc, char **argv)
7157 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7160 static int lfs_hsm_remove(int argc, char **argv)
7162 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7165 static int lfs_hsm_cancel(int argc, char **argv)
7167 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7170 static int lfs_swap_layouts(int argc, char **argv)
7175 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7176 SWAP_LAYOUTS_KEEP_MTIME |
7177 SWAP_LAYOUTS_KEEP_ATIME);
7180 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7182 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7184 int lfs_get_mode(const char *string)
7186 enum lock_mode_user mode;
7188 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7189 if (lock_mode_names[mode] == NULL)
7191 if (strcmp(string, lock_mode_names[mode]) == 0)
7198 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7200 enum lu_ladvise_type advice;
7203 advice < ARRAY_SIZE(ladvise_names); advice++) {
7204 if (ladvise_names[advice] == NULL)
7206 if (strcmp(string, ladvise_names[advice]) == 0)
7210 return LU_LADVISE_INVALID;
7213 static int lfs_ladvise(int argc, char **argv)
7215 struct option long_opts[] = {
7216 { .val = 'a', .name = "advice", .has_arg = required_argument },
7217 { .val = 'b', .name = "background", .has_arg = no_argument },
7218 { .val = 'e', .name = "end", .has_arg = required_argument },
7219 { .val = 'l', .name = "length", .has_arg = required_argument },
7220 { .val = 'm', .name = "mode", .has_arg = required_argument },
7221 { .val = 's', .name = "start", .has_arg = required_argument },
7222 { .val = 'u', .name = "unset", .has_arg = no_argument },
7224 char short_opts[] = "a:be:l:m:s:u";
7229 struct llapi_lu_ladvise advice;
7230 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7231 unsigned long long start = 0;
7232 unsigned long long end = LUSTRE_EOF;
7233 unsigned long long length = 0;
7234 unsigned long long size_units;
7235 unsigned long long flags = 0;
7239 while ((c = getopt_long(argc, argv, short_opts,
7240 long_opts, NULL)) != -1) {
7243 advice_type = lfs_get_ladvice(optarg);
7244 if (advice_type == LU_LADVISE_INVALID) {
7245 fprintf(stderr, "%s: invalid advice type "
7246 "'%s'\n", argv[0], optarg);
7247 fprintf(stderr, "Valid types:");
7249 for (advice_type = 0;
7250 advice_type < ARRAY_SIZE(ladvise_names);
7252 if (ladvise_names[advice_type] == NULL)
7254 fprintf(stderr, " %s",
7255 ladvise_names[advice_type]);
7257 fprintf(stderr, "\n");
7270 rc = llapi_parse_size(optarg, &end,
7273 fprintf(stderr, "%s: bad end offset '%s'\n",
7280 rc = llapi_parse_size(optarg, &start,
7283 fprintf(stderr, "%s: bad start offset "
7284 "'%s'\n", argv[0], optarg);
7290 rc = llapi_parse_size(optarg, &length,
7293 fprintf(stderr, "%s: bad length '%s'\n",
7299 mode = lfs_get_mode(optarg);
7301 fprintf(stderr, "%s: bad mode '%s', valid "
7302 "modes are READ or WRITE\n",
7310 fprintf(stderr, "%s: option '%s' unrecognized\n",
7311 argv[0], argv[optind - 1]);
7316 if (advice_type == LU_LADVISE_INVALID) {
7317 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7318 fprintf(stderr, "Valid types:");
7319 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7321 if (ladvise_names[advice_type] == NULL)
7323 fprintf(stderr, " %s", ladvise_names[advice_type]);
7325 fprintf(stderr, "\n");
7329 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7330 fprintf(stderr, "%s: Lock no expand advice is a per file "
7331 "descriptor advice, so when called from lfs, "
7332 "it does nothing.\n", argv[0]);
7336 if (argc <= optind) {
7337 fprintf(stderr, "%s: please give one or more file names\n",
7342 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7343 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7348 if (end == LUSTRE_EOF && length != 0)
7349 end = start + length;
7352 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7353 argv[0], start, end);
7357 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7358 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7363 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7364 fprintf(stderr, "%s: mode is required with lockahead\n",
7369 while (optind < argc) {
7372 path = argv[optind++];
7374 fd = open(path, O_RDONLY);
7376 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7377 argv[0], path, strerror(errno));
7382 advice.lla_start = start;
7383 advice.lla_end = end;
7384 advice.lla_advice = advice_type;
7385 advice.lla_value1 = 0;
7386 advice.lla_value2 = 0;
7387 advice.lla_value3 = 0;
7388 advice.lla_value4 = 0;
7389 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7390 advice.lla_lockahead_mode = mode;
7391 advice.lla_peradvice_flags = flags;
7394 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7397 fprintf(stderr, "%s: cannot give advice '%s' to file "
7398 "'%s': %s\n", argv[0],
7399 ladvise_names[advice_type],
7400 path, strerror(errno));
7406 if (rc == 0 && rc2 < 0)
7412 /** The input string contains a comma delimited list of component ids and
7413 * ranges, for example "1,2-4,7".
7415 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7417 bool end_of_loop = false;
7425 while (!end_of_loop) {
7429 char *endptr = NULL;
7432 ptr = strchrnul(arg, ',');
7433 end_of_loop = *ptr == '\0';
7436 start_index = strtol(arg, &endptr, 0);
7437 if (endptr == arg) /* no data at all */
7439 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7441 if (start_index < 0)
7444 end_index = start_index;
7445 if (*endptr == '-') {
7446 end_index = strtol(endptr + 1, &endptr, 0);
7447 if (*endptr != '\0')
7449 if (end_index < start_index)
7453 for (i = start_index; i <= end_index && size > 0; i++) {
7456 /* remove duplicate */
7457 for (j = 0; j < nr; j++) {
7461 if (j == nr) { /* no duplicate */
7467 if (size == 0 && i < end_index)
7474 if (!end_of_loop && ptr != NULL)
7477 return rc < 0 ? rc : nr;
7481 * struct verify_mirror_id - Mirror id to be verified.
7482 * @mirror_id: A specified mirror id.
7483 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7485 struct verify_mirror_id {
7491 * compare_mirror_ids() - Compare mirror ids.
7492 * @layout: Mirror component list.
7493 * @cbdata: Callback data in verify_mirror_id structure.
7495 * This is a callback function called by llapi_layout_comp_iterate()
7496 * to compare the specified mirror id with the one in the current
7497 * component of @layout. If they are the same, then the specified
7498 * mirror id is valid.
7500 * Return: a negative error code on failure or
7501 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7502 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7505 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7507 struct verify_mirror_id *mirror_id_cbdata =
7508 (struct verify_mirror_id *)cbdata;
7512 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7516 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7517 progname, strerror(errno));
7521 if (mirror_id_cbdata->mirror_id == mirror_id) {
7522 mirror_id_cbdata->is_valid_id = true;
7523 return LLAPI_LAYOUT_ITER_STOP;
7526 return LLAPI_LAYOUT_ITER_CONT;
7530 * verify_mirror_ids() - Verify specified mirror ids.
7531 * @fname: Mirrored file name.
7532 * @mirror_ids: Specified mirror ids to be verified.
7533 * @ids_nr: Number of specified mirror ids.
7535 * This function verifies that specified @mirror_ids are valid
7536 * in the mirrored file @fname.
7538 * Return: 0 on success or a negative error code on failure.
7541 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7543 struct llapi_layout *layout = NULL;
7544 struct verify_mirror_id mirror_id_cbdata = { 0 };
7555 if (stat(fname, &stbuf) < 0) {
7556 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7557 progname, fname, strerror(errno));
7562 if (!S_ISREG(stbuf.st_mode)) {
7563 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7569 fd = open(fname, O_DIRECT | O_RDONLY);
7571 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7572 progname, fname, strerror(errno));
7577 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
7579 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
7580 progname, fname, strerror(errno));
7584 layout = llapi_layout_get_by_fd(fd, 0);
7585 if (layout == NULL) {
7586 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7587 progname, fname, strerror(errno));
7589 llapi_lease_release(fd);
7593 rc = llapi_layout_flags_get(layout, &flr_state);
7595 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7596 progname, fname, strerror(errno));
7601 flr_state &= LCM_FL_FLR_MASK;
7602 switch (flr_state) {
7605 fprintf(stderr, "%s: '%s' file state error: %s.\n",
7606 progname, fname, llapi_layout_flags_string(flr_state));
7613 for (i = 0; i < ids_nr; i++) {
7614 mirror_id_cbdata.mirror_id = mirror_ids[i];
7615 mirror_id_cbdata.is_valid_id = false;
7617 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
7622 "%s: '%s' failed to verify mirror id: %u.\n",
7623 progname, fname, mirror_ids[i]);
7627 if (!mirror_id_cbdata.is_valid_id) {
7630 "%s: '%s' invalid specified mirror id: %u.\n",
7631 progname, fname, mirror_ids[i]);
7637 llapi_layout_free(layout);
7638 llapi_lease_release(fd);
7646 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
7647 __u16 *mirror_ids, int ids_nr)
7649 struct llapi_resync_comp comp_array[1024] = { { 0 } };
7650 struct llapi_layout *layout;
7658 if (stat(fname, &stbuf) < 0) {
7659 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7660 progname, fname, strerror(errno));
7664 if (!S_ISREG(stbuf.st_mode)) {
7665 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7671 fd = open(fname, O_DIRECT | O_RDWR);
7673 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7674 progname, fname, strerror(errno));
7679 layout = llapi_layout_get_by_fd(fd, 0);
7680 if (layout == NULL) {
7681 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7682 progname, fname, strerror(errno));
7687 rc = llapi_layout_flags_get(layout, &flr_state);
7689 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
7690 progname, fname, strerror(errno));
7695 flr_state &= LCM_FL_FLR_MASK;
7696 if (flr_state == LCM_FL_NONE) {
7698 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
7703 /* get stale component info */
7704 comp_size = llapi_mirror_find_stale(layout, comp_array,
7705 ARRAY_SIZE(comp_array),
7706 mirror_ids, ids_nr);
7707 if (comp_size <= 0) {
7712 ioc->lil_mode = LL_LEASE_WRLCK;
7713 ioc->lil_flags = LL_LEASE_RESYNC;
7714 rc = llapi_lease_set(fd, ioc);
7716 if (rc == -EALREADY)
7720 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
7721 progname, fname, strerror(errno));
7726 while (idx < comp_size) {
7732 rc = llapi_lease_check(fd);
7733 if (rc != LL_LEASE_WRLCK) {
7734 fprintf(stderr, "%s: '%s' lost lease lock.\n",
7739 mirror_id = comp_array[idx].lrc_mirror_id;
7740 end = comp_array[idx].lrc_end;
7742 /* try to combine adjacent component */
7743 for (i = idx + 1; i < comp_size; i++) {
7744 if (mirror_id != comp_array[i].lrc_mirror_id ||
7745 end != comp_array[i].lrc_start)
7747 end = comp_array[i].lrc_end;
7750 result = llapi_mirror_resync_one(fd, layout, mirror_id,
7751 comp_array[idx].lrc_start,
7754 fprintf(stderr, "%s: '%s' llapi_mirror_resync_one: "
7755 "%ld.\n", progname, fname, result);
7758 } else if (result > 0) {
7761 /* mark synced components */
7762 for (j = idx; j < i; j++)
7763 comp_array[j].lrc_synced = true;
7770 /* prepare ioc for lease put */
7771 ioc->lil_mode = LL_LEASE_UNLCK;
7772 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
7774 for (idx = 0; idx < comp_size; idx++) {
7775 if (comp_array[idx].lrc_synced) {
7776 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
7781 rc = llapi_lease_set(fd, ioc);
7783 if (rc == 0) /* lost lease lock */
7785 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
7786 progname, fname, strerror(errno));
7790 * llapi_lease_set returns lease mode when it request to unlock
7796 llapi_layout_free(layout);
7803 static inline int lfs_mirror_resync(int argc, char **argv)
7805 struct ll_ioc_lease *ioc = NULL;
7806 __u16 mirror_ids[128] = { 0 };
7811 struct option long_opts[] = {
7812 { .val = 'o', .name = "only", .has_arg = required_argument },
7815 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
7818 rc = parse_mirror_ids(mirror_ids,
7819 sizeof(mirror_ids) / sizeof(__u16),
7823 "%s: bad mirror ids '%s'.\n",
7830 fprintf(stderr, "%s: options '%s' unrecognized.\n",
7831 argv[0], argv[optind - 1]);
7837 if (argc == optind) {
7838 fprintf(stderr, "%s: no file name given.\n", argv[0]);
7843 if (ids_nr > 0 && argc > optind + 1) {
7845 "%s: option '--only' cannot be used upon multiple files.\n",
7853 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
7858 /* set the lease on the file */
7859 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
7861 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
7862 argv[0], strerror(errno));
7867 for (; optind < argc; optind++) {
7868 rc = lfs_mirror_resync_file(argv[optind], ioc,
7869 mirror_ids, ids_nr);
7870 /* ignore previous file's error, continue with next file */
7873 memset(ioc, 0, sizeof(__u32) * 4096);
7882 * struct verify_chunk - Mirror chunk to be verified.
7883 * @chunk: [start, end) of the chunk.
7884 * @mirror_count: Number of mirror ids in @mirror_id array.
7885 * @mirror_id: Array of valid mirror ids that cover the chunk.
7887 struct verify_chunk {
7888 struct lu_extent chunk;
7889 unsigned int mirror_count;
7890 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
7894 * print_chunks() - Print chunk information.
7895 * @fname: Mirrored file name.
7896 * @chunks: Array of chunks.
7897 * @chunk_count: Number of chunks in @chunks array.
7899 * This function prints [start, end) of each chunk in @chunks
7900 * for mirrored file @fname, and also prints the valid mirror ids
7901 * that cover the chunk.
7906 void print_chunks(const char *fname, struct verify_chunk *chunks,
7912 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
7913 for (i = 0; i < chunk_count; i++) {
7914 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
7916 if (chunks[i].mirror_count == 0)
7917 fprintf(stdout, "\t[");
7919 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
7920 for (j = 1; j < chunks[i].mirror_count; j++)
7921 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
7923 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
7925 fprintf(stdout, "\n");
7929 * print_checksums() - Print CRC-32 checksum values.
7930 * @chunk: A chunk and its corresponding valid mirror ids.
7931 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
7933 * This function prints CRC-32 checksum values on @chunk for
7934 * each valid mirror that covers it.
7939 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
7944 "CRC-32 checksum value for chunk "DEXT":\n",
7945 PEXT(&chunk->chunk));
7946 for (i = 0; i < chunk->mirror_count; i++)
7947 fprintf(stdout, "Mirror %u:\t%#lx\n",
7948 chunk->mirror_id[i], crc[i]);
7949 fprintf(stdout, "\n");
7953 * filter_mirror_id() - Filter specified mirror ids.
7954 * @chunks: Array of chunks.
7955 * @chunk_count: Number of chunks in @chunks array.
7956 * @mirror_ids: Specified mirror ids to be verified.
7957 * @ids_nr: Number of specified mirror ids.
7959 * This function scans valid mirror ids that cover each chunk in @chunks
7960 * and filters specified mirror ids.
7965 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
7966 __u16 *mirror_ids, int ids_nr)
7971 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
7972 unsigned int valid_count = 0;
7974 for (i = 0; i < chunk_count; i++) {
7975 if (chunks[i].mirror_count == 0)
7979 for (j = 0; j < ids_nr; j++) {
7980 for (k = 0; k < chunks[i].mirror_count; k++) {
7981 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
7982 valid_id[valid_count] = mirror_ids[j];
7989 memcpy(chunks[i].mirror_id, valid_id,
7990 sizeof(__u16) * valid_count);
7991 chunks[i].mirror_count = valid_count;
7996 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
7997 * @layout: Mirror component list.
7998 * @chunks: Array of chunks.
7999 * @chunks_size: Array size of @chunks.
8001 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
8002 * to find out chunk segments and store them in @chunks array.
8004 * The @mirror_id array in each element of @chunks will store the valid
8005 * mirror ids that cover the chunk. If a mirror component covering the
8006 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
8007 * will not be stored into the @mirror_id array, and the chunk for that
8008 * mirror will not be verified.
8010 * The @mirror_count in each element of @chunks will store the number of
8011 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
8012 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
8013 * indicates the chunk is valid in only one mirror. In both cases, the
8014 * chunk will not be verified.
8016 * Here is an example:
8019 * +------+-------------+--------------------+
8021 * +------+------+------+------+-------------+
8022 * | | S | S | | mirror2
8023 * +-------------+------+------+-------------+
8025 * prepared @chunks array will contain 5 elements:
8026 * (([0, 1M), [1, 2], 2),
8027 * ([1M, 2M), [1, 2], 2),
8028 * ([2M, 3M), [1], 1),
8029 * ([3M, 4M], [], 0),
8030 * ([4M, EOF), [2], 1))
8032 * Return: the actual array size of @chunks on success
8033 * or a negative error code on failure.
8036 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
8037 struct verify_chunk *chunks,
8048 memset(chunks, 0, sizeof(*chunks) * chunks_size);
8051 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
8054 "%s: move to the first layout component: %s.\n",
8055 progname, strerror(errno));
8061 chunks[idx].chunk.e_end = LUSTRE_EOF;
8063 rc = llapi_layout_comp_extent_get(layout, &start, &end);
8066 "%s: llapi_layout_comp_extent_get failed: %s.\n",
8067 progname, strerror(errno));
8071 if (start > chunks[idx].chunk.e_start ||
8072 end <= chunks[idx].chunk.e_start)
8075 if (end < chunks[idx].chunk.e_end)
8076 chunks[idx].chunk.e_end = end;
8078 rc = llapi_layout_comp_flags_get(layout, &flags);
8081 "%s: llapi_layout_comp_flags_get failed: %s.\n",
8082 progname, strerror(errno));
8086 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8089 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8092 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8093 progname, strerror(errno));
8097 chunks[idx].mirror_id[i] = mirror_id;
8099 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8101 "%s: mirror_id array is too small.\n",
8108 rc = llapi_layout_comp_use(layout,
8109 LLAPI_LAYOUT_COMP_USE_NEXT);
8112 "%s: move to the next layout component: %s.\n",
8113 progname, strerror(errno));
8116 } /* loop through all components */
8118 chunks[idx].mirror_count = i;
8120 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8124 if (idx >= chunks_size) {
8125 fprintf(stderr, "%s: chunks array is too small.\n",
8131 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
8135 return rc < 0 ? rc : idx + 1;
8139 * lfs_mirror_verify_chunk() - Verify a chunk.
8140 * @fd: File descriptor of the mirrored file.
8141 * @file_size: Size of the mirrored file.
8142 * @chunk: A chunk and its corresponding valid mirror ids.
8143 * @verbose: Verbose mode.
8145 * This function verifies a @chunk contains exactly the same data
8146 * ammong the mirrors that cover it.
8148 * If @verbose is specified, then the function will print where the
8149 * differences are if the data do not match. Otherwise, it will
8150 * just return an error in that case.
8152 * Return: 0 on success or a negative error code on failure.
8155 int lfs_mirror_verify_chunk(int fd, size_t file_size,
8156 struct verify_chunk *chunk, int verbose)
8158 const size_t buflen = 4 * 1024 * 1024; /* 4M */
8160 size_t page_size = sysconf(_SC_PAGESIZE);
8166 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8173 rc = posix_memalign(&buf, page_size, buflen);
8174 if (rc) /* error code is returned directly */
8178 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8179 PEXT(&chunk->chunk));
8180 for (i = 0; i < chunk->mirror_count; i++)
8181 fprintf(stdout, " %u", chunk->mirror_id[i]);
8182 fprintf(stdout, "\n");
8186 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
8187 pos = chunk->chunk.e_start;
8188 while (bytes_done < count) {
8189 /* compute initial CRC-32 checksum */
8190 crc = crc32(0L, Z_NULL, 0);
8191 memset(crc_array, 0, sizeof(crc_array));
8194 for (i = 0; i < chunk->mirror_count; i++) {
8195 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
8197 if (bytes_read < 0) {
8200 "%s: failed to read data from mirror %u: %s.\n",
8201 progname, chunk->mirror_id[i],
8206 /* compute new CRC-32 checksum */
8207 crc_array[i] = crc32(crc, buf, bytes_read);
8211 print_checksums(chunk, crc_array);
8213 /* compare CRC-32 checksum values */
8214 for (i = 1; i < chunk->mirror_count; i++) {
8215 if (crc_array[i] != crc_array[0]) {
8221 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
8222 progname, PEXT(&chunk->chunk),
8223 chunk->mirror_id[0],
8224 chunk->mirror_id[i]);
8229 bytes_done += bytes_read;
8232 if (verbose > 1 && rc == 0) {
8233 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8234 PEXT(&chunk->chunk));
8235 for (i = 0; i < chunk->mirror_count; i++)
8236 fprintf(stdout, " %u", chunk->mirror_id[i]);
8237 fprintf(stdout, " PASS\n\n");
8246 * lfs_mirror_verify_file() - Verify a mirrored file.
8247 * @fname: Mirrored file name.
8248 * @mirror_ids: Specified mirror ids to be verified.
8249 * @ids_nr: Number of specified mirror ids.
8250 * @verbose: Verbose mode.
8252 * This function verifies that each SYNC mirror of a mirrored file
8253 * specified by @fname contains exactly the same data.
8255 * If @mirror_ids is specified, then the function will verify the
8256 * mirrors specified by @mirror_ids contain exactly the same data.
8258 * If @verbose is specified, then the function will print where the
8259 * differences are if the data do not match. Otherwise, it will
8260 * just return an error in that case.
8262 * Return: 0 on success or a negative error code on failure.
8265 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8268 struct verify_chunk chunks_array[1024] = { };
8269 struct llapi_layout *layout = NULL;
8273 int chunk_count = 0;
8279 if (stat(fname, &stbuf) < 0) {
8280 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8281 progname, fname, strerror(errno));
8286 if (!S_ISREG(stbuf.st_mode)) {
8287 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8293 if (stbuf.st_size == 0) {
8295 fprintf(stdout, "%s: '%s' file size is 0.\n",
8301 fd = open(fname, O_DIRECT | O_RDONLY);
8303 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8304 progname, fname, strerror(errno));
8309 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8311 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8312 progname, fname, strerror(errno));
8316 layout = llapi_layout_get_by_fd(fd, 0);
8317 if (layout == NULL) {
8318 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8319 progname, fname, strerror(errno));
8321 llapi_lease_release(fd);
8325 rc = llapi_layout_flags_get(layout, &flr_state);
8327 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8328 progname, fname, strerror(errno));
8333 flr_state &= LCM_FL_FLR_MASK;
8334 switch (flr_state) {
8337 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8338 progname, fname, llapi_layout_flags_string(flr_state));
8344 /* find out mirror chunks to be verified */
8345 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8346 ARRAY_SIZE(chunks_array));
8347 if (chunk_count < 0) {
8353 /* filter specified mirror ids */
8354 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8357 print_chunks(fname, chunks_array, chunk_count);
8359 for (idx = 0; idx < chunk_count; idx++) {
8360 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8363 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8365 PEXT(&chunks_array[idx].chunk),
8366 (unsigned long long)stbuf.st_size);
8370 if (chunks_array[idx].mirror_count == 0) {
8372 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8374 PEXT(&chunks_array[idx].chunk));
8376 fprintf(stderr, "skipped\n");
8380 fprintf(stderr, "failed\n");
8384 if (chunks_array[idx].mirror_count == 1) {
8387 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8389 PEXT(&chunks_array[idx].chunk),
8390 chunks_array[idx].mirror_id[0]);
8394 rc = llapi_lease_check(fd);
8395 if (rc != LL_LEASE_RDLCK) {
8396 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8401 /* verify one chunk */
8402 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8403 &chunks_array[idx], verbose);
8417 llapi_layout_free(layout);
8418 llapi_lease_release(fd);
8426 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
8427 * @argc: The count of lfs mirror verify command line arguments.
8428 * @argv: Array of strings for lfs mirror verify command line arguments.
8430 * This function parses lfs mirror verify command and verifies the
8431 * specified mirrored file(s).
8433 * Return: 0 on success or a negative error code on failure.
8435 static inline int lfs_mirror_verify(int argc, char **argv)
8437 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8445 struct option long_opts[] = {
8446 { .val = 'o', .name = "only", .has_arg = required_argument },
8447 { .val = 'v', .name = "verbose", .has_arg = no_argument },
8450 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8452 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
8455 rc = parse_mirror_ids(mirror_ids,
8456 ARRAY_SIZE(mirror_ids),
8460 "%s: bad mirror ids '%s'.\n",
8467 "%s: at least 2 mirror ids needed with '--only' option.\n",
8477 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8478 progname, argv[optind - 1]);
8484 if (argc == optind) {
8485 fprintf(stderr, "%s: no file name given.\n", progname);
8490 if (ids_nr > 0 && argc > optind + 1) {
8492 "%s: '--only' cannot be used upon multiple files.\n",
8500 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8506 for (; optind < argc; optind++) {
8507 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
8517 * lfs_mirror() - Parse and execute lfs mirror commands.
8518 * @argc: The count of lfs mirror command line arguments.
8519 * @argv: Array of strings for lfs mirror command line arguments.
8521 * This function parses lfs mirror commands and performs the
8522 * corresponding functions specified in mirror_cmdlist[].
8524 * Return: 0 on success or an error code on failure.
8526 static int lfs_mirror(int argc, char **argv)
8533 Parser_init("lfs-mirror > ", mirror_cmdlist);
8535 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
8537 program_invocation_short_name = cmd;
8539 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
8541 rc = Parser_commands();
8543 return rc < 0 ? -rc : rc;
8547 * lfs_mirror_list_commands() - List lfs mirror commands.
8548 * @argc: The count of command line arguments.
8549 * @argv: Array of strings for command line arguments.
8551 * This function lists lfs mirror commands defined in mirror_cmdlist[].
8553 * Return: 0 on success.
8555 static int lfs_mirror_list_commands(int argc, char **argv)
8557 char buffer[81] = "";
8559 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
8565 static int lfs_list_commands(int argc, char **argv)
8567 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
8569 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
8574 int main(int argc, char **argv)
8578 /* Ensure that liblustreapi constructor has run */
8579 if (!llapi_liblustreapi_initialized())
8580 fprintf(stderr, "liblustreapi was not properly initialized\n");
8585 Parser_init("lfs > ", cmdlist);
8587 progname = program_invocation_short_name; /* Used in error messages */
8589 llapi_set_command_name(argv[1]);
8590 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
8591 llapi_clear_command_name();
8593 rc = Parser_commands();
8596 return rc < 0 ? -rc : rc;
8599 #ifdef _LUSTRE_IDL_H_
8600 /* Everything we need here should be included by lustreapi.h. */
8601 # error "lfs should not depend on lustre_idl.h"
8602 #endif /* _LUSTRE_IDL_H_ */