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>
61 #include <sys/xattr.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
81 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
82 #endif /* !ARRAY_SIZE */
85 static int lfs_find(int argc, char **argv);
86 static int lfs_getstripe(int argc, char **argv);
87 static int lfs_getdirstripe(int argc, char **argv);
88 static int lfs_setdirstripe(int argc, char **argv);
89 static int lfs_rmentry(int argc, char **argv);
90 static int lfs_osts(int argc, char **argv);
91 static int lfs_mdts(int argc, char **argv);
92 static int lfs_df(int argc, char **argv);
93 static int lfs_getname(int argc, char **argv);
94 static int lfs_check(int argc, char **argv);
95 #ifdef HAVE_SYS_QUOTA_H
96 static int lfs_setquota(int argc, char **argv);
97 static int lfs_quota(int argc, char **argv);
98 static int lfs_project(int argc, char **argv);
100 static int lfs_flushctx(int argc, char **argv);
101 static int lfs_cp(int argc, char **argv);
102 static int lfs_ls(int argc, char **argv);
103 static int lfs_poollist(int argc, char **argv);
104 static int lfs_changelog(int argc, char **argv);
105 static int lfs_changelog_clear(int argc, char **argv);
106 static int lfs_fid2path(int argc, char **argv);
107 static int lfs_path2fid(int argc, char **argv);
108 static int lfs_data_version(int argc, char **argv);
109 static int lfs_hsm_state(int argc, char **argv);
110 static int lfs_hsm_set(int argc, char **argv);
111 static int lfs_hsm_clear(int argc, char **argv);
112 static int lfs_hsm_action(int argc, char **argv);
113 static int lfs_hsm_archive(int argc, char **argv);
114 static int lfs_hsm_restore(int argc, char **argv);
115 static int lfs_hsm_release(int argc, char **argv);
116 static int lfs_hsm_remove(int argc, char **argv);
117 static int lfs_hsm_cancel(int argc, char **argv);
118 static int lfs_swap_layouts(int argc, char **argv);
119 static int lfs_mv(int argc, char **argv);
120 static int lfs_ladvise(int argc, char **argv);
121 static int lfs_getsom(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_dump(int argc, char **argv);
129 enum setstripe_origin {
137 static int lfs_setstripe_internal(int argc, char **argv,
138 enum setstripe_origin opc);
140 static inline int lfs_setstripe(int argc, char **argv)
142 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
144 static inline int lfs_setstripe_migrate(int argc, char **argv)
146 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
148 static inline int lfs_mirror_create(int argc, char **argv)
150 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
152 static inline int lfs_mirror_extend(int argc, char **argv)
154 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
156 static inline int lfs_mirror_split(int argc, char **argv)
158 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
161 /* Setstripe and migrate share mostly the same parameters */
162 #define SSM_CMD_COMMON(cmd) \
163 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
164 " [--stripe-count|-c <stripe_count>]\n" \
165 " [--stripe-index|-i <start_ost_idx>]\n" \
166 " [--stripe-size|-S <stripe_size>]\n" \
167 " [--layout|-L <pattern>]\n" \
168 " [--pool|-p <pool_name>]\n" \
169 " [--ost|-o <ost_indices>]\n" \
170 " [--yaml|-y <yaml_template_file>]\n" \
171 " [--copy=<lustre_src>]\n"
173 #define SSM_HELP_COMMON \
174 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
175 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
176 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
177 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
178 "\t respectively)\n" \
179 "\tpool_name: Name of OST pool to use (default none)\n" \
180 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
181 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
182 "\t Indices be specified in a format of:\n" \
183 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
185 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
186 "\t If --pool is set with --ost, then the OSTs\n" \
187 "\t must be the members of the pool.\n" \
188 "\tcomp_end: Extent end of component, start after previous end.\n"\
189 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
190 "\t respectively, -1 for EOF). Must be a multiple of\n"\
191 "\t stripe_size.\n" \
192 "\tyaml_template_file:\n" \
193 "\t YAML layout template file, can't be used with -c,\n" \
194 "\t -i, -S, -p, -o, or -E arguments.\n" \
195 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
196 "\t another lustre file or directory, can't used with\n" \
197 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
199 #define MIRROR_CREATE_HELP \
200 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
201 "\t setstripe layout options\n" \
202 "\t It defaults to 1 if not specified; if specified,\n" \
203 "\t it must follow the option without a space.\n" \
204 "\t The option can also be repeated multiple times to\n" \
205 "\t separate mirrors that have different layouts.\n" \
206 "\tsetstripe options: Mirror layout\n" \
207 "\t It can be a plain layout or a composite layout.\n" \
208 "\t If not specified, the stripe options inherited\n" \
209 "\t from the previous component will be used.\n" \
210 "\tflags: set flags to the component of the current mirror.\n" \
211 "\t Only \"prefer\" flag is supported so far.\n"
213 #define MIRROR_EXTEND_HELP \
215 "\tvictim_file: The layout of victim_file will be split and used\n" \
216 "\t as a mirror added to the mirrored file.\n" \
217 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
218 "\t from victim file(s) in case the victim file(s)\n" \
219 "\t contains the same data as the original mirrored\n" \
222 #define MIRROR_EXTEND_USAGE \
223 " <--mirror-count|-N[mirror_count]>\n" \
224 " [setstripe options|-f <victim_file>]\n" \
227 #define SETSTRIPE_USAGE \
228 SSM_CMD_COMMON("setstripe") \
229 MIRROR_EXTEND_USAGE \
230 " <directory|filename>\n" \
234 #define MIGRATE_USAGE \
235 SSM_CMD_COMMON("migrate ") \
237 " [--non-block|-n]\n" \
238 " [--non-direct|-D]\n" \
242 "\tblock: Block file access during data migration (default)\n" \
243 "\tnon-block: Abort migrations if concurrent access is detected\n" \
244 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
246 #define SETDIRSTRIPE_USAGE \
247 " [--mdt-count|-c stripe_count>\n" \
248 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
249 " [--mdt-hash|-H mdt_hash]\n" \
250 " [--default|-D] [--mode|-m mode] <dir>\n" \
251 "\tstripe_count: stripe count of the striped directory\n" \
252 "\tmdt_index: MDT index of first stripe\n" \
253 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
254 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
255 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
256 "\tdefault_stripe: set default dirstripe of the directory\n" \
257 "\tmode: the mode of the directory\n"
260 * command_t mirror_cmdlist - lfs mirror commands.
262 command_t mirror_cmdlist[] = {
263 { .pc_name = "create", .pc_func = lfs_mirror_create,
264 .pc_help = "Create a mirrored file.\n"
265 "usage: lfs mirror create "
266 "<--mirror-count|-N[mirror_count]> "
267 "[setstripe options] ... <filename|directory>\n"
268 MIRROR_CREATE_HELP },
269 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
270 .pc_help = "Extend a mirrored file.\n"
271 "usage: lfs mirror extend "
272 "<--mirror-count|-N[mirror_count]> [--no-verify] "
273 "[setstripe options|-f <victim_file>] ... <filename>\n"
274 MIRROR_EXTEND_HELP },
275 { .pc_name = "split", .pc_func = lfs_mirror_split,
276 .pc_help = "Split a mirrored file.\n"
277 "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
278 "\t <--component-id|-I <comp_id>> [--destroy|-d] \n"
279 "\t [-f <new_file>] <mirrored file>\n"
280 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
281 "\t can be fetched by lfs getstripe command.\n"
282 "\tcomp_id: Unique component ID within a mirror.\n"
283 "\tnew_file: This option indicates the layout of the split\n"
284 "\t mirror will be stored into. If not specified,\n"
285 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
286 "\t will be used.\n" },
287 { .pc_name = "dump", .pc_func = lfs_mirror_dump,
288 .pc_help = "Dump the content of a specified mirror of a file.\n"
289 "usage: lfs mirror dump <--mirror-id|-N <mirror_id> "
290 "[--outfile|-o <output_file>] <mirrored_file>\n" },
291 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
292 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
293 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
294 "<mirrored file> [<mirrored file2>...]\n"},
295 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
296 .pc_help = "Verify mirrored file(s).\n"
297 "usage: lfs mirror verify "
298 "[--only <mirror_id,mirror_id2[,...]>] "
299 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
300 { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
301 .pc_help = "list commands supported by lfs mirror"},
302 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
303 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
304 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
308 /* all available commands */
309 command_t cmdlist[] = {
310 {"setstripe", lfs_setstripe, 0,
311 "To create a file with specified striping/composite layout, or\n"
312 "create/replace the default layout on an existing directory:\n"
313 SSM_CMD_COMMON("setstripe")
314 " <directory|filename>\n"
316 "To add component(s) to an existing composite file:\n"
317 SSM_CMD_COMMON("setstripe --component-add")
319 "To totally delete the default striping from an existing directory:\n"
320 "usage: setstripe -d <directory>\n"
322 "To create a mirrored file or set s default mirror layout on a directory:\n"
323 "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
325 "To delete the last component(s) from an existing composite file\n"
326 "(note that this will also delete any data in those components):\n"
327 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
328 " [--component-flags|-F <comp_flags>]\n"
330 "\tcomp_id: Unique component ID to delete\n"
331 "\tcomp_flags: 'init' indicating all instantiated components\n"
332 "\t '^init' indicating all uninstantiated components\n"
333 "\t-I and -F cannot be specified at the same time\n"},
334 {"getstripe", lfs_getstripe, 0,
335 "To list the layout pattern for a given file or files in a\n"
336 "directory or recursively for all files in a directory tree.\n"
337 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
338 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
339 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
340 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
341 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
342 " [--component-id[=comp_id]|-I[comp_id]]\n"
343 " [--component-flags[=comp_flags]]\n"
344 " [--component-count]\n"
345 " [--component-start[=[+-]comp_start]]\n"
346 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
347 " [[!] --mirror-index=[+-]<index> |\n"
348 " [!] --mirror-id=[+-]<id>]\n"
349 " <directory|filename> ..."},
350 {"setdirstripe", lfs_setdirstripe, 0,
351 "To create a striped directory on a specified MDT. This can only\n"
352 "be done on MDT0 with the right of administrator.\n"
353 "usage: setdirstripe [OPTION] <directory>\n"
355 {"getdirstripe", lfs_getdirstripe, 0,
356 "To list the layout pattern info for a given directory\n"
357 "or recursively for all directories in a directory tree.\n"
358 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
359 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
360 " [--recursive|-r] [--yaml|-y]\n"
361 " [--default|-D] <dir> ..."},
362 {"mkdir", lfs_setdirstripe, 0,
363 "To create a striped directory on a specified MDT. This can only\n"
364 "be done on MDT0 with the right of administrator.\n"
365 "usage: mkdir [OPTION] <directory>\n"
367 {"rm_entry", lfs_rmentry, 0,
368 "To remove the name entry of the remote directory. Note: This\n"
369 "command will only delete the name entry, i.e. the remote directory\n"
370 "will become inaccessable after this command. This can only be done\n"
371 "by the administrator\n"
372 "usage: rm_entry <dir>\n"},
373 {"pool_list", lfs_poollist, 0,
374 "List pools or pool OSTs\n"
375 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
376 {"find", lfs_find, 0,
377 "find files matching given attributes recursively in directory tree.\n"
378 "usage: find <directory|filename> ...\n"
379 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
380 " [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
381 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
382 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
383 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
384 " [[!] --stripe-count|-c [+-]<stripes>]\n"
385 " [[!] --stripe-index|-i <index,...>]\n"
386 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
387 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
388 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
389 " [[!] --projid <projid>]\n"
390 " [[!] --layout|-L released,raid0,mdt]\n"
391 " [[!] --component-count [+-]<comp_cnt>]\n"
392 " [[!] --component-start [+-]N[kMGTPE]]\n"
393 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
394 " [[!] --component-flags <comp_flags>]\n"
395 " [[!] --mirror-count|-N [+-]<n>]\n"
396 " [[!] --mirror-state <[^]state>]\n"
397 " [[!] --mdt-count|-T [+-]<stripes>]\n"
398 " [[!] --mdt-hash|-H <hashtype>\n"
399 "\t !: used before an option indicates 'NOT' requested attribute\n"
400 "\t -: used before a value indicates less than requested value\n"
401 "\t +: used before a value indicates more than requested value\n"
402 "\thashtype: hash type of the striped directory.\n"
403 "\t fnv_1a_64 FNV-1a hash algorithm\n"
404 "\t all_char sum of characters % MDT_COUNT\n"},
405 {"check", lfs_check, 0,
406 "Display the status of MDS or OSTs (as specified in the command)\n"
407 "or all the servers (MDS and OSTs).\n"
408 "usage: check <osts|mds|servers>"},
409 {"osts", lfs_osts, 0, "list OSTs connected to client "
410 "[for specified path only]\n" "usage: osts [path]"},
411 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
412 "[for specified path only]\n" "usage: mdts [path]"},
414 "report filesystem disk space usage or inodes usage"
415 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
416 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
417 {"getname", lfs_getname, 0, "list instances and specified mount points "
418 "[for specified path only]\n"
419 "Usage: getname [-h]|[path ...] "},
420 #ifdef HAVE_SYS_QUOTA_H
421 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
422 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
423 " -b <block-softlimit> -B <block-hardlimit>\n"
424 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
425 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
426 " [--block-softlimit <block-softlimit>]\n"
427 " [--block-hardlimit <block-hardlimit>]\n"
428 " [--inode-softlimit <inode-softlimit>]\n"
429 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
430 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
431 " [--block-grace <block-grace>]\n"
432 " [--inode-grace <inode-grace>] <filesystem>\n"
433 " setquota <-U|-G|-P>\n"
434 " -b <block-softlimit> -B <block-hardlimit>\n"
435 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
436 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
437 " [--block-softlimit <block-softlimit>]\n"
438 " [--block-hardlimit <block-hardlimit>]\n"
439 " [--inode-softlimit <inode-softlimit>]\n"
440 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
441 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
443 " -b can be used instead of --block-softlimit/--block-grace\n"
444 " -B can be used instead of --block-hardlimit\n"
445 " -i can be used instead of --inode-softlimit/--inode-grace\n"
446 " -I can be used instead of --inode-hardlimit\n"
447 " -d can be used instead of --default\n\n"
448 "Note: The total quota space will be split into many qunits and\n"
449 " balanced over all server targets, the minimal qunit size is\n"
450 " 1M bytes for block space and 1K inodes for inode space.\n\n"
451 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
452 " Quota space rebalancing process will stop when this mininum\n"
453 " value is reached. As a result, quota exceeded can be returned\n"
454 " while many targets still have 1MB or 1K inodes of spare\n"
456 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
457 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
459 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
460 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
461 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
462 {"project", lfs_project, 0,
463 "Change or list project attribute for specified file or directory.\n"
464 "usage: project [-d|-r] <file|directory...>\n"
465 " list project ID and flags on file(s) or directories\n"
466 " project [-p id] [-s] [-r] <file|directory...>\n"
467 " set project ID and/or inherit flag for specified file(s) or directories\n"
468 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
469 " check project ID and flags on file(s) or directories, print outliers\n"
470 " project -C [-r] [-k] <file|directory...>\n"
471 " clear the project inherit flag and ID on the file or directory\n"
474 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
475 "usage: flushctx [-k] [mountpoint...]"},
477 "Remote user copy files and directories.\n"
478 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
480 "Remote user list directory contents.\n"
481 "usage: ls [OPTION]... [FILE]..."},
482 {"changelog", lfs_changelog, 0,
483 "Show the metadata changes on an MDT."
484 "\nusage: changelog <mdtname> [startrec [endrec]]"},
485 {"changelog_clear", lfs_changelog_clear, 0,
486 "Indicate that old changelog records up to <endrec> are no longer of "
487 "interest to consumer <id>, allowing the system to free up space.\n"
488 "An <endrec> of 0 means all records.\n"
489 "usage: changelog_clear <mdtname> <id> <endrec>"},
490 {"fid2path", lfs_fid2path, 0,
491 "Resolve the full path(s) for given FID(s). For a specific hardlink "
492 "specify link number <linkno>.\n"
493 /* "For a historical link name, specify changelog record <recno>.\n" */
494 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
495 /* [ --rec <recno> ] */ },
496 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
497 "usage: path2fid [--parents] <path> ..."},
498 {"data_version", lfs_data_version, 0, "Display file data version for "
499 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
500 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
501 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
502 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
503 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
504 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
505 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
507 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
508 "[--archived] [--lost] <file> ..."},
509 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
510 "given files.\n" "usage: hsm_action <file> ..."},
511 {"hsm_archive", lfs_hsm_archive, 0,
512 "Archive file to external storage.\n"
513 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
515 {"hsm_restore", lfs_hsm_restore, 0,
516 "Restore file from external storage.\n"
517 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
518 {"hsm_release", lfs_hsm_release, 0,
519 "Release files from Lustre.\n"
520 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
521 {"hsm_remove", lfs_hsm_remove, 0,
522 "Remove file copy from external storage.\n"
523 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
525 " (FILE [FILE ...] | "
526 "--mntpath MOUNTPATH FID [FID ...])\n"
528 "Note: To remove an archived copy of a file already deleted from a "
530 "--mntpath option and a list of FIDs must be specified"
532 {"hsm_cancel", lfs_hsm_cancel, 0,
533 "Cancel requests related to specified files.\n"
534 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
535 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
536 "usage: swap_layouts <path1> <path2>"},
537 {"migrate", lfs_setstripe_migrate, 0,
538 "migrate a directory between MDTs.\n"
539 "usage: migrate [--mdt|-m] <start_mdt_index>\n"
540 " [--mdt-count|-c] <stripe_count>\n"
541 " [--mdt-hash|-H] <hash_type>\n"
544 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
545 " it's the MDT index of first stripe\n"
546 "\tmdt_count: number of MDTs to stripe a directory over\n"
547 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
548 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
549 " all_char sum of characters % MDT_COUNT\n"
551 "migrate file objects from one OST "
552 "layout\nto another (may be not safe with concurent writes).\n"
554 "[--stripe-count|-c] <stripe_count>\n"
555 " [--stripe-index|-i] <start_ost_index>\n"
556 " [--stripe-size|-S] <stripe_size>\n"
557 " [--pool|-p] <pool_name>\n"
558 " [--ost|-o] <ost_indices>\n"
560 " [--non-block|-n]\n"
561 " [--non-direct|-D]\n"
562 " <file|directory>\n"
563 "\tstripe_count: number of OSTs to stripe a file over\n"
564 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
565 "\tstripe_size: number of bytes to store before moving to the next OST\n"
566 "\tpool_name: name of the predefined pool of OSTs\n"
567 "\tost_indices: OSTs to stripe over, in order\n"
568 "\tblock: wait for the operation to return before continuing\n"
569 "\tnon-block: do not wait for the operation to return\n"
570 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
572 "To move directories between MDTs. This command is deprecated, "
573 "use \"migrate\" instead.\n"
574 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
576 {"ladvise", lfs_ladvise, 0,
577 "Provide servers with advice about access patterns for a file.\n"
578 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
579 " [--background|-b] [--unset|-u]\n\n"
580 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
581 " {[--mode|-m [READ,WRITE]}\n"
583 {"mirror", lfs_mirror, mirror_cmdlist,
584 "lfs commands used to manage files with mirrored components:\n"
585 "lfs mirror create - create a mirrored file or directory\n"
586 "lfs mirror extend - add mirror(s) to an existing file\n"
587 "lfs mirror split - split a mirror from an existing mirrored file\n"
588 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
589 "lfs mirror dump - dump a mirror content of a mirrored file\n"
590 "lfs mirror verify - verify mirrored file(s)\n"},
591 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
592 "usage: getsom [-s] [-b] [-f] <path>\n"
593 "\t-s: Only show the size value of the SOM data for a given file\n"
594 "\t-b: Only show the blocks value of the SOM data for a given file\n"
595 "\t-f: Only show the flags value of the SOM data for a given file\n"},
596 {"help", Parser_help, 0, "help"},
597 {"exit", Parser_quit, 0, "quit"},
598 {"quit", Parser_quit, 0, "quit"},
599 {"--version", Parser_version, 0,
600 "output build version of the utility and exit"},
601 {"--list-commands", lfs_list_commands, 0,
602 "list commands supported by the utility and exit"},
607 static int check_hashtype(const char *hashtype)
611 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
612 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
619 static const char *error_loc = "syserror";
622 MIGRATION_NONBLOCK = 1 << 0,
623 MIGRATION_MIRROR = 1 << 1,
624 MIGRATION_NONDIRECT = 1 << 2,
627 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
628 struct llapi_layout *layout);
631 migrate_open_files(const char *name, __u64 migration_flags,
632 const struct llapi_stripe_param *param,
633 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
640 char parent[PATH_MAX];
641 char volatile_file[PATH_MAX];
647 if (param == NULL && layout == NULL) {
648 error_loc = "layout information";
652 /* search for file directory pathname */
653 if (strlen(name) > sizeof(parent) - 1) {
654 error_loc = "source file name";
658 strncpy(parent, name, sizeof(parent));
659 ptr = strrchr(parent, '/');
661 if (getcwd(parent, sizeof(parent)) == NULL) {
662 error_loc = "getcwd";
666 if (ptr == parent) /* leading '/' */
671 /* open file, direct io */
672 /* even if the file is only read, WR mode is nedeed to allow
673 * layout swap on fd */
675 if (!(migration_flags & MIGRATION_NONDIRECT))
677 fd = open(name, rflags);
680 error_loc = "cannot open source file";
684 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
686 error_loc = "cannot get MDT index";
691 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
692 mode_t open_mode = S_IRUSR | S_IWUSR;
694 random_value = random();
695 rc = snprintf(volatile_file, sizeof(volatile_file),
696 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
697 mdt_index, random_value);
698 if (rc >= sizeof(volatile_file)) {
703 /* create, open a volatile file, use caching (ie no directio) */
705 fdv = llapi_file_open_param(volatile_file, open_flags,
708 fdv = lfs_component_create(volatile_file, open_flags,
710 } while (fdv < 0 && (rc = fdv) == -EEXIST);
713 error_loc = "cannot create volatile file";
717 /* In case the MDT does not support creation of volatile files
718 * we should try to unlink it. */
719 (void)unlink(volatile_file);
721 /* Not-owner (root?) special case.
722 * Need to set owner/group of volatile file like original.
723 * This will allow to pass related check during layout_swap.
728 error_loc = "cannot stat source file";
732 rc = fstat(fdv, &stv);
735 error_loc = "cannot stat volatile";
739 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
740 rc = fchown(fdv, st.st_uid, st.st_gid);
743 error_loc = "cannot change ownwership of volatile";
762 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
764 struct llapi_layout *layout;
765 size_t buf_size = 4 * 1024 * 1024;
774 layout = llapi_layout_get_by_fd(fd_src, 0);
775 if (layout != NULL) {
776 uint64_t stripe_size;
778 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
780 buf_size = stripe_size;
782 llapi_layout_free(layout);
785 /* Use a page-aligned buffer for direct I/O */
786 rc = posix_memalign(&buf, getpagesize(), buf_size);
791 /* read new data only if we have written all
792 * previously read data */
795 rc = check_file(fd_src);
800 rsize = read(fd_src, buf, buf_size);
812 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
831 static int migrate_copy_timestamps(int fd, int fdv)
835 if (fstat(fd, &st) == 0) {
836 struct timeval tv[2] = {
837 {.tv_sec = st.st_atime},
838 {.tv_sec = st.st_mtime}
841 return futimes(fdv, tv);
847 static int migrate_block(int fd, int fdv)
854 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
856 error_loc = "cannot get dataversion";
864 /* The grouplock blocks all concurrent accesses to the file.
865 * It has to be taken after llapi_get_data_version as it would
867 rc = llapi_group_lock(fd, gid);
869 error_loc = "cannot get group lock";
873 rc = migrate_copy_data(fd, fdv, NULL);
875 error_loc = "data copy failed";
879 /* Make sure we keep original atime/mtime values */
880 rc = migrate_copy_timestamps(fd, fdv);
882 error_loc = "timestamp copy failed";
887 * for a migration we need to check data version on file did
890 * Pass in gid=0 since we already own grouplock. */
891 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
892 SWAP_LAYOUTS_CHECK_DV1);
894 error_loc = "file changed";
897 error_loc = "cannot swap layout";
902 rc2 = llapi_group_unlock(fd, gid);
903 if (rc2 < 0 && rc == 0) {
904 error_loc = "unlock group lock";
912 * Internal helper for migrate_copy_data(). Check lease and report error if
915 * \param[in] fd File descriptor on which to check the lease.
917 * \retval 0 Migration can keep on going.
918 * \retval -errno Error occurred, abort migration.
920 static int check_lease(int fd)
924 rc = llapi_lease_check(fd);
926 return 0; /* llapi_check_lease returns > 0 on success. */
931 static int migrate_nonblock(int fd, int fdv)
937 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
939 error_loc = "cannot get data version";
943 rc = migrate_copy_data(fd, fdv, check_lease);
945 error_loc = "data copy failed";
949 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
951 error_loc = "cannot get data version";
957 error_loc = "source file changed";
961 /* Make sure we keep original atime/mtime values */
962 rc = migrate_copy_timestamps(fd, fdv);
964 error_loc = "timestamp copy failed";
971 static int lfs_component_set(char *fname, int comp_id,
972 __u32 flags, __u32 neg_flags)
975 __u32 flags_array[2];
980 ids[count] = comp_id;
981 flags_array[count] = flags;
986 ids[count] = comp_id;
987 flags_array[count] = neg_flags | LCME_FL_NEG;
991 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
994 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
995 progname, comp_id, fname, flags, neg_flags);
1000 static int lfs_component_del(char *fname, __u32 comp_id,
1001 __u32 flags, __u32 neg_flags)
1005 if (flags && neg_flags)
1008 if (!flags && neg_flags)
1009 flags = neg_flags | LCME_FL_NEG;
1011 if ((flags && comp_id) || (!flags && !comp_id))
1014 /* LCME_FL_INIT is the only supported flag in PFL */
1016 if (flags & ~LCME_KNOWN_FLAGS) {
1018 "%s setstripe: unknown flags %#x\n",
1022 } else if (comp_id > LCME_ID_MAX) {
1023 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1028 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1031 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1032 progname, comp_id, fname, strerror(errno));
1036 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1043 rc = llapi_layout_file_comp_add(fname, layout);
1045 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1046 fname, strerror(errno));
1050 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1051 struct llapi_layout *layout)
1059 fd = lstat(fname, &st);
1060 if (fd == 0 && S_ISDIR(st.st_mode))
1061 open_flags = O_DIRECTORY | O_RDONLY;
1063 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1065 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1066 S_ISDIR(st.st_mode) ?
1067 "set default composite layout for" :
1068 "create composite file",
1069 fname, strerror(errno));
1073 static int lfs_migrate(char *name, __u64 migration_flags,
1074 struct llapi_stripe_param *param,
1075 struct llapi_layout *layout)
1081 rc = migrate_open_files(name, migration_flags, param, layout,
1086 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1087 /* Blocking mode (forced if servers do not support file lease).
1088 * It is also the default mode, since we cannot distinguish
1089 * between a broken lease and a server that does not support
1090 * atomic swap/close (LU-6785) */
1091 rc = migrate_block(fd, fdv);
1095 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1097 error_loc = "cannot get lease";
1101 rc = migrate_nonblock(fd, fdv);
1103 llapi_lease_release(fd);
1107 /* Atomically put lease, swap layouts and close.
1108 * for a migration we need to check data version on file did
1110 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1112 error_loc = "cannot swap layout";
1124 fprintf(stderr, "error: %s: %s: %s: %s\n",
1125 progname, name, error_loc, strerror(-rc));
1129 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1138 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1142 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1143 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1144 const char *comp_name = comp_flags_table[i].cfn_name;
1146 if (strcmp(name, comp_name) == 0) {
1147 *flags |= comp_flag;
1149 } else if (strncmp(name, "^", 1) == 0 &&
1150 strcmp(name + 1, comp_name) == 0) {
1151 *neg_flags |= comp_flag;
1156 llapi_printf(LLAPI_MSG_ERROR,
1157 "%s: component flag '%s' not supported\n",
1163 if (!*flags && !*neg_flags)
1166 /* don't allow to set and exclude the same flag */
1167 if (*flags & *neg_flags)
1173 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1181 if (strncmp(string, "^", 1) == 0) {
1182 *neg_state = llapi_layout_string_flags(string + 1);
1183 if (*neg_state != 0)
1186 *state = llapi_layout_string_flags(string);
1191 llapi_printf(LLAPI_MSG_ERROR,
1192 "%s: mirrored file state '%s' not supported\n",
1198 * struct mirror_args - Command-line arguments for mirror(s).
1199 * @m_count: Number of mirrors to be created with this layout.
1200 * @m_flags: Mirror level flags, only 'prefer' is supported.
1201 * @m_layout: Mirror layout.
1202 * @m_file: A victim file. Its layout will be split and used as a mirror.
1203 * @m_next: Point to the next node of the list.
1205 * Command-line arguments for mirror(s) will be parsed and stored in
1206 * a linked list that consists of this structure.
1208 struct mirror_args {
1211 struct llapi_layout *m_layout;
1213 struct mirror_args *m_next;
1216 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1221 rc = llapi_layout_comp_flags_get(layout, &flags);
1225 if (flags & LCME_FL_NEG) {
1226 fprintf(stderr, "error: %s: negative flags are not supported\n",
1231 if (flags & LCME_FL_STALE) {
1232 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1233 progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1237 return LLAPI_LAYOUT_ITER_CONT;
1240 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1242 uint64_t start, end;
1246 /* LU-10112: do not support dom+flr in phase 1 */
1247 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1251 rc = llapi_layout_pattern_get(layout, &pattern);
1255 if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1256 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1261 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1265 rc = llapi_layout_comp_extent_get(layout, &start, &end);
1269 if (end != LUSTRE_EOF) {
1270 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1275 rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1280 * enum mirror_flags - Flags for extending a mirrored file.
1281 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1282 * in case the victim file(s) contains the same data as the
1283 * original mirrored file.
1284 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1285 * @MF_COMP_ID: specified component id instead of mirror id
1287 * Flags for extending a mirrored file.
1296 * mirror_create_sanity_check() - Check mirror list.
1297 * @list: A linked list that stores the mirror arguments.
1299 * This function does a sanity check on @list for creating
1302 * Return: 0 on success or a negative error code on failure.
1304 static int mirror_create_sanity_check(const char *fname,
1305 struct mirror_args *list)
1308 bool has_m_file = false;
1309 bool has_m_layout = false;
1315 struct llapi_layout *layout;
1317 layout = llapi_layout_get_by_path(fname, 0);
1320 "error: %s: file '%s' couldn't get layout\n",
1325 rc = mirror_sanity_check_one(layout);
1326 llapi_layout_free(layout);
1332 while (list != NULL) {
1333 if (list->m_file != NULL) {
1335 llapi_layout_free(list->m_layout);
1338 llapi_layout_get_by_path(list->m_file, 0);
1339 if (list->m_layout == NULL) {
1341 "error: %s: file '%s' has no layout\n",
1342 progname, list->m_file);
1346 has_m_layout = true;
1347 if (list->m_layout == NULL) {
1348 fprintf(stderr, "error: %s: no mirror layout\n",
1354 rc = mirror_sanity_check_one(list->m_layout);
1358 list = list->m_next;
1361 if (has_m_file && has_m_layout) {
1363 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1371 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1373 __u32 mirror_flags = *(__u32 *)cbdata;
1377 rc = llapi_layout_comp_flags_get(layout, &flags);
1382 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1387 return LLAPI_LAYOUT_ITER_CONT;
1391 * mirror_create() - Create a mirrored file.
1392 * @fname: The file to be created.
1393 * @mirror_list: A linked list that stores the mirror arguments.
1395 * This function creates a mirrored file @fname with the mirror(s)
1396 * from @mirror_list.
1398 * Return: 0 on success or a negative error code on failure.
1400 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1402 struct llapi_layout *layout = NULL;
1403 struct mirror_args *cur_mirror = NULL;
1404 uint16_t mirror_count = 0;
1408 rc = mirror_create_sanity_check(NULL, mirror_list);
1412 cur_mirror = mirror_list;
1413 while (cur_mirror != NULL) {
1414 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1416 &cur_mirror->m_flags);
1419 fprintf(stderr, "%s: failed to set mirror flags\n",
1424 for (i = 0; i < cur_mirror->m_count; i++) {
1425 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1428 fprintf(stderr, "error: %s: "
1429 "merge layout failed: %s\n",
1430 progname, strerror(errno));
1434 mirror_count += cur_mirror->m_count;
1435 cur_mirror = cur_mirror->m_next;
1438 if (layout == NULL) {
1439 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1443 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1446 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1447 progname, strerror(errno));
1451 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1459 llapi_layout_free(layout);
1464 * Compare files and check lease on @fd.
1466 * \retval bytes number of bytes are the same
1468 static ssize_t mirror_file_compare(int fd, int fdv)
1470 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1472 ssize_t bytes_done = 0;
1473 ssize_t bytes_read = 0;
1475 buf = malloc(buflen * 2);
1480 if (!llapi_lease_check(fd)) {
1481 bytes_done = -EBUSY;
1485 bytes_read = read(fd, buf, buflen);
1486 if (bytes_read <= 0)
1489 if (bytes_read != read(fdv, buf + buflen, buflen))
1492 /* XXX: should compute the checksum on each buffer and then
1493 * compare checksum to avoid cache collision */
1494 if (memcmp(buf, buf + buflen, bytes_read))
1497 bytes_done += bytes_read;
1505 static int mirror_extend_file(const char *fname, const char *victim_file,
1506 enum mirror_flags mirror_flags)
1511 struct stat stbuf_v;
1512 struct ll_ioc_lease *data = NULL;
1515 fd = open(fname, O_RDWR);
1517 error_loc = "open source file";
1522 fdv = open(victim_file, O_RDWR);
1524 error_loc = "open target file";
1529 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1530 error_loc = "stat source or target file";
1535 if (stbuf.st_dev != stbuf_v.st_dev) {
1536 error_loc = "stat source and target file";
1541 /* mirrors should be of the same size */
1542 if (stbuf.st_size != stbuf_v.st_size) {
1543 error_loc = "file sizes don't match";
1548 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1550 error_loc = "cannot get lease";
1554 if (!(mirror_flags & MF_NO_VERIFY)) {
1556 /* mirrors should have the same contents */
1557 ret = mirror_file_compare(fd, fdv);
1558 if (ret != stbuf.st_size) {
1559 error_loc = "file busy or contents don't match";
1560 rc = ret < 0 ? ret : -EINVAL;
1565 /* Get rid of caching pages from clients */
1566 rc = llapi_file_flush(fd);
1568 error_loc = "cannot get data version";
1572 rc = llapi_file_flush(fdv);
1574 error_loc = "cannot get data version";
1579 /* Make sure we keep original atime/mtime values */
1580 rc = migrate_copy_timestamps(fd, fdv);
1582 error_loc = "cannot copy timestamp";
1586 /* Atomically put lease, merge layouts and close. */
1587 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1589 error_loc = "memory allocation";
1592 data->lil_mode = LL_LEASE_UNLCK;
1593 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1594 data->lil_count = 1;
1595 data->lil_ids[0] = fdv;
1596 rc = llapi_lease_set(fd, data);
1598 error_loc = "cannot merge layout";
1600 } else if (rc == 0) {
1602 error_loc = "lost lease lock";
1615 (void) unlink(victim_file);
1617 fprintf(stderr, "error: %s: %s: %s: %s\n",
1618 progname, fname, error_loc, strerror(-rc));
1622 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1624 struct ll_ioc_lease *data = NULL;
1629 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1633 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1635 error_loc = "cannot get lease";
1639 rc = migrate_nonblock(fd, fdv);
1641 llapi_lease_release(fd);
1645 /* Atomically put lease, merge layouts and close. */
1646 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1648 error_loc = "memory allocation";
1651 data->lil_mode = LL_LEASE_UNLCK;
1652 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1653 data->lil_count = 1;
1654 data->lil_ids[0] = fdv;
1655 rc = llapi_lease_set(fd, data);
1657 error_loc = "cannot merge layout";
1659 } else if (rc == 0) {
1661 error_loc = "lost lease lock";
1674 fprintf(stderr, "error: %s: %s: %s: %s\n",
1675 progname, name, error_loc, strerror(-rc));
1679 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1680 enum mirror_flags mirror_flags)
1684 rc = mirror_create_sanity_check(fname, mirror_list);
1688 while (mirror_list) {
1689 if (mirror_list->m_file != NULL) {
1690 rc = mirror_extend_file(fname, mirror_list->m_file,
1693 __u32 mirror_count = mirror_list->m_count;
1695 while (mirror_count > 0) {
1696 rc = mirror_extend_layout(fname,
1697 mirror_list->m_layout);
1707 mirror_list = mirror_list->m_next;
1713 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1718 rc = llapi_layout_mirror_id_get(layout, &id);
1722 if ((__u16)id == *(__u16 *)cbdata)
1723 return LLAPI_LAYOUT_ITER_STOP;
1725 return LLAPI_LAYOUT_ITER_CONT;
1728 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1733 rc = llapi_layout_comp_id_get(layout, &id);
1737 if (id == *(__u32 *)cbdata)
1738 return LLAPI_LAYOUT_ITER_STOP;
1740 return LLAPI_LAYOUT_ITER_CONT;
1742 static int mirror_split(const char *fname, __u32 id,
1743 enum mirror_flags mflags, const char *victim_file)
1745 struct llapi_layout *layout;
1746 char parent[PATH_MAX];
1747 char victim[PATH_MAX];
1748 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1750 struct ll_ioc_lease *data;
1751 uint16_t mirror_count;
1756 /* check fname contains mirror with mirror_id/comp_id */
1757 layout = llapi_layout_get_by_path(fname, 0);
1760 "error %s: file '%s' couldn't get layout\n",
1765 rc = mirror_sanity_check_one(layout);
1769 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1772 "error %s: file '%s' couldn't get mirror count\n",
1776 if (mirror_count < 2) {
1778 "error %s: file '%s' has %d component, cannot split\n",
1779 progname, fname, mirror_count);
1783 if (mflags & MF_COMP_ID) {
1784 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1785 id = mirror_id_of(id);
1787 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1790 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1793 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1795 "error %s: file '%s' does not contain mirror with id %u\n",
1796 progname, fname, id);
1800 fd = open(fname, O_RDWR);
1803 "error %s: open file '%s' failed: %s\n",
1804 progname, fname, strerror(errno));
1808 /* get victim file directory pathname */
1809 if (strlen(fname) > sizeof(parent) - 1) {
1810 fprintf(stderr, "error %s: file name of '%s' too long\n",
1815 strncpy(parent, fname, sizeof(parent));
1816 ptr = strrchr(parent, '/');
1818 if (getcwd(parent, sizeof(parent)) == NULL) {
1819 fprintf(stderr, "error %s: getcwd failed: %s\n",
1820 progname, strerror(errno));
1830 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1832 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1837 if (victim_file == NULL) {
1838 /* use a temp file to store the splitted layout */
1839 if (mflags & MF_DESTROY) {
1840 fdv = llapi_create_volatile_idx(parent, mdt_index,
1841 O_LOV_DELAY_CREATE);
1843 snprintf(victim, sizeof(victim), "%s.mirror~%u",
1845 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1848 /* user specified victim file */
1849 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1854 "error %s: create victim file failed: %s\n",
1855 progname, strerror(errno));
1859 /* get lease lock of fname */
1860 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1863 "error %s: cannot get lease of file '%s': %d\n",
1864 progname, fname, rc);
1868 /* Atomatically put lease, split layouts and close. */
1869 data = malloc(offsetof(typeof(*data), lil_ids[2]));
1875 data->lil_mode = LL_LEASE_UNLCK;
1876 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1877 data->lil_count = 2;
1878 data->lil_ids[0] = fdv;
1879 data->lil_ids[1] = id;
1880 rc = llapi_lease_set(fd, data);
1882 if (rc == 0) /* lost lease lock */
1885 "error %s: cannot split '%s': %s\n",
1886 progname, fname, strerror(-rc));
1897 llapi_layout_free(layout);
1902 * Parse a string containing an target index list into an array of integers.
1904 * The input string contains a comma delimited list of individual
1905 * indices and ranges, for example "1,2-4,7". Add the indices into the
1906 * \a tgts array and remove duplicates.
1908 * \param[out] tgts array to store indices in
1909 * \param[in] size size of \a tgts array
1910 * \param[in] offset starting index in \a tgts
1911 * \param[in] arg string containing OST index list
1913 * \retval positive number of indices in \a tgts
1914 * \retval -EINVAL unable to parse \a arg
1916 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1920 int slots = size - offset;
1927 end_of_loop = false;
1928 while (!end_of_loop) {
1932 char *endptr = NULL;
1936 ptr = strchrnul(arg, ',');
1938 end_of_loop = *ptr == '\0';
1941 start_index = strtol(arg, &endptr, 0);
1942 if (endptr == arg) /* no data at all */
1944 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1947 end_index = start_index;
1948 if (*endptr == '-') {
1949 end_index = strtol(endptr + 1, &endptr, 0);
1950 if (*endptr != '\0')
1952 if (end_index < start_index)
1956 for (i = start_index; i <= end_index && slots > 0; i++) {
1959 /* remove duplicate */
1960 for (j = 0; j < offset; j++) {
1964 if (j == offset) { /* no duplicate */
1969 if (slots == 0 && i < end_index)
1977 if (!end_of_loop && ptr != NULL)
1980 return rc < 0 ? rc : nr;
1983 struct lfs_setstripe_args {
1984 unsigned long long lsa_comp_end;
1985 unsigned long long lsa_stripe_size;
1986 long long lsa_stripe_count;
1987 long long lsa_stripe_off;
1988 __u32 lsa_comp_flags;
1989 __u32 lsa_comp_neg_flags;
1990 unsigned long long lsa_pattern;
1991 unsigned int lsa_mirror_count;
1993 bool lsa_first_comp;
1995 char *lsa_pool_name;
1998 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2000 unsigned int mirror_count = lsa->lsa_mirror_count;
2001 bool first_comp = lsa->lsa_first_comp;
2003 memset(lsa, 0, sizeof(*lsa));
2005 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2006 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2007 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2008 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2009 lsa->lsa_pool_name = NULL;
2011 lsa->lsa_mirror_count = mirror_count;
2012 lsa->lsa_first_comp = first_comp;
2016 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2017 * @lsa: Stripe options to be initialized and inherited.
2019 * This function initializes stripe options in @lsa and inherit
2020 * stripe_size, stripe_count and OST pool_name options.
2024 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2026 unsigned long long stripe_size;
2027 long long stripe_count;
2028 char *pool_name = NULL;
2030 stripe_size = lsa->lsa_stripe_size;
2031 stripe_count = lsa->lsa_stripe_count;
2032 pool_name = lsa->lsa_pool_name;
2034 setstripe_args_init(lsa);
2036 lsa->lsa_stripe_size = stripe_size;
2037 lsa->lsa_stripe_count = stripe_count;
2038 lsa->lsa_pool_name = pool_name;
2041 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2043 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2044 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2045 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2046 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2047 lsa->lsa_pool_name != NULL ||
2048 lsa->lsa_comp_end != 0);
2052 * comp_args_to_layout() - Create or extend a composite layout.
2053 * @composite: Pointer to the composite layout.
2054 * @lsa: Stripe options for the new component.
2056 * This function creates or extends a composite layout by adding a new
2057 * component with stripe options from @lsa.
2059 * Return: 0 on success or an error code on failure.
2061 static int comp_args_to_layout(struct llapi_layout **composite,
2062 struct lfs_setstripe_args *lsa,
2065 struct llapi_layout *layout = *composite;
2066 uint64_t prev_end = 0;
2069 if (layout == NULL) {
2070 layout = llapi_layout_alloc();
2071 if (layout == NULL) {
2072 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2076 *composite = layout;
2080 /* Get current component extent, current component
2081 * must be the tail component. */
2082 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2084 fprintf(stderr, "Get comp extent failed. %s\n",
2089 if (lsa->lsa_first_comp)
2092 if (lsa->lsa_first_comp)
2093 rc = llapi_layout_add_first_comp(layout);
2095 rc = llapi_layout_comp_add(layout);
2097 fprintf(stderr, "Add component failed. %s\n",
2102 /* reset lsa_first_comp */
2103 lsa->lsa_first_comp = false;
2106 rc = llapi_layout_comp_extent_set(layout, prev_end,
2109 fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2110 prev_end, lsa->lsa_comp_end, strerror(errno));
2115 /* Data-on-MDT component setting */
2116 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2117 /* In case of Data-on-MDT patterns the only extra option
2118 * applicable is stripe size option. */
2119 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2120 fprintf(stderr, "Option 'stripe-count' can't be "
2121 "specified with Data-on-MDT component: %lld\n",
2122 lsa->lsa_stripe_count);
2125 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2126 fprintf(stderr, "Option 'stripe-size' can't be "
2127 "specified with Data-on-MDT component: %llu\n",
2128 lsa->lsa_stripe_size);
2131 if (lsa->lsa_nr_tgts != 0) {
2132 fprintf(stderr, "Option 'ost-list' can't be specified "
2133 "with Data-on-MDT component: '%i'\n",
2137 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2138 fprintf(stderr, "Option 'stripe-offset' can't be "
2139 "specified with Data-on-MDT component: %lld\n",
2140 lsa->lsa_stripe_off);
2143 if (lsa->lsa_pool_name != 0) {
2144 fprintf(stderr, "Option 'pool' can't be specified "
2145 "with Data-on-MDT component: '%s'\n",
2146 lsa->lsa_pool_name);
2150 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2152 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2153 lsa->lsa_pattern, strerror(errno));
2156 /* Data-on-MDT component has always single stripe up to end */
2157 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2160 rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2162 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2163 lsa->lsa_stripe_size, strerror(errno));
2167 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2169 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2170 lsa->lsa_stripe_count, strerror(errno));
2174 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2176 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2177 lsa->lsa_comp_flags, strerror(errno));
2181 if (lsa->lsa_pool_name != NULL) {
2182 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2184 fprintf(stderr, "Set pool name: %s failed. %s\n",
2185 lsa->lsa_pool_name, strerror(errno));
2189 rc = llapi_layout_pool_name_set(layout, "");
2191 fprintf(stderr, "Clear pool name failed: %s\n",
2197 if (lsa->lsa_nr_tgts > 0) {
2198 if (lsa->lsa_stripe_count > 0 &&
2199 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2200 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2201 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2202 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2203 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2206 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2207 rc = llapi_layout_ost_index_set(layout, i,
2212 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2213 lsa->lsa_stripe_off != -1) {
2214 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2217 fprintf(stderr, "Set ost index %d failed. %s\n",
2218 i, strerror(errno));
2225 static int build_component(struct llapi_layout **layout,
2226 struct lfs_setstripe_args *lsa, bool set_extent)
2230 rc = comp_args_to_layout(layout, lsa, set_extent);
2234 if (lsa->lsa_mirror_count > 0) {
2235 rc = llapi_layout_mirror_count_set(*layout,
2236 lsa->lsa_mirror_count);
2240 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2243 lsa->lsa_mirror_count = 0;
2249 static int build_layout_from_yaml_node(struct cYAML *node,
2250 struct llapi_layout **layout,
2251 struct lfs_setstripe_args *lsa,
2258 if (node->cy_type == CYAML_TYPE_OBJECT) {
2259 /* go deep to sub blocks */
2260 rc = build_layout_from_yaml_node(node->cy_child, layout,
2265 if (node->cy_string == NULL)
2268 string = node->cy_string;
2269 /* skip leading lmm_ if present, to simplify parsing */
2270 if (strncmp(string, "lmm_", 4) == 0)
2273 if (node->cy_type == CYAML_TYPE_STRING) {
2274 if (!strcmp(string, "lcme_extent.e_end")) {
2275 if (!strcmp(node->cy_valuestring, "EOF") ||
2276 !strcmp(node->cy_valuestring, "eof"))
2277 lsa->lsa_comp_end = LUSTRE_EOF;
2278 } else if (!strcmp(string, "pool")) {
2279 lsa->lsa_pool_name = node->cy_valuestring;
2280 } else if (!strcmp(string, "pattern")) {
2281 if (!strcmp(node->cy_valuestring, "mdt"))
2282 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2284 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2285 if (!strcmp(string, "lcm_mirror_count")) {
2286 lsa->lsa_mirror_count = node->cy_valueint;
2287 } else if (!strcmp(string, "lcme_extent.e_start")) {
2288 if (node->cy_valueint != 0 || *layout != NULL) {
2289 rc = build_component(layout, lsa, true);
2294 if (node->cy_valueint == 0)
2295 lsa->lsa_first_comp = true;
2297 /* initialize lsa */
2298 setstripe_args_init(lsa);
2299 lsa->lsa_tgts = osts;
2300 } else if (!strcmp(string, "lcme_extent.e_end")) {
2301 if (node->cy_valueint == -1)
2302 lsa->lsa_comp_end = LUSTRE_EOF;
2304 lsa->lsa_comp_end = node->cy_valueint;
2305 } else if (!strcmp(string, "stripe_count")) {
2306 lsa->lsa_stripe_count = node->cy_valueint;
2307 } else if (!strcmp(string, "stripe_size")) {
2308 lsa->lsa_stripe_size = node->cy_valueint;
2309 } else if (!strcmp(string, "stripe_offset")) {
2310 lsa->lsa_stripe_off = node->cy_valueint;
2311 } else if (!strcmp(string, "l_ost_idx")) {
2312 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2317 node = node->cy_next;
2323 static int lfs_comp_create_from_yaml(char *template,
2324 struct llapi_layout **layout,
2325 struct lfs_setstripe_args *lsa,
2328 struct cYAML *tree = NULL, *err_rc = NULL;
2331 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2333 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2334 progname, template);
2335 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2336 "can't parse", &err_rc);
2337 cYAML_print_tree2file(stderr, err_rc);
2338 cYAML_free_tree(err_rc);
2343 /* initialize lsa for plain file */
2344 setstripe_args_init(lsa);
2345 lsa->lsa_tgts = osts;
2347 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2349 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2350 progname, template);
2353 rc = build_component(layout, lsa, *layout != NULL);
2355 /* clean clean lsa */
2356 setstripe_args_init(lsa);
2360 cYAML_free_tree(tree);
2364 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2365 * end of the last component in the existing file, and adjust the
2366 * first extent start of the components to be added accordingly. */
2367 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2369 struct llapi_layout *head;
2370 uint64_t start, end, stripe_size, prev_end = 0;
2373 if (layout == NULL) {
2375 "%s setstripe: layout must be specified\n",
2381 head = llapi_layout_get_by_path(fname, 0);
2384 "%s setstripe: cannot read layout from '%s': %s\n",
2385 progname, fname, strerror(errno));
2387 } else if (errno == ENODATA) {
2388 /* file without LOVEA, this component-add will be turned
2389 * into a component-create. */
2390 llapi_layout_free(head);
2392 } else if (!llapi_layout_is_composite(head)) {
2393 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2395 llapi_layout_free(head);
2399 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2401 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2402 progname, strerror(errno));
2403 llapi_layout_free(head);
2407 llapi_layout_free(head);
2409 /* Make sure we use the first component of the layout to be added. */
2410 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2413 "%s setstripe: cannot move component cursor: %s\n",
2414 progname, strerror(errno));
2418 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2420 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2421 progname, strerror(errno));
2425 if (start > prev_end || end <= prev_end) {
2427 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2428 progname, start, end, prev_end);
2432 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2434 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2435 progname, strerror(errno));
2439 if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2440 (prev_end & (stripe_size - 1))) {
2442 "%s setstripe: stripe size %lu not aligned with %lu\n",
2443 progname, stripe_size, prev_end);
2447 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2450 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2451 progname, prev_end, end, strerror(errno));
2458 static inline bool arg_is_eof(char *arg)
2460 return !strncmp(arg, "-1", strlen("-1")) ||
2461 !strncmp(arg, "EOF", strlen("EOF")) ||
2462 !strncmp(arg, "eof", strlen("eof"));
2466 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2468 * Return: Valid mirror_args pointer on success and
2469 * NULL if memory allocation fails.
2471 static struct mirror_args *lfs_mirror_alloc(void)
2473 struct mirror_args *mirror = NULL;
2476 mirror = calloc(1, sizeof(*mirror));
2487 * lfs_mirror_free() - Free memory allocated for a mirror argument
2489 * @mirror: Previously allocated mirror argument structure by
2490 * lfs_mirror_alloc().
2492 * Free memory allocated for @mirror.
2496 static void lfs_mirror_free(struct mirror_args *mirror)
2498 if (mirror->m_layout != NULL)
2499 llapi_layout_free(mirror->m_layout);
2504 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2505 * @mirror_list: Previously allocated mirror list.
2507 * Free memory allocated for @mirror_list.
2511 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2513 struct mirror_args *next_mirror = NULL;
2515 while (mirror_list != NULL) {
2516 next_mirror = mirror_list->m_next;
2517 lfs_mirror_free(mirror_list);
2518 mirror_list = next_mirror;
2530 LFS_COMP_NO_VERIFY_OPT,
2532 LFS_MIRROR_FLAGS_OPT,
2534 LFS_MIRROR_STATE_OPT,
2536 LFS_MIRROR_INDEX_OPT,
2540 static int lfs_setstripe_internal(int argc, char **argv,
2541 enum setstripe_origin opc)
2543 struct lfs_setstripe_args lsa = { 0 };
2544 struct llapi_stripe_param *param = NULL;
2545 struct find_param migrate_mdt_param = {
2555 unsigned long long size_units = 1;
2556 bool migrate_mode = false;
2557 bool migrate_mdt_mode = false;
2558 bool migration_block = false;
2559 __u64 migration_flags = 0;
2560 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2561 int comp_del = 0, comp_set = 0;
2564 struct llapi_layout *layout = NULL;
2565 struct llapi_layout **lpp = &layout;
2566 bool mirror_mode = false;
2567 bool has_m_file = false;
2568 __u32 mirror_count = 0;
2569 enum mirror_flags mirror_flags = 0;
2570 struct mirror_args *mirror_list = NULL;
2571 struct mirror_args *new_mirror = NULL;
2572 struct mirror_args *last_mirror = NULL;
2573 __u16 mirror_id = 0;
2575 bool from_yaml = false;
2576 bool from_copy = false;
2577 char *template = NULL;
2579 struct option long_opts[] = {
2580 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
2581 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
2582 /* --block is only valid in migrate mode */
2583 { .val = 'b', .name = "block", .has_arg = no_argument },
2584 { .val = LFS_COMP_ADD_OPT,
2585 .name = "comp-add", .has_arg = no_argument },
2586 { .val = LFS_COMP_ADD_OPT,
2587 .name = "component-add", .has_arg = no_argument },
2588 { .val = LFS_COMP_DEL_OPT,
2589 .name = "comp-del", .has_arg = no_argument },
2590 { .val = LFS_COMP_DEL_OPT,
2591 .name = "component-del", .has_arg = no_argument },
2592 { .val = LFS_COMP_FLAGS_OPT,
2593 .name = "comp-flags", .has_arg = required_argument },
2594 { .val = LFS_COMP_FLAGS_OPT,
2595 .name = "component-flags",
2596 .has_arg = required_argument },
2597 { .val = LFS_COMP_SET_OPT,
2598 .name = "comp-set", .has_arg = no_argument },
2599 { .val = LFS_COMP_SET_OPT,
2600 .name = "component-set",
2601 .has_arg = no_argument},
2602 { .val = LFS_COMP_NO_VERIFY_OPT,
2603 .name = "no-verify", .has_arg = no_argument},
2604 { .val = LFS_MIRROR_FLAGS_OPT,
2605 .name = "flags", .has_arg = required_argument},
2606 { .val = LFS_MIRROR_ID_OPT,
2607 .name = "mirror-id", .has_arg = required_argument},
2608 { .val = LFS_LAYOUT_COPY,
2609 .name = "copy", .has_arg = required_argument},
2610 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
2611 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
2612 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
2613 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
2614 { .val = 'd', .name = "delete", .has_arg = no_argument},
2615 { .val = 'd', .name = "destroy", .has_arg = no_argument},
2616 /* --non-direct is only valid in migrate mode */
2617 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
2618 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
2619 { .val = 'E', .name = "component-end",
2620 .has_arg = required_argument},
2621 { .val = 'f', .name = "file", .has_arg = required_argument },
2622 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
2623 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
2624 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
2625 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
2626 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
2627 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
2628 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
2629 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
2630 { .val = 'I', .name = "component-id", .has_arg = required_argument},
2631 { .val = 'L', .name = "layout", .has_arg = required_argument },
2632 { .val = 'm', .name = "mdt", .has_arg = required_argument},
2633 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
2634 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
2635 /* --non-block is only valid in migrate mode */
2636 { .val = 'n', .name = "non-block", .has_arg = no_argument },
2637 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
2638 { .val = 'o', .name = "ost", .has_arg = required_argument },
2639 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2640 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
2641 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
2643 { .val = 'p', .name = "pool", .has_arg = required_argument },
2644 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
2645 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
2646 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2647 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
2648 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
2649 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
2650 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
2651 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2652 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
2653 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
2654 /* --verbose is only valid in migrate mode */
2655 { .val = 'v', .name = "verbose", .has_arg = no_argument},
2656 { .val = 'y', .name = "yaml", .has_arg = required_argument },
2659 setstripe_args_init(&lsa);
2661 migrate_mode = (opc == SO_MIGRATE);
2662 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2664 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2666 while ((c = getopt_long(argc, argv,
2667 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2673 case LFS_COMP_ADD_OPT:
2676 case LFS_COMP_DEL_OPT:
2679 case LFS_COMP_FLAGS_OPT:
2680 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2681 &lsa.lsa_comp_neg_flags);
2684 if (mirror_mode && lsa.lsa_comp_neg_flags) {
2685 fprintf(stderr, "%s: inverted flags are not supported\n",
2689 if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2691 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2698 case LFS_COMP_SET_OPT:
2701 case LFS_COMP_NO_VERIFY_OPT:
2702 mirror_flags |= MF_NO_VERIFY;
2704 case LFS_MIRROR_ID_OPT:
2705 mirror_id = strtoul(optarg, &end, 0);
2706 if (*end != '\0' || mirror_id == 0) {
2708 "%s %s: invalid mirror ID '%s'\n",
2709 progname, argv[0], optarg);
2713 case LFS_MIRROR_FLAGS_OPT: {
2716 if (!mirror_mode || !last_mirror) {
2717 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2722 result = comp_str2flags(optarg, &last_mirror->m_flags,
2728 fprintf(stderr, "%s: inverted flags are not supported\n",
2733 if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2735 "%s: unsupported mirror flags: %s\n",
2742 case LFS_LAYOUT_COPY:
2747 if (!migrate_mode) {
2749 "%s %s: -b|--block valid only for migrate command\n",
2753 migration_block = true;
2756 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2759 "%s %s: invalid stripe count '%s'\n",
2760 progname, argv[0], optarg);
2764 if (lsa.lsa_stripe_count == -1)
2765 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2768 /* delete the default striping pattern */
2770 if (opc == SO_MIRROR_SPLIT) {
2773 "%s %s: -d cannot used with -f\n",
2777 mirror_flags |= MF_DESTROY;
2781 if (!migrate_mode) {
2783 "%s %s: -D|--non-direct is valid "
2784 "only for migrate command\n",
2788 migration_flags |= MIGRATION_NONDIRECT;
2791 if (lsa.lsa_comp_end != 0) {
2792 result = comp_args_to_layout(lpp, &lsa, true);
2795 "%s %s: invalid layout\n",
2800 setstripe_args_init_inherit(&lsa);
2803 if (arg_is_eof(optarg)) {
2804 lsa.lsa_comp_end = LUSTRE_EOF;
2806 result = llapi_parse_size(optarg,
2811 "%s %s: invalid component end '%s'\n",
2812 progname, argv[0], optarg);
2818 if (!migrate_mode) {
2819 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2823 lsa.lsa_pattern = check_hashtype(optarg);
2824 if (lsa.lsa_pattern == 0) {
2826 "%s %s: bad stripe hash type '%s'\n",
2827 progname, argv[0], optarg);
2832 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2835 "%s %s: invalid stripe offset '%s'\n",
2836 progname, argv[0], optarg);
2839 if (lsa.lsa_stripe_off == -1)
2840 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2843 comp_id = strtoul(optarg, &end, 0);
2844 if (*end != '\0' || comp_id == 0 ||
2845 comp_id > LCME_ID_MAX) {
2847 "%s %s: invalid component ID '%s'\n",
2848 progname, argv[0], optarg);
2853 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2855 "error: %s: invalid option: %s\n",
2856 progname, argv[optopt + 1]);
2859 if (opc == SO_MIRROR_EXTEND) {
2860 if (last_mirror == NULL) {
2862 "error: %s: '-N' must exist in front of '%s'\n",
2863 progname, argv[optopt + 1]);
2866 last_mirror->m_file = optarg;
2867 last_mirror->m_count = 1;
2870 if (mirror_list == NULL)
2871 mirror_list = lfs_mirror_alloc();
2872 mirror_list->m_file = optarg;
2877 if (strcmp(argv[optind - 1], "mdt") == 0) {
2878 /* Can be only the first component */
2879 if (layout != NULL) {
2881 fprintf(stderr, "error: 'mdt' layout "
2882 "can be only the first one\n");
2885 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2887 fprintf(stderr, "error: 'mdt' layout "
2888 "size is too big\n");
2891 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2892 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2894 fprintf(stderr, "error: layout '%s' is "
2895 "unknown, supported layouts are: "
2896 "'mdt', 'raid0'\n", argv[optind]);
2901 if (!migrate_mode) {
2903 "%s %s: -m|--mdt is valid only for migrate command\n",
2907 migrate_mdt_mode = true;
2908 lsa.lsa_nr_tgts = parse_targets(tgts,
2909 sizeof(tgts) / sizeof(__u32),
2910 lsa.lsa_nr_tgts, optarg);
2911 if (lsa.lsa_nr_tgts < 0) {
2913 "%s %s: invalid MDT target(s) '%s'\n",
2914 progname, argv[0], optarg);
2918 lsa.lsa_tgts = tgts;
2919 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2920 lsa.lsa_stripe_off = tgts[0];
2923 if (!migrate_mode) {
2925 "%s %s: -n|--non-block valid only for migrate command\n",
2929 migration_flags |= MIGRATION_NONBLOCK;
2932 if (opc == SO_SETSTRIPE) {
2933 opc = SO_MIRROR_CREATE;
2937 if (optarg != NULL) {
2938 mirror_count = strtoul(optarg, &end, 0);
2939 if (*end != '\0' || mirror_count == 0) {
2941 "error: %s: bad mirror count: %s\n",
2948 new_mirror = lfs_mirror_alloc();
2949 new_mirror->m_count = mirror_count;
2951 if (mirror_list == NULL)
2952 mirror_list = new_mirror;
2954 if (last_mirror != NULL) {
2955 /* wrap up last mirror */
2956 if (lsa.lsa_comp_end == 0)
2957 lsa.lsa_comp_end = LUSTRE_EOF;
2959 result = comp_args_to_layout(lpp, &lsa, true);
2961 lfs_mirror_free(new_mirror);
2965 setstripe_args_init_inherit(&lsa);
2967 last_mirror->m_next = new_mirror;
2970 last_mirror = new_mirror;
2971 lpp = &last_mirror->m_layout;
2974 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2975 if (strcmp(argv[optind - 1], "--ost-list") == 0)
2976 fprintf(stderr, "warning: '--ost-list' is "
2977 "deprecated, use '--ost' instead\n");
2979 lsa.lsa_nr_tgts = parse_targets(tgts,
2980 sizeof(tgts) / sizeof(__u32),
2981 lsa.lsa_nr_tgts, optarg);
2982 if (lsa.lsa_nr_tgts < 0) {
2984 "%s %s: invalid OST target(s) '%s'\n",
2985 progname, argv[0], optarg);
2989 lsa.lsa_tgts = tgts;
2990 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2991 lsa.lsa_stripe_off = tgts[0];
2996 lsa.lsa_pool_name = optarg;
2998 if (strlen(lsa.lsa_pool_name) == 0 ||
2999 strncmp(lsa.lsa_pool_name, "none",
3000 LOV_MAXPOOLNAME) == 0)
3001 lsa.lsa_pool_name = NULL;
3004 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3008 "%s %s: invalid stripe size '%s'\n",
3009 progname, argv[0], optarg);
3014 if (!migrate_mode) {
3016 "%s %s: -v|--verbose valid only for migrate command\n",
3020 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3027 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3028 progname, argv[0], argv[optind - 1]);
3033 fname = argv[optind];
3035 if (optind == argc) {
3036 fprintf(stderr, "%s %s: FILE must be specified\n",
3041 if (mirror_mode && mirror_count == 0) {
3043 "error: %s: --mirror-count|-N option is required\n",
3050 if (lsa.lsa_comp_end == 0)
3051 lsa.lsa_comp_end = LUSTRE_EOF;
3054 if (lsa.lsa_comp_end != 0) {
3055 result = comp_args_to_layout(lpp, &lsa, true);
3060 if (mirror_flags & MF_NO_VERIFY) {
3061 if (opc != SO_MIRROR_EXTEND) {
3063 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3067 } else if (!has_m_file) {
3069 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3076 /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3077 * altered by user space tool, so we don't need to support the
3078 * --component-set for this moment. */
3079 if (comp_set && !comp_id) {
3080 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3085 if ((delete + comp_set + comp_del + comp_add) > 1) {
3087 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3092 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3093 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3095 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3100 if ((comp_set || comp_del) &&
3101 (setstripe_args_specified(&lsa) || layout != NULL)) {
3103 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3108 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3110 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3115 if (comp_add || comp_del) {
3118 result = lstat(fname, &st);
3119 if (result == 0 && S_ISDIR(st.st_mode)) {
3121 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3127 fprintf(stderr, "error: %s: can't use --component-add "
3128 "or --component-del for mirror operation\n",
3135 if (layout == NULL) {
3137 "%s %s: option -E must be specified with --component-add\n",
3142 result = adjust_first_extent(fname, layout);
3143 if (result == -ENODATA)
3145 else if (result != 0)
3149 if (from_yaml && from_copy) {
3151 "%s: can't specify --yaml and --copy together\n",
3156 if ((from_yaml || from_copy) &&
3157 (setstripe_args_specified(&lsa) || layout != NULL)) {
3158 fprintf(stderr, "error: %s: can't specify --yaml with "
3159 "-c, -S, -i, -o, -p or -E options.\n",
3164 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3166 "%s %s: options --non-block and --block are mutually exclusive\n",
3171 if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3174 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3179 if (migrate_mdt_mode) {
3180 struct lmv_user_md *lmu;
3182 /* initialize migrate mdt parameters */
3183 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3184 LMV_USER_MAGIC_SPECIFIC));
3187 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3188 progname, argv[0], strerror(ENOMEM));
3192 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3193 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3194 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3196 "%s %s: migrate should specify MDT index\n",
3201 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3202 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3203 lmu->lum_hash_type = lsa.lsa_pattern;
3205 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3206 if (lsa.lsa_pool_name)
3207 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3208 sizeof(lmu->lum_pool_name));
3209 if (lsa.lsa_nr_tgts > 1) {
3212 if (lsa.lsa_stripe_count > 0 &&
3213 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3214 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3216 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3217 progname, lsa.lsa_stripe_count,
3223 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3224 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3225 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3226 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3228 lmu->lum_magic = LMV_USER_MAGIC;
3231 migrate_mdt_param.fp_lmv_md = lmu;
3232 migrate_mdt_param.fp_migrate = 1;
3233 } else if (layout == NULL) {
3234 /* initialize stripe parameters */
3235 param = calloc(1, offsetof(typeof(*param),
3236 lsp_osts[lsa.lsa_nr_tgts]));
3237 if (param == NULL) {
3239 "%s %s: cannot allocate memory for parameters: %s\n",
3240 progname, argv[0], strerror(ENOMEM));
3245 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3246 param->lsp_stripe_size = lsa.lsa_stripe_size;
3247 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3248 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3249 param->lsp_stripe_count = -1;
3251 param->lsp_stripe_count = lsa.lsa_stripe_count;
3253 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3254 param->lsp_stripe_offset = -1;
3256 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3257 param->lsp_pool = lsa.lsa_pool_name;
3258 param->lsp_is_specific = false;
3259 if (lsa.lsa_nr_tgts > 0) {
3260 if (lsa.lsa_stripe_count > 0 &&
3261 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3262 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3263 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3264 fprintf(stderr, "error: %s: stripe count %lld "
3265 "doesn't match the number of OSTs: %d\n"
3266 , argv[0], lsa.lsa_stripe_count,
3272 param->lsp_is_specific = true;
3273 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3274 memcpy(param->lsp_osts, tgts,
3275 sizeof(*tgts) * lsa.lsa_nr_tgts);
3280 /* generate a layout from a YAML template */
3281 result = lfs_comp_create_from_yaml(template, &layout,
3284 fprintf(stderr, "error: %s: can't create composite "
3285 "layout from template file %s\n",
3289 } else if (from_copy) {
3290 layout = llapi_layout_get_by_path(template, 0);
3291 if (layout == NULL) {
3293 "%s: can't create composite layout from file %s.\n",
3294 progname, template);
3299 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3300 if (migrate_mdt_mode) {
3301 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3302 } else if (migrate_mode) {
3303 result = lfs_migrate(fname, migration_flags, param,
3305 } else if (comp_set != 0) {
3306 result = lfs_component_set(fname, comp_id,
3308 lsa.lsa_comp_neg_flags);
3309 } else if (comp_del != 0) {
3310 result = lfs_component_del(fname, comp_id,
3312 lsa.lsa_comp_neg_flags);
3313 } else if (comp_add != 0) {
3314 result = lfs_component_add(fname, layout);
3315 } else if (opc == SO_MIRROR_CREATE) {
3316 result = mirror_create(fname, mirror_list);
3317 } else if (opc == SO_MIRROR_EXTEND) {
3318 result = mirror_extend(fname, mirror_list,
3320 } else if (opc == SO_MIRROR_SPLIT) {
3321 if (mirror_id == 0 && comp_id == 0) {
3323 "%s %s: no mirror id or component id is specified\n",
3328 comp_id = mirror_id;
3330 mirror_flags |= MF_COMP_ID;
3331 result = mirror_split(fname, comp_id, mirror_flags,
3332 has_m_file ? mirror_list->m_file :
3334 } else if (layout != NULL) {
3335 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3342 result = llapi_file_open_param(fname,
3351 /* Save the first error encountered. */
3359 free(migrate_mdt_param.fp_lmv_md);
3360 llapi_layout_free(layout);
3361 lfs_mirror_list_free(mirror_list);
3366 llapi_layout_free(layout);
3367 lfs_mirror_list_free(mirror_list);
3371 static int lfs_poollist(int argc, char **argv)
3376 return llapi_poollist(argv[1]);
3379 static int set_time(time_t *time, time_t *set, char *str)
3387 else if (str[0] == '-')
3393 t = strtol(str, &endptr, 0);
3394 if (*endptr != '\0') {
3396 "%s find: bad time '%s': %s\n",
3397 progname, str, strerror(EINVAL));
3400 if (*time < t * 24 * 60 * 60) {
3404 "%s find: bad time '%s': too large\n",
3409 *set = *time - t * 24 * 60 * 60;
3413 static int name2uid(unsigned int *id, const char *name)
3415 struct passwd *passwd;
3417 passwd = getpwnam(name);
3420 *id = passwd->pw_uid;
3425 static int name2gid(unsigned int *id, const char *name)
3427 struct group *group;
3429 group = getgrnam(name);
3432 *id = group->gr_gid;
3437 static inline int name2projid(unsigned int *id, const char *name)
3442 static int uid2name(char **name, unsigned int id)
3444 struct passwd *passwd;
3446 passwd = getpwuid(id);
3449 *name = passwd->pw_name;
3454 static inline int gid2name(char **name, unsigned int id)
3456 struct group *group;
3458 group = getgrgid(id);
3461 *name = group->gr_name;
3466 static int name2layout(__u32 *layout, char *name)
3468 char *ptr, *layout_name;
3471 for (ptr = name; ; ptr = NULL) {
3472 layout_name = strtok(ptr, ",");
3473 if (layout_name == NULL)
3475 if (strcmp(layout_name, "released") == 0)
3476 *layout |= LOV_PATTERN_F_RELEASED;
3477 else if (strcmp(layout_name, "raid0") == 0)
3478 *layout |= LOV_PATTERN_RAID0;
3479 else if (strcmp(layout_name, "mdt") == 0)
3480 *layout |= LOV_PATTERN_MDT;
3487 static int lfs_find(int argc, char **argv)
3492 struct find_param param = {
3496 struct option long_opts[] = {
3497 { .val = 'A', .name = "atime", .has_arg = required_argument },
3498 { .val = 'b', .name = "blocks", .has_arg = required_argument },
3499 { .val = LFS_COMP_COUNT_OPT,
3500 .name = "comp-count", .has_arg = required_argument },
3501 { .val = LFS_COMP_COUNT_OPT,
3502 .name = "component-count",
3503 .has_arg = required_argument },
3504 { .val = LFS_COMP_FLAGS_OPT,
3505 .name = "comp-flags", .has_arg = required_argument },
3506 { .val = LFS_COMP_FLAGS_OPT,
3507 .name = "component-flags",
3508 .has_arg = required_argument },
3509 { .val = LFS_COMP_START_OPT,
3510 .name = "comp-start", .has_arg = required_argument },
3511 { .val = LFS_COMP_START_OPT,
3512 .name = "component-start",
3513 .has_arg = required_argument },
3514 { .val = LFS_MIRROR_STATE_OPT,
3515 .name = "mirror-state", .has_arg = required_argument },
3516 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
3517 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
3518 { .val = 'C', .name = "ctime", .has_arg = required_argument },
3519 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3520 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
3521 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
3522 { .val = 'E', .name = "component-end",
3523 .has_arg = required_argument },
3524 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3525 { .val = 'g', .name = "gid", .has_arg = required_argument },
3526 { .val = 'G', .name = "group", .has_arg = required_argument },
3527 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
3528 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
3529 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
3530 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
3531 { .val = 'L', .name = "layout", .has_arg = required_argument },
3532 { .val = 'm', .name = "mdt", .has_arg = required_argument },
3533 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
3534 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
3535 { .val = 'M', .name = "mtime", .has_arg = required_argument },
3536 { .val = 'n', .name = "name", .has_arg = required_argument },
3537 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
3538 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
3539 { .val = 'O', .name = "obd", .has_arg = required_argument },
3540 { .val = 'O', .name = "ost", .has_arg = required_argument },
3541 /* no short option for pool yet, can be 'p' after 2.18 */
3542 { .val = LFS_POOL_OPT,
3543 .name = "pool", .has_arg = required_argument },
3544 { .val = '0', .name = "print0", .has_arg = no_argument },
3545 { .val = 'P', .name = "print", .has_arg = no_argument },
3546 { .val = LFS_PROJID_OPT,
3547 .name = "projid", .has_arg = required_argument },
3548 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3549 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3550 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3551 { .val = 's', .name = "size", .has_arg = required_argument },
3552 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3553 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3554 { .val = 't', .name = "type", .has_arg = required_argument },
3555 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
3556 { .val = 'u', .name = "uid", .has_arg = required_argument },
3557 { .val = 'U', .name = "user", .has_arg = required_argument },
3558 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
3559 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
3571 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3572 while ((c = getopt_long_only(argc, argv,
3573 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3574 long_opts, NULL)) >= 0) {
3579 /* '!' is part of option */
3580 /* when getopt_long_only() finds a string which is not
3581 * an option nor a known option argument it returns 1
3582 * in that case if we already have found pathstart and pathend
3583 * (i.e. we have the list of pathnames),
3584 * the only supported value is "!"
3586 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3587 if (!isoption && pathend != -1) {
3588 fprintf(stderr, "err: %s: filename|dirname must either "
3589 "precede options or follow options\n",
3594 if (!isoption && pathstart == -1)
3595 pathstart = optind - 1;
3596 if (isoption && pathstart != -1 && pathend == -1)
3597 pathend = optind - 2;
3603 /* unknown; opt is "!" or path component,
3604 * checking done above.
3606 if (strcmp(optarg, "!") == 0)
3610 xtime = ¶m.fp_atime;
3611 xsign = ¶m.fp_asign;
3612 param.fp_exclude_atime = !!neg_opt;
3613 /* no break, this falls through to 'C' for ctime */
3616 xtime = ¶m.fp_ctime;
3617 xsign = ¶m.fp_csign;
3618 param.fp_exclude_ctime = !!neg_opt;
3620 /* no break, this falls through to 'M' for mtime */
3623 xtime = ¶m.fp_mtime;
3624 xsign = ¶m.fp_msign;
3625 param.fp_exclude_mtime = !!neg_opt;
3627 rc = set_time(&t, xtime, optarg);
3628 if (rc == INT_MAX) {
3636 if (optarg[0] == '+') {
3637 param.fp_blocks_sign = -1;
3639 } else if (optarg[0] == '-') {
3640 param.fp_blocks_sign = 1;
3644 param.fp_blocks_units = 1024;
3645 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
3646 ¶m.fp_blocks_units, 0);
3648 fprintf(stderr, "error: bad blocks '%s'\n",
3652 param.fp_check_blocks = 1;
3653 param.fp_exclude_blocks = !!neg_opt;
3655 case LFS_COMP_COUNT_OPT:
3656 if (optarg[0] == '+') {
3657 param.fp_comp_count_sign = -1;
3659 } else if (optarg[0] == '-') {
3660 param.fp_comp_count_sign = 1;
3664 param.fp_comp_count = strtoul(optarg, &endptr, 0);
3665 if (*endptr != '\0') {
3666 fprintf(stderr, "error: bad component count "
3670 param.fp_check_comp_count = 1;
3671 param.fp_exclude_comp_count = !!neg_opt;
3673 case LFS_COMP_FLAGS_OPT:
3674 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
3675 ¶m.fp_comp_neg_flags);
3677 fprintf(stderr, "error: bad component flags "
3681 param.fp_check_comp_flags = 1;
3683 __u32 flags = param.fp_comp_neg_flags;
3684 param.fp_comp_neg_flags = param.fp_comp_flags;
3685 param.fp_comp_flags = flags;
3688 case LFS_COMP_START_OPT:
3689 if (optarg[0] == '+') {
3690 param.fp_comp_start_sign = -1;
3692 } else if (optarg[0] == '-') {
3693 param.fp_comp_start_sign = 1;
3697 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
3698 ¶m.fp_comp_start_units, 0);
3700 fprintf(stderr, "error: bad component start "
3704 param.fp_check_comp_start = 1;
3705 param.fp_exclude_comp_start = !!neg_opt;
3707 case LFS_MIRROR_STATE_OPT:
3708 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
3709 ¶m.fp_mirror_neg_state);
3712 "error: bad mirrored file state '%s'\n",
3716 param.fp_check_mirror_state = 1;
3718 __u16 state = param.fp_mirror_neg_state;
3719 param.fp_mirror_neg_state =
3720 param.fp_mirror_state;
3721 param.fp_mirror_state = state;
3725 if (optarg[0] == '+') {
3726 param.fp_stripe_count_sign = -1;
3728 } else if (optarg[0] == '-') {
3729 param.fp_stripe_count_sign = 1;
3733 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3734 if (*endptr != '\0') {
3735 fprintf(stderr,"error: bad stripe_count '%s'\n",
3740 param.fp_check_stripe_count = 1;
3741 param.fp_exclude_stripe_count = !!neg_opt;
3744 param.fp_max_depth = strtol(optarg, 0, 0);
3747 if (optarg[0] == '+') {
3748 param.fp_comp_end_sign = -1;
3750 } else if (optarg[0] == '-') {
3751 param.fp_comp_end_sign = 1;
3755 if (arg_is_eof(optarg)) {
3756 param.fp_comp_end = LUSTRE_EOF;
3757 param.fp_comp_end_units = 1;
3760 rc = llapi_parse_size(optarg,
3762 ¶m.fp_comp_end_units, 0);
3765 fprintf(stderr, "error: bad component end "
3769 param.fp_check_comp_end = 1;
3770 param.fp_exclude_comp_end = !!neg_opt;
3774 rc = name2gid(¶m.fp_gid, optarg);
3776 param.fp_gid = strtoul(optarg, &endptr, 10);
3777 if (*endptr != '\0') {
3778 fprintf(stderr, "Group/GID: %s cannot "
3779 "be found.\n", optarg);
3784 param.fp_exclude_gid = !!neg_opt;
3785 param.fp_check_gid = 1;
3788 param.fp_hash_type = check_hashtype(optarg);
3789 if (param.fp_hash_type == 0) {
3790 fprintf(stderr, "error: bad hash_type '%s'\n",
3795 param.fp_check_hash_type = 1;
3796 param.fp_exclude_hash_type = !!neg_opt;
3799 ret = name2layout(¶m.fp_layout, optarg);
3802 param.fp_exclude_layout = !!neg_opt;
3803 param.fp_check_layout = 1;
3807 rc = name2uid(¶m.fp_uid, optarg);
3809 param.fp_uid = strtoul(optarg, &endptr, 10);
3810 if (*endptr != '\0') {
3811 fprintf(stderr, "User/UID: %s cannot "
3812 "be found.\n", optarg);
3817 param.fp_exclude_uid = !!neg_opt;
3818 param.fp_check_uid = 1;
3821 param.fp_pattern = (char *)optarg;
3822 param.fp_exclude_pattern = !!neg_opt;
3825 if (optarg[0] == '+') {
3826 param.fp_mirror_count_sign = -1;
3828 } else if (optarg[0] == '-') {
3829 param.fp_mirror_count_sign = 1;
3833 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3834 if (*endptr != '\0') {
3836 "error: bad mirror count '%s'\n",
3840 param.fp_check_mirror_count = 1;
3841 param.fp_exclude_mirror_count = !!neg_opt;
3846 char *buf, *token, *next, *p;
3850 buf = strdup(optarg);
3856 param.fp_exclude_obd = !!neg_opt;
3859 while (token && *token) {
3860 token = strchr(token, ',');
3867 param.fp_exclude_mdt = !!neg_opt;
3868 param.fp_num_alloc_mdts += len;
3869 tmp = realloc(param.fp_mdt_uuid,
3870 param.fp_num_alloc_mdts *
3871 sizeof(*param.fp_mdt_uuid));
3877 param.fp_mdt_uuid = tmp;
3879 param.fp_exclude_obd = !!neg_opt;
3880 param.fp_num_alloc_obds += len;
3881 tmp = realloc(param.fp_obd_uuid,
3882 param.fp_num_alloc_obds *
3883 sizeof(*param.fp_obd_uuid));
3889 param.fp_obd_uuid = tmp;
3891 for (token = buf; token && *token; token = next) {
3892 struct obd_uuid *puuid;
3895 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
3898 ¶m.fp_obd_uuid[param.fp_num_obds++];
3900 p = strchr(token, ',');
3907 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3912 strncpy(puuid->uuid, token,
3913 sizeof(puuid->uuid));
3920 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3924 if (strlen(optarg) > LOV_MAXPOOLNAME) {
3926 "Pool name %s is too long (max %d)\n",
3927 optarg, LOV_MAXPOOLNAME);
3932 * We do check for empty pool because empty pool
3933 * is used to find V1 LOV attributes
3935 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3936 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3937 param.fp_exclude_pool = !!neg_opt;
3938 param.fp_check_pool = 1;
3940 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3941 case 'p': /* want this for --pool, to match getstripe/find */
3943 "warning: -p deprecated, use --print0 or -0\n");
3946 param.fp_zero_end = 1;
3948 case 'P': /* we always print, this option is a no-op */
3950 case LFS_PROJID_OPT:
3951 rc = name2projid(¶m.fp_projid, optarg);
3953 param.fp_projid = strtoul(optarg, &endptr, 10);
3954 if (*endptr != '\0') {
3956 "Invalid project ID: %s",
3962 param.fp_exclude_projid = !!neg_opt;
3963 param.fp_check_projid = 1;
3966 if (optarg[0] == '+') {
3967 param.fp_size_sign = -1;
3969 } else if (optarg[0] == '-') {
3970 param.fp_size_sign = 1;
3974 ret = llapi_parse_size(optarg, ¶m.fp_size,
3975 ¶m.fp_size_units, 0);
3977 fprintf(stderr, "error: bad file size '%s'\n",
3981 param.fp_check_size = 1;
3982 param.fp_exclude_size = !!neg_opt;
3985 if (optarg[0] == '+') {
3986 param.fp_stripe_size_sign = -1;
3988 } else if (optarg[0] == '-') {
3989 param.fp_stripe_size_sign = 1;
3993 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
3994 ¶m.fp_stripe_size_units, 0);
3996 fprintf(stderr, "error: bad stripe_size '%s'\n",
4000 param.fp_check_stripe_size = 1;
4001 param.fp_exclude_stripe_size = !!neg_opt;
4004 param.fp_exclude_type = !!neg_opt;
4005 switch (optarg[0]) {
4007 param.fp_type = S_IFBLK;
4010 param.fp_type = S_IFCHR;
4013 param.fp_type = S_IFDIR;
4016 param.fp_type = S_IFREG;
4019 param.fp_type = S_IFLNK;
4022 param.fp_type = S_IFIFO;
4025 param.fp_type = S_IFSOCK;
4028 fprintf(stderr, "error: %s: bad type '%s'\n",
4035 if (optarg[0] == '+') {
4036 param.fp_mdt_count_sign = -1;
4038 } else if (optarg[0] == '-') {
4039 param.fp_mdt_count_sign = 1;
4043 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4044 if (*endptr != '\0') {
4045 fprintf(stderr, "error: bad mdt_count '%s'\n",
4050 param.fp_check_mdt_count = 1;
4051 param.fp_exclude_mdt_count = !!neg_opt;
4059 if (pathstart == -1) {
4060 fprintf(stderr, "error: %s: no filename|pathname\n",
4064 } else if (pathend == -1) {
4070 rc = llapi_find(argv[pathstart], ¶m);
4071 if (rc != 0 && ret == 0)
4073 } while (++pathstart < pathend);
4076 fprintf(stderr, "error: %s failed for %s.\n",
4077 argv[0], argv[optind - 1]);
4079 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4080 free(param.fp_obd_uuid);
4082 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4083 free(param.fp_mdt_uuid);
4088 static int lfs_getstripe_internal(int argc, char **argv,
4089 struct find_param *param)
4091 struct option long_opts[] = {
4092 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4093 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4094 { .val = LFS_COMP_COUNT_OPT,
4095 .name = "comp-count", .has_arg = no_argument },
4096 { .val = LFS_COMP_COUNT_OPT,
4097 .name = "component-count", .has_arg = no_argument },
4098 { .val = LFS_COMP_FLAGS_OPT,
4099 .name = "comp-flags", .has_arg = optional_argument },
4100 { .val = LFS_COMP_FLAGS_OPT,
4101 .name = "component-flags", .has_arg = optional_argument },
4102 { .val = LFS_COMP_START_OPT,
4103 .name = "comp-start", .has_arg = optional_argument },
4104 { .val = LFS_COMP_START_OPT,
4105 .name = "component-start", .has_arg = optional_argument },
4106 { .val = LFS_MIRROR_INDEX_OPT,
4107 .name = "mirror-index", .has_arg = required_argument },
4108 { .val = LFS_MIRROR_ID_OPT,
4109 .name = "mirror-id", .has_arg = required_argument },
4110 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4111 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4112 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4113 { .val = 'd', .name = "directory", .has_arg = no_argument },
4114 { .val = 'D', .name = "default", .has_arg = no_argument },
4115 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4116 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4117 { .val = 'F', .name = "fid", .has_arg = no_argument },
4118 { .val = 'g', .name = "generation", .has_arg = no_argument },
4119 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4120 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4121 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4122 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4123 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4124 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4125 { .val = 'L', .name = "layout", .has_arg = no_argument },
4126 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4127 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4128 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4129 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4130 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4131 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4132 { .val = 'O', .name = "obd", .has_arg = required_argument },
4133 { .val = 'O', .name = "ost", .has_arg = required_argument },
4134 { .val = 'p', .name = "pool", .has_arg = no_argument },
4135 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4136 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4137 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4138 { .val = 'R', .name = "raw", .has_arg = no_argument },
4139 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4140 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4141 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4142 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4143 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4144 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4145 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4146 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4150 int pathstart = -1, pathend = -1;
4154 while ((c = getopt_long(argc, argv,
4155 "-cdDE::FghiI::LmMNoO:pqrRsSvy",
4156 long_opts, NULL)) != -1) {
4160 /* '!' is part of option */
4161 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4162 if (!isoption && pathend != -1) {
4164 "error: %s: filename|dirname must either precede options or follow options\n",
4168 if (!isoption && pathstart == -1)
4169 pathstart = optind - 1;
4170 if (isoption && pathstart != -1 && pathend == -1)
4171 pathend = optind - 2;
4175 /* unknown: opt is "!" */
4176 if (strcmp(optarg, "!") == 0)
4180 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4181 param->fp_verbose |= VERBOSE_COUNT;
4182 param->fp_max_depth = 0;
4185 case LFS_COMP_COUNT_OPT:
4186 param->fp_verbose |= VERBOSE_COMP_COUNT;
4187 param->fp_max_depth = 0;
4189 case LFS_COMP_FLAGS_OPT:
4190 if (optarg != NULL) {
4191 rc = comp_str2flags(optarg,
4192 ¶m->fp_comp_flags,
4193 ¶m->fp_comp_neg_flags);
4195 fprintf(stderr, "error: %s bad "
4196 "component flags '%s'.\n",
4200 param->fp_check_comp_flags = 1;
4202 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4203 param->fp_max_depth = 0;
4206 case LFS_COMP_START_OPT:
4207 if (optarg != NULL) {
4209 if (tmp[0] == '+') {
4210 param->fp_comp_start_sign = -1;
4212 } else if (tmp[0] == '-') {
4213 param->fp_comp_start_sign = 1;
4216 rc = llapi_parse_size(tmp,
4217 ¶m->fp_comp_start,
4218 ¶m->fp_comp_start_units, 0);
4220 fprintf(stderr, "error: %s bad "
4221 "component start '%s'.\n",
4225 param->fp_check_comp_start = 1;
4228 param->fp_verbose |= VERBOSE_COMP_START;
4229 param->fp_max_depth = 0;
4232 case LFS_MIRROR_INDEX_OPT:
4233 if (optarg[0] == '+') {
4234 param->fp_mirror_index_sign = -1;
4236 } else if (optarg[0] == '-') {
4237 param->fp_mirror_index_sign = 1;
4241 param->fp_mirror_index = strtoul(optarg, &end, 0);
4242 if (*end != '\0' || (param->fp_mirror_index == 0 &&
4243 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4245 "%s %s: invalid mirror index '%s'\n",
4246 progname, argv[0], optarg);
4249 if (param->fp_mirror_id != 0) {
4251 "%s %s: can't specify both mirror index and mirror ID\n",
4255 param->fp_check_mirror_index = 1;
4256 param->fp_exclude_mirror_index = !!neg_opt;
4258 case LFS_MIRROR_ID_OPT:
4259 if (optarg[0] == '+') {
4260 param->fp_mirror_id_sign = -1;
4262 } else if (optarg[0] == '-') {
4263 param->fp_mirror_id_sign = 1;
4267 param->fp_mirror_id = strtoul(optarg, &end, 0);
4268 if (*end != '\0' || (param->fp_mirror_id == 0 &&
4269 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4271 "%s %s: invalid mirror ID '%s'\n",
4272 progname, argv[0], optarg);
4275 if (param->fp_mirror_index != 0) {
4277 "%s %s: can't specify both mirror index and mirror ID\n",
4281 param->fp_check_mirror_id = 1;
4282 param->fp_exclude_mirror_id = !!neg_opt;
4285 param->fp_max_depth = 0;
4288 param->fp_get_default_lmv = 1;
4291 if (optarg != NULL) {
4293 if (tmp[0] == '+') {
4294 param->fp_comp_end_sign = -1;
4296 } else if (tmp[0] == '-') {
4297 param->fp_comp_end_sign = 1;
4301 if (arg_is_eof(tmp)) {
4302 param->fp_comp_end = LUSTRE_EOF;
4303 param->fp_comp_end_units = 1;
4306 rc = llapi_parse_size(tmp,
4307 ¶m->fp_comp_end,
4308 ¶m->fp_comp_end_units, 0);
4311 fprintf(stderr, "error: %s bad "
4312 "component end '%s'.\n",
4316 param->fp_check_comp_end = 1;
4318 param->fp_verbose |= VERBOSE_COMP_END;
4319 param->fp_max_depth = 0;
4323 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4324 param->fp_verbose |= VERBOSE_DFID;
4325 param->fp_max_depth = 0;
4329 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4330 param->fp_verbose |= VERBOSE_GENERATION;
4331 param->fp_max_depth = 0;
4335 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4336 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
4337 param->fp_max_depth = 0;
4341 if (optarg != NULL) {
4342 param->fp_comp_id = strtoul(optarg, &end, 0);
4343 if (*end != '\0' || param->fp_comp_id == 0 ||
4344 param->fp_comp_id > LCME_ID_MAX) {
4345 fprintf(stderr, "error: %s bad "
4346 "component id '%s'\n",
4350 param->fp_check_comp_id = 1;
4353 param->fp_max_depth = 0;
4354 param->fp_verbose |= VERBOSE_COMP_ID;
4358 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4359 param->fp_verbose |= VERBOSE_PATTERN;
4360 param->fp_max_depth = 0;
4363 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4365 fprintf(stderr, "warning: '-M' deprecated"
4366 ", use '--mdt-index' or '-m' instead\n");
4369 if (!(param->fp_verbose & VERBOSE_DETAIL))
4370 param->fp_max_depth = 0;
4371 param->fp_verbose |= VERBOSE_MDTINDEX;
4374 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4375 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
4376 param->fp_max_depth = 0;
4380 if (param->fp_obd_uuid) {
4382 "error: %s: only one obduuid allowed",
4386 param->fp_obd_uuid = (struct obd_uuid *)optarg;
4389 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4390 param->fp_verbose |= VERBOSE_POOL;
4391 param->fp_max_depth = 0;
4398 param->fp_recursive = 1;
4404 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4405 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
4406 param->fp_max_depth = 0;
4410 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4420 if (pathstart == -1) {
4421 fprintf(stderr, "error: %s: no filename|pathname\n",
4424 } else if (pathend == -1) {
4432 if (param->fp_recursive)
4433 param->fp_max_depth = -1;
4434 else if (param->fp_verbose & VERBOSE_DETAIL)
4435 param->fp_max_depth = 1;
4437 if (!param->fp_verbose)
4438 param->fp_verbose = VERBOSE_DEFAULT;
4439 if (param->fp_quiet)
4440 param->fp_verbose = VERBOSE_OBJID;
4443 rc = llapi_getstripe(argv[pathstart], param);
4444 } while (++pathstart < pathend && !rc);
4447 fprintf(stderr, "error: %s failed for %s.\n",
4448 argv[0], argv[optind - 1]);
4452 static int lfs_tgts(int argc, char **argv)
4454 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4455 struct find_param param;
4456 int index = 0, rc=0;
4461 if (argc == 2 && !realpath(argv[1], path)) {
4463 fprintf(stderr, "error: invalid path '%s': %s\n",
4464 argv[1], strerror(-rc));
4468 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4469 /* Check if we have a mount point */
4470 if (mntdir[0] == '\0')
4473 memset(¶m, 0, sizeof(param));
4474 if (!strcmp(argv[0], "mdts"))
4475 param.fp_get_lmv = 1;
4477 rc = llapi_ostlist(mntdir, ¶m);
4479 fprintf(stderr, "error: %s: failed on %s\n",
4482 if (path[0] != '\0')
4484 memset(mntdir, 0, PATH_MAX);
4490 static int lfs_getstripe(int argc, char **argv)
4492 struct find_param param = { 0 };
4494 param.fp_max_depth = 1;
4495 return lfs_getstripe_internal(argc, argv, ¶m);
4499 static int lfs_getdirstripe(int argc, char **argv)
4501 struct find_param param = { 0 };
4502 struct option long_opts[] = {
4503 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
4504 { .val = 'D', .name = "default", .has_arg = no_argument },
4505 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
4506 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
4507 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4508 { .val = 'O', .name = "obd", .has_arg = required_argument },
4509 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4510 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
4511 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4515 param.fp_get_lmv = 1;
4517 while ((c = getopt_long(argc, argv,
4518 "cDHimO:rtTy", long_opts, NULL)) != -1)
4523 param.fp_verbose |= VERBOSE_COUNT;
4526 param.fp_get_default_lmv = 1;
4530 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
4532 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4534 fprintf(stderr, "warning: '-t' deprecated, "
4535 "use '--mdt-hash' or '-H' instead\n");
4538 param.fp_verbose |= VERBOSE_HASH_TYPE;
4541 if (param.fp_obd_uuid) {
4543 "error: %s: only one obduuid allowed",
4547 param.fp_obd_uuid = (struct obd_uuid *)optarg;
4550 param.fp_recursive = 1;
4563 if (param.fp_recursive)
4564 param.fp_max_depth = -1;
4566 if (!param.fp_verbose)
4567 param.fp_verbose = VERBOSE_DEFAULT;
4570 rc = llapi_getstripe(argv[optind], ¶m);
4571 } while (++optind < argc && !rc);
4574 fprintf(stderr, "error: %s failed for %s.\n",
4575 argv[0], argv[optind - 1]);
4580 MNTDF_INODES = 0x0001,
4581 MNTDF_COOKED = 0x0002,
4582 MNTDF_LAZY = 0x0004,
4583 MNTDF_VERBOSE = 0x0008,
4584 MNTDF_SHOW = 0x0010,
4587 #define COOK(value) \
4590 while (value > 1024) { \
4598 #define CDF "%11llu"
4599 #define HDF "%8.1f%c"
4603 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4605 double avail, used, ratio = 0;
4607 avail = st->os_bavail;
4608 used = st->os_blocks - st->os_bfree;
4609 if (avail + used > 0)
4610 ratio = used / (used + avail) * 100 + 0.5;
4615 static int showdf(char *mntdir, struct obd_statfs *stat,
4616 char *uuid, enum mntdf_flags flags,
4617 char *type, int index, int rc)
4619 long long avail, used, total;
4621 char *suffix = "KMGTPEZY";
4622 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4623 char tbuf[3 * sizeof(__u64)];
4624 char ubuf[3 * sizeof(__u64)];
4625 char abuf[3 * sizeof(__u64)];
4626 char rbuf[3 * sizeof(__u64)];
4633 if (flags & MNTDF_INODES) {
4634 avail = stat->os_ffree;
4635 used = stat->os_files - stat->os_ffree;
4636 total = stat->os_files;
4638 int shift = flags & MNTDF_COOKED ? 0 : 10;
4640 avail = (stat->os_bavail * stat->os_bsize) >> shift;
4641 used = ((stat->os_blocks - stat->os_bfree) *
4642 stat->os_bsize) >> shift;
4643 total = (stat->os_blocks * stat->os_bsize) >> shift;
4646 ratio = obd_statfs_ratio(stat);
4648 if (flags & MNTDF_COOKED) {
4652 cook_val = (double)total;
4655 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4658 snprintf(tbuf, sizeof(tbuf), CDF, total);
4660 cook_val = (double)used;
4663 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4666 snprintf(ubuf, sizeof(ubuf), CDF, used);
4668 cook_val = (double)avail;
4671 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4674 snprintf(abuf, sizeof(abuf), CDF, avail);
4676 snprintf(tbuf, sizeof(tbuf), CDF, total);
4677 snprintf(ubuf, sizeof(tbuf), CDF, used);
4678 snprintf(abuf, sizeof(tbuf), CDF, avail);
4681 sprintf(rbuf, RDF, ratio);
4682 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4683 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4685 printf("[%s:%d]", type, index);
4687 if (stat->os_state) {
4689 * Each character represents the matching
4692 const char state_names[] = "DRSI";
4697 for (i = 0, state = stat->os_state;
4698 state && i < sizeof(state_names); i++) {
4699 if (!(state & (1 << i)))
4701 printf("%c", state_names[i]);
4709 printf(UUF": inactive device\n", uuid);
4712 printf(UUF": %s\n", uuid, strerror(-rc));
4719 struct ll_stat_type {
4724 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
4726 struct ll_statfs_data {
4728 struct obd_statfs sd_st;
4731 struct ll_statfs_buf {
4733 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
4736 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4737 int ops, struct ll_statfs_buf *lsb)
4739 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4740 struct obd_uuid uuid_buf;
4741 char *poolname = NULL;
4742 struct ll_stat_type types[] = {
4743 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
4744 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
4745 { .st_name = NULL } };
4746 struct ll_stat_type *tp;
4747 __u64 ost_ffree = 0;
4755 poolname = strchr(pool, '.');
4756 if (poolname != NULL) {
4757 if (strncmp(fsname, pool, strlen(fsname))) {
4758 fprintf(stderr, "filesystem name incorrect\n");
4766 fd = open(mntdir, O_RDONLY);
4769 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4774 if (flags & MNTDF_SHOW) {
4775 if (flags & MNTDF_INODES)
4776 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4777 "UUID", "Inodes", "IUsed", "IFree",
4778 "IUse%", "Mounted on");
4780 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4782 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4783 "Used", "Available", "Use%", "Mounted on");
4786 for (tp = types; tp->st_name != NULL; tp++) {
4787 if (!(tp->st_op & ops))
4790 for (index = 0; ; index++) {
4791 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4792 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4793 type = flags & MNTDF_LAZY ?
4794 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4795 rc2 = llapi_obd_fstatfs(fd, type, index,
4796 &stat_buf, &uuid_buf);
4801 if (rc2 == -ENODATA) { /* Inactive device, OK. */
4802 if (!(flags & MNTDF_VERBOSE))
4804 } else if (rc2 < 0 && rc == 0) {
4808 if (poolname && tp->st_op == LL_STATFS_LOV &&
4809 llapi_search_ost(fsname, poolname,
4810 obd_uuid2str(&uuid_buf)) != 1)
4813 /* the llapi_obd_statfs() call may have returned with
4814 * an error, but if it filled in uuid_buf we will at
4815 * lease use that to print out a message for that OBD.
4816 * If we didn't get anything in the uuid_buf, then fill
4817 * it in so that we can print an error message. */
4818 if (uuid_buf.uuid[0] == '\0')
4819 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4820 "%s%04x", tp->st_name, index);
4822 lsb->sb_buf[lsb->sb_count].sd_index = index;
4823 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4826 if (flags & MNTDF_SHOW)
4827 showdf(mntdir, &stat_buf,
4828 obd_uuid2str(&uuid_buf), flags,
4829 tp->st_name, index, rc2);
4832 if (tp->st_op == LL_STATFS_LMV) {
4833 sum.os_ffree += stat_buf.os_ffree;
4834 sum.os_files += stat_buf.os_files;
4835 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4836 sum.os_blocks += stat_buf.os_blocks *
4838 sum.os_bfree += stat_buf.os_bfree *
4840 sum.os_bavail += stat_buf.os_bavail *
4842 ost_ffree += stat_buf.os_ffree;
4850 /* If we don't have as many objects free on the OST as inodes
4851 * on the MDS, we reduce the total number of inodes to
4852 * compensate, so that the "inodes in use" number is correct.
4853 * Matches ll_statfs_internal() so the results are consistent. */
4854 if (ost_ffree < sum.os_ffree) {
4855 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4856 sum.os_ffree = ost_ffree;
4858 if (flags & MNTDF_SHOW) {
4860 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4867 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4869 const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4871 const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4873 int r1 = obd_statfs_ratio(st1);
4874 int r2 = obd_statfs_ratio(st2);
4875 int64_t result = r1 - r2;
4877 /* if both space usage are above 90, compare free inodes */
4878 if (r1 > 90 && r2 > 90)
4879 result = st2->os_ffree - st1->os_ffree;
4883 else if (result == 0)
4890 static int lfs_setdirstripe(int argc, char **argv)
4894 struct lfs_setstripe_args lsa = { 0 };
4895 struct llapi_stripe_param *param = NULL;
4896 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4899 char *mode_opt = NULL;
4900 bool default_stripe = false;
4901 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
4902 mode_t previous_mode = 0;
4903 bool delete = false;
4904 struct ll_statfs_buf *lsb = NULL;
4905 char mntdir[PATH_MAX] = "";
4906 bool auto_distributed = false;
4908 struct option long_opts[] = {
4909 { .val = 'c', .name = "count", .has_arg = required_argument },
4910 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
4911 { .val = 'd', .name = "delete", .has_arg = no_argument },
4912 { .val = 'D', .name = "default", .has_arg = no_argument },
4913 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
4914 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4915 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
4916 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4917 { .val = 'i', .name = "index", .has_arg = required_argument },
4919 { .val = 'o', .name = "mode", .has_arg = required_argument },
4920 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4921 { .val = 't', .name = "hash-type", .has_arg = required_argument },
4923 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4924 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4927 setstripe_args_init(&lsa);
4929 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4937 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4940 "%s %s: invalid stripe count '%s'\n",
4941 progname, argv[0], optarg);
4947 default_stripe = true;
4950 default_stripe = true;
4952 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4954 fprintf(stderr, "warning: '--hash-type' and '-t' "
4955 "deprecated, use '--mdt-hash' or '-H' instead\n");
4958 lsa.lsa_pattern = check_hashtype(optarg);
4959 if (lsa.lsa_pattern == 0) {
4961 "%s %s: bad stripe hash type '%s'\n",
4962 progname, argv[0], optarg);
4967 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4968 if (strcmp(argv[optind - 1], "--index") == 0)
4970 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4973 lsa.lsa_nr_tgts = parse_targets(mdts,
4974 sizeof(mdts) / sizeof(__u32),
4975 lsa.lsa_nr_tgts, optarg);
4976 if (lsa.lsa_nr_tgts < 0) {
4978 "%s %s: invalid MDT target(s) '%s'\n",
4979 progname, argv[0], optarg);
4983 lsa.lsa_tgts = mdts;
4984 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4985 lsa.lsa_stripe_off = mdts[0];
4987 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4989 fprintf(stderr, "warning: '-m' is deprecated, "
4990 "use '--mode' or '-o' instead\n");
4996 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4997 progname, argv[0], argv[optind - 1]);
5002 if (optind == argc) {
5003 fprintf(stderr, "%s %s: DIR must be specified\n",
5008 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5009 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5011 "%s %s: stripe offset and count must be specified\n",
5017 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5018 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5020 "%s %s: cannot specify -d with -c or -i options\n",
5025 if (mode_opt != NULL) {
5026 mode = strtoul(mode_opt, &end, 8);
5029 "%s %s: bad MODE '%s'\n",
5030 progname, argv[0], mode_opt);
5033 previous_mode = umask(0);
5037 * initialize stripe parameters, in case param is converted to specific,
5038 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5040 param = calloc(1, offsetof(typeof(*param),
5041 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5042 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5043 if (param == NULL) {
5045 "%s %s: cannot allocate memory for parameters: %s\n",
5046 progname, argv[0], strerror(ENOMEM));
5050 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5051 param->lsp_stripe_count = lsa.lsa_stripe_count;
5052 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5053 param->lsp_stripe_offset = -1;
5055 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5056 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5057 param->lsp_stripe_pattern = lsa.lsa_pattern;
5059 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5060 param->lsp_pool = lsa.lsa_pool_name;
5061 param->lsp_is_specific = false;
5062 if (lsa.lsa_nr_tgts > 1) {
5063 if (lsa.lsa_stripe_count > 0 &&
5064 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5065 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5066 fprintf(stderr, "error: %s: stripe count %lld doesn't "
5067 "match the number of MDTs: %d\n",
5068 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5073 param->lsp_is_specific = true;
5074 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5075 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5078 dname = argv[optind];
5080 if (default_stripe) {
5081 result = llapi_dir_set_default_lmv(dname, param);
5083 /* if current \a dname isn't under the same \a mntdir
5084 * as the last one, and the last one was
5085 * auto-distributed, restore \a param.
5087 if (mntdir[0] != '\0' &&
5088 strncmp(dname, mntdir, strlen(mntdir)) &&
5090 param->lsp_is_specific = false;
5091 param->lsp_stripe_offset = -1;
5092 auto_distributed = false;
5095 if (!param->lsp_is_specific &&
5096 param->lsp_stripe_offset == -1) {
5097 char path[PATH_MAX] = "";
5100 lsb = malloc(sizeof(*lsb));
5108 /* use mntdir for dirname() temporarily */
5109 strncpy(mntdir, dname, sizeof(mntdir));
5110 if (!realpath(dirname(mntdir), path)) {
5113 "error: invalid path '%s': %s\n",
5114 argv[optind], strerror(errno));
5119 result = llapi_search_mounts(path, 0, mntdir,
5121 if (result < 0 || mntdir[0] == '\0') {
5123 "No suitable Lustre mount found\n");
5127 result = mntdf(mntdir, NULL, NULL, 0,
5128 LL_STATFS_LMV, lsb);
5132 if (param->lsp_stripe_count > lsb->sb_count) {
5134 "error: stripe count %d is too big\n",
5135 param->lsp_stripe_count);
5140 qsort(lsb->sb_buf, lsb->sb_count,
5141 sizeof(struct ll_statfs_data),
5142 ll_statfs_data_comp);
5144 auto_distributed = true;
5147 if (auto_distributed) {
5149 int nr = MAX(param->lsp_stripe_count,
5152 /* don't use server whose usage is above 90% */
5153 while (nr != param->lsp_stripe_count &&
5154 obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
5156 nr = MAX(param->lsp_stripe_count,
5159 /* get \a r between [0, nr) */
5162 param->lsp_stripe_offset =
5163 lsb->sb_buf[r].sd_index;
5164 if (param->lsp_stripe_count > 1) {
5167 param->lsp_is_specific = true;
5168 for (; i < param->lsp_stripe_count; i++)
5169 param->lsp_tgts[(i + r) % nr] =
5170 lsb->sb_buf[i].sd_index;
5174 result = llapi_dir_create(dname, mode, param);
5179 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5180 progname, dname, strerror(-result));
5183 dname = argv[++optind];
5184 } while (dname != NULL);
5186 if (mode_opt != NULL)
5187 umask(previous_mode);
5195 static int lfs_rmentry(int argc, char **argv)
5202 fprintf(stderr, "error: %s: missing dirname\n",
5208 dname = argv[index];
5209 while (dname != NULL) {
5210 result = llapi_direntry_remove(dname);
5212 fprintf(stderr, "error: %s: remove dir entry '%s' "
5213 "failed\n", argv[0], dname);
5216 dname = argv[++index];
5221 static int lfs_mv(int argc, char **argv)
5223 struct lmv_user_md lmu = { LMV_USER_MAGIC };
5224 struct find_param param = {
5231 struct option long_opts[] = {
5232 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5233 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5236 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5238 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5240 fprintf(stderr, "warning: '-M' deprecated"
5241 ", use '--mdt-index' or '-m' instead\n");
5244 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5246 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5252 param.fp_verbose = VERBOSE_DETAIL;
5255 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5256 progname, argv[optind - 1]);
5261 if (lmu.lum_stripe_offset == -1) {
5262 fprintf(stderr, "%s mv: MDT index must be specified\n",
5267 if (optind >= argc) {
5268 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5273 /* initialize migrate mdt parameters */
5274 param.fp_lmv_md = &lmu;
5275 param.fp_migrate = 1;
5276 rc = llapi_migrate_mdt(argv[optind], ¶m);
5278 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5279 progname, argv[optind], param.fp_mdt_index,
5284 static int lfs_osts(int argc, char **argv)
5286 return lfs_tgts(argc, argv);
5289 static int lfs_mdts(int argc, char **argv)
5291 return lfs_tgts(argc, argv);
5294 static int lfs_df(int argc, char **argv)
5296 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5297 enum mntdf_flags flags = MNTDF_SHOW;
5298 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5299 int c, rc = 0, index = 0;
5300 char fsname[PATH_MAX] = "", *pool_name = NULL;
5301 struct option long_opts[] = {
5302 { .val = 'h', .name = "human-readable",
5303 .has_arg = no_argument },
5304 { .val = 'i', .name = "inodes", .has_arg = no_argument },
5305 { .val = 'l', .name = "lazy", .has_arg = no_argument },
5306 { .val = 'p', .name = "pool", .has_arg = required_argument },
5307 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5310 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5313 flags |= MNTDF_COOKED;
5316 flags |= MNTDF_INODES;
5319 flags |= MNTDF_LAZY;
5325 flags |= MNTDF_VERBOSE;
5331 if (optind < argc && !realpath(argv[optind], path)) {
5333 fprintf(stderr, "error: invalid path '%s': %s\n",
5334 argv[optind], strerror(-rc));
5338 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5339 /* Check if we have a mount point */
5340 if (mntdir[0] == '\0')
5343 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5344 if (rc || path[0] != '\0')
5346 fsname[0] = '\0'; /* avoid matching in next loop */
5347 mntdir[0] = '\0'; /* avoid matching in next loop */
5353 static int lfs_getname(int argc, char **argv)
5355 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5356 int rc = 0, index = 0, c;
5357 char buf[sizeof(struct obd_uuid)];
5359 while ((c = getopt(argc, argv, "h")) != -1)
5362 if (optind == argc) { /* no paths specified, get all paths. */
5363 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5364 rc = llapi_getname(mntdir, buf, sizeof(buf));
5367 "cannot get name for `%s': %s\n",
5368 mntdir, strerror(-rc));
5372 printf("%s %s\n", buf, mntdir);
5374 path[0] = fsname[0] = mntdir[0] = 0;
5376 } else { /* paths specified, only attempt to search these. */
5377 for (; optind < argc; optind++) {
5378 rc = llapi_getname(argv[optind], buf, sizeof(buf));
5381 "cannot get name for `%s': %s\n",
5382 argv[optind], strerror(-rc));
5386 printf("%s %s\n", buf, argv[optind]);
5392 static int lfs_check(int argc, char **argv)
5395 char mntdir[PATH_MAX] = {'\0'};
5402 fprintf(stderr, "%s check: server type must be specified\n",
5407 obd_types[0] = obd_type1;
5408 obd_types[1] = obd_type2;
5410 if (strcmp(argv[1], "osts") == 0) {
5411 strcpy(obd_types[0], "osc");
5412 } else if (strcmp(argv[1], "mds") == 0) {
5413 strcpy(obd_types[0], "mdc");
5414 } else if (strcmp(argv[1], "servers") == 0) {
5416 strcpy(obd_types[0], "osc");
5417 strcpy(obd_types[1], "mdc");
5419 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5424 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5425 if (rc < 0 || mntdir[0] == '\0') {
5427 "%s check: cannot find mounted Lustre filesystem: %s\n",
5428 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5432 rc = llapi_target_check(num_types, obd_types, mntdir);
5434 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5435 progname, argv[1], strerror(-rc));
5441 #ifdef HAVE_SYS_QUOTA_H
5442 #define ARG2INT(nr, str, msg) \
5445 nr = strtol(str, &endp, 0); \
5446 if (*endp != '\0') { \
5447 fprintf(stderr, "%s: bad %s '%s'\n", \
5448 progname, msg, str); \
5453 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5455 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5456 * returns the value or ULONG_MAX on integer overflow or incorrect format
5458 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5459 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5460 * 3. empty integer value is interpreted as 0
5462 static unsigned long str2sec(const char* timestr)
5464 const char spec[] = "smhdw";
5465 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5466 unsigned long val = 0;
5469 if (strpbrk(timestr, spec) == NULL) {
5470 /* no specifiers inside the time string,
5471 should treat it as an integer value */
5472 val = strtoul(timestr, &tail, 10);
5473 return *tail ? ULONG_MAX : val;
5476 /* format string is XXwXXdXXhXXmXXs */
5482 v = strtoul(timestr, &tail, 10);
5483 if (v == ULONG_MAX || *tail == '\0')
5484 /* value too large (ULONG_MAX or more)
5485 or missing specifier */
5488 ptr = strchr(spec, *tail);
5490 /* unknown specifier */
5495 /* check if product will overflow the type */
5496 if (!(v < ULONG_MAX / mult[ind]))
5499 ADD_OVERFLOW(val, mult[ind] * v);
5500 if (val == ULONG_MAX)
5512 #define ARG2ULL(nr, str, def_units) \
5514 unsigned long long limit, units = def_units; \
5517 rc = llapi_parse_size(str, &limit, &units, 1); \
5519 fprintf(stderr, "%s: invalid limit '%s'\n", \
5526 static inline int has_times_option(int argc, char **argv)
5530 for (i = 1; i < argc; i++)
5531 if (!strcmp(argv[i], "-t"))
5537 int lfs_setquota_times(int argc, char **argv)
5540 struct if_quotactl qctl;
5541 char *mnt, *obd_type = (char *)qctl.obd_type;
5542 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5543 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5544 struct option long_opts[] = {
5545 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
5546 { .val = 'g', .name = "group", .has_arg = no_argument },
5547 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
5548 { .val = 'p', .name = "projid", .has_arg = no_argument },
5549 { .val = 't', .name = "times", .has_arg = no_argument },
5550 { .val = 'u', .name = "user", .has_arg = no_argument },
5554 memset(&qctl, 0, sizeof(qctl));
5555 qctl.qc_cmd = LUSTRE_Q_SETINFO;
5556 qctl.qc_type = ALLQUOTA;
5558 while ((c = getopt_long(argc, argv, "b:gi:ptu",
5559 long_opts, NULL)) != -1) {
5570 if (qctl.qc_type != ALLQUOTA) {
5571 fprintf(stderr, "error: -u/g/p can't be used "
5572 "more than once\n");
5575 qctl.qc_type = qtype;
5578 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5579 fprintf(stderr, "error: bad block-grace: %s\n",
5583 dqb->dqb_valid |= QIF_BTIME;
5586 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5587 fprintf(stderr, "error: bad inode-grace: %s\n",
5591 dqb->dqb_valid |= QIF_ITIME;
5593 case 't': /* Yes, of course! */
5595 default: /* getopt prints error message for us when opterr != 0 */
5600 if (qctl.qc_type == ALLQUOTA) {
5601 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5605 if (optind != argc - 1) {
5606 fprintf(stderr, "error: unexpected parameters encountered\n");
5610 if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5611 (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5612 fprintf(stderr, "error: grace time is too large\n");
5617 rc = llapi_quotactl(mnt, &qctl);
5620 fprintf(stderr, "%s %s ", obd_type,
5621 obd_uuid2str(&qctl.obd_uuid));
5622 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5629 #define BSLIMIT (1 << 0)
5630 #define BHLIMIT (1 << 1)
5631 #define ISLIMIT (1 << 2)
5632 #define IHLIMIT (1 << 3)
5634 int lfs_setquota(int argc, char **argv)
5637 struct if_quotactl qctl;
5638 char *mnt, *obd_type = (char *)qctl.obd_type;
5639 struct obd_dqblk *dqb = &qctl.qc_dqblk;
5640 struct option long_opts[] = {
5641 { .val = 'b', .name = "block-softlimit",
5642 .has_arg = required_argument },
5643 { .val = 'B', .name = "block-hardlimit",
5644 .has_arg = required_argument },
5645 { .val = 'd', .name = "default", .has_arg = no_argument },
5646 { .val = 'g', .name = "group", .has_arg = required_argument },
5647 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
5648 { .val = 'i', .name = "inode-softlimit",
5649 .has_arg = required_argument },
5650 { .val = 'I', .name = "inode-hardlimit",
5651 .has_arg = required_argument },
5652 { .val = 'p', .name = "projid", .has_arg = required_argument },
5653 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
5654 { .val = 'u', .name = "user", .has_arg = required_argument },
5655 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
5657 unsigned limit_mask = 0;
5659 bool use_default = false;
5662 if (has_times_option(argc, argv))
5663 return lfs_setquota_times(argc, argv);
5665 memset(&qctl, 0, sizeof(qctl));
5666 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
5667 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5668 * so it can be used as a marker that qc_type
5669 * isn't reinitialized from command line */
5671 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5672 long_opts, NULL)) != -1) {
5675 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5678 goto quota_type_def;
5681 rc = name2uid(&qctl.qc_id, optarg);
5684 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5687 goto quota_type_def;
5690 rc = name2gid(&qctl.qc_id, optarg);
5693 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5696 goto quota_type_def;
5699 rc = name2projid(&qctl.qc_id, optarg);
5702 qctl.qc_id = strtoul(optarg, &endptr, 10);
5703 if (*endptr != '\0') {
5704 fprintf(stderr, "%s setquota: invalid"
5705 " id '%s'\n", progname, optarg);
5710 if (qctl.qc_id == 0) {
5711 fprintf(stderr, "%s setquota: can't set quota"
5712 " for root usr/group/project.\n",
5718 if (qctl.qc_type != ALLQUOTA) {
5720 "%s setquota: only one of -u, -U, -g,"
5721 " -G, -p or -P may be specified\n",
5725 qctl.qc_type = qtype;
5728 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5732 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5733 dqb->dqb_bsoftlimit >>= 10;
5734 limit_mask |= BSLIMIT;
5735 if (dqb->dqb_bsoftlimit &&
5736 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5738 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5739 "See '%s help setquota' or Lustre manual for details\n",
5740 progname, dqb->dqb_bsoftlimit,
5744 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5745 dqb->dqb_bhardlimit >>= 10;
5746 limit_mask |= BHLIMIT;
5747 if (dqb->dqb_bhardlimit &&
5748 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5750 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5751 "See '%s help setquota' or Lustre manual for details\n",
5752 progname, dqb->dqb_bhardlimit,
5756 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5757 limit_mask |= ISLIMIT;
5758 if (dqb->dqb_isoftlimit &&
5759 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5761 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5762 "See '%s help setquota' or Lustre manual for details\n",
5763 progname, dqb->dqb_isoftlimit,
5767 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5768 limit_mask |= IHLIMIT;
5769 if (dqb->dqb_ihardlimit &&
5770 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5772 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5773 "See '%s help setquota' or Lustre manual for details\n",
5774 progname, dqb->dqb_ihardlimit,
5779 "%s setquota: unrecognized option '%s'\n",
5780 progname, argv[optind - 1]);
5785 if (qctl.qc_type == ALLQUOTA) {
5787 "%s setquota: either -u or -g must be specified\n",
5792 if (!use_default && limit_mask == 0) {
5794 "%s setquota: at least one limit must be specified\n",
5799 if (use_default && limit_mask != 0) {
5801 "%s setquota: limits should not be specified when"
5802 " using default quota\n",
5807 if (use_default && qctl.qc_id == 0) {
5809 "%s setquota: can not set default quota for root"
5810 " user/group/project\n",
5815 if (optind != argc - 1) {
5817 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5818 progname, argv[optind]);
5825 dqb->dqb_bhardlimit = 0;
5826 dqb->dqb_bsoftlimit = 0;
5827 dqb->dqb_ihardlimit = 0;
5828 dqb->dqb_isoftlimit = 0;
5831 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5832 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5833 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5834 /* sigh, we can't just set blimits/ilimits */
5835 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
5836 .qc_type = qctl.qc_type,
5837 .qc_id = qctl.qc_id};
5839 rc = llapi_quotactl(mnt, &tmp_qctl);
5843 if (!(limit_mask & BHLIMIT))
5844 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5845 if (!(limit_mask & BSLIMIT))
5846 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5847 if (!(limit_mask & IHLIMIT))
5848 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5849 if (!(limit_mask & ISLIMIT))
5850 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5852 /* Keep grace times if we have got no softlimit arguments */
5853 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5854 dqb->dqb_valid |= QIF_BTIME;
5855 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5858 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5859 dqb->dqb_valid |= QIF_ITIME;
5860 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5864 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5865 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5867 rc = llapi_quotactl(mnt, &qctl);
5871 "%s setquota: cannot quotactl '%s' '%s': %s",
5873 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5880 /* Converts seconds value into format string
5881 * result is returned in buf
5883 * 1. result is in descenting order: 1w2d3h4m5s
5884 * 2. zero fields are not filled (except for p. 3): 5d1s
5885 * 3. zero seconds value is presented as "0s"
5887 static char * __sec2str(time_t seconds, char *buf)
5889 const char spec[] = "smhdw";
5890 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5895 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5896 c = seconds / mult[i];
5898 if (c > 0 || (i == 0 && buf == tail))
5899 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5907 static void sec2str(time_t seconds, char *buf, int rc)
5914 tail = __sec2str(seconds, tail);
5916 if (rc && tail - buf < 39) {
5922 static void diff2str(time_t seconds, char *buf, time_t now)
5928 if (seconds <= now) {
5929 strcpy(buf, "none");
5932 __sec2str(seconds - now, buf);
5935 static void print_quota_title(char *name, struct if_quotactl *qctl,
5936 bool human_readable, bool show_default)
5939 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
5940 printf("%15s %8s%8s%8s %8s%8s%8s\n",
5941 "Filesystem", "bquota", "blimit", "bgrace",
5942 "iquota", "ilimit", "igrace");
5944 printf("Disk quotas for %s %s (%cid %u):\n",
5945 qtype_name(qctl->qc_type), name,
5946 *qtype_name(qctl->qc_type), qctl->qc_id);
5947 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5948 "Filesystem", human_readable ? "used" : "kbytes",
5949 "quota", "limit", "grace",
5950 "files", "quota", "limit", "grace");
5954 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5957 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5960 snprintf(buf, buflen, "%5.4gP",
5961 (double)num / ((__u64)1 << 40));
5963 snprintf(buf, buflen, "%5.4gT",
5964 (double)num / (1 << 30));
5966 snprintf(buf, buflen, "%5.4gG",
5967 (double)num / (1 << 20));
5969 snprintf(buf, buflen, "%5.4gM",
5970 (double)num / (1 << 10));
5972 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5976 #define STRBUF_LEN 32
5977 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5978 int rc, bool h, bool show_default)
5984 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
5985 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
5986 int bover = 0, iover = 0;
5987 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5988 char numbuf[3][STRBUF_LEN];
5990 char strbuf[STRBUF_LEN];
5992 if (dqb->dqb_bhardlimit &&
5993 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5995 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5996 if (dqb->dqb_btime > now) {
6003 if (dqb->dqb_ihardlimit &&
6004 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6006 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6007 if (dqb->dqb_itime > now) {
6015 if (strlen(mnt) > 15)
6016 printf("%s\n%15s", mnt, "");
6018 printf("%15s", mnt);
6021 diff2str(dqb->dqb_btime, timebuf, now);
6022 else if (show_default)
6023 snprintf(timebuf, sizeof(timebuf), "%llu",
6026 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6027 strbuf, sizeof(strbuf), h);
6028 if (rc == -EREMOTEIO)
6029 sprintf(numbuf[0], "%s*", strbuf);
6031 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6032 "%s" : "[%s]", strbuf);
6034 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6035 if (type == QC_GENERAL)
6036 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6037 "%s" : "[%s]", strbuf);
6039 sprintf(numbuf[1], "%s", "-");
6041 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6042 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6043 "%s" : "[%s]", strbuf);
6046 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6048 printf(" %7s%c %6s %7s %7s",
6049 numbuf[0], bover ? '*' : ' ', numbuf[1],
6050 numbuf[2], bover > 1 ? timebuf : "-");
6054 diff2str(dqb->dqb_itime, timebuf, now);
6055 else if (show_default)
6056 snprintf(timebuf, sizeof(timebuf), "%llu",
6059 snprintf(numbuf[0], sizeof(numbuf),
6060 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6061 (uintmax_t)dqb->dqb_curinodes);
6063 if (type == QC_GENERAL)
6064 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6066 (uintmax_t)dqb->dqb_isoftlimit);
6068 sprintf(numbuf[1], "%s", "-");
6070 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6071 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6074 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6075 else if (type != QC_OSTIDX)
6076 printf(" %7s%c %6s %7s %7s",
6077 numbuf[0], iover ? '*' : ' ', numbuf[1],
6078 numbuf[2], iover > 1 ? timebuf : "-");
6080 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6083 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6084 qctl->qc_cmd == Q_GETOINFO) {
6088 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6089 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6090 printf("Block grace time: %s; Inode grace time: %s\n",
6091 bgtimebuf, igtimebuf);
6095 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6096 bool h, __u64 *total)
6098 int rc = 0, rc1 = 0, count = 0;
6099 __u32 valid = qctl->qc_valid;
6101 rc = llapi_get_obd_count(mnt, &count, is_mdt);
6103 fprintf(stderr, "can not get %s count: %s\n",
6104 is_mdt ? "mdt": "ost", strerror(-rc));
6108 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6109 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6110 rc = llapi_quotactl(mnt, qctl);
6112 /* It is remote client case. */
6113 if (rc == -EOPNOTSUPP) {
6120 fprintf(stderr, "quotactl %s%d failed.\n",
6121 is_mdt ? "mdt": "ost", qctl->qc_idx);
6125 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6126 qctl->qc_valid, 0, h, false);
6127 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6128 qctl->qc_dqblk.dqb_bhardlimit;
6131 qctl->qc_valid = valid;
6135 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6136 int verbose, int quiet, bool human_readable,
6139 int rc1 = 0, rc2 = 0, rc3 = 0;
6140 char *obd_type = (char *)qctl->obd_type;
6141 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6142 __u64 total_ialloc = 0, total_balloc = 0;
6143 bool use_default_for_blk = false;
6144 bool use_default_for_file = false;
6147 rc1 = llapi_quotactl(mnt, qctl);
6151 fprintf(stderr, "%s quotas are not enabled.\n",
6152 qtype_name(qctl->qc_type));
6155 fprintf(stderr, "Permission denied.\n");
6158 /* We already got error message. */
6161 fprintf(stderr, "Unexpected quotactl error: %s\n",
6166 if (!show_default && qctl->qc_id == 0) {
6167 qctl->qc_dqblk.dqb_bhardlimit = 0;
6168 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6169 qctl->qc_dqblk.dqb_ihardlimit = 0;
6170 qctl->qc_dqblk.dqb_isoftlimit = 0;
6171 qctl->qc_dqblk.dqb_btime = 0;
6172 qctl->qc_dqblk.dqb_itime = 0;
6173 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6176 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6177 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6178 use_default_for_blk = true;
6179 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6182 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6183 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6184 use_default_for_file = true;
6185 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6188 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6189 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6190 print_quota_title(name, qctl, human_readable, show_default);
6192 if (rc1 && *obd_type)
6193 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6195 if (qctl->qc_valid != QC_GENERAL)
6198 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6199 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6200 (QIF_LIMITS|QIF_USAGE));
6202 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6204 if (!show_default && verbose &&
6205 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6206 char strbuf[STRBUF_LEN];
6208 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6210 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6212 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6214 printf("Total allocated inode limit: %ju, total "
6215 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6219 if (use_default_for_blk)
6220 printf("%cid %u is using default block quota setting\n",
6221 *qtype_name(qctl->qc_type), qctl->qc_id);
6223 if (use_default_for_file)
6224 printf("%cid %u is using default file quota setting\n",
6225 *qtype_name(qctl->qc_type), qctl->qc_id);
6227 if (rc1 || rc2 || rc3 || inacc)
6228 printf("Some errors happened when getting quota info. "
6229 "Some devices may be not working or deactivated. "
6230 "The data in \"[]\" is inaccurate.\n");
6236 static int lfs_project(int argc, char **argv)
6238 int ret = 0, err = 0, c, i;
6239 struct project_handle_control phc = { 0 };
6240 enum lfs_project_ops_t op;
6243 phc.assign_projid = false;
6244 /* default action */
6245 op = LFS_PROJECT_LIST;
6247 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6250 if (op != LFS_PROJECT_LIST) {
6252 "%s: cannot specify '-c' '-C' '-s' together\n",
6257 op = LFS_PROJECT_CHECK;
6260 if (op != LFS_PROJECT_LIST) {
6262 "%s: cannot specify '-c' '-C' '-s' together\n",
6267 op = LFS_PROJECT_CLEAR;
6270 if (op != LFS_PROJECT_LIST) {
6272 "%s: cannot specify '-c' '-C' '-s' together\n",
6277 phc.set_inherit = true;
6278 op = LFS_PROJECT_SET;
6284 phc.keep_projid = true;
6287 phc.recursive = true;
6290 phc.projid = strtoul(optarg, NULL, 0);
6291 phc.assign_projid = true;
6295 phc.newline = false;
6298 fprintf(stderr, "%s: invalid option '%c'\n",
6304 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6305 op = LFS_PROJECT_SET;
6306 phc.set_projid = true;
6307 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6308 phc.set_projid = true;
6312 case LFS_PROJECT_CHECK:
6313 if (phc.keep_projid) {
6315 "%s: '-k' is useless together with '-c'\n",
6320 case LFS_PROJECT_CLEAR:
6323 "%s: '-0' is useless together with '-C'\n",
6327 if (phc.assign_projid) {
6329 "%s: '-p' is useless together with '-C'\n",
6334 case LFS_PROJECT_SET:
6337 "%s: '-0' is useless together with '-s'\n",
6341 if (phc.keep_projid) {
6343 "%s: '-k' is useless together with '-s'\n",
6351 "%s: '-0' is useless for list operations\n",
6361 fprintf(stderr, "%s: missing file or directory target(s)\n",
6366 for (i = 0; i < argc; i++) {
6368 case LFS_PROJECT_CHECK:
6369 err = lfs_project_check(argv[i], &phc);
6371 case LFS_PROJECT_LIST:
6372 err = lfs_project_list(argv[i], &phc);
6374 case LFS_PROJECT_CLEAR:
6375 err = lfs_project_clear(argv[i], &phc);
6377 case LFS_PROJECT_SET:
6378 err = lfs_project_set(argv[i], &phc);
6390 static int lfs_quota(int argc, char **argv)
6393 char *mnt, *name = NULL;
6394 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6395 .qc_type = ALLQUOTA };
6396 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6397 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6399 __u32 valid = QC_GENERAL, idx = 0;
6400 bool human_readable = false;
6401 bool show_default = false;
6404 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6407 show_default = true;
6412 show_default = true;
6417 show_default = true;
6421 if (qctl.qc_type != ALLQUOTA) {
6423 "%s quota: only one of -u, -g, or -p may be specified\n",
6427 qctl.qc_type = qtype;
6430 qctl.qc_cmd = LUSTRE_Q_GETINFO;
6433 valid = qctl.qc_valid = QC_UUID;
6434 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6437 valid = qctl.qc_valid = QC_MDTIDX;
6438 idx = qctl.qc_idx = atoi(optarg);
6439 if (idx == 0 && *optarg != '0') {
6441 "%s quota: invalid MDT index '%s'\n",
6447 valid = qctl.qc_valid = QC_OSTIDX;
6448 idx = qctl.qc_idx = atoi(optarg);
6449 if (idx == 0 && *optarg != '0') {
6451 "%s quota: invalid OST index '%s'\n",
6463 human_readable = true;
6466 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6467 progname, argv[optind - 1]);
6472 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6473 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6474 optind == argc - 1 && !show_default) {
6476 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6477 qctl.qc_valid = valid;
6480 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6481 qctl.qc_type = qtype;
6482 if (qtype == USRQUOTA) {
6483 qctl.qc_id = geteuid();
6484 rc = uid2name(&name, qctl.qc_id);
6486 qctl.qc_id = getegid();
6487 rc = gid2name(&name, qctl.qc_id);
6492 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6493 human_readable, show_default);
6498 /* lfs quota -u username /path/to/lustre/mount */
6499 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6500 /* options should be followed by u/g-name and mntpoint */
6501 if ((!show_default && optind + 2 != argc) ||
6502 (show_default && optind + 1 != argc) ||
6503 qctl.qc_type == ALLQUOTA) {
6505 "%s quota: name and mount point must be specified\n",
6510 if (!show_default) {
6511 name = argv[optind++];
6512 switch (qctl.qc_type) {
6514 rc = name2uid(&qctl.qc_id, name);
6517 rc = name2gid(&qctl.qc_id, name);
6520 rc = name2projid(&qctl.qc_id, name);
6527 qctl.qc_valid = QC_GENERAL;
6528 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6533 qctl.qc_id = strtoul(name, &endptr, 10);
6534 if (*endptr != '\0') {
6535 fprintf(stderr, "%s quota: invalid id '%s'\n",
6540 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6541 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6547 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6548 human_readable, show_default);
6551 #endif /* HAVE_SYS_QUOTA_H! */
6553 static int flushctx_ioctl(char *mp)
6557 fd = open(mp, O_RDONLY);
6559 fprintf(stderr, "flushctx: error open %s: %s\n",
6560 mp, strerror(errno));
6564 rc = ioctl(fd, LL_IOC_FLUSHCTX);
6566 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6567 mp, strerror(errno));
6573 static int lfs_flushctx(int argc, char **argv)
6575 int kdestroy = 0, c;
6576 char mntdir[PATH_MAX] = {'\0'};
6580 while ((c = getopt(argc, argv, "k")) != -1) {
6586 fprintf(stderr, "error: %s: option '-%c' "
6587 "unrecognized\n", argv[0], c);
6593 if ((rc = system("kdestroy > /dev/null")) != 0) {
6594 rc = WEXITSTATUS(rc);
6595 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6599 if (optind >= argc) {
6600 /* flush for all mounted lustre fs. */
6601 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6602 /* Check if we have a mount point */
6603 if (mntdir[0] == '\0')
6606 if (flushctx_ioctl(mntdir))
6609 mntdir[0] = '\0'; /* avoid matching in next loop */
6612 /* flush fs as specified */
6613 while (optind < argc) {
6614 if (flushctx_ioctl(argv[optind++]))
6621 static int lfs_cp(int argc, char **argv)
6623 fprintf(stderr, "remote client copy file(s).\n"
6624 "obsolete, does not support it anymore.\n");
6628 static int lfs_ls(int argc, char **argv)
6630 fprintf(stderr, "remote client lists directory contents.\n"
6631 "obsolete, does not support it anymore.\n");
6635 static int lfs_changelog(int argc, char **argv)
6637 void *changelog_priv;
6638 struct changelog_rec *rec;
6639 long long startrec = 0, endrec = 0;
6641 struct option long_opts[] = {
6642 { .val = 'f', .name = "follow", .has_arg = no_argument },
6644 char short_opts[] = "f";
6647 while ((rc = getopt_long(argc, argv, short_opts,
6648 long_opts, NULL)) != -1) {
6655 "%s changelog: unrecognized option '%s'\n",
6656 progname, argv[optind - 1]);
6660 if (optind >= argc) {
6661 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6666 mdd = argv[optind++];
6668 startrec = strtoll(argv[optind++], NULL, 10);
6670 endrec = strtoll(argv[optind++], NULL, 10);
6672 rc = llapi_changelog_start(&changelog_priv,
6673 CHANGELOG_FLAG_BLOCK |
6674 CHANGELOG_FLAG_JOBID |
6675 CHANGELOG_FLAG_EXTRA_FLAGS |
6676 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6679 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6680 progname, strerror(errno = -rc));
6684 rc = llapi_changelog_set_xflags(changelog_priv,
6685 CHANGELOG_EXTRA_FLAG_UIDGID |
6686 CHANGELOG_EXTRA_FLAG_NID |
6687 CHANGELOG_EXTRA_FLAG_OMODE |
6688 CHANGELOG_EXTRA_FLAG_XATTR);
6691 "%s changelog: cannot set xflags for changelog: %s\n",
6692 progname, strerror(errno = -rc));
6696 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6700 if (endrec && rec->cr_index > endrec) {
6701 llapi_changelog_free(&rec);
6704 if (rec->cr_index < startrec) {
6705 llapi_changelog_free(&rec);
6709 secs = rec->cr_time >> 30;
6710 gmtime_r(&secs, &ts);
6711 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6712 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6713 changelog_type2str(rec->cr_type),
6714 ts.tm_hour, ts.tm_min, ts.tm_sec,
6715 (int)(rec->cr_time & ((1 << 30) - 1)),
6716 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6717 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6719 if (rec->cr_flags & CLF_JOBID) {
6720 struct changelog_ext_jobid *jid =
6721 changelog_rec_jobid(rec);
6723 if (jid->cr_jobid[0] != '\0')
6724 printf(" j=%s", jid->cr_jobid);
6727 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6728 struct changelog_ext_extra_flags *ef =
6729 changelog_rec_extra_flags(rec);
6731 printf(" ef=0x%llx", ef->cr_extra_flags);
6733 if (ef->cr_extra_flags & CLFE_UIDGID) {
6734 struct changelog_ext_uidgid *uidgid =
6735 changelog_rec_uidgid(rec);
6737 printf(" u=%llu:%llu",
6738 uidgid->cr_uid, uidgid->cr_gid);
6740 if (ef->cr_extra_flags & CLFE_NID) {
6741 struct changelog_ext_nid *nid =
6742 changelog_rec_nid(rec);
6745 libcfs_nid2str(nid->cr_nid));
6748 if (ef->cr_extra_flags & CLFE_OPEN) {
6749 struct changelog_ext_openmode *omd =
6750 changelog_rec_openmode(rec);
6751 char mode[] = "---";
6753 /* exec mode must be exclusive */
6754 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6757 if (omd->cr_openflags & MDS_FMODE_READ)
6759 if (omd->cr_openflags &
6766 if (strcmp(mode, "---") != 0)
6767 printf(" m=%s", mode);
6771 if (ef->cr_extra_flags & CLFE_XATTR) {
6772 struct changelog_ext_xattr *xattr =
6773 changelog_rec_xattr(rec);
6775 if (xattr->cr_xattr[0] != '\0')
6776 printf(" x=%s", xattr->cr_xattr);
6780 if (rec->cr_namelen)
6781 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6782 rec->cr_namelen, changelog_rec_name(rec));
6784 if (rec->cr_flags & CLF_RENAME) {
6785 struct changelog_ext_rename *rnm =
6786 changelog_rec_rename(rec);
6788 if (!fid_is_zero(&rnm->cr_sfid))
6789 printf(" s="DFID" sp="DFID" %.*s",
6790 PFID(&rnm->cr_sfid),
6791 PFID(&rnm->cr_spfid),
6792 (int)changelog_rec_snamelen(rec),
6793 changelog_rec_sname(rec));
6797 llapi_changelog_free(&rec);
6800 llapi_changelog_fini(&changelog_priv);
6803 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6804 progname, strerror(errno = -rc));
6806 return (rc == 1 ? 0 : rc);
6809 static int lfs_changelog_clear(int argc, char **argv)
6817 endrec = strtoll(argv[3], NULL, 10);
6819 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6822 fprintf(stderr, "%s: record out of range: %llu\n",
6824 else if (rc == -ENOENT)
6825 fprintf(stderr, "%s: no changelog user: %s\n",
6828 fprintf(stderr, "%s error: %s\n", argv[0],
6837 static int lfs_fid2path(int argc, char **argv)
6839 struct option long_opts[] = {
6840 { .val = 'c', .name = "cur", .has_arg = no_argument },
6841 { .val = 'l', .name = "link", .has_arg = required_argument },
6842 { .val = 'r', .name = "rec", .has_arg = required_argument },
6844 char short_opts[] = "cl:r:";
6845 char *device, *fid, *path;
6846 long long recno = -1;
6851 char *endptr = NULL;
6853 while ((rc = getopt_long(argc, argv, short_opts,
6854 long_opts, NULL)) != -1) {
6860 linkno = strtol(optarg, &endptr, 10);
6861 if (*endptr != '\0') {
6863 "%s fid2path: invalid linkno '%s'\n",
6869 recno = strtoll(optarg, &endptr, 10);
6870 if (*endptr != '\0') {
6872 "%s fid2path: invalid recno '%s'\n",
6879 "%s fid2path: unrecognized option '%s'\n",
6880 progname, argv[optind - 1]);
6887 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6892 device = argv[optind++];
6893 path = calloc(1, PATH_MAX);
6897 "%s fid2path: cannot allocate memory for path: %s\n",
6898 progname, strerror(-rc));
6903 while (optind < argc) {
6904 fid = argv[optind++];
6906 lnktmp = (linkno >= 0) ? linkno : 0;
6908 int oldtmp = lnktmp;
6909 long long rectmp = recno;
6911 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6915 "%s fid2path: cannot find '%s': %s\n",
6916 progname, fid, strerror(errno = -rc2));
6923 fprintf(stdout, "%lld ", rectmp);
6924 if (device[0] == '/') {
6925 fprintf(stdout, "%s", device);
6926 if (device[strlen(device) - 1] != '/')
6927 fprintf(stdout, "/");
6928 } else if (path[0] == '\0') {
6929 fprintf(stdout, "/");
6931 fprintf(stdout, "%s\n", path);
6934 /* specified linkno */
6936 if (oldtmp == lnktmp)
6946 static int lfs_path2fid(int argc, char **argv)
6948 struct option long_opts[] = {
6949 { .val = 'p', .name = "parents", .has_arg = no_argument },
6952 const char short_opts[] = "p";
6953 const char *sep = "";
6956 bool show_parents = false;
6958 while ((rc = getopt_long(argc, argv, short_opts,
6959 long_opts, NULL)) != -1) {
6962 show_parents = true;
6966 "%s path2fid: unrecognized option '%s'\n",
6967 progname, argv[optind - 1]);
6972 if (optind > argc - 1) {
6973 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
6977 else if (optind < argc - 1)
6981 for (path = argv + optind; *path != NULL; path++) {
6983 if (!show_parents) {
6984 err = llapi_path2fid(*path, &fid);
6986 printf("%s%s"DFID"\n",
6987 *sep != '\0' ? *path : "", sep,
6990 char name[NAME_MAX + 1];
6991 unsigned int linkno = 0;
6993 while ((err = llapi_path2parent(*path, linkno, &fid,
6994 name, sizeof(name))) == 0) {
6995 if (*sep != '\0' && linkno == 0)
6996 printf("%s%s", *path, sep);
6998 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7003 /* err == -ENODATA is end-of-loop */
7004 if (linkno > 0 && err == -ENODATA) {
7012 "%s path2fid: cannot get %sfid for '%s': %s\n",
7013 progname, show_parents ? "parent " : "", *path,
7025 static int lfs_data_version(int argc, char **argv)
7032 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7035 fprintf(stderr, "%s data_version: FILE must be specified\n",
7040 while ((c = getopt(argc, argv, "nrw")) != -1) {
7043 data_version_flags = 0;
7046 data_version_flags |= LL_DV_RD_FLUSH;
7049 data_version_flags |= LL_DV_WR_FLUSH;
7053 "%s data_version: unrecognized option '%s'\n",
7054 progname, argv[optind - 1]);
7058 if (optind == argc) {
7059 fprintf(stderr, "%s data_version: FILE must be specified\n",
7064 path = argv[optind];
7065 fd = open(path, O_RDONLY);
7068 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7069 progname, path, strerror(-rc));
7073 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7076 "%s data_version: cannot get version for '%s': %s\n",
7077 progname, path, strerror(-rc));
7079 printf("%ju" "\n", (uintmax_t)data_version);
7085 static int lfs_hsm_state(int argc, char **argv)
7090 struct hsm_user_state hus;
7098 rc = llapi_hsm_state_get(path, &hus);
7100 fprintf(stderr, "can't get hsm state for %s: %s\n",
7101 path, strerror(errno = -rc));
7105 /* Display path name and status flags */
7106 printf("%s: (0x%08x)", path, hus.hus_states);
7108 if (hus.hus_states & HS_RELEASED)
7109 printf(" released");
7110 if (hus.hus_states & HS_EXISTS)
7112 if (hus.hus_states & HS_DIRTY)
7114 if (hus.hus_states & HS_ARCHIVED)
7115 printf(" archived");
7116 /* Display user-settable flags */
7117 if (hus.hus_states & HS_NORELEASE)
7118 printf(" never_release");
7119 if (hus.hus_states & HS_NOARCHIVE)
7120 printf(" never_archive");
7121 if (hus.hus_states & HS_LOST)
7122 printf(" lost_from_hsm");
7124 if (hus.hus_archive_id != 0)
7125 printf(", archive_id:%d", hus.hus_archive_id);
7128 } while (++i < argc);
7133 #define LFS_HSM_SET 0
7134 #define LFS_HSM_CLEAR 1
7137 * Generic function to set or clear HSM flags.
7138 * Used by hsm_set and hsm_clear.
7140 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7142 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7144 struct option long_opts[] = {
7145 { .val = 'A', .name = "archived", .has_arg = no_argument },
7146 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
7147 { .val = 'd', .name = "dirty", .has_arg = no_argument },
7148 { .val = 'e', .name = "exists", .has_arg = no_argument },
7149 { .val = 'l', .name = "lost", .has_arg = no_argument },
7150 { .val = 'r', .name = "norelease", .has_arg = no_argument },
7151 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
7153 char short_opts[] = "lraAdei:";
7157 __u32 archive_id = 0;
7163 while ((c = getopt_long(argc, argv, short_opts,
7164 long_opts, NULL)) != -1) {
7170 mask |= HS_NOARCHIVE;
7173 mask |= HS_ARCHIVED;
7176 mask |= HS_NORELEASE;
7185 archive_id = strtol(optarg, &end, 10);
7187 fprintf(stderr, "invalid archive_id: '%s'\n",
7195 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7196 argv[0], argv[optind - 1]);
7201 /* User should have specified a flag */
7205 while (optind < argc) {
7207 path = argv[optind];
7209 /* If mode == 0, this means we apply the mask. */
7210 if (mode == LFS_HSM_SET)
7211 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7213 rc = llapi_hsm_state_set(path, 0, mask, 0);
7216 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7217 path, strerror(errno = -rc));
7226 static int lfs_hsm_action(int argc, char **argv)
7231 struct hsm_current_action hca;
7232 struct hsm_extent he;
7233 enum hsm_user_action hua;
7234 enum hsm_progress_states hps;
7242 rc = llapi_hsm_current_action(path, &hca);
7244 fprintf(stderr, "can't get hsm action for %s: %s\n",
7245 path, strerror(errno = -rc));
7248 he = hca.hca_location;
7249 hua = hca.hca_action;
7250 hps = hca.hca_state;
7252 printf("%s: %s", path, hsm_user_action2name(hua));
7254 /* Skip file without action */
7255 if (hca.hca_action == HUA_NONE) {
7260 printf(" %s ", hsm_progress_state2name(hps));
7262 if ((hps == HPS_RUNNING) &&
7263 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7264 printf("(%llu bytes moved)\n",
7265 (unsigned long long)he.length);
7266 else if ((he.offset + he.length) == LUSTRE_EOF)
7267 printf("(from %llu to EOF)\n",
7268 (unsigned long long)he.offset);
7270 printf("(from %llu to %llu)\n",
7271 (unsigned long long)he.offset,
7272 (unsigned long long)(he.offset + he.length));
7274 } while (++i < argc);
7279 static int lfs_hsm_set(int argc, char **argv)
7281 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7284 static int lfs_hsm_clear(int argc, char **argv)
7286 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7290 * Check file state and return its fid, to be used by lfs_hsm_request().
7292 * \param[in] file Path to file to check
7293 * \param[in,out] fid Pointer to allocated lu_fid struct.
7294 * \param[in,out] last_dev Pointer to last device id used.
7296 * \return 0 on success.
7298 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7304 rc = lstat(file, &st);
7306 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7309 /* Checking for regular file as archiving as posix copytool
7310 * rejects archiving files other than regular files
7312 if (!S_ISREG(st.st_mode)) {
7313 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7316 /* A request should be ... */
7317 if (*last_dev != st.st_dev && *last_dev != 0) {
7318 fprintf(stderr, "All files should be "
7319 "on the same filesystem: %s\n", file);
7322 *last_dev = st.st_dev;
7324 rc = llapi_path2fid(file, fid);
7326 fprintf(stderr, "Cannot read FID of %s: %s\n",
7327 file, strerror(-rc));
7333 /* Fill an HSM HUR item with a given file name.
7335 * If mntpath is set, then the filename is actually a FID, and no
7336 * lookup on the filesystem will be performed.
7338 * \param[in] hur the user request to fill
7339 * \param[in] idx index of the item inside the HUR to fill
7340 * \param[in] mntpath mountpoint of Lustre
7341 * \param[in] fname filename (if mtnpath is NULL)
7342 * or FID (if mntpath is set)
7343 * \param[in] last_dev pointer to last device id used
7345 * \retval 0 on success
7346 * \retval CMD_HELP or a negative errno on error
7348 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7349 const char *mntpath, const char *fname,
7352 struct hsm_user_item *hui = &hur->hur_user_item[idx];
7355 hui->hui_extent.length = -1;
7357 if (mntpath != NULL) {
7360 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7364 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7369 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7373 hur->hur_request.hr_itemcount++;
7378 static int lfs_hsm_request(int argc, char **argv, int action)
7380 struct option long_opts[] = {
7381 { .val = 'a', .name = "archive", .has_arg = required_argument },
7382 { .val = 'D', .name = "data", .has_arg = required_argument },
7383 { .val = 'l', .name = "filelist", .has_arg = required_argument },
7384 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
7387 char short_opts[] = "l:D:a:m:";
7388 struct hsm_user_request *hur, *oldhur;
7393 char *filelist = NULL;
7394 char fullpath[PATH_MAX];
7395 char *opaque = NULL;
7399 int nbfile_alloc = 0;
7400 char *some_file = NULL;
7401 char *mntpath = NULL;
7407 while ((c = getopt_long(argc, argv, short_opts,
7408 long_opts, NULL)) != -1) {
7417 if (action != HUA_ARCHIVE &&
7418 action != HUA_REMOVE) {
7420 "error: -a is supported only "
7421 "when archiving or removing\n");
7424 archive_id = atoi(optarg);
7427 if (some_file == NULL) {
7429 some_file = strdup(optarg);
7435 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7436 argv[0], argv[optind - 1]);
7441 /* All remaining args are files, so we have at least nbfile */
7442 nbfile = argc - optind;
7444 if ((nbfile == 0) && (filelist == NULL))
7448 opaque_len = strlen(opaque);
7450 /* Alloc the request structure with enough place to store all files
7451 * from command line. */
7452 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7454 fprintf(stderr, "Cannot create the request: %s\n",
7458 nbfile_alloc = nbfile;
7460 hur->hur_request.hr_action = action;
7461 hur->hur_request.hr_archive_id = archive_id;
7462 hur->hur_request.hr_flags = 0;
7464 /* All remaining args are files, add them */
7465 if (nbfile != 0 && some_file == NULL)
7466 some_file = strdup(argv[optind]);
7468 for (i = 0; i < nbfile; i++) {
7469 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7475 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7477 /* If a filelist was specified, read the filelist from it. */
7478 if (filelist != NULL) {
7479 fp = fopen(filelist, "r");
7481 fprintf(stderr, "Cannot read the file list %s: %s\n",
7482 filelist, strerror(errno));
7487 while ((rc = getline(&line, &len, fp)) != -1) {
7488 /* If allocated buffer was too small, get something
7490 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7493 nbfile_alloc = nbfile_alloc * 2 + 1;
7495 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7498 fprintf(stderr, "hsm: cannot allocate "
7499 "the request: %s\n",
7506 size = hur_len(oldhur);
7508 fprintf(stderr, "hsm: cannot allocate "
7509 "%u files + %u bytes data\n",
7510 oldhur->hur_request.hr_itemcount,
7511 oldhur->hur_request.hr_data_len);
7518 memcpy(hur, oldhur, size);
7523 if (line[strlen(line) - 1] == '\n')
7524 line[strlen(line) - 1] = '\0';
7526 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7527 mntpath, line, &last_dev);
7533 if (some_file == NULL) {
7543 /* If a --data was used, add it to the request */
7544 hur->hur_request.hr_data_len = opaque_len;
7546 memcpy(hur_data(hur), opaque, opaque_len);
7548 /* Send the HSM request */
7549 if (realpath(some_file, fullpath) == NULL) {
7550 fprintf(stderr, "Could not find path '%s': %s\n",
7551 some_file, strerror(errno));
7553 rc = llapi_hsm_request(fullpath, hur);
7555 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7556 some_file, strerror(-rc));
7566 static int lfs_hsm_archive(int argc, char **argv)
7568 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7571 static int lfs_hsm_restore(int argc, char **argv)
7573 return lfs_hsm_request(argc, argv, HUA_RESTORE);
7576 static int lfs_hsm_release(int argc, char **argv)
7578 return lfs_hsm_request(argc, argv, HUA_RELEASE);
7581 static int lfs_hsm_remove(int argc, char **argv)
7583 return lfs_hsm_request(argc, argv, HUA_REMOVE);
7586 static int lfs_hsm_cancel(int argc, char **argv)
7588 return lfs_hsm_request(argc, argv, HUA_CANCEL);
7591 static int lfs_swap_layouts(int argc, char **argv)
7596 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7597 SWAP_LAYOUTS_KEEP_MTIME |
7598 SWAP_LAYOUTS_KEEP_ATIME);
7601 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7603 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7605 int lfs_get_mode(const char *string)
7607 enum lock_mode_user mode;
7609 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7610 if (lock_mode_names[mode] == NULL)
7612 if (strcmp(string, lock_mode_names[mode]) == 0)
7619 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7621 enum lu_ladvise_type advice;
7624 advice < ARRAY_SIZE(ladvise_names); advice++) {
7625 if (ladvise_names[advice] == NULL)
7627 if (strcmp(string, ladvise_names[advice]) == 0)
7631 return LU_LADVISE_INVALID;
7634 static int lfs_ladvise(int argc, char **argv)
7636 struct option long_opts[] = {
7637 { .val = 'a', .name = "advice", .has_arg = required_argument },
7638 { .val = 'b', .name = "background", .has_arg = no_argument },
7639 { .val = 'e', .name = "end", .has_arg = required_argument },
7640 { .val = 'l', .name = "length", .has_arg = required_argument },
7641 { .val = 'm', .name = "mode", .has_arg = required_argument },
7642 { .val = 's', .name = "start", .has_arg = required_argument },
7643 { .val = 'u', .name = "unset", .has_arg = no_argument },
7645 char short_opts[] = "a:be:l:m:s:u";
7650 struct llapi_lu_ladvise advice;
7651 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
7652 unsigned long long start = 0;
7653 unsigned long long end = LUSTRE_EOF;
7654 unsigned long long length = 0;
7655 unsigned long long size_units;
7656 unsigned long long flags = 0;
7660 while ((c = getopt_long(argc, argv, short_opts,
7661 long_opts, NULL)) != -1) {
7664 advice_type = lfs_get_ladvice(optarg);
7665 if (advice_type == LU_LADVISE_INVALID) {
7666 fprintf(stderr, "%s: invalid advice type "
7667 "'%s'\n", argv[0], optarg);
7668 fprintf(stderr, "Valid types:");
7670 for (advice_type = 0;
7671 advice_type < ARRAY_SIZE(ladvise_names);
7673 if (ladvise_names[advice_type] == NULL)
7675 fprintf(stderr, " %s",
7676 ladvise_names[advice_type]);
7678 fprintf(stderr, "\n");
7691 rc = llapi_parse_size(optarg, &end,
7694 fprintf(stderr, "%s: bad end offset '%s'\n",
7701 rc = llapi_parse_size(optarg, &start,
7704 fprintf(stderr, "%s: bad start offset "
7705 "'%s'\n", argv[0], optarg);
7711 rc = llapi_parse_size(optarg, &length,
7714 fprintf(stderr, "%s: bad length '%s'\n",
7720 mode = lfs_get_mode(optarg);
7722 fprintf(stderr, "%s: bad mode '%s', valid "
7723 "modes are READ or WRITE\n",
7731 fprintf(stderr, "%s: option '%s' unrecognized\n",
7732 argv[0], argv[optind - 1]);
7737 if (advice_type == LU_LADVISE_INVALID) {
7738 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7739 fprintf(stderr, "Valid types:");
7740 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7742 if (ladvise_names[advice_type] == NULL)
7744 fprintf(stderr, " %s", ladvise_names[advice_type]);
7746 fprintf(stderr, "\n");
7750 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7751 fprintf(stderr, "%s: Lock no expand advice is a per file "
7752 "descriptor advice, so when called from lfs, "
7753 "it does nothing.\n", argv[0]);
7757 if (argc <= optind) {
7758 fprintf(stderr, "%s: please give one or more file names\n",
7763 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7764 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7769 if (end == LUSTRE_EOF && length != 0)
7770 end = start + length;
7773 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7774 argv[0], start, end);
7778 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7779 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7784 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7785 fprintf(stderr, "%s: mode is required with lockahead\n",
7790 while (optind < argc) {
7793 path = argv[optind++];
7795 fd = open(path, O_RDONLY);
7797 fprintf(stderr, "%s: cannot open file '%s': %s\n",
7798 argv[0], path, strerror(errno));
7803 advice.lla_start = start;
7804 advice.lla_end = end;
7805 advice.lla_advice = advice_type;
7806 advice.lla_value1 = 0;
7807 advice.lla_value2 = 0;
7808 advice.lla_value3 = 0;
7809 advice.lla_value4 = 0;
7810 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7811 advice.lla_lockahead_mode = mode;
7812 advice.lla_peradvice_flags = flags;
7815 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7818 fprintf(stderr, "%s: cannot give advice '%s' to file "
7819 "'%s': %s\n", argv[0],
7820 ladvise_names[advice_type],
7821 path, strerror(errno));
7827 if (rc == 0 && rc2 < 0)
7833 /** The input string contains a comma delimited list of component ids and
7834 * ranges, for example "1,2-4,7".
7836 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7838 bool end_of_loop = false;
7846 while (!end_of_loop) {
7850 char *endptr = NULL;
7853 ptr = strchrnul(arg, ',');
7854 end_of_loop = *ptr == '\0';
7857 start_index = strtol(arg, &endptr, 0);
7858 if (endptr == arg) /* no data at all */
7860 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7862 if (start_index < 0)
7865 end_index = start_index;
7866 if (*endptr == '-') {
7867 end_index = strtol(endptr + 1, &endptr, 0);
7868 if (*endptr != '\0')
7870 if (end_index < start_index)
7874 for (i = start_index; i <= end_index && size > 0; i++) {
7877 /* remove duplicate */
7878 for (j = 0; j < nr; j++) {
7882 if (j == nr) { /* no duplicate */
7888 if (size == 0 && i < end_index)
7895 if (!end_of_loop && ptr != NULL)
7898 return rc < 0 ? rc : nr;
7902 * struct verify_mirror_id - Mirror id to be verified.
7903 * @mirror_id: A specified mirror id.
7904 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7906 struct verify_mirror_id {
7912 * compare_mirror_ids() - Compare mirror ids.
7913 * @layout: Mirror component list.
7914 * @cbdata: Callback data in verify_mirror_id structure.
7916 * This is a callback function called by llapi_layout_comp_iterate()
7917 * to compare the specified mirror id with the one in the current
7918 * component of @layout. If they are the same, then the specified
7919 * mirror id is valid.
7921 * Return: a negative error code on failure or
7922 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7923 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
7926 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7928 struct verify_mirror_id *mirror_id_cbdata =
7929 (struct verify_mirror_id *)cbdata;
7933 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7937 "%s: llapi_layout_mirror_id_get failed: %s.\n",
7938 progname, strerror(errno));
7942 if (mirror_id_cbdata->mirror_id == mirror_id) {
7943 mirror_id_cbdata->is_valid_id = true;
7944 return LLAPI_LAYOUT_ITER_STOP;
7947 return LLAPI_LAYOUT_ITER_CONT;
7951 * verify_mirror_ids() - Verify specified mirror ids.
7952 * @fname: Mirrored file name.
7953 * @mirror_ids: Specified mirror ids to be verified.
7954 * @ids_nr: Number of specified mirror ids.
7956 * This function verifies that specified @mirror_ids are valid
7957 * in the mirrored file @fname.
7959 * Return: 0 on success or a negative error code on failure.
7962 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7964 struct llapi_layout *layout = NULL;
7965 struct verify_mirror_id mirror_id_cbdata = { 0 };
7976 if (stat(fname, &stbuf) < 0) {
7977 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7978 progname, fname, strerror(errno));
7983 if (!S_ISREG(stbuf.st_mode)) {
7984 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7990 fd = open(fname, O_DIRECT | O_RDONLY);
7992 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7993 progname, fname, strerror(errno));
7998 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8000 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8001 progname, fname, strerror(errno));
8005 layout = llapi_layout_get_by_fd(fd, 0);
8006 if (layout == NULL) {
8007 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8008 progname, fname, strerror(errno));
8010 llapi_lease_release(fd);
8014 rc = llapi_layout_flags_get(layout, &flr_state);
8016 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8017 progname, fname, strerror(errno));
8022 flr_state &= LCM_FL_FLR_MASK;
8023 switch (flr_state) {
8026 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8027 progname, fname, llapi_layout_flags_string(flr_state));
8034 for (i = 0; i < ids_nr; i++) {
8035 mirror_id_cbdata.mirror_id = mirror_ids[i];
8036 mirror_id_cbdata.is_valid_id = false;
8038 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8043 "%s: '%s' failed to verify mirror id: %u.\n",
8044 progname, fname, mirror_ids[i]);
8048 if (!mirror_id_cbdata.is_valid_id) {
8051 "%s: '%s' invalid specified mirror id: %u.\n",
8052 progname, fname, mirror_ids[i]);
8058 llapi_layout_free(layout);
8059 llapi_lease_release(fd);
8067 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8068 __u16 *mirror_ids, int ids_nr)
8070 struct llapi_resync_comp comp_array[1024] = { { 0 } };
8071 struct llapi_layout *layout;
8081 if (stat(fname, &stbuf) < 0) {
8082 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8083 progname, fname, strerror(errno));
8087 if (!S_ISREG(stbuf.st_mode)) {
8088 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8094 fd = open(fname, O_DIRECT | O_RDWR);
8096 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8097 progname, fname, strerror(errno));
8102 layout = llapi_layout_get_by_fd(fd, 0);
8103 if (layout == NULL) {
8104 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8105 progname, fname, strerror(errno));
8110 rc = llapi_layout_flags_get(layout, &flr_state);
8112 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8113 progname, fname, strerror(errno));
8118 flr_state &= LCM_FL_FLR_MASK;
8119 if (flr_state == LCM_FL_NONE) {
8121 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8126 /* get stale component info */
8127 comp_size = llapi_mirror_find_stale(layout, comp_array,
8128 ARRAY_SIZE(comp_array),
8129 mirror_ids, ids_nr);
8130 if (comp_size <= 0) {
8135 ioc->lil_mode = LL_LEASE_WRLCK;
8136 ioc->lil_flags = LL_LEASE_RESYNC;
8137 rc = llapi_lease_set(fd, ioc);
8139 if (rc == -EALREADY)
8143 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8144 progname, fname, strerror(errno));
8148 /* get the read range [start, end) */
8149 start = comp_array[0].lrc_start;
8150 end = comp_array[0].lrc_end;
8151 for (idx = 1; idx < comp_size; idx++) {
8152 if (comp_array[idx].lrc_start < start)
8153 start = comp_array[idx].lrc_start;
8154 if (end < comp_array[idx].lrc_end)
8155 end = comp_array[idx].lrc_end;
8158 rc = llapi_lease_check(fd);
8159 if (rc != LL_LEASE_WRLCK) {
8160 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8165 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8168 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8169 progname, fname, rc);
8171 /* prepare ioc for lease put */
8172 ioc->lil_mode = LL_LEASE_UNLCK;
8173 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8175 for (idx = 0; idx < comp_size; idx++) {
8176 if (comp_array[idx].lrc_synced) {
8177 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8182 rc = llapi_lease_set(fd, ioc);
8184 if (rc == 0) /* lost lease lock */
8186 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8187 progname, fname, strerror(errno));
8191 * llapi_lease_set returns lease mode when it request to unlock
8197 llapi_layout_free(layout);
8204 static inline int lfs_mirror_resync(int argc, char **argv)
8206 struct ll_ioc_lease *ioc = NULL;
8207 __u16 mirror_ids[128] = { 0 };
8212 struct option long_opts[] = {
8213 { .val = 'o', .name = "only", .has_arg = required_argument },
8216 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8219 rc = parse_mirror_ids(mirror_ids,
8220 sizeof(mirror_ids) / sizeof(__u16),
8224 "%s: bad mirror ids '%s'.\n",
8231 fprintf(stderr, "%s: options '%s' unrecognized.\n",
8232 argv[0], argv[optind - 1]);
8238 if (argc == optind) {
8239 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8244 if (ids_nr > 0 && argc > optind + 1) {
8246 "%s: option '--only' cannot be used upon multiple files.\n",
8254 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8259 /* set the lease on the file */
8260 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8262 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8263 argv[0], strerror(errno));
8268 for (; optind < argc; optind++) {
8269 rc = lfs_mirror_resync_file(argv[optind], ioc,
8270 mirror_ids, ids_nr);
8271 /* ignore previous file's error, continue with next file */
8274 memset(ioc, 0, sizeof(__u32) * 4096);
8282 static inline int lfs_mirror_dump(int argc, char **argv)
8285 __u16 mirror_id = 0;
8286 const char *outfile = NULL;
8290 struct llapi_layout *layout;
8293 const size_t buflen = 4 << 20;
8297 struct option long_opts[] = {
8298 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
8299 { .val = 'o', .name = "outfile", .has_arg = required_argument },
8302 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8307 mirror_id = strtoul(optarg, &end, 0);
8308 if (*end != '\0' || mirror_id == 0) {
8310 "%s %s: invalid mirror ID '%s'\n",
8311 progname, argv[0], optarg);
8321 if (argc == optind) {
8322 fprintf(stderr, "%s %s: no mirrored file provided\n",
8325 } else if (argc > optind + 1) {
8326 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8330 if (mirror_id == 0) {
8331 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8336 /* open mirror file */
8337 fname = argv[optind];
8339 if (stat(fname, &stbuf) < 0) {
8340 fprintf(stderr, "%s %s: cannot stat file '%s': %s.\n",
8341 progname, argv[0], fname, strerror(errno));
8345 if (!S_ISREG(stbuf.st_mode)) {
8346 fprintf(stderr, "%s %s: '%s' is not a regular file.\n",
8347 progname, argv[0], fname);
8351 fd = open(fname, O_DIRECT | O_RDONLY);
8353 fprintf(stderr, "%s %s: cannot open '%s': %s.\n",
8354 progname, argv[0], fname, strerror(errno));
8358 /* verify mirror id */
8359 layout = llapi_layout_get_by_fd(fd, 0);
8360 if (layout == NULL) {
8361 fprintf(stderr, "%s %s: could not get layout of file '%s'.\n",
8362 progname, argv[0], fname);
8367 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8369 fprintf(stderr, "%s %s: failed to iterate layout of '%s'.\n",
8370 progname, argv[0], fname);
8371 llapi_layout_free(layout);
8373 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8375 "%s %s: file '%s' does not contain mirror with ID %u\n",
8376 progname, argv[0], fname, mirror_id);
8377 llapi_layout_free(layout);
8382 llapi_layout_free(layout);
8384 /* open output file */
8386 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8388 fprintf(stderr, "%s %s: cannot create file '%s':%s.\n",
8389 progname, argv[0], outfile, strerror(errno));
8394 outfd = STDOUT_FILENO;
8397 /* allocate buffer */
8398 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8400 fprintf(stderr, "%s %s: posix_memalign returns %d.\n",
8401 progname, argv[0], rc);
8408 ssize_t written = 0;
8410 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8411 if (bytes_read < 0) {
8414 "%s %s: fail to read data from mirror %u:%s.\n",
8415 progname, argv[0], mirror_id, strerror(-rc));
8420 if (bytes_read == 0)
8423 while (written < bytes_read) {
8426 written2 = write(outfd, buf + written,
8427 bytes_read - written);
8430 "%s %s: fail to write %s:%s.\n",
8431 progname, argv[0], outfile ? : "STDOUT",
8436 written += written2;
8439 if (written != bytes_read) {
8441 "%s %s: written %ld bytes does not match with %ld read.\n",
8442 progname, argv[0], written, bytes_read);
8465 * struct verify_chunk - Mirror chunk to be verified.
8466 * @chunk: [start, end) of the chunk.
8467 * @mirror_count: Number of mirror ids in @mirror_id array.
8468 * @mirror_id: Array of valid mirror ids that cover the chunk.
8470 struct verify_chunk {
8471 struct lu_extent chunk;
8472 unsigned int mirror_count;
8473 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
8477 * print_chunks() - Print chunk information.
8478 * @fname: Mirrored file name.
8479 * @chunks: Array of chunks.
8480 * @chunk_count: Number of chunks in @chunks array.
8482 * This function prints [start, end) of each chunk in @chunks
8483 * for mirrored file @fname, and also prints the valid mirror ids
8484 * that cover the chunk.
8489 void print_chunks(const char *fname, struct verify_chunk *chunks,
8495 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
8496 for (i = 0; i < chunk_count; i++) {
8497 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
8499 if (chunks[i].mirror_count == 0)
8500 fprintf(stdout, "\t[");
8502 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
8503 for (j = 1; j < chunks[i].mirror_count; j++)
8504 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
8506 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
8508 fprintf(stdout, "\n");
8512 * print_checksums() - Print CRC-32 checksum values.
8513 * @chunk: A chunk and its corresponding valid mirror ids.
8514 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
8516 * This function prints CRC-32 checksum values on @chunk for
8517 * each valid mirror that covers it.
8522 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
8527 "CRC-32 checksum value for chunk "DEXT":\n",
8528 PEXT(&chunk->chunk));
8529 for (i = 0; i < chunk->mirror_count; i++)
8530 fprintf(stdout, "Mirror %u:\t%#lx\n",
8531 chunk->mirror_id[i], crc[i]);
8532 fprintf(stdout, "\n");
8536 * filter_mirror_id() - Filter specified mirror ids.
8537 * @chunks: Array of chunks.
8538 * @chunk_count: Number of chunks in @chunks array.
8539 * @mirror_ids: Specified mirror ids to be verified.
8540 * @ids_nr: Number of specified mirror ids.
8542 * This function scans valid mirror ids that cover each chunk in @chunks
8543 * and filters specified mirror ids.
8548 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
8549 __u16 *mirror_ids, int ids_nr)
8554 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8555 unsigned int valid_count = 0;
8557 for (i = 0; i < chunk_count; i++) {
8558 if (chunks[i].mirror_count == 0)
8562 for (j = 0; j < ids_nr; j++) {
8563 for (k = 0; k < chunks[i].mirror_count; k++) {
8564 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
8565 valid_id[valid_count] = mirror_ids[j];
8572 memcpy(chunks[i].mirror_id, valid_id,
8573 sizeof(__u16) * valid_count);
8574 chunks[i].mirror_count = valid_count;
8579 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
8580 * @layout: Mirror component list.
8581 * @chunks: Array of chunks.
8582 * @chunks_size: Array size of @chunks.
8584 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
8585 * to find out chunk segments and store them in @chunks array.
8587 * The @mirror_id array in each element of @chunks will store the valid
8588 * mirror ids that cover the chunk. If a mirror component covering the
8589 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
8590 * will not be stored into the @mirror_id array, and the chunk for that
8591 * mirror will not be verified.
8593 * The @mirror_count in each element of @chunks will store the number of
8594 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
8595 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
8596 * indicates the chunk is valid in only one mirror. In both cases, the
8597 * chunk will not be verified.
8599 * Here is an example:
8602 * +------+-------------+--------------------+
8604 * +------+------+------+------+-------------+
8605 * | | S | S | | mirror2
8606 * +-------------+------+------+-------------+
8608 * prepared @chunks array will contain 5 elements:
8609 * (([0, 1M), [1, 2], 2),
8610 * ([1M, 2M), [1, 2], 2),
8611 * ([2M, 3M), [1], 1),
8612 * ([3M, 4M], [], 0),
8613 * ([4M, EOF), [2], 1))
8615 * Return: the actual array size of @chunks on success
8616 * or a negative error code on failure.
8619 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
8620 struct verify_chunk *chunks,
8631 memset(chunks, 0, sizeof(*chunks) * chunks_size);
8634 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
8637 "%s: move to the first layout component: %s.\n",
8638 progname, strerror(errno));
8644 chunks[idx].chunk.e_end = LUSTRE_EOF;
8646 rc = llapi_layout_comp_extent_get(layout, &start, &end);
8649 "%s: llapi_layout_comp_extent_get failed: %s.\n",
8650 progname, strerror(errno));
8654 if (start > chunks[idx].chunk.e_start ||
8655 end <= chunks[idx].chunk.e_start)
8658 if (end < chunks[idx].chunk.e_end)
8659 chunks[idx].chunk.e_end = end;
8661 rc = llapi_layout_comp_flags_get(layout, &flags);
8664 "%s: llapi_layout_comp_flags_get failed: %s.\n",
8665 progname, strerror(errno));
8669 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8672 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8675 "%s: llapi_layout_mirror_id_get failed: %s.\n",
8676 progname, strerror(errno));
8680 chunks[idx].mirror_id[i] = mirror_id;
8682 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8684 "%s: mirror_id array is too small.\n",
8691 rc = llapi_layout_comp_use(layout,
8692 LLAPI_LAYOUT_COMP_USE_NEXT);
8695 "%s: move to the next layout component: %s.\n",
8696 progname, strerror(errno));
8699 } /* loop through all components */
8701 chunks[idx].mirror_count = i;
8703 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8707 if (idx >= chunks_size) {
8708 fprintf(stderr, "%s: chunks array is too small.\n",
8714 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
8718 return rc < 0 ? rc : idx + 1;
8722 * lfs_mirror_verify_chunk() - Verify a chunk.
8723 * @fd: File descriptor of the mirrored file.
8724 * @file_size: Size of the mirrored file.
8725 * @chunk: A chunk and its corresponding valid mirror ids.
8726 * @verbose: Verbose mode.
8728 * This function verifies a @chunk contains exactly the same data
8729 * ammong the mirrors that cover it.
8731 * If @verbose is specified, then the function will print where the
8732 * differences are if the data do not match. Otherwise, it will
8733 * just return an error in that case.
8735 * Return: 0 on success or a negative error code on failure.
8738 int lfs_mirror_verify_chunk(int fd, size_t file_size,
8739 struct verify_chunk *chunk, int verbose)
8741 const size_t buflen = 4 * 1024 * 1024; /* 4M */
8743 size_t page_size = sysconf(_SC_PAGESIZE);
8749 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8756 rc = posix_memalign(&buf, page_size, buflen);
8757 if (rc) /* error code is returned directly */
8761 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8762 PEXT(&chunk->chunk));
8763 for (i = 0; i < chunk->mirror_count; i++)
8764 fprintf(stdout, " %u", chunk->mirror_id[i]);
8765 fprintf(stdout, "\n");
8769 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
8770 pos = chunk->chunk.e_start;
8771 while (bytes_done < count) {
8772 /* compute initial CRC-32 checksum */
8773 crc = crc32(0L, Z_NULL, 0);
8774 memset(crc_array, 0, sizeof(crc_array));
8777 for (i = 0; i < chunk->mirror_count; i++) {
8778 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
8780 if (bytes_read < 0) {
8783 "%s: failed to read data from mirror %u: %s.\n",
8784 progname, chunk->mirror_id[i],
8789 /* compute new CRC-32 checksum */
8790 crc_array[i] = crc32(crc, buf, bytes_read);
8794 print_checksums(chunk, crc_array);
8796 /* compare CRC-32 checksum values */
8797 for (i = 1; i < chunk->mirror_count; i++) {
8798 if (crc_array[i] != crc_array[0]) {
8804 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
8805 progname, PEXT(&chunk->chunk),
8806 chunk->mirror_id[0],
8807 chunk->mirror_id[i]);
8812 bytes_done += bytes_read;
8815 if (verbose > 1 && rc == 0) {
8816 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8817 PEXT(&chunk->chunk));
8818 for (i = 0; i < chunk->mirror_count; i++)
8819 fprintf(stdout, " %u", chunk->mirror_id[i]);
8820 fprintf(stdout, " PASS\n\n");
8829 * lfs_mirror_verify_file() - Verify a mirrored file.
8830 * @fname: Mirrored file name.
8831 * @mirror_ids: Specified mirror ids to be verified.
8832 * @ids_nr: Number of specified mirror ids.
8833 * @verbose: Verbose mode.
8835 * This function verifies that each SYNC mirror of a mirrored file
8836 * specified by @fname contains exactly the same data.
8838 * If @mirror_ids is specified, then the function will verify the
8839 * mirrors specified by @mirror_ids contain exactly the same data.
8841 * If @verbose is specified, then the function will print where the
8842 * differences are if the data do not match. Otherwise, it will
8843 * just return an error in that case.
8845 * Return: 0 on success or a negative error code on failure.
8848 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8851 struct verify_chunk chunks_array[1024] = { };
8852 struct llapi_layout *layout = NULL;
8856 int chunk_count = 0;
8862 if (stat(fname, &stbuf) < 0) {
8863 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8864 progname, fname, strerror(errno));
8869 if (!S_ISREG(stbuf.st_mode)) {
8870 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8876 if (stbuf.st_size == 0) {
8878 fprintf(stdout, "%s: '%s' file size is 0.\n",
8884 fd = open(fname, O_DIRECT | O_RDONLY);
8886 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8887 progname, fname, strerror(errno));
8892 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8894 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8895 progname, fname, strerror(errno));
8899 layout = llapi_layout_get_by_fd(fd, 0);
8900 if (layout == NULL) {
8901 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8902 progname, fname, strerror(errno));
8904 llapi_lease_release(fd);
8908 rc = llapi_layout_flags_get(layout, &flr_state);
8910 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8911 progname, fname, strerror(errno));
8916 flr_state &= LCM_FL_FLR_MASK;
8917 switch (flr_state) {
8920 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8921 progname, fname, llapi_layout_flags_string(flr_state));
8927 /* find out mirror chunks to be verified */
8928 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8929 ARRAY_SIZE(chunks_array));
8930 if (chunk_count < 0) {
8936 /* filter specified mirror ids */
8937 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8940 print_chunks(fname, chunks_array, chunk_count);
8942 for (idx = 0; idx < chunk_count; idx++) {
8943 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8946 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8948 PEXT(&chunks_array[idx].chunk),
8949 (unsigned long long)stbuf.st_size);
8953 if (chunks_array[idx].mirror_count == 0) {
8955 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8957 PEXT(&chunks_array[idx].chunk));
8959 fprintf(stderr, "skipped\n");
8963 fprintf(stderr, "failed\n");
8967 if (chunks_array[idx].mirror_count == 1) {
8970 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8972 PEXT(&chunks_array[idx].chunk),
8973 chunks_array[idx].mirror_id[0]);
8977 rc = llapi_lease_check(fd);
8978 if (rc != LL_LEASE_RDLCK) {
8979 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8984 /* verify one chunk */
8985 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8986 &chunks_array[idx], verbose);
9000 llapi_layout_free(layout);
9001 llapi_lease_release(fd);
9009 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9010 * @argc: The count of lfs mirror verify command line arguments.
9011 * @argv: Array of strings for lfs mirror verify command line arguments.
9013 * This function parses lfs mirror verify command and verifies the
9014 * specified mirrored file(s).
9016 * Return: 0 on success or a negative error code on failure.
9018 static inline int lfs_mirror_verify(int argc, char **argv)
9020 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9028 struct option long_opts[] = {
9029 { .val = 'o', .name = "only", .has_arg = required_argument },
9030 { .val = 'v', .name = "verbose", .has_arg = no_argument },
9033 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9035 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9038 rc = parse_mirror_ids(mirror_ids,
9039 ARRAY_SIZE(mirror_ids),
9043 "%s: bad mirror ids '%s'.\n",
9050 "%s: at least 2 mirror ids needed with '--only' option.\n",
9060 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9061 progname, argv[optind - 1]);
9067 if (argc == optind) {
9068 fprintf(stderr, "%s: no file name given.\n", progname);
9073 if (ids_nr > 0 && argc > optind + 1) {
9075 "%s: '--only' cannot be used upon multiple files.\n",
9083 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9089 for (; optind < argc; optind++) {
9090 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9100 * lfs_mirror() - Parse and execute lfs mirror commands.
9101 * @argc: The count of lfs mirror command line arguments.
9102 * @argv: Array of strings for lfs mirror command line arguments.
9104 * This function parses lfs mirror commands and performs the
9105 * corresponding functions specified in mirror_cmdlist[].
9107 * Return: 0 on success or an error code on failure.
9109 static int lfs_mirror(int argc, char **argv)
9116 Parser_init("lfs-mirror > ", mirror_cmdlist);
9118 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9120 program_invocation_short_name = cmd;
9122 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9124 rc = Parser_commands();
9126 return rc < 0 ? -rc : rc;
9129 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9131 #if __BYTE_ORDER == __BIG_ENDIAN
9132 __swab16s(&attrs->lsa_valid);
9133 __swab64s(&attrs->lsa_size);
9134 __swab64s(&attrs->lsa_blocks);
9140 LFS_SOM_BLOCKS = 0x2,
9141 LFS_SOM_FLAGS = 0x4,
9142 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9146 static int lfs_getsom(int argc, char **argv)
9149 struct lustre_som_attrs *attrs;
9150 char buf[sizeof(*attrs) + 64];
9151 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9154 while ((c = getopt(argc, argv, "sbf")) != -1) {
9157 type = LFS_SOM_SIZE;
9160 type = LFS_SOM_BLOCKS;
9163 type = LFS_SOM_FLAGS;
9166 fprintf(stderr, "%s: invalid option '%c'\n",
9176 fprintf(stderr, "%s: %s\n",
9177 progname, argc == 0 ? "miss file target" :
9178 "input more than 2 files");
9183 attrs = (void *)buf;
9184 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9186 fprintf(stderr, "%s failed to get som xattr: %s\n", argv[0],
9191 lustre_som_swab(attrs);
9194 case LFS_SOM_ATTR_ALL:
9195 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9196 path, attrs->lsa_size, attrs->lsa_blocks,
9200 printf("%llu\n", attrs->lsa_size);
9202 case LFS_SOM_BLOCKS:
9203 printf("%llu\n", attrs->lsa_blocks);
9206 printf("%x\n", attrs->lsa_valid);
9209 fprintf(stderr, "%s: unknown option\n", progname);
9217 * lfs_mirror_list_commands() - List lfs mirror commands.
9218 * @argc: The count of command line arguments.
9219 * @argv: Array of strings for command line arguments.
9221 * This function lists lfs mirror commands defined in mirror_cmdlist[].
9223 * Return: 0 on success.
9225 static int lfs_mirror_list_commands(int argc, char **argv)
9227 char buffer[81] = "";
9229 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9235 static int lfs_list_commands(int argc, char **argv)
9237 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9239 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9244 int main(int argc, char **argv)
9248 /* Ensure that liblustreapi constructor has run */
9249 if (!llapi_liblustreapi_initialized())
9250 fprintf(stderr, "liblustreapi was not properly initialized\n");
9255 Parser_init("lfs > ", cmdlist);
9257 progname = program_invocation_short_name; /* Used in error messages */
9259 llapi_set_command_name(argv[1]);
9260 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9261 llapi_clear_command_name();
9263 rc = Parser_commands();
9266 return rc < 0 ? -rc : rc;
9269 #ifdef _LUSTRE_IDL_H_
9270 /* Everything we need here should be included by lustreapi.h. */
9271 # error "lfs should not depend on lustre_idl.h"
9272 #endif /* _LUSTRE_IDL_H_ */