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 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
498 " [[!] --stripe-count|-c [+-]<stripes>]\n"
499 " [[!] --stripe-index|-i <index,...>]\n"
500 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
501 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
502 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
503 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
504 " [[!] --projid <projid>]\n"
505 " [[!] --layout|-L released,raid0,mdt]\n"
506 " [[!] --foreign[=<foreign_type>]]\n"
507 " [[!] --component-count [+-]<comp_cnt>]\n"
508 " [[!] --component-start [+-]N[kMGTPE]]\n"
509 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
510 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
511 " [[!] --mirror-count|-N [+-]<n>]\n"
512 " [[!] --mirror-state <[^]state>]\n"
513 " [[!] --mdt-count|-T [+-]<stripes>]\n"
514 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
515 " [[!] --mdt-index|-m <uuid|index,...>]\n"
516 "\t !: used before an option indicates 'NOT' requested attribute\n"
517 "\t -: used before a value indicates less than requested value\n"
518 "\t +: used before a value indicates more than requested value\n"
519 "\t ^: used before a flag indicates to exclude it\n"},
520 {"check", lfs_check, 0,
521 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
522 "or all the servers (MGTs, MDTs and OSTs).\n"
523 "usage: check <mgts|osts|mdts|all>"},
524 {"osts", lfs_osts, 0, "list OSTs connected to client "
525 "[for specified path only]\n" "usage: osts [path]"},
526 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
527 "[for specified path only]\n" "usage: mdts [path]"},
529 "report filesystem disk space usage or inodes usage "
530 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
531 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
532 " [--pool|-p <fsname>[.<pool>]] [path]"},
533 {"getname", lfs_getname, 0,
534 "list instances and specified mount points [for specified path only]\n"
535 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
536 #ifdef HAVE_SYS_QUOTA_H
537 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
538 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
539 " -b <block-softlimit> -B <block-hardlimit>\n"
540 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
541 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
542 " [--block-softlimit <block-softlimit>]\n"
543 " [--block-hardlimit <block-hardlimit>]\n"
544 " [--inode-softlimit <inode-softlimit>]\n"
545 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
546 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
547 " [--block-grace 'notify'|<block-grace>]\n"
548 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
549 " setquota <-U|-G|-P>\n"
550 " -b <block-softlimit> -B <block-hardlimit>\n"
551 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
552 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
553 " [--block-softlimit <block-softlimit>]\n"
554 " [--block-hardlimit <block-hardlimit>]\n"
555 " [--inode-softlimit <inode-softlimit>]\n"
556 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
557 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
559 " -b can be used instead of --block-softlimit/--block-grace\n"
560 " -B can be used instead of --block-hardlimit\n"
561 " -i can be used instead of --inode-softlimit/--inode-grace\n"
562 " -I can be used instead of --inode-hardlimit\n"
563 " -d can be used instead of --default\n\n"
564 "Note: The total quota space will be split into many qunits and\n"
565 " balanced over all server targets, the minimal qunit size is\n"
566 " 1M bytes for block space and 1K inodes for inode space.\n\n"
567 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
568 " Quota space rebalancing process will stop when this mininum\n"
569 " value is reached. As a result, quota exceeded can be returned\n"
570 " while many targets still have 1MB or 1K inodes of spare\n"
572 " When setting the grace time, 'notify' can be used as grace to\n"
573 " be notified after the quota is over soft limit but prevents\n"
574 " the soft limit from becoming the hard limit."},
575 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
576 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
578 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
579 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
580 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
581 {"project", lfs_project, 0,
582 "Change or list project attribute for specified file or directory.\n"
583 "usage: project [-d|-r] <file|directory...>\n"
584 " list project ID and flags on file(s) or directories\n"
585 " project [-p id] [-s] [-r] <file|directory...>\n"
586 " set project ID and/or inherit flag for specified file(s) or directories\n"
587 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
588 " check project ID and flags on file(s) or directories, print outliers\n"
589 " project -C [-r] [-k] <file|directory...>\n"
590 " clear the project inherit flag and ID on the file or directory\n"
593 {"flushctx", lfs_flushctx, 0,
594 "Flush security context for current user.\n"
595 "usage: flushctx [-k] [-r] [mountpoint...]"},
596 {"changelog", lfs_changelog, 0,
597 "Show the metadata changes on an MDT."
598 "\nusage: changelog <mdtname> [startrec [endrec]]"},
599 {"changelog_clear", lfs_changelog_clear, 0,
600 "Indicate that old changelog records up to <endrec> are no longer of "
601 "interest to consumer <id>, allowing the system to free up space.\n"
602 "An <endrec> of 0 means all records.\n"
603 "usage: changelog_clear <mdtname> <id> <endrec>"},
604 {"fid2path", lfs_fid2path, 0,
605 "Resolve the full path(s) for given FID(s). For a specific hardlink "
606 "specify link number <linkno>.\n"
607 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
608 "<fsname|root> <fid>..."},
609 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
610 "usage: path2fid [--parents] <path> ..."},
611 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
612 "usage: rmfid <fsname|rootpath> <fid> ..."},
613 {"data_version", lfs_data_version, 0, "Display file data version for "
614 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
615 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
616 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
617 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
618 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
619 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
620 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
622 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
623 "[--archived] [--lost] <file> ..."},
624 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
625 "given files.\n" "usage: hsm_action <file> ..."},
626 {"hsm_archive", lfs_hsm_archive, 0,
627 "Archive file to external storage.\n"
628 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
630 {"hsm_restore", lfs_hsm_restore, 0,
631 "Restore file from external storage.\n"
632 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
633 {"hsm_release", lfs_hsm_release, 0,
634 "Release files from Lustre.\n"
635 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
636 {"hsm_remove", lfs_hsm_remove, 0,
637 "Remove file copy from external storage.\n"
638 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
640 " (FILE [FILE ...] | "
641 "--mntpath MOUNTPATH FID [FID ...])\n"
643 "Note: To remove an archived copy of a file already deleted from a "
645 "--mntpath option and a list of FIDs must be specified"
647 {"hsm_cancel", lfs_hsm_cancel, 0,
648 "Cancel requests related to specified files.\n"
649 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
650 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
651 "usage: swap_layouts <path1> <path2>"},
652 {"migrate", lfs_setstripe_migrate, 0,
653 "migrate a directory between MDTs.\n"
654 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
655 " [--mdt-hash|-H] <hash_type>\n"
656 " [--mdt-index|-m] <start_mdt_index>\n"
659 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
660 " it's the MDT index of first stripe\n"
661 "\tmdt_count: number of MDTs to stripe a directory over\n"
662 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
663 " all_char (type 1)sum of characters % MDT_COUNT\n"
664 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
665 " crush (type 3)CRUSH hash algorithm\n"
667 "migrate file objects from one OST "
668 "layout\nto another (may be not safe with concurent writes).\n"
670 "[--stripe-count|-c] <stripe_count>\n"
671 "[--overstripe-count|-C] <stripe_count>\n"
672 " [--stripe-index|-i] <start_ost_index>\n"
673 " [--stripe-size|-S] <stripe_size>\n"
674 " [--pool|-p] <pool_name>\n"
675 " [--ost|-o] <ost_indices>\n"
677 " [--non-block|-n]\n"
678 " [--non-direct|-D]\n"
679 " <file|directory>\n"
680 "\tstripe_count: number of OSTs to stripe a file over\n"
681 "\t Using -C instead of -c allows overstriping, which\n"
682 "\t will place more than one stripe per OST if\n"
683 "\t stripe_count is greater than the number of OSTs\n"
684 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
685 "\tstripe_size: number of bytes to store before moving to the next OST\n"
686 "\tpool_name: name of the predefined pool of OSTs\n"
687 "\tost_indices: OSTs to stripe over, in order\n"
688 "\tblock: Block file access during data migration (default)\n"
689 "\tnon-block: Abort migrations if concurrent access is detected\n"
690 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
692 "To move directories between MDTs. This command is deprecated, "
693 "use \"migrate\" instead.\n"
694 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
696 {"ladvise", lfs_ladvise, 0,
697 "Provide servers with advice about access patterns for a file.\n"
698 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
699 " [--background|-b] [--unset|-u]\n\n"
700 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
701 " {[--mode|-m [READ,WRITE]}\n"
703 {"mirror", lfs_mirror, mirror_cmdlist,
704 "lfs commands used to manage files with mirrored components:\n"
705 "lfs mirror create - create a mirrored file or directory\n"
706 "lfs mirror extend - add mirror(s) to an existing file\n"
707 "lfs mirror split - split a mirror from an existing mirrored file\n"
708 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
709 "lfs mirror read - read a mirror content of a mirrored file\n"
710 "lfs mirror write - write to a mirror of a mirrored file\n"
711 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
712 "lfs mirror verify - verify mirrored file(s)\n"},
713 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
714 "usage: getsom [-s] [-b] [-f] <path>\n"
715 "\t-s: Only show the size value of the SOM data for a given file\n"
716 "\t-b: Only show the blocks value of the SOM data for a given file\n"
717 "\t-f: Only show the flags value of the SOM data for a given file\n"},
718 {"heat_get", lfs_heat_get, 0,
719 "To get heat of files.\n"
720 "usage: heat_get <file> ...\n"},
721 {"heat_set", lfs_heat_set, 0,
722 "To set heat flags of files.\n"
723 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
724 "\t--clear|-c: Clear file heat for given files\n"
725 "\t--off|-o: Turn off file heat for given files\n"
726 "\t--on|-O: Turn on file heat for given files\n"},
727 {"pcc", lfs_pcc, pcc_cmdlist,
728 "lfs commands used to interact with PCC features:\n"
729 "lfs pcc attach - attach given files to Persistent Client Cache\n"
730 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
731 "lfs pcc state - display the PCC state for given files\n"
732 "lfs pcc detach - detach given files from Persistent Client Cache\n"
733 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
734 {"help", Parser_help, 0, "help"},
735 {"exit", Parser_quit, 0, "quit"},
736 {"quit", Parser_quit, 0, "quit"},
737 {"--version", Parser_version, 0,
738 "output build version of the utility and exit"},
739 {"--list-commands", lfs_list_commands, 0,
740 "list commands supported by the utility and exit"},
744 static int check_hashtype(const char *hashtype)
746 int type_num = atoi(hashtype);
749 /* numeric hash type */
750 if (hashtype && strlen(hashtype) == 1 &&
751 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
753 /* string hash type */
754 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
755 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
761 static uint32_t check_foreign_type_name(const char *foreign_type_name)
765 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
766 if (!lu_foreign_types[i].lft_name)
768 if (strcmp(foreign_type_name,
769 lu_foreign_types[i].lft_name) == 0)
770 return lu_foreign_types[i].lft_type;
773 return LU_FOREIGN_TYPE_UNKNOWN;
776 static const char *error_loc = "syserror";
779 MIGRATION_NONBLOCK = 0x0001,
780 MIGRATION_MIRROR = 0x0002,
781 MIGRATION_NONDIRECT = 0x0004,
782 MIGRATION_VERBOSE = 0x0008,
786 migrate_open_files(const char *name, __u64 migration_flags,
787 const struct llapi_stripe_param *param,
788 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
795 char parent[PATH_MAX];
796 char volatile_file[PATH_MAX];
802 if (!param && !layout) {
803 error_loc = "layout information";
807 /* search for file directory pathname */
808 if (strlen(name) > sizeof(parent) - 1) {
809 error_loc = "source file name";
813 strncpy(parent, name, sizeof(parent));
814 ptr = strrchr(parent, '/');
816 if (!getcwd(parent, sizeof(parent))) {
817 error_loc = "getcwd";
821 if (ptr == parent) /* leading '/' */
826 /* open file, direct io */
827 /* even if the file is only read, WR mode is nedeed to allow
831 if (!(migration_flags & MIGRATION_NONDIRECT))
833 fd = open(name, rflags);
836 error_loc = "cannot open source file";
840 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
842 error_loc = "cannot get MDT index";
847 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
848 mode_t open_mode = S_IRUSR | S_IWUSR;
850 random_value = random();
851 rc = snprintf(volatile_file, sizeof(volatile_file),
852 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
853 mdt_index, random_value);
854 if (rc >= sizeof(volatile_file)) {
859 /* create, open a volatile file, use caching (ie no directio) */
861 /* Returns -1 and sets errno on error: */
862 fdv = llapi_layout_file_open(volatile_file, open_flags,
867 /* Does the right thing on error: */
868 fdv = llapi_file_open_param(volatile_file, open_flags,
871 } while (fdv < 0 && (rc = fdv) == -EEXIST);
874 error_loc = "cannot create volatile file";
879 * In case the MDT does not support creation of volatile files
880 * we should try to unlink it.
882 (void)unlink(volatile_file);
885 * Not-owner (root?) special case.
886 * Need to set owner/group of volatile file like original.
887 * This will allow to pass related check during layout_swap.
892 error_loc = "cannot stat source file";
896 rc = fstat(fdv, &stv);
899 error_loc = "cannot stat volatile";
903 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
904 rc = fchown(fdv, st.st_uid, st.st_gid);
907 error_loc = "cannot change ownwership of volatile";
926 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
928 struct llapi_layout *layout;
929 size_t buf_size = 4 * 1024 * 1024;
933 size_t page_size = sysconf(_SC_PAGESIZE);
937 layout = llapi_layout_get_by_fd(fd_src, 0);
939 uint64_t stripe_size;
941 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
943 buf_size = stripe_size;
945 llapi_layout_free(layout);
948 /* Use a page-aligned buffer for direct I/O */
949 rc = posix_memalign(&buf, page_size, buf_size);
953 sparse = llapi_file_is_sparse(fd_src);
955 rc = ftruncate(fd_dst, pos);
964 size_t to_read, to_write;
967 if (sparse && pos >= data_end) {
970 data_off = llapi_data_seek(fd_src, pos, &data_size);
972 /* Non-fatal, switch to full copy */
976 /* hole at the end of file, truncate up to it */
978 rc = ftruncate(fd_dst, data_off);
982 pos = data_off & ~(page_size - 1);
983 data_end = data_off + data_size;
984 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
985 to_read = MIN(to_read, buf_size);
991 rc = check_file(fd_src);
996 rsize = pread(fd_src, buf, to_read, pos);
1006 while (to_write > 0) {
1009 written = pwrite(fd_dst, buf, to_write, pos);
1015 to_write -= written;
1017 if (rc || rsize < to_read)
1025 /* Try to avoid page cache pollution after migration. */
1026 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
1027 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1033 static int migrate_copy_timestamps(int fd, int fdv)
1037 if (fstat(fd, &st) == 0) {
1038 struct timeval tv[2] = {
1039 {.tv_sec = st.st_atime},
1040 {.tv_sec = st.st_mtime}
1043 return futimes(fdv, tv);
1049 static int migrate_block(int fd, int fdv)
1056 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1058 error_loc = "cannot get dataversion";
1067 * The grouplock blocks all concurrent accesses to the file.
1068 * It has to be taken after llapi_get_data_version as it would
1071 rc = llapi_group_lock(fd, gid);
1073 error_loc = "cannot get group lock";
1077 rc = migrate_copy_data(fd, fdv, NULL);
1079 error_loc = "data copy failed";
1083 /* Make sure we keep original atime/mtime values */
1084 rc = migrate_copy_timestamps(fd, fdv);
1086 error_loc = "timestamp copy failed";
1092 * for a migration we need to check data version on file did
1095 * Pass in gid=0 since we already own grouplock.
1097 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1098 SWAP_LAYOUTS_CHECK_DV1);
1099 if (rc == -EAGAIN) {
1100 error_loc = "file changed";
1102 } else if (rc < 0) {
1103 error_loc = "cannot swap layout";
1108 rc2 = llapi_group_unlock(fd, gid);
1109 if (rc2 < 0 && rc == 0) {
1110 error_loc = "unlock group lock";
1118 * Internal helper for migrate_copy_data(). Check lease and report error if
1121 * \param[in] fd File descriptor on which to check the lease.
1123 * \retval 0 Migration can keep on going.
1124 * \retval -errno Error occurred, abort migration.
1126 static int check_lease(int fd)
1130 rc = llapi_lease_check(fd);
1132 return 0; /* llapi_check_lease returns > 0 on success. */
1137 static int migrate_nonblock(int fd, int fdv)
1143 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1145 error_loc = "cannot get data version";
1149 rc = migrate_copy_data(fd, fdv, check_lease);
1151 error_loc = "data copy failed";
1155 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1157 error_loc = "cannot get data version";
1163 error_loc = "source file changed";
1167 /* Make sure we keep original atime/mtime values */
1168 rc = migrate_copy_timestamps(fd, fdv);
1170 error_loc = "timestamp copy failed";
1178 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1180 struct pool_to_id_cbdata data = { .pool = pool };
1181 struct llapi_layout *layout = NULL;
1184 layout = llapi_layout_get_by_path(fname, 0);
1187 "error %s: file '%s' couldn't get layout: rc=%d\n",
1188 progname, fname, errno);
1192 rc = llapi_layout_sanity(layout, false, true);
1194 llapi_layout_sanity_perror(errno);
1197 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1206 llapi_layout_free(layout);
1210 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1211 __u32 flags, __u32 neg_flags)
1214 __u32 flags_array[2];
1221 "error %s: neither component id nor pool is specified\n",
1225 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1231 ids[count] = comp_id;
1232 flags_array[count] = flags;
1237 if (neg_flags & LCME_FL_STALE) {
1239 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1244 ids[count] = comp_id;
1245 flags_array[count] = neg_flags | LCME_FL_NEG;
1249 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1251 if (errno == EUCLEAN) {
1254 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1255 progname, comp_id, fname);
1258 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1259 progname, comp_id, fname, flags, neg_flags);
1266 static int lfs_component_del(char *fname, __u32 comp_id,
1267 __u32 flags, __u32 neg_flags)
1271 if (flags && neg_flags)
1274 if (!flags && neg_flags)
1275 flags = neg_flags | LCME_FL_NEG;
1277 if ((flags && comp_id) || (!flags && !comp_id))
1281 if (flags & ~LCME_KNOWN_FLAGS) {
1283 "%s setstripe: unknown flags %#x\n",
1287 } else if (comp_id > LCME_ID_MAX) {
1288 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1293 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1296 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1297 progname, comp_id, fname, strerror(errno));
1301 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1308 rc = llapi_layout_file_comp_add(fname, layout);
1310 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1311 fname, strerror(errno));
1315 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1316 struct llapi_layout *layout)
1324 fd = lstat(fname, &st);
1325 if (fd == 0 && S_ISDIR(st.st_mode))
1326 open_flags = O_DIRECTORY | O_RDONLY;
1328 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1330 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1331 S_ISDIR(st.st_mode) ?
1332 "set default composite layout for" :
1333 "create composite file",
1334 fname, strerror(errno));
1338 static int lfs_migrate(char *name, __u64 migration_flags,
1339 struct llapi_stripe_param *param,
1340 struct llapi_layout *layout)
1342 struct llapi_layout *existing;
1343 uint64_t dom_new, dom_cur;
1348 rc = migrate_open_files(name, migration_flags, param, layout,
1353 rc = llapi_layout_dom_size(layout, &dom_new);
1355 error_loc = "cannot get new layout DoM size";
1358 /* special case for migration to DOM layout*/
1359 existing = llapi_layout_get_by_fd(fd, 0);
1361 error_loc = "cannot get existing layout";
1365 rc = llapi_layout_dom_size(existing, &dom_cur);
1367 error_loc = "cannot get current layout DoM size";
1372 * if file has DoM layout already then migration is possible to
1373 * the new layout with the same DoM component via swap layout,
1374 * if new layout used bigger DOM size, then mirroring is used
1376 if (dom_new > dom_cur) {
1377 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1380 error_loc = "cannot migrate to DOM layout";
1384 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1386 * Blocking mode (forced if servers do not support file lease).
1387 * It is also the default mode, since we cannot distinguish
1388 * between a broken lease and a server that does not support
1389 * atomic swap/close (LU-6785)
1391 rc = migrate_block(fd, fdv);
1395 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1397 error_loc = "cannot get lease";
1401 rc = migrate_nonblock(fd, fdv);
1403 llapi_lease_release(fd);
1408 * Atomically put lease, swap layouts and close.
1409 * for a migration we need to check data version on file did
1412 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1414 error_loc = "cannot swap layout";
1426 fprintf(stderr, "error: %s: %s: %s: %s\n",
1427 progname, name, error_loc, strerror(-rc));
1428 else if (migration_flags & MIGRATION_VERBOSE)
1429 printf("%s\n", name);
1434 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1437 char *dup_string = NULL;
1443 if (!string || !string[0])
1446 dup_string = strdup(string);
1448 llapi_printf(LLAPI_MSG_ERROR,
1449 "%s: insufficient memory\n",
1454 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1458 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1459 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1460 const char *comp_name = comp_flags_table[i].cfn_name;
1462 if (strcmp(name, comp_name) == 0) {
1463 *flags |= comp_flag;
1465 } else if (strncmp(name, "^", 1) == 0 &&
1466 strcmp(name + 1, comp_name) == 0) {
1467 *neg_flags |= comp_flag;
1472 llapi_printf(LLAPI_MSG_ERROR,
1473 "%s: component flag '%s' not supported\n",
1480 if (!*flags && !*neg_flags)
1483 /* don't allow to set and exclude the same flag */
1484 if (*flags & *neg_flags)
1492 static int mdthash_input(char *string, __u32 *inflags,
1493 __u32 *exflags, __u32 *type)
1500 {"migrating", LMV_HASH_FLAG_MIGRATION},
1501 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1502 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1511 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1515 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1516 if (strcmp(name, mhflist[i].name) == 0 ||
1517 name[0] == mhflist[i].name[0]) {
1518 *inflags |= mhflist[i].flag;
1520 } else if (name[0] == '^' &&
1521 (strcmp(name + 1, mhflist[i].name) == 0 ||
1522 name[1] == mhflist[i].name[0])) {
1523 *exflags |= mhflist[i].flag;
1528 i = check_hashtype(name);
1533 llapi_printf(LLAPI_MSG_ERROR,
1534 "%s: invalid mdt_hash value '%s'\n",
1540 /* don't allow to include and exclude the same flag */
1541 if (*inflags & *exflags) {
1542 llapi_printf(LLAPI_MSG_ERROR,
1543 "%s: include and exclude same flag '%s'\n",
1551 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1559 if (strncmp(string, "^", 1) == 0) {
1560 *neg_state = llapi_layout_string_flags(string + 1);
1561 if (*neg_state != 0)
1564 *state = llapi_layout_string_flags(string);
1569 llapi_printf(LLAPI_MSG_ERROR,
1570 "%s: mirrored file state '%s' not supported\n",
1576 * struct mirror_args - Command-line arguments for mirror(s).
1577 * @m_count: Number of mirrors to be created with this layout.
1578 * @m_flags: Mirror level flags, only 'prefer' is supported.
1579 * @m_layout: Mirror layout.
1580 * @m_file: A victim file. Its layout will be split and used as a mirror.
1581 * @m_next: Point to the next node of the list.
1583 * Command-line arguments for mirror(s) will be parsed and stored in
1584 * a linked list that consists of this structure.
1586 struct mirror_args {
1589 struct llapi_layout *m_layout;
1591 struct mirror_args *m_next;
1596 * enum mirror_flags - Flags for extending a mirrored file.
1597 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1598 * in case the victim file(s) contains the same data as the
1599 * original mirrored file.
1600 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1601 * @MF_COMP_ID: specified component id instead of mirror id
1603 * Flags for extending a mirrored file.
1613 * mirror_create_sanity_check() - Check mirror list.
1614 * @list: A linked list that stores the mirror arguments.
1616 * This function does a sanity check on @list for creating
1619 * Return: 0 on success or a negative error code on failure.
1621 static int mirror_create_sanity_check(const char *fname,
1622 struct mirror_args *list)
1625 bool has_m_file = false;
1626 bool has_m_layout = false;
1632 struct llapi_layout *layout;
1634 layout = llapi_layout_get_by_path(fname, 0);
1637 "error: %s: file '%s' couldn't get layout\n",
1642 rc = llapi_layout_sanity(layout, false, true);
1644 llapi_layout_free(layout);
1647 llapi_layout_sanity_perror(rc);
1655 llapi_layout_free(list->m_layout);
1658 llapi_layout_get_by_path(list->m_file, 0);
1659 if (!list->m_layout) {
1661 "error: %s: file '%s' has no layout\n",
1662 progname, list->m_file);
1666 has_m_layout = true;
1667 if (!list->m_layout) {
1668 fprintf(stderr, "error: %s: no mirror layout\n",
1674 rc = llapi_layout_sanity(list->m_layout, false, true);
1676 llapi_layout_sanity_perror(rc);
1680 list = list->m_next;
1683 if (has_m_file && has_m_layout) {
1685 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1693 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1695 __u32 mirror_flags = *(__u32 *)cbdata;
1699 rc = llapi_layout_comp_flags_get(layout, &flags);
1704 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1709 return LLAPI_LAYOUT_ITER_CONT;
1713 * mirror_create() - Create a mirrored file.
1714 * @fname: The file to be created.
1715 * @mirror_list: A linked list that stores the mirror arguments.
1717 * This function creates a mirrored file @fname with the mirror(s)
1718 * from @mirror_list.
1720 * Return: 0 on success or a negative error code on failure.
1722 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1724 struct llapi_layout *layout = NULL;
1725 struct mirror_args *cur_mirror = NULL;
1726 uint16_t mirror_count = 0;
1730 rc = mirror_create_sanity_check(NULL, mirror_list);
1734 cur_mirror = mirror_list;
1735 while (cur_mirror) {
1736 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1738 &cur_mirror->m_flags);
1741 fprintf(stderr, "%s: failed to set mirror flags\n",
1746 for (i = 0; i < cur_mirror->m_count; i++) {
1747 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1751 "error: %s: merge layout failed: %s\n",
1752 progname, strerror(errno));
1756 mirror_count += cur_mirror->m_count;
1757 cur_mirror = cur_mirror->m_next;
1761 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1765 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1768 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1769 progname, strerror(errno));
1773 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1781 llapi_layout_free(layout);
1786 * Compare files and check lease on @fd.
1788 * \retval bytes number of bytes are the same
1790 static ssize_t mirror_file_compare(int fd, int fdv)
1792 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1794 ssize_t bytes_done = 0;
1795 ssize_t bytes_read = 0;
1797 buf = malloc(buflen * 2);
1802 if (!llapi_lease_check(fd)) {
1803 bytes_done = -EBUSY;
1807 bytes_read = read(fd, buf, buflen);
1808 if (bytes_read <= 0)
1811 if (bytes_read != read(fdv, buf + buflen, buflen))
1815 * XXX: should compute the checksum on each buffer and then
1816 * compare checksum to avoid cache collision
1818 if (memcmp(buf, buf + buflen, bytes_read))
1821 bytes_done += bytes_read;
1829 static int mirror_extend_file(const char *fname, const char *victim_file,
1830 enum mirror_flags mirror_flags)
1835 struct stat stbuf_v;
1836 struct ll_ioc_lease *data = NULL;
1839 fd = open(fname, O_RDWR);
1841 error_loc = "open source file";
1846 fdv = open(victim_file, O_RDWR);
1848 error_loc = "open target file";
1853 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1854 error_loc = "stat source or target file";
1859 if (stbuf.st_dev != stbuf_v.st_dev) {
1860 error_loc = "stat source and target file";
1865 /* mirrors should be of the same size */
1866 if (stbuf.st_size != stbuf_v.st_size) {
1867 error_loc = "file sizes don't match";
1872 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1874 error_loc = "cannot get lease";
1878 if (!(mirror_flags & MF_NO_VERIFY)) {
1880 /* mirrors should have the same contents */
1881 ret = mirror_file_compare(fd, fdv);
1882 if (ret != stbuf.st_size) {
1883 error_loc = "file busy or contents don't match";
1884 rc = ret < 0 ? ret : -EINVAL;
1889 /* Get rid of caching pages from clients */
1890 rc = llapi_file_flush(fd);
1892 error_loc = "cannot get data version";
1896 rc = llapi_file_flush(fdv);
1898 error_loc = "cannot get data version";
1902 /* Make sure we keep original atime/mtime values */
1903 rc = migrate_copy_timestamps(fd, fdv);
1905 error_loc = "cannot copy timestamp";
1909 /* Atomically put lease, merge layouts and close. */
1910 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1912 error_loc = "memory allocation";
1915 data->lil_mode = LL_LEASE_UNLCK;
1916 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1917 data->lil_count = 1;
1918 data->lil_ids[0] = fdv;
1919 rc = llapi_lease_set(fd, data);
1921 error_loc = "cannot merge layout";
1923 } else if (rc == 0) {
1925 error_loc = "lost lease lock";
1938 (void) unlink(victim_file);
1940 fprintf(stderr, "error: %s: %s: %s: %s\n",
1941 progname, fname, error_loc, strerror(-rc));
1945 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1946 bool inherit, uint32_t flags)
1948 struct llapi_layout *f_layout = NULL;
1949 struct ll_ioc_lease *data = NULL;
1955 f_layout = llapi_layout_get_by_path(name, 0);
1957 fprintf(stderr, "%s: cannot get layout\n", progname);
1960 rc = llapi_layout_get_last_init_comp(f_layout);
1962 fprintf(stderr, "%s: cannot get the last init comp\n",
1966 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1969 "%s: cannot inherit from the last init comp\n",
1974 llapi_layout_comp_flags_set(m_layout, flags);
1975 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1980 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1982 error_loc = "cannot get lease";
1986 rc = migrate_nonblock(fd, fdv);
1988 llapi_lease_release(fd);
1992 /* Atomically put lease, merge layouts and close. */
1993 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1995 error_loc = "memory allocation";
1998 data->lil_mode = LL_LEASE_UNLCK;
1999 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2000 data->lil_count = 1;
2001 data->lil_ids[0] = fdv;
2002 rc = llapi_lease_set(fd, data);
2004 error_loc = "cannot merge layout";
2006 } else if (rc == 0) {
2008 error_loc = "lost lease lock";
2021 fprintf(stderr, "error: %s: %s: %s: %s\n",
2022 progname, name, error_loc, strerror(-rc));
2026 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2027 enum mirror_flags mirror_flags)
2031 rc = mirror_create_sanity_check(fname, mirror_list);
2035 while (mirror_list) {
2036 if (mirror_list->m_file) {
2037 rc = mirror_extend_file(fname, mirror_list->m_file,
2040 __u32 mirror_count = mirror_list->m_count;
2042 while (mirror_count > 0) {
2043 rc = mirror_extend_layout(fname,
2044 mirror_list->m_layout,
2045 mirror_list->m_inherit,
2046 mirror_list->m_flags);
2056 mirror_list = mirror_list->m_next;
2062 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2067 rc = llapi_layout_mirror_id_get(layout, &id);
2071 if ((__u16)id == *(__u16 *)cbdata)
2072 return LLAPI_LAYOUT_ITER_STOP;
2074 return LLAPI_LAYOUT_ITER_CONT;
2077 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2082 rc = llapi_layout_comp_id_get(layout, &id);
2086 if (id == *(__u32 *)cbdata)
2087 return LLAPI_LAYOUT_ITER_STOP;
2089 return LLAPI_LAYOUT_ITER_CONT;
2092 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2094 char buf[LOV_MAXPOOLNAME + 1];
2095 struct pool_to_id_cbdata *d = (void *)cbdata;
2099 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2102 if (strcmp(d->pool, buf))
2103 return LLAPI_LAYOUT_ITER_CONT;
2105 rc = llapi_layout_mirror_id_get(layout, &id);
2110 return LLAPI_LAYOUT_ITER_STOP;
2113 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2115 char buf[LOV_MAXPOOLNAME + 1];
2116 struct pool_to_id_cbdata *d = (void *)cbdata;
2120 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2123 if (strcmp(d->pool, buf))
2124 return LLAPI_LAYOUT_ITER_CONT;
2126 rc = llapi_layout_comp_id_get(layout, &id);
2131 return LLAPI_LAYOUT_ITER_STOP;
2134 struct collect_ids_data {
2140 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2142 struct collect_ids_data *cid = cbdata;
2146 rc = llapi_layout_mirror_id_get(layout, &id);
2150 if ((__u16)id != cid->cid_exclude) {
2153 for (i = 0; i < cid->cid_count; i++) {
2154 /* already collected the mirror id */
2155 if (id == cid->cid_ids[i])
2156 return LLAPI_LAYOUT_ITER_CONT;
2158 cid->cid_ids[cid->cid_count] = id;
2162 return LLAPI_LAYOUT_ITER_CONT;
2166 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2167 * @mirror_id: Mirror id to be checked.
2168 * @layout: Mirror component list.
2170 * This function checks if a mirror with specified @mirror_id is the last
2171 * non-stale mirror of a layout @layout.
2173 * Return: true or false.
2176 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2178 __u16 mirror_ids[128] = { 0 };
2179 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2181 .cid_exclude = mirror_id, };
2184 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2186 for (i = 0; i < cid.cid_count; i++) {
2187 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2190 comp_size = llapi_mirror_find_stale(layout, comp_array,
2191 ARRAY_SIZE(comp_array),
2200 static int mirror_split(const char *fname, __u32 id, const char *pool,
2201 enum mirror_flags mflags, const char *victim_file)
2203 struct llapi_layout *layout;
2204 char parent[PATH_MAX];
2205 char victim[PATH_MAX];
2206 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2208 struct ll_ioc_lease *data;
2209 uint16_t mirror_count;
2213 bool purge = true; /* delete mirror by setting fdv=fd */
2216 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2218 "error %s: the source file '%s' and -f file are the same\n",
2223 /* check fname contains mirror with mirror_id/comp_id */
2224 layout = llapi_layout_get_by_path(fname, 0);
2227 "error %s: file '%s' couldn't get layout\n",
2232 rc = llapi_layout_sanity(layout, false, true);
2234 llapi_layout_sanity_perror(rc);
2238 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2241 "error %s: file '%s' couldn't get mirror count\n",
2245 if (mirror_count < 2) {
2247 "error %s: file '%s' has %d component, cannot split\n",
2248 progname, fname, mirror_count);
2252 if (mflags & MF_COMP_POOL) {
2253 struct pool_to_id_cbdata data = { .pool = pool };
2255 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2257 mirror_id = data.id;
2258 } else if (mflags & MF_COMP_ID) {
2259 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2260 mirror_id = mirror_id_of(id);
2262 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2266 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2269 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2270 if (mflags & MF_COMP_POOL) {
2272 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2273 progname, fname, pool);
2275 } else if (mflags & MF_COMP_ID) {
2277 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2278 progname, fname, id);
2282 "error %s: file '%s' does not contain mirror with id %u\n",
2283 progname, fname, id);
2288 fd = open(fname, O_RDWR);
2291 "error %s: open file '%s' failed: %s\n",
2292 progname, fname, strerror(errno));
2296 /* get victim file directory pathname */
2297 if (strlen(fname) > sizeof(parent) - 1) {
2298 fprintf(stderr, "error %s: file name of '%s' too long\n",
2303 strncpy(parent, fname, sizeof(parent));
2304 ptr = strrchr(parent, '/');
2306 if (!getcwd(parent, sizeof(parent))) {
2307 fprintf(stderr, "error %s: getcwd failed: %s\n",
2308 progname, strerror(errno));
2318 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2320 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2327 /* use a temp file to store the splitted layout */
2328 if (mflags & MF_DESTROY) {
2329 if (last_non_stale_mirror(mirror_id, layout)) {
2332 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2338 /* don't use volatile file for mirror destroy */
2342 * try the old way to delete mirror using
2345 fdv = llapi_create_volatile_idx(parent,
2346 mdt_index, O_LOV_DELAY_CREATE);
2349 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2351 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2354 /* user specified victim file */
2355 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2360 "error %s: create victim file failed: %s\n",
2361 progname, strerror(errno));
2365 /* get lease lock of fname */
2366 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2369 "error %s: cannot get lease of file '%s': %d\n",
2370 progname, fname, rc);
2374 /* Atomatically put lease, split layouts and close. */
2375 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2381 data->lil_mode = LL_LEASE_UNLCK;
2382 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2383 data->lil_count = 2;
2384 data->lil_ids[0] = fdv;
2385 data->lil_ids[1] = mirror_id;
2386 rc = llapi_lease_set(fd, data);
2388 if (rc == -EINVAL && purge) {
2389 /* could be old MDS which prohibit fd==fdv */
2394 if (rc == 0) /* lost lease lock */
2397 "error %s: cannot split '%s': %s\n",
2398 progname, fname, strerror(-rc));
2410 llapi_layout_free(layout);
2415 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2416 __u16 *mirror_ids, int ids_nr);
2418 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2419 __u64 migration_flags,
2420 struct llapi_stripe_param *param,
2421 struct llapi_layout *layout)
2423 struct ll_ioc_lease *data = NULL;
2426 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2428 error_loc = "cannot get lease";
2432 /* Atomically put lease, merge layouts, resync and close. */
2433 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2435 error_loc = "memory allocation";
2438 data->lil_mode = LL_LEASE_UNLCK;
2439 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2440 data->lil_count = 1;
2441 data->lil_ids[0] = fdv;
2442 rc = llapi_lease_set(fd, data);
2444 error_loc = "cannot merge layout";
2446 } else if (rc == 0) {
2448 error_loc = "lost lease lock";
2454 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2456 error_loc = "cannot resync file";
2460 /* delete first mirror now */
2461 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2463 error_loc = "cannot delete old layout";
2471 fprintf(stderr, "error: %s: %s: %s: %s\n",
2472 progname, name, error_loc, strerror(-rc));
2473 else if (migration_flags & MIGRATION_VERBOSE)
2474 printf("%s\n", name);
2481 * Parse a string containing an target index list into an array of integers.
2483 * The input string contains a comma delimited list of individual
2484 * indices and ranges, for example "1,2-4,7". Add the indices into the
2485 * \a tgts array and remove duplicates.
2487 * \param[out] tgts array to store indices in
2488 * \param[in] size size of \a tgts array
2489 * \param[in] offset starting index in \a tgts
2490 * \param[in] arg string containing OST index list
2491 * \param[in/out] overstriping index list may contain duplicates
2493 * \retval positive number of indices in \a tgts
2494 * \retval -EINVAL unable to parse \a arg
2496 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2497 unsigned long long *pattern)
2501 int slots = size - offset;
2503 bool overstriped = false;
2509 end_of_loop = false;
2510 while (!end_of_loop) {
2511 int start_index = 0;
2514 char *endptr = NULL;
2518 ptr = strchrnul(arg, ',');
2520 end_of_loop = *ptr == '\0';
2524 start_index = strtol(arg, &endptr, 0);
2525 if (endptr == arg) /* no data at all */
2527 if (errno != 0 || start_index < -1 ||
2528 (*endptr != '-' && *endptr != '\0'))
2531 end_index = start_index;
2532 if (*endptr == '-') {
2534 end_index = strtol(endptr + 1, &endptr, 0);
2535 if (errno != 0 || *endptr != '\0' || end_index < -1)
2537 if (end_index < start_index)
2541 for (i = start_index; i <= end_index && slots > 0; i++) {
2544 /* remove duplicate */
2545 for (j = 0; j < offset; j++) {
2546 if (tgts[j] == i && pattern &&
2547 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2549 else if (tgts[j] == i)
2555 if (j == offset) { /* check complete */
2561 if (slots == 0 && i < end_index)
2569 if (!end_of_loop && ptr)
2572 if (!overstriped && pattern)
2573 *pattern = LLAPI_LAYOUT_DEFAULT;
2575 return rc < 0 ? rc : nr;
2578 struct lfs_setstripe_args {
2579 unsigned long long lsa_comp_end;
2580 unsigned long long lsa_stripe_size;
2581 unsigned long long lsa_extension_size;
2582 long long lsa_stripe_count;
2583 long long lsa_stripe_off;
2584 __u32 lsa_comp_flags;
2585 __u32 lsa_comp_neg_flags;
2586 unsigned long long lsa_pattern;
2587 unsigned int lsa_mirror_count;
2589 bool lsa_first_comp;
2590 bool lsa_extension_comp;
2592 char *lsa_pool_name;
2595 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2597 unsigned int mirror_count = lsa->lsa_mirror_count;
2598 bool first_comp = lsa->lsa_first_comp;
2600 memset(lsa, 0, sizeof(*lsa));
2602 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2603 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2604 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2605 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2606 lsa->lsa_pool_name = NULL;
2608 lsa->lsa_mirror_count = mirror_count;
2609 lsa->lsa_first_comp = first_comp;
2613 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2614 * @lsa: Stripe options to be initialized and inherited.
2616 * This function initializes stripe options in @lsa and inherit
2617 * stripe_size, stripe_count and OST pool_name options.
2621 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2623 unsigned long long stripe_size;
2624 long long stripe_count;
2625 char *pool_name = NULL;
2627 stripe_size = lsa->lsa_stripe_size;
2628 stripe_count = lsa->lsa_stripe_count;
2629 pool_name = lsa->lsa_pool_name;
2631 setstripe_args_init(lsa);
2633 lsa->lsa_stripe_size = stripe_size;
2634 lsa->lsa_stripe_count = stripe_count;
2635 lsa->lsa_pool_name = pool_name;
2638 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2640 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2641 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2642 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2643 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2644 lsa->lsa_comp_end != 0);
2648 * comp_args_to_layout() - Create or extend a composite layout.
2649 * @composite: Pointer to the composite layout.
2650 * @lsa: Stripe options for the new component.
2652 * This function creates or extends a composite layout by adding a new
2653 * component with stripe options from @lsa.
2655 * Return: 0 on success or an error code on failure.
2657 static int comp_args_to_layout(struct llapi_layout **composite,
2658 struct lfs_setstripe_args *lsa,
2661 struct llapi_layout *layout = *composite;
2662 uint64_t prev_end = 0;
2668 layout = llapi_layout_alloc();
2670 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2675 *composite = layout;
2676 lsa->lsa_first_comp = true;
2681 * Get current component extent, current component
2682 * must be the tail component.
2684 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2686 fprintf(stderr, "Get comp extent failed. %s\n",
2691 if (lsa->lsa_first_comp) {
2693 rc = llapi_layout_add_first_comp(layout);
2695 rc = llapi_layout_comp_add(layout);
2698 fprintf(stderr, "Add component failed. %s\n",
2704 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2706 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2707 lsa->lsa_comp_flags, strerror(errno));
2712 uint64_t comp_end = lsa->lsa_comp_end;
2715 * The extendable component is 0-length, so it can be removed
2716 * if there is insufficient space to extend it.
2718 if (lsa->lsa_extension_comp)
2719 comp_end = prev_end;
2721 rc = llapi_layout_comp_extent_set(layout, prev_end,
2724 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2725 prev_end, comp_end, strerror(errno));
2729 /* reset lsa_first_comp */
2730 lsa->lsa_first_comp = false;
2732 /* Data-on-MDT component setting */
2733 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2735 * In case of Data-on-MDT patterns the only extra option
2736 * applicable is stripe size option.
2738 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2740 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2741 lsa->lsa_stripe_count);
2745 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2747 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2748 lsa->lsa_stripe_size);
2752 if (lsa->lsa_nr_tgts != 0) {
2754 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2759 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2761 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2762 lsa->lsa_stripe_off);
2766 if (lsa->lsa_pool_name != 0) {
2768 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2769 lsa->lsa_pool_name);
2774 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2776 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2781 /* Data-on-MDT component has always single stripe up to end */
2782 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2783 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2784 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2786 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2793 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2794 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2796 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2797 rc = llapi_layout_extension_size_set(layout, size);
2799 rc = llapi_layout_stripe_size_set(layout, size);
2802 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2803 size, strerror(errno));
2807 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2809 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2810 lsa->lsa_stripe_count, strerror(errno));
2814 if (lsa->lsa_pool_name) {
2815 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2817 fprintf(stderr, "Set pool name: %s failed. %s\n",
2818 lsa->lsa_pool_name, strerror(errno));
2822 rc = llapi_layout_pool_name_set(layout, "");
2824 fprintf(stderr, "Clear pool name failed: %s\n",
2830 if (lsa->lsa_nr_tgts > 0) {
2831 if (lsa->lsa_stripe_count > 0 &&
2832 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2833 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2834 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2835 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2836 lsa->lsa_stripe_count,
2841 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2842 rc = llapi_layout_ost_index_set(layout, i,
2847 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2848 lsa->lsa_stripe_off != -1) {
2849 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2852 fprintf(stderr, "Set ost index %d failed. %s\n",
2853 i, strerror(errno));
2857 /* Create the second, virtual component of extension space */
2858 if (lsa->lsa_extension_comp) {
2859 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2860 lsa->lsa_extension_comp = false;
2867 static int build_component(struct llapi_layout **layout,
2868 struct lfs_setstripe_args *lsa, bool set_extent)
2872 rc = comp_args_to_layout(layout, lsa, set_extent);
2876 if (lsa->lsa_mirror_count > 0) {
2877 rc = llapi_layout_mirror_count_set(*layout,
2878 lsa->lsa_mirror_count);
2882 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2885 lsa->lsa_mirror_count = 0;
2891 static int build_prev_component(struct llapi_layout **layout,
2892 struct lfs_setstripe_args *prev,
2893 struct lfs_setstripe_args *lsa,
2896 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2899 if (prev->lsa_stripe_size) {
2901 prev->lsa_comp_end = lsa->lsa_comp_end;
2902 prev->lsa_extension_size = lsa->lsa_extension_size;
2903 prev->lsa_extension_comp = true;
2906 rc = build_component(layout, prev, true);
2912 * Copy lsa to previous lsa;
2913 * if this is an extension component, make the previous invalid;
2916 prev->lsa_stripe_size = 0;
2923 static int build_layout_from_yaml_node(struct cYAML *node,
2924 struct llapi_layout **layout,
2925 struct lfs_setstripe_args *lsa,
2926 struct lfs_setstripe_args *prevp)
2928 struct lfs_setstripe_args prev = { 0 };
2929 __u32 *osts = lsa->lsa_tgts;
2937 string = node->cy_string;
2939 if (node->cy_type == CYAML_TYPE_OBJECT) {
2940 /* go deep to sub blocks */
2941 if (string && !strncmp(string, "component", 9) &&
2942 strncmp(string, "component0", 10) &&
2943 strncmp(string, "components", 10)) {
2944 rc = build_prev_component(layout, prevp, lsa,
2949 /* initialize lsa. */
2950 setstripe_args_init(lsa);
2951 lsa->lsa_first_comp = false;
2952 lsa->lsa_tgts = osts;
2955 rc = build_layout_from_yaml_node(node->cy_child, layout,
2960 if (!node->cy_string)
2963 /* skip leading lmm_ if present, to simplify parsing */
2964 if (strncmp(string, "lmm_", 4) == 0)
2967 if (node->cy_type == CYAML_TYPE_STRING) {
2968 if (!strcmp(string, "lcme_extent.e_end")) {
2969 if (!strcmp(node->cy_valuestring, "EOF") ||
2970 !strcmp(node->cy_valuestring, "eof"))
2971 lsa->lsa_comp_end = LUSTRE_EOF;
2972 } else if (!strcmp(string, "pool")) {
2973 lsa->lsa_pool_name = node->cy_valuestring;
2974 } else if (!strcmp(string, "pattern")) {
2975 if (!strcmp(node->cy_valuestring, "mdt"))
2976 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2977 if (!strcmp(node->cy_valuestring,
2978 "raid0,overstriped"))
2980 LLAPI_LAYOUT_OVERSTRIPING;
2981 } else if (!strcmp(string, "lcme_flags")) {
2982 rc = comp_str2flags(node->cy_valuestring,
2983 &lsa->lsa_comp_flags,
2984 &lsa->lsa_comp_neg_flags);
2988 * Only template flags have meaning in
2989 * the layout for a new file
2991 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2993 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2994 if (!strcmp(string, "lcm_mirror_count")) {
2995 lsa->lsa_mirror_count = node->cy_valueint;
2996 } else if (!strcmp(string, "lcme_extent.e_start")) {
2997 if (node->cy_valueint == 0)
2998 lsa->lsa_first_comp = true;
2999 } else if (!strcmp(string, "lcme_extent.e_end")) {
3000 if (node->cy_valueint == -1)
3001 lsa->lsa_comp_end = LUSTRE_EOF;
3003 lsa->lsa_comp_end = node->cy_valueint;
3004 } else if (!strcmp(string, "stripe_count")) {
3005 lsa->lsa_stripe_count = node->cy_valueint;
3006 } else if (!strcmp(string, "stripe_size")) {
3007 lsa->lsa_stripe_size = node->cy_valueint;
3008 } else if (!strcmp(string, "extension_size")) {
3009 lsa->lsa_extension_size = node->cy_valueint;
3010 lsa->lsa_extension_comp = true;
3011 } else if (!strcmp(string, "stripe_offset")) {
3012 lsa->lsa_stripe_off = node->cy_valueint;
3013 } else if (!strcmp(string, "l_ost_idx")) {
3014 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3019 node = node->cy_next;
3022 if (prevp == &prev) {
3023 rc = build_prev_component(layout, prevp, lsa, true);
3027 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3028 rc = build_component(layout, lsa, *layout != NULL);
3034 static int lfs_comp_create_from_yaml(char *template,
3035 struct llapi_layout **layout,
3036 struct lfs_setstripe_args *lsa,
3039 struct cYAML *tree = NULL, *err_rc = NULL;
3042 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3044 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3045 progname, template);
3046 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3047 "can't parse", &err_rc);
3048 cYAML_print_tree2file(stderr, err_rc);
3049 cYAML_free_tree(err_rc);
3054 /* initialize lsa for plain file */
3055 setstripe_args_init(lsa);
3056 lsa->lsa_tgts = osts;
3058 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3060 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3061 progname, template);
3064 /* clean clean lsa */
3065 setstripe_args_init(lsa);
3069 cYAML_free_tree(tree);
3074 * Get the extension size from the next (SEL) component and extend the
3075 * current component on it. The start of the next component is to be
3078 * \param[in] layout the current layout
3079 * \param[in] start the start of the current component
3080 * \param[in,out] end the end of the current component
3081 * \param[in] offset the offset to adjust the end position to instead of
3084 * \retval 0 - extended successfully
3085 * \retval < 0 - error
3087 static int layout_extend_comp(struct llapi_layout *layout,
3088 uint64_t start, uint64_t *end,
3091 uint64_t size, next_start, next_end;
3094 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3097 "%s setstripe: cannot move component cursor: %s\n",
3098 progname, strerror(errno));
3103 * Even if the @size will not be used below, this will fail if
3104 * this is not a SEL component - a good confirmation we are
3105 * working on right components.
3107 rc = llapi_layout_extension_size_get(layout, &size);
3110 "%s setstripe: cannot get component ext size: %s\n",
3111 progname, strerror(errno));
3115 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3117 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3118 progname, strerror(errno));
3122 next_start += offset ?: size;
3123 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3125 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3126 progname, strerror(errno));
3130 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3133 "%s setstripe: cannot move component cursor: %s\n",
3134 progname, strerror(errno));
3138 *end += offset ?: size;
3139 rc = llapi_layout_comp_extent_set(layout, start, *end);
3141 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3142 progname, strerror(errno));
3150 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3151 * end of the last component in the existing file, and adjust the
3152 * first extent start of the components to be added accordingly.
3154 * In the create mode, we need to check if the first component is an extendable
3155 * SEL component and extend its length to the extension size (first component
3156 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3158 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3161 struct llapi_layout *head;
3162 uint64_t start = 0, prev_end = 0;
3166 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3171 head = llapi_layout_get_by_path(fname, 0);
3174 "%s setstripe: cannot read layout from '%s': %s\n",
3175 progname, fname, strerror(errno));
3177 } else if (errno == ENODATA) {
3179 * file without LOVEA, this component-add will be turned
3180 * into a component-create.
3182 llapi_layout_free(head);
3186 * the new layout will be added to an empty one, it
3187 * still needs to be adjusted below
3191 } else if (!llapi_layout_is_composite(head)) {
3193 "%s setstripe: '%s' not a composite file\n",
3195 llapi_layout_free(head);
3199 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3202 "%s setstripe: cannot get prev extent: %s\n",
3203 progname, strerror(errno));
3204 llapi_layout_free(head);
3208 llapi_layout_free(head);
3212 /* Make sure we use the first component of the layout to be added. */
3213 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3216 "%s setstripe: cannot move component cursor: %s\n",
3217 progname, strerror(errno));
3221 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3223 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3224 progname, strerror(errno));
3228 if (start == 0 && end == 0) {
3229 rc = layout_extend_comp(layout, start, &end,
3230 comp_add ? prev_end : 0);
3235 if (start > prev_end || end < prev_end) {
3237 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3238 progname, start, end, prev_end);
3242 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3245 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3246 progname, prev_end, end, strerror(errno));
3253 static int mirror_adjust_first_extents(struct mirror_args *list)
3261 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3264 list = list->m_next;
3270 static inline bool arg_is_eof(char *arg)
3272 return !strncmp(arg, "-1", strlen("-1")) ||
3273 !strncmp(arg, "EOF", strlen("EOF")) ||
3274 !strncmp(arg, "eof", strlen("eof"));
3278 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3280 * Return: Valid mirror_args pointer on success and
3281 * NULL if memory allocation fails.
3283 static struct mirror_args *lfs_mirror_alloc(void)
3285 struct mirror_args *mirror = NULL;
3288 mirror = calloc(1, sizeof(*mirror));
3290 mirror->m_inherit = false;
3301 * lfs_mirror_free() - Free memory allocated for a mirror argument
3303 * @mirror: Previously allocated mirror argument structure by
3304 * lfs_mirror_alloc().
3306 * Free memory allocated for @mirror.
3310 static void lfs_mirror_free(struct mirror_args *mirror)
3312 if (mirror->m_layout)
3313 llapi_layout_free(mirror->m_layout);
3318 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3319 * @mirror_list: Previously allocated mirror list.
3321 * Free memory allocated for @mirror_list.
3325 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3327 struct mirror_args *next_mirror = NULL;
3329 while (mirror_list) {
3330 next_mirror = mirror_list->m_next;
3331 lfs_mirror_free(mirror_list);
3332 mirror_list = next_mirror;
3344 LFS_COMP_NO_VERIFY_OPT,
3346 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3348 LFS_MIRROR_STATE_OPT,
3350 LFS_MIRROR_INDEX_OPT,
3351 LFS_LAYOUT_FOREIGN_OPT,
3358 static int lfs_setstripe_internal(int argc, char **argv,
3359 enum setstripe_origin opc)
3361 struct lfs_setstripe_args lsa = { 0 };
3362 struct llapi_stripe_param *param = NULL;
3363 struct find_param migrate_mdt_param = {
3373 unsigned long long size_units = 1;
3374 bool migrate_mode = false;
3375 bool migrate_mdt_mode = false;
3376 bool setstripe_mode = false;
3377 bool migration_block = false;
3378 __u64 migration_flags = 0;
3379 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3380 int comp_del = 0, comp_set = 0;
3383 struct llapi_layout *layout = NULL;
3384 struct llapi_layout **lpp = &layout;
3385 bool mirror_mode = false;
3386 bool has_m_file = false;
3387 __u32 mirror_count = 0;
3388 enum mirror_flags mirror_flags = 0;
3389 struct mirror_args *mirror_list = NULL;
3390 struct mirror_args *new_mirror = NULL;
3391 struct mirror_args *last_mirror = NULL;
3392 __u16 mirror_id = 0;
3394 bool from_yaml = false;
3395 bool from_copy = false;
3396 char *template = NULL;
3397 bool foreign_mode = false;
3399 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3400 char *mode_opt = NULL;
3401 mode_t previous_umask = 0;
3402 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3404 struct option long_opts[] = {
3405 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3406 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3407 /* --block is only valid in migrate mode */
3408 { .val = 'b', .name = "block", .has_arg = no_argument },
3409 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3410 { .val = LFS_COMP_ADD_OPT,
3411 .name = "comp-add", .has_arg = no_argument },
3412 { .val = LFS_COMP_ADD_OPT,
3413 .name = "component-add", .has_arg = no_argument },
3414 { .val = LFS_COMP_DEL_OPT,
3415 .name = "comp-del", .has_arg = no_argument },
3416 { .val = LFS_COMP_DEL_OPT,
3417 .name = "component-del", .has_arg = no_argument },
3418 { .val = LFS_COMP_FLAGS_OPT,
3419 .name = "comp-flags", .has_arg = required_argument },
3420 { .val = LFS_COMP_FLAGS_OPT,
3421 .name = "component-flags",
3422 .has_arg = required_argument },
3423 { .val = LFS_COMP_SET_OPT,
3424 .name = "comp-set", .has_arg = no_argument },
3425 { .val = LFS_COMP_SET_OPT,
3426 .name = "component-set",
3427 .has_arg = no_argument},
3428 { .val = LFS_COMP_NO_VERIFY_OPT,
3429 .name = "no-verify", .has_arg = no_argument},
3430 { .val = LFS_LAYOUT_FLAGS_OPT,
3431 .name = "flags", .has_arg = required_argument},
3432 { .val = LFS_LAYOUT_FOREIGN_OPT,
3433 .name = "foreign", .has_arg = optional_argument},
3434 { .val = LFS_MIRROR_ID_OPT,
3435 .name = "mirror-id", .has_arg = required_argument},
3436 { .val = LFS_MODE_OPT,
3437 .name = "mode", .has_arg = required_argument},
3438 { .val = LFS_LAYOUT_COPY,
3439 .name = "copy", .has_arg = required_argument},
3440 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3441 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3442 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3443 { .val = 'C', .name = "overstripe-count",
3444 .has_arg = required_argument},
3445 { .val = 'd', .name = "delete", .has_arg = no_argument},
3446 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3447 /* --non-direct is only valid in migrate mode */
3448 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3449 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3450 { .val = 'E', .name = "component-end",
3451 .has_arg = required_argument},
3452 { .val = 'f', .name = "file", .has_arg = required_argument },
3453 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3454 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3455 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3456 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3457 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3458 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3459 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3460 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3461 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3462 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3463 { .val = 'L', .name = "layout", .has_arg = required_argument },
3464 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3465 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3466 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3467 /* --non-block is only valid in migrate mode */
3468 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3469 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3470 { .val = 'o', .name = "ost", .has_arg = required_argument },
3471 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3472 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3473 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3475 { .val = 'p', .name = "pool", .has_arg = required_argument },
3476 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3477 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3478 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3479 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3480 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3481 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3482 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3483 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3484 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3485 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3486 /* --verbose is only valid in migrate mode */
3487 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3488 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3489 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3490 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3491 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3492 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3495 setstripe_args_init(&lsa);
3497 migrate_mode = (opc == SO_MIGRATE);
3498 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3499 setstripe_mode = (opc == SO_SETSTRIPE);
3500 if (opc == SO_MIRROR_DELETE) {
3502 mirror_flags = MF_DESTROY;
3505 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3507 while ((c = getopt_long(argc, argv,
3508 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3509 long_opts, NULL)) >= 0) {
3515 case LFS_COMP_ADD_OPT:
3518 case LFS_COMP_DEL_OPT:
3521 case LFS_COMP_FLAGS_OPT:
3522 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3523 &lsa.lsa_comp_neg_flags);
3526 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3528 "%s: inverted flags are not supported\n",
3533 case LFS_COMP_SET_OPT:
3536 case LFS_COMP_NO_VERIFY_OPT:
3537 mirror_flags |= MF_NO_VERIFY;
3539 case LFS_MIRROR_ID_OPT: {
3540 unsigned long int id;
3543 id = strtoul(optarg, &end, 0);
3544 if (errno != 0 || *end != '\0' || id == 0 ||
3547 "%s %s: invalid mirror ID '%s'\n",
3548 progname, argv[0], optarg);
3552 mirror_id = (__u16)id;
3555 case LFS_LAYOUT_FLAGS_OPT: {
3558 /* check for numeric flags (foreign and mirror cases) */
3559 if (setstripe_mode && !mirror_mode && !last_mirror) {
3561 flags = strtoul(optarg, &end, 16);
3562 if (errno != 0 || *end != '\0' ||
3563 flags >= UINT32_MAX) {
3565 "%s %s: invalid hex flags '%s'\n",
3566 progname, argv[0], optarg);
3569 if (!foreign_mode) {
3571 "%s %s: hex flags must be specified with --foreign option\n",
3578 if (!mirror_mode || !last_mirror) {
3580 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3585 result = comp_str2flags(optarg, &last_mirror->m_flags,
3592 "%s: inverted flags are not supported\n",
3597 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3599 "%s: unsupported mirror flags: %s\n",
3606 case LFS_LAYOUT_FOREIGN_OPT:
3608 /* check pure numeric */
3609 type = strtoul(optarg, &end, 0);
3612 type = check_foreign_type_name(optarg);
3613 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3615 "%s %s: unrecognized foreign type '%s'\n",
3620 } else if (type >= UINT32_MAX) {
3622 "%s %s: invalid foreign type '%s'\n",
3623 progname, argv[0], optarg);
3627 foreign_mode = true;
3632 mode = strtoul(mode_opt, &end, 8);
3635 "%s %s: bad mode '%s'\n",
3636 progname, argv[0], mode_opt);
3639 previous_umask = umask(0);
3642 case LFS_LAYOUT_COPY:
3647 if (!migrate_mode) {
3649 "%s %s: -b|--block valid only for migrate command\n",
3653 migration_block = true;
3656 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3658 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3662 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3666 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3667 if (errno != 0 || *end != '\0'||
3668 lsa.lsa_stripe_count < -1 ||
3669 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3671 "%s %s: invalid stripe count '%s'\n",
3672 progname, argv[0], optarg);
3676 if (lsa.lsa_stripe_count == -1)
3677 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3680 /* delete the default striping pattern */
3682 if (opc == SO_MIRROR_SPLIT) {
3685 "%s %s: -d cannot used with -f\n",
3689 mirror_flags |= MF_DESTROY;
3693 if (!migrate_mode) {
3695 "%s %s: -D|--non-direct is valid only for migrate command\n",
3699 migration_flags |= MIGRATION_NONDIRECT;
3702 if (lsa.lsa_comp_end != 0) {
3703 result = comp_args_to_layout(lpp, &lsa, true);
3705 fprintf(stderr, "%s: invalid layout\n",
3710 setstripe_args_init_inherit(&lsa);
3713 if (arg_is_eof(optarg)) {
3714 lsa.lsa_comp_end = LUSTRE_EOF;
3716 result = llapi_parse_size(optarg,
3719 /* assume units of KB if too small */
3720 if (lsa.lsa_comp_end < 4096)
3721 lsa.lsa_comp_end *= 1024;
3723 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3725 "%s %s: invalid component end '%s'\n",
3726 progname, argv[0], optarg);
3732 if (!migrate_mode) {
3734 "--mdt-hash is valid only for migrate command\n");
3738 lsa.lsa_pattern = check_hashtype(optarg);
3739 if (lsa.lsa_pattern == 0) {
3741 "%s %s: bad stripe hash type '%s'\n",
3742 progname, argv[0], optarg);
3748 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3749 if (errno != 0 || *end != '\0' ||
3750 lsa.lsa_stripe_off < -1 ||
3751 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3753 "%s %s: invalid stripe offset '%s'\n",
3754 progname, argv[0], optarg);
3757 if (lsa.lsa_stripe_off == -1)
3758 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3761 comp_id = strtoul(optarg, &end, 0);
3762 if (*end != '\0' || comp_id == 0 ||
3763 comp_id > LCME_ID_MAX) {
3765 "%s %s: invalid component ID '%s'\n",
3766 progname, argv[0], optarg);
3771 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3773 "error: %s: invalid option: %s\n",
3774 progname, argv[optopt + 1]);
3777 if (opc == SO_MIRROR_EXTEND) {
3780 "error: %s: '-N' must exist in front of '%s'\n",
3781 progname, argv[optopt + 1]);
3784 last_mirror->m_file = optarg;
3785 last_mirror->m_count = 1;
3789 mirror_list = lfs_mirror_alloc();
3790 mirror_list->m_file = optarg;
3795 if (strcmp(argv[optind - 1], "mdt") == 0) {
3796 /* Can be only the first component */
3800 "error: 'mdt' layout can be only the first one\n");
3803 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3806 "error: 'mdt' layout size is too big\n");
3809 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3810 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3813 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3819 if (!migrate_mode) {
3821 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3825 migrate_mdt_mode = true;
3826 lsa.lsa_nr_tgts = parse_targets(tgts,
3827 sizeof(tgts) / sizeof(__u32),
3828 lsa.lsa_nr_tgts, optarg, NULL);
3829 if (lsa.lsa_nr_tgts < 0) {
3831 "%s %s: invalid MDT target(s) '%s'\n",
3832 progname, argv[0], optarg);
3836 lsa.lsa_tgts = tgts;
3837 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3838 lsa.lsa_stripe_off = tgts[0];
3841 if (!migrate_mode) {
3843 "%s %s: -n|--non-block valid only for migrate command\n",
3847 migration_flags |= MIGRATION_NONBLOCK;
3850 if (opc == SO_SETSTRIPE) {
3851 opc = SO_MIRROR_CREATE;
3857 mirror_count = strtoul(optarg, &end, 0);
3858 if (errno != 0 || *end != '\0' ||
3859 mirror_count == 0 ||
3860 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3862 "error: %s: bad mirror count: %s\n",
3869 new_mirror = lfs_mirror_alloc();
3870 new_mirror->m_count = mirror_count;
3873 mirror_list = new_mirror;
3876 /* wrap up last mirror */
3877 if (!setstripe_args_specified(&lsa))
3878 last_mirror->m_inherit = true;
3879 if (lsa.lsa_comp_end == 0)
3880 lsa.lsa_comp_end = LUSTRE_EOF;
3882 result = comp_args_to_layout(lpp, &lsa, true);
3884 lfs_mirror_free(new_mirror);
3888 setstripe_args_init_inherit(&lsa);
3890 last_mirror->m_next = new_mirror;
3893 last_mirror = new_mirror;
3894 lpp = &last_mirror->m_layout;
3897 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3898 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3900 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3902 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3904 "%s %s: -o|--ost incompatible with DoM layout\n",
3909 * -o allows overstriping, and must note it because
3910 * parse_targets is shared with MDT striping, which
3911 * does not allow duplicates
3913 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3914 lsa.lsa_nr_tgts = parse_targets(tgts,
3915 sizeof(tgts) / sizeof(__u32),
3916 lsa.lsa_nr_tgts, optarg,
3918 if (lsa.lsa_nr_tgts < 0) {
3920 "%s %s: invalid OST target(s) '%s'\n",
3921 progname, argv[0], optarg);
3925 lsa.lsa_tgts = tgts;
3926 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3927 lsa.lsa_stripe_off = tgts[0];
3932 lsa.lsa_pool_name = optarg;
3934 if (strlen(lsa.lsa_pool_name) == 0 ||
3935 strncmp(lsa.lsa_pool_name, "none",
3936 LOV_MAXPOOLNAME) == 0)
3937 lsa.lsa_pool_name = NULL;
3940 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3942 /* assume units of KB if too small to be valid */
3943 if (lsa.lsa_stripe_size < 4096)
3944 lsa.lsa_stripe_size *= 1024;
3946 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3948 "%s %s: invalid stripe size '%s'\n",
3949 progname, argv[0], optarg);
3954 if (!migrate_mode) {
3956 "%s %s: -v|--verbose valid only for migrate command\n",
3960 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3961 migration_flags = MIGRATION_VERBOSE;
3971 result = llapi_parse_size(optarg,
3972 &lsa.lsa_extension_size,
3976 "%s %s: invalid extension size '%s'\n",
3977 progname, argv[0], optarg);
3981 lsa.lsa_extension_comp = true;
3984 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3985 progname, argv[0], argv[optind - 1]);
3990 fname = argv[optind];
3992 if (optind == argc) {
3993 fprintf(stderr, "%s %s: FILE must be specified\n",
3998 /* lfs migrate $filename should keep the file's layout by default */
3999 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
4003 if (xattr && !foreign_mode) {
4005 * only print a warning as this is harmless and will be ignored
4008 "%s %s: xattr has been specified for non-foreign layout\n",
4010 } else if (foreign_mode && !xattr) {
4012 "%s %s: xattr must be provided in foreign mode\n",
4017 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4018 comp_set || comp_id || delete || from_copy ||
4019 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4022 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4027 if (mirror_mode && mirror_count == 0) {
4029 "error: %s: --mirror-count|-N option is required\n",
4036 if (!setstripe_args_specified(&lsa))
4037 last_mirror->m_inherit = true;
4038 if (lsa.lsa_comp_end == 0)
4039 lsa.lsa_comp_end = LUSTRE_EOF;
4042 if (lsa.lsa_comp_end != 0) {
4043 result = comp_args_to_layout(lpp, &lsa, true);
4045 fprintf(stderr, "error: %s: invalid layout\n",
4052 if (mirror_flags & MF_NO_VERIFY) {
4053 if (opc != SO_MIRROR_EXTEND) {
4055 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4059 } else if (!has_m_file) {
4061 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4068 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4070 "%s %s: --component-set doesn't have component-id set\n",
4075 if ((delete + comp_set + comp_del + comp_add) > 1) {
4077 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4082 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4083 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4085 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4090 if ((comp_set || comp_del) &&
4091 (setstripe_args_specified(&lsa) || layout != NULL)) {
4093 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4098 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4100 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4105 if (comp_add || comp_del) {
4108 result = lstat(fname, &st);
4109 if (result == 0 && S_ISDIR(st.st_mode)) {
4111 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4118 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4127 "%s %s: option -E must be specified with --component-add\n",
4133 if (from_yaml && from_copy) {
4135 "%s: can't specify --yaml and --copy together\n",
4140 if ((from_yaml || from_copy) &&
4141 (setstripe_args_specified(&lsa) || layout != NULL)) {
4143 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4148 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4150 "%s %s: options --non-block and --block are mutually exclusive\n",
4155 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4156 opc != SO_MIRROR_DELETE && comp_id != 0) {
4158 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4163 if (migrate_mdt_mode) {
4164 struct lmv_user_md *lmu;
4166 /* initialize migrate mdt parameters */
4167 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4168 LMV_USER_MAGIC_SPECIFIC));
4171 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4172 progname, argv[0], strerror(ENOMEM));
4176 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4177 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4178 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4180 "%s %s: migrate should specify MDT index\n",
4185 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4186 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4187 lmu->lum_hash_type = lsa.lsa_pattern;
4189 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4190 if (lsa.lsa_pool_name) {
4191 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4192 sizeof(lmu->lum_pool_name) - 1);
4193 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4195 if (lsa.lsa_nr_tgts > 1) {
4198 if (lsa.lsa_stripe_count > 0 &&
4199 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4200 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4202 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4203 progname, lsa.lsa_stripe_count,
4209 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4210 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4211 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4212 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4214 lmu->lum_magic = LMV_USER_MAGIC;
4217 migrate_mdt_param.fp_lmv_md = lmu;
4218 migrate_mdt_param.fp_migrate = 1;
4219 } else if (!layout) {
4220 /* initialize stripe parameters */
4221 param = calloc(1, offsetof(typeof(*param),
4222 lsp_osts[lsa.lsa_nr_tgts]));
4225 "%s %s: cannot allocate memory for parameters: %s\n",
4226 progname, argv[0], strerror(ENOMEM));
4231 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4232 param->lsp_stripe_size = lsa.lsa_stripe_size;
4233 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4234 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4235 param->lsp_stripe_count = -1;
4237 param->lsp_stripe_count = lsa.lsa_stripe_count;
4239 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4240 param->lsp_stripe_offset = -1;
4242 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4243 param->lsp_stripe_pattern =
4244 llapi_pattern_to_lov(lsa.lsa_pattern);
4245 if (param->lsp_stripe_pattern == EINVAL) {
4246 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4251 param->lsp_pool = lsa.lsa_pool_name;
4252 param->lsp_is_specific = false;
4253 if (lsa.lsa_nr_tgts > 0) {
4254 if (lsa.lsa_stripe_count > 0 &&
4255 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4256 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4257 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4259 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4260 argv[0], lsa.lsa_stripe_count,
4266 param->lsp_is_specific = true;
4267 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4268 memcpy(param->lsp_osts, tgts,
4269 sizeof(*tgts) * lsa.lsa_nr_tgts);
4274 /* generate a layout from a YAML template */
4275 result = lfs_comp_create_from_yaml(template, &layout,
4279 "error: %s: can't create composite layout from template file %s\n",
4285 if (layout != NULL || mirror_list != NULL) {
4287 result = mirror_adjust_first_extents(mirror_list);
4289 result = layout_adjust_first_extent(fname, layout,
4291 if (result == -ENODATA)
4293 else if (result != 0) {
4294 fprintf(stderr, "error: %s: invalid layout\n",
4300 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4302 layout = llapi_layout_get_by_path(template ?: fname, 0);
4305 "%s: can't create composite layout from file %s: %s\n",
4306 progname, template ?: fname,
4313 if (migrate_mdt_mode) {
4314 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4315 } else if (migrate_mode) {
4316 result = lfs_migrate(fname, migration_flags, param,
4318 } else if (comp_set != 0) {
4319 result = lfs_component_set(fname, comp_id,
4322 lsa.lsa_comp_neg_flags);
4323 } else if (comp_del != 0) {
4324 result = lfs_component_del(fname, comp_id,
4326 lsa.lsa_comp_neg_flags);
4327 } else if (comp_add != 0) {
4328 result = lfs_component_add(fname, layout);
4329 } else if (opc == SO_MIRROR_CREATE) {
4330 result = mirror_create(fname, mirror_list);
4331 } else if (opc == SO_MIRROR_EXTEND) {
4332 result = mirror_extend(fname, mirror_list,
4334 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4335 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4337 "%s: no mirror specified to delete from '%s'\n",
4341 if (lsa.lsa_pool_name)
4342 mirror_flags |= MF_COMP_POOL;
4343 else if (mirror_id != 0)
4344 comp_id = mirror_id;
4346 mirror_flags |= MF_COMP_ID;
4347 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4349 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4353 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4355 has_m_file ? mirror_list->m_file :
4357 } else if (layout) {
4358 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4364 } else if (foreign_mode) {
4365 result = llapi_file_create_foreign(fname, mode, type,
4372 result = llapi_file_open_param(fname,
4381 /* Save the first error encountered. */
4389 umask(previous_umask);
4392 free(migrate_mdt_param.fp_lmv_md);
4393 llapi_layout_free(layout);
4394 lfs_mirror_list_free(mirror_list);
4399 llapi_layout_free(layout);
4400 lfs_mirror_list_free(mirror_list);
4404 static int lfs_poollist(int argc, char **argv)
4409 return llapi_poollist(argv[1]);
4412 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4413 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4418 char *endptr = "AD";
4423 else if (str[0] == '-')
4429 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4430 long long val = strtoll(timebuf, &endptr, 0);
4435 unit *= 52; /* 52 weeks + 1 day below */
4436 case 'w': /* fallthrough */
4438 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4439 param->fp_time_margin *= (1 + unit / 52);
4440 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4441 case '\0': /* days are default unit if none used */
4442 case 'd': /* fallthrough */
4444 case 'h': /* fallthrough */
4446 case 'm': /* fallthrough */
4448 case 's': /* fallthrough */
4450 /* don't need to multiply by 1 for seconds */
4453 "%s find: bad time string '%s': %s\n",
4454 progname, timebuf, strerror(EINVAL));
4458 if (param->fp_time_margin == 0 ||
4459 (*endptr && unit < param->fp_time_margin))
4460 param->fp_time_margin = unit;
4467 fprintf(stderr, "%s find: bad time '%s': too large\n",
4477 static int str2quotaid(__u32 *id, const char *arg)
4479 unsigned long int projid_tmp = 0;
4480 char *endptr = NULL;
4482 projid_tmp = strtoul(arg, &endptr, 10);
4483 if (*endptr != '\0')
4485 if (projid_tmp >= UINT32_MAX)
4492 static int name2uid(unsigned int *id, const char *name)
4494 struct passwd *passwd;
4496 passwd = getpwnam(name);
4499 *id = passwd->pw_uid;
4504 static int name2gid(unsigned int *id, const char *name)
4506 struct group *group;
4508 group = getgrnam(name);
4511 *id = group->gr_gid;
4516 static inline int name2projid(unsigned int *id, const char *name)
4521 static int uid2name(char **name, unsigned int id)
4523 struct passwd *passwd;
4525 passwd = getpwuid(id);
4528 *name = passwd->pw_name;
4533 static inline int gid2name(char **name, unsigned int id)
4535 struct group *group;
4537 group = getgrgid(id);
4540 *name = group->gr_name;
4545 static int name2layout(__u32 *layout, char *name)
4547 char *ptr, *layout_name;
4550 for (ptr = name; ; ptr = NULL) {
4551 layout_name = strtok(ptr, ",");
4554 if (strcmp(layout_name, "released") == 0)
4555 *layout |= LOV_PATTERN_F_RELEASED;
4556 else if (strcmp(layout_name, "raid0") == 0)
4557 *layout |= LOV_PATTERN_RAID0;
4558 else if (strcmp(layout_name, "mdt") == 0)
4559 *layout |= LOV_PATTERN_MDT;
4560 else if (strcmp(layout_name, "overstriping") == 0)
4561 *layout |= LOV_PATTERN_OVERSTRIPING;
4568 static int lfs_find(int argc, char **argv)
4573 struct find_param param = {
4576 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4578 struct option long_opts[] = {
4579 { .val = 'A', .name = "atime", .has_arg = required_argument },
4580 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4581 { .val = 'B', .name = "btime", .has_arg = required_argument },
4582 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4583 { .val = LFS_COMP_COUNT_OPT,
4584 .name = "comp-count", .has_arg = required_argument },
4585 { .val = LFS_COMP_COUNT_OPT,
4586 .name = "component-count",
4587 .has_arg = required_argument },
4588 { .val = LFS_COMP_FLAGS_OPT,
4589 .name = "comp-flags", .has_arg = required_argument },
4590 { .val = LFS_COMP_FLAGS_OPT,
4591 .name = "component-flags",
4592 .has_arg = required_argument },
4593 { .val = LFS_COMP_START_OPT,
4594 .name = "comp-start", .has_arg = required_argument },
4595 { .val = LFS_COMP_START_OPT,
4596 .name = "component-start",
4597 .has_arg = required_argument },
4598 { .val = LFS_MIRROR_STATE_OPT,
4599 .name = "mirror-state", .has_arg = required_argument },
4600 { .val = LFS_NEWERXY_OPT,
4601 .name = "newer", .has_arg = required_argument},
4602 { .val = LFS_NEWERXY_OPT,
4603 .name = "neweraa", .has_arg = required_argument},
4604 { .val = LFS_NEWERXY_OPT,
4605 .name = "neweram", .has_arg = required_argument},
4606 { .val = LFS_NEWERXY_OPT,
4607 .name = "newerac", .has_arg = required_argument},
4608 { .val = LFS_NEWERXY_OPT,
4609 .name = "newerab", .has_arg = required_argument},
4610 { .val = LFS_NEWERXY_OPT,
4611 .name = "newerma", .has_arg = required_argument},
4612 { .val = LFS_NEWERXY_OPT,
4613 .name = "newermm", .has_arg = required_argument},
4614 { .val = LFS_NEWERXY_OPT,
4615 .name = "newermc", .has_arg = required_argument},
4616 { .val = LFS_NEWERXY_OPT,
4617 .name = "newermb", .has_arg = required_argument},
4618 { .val = LFS_NEWERXY_OPT,
4619 .name = "newerca", .has_arg = required_argument},
4620 { .val = LFS_NEWERXY_OPT,
4621 .name = "newercm", .has_arg = required_argument},
4622 { .val = LFS_NEWERXY_OPT,
4623 .name = "newercc", .has_arg = required_argument},
4624 { .val = LFS_NEWERXY_OPT,
4625 .name = "newercb", .has_arg = required_argument},
4626 { .val = LFS_NEWERXY_OPT,
4627 .name = "newerba", .has_arg = required_argument},
4628 { .val = LFS_NEWERXY_OPT,
4629 .name = "newerbm", .has_arg = required_argument},
4630 { .val = LFS_NEWERXY_OPT,
4631 .name = "newerbc", .has_arg = required_argument},
4632 { .val = LFS_NEWERXY_OPT,
4633 .name = "newerbb", .has_arg = required_argument},
4634 { .val = LFS_NEWERXY_OPT,
4635 .name = "newerBa", .has_arg = required_argument},
4636 { .val = LFS_NEWERXY_OPT,
4637 .name = "newerBm", .has_arg = required_argument},
4638 { .val = LFS_NEWERXY_OPT,
4639 .name = "newerBc", .has_arg = required_argument},
4640 { .val = LFS_NEWERXY_OPT,
4641 .name = "newerBB", .has_arg = required_argument},
4642 { .val = LFS_NEWERXY_OPT,
4643 .name = "newerat", .has_arg = required_argument},
4644 { .val = LFS_NEWERXY_OPT,
4645 .name = "newermt", .has_arg = required_argument},
4646 { .val = LFS_NEWERXY_OPT,
4647 .name = "newerct", .has_arg = required_argument},
4648 { .val = LFS_NEWERXY_OPT,
4649 .name = "newerbt", .has_arg = required_argument},
4650 { .val = LFS_NEWERXY_OPT,
4651 .name = "newerBt", .has_arg = required_argument},
4652 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4653 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4654 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4655 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4656 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4657 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4658 { .val = 'E', .name = "component-end",
4659 .has_arg = required_argument },
4660 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4661 { .val = LFS_LAYOUT_FOREIGN_OPT,
4662 .name = "foreign", .has_arg = optional_argument},
4663 { .val = 'g', .name = "gid", .has_arg = required_argument },
4664 { .val = 'G', .name = "group", .has_arg = required_argument },
4665 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4666 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4667 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4668 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4669 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4670 { .val = 'L', .name = "layout", .has_arg = required_argument },
4671 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4672 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4673 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4674 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4675 { .val = 'n', .name = "name", .has_arg = required_argument },
4676 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4677 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4678 { .val = 'O', .name = "obd", .has_arg = required_argument },
4679 { .val = 'O', .name = "ost", .has_arg = required_argument },
4680 /* no short option for pool yet, can be 'p' after 2.18 */
4681 { .val = LFS_POOL_OPT,
4682 .name = "pool", .has_arg = required_argument },
4683 { .val = '0', .name = "print0", .has_arg = no_argument },
4684 { .val = 'P', .name = "print", .has_arg = no_argument },
4685 { .val = LFS_PROJID_OPT,
4686 .name = "projid", .has_arg = required_argument },
4687 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4688 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4689 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4690 { .val = 's', .name = "size", .has_arg = required_argument },
4691 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4692 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4693 { .val = 't', .name = "type", .has_arg = required_argument },
4694 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4695 { .val = 'u', .name = "uid", .has_arg = required_argument },
4696 { .val = 'U', .name = "user", .has_arg = required_argument },
4697 { .val = 'z', .name = "extension-size",
4698 .has_arg = required_argument },
4699 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4700 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4701 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4715 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4716 while ((c = getopt_long_only(argc, argv,
4717 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4718 long_opts, &optidx)) >= 0) {
4723 /* '!' is part of option */
4725 * when getopt_long_only() finds a string which is not
4726 * an option nor a known option argument it returns 1
4727 * in that case if we already have found pathstart and pathend
4728 * (i.e. we have the list of pathnames),
4729 * the only supported value is "!"
4731 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4732 if (!isoption && pathend != -1) {
4734 "err: %s: filename|dirname must either precede options or follow options\n",
4739 if (!isoption && pathstart == -1)
4740 pathstart = optind - 1;
4741 if (isoption && pathstart != -1 && pathend == -1)
4742 pathend = optind - 2;
4749 * unknown; opt is "!" or path component,
4750 * checking done above.
4752 if (strcmp(optarg, "!") == 0)
4756 xtime = ¶m.fp_atime;
4757 xsign = ¶m.fp_asign;
4758 param.fp_exclude_atime = !!neg_opt;
4759 /* no break, this falls through to 'B' for btime */
4762 xtime = ¶m.fp_btime;
4763 xsign = ¶m.fp_bsign;
4764 param.fp_exclude_btime = !!neg_opt;
4766 /* no break, this falls through to 'C' for ctime */
4769 xtime = ¶m.fp_ctime;
4770 xsign = ¶m.fp_csign;
4771 param.fp_exclude_ctime = !!neg_opt;
4773 /* no break, this falls through to 'M' for mtime */
4776 xtime = ¶m.fp_mtime;
4777 xsign = ¶m.fp_msign;
4778 param.fp_exclude_mtime = !!neg_opt;
4780 rc = set_time(¶m, &t, xtime, optarg);
4781 if (rc == LONG_MAX) {
4789 if (optarg[0] == '+') {
4790 param.fp_blocks_sign = -1;
4792 } else if (optarg[0] == '-') {
4793 param.fp_blocks_sign = 1;
4797 param.fp_blocks_units = 1024;
4798 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4799 ¶m.fp_blocks_units, 0);
4801 fprintf(stderr, "error: bad blocks '%s'\n",
4805 param.fp_check_blocks = 1;
4806 param.fp_exclude_blocks = !!neg_opt;
4808 case LFS_COMP_COUNT_OPT:
4809 if (optarg[0] == '+') {
4810 param.fp_comp_count_sign = -1;
4812 } else if (optarg[0] == '-') {
4813 param.fp_comp_count_sign = 1;
4818 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4819 if (errno != 0 || *endptr != '\0' ||
4820 param.fp_comp_count > UINT32_MAX) {
4822 "error: bad component count '%s'\n",
4826 param.fp_check_comp_count = 1;
4827 param.fp_exclude_comp_count = !!neg_opt;
4829 case LFS_COMP_FLAGS_OPT:
4830 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4831 ¶m.fp_comp_neg_flags);
4834 "error: bad component flags '%s'\n",
4838 param.fp_check_comp_flags = 1;
4840 __u32 flags = param.fp_comp_neg_flags;
4842 param.fp_comp_neg_flags = param.fp_comp_flags;
4843 param.fp_comp_flags = flags;
4846 case LFS_COMP_START_OPT:
4847 if (optarg[0] == '+') {
4848 param.fp_comp_start_sign = -1;
4850 } else if (optarg[0] == '-') {
4851 param.fp_comp_start_sign = 1;
4855 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4856 ¶m.fp_comp_start_units, 0);
4859 "error: bad component start '%s'\n",
4863 param.fp_check_comp_start = 1;
4864 param.fp_exclude_comp_start = !!neg_opt;
4866 case LFS_MIRROR_STATE_OPT:
4867 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4868 ¶m.fp_mirror_neg_state);
4871 "error: bad mirrored file state '%s'\n",
4875 param.fp_check_mirror_state = 1;
4877 __u16 state = param.fp_mirror_neg_state;
4879 param.fp_mirror_neg_state =
4880 param.fp_mirror_state;
4881 param.fp_mirror_state = state;
4885 if (optarg[0] == '+') {
4886 param.fp_stripe_count_sign = -1;
4888 } else if (optarg[0] == '-') {
4889 param.fp_stripe_count_sign = 1;
4894 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4895 if (errno != 0 || *endptr != '\0' ||
4896 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
4898 "error: bad stripe_count '%s'\n",
4903 param.fp_check_stripe_count = 1;
4904 param.fp_exclude_stripe_count = !!neg_opt;
4908 param.fp_max_depth = strtol(optarg, 0, 0);
4909 if (errno != 0 || param.fp_max_depth < 0) {
4911 "error: bad maxdepth '%s'\n",
4918 if (optarg[0] == '+') {
4919 param.fp_comp_end_sign = -1;
4921 } else if (optarg[0] == '-') {
4922 param.fp_comp_end_sign = 1;
4926 if (arg_is_eof(optarg)) {
4927 param.fp_comp_end = LUSTRE_EOF;
4928 param.fp_comp_end_units = 1;
4931 rc = llapi_parse_size(optarg,
4933 ¶m.fp_comp_end_units, 0);
4934 /* assume units of KB if too small */
4935 if (param.fp_comp_end < 4096)
4936 param.fp_comp_end *= 1024;
4940 "error: bad component end '%s'\n",
4944 param.fp_check_comp_end = 1;
4945 param.fp_exclude_comp_end = !!neg_opt;
4947 case LFS_LAYOUT_FOREIGN_OPT: {
4948 /* all types by default */
4949 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4952 /* check pure numeric */
4953 type = strtoul(optarg, &endptr, 0);
4956 type = check_foreign_type_name(optarg);
4957 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4959 "%s %s: unknown foreign type '%s'\n",
4964 } else if (type >= UINT32_MAX) {
4966 "%s %s: invalid foreign type '%s'\n",
4967 progname, argv[0], optarg);
4971 param.fp_foreign_type = type;
4972 param.fp_check_foreign = 1;
4973 param.fp_exclude_foreign = !!neg_opt;
4976 case LFS_NEWERXY_OPT: {
4982 time_t ref = time(NULL);
4984 /* no need to check bad options, they won't get here */
4985 if (strlen(long_opts[optidx].name) == 7) {
4986 x = long_opts[optidx].name[5];
4987 y = long_opts[optidx].name[6];
4991 static const char *const fmts[] = {
4992 "%Y-%m-%d %H:%M:%S",
4995 "%H:%M:%S", /* sometime today */
5004 for (i = 0; fmts[i] != NULL; i++) {
5007 /* Init for times relative to today */
5008 if (strncmp(fmts[i], "%H", 2) == 0)
5009 localtime_r(&ref, &tm);
5011 memset(&tm, 0, sizeof(tm));
5012 ptr = strptime(optarg, fmts[i], &tm);
5014 while (ptr && isspace(*ptr))
5016 if (ptr == optarg + strlen(optarg)) {
5024 "%s: invalid time '%s'\n",
5027 "supported formats are:\n ");
5028 for (i = 0; fmts[i] != NULL; i++)
5029 fprintf(stderr, "'%s', ",
5031 fprintf(stderr, "\n");
5037 } else if (y == 'b' || y == 'B') {
5040 rc = llapi_get_lum_file(optarg, NULL, &stx,
5042 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5043 if (!(stx.stx_mask & STATX_BTIME))
5048 "%s: get btime failed '%s': %s\n",
5054 ref = stx.stx_btime.tv_sec;
5056 struct stat statbuf;
5058 if (stat(optarg, &statbuf) < 0) {
5060 "%s: cannot stat file '%s': %s\n",
5069 ref = statbuf.st_atime;
5072 ref = statbuf.st_mtime;
5075 ref = statbuf.st_ctime;
5079 "%s: invalid Y argument: '%c'\n",
5088 xidx = NEWERXY_ATIME;
5091 xidx = NEWERXY_MTIME;
5094 xidx = NEWERXY_CTIME;
5098 xidx = NEWERXY_BTIME;
5102 "%s: invalid X argument: '%c'\n",
5109 newery = ¶m.fp_newery[xidx][negidx];
5115 *newery = *newery > ref ? ref : *newery;
5117 *newery = *newery > ref ? *newery : ref;
5119 param.fp_newerxy = 1;
5124 rc = name2gid(¶m.fp_gid, optarg);
5126 if (str2quotaid(¶m.fp_gid, optarg)) {
5128 "Group/GID: %s cannot be found.\n",
5134 param.fp_exclude_gid = !!neg_opt;
5135 param.fp_check_gid = 1;
5138 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5139 ¶m.fp_hash_exflags,
5140 ¶m.fp_hash_type);
5145 if (param.fp_hash_inflags || param.fp_hash_exflags)
5146 param.fp_check_hash_flag = 1;
5147 param.fp_exclude_hash_type = !!neg_opt;
5153 ret = name2layout(¶m.fp_layout, optarg);
5156 param.fp_exclude_layout = !!neg_opt;
5157 param.fp_check_layout = 1;
5161 rc = name2uid(¶m.fp_uid, optarg);
5163 if (str2quotaid(¶m.fp_uid, optarg)) {
5165 "User/UID: %s cannot be found.\n",
5171 param.fp_exclude_uid = !!neg_opt;
5172 param.fp_check_uid = 1;
5175 param.fp_pattern = (char *)optarg;
5176 param.fp_exclude_pattern = !!neg_opt;
5179 if (optarg[0] == '+') {
5180 param.fp_mirror_count_sign = -1;
5182 } else if (optarg[0] == '-') {
5183 param.fp_mirror_count_sign = 1;
5188 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5189 if (errno != 0 || *endptr != '\0' ||
5190 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5192 "error: bad mirror count '%s'\n",
5196 param.fp_check_mirror_count = 1;
5197 param.fp_exclude_mirror_count = !!neg_opt;
5202 char *buf, *token, *next, *p;
5206 buf = strdup(optarg);
5212 param.fp_exclude_obd = !!neg_opt;
5215 while (token && *token) {
5216 token = strchr(token, ',');
5223 param.fp_exclude_mdt = !!neg_opt;
5224 param.fp_num_alloc_mdts += len;
5225 tmp = realloc(param.fp_mdt_uuid,
5226 param.fp_num_alloc_mdts *
5227 sizeof(*param.fp_mdt_uuid));
5233 param.fp_mdt_uuid = tmp;
5235 param.fp_exclude_obd = !!neg_opt;
5236 param.fp_num_alloc_obds += len;
5237 tmp = realloc(param.fp_obd_uuid,
5238 param.fp_num_alloc_obds *
5239 sizeof(*param.fp_obd_uuid));
5245 param.fp_obd_uuid = tmp;
5247 for (token = buf; token && *token; token = next) {
5248 struct obd_uuid *puuid;
5252 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5255 ¶m.fp_obd_uuid[param.fp_num_obds++];
5257 p = strchr(token, ',');
5264 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5269 strncpy(puuid->uuid, token,
5270 sizeof(puuid->uuid));
5277 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5281 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5283 "Pool name %s is too long (max %d)\n",
5284 optarg, LOV_MAXPOOLNAME);
5289 * We do check for empty pool because empty pool
5290 * is used to find V1 LOV attributes
5292 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5293 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5294 param.fp_exclude_pool = !!neg_opt;
5295 param.fp_check_pool = 1;
5297 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5298 case 'p': /* want this for --pool, to match getstripe/find */
5300 "warning: -p deprecated, use --print0 or -0\n");
5303 param.fp_zero_end = 1;
5305 case 'P': /* we always print, this option is a no-op */
5307 case LFS_PROJID_OPT:
5308 rc = name2projid(¶m.fp_projid, optarg);
5310 if (str2quotaid(¶m.fp_projid, optarg)) {
5312 "Invalid project ID: %s\n",
5318 param.fp_exclude_projid = !!neg_opt;
5319 param.fp_check_projid = 1;
5322 if (optarg[0] == '+') {
5323 param.fp_size_sign = -1;
5325 } else if (optarg[0] == '-') {
5326 param.fp_size_sign = 1;
5330 ret = llapi_parse_size(optarg, ¶m.fp_size,
5331 ¶m.fp_size_units, 0);
5333 fprintf(stderr, "error: bad file size '%s'\n",
5337 param.fp_check_size = 1;
5338 param.fp_exclude_size = !!neg_opt;
5341 if (optarg[0] == '+') {
5342 param.fp_stripe_size_sign = -1;
5344 } else if (optarg[0] == '-') {
5345 param.fp_stripe_size_sign = 1;
5349 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5350 ¶m.fp_stripe_size_units, 0);
5351 /* assume units of KB if too small to be valid */
5352 if (param.fp_stripe_size < 4096)
5353 param.fp_stripe_size *= 1024;
5355 fprintf(stderr, "error: bad stripe_size '%s'\n",
5359 param.fp_check_stripe_size = 1;
5360 param.fp_exclude_stripe_size = !!neg_opt;
5363 param.fp_exclude_type = !!neg_opt;
5364 switch (optarg[0]) {
5366 param.fp_type = S_IFBLK;
5369 param.fp_type = S_IFCHR;
5372 param.fp_type = S_IFDIR;
5375 param.fp_type = S_IFREG;
5378 param.fp_type = S_IFLNK;
5381 param.fp_type = S_IFIFO;
5384 param.fp_type = S_IFSOCK;
5387 fprintf(stderr, "error: %s: bad type '%s'\n",
5394 if (optarg[0] == '+') {
5395 param.fp_mdt_count_sign = -1;
5397 } else if (optarg[0] == '-') {
5398 param.fp_mdt_count_sign = 1;
5403 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5404 if (errno != 0 || *endptr != '\0' ||
5405 param.fp_mdt_count >= UINT32_MAX) {
5406 fprintf(stderr, "error: bad mdt_count '%s'\n",
5411 param.fp_check_mdt_count = 1;
5412 param.fp_exclude_mdt_count = !!neg_opt;
5415 if (optarg[0] == '+') {
5416 param.fp_ext_size_sign = -1;
5418 } else if (optarg[0] == '-') {
5419 param.fp_ext_size_sign = 1;
5423 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5424 ¶m.fp_ext_size_units, 0);
5426 fprintf(stderr, "error: bad ext-size '%s'\n",
5430 param.fp_ext_size /= SEL_UNIT_SIZE;
5431 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5432 param.fp_check_ext_size = 1;
5433 param.fp_exclude_ext_size = !!neg_opt;
5441 if (pathstart == -1) {
5442 fprintf(stderr, "error: %s: no filename|pathname\n",
5446 } else if (pathend == -1) {
5452 rc = llapi_find(argv[pathstart], ¶m);
5455 pathbad = pathstart;
5457 } while (++pathstart < pathend);
5460 fprintf(stderr, "%s: failed for '%s': %s\n",
5461 progname, argv[pathbad], strerror(-rc));
5464 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5465 free(param.fp_obd_uuid);
5467 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5468 free(param.fp_mdt_uuid);
5473 static int lfs_getstripe_internal(int argc, char **argv,
5474 struct find_param *param)
5476 struct option long_opts[] = {
5477 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5478 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5479 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5480 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5481 { .val = LFS_COMP_COUNT_OPT,
5482 .name = "comp-count", .has_arg = no_argument },
5483 { .val = LFS_COMP_COUNT_OPT,
5484 .name = "component-count", .has_arg = no_argument },
5485 { .val = LFS_COMP_FLAGS_OPT,
5486 .name = "comp-flags", .has_arg = optional_argument },
5487 { .val = LFS_COMP_FLAGS_OPT,
5488 .name = "component-flags", .has_arg = optional_argument },
5489 { .val = LFS_COMP_START_OPT,
5490 .name = "comp-start", .has_arg = optional_argument },
5491 { .val = LFS_COMP_START_OPT,
5492 .name = "component-start", .has_arg = optional_argument },
5493 { .val = LFS_MIRROR_INDEX_OPT,
5494 .name = "mirror-index", .has_arg = required_argument },
5495 { .val = LFS_MIRROR_ID_OPT,
5496 .name = "mirror-id", .has_arg = required_argument },
5497 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5498 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5499 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5500 { .val = 'd', .name = "directory", .has_arg = no_argument },
5501 { .val = 'D', .name = "default", .has_arg = no_argument },
5502 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5503 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5504 { .val = 'F', .name = "fid", .has_arg = no_argument },
5505 { .val = 'g', .name = "generation", .has_arg = no_argument },
5506 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5507 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5508 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5509 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5510 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5511 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5512 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5513 { .val = 'L', .name = "layout", .has_arg = no_argument },
5514 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5515 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5516 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5517 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5518 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5519 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5520 { .val = 'O', .name = "obd", .has_arg = required_argument },
5521 { .val = 'O', .name = "ost", .has_arg = required_argument },
5522 { .val = 'p', .name = "pool", .has_arg = no_argument },
5523 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5524 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5525 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5526 { .val = 'R', .name = "raw", .has_arg = no_argument },
5527 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5528 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5529 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5530 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5531 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5532 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5533 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5534 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5535 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5536 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5537 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5541 int pathstart = -1, pathend = -1;
5545 while ((c = getopt_long(argc, argv,
5546 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5547 long_opts, NULL)) != -1) {
5551 /* '!' is part of option */
5552 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5553 if (!isoption && pathend != -1) {
5555 "error: %s: filename|dirname must either precede options or follow options\n",
5559 if (!isoption && pathstart == -1)
5560 pathstart = optind - 1;
5561 if (isoption && pathstart != -1 && pathend == -1)
5562 pathend = optind - 2;
5566 /* unknown: opt is "!" */
5567 if (strcmp(optarg, "!") == 0)
5571 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5572 param->fp_verbose |= VERBOSE_COUNT;
5573 param->fp_max_depth = 0;
5576 case LFS_COMP_COUNT_OPT:
5577 param->fp_verbose |= VERBOSE_COMP_COUNT;
5578 param->fp_max_depth = 0;
5580 case LFS_COMP_FLAGS_OPT:
5582 rc = comp_str2flags(optarg,
5583 ¶m->fp_comp_flags,
5584 ¶m->fp_comp_neg_flags);
5587 "error: %s bad component flags '%s'.\n",
5591 param->fp_check_comp_flags = 1;
5593 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5594 param->fp_max_depth = 0;
5597 case LFS_COMP_START_OPT:
5600 if (tmp[0] == '+') {
5601 param->fp_comp_start_sign = -1;
5603 } else if (tmp[0] == '-') {
5604 param->fp_comp_start_sign = 1;
5607 rc = llapi_parse_size(tmp,
5608 ¶m->fp_comp_start,
5609 ¶m->fp_comp_start_units, 0);
5612 "error: %s bad component start '%s'.\n",
5616 param->fp_check_comp_start = 1;
5618 param->fp_verbose |= VERBOSE_COMP_START;
5619 param->fp_max_depth = 0;
5622 case LFS_MIRROR_INDEX_OPT: {
5623 unsigned long int mirror_index;
5625 if (optarg[0] == '+') {
5626 param->fp_mirror_index_sign = -1;
5628 } else if (optarg[0] == '-') {
5629 param->fp_mirror_index_sign = 1;
5634 mirror_index = strtoul(optarg, &end, 0);
5635 if (errno != 0 || *end != '\0' ||
5636 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5637 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5639 "%s %s: invalid mirror index '%s'\n",
5640 progname, argv[0], optarg);
5644 param->fp_mirror_index = (__u16)mirror_index;
5646 if (param->fp_mirror_id != 0) {
5648 "%s %s: can't specify both mirror index and mirror ID\n",
5652 param->fp_check_mirror_index = 1;
5653 param->fp_exclude_mirror_index = !!neg_opt;
5656 case LFS_MIRROR_ID_OPT: {
5657 unsigned long int mirror_id;
5659 if (optarg[0] == '+') {
5660 param->fp_mirror_id_sign = -1;
5662 } else if (optarg[0] == '-') {
5663 param->fp_mirror_id_sign = 1;
5668 mirror_id = strtoul(optarg, &end, 0);
5669 if (errno != 0 || *end != '\0' ||
5670 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5671 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5673 "%s %s: invalid mirror ID '%s'\n",
5674 progname, argv[0], optarg);
5678 param->fp_mirror_id = (__u16)mirror_id;
5680 if (param->fp_mirror_index != 0) {
5682 "%s %s: can't specify both mirror index and mirror ID\n",
5686 param->fp_check_mirror_id = 1;
5687 param->fp_exclude_mirror_id = !!neg_opt;
5691 param->fp_max_depth = 0;
5694 param->fp_get_default_lmv = 1;
5699 if (tmp[0] == '+') {
5700 param->fp_comp_end_sign = -1;
5702 } else if (tmp[0] == '-') {
5703 param->fp_comp_end_sign = 1;
5707 if (arg_is_eof(tmp)) {
5708 param->fp_comp_end = LUSTRE_EOF;
5709 param->fp_comp_end_units = 1;
5712 rc = llapi_parse_size(tmp,
5713 ¶m->fp_comp_end,
5714 ¶m->fp_comp_end_units, 0);
5715 /* assume units of KB if too small */
5716 if (param->fp_comp_end < 4096)
5717 param->fp_comp_end *= 1024;
5721 "error: %s bad component end '%s'.\n",
5725 param->fp_check_comp_end = 1;
5727 param->fp_verbose |= VERBOSE_COMP_END;
5728 param->fp_max_depth = 0;
5732 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5733 param->fp_verbose |= VERBOSE_DFID;
5734 param->fp_max_depth = 0;
5738 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5739 param->fp_verbose |= VERBOSE_GENERATION;
5740 param->fp_max_depth = 0;
5744 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5745 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5746 param->fp_max_depth = 0;
5751 param->fp_comp_id = strtoul(optarg, &end, 0);
5752 if (*end != '\0' || param->fp_comp_id == 0 ||
5753 param->fp_comp_id > LCME_ID_MAX) {
5755 "error: %s bad component id '%s'\n",
5759 param->fp_check_comp_id = 1;
5761 param->fp_max_depth = 0;
5762 param->fp_verbose |= VERBOSE_COMP_ID;
5766 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5767 param->fp_verbose |= VERBOSE_PATTERN;
5768 param->fp_max_depth = 0;
5771 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5774 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5777 if (!(param->fp_verbose & VERBOSE_DETAIL))
5778 param->fp_max_depth = 0;
5779 param->fp_verbose |= VERBOSE_MDTINDEX;
5782 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5783 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5784 param->fp_max_depth = 0;
5788 if (param->fp_obd_uuid) {
5790 "error: %s: only one obduuid allowed",
5794 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5797 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5798 param->fp_verbose |= VERBOSE_POOL;
5799 param->fp_max_depth = 0;
5806 param->fp_recursive = 1;
5812 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5813 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5814 param->fp_max_depth = 0;
5818 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5824 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5825 param->fp_verbose |= VERBOSE_EXT_SIZE;
5826 param->fp_max_depth = 0;
5834 if (pathstart == -1) {
5835 fprintf(stderr, "error: %s: no filename|pathname\n",
5838 } else if (pathend == -1) {
5846 if (param->fp_recursive)
5847 param->fp_max_depth = -1;
5848 else if (param->fp_verbose & VERBOSE_DETAIL)
5849 param->fp_max_depth = 1;
5851 if (!param->fp_verbose)
5852 param->fp_verbose = VERBOSE_DEFAULT;
5853 if (param->fp_quiet)
5854 param->fp_verbose = VERBOSE_OBJID;
5857 rc = llapi_getstripe(argv[pathstart], param);
5858 } while (++pathstart < pathend && !rc);
5861 fprintf(stderr, "error: %s failed for %s.\n",
5862 argv[0], argv[optind - 1]);
5866 static int lfs_tgts(int argc, char **argv)
5868 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5869 struct find_param param;
5870 int index = 0, rc = 0;
5875 if (argc == 2 && !realpath(argv[1], path)) {
5877 fprintf(stderr, "error: invalid path '%s': %s\n",
5878 argv[1], strerror(-rc));
5882 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5883 /* Check if we have a mount point */
5884 if (mntdir[0] == '\0')
5887 memset(¶m, 0, sizeof(param));
5888 if (!strcmp(argv[0], "mdts"))
5889 param.fp_get_lmv = 1;
5891 rc = llapi_ostlist(mntdir, ¶m);
5893 fprintf(stderr, "error: %s: failed on %s\n",
5896 if (path[0] != '\0')
5898 memset(mntdir, 0, PATH_MAX);
5904 static int lfs_getstripe(int argc, char **argv)
5906 struct find_param param = { 0 };
5908 param.fp_max_depth = 1;
5909 return lfs_getstripe_internal(argc, argv, ¶m);
5913 static int lfs_getdirstripe(int argc, char **argv)
5915 struct find_param param = { 0 };
5916 struct option long_opts[] = {
5917 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5918 { .val = 'D', .name = "default", .has_arg = no_argument },
5919 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5920 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5921 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5922 { .val = 'O', .name = "obd", .has_arg = required_argument },
5923 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5924 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5925 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5926 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
5927 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5928 { .val = LFS_INHERIT_RR_OPT,
5929 .name = "max-inherit-rr", .has_arg = no_argument },
5933 param.fp_get_lmv = 1;
5935 while ((c = getopt_long(argc, argv,
5936 "cDHimO:rtTvXy", long_opts, NULL)) != -1) {
5940 param.fp_verbose |= VERBOSE_COUNT;
5943 param.fp_get_default_lmv = 1;
5947 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5949 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5952 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5955 param.fp_verbose |= VERBOSE_HASH_TYPE;
5958 if (param.fp_obd_uuid) {
5960 "error: %s: only one obduuid allowed",
5964 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5967 param.fp_recursive = 1;
5970 param.fp_verbose |= VERBOSE_DETAIL;
5973 param.fp_verbose |= VERBOSE_INHERIT;
5975 case LFS_INHERIT_RR_OPT:
5976 param.fp_verbose |= VERBOSE_INHERIT_RR;
5982 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5983 progname, argv[0], argv[optind - 1]);
5991 if (param.fp_recursive)
5992 param.fp_max_depth = -1;
5994 if (!param.fp_verbose)
5995 param.fp_verbose = VERBOSE_DEFAULT;
5998 rc = llapi_getstripe(argv[optind], ¶m);
5999 } while (++optind < argc && !rc);
6002 fprintf(stderr, "error: %s failed for %s.\n",
6003 argv[0], argv[optind - 1]);
6008 MNTDF_INODES = 0x0001,
6009 MNTDF_COOKED = 0x0002,
6010 MNTDF_LAZY = 0x0004,
6011 MNTDF_VERBOSE = 0x0008,
6012 MNTDF_SHOW = 0x0010,
6013 MNTDF_DECIMAL = 0x0020,
6016 #define COOK(value, base) \
6019 while (value > base) { \
6027 #define CDF "%11llu"
6028 #define HDF "%8.1f%c"
6032 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6034 double avail, used, ratio = 0;
6037 avail = st->os_ffree;
6038 used = st->os_files - st->os_ffree;
6040 avail = st->os_bavail;
6041 used = st->os_blocks - st->os_bfree;
6043 if (avail + used > 0)
6044 ratio = used / (used + avail) * 100;
6046 /* Round up to match df(1) usage percentage */
6047 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6051 * This is to identify various problem states for "lfs df" if .osn_err = true,
6052 * so only show flags reflecting those states by default. Informational states
6053 * are only shown with "-v" and use lower-case names to distinguish them.
6054 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6056 static struct obd_statfs_state_names {
6057 enum obd_statfs_state osn_state;
6058 const char osn_name;
6061 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6062 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6063 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6064 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6065 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6066 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6067 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6068 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6069 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6072 static int showdf(char *mntdir, struct obd_statfs *stat,
6073 char *uuid, enum mntdf_flags flags,
6074 char *type, int index, int rc)
6076 long long avail, used, total;
6078 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6079 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6080 char tbuf[3 * sizeof(__u64)];
6081 char ubuf[3 * sizeof(__u64)];
6082 char abuf[3 * sizeof(__u64)];
6083 char rbuf[3 * sizeof(__u64)];
6090 if (flags & MNTDF_INODES) {
6091 avail = stat->os_ffree;
6092 used = stat->os_files - stat->os_ffree;
6093 total = stat->os_files;
6095 int shift = flags & MNTDF_COOKED ? 0 : 10;
6097 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6098 used = ((stat->os_blocks - stat->os_bfree) *
6099 stat->os_bsize) >> shift;
6100 total = (stat->os_blocks * stat->os_bsize) >> shift;
6103 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6105 if (flags & MNTDF_COOKED) {
6106 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6110 cook_val = (double)total;
6111 i = COOK(cook_val, base);
6113 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6116 snprintf(tbuf, sizeof(tbuf), CDF, total);
6118 cook_val = (double)used;
6119 i = COOK(cook_val, base);
6121 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6124 snprintf(ubuf, sizeof(ubuf), CDF, used);
6126 cook_val = (double)avail;
6127 i = COOK(cook_val, base);
6129 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6132 snprintf(abuf, sizeof(abuf), CDF, avail);
6134 snprintf(tbuf, sizeof(tbuf), CDF, total);
6135 snprintf(ubuf, sizeof(tbuf), CDF, used);
6136 snprintf(abuf, sizeof(tbuf), CDF, avail);
6139 sprintf(rbuf, RDF, ratio);
6140 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6141 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6143 printf("[%s:%d]", type, index);
6145 if (stat->os_state) {
6149 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6150 if (oss_names[i].osn_state & stat->os_state &&
6151 (oss_names[i].osn_err ||
6152 flags & MNTDF_VERBOSE))
6153 printf("%c", oss_names[i].osn_name);
6160 printf(UUF": inactive device\n", uuid);
6163 printf(UUF": %s\n", uuid, strerror(-rc));
6170 struct ll_stat_type {
6175 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6177 struct ll_statfs_data {
6179 struct obd_statfs sd_st;
6182 struct ll_statfs_buf {
6184 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6187 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6188 int ops, struct ll_statfs_buf *lsb)
6190 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6191 struct obd_uuid uuid_buf;
6192 char *poolname = NULL;
6193 struct ll_stat_type types[] = {
6194 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6195 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6196 { .st_name = NULL } };
6197 struct ll_stat_type *tp;
6198 __u64 ost_files = 0;
6199 __u64 ost_ffree = 0;
6207 poolname = strchr(pool, '.');
6209 if (strncmp(fsname, pool, strlen(fsname))) {
6210 fprintf(stderr, "filesystem name incorrect\n");
6218 fd = open(mntdir, O_RDONLY);
6221 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6226 if (flags & MNTDF_SHOW) {
6227 if (flags & MNTDF_INODES)
6228 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6229 "UUID", "Inodes", "IUsed", "IFree",
6230 "IUse%", "Mounted on");
6232 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6234 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6235 "Used", "Available", "Use%", "Mounted on");
6238 for (tp = types; tp->st_name != NULL; tp++) {
6239 bool have_ost = false;
6241 if (!(tp->st_op & ops))
6244 for (index = 0; ; index++) {
6245 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6246 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6247 type = flags & MNTDF_LAZY ?
6248 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6249 rc2 = llapi_obd_fstatfs(fd, type, index,
6250 &stat_buf, &uuid_buf);
6255 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6256 if (!(flags & MNTDF_VERBOSE))
6258 } else if (rc2 < 0 && rc == 0) {
6263 * If we have OSTs then don't report MDT block counts.
6264 * For MDT-only filesystems the expectation is that all
6265 * layouts have a DoM component. For filesystems with
6266 * OSTs, files are not necessarily going to store data
6267 * on MDTs, and MDT space is limited to a fraction of
6268 * OST space, so don't include it in the summary.
6270 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6277 if (poolname && tp->st_op == LL_STATFS_LOV &&
6278 llapi_search_ost(fsname, poolname,
6279 obd_uuid2str(&uuid_buf)) != 1)
6283 * the llapi_obd_fstatfs() call may have returned with
6284 * an error, but if it filled in uuid_buf we will at
6285 * lease use that to print out a message for that OBD.
6286 * If we didn't get anything in the uuid_buf, then fill
6287 * it in so that we can print an error message.
6289 if (uuid_buf.uuid[0] == '\0')
6290 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6291 "%s%04x", tp->st_name, index);
6293 lsb->sb_buf[lsb->sb_count].sd_index = index;
6294 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6297 if (flags & MNTDF_SHOW)
6298 showdf(mntdir, &stat_buf,
6299 obd_uuid2str(&uuid_buf), flags,
6300 tp->st_name, index, rc2);
6305 if (tp->st_op == LL_STATFS_LMV) {
6306 sum.os_ffree += stat_buf.os_ffree;
6307 sum.os_files += stat_buf.os_files;
6308 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6309 ost_files += stat_buf.os_files;
6310 ost_ffree += stat_buf.os_ffree;
6312 sum.os_blocks += stat_buf.os_blocks *
6314 sum.os_bfree += stat_buf.os_bfree *
6316 sum.os_bavail += stat_buf.os_bavail *
6324 * If we have _some_ OSTs, but don't have as many free objects on the
6325 * OST as inodes on the MDTs, reduce the reported number of inodes
6326 * to compensate, so that the "inodes in use" number is correct.
6327 * This should be kept in sync with ll_statfs_internal().
6329 if (ost_files && ost_ffree < sum.os_ffree) {
6330 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6331 sum.os_ffree = ost_ffree;
6333 if (flags & MNTDF_SHOW) {
6335 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6343 LAYOUT_INHERIT_UNSET = -2,
6347 static int lfs_setdirstripe(int argc, char **argv)
6350 struct lfs_setstripe_args lsa = { 0 };
6351 struct llapi_stripe_param *param = NULL;
6352 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6355 char *mode_opt = NULL;
6356 bool default_stripe = false;
6357 bool delete = false;
6358 bool foreign_mode = false;
6359 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6360 mode_t previous_mode = 0;
6362 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6363 int max_inherit = LAYOUT_INHERIT_UNSET;
6364 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6365 struct option long_opts[] = {
6366 { .val = 'c', .name = "count", .has_arg = required_argument },
6367 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6368 { .val = 'd', .name = "delete", .has_arg = no_argument },
6369 { .val = 'D', .name = "default", .has_arg = no_argument },
6370 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6371 { .val = LFS_LAYOUT_FLAGS_OPT,
6372 .name = "flags", .has_arg = required_argument },
6373 { .val = LFS_LAYOUT_FOREIGN_OPT,
6374 .name = "foreign", .has_arg = optional_argument},
6375 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6376 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6377 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6378 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6380 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6381 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6382 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6384 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6385 { .val = 'i', .name = "index", .has_arg = required_argument },
6387 { .val = 'o', .name = "mode", .has_arg = required_argument },
6388 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6389 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6391 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6392 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6393 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
6394 { .val = LFS_INHERIT_RR_OPT,
6395 .name = "max-inherit-rr", .has_arg = required_argument},
6396 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6400 setstripe_args_init(&lsa);
6402 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:X:",
6403 long_opts, NULL)) >= 0) {
6411 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6412 if (errno != 0 || *end != '\0' ||
6413 lsa.lsa_stripe_count < -1 ||
6414 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6416 "%s %s: invalid stripe count '%s'\n",
6417 progname, argv[0], optarg);
6423 default_stripe = true;
6426 default_stripe = true;
6428 case LFS_LAYOUT_FOREIGN_OPT:
6430 /* check pure numeric */
6431 type = strtoul(optarg, &end, 0);
6434 type = check_foreign_type_name(optarg);
6435 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6437 "%s %s: unknown foreign type '%s'\n",
6442 } else if (type >= UINT32_MAX) {
6444 "%s %s: invalid foreign type '%s'\n",
6445 progname, argv[0], optarg);
6449 foreign_mode = true;
6451 case LFS_LAYOUT_FLAGS_OPT:
6453 flags = strtoul(optarg, &end, 16);
6454 if (errno != 0 || *end != '\0' ||
6455 flags >= UINT32_MAX) {
6457 "%s %s: invalid hex flags '%s'\n",
6458 progname, argv[0], optarg);
6461 if (!foreign_mode) {
6463 "%s %s: hex flags must be specified with --foreign option\n",
6468 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6471 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6474 lsa.lsa_pattern = check_hashtype(optarg);
6475 if (lsa.lsa_pattern == 0) {
6477 "%s %s: bad stripe hash type '%s'\n",
6478 progname, argv[0], optarg);
6483 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6486 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6487 if (strcmp(argv[optind - 1], "--index") == 0)
6489 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6492 lsa.lsa_nr_tgts = parse_targets(mdts,
6493 sizeof(mdts) / sizeof(__u32),
6494 lsa.lsa_nr_tgts, optarg, NULL);
6495 if (lsa.lsa_nr_tgts < 0) {
6497 "%s %s: invalid MDT target(s) '%s'\n",
6498 progname, argv[0], optarg);
6502 lsa.lsa_tgts = mdts;
6503 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6504 lsa.lsa_stripe_off = mdts[0];
6506 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6509 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6518 if (!default_stripe) {
6520 "%s %s: '--max-inherit' must be specified with '-D'\n",
6525 max_inherit = strtol(optarg, &end, 10);
6526 if (errno != 0 || *end != '\0' || max_inherit < -2) {
6528 "%s %s: invalid max-inherit '%s'\n",
6529 progname, argv[0], optarg);
6532 if (max_inherit == 0) {
6533 max_inherit = LMV_INHERIT_NONE;
6534 } else if (max_inherit == -1) {
6535 max_inherit = LMV_INHERIT_UNLIMITED;
6536 } else if (max_inherit > LMV_INHERIT_MAX) {
6538 "%s %s: max-inherit %d exceeds maximum %u\n",
6539 progname, argv[0], max_inherit,
6544 case LFS_INHERIT_RR_OPT:
6545 if (!default_stripe) {
6547 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6552 max_inherit_rr = strtol(optarg, &end, 10);
6553 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6555 "%s %s: invalid max-inherit-rr '%s'\n",
6556 progname, argv[0], optarg);
6559 if (max_inherit_rr == 0) {
6560 max_inherit_rr = LMV_INHERIT_RR_NONE;
6561 } else if (max_inherit_rr == -1) {
6562 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6563 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6565 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6566 progname, argv[0], max_inherit_rr,
6567 LMV_INHERIT_RR_MAX);
6572 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6573 progname, argv[0], argv[optind - 1]);
6578 if (optind == argc) {
6579 fprintf(stderr, "%s %s: DIR must be specified\n",
6584 if (xattr && !foreign_mode) {
6586 * only print a warning as this is armless and will be
6590 "%s %s: xattr has been specified for non-foreign layout\n",
6592 } else if (foreign_mode && !xattr) {
6594 "%s %s: xattr must be provided in foreign mode\n",
6599 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6600 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6602 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6607 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6608 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6609 /* if no parameters set, create directory on least-used MDTs */
6610 lsa.lsa_stripe_off = -1;
6611 lsa.lsa_stripe_count = 1;
6615 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6616 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6618 "%s %s: cannot specify -d with -c or -i options\n",
6624 mode = strtoul(mode_opt, &end, 8);
6627 "%s %s: bad MODE '%s'\n",
6628 progname, argv[0], mode_opt);
6631 previous_mode = umask(0);
6634 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6635 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6636 lsa.lsa_stripe_off != -1) {
6638 "%s %s: max-inherit-rr is meaningless if stripe offset != -1\n",
6643 /* foreign LMV/dir case */
6645 if (argc > optind + 1) {
6647 "%s %s: cannot specify multiple foreign dirs\n",
6652 dname = argv[optind];
6653 result = llapi_dir_create_foreign(dname, mode, type, flags,
6657 "%s mkdir: can't create foreign dir '%s': %s\n",
6658 progname, dname, strerror(-result));
6663 * initialize stripe parameters, in case param is converted to specific,
6664 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6666 param = calloc(1, offsetof(typeof(*param),
6667 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6668 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6671 "%s %s: cannot allocate memory for parameters: %s\n",
6672 progname, argv[0], strerror(ENOMEM));
6676 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6677 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6678 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6679 lsa.lsa_stripe_count == 0))
6680 lsa.lsa_stripe_count = 1;
6681 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6682 param->lsp_stripe_count = lsa.lsa_stripe_count;
6683 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6684 param->lsp_stripe_offset = -1;
6686 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6687 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6688 param->lsp_stripe_pattern = lsa.lsa_pattern;
6690 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6691 param->lsp_pool = lsa.lsa_pool_name;
6692 param->lsp_is_specific = false;
6693 if (default_stripe) {
6694 if (max_inherit == LAYOUT_INHERIT_UNSET)
6695 max_inherit = LMV_INHERIT_DEFAULT;
6696 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6697 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6698 param->lsp_max_inherit = max_inherit;
6699 param->lsp_max_inherit_rr = max_inherit_rr;
6701 if (strcmp(argv[0], "mkdir") == 0)
6702 param->lsp_is_create = true;
6703 if (lsa.lsa_nr_tgts > 1) {
6704 if (lsa.lsa_stripe_count > 0 &&
6705 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6706 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6708 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6709 argv[0], lsa.lsa_stripe_count,
6715 param->lsp_is_specific = true;
6716 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6717 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6720 dname = argv[optind];
6722 if (default_stripe) {
6723 result = llapi_dir_set_default_lmv(dname, param);
6726 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6727 progname, dname, strerror(-result));
6731 result = llapi_dir_create(dname, mode, param);
6734 "%s setdirstripe: cannot create dir '%s': %s\n",
6735 progname, dname, strerror(-result));
6736 } while (!result && (dname = argv[++optind]));
6739 umask(previous_mode);
6746 static int lfs_rmentry(int argc, char **argv)
6753 fprintf(stderr, "error: %s: missing dirname\n",
6759 dname = argv[index];
6761 result = llapi_direntry_remove(dname);
6764 "error: %s: remove dir entry '%s' failed\n",
6768 dname = argv[++index];
6773 static int lfs_unlink_foreign(int argc, char **argv)
6780 fprintf(stderr, "error: %s: missing pathname\n",
6787 while (name != NULL) {
6788 result = llapi_unlink_foreign(name);
6791 "error: %s: unlink foreign entry '%s' failed\n",
6795 name = argv[++index];
6800 static int lfs_mv(int argc, char **argv)
6802 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6803 struct find_param param = {
6810 struct option long_opts[] = {
6811 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6812 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6813 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6816 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6818 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6821 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6825 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6826 if (errno != 0 || *end != '\0' ||
6827 lmu.lum_stripe_offset >= UINT32_MAX) {
6828 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6834 param.fp_verbose = VERBOSE_DETAIL;
6837 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6838 progname, argv[optind - 1]);
6843 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6844 fprintf(stderr, "%s mv: MDT index must be specified\n",
6849 if (optind >= argc) {
6850 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6854 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6856 /* initialize migrate mdt parameters */
6857 param.fp_lmv_md = &lmu;
6858 param.fp_migrate = 1;
6859 rc = llapi_migrate_mdt(argv[optind], ¶m);
6861 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6862 progname, argv[optind], lmu.lum_stripe_offset,
6867 static int lfs_osts(int argc, char **argv)
6869 return lfs_tgts(argc, argv);
6872 static int lfs_mdts(int argc, char **argv)
6874 return lfs_tgts(argc, argv);
6877 static int lfs_df(int argc, char **argv)
6879 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6880 enum mntdf_flags flags = MNTDF_SHOW;
6881 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6882 int c, rc = 0, index = 0;
6883 char fsname[PATH_MAX] = "", *pool_name = NULL;
6884 struct option long_opts[] = {
6885 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
6886 { .val = 'H', .name = "si", .has_arg = no_argument },
6887 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6888 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6889 { .val = 'p', .name = "pool", .has_arg = required_argument },
6890 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6893 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
6896 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
6899 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
6902 flags |= MNTDF_INODES;
6905 flags |= MNTDF_LAZY;
6911 flags |= MNTDF_VERBOSE;
6917 if (optind < argc && !realpath(argv[optind], path)) {
6919 fprintf(stderr, "error: invalid path '%s': %s\n",
6920 argv[optind], strerror(-rc));
6924 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6925 /* Check if we have a mount point */
6926 if (mntdir[0] == '\0')
6929 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6930 if (rc || path[0] != '\0')
6932 fsname[0] = '\0'; /* avoid matching in next loop */
6933 mntdir[0] = '\0'; /* avoid matching in next loop */
6939 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6940 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6944 if (opt_fsname == opt_instance) { /* both true or both false */
6945 rc = llapi_getname(mntdir, buf, buflen);
6946 } else if (opt_fsname) {
6948 * llapi_search_mounts() fills @buf with fsname, but that is not
6949 * called if explicit paths are specified on the command-line
6952 rc = llapi_get_fsname(mntdir, buf, buflen);
6953 } else /* if (opt_instance) */ {
6954 rc = llapi_get_instance(mntdir, buf, buflen);
6958 fprintf(stderr, "cannot get instance for '%s': %s\n",
6959 mntdir, strerror(-rc));
6964 printf("%s %s\n", buf, mntdir);
6966 printf("%s\n", buf);
6971 static int lfs_getname(int argc, char **argv)
6973 struct option long_opts[] = {
6974 { .val = 'h', .name = "help", .has_arg = no_argument },
6975 { .val = 'i', .name = "instance", .has_arg = no_argument },
6976 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6978 bool opt_instance = false, opt_fsname = false;
6979 char fsname[PATH_MAX] = "";
6982 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6985 opt_instance = true;
6996 if (optind == argc) { /* no paths specified, get all paths. */
6997 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7000 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7001 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7002 opt_instance, opt_fsname, true);
7005 path[0] = fsname[0] = mntdir[0] = '\0';
7007 } else { /* paths specified, only attempt to search these. */
7010 /* if only one path is given, print only requested info */
7011 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7013 for (; optind < argc; optind++) {
7014 rc2 = print_instance(argv[optind], fsname,
7015 sizeof(fsname), opt_instance,
7016 opt_fsname, opt_mntdir);
7026 static int lfs_check(int argc, char **argv)
7028 char mntdir[PATH_MAX] = {'\0'};
7037 fprintf(stderr, "%s check: server type must be specified\n",
7042 obd_types[0] = obd_type1;
7043 obd_types[1] = obd_type2;
7044 obd_types[2] = obd_type3;
7046 if (strcmp(argv[1], "osts") == 0) {
7047 strcpy(obd_types[0], "osc");
7048 } else if (strcmp(argv[1], "mdts") == 0 ||
7049 strcmp(argv[1], "mds") == 0) {
7050 strcpy(obd_types[0], "mdc");
7051 } else if (strcmp(argv[1], "mgts") == 0) {
7052 strcpy(obd_types[0], "mgc");
7053 } else if (strcmp(argv[1], "all") == 0 ||
7054 strcmp(argv[1], "servers") == 0) {
7056 strcpy(obd_types[0], "osc");
7057 strcpy(obd_types[1], "mdc");
7058 strcpy(obd_types[2], "mgc");
7060 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7065 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7066 if (rc < 0 || mntdir[0] == '\0') {
7068 "%s check: cannot find mounted Lustre filesystem: %s\n",
7069 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7073 rc = llapi_target_check(num_types, obd_types, mntdir);
7075 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7076 progname, argv[1], strerror(-rc));
7081 #ifdef HAVE_SYS_QUOTA_H
7082 #define ADD_OVERFLOW(a, b) \
7083 ((((a) + (b)) < (a)) ? \
7084 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7086 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7087 * returns the value or ULONG_MAX on integer overflow or incorrect format
7089 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7090 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7091 * 3. empty integer value is interpreted as 0
7093 static unsigned long str2sec(const char *timestr)
7095 const char spec[] = "smhdw";
7096 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7097 unsigned long val = 0;
7100 if (strpbrk(timestr, spec) == NULL) {
7102 * no specifiers inside the time string,
7103 * should treat it as an integer value
7105 val = strtoul(timestr, &tail, 10);
7106 return *tail ? ULONG_MAX : val;
7109 /* format string is XXwXXdXXhXXmXXs */
7115 v = strtoul(timestr, &tail, 10);
7116 if (v == ULONG_MAX || *tail == '\0')
7118 * value too large (ULONG_MAX or more)
7119 * or missing specifier
7123 ptr = strchr(spec, *tail);
7125 /* unknown specifier */
7130 /* check if product will overflow the type */
7131 if (!(v < ULONG_MAX / mult[ind]))
7134 ADD_OVERFLOW(val, mult[ind] * v);
7135 if (val == ULONG_MAX)
7147 #define ARG2ULL(nr, str, def_units) \
7149 unsigned long long limit, units = def_units; \
7152 rc = llapi_parse_size(str, &limit, &units, 1); \
7154 fprintf(stderr, "%s: invalid limit '%s'\n", \
7161 static inline int has_times_option(int argc, char **argv)
7165 for (i = 1; i < argc; i++)
7166 if (!strcmp(argv[i], "-t"))
7172 static inline int lfs_verify_poolarg(char *pool)
7174 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7176 "Pool name '%.*s' is longer than %d\n",
7177 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7183 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7186 char *mnt, *obd_type = (char *)qctl->obd_type;
7187 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7188 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7189 struct option long_opts[] = {
7190 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7191 { .val = 'g', .name = "group", .has_arg = no_argument },
7192 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7193 { .val = 'p', .name = "projid", .has_arg = no_argument },
7194 { .val = 't', .name = "times", .has_arg = no_argument },
7195 { .val = 'u', .name = "user", .has_arg = no_argument },
7196 { .val = LFS_POOL_OPT,
7197 .name = "pool", .has_arg = required_argument },
7201 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7202 qctl->qc_type = ALLQUOTA;
7204 while ((c = getopt_long(argc, argv, "b:gi:ptu",
7205 long_opts, NULL)) != -1) {
7216 if (qctl->qc_type != ALLQUOTA) {
7218 "error: -u/g/p can't be used more than once\n");
7221 qctl->qc_type = qtype;
7224 if (strncmp(optarg, NOTIFY_GRACE,
7225 strlen(NOTIFY_GRACE)) == 0) {
7226 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7228 dqi->dqi_bgrace = str2sec(optarg);
7229 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7231 "error: bad block-grace: %s\n",
7236 dqb->dqb_valid |= QIF_BTIME;
7239 if (strncmp(optarg, NOTIFY_GRACE,
7240 strlen(NOTIFY_GRACE)) == 0) {
7241 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7243 dqi->dqi_igrace = str2sec(optarg);
7244 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7246 "error: bad inode-grace: %s\n",
7251 dqb->dqb_valid |= QIF_ITIME;
7253 case 't': /* Yes, of course! */
7256 if (lfs_verify_poolarg(optarg))
7259 "Trying to set grace for pool %s\n", optarg);
7260 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7261 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7263 /* getopt prints error message for us when opterr != 0 */
7269 if (qctl->qc_type == ALLQUOTA) {
7270 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
7274 if (optind != argc - 1) {
7275 fprintf(stderr, "error: unexpected parameters encountered\n");
7280 rc = llapi_quotactl(mnt, qctl);
7283 fprintf(stderr, "%s %s ", obd_type,
7284 obd_uuid2str(&qctl->obd_uuid));
7285 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7292 #define BSLIMIT (1 << 0)
7293 #define BHLIMIT (1 << 1)
7294 #define ISLIMIT (1 << 2)
7295 #define IHLIMIT (1 << 3)
7297 int lfs_setquota(int argc, char **argv)
7300 struct if_quotactl *qctl;
7301 char *mnt, *obd_type;
7302 struct obd_dqblk *dqb;
7303 struct option long_opts[] = {
7304 { .val = 'b', .name = "block-softlimit",
7305 .has_arg = required_argument },
7306 { .val = 'B', .name = "block-hardlimit",
7307 .has_arg = required_argument },
7308 { .val = 'd', .name = "default", .has_arg = no_argument },
7309 { .val = 'g', .name = "group", .has_arg = required_argument },
7310 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7311 { .val = 'i', .name = "inode-softlimit",
7312 .has_arg = required_argument },
7313 { .val = 'I', .name = "inode-hardlimit",
7314 .has_arg = required_argument },
7315 { .val = 'p', .name = "projid", .has_arg = required_argument },
7316 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7317 { .val = 'u', .name = "user", .has_arg = required_argument },
7318 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7319 { .val = LFS_POOL_OPT,
7320 .name = "pool", .has_arg = required_argument },
7322 unsigned int limit_mask = 0;
7323 bool use_default = false;
7324 int qtype, qctl_len;
7326 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7327 qctl = malloc(qctl_len);
7331 memset(qctl, 0, qctl_len);
7332 obd_type = (char *)qctl->obd_type;
7333 dqb = &qctl->qc_dqblk;
7335 if (has_times_option(argc, argv)) {
7336 rc = lfs_setquota_times(argc, argv, qctl);
7340 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7341 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7342 * so it can be used as a marker that qc_type
7343 * isn't reinitialized from command line
7346 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7347 long_opts, NULL)) != -1) {
7350 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7353 goto quota_type_def;
7356 rc = name2uid(&qctl->qc_id, optarg);
7359 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7362 goto quota_type_def;
7365 rc = name2gid(&qctl->qc_id, optarg);
7368 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7371 goto quota_type_def;
7374 rc = name2projid(&qctl->qc_id, optarg);
7377 if (str2quotaid(&qctl->qc_id, optarg)) {
7379 "%s setquota: invalid id '%s'\n",
7386 if (qctl->qc_id == 0) {
7388 "%s setquota: can't set quota for root usr/group/project.\n",
7395 if (qctl->qc_type != ALLQUOTA) {
7397 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7402 qctl->qc_type = qtype;
7405 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7409 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7410 dqb->dqb_bsoftlimit >>= 10;
7411 limit_mask |= BSLIMIT;
7412 if (dqb->dqb_bsoftlimit &&
7413 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7415 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7417 (unsigned long long)dqb->dqb_bsoftlimit,
7421 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7422 dqb->dqb_bhardlimit >>= 10;
7423 limit_mask |= BHLIMIT;
7424 if (dqb->dqb_bhardlimit &&
7425 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7427 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7428 "See '%s help setquota' or Lustre manual for details\n",
7430 (unsigned long long)dqb->dqb_bhardlimit,
7434 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7435 limit_mask |= ISLIMIT;
7436 if (dqb->dqb_isoftlimit &&
7437 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7439 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7441 (unsigned long long)dqb->dqb_isoftlimit,
7445 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7446 limit_mask |= IHLIMIT;
7447 if (dqb->dqb_ihardlimit &&
7448 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7450 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7452 (unsigned long long)dqb->dqb_ihardlimit,
7456 if (lfs_verify_poolarg(optarg)) {
7461 "Trying to set quota for pool %s\n", optarg);
7462 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7463 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7467 "%s setquota: unrecognized option '%s'\n",
7468 progname, argv[optind - 1]);
7474 if (qctl->qc_type == ALLQUOTA) {
7476 "%s setquota: either -u or -g must be specified\n",
7482 if (!use_default && limit_mask == 0) {
7484 "%s setquota: at least one limit must be specified\n",
7490 if (use_default && limit_mask != 0) {
7492 "%s setquota: limits should not be specified when using default quota\n",
7498 if (use_default && qctl->qc_id == 0) {
7500 "%s setquota: can not set default quota for root user/group/project\n",
7506 if (optind != argc - 1) {
7508 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7509 progname, argv[optind]);
7517 dqb->dqb_bhardlimit = 0;
7518 dqb->dqb_bsoftlimit = 0;
7519 dqb->dqb_ihardlimit = 0;
7520 dqb->dqb_isoftlimit = 0;
7523 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7524 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7525 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7526 /* sigh, we can't just set blimits/ilimits */
7527 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7528 .qc_type = qctl->qc_type,
7529 .qc_id = qctl->qc_id};
7531 rc = llapi_quotactl(mnt, &tmp_qctl);
7535 if (!(limit_mask & BHLIMIT))
7536 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7537 if (!(limit_mask & BSLIMIT))
7538 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7539 if (!(limit_mask & IHLIMIT))
7540 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7541 if (!(limit_mask & ISLIMIT))
7542 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7544 /* Keep grace times if we have got no softlimit arguments */
7545 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7546 dqb->dqb_valid |= QIF_BTIME;
7547 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7550 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7551 dqb->dqb_valid |= QIF_ITIME;
7552 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7556 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7557 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7559 rc = llapi_quotactl(mnt, qctl);
7563 "%s setquota: cannot quotactl '%s' '%s': %s",
7565 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7572 /* Converts seconds value into format string
7573 * result is returned in buf
7575 * 1. result is in descenting order: 1w2d3h4m5s
7576 * 2. zero fields are not filled (except for p. 3): 5d1s
7577 * 3. zero seconds value is presented as "0s"
7579 static char *__sec2str(time_t seconds, char *buf)
7581 const char spec[] = "smhdw";
7582 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7587 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7588 c = seconds / mult[i];
7590 if (c > 0 || (i == 0 && buf == tail))
7591 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7600 static void sec2str(time_t seconds, char *buf, int rc)
7607 tail = __sec2str(seconds, tail);
7609 if (rc && tail - buf < 39) {
7615 static void diff2str(time_t seconds, char *buf, time_t now)
7620 if (seconds <= now) {
7621 strcpy(buf, "none");
7624 __sec2str(seconds - now, buf);
7627 static void print_quota_title(char *name, struct if_quotactl *qctl,
7628 bool human_readable, bool show_default)
7631 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7632 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7633 "Filesystem", "bquota", "blimit", "bgrace",
7634 "iquota", "ilimit", "igrace");
7636 printf("Disk quotas for %s %s (%cid %u):\n",
7637 qtype_name(qctl->qc_type), name,
7638 *qtype_name(qctl->qc_type), qctl->qc_id);
7639 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7640 "Filesystem", human_readable ? "used" : "kbytes",
7641 "quota", "limit", "grace",
7642 "files", "quota", "limit", "grace");
7646 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7649 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7652 snprintf(buf, buflen, "%5.4gP",
7653 (double)num / ((__u64)1 << 40));
7655 snprintf(buf, buflen, "%5.4gT",
7656 (double)num / (1 << 30));
7658 snprintf(buf, buflen, "%5.4gG",
7659 (double)num / (1 << 20));
7661 snprintf(buf, buflen, "%5.4gM",
7662 (double)num / (1 << 10));
7664 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7668 #define STRBUF_LEN 32
7669 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7670 int rc, bool h, bool show_default)
7676 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7677 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7678 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7679 int bover = 0, iover = 0;
7680 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7681 char numbuf[3][STRBUF_LEN];
7683 char strbuf[STRBUF_LEN];
7685 if (dqb->dqb_bhardlimit &&
7686 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7688 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7689 if (dqb->dqb_btime > now)
7695 if (dqb->dqb_ihardlimit &&
7696 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7698 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7699 if (dqb->dqb_itime > now)
7705 if (strlen(mnt) > 15)
7706 printf("%s\n%15s", mnt, "");
7708 printf("%15s", mnt);
7711 diff2str(dqb->dqb_btime, timebuf, now);
7712 else if (show_default)
7713 snprintf(timebuf, sizeof(timebuf), "%llu",
7714 (unsigned long long)dqb->dqb_btime);
7716 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7717 strbuf, sizeof(strbuf), h);
7718 if (rc == -EREMOTEIO)
7719 sprintf(numbuf[0], "%s*", strbuf);
7721 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7722 "%s" : "[%s]", strbuf);
7724 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7725 if (type == QC_GENERAL)
7726 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7727 "%s" : "[%s]", strbuf);
7729 sprintf(numbuf[1], "%s", "-");
7731 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7732 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7733 "%s" : "[%s]", strbuf);
7736 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7738 printf(" %7s%c %6s %7s %7s",
7739 numbuf[0], bover ? '*' : ' ', numbuf[1],
7740 numbuf[2], bover > 1 ? timebuf : "-");
7743 diff2str(dqb->dqb_itime, timebuf, now);
7744 else if (show_default)
7745 snprintf(timebuf, sizeof(timebuf), "%llu",
7746 (unsigned long long)dqb->dqb_itime);
7748 snprintf(numbuf[0], sizeof(numbuf),
7749 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7750 (uintmax_t)dqb->dqb_curinodes);
7752 if (type == QC_GENERAL)
7753 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7755 (uintmax_t)dqb->dqb_isoftlimit);
7757 sprintf(numbuf[1], "%s", "-");
7759 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7760 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7763 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7764 else if (type != QC_OSTIDX)
7765 printf(" %7s%c %6s %7s %7s",
7766 numbuf[0], iover ? '*' : ' ', numbuf[1],
7767 numbuf[2], iover > 1 ? timebuf : "-");
7769 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7771 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7772 qctl->qc_cmd == Q_GETOINFO) {
7776 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7777 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7779 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7780 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7781 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7783 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7785 printf("Block grace time: %s; Inode grace time: %s\n",
7786 bgtimebuf, igtimebuf);
7790 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7794 /* format is "lustre-OST0001" */
7795 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7797 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7802 *idx = strtoul(dash, &endp, 16);
7803 if (*idx > 0xffff) {
7804 fprintf(stderr, "wrong index %s\n", tgtname);
7811 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7812 bool h, __u64 *total)
7814 int rc = 0, rc1 = 0, count = 0, i = 0;
7815 char **list = NULL, *buffer = NULL;
7816 __u32 valid = qctl->qc_valid;
7818 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7821 /* Is it correct for the case OST0000, OST0002, OST0003 -
7822 * we will ask OST0001 that is absent and won't ask OST0003? */
7823 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7825 fprintf(stderr, "can not get %s count: %s\n",
7826 is_mdt ? "mdt" : "ost", strerror(-rc));
7830 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7831 char fname[PATH_MAX];
7832 char fsname[LUSTRE_MAXFSNAME + 1];
7833 int bufsize = sizeof(struct obd_uuid) * count;
7835 rc = llapi_search_fsname(mnt, fsname);
7837 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7841 buffer = malloc(bufsize + sizeof(*list) * count);
7844 list = (char **)(buffer + bufsize);
7845 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7846 count = llapi_get_poolmembers(fname, list, count,
7852 for (i = 0; i < count; i++) {
7853 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7856 if (tgt_name2index(list[i], &index))
7858 qctl->qc_idx = index;
7863 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7864 rc = llapi_quotactl(mnt, qctl);
7866 /* It is remote client case. */
7867 if (rc == -EOPNOTSUPP) {
7874 fprintf(stderr, "quotactl %s%d failed.\n",
7875 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7879 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7880 qctl->qc_valid, 0, h, false);
7881 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7882 qctl->qc_dqblk.dqb_bhardlimit;
7887 qctl->qc_valid = valid;
7891 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7892 int verbose, int quiet, bool human_readable,
7895 int rc1 = 0, rc2 = 0, rc3 = 0;
7896 char *obd_type = (char *)qctl->obd_type;
7897 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7898 __u64 total_ialloc = 0, total_balloc = 0;
7899 bool use_default_for_blk = false;
7900 bool use_default_for_file = false;
7903 rc1 = llapi_quotactl(mnt, qctl);
7907 fprintf(stderr, "%s quotas are not enabled.\n",
7908 qtype_name(qctl->qc_type));
7911 fprintf(stderr, "Permission denied.\n");
7914 /* We already got error message. */
7917 fprintf(stderr, "Unexpected quotactl error: %s\n",
7922 if (!show_default && qctl->qc_id == 0) {
7923 qctl->qc_dqblk.dqb_bhardlimit = 0;
7924 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7925 qctl->qc_dqblk.dqb_ihardlimit = 0;
7926 qctl->qc_dqblk.dqb_isoftlimit = 0;
7927 qctl->qc_dqblk.dqb_btime = 0;
7928 qctl->qc_dqblk.dqb_itime = 0;
7929 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7932 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7933 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7934 use_default_for_blk = true;
7935 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7938 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7939 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7940 use_default_for_file = true;
7941 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7944 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7945 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7946 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7947 print_quota_title(name, qctl, human_readable, show_default);
7949 if (rc1 && *obd_type)
7950 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7952 if (qctl->qc_valid != QC_GENERAL)
7955 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7956 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7957 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7958 (QIF_LIMITS|QIF_USAGE));
7960 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7962 if (!show_default && verbose &&
7963 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7964 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7965 char strbuf[STRBUF_LEN];
7967 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7969 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7971 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7973 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7974 (uintmax_t)total_ialloc, strbuf);
7977 if (use_default_for_blk)
7978 printf("%cid %u is using default block quota setting\n",
7979 *qtype_name(qctl->qc_type), qctl->qc_id);
7981 if (use_default_for_file)
7982 printf("%cid %u is using default file quota setting\n",
7983 *qtype_name(qctl->qc_type), qctl->qc_id);
7985 if (rc1 || rc2 || rc3 || inacc)
7986 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8000 static int lfs_project(int argc, char **argv)
8002 int ret = 0, err = 0, c, i;
8003 struct project_handle_control phc = { 0 };
8004 enum lfs_project_ops_t op;
8007 phc.assign_projid = false;
8008 /* default action */
8009 op = LFS_PROJECT_LIST;
8011 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8014 if (op != LFS_PROJECT_LIST) {
8016 "%s: cannot specify '-c' '-C' '-s' together\n",
8021 op = LFS_PROJECT_CHECK;
8024 if (op != LFS_PROJECT_LIST) {
8026 "%s: cannot specify '-c' '-C' '-s' together\n",
8031 op = LFS_PROJECT_CLEAR;
8034 if (op != LFS_PROJECT_LIST) {
8036 "%s: cannot specify '-c' '-C' '-s' together\n",
8041 phc.set_inherit = true;
8042 op = LFS_PROJECT_SET;
8048 phc.keep_projid = true;
8051 phc.recursive = true;
8054 if (str2quotaid(&phc.projid, optarg)) {
8056 "Invalid project ID: %s\n",
8061 phc.assign_projid = true;
8065 phc.newline = false;
8068 fprintf(stderr, "%s: invalid option '%c'\n",
8074 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8075 op = LFS_PROJECT_SET;
8076 phc.set_projid = true;
8077 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8078 phc.set_projid = true;
8082 case LFS_PROJECT_CHECK:
8083 if (phc.keep_projid) {
8085 "%s: '-k' is useless together with '-c'\n",
8090 case LFS_PROJECT_CLEAR:
8093 "%s: '-0' is useless together with '-C'\n",
8097 if (phc.assign_projid) {
8099 "%s: '-p' is useless together with '-C'\n",
8104 case LFS_PROJECT_SET:
8107 "%s: '-0' is useless together with '-s'\n",
8111 if (phc.keep_projid) {
8113 "%s: '-k' is useless together with '-s'\n",
8121 "%s: '-0' is useless for list operations\n",
8131 fprintf(stderr, "%s: missing file or directory target(s)\n",
8136 for (i = 0; i < argc; i++) {
8138 case LFS_PROJECT_CHECK:
8139 err = lfs_project_check(argv[i], &phc);
8141 case LFS_PROJECT_LIST:
8142 err = lfs_project_list(argv[i], &phc);
8144 case LFS_PROJECT_CLEAR:
8145 err = lfs_project_clear(argv[i], &phc);
8147 case LFS_PROJECT_SET:
8148 err = lfs_project_set(argv[i], &phc);
8160 static int lfs_quota(int argc, char **argv)
8163 char *mnt, *name = NULL;
8164 struct if_quotactl *qctl;
8166 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8167 __u32 valid = QC_GENERAL, idx = 0;
8168 bool human_readable = false;
8169 bool show_default = false;
8171 struct option long_opts[] = {
8172 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
8175 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8179 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8180 qctl->qc_type = ALLQUOTA;
8181 obd_uuid = (char *)qctl->obd_uuid.uuid;
8183 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8184 long_opts, NULL)) != -1) {
8187 show_default = true;
8192 show_default = true;
8197 show_default = true;
8201 if (qctl->qc_type != ALLQUOTA) {
8203 "%s quota: only one of -u, -g, or -p may be specified\n",
8208 qctl->qc_type = qtype;
8211 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8214 valid = qctl->qc_valid = QC_UUID;
8215 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8218 valid = qctl->qc_valid = QC_MDTIDX;
8219 idx = qctl->qc_idx = atoi(optarg);
8220 if (idx == 0 && *optarg != '0') {
8222 "%s quota: invalid MDT index '%s'\n",
8229 valid = qctl->qc_valid = QC_OSTIDX;
8230 idx = qctl->qc_idx = atoi(optarg);
8231 if (idx == 0 && *optarg != '0') {
8233 "%s quota: invalid OST index '%s'\n",
8246 human_readable = true;
8249 if (lfs_verify_poolarg(optarg)) {
8253 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8254 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
8255 LUSTRE_Q_GETINFOPOOL :
8256 LUSTRE_Q_GETQUOTAPOOL;
8259 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8260 progname, argv[optind - 1]);
8266 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8267 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8268 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8269 qctl->qc_type == ALLQUOTA &&
8270 optind == argc - 1 && !show_default) {
8273 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8274 qctl->qc_type = qtype;
8275 qctl->qc_valid = valid;
8276 if (qtype == USRQUOTA) {
8277 qctl->qc_id = geteuid();
8278 rc = uid2name(&name, qctl->qc_id);
8280 qctl->qc_id = getegid();
8281 rc = gid2name(&name, qctl->qc_id);
8282 memset(&qctl->qc_dqblk, 0,
8283 sizeof(qctl->qc_dqblk));
8288 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8289 human_readable, show_default);
8294 /* lfs quota -u username /path/to/lustre/mount */
8295 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8296 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8297 /* options should be followed by u/g-name and mntpoint */
8298 if ((!show_default && optind + 2 != argc) ||
8299 (show_default && optind + 1 != argc) ||
8300 qctl->qc_type == ALLQUOTA) {
8302 "%s quota: name and mount point must be specified\n",
8308 if (!show_default) {
8309 name = argv[optind++];
8310 switch (qctl->qc_type) {
8312 rc = name2uid(&qctl->qc_id, name);
8315 rc = name2gid(&qctl->qc_id, name);
8318 rc = name2projid(&qctl->qc_id, name);
8325 qctl->qc_valid = QC_GENERAL;
8326 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
8331 if (str2quotaid(&qctl->qc_id, name)) {
8332 fprintf(stderr, "%s quota: invalid id '%s'\n",
8338 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8339 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8346 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8347 human_readable, show_default);
8352 #endif /* HAVE_SYS_QUOTA_H! */
8354 static int flushctx_ioctl(char *mp)
8358 fd = open(mp, O_RDONLY);
8360 fprintf(stderr, "flushctx: error open %s: %s\n",
8361 mp, strerror(errno));
8365 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8367 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8368 mp, strerror(errno));
8374 static int lfs_flushctx(int argc, char **argv)
8376 int kdestroy = 0, reap = 0, c;
8377 char mntdir[PATH_MAX] = {'\0'};
8381 while ((c = getopt(argc, argv, "kr")) != -1) {
8391 "error: %s: option '-%c' unrecognized\n",
8398 rc = system("kdestroy > /dev/null");
8400 rc = WEXITSTATUS(rc);
8402 "error destroying tickets: %d, continuing\n",
8407 if (optind >= argc) {
8408 /* flush for all mounted lustre fs. */
8409 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8410 /* Check if we have a mount point */
8411 if (mntdir[0] == '\0')
8414 if (flushctx_ioctl(mntdir))
8417 mntdir[0] = '\0'; /* avoid matching in next loop */
8420 /* flush fs as specified */
8421 while (optind < argc) {
8422 if (flushctx_ioctl(argv[optind++]))
8428 rc = system("keyctl reap > /dev/null");
8430 rc = WEXITSTATUS(rc);
8431 fprintf(stderr, "error reaping keyring: %d\n", rc);
8438 static int lfs_changelog(int argc, char **argv)
8440 void *changelog_priv;
8441 struct changelog_rec *rec;
8442 long long startrec = 0, endrec = 0;
8444 struct option long_opts[] = {
8445 { .val = 'f', .name = "follow", .has_arg = no_argument },
8447 char short_opts[] = "f";
8450 while ((rc = getopt_long(argc, argv, short_opts,
8451 long_opts, NULL)) != -1) {
8458 "%s changelog: unrecognized option '%s'\n",
8459 progname, argv[optind - 1]);
8463 if (optind >= argc) {
8464 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8469 mdd = argv[optind++];
8470 if (argc > optind) {
8472 startrec = strtoll(argv[optind++], NULL, 10);
8473 if (errno != 0 || startrec < 0) {
8475 "%s changelog: bad startrec\n",
8481 if (argc > optind) {
8483 endrec = strtoll(argv[optind++], NULL, 10);
8484 if (errno != 0 || endrec < 0) {
8486 "%s changelog: bad endrec\n",
8492 rc = llapi_changelog_start(&changelog_priv,
8493 CHANGELOG_FLAG_BLOCK |
8494 CHANGELOG_FLAG_JOBID |
8495 CHANGELOG_FLAG_EXTRA_FLAGS |
8496 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8499 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8500 progname, strerror(errno = -rc));
8504 rc = llapi_changelog_set_xflags(changelog_priv,
8505 CHANGELOG_EXTRA_FLAG_UIDGID |
8506 CHANGELOG_EXTRA_FLAG_NID |
8507 CHANGELOG_EXTRA_FLAG_OMODE |
8508 CHANGELOG_EXTRA_FLAG_XATTR);
8511 "%s changelog: cannot set xflags for changelog: %s\n",
8512 progname, strerror(errno = -rc));
8516 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8520 if (endrec && rec->cr_index > endrec) {
8521 llapi_changelog_free(&rec);
8524 if (rec->cr_index < startrec) {
8525 llapi_changelog_free(&rec);
8529 secs = rec->cr_time >> 30;
8530 gmtime_r(&secs, &ts);
8531 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8532 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8533 changelog_type2str(rec->cr_type),
8534 ts.tm_hour, ts.tm_min, ts.tm_sec,
8535 (int)(rec->cr_time & ((1 << 30) - 1)),
8536 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8537 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8539 if (rec->cr_flags & CLF_JOBID) {
8540 struct changelog_ext_jobid *jid =
8541 changelog_rec_jobid(rec);
8543 if (jid->cr_jobid[0] != '\0')
8544 printf(" j=%s", jid->cr_jobid);
8547 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8548 struct changelog_ext_extra_flags *ef =
8549 changelog_rec_extra_flags(rec);
8551 printf(" ef=0x%llx",
8552 (unsigned long long)ef->cr_extra_flags);
8554 if (ef->cr_extra_flags & CLFE_UIDGID) {
8555 struct changelog_ext_uidgid *uidgid =
8556 changelog_rec_uidgid(rec);
8558 printf(" u=%llu:%llu",
8559 (unsigned long long)uidgid->cr_uid,
8560 (unsigned long long)uidgid->cr_gid);
8562 if (ef->cr_extra_flags & CLFE_NID) {
8563 struct changelog_ext_nid *nid =
8564 changelog_rec_nid(rec);
8567 libcfs_nid2str(nid->cr_nid));
8570 if (ef->cr_extra_flags & CLFE_OPEN) {
8571 struct changelog_ext_openmode *omd =
8572 changelog_rec_openmode(rec);
8573 char mode[] = "---";
8575 /* exec mode must be exclusive */
8576 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8579 if (omd->cr_openflags & MDS_FMODE_READ)
8581 if (omd->cr_openflags &
8588 if (strcmp(mode, "---") != 0)
8589 printf(" m=%s", mode);
8592 if (ef->cr_extra_flags & CLFE_XATTR) {
8593 struct changelog_ext_xattr *xattr =
8594 changelog_rec_xattr(rec);
8596 if (xattr->cr_xattr[0] != '\0')
8597 printf(" x=%s", xattr->cr_xattr);
8601 if (!fid_is_zero(&rec->cr_pfid))
8602 printf(" p="DFID, PFID(&rec->cr_pfid));
8603 if (rec->cr_namelen)
8604 printf(" %.*s", rec->cr_namelen,
8605 changelog_rec_name(rec));
8607 if (rec->cr_flags & CLF_RENAME) {
8608 struct changelog_ext_rename *rnm =
8609 changelog_rec_rename(rec);
8611 if (!fid_is_zero(&rnm->cr_sfid))
8612 printf(" s="DFID" sp="DFID" %.*s",
8613 PFID(&rnm->cr_sfid),
8614 PFID(&rnm->cr_spfid),
8615 (int)changelog_rec_snamelen(rec),
8616 changelog_rec_sname(rec));
8620 llapi_changelog_free(&rec);
8623 llapi_changelog_fini(&changelog_priv);
8626 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8627 progname, strerror(errno = -rc));
8629 return (rc == 1 ? 0 : rc);
8632 static int lfs_changelog_clear(int argc, char **argv)
8641 endrec = strtoll(argv[3], NULL, 10);
8642 if (errno != 0 || endrec < 0) {
8644 "%s: bad endrec '%s'\n",
8649 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8652 fprintf(stderr, "%s: record out of range: %llu\n",
8654 else if (rc == -ENOENT)
8655 fprintf(stderr, "%s: no changelog user: %s\n",
8658 fprintf(stderr, "%s error: %s\n", argv[0],
8667 static void rstripc(char *str, int c)
8669 char *end = str + strlen(str);
8671 for (; str < end && end[-1] == c; --end)
8675 static int lfs_fid2path(int argc, char **argv)
8677 struct option long_opts[] = {
8678 { .val = 'c', .name = "cur", .has_arg = no_argument },
8679 { .val = 'c', .name = "current", .has_arg = no_argument },
8680 { .val = 'c', .name = "print-link", .has_arg = no_argument },
8681 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
8682 { .val = 'l', .name = "link", .has_arg = required_argument },
8684 char short_opts[] = "cfl:pr:";
8685 bool print_link = false;
8686 bool print_fid = false;
8688 char mnt_dir[PATH_MAX] = "";
8690 char *path_or_fsname;
8691 long long recno = -1;
8693 char *endptr = NULL;
8698 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
8708 linkno = strtol(optarg, &endptr, 10);
8709 if (errno != 0 || *endptr != '\0' || linkno < 0) {
8711 "%s fid2path: invalid linkno '%s'\n",
8717 /* recno is something to do with changelogs
8718 * that was never implemented. We just pass it
8719 * through for the MDT to ignore.
8722 recno = strtoll(optarg, &endptr, 10);
8723 if (errno != 0 || *endptr != '\0' || recno < 0) {
8725 "%s fid2path: invalid recno '%s'\n",
8732 "%s fid2path: unrecognized option '%s'\n",
8733 progname, argv[optind - 1]);
8738 if (argc - optind < 2) {
8740 "Usage: %s fid2path FSNAME|ROOT FID...\n",
8745 path_or_fsname = argv[optind];
8747 if (*path_or_fsname == '/') {
8748 print_mnt_dir = true;
8749 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
8751 print_mnt_dir = false;
8752 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
8757 "%s fid2path: cannot resolve mount point for '%s': %s\n",
8758 progname, path_or_fsname, strerror(-rc));
8762 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
8765 "%s fid2path: cannot open mount point for '%s': %s\n",
8766 progname, path_or_fsname, strerror(-rc));
8770 /* Strip trailing slashes from mnt_dir. */
8771 rstripc(mnt_dir + 1, '/');
8773 for (i = optind + 1; i < argc; i++) {
8774 const char *fid_str = argv[i];
8778 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
8781 "%s fid2path: invalid FID '%s'\n",
8789 int linktmp = (linkno >= 0) ? linkno : 0;
8791 int oldtmp = linktmp;
8792 long long rectmp = recno;
8793 char path_buf[PATH_MAX];
8795 rc2 = llapi_fid2path_at(mnt_fd, &fid,
8796 path_buf, sizeof(path_buf), &rectmp, &linktmp);
8799 "%s fid2path: cannot find %s %s: %s\n",
8800 progname, path_or_fsname, fid_str,
8808 printf("%s ", fid_str);
8811 printf("%d ", linktmp);
8813 /* You may think this looks wrong or weird (and it is!)
8814 * but we are actually trying to preserve the old quirky
8815 * behaviors (enforced by our old quirky tests!) that
8816 * make lfs so much fun to work on:
8818 * lustre 0x200000007:0x1:0x0 => "/"
8819 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
8821 * Note that llapi_fid2path() returns "" for the root
8825 print_mnt_dir ? mnt_dir : "",
8826 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
8830 /* specified linkno */
8833 if (oldtmp == linktmp)
8845 static int lfs_path2fid(int argc, char **argv)
8847 struct option long_opts[] = {
8848 { .val = 'p', .name = "parents", .has_arg = no_argument },
8851 const char short_opts[] = "p";
8852 const char *sep = "";
8855 bool show_parents = false;
8857 while ((rc = getopt_long(argc, argv, short_opts,
8858 long_opts, NULL)) != -1) {
8861 show_parents = true;
8865 "%s path2fid: unrecognized option '%s'\n",
8866 progname, argv[optind - 1]);
8871 if (optind > argc - 1) {
8872 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8875 } else if (optind < argc - 1) {
8880 for (path = argv + optind; *path != NULL; path++) {
8883 if (!show_parents) {
8884 err = llapi_path2fid(*path, &fid);
8886 printf("%s%s"DFID"\n",
8887 *sep != '\0' ? *path : "", sep,
8890 char name[NAME_MAX + 1];
8891 unsigned int linkno = 0;
8893 while ((err = llapi_path2parent(*path, linkno, &fid,
8894 name, sizeof(name))) == 0) {
8895 if (*sep != '\0' && linkno == 0)
8896 printf("%s%s", *path, sep);
8898 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8903 /* err == -ENODATA is end-of-loop */
8904 if (linkno > 0 && err == -ENODATA) {
8912 "%s path2fid: cannot get %sfid for '%s': %s\n",
8913 progname, show_parents ? "parent " : "", *path,
8925 #define MAX_ERRNO 4095
8926 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8928 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8932 rc = llapi_rmfid(device, fa);
8934 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
8935 progname, strerror(-rc));
8939 for (k = 0; k < fa->fa_nr; k++) {
8940 rc2 = (__s32)fa->fa_fids[k].f_ver;
8941 if (!IS_ERR_VALUE(rc2))
8947 fa->fa_fids[k].f_ver = 0;
8948 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
8949 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
8955 static int lfs_rmfid(int argc, char **argv)
8957 char *fidstr, *device;
8958 int rc = 0, rc2, nr;
8959 struct fid_array *fa;
8961 if (optind > argc - 1) {
8962 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8966 device = argv[optind++];
8969 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8975 while (optind < argc) {
8978 fidstr = argv[optind++];
8979 while (*fidstr == '[')
8981 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8983 fprintf(stderr, "unrecognized FID: %s\n",
8988 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8989 /* start another batch */
8990 rc2 = lfs_rmfid_and_show_errors(device, fa);
8997 rc2 = lfs_rmfid_and_show_errors(device, fa);
9005 static int lfs_data_version(int argc, char **argv)
9012 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9015 fprintf(stderr, "%s data_version: FILE must be specified\n",
9020 while ((c = getopt(argc, argv, "nrw")) != -1) {
9023 data_version_flags = 0;
9026 data_version_flags |= LL_DV_RD_FLUSH;
9029 data_version_flags |= LL_DV_WR_FLUSH;
9033 "%s data_version: unrecognized option '%s'\n",
9034 progname, argv[optind - 1]);
9038 if (optind == argc) {
9039 fprintf(stderr, "%s data_version: FILE must be specified\n",
9044 path = argv[optind];
9045 fd = open(path, O_RDONLY);
9048 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9049 progname, path, strerror(-rc));
9053 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9056 "%s data_version: cannot get version for '%s': %s\n",
9057 progname, path, strerror(-rc));
9059 printf("%ju" "\n", (uintmax_t)data_version);
9065 static int lfs_hsm_state(int argc, char **argv)
9070 struct hsm_user_state hus;
9078 rc = llapi_hsm_state_get(path, &hus);
9080 fprintf(stderr, "can't get hsm state for %s: %s\n",
9081 path, strerror(errno = -rc));
9085 /* Display path name and status flags */
9086 printf("%s: (0x%08x)", path, hus.hus_states);
9088 if (hus.hus_states & HS_RELEASED)
9089 printf(" released");
9090 if (hus.hus_states & HS_EXISTS)
9092 if (hus.hus_states & HS_DIRTY)
9094 if (hus.hus_states & HS_ARCHIVED)
9095 printf(" archived");
9096 /* Display user-settable flags */
9097 if (hus.hus_states & HS_NORELEASE)
9098 printf(" never_release");
9099 if (hus.hus_states & HS_NOARCHIVE)
9100 printf(" never_archive");
9101 if (hus.hus_states & HS_LOST)
9102 printf(" lost_from_hsm");
9104 if (hus.hus_archive_id != 0)
9105 printf(", archive_id:%d", hus.hus_archive_id);
9108 } while (++i < argc);
9113 #define LFS_HSM_SET 0
9114 #define LFS_HSM_CLEAR 1
9117 * Generic function to set or clear HSM flags.
9118 * Used by hsm_set and hsm_clear.
9120 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9122 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9124 struct option long_opts[] = {
9125 { .val = 'A', .name = "archived", .has_arg = no_argument },
9126 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
9127 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9128 { .val = 'e', .name = "exists", .has_arg = no_argument },
9129 { .val = 'l', .name = "lost", .has_arg = no_argument },
9130 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9131 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9133 char short_opts[] = "lraAdei:";
9137 __u32 archive_id = 0;
9143 while ((c = getopt_long(argc, argv, short_opts,
9144 long_opts, NULL)) != -1) {
9150 mask |= HS_NOARCHIVE;
9153 mask |= HS_ARCHIVED;
9156 mask |= HS_NORELEASE;
9166 archive_id = strtol(optarg, &end, 10);
9167 if (errno != 0 || *end != '\0' || archive_id < 0) {
9168 fprintf(stderr, "invalid archive_id: '%s'\n",
9176 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9177 argv[0], argv[optind - 1]);
9182 /* User should have specified a flag */
9186 while (optind < argc) {
9187 path = argv[optind];
9189 /* If mode == 0, this means we apply the mask. */
9190 if (mode == LFS_HSM_SET)
9191 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9193 rc = llapi_hsm_state_set(path, 0, mask, 0);
9196 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9197 path, strerror(errno = -rc));
9206 static int lfs_hsm_action(int argc, char **argv)
9211 struct hsm_current_action hca;
9212 struct hsm_extent he;
9213 enum hsm_user_action hua;
9214 enum hsm_progress_states hps;
9222 rc = llapi_hsm_current_action(path, &hca);
9224 fprintf(stderr, "can't get hsm action for %s: %s\n",
9225 path, strerror(errno = -rc));
9228 he = hca.hca_location;
9229 hua = hca.hca_action;
9230 hps = hca.hca_state;
9232 printf("%s: %s", path, hsm_user_action2name(hua));
9234 /* Skip file without action */
9235 if (hca.hca_action == HUA_NONE) {
9240 printf(" %s ", hsm_progress_state2name(hps));
9242 if ((hps == HPS_RUNNING) &&
9243 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9244 printf("(%llu bytes moved)\n",
9245 (unsigned long long)he.length);
9246 else if ((he.offset + he.length) == LUSTRE_EOF)
9247 printf("(from %llu to EOF)\n",
9248 (unsigned long long)he.offset);
9250 printf("(from %llu to %llu)\n",
9251 (unsigned long long)he.offset,
9252 (unsigned long long)(he.offset + he.length));
9254 } while (++i < argc);
9259 static int lfs_hsm_set(int argc, char **argv)
9261 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9264 static int lfs_hsm_clear(int argc, char **argv)
9266 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9270 * Check file state and return its fid, to be used by lfs_hsm_request().
9272 * \param[in] file Path to file to check
9273 * \param[in,out] fid Pointer to allocated lu_fid struct.
9274 * \param[in,out] last_dev Pointer to last device id used.
9276 * \return 0 on success.
9278 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9284 rc = lstat(file, &st);
9286 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9290 * Checking for regular file as archiving as posix copytool
9291 * rejects archiving files other than regular files
9293 if (!S_ISREG(st.st_mode)) {
9294 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9297 /* A request should be ... */
9298 if (*last_dev != st.st_dev && *last_dev != 0) {
9300 "All files should be on the same filesystem: %s\n",
9304 *last_dev = st.st_dev;
9306 rc = llapi_path2fid(file, fid);
9308 fprintf(stderr, "Cannot read FID of %s: %s\n",
9309 file, strerror(-rc));
9315 /* Fill an HSM HUR item with a given file name.
9317 * If mntpath is set, then the filename is actually a FID, and no
9318 * lookup on the filesystem will be performed.
9320 * \param[in] hur the user request to fill
9321 * \param[in] idx index of the item inside the HUR to fill
9322 * \param[in] mntpath mountpoint of Lustre
9323 * \param[in] fname filename (if mtnpath is NULL)
9324 * or FID (if mntpath is set)
9325 * \param[in] last_dev pointer to last device id used
9327 * \retval 0 on success
9328 * \retval CMD_HELP or a negative errno on error
9330 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9331 const char *mntpath, const char *fname,
9334 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9337 hui->hui_extent.length = -1;
9340 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9342 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9345 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9349 hur->hur_request.hr_itemcount++;
9354 static int lfs_hsm_request(int argc, char **argv, int action)
9356 struct option long_opts[] = {
9357 { .val = 'a', .name = "archive", .has_arg = required_argument },
9358 { .val = 'D', .name = "data", .has_arg = required_argument },
9359 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9360 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9363 char short_opts[] = "l:D:a:m:";
9364 struct hsm_user_request *hur, *oldhur;
9369 char *filelist = NULL;
9370 char fullpath[PATH_MAX];
9371 char *opaque = NULL;
9375 int nbfile_alloc = 0;
9376 char *some_file = NULL;
9377 char *mntpath = NULL;
9383 while ((c = getopt_long(argc, argv, short_opts,
9384 long_opts, NULL)) != -1) {
9393 if (action != HUA_ARCHIVE &&
9394 action != HUA_REMOVE) {
9396 "error: -a is supported only when archiving or removing\n");
9399 archive_id = atoi(optarg);
9404 some_file = strdup(optarg);
9410 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9411 argv[0], argv[optind - 1]);
9416 /* All remaining args are files, so we have at least nbfile */
9417 nbfile = argc - optind;
9419 if ((nbfile == 0) && (!filelist))
9423 opaque_len = strlen(opaque);
9426 * Alloc the request structure with enough place to store all files
9427 * from command line.
9429 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9431 fprintf(stderr, "Cannot create the request: %s\n",
9435 nbfile_alloc = nbfile;
9437 hur->hur_request.hr_action = action;
9438 hur->hur_request.hr_archive_id = archive_id;
9439 hur->hur_request.hr_flags = 0;
9441 /* All remaining args are files, add them */
9442 if (nbfile != 0 && some_file == NULL)
9443 some_file = strdup(argv[optind]);
9445 for (i = 0; i < nbfile; i++) {
9446 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9452 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9454 /* If a filelist was specified, read the filelist from it. */
9456 fp = fopen(filelist, "r");
9458 fprintf(stderr, "Cannot read the file list %s: %s\n",
9459 filelist, strerror(errno));
9464 while ((rc = getline(&line, &len, fp)) != -1) {
9466 * If allocated buffer was too small, get something
9469 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9472 nbfile_alloc = nbfile_alloc * 2 + 1;
9474 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9478 "hsm: cannot allocate the request: %s\n",
9485 size = hur_len(oldhur);
9488 "hsm: cannot allocate %u files + %u bytes data\n",
9489 oldhur->hur_request.hr_itemcount,
9490 oldhur->hur_request.hr_data_len);
9497 memcpy(hur, oldhur, size);
9502 if (line[strlen(line) - 1] == '\n')
9503 line[strlen(line) - 1] = '\0';
9505 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9506 mntpath, line, &last_dev);
9522 /* If a --data was used, add it to the request */
9523 hur->hur_request.hr_data_len = opaque_len;
9525 memcpy(hur_data(hur), opaque, opaque_len);
9527 /* Send the HSM request */
9528 if (realpath(some_file, fullpath) == NULL) {
9529 fprintf(stderr, "Could not find path '%s': %s\n",
9530 some_file, strerror(errno));
9532 rc = llapi_hsm_request(fullpath, hur);
9534 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9535 some_file, strerror(-rc));
9545 static int lfs_hsm_archive(int argc, char **argv)
9547 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9550 static int lfs_hsm_restore(int argc, char **argv)
9552 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9555 static int lfs_hsm_release(int argc, char **argv)
9557 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9560 static int lfs_hsm_remove(int argc, char **argv)
9562 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9565 static int lfs_hsm_cancel(int argc, char **argv)
9567 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9570 static int lfs_swap_layouts(int argc, char **argv)
9575 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9576 SWAP_LAYOUTS_KEEP_MTIME |
9577 SWAP_LAYOUTS_KEEP_ATIME);
9580 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9582 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9584 int lfs_get_mode(const char *string)
9586 enum lock_mode_user mode;
9588 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9589 if (lock_mode_names[mode] == NULL)
9591 if (strcmp(string, lock_mode_names[mode]) == 0)
9598 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9600 enum lu_ladvise_type advice;
9603 advice < ARRAY_SIZE(ladvise_names); advice++) {
9604 if (ladvise_names[advice] == NULL)
9606 if (strcmp(string, ladvise_names[advice]) == 0)
9610 return LU_LADVISE_INVALID;
9613 static int lfs_ladvise(int argc, char **argv)
9615 struct option long_opts[] = {
9616 { .val = 'a', .name = "advice", .has_arg = required_argument },
9617 { .val = 'b', .name = "background", .has_arg = no_argument },
9618 { .val = 'e', .name = "end", .has_arg = required_argument },
9619 { .val = 'l', .name = "length", .has_arg = required_argument },
9620 { .val = 'm', .name = "mode", .has_arg = required_argument },
9621 { .val = 's', .name = "start", .has_arg = required_argument },
9622 { .val = 'u', .name = "unset", .has_arg = no_argument },
9624 char short_opts[] = "a:be:l:m:s:u";
9629 struct llapi_lu_ladvise advice;
9630 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9631 unsigned long long start = 0;
9632 unsigned long long end = LUSTRE_EOF;
9633 unsigned long long length = 0;
9634 unsigned long long size_units;
9635 unsigned long long flags = 0;
9639 while ((c = getopt_long(argc, argv, short_opts,
9640 long_opts, NULL)) != -1) {
9643 advice_type = lfs_get_ladvice(optarg);
9644 if (advice_type == LU_LADVISE_INVALID) {
9646 "%s: invalid advice type '%s'\n",
9648 fprintf(stderr, "Valid types:");
9650 for (advice_type = 0;
9651 advice_type < ARRAY_SIZE(ladvise_names);
9653 if (ladvise_names[advice_type] == NULL)
9655 fprintf(stderr, " %s",
9656 ladvise_names[advice_type]);
9658 fprintf(stderr, "\n");
9671 rc = llapi_parse_size(optarg, &end,
9674 fprintf(stderr, "%s: bad end offset '%s'\n",
9681 rc = llapi_parse_size(optarg, &start,
9685 "%s: bad start offset '%s'\n",
9692 rc = llapi_parse_size(optarg, &length,
9695 fprintf(stderr, "%s: bad length '%s'\n",
9701 mode = lfs_get_mode(optarg);
9704 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9712 fprintf(stderr, "%s: option '%s' unrecognized\n",
9713 argv[0], argv[optind - 1]);
9718 if (advice_type == LU_LADVISE_INVALID) {
9719 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9720 fprintf(stderr, "Valid types:");
9721 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9723 if (ladvise_names[advice_type] == NULL)
9725 fprintf(stderr, " %s", ladvise_names[advice_type]);
9727 fprintf(stderr, "\n");
9731 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9733 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9738 if (argc <= optind) {
9739 fprintf(stderr, "%s: please give one or more file names\n",
9744 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9745 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9750 if (end == LUSTRE_EOF && length != 0)
9751 end = start + length;
9754 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9755 argv[0], start, end);
9759 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9760 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9765 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9766 fprintf(stderr, "%s: mode is required with lockahead\n",
9771 while (optind < argc) {
9774 path = argv[optind++];
9776 fd = open(path, O_RDONLY);
9778 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9779 argv[0], path, strerror(errno));
9784 advice.lla_start = start;
9785 advice.lla_end = end;
9786 advice.lla_advice = advice_type;
9787 advice.lla_value1 = 0;
9788 advice.lla_value2 = 0;
9789 advice.lla_value3 = 0;
9790 advice.lla_value4 = 0;
9791 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9792 advice.lla_lockahead_mode = mode;
9793 advice.lla_peradvice_flags = flags;
9796 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9800 "%s: cannot give advice '%s' to file '%s': %s\n",
9801 argv[0], ladvise_names[advice_type],
9802 path, strerror(errno));
9808 if (rc == 0 && rc2 < 0)
9814 static const char *const heat_names[] = LU_HEAT_NAMES;
9816 static int lfs_heat_get(int argc, char **argv)
9818 struct lu_heat *heat;
9827 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9829 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9834 while (optind < argc) {
9835 path = argv[optind++];
9837 fd = open(path, O_RDONLY);
9839 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9840 argv[0], path, strerror(errno));
9845 heat->lh_count = OBD_HEAT_COUNT;
9846 rc2 = llapi_heat_get(fd, heat);
9850 "%s: cannot get heat of file '%s': %s\n",
9851 argv[0], path, strerror(errno));
9855 printf("flags: %x\n", heat->lh_flags);
9856 for (i = 0; i < heat->lh_count; i++)
9857 printf("%s: %llu\n", heat_names[i],
9858 (unsigned long long)heat->lh_heat[i]);
9860 if (rc == 0 && rc2 < 0)
9868 static int lfs_heat_set(int argc, char **argv)
9870 struct option long_opts[] = {
9871 { .val = 'c', .name = "clear", .has_arg = no_argument },
9872 { .val = 'o', .name = "off", .has_arg = no_argument },
9873 { .val = 'O', .name = "on", .has_arg = no_argument },
9875 enum lu_heat_flag flags = 0;
9885 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9888 flags |= LU_HEAT_FLAG_CLEAR;
9891 flags |= LU_HEAT_FLAG_CLEAR;
9892 flags |= LU_HEAT_FLAG_OFF;
9895 flags &= ~LU_HEAT_FLAG_OFF;
9900 fprintf(stderr, "%s: option '%s' unrecognized\n",
9901 argv[0], argv[optind - 1]);
9906 if (argc <= optind) {
9907 fprintf(stderr, "%s: please give one or more file names\n",
9912 while (optind < argc) {
9913 path = argv[optind++];
9915 fd = open(path, O_RDONLY);
9917 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9918 argv[0], path, strerror(errno));
9923 rc2 = llapi_heat_set(fd, flags);
9927 "%s: cannot setflags heat of file '%s': %s\n",
9928 argv[0], path, strerror(errno));
9932 if (rc == 0 && rc2 < 0)
9939 * The input string contains a comma delimited list of component ids and
9940 * ranges, for example "1,2-4,7".
9942 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9944 bool end_of_loop = false;
9952 while (!end_of_loop) {
9956 char *endptr = NULL;
9959 ptr = strchrnul(arg, ',');
9960 end_of_loop = *ptr == '\0';
9963 start_index = strtol(arg, &endptr, 0);
9964 if (endptr == arg) /* no data at all */
9966 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9968 if (start_index < 0)
9971 end_index = start_index;
9972 if (*endptr == '-') {
9973 end_index = strtol(endptr + 1, &endptr, 0);
9974 if (*endptr != '\0')
9976 if (end_index < start_index)
9980 for (i = start_index; i <= end_index && size > 0; i++) {
9983 /* remove duplicate */
9984 for (j = 0; j < nr; j++) {
9988 if (j == nr) { /* no duplicate */
9994 if (size == 0 && i < end_index)
10001 if (!end_of_loop && ptr)
10004 return rc < 0 ? rc : nr;
10008 * struct verify_mirror_id - Mirror id to be verified.
10009 * @mirror_id: A specified mirror id.
10010 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10012 struct verify_mirror_id {
10018 * compare_mirror_ids() - Compare mirror ids.
10019 * @layout: Mirror component list.
10020 * @cbdata: Callback data in verify_mirror_id structure.
10022 * This is a callback function called by llapi_layout_comp_iterate()
10023 * to compare the specified mirror id with the one in the current
10024 * component of @layout. If they are the same, then the specified
10025 * mirror id is valid.
10027 * Return: a negative error code on failure or
10028 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10029 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
10032 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10034 struct verify_mirror_id *mirror_id_cbdata =
10035 (struct verify_mirror_id *)cbdata;
10036 uint32_t mirror_id;
10039 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10043 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10044 progname, strerror(errno));
10048 if (mirror_id_cbdata->mirror_id == mirror_id) {
10049 mirror_id_cbdata->is_valid_id = true;
10050 return LLAPI_LAYOUT_ITER_STOP;
10053 return LLAPI_LAYOUT_ITER_CONT;
10057 * verify_mirror_ids() - Verify specified mirror ids.
10058 * @fname: Mirrored file name.
10059 * @mirror_ids: Specified mirror ids to be verified.
10060 * @ids_nr: Number of specified mirror ids.
10062 * This function verifies that specified @mirror_ids are valid
10063 * in the mirrored file @fname.
10065 * Return: 0 on success or a negative error code on failure.
10068 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10070 struct llapi_layout *layout = NULL;
10071 struct verify_mirror_id mirror_id_cbdata = { 0 };
10073 uint32_t flr_state;
10082 if (stat(fname, &stbuf) < 0) {
10083 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10084 progname, fname, strerror(errno));
10089 if (!S_ISREG(stbuf.st_mode)) {
10090 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10096 fd = open(fname, O_DIRECT | O_RDONLY);
10098 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10099 progname, fname, strerror(errno));
10104 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10106 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10107 progname, fname, strerror(errno));
10111 layout = llapi_layout_get_by_fd(fd, 0);
10113 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10114 progname, fname, strerror(errno));
10116 llapi_lease_release(fd);
10120 rc = llapi_layout_flags_get(layout, &flr_state);
10122 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10123 progname, fname, strerror(errno));
10128 flr_state &= LCM_FL_FLR_MASK;
10129 switch (flr_state) {
10132 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10133 progname, fname, llapi_layout_flags_string(flr_state));
10140 for (i = 0; i < ids_nr; i++) {
10141 mirror_id_cbdata.mirror_id = mirror_ids[i];
10142 mirror_id_cbdata.is_valid_id = false;
10144 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10145 &mirror_id_cbdata);
10149 "%s: '%s' failed to verify mirror id: %u.\n",
10150 progname, fname, mirror_ids[i]);
10154 if (!mirror_id_cbdata.is_valid_id) {
10157 "%s: '%s' invalid specified mirror id: %u.\n",
10158 progname, fname, mirror_ids[i]);
10164 llapi_layout_free(layout);
10165 llapi_lease_release(fd);
10173 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10174 __u16 *mirror_ids, int ids_nr)
10176 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10177 struct llapi_layout *layout;
10179 uint32_t flr_state;
10188 if (stat(fname, &stbuf) < 0) {
10189 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10190 progname, fname, strerror(errno));
10194 if (!S_ISREG(stbuf.st_mode)) {
10195 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10201 fd = open(fname, O_DIRECT | O_RDWR);
10203 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10204 progname, fname, strerror(errno));
10209 layout = llapi_layout_get_by_fd(fd, 0);
10211 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10212 progname, fname, strerror(errno));
10217 rc = llapi_layout_flags_get(layout, &flr_state);
10219 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10220 progname, fname, strerror(errno));
10225 flr_state &= LCM_FL_FLR_MASK;
10226 if (flr_state == LCM_FL_NONE) {
10228 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10233 /* get stale component info */
10234 comp_size = llapi_mirror_find_stale(layout, comp_array,
10235 ARRAY_SIZE(comp_array),
10236 mirror_ids, ids_nr);
10237 if (comp_size <= 0) {
10242 ioc->lil_mode = LL_LEASE_WRLCK;
10243 ioc->lil_flags = LL_LEASE_RESYNC;
10244 rc = llapi_lease_set(fd, ioc);
10246 if (rc == -EALREADY)
10250 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10251 progname, fname, strerror(-rc));
10255 /* get the read range [start, end) */
10256 start = comp_array[0].lrc_start;
10257 end = comp_array[0].lrc_end;
10258 for (idx = 1; idx < comp_size; idx++) {
10259 if (comp_array[idx].lrc_start < start)
10260 start = comp_array[idx].lrc_start;
10261 if (end < comp_array[idx].lrc_end)
10262 end = comp_array[idx].lrc_end;
10265 rc = llapi_lease_check(fd);
10266 if (rc != LL_LEASE_WRLCK) {
10267 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10272 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10275 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10276 progname, fname, strerror(-rc));
10278 /* need to do the lease unlock even resync fails */
10279 ioc->lil_mode = LL_LEASE_UNLCK;
10280 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10281 ioc->lil_count = 0;
10282 for (idx = 0; idx < comp_size; idx++) {
10283 if (comp_array[idx].lrc_synced) {
10284 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10289 rc2 = llapi_lease_set(fd, ioc);
10291 * llapi_lease_set returns lease mode when it request to unlock
10295 /* rc2 == 0 means lost lease lock */
10296 if (rc2 == 0 && rc == 0)
10298 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10300 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10304 llapi_layout_free(layout);
10311 static inline int lfs_mirror_resync(int argc, char **argv)
10313 struct ll_ioc_lease *ioc = NULL;
10314 __u16 mirror_ids[128] = { 0 };
10319 struct option long_opts[] = {
10320 { .val = 'o', .name = "only", .has_arg = required_argument },
10321 { .name = NULL } };
10323 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10326 rc = parse_mirror_ids(mirror_ids,
10327 sizeof(mirror_ids) / sizeof(__u16),
10331 "%s: bad mirror ids '%s'.\n",
10338 fprintf(stderr, "%s: options '%s' unrecognized.\n",
10339 argv[0], argv[optind - 1]);
10345 if (argc == optind) {
10346 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10351 if (ids_nr > 0 && argc > optind + 1) {
10353 "%s: option '--only' cannot be used upon multiple files.\n",
10360 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10365 /* set the lease on the file */
10366 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10368 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10369 argv[0], strerror(errno));
10374 for (; optind < argc; optind++) {
10375 rc = lfs_mirror_resync_file(argv[optind], ioc,
10376 mirror_ids, ids_nr);
10377 /* ignore previous file's error, continue with next file */
10380 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10388 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10390 struct llapi_layout *layout;
10393 layout = llapi_layout_get_by_fd(fd, 0);
10395 fprintf(stderr, "could not get layout.\n");
10399 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10401 fprintf(stderr, "failed to iterate layout\n");
10402 llapi_layout_free(layout);
10405 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10406 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10407 llapi_layout_free(layout);
10411 llapi_layout_free(layout);
10417 * Check whether two files are the same file
10418 * \retval 0 same file
10419 * \retval 1 not the same file
10420 * \retval <0 error code
10422 static inline int check_same_file(int fd, const char *f2)
10424 struct stat stbuf1;
10425 struct stat stbuf2;
10427 if (fstat(fd, &stbuf1) < 0)
10430 if (stat(f2, &stbuf2) < 0)
10433 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10434 stbuf1.st_ino == stbuf2.st_ino)
10440 static inline int lfs_mirror_read(int argc, char **argv)
10443 __u16 mirror_id = 0;
10444 const char *outfile = NULL;
10450 const size_t buflen = 4 << 20;
10452 struct option long_opts[] = {
10453 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10454 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10455 { .name = NULL } };
10457 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10462 unsigned long int id;
10465 id = strtoul(optarg, &end, 0);
10466 if (errno != 0 || *end != '\0' || id == 0 ||
10469 "%s %s: invalid mirror ID '%s'\n",
10470 progname, argv[0], optarg);
10474 mirror_id = (__u16)id;
10481 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10482 progname, argv[optind - 1]);
10487 if (argc == optind) {
10488 fprintf(stderr, "%s %s: no mirrored file provided\n",
10489 progname, argv[0]);
10491 } else if (argc > optind + 1) {
10492 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10496 if (mirror_id == 0) {
10497 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10498 progname, argv[0]);
10502 /* open mirror file */
10503 fname = argv[optind];
10504 fd = open(fname, O_DIRECT | O_RDONLY);
10506 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10507 progname, argv[0], fname, strerror(errno));
10511 /* verify mirror id */
10512 rc = verify_mirror_id_by_fd(fd, mirror_id);
10515 "%s %s: cannot find mirror with ID %u in '%s'\n",
10516 progname, argv[0], mirror_id, fname);
10520 /* open output file - O_EXCL ensures output is not the same as input */
10522 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10524 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10525 progname, argv[0], outfile, strerror(errno));
10530 outfd = STDOUT_FILENO;
10533 /* allocate buffer */
10534 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10536 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10537 progname, argv[0], rc);
10543 ssize_t bytes_read;
10544 ssize_t written = 0;
10546 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10547 if (bytes_read < 0) {
10550 "%s %s: fail to read data from mirror %u: %s\n",
10551 progname, argv[0], mirror_id, strerror(-rc));
10556 if (bytes_read == 0)
10559 while (written < bytes_read) {
10562 written2 = write(outfd, buf + written,
10563 bytes_read - written);
10564 if (written2 < 0) {
10566 "%s %s: fail to write %s: %s\n",
10567 progname, argv[0], outfile ? : "STDOUT",
10572 written += written2;
10575 if (written != bytes_read) {
10577 "%s %s: written %ld bytes does not match with %ld read.\n",
10578 progname, argv[0], written, bytes_read);
10600 static inline int lfs_mirror_write(int argc, char **argv)
10603 __u16 mirror_id = 0;
10604 const char *inputfile = NULL;
10610 const size_t buflen = 4 << 20;
10612 size_t page_size = sysconf(_SC_PAGESIZE);
10613 struct ll_ioc_lease_id ioc;
10615 struct option long_opts[] = {
10616 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10617 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10618 { .name = NULL } };
10620 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10625 unsigned long int id;
10628 id = strtoul(optarg, &end, 0);
10629 if (errno != 0 || *end != '\0' || id == 0 ||
10632 "%s %s: invalid mirror ID '%s'\n",
10633 progname, argv[0], optarg);
10637 mirror_id = (__u16)id;
10641 inputfile = optarg;
10644 fprintf(stderr, "%s: option '%s' unrecognized\n",
10645 progname, argv[optind - 1]);
10650 if (argc == optind) {
10651 fprintf(stderr, "%s %s: no mirrored file provided\n",
10652 progname, argv[0]);
10654 } else if (argc > optind + 1) {
10655 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10659 if (mirror_id == 0) {
10660 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10661 progname, argv[0]);
10665 /* open mirror file */
10666 fname = argv[optind];
10667 fd = open(fname, O_DIRECT | O_WRONLY);
10669 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10670 progname, argv[0], fname, strerror(errno));
10674 /* verify mirror id */
10675 rc = verify_mirror_id_by_fd(fd, mirror_id);
10678 "%s %s: cannot find mirror with ID %u in '%s'\n",
10679 progname, argv[0], mirror_id, fname);
10683 /* open input file */
10685 rc = check_same_file(fd, inputfile);
10688 "%s %s: input file cannot be the mirrored file\n",
10689 progname, argv[0]);
10695 inputfd = open(inputfile, O_RDONLY, 0644);
10697 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10698 progname, argv[0], inputfile, strerror(errno));
10703 inputfd = STDIN_FILENO;
10706 /* allocate buffer */
10707 rc = posix_memalign(&buf, page_size, buflen);
10709 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10710 progname, argv[0], rc);
10711 goto close_inputfd;
10714 /* prepare target mirror components instantiation */
10715 ioc.lil_mode = LL_LEASE_WRLCK;
10716 ioc.lil_flags = LL_LEASE_RESYNC;
10717 ioc.lil_mirror_id = mirror_id;
10718 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10721 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10722 progname, argv[0], fname, strerror(errno));
10728 ssize_t bytes_read;
10732 rc = llapi_lease_check(fd);
10733 if (rc != LL_LEASE_WRLCK) {
10734 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10735 progname, argv[0], fname);
10739 bytes_read = read(inputfd, buf, buflen);
10740 if (bytes_read < 0) {
10743 "%s %s: fail to read data from '%s': %s\n",
10744 progname, argv[0], inputfile ? : "STDIN",
10751 if (bytes_read == 0)
10754 /* round up to page align to make direct IO happy. */
10755 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10757 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10762 "%s %s: fail to write to mirror %u: %s\n",
10763 progname, argv[0], mirror_id,
10771 if (pos & (page_size - 1)) {
10772 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10777 ioc.lil_mode = LL_LEASE_UNLCK;
10778 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10780 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10785 "%s %s: release lease lock of '%s' failed: %s\n",
10786 progname, argv[0], fname, strerror(errno));
10803 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10805 struct llapi_layout *layout;
10806 struct collect_ids_data cid = { .cid_ids = ids,
10808 .cid_exclude = exclude_id, };
10811 layout = llapi_layout_get_by_fd(fd, 0);
10813 fprintf(stderr, "could not get layout\n");
10817 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10819 fprintf(stderr, "failed to iterate layout\n");
10820 llapi_layout_free(layout);
10824 llapi_layout_free(layout);
10826 return cid.cid_count;
10829 static inline int lfs_mirror_copy(int argc, char **argv)
10832 __u16 read_mirror_id = 0;
10833 __u16 ids[128] = { 0 };
10835 struct llapi_layout *layout = NULL;
10836 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10843 struct ll_ioc_lease *ioc = NULL;
10844 struct ll_ioc_lease_id *resync_ioc;
10846 struct option long_opts[] = {
10847 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10848 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10849 { .name = NULL } };
10851 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10856 unsigned long int id;
10859 id = strtoul(optarg, &end, 0);
10860 if (errno != 0 || *end != '\0' || id == 0 ||
10863 "%s %s: invalid read mirror ID '%s'\n",
10864 progname, argv[0], optarg);
10868 read_mirror_id = (__u16)id;
10872 if (!strcmp(optarg, "-1")) {
10873 /* specify all other mirrors */
10874 ids[0] = (__u16)-1;
10877 count = parse_mirror_ids((__u16 *)ids,
10885 fprintf(stderr, "%s: option '%s' unrecognized\n",
10886 progname, argv[optind - 1]);
10891 if (argc == optind) {
10892 fprintf(stderr, "%s %s: no mirrored file provided\n",
10893 progname, argv[0]);
10895 } else if (argc > optind + 1) {
10896 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10900 if (read_mirror_id == 0) {
10902 "%s %s: no valid read mirror ID %d is provided\n",
10903 progname, argv[0], read_mirror_id);
10909 "%s %s: no write mirror ID is provided\n",
10910 progname, argv[0]);
10914 for (i = 0; i < count; i++) {
10915 if (read_mirror_id == ids[i]) {
10917 "%s %s: read and write mirror ID cannot be the same\n",
10918 progname, argv[0]);
10923 /* open mirror file */
10924 fname = argv[optind];
10926 fd = open(fname, O_DIRECT | O_RDWR);
10928 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10929 progname, argv[0], fname, strerror(errno));
10933 /* write to all other mirrors */
10934 if (ids[0] == (__u16)-1) {
10935 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10939 "%s %s: failed to get other mirror ids in '%s': %d\n",
10940 progname, argv[0], fname, rc);
10945 /* verify mirror id */
10946 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10949 "%s %s: cannot find mirror with ID %u in '%s'\n",
10950 progname, argv[0], read_mirror_id, fname);
10954 for (i = 0; i < count; i++) {
10955 rc = verify_mirror_id_by_fd(fd, ids[i]);
10958 "%s %s: cannot find mirror with ID %u in '%s'\n",
10959 progname, argv[0], ids[i], fname);
10964 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10967 "%s %s: cannot alloc comp id array for ioc: %s\n",
10968 progname, argv[0], strerror(errno));
10973 /* get stale component info */
10974 layout = llapi_layout_get_by_fd(fd, 0);
10976 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10977 progname, argv[0], fname, strerror(errno));
10981 comp_size = llapi_mirror_find_stale(layout, comp_array,
10982 ARRAY_SIZE(comp_array),
10984 llapi_layout_free(layout);
10985 if (comp_size < 0) {
10990 /* prepare target mirror components instantiation */
10991 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10992 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10993 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10995 resync_ioc->lil_mirror_id = ids[0];
10997 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10998 rc = llapi_lease_set(fd, ioc);
11001 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11002 progname, argv[0], fname, strerror(errno));
11006 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11009 fprintf(stderr, "%s %s: copy error: %d\n",
11010 progname, argv[0], rc);
11014 fprintf(stdout, "mirror copied successfully: ");
11015 for (i = 0; i < copied; i++)
11016 fprintf(stdout, "%d ", ids[i]);
11017 fprintf(stdout, "\n");
11019 ioc->lil_mode = LL_LEASE_UNLCK;
11020 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11021 ioc->lil_count = 0;
11022 for (i = 0; i < comp_size; i++) {
11025 for (j = 0; j < copied; j++) {
11026 if (comp_array[i].lrc_mirror_id != ids[j])
11029 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11033 rc = llapi_lease_set(fd, ioc);
11038 "%s %s: release lease lock of '%s' failed: %s\n",
11039 progname, argv[0], fname, strerror(errno));
11054 * struct verify_chunk - Mirror chunk to be verified.
11055 * @chunk: [start, end) of the chunk.
11056 * @mirror_count: Number of mirror ids in @mirror_id array.
11057 * @mirror_id: Array of valid mirror ids that cover the chunk.
11059 struct verify_chunk {
11060 struct lu_extent chunk;
11061 unsigned int mirror_count;
11062 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11066 * print_chunks() - Print chunk information.
11067 * @fname: Mirrored file name.
11068 * @chunks: Array of chunks.
11069 * @chunk_count: Number of chunks in @chunks array.
11071 * This function prints [start, end) of each chunk in @chunks
11072 * for mirrored file @fname, and also prints the valid mirror ids
11073 * that cover the chunk.
11078 void print_chunks(const char *fname, struct verify_chunk *chunks,
11084 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11085 for (i = 0; i < chunk_count; i++) {
11086 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11088 if (chunks[i].mirror_count == 0)
11089 fprintf(stdout, "\t[");
11091 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11092 for (j = 1; j < chunks[i].mirror_count; j++)
11093 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11095 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11097 fprintf(stdout, "\n");
11101 * print_checksums() - Print CRC-32 checksum values.
11102 * @chunk: A chunk and its corresponding valid mirror ids.
11103 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
11105 * This function prints CRC-32 checksum values on @chunk for
11106 * each valid mirror that covers it.
11111 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11116 "CRC-32 checksum value for chunk "DEXT":\n",
11117 PEXT(&chunk->chunk));
11118 for (i = 0; i < chunk->mirror_count; i++)
11119 fprintf(stdout, "Mirror %u:\t%#lx\n",
11120 chunk->mirror_id[i], crc[i]);
11121 fprintf(stdout, "\n");
11125 * filter_mirror_id() - Filter specified mirror ids.
11126 * @chunks: Array of chunks.
11127 * @chunk_count: Number of chunks in @chunks array.
11128 * @mirror_ids: Specified mirror ids to be verified.
11129 * @ids_nr: Number of specified mirror ids.
11131 * This function scans valid mirror ids that cover each chunk in @chunks
11132 * and filters specified mirror ids.
11137 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11138 __u16 *mirror_ids, int ids_nr)
11143 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11144 unsigned int valid_count = 0;
11146 for (i = 0; i < chunk_count; i++) {
11147 if (chunks[i].mirror_count == 0)
11151 for (j = 0; j < ids_nr; j++) {
11152 for (k = 0; k < chunks[i].mirror_count; k++) {
11153 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11154 valid_id[valid_count] = mirror_ids[j];
11161 memcpy(chunks[i].mirror_id, valid_id,
11162 sizeof(__u16) * valid_count);
11163 chunks[i].mirror_count = valid_count;
11168 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11169 * @layout: Mirror component list.
11170 * @chunks: Array of chunks.
11171 * @chunks_size: Array size of @chunks.
11173 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11174 * to find out chunk segments and store them in @chunks array.
11176 * The @mirror_id array in each element of @chunks will store the valid
11177 * mirror ids that cover the chunk. If a mirror component covering the
11178 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11179 * will not be stored into the @mirror_id array, and the chunk for that
11180 * mirror will not be verified.
11182 * The @mirror_count in each element of @chunks will store the number of
11183 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11184 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11185 * indicates the chunk is valid in only one mirror. In both cases, the
11186 * chunk will not be verified.
11188 * Here is an example:
11190 * 0 1M 2M 3M 4M EOF
11191 * +------+-------------+--------------------+
11192 * | | | S | mirror1
11193 * +------+------+------+------+-------------+
11194 * | | S | S | | mirror2
11195 * +-------------+------+------+-------------+
11197 * prepared @chunks array will contain 5 elements:
11198 * (([0, 1M), [1, 2], 2),
11199 * ([1M, 2M), [1, 2], 2),
11200 * ([2M, 3M), [1], 1),
11201 * ([3M, 4M], [], 0),
11202 * ([4M, EOF), [2], 1))
11204 * Return: the actual array size of @chunks on success
11205 * or a negative error code on failure.
11208 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11209 struct verify_chunk *chunks,
11210 size_t chunks_size)
11214 uint32_t mirror_id;
11220 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11223 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11226 "%s: move to the first layout component: %s.\n",
11227 progname, strerror(errno));
11233 chunks[idx].chunk.e_end = LUSTRE_EOF;
11235 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11238 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11239 progname, strerror(errno));
11243 if (start > chunks[idx].chunk.e_start ||
11244 end <= chunks[idx].chunk.e_start)
11247 if (end < chunks[idx].chunk.e_end)
11248 chunks[idx].chunk.e_end = end;
11250 rc = llapi_layout_comp_flags_get(layout, &flags);
11253 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11254 progname, strerror(errno));
11258 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11261 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11264 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11265 progname, strerror(errno));
11269 chunks[idx].mirror_id[i] = mirror_id;
11271 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11273 "%s: mirror_id array is too small.\n",
11280 rc = llapi_layout_comp_use(layout,
11281 LLAPI_LAYOUT_COMP_USE_NEXT);
11284 "%s: move to the next layout component: %s.\n",
11285 progname, strerror(errno));
11288 } /* loop through all components */
11290 chunks[idx].mirror_count = i;
11292 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11296 if (idx >= chunks_size) {
11297 fprintf(stderr, "%s: chunks array is too small.\n",
11303 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11307 return rc < 0 ? rc : idx + 1;
11311 * lfs_mirror_verify_chunk() - Verify a chunk.
11312 * @fd: File descriptor of the mirrored file.
11313 * @file_size: Size of the mirrored file.
11314 * @chunk: A chunk and its corresponding valid mirror ids.
11315 * @verbose: Verbose mode.
11317 * This function verifies a @chunk contains exactly the same data
11318 * ammong the mirrors that cover it.
11320 * If @verbose is specified, then the function will print where the
11321 * differences are if the data do not match. Otherwise, it will
11322 * just return an error in that case.
11324 * Return: 0 on success or a negative error code on failure.
11327 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11328 struct verify_chunk *chunk, int verbose)
11330 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11332 size_t page_size = sysconf(_SC_PAGESIZE);
11333 ssize_t bytes_read;
11334 ssize_t bytes_done;
11338 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11342 if (file_size == 0)
11345 rc = posix_memalign(&buf, page_size, buflen);
11346 if (rc) /* error code is returned directly */
11350 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11351 PEXT(&chunk->chunk));
11352 for (i = 0; i < chunk->mirror_count; i++)
11353 fprintf(stdout, " %u", chunk->mirror_id[i]);
11354 fprintf(stdout, "\n");
11358 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11359 pos = chunk->chunk.e_start;
11360 while (bytes_done < count) {
11361 /* compute initial CRC-32 checksum */
11362 crc = crc32(0L, Z_NULL, 0);
11363 memset(crc_array, 0, sizeof(crc_array));
11366 for (i = 0; i < chunk->mirror_count; i++) {
11367 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11369 if (bytes_read < 0) {
11372 "%s: failed to read data from mirror %u: %s.\n",
11373 progname, chunk->mirror_id[i],
11378 /* compute new CRC-32 checksum */
11379 crc_array[i] = crc32(crc, buf, bytes_read);
11383 print_checksums(chunk, crc_array);
11385 /* compare CRC-32 checksum values */
11386 for (i = 1; i < chunk->mirror_count; i++) {
11387 if (crc_array[i] != crc_array[0]) {
11391 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11392 progname, PEXT(&chunk->chunk),
11393 chunk->mirror_id[0],
11394 chunk->mirror_id[i]);
11399 bytes_done += bytes_read;
11402 if (verbose > 1 && rc == 0) {
11403 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11404 PEXT(&chunk->chunk));
11405 for (i = 0; i < chunk->mirror_count; i++)
11406 fprintf(stdout, " %u", chunk->mirror_id[i]);
11407 fprintf(stdout, " PASS\n\n");
11416 * lfs_mirror_verify_file() - Verify a mirrored file.
11417 * @fname: Mirrored file name.
11418 * @mirror_ids: Specified mirror ids to be verified.
11419 * @ids_nr: Number of specified mirror ids.
11420 * @verbose: Verbose mode.
11422 * This function verifies that each SYNC mirror of a mirrored file
11423 * specified by @fname contains exactly the same data.
11425 * If @mirror_ids is specified, then the function will verify the
11426 * mirrors specified by @mirror_ids contain exactly the same data.
11428 * If @verbose is specified, then the function will print where the
11429 * differences are if the data do not match. Otherwise, it will
11430 * just return an error in that case.
11432 * Return: 0 on success or a negative error code on failure.
11435 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11438 struct verify_chunk chunks_array[1024] = { };
11439 struct llapi_layout *layout = NULL;
11441 uint32_t flr_state;
11443 int chunk_count = 0;
11449 if (stat(fname, &stbuf) < 0) {
11450 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11451 progname, fname, strerror(errno));
11456 if (!S_ISREG(stbuf.st_mode)) {
11457 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11463 if (stbuf.st_size == 0) {
11465 fprintf(stdout, "%s: '%s' file size is 0.\n",
11471 fd = open(fname, O_DIRECT | O_RDONLY);
11473 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11474 progname, fname, strerror(errno));
11479 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11481 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11482 progname, fname, strerror(errno));
11486 layout = llapi_layout_get_by_fd(fd, 0);
11488 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11489 progname, fname, strerror(errno));
11491 llapi_lease_release(fd);
11495 rc = llapi_layout_flags_get(layout, &flr_state);
11497 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11498 progname, fname, strerror(errno));
11503 flr_state &= LCM_FL_FLR_MASK;
11504 switch (flr_state) {
11507 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11508 progname, fname, llapi_layout_flags_string(flr_state));
11514 /* find out mirror chunks to be verified */
11515 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11516 ARRAY_SIZE(chunks_array));
11517 if (chunk_count < 0) {
11523 /* filter specified mirror ids */
11524 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11527 print_chunks(fname, chunks_array, chunk_count);
11529 for (idx = 0; idx < chunk_count; idx++) {
11530 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11533 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11535 PEXT(&chunks_array[idx].chunk),
11536 (unsigned long long)stbuf.st_size);
11540 if (chunks_array[idx].mirror_count == 0) {
11542 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11544 PEXT(&chunks_array[idx].chunk));
11546 fprintf(stderr, "skipped\n");
11550 fprintf(stderr, "failed\n");
11554 if (chunks_array[idx].mirror_count == 1) {
11557 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11559 PEXT(&chunks_array[idx].chunk),
11560 chunks_array[idx].mirror_id[0]);
11564 rc = llapi_lease_check(fd);
11565 if (rc != LL_LEASE_RDLCK) {
11566 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11571 /* verify one chunk */
11572 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11573 &chunks_array[idx], verbose);
11587 llapi_layout_free(layout);
11588 llapi_lease_release(fd);
11596 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11597 * @argc: The count of lfs mirror verify command line arguments.
11598 * @argv: Array of strings for lfs mirror verify command line arguments.
11600 * This function parses lfs mirror verify command and verifies the
11601 * specified mirrored file(s).
11603 * Return: 0 on success or a negative error code on failure.
11605 static inline int lfs_mirror_verify(int argc, char **argv)
11607 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11613 char cmd[PATH_MAX];
11615 struct option long_opts[] = {
11616 { .val = 'o', .name = "only", .has_arg = required_argument },
11617 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11618 { .name = NULL } };
11620 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11622 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11625 rc = parse_mirror_ids(mirror_ids,
11626 ARRAY_SIZE(mirror_ids),
11630 "%s: bad mirror ids '%s'.\n",
11637 "%s: at least 2 mirror ids needed with '--only' option.\n",
11647 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11648 progname, argv[optind - 1]);
11654 if (argc == optind) {
11655 fprintf(stderr, "%s: no file name given.\n", progname);
11660 if (ids_nr > 0 && argc > optind + 1) {
11662 "%s: '--only' cannot be used upon multiple files.\n",
11669 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11675 for (; optind < argc; optind++) {
11676 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11686 * lfs_mirror() - Parse and execute lfs mirror commands.
11687 * @argc: The count of lfs mirror command line arguments.
11688 * @argv: Array of strings for lfs mirror command line arguments.
11690 * This function parses lfs mirror commands and performs the
11691 * corresponding functions specified in mirror_cmdlist[].
11693 * Return: 0 on success or an error code on failure.
11695 static int lfs_mirror(int argc, char **argv)
11697 char cmd[PATH_MAX];
11700 setlinebuf(stdout);
11702 Parser_init("lfs-mirror > ", mirror_cmdlist);
11704 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11706 program_invocation_short_name = cmd;
11708 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11710 rc = Parser_commands();
11712 return rc < 0 ? -rc : rc;
11715 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11717 #if __BYTE_ORDER == __BIG_ENDIAN
11718 __swab16s(&attrs->lsa_valid);
11719 __swab64s(&attrs->lsa_size);
11720 __swab64s(&attrs->lsa_blocks);
11724 enum lfs_som_type {
11725 LFS_SOM_SIZE = 0x1,
11726 LFS_SOM_BLOCKS = 0x2,
11727 LFS_SOM_FLAGS = 0x4,
11728 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11732 static int lfs_getsom(int argc, char **argv)
11735 struct lustre_som_attrs *attrs;
11736 char buf[sizeof(*attrs) + 64];
11737 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11740 while ((c = getopt(argc, argv, "sbf")) != -1) {
11743 type = LFS_SOM_SIZE;
11746 type = LFS_SOM_BLOCKS;
11749 type = LFS_SOM_FLAGS;
11752 fprintf(stderr, "%s: invalid option '%c'\n",
11762 fprintf(stderr, "%s: %s\n",
11763 progname, argc == 0 ? "miss file target" :
11764 "input more than 2 files");
11769 attrs = (void *)buf;
11770 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11773 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11774 argv[0], strerror(errno), errno);
11778 lustre_som_swab(attrs);
11781 case LFS_SOM_ATTR_ALL:
11782 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11783 path, (unsigned long long)attrs->lsa_size,
11784 (unsigned long long)attrs->lsa_blocks,
11788 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11790 case LFS_SOM_BLOCKS:
11791 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11793 case LFS_SOM_FLAGS:
11794 printf("%x\n", attrs->lsa_valid);
11797 fprintf(stderr, "%s: unknown option\n", progname);
11805 * lfs_mirror_list_commands() - List lfs mirror commands.
11806 * @argc: The count of command line arguments.
11807 * @argv: Array of strings for command line arguments.
11809 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11811 * Return: 0 on success.
11813 static int lfs_mirror_list_commands(int argc, char **argv)
11815 char buffer[81] = "";
11817 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11823 static int lfs_pcc_attach(int argc, char **argv)
11825 struct option long_opts[] = {
11826 { .val = 'i', .name = "id", .has_arg = required_argument },
11827 { .name = NULL } };
11830 __u32 archive_id = 0;
11833 char fullpath[PATH_MAX];
11834 enum lu_pcc_type type = LU_PCC_READWRITE;
11837 while ((c = getopt_long(argc, argv, "i:",
11838 long_opts, NULL)) != -1) {
11842 archive_id = strtoul(optarg, &end, 0);
11843 if (errno != 0 || *end != '\0' ||
11844 archive_id == 0 || archive_id > UINT32_MAX) {
11846 "error: %s: bad archive ID '%s'\n",
11854 fprintf(stderr, "%s: option '%s' unrecognized\n",
11855 argv[0], argv[optind - 1]);
11860 if (archive_id == 0) {
11861 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11865 if (argc <= optind) {
11866 fprintf(stderr, "%s: must specify one or more file names\n",
11871 while (optind < argc) {
11874 path = argv[optind++];
11875 if (!realpath(path, fullpath)) {
11876 fprintf(stderr, "%s: could not find path '%s': %s\n",
11877 argv[0], path, strerror(errno));
11883 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11886 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11887 argv[0], path, archive_id, strerror(-rc2));
11895 static int lfs_pcc_attach_fid(int argc, char **argv)
11897 struct option long_opts[] = {
11898 { .val = 'i', .name = "id", .has_arg = required_argument },
11899 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11900 { .name = NULL } };
11901 char short_opts[] = "i:m:";
11904 __u32 archive_id = 0;
11906 const char *mntpath = NULL;
11907 const char *fidstr;
11908 enum lu_pcc_type type = LU_PCC_READWRITE;
11911 while ((c = getopt_long(argc, argv, short_opts,
11912 long_opts, NULL)) != -1) {
11916 archive_id = strtoul(optarg, &end, 0);
11917 if (errno != 0 || *end != '\0' ||
11918 archive_id > UINT32_MAX) {
11920 "error: %s: bad archive ID '%s'\n",
11931 fprintf(stderr, "%s: option '%s' unrecognized\n",
11932 argv[0], argv[optind - 1]);
11937 if (archive_id == 0) {
11938 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11943 fprintf(stderr, "%s: must specify Lustre mount point\n",
11948 if (argc <= optind) {
11949 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11953 while (optind < argc) {
11956 fidstr = argv[optind++];
11958 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11962 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11963 argv[0], fidstr, mntpath, archive_id,
11966 if (rc == 0 && rc2 < 0)
11972 static int lfs_pcc_detach(int argc, char **argv)
11974 struct option long_opts[] = {
11975 { .val = 'k', .name = "keep", .has_arg = no_argument },
11976 { .name = NULL } };
11977 char short_opts[] = "k";
11981 char fullpath[PATH_MAX];
11982 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11985 while ((c = getopt_long(argc, argv, short_opts,
11986 long_opts, NULL)) != -1) {
11989 detach_opt = PCC_DETACH_OPT_NONE;
11994 fprintf(stderr, "%s: option '%s' unrecognized\n",
11995 argv[0], argv[optind - 1]);
12000 while (optind < argc) {
12003 path = argv[optind++];
12004 if (!realpath(path, fullpath)) {
12005 fprintf(stderr, "%s: could not find path '%s': %s\n",
12006 argv[0], path, strerror(errno));
12012 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12016 "%s: cannot detach '%s' from PCC: %s\n",
12017 argv[0], path, strerror(errno));
12025 static int lfs_pcc_detach_fid(int argc, char **argv)
12027 struct option long_opts[] = {
12028 { .val = 'k', .name = "keep", .has_arg = no_argument },
12029 { .name = NULL } };
12030 char short_opts[] = "k";
12034 const char *mntpath;
12035 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12038 while ((c = getopt_long(argc, argv, short_opts,
12039 long_opts, NULL)) != -1) {
12042 detach_opt = PCC_DETACH_OPT_NONE;
12047 fprintf(stderr, "%s: option '%s' unrecognized\n",
12048 argv[0], argv[optind - 1]);
12053 mntpath = argv[optind++];
12055 while (optind < argc) {
12058 fid = argv[optind++];
12060 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12063 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12064 argv[0], fid, mntpath, strerror(-rc2));
12072 static int lfs_pcc_state(int argc, char **argv)
12076 char fullpath[PATH_MAX];
12077 struct lu_pcc_state state;
12082 fprintf(stderr, "%s: must specify one or more file names\n",
12087 while (optind < argc) {
12090 path = argv[optind++];
12091 if (!realpath(path, fullpath)) {
12092 fprintf(stderr, "%s: could not find path '%s': %s\n",
12093 argv[0], path, strerror(errno));
12099 rc2 = llapi_pcc_state_get(fullpath, &state);
12104 "%s: cannot get PCC state of '%s': %s\n",
12105 argv[0], path, strerror(-rc2));
12109 printf("file: %s", path);
12110 printf(", type: %s", pcc_type2string(state.pccs_type));
12111 if (state.pccs_type == LU_PCC_NONE &&
12112 state.pccs_open_count == 0) {
12117 printf(", PCC file: %s", state.pccs_path);
12118 printf(", user number: %u", state.pccs_open_count);
12119 printf(", flags: %x", state.pccs_flags);
12126 * lfs_pcc_list_commands() - List lfs pcc commands.
12127 * @argc: The count of command line arguments.
12128 * @argv: Array of strings for command line arguments.
12130 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12132 * Return: 0 on success.
12134 static int lfs_pcc_list_commands(int argc, char **argv)
12136 char buffer[81] = "";
12138 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12145 * lfs_pcc() - Parse and execute lfs pcc commands.
12146 * @argc: The count of lfs pcc command line arguments.
12147 * @argv: Array of strings for lfs pcc command line arguments.
12149 * This function parses lfs pcc commands and performs the
12150 * corresponding functions specified in pcc_cmdlist[].
12152 * Return: 0 on success or an error code on failure.
12154 static int lfs_pcc(int argc, char **argv)
12156 char cmd[PATH_MAX];
12159 setlinebuf(stdout);
12161 Parser_init("lfs-pcc > ", pcc_cmdlist);
12163 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12165 program_invocation_short_name = cmd;
12167 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12169 rc = Parser_commands();
12171 return rc < 0 ? -rc : rc;
12174 static int lfs_list_commands(int argc, char **argv)
12176 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12178 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12183 int main(int argc, char **argv)
12187 /* Ensure that liblustreapi constructor has run */
12188 if (!llapi_liblustreapi_initialized())
12189 fprintf(stderr, "liblustreapi was not properly initialized\n");
12191 setlinebuf(stdout);
12194 Parser_init("lfs > ", cmdlist);
12196 progname = program_invocation_short_name; /* Used in error messages */
12198 llapi_set_command_name(argv[1]);
12199 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12200 llapi_clear_command_name();
12202 rc = Parser_commands();
12205 return rc < 0 ? -rc : rc;
12208 #ifdef _LUSTRE_IDL_H_
12209 /* Everything we need here should be included by lustreapi.h. */
12210 # error "lfs should not depend on lustre_idl.h"
12211 #endif /* _LUSTRE_IDL_H_ */