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/
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Robert Read <rread@clusterfs.com>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
57 #include <sys/types.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.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>
78 #include <lnetconfig/cyaml.h>
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_unlink_foreign(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_rmfid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_getsom(int argc, char **argv);
119 static int lfs_heat_get(int argc, char **argv);
120 static int lfs_heat_set(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
129 static int lfs_pcc_attach(int argc, char **argv);
130 static int lfs_pcc_attach_fid(int argc, char **argv);
131 static int lfs_pcc_detach(int argc, char **argv);
132 static int lfs_pcc_detach_fid(int argc, char **argv);
133 static int lfs_pcc_state(int argc, char **argv);
134 static int lfs_pcc(int argc, char **argv);
135 static int lfs_pcc_list_commands(int argc, char **argv);
136 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
137 __u64 migration_flags,
138 struct llapi_stripe_param *param,
139 struct llapi_layout *layout);
141 struct pool_to_id_cbdata {
145 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
146 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
148 enum setstripe_origin {
158 static int lfs_setstripe_internal(int argc, char **argv,
159 enum setstripe_origin opc);
161 static inline int lfs_setstripe(int argc, char **argv)
163 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
166 static inline int lfs_setstripe_migrate(int argc, char **argv)
168 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
171 static inline int lfs_mirror_create(int argc, char **argv)
173 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
176 static inline int lfs_mirror_extend(int argc, char **argv)
178 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
181 static inline int lfs_mirror_split(int argc, char **argv)
183 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
186 static inline int lfs_mirror_delete(int argc, char **argv)
188 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
191 /* Setstripe and migrate share mostly the same parameters */
192 #define SSM_CMD_COMMON(cmd) \
193 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
194 " [--stripe-count|-c <stripe_count>]\n" \
195 " [--overstripe-count|-C <stripe_count>]\n" \
196 " [--stripe-index|-i <start_ost_idx>]\n" \
197 " [--stripe-size|-S <stripe_size>]\n" \
198 " [--extension-size|--ext-size|-z]\n" \
199 " [--layout|-L <pattern>]\n" \
200 " [--mirror-count|-N[mirror_count]]\n" \
201 " [--ost|-o <ost_indices>]\n" \
202 " [--pool|-p <pool_name>]\n" \
203 " [--yaml|-y <yaml_template_file>]\n" \
204 " [--copy=<lustre_src>]\n"
206 #define SSM_HELP_COMMON \
207 "\tstripe_count: Number of OSTs to stripe on (0=fs default, -1 all)\n" \
208 "\t Using -C instead of -c allows overstriping, which\n" \
209 "\t will place more than one stripe per OST if\n" \
210 "\t stripe_count is greater than the number of OSTs.\n" \
211 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
212 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
213 "\t Optional K, M, or G suffix (for KB, MB, GB\n" \
214 "\t respectively). Must be a multiple of 64KiB.\n" \
215 "\textension_size:\n" \
216 "\t Number of bytes the previous component is extended\n" \
217 "\t each time. Optional K, M, or G suffix (for KB,\n" \
218 "\t MB, GB respectively)\n" \
219 "\tpool_name: Name of OST pool to use (default none)\n" \
220 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n" \
221 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
222 "\t Indices be specified in a format of:\n" \
223 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
225 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
226 "\t If --pool is set with --ost then the OSTs\n" \
227 "\t must be the members of the pool.\n" \
228 "\tcomp_end: Extent end of component, start after previous end.\n" \
229 "\t Optional K, M, or G suffix (for KiB, MiB, GiB), or\n" \
230 "\t -1 or 'eof' for max file size). Must be a multiple\n" \
231 "\t of stripe_size and a multiple of 64KiB.\n" \
232 "\tyaml_template_file:\n" \
233 "\t YAML layout template file, can't be used with -c,\n" \
234 "\t -i, -S, -p, -o, or -E arguments.\n" \
235 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
236 "\t another lustre file or directory, can't used with\n" \
237 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
239 #define MIRROR_CREATE_HELP \
240 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
241 "\t setstripe layout options\n" \
242 "\t It defaults to 1 if not specified; if specified,\n" \
243 "\t it must follow the option without a space.\n" \
244 "\t The option can also be repeated multiple times to\n" \
245 "\t separate mirrors that have different layouts.\n" \
246 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
247 "\t It can be a plain layout or a composite layout.\n" \
248 "\t If not specified, the stripe options inherited\n" \
249 "\t from the previous component will be used.\n" \
250 "\tflags: set flags to the component of the current mirror.\n" \
251 "\t Only \"prefer\" flag is supported so far.\n"
253 #define MIRROR_EXTEND_HELP \
255 "\tvictim_file: The layout of victim_file will be split and used\n" \
256 "\t as a mirror added to the mirrored file.\n" \
257 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
258 "\t from victim file(s) in case the victim file(s)\n" \
259 "\t contains the same data as the original mirrored\n" \
262 #define MIRROR_EXTEND_USAGE \
263 " {--mirror-count|-N[mirror_count]}\n" \
264 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
267 #define SETSTRIPE_USAGE \
268 SSM_CMD_COMMON("setstripe") \
269 MIRROR_EXTEND_USAGE \
270 " <directory|filename>\n" \
274 #define MIGRATE_USAGE \
275 SSM_CMD_COMMON("migrate ") \
276 " [--block|-b] [--non-block|-n]\n" \
277 " [--non-direct|-D] [--verbose|-v]\n" \
281 "\tblock: Block file access during data migration (default)\n" \
282 "\tnon-block: Abort migrations if concurrent access is detected\n" \
283 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
284 "\tverbose: Print each filename as it is migrated\n" \
286 #define SETDIRSTRIPE_USAGE \
287 " [--mdt-count|-c stripe_count>\n" \
288 " [--mdt-hash|-H mdt_hash]\n" \
289 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
290 " [--default|-D] [--mode|-o mode]\n" \
291 " [--max-inherit|-X max_inherit]\n" \
292 " [--max-inherit-rr max_inherit_rr] <dir>\n" \
293 "\tstripe_count: stripe count of the striped directory\n" \
294 "\tmdt_index: MDT index of first stripe\n" \
295 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
296 " crush CRUSH hash algorithm (default)\n" \
297 " fnv_1a_64 FNV-1a hash algorithm\n" \
298 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
299 "\tdefault_stripe: set default dirstripe of the directory\n" \
300 "\tmode: the file access permission of the directory (octal)\n" \
301 "To create dir with a foreign (free format) layout :\n" \
302 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
303 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
304 "\tmode: the mode of the directory\n" \
305 "\tforeign_type: none or symlink\n"
308 * command_t mirror_cmdlist - lfs mirror commands.
310 command_t mirror_cmdlist[] = {
311 { .pc_name = "create", .pc_func = lfs_mirror_create,
312 .pc_help = "Create a mirrored file.\n"
313 "usage: lfs mirror create "
314 "<--mirror-count|-N[mirror_count]> "
315 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
316 MIRROR_CREATE_HELP },
317 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
318 .pc_help = "Delete a mirror from a file.\n"
319 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
320 "\t --component-id|--comp-id|-I <comp_id> |\n"
321 "\t -p <pool>} <mirrored_file> ...\n"
323 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
324 .pc_help = "Extend a mirrored file.\n"
325 "usage: lfs mirror extend "
326 "<--mirror-count|-N[mirror_count]> [--no-verify] "
327 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
328 MIRROR_EXTEND_HELP },
329 { .pc_name = "split", .pc_func = lfs_mirror_split,
330 .pc_help = "Split a mirrored file.\n"
331 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
332 "\t --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
333 "\t [-f <new_file>] <mirrored_file> ...\n"
334 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
335 "\t can be fetched by lfs getstripe command.\n"
336 "\tcomp_id: Unique component ID within a mirror.\n"
337 "\tpool: Components on specified pool.\n"
338 "\tnew_file: This option indicates the layout of the split\n"
339 "\t mirror will be stored into. If not specified,\n"
340 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
341 "\t will be used.\n" },
342 { .pc_name = "read", .pc_func = lfs_mirror_read,
343 .pc_help = "Read the content of a specified mirror of a file.\n"
344 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
345 "[--outfile|-o <output_file>] <mirrored_file>\n" },
346 { .pc_name = "write", .pc_func = lfs_mirror_write,
347 .pc_help = "Write to a specified mirror of a file.\n"
348 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
349 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
350 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
351 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
352 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
353 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
354 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
355 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
356 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
357 "<mirrored file> [<mirrored file2>...]\n"},
358 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
359 .pc_help = "Verify mirrored file(s).\n"
360 "usage: lfs mirror verify "
361 "[--only <mirror_id,mirror_id2[,...]>] "
362 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
363 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
364 .pc_help = "list commands supported by lfs mirror"},
365 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
366 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
367 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
372 * command_t pcc_cmdlist - lfs pcc commands.
374 command_t pcc_cmdlist[] = {
375 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
376 .pc_help = "Attach given files to the Persistent Client Cache.\n"
377 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
378 "\t-i: archive id for RW-PCC\n" },
379 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
380 .pc_help = "Attach given files into PCC by FID(s).\n"
381 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
383 "\t-i: archive id for RW-PCC\n"
384 "\t-m: Lustre mount point\n" },
385 { .pc_name = "state", .pc_func = lfs_pcc_state,
386 .pc_help = "Display the PCC state for given files.\n"
387 "usage: lfs pcc state <file> ...\n" },
388 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
389 .pc_help = "Detach given files from the Persistent Client Cache.\n"
390 "usage: lfs pcc detach <file> ...\n" },
391 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
392 .pc_help = "Detach given files from PCC by FID(s).\n"
393 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
394 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
395 .pc_help = "list commands supported by lfs pcc"},
396 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
397 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
398 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
402 /* all available commands */
403 command_t cmdlist[] = {
404 {"setstripe", lfs_setstripe, 0,
405 "To create a file with specified striping/composite layout, or\n"
406 "create/replace the default layout on an existing directory:\n"
407 SSM_CMD_COMMON("setstripe")
409 " <directory|filename>\n"
411 "To add component(s) to an existing composite file:\n"
412 SSM_CMD_COMMON("setstripe --component-add")
414 "To totally delete the default striping from an existing directory:\n"
415 "usage: setstripe [--delete|-d] <directory>\n"
417 "To create a mirrored file or set s default mirror layout on a directory:\n"
418 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
420 "To delete the last component(s) from an existing composite file\n"
421 "(note that this will also delete any data in those components):\n"
422 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
423 " [--component-flags|-F <comp_flags>]\n"
425 "\tcomp_id: Unique component ID to delete\n"
426 "\tcomp_flags: 'init' indicating all instantiated components\n"
427 "\t '^init' indicating all uninstantiated components\n"
428 "\t-I and -F cannot be specified at the same time\n"
430 "To set or clear flags on a specific component\n"
431 "(note that this command can only be applied to mirrored files:\n"
432 "usage: setstripe --comp-set {-I comp_id|--comp-flags=comp_flags}\n"
435 "To create a file with a foreign (free format) layout:\n"
436 "usage: setstripe --foreign[=<foreign_type>]\n"
437 " --xattr|-x <layout_string> [--flags <hex>]\n"
438 " [--mode <mode>] <filename>\n"},
439 {"getstripe", lfs_getstripe, 0,
440 "To list the layout pattern for a given file or files in a\n"
441 "directory or recursively for all files in a directory tree.\n"
442 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
443 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
444 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
445 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
446 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
447 " [--component-id[=comp_id]|-I[comp_id]]\n"
448 " [--component-flags[=comp_flags]]\n"
449 " [--component-count]\n"
450 " [--extension-size|--ext-size|-z]\n"
451 " [--component-start[=[+-]comp_start]]\n"
452 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
453 " [[!] --mirror-index=[+-]<index> |\n"
454 " [!] --mirror-id=[+-]<id>] [--mirror-count|-N]\n"
455 " <directory|filename> ..."},
456 {"setdirstripe", lfs_setdirstripe, 0,
457 "Create striped directory on specified MDT, same as mkdir.\n"
458 "May be restricted to root or group users, depending on settings.\n"
459 "usage: setdirstripe [OPTION] <directory>\n"
461 {"getdirstripe", lfs_getdirstripe, 0,
462 "To list the layout pattern info for a given directory\n"
463 "or recursively for all directories in a directory tree.\n"
464 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
465 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
466 " [--recursive|-r] [--yaml|-y]\n"
467 " [--verbose|-v] [--default|-D]\n"
468 " [--max-inherit|-X]\n"
469 " [--max-inherit-rr] <dir> ..."},
470 {"mkdir", lfs_setdirstripe, 0,
471 "Create striped directory on specified MDT, same as setdirstripe.\n"
472 "usage: mkdir [OPTION] <directory>\n"
474 {"rm_entry", lfs_rmentry, 0,
475 "To remove the name entry of the remote directory. Note: This\n"
476 "command will only delete the name entry, i.e. the remote directory\n"
477 "will become inaccessable after this command. This can only be done\n"
478 "by the administrator\n"
479 "usage: rm_entry <dir>\n"},
480 {"unlink_foreign", lfs_unlink_foreign, 0,
481 "To remove the foreign file/dir.\n"
482 "Note: This is for files/dirs prevented to be removed using\n"
483 "unlink/rmdir, but works also for regular ones\n"
484 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
485 {"pool_list", lfs_poollist, 0,
486 "List pools or pool OSTs\n"
487 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
488 {"find", lfs_find, 0,
489 "find files matching given attributes recursively in directory tree.\n"
490 "usage: find <directory|filename> ...\n"
491 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
492 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
493 " [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
494 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
495 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
496 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
497 " [[!] --perm [/-]mode] [[!] --pool <pool>] [--print|-P]\n"
498 " [--print0|-0] [[!] --projid <projid>]\n"
499 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
500 " [[!] --stripe-count|-c [+-]<stripes>]\n"
501 " [[!] --stripe-index|-i <index,...>]\n"
502 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
503 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
504 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
505 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
506 " [[!] --layout|-L released,raid0,mdt]\n"
507 " [[!] --foreign[=<foreign_type>]]\n"
508 " [[!] --component-count [+-]<comp_cnt>]\n"
509 " [[!] --component-start [+-]N[kMGTPE]]\n"
510 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
511 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
512 " [[!] --mirror-count|-N [+-]<n>]\n"
513 " [[!] --mirror-state <[^]state>]\n"
514 " [[!] --mdt-count|-T [+-]<stripes>]\n"
515 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
516 " [[!] --mdt-index|-m <uuid|index,...>]\n"
517 "\t !: used before an option indicates 'NOT' requested attribute\n"
518 "\t -: used before a value indicates less than requested value\n"
519 "\t +: used before a value indicates more than requested value\n"
520 "\t ^: used before a flag indicates to exclude it\n"},
521 {"check", lfs_check, 0,
522 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
523 "or all the servers (MGTs, MDTs and OSTs).\n"
524 "usage: check <mgts|osts|mdts|all>"},
525 {"osts", lfs_osts, 0, "list OSTs connected to client "
526 "[for specified path only]\n" "usage: osts [path]"},
527 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
528 "[for specified path only]\n" "usage: mdts [path]"},
530 "report filesystem disk space usage or inodes usage "
531 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
532 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
533 " [--pool|-p <fsname>[.<pool>]] [path]"},
534 {"getname", lfs_getname, 0,
535 "list instances and specified mount points [for specified path only]\n"
536 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
537 #ifdef HAVE_SYS_QUOTA_H
538 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
539 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
540 " -b <block-softlimit> -B <block-hardlimit>\n"
541 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
542 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
543 " [--block-softlimit <block-softlimit>]\n"
544 " [--block-hardlimit <block-hardlimit>]\n"
545 " [--inode-softlimit <inode-softlimit>]\n"
546 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
547 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
548 " [--block-grace 'notify'|<block-grace>]\n"
549 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
550 " setquota <-U|-G|-P>\n"
551 " -b <block-softlimit> -B <block-hardlimit>\n"
552 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
553 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
554 " [--block-softlimit <block-softlimit>]\n"
555 " [--block-hardlimit <block-hardlimit>]\n"
556 " [--inode-softlimit <inode-softlimit>]\n"
557 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
558 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
560 " -b can be used instead of --block-softlimit/--block-grace\n"
561 " -B can be used instead of --block-hardlimit\n"
562 " -i can be used instead of --inode-softlimit/--inode-grace\n"
563 " -I can be used instead of --inode-hardlimit\n"
564 " -d can be used instead of --default\n\n"
565 "Note: The total quota space will be split into many qunits and\n"
566 " balanced over all server targets, the minimal qunit size is\n"
567 " 1M bytes for block space and 1K inodes for inode space.\n\n"
568 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
569 " Quota space rebalancing process will stop when this mininum\n"
570 " value is reached. As a result, quota exceeded can be returned\n"
571 " while many targets still have 1MB or 1K inodes of spare\n"
573 " When setting the grace time, 'notify' can be used as grace to\n"
574 " be notified after the quota is over soft limit but prevents\n"
575 " the soft limit from becoming the hard limit."},
576 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
577 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
579 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
580 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
581 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
582 {"project", lfs_project, 0,
583 "Change or list project attribute for specified file or directory.\n"
584 "usage: project [-d|-r] <file|directory...>\n"
585 " list project ID and flags on file(s) or directories\n"
586 " project [-p id] [-s] [-r] <file|directory...>\n"
587 " set project ID and/or inherit flag for specified file(s) or directories\n"
588 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
589 " check project ID and flags on file(s) or directories, print outliers\n"
590 " project -C [-r] [-k] <file|directory...>\n"
591 " clear the project inherit flag and ID on the file or directory\n"
594 {"flushctx", lfs_flushctx, 0,
595 "Flush security context for current user.\n"
596 "usage: flushctx [-k] [-r] [mountpoint...]"},
597 {"changelog", lfs_changelog, 0,
598 "Show the metadata changes on an MDT."
599 "\nusage: changelog <mdtname> [startrec [endrec]]"},
600 {"changelog_clear", lfs_changelog_clear, 0,
601 "Indicate that old changelog records up to <endrec> are no longer of "
602 "interest to consumer <id>, allowing the system to free up space.\n"
603 "An <endrec> of 0 means all records.\n"
604 "usage: changelog_clear <mdtname> <id> <endrec>"},
605 {"fid2path", lfs_fid2path, 0,
606 "Resolve the full path(s) for given FID(s). For a specific hardlink "
607 "specify link number <linkno>.\n"
608 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
609 "<fsname|root> <fid>..."},
610 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
611 "usage: path2fid [--parents] <path> ..."},
612 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
613 "usage: rmfid <fsname|rootpath> <fid> ..."},
614 {"data_version", lfs_data_version, 0, "Display file data version for "
615 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
616 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
617 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
618 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
619 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
620 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
621 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
623 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
624 "[--archived] [--lost] <file> ..."},
625 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
626 "given files.\n" "usage: hsm_action <file> ..."},
627 {"hsm_archive", lfs_hsm_archive, 0,
628 "Archive file to external storage.\n"
629 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
631 {"hsm_restore", lfs_hsm_restore, 0,
632 "Restore file from external storage.\n"
633 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
634 {"hsm_release", lfs_hsm_release, 0,
635 "Release files from Lustre.\n"
636 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
637 {"hsm_remove", lfs_hsm_remove, 0,
638 "Remove file copy from external storage.\n"
639 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
641 " (FILE [FILE ...] | "
642 "--mntpath MOUNTPATH FID [FID ...])\n"
644 "Note: To remove an archived copy of a file already deleted from a "
646 "--mntpath option and a list of FIDs must be specified"
648 {"hsm_cancel", lfs_hsm_cancel, 0,
649 "Cancel requests related to specified files.\n"
650 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
651 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
652 "usage: swap_layouts <path1> <path2>"},
653 {"migrate", lfs_setstripe_migrate, 0,
654 "migrate a directory between MDTs.\n"
655 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
656 " [--mdt-hash|-H] <hash_type>\n"
657 " [--mdt-index|-m] <start_mdt_index>\n"
660 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
661 " it's the MDT index of first stripe\n"
662 "\tmdt_count: number of MDTs to stripe a directory over\n"
663 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
664 " all_char (type 1)sum of characters % MDT_COUNT\n"
665 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
666 " crush (type 3)CRUSH hash algorithm\n"
668 "migrate file objects from one OST "
669 "layout\nto another (may be not safe with concurent writes).\n"
671 "[--stripe-count|-c] <stripe_count>\n"
672 "[--overstripe-count|-C] <stripe_count>\n"
673 " [--stripe-index|-i] <start_ost_index>\n"
674 " [--stripe-size|-S] <stripe_size>\n"
675 " [--pool|-p] <pool_name>\n"
676 " [--ost|-o] <ost_indices>\n"
678 " [--non-block|-n]\n"
679 " [--non-direct|-D]\n"
680 " <file|directory>\n"
681 "\tstripe_count: number of OSTs to stripe a file over\n"
682 "\t Using -C instead of -c allows overstriping, which\n"
683 "\t will place more than one stripe per OST if\n"
684 "\t stripe_count is greater than the number of OSTs\n"
685 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
686 "\tstripe_size: number of bytes to store before moving to the next OST\n"
687 "\tpool_name: name of the predefined pool of OSTs\n"
688 "\tost_indices: OSTs to stripe over, in order\n"
689 "\tblock: Block file access during data migration (default)\n"
690 "\tnon-block: Abort migrations if concurrent access is detected\n"
691 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
693 "To move directories between MDTs. This command is deprecated, "
694 "use \"migrate\" instead.\n"
695 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
697 {"ladvise", lfs_ladvise, 0,
698 "Provide servers with advice about access patterns for a file.\n"
699 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
700 " [--background|-b] [--unset|-u]\n\n"
701 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
702 " {[--mode|-m [READ,WRITE]}\n"
704 {"mirror", lfs_mirror, mirror_cmdlist,
705 "lfs commands used to manage files with mirrored components:\n"
706 "lfs mirror create - create a mirrored file or directory\n"
707 "lfs mirror extend - add mirror(s) to an existing file\n"
708 "lfs mirror split - split a mirror from an existing mirrored file\n"
709 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
710 "lfs mirror read - read a mirror content of a mirrored file\n"
711 "lfs mirror write - write to a mirror of a mirrored file\n"
712 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
713 "lfs mirror verify - verify mirrored file(s)\n"},
714 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
715 "usage: getsom [-s] [-b] [-f] <path>\n"
716 "\t-s: Only show the size value of the SOM data for a given file\n"
717 "\t-b: Only show the blocks value of the SOM data for a given file\n"
718 "\t-f: Only show the flags value of the SOM data for a given file\n"},
719 {"heat_get", lfs_heat_get, 0,
720 "To get heat of files.\n"
721 "usage: heat_get <file> ...\n"},
722 {"heat_set", lfs_heat_set, 0,
723 "To set heat flags of files.\n"
724 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
725 "\t--clear|-c: Clear file heat for given files\n"
726 "\t--off|-o: Turn off file heat for given files\n"
727 "\t--on|-O: Turn on file heat for given files\n"},
728 {"pcc", lfs_pcc, pcc_cmdlist,
729 "lfs commands used to interact with PCC features:\n"
730 "lfs pcc attach - attach given files to Persistent Client Cache\n"
731 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
732 "lfs pcc state - display the PCC state for given files\n"
733 "lfs pcc detach - detach given files from Persistent Client Cache\n"
734 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
735 {"help", Parser_help, 0, "help"},
736 {"exit", Parser_quit, 0, "quit"},
737 {"quit", Parser_quit, 0, "quit"},
738 {"--version", Parser_version, 0,
739 "output build version of the utility and exit"},
740 {"--list-commands", lfs_list_commands, 0,
741 "list commands supported by the utility and exit"},
745 static int check_hashtype(const char *hashtype)
747 int type_num = atoi(hashtype);
750 /* numeric hash type */
751 if (hashtype && strlen(hashtype) == 1 &&
752 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
754 /* string hash type */
755 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
756 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
762 static uint32_t check_foreign_type_name(const char *foreign_type_name)
766 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
767 if (!lu_foreign_types[i].lft_name)
769 if (strcmp(foreign_type_name,
770 lu_foreign_types[i].lft_name) == 0)
771 return lu_foreign_types[i].lft_type;
774 return LU_FOREIGN_TYPE_UNKNOWN;
777 static const char *error_loc = "syserror";
780 MIGRATION_NONBLOCK = 0x0001,
781 MIGRATION_MIRROR = 0x0002,
782 MIGRATION_NONDIRECT = 0x0004,
783 MIGRATION_VERBOSE = 0x0008,
787 migrate_open_files(const char *name, __u64 migration_flags,
788 const struct llapi_stripe_param *param,
789 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
796 char parent[PATH_MAX];
797 char volatile_file[PATH_MAX];
803 if (!param && !layout) {
804 error_loc = "layout information";
808 /* search for file directory pathname */
809 if (strlen(name) > sizeof(parent) - 1) {
810 error_loc = "source file name";
814 strncpy(parent, name, sizeof(parent));
815 ptr = strrchr(parent, '/');
817 if (!getcwd(parent, sizeof(parent))) {
818 error_loc = "getcwd";
822 if (ptr == parent) /* leading '/' */
827 /* open file, direct io */
828 /* even if the file is only read, WR mode is nedeed to allow
832 if (!(migration_flags & MIGRATION_NONDIRECT))
834 fd = open(name, rflags);
837 error_loc = "cannot open source file";
841 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
843 error_loc = "cannot get MDT index";
848 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
849 mode_t open_mode = S_IRUSR | S_IWUSR;
851 random_value = random();
852 rc = snprintf(volatile_file, sizeof(volatile_file),
853 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
854 mdt_index, random_value);
855 if (rc >= sizeof(volatile_file)) {
860 /* create, open a volatile file, use caching (ie no directio) */
862 /* Returns -1 and sets errno on error: */
863 fdv = llapi_layout_file_open(volatile_file, open_flags,
868 /* Does the right thing on error: */
869 fdv = llapi_file_open_param(volatile_file, open_flags,
872 } while (fdv < 0 && (rc = fdv) == -EEXIST);
875 error_loc = "cannot create volatile file";
880 * In case the MDT does not support creation of volatile files
881 * we should try to unlink it.
883 (void)unlink(volatile_file);
886 * Not-owner (root?) special case.
887 * Need to set owner/group of volatile file like original.
888 * This will allow to pass related check during layout_swap.
893 error_loc = "cannot stat source file";
897 rc = fstat(fdv, &stv);
900 error_loc = "cannot stat volatile";
904 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
905 rc = fchown(fdv, st.st_uid, st.st_gid);
908 error_loc = "cannot change ownwership of volatile";
927 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
929 struct llapi_layout *layout;
930 size_t buf_size = 4 * 1024 * 1024;
934 size_t page_size = sysconf(_SC_PAGESIZE);
938 layout = llapi_layout_get_by_fd(fd_src, 0);
940 uint64_t stripe_size;
942 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
944 buf_size = stripe_size;
946 llapi_layout_free(layout);
949 /* Use a page-aligned buffer for direct I/O */
950 rc = posix_memalign(&buf, page_size, buf_size);
954 sparse = llapi_file_is_sparse(fd_src);
956 rc = ftruncate(fd_dst, pos);
965 size_t to_read, to_write;
968 if (sparse && pos >= data_end) {
971 data_off = llapi_data_seek(fd_src, pos, &data_size);
973 /* Non-fatal, switch to full copy */
977 /* hole at the end of file, truncate up to it */
979 rc = ftruncate(fd_dst, data_off);
983 pos = data_off & ~(page_size - 1);
984 data_end = data_off + data_size;
985 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
986 to_read = MIN(to_read, buf_size);
992 rc = check_file(fd_src);
997 rsize = pread(fd_src, buf, to_read, pos);
1007 while (to_write > 0) {
1010 written = pwrite(fd_dst, buf, to_write, pos);
1016 to_write -= written;
1018 if (rc || rsize < to_read)
1026 /* Try to avoid page cache pollution after migration. */
1027 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
1028 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1034 static int migrate_copy_timestamps(int fd, int fdv)
1038 if (fstat(fd, &st) == 0) {
1039 struct timeval tv[2] = {
1040 {.tv_sec = st.st_atime},
1041 {.tv_sec = st.st_mtime}
1044 return futimes(fdv, tv);
1050 static int migrate_block(int fd, int fdv)
1057 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1059 error_loc = "cannot get dataversion";
1068 * The grouplock blocks all concurrent accesses to the file.
1069 * It has to be taken after llapi_get_data_version as it would
1072 rc = llapi_group_lock(fd, gid);
1074 error_loc = "cannot get group lock";
1078 rc = migrate_copy_data(fd, fdv, NULL);
1080 error_loc = "data copy failed";
1084 /* Make sure we keep original atime/mtime values */
1085 rc = migrate_copy_timestamps(fd, fdv);
1087 error_loc = "timestamp copy failed";
1093 * for a migration we need to check data version on file did
1096 * Pass in gid=0 since we already own grouplock.
1098 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1099 SWAP_LAYOUTS_CHECK_DV1);
1100 if (rc == -EAGAIN) {
1101 error_loc = "file changed";
1103 } else if (rc < 0) {
1104 error_loc = "cannot swap layout";
1109 rc2 = llapi_group_unlock(fd, gid);
1110 if (rc2 < 0 && rc == 0) {
1111 error_loc = "unlock group lock";
1119 * Internal helper for migrate_copy_data(). Check lease and report error if
1122 * \param[in] fd File descriptor on which to check the lease.
1124 * \retval 0 Migration can keep on going.
1125 * \retval -errno Error occurred, abort migration.
1127 static int check_lease(int fd)
1131 rc = llapi_lease_check(fd);
1133 return 0; /* llapi_check_lease returns > 0 on success. */
1138 static int migrate_nonblock(int fd, int fdv)
1144 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1146 error_loc = "cannot get data version";
1150 rc = migrate_copy_data(fd, fdv, check_lease);
1152 error_loc = "data copy failed";
1156 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1158 error_loc = "cannot get data version";
1164 error_loc = "source file changed";
1168 /* Make sure we keep original atime/mtime values */
1169 rc = migrate_copy_timestamps(fd, fdv);
1171 error_loc = "timestamp copy failed";
1179 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1181 struct pool_to_id_cbdata data = { .pool = pool };
1182 struct llapi_layout *layout = NULL;
1185 layout = llapi_layout_get_by_path(fname, 0);
1188 "error %s: file '%s' couldn't get layout: rc=%d\n",
1189 progname, fname, errno);
1193 rc = llapi_layout_sanity(layout, fname, false, true);
1195 llapi_layout_sanity_perror(errno);
1198 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1207 llapi_layout_free(layout);
1211 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1212 __u32 flags, __u32 neg_flags)
1215 __u32 flags_array[2];
1222 "error %s: neither component id nor pool is specified\n",
1226 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1232 ids[count] = comp_id;
1233 flags_array[count] = flags;
1238 if (neg_flags & LCME_FL_STALE) {
1240 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1245 ids[count] = comp_id;
1246 flags_array[count] = neg_flags | LCME_FL_NEG;
1250 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1252 if (errno == EUCLEAN) {
1255 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1256 progname, comp_id, fname);
1259 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1260 progname, comp_id, fname, flags, neg_flags);
1267 static int lfs_component_del(char *fname, __u32 comp_id,
1268 __u32 flags, __u32 neg_flags)
1272 if (flags && neg_flags)
1275 if (!flags && neg_flags)
1276 flags = neg_flags | LCME_FL_NEG;
1278 if ((flags && comp_id) || (!flags && !comp_id))
1282 if (flags & ~LCME_KNOWN_FLAGS) {
1284 "%s setstripe: unknown flags %#x\n",
1288 } else if (comp_id > LCME_ID_MAX) {
1289 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1294 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1297 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1298 progname, comp_id, fname, strerror(errno));
1302 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1309 rc = llapi_layout_file_comp_add(fname, layout);
1311 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1312 fname, strerror(errno));
1316 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1317 struct llapi_layout *layout)
1325 fd = lstat(fname, &st);
1326 if (fd == 0 && S_ISDIR(st.st_mode))
1327 open_flags = O_DIRECTORY | O_RDONLY;
1329 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1331 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1332 S_ISDIR(st.st_mode) ?
1333 "set default composite layout for" :
1334 "create composite file",
1335 fname, strerror(errno));
1339 static int lfs_migrate(char *name, __u64 migration_flags,
1340 struct llapi_stripe_param *param,
1341 struct llapi_layout *layout)
1343 struct llapi_layout *existing;
1344 uint64_t dom_new, dom_cur;
1349 rc = migrate_open_files(name, migration_flags, param, layout,
1354 rc = llapi_layout_dom_size(layout, &dom_new);
1356 error_loc = "cannot get new layout DoM size";
1359 /* special case for migration to DOM layout*/
1360 existing = llapi_layout_get_by_fd(fd, 0);
1362 error_loc = "cannot get existing layout";
1366 rc = llapi_layout_dom_size(existing, &dom_cur);
1368 error_loc = "cannot get current layout DoM size";
1373 * if file has DoM layout already then migration is possible to
1374 * the new layout with the same DoM component via swap layout,
1375 * if new layout used bigger DOM size, then mirroring is used
1377 if (dom_new > dom_cur) {
1378 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1381 error_loc = "cannot migrate to DOM layout";
1385 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1387 * Blocking mode (forced if servers do not support file lease).
1388 * It is also the default mode, since we cannot distinguish
1389 * between a broken lease and a server that does not support
1390 * atomic swap/close (LU-6785)
1392 rc = migrate_block(fd, fdv);
1396 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1398 error_loc = "cannot get lease";
1402 rc = migrate_nonblock(fd, fdv);
1404 llapi_lease_release(fd);
1409 * Atomically put lease, swap layouts and close.
1410 * for a migration we need to check data version on file did
1413 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1415 error_loc = "cannot swap layout";
1427 fprintf(stderr, "error: %s: %s: %s: %s\n",
1428 progname, name, error_loc, strerror(-rc));
1429 else if (migration_flags & MIGRATION_VERBOSE)
1430 printf("%s\n", name);
1435 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1438 char *dup_string = NULL;
1444 if (!string || !string[0])
1447 dup_string = strdup(string);
1449 llapi_printf(LLAPI_MSG_ERROR,
1450 "%s: insufficient memory\n",
1455 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1459 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1460 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1461 const char *comp_name = comp_flags_table[i].cfn_name;
1463 if (strcmp(name, comp_name) == 0) {
1464 *flags |= comp_flag;
1466 } else if (strncmp(name, "^", 1) == 0 &&
1467 strcmp(name + 1, comp_name) == 0) {
1468 *neg_flags |= comp_flag;
1473 llapi_printf(LLAPI_MSG_ERROR,
1474 "%s: component flag '%s' not supported\n",
1481 if (!*flags && !*neg_flags)
1484 /* don't allow to set and exclude the same flag */
1485 if (*flags & *neg_flags)
1493 static int mdthash_input(char *string, __u32 *inflags,
1494 __u32 *exflags, __u32 *type)
1501 {"migrating", LMV_HASH_FLAG_MIGRATION},
1502 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1503 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1512 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1516 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1517 if (strcmp(name, mhflist[i].name) == 0 ||
1518 name[0] == mhflist[i].name[0]) {
1519 *inflags |= mhflist[i].flag;
1521 } else if (name[0] == '^' &&
1522 (strcmp(name + 1, mhflist[i].name) == 0 ||
1523 name[1] == mhflist[i].name[0])) {
1524 *exflags |= mhflist[i].flag;
1529 i = check_hashtype(name);
1534 llapi_printf(LLAPI_MSG_ERROR,
1535 "%s: invalid mdt_hash value '%s'\n",
1541 /* don't allow to include and exclude the same flag */
1542 if (*inflags & *exflags) {
1543 llapi_printf(LLAPI_MSG_ERROR,
1544 "%s: include and exclude same flag '%s'\n",
1552 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1560 if (strncmp(string, "^", 1) == 0) {
1561 *neg_state = llapi_layout_string_flags(string + 1);
1562 if (*neg_state != 0)
1565 *state = llapi_layout_string_flags(string);
1570 llapi_printf(LLAPI_MSG_ERROR,
1571 "%s: mirrored file state '%s' not supported\n",
1577 * struct mirror_args - Command-line arguments for mirror(s).
1578 * @m_count: Number of mirrors to be created with this layout.
1579 * @m_flags: Mirror level flags, only 'prefer' is supported.
1580 * @m_layout: Mirror layout.
1581 * @m_file: A victim file. Its layout will be split and used as a mirror.
1582 * @m_next: Point to the next node of the list.
1584 * Command-line arguments for mirror(s) will be parsed and stored in
1585 * a linked list that consists of this structure.
1587 struct mirror_args {
1590 struct llapi_layout *m_layout;
1592 struct mirror_args *m_next;
1597 * enum mirror_flags - Flags for extending a mirrored file.
1598 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1599 * in case the victim file(s) contains the same data as the
1600 * original mirrored file.
1601 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1602 * @MF_COMP_ID: specified component id instead of mirror id
1604 * Flags for extending a mirrored file.
1614 * mirror_create_sanity_check() - Check mirror list.
1615 * @list: A linked list that stores the mirror arguments.
1617 * This function does a sanity check on @list for creating
1620 * Return: 0 on success or a negative error code on failure.
1622 static int mirror_create_sanity_check(const char *fname,
1623 struct mirror_args *list,
1627 bool has_m_file = false;
1628 bool has_m_layout = false;
1633 if (fname && check_fname) {
1634 struct llapi_layout *layout;
1636 layout = llapi_layout_get_by_path(fname, 0);
1639 "error: %s: file '%s' couldn't get layout\n",
1644 rc = llapi_layout_sanity(layout, fname, false, true);
1646 llapi_layout_free(layout);
1649 llapi_layout_sanity_perror(rc);
1657 llapi_layout_free(list->m_layout);
1660 llapi_layout_get_by_path(list->m_file, 0);
1661 if (!list->m_layout) {
1663 "error: %s: file '%s' has no layout\n",
1664 progname, list->m_file);
1668 has_m_layout = true;
1669 if (!list->m_layout) {
1670 fprintf(stderr, "error: %s: no mirror layout\n",
1676 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1678 llapi_layout_sanity_perror(rc);
1682 list = list->m_next;
1685 if (has_m_file && has_m_layout) {
1687 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1695 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1697 __u32 mirror_flags = *(__u32 *)cbdata;
1701 rc = llapi_layout_comp_flags_get(layout, &flags);
1706 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1711 return LLAPI_LAYOUT_ITER_CONT;
1715 * mirror_create() - Create a mirrored file.
1716 * @fname: The file to be created.
1717 * @mirror_list: A linked list that stores the mirror arguments.
1719 * This function creates a mirrored file @fname with the mirror(s)
1720 * from @mirror_list.
1722 * Return: 0 on success or a negative error code on failure.
1724 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1726 struct llapi_layout *layout = NULL;
1727 struct mirror_args *cur_mirror = NULL;
1728 uint16_t mirror_count = 0;
1732 rc = mirror_create_sanity_check(fname, mirror_list, false);
1736 cur_mirror = mirror_list;
1737 while (cur_mirror) {
1738 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1740 &cur_mirror->m_flags);
1743 fprintf(stderr, "%s: failed to set mirror flags\n",
1748 for (i = 0; i < cur_mirror->m_count; i++) {
1749 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1753 "error: %s: merge layout failed: %s\n",
1754 progname, strerror(errno));
1758 mirror_count += cur_mirror->m_count;
1759 cur_mirror = cur_mirror->m_next;
1763 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1767 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1770 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1771 progname, strerror(errno));
1775 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1783 llapi_layout_free(layout);
1788 * Compare files and check lease on @fd.
1790 * \retval bytes number of bytes are the same
1792 static ssize_t mirror_file_compare(int fd, int fdv)
1794 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1796 ssize_t bytes_done = 0;
1797 ssize_t bytes_read = 0;
1799 buf = malloc(buflen * 2);
1804 if (!llapi_lease_check(fd)) {
1805 bytes_done = -EBUSY;
1809 bytes_read = read(fd, buf, buflen);
1810 if (bytes_read <= 0)
1813 if (bytes_read != read(fdv, buf + buflen, buflen))
1817 * XXX: should compute the checksum on each buffer and then
1818 * compare checksum to avoid cache collision
1820 if (memcmp(buf, buf + buflen, bytes_read))
1823 bytes_done += bytes_read;
1831 static int mirror_extend_file(const char *fname, const char *victim_file,
1832 enum mirror_flags mirror_flags)
1837 struct stat stbuf_v;
1838 struct ll_ioc_lease *data = NULL;
1841 fd = open(fname, O_RDWR);
1843 error_loc = "open source file";
1848 fdv = open(victim_file, O_RDWR);
1850 error_loc = "open target file";
1855 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1856 error_loc = "stat source or target file";
1861 if (stbuf.st_dev != stbuf_v.st_dev) {
1862 error_loc = "stat source and target file";
1867 /* mirrors should be of the same size */
1868 if (stbuf.st_size != stbuf_v.st_size) {
1869 error_loc = "file sizes don't match";
1874 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1876 error_loc = "cannot get lease";
1880 if (!(mirror_flags & MF_NO_VERIFY)) {
1882 /* mirrors should have the same contents */
1883 ret = mirror_file_compare(fd, fdv);
1884 if (ret != stbuf.st_size) {
1885 error_loc = "file busy or contents don't match";
1886 rc = ret < 0 ? ret : -EINVAL;
1891 /* Get rid of caching pages from clients */
1892 rc = llapi_file_flush(fd);
1894 error_loc = "cannot get data version";
1898 rc = llapi_file_flush(fdv);
1900 error_loc = "cannot get data version";
1904 /* Make sure we keep original atime/mtime values */
1905 rc = migrate_copy_timestamps(fd, fdv);
1907 error_loc = "cannot copy timestamp";
1911 /* Atomically put lease, merge layouts and close. */
1912 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1914 error_loc = "memory allocation";
1917 data->lil_mode = LL_LEASE_UNLCK;
1918 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1919 data->lil_count = 1;
1920 data->lil_ids[0] = fdv;
1921 rc = llapi_lease_set(fd, data);
1923 error_loc = "cannot merge layout";
1925 } else if (rc == 0) {
1927 error_loc = "lost lease lock";
1940 (void) unlink(victim_file);
1942 fprintf(stderr, "error: %s: %s: %s: %s\n",
1943 progname, fname, error_loc, strerror(-rc));
1947 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1948 bool inherit, uint32_t flags)
1950 struct llapi_layout *f_layout = NULL;
1951 struct ll_ioc_lease *data = NULL;
1957 f_layout = llapi_layout_get_by_path(name, 0);
1959 fprintf(stderr, "%s: cannot get layout\n", progname);
1962 rc = llapi_layout_get_last_init_comp(f_layout);
1964 fprintf(stderr, "%s: cannot get the last init comp\n",
1968 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1971 "%s: cannot inherit from the last init comp\n",
1976 llapi_layout_comp_flags_set(m_layout, flags);
1977 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1982 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1984 error_loc = "cannot get lease";
1988 rc = migrate_nonblock(fd, fdv);
1990 llapi_lease_release(fd);
1994 /* Atomically put lease, merge layouts and close. */
1995 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1997 error_loc = "memory allocation";
2000 data->lil_mode = LL_LEASE_UNLCK;
2001 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2002 data->lil_count = 1;
2003 data->lil_ids[0] = fdv;
2004 rc = llapi_lease_set(fd, data);
2006 error_loc = "cannot merge layout";
2008 } else if (rc == 0) {
2010 error_loc = "lost lease lock";
2023 fprintf(stderr, "error: %s: %s: %s: %s\n",
2024 progname, name, error_loc, strerror(-rc));
2028 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2029 enum mirror_flags mirror_flags)
2033 rc = mirror_create_sanity_check(fname, mirror_list, true);
2037 while (mirror_list) {
2038 if (mirror_list->m_file) {
2039 rc = mirror_extend_file(fname, mirror_list->m_file,
2042 __u32 mirror_count = mirror_list->m_count;
2044 while (mirror_count > 0) {
2045 rc = mirror_extend_layout(fname,
2046 mirror_list->m_layout,
2047 mirror_list->m_inherit,
2048 mirror_list->m_flags);
2058 mirror_list = mirror_list->m_next;
2064 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2069 rc = llapi_layout_mirror_id_get(layout, &id);
2073 if ((__u16)id == *(__u16 *)cbdata)
2074 return LLAPI_LAYOUT_ITER_STOP;
2076 return LLAPI_LAYOUT_ITER_CONT;
2079 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2084 rc = llapi_layout_comp_id_get(layout, &id);
2088 if (id == *(__u32 *)cbdata)
2089 return LLAPI_LAYOUT_ITER_STOP;
2091 return LLAPI_LAYOUT_ITER_CONT;
2094 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2096 char buf[LOV_MAXPOOLNAME + 1];
2097 struct pool_to_id_cbdata *d = (void *)cbdata;
2101 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2104 if (strcmp(d->pool, buf))
2105 return LLAPI_LAYOUT_ITER_CONT;
2107 rc = llapi_layout_mirror_id_get(layout, &id);
2112 return LLAPI_LAYOUT_ITER_STOP;
2115 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2117 char buf[LOV_MAXPOOLNAME + 1];
2118 struct pool_to_id_cbdata *d = (void *)cbdata;
2122 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2125 if (strcmp(d->pool, buf))
2126 return LLAPI_LAYOUT_ITER_CONT;
2128 rc = llapi_layout_comp_id_get(layout, &id);
2133 return LLAPI_LAYOUT_ITER_STOP;
2136 struct collect_ids_data {
2142 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2144 struct collect_ids_data *cid = cbdata;
2148 rc = llapi_layout_mirror_id_get(layout, &id);
2152 if ((__u16)id != cid->cid_exclude) {
2155 for (i = 0; i < cid->cid_count; i++) {
2156 /* already collected the mirror id */
2157 if (id == cid->cid_ids[i])
2158 return LLAPI_LAYOUT_ITER_CONT;
2160 cid->cid_ids[cid->cid_count] = id;
2164 return LLAPI_LAYOUT_ITER_CONT;
2168 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2169 * @mirror_id: Mirror id to be checked.
2170 * @layout: Mirror component list.
2172 * This function checks if a mirror with specified @mirror_id is the last
2173 * non-stale mirror of a layout @layout.
2175 * Return: true or false.
2178 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2180 __u16 mirror_ids[128] = { 0 };
2181 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2183 .cid_exclude = mirror_id, };
2186 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2188 for (i = 0; i < cid.cid_count; i++) {
2189 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2192 comp_size = llapi_mirror_find_stale(layout, comp_array,
2193 ARRAY_SIZE(comp_array),
2202 static int mirror_split(const char *fname, __u32 id, const char *pool,
2203 enum mirror_flags mflags, const char *victim_file)
2205 struct llapi_layout *layout;
2206 char parent[PATH_MAX];
2207 char victim[PATH_MAX];
2208 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2210 struct ll_ioc_lease *data;
2211 uint16_t mirror_count;
2215 bool purge = true; /* delete mirror by setting fdv=fd */
2218 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2220 "error %s: the source file '%s' and -f file are the same\n",
2225 /* check fname contains mirror with mirror_id/comp_id */
2226 layout = llapi_layout_get_by_path(fname, 0);
2229 "error %s: file '%s' couldn't get layout\n",
2234 rc = llapi_layout_sanity(layout, fname, false, true);
2236 llapi_layout_sanity_perror(rc);
2240 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2243 "error %s: file '%s' couldn't get mirror count\n",
2247 if (mirror_count < 2) {
2249 "error %s: file '%s' has %d component, cannot split\n",
2250 progname, fname, mirror_count);
2254 if (mflags & MF_COMP_POOL) {
2255 struct pool_to_id_cbdata data = { .pool = pool };
2257 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2259 mirror_id = data.id;
2260 } else if (mflags & MF_COMP_ID) {
2261 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2262 mirror_id = mirror_id_of(id);
2264 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2268 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2271 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2272 if (mflags & MF_COMP_POOL) {
2274 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2275 progname, fname, pool);
2277 } else if (mflags & MF_COMP_ID) {
2279 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2280 progname, fname, id);
2284 "error %s: file '%s' does not contain mirror with id %u\n",
2285 progname, fname, id);
2290 fd = open(fname, O_RDWR);
2293 "error %s: open file '%s' failed: %s\n",
2294 progname, fname, strerror(errno));
2298 /* get victim file directory pathname */
2299 if (strlen(fname) > sizeof(parent) - 1) {
2300 fprintf(stderr, "error %s: file name of '%s' too long\n",
2305 strncpy(parent, fname, sizeof(parent));
2306 ptr = strrchr(parent, '/');
2308 if (!getcwd(parent, sizeof(parent))) {
2309 fprintf(stderr, "error %s: getcwd failed: %s\n",
2310 progname, strerror(errno));
2320 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2322 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2329 /* use a temp file to store the splitted layout */
2330 if (mflags & MF_DESTROY) {
2331 if (last_non_stale_mirror(mirror_id, layout)) {
2334 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2340 /* don't use volatile file for mirror destroy */
2344 * try the old way to delete mirror using
2347 fdv = llapi_create_volatile_idx(parent,
2348 mdt_index, O_LOV_DELAY_CREATE);
2351 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2353 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2356 /* user specified victim file */
2357 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2362 "error %s: create victim file failed: %s\n",
2363 progname, strerror(errno));
2367 /* get lease lock of fname */
2368 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2371 "error %s: cannot get lease of file '%s': %d\n",
2372 progname, fname, rc);
2376 /* Atomatically put lease, split layouts and close. */
2377 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2383 data->lil_mode = LL_LEASE_UNLCK;
2384 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2385 data->lil_count = 2;
2386 data->lil_ids[0] = fdv;
2387 data->lil_ids[1] = mirror_id;
2388 rc = llapi_lease_set(fd, data);
2390 if (rc == -EINVAL && purge) {
2391 /* could be old MDS which prohibit fd==fdv */
2396 if (rc == 0) /* lost lease lock */
2399 "error %s: cannot split '%s': %s\n",
2400 progname, fname, strerror(-rc));
2412 llapi_layout_free(layout);
2417 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2418 __u16 *mirror_ids, int ids_nr);
2420 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2421 __u64 migration_flags,
2422 struct llapi_stripe_param *param,
2423 struct llapi_layout *layout)
2425 struct ll_ioc_lease *data = NULL;
2428 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2430 error_loc = "cannot get lease";
2434 /* Atomically put lease, merge layouts, resync and close. */
2435 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2437 error_loc = "memory allocation";
2440 data->lil_mode = LL_LEASE_UNLCK;
2441 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2442 data->lil_count = 1;
2443 data->lil_ids[0] = fdv;
2444 rc = llapi_lease_set(fd, data);
2446 error_loc = "cannot merge layout";
2448 } else if (rc == 0) {
2450 error_loc = "lost lease lock";
2456 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2458 error_loc = "cannot resync file";
2462 /* delete first mirror now */
2463 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2465 error_loc = "cannot delete old layout";
2473 fprintf(stderr, "error: %s: %s: %s: %s\n",
2474 progname, name, error_loc, strerror(-rc));
2475 else if (migration_flags & MIGRATION_VERBOSE)
2476 printf("%s\n", name);
2483 * Parse a string containing an target index list into an array of integers.
2485 * The input string contains a comma delimited list of individual
2486 * indices and ranges, for example "1,2-4,7". Add the indices into the
2487 * \a tgts array and remove duplicates.
2489 * \param[out] tgts array to store indices in
2490 * \param[in] size size of \a tgts array
2491 * \param[in] offset starting index in \a tgts
2492 * \param[in] arg string containing OST index list
2493 * \param[in/out] overstriping index list may contain duplicates
2495 * \retval positive number of indices in \a tgts
2496 * \retval -EINVAL unable to parse \a arg
2498 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2499 unsigned long long *pattern)
2503 int slots = size - offset;
2505 bool overstriped = false;
2511 end_of_loop = false;
2512 while (!end_of_loop) {
2513 int start_index = 0;
2516 char *endptr = NULL;
2520 ptr = strchrnul(arg, ',');
2522 end_of_loop = *ptr == '\0';
2526 start_index = strtol(arg, &endptr, 0);
2527 if (endptr == arg) /* no data at all */
2529 if (errno != 0 || start_index < -1 ||
2530 (*endptr != '-' && *endptr != '\0'))
2533 end_index = start_index;
2534 if (*endptr == '-') {
2536 end_index = strtol(endptr + 1, &endptr, 0);
2537 if (errno != 0 || *endptr != '\0' || end_index < -1)
2539 if (end_index < start_index)
2543 for (i = start_index; i <= end_index && slots > 0; i++) {
2546 /* remove duplicate */
2547 for (j = 0; j < offset; j++) {
2548 if (tgts[j] == i && pattern &&
2549 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2551 else if (tgts[j] == i)
2557 if (j == offset) { /* check complete */
2563 if (slots == 0 && i < end_index)
2571 if (!end_of_loop && ptr)
2574 if (!overstriped && pattern)
2575 *pattern = LLAPI_LAYOUT_DEFAULT;
2577 return rc < 0 ? rc : nr;
2580 struct lfs_setstripe_args {
2581 unsigned long long lsa_comp_end;
2582 unsigned long long lsa_stripe_size;
2583 unsigned long long lsa_extension_size;
2584 long long lsa_stripe_count;
2585 long long lsa_stripe_off;
2586 __u32 lsa_comp_flags;
2587 __u32 lsa_comp_neg_flags;
2588 unsigned long long lsa_pattern;
2589 unsigned int lsa_mirror_count;
2591 bool lsa_first_comp;
2592 bool lsa_extension_comp;
2594 char *lsa_pool_name;
2597 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2599 unsigned int mirror_count = lsa->lsa_mirror_count;
2600 bool first_comp = lsa->lsa_first_comp;
2602 memset(lsa, 0, sizeof(*lsa));
2604 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2605 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2606 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2607 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2608 lsa->lsa_pool_name = NULL;
2610 lsa->lsa_mirror_count = mirror_count;
2611 lsa->lsa_first_comp = first_comp;
2615 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2616 * @lsa: Stripe options to be initialized and inherited.
2618 * This function initializes stripe options in @lsa and inherit
2619 * stripe_size, stripe_count and OST pool_name options.
2623 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2625 unsigned long long stripe_size;
2626 long long stripe_count;
2627 char *pool_name = NULL;
2629 stripe_size = lsa->lsa_stripe_size;
2630 stripe_count = lsa->lsa_stripe_count;
2631 pool_name = lsa->lsa_pool_name;
2633 setstripe_args_init(lsa);
2635 lsa->lsa_stripe_size = stripe_size;
2636 lsa->lsa_stripe_count = stripe_count;
2637 lsa->lsa_pool_name = pool_name;
2640 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2642 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2643 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2644 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2645 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2646 lsa->lsa_comp_end != 0);
2649 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2651 if (lsa->lsa_nr_tgts) {
2652 if (lsa->lsa_nr_tgts < 0 ||
2653 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2654 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2660 if (lsa->lsa_stripe_count > 0 &&
2661 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2662 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2663 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2664 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2665 lsa->lsa_stripe_count,
2677 * comp_args_to_layout() - Create or extend a composite layout.
2678 * @composite: Pointer to the composite layout.
2679 * @lsa: Stripe options for the new component.
2681 * This function creates or extends a composite layout by adding a new
2682 * component with stripe options from @lsa.
2684 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2686 * Return: 0 on success or an error code on failure.
2688 static int comp_args_to_layout(struct llapi_layout **composite,
2689 struct lfs_setstripe_args *lsa,
2692 struct llapi_layout *layout = *composite;
2693 uint64_t prev_end = 0;
2699 layout = llapi_layout_alloc();
2701 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2706 *composite = layout;
2707 lsa->lsa_first_comp = true;
2712 * Get current component extent, current component
2713 * must be the tail component.
2715 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2717 fprintf(stderr, "Get comp extent failed. %s\n",
2722 if (lsa->lsa_first_comp) {
2724 rc = llapi_layout_add_first_comp(layout);
2726 rc = llapi_layout_comp_add(layout);
2729 fprintf(stderr, "Add component failed. %s\n",
2735 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2737 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2738 lsa->lsa_comp_flags, strerror(errno));
2743 uint64_t comp_end = lsa->lsa_comp_end;
2746 * The extendable component is 0-length, so it can be removed
2747 * if there is insufficient space to extend it.
2749 if (lsa->lsa_extension_comp)
2750 comp_end = prev_end;
2752 rc = llapi_layout_comp_extent_set(layout, prev_end,
2755 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2756 prev_end, comp_end, strerror(errno));
2760 /* reset lsa_first_comp */
2761 lsa->lsa_first_comp = false;
2763 /* Data-on-MDT component setting */
2764 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2766 * In case of Data-on-MDT patterns the only extra option
2767 * applicable is stripe size option.
2769 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2771 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2772 lsa->lsa_stripe_count);
2776 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2778 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2779 lsa->lsa_stripe_size);
2783 if (lsa->lsa_nr_tgts != 0) {
2785 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2790 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2792 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2793 lsa->lsa_stripe_off);
2797 if (lsa->lsa_pool_name != 0) {
2799 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2800 lsa->lsa_pool_name);
2805 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2807 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2812 /* Data-on-MDT component has always single stripe up to end */
2813 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2814 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2815 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2817 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2824 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2825 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2827 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2828 rc = llapi_layout_extension_size_set(layout, size);
2830 rc = llapi_layout_stripe_size_set(layout, size);
2833 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2834 size, strerror(errno));
2838 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2840 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2841 lsa->lsa_stripe_count, strerror(errno));
2845 if (lsa->lsa_pool_name) {
2846 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2848 fprintf(stderr, "Set pool name: %s failed. %s\n",
2849 lsa->lsa_pool_name, strerror(errno));
2853 rc = llapi_layout_pool_name_set(layout, "");
2855 fprintf(stderr, "Clear pool name failed: %s\n",
2861 rc = lsa_args_stripe_count_check(lsa);
2865 if (lsa->lsa_nr_tgts > 0) {
2868 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2869 rc = llapi_layout_ost_index_set(layout, i,
2874 /* Make sure stripe offset is in OST list. */
2875 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2879 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2880 lsa->lsa_stripe_off);
2884 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2885 lsa->lsa_stripe_off != -1) {
2886 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2889 fprintf(stderr, "Set ost index %d failed. %s\n",
2890 i, strerror(errno));
2894 /* Create the second, virtual component of extension space */
2895 if (lsa->lsa_extension_comp) {
2896 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2897 lsa->lsa_extension_comp = false;
2904 static int build_component(struct llapi_layout **layout,
2905 struct lfs_setstripe_args *lsa, bool set_extent)
2909 rc = comp_args_to_layout(layout, lsa, set_extent);
2913 if (lsa->lsa_mirror_count > 0) {
2914 rc = llapi_layout_mirror_count_set(*layout,
2915 lsa->lsa_mirror_count);
2919 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2922 lsa->lsa_mirror_count = 0;
2928 static int build_prev_component(struct llapi_layout **layout,
2929 struct lfs_setstripe_args *prev,
2930 struct lfs_setstripe_args *lsa,
2933 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2936 if (prev->lsa_stripe_size) {
2938 prev->lsa_comp_end = lsa->lsa_comp_end;
2939 prev->lsa_extension_size = lsa->lsa_extension_size;
2940 prev->lsa_extension_comp = true;
2943 rc = build_component(layout, prev, true);
2949 * Copy lsa to previous lsa;
2950 * if this is an extension component, make the previous invalid;
2953 prev->lsa_stripe_size = 0;
2960 static int build_layout_from_yaml_node(struct cYAML *node,
2961 struct llapi_layout **layout,
2962 struct lfs_setstripe_args *lsa,
2963 struct lfs_setstripe_args *prevp)
2965 struct lfs_setstripe_args prev = { 0 };
2966 __u32 *osts = lsa->lsa_tgts;
2974 string = node->cy_string;
2976 if (node->cy_type == CYAML_TYPE_OBJECT) {
2977 /* go deep to sub blocks */
2978 if (string && !strncmp(string, "component", 9) &&
2979 strncmp(string, "component0", 10) &&
2980 strncmp(string, "components", 10)) {
2981 rc = build_prev_component(layout, prevp, lsa,
2986 /* initialize lsa. */
2987 setstripe_args_init(lsa);
2988 lsa->lsa_first_comp = false;
2989 lsa->lsa_tgts = osts;
2992 rc = build_layout_from_yaml_node(node->cy_child, layout,
2997 if (!node->cy_string)
3000 /* skip leading lmm_ if present, to simplify parsing */
3001 if (strncmp(string, "lmm_", 4) == 0)
3004 if (node->cy_type == CYAML_TYPE_STRING) {
3005 if (!strcmp(string, "lcme_extent.e_end")) {
3006 if (!strcmp(node->cy_valuestring, "EOF") ||
3007 !strcmp(node->cy_valuestring, "eof"))
3008 lsa->lsa_comp_end = LUSTRE_EOF;
3009 } else if (!strcmp(string, "pool")) {
3010 lsa->lsa_pool_name = node->cy_valuestring;
3011 } else if (!strcmp(string, "pattern")) {
3012 if (!strcmp(node->cy_valuestring, "mdt"))
3013 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3014 if (!strcmp(node->cy_valuestring,
3015 "raid0,overstriped"))
3017 LLAPI_LAYOUT_OVERSTRIPING;
3018 } else if (!strcmp(string, "lcme_flags")) {
3019 rc = comp_str2flags(node->cy_valuestring,
3020 &lsa->lsa_comp_flags,
3021 &lsa->lsa_comp_neg_flags);
3025 * Only template flags have meaning in
3026 * the layout for a new file
3028 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3030 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3031 if (!strcmp(string, "lcm_mirror_count")) {
3032 lsa->lsa_mirror_count = node->cy_valueint;
3033 } else if (!strcmp(string, "lcme_extent.e_start")) {
3034 if (node->cy_valueint == 0)
3035 lsa->lsa_first_comp = true;
3036 } else if (!strcmp(string, "lcme_extent.e_end")) {
3037 if (node->cy_valueint == -1)
3038 lsa->lsa_comp_end = LUSTRE_EOF;
3040 lsa->lsa_comp_end = node->cy_valueint;
3041 } else if (!strcmp(string, "stripe_count")) {
3042 lsa->lsa_stripe_count = node->cy_valueint;
3043 } else if (!strcmp(string, "stripe_size")) {
3044 lsa->lsa_stripe_size = node->cy_valueint;
3045 } else if (!strcmp(string, "extension_size")) {
3046 lsa->lsa_extension_size = node->cy_valueint;
3047 lsa->lsa_extension_comp = true;
3048 } else if (!strcmp(string, "stripe_offset")) {
3049 lsa->lsa_stripe_off = node->cy_valueint;
3050 } else if (!strcmp(string, "l_ost_idx")) {
3051 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3056 node = node->cy_next;
3059 if (prevp == &prev) {
3060 rc = build_prev_component(layout, prevp, lsa, true);
3064 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3065 rc = build_component(layout, lsa, *layout != NULL);
3071 static int lfs_comp_create_from_yaml(char *template,
3072 struct llapi_layout **layout,
3073 struct lfs_setstripe_args *lsa,
3076 struct cYAML *tree = NULL, *err_rc = NULL;
3079 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3081 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3082 progname, template);
3083 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3084 "can't parse", &err_rc);
3085 cYAML_print_tree2file(stderr, err_rc);
3086 cYAML_free_tree(err_rc);
3091 /* initialize lsa for plain file */
3092 setstripe_args_init(lsa);
3093 lsa->lsa_tgts = osts;
3095 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3097 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3098 progname, template);
3101 /* clean clean lsa */
3102 setstripe_args_init(lsa);
3106 cYAML_free_tree(tree);
3111 * Get the extension size from the next (SEL) component and extend the
3112 * current component on it. The start of the next component is to be
3115 * \param[in] layout the current layout
3116 * \param[in] start the start of the current component
3117 * \param[in,out] end the end of the current component
3118 * \param[in] offset the offset to adjust the end position to instead of
3121 * \retval 0 - extended successfully
3122 * \retval < 0 - error
3124 static int layout_extend_comp(struct llapi_layout *layout,
3125 uint64_t start, uint64_t *end,
3128 uint64_t size, next_start, next_end;
3131 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3134 "%s setstripe: cannot move component cursor: %s\n",
3135 progname, strerror(errno));
3140 * Even if the @size will not be used below, this will fail if
3141 * this is not a SEL component - a good confirmation we are
3142 * working on right components.
3144 rc = llapi_layout_extension_size_get(layout, &size);
3147 "%s setstripe: cannot get component ext size: %s\n",
3148 progname, strerror(errno));
3152 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3154 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3155 progname, strerror(errno));
3159 next_start += offset ?: size;
3160 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3162 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3163 progname, strerror(errno));
3167 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3170 "%s setstripe: cannot move component cursor: %s\n",
3171 progname, strerror(errno));
3175 *end += offset ?: size;
3176 rc = llapi_layout_comp_extent_set(layout, start, *end);
3178 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3179 progname, strerror(errno));
3187 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3188 * end of the last component in the existing file, and adjust the
3189 * first extent start of the components to be added accordingly.
3191 * In the create mode, we need to check if the first component is an extendable
3192 * SEL component and extend its length to the extension size (first component
3193 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3195 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3198 struct llapi_layout *head;
3199 uint64_t start = 0, prev_end = 0;
3203 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3208 head = llapi_layout_get_by_path(fname, 0);
3211 "%s setstripe: cannot read layout from '%s': %s\n",
3212 progname, fname, strerror(errno));
3214 } else if (errno == ENODATA) {
3216 * file without LOVEA, this component-add will be turned
3217 * into a component-create.
3219 llapi_layout_free(head);
3223 * the new layout will be added to an empty one, it
3224 * still needs to be adjusted below
3228 } else if (!llapi_layout_is_composite(head)) {
3230 "%s setstripe: '%s' not a composite file\n",
3232 llapi_layout_free(head);
3236 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3239 "%s setstripe: cannot get prev extent: %s\n",
3240 progname, strerror(errno));
3241 llapi_layout_free(head);
3245 llapi_layout_free(head);
3249 /* Make sure we use the first component of the layout to be added. */
3250 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3253 "%s setstripe: cannot move component cursor: %s\n",
3254 progname, strerror(errno));
3258 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3260 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3261 progname, strerror(errno));
3265 if (start == 0 && end == 0) {
3266 rc = layout_extend_comp(layout, start, &end,
3267 comp_add ? prev_end : 0);
3272 if (start > prev_end || end < prev_end) {
3274 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3275 progname, start, end, prev_end);
3279 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3282 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3283 progname, prev_end, end, strerror(errno));
3290 static int mirror_adjust_first_extents(struct mirror_args *list)
3298 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3301 list = list->m_next;
3307 static inline bool arg_is_eof(char *arg)
3309 return !strncmp(arg, "-1", strlen("-1")) ||
3310 !strncmp(arg, "EOF", strlen("EOF")) ||
3311 !strncmp(arg, "eof", strlen("eof"));
3315 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3317 * Return: Valid mirror_args pointer on success and
3318 * NULL if memory allocation fails.
3320 static struct mirror_args *lfs_mirror_alloc(void)
3322 struct mirror_args *mirror = NULL;
3325 mirror = calloc(1, sizeof(*mirror));
3327 mirror->m_inherit = false;
3338 * lfs_mirror_free() - Free memory allocated for a mirror argument
3340 * @mirror: Previously allocated mirror argument structure by
3341 * lfs_mirror_alloc().
3343 * Free memory allocated for @mirror.
3347 static void lfs_mirror_free(struct mirror_args *mirror)
3349 if (mirror->m_layout)
3350 llapi_layout_free(mirror->m_layout);
3355 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3356 * @mirror_list: Previously allocated mirror list.
3358 * Free memory allocated for @mirror_list.
3362 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3364 struct mirror_args *next_mirror = NULL;
3366 while (mirror_list) {
3367 next_mirror = mirror_list->m_next;
3368 lfs_mirror_free(mirror_list);
3369 mirror_list = next_mirror;
3381 LFS_COMP_NO_VERIFY_OPT,
3383 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3385 LFS_MIRROR_STATE_OPT,
3387 LFS_MIRROR_INDEX_OPT,
3388 LFS_LAYOUT_FOREIGN_OPT,
3396 static int lfs_setstripe_internal(int argc, char **argv,
3397 enum setstripe_origin opc)
3399 struct lfs_setstripe_args lsa = { 0 };
3400 struct llapi_stripe_param *param = NULL;
3401 struct find_param migrate_mdt_param = {
3411 unsigned long long size_units = 1;
3412 bool migrate_mode = false;
3413 bool migrate_mdt_mode = false;
3414 bool setstripe_mode = false;
3415 bool migration_block = false;
3416 __u64 migration_flags = 0;
3417 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3418 int comp_del = 0, comp_set = 0;
3421 struct llapi_layout *layout = NULL;
3422 struct llapi_layout **lpp = &layout;
3423 bool mirror_mode = false;
3424 bool has_m_file = false;
3425 __u32 mirror_count = 0;
3426 enum mirror_flags mirror_flags = 0;
3427 struct mirror_args *mirror_list = NULL;
3428 struct mirror_args *new_mirror = NULL;
3429 struct mirror_args *last_mirror = NULL;
3430 __u16 mirror_id = 0;
3432 bool from_yaml = false;
3433 bool from_copy = false;
3434 char *template = NULL;
3435 bool foreign_mode = false;
3437 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3438 char *mode_opt = NULL;
3439 mode_t previous_umask = 0;
3440 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3442 struct option long_opts[] = {
3443 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3444 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3445 /* --block is only valid in migrate mode */
3446 { .val = 'b', .name = "block", .has_arg = no_argument },
3447 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3448 { .val = LFS_COMP_ADD_OPT,
3449 .name = "comp-add", .has_arg = no_argument },
3450 { .val = LFS_COMP_ADD_OPT,
3451 .name = "component-add", .has_arg = no_argument },
3452 { .val = LFS_COMP_DEL_OPT,
3453 .name = "comp-del", .has_arg = no_argument },
3454 { .val = LFS_COMP_DEL_OPT,
3455 .name = "component-del", .has_arg = no_argument },
3456 { .val = LFS_COMP_FLAGS_OPT,
3457 .name = "comp-flags", .has_arg = required_argument },
3458 { .val = LFS_COMP_FLAGS_OPT,
3459 .name = "component-flags",
3460 .has_arg = required_argument },
3461 { .val = LFS_COMP_SET_OPT,
3462 .name = "comp-set", .has_arg = no_argument },
3463 { .val = LFS_COMP_SET_OPT,
3464 .name = "component-set",
3465 .has_arg = no_argument},
3466 { .val = LFS_COMP_NO_VERIFY_OPT,
3467 .name = "no-verify", .has_arg = no_argument},
3468 { .val = LFS_LAYOUT_FLAGS_OPT,
3469 .name = "flags", .has_arg = required_argument},
3470 { .val = LFS_LAYOUT_FOREIGN_OPT,
3471 .name = "foreign", .has_arg = optional_argument},
3472 { .val = LFS_MIRROR_ID_OPT,
3473 .name = "mirror-id", .has_arg = required_argument},
3474 { .val = LFS_MODE_OPT,
3475 .name = "mode", .has_arg = required_argument},
3476 { .val = LFS_LAYOUT_COPY,
3477 .name = "copy", .has_arg = required_argument},
3478 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3479 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3480 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3481 { .val = 'C', .name = "overstripe-count",
3482 .has_arg = required_argument},
3483 { .val = 'd', .name = "delete", .has_arg = no_argument},
3484 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3485 /* --non-direct is only valid in migrate mode */
3486 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3487 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3488 { .val = 'E', .name = "component-end",
3489 .has_arg = required_argument},
3490 { .val = 'f', .name = "file", .has_arg = required_argument },
3491 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3492 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3493 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3494 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3495 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3496 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3497 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3498 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3499 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3500 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3501 { .val = 'L', .name = "layout", .has_arg = required_argument },
3502 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3503 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3504 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3505 /* --non-block is only valid in migrate mode */
3506 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3507 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3508 { .val = 'o', .name = "ost", .has_arg = required_argument },
3509 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3510 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3511 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3513 { .val = 'p', .name = "pool", .has_arg = required_argument },
3514 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3515 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3516 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3517 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3518 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3519 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3520 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3521 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3522 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3523 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3524 /* --verbose is only valid in migrate mode */
3525 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3526 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3527 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3528 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3529 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3530 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3533 setstripe_args_init(&lsa);
3535 migrate_mode = (opc == SO_MIGRATE);
3536 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3537 setstripe_mode = (opc == SO_SETSTRIPE);
3538 if (opc == SO_MIRROR_DELETE) {
3540 mirror_flags = MF_DESTROY;
3543 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3545 while ((c = getopt_long(argc, argv,
3546 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3547 long_opts, NULL)) >= 0) {
3553 case LFS_COMP_ADD_OPT:
3556 case LFS_COMP_DEL_OPT:
3559 case LFS_COMP_FLAGS_OPT:
3560 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3561 &lsa.lsa_comp_neg_flags);
3564 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3566 "%s: inverted flags are not supported\n",
3571 case LFS_COMP_SET_OPT:
3574 case LFS_COMP_NO_VERIFY_OPT:
3575 mirror_flags |= MF_NO_VERIFY;
3577 case LFS_MIRROR_ID_OPT: {
3578 unsigned long int id;
3581 id = strtoul(optarg, &end, 0);
3582 if (errno != 0 || *end != '\0' || id == 0 ||
3585 "%s %s: invalid mirror ID '%s'\n",
3586 progname, argv[0], optarg);
3590 mirror_id = (__u16)id;
3593 case LFS_LAYOUT_FLAGS_OPT: {
3596 /* check for numeric flags (foreign and mirror cases) */
3597 if (setstripe_mode && !mirror_mode && !last_mirror) {
3599 flags = strtoul(optarg, &end, 16);
3600 if (errno != 0 || *end != '\0' ||
3601 flags >= UINT32_MAX) {
3603 "%s %s: invalid hex flags '%s'\n",
3604 progname, argv[0], optarg);
3607 if (!foreign_mode) {
3609 "%s %s: hex flags must be specified with --foreign option\n",
3616 if (!mirror_mode || !last_mirror) {
3618 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3623 result = comp_str2flags(optarg, &last_mirror->m_flags,
3630 "%s: inverted flags are not supported\n",
3635 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3637 "%s: unsupported mirror flags: %s\n",
3644 case LFS_LAYOUT_FOREIGN_OPT:
3646 /* check pure numeric */
3647 type = strtoul(optarg, &end, 0);
3650 type = check_foreign_type_name(optarg);
3651 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3653 "%s %s: unrecognized foreign type '%s'\n",
3658 } else if (type >= UINT32_MAX) {
3660 "%s %s: invalid foreign type '%s'\n",
3661 progname, argv[0], optarg);
3665 foreign_mode = true;
3670 mode = strtoul(mode_opt, &end, 8);
3673 "%s %s: bad mode '%s'\n",
3674 progname, argv[0], mode_opt);
3677 previous_umask = umask(0);
3680 case LFS_LAYOUT_COPY:
3685 if (!migrate_mode) {
3687 "%s %s: -b|--block valid only for migrate command\n",
3691 migration_block = true;
3694 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3696 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3700 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3704 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3705 if (errno != 0 || *end != '\0'||
3706 lsa.lsa_stripe_count < -1 ||
3707 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3709 "%s %s: invalid stripe count '%s'\n",
3710 progname, argv[0], optarg);
3714 if (lsa.lsa_stripe_count == -1)
3715 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3718 /* delete the default striping pattern */
3720 if (opc == SO_MIRROR_SPLIT) {
3723 "%s %s: -d cannot used with -f\n",
3727 mirror_flags |= MF_DESTROY;
3731 if (!migrate_mode) {
3733 "%s %s: -D|--non-direct is valid only for migrate command\n",
3737 migration_flags |= MIGRATION_NONDIRECT;
3740 if (lsa.lsa_comp_end != 0) {
3741 result = comp_args_to_layout(lpp, &lsa, true);
3743 fprintf(stderr, "%s: invalid layout\n",
3748 setstripe_args_init_inherit(&lsa);
3751 if (arg_is_eof(optarg)) {
3752 lsa.lsa_comp_end = LUSTRE_EOF;
3754 result = llapi_parse_size(optarg,
3757 /* assume units of KB if too small */
3758 if (lsa.lsa_comp_end < 4096)
3759 lsa.lsa_comp_end *= 1024;
3761 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3763 "%s %s: invalid component end '%s'\n",
3764 progname, argv[0], optarg);
3770 if (!migrate_mode) {
3772 "--mdt-hash is valid only for migrate command\n");
3776 lsa.lsa_pattern = check_hashtype(optarg);
3777 if (lsa.lsa_pattern == 0) {
3779 "%s %s: bad stripe hash type '%s'\n",
3780 progname, argv[0], optarg);
3786 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3787 if (errno != 0 || *end != '\0' ||
3788 lsa.lsa_stripe_off < -1 ||
3789 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3791 "%s %s: invalid stripe offset '%s'\n",
3792 progname, argv[0], optarg);
3795 if (lsa.lsa_stripe_off == -1)
3796 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3799 comp_id = strtoul(optarg, &end, 0);
3800 if (*end != '\0' || comp_id == 0 ||
3801 comp_id > LCME_ID_MAX) {
3803 "%s %s: invalid component ID '%s'\n",
3804 progname, argv[0], optarg);
3809 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3811 "error: %s: invalid option: %s\n",
3812 progname, argv[optopt + 1]);
3815 if (opc == SO_MIRROR_EXTEND) {
3818 "error: %s: '-N' must exist in front of '%s'\n",
3819 progname, argv[optopt + 1]);
3822 last_mirror->m_file = optarg;
3823 last_mirror->m_count = 1;
3827 mirror_list = lfs_mirror_alloc();
3828 mirror_list->m_file = optarg;
3833 if (strcmp(argv[optind - 1], "mdt") == 0) {
3834 /* Can be only the first component */
3838 "error: 'mdt' layout can be only the first one\n");
3841 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3844 "error: 'mdt' layout size is too big\n");
3847 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3848 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3851 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3857 if (!migrate_mode) {
3859 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3863 migrate_mdt_mode = true;
3864 lsa.lsa_nr_tgts = parse_targets(tgts,
3865 sizeof(tgts) / sizeof(__u32),
3866 lsa.lsa_nr_tgts, optarg, NULL);
3867 if (lsa.lsa_nr_tgts < 0) {
3869 "%s %s: invalid MDT target(s) '%s'\n",
3870 progname, argv[0], optarg);
3874 lsa.lsa_tgts = tgts;
3875 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3876 lsa.lsa_stripe_off = tgts[0];
3879 if (!migrate_mode) {
3881 "%s %s: -n|--non-block valid only for migrate command\n",
3885 migration_flags |= MIGRATION_NONBLOCK;
3888 if (opc == SO_SETSTRIPE) {
3889 opc = SO_MIRROR_CREATE;
3895 mirror_count = strtoul(optarg, &end, 0);
3896 if (errno != 0 || *end != '\0' ||
3897 mirror_count == 0 ||
3898 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3900 "error: %s: bad mirror count: %s\n",
3907 new_mirror = lfs_mirror_alloc();
3908 new_mirror->m_count = mirror_count;
3911 mirror_list = new_mirror;
3914 /* wrap up last mirror */
3915 if (!setstripe_args_specified(&lsa))
3916 last_mirror->m_inherit = true;
3917 if (lsa.lsa_comp_end == 0)
3918 lsa.lsa_comp_end = LUSTRE_EOF;
3920 result = comp_args_to_layout(lpp, &lsa, true);
3922 lfs_mirror_free(new_mirror);
3926 setstripe_args_init_inherit(&lsa);
3928 last_mirror->m_next = new_mirror;
3931 last_mirror = new_mirror;
3932 lpp = &last_mirror->m_layout;
3935 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3936 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3938 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3940 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3942 "%s %s: -o|--ost incompatible with DoM layout\n",
3947 * -o allows overstriping, and must note it because
3948 * parse_targets is shared with MDT striping, which
3949 * does not allow duplicates
3951 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3952 lsa.lsa_nr_tgts = parse_targets(tgts,
3953 sizeof(tgts) / sizeof(__u32),
3954 lsa.lsa_nr_tgts, optarg,
3956 if (lsa.lsa_nr_tgts < 0) {
3958 "%s %s: invalid OST target(s) '%s'\n",
3959 progname, argv[0], optarg);
3963 lsa.lsa_tgts = tgts;
3964 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3965 lsa.lsa_stripe_off = tgts[0];
3970 lsa.lsa_pool_name = optarg;
3972 if (strlen(lsa.lsa_pool_name) == 0 ||
3973 strncmp(lsa.lsa_pool_name, "none",
3974 LOV_MAXPOOLNAME) == 0)
3975 lsa.lsa_pool_name = NULL;
3978 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3980 /* assume units of KB if too small to be valid */
3981 if (lsa.lsa_stripe_size < 4096)
3982 lsa.lsa_stripe_size *= 1024;
3984 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3986 "%s %s: invalid stripe size '%s'\n",
3987 progname, argv[0], optarg);
3992 if (!migrate_mode) {
3994 "%s %s: -v|--verbose valid only for migrate command\n",
3998 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3999 migration_flags = MIGRATION_VERBOSE;
4009 result = llapi_parse_size(optarg,
4010 &lsa.lsa_extension_size,
4014 "%s %s: invalid extension size '%s'\n",
4015 progname, argv[0], optarg);
4019 lsa.lsa_extension_comp = true;
4022 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4023 progname, argv[0], argv[optind - 1]);
4028 fname = argv[optind];
4030 if (optind == argc) {
4031 fprintf(stderr, "%s %s: FILE must be specified\n",
4036 /* lfs migrate $filename should keep the file's layout by default */
4037 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
4041 if (xattr && !foreign_mode) {
4043 * only print a warning as this is harmless and will be ignored
4046 "%s %s: xattr has been specified for non-foreign layout\n",
4048 } else if (foreign_mode && !xattr) {
4050 "%s %s: xattr must be provided in foreign mode\n",
4055 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4056 comp_set || comp_id || delete || from_copy ||
4057 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4060 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4065 if (mirror_mode && mirror_count == 0) {
4067 "error: %s: --mirror-count|-N option is required\n",
4074 if (!setstripe_args_specified(&lsa))
4075 last_mirror->m_inherit = true;
4076 if (lsa.lsa_comp_end == 0)
4077 lsa.lsa_comp_end = LUSTRE_EOF;
4080 if (lsa.lsa_comp_end != 0) {
4081 result = comp_args_to_layout(lpp, &lsa, true);
4083 fprintf(stderr, "error: %s: invalid layout\n",
4090 if (mirror_flags & MF_NO_VERIFY) {
4091 if (opc != SO_MIRROR_EXTEND) {
4093 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4097 } else if (!has_m_file) {
4099 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4106 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4108 "%s %s: --component-set doesn't have component-id set\n",
4113 if ((delete + comp_set + comp_del + comp_add) > 1) {
4115 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4120 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4121 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4123 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4128 if ((comp_set || comp_del) &&
4129 (setstripe_args_specified(&lsa) || layout != NULL)) {
4131 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4136 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4138 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4143 if (comp_add || comp_del) {
4146 result = lstat(fname, &st);
4147 if (result == 0 && S_ISDIR(st.st_mode)) {
4149 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4156 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4165 "%s %s: option -E must be specified with --component-add\n",
4171 if (from_yaml && from_copy) {
4173 "%s: can't specify --yaml and --copy together\n",
4178 if ((from_yaml || from_copy) &&
4179 (setstripe_args_specified(&lsa) || layout != NULL)) {
4181 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4186 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4188 "%s %s: options --non-block and --block are mutually exclusive\n",
4193 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4194 opc != SO_MIRROR_DELETE && comp_id != 0) {
4196 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4201 if (migrate_mdt_mode) {
4202 struct lmv_user_md *lmu;
4204 /* initialize migrate mdt parameters */
4205 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4206 LMV_USER_MAGIC_SPECIFIC));
4209 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4210 progname, argv[0], strerror(ENOMEM));
4214 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4215 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4216 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4218 "%s %s: migrate should specify MDT index\n",
4223 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4224 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4225 lmu->lum_hash_type = lsa.lsa_pattern;
4227 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4228 if (lsa.lsa_pool_name) {
4229 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4230 sizeof(lmu->lum_pool_name) - 1);
4231 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4233 if (lsa.lsa_nr_tgts > 1) {
4236 if (lsa.lsa_stripe_count > 0 &&
4237 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4238 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4240 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4241 progname, lsa.lsa_stripe_count,
4247 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4248 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4249 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4250 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4252 lmu->lum_magic = LMV_USER_MAGIC;
4255 migrate_mdt_param.fp_lmv_md = lmu;
4256 migrate_mdt_param.fp_migrate = 1;
4257 } else if (!layout) {
4258 if (lsa_args_stripe_count_check(&lsa))
4261 /* initialize stripe parameters */
4262 param = calloc(1, offsetof(typeof(*param),
4263 lsp_osts[lsa.lsa_nr_tgts]));
4266 "%s %s: cannot allocate memory for parameters: %s\n",
4267 progname, argv[0], strerror(ENOMEM));
4272 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4273 param->lsp_stripe_size = lsa.lsa_stripe_size;
4274 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4275 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4276 param->lsp_stripe_count = -1;
4278 param->lsp_stripe_count = lsa.lsa_stripe_count;
4280 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4281 param->lsp_stripe_offset = -1;
4283 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4284 param->lsp_stripe_pattern =
4285 llapi_pattern_to_lov(lsa.lsa_pattern);
4286 if (param->lsp_stripe_pattern == EINVAL) {
4287 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4292 param->lsp_pool = lsa.lsa_pool_name;
4293 param->lsp_is_specific = false;
4295 if (lsa.lsa_nr_tgts > 0) {
4296 param->lsp_is_specific = true;
4297 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4298 memcpy(param->lsp_osts, tgts,
4299 sizeof(*tgts) * lsa.lsa_nr_tgts);
4304 /* generate a layout from a YAML template */
4305 result = lfs_comp_create_from_yaml(template, &layout,
4309 "error: %s: can't create composite layout from template file %s\n",
4315 if (layout != NULL || mirror_list != NULL) {
4317 result = mirror_adjust_first_extents(mirror_list);
4319 result = layout_adjust_first_extent(fname, layout,
4321 if (result == -ENODATA)
4323 else if (result != 0) {
4324 fprintf(stderr, "error: %s: invalid layout\n",
4330 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4332 layout = llapi_layout_get_by_path(template ?: fname, 0);
4335 "%s: can't create composite layout from file %s: %s\n",
4336 progname, template ?: fname,
4343 if (migrate_mdt_mode) {
4344 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4345 } else if (migrate_mode) {
4346 result = lfs_migrate(fname, migration_flags, param,
4348 } else if (comp_set != 0) {
4349 result = lfs_component_set(fname, comp_id,
4352 lsa.lsa_comp_neg_flags);
4353 } else if (comp_del != 0) {
4354 result = lfs_component_del(fname, comp_id,
4356 lsa.lsa_comp_neg_flags);
4357 } else if (comp_add != 0) {
4358 result = lfs_component_add(fname, layout);
4359 } else if (opc == SO_MIRROR_CREATE) {
4360 result = mirror_create(fname, mirror_list);
4361 } else if (opc == SO_MIRROR_EXTEND) {
4362 result = mirror_extend(fname, mirror_list,
4364 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4365 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4367 "%s: no mirror specified to delete from '%s'\n",
4371 if (lsa.lsa_pool_name)
4372 mirror_flags |= MF_COMP_POOL;
4373 else if (mirror_id != 0)
4374 comp_id = mirror_id;
4376 mirror_flags |= MF_COMP_ID;
4377 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4379 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4383 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4385 has_m_file ? mirror_list->m_file :
4387 } else if (layout) {
4388 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4394 } else if (foreign_mode) {
4395 result = llapi_file_create_foreign(fname, mode, type,
4402 result = llapi_file_open_param(fname,
4411 /* Save the first error encountered. */
4419 umask(previous_umask);
4422 free(migrate_mdt_param.fp_lmv_md);
4423 llapi_layout_free(layout);
4424 lfs_mirror_list_free(mirror_list);
4429 llapi_layout_free(layout);
4430 lfs_mirror_list_free(mirror_list);
4434 static int lfs_poollist(int argc, char **argv)
4439 return llapi_poollist(argv[1]);
4442 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4443 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4448 char *endptr = "AD";
4453 else if (str[0] == '-')
4459 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4460 long long val = strtoll(timebuf, &endptr, 0);
4465 unit *= 52; /* 52 weeks + 1 day below */
4466 case 'w': /* fallthrough */
4468 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4469 param->fp_time_margin *= (1 + unit / 52);
4470 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4471 case '\0': /* days are default unit if none used */
4472 case 'd': /* fallthrough */
4474 case 'h': /* fallthrough */
4476 case 'm': /* fallthrough */
4478 case 's': /* fallthrough */
4480 /* don't need to multiply by 1 for seconds */
4483 "%s find: bad time string '%s': %s\n",
4484 progname, timebuf, strerror(EINVAL));
4488 if (param->fp_time_margin == 0 ||
4489 (*endptr && unit < param->fp_time_margin))
4490 param->fp_time_margin = unit;
4497 fprintf(stderr, "%s find: bad time '%s': too large\n",
4507 static int str2quotaid(__u32 *id, const char *arg)
4509 unsigned long int projid_tmp = 0;
4510 char *endptr = NULL;
4512 projid_tmp = strtoul(arg, &endptr, 10);
4513 if (*endptr != '\0')
4515 if (projid_tmp >= UINT32_MAX)
4522 static int name2uid(unsigned int *id, const char *name)
4524 struct passwd *passwd;
4526 passwd = getpwnam(name);
4529 *id = passwd->pw_uid;
4534 static int name2gid(unsigned int *id, const char *name)
4536 struct group *group;
4538 group = getgrnam(name);
4541 *id = group->gr_gid;
4546 static inline int name2projid(unsigned int *id, const char *name)
4551 static int uid2name(char **name, unsigned int id)
4553 struct passwd *passwd;
4555 passwd = getpwuid(id);
4558 *name = passwd->pw_name;
4563 static inline int gid2name(char **name, unsigned int id)
4565 struct group *group;
4567 group = getgrgid(id);
4570 *name = group->gr_name;
4575 static int name2layout(__u32 *layout, char *name)
4577 char *ptr, *layout_name;
4580 for (ptr = name; ; ptr = NULL) {
4581 layout_name = strtok(ptr, ",");
4584 if (strcmp(layout_name, "released") == 0)
4585 *layout |= LOV_PATTERN_F_RELEASED;
4586 else if (strcmp(layout_name, "raid0") == 0)
4587 *layout |= LOV_PATTERN_RAID0;
4588 else if (strcmp(layout_name, "mdt") == 0)
4589 *layout |= LOV_PATTERN_MDT;
4590 else if (strcmp(layout_name, "overstriping") == 0)
4591 *layout |= LOV_PATTERN_OVERSTRIPING;
4598 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4601 int user, group, other;
4606 mode_t previous_flags;
4608 user = group = other = 0;
4628 user = group = other = 1;
4639 who = user || group || other;
4642 usermask = umask(0022);
4647 if (*input == '-' || *input == '+' || *input == '=')
4650 /* operation is required */
4653 /* get the flags in *outmode */
4656 previous_flags = (*outmode & 0700);
4657 perm |= user ? previous_flags : 0;
4658 perm |= group ? (previous_flags >> 3) : 0;
4659 perm |= other ? (previous_flags >> 6) : 0;
4663 previous_flags = (*outmode & 0070);
4664 perm |= user ? (previous_flags << 3) : 0;
4665 perm |= group ? previous_flags : 0;
4666 perm |= other ? (previous_flags >> 3) : 0;
4670 previous_flags = (*outmode & 0007);
4671 perm |= user ? (previous_flags << 6) : 0;
4672 perm |= group ? (previous_flags << 3) : 0;
4673 perm |= other ? previous_flags : 0;
4680 /* this part is optional,
4681 * if empty perm = 0 and *outmode is not modified
4688 perm |= user ? 0400 : 0;
4689 perm |= group ? 0040 : 0;
4690 perm |= other ? 0004 : 0;
4691 /* set read permission for uog except for umask's
4694 perm |= who ? 0 : (0444 & ~usermask);
4697 perm |= user ? 0200 : 0;
4698 perm |= group ? 0020 : 0;
4699 perm |= other ? 0002 : 0;
4700 /* set write permission for uog except for umask'
4703 perm |= who ? 0 : (0222 & ~usermask);
4706 perm |= user ? 0100 : 0;
4707 perm |= group ? 0010 : 0;
4708 perm |= other ? 0001 : 0;
4709 /* set execute permission for uog except for umask'
4712 perm |= who ? 0 : (0111 & ~usermask);
4716 * Adds execute permission to 'u', 'g' and/or 'g' if
4717 * specified and either 'u', 'g' or 'o' already has
4718 * execute permissions.
4720 if ((*outmode & 0111) != 0) {
4721 perm |= user ? 0100 : 0;
4722 perm |= group ? 0010 : 0;
4723 perm |= other ? 0001 : 0;
4724 perm |= !who ? 0111 : 0;
4728 /* s is ignored if o is given, but it's not an error */
4729 if (other && !group && !user)
4731 perm |= user ? S_ISUID : 0;
4732 perm |= group ? S_ISGID : 0;
4735 /* 't' should be used when 'a' is given
4738 perm |= (!who || all) ? S_ISVTX : 0;
4739 /* using ugo with t is not an error */
4750 /* uog flags should be only one character long */
4751 if (previous_flags && (*input != '\0' && *input != ','))
4756 /* remove the flags from outmode */
4760 /* add the flags to outmode */
4764 /* set the flags of outmode to perm */
4774 static int str2mode_t(const char *input, mode_t *outmode)
4781 if (*input >= '0' && *input <= '7') {
4782 /* parse octal representation */
4787 /* look for invalid digits in octal representation */
4788 while (isdigit(*iter))
4793 *outmode = strtoul(input, &end, 8);
4795 if (errno != 0 || *outmode > 07777) {
4800 } else if (*input == '8' || *input == '9') {
4801 /* error: invalid octal number */
4804 /* parse coma seperated list of symbolic representation */
4813 rc = parse_symbolic(input, outmode, &end);
4818 } while (*end == ',');
4826 static int lfs_find(int argc, char **argv)
4831 struct find_param param = {
4834 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4836 struct option long_opts[] = {
4837 { .val = 'A', .name = "atime", .has_arg = required_argument },
4838 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4839 { .val = 'B', .name = "btime", .has_arg = required_argument },
4840 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4841 { .val = LFS_COMP_COUNT_OPT,
4842 .name = "comp-count", .has_arg = required_argument },
4843 { .val = LFS_COMP_COUNT_OPT,
4844 .name = "component-count",
4845 .has_arg = required_argument },
4846 { .val = LFS_COMP_FLAGS_OPT,
4847 .name = "comp-flags", .has_arg = required_argument },
4848 { .val = LFS_COMP_FLAGS_OPT,
4849 .name = "component-flags",
4850 .has_arg = required_argument },
4851 { .val = LFS_COMP_START_OPT,
4852 .name = "comp-start", .has_arg = required_argument },
4853 { .val = LFS_COMP_START_OPT,
4854 .name = "component-start",
4855 .has_arg = required_argument },
4856 { .val = LFS_MIRROR_STATE_OPT,
4857 .name = "mirror-state", .has_arg = required_argument },
4858 { .val = LFS_NEWERXY_OPT,
4859 .name = "newer", .has_arg = required_argument},
4860 { .val = LFS_NEWERXY_OPT,
4861 .name = "neweraa", .has_arg = required_argument},
4862 { .val = LFS_NEWERXY_OPT,
4863 .name = "neweram", .has_arg = required_argument},
4864 { .val = LFS_NEWERXY_OPT,
4865 .name = "newerac", .has_arg = required_argument},
4866 { .val = LFS_NEWERXY_OPT,
4867 .name = "newerab", .has_arg = required_argument},
4868 { .val = LFS_NEWERXY_OPT,
4869 .name = "newerma", .has_arg = required_argument},
4870 { .val = LFS_NEWERXY_OPT,
4871 .name = "newermm", .has_arg = required_argument},
4872 { .val = LFS_NEWERXY_OPT,
4873 .name = "newermc", .has_arg = required_argument},
4874 { .val = LFS_NEWERXY_OPT,
4875 .name = "newermb", .has_arg = required_argument},
4876 { .val = LFS_NEWERXY_OPT,
4877 .name = "newerca", .has_arg = required_argument},
4878 { .val = LFS_NEWERXY_OPT,
4879 .name = "newercm", .has_arg = required_argument},
4880 { .val = LFS_NEWERXY_OPT,
4881 .name = "newercc", .has_arg = required_argument},
4882 { .val = LFS_NEWERXY_OPT,
4883 .name = "newercb", .has_arg = required_argument},
4884 { .val = LFS_NEWERXY_OPT,
4885 .name = "newerba", .has_arg = required_argument},
4886 { .val = LFS_NEWERXY_OPT,
4887 .name = "newerbm", .has_arg = required_argument},
4888 { .val = LFS_NEWERXY_OPT,
4889 .name = "newerbc", .has_arg = required_argument},
4890 { .val = LFS_NEWERXY_OPT,
4891 .name = "newerbb", .has_arg = required_argument},
4892 { .val = LFS_NEWERXY_OPT,
4893 .name = "newerBa", .has_arg = required_argument},
4894 { .val = LFS_NEWERXY_OPT,
4895 .name = "newerBm", .has_arg = required_argument},
4896 { .val = LFS_NEWERXY_OPT,
4897 .name = "newerBc", .has_arg = required_argument},
4898 { .val = LFS_NEWERXY_OPT,
4899 .name = "newerBB", .has_arg = required_argument},
4900 { .val = LFS_NEWERXY_OPT,
4901 .name = "newerat", .has_arg = required_argument},
4902 { .val = LFS_NEWERXY_OPT,
4903 .name = "newermt", .has_arg = required_argument},
4904 { .val = LFS_NEWERXY_OPT,
4905 .name = "newerct", .has_arg = required_argument},
4906 { .val = LFS_NEWERXY_OPT,
4907 .name = "newerbt", .has_arg = required_argument},
4908 { .val = LFS_NEWERXY_OPT,
4909 .name = "newerBt", .has_arg = required_argument},
4910 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4911 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4912 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4913 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4914 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4915 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4916 { .val = 'E', .name = "component-end",
4917 .has_arg = required_argument },
4918 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4919 { .val = LFS_LAYOUT_FOREIGN_OPT,
4920 .name = "foreign", .has_arg = optional_argument},
4921 { .val = 'g', .name = "gid", .has_arg = required_argument },
4922 { .val = 'G', .name = "group", .has_arg = required_argument },
4923 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4924 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4925 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4926 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4927 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4928 { .val = 'L', .name = "layout", .has_arg = required_argument },
4929 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4930 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4931 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4932 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4933 { .val = 'n', .name = "name", .has_arg = required_argument },
4934 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4935 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4936 { .val = 'O', .name = "obd", .has_arg = required_argument },
4937 { .val = 'O', .name = "ost", .has_arg = required_argument },
4938 /* no short option for pool yet, can be 'p' after 2.18 */
4939 { .val = LFS_POOL_OPT,
4940 .name = "pool", .has_arg = required_argument },
4941 { .val = '0', .name = "print0", .has_arg = no_argument },
4942 { .val = 'P', .name = "print", .has_arg = no_argument },
4943 { .val = LFS_PROJID_OPT,
4944 .name = "projid", .has_arg = required_argument },
4945 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4946 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4947 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4948 { .val = 's', .name = "size", .has_arg = required_argument },
4949 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4950 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4951 { .val = 't', .name = "type", .has_arg = required_argument },
4952 { .val = LFS_FIND_PERM,
4953 .name = "perm", .has_arg = required_argument },
4954 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4955 { .val = 'u', .name = "uid", .has_arg = required_argument },
4956 { .val = 'U', .name = "user", .has_arg = required_argument },
4957 { .val = 'z', .name = "extension-size",
4958 .has_arg = required_argument },
4959 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4960 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4961 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4975 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4976 while ((c = getopt_long_only(argc, argv,
4977 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4978 long_opts, &optidx)) >= 0) {
4983 /* '!' is part of option */
4985 * when getopt_long_only() finds a string which is not
4986 * an option nor a known option argument it returns 1
4987 * in that case if we already have found pathstart and pathend
4988 * (i.e. we have the list of pathnames),
4989 * the only supported value is "!"
4991 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4992 if (!isoption && pathend != -1) {
4994 "err: %s: filename|dirname must either precede options or follow options\n",
4999 if (!isoption && pathstart == -1)
5000 pathstart = optind - 1;
5001 if (isoption && pathstart != -1 && pathend == -1)
5002 pathend = optind - 2;
5009 * unknown; opt is "!" or path component,
5010 * checking done above.
5012 if (strcmp(optarg, "!") == 0)
5016 xtime = ¶m.fp_atime;
5017 xsign = ¶m.fp_asign;
5018 param.fp_exclude_atime = !!neg_opt;
5019 /* no break, this falls through to 'B' for btime */
5022 xtime = ¶m.fp_btime;
5023 xsign = ¶m.fp_bsign;
5024 param.fp_exclude_btime = !!neg_opt;
5026 /* no break, this falls through to 'C' for ctime */
5029 xtime = ¶m.fp_ctime;
5030 xsign = ¶m.fp_csign;
5031 param.fp_exclude_ctime = !!neg_opt;
5033 /* no break, this falls through to 'M' for mtime */
5036 xtime = ¶m.fp_mtime;
5037 xsign = ¶m.fp_msign;
5038 param.fp_exclude_mtime = !!neg_opt;
5040 rc = set_time(¶m, &t, xtime, optarg);
5041 if (rc == LONG_MAX) {
5049 if (optarg[0] == '+') {
5050 param.fp_blocks_sign = -1;
5052 } else if (optarg[0] == '-') {
5053 param.fp_blocks_sign = 1;
5057 param.fp_blocks_units = 1024;
5058 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5059 ¶m.fp_blocks_units, 0);
5061 fprintf(stderr, "error: bad blocks '%s'\n",
5065 param.fp_check_blocks = 1;
5066 param.fp_exclude_blocks = !!neg_opt;
5068 case LFS_COMP_COUNT_OPT:
5069 if (optarg[0] == '+') {
5070 param.fp_comp_count_sign = -1;
5072 } else if (optarg[0] == '-') {
5073 param.fp_comp_count_sign = 1;
5078 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5079 if (errno != 0 || *endptr != '\0' ||
5080 param.fp_comp_count > UINT32_MAX) {
5082 "error: bad component count '%s'\n",
5086 param.fp_check_comp_count = 1;
5087 param.fp_exclude_comp_count = !!neg_opt;
5089 case LFS_COMP_FLAGS_OPT:
5090 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5091 ¶m.fp_comp_neg_flags);
5094 "error: bad component flags '%s'\n",
5098 param.fp_check_comp_flags = 1;
5100 __u32 flags = param.fp_comp_neg_flags;
5102 param.fp_comp_neg_flags = param.fp_comp_flags;
5103 param.fp_comp_flags = flags;
5106 case LFS_COMP_START_OPT:
5107 if (optarg[0] == '+') {
5108 param.fp_comp_start_sign = -1;
5110 } else if (optarg[0] == '-') {
5111 param.fp_comp_start_sign = 1;
5115 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5116 ¶m.fp_comp_start_units, 0);
5119 "error: bad component start '%s'\n",
5123 param.fp_check_comp_start = 1;
5124 param.fp_exclude_comp_start = !!neg_opt;
5126 case LFS_MIRROR_STATE_OPT:
5127 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5128 ¶m.fp_mirror_neg_state);
5131 "error: bad mirrored file state '%s'\n",
5135 param.fp_check_mirror_state = 1;
5137 __u16 state = param.fp_mirror_neg_state;
5139 param.fp_mirror_neg_state =
5140 param.fp_mirror_state;
5141 param.fp_mirror_state = state;
5145 if (optarg[0] == '+') {
5146 param.fp_stripe_count_sign = -1;
5148 } else if (optarg[0] == '-') {
5149 param.fp_stripe_count_sign = 1;
5154 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5155 if (errno != 0 || *endptr != '\0' ||
5156 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5158 "error: bad stripe_count '%s'\n",
5163 param.fp_check_stripe_count = 1;
5164 param.fp_exclude_stripe_count = !!neg_opt;
5168 param.fp_max_depth = strtol(optarg, 0, 0);
5169 if (errno != 0 || param.fp_max_depth < 0) {
5171 "error: bad maxdepth '%s'\n",
5178 if (optarg[0] == '+') {
5179 param.fp_comp_end_sign = -1;
5181 } else if (optarg[0] == '-') {
5182 param.fp_comp_end_sign = 1;
5186 if (arg_is_eof(optarg)) {
5187 param.fp_comp_end = LUSTRE_EOF;
5188 param.fp_comp_end_units = 1;
5191 rc = llapi_parse_size(optarg,
5193 ¶m.fp_comp_end_units, 0);
5194 /* assume units of KB if too small */
5195 if (param.fp_comp_end < 4096)
5196 param.fp_comp_end *= 1024;
5200 "error: bad component end '%s'\n",
5204 param.fp_check_comp_end = 1;
5205 param.fp_exclude_comp_end = !!neg_opt;
5207 case LFS_LAYOUT_FOREIGN_OPT: {
5208 /* all types by default */
5209 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5212 /* check pure numeric */
5213 type = strtoul(optarg, &endptr, 0);
5216 type = check_foreign_type_name(optarg);
5217 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5219 "%s %s: unknown foreign type '%s'\n",
5224 } else if (type >= UINT32_MAX) {
5226 "%s %s: invalid foreign type '%s'\n",
5227 progname, argv[0], optarg);
5231 param.fp_foreign_type = type;
5232 param.fp_check_foreign = 1;
5233 param.fp_exclude_foreign = !!neg_opt;
5236 case LFS_NEWERXY_OPT: {
5242 time_t ref = time(NULL);
5244 /* no need to check bad options, they won't get here */
5245 if (strlen(long_opts[optidx].name) == 7) {
5246 x = long_opts[optidx].name[5];
5247 y = long_opts[optidx].name[6];
5251 static const char *const fmts[] = {
5252 "%Y-%m-%d %H:%M:%S",
5255 "%H:%M:%S", /* sometime today */
5264 for (i = 0; fmts[i] != NULL; i++) {
5267 /* Init for times relative to today */
5268 if (strncmp(fmts[i], "%H", 2) == 0)
5269 localtime_r(&ref, &tm);
5271 memset(&tm, 0, sizeof(tm));
5272 ptr = strptime(optarg, fmts[i], &tm);
5274 while (ptr && isspace(*ptr))
5276 if (ptr == optarg + strlen(optarg)) {
5284 "%s: invalid time '%s'\n",
5287 "supported formats are:\n ");
5288 for (i = 0; fmts[i] != NULL; i++)
5289 fprintf(stderr, "'%s', ",
5291 fprintf(stderr, "\n");
5297 } else if (y == 'b' || y == 'B') {
5300 rc = llapi_get_lum_file(optarg, NULL, &stx,
5302 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5303 if (!(stx.stx_mask & STATX_BTIME))
5308 "%s: get btime failed '%s': %s\n",
5314 ref = stx.stx_btime.tv_sec;
5316 struct stat statbuf;
5318 if (stat(optarg, &statbuf) < 0) {
5320 "%s: cannot stat file '%s': %s\n",
5329 ref = statbuf.st_atime;
5332 ref = statbuf.st_mtime;
5335 ref = statbuf.st_ctime;
5339 "%s: invalid Y argument: '%c'\n",
5348 xidx = NEWERXY_ATIME;
5351 xidx = NEWERXY_MTIME;
5354 xidx = NEWERXY_CTIME;
5358 xidx = NEWERXY_BTIME;
5362 "%s: invalid X argument: '%c'\n",
5369 newery = ¶m.fp_newery[xidx][negidx];
5375 *newery = *newery > ref ? ref : *newery;
5377 *newery = *newery > ref ? *newery : ref;
5379 param.fp_newerxy = 1;
5384 rc = name2gid(¶m.fp_gid, optarg);
5386 if (str2quotaid(¶m.fp_gid, optarg)) {
5388 "Group/GID: %s cannot be found.\n",
5394 param.fp_exclude_gid = !!neg_opt;
5395 param.fp_check_gid = 1;
5398 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5399 ¶m.fp_hash_exflags,
5400 ¶m.fp_hash_type);
5405 if (param.fp_hash_inflags || param.fp_hash_exflags)
5406 param.fp_check_hash_flag = 1;
5407 param.fp_exclude_hash_type = !!neg_opt;
5413 ret = name2layout(¶m.fp_layout, optarg);
5416 param.fp_exclude_layout = !!neg_opt;
5417 param.fp_check_layout = 1;
5421 rc = name2uid(¶m.fp_uid, optarg);
5423 if (str2quotaid(¶m.fp_uid, optarg)) {
5425 "User/UID: %s cannot be found.\n",
5431 param.fp_exclude_uid = !!neg_opt;
5432 param.fp_check_uid = 1;
5435 param.fp_pattern = (char *)optarg;
5436 param.fp_exclude_pattern = !!neg_opt;
5439 if (optarg[0] == '+') {
5440 param.fp_mirror_count_sign = -1;
5442 } else if (optarg[0] == '-') {
5443 param.fp_mirror_count_sign = 1;
5448 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5449 if (errno != 0 || *endptr != '\0' ||
5450 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5452 "error: bad mirror count '%s'\n",
5456 param.fp_check_mirror_count = 1;
5457 param.fp_exclude_mirror_count = !!neg_opt;
5462 char *buf, *token, *next, *p;
5466 buf = strdup(optarg);
5472 param.fp_exclude_obd = !!neg_opt;
5475 while (token && *token) {
5476 token = strchr(token, ',');
5483 param.fp_exclude_mdt = !!neg_opt;
5484 param.fp_num_alloc_mdts += len;
5485 tmp = realloc(param.fp_mdt_uuid,
5486 param.fp_num_alloc_mdts *
5487 sizeof(*param.fp_mdt_uuid));
5493 param.fp_mdt_uuid = tmp;
5495 param.fp_exclude_obd = !!neg_opt;
5496 param.fp_num_alloc_obds += len;
5497 tmp = realloc(param.fp_obd_uuid,
5498 param.fp_num_alloc_obds *
5499 sizeof(*param.fp_obd_uuid));
5505 param.fp_obd_uuid = tmp;
5507 for (token = buf; token && *token; token = next) {
5508 struct obd_uuid *puuid;
5512 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5515 ¶m.fp_obd_uuid[param.fp_num_obds++];
5517 p = strchr(token, ',');
5524 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5529 strncpy(puuid->uuid, token,
5530 sizeof(puuid->uuid));
5537 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5541 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5543 "Pool name %s is too long (max %d)\n",
5544 optarg, LOV_MAXPOOLNAME);
5549 * We do check for empty pool because empty pool
5550 * is used to find V1 LOV attributes
5552 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5553 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5554 param.fp_exclude_pool = !!neg_opt;
5555 param.fp_check_pool = 1;
5557 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5558 case 'p': /* want this for --pool, to match getstripe/find */
5560 "warning: -p deprecated, use --print0 or -0\n");
5563 param.fp_zero_end = 1;
5565 case 'P': /* we always print, this option is a no-op */
5567 case LFS_PROJID_OPT:
5568 rc = name2projid(¶m.fp_projid, optarg);
5570 if (str2quotaid(¶m.fp_projid, optarg)) {
5572 "Invalid project ID: %s\n",
5578 param.fp_exclude_projid = !!neg_opt;
5579 param.fp_check_projid = 1;
5582 if (optarg[0] == '+') {
5583 param.fp_size_sign = -1;
5585 } else if (optarg[0] == '-') {
5586 param.fp_size_sign = 1;
5590 ret = llapi_parse_size(optarg, ¶m.fp_size,
5591 ¶m.fp_size_units, 0);
5593 fprintf(stderr, "error: bad file size '%s'\n",
5597 param.fp_check_size = 1;
5598 param.fp_exclude_size = !!neg_opt;
5601 if (optarg[0] == '+') {
5602 param.fp_stripe_size_sign = -1;
5604 } else if (optarg[0] == '-') {
5605 param.fp_stripe_size_sign = 1;
5609 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5610 ¶m.fp_stripe_size_units, 0);
5611 /* assume units of KB if too small to be valid */
5612 if (param.fp_stripe_size < 4096)
5613 param.fp_stripe_size *= 1024;
5615 fprintf(stderr, "error: bad stripe_size '%s'\n",
5619 param.fp_check_stripe_size = 1;
5620 param.fp_exclude_stripe_size = !!neg_opt;
5623 param.fp_exclude_type = !!neg_opt;
5624 switch (optarg[0]) {
5626 param.fp_type = S_IFBLK;
5629 param.fp_type = S_IFCHR;
5632 param.fp_type = S_IFDIR;
5635 param.fp_type = S_IFREG;
5638 param.fp_type = S_IFLNK;
5641 param.fp_type = S_IFIFO;
5644 param.fp_type = S_IFSOCK;
5647 fprintf(stderr, "error: %s: bad type '%s'\n",
5654 param.fp_exclude_perm = !!neg_opt;
5655 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5656 if (*optarg == '/') {
5657 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5659 } else if (*optarg == '-') {
5660 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5664 if (str2mode_t(optarg, ¶m.fp_perm)) {
5665 fprintf(stderr, "error: invalid mode '%s'\n",
5672 if (optarg[0] == '+') {
5673 param.fp_mdt_count_sign = -1;
5675 } else if (optarg[0] == '-') {
5676 param.fp_mdt_count_sign = 1;
5681 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5682 if (errno != 0 || *endptr != '\0' ||
5683 param.fp_mdt_count >= UINT32_MAX) {
5684 fprintf(stderr, "error: bad mdt_count '%s'\n",
5689 param.fp_check_mdt_count = 1;
5690 param.fp_exclude_mdt_count = !!neg_opt;
5693 if (optarg[0] == '+') {
5694 param.fp_ext_size_sign = -1;
5696 } else if (optarg[0] == '-') {
5697 param.fp_ext_size_sign = 1;
5701 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5702 ¶m.fp_ext_size_units, 0);
5704 fprintf(stderr, "error: bad ext-size '%s'\n",
5708 param.fp_ext_size /= SEL_UNIT_SIZE;
5709 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5710 param.fp_check_ext_size = 1;
5711 param.fp_exclude_ext_size = !!neg_opt;
5719 if (pathstart == -1) {
5720 fprintf(stderr, "error: %s: no filename|pathname\n",
5724 } else if (pathend == -1) {
5730 rc = llapi_find(argv[pathstart], ¶m);
5733 pathbad = pathstart;
5735 } while (++pathstart < pathend);
5738 fprintf(stderr, "%s: failed for '%s': %s\n",
5739 progname, argv[pathbad], strerror(-rc));
5742 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5743 free(param.fp_obd_uuid);
5745 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5746 free(param.fp_mdt_uuid);
5751 static int lfs_getstripe_internal(int argc, char **argv,
5752 struct find_param *param)
5754 struct option long_opts[] = {
5755 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5756 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5757 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5758 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5759 { .val = LFS_COMP_COUNT_OPT,
5760 .name = "comp-count", .has_arg = no_argument },
5761 { .val = LFS_COMP_COUNT_OPT,
5762 .name = "component-count", .has_arg = no_argument },
5763 { .val = LFS_COMP_FLAGS_OPT,
5764 .name = "comp-flags", .has_arg = optional_argument },
5765 { .val = LFS_COMP_FLAGS_OPT,
5766 .name = "component-flags", .has_arg = optional_argument },
5767 { .val = LFS_COMP_START_OPT,
5768 .name = "comp-start", .has_arg = optional_argument },
5769 { .val = LFS_COMP_START_OPT,
5770 .name = "component-start", .has_arg = optional_argument },
5771 { .val = LFS_MIRROR_INDEX_OPT,
5772 .name = "mirror-index", .has_arg = required_argument },
5773 { .val = LFS_MIRROR_ID_OPT,
5774 .name = "mirror-id", .has_arg = required_argument },
5775 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5776 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5777 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5778 { .val = 'd', .name = "directory", .has_arg = no_argument },
5779 { .val = 'D', .name = "default", .has_arg = no_argument },
5780 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5781 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5782 { .val = 'F', .name = "fid", .has_arg = no_argument },
5783 { .val = 'g', .name = "generation", .has_arg = no_argument },
5784 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5785 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5786 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5787 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5788 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5789 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5790 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5791 { .val = 'L', .name = "layout", .has_arg = no_argument },
5792 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5793 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5794 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5795 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5796 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5797 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5798 { .val = 'O', .name = "obd", .has_arg = required_argument },
5799 { .val = 'O', .name = "ost", .has_arg = required_argument },
5800 { .val = 'p', .name = "pool", .has_arg = no_argument },
5801 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5802 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5803 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5804 { .val = 'R', .name = "raw", .has_arg = no_argument },
5805 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5806 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5807 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5808 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5809 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5810 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5811 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5812 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5813 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5814 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5815 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5819 int pathstart = -1, pathend = -1;
5823 while ((c = getopt_long(argc, argv,
5824 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5825 long_opts, NULL)) != -1) {
5829 /* '!' is part of option */
5830 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5831 if (!isoption && pathend != -1) {
5833 "error: %s: filename|dirname must either precede options or follow options\n",
5837 if (!isoption && pathstart == -1)
5838 pathstart = optind - 1;
5839 if (isoption && pathstart != -1 && pathend == -1)
5840 pathend = optind - 2;
5844 /* unknown: opt is "!" */
5845 if (strcmp(optarg, "!") == 0)
5849 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5850 param->fp_verbose |= VERBOSE_COUNT;
5851 param->fp_max_depth = 0;
5854 case LFS_COMP_COUNT_OPT:
5855 param->fp_verbose |= VERBOSE_COMP_COUNT;
5856 param->fp_max_depth = 0;
5858 case LFS_COMP_FLAGS_OPT:
5860 rc = comp_str2flags(optarg,
5861 ¶m->fp_comp_flags,
5862 ¶m->fp_comp_neg_flags);
5865 "error: %s bad component flags '%s'.\n",
5869 param->fp_check_comp_flags = 1;
5871 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5872 param->fp_max_depth = 0;
5875 case LFS_COMP_START_OPT:
5878 if (tmp[0] == '+') {
5879 param->fp_comp_start_sign = -1;
5881 } else if (tmp[0] == '-') {
5882 param->fp_comp_start_sign = 1;
5885 rc = llapi_parse_size(tmp,
5886 ¶m->fp_comp_start,
5887 ¶m->fp_comp_start_units, 0);
5890 "error: %s bad component start '%s'.\n",
5894 param->fp_check_comp_start = 1;
5896 param->fp_verbose |= VERBOSE_COMP_START;
5897 param->fp_max_depth = 0;
5900 case LFS_MIRROR_INDEX_OPT: {
5901 unsigned long int mirror_index;
5903 if (optarg[0] == '+') {
5904 param->fp_mirror_index_sign = -1;
5906 } else if (optarg[0] == '-') {
5907 param->fp_mirror_index_sign = 1;
5912 mirror_index = strtoul(optarg, &end, 0);
5913 if (errno != 0 || *end != '\0' ||
5914 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5915 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5917 "%s %s: invalid mirror index '%s'\n",
5918 progname, argv[0], optarg);
5922 param->fp_mirror_index = (__u16)mirror_index;
5924 if (param->fp_mirror_id != 0) {
5926 "%s %s: can't specify both mirror index and mirror ID\n",
5930 param->fp_check_mirror_index = 1;
5931 param->fp_exclude_mirror_index = !!neg_opt;
5934 case LFS_MIRROR_ID_OPT: {
5935 unsigned long int mirror_id;
5937 if (optarg[0] == '+') {
5938 param->fp_mirror_id_sign = -1;
5940 } else if (optarg[0] == '-') {
5941 param->fp_mirror_id_sign = 1;
5946 mirror_id = strtoul(optarg, &end, 0);
5947 if (errno != 0 || *end != '\0' ||
5948 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5949 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5951 "%s %s: invalid mirror ID '%s'\n",
5952 progname, argv[0], optarg);
5956 param->fp_mirror_id = (__u16)mirror_id;
5958 if (param->fp_mirror_index != 0) {
5960 "%s %s: can't specify both mirror index and mirror ID\n",
5964 param->fp_check_mirror_id = 1;
5965 param->fp_exclude_mirror_id = !!neg_opt;
5969 param->fp_max_depth = 0;
5972 param->fp_get_default_lmv = 1;
5977 if (tmp[0] == '+') {
5978 param->fp_comp_end_sign = -1;
5980 } else if (tmp[0] == '-') {
5981 param->fp_comp_end_sign = 1;
5985 if (arg_is_eof(tmp)) {
5986 param->fp_comp_end = LUSTRE_EOF;
5987 param->fp_comp_end_units = 1;
5990 rc = llapi_parse_size(tmp,
5991 ¶m->fp_comp_end,
5992 ¶m->fp_comp_end_units, 0);
5993 /* assume units of KB if too small */
5994 if (param->fp_comp_end < 4096)
5995 param->fp_comp_end *= 1024;
5999 "error: %s bad component end '%s'.\n",
6003 param->fp_check_comp_end = 1;
6005 param->fp_verbose |= VERBOSE_COMP_END;
6006 param->fp_max_depth = 0;
6010 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6011 param->fp_verbose |= VERBOSE_DFID;
6012 param->fp_max_depth = 0;
6016 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6017 param->fp_verbose |= VERBOSE_GENERATION;
6018 param->fp_max_depth = 0;
6022 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6023 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6024 param->fp_max_depth = 0;
6029 param->fp_comp_id = strtoul(optarg, &end, 0);
6030 if (*end != '\0' || param->fp_comp_id == 0 ||
6031 param->fp_comp_id > LCME_ID_MAX) {
6033 "error: %s bad component id '%s'\n",
6037 param->fp_check_comp_id = 1;
6039 param->fp_max_depth = 0;
6040 param->fp_verbose |= VERBOSE_COMP_ID;
6044 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6045 param->fp_verbose |= VERBOSE_PATTERN;
6046 param->fp_max_depth = 0;
6049 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6052 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6055 if (!(param->fp_verbose & VERBOSE_DETAIL))
6056 param->fp_max_depth = 0;
6057 param->fp_verbose |= VERBOSE_MDTINDEX;
6060 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6061 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6062 param->fp_max_depth = 0;
6066 if (param->fp_obd_uuid) {
6068 "error: %s: only one obduuid allowed",
6072 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6075 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6076 param->fp_verbose |= VERBOSE_POOL;
6077 param->fp_max_depth = 0;
6084 param->fp_recursive = 1;
6090 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6091 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6092 param->fp_max_depth = 0;
6096 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6102 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6103 param->fp_verbose |= VERBOSE_EXT_SIZE;
6104 param->fp_max_depth = 0;
6112 if (pathstart == -1) {
6113 fprintf(stderr, "error: %s: no filename|pathname\n",
6116 } else if (pathend == -1) {
6124 if (param->fp_recursive)
6125 param->fp_max_depth = -1;
6126 else if (param->fp_verbose & VERBOSE_DETAIL)
6127 param->fp_max_depth = 1;
6129 if (!param->fp_verbose)
6130 param->fp_verbose = VERBOSE_DEFAULT;
6131 if (param->fp_quiet)
6132 param->fp_verbose = VERBOSE_OBJID;
6135 rc = llapi_getstripe(argv[pathstart], param);
6136 } while (++pathstart < pathend && !rc);
6139 fprintf(stderr, "error: %s failed for %s.\n",
6140 argv[0], argv[optind - 1]);
6144 static int lfs_tgts(int argc, char **argv)
6146 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6147 struct find_param param;
6148 int index = 0, rc = 0;
6153 if (argc == 2 && !realpath(argv[1], path)) {
6155 fprintf(stderr, "error: invalid path '%s': %s\n",
6156 argv[1], strerror(-rc));
6160 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6161 /* Check if we have a mount point */
6162 if (mntdir[0] == '\0')
6165 memset(¶m, 0, sizeof(param));
6166 if (!strcmp(argv[0], "mdts"))
6167 param.fp_get_lmv = 1;
6169 rc = llapi_ostlist(mntdir, ¶m);
6171 fprintf(stderr, "error: %s: failed on %s\n",
6174 if (path[0] != '\0')
6176 memset(mntdir, 0, PATH_MAX);
6182 static int lfs_getstripe(int argc, char **argv)
6184 struct find_param param = { 0 };
6186 param.fp_max_depth = 1;
6187 return lfs_getstripe_internal(argc, argv, ¶m);
6191 static int lfs_getdirstripe(int argc, char **argv)
6193 struct find_param param = { 0 };
6194 struct option long_opts[] = {
6195 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6196 { .val = 'D', .name = "default", .has_arg = no_argument },
6197 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6198 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6199 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6200 { .val = 'O', .name = "obd", .has_arg = required_argument },
6201 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6202 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6203 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6204 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6205 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6206 { .val = LFS_INHERIT_RR_OPT,
6207 .name = "max-inherit-rr", .has_arg = no_argument },
6211 param.fp_get_lmv = 1;
6213 while ((c = getopt_long(argc, argv,
6214 "cDHimO:rtTvXy", long_opts, NULL)) != -1) {
6218 param.fp_verbose |= VERBOSE_COUNT;
6221 param.fp_get_default_lmv = 1;
6225 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6227 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6230 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6233 param.fp_verbose |= VERBOSE_HASH_TYPE;
6236 if (param.fp_obd_uuid) {
6238 "error: %s: only one obduuid allowed",
6242 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6245 param.fp_recursive = 1;
6248 param.fp_verbose |= VERBOSE_DETAIL;
6251 param.fp_verbose |= VERBOSE_INHERIT;
6253 case LFS_INHERIT_RR_OPT:
6254 param.fp_verbose |= VERBOSE_INHERIT_RR;
6260 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6261 progname, argv[0], argv[optind - 1]);
6269 if (param.fp_recursive)
6270 param.fp_max_depth = -1;
6272 if (!param.fp_verbose)
6273 param.fp_verbose = VERBOSE_DEFAULT;
6276 rc = llapi_getstripe(argv[optind], ¶m);
6277 } while (++optind < argc && !rc);
6280 fprintf(stderr, "error: %s failed for %s.\n",
6281 argv[0], argv[optind - 1]);
6286 MNTDF_INODES = 0x0001,
6287 MNTDF_COOKED = 0x0002,
6288 MNTDF_LAZY = 0x0004,
6289 MNTDF_VERBOSE = 0x0008,
6290 MNTDF_SHOW = 0x0010,
6291 MNTDF_DECIMAL = 0x0020,
6294 #define COOK(value, base) \
6297 while (value > base) { \
6305 #define CDF "%11llu"
6306 #define HDF "%8.1f%c"
6310 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6312 double avail, used, ratio = 0;
6315 avail = st->os_ffree;
6316 used = st->os_files - st->os_ffree;
6318 avail = st->os_bavail;
6319 used = st->os_blocks - st->os_bfree;
6321 if (avail + used > 0)
6322 ratio = used / (used + avail) * 100;
6324 /* Round up to match df(1) usage percentage */
6325 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6329 * This is to identify various problem states for "lfs df" if .osn_err = true,
6330 * so only show flags reflecting those states by default. Informational states
6331 * are only shown with "-v" and use lower-case names to distinguish them.
6332 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6334 static struct obd_statfs_state_names {
6335 enum obd_statfs_state osn_state;
6336 const char osn_name;
6339 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6340 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6341 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6342 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6343 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6344 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6345 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6346 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6347 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6350 static int showdf(char *mntdir, struct obd_statfs *stat,
6351 char *uuid, enum mntdf_flags flags,
6352 char *type, int index, int rc)
6354 long long avail, used, total;
6356 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6357 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6358 char tbuf[3 * sizeof(__u64)];
6359 char ubuf[3 * sizeof(__u64)];
6360 char abuf[3 * sizeof(__u64)];
6361 char rbuf[3 * sizeof(__u64)];
6368 if (flags & MNTDF_INODES) {
6369 avail = stat->os_ffree;
6370 used = stat->os_files - stat->os_ffree;
6371 total = stat->os_files;
6373 int shift = flags & MNTDF_COOKED ? 0 : 10;
6375 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6376 used = ((stat->os_blocks - stat->os_bfree) *
6377 stat->os_bsize) >> shift;
6378 total = (stat->os_blocks * stat->os_bsize) >> shift;
6381 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6383 if (flags & MNTDF_COOKED) {
6384 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6388 cook_val = (double)total;
6389 i = COOK(cook_val, base);
6391 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6394 snprintf(tbuf, sizeof(tbuf), CDF, total);
6396 cook_val = (double)used;
6397 i = COOK(cook_val, base);
6399 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6402 snprintf(ubuf, sizeof(ubuf), CDF, used);
6404 cook_val = (double)avail;
6405 i = COOK(cook_val, base);
6407 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6410 snprintf(abuf, sizeof(abuf), CDF, avail);
6412 snprintf(tbuf, sizeof(tbuf), CDF, total);
6413 snprintf(ubuf, sizeof(tbuf), CDF, used);
6414 snprintf(abuf, sizeof(tbuf), CDF, avail);
6417 sprintf(rbuf, RDF, ratio);
6418 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6419 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6421 printf("[%s:%d]", type, index);
6423 if (stat->os_state) {
6427 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6428 if (oss_names[i].osn_state & stat->os_state &&
6429 (oss_names[i].osn_err ||
6430 flags & MNTDF_VERBOSE))
6431 printf("%c", oss_names[i].osn_name);
6438 printf(UUF": inactive device\n", uuid);
6441 printf(UUF": %s\n", uuid, strerror(-rc));
6448 struct ll_stat_type {
6453 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6455 struct ll_statfs_data {
6457 struct obd_statfs sd_st;
6460 struct ll_statfs_buf {
6462 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6465 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6466 int ops, struct ll_statfs_buf *lsb)
6468 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6469 struct obd_uuid uuid_buf;
6470 char *poolname = NULL;
6471 struct ll_stat_type types[] = {
6472 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6473 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6474 { .st_name = NULL } };
6475 struct ll_stat_type *tp;
6476 __u64 ost_files = 0;
6477 __u64 ost_ffree = 0;
6485 poolname = strchr(pool, '.');
6487 if (strncmp(fsname, pool, strlen(fsname))) {
6488 fprintf(stderr, "filesystem name incorrect\n");
6496 fd = open(mntdir, O_RDONLY);
6499 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6504 if (flags & MNTDF_SHOW) {
6505 if (flags & MNTDF_INODES)
6506 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6507 "UUID", "Inodes", "IUsed", "IFree",
6508 "IUse%", "Mounted on");
6510 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6512 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6513 "Used", "Available", "Use%", "Mounted on");
6516 for (tp = types; tp->st_name != NULL; tp++) {
6517 bool have_ost = false;
6519 if (!(tp->st_op & ops))
6522 for (index = 0; ; index++) {
6523 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6524 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6525 type = flags & MNTDF_LAZY ?
6526 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6527 rc2 = llapi_obd_fstatfs(fd, type, index,
6528 &stat_buf, &uuid_buf);
6533 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6534 if (!(flags & MNTDF_VERBOSE))
6536 } else if (rc2 < 0 && rc == 0) {
6541 * If we have OSTs then don't report MDT block counts.
6542 * For MDT-only filesystems the expectation is that all
6543 * layouts have a DoM component. For filesystems with
6544 * OSTs, files are not necessarily going to store data
6545 * on MDTs, and MDT space is limited to a fraction of
6546 * OST space, so don't include it in the summary.
6548 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6555 if (poolname && tp->st_op == LL_STATFS_LOV &&
6556 llapi_search_ost(fsname, poolname,
6557 obd_uuid2str(&uuid_buf)) != 1)
6561 * the llapi_obd_fstatfs() call may have returned with
6562 * an error, but if it filled in uuid_buf we will at
6563 * lease use that to print out a message for that OBD.
6564 * If we didn't get anything in the uuid_buf, then fill
6565 * it in so that we can print an error message.
6567 if (uuid_buf.uuid[0] == '\0')
6568 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6569 "%s%04x", tp->st_name, index);
6571 lsb->sb_buf[lsb->sb_count].sd_index = index;
6572 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6575 if (flags & MNTDF_SHOW)
6576 showdf(mntdir, &stat_buf,
6577 obd_uuid2str(&uuid_buf), flags,
6578 tp->st_name, index, rc2);
6583 if (tp->st_op == LL_STATFS_LMV) {
6584 sum.os_ffree += stat_buf.os_ffree;
6585 sum.os_files += stat_buf.os_files;
6586 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6587 ost_files += stat_buf.os_files;
6588 ost_ffree += stat_buf.os_ffree;
6590 sum.os_blocks += stat_buf.os_blocks *
6592 sum.os_bfree += stat_buf.os_bfree *
6594 sum.os_bavail += stat_buf.os_bavail *
6602 * If we have _some_ OSTs, but don't have as many free objects on the
6603 * OST as inodes on the MDTs, reduce the reported number of inodes
6604 * to compensate, so that the "inodes in use" number is correct.
6605 * This should be kept in sync with ll_statfs_internal().
6607 if (ost_files && ost_ffree < sum.os_ffree) {
6608 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6609 sum.os_ffree = ost_ffree;
6611 if (flags & MNTDF_SHOW) {
6613 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6621 LAYOUT_INHERIT_UNSET = -2,
6625 static int lfs_setdirstripe(int argc, char **argv)
6628 struct lfs_setstripe_args lsa = { 0 };
6629 struct llapi_stripe_param *param = NULL;
6630 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6633 char *mode_opt = NULL;
6634 bool default_stripe = false;
6635 bool delete = false;
6636 bool foreign_mode = false;
6637 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6638 mode_t previous_mode = 0;
6640 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6641 int max_inherit = LAYOUT_INHERIT_UNSET;
6642 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6643 struct option long_opts[] = {
6644 { .val = 'c', .name = "count", .has_arg = required_argument },
6645 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6646 { .val = 'd', .name = "delete", .has_arg = no_argument },
6647 { .val = 'D', .name = "default", .has_arg = no_argument },
6648 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6649 { .val = LFS_LAYOUT_FLAGS_OPT,
6650 .name = "flags", .has_arg = required_argument },
6651 { .val = LFS_LAYOUT_FOREIGN_OPT,
6652 .name = "foreign", .has_arg = optional_argument},
6653 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6654 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6655 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6656 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6658 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6659 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6660 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6662 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6663 { .val = 'i', .name = "index", .has_arg = required_argument },
6665 { .val = 'o', .name = "mode", .has_arg = required_argument },
6666 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6667 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6669 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6670 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6671 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
6672 { .val = LFS_INHERIT_RR_OPT,
6673 .name = "max-inherit-rr", .has_arg = required_argument},
6674 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6678 setstripe_args_init(&lsa);
6680 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:X:",
6681 long_opts, NULL)) >= 0) {
6689 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6690 if (errno != 0 || *end != '\0' ||
6691 lsa.lsa_stripe_count < -1 ||
6692 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6694 "%s %s: invalid stripe count '%s'\n",
6695 progname, argv[0], optarg);
6701 default_stripe = true;
6704 default_stripe = true;
6706 case LFS_LAYOUT_FOREIGN_OPT:
6708 /* check pure numeric */
6709 type = strtoul(optarg, &end, 0);
6712 type = check_foreign_type_name(optarg);
6713 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6715 "%s %s: unknown foreign type '%s'\n",
6720 } else if (type >= UINT32_MAX) {
6722 "%s %s: invalid foreign type '%s'\n",
6723 progname, argv[0], optarg);
6727 foreign_mode = true;
6729 case LFS_LAYOUT_FLAGS_OPT:
6731 flags = strtoul(optarg, &end, 16);
6732 if (errno != 0 || *end != '\0' ||
6733 flags >= UINT32_MAX) {
6735 "%s %s: invalid hex flags '%s'\n",
6736 progname, argv[0], optarg);
6739 if (!foreign_mode) {
6741 "%s %s: hex flags must be specified with --foreign option\n",
6746 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6749 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6752 lsa.lsa_pattern = check_hashtype(optarg);
6753 if (lsa.lsa_pattern == 0) {
6755 "%s %s: bad stripe hash type '%s'\n",
6756 progname, argv[0], optarg);
6761 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6764 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6765 if (strcmp(argv[optind - 1], "--index") == 0)
6767 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6770 lsa.lsa_nr_tgts = parse_targets(mdts,
6771 sizeof(mdts) / sizeof(__u32),
6772 lsa.lsa_nr_tgts, optarg, NULL);
6773 if (lsa.lsa_nr_tgts < 0) {
6775 "%s %s: invalid MDT target(s) '%s'\n",
6776 progname, argv[0], optarg);
6780 lsa.lsa_tgts = mdts;
6781 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6782 lsa.lsa_stripe_off = mdts[0];
6784 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6787 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6796 if (!default_stripe) {
6798 "%s %s: '--max-inherit' must be specified with '-D'\n",
6803 max_inherit = strtol(optarg, &end, 10);
6804 if (errno != 0 || *end != '\0' || max_inherit < -2) {
6806 "%s %s: invalid max-inherit '%s'\n",
6807 progname, argv[0], optarg);
6810 if (max_inherit == 0) {
6811 max_inherit = LMV_INHERIT_NONE;
6812 } else if (max_inherit == -1) {
6813 max_inherit = LMV_INHERIT_UNLIMITED;
6814 } else if (max_inherit > LMV_INHERIT_MAX) {
6816 "%s %s: max-inherit %d exceeds maximum %u\n",
6817 progname, argv[0], max_inherit,
6822 case LFS_INHERIT_RR_OPT:
6823 if (!default_stripe) {
6825 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6830 max_inherit_rr = strtol(optarg, &end, 10);
6831 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6833 "%s %s: invalid max-inherit-rr '%s'\n",
6834 progname, argv[0], optarg);
6837 if (max_inherit_rr == 0) {
6838 max_inherit_rr = LMV_INHERIT_RR_NONE;
6839 } else if (max_inherit_rr == -1) {
6840 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6841 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6843 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6844 progname, argv[0], max_inherit_rr,
6845 LMV_INHERIT_RR_MAX);
6850 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6851 progname, argv[0], argv[optind - 1]);
6856 if (optind == argc) {
6857 fprintf(stderr, "%s %s: DIR must be specified\n",
6862 if (xattr && !foreign_mode) {
6864 * only print a warning as this is armless and will be
6868 "%s %s: xattr has been specified for non-foreign layout\n",
6870 } else if (foreign_mode && !xattr) {
6872 "%s %s: xattr must be provided in foreign mode\n",
6877 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6878 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6880 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6885 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6886 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6887 /* if no parameters set, create directory on least-used MDTs */
6888 lsa.lsa_stripe_off = -1;
6889 lsa.lsa_stripe_count = 1;
6893 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6894 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6896 "%s %s: cannot specify -d with -c or -i options\n",
6902 mode = strtoul(mode_opt, &end, 8);
6905 "%s %s: bad MODE '%s'\n",
6906 progname, argv[0], mode_opt);
6909 previous_mode = umask(0);
6912 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6913 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6914 lsa.lsa_stripe_off != -1) {
6916 "%s %s: max-inherit-rr is meaningless if stripe offset != -1\n",
6921 /* foreign LMV/dir case */
6923 if (argc > optind + 1) {
6925 "%s %s: cannot specify multiple foreign dirs\n",
6930 dname = argv[optind];
6931 result = llapi_dir_create_foreign(dname, mode, type, flags,
6935 "%s mkdir: can't create foreign dir '%s': %s\n",
6936 progname, dname, strerror(-result));
6941 * initialize stripe parameters, in case param is converted to specific,
6942 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6944 param = calloc(1, offsetof(typeof(*param),
6945 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6946 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6949 "%s %s: cannot allocate memory for parameters: %s\n",
6950 progname, argv[0], strerror(ENOMEM));
6954 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6955 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6956 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6957 lsa.lsa_stripe_count == 0))
6958 lsa.lsa_stripe_count = 1;
6959 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6960 param->lsp_stripe_count = lsa.lsa_stripe_count;
6961 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6962 param->lsp_stripe_offset = -1;
6964 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6965 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6966 param->lsp_stripe_pattern = lsa.lsa_pattern;
6968 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6969 param->lsp_pool = lsa.lsa_pool_name;
6970 param->lsp_is_specific = false;
6971 if (default_stripe) {
6972 if (max_inherit == LAYOUT_INHERIT_UNSET)
6973 max_inherit = LMV_INHERIT_DEFAULT;
6974 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6975 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6976 param->lsp_max_inherit = max_inherit;
6977 param->lsp_max_inherit_rr = max_inherit_rr;
6979 if (strcmp(argv[0], "mkdir") == 0)
6980 param->lsp_is_create = true;
6981 if (lsa.lsa_nr_tgts > 1) {
6982 if (lsa.lsa_stripe_count > 0 &&
6983 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6984 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6986 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6987 argv[0], lsa.lsa_stripe_count,
6993 param->lsp_is_specific = true;
6994 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6995 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6998 dname = argv[optind];
7000 if (default_stripe) {
7001 result = llapi_dir_set_default_lmv(dname, param);
7004 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7005 progname, dname, strerror(-result));
7009 result = llapi_dir_create(dname, mode, param);
7012 "%s setdirstripe: cannot create dir '%s': %s\n",
7013 progname, dname, strerror(-result));
7014 } while (!result && (dname = argv[++optind]));
7017 umask(previous_mode);
7024 static int lfs_rmentry(int argc, char **argv)
7031 fprintf(stderr, "error: %s: missing dirname\n",
7037 dname = argv[index];
7039 result = llapi_direntry_remove(dname);
7042 "error: %s: remove dir entry '%s' failed\n",
7046 dname = argv[++index];
7051 static int lfs_unlink_foreign(int argc, char **argv)
7058 fprintf(stderr, "error: %s: missing pathname\n",
7065 while (name != NULL) {
7066 result = llapi_unlink_foreign(name);
7069 "error: %s: unlink foreign entry '%s' failed\n",
7073 name = argv[++index];
7078 static int lfs_mv(int argc, char **argv)
7080 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7081 struct find_param param = {
7088 struct option long_opts[] = {
7089 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7090 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7091 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7094 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7096 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7099 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7103 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7104 if (errno != 0 || *end != '\0' ||
7105 lmu.lum_stripe_offset >= UINT32_MAX) {
7106 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7112 param.fp_verbose = VERBOSE_DETAIL;
7115 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7116 progname, argv[optind - 1]);
7121 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7122 fprintf(stderr, "%s mv: MDT index must be specified\n",
7127 if (optind >= argc) {
7128 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7132 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7134 /* initialize migrate mdt parameters */
7135 param.fp_lmv_md = &lmu;
7136 param.fp_migrate = 1;
7137 rc = llapi_migrate_mdt(argv[optind], ¶m);
7139 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7140 progname, argv[optind], lmu.lum_stripe_offset,
7145 static int lfs_osts(int argc, char **argv)
7147 return lfs_tgts(argc, argv);
7150 static int lfs_mdts(int argc, char **argv)
7152 return lfs_tgts(argc, argv);
7155 static int lfs_df(int argc, char **argv)
7157 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7158 enum mntdf_flags flags = MNTDF_SHOW;
7159 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7160 int c, rc = 0, index = 0;
7161 char fsname[PATH_MAX] = "", *pool_name = NULL;
7162 struct option long_opts[] = {
7163 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7164 { .val = 'H', .name = "si", .has_arg = no_argument },
7165 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7166 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7167 { .val = 'p', .name = "pool", .has_arg = required_argument },
7168 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7171 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7174 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7177 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7180 flags |= MNTDF_INODES;
7183 flags |= MNTDF_LAZY;
7189 flags |= MNTDF_VERBOSE;
7195 if (optind < argc && !realpath(argv[optind], path)) {
7197 fprintf(stderr, "error: invalid path '%s': %s\n",
7198 argv[optind], strerror(-rc));
7202 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7203 /* Check if we have a mount point */
7204 if (mntdir[0] == '\0')
7207 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7208 if (rc || path[0] != '\0')
7210 fsname[0] = '\0'; /* avoid matching in next loop */
7211 mntdir[0] = '\0'; /* avoid matching in next loop */
7217 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7218 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7222 if (opt_fsname == opt_instance) { /* both true or both false */
7223 rc = llapi_getname(mntdir, buf, buflen);
7224 } else if (opt_fsname) {
7226 * llapi_search_mounts() fills @buf with fsname, but that is not
7227 * called if explicit paths are specified on the command-line
7230 rc = llapi_get_fsname(mntdir, buf, buflen);
7231 } else /* if (opt_instance) */ {
7232 rc = llapi_get_instance(mntdir, buf, buflen);
7236 fprintf(stderr, "cannot get instance for '%s': %s\n",
7237 mntdir, strerror(-rc));
7242 printf("%s %s\n", buf, mntdir);
7244 printf("%s\n", buf);
7249 static int lfs_getname(int argc, char **argv)
7251 struct option long_opts[] = {
7252 { .val = 'h', .name = "help", .has_arg = no_argument },
7253 { .val = 'i', .name = "instance", .has_arg = no_argument },
7254 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7256 bool opt_instance = false, opt_fsname = false;
7257 char fsname[PATH_MAX] = "";
7260 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7263 opt_instance = true;
7274 if (optind == argc) { /* no paths specified, get all paths. */
7275 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7278 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7279 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7280 opt_instance, opt_fsname, true);
7283 path[0] = fsname[0] = mntdir[0] = '\0';
7285 } else { /* paths specified, only attempt to search these. */
7288 /* if only one path is given, print only requested info */
7289 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7291 for (; optind < argc; optind++) {
7292 rc2 = print_instance(argv[optind], fsname,
7293 sizeof(fsname), opt_instance,
7294 opt_fsname, opt_mntdir);
7304 static int lfs_check(int argc, char **argv)
7306 char mntdir[PATH_MAX] = {'\0'};
7315 fprintf(stderr, "%s check: server type must be specified\n",
7320 obd_types[0] = obd_type1;
7321 obd_types[1] = obd_type2;
7322 obd_types[2] = obd_type3;
7324 if (strcmp(argv[1], "osts") == 0) {
7325 strcpy(obd_types[0], "osc");
7326 } else if (strcmp(argv[1], "mdts") == 0 ||
7327 strcmp(argv[1], "mds") == 0) {
7328 strcpy(obd_types[0], "mdc");
7329 } else if (strcmp(argv[1], "mgts") == 0) {
7330 strcpy(obd_types[0], "mgc");
7331 } else if (strcmp(argv[1], "all") == 0 ||
7332 strcmp(argv[1], "servers") == 0) {
7334 strcpy(obd_types[0], "osc");
7335 strcpy(obd_types[1], "mdc");
7336 strcpy(obd_types[2], "mgc");
7338 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7343 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7344 if (rc < 0 || mntdir[0] == '\0') {
7346 "%s check: cannot find mounted Lustre filesystem: %s\n",
7347 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7351 rc = llapi_target_check(num_types, obd_types, mntdir);
7353 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7354 progname, argv[1], strerror(-rc));
7359 #ifdef HAVE_SYS_QUOTA_H
7360 #define ADD_OVERFLOW(a, b) \
7361 ((((a) + (b)) < (a)) ? \
7362 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7364 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7365 * returns the value or ULONG_MAX on integer overflow or incorrect format
7367 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7368 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7369 * 3. empty integer value is interpreted as 0
7371 static unsigned long str2sec(const char *timestr)
7373 const char spec[] = "smhdw";
7374 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7375 unsigned long val = 0;
7378 if (strpbrk(timestr, spec) == NULL) {
7380 * no specifiers inside the time string,
7381 * should treat it as an integer value
7383 val = strtoul(timestr, &tail, 10);
7384 return *tail ? ULONG_MAX : val;
7387 /* format string is XXwXXdXXhXXmXXs */
7393 v = strtoul(timestr, &tail, 10);
7394 if (v == ULONG_MAX || *tail == '\0')
7396 * value too large (ULONG_MAX or more)
7397 * or missing specifier
7401 ptr = strchr(spec, *tail);
7403 /* unknown specifier */
7408 /* check if product will overflow the type */
7409 if (!(v < ULONG_MAX / mult[ind]))
7412 ADD_OVERFLOW(val, mult[ind] * v);
7413 if (val == ULONG_MAX)
7425 #define ARG2ULL(nr, str, def_units) \
7427 unsigned long long limit, units = def_units; \
7430 rc = llapi_parse_size(str, &limit, &units, 1); \
7432 fprintf(stderr, "%s: invalid limit '%s'\n", \
7439 static inline int has_times_option(int argc, char **argv)
7443 for (i = 1; i < argc; i++)
7444 if (!strcmp(argv[i], "-t"))
7450 static inline int lfs_verify_poolarg(char *pool)
7452 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7454 "Pool name '%.*s' is longer than %d\n",
7455 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7461 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7464 char *mnt, *obd_type = (char *)qctl->obd_type;
7465 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7466 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7467 struct option long_opts[] = {
7468 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7469 { .val = 'g', .name = "group", .has_arg = no_argument },
7470 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7471 { .val = 'p', .name = "projid", .has_arg = no_argument },
7472 { .val = 't', .name = "times", .has_arg = no_argument },
7473 { .val = 'u', .name = "user", .has_arg = no_argument },
7474 { .val = LFS_POOL_OPT,
7475 .name = "pool", .has_arg = required_argument },
7479 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7480 qctl->qc_type = ALLQUOTA;
7482 while ((c = getopt_long(argc, argv, "b:gi:ptu",
7483 long_opts, NULL)) != -1) {
7494 if (qctl->qc_type != ALLQUOTA) {
7496 "error: -u/g/p can't be used more than once\n");
7499 qctl->qc_type = qtype;
7502 if (strncmp(optarg, NOTIFY_GRACE,
7503 strlen(NOTIFY_GRACE)) == 0) {
7504 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7506 dqi->dqi_bgrace = str2sec(optarg);
7507 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7509 "error: bad block-grace: %s\n",
7514 dqb->dqb_valid |= QIF_BTIME;
7517 if (strncmp(optarg, NOTIFY_GRACE,
7518 strlen(NOTIFY_GRACE)) == 0) {
7519 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7521 dqi->dqi_igrace = str2sec(optarg);
7522 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7524 "error: bad inode-grace: %s\n",
7529 dqb->dqb_valid |= QIF_ITIME;
7531 case 't': /* Yes, of course! */
7534 if (lfs_verify_poolarg(optarg))
7536 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7537 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7539 /* getopt prints error message for us when opterr != 0 */
7545 if (qctl->qc_type == ALLQUOTA) {
7546 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
7550 if (optind != argc - 1) {
7551 fprintf(stderr, "error: unexpected parameters encountered\n");
7556 rc = llapi_quotactl(mnt, qctl);
7559 fprintf(stderr, "%s %s ", obd_type,
7560 obd_uuid2str(&qctl->obd_uuid));
7561 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7568 #define BSLIMIT (1 << 0)
7569 #define BHLIMIT (1 << 1)
7570 #define ISLIMIT (1 << 2)
7571 #define IHLIMIT (1 << 3)
7573 int lfs_setquota(int argc, char **argv)
7576 struct if_quotactl *qctl;
7577 char *mnt, *obd_type;
7578 struct obd_dqblk *dqb;
7579 struct option long_opts[] = {
7580 { .val = 'b', .name = "block-softlimit",
7581 .has_arg = required_argument },
7582 { .val = 'B', .name = "block-hardlimit",
7583 .has_arg = required_argument },
7584 { .val = 'd', .name = "default", .has_arg = no_argument },
7585 { .val = 'g', .name = "group", .has_arg = required_argument },
7586 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7587 { .val = 'i', .name = "inode-softlimit",
7588 .has_arg = required_argument },
7589 { .val = 'I', .name = "inode-hardlimit",
7590 .has_arg = required_argument },
7591 { .val = 'p', .name = "projid", .has_arg = required_argument },
7592 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7593 { .val = 'u', .name = "user", .has_arg = required_argument },
7594 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7595 { .val = LFS_POOL_OPT,
7596 .name = "pool", .has_arg = required_argument },
7598 unsigned int limit_mask = 0;
7599 bool use_default = false;
7600 int qtype, qctl_len;
7602 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7603 qctl = malloc(qctl_len);
7607 memset(qctl, 0, qctl_len);
7608 obd_type = (char *)qctl->obd_type;
7609 dqb = &qctl->qc_dqblk;
7611 if (has_times_option(argc, argv)) {
7612 rc = lfs_setquota_times(argc, argv, qctl);
7616 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7617 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7618 * so it can be used as a marker that qc_type
7619 * isn't reinitialized from command line
7622 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7623 long_opts, NULL)) != -1) {
7626 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7629 goto quota_type_def;
7632 rc = name2uid(&qctl->qc_id, optarg);
7635 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7638 goto quota_type_def;
7641 rc = name2gid(&qctl->qc_id, optarg);
7644 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7647 goto quota_type_def;
7650 rc = name2projid(&qctl->qc_id, optarg);
7653 if (str2quotaid(&qctl->qc_id, optarg)) {
7655 "%s setquota: invalid id '%s'\n",
7662 if (qctl->qc_id == 0) {
7664 "%s setquota: can't set quota for root usr/group/project.\n",
7671 if (qctl->qc_type != ALLQUOTA) {
7673 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7678 qctl->qc_type = qtype;
7681 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7685 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7686 dqb->dqb_bsoftlimit >>= 10;
7687 limit_mask |= BSLIMIT;
7688 if (dqb->dqb_bsoftlimit &&
7689 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7691 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7693 (unsigned long long)dqb->dqb_bsoftlimit,
7697 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7698 dqb->dqb_bhardlimit >>= 10;
7699 limit_mask |= BHLIMIT;
7700 if (dqb->dqb_bhardlimit &&
7701 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7703 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7704 "See '%s help setquota' or Lustre manual for details\n",
7706 (unsigned long long)dqb->dqb_bhardlimit,
7710 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7711 limit_mask |= ISLIMIT;
7712 if (dqb->dqb_isoftlimit &&
7713 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7715 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7717 (unsigned long long)dqb->dqb_isoftlimit,
7721 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7722 limit_mask |= IHLIMIT;
7723 if (dqb->dqb_ihardlimit &&
7724 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7726 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7728 (unsigned long long)dqb->dqb_ihardlimit,
7732 if (lfs_verify_poolarg(optarg)) {
7736 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7737 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7738 LUSTRE_Q_SETDEFAULT_POOL :
7739 LUSTRE_Q_SETQUOTAPOOL;
7743 "%s setquota: unrecognized option '%s'\n",
7744 progname, argv[optind - 1]);
7750 if (qctl->qc_type == ALLQUOTA) {
7752 "%s setquota: either -u or -g must be specified\n",
7758 if (!use_default && limit_mask == 0) {
7760 "%s setquota: at least one limit must be specified\n",
7766 if (use_default && limit_mask != 0) {
7768 "%s setquota: limits should not be specified when using default quota\n",
7774 if (use_default && qctl->qc_id == 0) {
7776 "%s setquota: can not set default quota for root user/group/project\n",
7782 if (optind != argc - 1) {
7784 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7785 progname, argv[optind]);
7793 dqb->dqb_bhardlimit = 0;
7794 dqb->dqb_bsoftlimit = 0;
7795 dqb->dqb_ihardlimit = 0;
7796 dqb->dqb_isoftlimit = 0;
7799 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7800 /* do not set inode limits for Pool Quotas */
7801 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
7802 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7803 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7804 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7805 /* sigh, we can't just set blimits/ilimits */
7806 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7807 .qc_type = qctl->qc_type,
7808 .qc_id = qctl->qc_id};
7810 rc = llapi_quotactl(mnt, &tmp_qctl);
7814 if (!(limit_mask & BHLIMIT))
7815 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7816 if (!(limit_mask & BSLIMIT))
7817 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7818 if (!(limit_mask & IHLIMIT))
7819 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7820 if (!(limit_mask & ISLIMIT))
7821 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7823 /* Keep grace times if we have got no softlimit arguments */
7824 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7825 dqb->dqb_valid |= QIF_BTIME;
7826 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7829 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7830 dqb->dqb_valid |= QIF_ITIME;
7831 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7835 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7836 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7838 rc = llapi_quotactl(mnt, qctl);
7842 "%s setquota: cannot quotactl '%s' '%s': %s",
7844 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7851 /* Converts seconds value into format string
7852 * result is returned in buf
7854 * 1. result is in descenting order: 1w2d3h4m5s
7855 * 2. zero fields are not filled (except for p. 3): 5d1s
7856 * 3. zero seconds value is presented as "0s"
7858 static char *__sec2str(time_t seconds, char *buf)
7860 const char spec[] = "smhdw";
7861 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7866 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7867 c = seconds / mult[i];
7869 if (c > 0 || (i == 0 && buf == tail))
7870 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7879 static void sec2str(time_t seconds, char *buf, int rc)
7886 tail = __sec2str(seconds, tail);
7888 if (rc && tail - buf < 39) {
7894 static void diff2str(time_t seconds, char *buf, time_t now)
7899 if (seconds <= now) {
7900 strcpy(buf, "none");
7903 __sec2str(seconds - now, buf);
7906 static void print_quota_title(char *name, struct if_quotactl *qctl,
7907 bool human_readable, bool show_default)
7910 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7911 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7912 "Filesystem", "bquota", "blimit", "bgrace",
7913 "iquota", "ilimit", "igrace");
7915 printf("Disk quotas for %s %s (%cid %u):\n",
7916 qtype_name(qctl->qc_type), name,
7917 *qtype_name(qctl->qc_type), qctl->qc_id);
7918 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7919 "Filesystem", human_readable ? "used" : "kbytes",
7920 "quota", "limit", "grace",
7921 "files", "quota", "limit", "grace");
7925 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7928 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7931 snprintf(buf, buflen, "%5.4gP",
7932 (double)num / ((__u64)1 << 40));
7934 snprintf(buf, buflen, "%5.4gT",
7935 (double)num / (1 << 30));
7937 snprintf(buf, buflen, "%5.4gG",
7938 (double)num / (1 << 20));
7940 snprintf(buf, buflen, "%5.4gM",
7941 (double)num / (1 << 10));
7943 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7947 #define STRBUF_LEN 24
7948 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7949 int rc, bool h, bool show_default)
7955 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7956 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7957 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
7958 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
7959 int bover = 0, iover = 0;
7960 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7961 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
7963 char strbuf[STRBUF_LEN];
7965 if (dqb->dqb_bhardlimit &&
7966 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7968 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7969 if (dqb->dqb_btime > now)
7975 if (dqb->dqb_ihardlimit &&
7976 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7978 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7979 if (dqb->dqb_itime > now)
7985 if (strlen(mnt) > 15)
7986 printf("%s\n%15s", mnt, "");
7988 printf("%15s", mnt);
7991 diff2str(dqb->dqb_btime, timebuf, now);
7992 else if (show_default)
7993 snprintf(timebuf, sizeof(timebuf), "%llu",
7994 (unsigned long long)dqb->dqb_btime);
7996 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7997 strbuf, sizeof(strbuf), h);
7998 if (rc == -EREMOTEIO)
7999 sprintf(numbuf[0], "%s*", strbuf);
8001 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8002 "%s" : "[%s]", strbuf);
8004 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8005 if (type == QC_GENERAL)
8006 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8007 "%s" : "[%s]", strbuf);
8009 sprintf(numbuf[1], "%s", "-");
8011 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8012 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8013 "%s" : "[%s]", strbuf);
8016 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8018 printf(" %7s%c %6s %7s %7s",
8019 numbuf[0], bover ? '*' : ' ', numbuf[1],
8020 numbuf[2], bover > 1 ? timebuf : "-");
8023 diff2str(dqb->dqb_itime, timebuf, now);
8024 else if (show_default)
8025 snprintf(timebuf, sizeof(timebuf), "%llu",
8026 (unsigned long long)dqb->dqb_itime);
8028 snprintf(numbuf[0], sizeof(numbuf),
8029 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8030 (uintmax_t)dqb->dqb_curinodes);
8032 if (type == QC_GENERAL)
8033 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8035 (uintmax_t)dqb->dqb_isoftlimit);
8037 sprintf(numbuf[1], "%s", "-");
8039 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8040 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8043 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8044 else if (type != QC_OSTIDX)
8045 printf(" %7s%c %6s %7s %7s",
8046 numbuf[0], iover ? '*' : ' ', numbuf[1],
8047 numbuf[2], iover > 1 ? timebuf : "-");
8049 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8051 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8052 qctl->qc_cmd == Q_GETOINFO) {
8056 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8057 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8059 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8060 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8061 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8063 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8065 printf("Block grace time: %s; Inode grace time: %s\n",
8066 bgtimebuf, igtimebuf);
8070 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8074 /* format is "lustre-OST0001" */
8075 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8077 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8082 *idx = strtoul(dash, &endp, 16);
8083 if (*idx > 0xffff) {
8084 fprintf(stderr, "wrong index %s\n", tgtname);
8091 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8092 bool h, __u64 *total)
8094 int rc = 0, rc1 = 0, count = 0, i = 0;
8095 char **list = NULL, *buffer = NULL;
8096 __u32 valid = qctl->qc_valid;
8098 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8101 /* Is it correct for the case OST0000, OST0002, OST0003 -
8102 * we will ask OST0001 that is absent and won't ask OST0003? */
8103 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8105 fprintf(stderr, "can not get %s count: %s\n",
8106 is_mdt ? "mdt" : "ost", strerror(-rc));
8110 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8111 char fname[PATH_MAX];
8112 char fsname[LUSTRE_MAXFSNAME + 1];
8113 int bufsize = sizeof(struct obd_uuid) * count;
8115 rc = llapi_search_fsname(mnt, fsname);
8117 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8121 buffer = malloc(bufsize + sizeof(*list) * count);
8124 list = (char **)(buffer + bufsize);
8125 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8126 count = llapi_get_poolmembers(fname, list, count,
8132 for (i = 0; i < count; i++) {
8133 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8136 if (tgt_name2index(list[i], &index))
8138 qctl->qc_idx = index;
8143 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8144 rc = llapi_quotactl(mnt, qctl);
8146 /* It is remote client case. */
8147 if (rc == -EOPNOTSUPP) {
8154 fprintf(stderr, "quotactl %s%d failed.\n",
8155 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8159 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8160 qctl->qc_valid, 0, h, false);
8161 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8162 qctl->qc_dqblk.dqb_bhardlimit;
8167 qctl->qc_valid = valid;
8171 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8172 int verbose, int quiet, bool human_readable,
8175 int rc1 = 0, rc2 = 0, rc3 = 0;
8176 char *obd_type = (char *)qctl->obd_type;
8177 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8178 __u64 total_ialloc = 0, total_balloc = 0;
8179 bool use_default_for_blk = false;
8180 bool use_default_for_file = false;
8183 rc1 = llapi_quotactl(mnt, qctl);
8187 fprintf(stderr, "%s quotas are not enabled.\n",
8188 qtype_name(qctl->qc_type));
8191 fprintf(stderr, "Permission denied.\n");
8194 /* We already got error message. */
8197 fprintf(stderr, "Unexpected quotactl error: %s\n",
8202 if (!show_default && qctl->qc_id == 0) {
8203 qctl->qc_dqblk.dqb_bhardlimit = 0;
8204 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8205 qctl->qc_dqblk.dqb_ihardlimit = 0;
8206 qctl->qc_dqblk.dqb_isoftlimit = 0;
8207 qctl->qc_dqblk.dqb_btime = 0;
8208 qctl->qc_dqblk.dqb_itime = 0;
8209 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8212 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8213 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8214 use_default_for_blk = true;
8215 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8218 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8219 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8220 use_default_for_file = true;
8221 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8224 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8225 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8226 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8227 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8228 print_quota_title(name, qctl, human_readable, show_default);
8230 if (rc1 && *obd_type)
8231 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8233 if (qctl->qc_valid != QC_GENERAL)
8236 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8237 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8238 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8239 (QIF_LIMITS|QIF_USAGE));
8241 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8243 if (!show_default && verbose &&
8244 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8245 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8246 char strbuf[STRBUF_LEN];
8248 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8250 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8252 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8254 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8255 (uintmax_t)total_ialloc, strbuf);
8258 if (use_default_for_blk)
8259 printf("%cid %u is using default block quota setting\n",
8260 *qtype_name(qctl->qc_type), qctl->qc_id);
8262 if (use_default_for_file)
8263 printf("%cid %u is using default file quota setting\n",
8264 *qtype_name(qctl->qc_type), qctl->qc_id);
8266 if (rc1 || rc2 || rc3 || inacc)
8267 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8281 static int lfs_project(int argc, char **argv)
8283 int ret = 0, err = 0, c, i;
8284 struct project_handle_control phc = { 0 };
8285 enum lfs_project_ops_t op;
8288 phc.assign_projid = false;
8289 /* default action */
8290 op = LFS_PROJECT_LIST;
8292 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8295 if (op != LFS_PROJECT_LIST) {
8297 "%s: cannot specify '-c' '-C' '-s' together\n",
8302 op = LFS_PROJECT_CHECK;
8305 if (op != LFS_PROJECT_LIST) {
8307 "%s: cannot specify '-c' '-C' '-s' together\n",
8312 op = LFS_PROJECT_CLEAR;
8315 if (op != LFS_PROJECT_LIST) {
8317 "%s: cannot specify '-c' '-C' '-s' together\n",
8322 phc.set_inherit = true;
8323 op = LFS_PROJECT_SET;
8329 phc.keep_projid = true;
8332 phc.recursive = true;
8335 if (str2quotaid(&phc.projid, optarg)) {
8337 "Invalid project ID: %s\n",
8342 phc.assign_projid = true;
8346 phc.newline = false;
8349 fprintf(stderr, "%s: invalid option '%c'\n",
8355 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8356 op = LFS_PROJECT_SET;
8357 phc.set_projid = true;
8358 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8359 phc.set_projid = true;
8363 case LFS_PROJECT_CHECK:
8364 if (phc.keep_projid) {
8366 "%s: '-k' is useless together with '-c'\n",
8371 case LFS_PROJECT_CLEAR:
8374 "%s: '-0' is useless together with '-C'\n",
8378 if (phc.assign_projid) {
8380 "%s: '-p' is useless together with '-C'\n",
8385 case LFS_PROJECT_SET:
8388 "%s: '-0' is useless together with '-s'\n",
8392 if (phc.keep_projid) {
8394 "%s: '-k' is useless together with '-s'\n",
8402 "%s: '-0' is useless for list operations\n",
8412 fprintf(stderr, "%s: missing file or directory target(s)\n",
8417 for (i = 0; i < argc; i++) {
8419 case LFS_PROJECT_CHECK:
8420 err = lfs_project_check(argv[i], &phc);
8422 case LFS_PROJECT_LIST:
8423 err = lfs_project_list(argv[i], &phc);
8425 case LFS_PROJECT_CLEAR:
8426 err = lfs_project_clear(argv[i], &phc);
8428 case LFS_PROJECT_SET:
8429 err = lfs_project_set(argv[i], &phc);
8441 static int lfs_quota(int argc, char **argv)
8444 char *mnt, *name = NULL;
8445 struct if_quotactl *qctl;
8447 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8448 __u32 valid = QC_GENERAL, idx = 0;
8449 bool human_readable = false;
8450 bool show_default = false;
8452 bool show_pools = false;
8453 struct option long_opts[] = {
8454 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8456 char **poollist = NULL;
8460 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8464 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8465 qctl->qc_type = ALLQUOTA;
8466 obd_uuid = (char *)qctl->obd_uuid.uuid;
8468 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8469 long_opts, NULL)) != -1) {
8472 show_default = true;
8477 show_default = true;
8482 show_default = true;
8486 if (qctl->qc_type != ALLQUOTA) {
8488 "%s quota: only one of -u, -g, or -p may be specified\n",
8493 qctl->qc_type = qtype;
8496 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8499 valid = qctl->qc_valid = QC_UUID;
8500 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8503 valid = qctl->qc_valid = QC_MDTIDX;
8504 idx = qctl->qc_idx = atoi(optarg);
8505 if (idx == 0 && *optarg != '0') {
8507 "%s quota: invalid MDT index '%s'\n",
8514 valid = qctl->qc_valid = QC_OSTIDX;
8515 idx = qctl->qc_idx = atoi(optarg);
8516 if (idx == 0 && *optarg != '0') {
8518 "%s quota: invalid OST index '%s'\n",
8531 human_readable = true;
8534 if ((!optarg) && (argv[optind] != NULL) &&
8535 (argv[optind][0] != '-') &&
8536 (argv[optind][0] != '/')) {
8537 optarg = argv[optind++];
8538 if (lfs_verify_poolarg(optarg)) {
8542 strncpy(qctl->qc_poolname, optarg,
8544 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8545 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8547 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8551 /* optarg is NULL */
8553 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8556 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8557 progname, argv[optind - 1]);
8563 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8564 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8565 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8566 qctl->qc_type == ALLQUOTA &&
8567 optind == argc - 1 && !show_default) {
8570 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8571 qctl->qc_type = qtype;
8572 qctl->qc_valid = valid;
8573 if (qtype == USRQUOTA) {
8574 qctl->qc_id = geteuid();
8575 rc = uid2name(&name, qctl->qc_id);
8577 qctl->qc_id = getegid();
8578 rc = gid2name(&name, qctl->qc_id);
8579 memset(&qctl->qc_dqblk, 0,
8580 sizeof(qctl->qc_dqblk));
8585 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8586 human_readable, show_default);
8591 /* lfs quota -u username /path/to/lustre/mount */
8592 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8593 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8594 /* options should be followed by u/g-name and mntpoint */
8595 if ((!show_default && optind + 2 != argc) ||
8596 (show_default && optind + 1 != argc) ||
8597 qctl->qc_type == ALLQUOTA) {
8599 "%s quota: name and mount point must be specified\n",
8605 if (!show_default) {
8606 name = argv[optind++];
8607 switch (qctl->qc_type) {
8609 rc = name2uid(&qctl->qc_id, name);
8612 rc = name2gid(&qctl->qc_id, name);
8615 rc = name2projid(&qctl->qc_id, name);
8622 qctl->qc_valid = QC_GENERAL;
8623 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8624 LUSTRE_Q_GETDEFAULT_POOL :
8625 LUSTRE_Q_GETDEFAULT;
8630 if (str2quotaid(&qctl->qc_id, name)) {
8631 fprintf(stderr, "%s quota: invalid id '%s'\n",
8637 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8638 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8649 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8653 for (i = 0; i < poolcount; i++) {
8654 p = memchr(poollist[i], '.', MAXNAMLEN);
8656 fprintf(stderr, "bad string format %.*s\n",
8657 MAXNAMLEN, poollist[i]);
8662 printf("Quotas for pool: %s\n", p);
8663 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8664 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8665 human_readable, show_default);
8672 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8673 human_readable, show_default);
8679 #endif /* HAVE_SYS_QUOTA_H! */
8681 static int flushctx_ioctl(char *mp)
8685 fd = open(mp, O_RDONLY);
8687 fprintf(stderr, "flushctx: error open %s: %s\n",
8688 mp, strerror(errno));
8692 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8694 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8695 mp, strerror(errno));
8701 static int lfs_flushctx(int argc, char **argv)
8703 int kdestroy = 0, reap = 0, c;
8704 char mntdir[PATH_MAX] = {'\0'};
8708 while ((c = getopt(argc, argv, "kr")) != -1) {
8718 "error: %s: option '-%c' unrecognized\n",
8725 rc = system("kdestroy > /dev/null");
8727 rc = WEXITSTATUS(rc);
8729 "error destroying tickets: %d, continuing\n",
8734 if (optind >= argc) {
8735 /* flush for all mounted lustre fs. */
8736 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8737 /* Check if we have a mount point */
8738 if (mntdir[0] == '\0')
8741 if (flushctx_ioctl(mntdir))
8744 mntdir[0] = '\0'; /* avoid matching in next loop */
8747 /* flush fs as specified */
8748 while (optind < argc) {
8749 if (flushctx_ioctl(argv[optind++]))
8755 rc = system("keyctl reap > /dev/null");
8757 rc = WEXITSTATUS(rc);
8758 fprintf(stderr, "error reaping keyring: %d\n", rc);
8765 static int lfs_changelog(int argc, char **argv)
8767 void *changelog_priv;
8768 struct changelog_rec *rec;
8769 long long startrec = 0, endrec = 0;
8771 struct option long_opts[] = {
8772 { .val = 'f', .name = "follow", .has_arg = no_argument },
8774 char short_opts[] = "f";
8777 while ((rc = getopt_long(argc, argv, short_opts,
8778 long_opts, NULL)) != -1) {
8785 "%s changelog: unrecognized option '%s'\n",
8786 progname, argv[optind - 1]);
8790 if (optind >= argc) {
8791 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8796 mdd = argv[optind++];
8797 if (argc > optind) {
8799 startrec = strtoll(argv[optind++], NULL, 10);
8800 if (errno != 0 || startrec < 0) {
8802 "%s changelog: bad startrec\n",
8808 if (argc > optind) {
8810 endrec = strtoll(argv[optind++], NULL, 10);
8811 if (errno != 0 || endrec < 0) {
8813 "%s changelog: bad endrec\n",
8819 rc = llapi_changelog_start(&changelog_priv,
8820 CHANGELOG_FLAG_BLOCK |
8821 CHANGELOG_FLAG_JOBID |
8822 CHANGELOG_FLAG_EXTRA_FLAGS |
8823 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8826 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8827 progname, strerror(errno = -rc));
8831 rc = llapi_changelog_set_xflags(changelog_priv,
8832 CHANGELOG_EXTRA_FLAG_UIDGID |
8833 CHANGELOG_EXTRA_FLAG_NID |
8834 CHANGELOG_EXTRA_FLAG_OMODE |
8835 CHANGELOG_EXTRA_FLAG_XATTR);
8838 "%s changelog: cannot set xflags for changelog: %s\n",
8839 progname, strerror(errno = -rc));
8843 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8847 if (endrec && rec->cr_index > endrec) {
8848 llapi_changelog_free(&rec);
8851 if (rec->cr_index < startrec) {
8852 llapi_changelog_free(&rec);
8856 secs = rec->cr_time >> 30;
8857 gmtime_r(&secs, &ts);
8858 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8859 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8860 changelog_type2str(rec->cr_type),
8861 ts.tm_hour, ts.tm_min, ts.tm_sec,
8862 (int)(rec->cr_time & ((1 << 30) - 1)),
8863 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8864 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8866 if (rec->cr_flags & CLF_JOBID) {
8867 struct changelog_ext_jobid *jid =
8868 changelog_rec_jobid(rec);
8870 if (jid->cr_jobid[0] != '\0')
8871 printf(" j=%s", jid->cr_jobid);
8874 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8875 struct changelog_ext_extra_flags *ef =
8876 changelog_rec_extra_flags(rec);
8878 printf(" ef=0x%llx",
8879 (unsigned long long)ef->cr_extra_flags);
8881 if (ef->cr_extra_flags & CLFE_UIDGID) {
8882 struct changelog_ext_uidgid *uidgid =
8883 changelog_rec_uidgid(rec);
8885 printf(" u=%llu:%llu",
8886 (unsigned long long)uidgid->cr_uid,
8887 (unsigned long long)uidgid->cr_gid);
8889 if (ef->cr_extra_flags & CLFE_NID) {
8890 struct changelog_ext_nid *nid =
8891 changelog_rec_nid(rec);
8894 libcfs_nid2str(nid->cr_nid));
8897 if (ef->cr_extra_flags & CLFE_OPEN) {
8898 struct changelog_ext_openmode *omd =
8899 changelog_rec_openmode(rec);
8900 char mode[] = "---";
8902 /* exec mode must be exclusive */
8903 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8906 if (omd->cr_openflags & MDS_FMODE_READ)
8908 if (omd->cr_openflags &
8915 if (strcmp(mode, "---") != 0)
8916 printf(" m=%s", mode);
8919 if (ef->cr_extra_flags & CLFE_XATTR) {
8920 struct changelog_ext_xattr *xattr =
8921 changelog_rec_xattr(rec);
8923 if (xattr->cr_xattr[0] != '\0')
8924 printf(" x=%s", xattr->cr_xattr);
8928 if (!fid_is_zero(&rec->cr_pfid))
8929 printf(" p="DFID, PFID(&rec->cr_pfid));
8930 if (rec->cr_namelen)
8931 printf(" %.*s", rec->cr_namelen,
8932 changelog_rec_name(rec));
8934 if (rec->cr_flags & CLF_RENAME) {
8935 struct changelog_ext_rename *rnm =
8936 changelog_rec_rename(rec);
8938 if (!fid_is_zero(&rnm->cr_sfid))
8939 printf(" s="DFID" sp="DFID" %.*s",
8940 PFID(&rnm->cr_sfid),
8941 PFID(&rnm->cr_spfid),
8942 (int)changelog_rec_snamelen(rec),
8943 changelog_rec_sname(rec));
8947 llapi_changelog_free(&rec);
8950 llapi_changelog_fini(&changelog_priv);
8953 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8954 progname, strerror(errno = -rc));
8956 return (rc == 1 ? 0 : rc);
8959 static int lfs_changelog_clear(int argc, char **argv)
8968 endrec = strtoll(argv[3], NULL, 10);
8969 if (errno != 0 || endrec < 0) {
8971 "%s: bad endrec '%s'\n",
8976 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8979 fprintf(stderr, "%s: record out of range: %llu\n",
8981 else if (rc == -ENOENT)
8982 fprintf(stderr, "%s: no changelog user: %s\n",
8985 fprintf(stderr, "%s error: %s\n", argv[0],
8994 static void rstripc(char *str, int c)
8996 char *end = str + strlen(str);
8998 for (; str < end && end[-1] == c; --end)
9002 static int lfs_fid2path(int argc, char **argv)
9004 struct option long_opts[] = {
9005 { .val = 'c', .name = "cur", .has_arg = no_argument },
9006 { .val = 'c', .name = "current", .has_arg = no_argument },
9007 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9008 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9009 { .val = 'l', .name = "link", .has_arg = required_argument },
9011 char short_opts[] = "cfl:pr:";
9012 bool print_link = false;
9013 bool print_fid = false;
9015 char mnt_dir[PATH_MAX] = "";
9017 char *path_or_fsname;
9018 long long recno = -1;
9020 char *endptr = NULL;
9025 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9035 linkno = strtol(optarg, &endptr, 10);
9036 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9038 "%s fid2path: invalid linkno '%s'\n",
9044 /* recno is something to do with changelogs
9045 * that was never implemented. We just pass it
9046 * through for the MDT to ignore.
9049 recno = strtoll(optarg, &endptr, 10);
9050 if (errno != 0 || *endptr != '\0' || recno < 0) {
9052 "%s fid2path: invalid recno '%s'\n",
9059 "%s fid2path: unrecognized option '%s'\n",
9060 progname, argv[optind - 1]);
9065 if (argc - optind < 2) {
9067 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9072 path_or_fsname = argv[optind];
9074 if (*path_or_fsname == '/') {
9075 print_mnt_dir = true;
9076 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9078 print_mnt_dir = false;
9079 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9084 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9085 progname, path_or_fsname, strerror(-rc));
9089 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9092 "%s fid2path: cannot open mount point for '%s': %s\n",
9093 progname, path_or_fsname, strerror(-rc));
9097 /* Strip trailing slashes from mnt_dir. */
9098 rstripc(mnt_dir + 1, '/');
9100 for (i = optind + 1; i < argc; i++) {
9101 const char *fid_str = argv[i];
9105 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9108 "%s fid2path: invalid FID '%s'\n",
9116 int linktmp = (linkno >= 0) ? linkno : 0;
9118 int oldtmp = linktmp;
9119 long long rectmp = recno;
9120 char path_buf[PATH_MAX];
9122 rc2 = llapi_fid2path_at(mnt_fd, &fid,
9123 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9126 "%s fid2path: cannot find %s %s: %s\n",
9127 progname, path_or_fsname, fid_str,
9135 printf("%s ", fid_str);
9138 printf("%d ", linktmp);
9140 /* You may think this looks wrong or weird (and it is!)
9141 * but we are actually trying to preserve the old quirky
9142 * behaviors (enforced by our old quirky tests!) that
9143 * make lfs so much fun to work on:
9145 * lustre 0x200000007:0x1:0x0 => "/"
9146 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9148 * Note that llapi_fid2path() returns "" for the root
9152 print_mnt_dir ? mnt_dir : "",
9153 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9157 /* specified linkno */
9160 if (oldtmp == linktmp)
9172 static int lfs_path2fid(int argc, char **argv)
9174 struct option long_opts[] = {
9175 { .val = 'p', .name = "parents", .has_arg = no_argument },
9178 const char short_opts[] = "p";
9179 const char *sep = "";
9182 bool show_parents = false;
9184 while ((rc = getopt_long(argc, argv, short_opts,
9185 long_opts, NULL)) != -1) {
9188 show_parents = true;
9192 "%s path2fid: unrecognized option '%s'\n",
9193 progname, argv[optind - 1]);
9198 if (optind > argc - 1) {
9199 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9202 } else if (optind < argc - 1) {
9207 for (path = argv + optind; *path != NULL; path++) {
9210 if (!show_parents) {
9211 err = llapi_path2fid(*path, &fid);
9213 printf("%s%s"DFID"\n",
9214 *sep != '\0' ? *path : "", sep,
9217 char name[NAME_MAX + 1];
9218 unsigned int linkno = 0;
9220 while ((err = llapi_path2parent(*path, linkno, &fid,
9221 name, sizeof(name))) == 0) {
9222 if (*sep != '\0' && linkno == 0)
9223 printf("%s%s", *path, sep);
9225 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9230 /* err == -ENODATA is end-of-loop */
9231 if (linkno > 0 && err == -ENODATA) {
9239 "%s path2fid: cannot get %sfid for '%s': %s\n",
9240 progname, show_parents ? "parent " : "", *path,
9252 #define MAX_ERRNO 4095
9253 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9255 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9259 rc = llapi_rmfid(device, fa);
9261 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9262 progname, strerror(-rc));
9266 for (k = 0; k < fa->fa_nr; k++) {
9267 rc2 = (__s32)fa->fa_fids[k].f_ver;
9268 if (!IS_ERR_VALUE(rc2))
9274 fa->fa_fids[k].f_ver = 0;
9275 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9276 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9282 static int lfs_rmfid(int argc, char **argv)
9284 char *fidstr, *device;
9285 int rc = 0, rc2, nr;
9286 struct fid_array *fa;
9288 if (optind > argc - 1) {
9289 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9293 device = argv[optind++];
9296 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9302 while (optind < argc) {
9305 fidstr = argv[optind++];
9306 while (*fidstr == '[')
9308 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9310 fprintf(stderr, "unrecognized FID: %s\n",
9315 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9316 /* start another batch */
9317 rc2 = lfs_rmfid_and_show_errors(device, fa);
9324 rc2 = lfs_rmfid_and_show_errors(device, fa);
9332 static int lfs_data_version(int argc, char **argv)
9339 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9342 fprintf(stderr, "%s data_version: FILE must be specified\n",
9347 while ((c = getopt(argc, argv, "nrw")) != -1) {
9350 data_version_flags = 0;
9353 data_version_flags |= LL_DV_RD_FLUSH;
9356 data_version_flags |= LL_DV_WR_FLUSH;
9360 "%s data_version: unrecognized option '%s'\n",
9361 progname, argv[optind - 1]);
9365 if (optind == argc) {
9366 fprintf(stderr, "%s data_version: FILE must be specified\n",
9371 path = argv[optind];
9372 fd = open(path, O_RDONLY);
9375 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9376 progname, path, strerror(-rc));
9380 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9383 "%s data_version: cannot get version for '%s': %s\n",
9384 progname, path, strerror(-rc));
9386 printf("%ju" "\n", (uintmax_t)data_version);
9392 static int lfs_hsm_state(int argc, char **argv)
9397 struct hsm_user_state hus;
9405 rc = llapi_hsm_state_get(path, &hus);
9407 fprintf(stderr, "can't get hsm state for %s: %s\n",
9408 path, strerror(errno = -rc));
9412 /* Display path name and status flags */
9413 printf("%s: (0x%08x)", path, hus.hus_states);
9415 if (hus.hus_states & HS_RELEASED)
9416 printf(" released");
9417 if (hus.hus_states & HS_EXISTS)
9419 if (hus.hus_states & HS_DIRTY)
9421 if (hus.hus_states & HS_ARCHIVED)
9422 printf(" archived");
9423 /* Display user-settable flags */
9424 if (hus.hus_states & HS_NORELEASE)
9425 printf(" never_release");
9426 if (hus.hus_states & HS_NOARCHIVE)
9427 printf(" never_archive");
9428 if (hus.hus_states & HS_LOST)
9429 printf(" lost_from_hsm");
9431 if (hus.hus_archive_id != 0)
9432 printf(", archive_id:%d", hus.hus_archive_id);
9435 } while (++i < argc);
9440 #define LFS_HSM_SET 0
9441 #define LFS_HSM_CLEAR 1
9444 * Generic function to set or clear HSM flags.
9445 * Used by hsm_set and hsm_clear.
9447 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9449 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9451 struct option long_opts[] = {
9452 { .val = 'A', .name = "archived", .has_arg = no_argument },
9453 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
9454 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9455 { .val = 'e', .name = "exists", .has_arg = no_argument },
9456 { .val = 'l', .name = "lost", .has_arg = no_argument },
9457 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9458 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9460 char short_opts[] = "lraAdei:";
9464 __u32 archive_id = 0;
9470 while ((c = getopt_long(argc, argv, short_opts,
9471 long_opts, NULL)) != -1) {
9477 mask |= HS_NOARCHIVE;
9480 mask |= HS_ARCHIVED;
9483 mask |= HS_NORELEASE;
9493 archive_id = strtol(optarg, &end, 10);
9494 if (errno != 0 || *end != '\0' || archive_id < 0) {
9495 fprintf(stderr, "invalid archive_id: '%s'\n",
9503 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9504 argv[0], argv[optind - 1]);
9509 /* User should have specified a flag */
9513 while (optind < argc) {
9514 path = argv[optind];
9516 /* If mode == 0, this means we apply the mask. */
9517 if (mode == LFS_HSM_SET)
9518 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9520 rc = llapi_hsm_state_set(path, 0, mask, 0);
9523 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9524 path, strerror(errno = -rc));
9533 static int lfs_hsm_action(int argc, char **argv)
9538 struct hsm_current_action hca;
9539 struct hsm_extent he;
9540 enum hsm_user_action hua;
9541 enum hsm_progress_states hps;
9549 rc = llapi_hsm_current_action(path, &hca);
9551 fprintf(stderr, "can't get hsm action for %s: %s\n",
9552 path, strerror(errno = -rc));
9555 he = hca.hca_location;
9556 hua = hca.hca_action;
9557 hps = hca.hca_state;
9559 printf("%s: %s", path, hsm_user_action2name(hua));
9561 /* Skip file without action */
9562 if (hca.hca_action == HUA_NONE) {
9567 printf(" %s ", hsm_progress_state2name(hps));
9569 if ((hps == HPS_RUNNING) &&
9570 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9571 printf("(%llu bytes moved)\n",
9572 (unsigned long long)he.length);
9573 else if ((he.offset + he.length) == LUSTRE_EOF)
9574 printf("(from %llu to EOF)\n",
9575 (unsigned long long)he.offset);
9577 printf("(from %llu to %llu)\n",
9578 (unsigned long long)he.offset,
9579 (unsigned long long)(he.offset + he.length));
9581 } while (++i < argc);
9586 static int lfs_hsm_set(int argc, char **argv)
9588 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9591 static int lfs_hsm_clear(int argc, char **argv)
9593 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9597 * Check file state and return its fid, to be used by lfs_hsm_request().
9599 * \param[in] file Path to file to check
9600 * \param[in,out] fid Pointer to allocated lu_fid struct.
9601 * \param[in,out] last_dev Pointer to last device id used.
9603 * \return 0 on success.
9605 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9611 rc = lstat(file, &st);
9613 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9617 * Checking for regular file as archiving as posix copytool
9618 * rejects archiving files other than regular files
9620 if (!S_ISREG(st.st_mode)) {
9621 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9624 /* A request should be ... */
9625 if (*last_dev != st.st_dev && *last_dev != 0) {
9627 "All files should be on the same filesystem: %s\n",
9631 *last_dev = st.st_dev;
9633 rc = llapi_path2fid(file, fid);
9635 fprintf(stderr, "Cannot read FID of %s: %s\n",
9636 file, strerror(-rc));
9642 /* Fill an HSM HUR item with a given file name.
9644 * If mntpath is set, then the filename is actually a FID, and no
9645 * lookup on the filesystem will be performed.
9647 * \param[in] hur the user request to fill
9648 * \param[in] idx index of the item inside the HUR to fill
9649 * \param[in] mntpath mountpoint of Lustre
9650 * \param[in] fname filename (if mtnpath is NULL)
9651 * or FID (if mntpath is set)
9652 * \param[in] last_dev pointer to last device id used
9654 * \retval 0 on success
9655 * \retval CMD_HELP or a negative errno on error
9657 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9658 const char *mntpath, const char *fname,
9661 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9664 hui->hui_extent.length = -1;
9667 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9669 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9672 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9676 hur->hur_request.hr_itemcount++;
9681 static int lfs_hsm_request(int argc, char **argv, int action)
9683 struct option long_opts[] = {
9684 { .val = 'a', .name = "archive", .has_arg = required_argument },
9685 { .val = 'D', .name = "data", .has_arg = required_argument },
9686 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9687 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9690 char short_opts[] = "l:D:a:m:";
9691 struct hsm_user_request *hur, *oldhur;
9696 char *filelist = NULL;
9697 char fullpath[PATH_MAX];
9698 char *opaque = NULL;
9702 int nbfile_alloc = 0;
9703 char *some_file = NULL;
9704 char *mntpath = NULL;
9710 while ((c = getopt_long(argc, argv, short_opts,
9711 long_opts, NULL)) != -1) {
9720 if (action != HUA_ARCHIVE &&
9721 action != HUA_REMOVE) {
9723 "error: -a is supported only when archiving or removing\n");
9726 archive_id = atoi(optarg);
9731 some_file = strdup(optarg);
9737 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9738 argv[0], argv[optind - 1]);
9743 /* All remaining args are files, so we have at least nbfile */
9744 nbfile = argc - optind;
9746 if ((nbfile == 0) && (!filelist))
9750 opaque_len = strlen(opaque);
9753 * Alloc the request structure with enough place to store all files
9754 * from command line.
9756 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9758 fprintf(stderr, "Cannot create the request: %s\n",
9762 nbfile_alloc = nbfile;
9764 hur->hur_request.hr_action = action;
9765 hur->hur_request.hr_archive_id = archive_id;
9766 hur->hur_request.hr_flags = 0;
9768 /* All remaining args are files, add them */
9769 if (nbfile != 0 && some_file == NULL)
9770 some_file = strdup(argv[optind]);
9772 for (i = 0; i < nbfile; i++) {
9773 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9779 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9781 /* If a filelist was specified, read the filelist from it. */
9783 fp = fopen(filelist, "r");
9785 fprintf(stderr, "Cannot read the file list %s: %s\n",
9786 filelist, strerror(errno));
9791 while ((rc = getline(&line, &len, fp)) != -1) {
9793 * If allocated buffer was too small, get something
9796 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9799 nbfile_alloc = nbfile_alloc * 2 + 1;
9801 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9805 "hsm: cannot allocate the request: %s\n",
9812 size = hur_len(oldhur);
9815 "hsm: cannot allocate %u files + %u bytes data\n",
9816 oldhur->hur_request.hr_itemcount,
9817 oldhur->hur_request.hr_data_len);
9824 memcpy(hur, oldhur, size);
9829 if (line[strlen(line) - 1] == '\n')
9830 line[strlen(line) - 1] = '\0';
9832 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9833 mntpath, line, &last_dev);
9849 /* If a --data was used, add it to the request */
9850 hur->hur_request.hr_data_len = opaque_len;
9852 memcpy(hur_data(hur), opaque, opaque_len);
9854 /* Send the HSM request */
9855 if (realpath(some_file, fullpath) == NULL) {
9856 fprintf(stderr, "Could not find path '%s': %s\n",
9857 some_file, strerror(errno));
9859 rc = llapi_hsm_request(fullpath, hur);
9861 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9862 some_file, strerror(-rc));
9872 static int lfs_hsm_archive(int argc, char **argv)
9874 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9877 static int lfs_hsm_restore(int argc, char **argv)
9879 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9882 static int lfs_hsm_release(int argc, char **argv)
9884 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9887 static int lfs_hsm_remove(int argc, char **argv)
9889 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9892 static int lfs_hsm_cancel(int argc, char **argv)
9894 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9897 static int lfs_swap_layouts(int argc, char **argv)
9902 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9903 SWAP_LAYOUTS_KEEP_MTIME |
9904 SWAP_LAYOUTS_KEEP_ATIME);
9907 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9909 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9911 int lfs_get_mode(const char *string)
9913 enum lock_mode_user mode;
9915 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9916 if (lock_mode_names[mode] == NULL)
9918 if (strcmp(string, lock_mode_names[mode]) == 0)
9925 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9927 enum lu_ladvise_type advice;
9930 advice < ARRAY_SIZE(ladvise_names); advice++) {
9931 if (ladvise_names[advice] == NULL)
9933 if (strcmp(string, ladvise_names[advice]) == 0)
9937 return LU_LADVISE_INVALID;
9940 static int lfs_ladvise(int argc, char **argv)
9942 struct option long_opts[] = {
9943 { .val = 'a', .name = "advice", .has_arg = required_argument },
9944 { .val = 'b', .name = "background", .has_arg = no_argument },
9945 { .val = 'e', .name = "end", .has_arg = required_argument },
9946 { .val = 'l', .name = "length", .has_arg = required_argument },
9947 { .val = 'm', .name = "mode", .has_arg = required_argument },
9948 { .val = 's', .name = "start", .has_arg = required_argument },
9949 { .val = 'u', .name = "unset", .has_arg = no_argument },
9951 char short_opts[] = "a:be:l:m:s:u";
9956 struct llapi_lu_ladvise advice;
9957 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9958 unsigned long long start = 0;
9959 unsigned long long end = LUSTRE_EOF;
9960 unsigned long long length = 0;
9961 unsigned long long size_units;
9962 unsigned long long flags = 0;
9966 while ((c = getopt_long(argc, argv, short_opts,
9967 long_opts, NULL)) != -1) {
9970 advice_type = lfs_get_ladvice(optarg);
9971 if (advice_type == LU_LADVISE_INVALID) {
9973 "%s: invalid advice type '%s'\n",
9975 fprintf(stderr, "Valid types:");
9977 for (advice_type = 0;
9978 advice_type < ARRAY_SIZE(ladvise_names);
9980 if (ladvise_names[advice_type] == NULL)
9982 fprintf(stderr, " %s",
9983 ladvise_names[advice_type]);
9985 fprintf(stderr, "\n");
9998 rc = llapi_parse_size(optarg, &end,
10001 fprintf(stderr, "%s: bad end offset '%s'\n",
10008 rc = llapi_parse_size(optarg, &start,
10012 "%s: bad start offset '%s'\n",
10019 rc = llapi_parse_size(optarg, &length,
10022 fprintf(stderr, "%s: bad length '%s'\n",
10028 mode = lfs_get_mode(optarg);
10031 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10039 fprintf(stderr, "%s: option '%s' unrecognized\n",
10040 argv[0], argv[optind - 1]);
10045 if (advice_type == LU_LADVISE_INVALID) {
10046 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10047 fprintf(stderr, "Valid types:");
10048 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10050 if (ladvise_names[advice_type] == NULL)
10052 fprintf(stderr, " %s", ladvise_names[advice_type]);
10054 fprintf(stderr, "\n");
10058 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10060 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10065 if (argc <= optind) {
10066 fprintf(stderr, "%s: please give one or more file names\n",
10071 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10072 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10077 if (end == LUSTRE_EOF && length != 0)
10078 end = start + length;
10080 if (end <= start) {
10081 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10082 argv[0], start, end);
10086 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10087 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10092 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10093 fprintf(stderr, "%s: mode is required with lockahead\n",
10098 while (optind < argc) {
10101 path = argv[optind++];
10103 fd = open(path, O_RDONLY);
10105 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10106 argv[0], path, strerror(errno));
10111 advice.lla_start = start;
10112 advice.lla_end = end;
10113 advice.lla_advice = advice_type;
10114 advice.lla_value1 = 0;
10115 advice.lla_value2 = 0;
10116 advice.lla_value3 = 0;
10117 advice.lla_value4 = 0;
10118 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10119 advice.lla_lockahead_mode = mode;
10120 advice.lla_peradvice_flags = flags;
10123 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10127 "%s: cannot give advice '%s' to file '%s': %s\n",
10128 argv[0], ladvise_names[advice_type],
10129 path, strerror(errno));
10135 if (rc == 0 && rc2 < 0)
10141 static const char *const heat_names[] = LU_HEAT_NAMES;
10143 static int lfs_heat_get(int argc, char **argv)
10145 struct lu_heat *heat;
10154 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10156 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10161 while (optind < argc) {
10162 path = argv[optind++];
10164 fd = open(path, O_RDONLY);
10166 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10167 argv[0], path, strerror(errno));
10172 heat->lh_count = OBD_HEAT_COUNT;
10173 rc2 = llapi_heat_get(fd, heat);
10177 "%s: cannot get heat of file '%s': %s\n",
10178 argv[0], path, strerror(errno));
10182 printf("flags: %x\n", heat->lh_flags);
10183 for (i = 0; i < heat->lh_count; i++)
10184 printf("%s: %llu\n", heat_names[i],
10185 (unsigned long long)heat->lh_heat[i]);
10187 if (rc == 0 && rc2 < 0)
10195 static int lfs_heat_set(int argc, char **argv)
10197 struct option long_opts[] = {
10198 { .val = 'c', .name = "clear", .has_arg = no_argument },
10199 { .val = 'o', .name = "off", .has_arg = no_argument },
10200 { .val = 'O', .name = "on", .has_arg = no_argument },
10201 { .name = NULL } };
10202 enum lu_heat_flag flags = 0;
10212 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
10215 flags |= LU_HEAT_FLAG_CLEAR;
10218 flags |= LU_HEAT_FLAG_CLEAR;
10219 flags |= LU_HEAT_FLAG_OFF;
10222 flags &= ~LU_HEAT_FLAG_OFF;
10227 fprintf(stderr, "%s: option '%s' unrecognized\n",
10228 argv[0], argv[optind - 1]);
10233 if (argc <= optind) {
10234 fprintf(stderr, "%s: please give one or more file names\n",
10239 while (optind < argc) {
10240 path = argv[optind++];
10242 fd = open(path, O_RDONLY);
10244 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10245 argv[0], path, strerror(errno));
10250 rc2 = llapi_heat_set(fd, flags);
10254 "%s: cannot setflags heat of file '%s': %s\n",
10255 argv[0], path, strerror(errno));
10259 if (rc == 0 && rc2 < 0)
10266 * The input string contains a comma delimited list of component ids and
10267 * ranges, for example "1,2-4,7".
10269 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10271 bool end_of_loop = false;
10279 while (!end_of_loop) {
10283 char *endptr = NULL;
10286 ptr = strchrnul(arg, ',');
10287 end_of_loop = *ptr == '\0';
10290 start_index = strtol(arg, &endptr, 0);
10291 if (endptr == arg) /* no data at all */
10293 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10295 if (start_index < 0)
10298 end_index = start_index;
10299 if (*endptr == '-') {
10300 end_index = strtol(endptr + 1, &endptr, 0);
10301 if (*endptr != '\0')
10303 if (end_index < start_index)
10307 for (i = start_index; i <= end_index && size > 0; i++) {
10310 /* remove duplicate */
10311 for (j = 0; j < nr; j++) {
10315 if (j == nr) { /* no duplicate */
10321 if (size == 0 && i < end_index)
10328 if (!end_of_loop && ptr)
10331 return rc < 0 ? rc : nr;
10335 * struct verify_mirror_id - Mirror id to be verified.
10336 * @mirror_id: A specified mirror id.
10337 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10339 struct verify_mirror_id {
10345 * compare_mirror_ids() - Compare mirror ids.
10346 * @layout: Mirror component list.
10347 * @cbdata: Callback data in verify_mirror_id structure.
10349 * This is a callback function called by llapi_layout_comp_iterate()
10350 * to compare the specified mirror id with the one in the current
10351 * component of @layout. If they are the same, then the specified
10352 * mirror id is valid.
10354 * Return: a negative error code on failure or
10355 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10356 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
10359 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10361 struct verify_mirror_id *mirror_id_cbdata =
10362 (struct verify_mirror_id *)cbdata;
10363 uint32_t mirror_id;
10366 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10370 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10371 progname, strerror(errno));
10375 if (mirror_id_cbdata->mirror_id == mirror_id) {
10376 mirror_id_cbdata->is_valid_id = true;
10377 return LLAPI_LAYOUT_ITER_STOP;
10380 return LLAPI_LAYOUT_ITER_CONT;
10384 * verify_mirror_ids() - Verify specified mirror ids.
10385 * @fname: Mirrored file name.
10386 * @mirror_ids: Specified mirror ids to be verified.
10387 * @ids_nr: Number of specified mirror ids.
10389 * This function verifies that specified @mirror_ids are valid
10390 * in the mirrored file @fname.
10392 * Return: 0 on success or a negative error code on failure.
10395 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10397 struct llapi_layout *layout = NULL;
10398 struct verify_mirror_id mirror_id_cbdata = { 0 };
10400 uint32_t flr_state;
10409 if (stat(fname, &stbuf) < 0) {
10410 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10411 progname, fname, strerror(errno));
10416 if (!S_ISREG(stbuf.st_mode)) {
10417 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10423 fd = open(fname, O_DIRECT | O_RDONLY);
10425 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10426 progname, fname, strerror(errno));
10431 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10433 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10434 progname, fname, strerror(errno));
10438 layout = llapi_layout_get_by_fd(fd, 0);
10440 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10441 progname, fname, strerror(errno));
10443 llapi_lease_release(fd);
10447 rc = llapi_layout_flags_get(layout, &flr_state);
10449 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10450 progname, fname, strerror(errno));
10455 flr_state &= LCM_FL_FLR_MASK;
10456 switch (flr_state) {
10459 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10460 progname, fname, llapi_layout_flags_string(flr_state));
10467 for (i = 0; i < ids_nr; i++) {
10468 mirror_id_cbdata.mirror_id = mirror_ids[i];
10469 mirror_id_cbdata.is_valid_id = false;
10471 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10472 &mirror_id_cbdata);
10476 "%s: '%s' failed to verify mirror id: %u.\n",
10477 progname, fname, mirror_ids[i]);
10481 if (!mirror_id_cbdata.is_valid_id) {
10484 "%s: '%s' invalid specified mirror id: %u.\n",
10485 progname, fname, mirror_ids[i]);
10491 llapi_layout_free(layout);
10492 llapi_lease_release(fd);
10500 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10501 __u16 *mirror_ids, int ids_nr)
10503 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10504 struct llapi_layout *layout;
10506 uint32_t flr_state;
10515 if (stat(fname, &stbuf) < 0) {
10516 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10517 progname, fname, strerror(errno));
10521 if (!S_ISREG(stbuf.st_mode)) {
10522 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10528 fd = open(fname, O_DIRECT | O_RDWR);
10530 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10531 progname, fname, strerror(errno));
10536 layout = llapi_layout_get_by_fd(fd, 0);
10538 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10539 progname, fname, strerror(errno));
10544 rc = llapi_layout_flags_get(layout, &flr_state);
10546 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10547 progname, fname, strerror(errno));
10552 flr_state &= LCM_FL_FLR_MASK;
10553 if (flr_state == LCM_FL_NONE) {
10555 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10560 /* get stale component info */
10561 comp_size = llapi_mirror_find_stale(layout, comp_array,
10562 ARRAY_SIZE(comp_array),
10563 mirror_ids, ids_nr);
10564 if (comp_size <= 0) {
10569 ioc->lil_mode = LL_LEASE_WRLCK;
10570 ioc->lil_flags = LL_LEASE_RESYNC;
10571 rc = llapi_lease_set(fd, ioc);
10573 if (rc == -EALREADY)
10577 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10578 progname, fname, strerror(-rc));
10582 /* get the read range [start, end) */
10583 start = comp_array[0].lrc_start;
10584 end = comp_array[0].lrc_end;
10585 for (idx = 1; idx < comp_size; idx++) {
10586 if (comp_array[idx].lrc_start < start)
10587 start = comp_array[idx].lrc_start;
10588 if (end < comp_array[idx].lrc_end)
10589 end = comp_array[idx].lrc_end;
10592 rc = llapi_lease_check(fd);
10593 if (rc != LL_LEASE_WRLCK) {
10594 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10599 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10602 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10603 progname, fname, strerror(-rc));
10605 /* need to do the lease unlock even resync fails */
10606 ioc->lil_mode = LL_LEASE_UNLCK;
10607 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10608 ioc->lil_count = 0;
10609 for (idx = 0; idx < comp_size; idx++) {
10610 if (comp_array[idx].lrc_synced) {
10611 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10616 rc2 = llapi_lease_set(fd, ioc);
10618 * llapi_lease_set returns lease mode when it request to unlock
10622 /* rc2 == 0 means lost lease lock */
10623 if (rc2 == 0 && rc == 0)
10625 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10627 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10631 llapi_layout_free(layout);
10638 static inline int lfs_mirror_resync(int argc, char **argv)
10640 struct ll_ioc_lease *ioc = NULL;
10641 __u16 mirror_ids[128] = { 0 };
10646 struct option long_opts[] = {
10647 { .val = 'o', .name = "only", .has_arg = required_argument },
10648 { .name = NULL } };
10650 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10653 rc = parse_mirror_ids(mirror_ids,
10654 sizeof(mirror_ids) / sizeof(__u16),
10658 "%s: bad mirror ids '%s'.\n",
10665 fprintf(stderr, "%s: options '%s' unrecognized.\n",
10666 argv[0], argv[optind - 1]);
10672 if (argc == optind) {
10673 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10678 if (ids_nr > 0 && argc > optind + 1) {
10680 "%s: option '--only' cannot be used upon multiple files.\n",
10687 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10692 /* set the lease on the file */
10693 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10695 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10696 argv[0], strerror(errno));
10701 for (; optind < argc; optind++) {
10702 rc = lfs_mirror_resync_file(argv[optind], ioc,
10703 mirror_ids, ids_nr);
10704 /* ignore previous file's error, continue with next file */
10707 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10715 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10717 struct llapi_layout *layout;
10720 layout = llapi_layout_get_by_fd(fd, 0);
10722 fprintf(stderr, "could not get layout.\n");
10726 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10728 fprintf(stderr, "failed to iterate layout\n");
10729 llapi_layout_free(layout);
10732 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10733 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10734 llapi_layout_free(layout);
10738 llapi_layout_free(layout);
10744 * Check whether two files are the same file
10745 * \retval 0 same file
10746 * \retval 1 not the same file
10747 * \retval <0 error code
10749 static inline int check_same_file(int fd, const char *f2)
10751 struct stat stbuf1;
10752 struct stat stbuf2;
10754 if (fstat(fd, &stbuf1) < 0)
10757 if (stat(f2, &stbuf2) < 0)
10760 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10761 stbuf1.st_ino == stbuf2.st_ino)
10767 static inline int lfs_mirror_read(int argc, char **argv)
10770 __u16 mirror_id = 0;
10771 const char *outfile = NULL;
10777 const size_t buflen = 4 << 20;
10779 struct option long_opts[] = {
10780 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10781 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10782 { .name = NULL } };
10784 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10789 unsigned long int id;
10792 id = strtoul(optarg, &end, 0);
10793 if (errno != 0 || *end != '\0' || id == 0 ||
10796 "%s %s: invalid mirror ID '%s'\n",
10797 progname, argv[0], optarg);
10801 mirror_id = (__u16)id;
10808 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10809 progname, argv[optind - 1]);
10814 if (argc == optind) {
10815 fprintf(stderr, "%s %s: no mirrored file provided\n",
10816 progname, argv[0]);
10818 } else if (argc > optind + 1) {
10819 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10823 if (mirror_id == 0) {
10824 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10825 progname, argv[0]);
10829 /* open mirror file */
10830 fname = argv[optind];
10831 fd = open(fname, O_DIRECT | O_RDONLY);
10833 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10834 progname, argv[0], fname, strerror(errno));
10838 /* verify mirror id */
10839 rc = verify_mirror_id_by_fd(fd, mirror_id);
10842 "%s %s: cannot find mirror with ID %u in '%s'\n",
10843 progname, argv[0], mirror_id, fname);
10847 /* open output file - O_EXCL ensures output is not the same as input */
10849 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10851 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10852 progname, argv[0], outfile, strerror(errno));
10857 outfd = STDOUT_FILENO;
10860 /* allocate buffer */
10861 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10863 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10864 progname, argv[0], rc);
10870 ssize_t bytes_read;
10871 ssize_t written = 0;
10873 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10874 if (bytes_read < 0) {
10877 "%s %s: fail to read data from mirror %u: %s\n",
10878 progname, argv[0], mirror_id, strerror(-rc));
10883 if (bytes_read == 0)
10886 while (written < bytes_read) {
10889 written2 = write(outfd, buf + written,
10890 bytes_read - written);
10891 if (written2 < 0) {
10893 "%s %s: fail to write %s: %s\n",
10894 progname, argv[0], outfile ? : "STDOUT",
10899 written += written2;
10902 if (written != bytes_read) {
10904 "%s %s: written %ld bytes does not match with %ld read.\n",
10905 progname, argv[0], written, bytes_read);
10927 static inline int lfs_mirror_write(int argc, char **argv)
10930 __u16 mirror_id = 0;
10931 const char *inputfile = NULL;
10937 const size_t buflen = 4 << 20;
10939 size_t page_size = sysconf(_SC_PAGESIZE);
10940 struct ll_ioc_lease_id ioc;
10942 struct option long_opts[] = {
10943 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10944 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10945 { .name = NULL } };
10947 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10952 unsigned long int id;
10955 id = strtoul(optarg, &end, 0);
10956 if (errno != 0 || *end != '\0' || id == 0 ||
10959 "%s %s: invalid mirror ID '%s'\n",
10960 progname, argv[0], optarg);
10964 mirror_id = (__u16)id;
10968 inputfile = optarg;
10971 fprintf(stderr, "%s: option '%s' unrecognized\n",
10972 progname, argv[optind - 1]);
10977 if (argc == optind) {
10978 fprintf(stderr, "%s %s: no mirrored file provided\n",
10979 progname, argv[0]);
10981 } else if (argc > optind + 1) {
10982 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10986 if (mirror_id == 0) {
10987 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10988 progname, argv[0]);
10992 /* open mirror file */
10993 fname = argv[optind];
10994 fd = open(fname, O_DIRECT | O_WRONLY);
10996 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10997 progname, argv[0], fname, strerror(errno));
11001 /* verify mirror id */
11002 rc = verify_mirror_id_by_fd(fd, mirror_id);
11005 "%s %s: cannot find mirror with ID %u in '%s'\n",
11006 progname, argv[0], mirror_id, fname);
11010 /* open input file */
11012 rc = check_same_file(fd, inputfile);
11015 "%s %s: input file cannot be the mirrored file\n",
11016 progname, argv[0]);
11022 inputfd = open(inputfile, O_RDONLY, 0644);
11024 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11025 progname, argv[0], inputfile, strerror(errno));
11030 inputfd = STDIN_FILENO;
11033 /* allocate buffer */
11034 rc = posix_memalign(&buf, page_size, buflen);
11036 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11037 progname, argv[0], rc);
11038 goto close_inputfd;
11041 /* prepare target mirror components instantiation */
11042 ioc.lil_mode = LL_LEASE_WRLCK;
11043 ioc.lil_flags = LL_LEASE_RESYNC;
11044 ioc.lil_mirror_id = mirror_id;
11045 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11048 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11049 progname, argv[0], fname, strerror(errno));
11055 ssize_t bytes_read;
11059 rc = llapi_lease_check(fd);
11060 if (rc != LL_LEASE_WRLCK) {
11061 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11062 progname, argv[0], fname);
11066 bytes_read = read(inputfd, buf, buflen);
11067 if (bytes_read < 0) {
11070 "%s %s: fail to read data from '%s': %s\n",
11071 progname, argv[0], inputfile ? : "STDIN",
11078 if (bytes_read == 0)
11081 /* round up to page align to make direct IO happy. */
11082 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11084 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11089 "%s %s: fail to write to mirror %u: %s\n",
11090 progname, argv[0], mirror_id,
11098 if (pos & (page_size - 1)) {
11099 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11104 ioc.lil_mode = LL_LEASE_UNLCK;
11105 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11107 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11112 "%s %s: release lease lock of '%s' failed: %s\n",
11113 progname, argv[0], fname, strerror(errno));
11130 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11132 struct llapi_layout *layout;
11133 struct collect_ids_data cid = { .cid_ids = ids,
11135 .cid_exclude = exclude_id, };
11138 layout = llapi_layout_get_by_fd(fd, 0);
11140 fprintf(stderr, "could not get layout\n");
11144 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11146 fprintf(stderr, "failed to iterate layout\n");
11147 llapi_layout_free(layout);
11151 llapi_layout_free(layout);
11153 return cid.cid_count;
11156 static inline int lfs_mirror_copy(int argc, char **argv)
11159 __u16 read_mirror_id = 0;
11160 __u16 ids[128] = { 0 };
11162 struct llapi_layout *layout = NULL;
11163 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11170 struct ll_ioc_lease *ioc = NULL;
11171 struct ll_ioc_lease_id *resync_ioc;
11173 struct option long_opts[] = {
11174 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
11175 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
11176 { .name = NULL } };
11178 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
11183 unsigned long int id;
11186 id = strtoul(optarg, &end, 0);
11187 if (errno != 0 || *end != '\0' || id == 0 ||
11190 "%s %s: invalid read mirror ID '%s'\n",
11191 progname, argv[0], optarg);
11195 read_mirror_id = (__u16)id;
11199 if (!strcmp(optarg, "-1")) {
11200 /* specify all other mirrors */
11201 ids[0] = (__u16)-1;
11204 count = parse_mirror_ids((__u16 *)ids,
11212 fprintf(stderr, "%s: option '%s' unrecognized\n",
11213 progname, argv[optind - 1]);
11218 if (argc == optind) {
11219 fprintf(stderr, "%s %s: no mirrored file provided\n",
11220 progname, argv[0]);
11222 } else if (argc > optind + 1) {
11223 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11227 if (read_mirror_id == 0) {
11229 "%s %s: no valid read mirror ID %d is provided\n",
11230 progname, argv[0], read_mirror_id);
11236 "%s %s: no write mirror ID is provided\n",
11237 progname, argv[0]);
11241 for (i = 0; i < count; i++) {
11242 if (read_mirror_id == ids[i]) {
11244 "%s %s: read and write mirror ID cannot be the same\n",
11245 progname, argv[0]);
11250 /* open mirror file */
11251 fname = argv[optind];
11253 fd = open(fname, O_DIRECT | O_RDWR);
11255 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11256 progname, argv[0], fname, strerror(errno));
11260 /* write to all other mirrors */
11261 if (ids[0] == (__u16)-1) {
11262 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11266 "%s %s: failed to get other mirror ids in '%s': %d\n",
11267 progname, argv[0], fname, rc);
11272 /* verify mirror id */
11273 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11276 "%s %s: cannot find mirror with ID %u in '%s'\n",
11277 progname, argv[0], read_mirror_id, fname);
11281 for (i = 0; i < count; i++) {
11282 rc = verify_mirror_id_by_fd(fd, ids[i]);
11285 "%s %s: cannot find mirror with ID %u in '%s'\n",
11286 progname, argv[0], ids[i], fname);
11291 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11294 "%s %s: cannot alloc comp id array for ioc: %s\n",
11295 progname, argv[0], strerror(errno));
11300 /* get stale component info */
11301 layout = llapi_layout_get_by_fd(fd, 0);
11303 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11304 progname, argv[0], fname, strerror(errno));
11308 comp_size = llapi_mirror_find_stale(layout, comp_array,
11309 ARRAY_SIZE(comp_array),
11311 llapi_layout_free(layout);
11312 if (comp_size < 0) {
11317 /* prepare target mirror components instantiation */
11318 resync_ioc = (struct ll_ioc_lease_id *)ioc;
11319 resync_ioc->lil_mode = LL_LEASE_WRLCK;
11320 resync_ioc->lil_flags = LL_LEASE_RESYNC;
11322 resync_ioc->lil_mirror_id = ids[0];
11324 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11325 rc = llapi_lease_set(fd, ioc);
11328 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11329 progname, argv[0], fname, strerror(errno));
11333 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11336 fprintf(stderr, "%s %s: copy error: %d\n",
11337 progname, argv[0], rc);
11341 fprintf(stdout, "mirror copied successfully: ");
11342 for (i = 0; i < copied; i++)
11343 fprintf(stdout, "%d ", ids[i]);
11344 fprintf(stdout, "\n");
11346 ioc->lil_mode = LL_LEASE_UNLCK;
11347 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11348 ioc->lil_count = 0;
11349 for (i = 0; i < comp_size; i++) {
11352 for (j = 0; j < copied; j++) {
11353 if (comp_array[i].lrc_mirror_id != ids[j])
11356 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11360 rc = llapi_lease_set(fd, ioc);
11365 "%s %s: release lease lock of '%s' failed: %s\n",
11366 progname, argv[0], fname, strerror(errno));
11381 * struct verify_chunk - Mirror chunk to be verified.
11382 * @chunk: [start, end) of the chunk.
11383 * @mirror_count: Number of mirror ids in @mirror_id array.
11384 * @mirror_id: Array of valid mirror ids that cover the chunk.
11386 struct verify_chunk {
11387 struct lu_extent chunk;
11388 unsigned int mirror_count;
11389 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11393 * print_chunks() - Print chunk information.
11394 * @fname: Mirrored file name.
11395 * @chunks: Array of chunks.
11396 * @chunk_count: Number of chunks in @chunks array.
11398 * This function prints [start, end) of each chunk in @chunks
11399 * for mirrored file @fname, and also prints the valid mirror ids
11400 * that cover the chunk.
11405 void print_chunks(const char *fname, struct verify_chunk *chunks,
11411 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11412 for (i = 0; i < chunk_count; i++) {
11413 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11415 if (chunks[i].mirror_count == 0)
11416 fprintf(stdout, "\t[");
11418 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11419 for (j = 1; j < chunks[i].mirror_count; j++)
11420 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11422 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11424 fprintf(stdout, "\n");
11428 * print_checksums() - Print CRC-32 checksum values.
11429 * @chunk: A chunk and its corresponding valid mirror ids.
11430 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
11432 * This function prints CRC-32 checksum values on @chunk for
11433 * each valid mirror that covers it.
11438 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11443 "CRC-32 checksum value for chunk "DEXT":\n",
11444 PEXT(&chunk->chunk));
11445 for (i = 0; i < chunk->mirror_count; i++)
11446 fprintf(stdout, "Mirror %u:\t%#lx\n",
11447 chunk->mirror_id[i], crc[i]);
11448 fprintf(stdout, "\n");
11452 * filter_mirror_id() - Filter specified mirror ids.
11453 * @chunks: Array of chunks.
11454 * @chunk_count: Number of chunks in @chunks array.
11455 * @mirror_ids: Specified mirror ids to be verified.
11456 * @ids_nr: Number of specified mirror ids.
11458 * This function scans valid mirror ids that cover each chunk in @chunks
11459 * and filters specified mirror ids.
11464 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11465 __u16 *mirror_ids, int ids_nr)
11470 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11471 unsigned int valid_count = 0;
11473 for (i = 0; i < chunk_count; i++) {
11474 if (chunks[i].mirror_count == 0)
11478 for (j = 0; j < ids_nr; j++) {
11479 for (k = 0; k < chunks[i].mirror_count; k++) {
11480 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11481 valid_id[valid_count] = mirror_ids[j];
11488 memcpy(chunks[i].mirror_id, valid_id,
11489 sizeof(__u16) * valid_count);
11490 chunks[i].mirror_count = valid_count;
11495 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11496 * @layout: Mirror component list.
11497 * @chunks: Array of chunks.
11498 * @chunks_size: Array size of @chunks.
11500 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11501 * to find out chunk segments and store them in @chunks array.
11503 * The @mirror_id array in each element of @chunks will store the valid
11504 * mirror ids that cover the chunk. If a mirror component covering the
11505 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11506 * will not be stored into the @mirror_id array, and the chunk for that
11507 * mirror will not be verified.
11509 * The @mirror_count in each element of @chunks will store the number of
11510 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11511 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11512 * indicates the chunk is valid in only one mirror. In both cases, the
11513 * chunk will not be verified.
11515 * Here is an example:
11517 * 0 1M 2M 3M 4M EOF
11518 * +------+-------------+--------------------+
11519 * | | | S | mirror1
11520 * +------+------+------+------+-------------+
11521 * | | S | S | | mirror2
11522 * +-------------+------+------+-------------+
11524 * prepared @chunks array will contain 5 elements:
11525 * (([0, 1M), [1, 2], 2),
11526 * ([1M, 2M), [1, 2], 2),
11527 * ([2M, 3M), [1], 1),
11528 * ([3M, 4M], [], 0),
11529 * ([4M, EOF), [2], 1))
11531 * Return: the actual array size of @chunks on success
11532 * or a negative error code on failure.
11535 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11536 struct verify_chunk *chunks,
11537 size_t chunks_size)
11541 uint32_t mirror_id;
11547 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11550 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11553 "%s: move to the first layout component: %s.\n",
11554 progname, strerror(errno));
11560 chunks[idx].chunk.e_end = LUSTRE_EOF;
11562 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11565 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11566 progname, strerror(errno));
11570 if (start > chunks[idx].chunk.e_start ||
11571 end <= chunks[idx].chunk.e_start)
11574 if (end < chunks[idx].chunk.e_end)
11575 chunks[idx].chunk.e_end = end;
11577 rc = llapi_layout_comp_flags_get(layout, &flags);
11580 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11581 progname, strerror(errno));
11585 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11588 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11591 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11592 progname, strerror(errno));
11596 chunks[idx].mirror_id[i] = mirror_id;
11598 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11600 "%s: mirror_id array is too small.\n",
11607 rc = llapi_layout_comp_use(layout,
11608 LLAPI_LAYOUT_COMP_USE_NEXT);
11611 "%s: move to the next layout component: %s.\n",
11612 progname, strerror(errno));
11615 } /* loop through all components */
11617 chunks[idx].mirror_count = i;
11619 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11623 if (idx >= chunks_size) {
11624 fprintf(stderr, "%s: chunks array is too small.\n",
11630 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11634 return rc < 0 ? rc : idx + 1;
11638 * lfs_mirror_verify_chunk() - Verify a chunk.
11639 * @fd: File descriptor of the mirrored file.
11640 * @file_size: Size of the mirrored file.
11641 * @chunk: A chunk and its corresponding valid mirror ids.
11642 * @verbose: Verbose mode.
11644 * This function verifies a @chunk contains exactly the same data
11645 * ammong the mirrors that cover it.
11647 * If @verbose is specified, then the function will print where the
11648 * differences are if the data do not match. Otherwise, it will
11649 * just return an error in that case.
11651 * Return: 0 on success or a negative error code on failure.
11654 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11655 struct verify_chunk *chunk, int verbose)
11657 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11659 size_t page_size = sysconf(_SC_PAGESIZE);
11660 ssize_t bytes_read;
11661 ssize_t bytes_done;
11665 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11669 if (file_size == 0)
11672 rc = posix_memalign(&buf, page_size, buflen);
11673 if (rc) /* error code is returned directly */
11677 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11678 PEXT(&chunk->chunk));
11679 for (i = 0; i < chunk->mirror_count; i++)
11680 fprintf(stdout, " %u", chunk->mirror_id[i]);
11681 fprintf(stdout, "\n");
11685 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11686 pos = chunk->chunk.e_start;
11687 while (bytes_done < count) {
11688 /* compute initial CRC-32 checksum */
11689 crc = crc32(0L, Z_NULL, 0);
11690 memset(crc_array, 0, sizeof(crc_array));
11693 for (i = 0; i < chunk->mirror_count; i++) {
11694 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11696 if (bytes_read < 0) {
11699 "%s: failed to read data from mirror %u: %s.\n",
11700 progname, chunk->mirror_id[i],
11705 /* compute new CRC-32 checksum */
11706 crc_array[i] = crc32(crc, buf, bytes_read);
11710 print_checksums(chunk, crc_array);
11712 /* compare CRC-32 checksum values */
11713 for (i = 1; i < chunk->mirror_count; i++) {
11714 if (crc_array[i] != crc_array[0]) {
11718 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11719 progname, PEXT(&chunk->chunk),
11720 chunk->mirror_id[0],
11721 chunk->mirror_id[i]);
11726 bytes_done += bytes_read;
11729 if (verbose > 1 && rc == 0) {
11730 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11731 PEXT(&chunk->chunk));
11732 for (i = 0; i < chunk->mirror_count; i++)
11733 fprintf(stdout, " %u", chunk->mirror_id[i]);
11734 fprintf(stdout, " PASS\n\n");
11743 * lfs_mirror_verify_file() - Verify a mirrored file.
11744 * @fname: Mirrored file name.
11745 * @mirror_ids: Specified mirror ids to be verified.
11746 * @ids_nr: Number of specified mirror ids.
11747 * @verbose: Verbose mode.
11749 * This function verifies that each SYNC mirror of a mirrored file
11750 * specified by @fname contains exactly the same data.
11752 * If @mirror_ids is specified, then the function will verify the
11753 * mirrors specified by @mirror_ids contain exactly the same data.
11755 * If @verbose is specified, then the function will print where the
11756 * differences are if the data do not match. Otherwise, it will
11757 * just return an error in that case.
11759 * Return: 0 on success or a negative error code on failure.
11762 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11765 struct verify_chunk chunks_array[1024] = { };
11766 struct llapi_layout *layout = NULL;
11768 uint32_t flr_state;
11770 int chunk_count = 0;
11776 if (stat(fname, &stbuf) < 0) {
11777 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11778 progname, fname, strerror(errno));
11783 if (!S_ISREG(stbuf.st_mode)) {
11784 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11790 if (stbuf.st_size == 0) {
11792 fprintf(stdout, "%s: '%s' file size is 0.\n",
11798 fd = open(fname, O_DIRECT | O_RDONLY);
11800 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11801 progname, fname, strerror(errno));
11806 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11808 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11809 progname, fname, strerror(errno));
11813 layout = llapi_layout_get_by_fd(fd, 0);
11815 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11816 progname, fname, strerror(errno));
11818 llapi_lease_release(fd);
11822 rc = llapi_layout_flags_get(layout, &flr_state);
11824 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11825 progname, fname, strerror(errno));
11830 flr_state &= LCM_FL_FLR_MASK;
11831 switch (flr_state) {
11834 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11835 progname, fname, llapi_layout_flags_string(flr_state));
11841 /* find out mirror chunks to be verified */
11842 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11843 ARRAY_SIZE(chunks_array));
11844 if (chunk_count < 0) {
11850 /* filter specified mirror ids */
11851 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11854 print_chunks(fname, chunks_array, chunk_count);
11856 for (idx = 0; idx < chunk_count; idx++) {
11857 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11860 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11862 PEXT(&chunks_array[idx].chunk),
11863 (unsigned long long)stbuf.st_size);
11867 if (chunks_array[idx].mirror_count == 0) {
11869 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11871 PEXT(&chunks_array[idx].chunk));
11873 fprintf(stderr, "skipped\n");
11877 fprintf(stderr, "failed\n");
11881 if (chunks_array[idx].mirror_count == 1) {
11884 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11886 PEXT(&chunks_array[idx].chunk),
11887 chunks_array[idx].mirror_id[0]);
11891 rc = llapi_lease_check(fd);
11892 if (rc != LL_LEASE_RDLCK) {
11893 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11898 /* verify one chunk */
11899 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11900 &chunks_array[idx], verbose);
11914 llapi_layout_free(layout);
11915 llapi_lease_release(fd);
11923 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11924 * @argc: The count of lfs mirror verify command line arguments.
11925 * @argv: Array of strings for lfs mirror verify command line arguments.
11927 * This function parses lfs mirror verify command and verifies the
11928 * specified mirrored file(s).
11930 * Return: 0 on success or a negative error code on failure.
11932 static inline int lfs_mirror_verify(int argc, char **argv)
11934 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11940 char cmd[PATH_MAX];
11942 struct option long_opts[] = {
11943 { .val = 'o', .name = "only", .has_arg = required_argument },
11944 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11945 { .name = NULL } };
11947 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11949 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11952 rc = parse_mirror_ids(mirror_ids,
11953 ARRAY_SIZE(mirror_ids),
11957 "%s: bad mirror ids '%s'.\n",
11964 "%s: at least 2 mirror ids needed with '--only' option.\n",
11974 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11975 progname, argv[optind - 1]);
11981 if (argc == optind) {
11982 fprintf(stderr, "%s: no file name given.\n", progname);
11987 if (ids_nr > 0 && argc > optind + 1) {
11989 "%s: '--only' cannot be used upon multiple files.\n",
11996 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12002 for (; optind < argc; optind++) {
12003 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12013 * lfs_mirror() - Parse and execute lfs mirror commands.
12014 * @argc: The count of lfs mirror command line arguments.
12015 * @argv: Array of strings for lfs mirror command line arguments.
12017 * This function parses lfs mirror commands and performs the
12018 * corresponding functions specified in mirror_cmdlist[].
12020 * Return: 0 on success or an error code on failure.
12022 static int lfs_mirror(int argc, char **argv)
12024 char cmd[PATH_MAX];
12027 setlinebuf(stdout);
12029 Parser_init("lfs-mirror > ", mirror_cmdlist);
12031 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12033 program_invocation_short_name = cmd;
12035 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12037 rc = Parser_commands();
12039 return rc < 0 ? -rc : rc;
12042 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12044 #if __BYTE_ORDER == __BIG_ENDIAN
12045 __swab16s(&attrs->lsa_valid);
12046 __swab64s(&attrs->lsa_size);
12047 __swab64s(&attrs->lsa_blocks);
12051 enum lfs_som_type {
12052 LFS_SOM_SIZE = 0x1,
12053 LFS_SOM_BLOCKS = 0x2,
12054 LFS_SOM_FLAGS = 0x4,
12055 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12059 static int lfs_getsom(int argc, char **argv)
12062 struct lustre_som_attrs *attrs;
12063 char buf[sizeof(*attrs) + 64];
12064 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12067 while ((c = getopt(argc, argv, "sbf")) != -1) {
12070 type = LFS_SOM_SIZE;
12073 type = LFS_SOM_BLOCKS;
12076 type = LFS_SOM_FLAGS;
12079 fprintf(stderr, "%s: invalid option '%c'\n",
12089 fprintf(stderr, "%s: %s\n",
12090 progname, argc == 0 ? "miss file target" :
12091 "input more than 2 files");
12096 attrs = (void *)buf;
12097 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12100 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12101 argv[0], strerror(errno), errno);
12105 lustre_som_swab(attrs);
12108 case LFS_SOM_ATTR_ALL:
12109 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12110 path, (unsigned long long)attrs->lsa_size,
12111 (unsigned long long)attrs->lsa_blocks,
12115 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12117 case LFS_SOM_BLOCKS:
12118 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12120 case LFS_SOM_FLAGS:
12121 printf("%x\n", attrs->lsa_valid);
12124 fprintf(stderr, "%s: unknown option\n", progname);
12132 * lfs_mirror_list_commands() - List lfs mirror commands.
12133 * @argc: The count of command line arguments.
12134 * @argv: Array of strings for command line arguments.
12136 * This function lists lfs mirror commands defined in mirror_cmdlist[].
12138 * Return: 0 on success.
12140 static int lfs_mirror_list_commands(int argc, char **argv)
12142 char buffer[81] = "";
12144 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12150 static int lfs_pcc_attach(int argc, char **argv)
12152 struct option long_opts[] = {
12153 { .val = 'i', .name = "id", .has_arg = required_argument },
12154 { .name = NULL } };
12157 __u32 archive_id = 0;
12160 char fullpath[PATH_MAX];
12161 enum lu_pcc_type type = LU_PCC_READWRITE;
12164 while ((c = getopt_long(argc, argv, "i:",
12165 long_opts, NULL)) != -1) {
12169 archive_id = strtoul(optarg, &end, 0);
12170 if (errno != 0 || *end != '\0' ||
12171 archive_id == 0 || archive_id > UINT32_MAX) {
12173 "error: %s: bad archive ID '%s'\n",
12181 fprintf(stderr, "%s: option '%s' unrecognized\n",
12182 argv[0], argv[optind - 1]);
12187 if (archive_id == 0) {
12188 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12192 if (argc <= optind) {
12193 fprintf(stderr, "%s: must specify one or more file names\n",
12198 while (optind < argc) {
12201 path = argv[optind++];
12202 if (!realpath(path, fullpath)) {
12203 fprintf(stderr, "%s: could not find path '%s': %s\n",
12204 argv[0], path, strerror(errno));
12210 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12213 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12214 argv[0], path, archive_id, strerror(-rc2));
12222 static int lfs_pcc_attach_fid(int argc, char **argv)
12224 struct option long_opts[] = {
12225 { .val = 'i', .name = "id", .has_arg = required_argument },
12226 { .val = 'm', .name = "mnt", .has_arg = required_argument },
12227 { .name = NULL } };
12228 char short_opts[] = "i:m:";
12231 __u32 archive_id = 0;
12233 const char *mntpath = NULL;
12234 const char *fidstr;
12235 enum lu_pcc_type type = LU_PCC_READWRITE;
12238 while ((c = getopt_long(argc, argv, short_opts,
12239 long_opts, NULL)) != -1) {
12243 archive_id = strtoul(optarg, &end, 0);
12244 if (errno != 0 || *end != '\0' ||
12245 archive_id > UINT32_MAX) {
12247 "error: %s: bad archive ID '%s'\n",
12258 fprintf(stderr, "%s: option '%s' unrecognized\n",
12259 argv[0], argv[optind - 1]);
12264 if (archive_id == 0) {
12265 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12270 fprintf(stderr, "%s: must specify Lustre mount point\n",
12275 if (argc <= optind) {
12276 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12280 while (optind < argc) {
12283 fidstr = argv[optind++];
12285 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12289 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12290 argv[0], fidstr, mntpath, archive_id,
12293 if (rc == 0 && rc2 < 0)
12299 static int lfs_pcc_detach(int argc, char **argv)
12301 struct option long_opts[] = {
12302 { .val = 'k', .name = "keep", .has_arg = no_argument },
12303 { .name = NULL } };
12304 char short_opts[] = "k";
12308 char fullpath[PATH_MAX];
12309 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12312 while ((c = getopt_long(argc, argv, short_opts,
12313 long_opts, NULL)) != -1) {
12316 detach_opt = PCC_DETACH_OPT_NONE;
12321 fprintf(stderr, "%s: option '%s' unrecognized\n",
12322 argv[0], argv[optind - 1]);
12327 while (optind < argc) {
12330 path = argv[optind++];
12331 if (!realpath(path, fullpath)) {
12332 fprintf(stderr, "%s: could not find path '%s': %s\n",
12333 argv[0], path, strerror(errno));
12339 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12343 "%s: cannot detach '%s' from PCC: %s\n",
12344 argv[0], path, strerror(errno));
12352 static int lfs_pcc_detach_fid(int argc, char **argv)
12354 struct option long_opts[] = {
12355 { .val = 'k', .name = "keep", .has_arg = no_argument },
12356 { .name = NULL } };
12357 char short_opts[] = "k";
12361 const char *mntpath;
12362 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12365 while ((c = getopt_long(argc, argv, short_opts,
12366 long_opts, NULL)) != -1) {
12369 detach_opt = PCC_DETACH_OPT_NONE;
12374 fprintf(stderr, "%s: option '%s' unrecognized\n",
12375 argv[0], argv[optind - 1]);
12380 mntpath = argv[optind++];
12382 while (optind < argc) {
12385 fid = argv[optind++];
12387 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12390 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12391 argv[0], fid, mntpath, strerror(-rc2));
12399 static int lfs_pcc_state(int argc, char **argv)
12403 char fullpath[PATH_MAX];
12404 struct lu_pcc_state state;
12409 fprintf(stderr, "%s: must specify one or more file names\n",
12414 while (optind < argc) {
12417 path = argv[optind++];
12418 if (!realpath(path, fullpath)) {
12419 fprintf(stderr, "%s: could not find path '%s': %s\n",
12420 argv[0], path, strerror(errno));
12426 rc2 = llapi_pcc_state_get(fullpath, &state);
12431 "%s: cannot get PCC state of '%s': %s\n",
12432 argv[0], path, strerror(-rc2));
12436 printf("file: %s", path);
12437 printf(", type: %s", pcc_type2string(state.pccs_type));
12438 if (state.pccs_type == LU_PCC_NONE &&
12439 state.pccs_open_count == 0) {
12444 printf(", PCC file: %s", state.pccs_path);
12445 printf(", user number: %u", state.pccs_open_count);
12446 printf(", flags: %x", state.pccs_flags);
12453 * lfs_pcc_list_commands() - List lfs pcc commands.
12454 * @argc: The count of command line arguments.
12455 * @argv: Array of strings for command line arguments.
12457 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12459 * Return: 0 on success.
12461 static int lfs_pcc_list_commands(int argc, char **argv)
12463 char buffer[81] = "";
12465 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12472 * lfs_pcc() - Parse and execute lfs pcc commands.
12473 * @argc: The count of lfs pcc command line arguments.
12474 * @argv: Array of strings for lfs pcc command line arguments.
12476 * This function parses lfs pcc commands and performs the
12477 * corresponding functions specified in pcc_cmdlist[].
12479 * Return: 0 on success or an error code on failure.
12481 static int lfs_pcc(int argc, char **argv)
12483 char cmd[PATH_MAX];
12486 setlinebuf(stdout);
12488 Parser_init("lfs-pcc > ", pcc_cmdlist);
12490 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12492 program_invocation_short_name = cmd;
12494 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12496 rc = Parser_commands();
12498 return rc < 0 ? -rc : rc;
12501 static int lfs_list_commands(int argc, char **argv)
12503 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12505 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12510 int main(int argc, char **argv)
12514 /* Ensure that liblustreapi constructor has run */
12515 if (!llapi_liblustreapi_initialized())
12516 fprintf(stderr, "liblustreapi was not properly initialized\n");
12518 setlinebuf(stdout);
12521 Parser_init("lfs > ", cmdlist);
12523 progname = program_invocation_short_name; /* Used in error messages */
12525 llapi_set_command_name(argv[1]);
12526 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12527 llapi_clear_command_name();
12529 rc = Parser_commands();
12532 return rc < 0 ? -rc : rc;
12535 #ifdef _LUSTRE_IDL_H_
12536 /* Everything we need here should be included by lustreapi.h. */
12537 # error "lfs should not depend on lustre_idl.h"
12538 #endif /* _LUSTRE_IDL_H_ */