4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
61 #include <sys/xattr.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <libcfs/util/string.h>
75 #include <lustre/lustreapi.h>
76 #include <linux/lustre/lustre_ver.h>
77 #include <linux/lustre/lustre_param.h>
78 #include <linux/lnet/nidstr.h>
79 #include <lnetconfig/cyaml.h>
83 static int lfs_find(int argc, char **argv);
84 static int lfs_getstripe(int argc, char **argv);
85 static int lfs_getdirstripe(int argc, char **argv);
86 static int lfs_setdirstripe(int argc, char **argv);
87 static int lfs_rmentry(int argc, char **argv);
88 static int lfs_unlink_foreign(int argc, char **argv);
89 static int lfs_osts(int argc, char **argv);
90 static int lfs_mdts(int argc, char **argv);
91 static int lfs_df(int argc, char **argv);
92 static int lfs_getname(int argc, char **argv);
93 static int lfs_check(int argc, char **argv);
94 #ifdef HAVE_SYS_QUOTA_H
95 static int lfs_setquota(int argc, char **argv);
96 static int lfs_quota(int argc, char **argv);
97 static int lfs_project(int argc, char **argv);
99 static int lfs_flushctx(int argc, char **argv);
100 static int lfs_poollist(int argc, char **argv);
101 static int lfs_changelog(int argc, char **argv);
102 static int lfs_changelog_clear(int argc, char **argv);
103 static int lfs_fid2path(int argc, char **argv);
104 static int lfs_path2fid(int argc, char **argv);
105 static int lfs_rmfid(int argc, char **argv);
106 static int lfs_data_version(int argc, char **argv);
107 static int lfs_hsm_state(int argc, char **argv);
108 static int lfs_hsm_set(int argc, char **argv);
109 static int lfs_hsm_clear(int argc, char **argv);
110 static int lfs_hsm_action(int argc, char **argv);
111 static int lfs_hsm_archive(int argc, char **argv);
112 static int lfs_hsm_restore(int argc, char **argv);
113 static int lfs_hsm_release(int argc, char **argv);
114 static int lfs_hsm_remove(int argc, char **argv);
115 static int lfs_hsm_cancel(int argc, char **argv);
116 static int lfs_swap_layouts(int argc, char **argv);
117 static int lfs_mv(int argc, char **argv);
118 static int lfs_ladvise(int argc, char **argv);
119 static int lfs_getsom(int argc, char **argv);
120 static int lfs_heat_get(int argc, char **argv);
121 static int lfs_heat_set(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_read(int argc, char **argv);
128 static inline int lfs_mirror_write(int argc, char **argv);
129 static inline int lfs_mirror_copy(int argc, char **argv);
130 static int lfs_pcc_attach(int argc, char **argv);
131 static int lfs_pcc_attach_fid(int argc, char **argv);
132 static int lfs_pcc_detach(int argc, char **argv);
133 static int lfs_pcc_detach_fid(int argc, char **argv);
134 static int lfs_pcc_state(int argc, char **argv);
135 static int lfs_pcc(int argc, char **argv);
136 static int lfs_pcc_list_commands(int argc, char **argv);
137 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
138 __u64 migration_flags,
139 struct llapi_stripe_param *param,
140 struct llapi_layout *layout);
142 struct pool_to_id_cbdata {
146 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
147 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
149 enum setstripe_origin {
159 static int lfs_setstripe_internal(int argc, char **argv,
160 enum setstripe_origin opc);
162 static inline int lfs_setstripe(int argc, char **argv)
164 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
167 static inline int lfs_setstripe_migrate(int argc, char **argv)
169 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
172 static inline int lfs_mirror_create(int argc, char **argv)
174 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
177 static inline int lfs_mirror_extend(int argc, char **argv)
179 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
182 static inline int lfs_mirror_split(int argc, char **argv)
184 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
187 static inline int lfs_mirror_delete(int argc, char **argv)
189 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
192 /* Setstripe and migrate share mostly the same parameters */
193 #define SSM_CMD_COMMON(cmd) \
194 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
195 " [--stripe-count|-c <stripe_count>]\n" \
196 " [--overstripe-count|-C <stripe_count>]\n" \
197 " [--stripe-index|-i <start_ost_idx>]\n" \
198 " [--stripe-size|-S <stripe_size>]\n" \
199 " [--extension-size|--ext-size|-z]\n" \
200 " [--layout|-L <pattern>]\n" \
201 " [--mirror-count|-N[mirror_count]]\n" \
202 " [--ost|-o <ost_indices>]\n" \
203 " [--pool|-p <pool_name>]\n" \
204 " [--yaml|-y <yaml_template_file>]\n" \
205 " [--copy=<lustre_src>]\n"
207 #define SSM_HELP_COMMON \
208 "\tstripe_count: Number of OSTs to stripe on (0=fs default, -1 all)\n" \
209 "\t Using -C instead of -c allows overstriping, which\n" \
210 "\t will place more than one stripe per OST if\n" \
211 "\t stripe_count is greater than the number of OSTs.\n" \
212 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
213 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
214 "\t Optional K, M, or G suffix (for KB, MB, GB\n" \
215 "\t respectively). Must be a multiple of 64KiB.\n" \
216 "\textension_size:\n" \
217 "\t Number of bytes the previous component is extended\n" \
218 "\t each time. Optional K, M, or G suffix (for KB,\n" \
219 "\t MB, GB respectively)\n" \
220 "\tpool_name: Name of OST pool to use (default none)\n" \
221 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n" \
222 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
223 "\t Indices be specified in a format of:\n" \
224 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
226 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
227 "\t If --pool is set with --ost then the OSTs\n" \
228 "\t must be the members of the pool.\n" \
229 "\tcomp_end: Extent end of component, start after previous end.\n" \
230 "\t Optional K, M, or G suffix (for KiB, MiB, GiB), or\n" \
231 "\t -1 or 'eof' for max file size). Must be a multiple\n" \
232 "\t of stripe_size and a multiple of 64KiB.\n" \
233 "\tyaml_template_file:\n" \
234 "\t YAML layout template file, can't be used with -c,\n" \
235 "\t -i, -S, -p, -o, or -E arguments.\n" \
236 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
237 "\t another lustre file or directory, can't used with\n" \
238 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
240 #define MIRROR_CREATE_HELP \
241 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
242 "\t setstripe layout options\n" \
243 "\t It defaults to 1 if not specified; if specified,\n" \
244 "\t it must follow the option without a space.\n" \
245 "\t The option can also be repeated multiple times to\n" \
246 "\t separate mirrors that have different layouts.\n" \
247 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
248 "\t It can be a plain layout or a composite layout.\n" \
249 "\t If not specified, the stripe options inherited\n" \
250 "\t from the previous component will be used.\n" \
251 "\tflags: set flags to the component of the current mirror.\n" \
252 "\t Only \"prefer\" flag is supported so far.\n"
254 #define MIRROR_EXTEND_HELP \
256 "\tvictim_file: The layout of victim_file will be split and used\n" \
257 "\t as a mirror added to the mirrored file.\n" \
258 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
259 "\t from victim file(s) in case the victim file(s)\n" \
260 "\t contains the same data as the original mirrored\n" \
263 #define MIRROR_EXTEND_USAGE \
264 " {--mirror-count|-N[mirror_count]}\n" \
265 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
268 #define SETSTRIPE_USAGE \
269 SSM_CMD_COMMON("setstripe") \
270 MIRROR_EXTEND_USAGE \
271 " <directory|filename>\n" \
275 #define MIGRATE_USAGE \
276 SSM_CMD_COMMON("migrate ") \
277 " [--block|-b] [--non-block|-n]\n" \
278 " [--non-direct|-D] [--verbose|-v]\n" \
282 "\tblock: Block file access during data migration (default)\n" \
283 "\tnon-block: Abort migrations if concurrent access is detected\n" \
284 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
285 "\tverbose: Print each filename as it is migrated\n" \
287 #define SETDIRSTRIPE_USAGE \
288 " [--mdt-count|-c stripe_count>\n" \
289 " [--mdt-hash|-H mdt_hash]\n" \
290 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
291 " [--default|-D] [--mode|-o mode] <dir>\n" \
292 "\tstripe_count: stripe count of the striped directory\n" \
293 "\tmdt_index: MDT index of first stripe\n" \
294 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
295 " crush CRUSH hash algorithm (default)\n" \
296 " fnv_1a_64 FNV-1a hash algorithm\n" \
297 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
298 "\tdefault_stripe: set default dirstripe of the directory\n" \
299 "\tmode: the file access permission of the directory (octal)\n" \
300 "To create dir with a foreign (free format) layout :\n" \
301 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
302 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
303 "\tmode: the mode of the directory\n" \
304 "\tforeign_type: none or symlink\n"
307 * command_t mirror_cmdlist - lfs mirror commands.
309 command_t mirror_cmdlist[] = {
310 { .pc_name = "create", .pc_func = lfs_mirror_create,
311 .pc_help = "Create a mirrored file.\n"
312 "usage: lfs mirror create "
313 "<--mirror-count|-N[mirror_count]> "
314 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
315 MIRROR_CREATE_HELP },
316 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
317 .pc_help = "Delete a mirror from a file.\n"
318 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
319 "\t --component-id|--comp-id|-I <comp_id> |\n"
320 "\t -p <pool>} <mirrored_file> ...\n"
322 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
323 .pc_help = "Extend a mirrored file.\n"
324 "usage: lfs mirror extend "
325 "<--mirror-count|-N[mirror_count]> [--no-verify] "
326 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
327 MIRROR_EXTEND_HELP },
328 { .pc_name = "split", .pc_func = lfs_mirror_split,
329 .pc_help = "Split a mirrored file.\n"
330 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
331 "\t --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
332 "\t [-f <new_file>] <mirrored_file> ...\n"
333 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
334 "\t can be fetched by lfs getstripe command.\n"
335 "\tcomp_id: Unique component ID within a mirror.\n"
336 "\tpool: Components on specified pool.\n"
337 "\tnew_file: This option indicates the layout of the split\n"
338 "\t mirror will be stored into. If not specified,\n"
339 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
340 "\t will be used.\n" },
341 { .pc_name = "read", .pc_func = lfs_mirror_read,
342 .pc_help = "Read the content of a specified mirror of a file.\n"
343 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
344 "[--outfile|-o <output_file>] <mirrored_file>\n" },
345 { .pc_name = "write", .pc_func = lfs_mirror_write,
346 .pc_help = "Write to a specified mirror of a file.\n"
347 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
348 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
349 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
350 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
351 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
352 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
353 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
354 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
355 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
356 "<mirrored file> [<mirrored file2>...]\n"},
357 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
358 .pc_help = "Verify mirrored file(s).\n"
359 "usage: lfs mirror verify "
360 "[--only <mirror_id,mirror_id2[,...]>] "
361 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
362 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
363 .pc_help = "list commands supported by lfs mirror"},
364 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
365 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
366 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
371 * command_t pcc_cmdlist - lfs pcc commands.
373 command_t pcc_cmdlist[] = {
374 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
375 .pc_help = "Attach given files to the Persistent Client Cache.\n"
376 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
377 "\t-i: archive id for RW-PCC\n" },
378 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
379 .pc_help = "Attach given files into PCC by FID(s).\n"
380 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
382 "\t-i: archive id for RW-PCC\n"
383 "\t-m: Lustre mount point\n" },
384 { .pc_name = "state", .pc_func = lfs_pcc_state,
385 .pc_help = "Display the PCC state for given files.\n"
386 "usage: lfs pcc state <file> ...\n" },
387 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
388 .pc_help = "Detach given files from the Persistent Client Cache.\n"
389 "usage: lfs pcc detach <file> ...\n" },
390 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
391 .pc_help = "Detach given files from PCC by FID(s).\n"
392 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
393 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
394 .pc_help = "list commands supported by lfs pcc"},
395 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
396 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
397 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
401 /* all available commands */
402 command_t cmdlist[] = {
403 {"setstripe", lfs_setstripe, 0,
404 "To create a file with specified striping/composite layout, or\n"
405 "create/replace the default layout on an existing directory:\n"
406 SSM_CMD_COMMON("setstripe")
408 " <directory|filename>\n"
410 "To add component(s) to an existing composite file:\n"
411 SSM_CMD_COMMON("setstripe --component-add")
413 "To totally delete the default striping from an existing directory:\n"
414 "usage: setstripe [--delete|-d] <directory>\n"
416 "To create a mirrored file or set s default mirror layout on a directory:\n"
417 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
419 "To delete the last component(s) from an existing composite file\n"
420 "(note that this will also delete any data in those components):\n"
421 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
422 " [--component-flags|-F <comp_flags>]\n"
424 "\tcomp_id: Unique component ID to delete\n"
425 "\tcomp_flags: 'init' indicating all instantiated components\n"
426 "\t '^init' indicating all uninstantiated components\n"
427 "\t-I and -F cannot be specified at the same time\n"
429 "To set or clear flags on a specific component\n"
430 "(note that this command can only be applied to mirrored files:\n"
431 "usage: setstripe --comp-set {-I comp_id|--comp-flags=comp_flags}\n"
434 "To create a file with a foreign (free format) layout:\n"
435 "usage: setstripe --foreign[=<foreign_type>]\n"
436 " --xattr|-x <layout_string> [--flags <hex>]\n"
437 " [--mode <mode>] <filename>\n"},
438 {"getstripe", lfs_getstripe, 0,
439 "To list the layout pattern for a given file or files in a\n"
440 "directory or recursively for all files in a directory tree.\n"
441 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
442 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
443 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
444 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
445 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
446 " [--component-id[=comp_id]|-I[comp_id]]\n"
447 " [--component-flags[=comp_flags]]\n"
448 " [--component-count]\n"
449 " [--extension-size|--ext-size|-z]\n"
450 " [--component-start[=[+-]comp_start]]\n"
451 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
452 " [[!] --mirror-index=[+-]<index> |\n"
453 " [!] --mirror-id=[+-]<id>] [--mirror-count|-N]\n"
454 " <directory|filename> ..."},
455 {"setdirstripe", lfs_setdirstripe, 0,
456 "Create striped directory on specified MDT, same as mkdir.\n"
457 "May be restricted to root or group users, depending on settings.\n"
458 "usage: setdirstripe [OPTION] <directory>\n"
460 {"getdirstripe", lfs_getdirstripe, 0,
461 "To list the layout pattern info for a given directory\n"
462 "or recursively for all directories in a directory tree.\n"
463 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
464 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
465 " [--recursive|-r] [--yaml|-y]\n"
466 " [--verbose|-v] [--default|-D] <dir> ..."},
467 {"mkdir", lfs_setdirstripe, 0,
468 "Create striped directory on specified MDT, same as setdirstripe.\n"
469 "usage: mkdir [OPTION] <directory>\n"
471 {"rm_entry", lfs_rmentry, 0,
472 "To remove the name entry of the remote directory. Note: This\n"
473 "command will only delete the name entry, i.e. the remote directory\n"
474 "will become inaccessable after this command. This can only be done\n"
475 "by the administrator\n"
476 "usage: rm_entry <dir>\n"},
477 {"unlink_foreign", lfs_unlink_foreign, 0,
478 "To remove the foreign file/dir.\n"
479 "Note: This is for files/dirs prevented to be removed using\n"
480 "unlink/rmdir, but works also for regular ones\n"
481 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
482 {"pool_list", lfs_poollist, 0,
483 "List pools or pool OSTs\n"
484 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
485 {"find", lfs_find, 0,
486 "find files matching given attributes recursively in directory tree.\n"
487 "usage: find <directory|filename> ...\n"
488 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
489 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
490 " [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
491 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
492 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
493 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
494 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
495 " [[!] --stripe-count|-c [+-]<stripes>]\n"
496 " [[!] --stripe-index|-i <index,...>]\n"
497 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
498 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
499 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
500 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
501 " [[!] --projid <projid>]\n"
502 " [[!] --layout|-L released,raid0,mdt]\n"
503 " [[!] --foreign[=<foreign_type>]]\n"
504 " [[!] --component-count [+-]<comp_cnt>]\n"
505 " [[!] --component-start [+-]N[kMGTPE]]\n"
506 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
507 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
508 " [[!] --mirror-count|-N [+-]<n>]\n"
509 " [[!] --mirror-state <[^]state>]\n"
510 " [[!] --mdt-count|-T [+-]<stripes>]\n"
511 " [[!] --mdt-hash|-H <hashtype>\n"
512 " [[!] --mdt-index|-m <uuid|index,...>]\n"
513 "\t !: used before an option indicates 'NOT' requested attribute\n"
514 "\t -: used before a value indicates less than requested value\n"
515 "\t +: used before a value indicates more than requested value\n"
516 "\thashtype: hash type of the striped directory.\n"
517 "\t fnv_1a_64 FNV-1a hash algorithm\n"
518 "\t all_char sum of characters % MDT_COUNT\n"},
519 {"check", lfs_check, 0,
520 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
521 "or all the servers (MGTs, MDTs and OSTs).\n"
522 "usage: check <mgts|osts|mdts|all>"},
523 {"osts", lfs_osts, 0, "list OSTs connected to client "
524 "[for specified path only]\n" "usage: osts [path]"},
525 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
526 "[for specified path only]\n" "usage: mdts [path]"},
528 "report filesystem disk space usage or inodes usage "
529 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
530 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
531 " [--pool|-p <fsname>[.<pool>]] [path]"},
532 {"getname", lfs_getname, 0,
533 "list instances and specified mount points [for specified path only]\n"
534 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
535 #ifdef HAVE_SYS_QUOTA_H
536 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
537 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
538 " -b <block-softlimit> -B <block-hardlimit>\n"
539 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
540 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
541 " [--block-softlimit <block-softlimit>]\n"
542 " [--block-hardlimit <block-hardlimit>]\n"
543 " [--inode-softlimit <inode-softlimit>]\n"
544 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
545 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
546 " [--block-grace 'notify'|<block-grace>]\n"
547 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
548 " setquota <-U|-G|-P>\n"
549 " -b <block-softlimit> -B <block-hardlimit>\n"
550 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
551 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
552 " [--block-softlimit <block-softlimit>]\n"
553 " [--block-hardlimit <block-hardlimit>]\n"
554 " [--inode-softlimit <inode-softlimit>]\n"
555 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
556 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
558 " -b can be used instead of --block-softlimit/--block-grace\n"
559 " -B can be used instead of --block-hardlimit\n"
560 " -i can be used instead of --inode-softlimit/--inode-grace\n"
561 " -I can be used instead of --inode-hardlimit\n"
562 " -d can be used instead of --default\n\n"
563 "Note: The total quota space will be split into many qunits and\n"
564 " balanced over all server targets, the minimal qunit size is\n"
565 " 1M bytes for block space and 1K inodes for inode space.\n\n"
566 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
567 " Quota space rebalancing process will stop when this mininum\n"
568 " value is reached. As a result, quota exceeded can be returned\n"
569 " while many targets still have 1MB or 1K inodes of spare\n"
571 " When setting the grace time, 'notify' can be used as grace to\n"
572 " be notified after the quota is over soft limit but prevents\n"
573 " the soft limit from becoming the hard limit."},
574 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
575 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
577 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
578 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
579 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
580 {"project", lfs_project, 0,
581 "Change or list project attribute for specified file or directory.\n"
582 "usage: project [-d|-r] <file|directory...>\n"
583 " list project ID and flags on file(s) or directories\n"
584 " project [-p id] [-s] [-r] <file|directory...>\n"
585 " set project ID and/or inherit flag for specified file(s) or directories\n"
586 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
587 " check project ID and flags on file(s) or directories, print outliers\n"
588 " project -C [-r] [-k] <file|directory...>\n"
589 " clear the project inherit flag and ID on the file or directory\n"
592 {"flushctx", lfs_flushctx, 0,
593 "Flush security context for current user.\n"
594 "usage: flushctx [-k] [-r] [mountpoint...]"},
595 {"changelog", lfs_changelog, 0,
596 "Show the metadata changes on an MDT."
597 "\nusage: changelog <mdtname> [startrec [endrec]]"},
598 {"changelog_clear", lfs_changelog_clear, 0,
599 "Indicate that old changelog records up to <endrec> are no longer of "
600 "interest to consumer <id>, allowing the system to free up space.\n"
601 "An <endrec> of 0 means all records.\n"
602 "usage: changelog_clear <mdtname> <id> <endrec>"},
603 {"fid2path", lfs_fid2path, 0,
604 "Resolve the full path(s) for given FID(s). For a specific hardlink "
605 "specify link number <linkno>.\n"
606 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
607 "<fsname|root> <fid>..."},
608 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
609 "usage: path2fid [--parents] <path> ..."},
610 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
611 "usage: rmfid <fsname|rootpath> <fid> ..."},
612 {"data_version", lfs_data_version, 0, "Display file data version for "
613 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
614 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
615 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
616 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
617 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
618 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
619 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
621 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
622 "[--archived] [--lost] <file> ..."},
623 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
624 "given files.\n" "usage: hsm_action <file> ..."},
625 {"hsm_archive", lfs_hsm_archive, 0,
626 "Archive file to external storage.\n"
627 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
629 {"hsm_restore", lfs_hsm_restore, 0,
630 "Restore file from external storage.\n"
631 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
632 {"hsm_release", lfs_hsm_release, 0,
633 "Release files from Lustre.\n"
634 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
635 {"hsm_remove", lfs_hsm_remove, 0,
636 "Remove file copy from external storage.\n"
637 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
639 " (FILE [FILE ...] | "
640 "--mntpath MOUNTPATH FID [FID ...])\n"
642 "Note: To remove an archived copy of a file already deleted from a "
644 "--mntpath option and a list of FIDs must be specified"
646 {"hsm_cancel", lfs_hsm_cancel, 0,
647 "Cancel requests related to specified files.\n"
648 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
649 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
650 "usage: swap_layouts <path1> <path2>"},
651 {"migrate", lfs_setstripe_migrate, 0,
652 "migrate a directory between MDTs.\n"
653 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
654 " [--mdt-hash|-H] <hash_type>\n"
655 " [--mdt-index|-m] <start_mdt_index>\n"
658 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
659 " it's the MDT index of first stripe\n"
660 "\tmdt_count: number of MDTs to stripe a directory over\n"
661 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
662 " all_char (type 1)sum of characters % MDT_COUNT\n"
663 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
664 " crush (type 3)CRUSH hash algorithm\n"
666 "migrate file objects from one OST "
667 "layout\nto another (may be not safe with concurent writes).\n"
669 "[--stripe-count|-c] <stripe_count>\n"
670 "[--overstripe-count|-C] <stripe_count>\n"
671 " [--stripe-index|-i] <start_ost_index>\n"
672 " [--stripe-size|-S] <stripe_size>\n"
673 " [--pool|-p] <pool_name>\n"
674 " [--ost|-o] <ost_indices>\n"
676 " [--non-block|-n]\n"
677 " [--non-direct|-D]\n"
678 " <file|directory>\n"
679 "\tstripe_count: number of OSTs to stripe a file over\n"
680 "\t Using -C instead of -c allows overstriping, which\n"
681 "\t will place more than one stripe per OST if\n"
682 "\t stripe_count is greater than the number of OSTs\n"
683 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
684 "\tstripe_size: number of bytes to store before moving to the next OST\n"
685 "\tpool_name: name of the predefined pool of OSTs\n"
686 "\tost_indices: OSTs to stripe over, in order\n"
687 "\tblock: Block file access during data migration (default)\n"
688 "\tnon-block: Abort migrations if concurrent access is detected\n"
689 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
691 "To move directories between MDTs. This command is deprecated, "
692 "use \"migrate\" instead.\n"
693 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
695 {"ladvise", lfs_ladvise, 0,
696 "Provide servers with advice about access patterns for a file.\n"
697 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
698 " [--background|-b] [--unset|-u]\n\n"
699 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
700 " {[--mode|-m [READ,WRITE]}\n"
702 {"mirror", lfs_mirror, mirror_cmdlist,
703 "lfs commands used to manage files with mirrored components:\n"
704 "lfs mirror create - create a mirrored file or directory\n"
705 "lfs mirror extend - add mirror(s) to an existing file\n"
706 "lfs mirror split - split a mirror from an existing mirrored file\n"
707 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
708 "lfs mirror read - read a mirror content of a mirrored file\n"
709 "lfs mirror write - write to a mirror of a mirrored file\n"
710 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
711 "lfs mirror verify - verify mirrored file(s)\n"},
712 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
713 "usage: getsom [-s] [-b] [-f] <path>\n"
714 "\t-s: Only show the size value of the SOM data for a given file\n"
715 "\t-b: Only show the blocks value of the SOM data for a given file\n"
716 "\t-f: Only show the flags value of the SOM data for a given file\n"},
717 {"heat_get", lfs_heat_get, 0,
718 "To get heat of files.\n"
719 "usage: heat_get <file> ...\n"},
720 {"heat_set", lfs_heat_set, 0,
721 "To set heat flags of files.\n"
722 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
723 "\t--clear|-c: Clear file heat for given files\n"
724 "\t--off|-o: Turn off file heat for given files\n"
725 "\t--on|-O: Turn on file heat for given files\n"},
726 {"pcc", lfs_pcc, pcc_cmdlist,
727 "lfs commands used to interact with PCC features:\n"
728 "lfs pcc attach - attach given files to Persistent Client Cache\n"
729 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
730 "lfs pcc state - display the PCC state for given files\n"
731 "lfs pcc detach - detach given files from Persistent Client Cache\n"
732 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
733 {"help", Parser_help, 0, "help"},
734 {"exit", Parser_quit, 0, "quit"},
735 {"quit", Parser_quit, 0, "quit"},
736 {"--version", Parser_version, 0,
737 "output build version of the utility and exit"},
738 {"--list-commands", lfs_list_commands, 0,
739 "list commands supported by the utility and exit"},
743 static int check_hashtype(const char *hashtype)
745 int type_num = atoi(hashtype);
748 /* numeric hash type */
749 if (hashtype && strlen(hashtype) == 1 &&
750 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
752 /* string hash type */
753 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
754 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
760 static uint32_t check_foreign_type_name(const char *foreign_type_name)
764 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
765 if (!lu_foreign_types[i].lft_name)
767 if (strcmp(foreign_type_name,
768 lu_foreign_types[i].lft_name) == 0)
769 return lu_foreign_types[i].lft_type;
772 return LU_FOREIGN_TYPE_UNKNOWN;
775 static const char *error_loc = "syserror";
778 MIGRATION_NONBLOCK = 0x0001,
779 MIGRATION_MIRROR = 0x0002,
780 MIGRATION_NONDIRECT = 0x0004,
781 MIGRATION_VERBOSE = 0x0008,
785 migrate_open_files(const char *name, __u64 migration_flags,
786 const struct llapi_stripe_param *param,
787 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
794 char parent[PATH_MAX];
795 char volatile_file[PATH_MAX];
801 if (!param && !layout) {
802 error_loc = "layout information";
806 /* search for file directory pathname */
807 if (strlen(name) > sizeof(parent) - 1) {
808 error_loc = "source file name";
812 strncpy(parent, name, sizeof(parent));
813 ptr = strrchr(parent, '/');
815 if (!getcwd(parent, sizeof(parent))) {
816 error_loc = "getcwd";
820 if (ptr == parent) /* leading '/' */
825 /* open file, direct io */
826 /* even if the file is only read, WR mode is nedeed to allow
830 if (!(migration_flags & MIGRATION_NONDIRECT))
832 fd = open(name, rflags);
835 error_loc = "cannot open source file";
839 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
841 error_loc = "cannot get MDT index";
846 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
847 mode_t open_mode = S_IRUSR | S_IWUSR;
849 random_value = random();
850 rc = snprintf(volatile_file, sizeof(volatile_file),
851 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
852 mdt_index, random_value);
853 if (rc >= sizeof(volatile_file)) {
858 /* create, open a volatile file, use caching (ie no directio) */
860 /* Returns -1 and sets errno on error: */
861 fdv = llapi_layout_file_open(volatile_file, open_flags,
866 /* Does the right thing on error: */
867 fdv = llapi_file_open_param(volatile_file, open_flags,
870 } while (fdv < 0 && (rc = fdv) == -EEXIST);
873 error_loc = "cannot create volatile file";
878 * In case the MDT does not support creation of volatile files
879 * we should try to unlink it.
881 (void)unlink(volatile_file);
884 * Not-owner (root?) special case.
885 * Need to set owner/group of volatile file like original.
886 * This will allow to pass related check during layout_swap.
891 error_loc = "cannot stat source file";
895 rc = fstat(fdv, &stv);
898 error_loc = "cannot stat volatile";
902 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
903 rc = fchown(fdv, st.st_uid, st.st_gid);
906 error_loc = "cannot change ownwership of volatile";
925 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
927 struct llapi_layout *layout;
928 size_t buf_size = 4 * 1024 * 1024;
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, getpagesize(), buf_size);
955 * read new data only if we have written all
956 * previously read data
960 rc = check_file(fd_src);
965 rsize = read(fd_src, buf, buf_size);
979 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
992 /* Try to avoid page cache pollution after migration. */
993 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
994 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1000 static int migrate_copy_timestamps(int fd, int fdv)
1004 if (fstat(fd, &st) == 0) {
1005 struct timeval tv[2] = {
1006 {.tv_sec = st.st_atime},
1007 {.tv_sec = st.st_mtime}
1010 return futimes(fdv, tv);
1016 static int migrate_block(int fd, int fdv)
1023 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1025 error_loc = "cannot get dataversion";
1034 * The grouplock blocks all concurrent accesses to the file.
1035 * It has to be taken after llapi_get_data_version as it would
1038 rc = llapi_group_lock(fd, gid);
1040 error_loc = "cannot get group lock";
1044 rc = migrate_copy_data(fd, fdv, NULL);
1046 error_loc = "data copy failed";
1050 /* Make sure we keep original atime/mtime values */
1051 rc = migrate_copy_timestamps(fd, fdv);
1053 error_loc = "timestamp copy failed";
1059 * for a migration we need to check data version on file did
1062 * Pass in gid=0 since we already own grouplock.
1064 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1065 SWAP_LAYOUTS_CHECK_DV1);
1066 if (rc == -EAGAIN) {
1067 error_loc = "file changed";
1069 } else if (rc < 0) {
1070 error_loc = "cannot swap layout";
1075 rc2 = llapi_group_unlock(fd, gid);
1076 if (rc2 < 0 && rc == 0) {
1077 error_loc = "unlock group lock";
1085 * Internal helper for migrate_copy_data(). Check lease and report error if
1088 * \param[in] fd File descriptor on which to check the lease.
1090 * \retval 0 Migration can keep on going.
1091 * \retval -errno Error occurred, abort migration.
1093 static int check_lease(int fd)
1097 rc = llapi_lease_check(fd);
1099 return 0; /* llapi_check_lease returns > 0 on success. */
1104 static int migrate_nonblock(int fd, int fdv)
1110 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1112 error_loc = "cannot get data version";
1116 rc = migrate_copy_data(fd, fdv, check_lease);
1118 error_loc = "data copy failed";
1122 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1124 error_loc = "cannot get data version";
1130 error_loc = "source file changed";
1134 /* Make sure we keep original atime/mtime values */
1135 rc = migrate_copy_timestamps(fd, fdv);
1137 error_loc = "timestamp copy failed";
1145 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1147 struct pool_to_id_cbdata data = { .pool = pool };
1148 struct llapi_layout *layout = NULL;
1151 layout = llapi_layout_get_by_path(fname, 0);
1154 "error %s: file '%s' couldn't get layout: rc=%d\n",
1155 progname, fname, errno);
1159 rc = llapi_layout_sanity(layout, false, true);
1161 llapi_layout_sanity_perror(errno);
1164 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1173 llapi_layout_free(layout);
1177 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1178 __u32 flags, __u32 neg_flags)
1181 __u32 flags_array[2];
1188 "error %s: neither component id nor pool is specified\n",
1192 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1198 ids[count] = comp_id;
1199 flags_array[count] = flags;
1204 if (neg_flags & LCME_FL_STALE) {
1206 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1211 ids[count] = comp_id;
1212 flags_array[count] = neg_flags | LCME_FL_NEG;
1216 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1218 if (errno == EUCLEAN) {
1221 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1222 progname, comp_id, fname);
1225 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1226 progname, comp_id, fname, flags, neg_flags);
1233 static int lfs_component_del(char *fname, __u32 comp_id,
1234 __u32 flags, __u32 neg_flags)
1238 if (flags && neg_flags)
1241 if (!flags && neg_flags)
1242 flags = neg_flags | LCME_FL_NEG;
1244 if ((flags && comp_id) || (!flags && !comp_id))
1248 if (flags & ~LCME_KNOWN_FLAGS) {
1250 "%s setstripe: unknown flags %#x\n",
1254 } else if (comp_id > LCME_ID_MAX) {
1255 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1260 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1263 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1264 progname, comp_id, fname, strerror(errno));
1268 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1275 rc = llapi_layout_file_comp_add(fname, layout);
1277 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1278 fname, strerror(errno));
1282 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1283 struct llapi_layout *layout)
1291 fd = lstat(fname, &st);
1292 if (fd == 0 && S_ISDIR(st.st_mode))
1293 open_flags = O_DIRECTORY | O_RDONLY;
1295 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1297 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1298 S_ISDIR(st.st_mode) ?
1299 "set default composite layout for" :
1300 "create composite file",
1301 fname, strerror(errno));
1305 static int lfs_migrate(char *name, __u64 migration_flags,
1306 struct llapi_stripe_param *param,
1307 struct llapi_layout *layout)
1309 struct llapi_layout *existing;
1310 uint64_t dom_new, dom_cur;
1315 rc = migrate_open_files(name, migration_flags, param, layout,
1320 rc = llapi_layout_dom_size(layout, &dom_new);
1322 error_loc = "cannot get new layout DoM size";
1325 /* special case for migration to DOM layout*/
1326 existing = llapi_layout_get_by_fd(fd, 0);
1328 error_loc = "cannot get existing layout";
1332 rc = llapi_layout_dom_size(existing, &dom_cur);
1334 error_loc = "cannot get current layout DoM size";
1339 * if file has DoM layout already then migration is possible to
1340 * the new layout with the same DoM component via swap layout,
1341 * if new layout used bigger DOM size, then mirroring is used
1343 if (dom_new > dom_cur) {
1344 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1347 error_loc = "cannot migrate to DOM layout";
1351 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1353 * Blocking mode (forced if servers do not support file lease).
1354 * It is also the default mode, since we cannot distinguish
1355 * between a broken lease and a server that does not support
1356 * atomic swap/close (LU-6785)
1358 rc = migrate_block(fd, fdv);
1362 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1364 error_loc = "cannot get lease";
1368 rc = migrate_nonblock(fd, fdv);
1370 llapi_lease_release(fd);
1375 * Atomically put lease, swap layouts and close.
1376 * for a migration we need to check data version on file did
1379 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1381 error_loc = "cannot swap layout";
1393 fprintf(stderr, "error: %s: %s: %s: %s\n",
1394 progname, name, error_loc, strerror(-rc));
1395 else if (migration_flags & MIGRATION_VERBOSE)
1396 printf("%s\n", name);
1401 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1404 char *dup_string = NULL;
1410 if (!string || !string[0])
1413 dup_string = strdup(string);
1415 llapi_printf(LLAPI_MSG_ERROR,
1416 "%s: insufficient memory\n",
1421 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1425 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1426 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1427 const char *comp_name = comp_flags_table[i].cfn_name;
1429 if (strcmp(name, comp_name) == 0) {
1430 *flags |= comp_flag;
1432 } else if (strncmp(name, "^", 1) == 0 &&
1433 strcmp(name + 1, comp_name) == 0) {
1434 *neg_flags |= comp_flag;
1439 llapi_printf(LLAPI_MSG_ERROR,
1440 "%s: component flag '%s' not supported\n",
1447 if (!*flags && !*neg_flags)
1450 /* don't allow to set and exclude the same flag */
1451 if (*flags & *neg_flags)
1459 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1467 if (strncmp(string, "^", 1) == 0) {
1468 *neg_state = llapi_layout_string_flags(string + 1);
1469 if (*neg_state != 0)
1472 *state = llapi_layout_string_flags(string);
1477 llapi_printf(LLAPI_MSG_ERROR,
1478 "%s: mirrored file state '%s' not supported\n",
1484 * struct mirror_args - Command-line arguments for mirror(s).
1485 * @m_count: Number of mirrors to be created with this layout.
1486 * @m_flags: Mirror level flags, only 'prefer' is supported.
1487 * @m_layout: Mirror layout.
1488 * @m_file: A victim file. Its layout will be split and used as a mirror.
1489 * @m_next: Point to the next node of the list.
1491 * Command-line arguments for mirror(s) will be parsed and stored in
1492 * a linked list that consists of this structure.
1494 struct mirror_args {
1497 struct llapi_layout *m_layout;
1499 struct mirror_args *m_next;
1504 * enum mirror_flags - Flags for extending a mirrored file.
1505 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1506 * in case the victim file(s) contains the same data as the
1507 * original mirrored file.
1508 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1509 * @MF_COMP_ID: specified component id instead of mirror id
1511 * Flags for extending a mirrored file.
1521 * mirror_create_sanity_check() - Check mirror list.
1522 * @list: A linked list that stores the mirror arguments.
1524 * This function does a sanity check on @list for creating
1527 * Return: 0 on success or a negative error code on failure.
1529 static int mirror_create_sanity_check(const char *fname,
1530 struct mirror_args *list)
1533 bool has_m_file = false;
1534 bool has_m_layout = false;
1540 struct llapi_layout *layout;
1542 layout = llapi_layout_get_by_path(fname, 0);
1545 "error: %s: file '%s' couldn't get layout\n",
1550 rc = llapi_layout_sanity(layout, false, true);
1552 llapi_layout_free(layout);
1555 llapi_layout_sanity_perror(rc);
1563 llapi_layout_free(list->m_layout);
1566 llapi_layout_get_by_path(list->m_file, 0);
1567 if (!list->m_layout) {
1569 "error: %s: file '%s' has no layout\n",
1570 progname, list->m_file);
1574 has_m_layout = true;
1575 if (!list->m_layout) {
1576 fprintf(stderr, "error: %s: no mirror layout\n",
1582 rc = llapi_layout_sanity(list->m_layout, false, true);
1584 llapi_layout_sanity_perror(rc);
1588 list = list->m_next;
1591 if (has_m_file && has_m_layout) {
1593 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1601 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1603 __u32 mirror_flags = *(__u32 *)cbdata;
1607 rc = llapi_layout_comp_flags_get(layout, &flags);
1612 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1617 return LLAPI_LAYOUT_ITER_CONT;
1621 * mirror_create() - Create a mirrored file.
1622 * @fname: The file to be created.
1623 * @mirror_list: A linked list that stores the mirror arguments.
1625 * This function creates a mirrored file @fname with the mirror(s)
1626 * from @mirror_list.
1628 * Return: 0 on success or a negative error code on failure.
1630 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1632 struct llapi_layout *layout = NULL;
1633 struct mirror_args *cur_mirror = NULL;
1634 uint16_t mirror_count = 0;
1638 rc = mirror_create_sanity_check(NULL, mirror_list);
1642 cur_mirror = mirror_list;
1643 while (cur_mirror) {
1644 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1646 &cur_mirror->m_flags);
1649 fprintf(stderr, "%s: failed to set mirror flags\n",
1654 for (i = 0; i < cur_mirror->m_count; i++) {
1655 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1659 "error: %s: merge layout failed: %s\n",
1660 progname, strerror(errno));
1664 mirror_count += cur_mirror->m_count;
1665 cur_mirror = cur_mirror->m_next;
1669 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1673 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1676 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1677 progname, strerror(errno));
1681 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1689 llapi_layout_free(layout);
1694 * Compare files and check lease on @fd.
1696 * \retval bytes number of bytes are the same
1698 static ssize_t mirror_file_compare(int fd, int fdv)
1700 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1702 ssize_t bytes_done = 0;
1703 ssize_t bytes_read = 0;
1705 buf = malloc(buflen * 2);
1710 if (!llapi_lease_check(fd)) {
1711 bytes_done = -EBUSY;
1715 bytes_read = read(fd, buf, buflen);
1716 if (bytes_read <= 0)
1719 if (bytes_read != read(fdv, buf + buflen, buflen))
1723 * XXX: should compute the checksum on each buffer and then
1724 * compare checksum to avoid cache collision
1726 if (memcmp(buf, buf + buflen, bytes_read))
1729 bytes_done += bytes_read;
1737 static int mirror_extend_file(const char *fname, const char *victim_file,
1738 enum mirror_flags mirror_flags)
1743 struct stat stbuf_v;
1744 struct ll_ioc_lease *data = NULL;
1747 fd = open(fname, O_RDWR);
1749 error_loc = "open source file";
1754 fdv = open(victim_file, O_RDWR);
1756 error_loc = "open target file";
1761 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1762 error_loc = "stat source or target file";
1767 if (stbuf.st_dev != stbuf_v.st_dev) {
1768 error_loc = "stat source and target file";
1773 /* mirrors should be of the same size */
1774 if (stbuf.st_size != stbuf_v.st_size) {
1775 error_loc = "file sizes don't match";
1780 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1782 error_loc = "cannot get lease";
1786 if (!(mirror_flags & MF_NO_VERIFY)) {
1788 /* mirrors should have the same contents */
1789 ret = mirror_file_compare(fd, fdv);
1790 if (ret != stbuf.st_size) {
1791 error_loc = "file busy or contents don't match";
1792 rc = ret < 0 ? ret : -EINVAL;
1797 /* Get rid of caching pages from clients */
1798 rc = llapi_file_flush(fd);
1800 error_loc = "cannot get data version";
1804 rc = llapi_file_flush(fdv);
1806 error_loc = "cannot get data version";
1810 /* Make sure we keep original atime/mtime values */
1811 rc = migrate_copy_timestamps(fd, fdv);
1813 error_loc = "cannot copy timestamp";
1817 /* Atomically put lease, merge layouts and close. */
1818 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1820 error_loc = "memory allocation";
1823 data->lil_mode = LL_LEASE_UNLCK;
1824 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1825 data->lil_count = 1;
1826 data->lil_ids[0] = fdv;
1827 rc = llapi_lease_set(fd, data);
1829 error_loc = "cannot merge layout";
1831 } else if (rc == 0) {
1833 error_loc = "lost lease lock";
1846 (void) unlink(victim_file);
1848 fprintf(stderr, "error: %s: %s: %s: %s\n",
1849 progname, fname, error_loc, strerror(-rc));
1853 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1854 bool inherit, uint32_t flags)
1856 struct llapi_layout *f_layout = NULL;
1857 struct ll_ioc_lease *data = NULL;
1863 f_layout = llapi_layout_get_by_path(name, 0);
1865 fprintf(stderr, "%s: cannot get layout\n", progname);
1868 rc = llapi_layout_get_last_init_comp(f_layout);
1870 fprintf(stderr, "%s: cannot get the last init comp\n",
1874 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1877 "%s: cannot inherit from the last init comp\n",
1882 llapi_layout_comp_flags_set(m_layout, flags);
1883 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1888 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1890 error_loc = "cannot get lease";
1894 rc = migrate_nonblock(fd, fdv);
1896 llapi_lease_release(fd);
1900 /* Atomically put lease, merge layouts and close. */
1901 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1903 error_loc = "memory allocation";
1906 data->lil_mode = LL_LEASE_UNLCK;
1907 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1908 data->lil_count = 1;
1909 data->lil_ids[0] = fdv;
1910 rc = llapi_lease_set(fd, data);
1912 error_loc = "cannot merge layout";
1914 } else if (rc == 0) {
1916 error_loc = "lost lease lock";
1929 fprintf(stderr, "error: %s: %s: %s: %s\n",
1930 progname, name, error_loc, strerror(-rc));
1934 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1935 enum mirror_flags mirror_flags)
1939 rc = mirror_create_sanity_check(fname, mirror_list);
1943 while (mirror_list) {
1944 if (mirror_list->m_file) {
1945 rc = mirror_extend_file(fname, mirror_list->m_file,
1948 __u32 mirror_count = mirror_list->m_count;
1950 while (mirror_count > 0) {
1951 rc = mirror_extend_layout(fname,
1952 mirror_list->m_layout,
1953 mirror_list->m_inherit,
1954 mirror_list->m_flags);
1964 mirror_list = mirror_list->m_next;
1970 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1975 rc = llapi_layout_mirror_id_get(layout, &id);
1979 if ((__u16)id == *(__u16 *)cbdata)
1980 return LLAPI_LAYOUT_ITER_STOP;
1982 return LLAPI_LAYOUT_ITER_CONT;
1985 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1990 rc = llapi_layout_comp_id_get(layout, &id);
1994 if (id == *(__u32 *)cbdata)
1995 return LLAPI_LAYOUT_ITER_STOP;
1997 return LLAPI_LAYOUT_ITER_CONT;
2000 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2002 char buf[LOV_MAXPOOLNAME + 1];
2003 struct pool_to_id_cbdata *d = (void *)cbdata;
2007 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2010 if (strcmp(d->pool, buf))
2011 return LLAPI_LAYOUT_ITER_CONT;
2013 rc = llapi_layout_mirror_id_get(layout, &id);
2018 return LLAPI_LAYOUT_ITER_STOP;
2021 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2023 char buf[LOV_MAXPOOLNAME + 1];
2024 struct pool_to_id_cbdata *d = (void *)cbdata;
2028 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2031 if (strcmp(d->pool, buf))
2032 return LLAPI_LAYOUT_ITER_CONT;
2034 rc = llapi_layout_comp_id_get(layout, &id);
2039 return LLAPI_LAYOUT_ITER_STOP;
2042 struct collect_ids_data {
2048 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2050 struct collect_ids_data *cid = cbdata;
2054 rc = llapi_layout_mirror_id_get(layout, &id);
2058 if ((__u16)id != cid->cid_exclude) {
2061 for (i = 0; i < cid->cid_count; i++) {
2062 /* already collected the mirror id */
2063 if (id == cid->cid_ids[i])
2064 return LLAPI_LAYOUT_ITER_CONT;
2066 cid->cid_ids[cid->cid_count] = id;
2070 return LLAPI_LAYOUT_ITER_CONT;
2074 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2075 * @mirror_id: Mirror id to be checked.
2076 * @layout: Mirror component list.
2078 * This function checks if a mirror with specified @mirror_id is the last
2079 * non-stale mirror of a layout @layout.
2081 * Return: true or false.
2084 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2086 __u16 mirror_ids[128] = { 0 };
2087 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2089 .cid_exclude = mirror_id, };
2092 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2094 for (i = 0; i < cid.cid_count; i++) {
2095 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2098 comp_size = llapi_mirror_find_stale(layout, comp_array,
2099 ARRAY_SIZE(comp_array),
2108 static int mirror_split(const char *fname, __u32 id, const char *pool,
2109 enum mirror_flags mflags, const char *victim_file)
2111 struct llapi_layout *layout;
2112 char parent[PATH_MAX];
2113 char victim[PATH_MAX];
2114 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2116 struct ll_ioc_lease *data;
2117 uint16_t mirror_count;
2123 /* check fname contains mirror with mirror_id/comp_id */
2124 layout = llapi_layout_get_by_path(fname, 0);
2127 "error %s: file '%s' couldn't get layout\n",
2132 rc = llapi_layout_sanity(layout, false, true);
2134 llapi_layout_sanity_perror(rc);
2138 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2141 "error %s: file '%s' couldn't get mirror count\n",
2145 if (mirror_count < 2) {
2147 "error %s: file '%s' has %d component, cannot split\n",
2148 progname, fname, mirror_count);
2152 if (mflags & MF_COMP_POOL) {
2153 struct pool_to_id_cbdata data = { .pool = pool };
2155 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2157 mirror_id = data.id;
2158 } else if (mflags & MF_COMP_ID) {
2159 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2160 mirror_id = mirror_id_of(id);
2162 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2166 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2169 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2170 if (mflags & MF_COMP_POOL) {
2172 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2173 progname, fname, pool);
2175 } else if (mflags & MF_COMP_ID) {
2177 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2178 progname, fname, id);
2182 "error %s: file '%s' does not contain mirror with id %u\n",
2183 progname, fname, id);
2188 fd = open(fname, O_RDWR);
2191 "error %s: open file '%s' failed: %s\n",
2192 progname, fname, strerror(errno));
2196 /* get victim file directory pathname */
2197 if (strlen(fname) > sizeof(parent) - 1) {
2198 fprintf(stderr, "error %s: file name of '%s' too long\n",
2203 strncpy(parent, fname, sizeof(parent));
2204 ptr = strrchr(parent, '/');
2206 if (!getcwd(parent, sizeof(parent))) {
2207 fprintf(stderr, "error %s: getcwd failed: %s\n",
2208 progname, strerror(errno));
2218 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2220 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2226 /* use a temp file to store the splitted layout */
2227 if (mflags & MF_DESTROY) {
2228 if (last_non_stale_mirror(mirror_id, layout)) {
2231 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2236 fdv = llapi_create_volatile_idx(parent, mdt_index,
2237 O_LOV_DELAY_CREATE);
2239 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2241 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2244 /* user specified victim file */
2245 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2250 "error %s: create victim file failed: %s\n",
2251 progname, strerror(errno));
2255 /* get lease lock of fname */
2256 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2259 "error %s: cannot get lease of file '%s': %d\n",
2260 progname, fname, rc);
2264 /* Atomatically put lease, split layouts and close. */
2265 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2271 data->lil_mode = LL_LEASE_UNLCK;
2272 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2273 data->lil_count = 2;
2274 data->lil_ids[0] = fdv;
2275 data->lil_ids[1] = mirror_id;
2276 rc = llapi_lease_set(fd, data);
2278 if (rc == 0) /* lost lease lock */
2281 "error %s: cannot split '%s': %s\n",
2282 progname, fname, strerror(-rc));
2293 llapi_layout_free(layout);
2298 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2299 __u16 *mirror_ids, int ids_nr);
2301 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2302 __u64 migration_flags,
2303 struct llapi_stripe_param *param,
2304 struct llapi_layout *layout)
2306 struct ll_ioc_lease *data = NULL;
2309 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2311 error_loc = "cannot get lease";
2315 /* Atomically put lease, merge layouts, resync and close. */
2316 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2318 error_loc = "memory allocation";
2321 data->lil_mode = LL_LEASE_UNLCK;
2322 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2323 data->lil_count = 1;
2324 data->lil_ids[0] = fdv;
2325 rc = llapi_lease_set(fd, data);
2327 error_loc = "cannot merge layout";
2329 } else if (rc == 0) {
2331 error_loc = "lost lease lock";
2337 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2339 error_loc = "cannot resync file";
2343 /* delete first mirror now */
2344 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2346 error_loc = "cannot delete old layout";
2354 fprintf(stderr, "error: %s: %s: %s: %s\n",
2355 progname, name, error_loc, strerror(-rc));
2356 else if (migration_flags & MIGRATION_VERBOSE)
2357 printf("%s\n", name);
2364 * Parse a string containing an target index list into an array of integers.
2366 * The input string contains a comma delimited list of individual
2367 * indices and ranges, for example "1,2-4,7". Add the indices into the
2368 * \a tgts array and remove duplicates.
2370 * \param[out] tgts array to store indices in
2371 * \param[in] size size of \a tgts array
2372 * \param[in] offset starting index in \a tgts
2373 * \param[in] arg string containing OST index list
2374 * \param[in/out] overstriping index list may contain duplicates
2376 * \retval positive number of indices in \a tgts
2377 * \retval -EINVAL unable to parse \a arg
2379 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2380 unsigned long long *pattern)
2384 int slots = size - offset;
2386 bool overstriped = false;
2392 end_of_loop = false;
2393 while (!end_of_loop) {
2394 int start_index = 0;
2397 char *endptr = NULL;
2401 ptr = strchrnul(arg, ',');
2403 end_of_loop = *ptr == '\0';
2406 start_index = strtol(arg, &endptr, 0);
2407 if (endptr == arg) /* no data at all */
2409 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2412 end_index = start_index;
2413 if (*endptr == '-') {
2414 end_index = strtol(endptr + 1, &endptr, 0);
2415 if (*endptr != '\0')
2417 if (end_index < start_index)
2421 for (i = start_index; i <= end_index && slots > 0; i++) {
2424 /* remove duplicate */
2425 for (j = 0; j < offset; j++) {
2426 if (tgts[j] == i && pattern &&
2427 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2429 else if (tgts[j] == i)
2435 if (j == offset) { /* check complete */
2441 if (slots == 0 && i < end_index)
2449 if (!end_of_loop && ptr)
2452 if (!overstriped && pattern)
2453 *pattern = LLAPI_LAYOUT_DEFAULT;
2455 return rc < 0 ? rc : nr;
2458 struct lfs_setstripe_args {
2459 unsigned long long lsa_comp_end;
2460 unsigned long long lsa_stripe_size;
2461 unsigned long long lsa_extension_size;
2462 long long lsa_stripe_count;
2463 long long lsa_stripe_off;
2464 __u32 lsa_comp_flags;
2465 __u32 lsa_comp_neg_flags;
2466 unsigned long long lsa_pattern;
2467 unsigned int lsa_mirror_count;
2469 bool lsa_first_comp;
2470 bool lsa_extension_comp;
2472 char *lsa_pool_name;
2475 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2477 unsigned int mirror_count = lsa->lsa_mirror_count;
2478 bool first_comp = lsa->lsa_first_comp;
2480 memset(lsa, 0, sizeof(*lsa));
2482 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2483 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2484 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2485 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2486 lsa->lsa_pool_name = NULL;
2488 lsa->lsa_mirror_count = mirror_count;
2489 lsa->lsa_first_comp = first_comp;
2493 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2494 * @lsa: Stripe options to be initialized and inherited.
2496 * This function initializes stripe options in @lsa and inherit
2497 * stripe_size, stripe_count and OST pool_name options.
2501 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2503 unsigned long long stripe_size;
2504 long long stripe_count;
2505 char *pool_name = NULL;
2507 stripe_size = lsa->lsa_stripe_size;
2508 stripe_count = lsa->lsa_stripe_count;
2509 pool_name = lsa->lsa_pool_name;
2511 setstripe_args_init(lsa);
2513 lsa->lsa_stripe_size = stripe_size;
2514 lsa->lsa_stripe_count = stripe_count;
2515 lsa->lsa_pool_name = pool_name;
2518 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2520 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2521 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2522 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2523 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2524 lsa->lsa_comp_end != 0);
2528 * comp_args_to_layout() - Create or extend a composite layout.
2529 * @composite: Pointer to the composite layout.
2530 * @lsa: Stripe options for the new component.
2532 * This function creates or extends a composite layout by adding a new
2533 * component with stripe options from @lsa.
2535 * Return: 0 on success or an error code on failure.
2537 static int comp_args_to_layout(struct llapi_layout **composite,
2538 struct lfs_setstripe_args *lsa,
2541 struct llapi_layout *layout = *composite;
2542 uint64_t prev_end = 0;
2548 layout = llapi_layout_alloc();
2550 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2555 *composite = layout;
2556 lsa->lsa_first_comp = true;
2561 * Get current component extent, current component
2562 * must be the tail component.
2564 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2566 fprintf(stderr, "Get comp extent failed. %s\n",
2571 if (lsa->lsa_first_comp) {
2573 rc = llapi_layout_add_first_comp(layout);
2575 rc = llapi_layout_comp_add(layout);
2578 fprintf(stderr, "Add component failed. %s\n",
2584 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2586 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2587 lsa->lsa_comp_flags, strerror(errno));
2592 uint64_t comp_end = lsa->lsa_comp_end;
2595 * The extendable component is 0-length, so it can be removed
2596 * if there is insufficient space to extend it.
2598 if (lsa->lsa_extension_comp)
2599 comp_end = prev_end;
2601 rc = llapi_layout_comp_extent_set(layout, prev_end,
2604 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2605 prev_end, comp_end, strerror(errno));
2609 /* reset lsa_first_comp */
2610 lsa->lsa_first_comp = false;
2612 /* Data-on-MDT component setting */
2613 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2615 * In case of Data-on-MDT patterns the only extra option
2616 * applicable is stripe size option.
2618 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2620 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2621 lsa->lsa_stripe_count);
2625 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2627 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2628 lsa->lsa_stripe_size);
2632 if (lsa->lsa_nr_tgts != 0) {
2634 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2639 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2641 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2642 lsa->lsa_stripe_off);
2646 if (lsa->lsa_pool_name != 0) {
2648 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2649 lsa->lsa_pool_name);
2654 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2656 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2661 /* Data-on-MDT component has always single stripe up to end */
2662 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2663 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2664 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2666 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2673 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2674 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2676 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2677 rc = llapi_layout_extension_size_set(layout, size);
2679 rc = llapi_layout_stripe_size_set(layout, size);
2682 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2683 size, strerror(errno));
2687 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2689 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2690 lsa->lsa_stripe_count, strerror(errno));
2694 if (lsa->lsa_pool_name) {
2695 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2697 fprintf(stderr, "Set pool name: %s failed. %s\n",
2698 lsa->lsa_pool_name, strerror(errno));
2702 rc = llapi_layout_pool_name_set(layout, "");
2704 fprintf(stderr, "Clear pool name failed: %s\n",
2710 if (lsa->lsa_nr_tgts > 0) {
2711 if (lsa->lsa_stripe_count > 0 &&
2712 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2713 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2714 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2715 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2716 lsa->lsa_stripe_count,
2721 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2722 rc = llapi_layout_ost_index_set(layout, i,
2727 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2728 lsa->lsa_stripe_off != -1) {
2729 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2732 fprintf(stderr, "Set ost index %d failed. %s\n",
2733 i, strerror(errno));
2737 /* Create the second, virtual component of extension space */
2738 if (lsa->lsa_extension_comp) {
2739 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2740 lsa->lsa_extension_comp = false;
2747 static int build_component(struct llapi_layout **layout,
2748 struct lfs_setstripe_args *lsa, bool set_extent)
2752 rc = comp_args_to_layout(layout, lsa, set_extent);
2756 if (lsa->lsa_mirror_count > 0) {
2757 rc = llapi_layout_mirror_count_set(*layout,
2758 lsa->lsa_mirror_count);
2762 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2765 lsa->lsa_mirror_count = 0;
2771 static int build_prev_component(struct llapi_layout **layout,
2772 struct lfs_setstripe_args *prev,
2773 struct lfs_setstripe_args *lsa,
2776 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2779 if (prev->lsa_stripe_size) {
2781 prev->lsa_comp_end = lsa->lsa_comp_end;
2782 prev->lsa_extension_size = lsa->lsa_extension_size;
2783 prev->lsa_extension_comp = true;
2786 rc = build_component(layout, prev, true);
2792 * Copy lsa to previous lsa;
2793 * if this is an extension component, make the previous invalid;
2796 prev->lsa_stripe_size = 0;
2803 static int build_layout_from_yaml_node(struct cYAML *node,
2804 struct llapi_layout **layout,
2805 struct lfs_setstripe_args *lsa,
2806 struct lfs_setstripe_args *prevp)
2808 struct lfs_setstripe_args prev = { 0 };
2809 __u32 *osts = lsa->lsa_tgts;
2817 string = node->cy_string;
2819 if (node->cy_type == CYAML_TYPE_OBJECT) {
2820 /* go deep to sub blocks */
2821 if (string && !strncmp(string, "component", 9) &&
2822 strncmp(string, "component0", 10) &&
2823 strncmp(string, "components", 10)) {
2824 rc = build_prev_component(layout, prevp, lsa,
2829 /* initialize lsa. */
2830 setstripe_args_init(lsa);
2831 lsa->lsa_first_comp = false;
2832 lsa->lsa_tgts = osts;
2835 rc = build_layout_from_yaml_node(node->cy_child, layout,
2840 if (!node->cy_string)
2843 /* skip leading lmm_ if present, to simplify parsing */
2844 if (strncmp(string, "lmm_", 4) == 0)
2847 if (node->cy_type == CYAML_TYPE_STRING) {
2848 if (!strcmp(string, "lcme_extent.e_end")) {
2849 if (!strcmp(node->cy_valuestring, "EOF") ||
2850 !strcmp(node->cy_valuestring, "eof"))
2851 lsa->lsa_comp_end = LUSTRE_EOF;
2852 } else if (!strcmp(string, "pool")) {
2853 lsa->lsa_pool_name = node->cy_valuestring;
2854 } else if (!strcmp(string, "pattern")) {
2855 if (!strcmp(node->cy_valuestring, "mdt"))
2856 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2857 if (!strcmp(node->cy_valuestring,
2858 "raid0,overstriped"))
2860 LLAPI_LAYOUT_OVERSTRIPING;
2861 } else if (!strcmp(string, "lcme_flags")) {
2862 rc = comp_str2flags(node->cy_valuestring,
2863 &lsa->lsa_comp_flags,
2864 &lsa->lsa_comp_neg_flags);
2868 * Only template flags have meaning in
2869 * the layout for a new file
2871 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2873 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2874 if (!strcmp(string, "lcm_mirror_count")) {
2875 lsa->lsa_mirror_count = node->cy_valueint;
2876 } else if (!strcmp(string, "lcme_extent.e_start")) {
2877 if (node->cy_valueint == 0)
2878 lsa->lsa_first_comp = true;
2879 } else if (!strcmp(string, "lcme_extent.e_end")) {
2880 if (node->cy_valueint == -1)
2881 lsa->lsa_comp_end = LUSTRE_EOF;
2883 lsa->lsa_comp_end = node->cy_valueint;
2884 } else if (!strcmp(string, "stripe_count")) {
2885 lsa->lsa_stripe_count = node->cy_valueint;
2886 } else if (!strcmp(string, "stripe_size")) {
2887 lsa->lsa_stripe_size = node->cy_valueint;
2888 } else if (!strcmp(string, "extension_size")) {
2889 lsa->lsa_extension_size = node->cy_valueint;
2890 lsa->lsa_extension_comp = true;
2891 } else if (!strcmp(string, "stripe_offset")) {
2892 lsa->lsa_stripe_off = node->cy_valueint;
2893 } else if (!strcmp(string, "l_ost_idx")) {
2894 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2899 node = node->cy_next;
2902 if (prevp == &prev) {
2903 rc = build_prev_component(layout, prevp, lsa, true);
2907 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2908 rc = build_component(layout, lsa, *layout != NULL);
2914 static int lfs_comp_create_from_yaml(char *template,
2915 struct llapi_layout **layout,
2916 struct lfs_setstripe_args *lsa,
2919 struct cYAML *tree = NULL, *err_rc = NULL;
2922 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2924 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2925 progname, template);
2926 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2927 "can't parse", &err_rc);
2928 cYAML_print_tree2file(stderr, err_rc);
2929 cYAML_free_tree(err_rc);
2934 /* initialize lsa for plain file */
2935 setstripe_args_init(lsa);
2936 lsa->lsa_tgts = osts;
2938 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2940 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2941 progname, template);
2944 /* clean clean lsa */
2945 setstripe_args_init(lsa);
2949 cYAML_free_tree(tree);
2954 * Get the extension size from the next (SEL) component and extend the
2955 * current component on it. The start of the next component is to be
2958 * \param[in] layout the current layout
2959 * \param[in] start the start of the current component
2960 * \param[in,out] end the end of the current component
2961 * \param[in] offset the offset to adjust the end position to instead of
2964 * \retval 0 - extended successfully
2965 * \retval < 0 - error
2967 static int layout_extend_comp(struct llapi_layout *layout,
2968 uint64_t start, uint64_t *end,
2971 uint64_t size, next_start, next_end;
2974 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2977 "%s setstripe: cannot move component cursor: %s\n",
2978 progname, strerror(errno));
2983 * Even if the @size will not be used below, this will fail if
2984 * this is not a SEL component - a good confirmation we are
2985 * working on right components.
2987 rc = llapi_layout_extension_size_get(layout, &size);
2990 "%s setstripe: cannot get component ext size: %s\n",
2991 progname, strerror(errno));
2995 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2997 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2998 progname, strerror(errno));
3002 next_start += offset ?: size;
3003 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3005 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3006 progname, strerror(errno));
3010 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3013 "%s setstripe: cannot move component cursor: %s\n",
3014 progname, strerror(errno));
3018 *end += offset ?: size;
3019 rc = llapi_layout_comp_extent_set(layout, start, *end);
3021 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3022 progname, strerror(errno));
3030 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3031 * end of the last component in the existing file, and adjust the
3032 * first extent start of the components to be added accordingly.
3034 * In the create mode, we need to check if the first component is an extendable
3035 * SEL component and extend its length to the extension size (first component
3036 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3038 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3041 struct llapi_layout *head;
3042 uint64_t start = 0, prev_end = 0;
3046 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3051 head = llapi_layout_get_by_path(fname, 0);
3054 "%s setstripe: cannot read layout from '%s': %s\n",
3055 progname, fname, strerror(errno));
3057 } else if (errno == ENODATA) {
3059 * file without LOVEA, this component-add will be turned
3060 * into a component-create.
3062 llapi_layout_free(head);
3066 * the new layout will be added to an empty one, it
3067 * still needs to be adjusted below
3071 } else if (!llapi_layout_is_composite(head)) {
3073 "%s setstripe: '%s' not a composite file\n",
3075 llapi_layout_free(head);
3079 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3082 "%s setstripe: cannot get prev extent: %s\n",
3083 progname, strerror(errno));
3084 llapi_layout_free(head);
3088 llapi_layout_free(head);
3092 /* Make sure we use the first component of the layout to be added. */
3093 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3096 "%s setstripe: cannot move component cursor: %s\n",
3097 progname, strerror(errno));
3101 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3103 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3104 progname, strerror(errno));
3108 if (start == 0 && end == 0) {
3109 rc = layout_extend_comp(layout, start, &end,
3110 comp_add ? prev_end : 0);
3115 if (start > prev_end || end < prev_end) {
3117 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3118 progname, start, end, prev_end);
3122 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3125 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3126 progname, prev_end, end, strerror(errno));
3133 static int mirror_adjust_first_extents(struct mirror_args *list)
3141 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3144 list = list->m_next;
3150 static inline bool arg_is_eof(char *arg)
3152 return !strncmp(arg, "-1", strlen("-1")) ||
3153 !strncmp(arg, "EOF", strlen("EOF")) ||
3154 !strncmp(arg, "eof", strlen("eof"));
3158 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3160 * Return: Valid mirror_args pointer on success and
3161 * NULL if memory allocation fails.
3163 static struct mirror_args *lfs_mirror_alloc(void)
3165 struct mirror_args *mirror = NULL;
3168 mirror = calloc(1, sizeof(*mirror));
3170 mirror->m_inherit = false;
3181 * lfs_mirror_free() - Free memory allocated for a mirror argument
3183 * @mirror: Previously allocated mirror argument structure by
3184 * lfs_mirror_alloc().
3186 * Free memory allocated for @mirror.
3190 static void lfs_mirror_free(struct mirror_args *mirror)
3192 if (mirror->m_layout)
3193 llapi_layout_free(mirror->m_layout);
3198 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3199 * @mirror_list: Previously allocated mirror list.
3201 * Free memory allocated for @mirror_list.
3205 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3207 struct mirror_args *next_mirror = NULL;
3209 while (mirror_list) {
3210 next_mirror = mirror_list->m_next;
3211 lfs_mirror_free(mirror_list);
3212 mirror_list = next_mirror;
3224 LFS_COMP_NO_VERIFY_OPT,
3226 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3228 LFS_MIRROR_STATE_OPT,
3230 LFS_MIRROR_INDEX_OPT,
3231 LFS_LAYOUT_FOREIGN_OPT,
3237 static int lfs_setstripe_internal(int argc, char **argv,
3238 enum setstripe_origin opc)
3240 struct lfs_setstripe_args lsa = { 0 };
3241 struct llapi_stripe_param *param = NULL;
3242 struct find_param migrate_mdt_param = {
3252 unsigned long long size_units = 1;
3253 bool migrate_mode = false;
3254 bool migrate_mdt_mode = false;
3255 bool setstripe_mode = false;
3256 bool migration_block = false;
3257 __u64 migration_flags = 0;
3258 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3259 int comp_del = 0, comp_set = 0;
3262 struct llapi_layout *layout = NULL;
3263 struct llapi_layout **lpp = &layout;
3264 bool mirror_mode = false;
3265 bool has_m_file = false;
3266 __u32 mirror_count = 0;
3267 enum mirror_flags mirror_flags = 0;
3268 struct mirror_args *mirror_list = NULL;
3269 struct mirror_args *new_mirror = NULL;
3270 struct mirror_args *last_mirror = NULL;
3271 __u16 mirror_id = 0;
3273 bool from_yaml = false;
3274 bool from_copy = false;
3275 char *template = NULL;
3276 bool foreign_mode = false;
3278 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3279 char *mode_opt = NULL;
3280 mode_t previous_umask = 0;
3281 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3283 struct option long_opts[] = {
3284 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3285 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3286 /* --block is only valid in migrate mode */
3287 { .val = 'b', .name = "block", .has_arg = no_argument },
3288 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3289 { .val = LFS_COMP_ADD_OPT,
3290 .name = "comp-add", .has_arg = no_argument },
3291 { .val = LFS_COMP_ADD_OPT,
3292 .name = "component-add", .has_arg = no_argument },
3293 { .val = LFS_COMP_DEL_OPT,
3294 .name = "comp-del", .has_arg = no_argument },
3295 { .val = LFS_COMP_DEL_OPT,
3296 .name = "component-del", .has_arg = no_argument },
3297 { .val = LFS_COMP_FLAGS_OPT,
3298 .name = "comp-flags", .has_arg = required_argument },
3299 { .val = LFS_COMP_FLAGS_OPT,
3300 .name = "component-flags",
3301 .has_arg = required_argument },
3302 { .val = LFS_COMP_SET_OPT,
3303 .name = "comp-set", .has_arg = no_argument },
3304 { .val = LFS_COMP_SET_OPT,
3305 .name = "component-set",
3306 .has_arg = no_argument},
3307 { .val = LFS_COMP_NO_VERIFY_OPT,
3308 .name = "no-verify", .has_arg = no_argument},
3309 { .val = LFS_LAYOUT_FLAGS_OPT,
3310 .name = "flags", .has_arg = required_argument},
3311 { .val = LFS_LAYOUT_FOREIGN_OPT,
3312 .name = "foreign", .has_arg = optional_argument},
3313 { .val = LFS_MIRROR_ID_OPT,
3314 .name = "mirror-id", .has_arg = required_argument},
3315 { .val = LFS_MODE_OPT,
3316 .name = "mode", .has_arg = required_argument},
3317 { .val = LFS_LAYOUT_COPY,
3318 .name = "copy", .has_arg = required_argument},
3319 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3320 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3321 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3322 { .val = 'C', .name = "overstripe-count",
3323 .has_arg = required_argument},
3324 { .val = 'd', .name = "delete", .has_arg = no_argument},
3325 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3326 /* --non-direct is only valid in migrate mode */
3327 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3328 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3329 { .val = 'E', .name = "component-end",
3330 .has_arg = required_argument},
3331 { .val = 'f', .name = "file", .has_arg = required_argument },
3332 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3333 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3334 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3335 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3336 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3337 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3338 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3339 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3340 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3341 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3342 { .val = 'L', .name = "layout", .has_arg = required_argument },
3343 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3344 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3345 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3346 /* --non-block is only valid in migrate mode */
3347 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3348 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3349 { .val = 'o', .name = "ost", .has_arg = required_argument },
3350 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3351 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3352 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3354 { .val = 'p', .name = "pool", .has_arg = required_argument },
3355 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3356 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3357 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3358 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3359 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3360 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3361 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3362 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3363 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3364 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3365 /* --verbose is only valid in migrate mode */
3366 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3367 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3368 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3369 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3370 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3373 setstripe_args_init(&lsa);
3375 migrate_mode = (opc == SO_MIGRATE);
3376 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3377 setstripe_mode = (opc == SO_SETSTRIPE);
3378 if (opc == SO_MIRROR_DELETE) {
3380 mirror_flags = MF_DESTROY;
3383 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3385 while ((c = getopt_long(argc, argv,
3386 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3387 long_opts, NULL)) >= 0) {
3393 case LFS_COMP_ADD_OPT:
3396 case LFS_COMP_DEL_OPT:
3399 case LFS_COMP_FLAGS_OPT:
3400 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3401 &lsa.lsa_comp_neg_flags);
3404 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3406 "%s: inverted flags are not supported\n",
3411 case LFS_COMP_SET_OPT:
3414 case LFS_COMP_NO_VERIFY_OPT:
3415 mirror_flags |= MF_NO_VERIFY;
3417 case LFS_MIRROR_ID_OPT: {
3418 unsigned long int id;
3421 id = strtoul(optarg, &end, 0);
3422 if (errno != 0 || *end != '\0' || id == 0 ||
3425 "%s %s: invalid mirror ID '%s'\n",
3426 progname, argv[0], optarg);
3430 mirror_id = (__u16)id;
3433 case LFS_LAYOUT_FLAGS_OPT: {
3436 /* check for numeric flags (foreign and mirror cases) */
3437 if (setstripe_mode && !mirror_mode && !last_mirror) {
3439 flags = strtoul(optarg, &end, 16);
3440 if (errno != 0 || *end != '\0' ||
3441 flags >= UINT32_MAX) {
3443 "%s %s: invalid hex flags '%s'\n",
3444 progname, argv[0], optarg);
3447 if (!foreign_mode) {
3449 "%s %s: hex flags must be specified with --foreign option\n",
3456 if (!mirror_mode || !last_mirror) {
3458 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3463 result = comp_str2flags(optarg, &last_mirror->m_flags,
3470 "%s: inverted flags are not supported\n",
3475 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3477 "%s: unsupported mirror flags: %s\n",
3484 case LFS_LAYOUT_FOREIGN_OPT:
3486 /* check pure numeric */
3487 type = strtoul(optarg, &end, 0);
3490 type = check_foreign_type_name(optarg);
3491 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3493 "%s %s: unrecognized foreign type '%s'\n",
3498 } else if (type >= UINT32_MAX) {
3500 "%s %s: invalid foreign type '%s'\n",
3501 progname, argv[0], optarg);
3505 foreign_mode = true;
3510 mode = strtoul(mode_opt, &end, 8);
3513 "%s %s: bad mode '%s'\n",
3514 progname, argv[0], mode_opt);
3517 previous_umask = umask(0);
3520 case LFS_LAYOUT_COPY:
3525 if (!migrate_mode) {
3527 "%s %s: -b|--block valid only for migrate command\n",
3531 migration_block = true;
3534 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3536 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3540 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3544 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3545 if (errno != 0 || *end != '\0'||
3546 lsa.lsa_stripe_count < -1 ||
3547 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3549 "%s %s: invalid stripe count '%s'\n",
3550 progname, argv[0], optarg);
3554 if (lsa.lsa_stripe_count == -1)
3555 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3558 /* delete the default striping pattern */
3560 if (opc == SO_MIRROR_SPLIT) {
3563 "%s %s: -d cannot used with -f\n",
3567 mirror_flags |= MF_DESTROY;
3571 if (!migrate_mode) {
3573 "%s %s: -D|--non-direct is valid only for migrate command\n",
3577 migration_flags |= MIGRATION_NONDIRECT;
3580 if (lsa.lsa_comp_end != 0) {
3581 result = comp_args_to_layout(lpp, &lsa, true);
3583 fprintf(stderr, "%s: invalid layout\n",
3588 setstripe_args_init_inherit(&lsa);
3591 if (arg_is_eof(optarg)) {
3592 lsa.lsa_comp_end = LUSTRE_EOF;
3594 result = llapi_parse_size(optarg,
3597 /* assume units of KB if too small */
3598 if (lsa.lsa_comp_end < 4096)
3599 lsa.lsa_comp_end *= 1024;
3601 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3603 "%s %s: invalid component end '%s'\n",
3604 progname, argv[0], optarg);
3610 if (!migrate_mode) {
3612 "--mdt-hash is valid only for migrate command\n");
3616 lsa.lsa_pattern = check_hashtype(optarg);
3617 if (lsa.lsa_pattern == 0) {
3619 "%s %s: bad stripe hash type '%s'\n",
3620 progname, argv[0], optarg);
3625 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3628 "%s %s: invalid stripe offset '%s'\n",
3629 progname, argv[0], optarg);
3632 if (lsa.lsa_stripe_off == -1)
3633 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3636 comp_id = strtoul(optarg, &end, 0);
3637 if (*end != '\0' || comp_id == 0 ||
3638 comp_id > LCME_ID_MAX) {
3640 "%s %s: invalid component ID '%s'\n",
3641 progname, argv[0], optarg);
3646 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3648 "error: %s: invalid option: %s\n",
3649 progname, argv[optopt + 1]);
3652 if (opc == SO_MIRROR_EXTEND) {
3655 "error: %s: '-N' must exist in front of '%s'\n",
3656 progname, argv[optopt + 1]);
3659 last_mirror->m_file = optarg;
3660 last_mirror->m_count = 1;
3664 mirror_list = lfs_mirror_alloc();
3665 mirror_list->m_file = optarg;
3670 if (strcmp(argv[optind - 1], "mdt") == 0) {
3671 /* Can be only the first component */
3675 "error: 'mdt' layout can be only the first one\n");
3678 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3681 "error: 'mdt' layout size is too big\n");
3684 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3685 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3688 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3694 if (!migrate_mode) {
3696 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3700 migrate_mdt_mode = true;
3701 lsa.lsa_nr_tgts = parse_targets(tgts,
3702 sizeof(tgts) / sizeof(__u32),
3703 lsa.lsa_nr_tgts, optarg, NULL);
3704 if (lsa.lsa_nr_tgts < 0) {
3706 "%s %s: invalid MDT target(s) '%s'\n",
3707 progname, argv[0], optarg);
3711 lsa.lsa_tgts = tgts;
3712 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3713 lsa.lsa_stripe_off = tgts[0];
3716 if (!migrate_mode) {
3718 "%s %s: -n|--non-block valid only for migrate command\n",
3722 migration_flags |= MIGRATION_NONBLOCK;
3725 if (opc == SO_SETSTRIPE) {
3726 opc = SO_MIRROR_CREATE;
3732 mirror_count = strtoul(optarg, &end, 0);
3733 if (errno != 0 || *end != '\0' ||
3734 mirror_count == 0 ||
3735 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3737 "error: %s: bad mirror count: %s\n",
3744 new_mirror = lfs_mirror_alloc();
3745 new_mirror->m_count = mirror_count;
3748 mirror_list = new_mirror;
3751 /* wrap up last mirror */
3752 if (!setstripe_args_specified(&lsa))
3753 last_mirror->m_inherit = true;
3754 if (lsa.lsa_comp_end == 0)
3755 lsa.lsa_comp_end = LUSTRE_EOF;
3757 result = comp_args_to_layout(lpp, &lsa, true);
3759 lfs_mirror_free(new_mirror);
3763 setstripe_args_init_inherit(&lsa);
3765 last_mirror->m_next = new_mirror;
3768 last_mirror = new_mirror;
3769 lpp = &last_mirror->m_layout;
3772 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3773 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3775 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3777 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3779 "%s %s: -o|--ost incompatible with DoM layout\n",
3784 * -o allows overstriping, and must note it because
3785 * parse_targets is shared with MDT striping, which
3786 * does not allow duplicates
3788 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3789 lsa.lsa_nr_tgts = parse_targets(tgts,
3790 sizeof(tgts) / sizeof(__u32),
3791 lsa.lsa_nr_tgts, optarg,
3793 if (lsa.lsa_nr_tgts < 0) {
3795 "%s %s: invalid OST target(s) '%s'\n",
3796 progname, argv[0], optarg);
3800 lsa.lsa_tgts = tgts;
3801 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3802 lsa.lsa_stripe_off = tgts[0];
3807 lsa.lsa_pool_name = optarg;
3809 if (strlen(lsa.lsa_pool_name) == 0 ||
3810 strncmp(lsa.lsa_pool_name, "none",
3811 LOV_MAXPOOLNAME) == 0)
3812 lsa.lsa_pool_name = NULL;
3815 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3817 /* assume units of KB if too small to be valid */
3818 if (lsa.lsa_stripe_size < 4096)
3819 lsa.lsa_stripe_size *= 1024;
3821 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3823 "%s %s: invalid stripe size '%s'\n",
3824 progname, argv[0], optarg);
3829 if (!migrate_mode) {
3831 "%s %s: -v|--verbose valid only for migrate command\n",
3835 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3836 migration_flags = MIGRATION_VERBOSE;
3846 result = llapi_parse_size(optarg,
3847 &lsa.lsa_extension_size,
3851 "%s %s: invalid extension size '%s'\n",
3852 progname, argv[0], optarg);
3856 lsa.lsa_extension_comp = true;
3859 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3860 progname, argv[0], argv[optind - 1]);
3865 fname = argv[optind];
3867 if (optind == argc) {
3868 fprintf(stderr, "%s %s: FILE must be specified\n",
3873 /* lfs migrate $filename should keep the file's layout by default */
3874 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3878 if (xattr && !foreign_mode) {
3880 * only print a warning as this is harmless and will be ignored
3883 "%s %s: xattr has been specified for non-foreign layout\n",
3885 } else if (foreign_mode && !xattr) {
3887 "%s %s: xattr must be provided in foreign mode\n",
3892 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3893 comp_set || comp_id || delete || from_copy ||
3894 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3897 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3902 if (mirror_mode && mirror_count == 0) {
3904 "error: %s: --mirror-count|-N option is required\n",
3911 if (!setstripe_args_specified(&lsa))
3912 last_mirror->m_inherit = true;
3913 if (lsa.lsa_comp_end == 0)
3914 lsa.lsa_comp_end = LUSTRE_EOF;
3917 if (lsa.lsa_comp_end != 0) {
3918 result = comp_args_to_layout(lpp, &lsa, true);
3920 fprintf(stderr, "error: %s: invalid layout\n",
3927 if (mirror_flags & MF_NO_VERIFY) {
3928 if (opc != SO_MIRROR_EXTEND) {
3930 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3934 } else if (!has_m_file) {
3936 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3943 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
3945 "%s %s: --component-set doesn't have component-id set\n",
3950 if ((delete + comp_set + comp_del + comp_add) > 1) {
3952 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3957 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3958 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3960 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3965 if ((comp_set || comp_del) &&
3966 (setstripe_args_specified(&lsa) || layout != NULL)) {
3968 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3973 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3975 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3980 if (comp_add || comp_del) {
3983 result = lstat(fname, &st);
3984 if (result == 0 && S_ISDIR(st.st_mode)) {
3986 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3993 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4002 "%s %s: option -E must be specified with --component-add\n",
4008 if (from_yaml && from_copy) {
4010 "%s: can't specify --yaml and --copy together\n",
4015 if ((from_yaml || from_copy) &&
4016 (setstripe_args_specified(&lsa) || layout != NULL)) {
4018 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4023 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4025 "%s %s: options --non-block and --block are mutually exclusive\n",
4030 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4031 opc != SO_MIRROR_DELETE && comp_id != 0) {
4033 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4038 if (migrate_mdt_mode) {
4039 struct lmv_user_md *lmu;
4041 /* initialize migrate mdt parameters */
4042 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4043 LMV_USER_MAGIC_SPECIFIC));
4046 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4047 progname, argv[0], strerror(ENOMEM));
4051 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4052 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4053 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4055 "%s %s: migrate should specify MDT index\n",
4060 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4061 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4062 lmu->lum_hash_type = lsa.lsa_pattern;
4064 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4065 if (lsa.lsa_pool_name) {
4066 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4067 sizeof(lmu->lum_pool_name) - 1);
4068 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4070 if (lsa.lsa_nr_tgts > 1) {
4073 if (lsa.lsa_stripe_count > 0 &&
4074 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4075 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4077 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4078 progname, lsa.lsa_stripe_count,
4084 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4085 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4086 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4087 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4089 lmu->lum_magic = LMV_USER_MAGIC;
4092 migrate_mdt_param.fp_lmv_md = lmu;
4093 migrate_mdt_param.fp_migrate = 1;
4094 } else if (!layout) {
4095 /* initialize stripe parameters */
4096 param = calloc(1, offsetof(typeof(*param),
4097 lsp_osts[lsa.lsa_nr_tgts]));
4100 "%s %s: cannot allocate memory for parameters: %s\n",
4101 progname, argv[0], strerror(ENOMEM));
4106 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4107 param->lsp_stripe_size = lsa.lsa_stripe_size;
4108 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4109 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4110 param->lsp_stripe_count = -1;
4112 param->lsp_stripe_count = lsa.lsa_stripe_count;
4114 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4115 param->lsp_stripe_offset = -1;
4117 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4118 param->lsp_stripe_pattern =
4119 llapi_pattern_to_lov(lsa.lsa_pattern);
4120 if (param->lsp_stripe_pattern == EINVAL) {
4121 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4126 param->lsp_pool = lsa.lsa_pool_name;
4127 param->lsp_is_specific = false;
4128 if (lsa.lsa_nr_tgts > 0) {
4129 if (lsa.lsa_stripe_count > 0 &&
4130 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4131 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4132 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4134 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4135 argv[0], lsa.lsa_stripe_count,
4141 param->lsp_is_specific = true;
4142 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4143 memcpy(param->lsp_osts, tgts,
4144 sizeof(*tgts) * lsa.lsa_nr_tgts);
4149 /* generate a layout from a YAML template */
4150 result = lfs_comp_create_from_yaml(template, &layout,
4154 "error: %s: can't create composite layout from template file %s\n",
4160 if (layout != NULL || mirror_list != NULL) {
4162 result = mirror_adjust_first_extents(mirror_list);
4164 result = layout_adjust_first_extent(fname, layout,
4166 if (result == -ENODATA)
4168 else if (result != 0) {
4169 fprintf(stderr, "error: %s: invalid layout\n",
4175 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4177 layout = llapi_layout_get_by_path(template ?: fname, 0);
4180 "%s: can't create composite layout from file %s: %s\n",
4181 progname, template ?: fname,
4188 if (migrate_mdt_mode) {
4189 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4190 } else if (migrate_mode) {
4191 result = lfs_migrate(fname, migration_flags, param,
4193 } else if (comp_set != 0) {
4194 result = lfs_component_set(fname, comp_id,
4197 lsa.lsa_comp_neg_flags);
4198 } else if (comp_del != 0) {
4199 result = lfs_component_del(fname, comp_id,
4201 lsa.lsa_comp_neg_flags);
4202 } else if (comp_add != 0) {
4203 result = lfs_component_add(fname, layout);
4204 } else if (opc == SO_MIRROR_CREATE) {
4205 result = mirror_create(fname, mirror_list);
4206 } else if (opc == SO_MIRROR_EXTEND) {
4207 result = mirror_extend(fname, mirror_list,
4209 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4210 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4212 "%s: no mirror specified to delete from '%s'\n",
4216 if (lsa.lsa_pool_name)
4217 mirror_flags |= MF_COMP_POOL;
4218 else if (mirror_id != 0)
4219 comp_id = mirror_id;
4221 mirror_flags |= MF_COMP_ID;
4222 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4224 has_m_file ? mirror_list->m_file :
4226 } else if (layout) {
4227 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4233 } else if (foreign_mode) {
4234 result = llapi_file_create_foreign(fname, mode, type,
4241 result = llapi_file_open_param(fname,
4250 /* Save the first error encountered. */
4258 umask(previous_umask);
4261 free(migrate_mdt_param.fp_lmv_md);
4262 llapi_layout_free(layout);
4263 lfs_mirror_list_free(mirror_list);
4268 llapi_layout_free(layout);
4269 lfs_mirror_list_free(mirror_list);
4273 static int lfs_poollist(int argc, char **argv)
4278 return llapi_poollist(argv[1]);
4281 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4282 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4287 char *endptr = "AD";
4292 else if (str[0] == '-')
4298 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4299 long long val = strtoll(timebuf, &endptr, 0);
4304 unit *= 52; /* 52 weeks + 1 day below */
4305 case 'w': /* fallthrough */
4307 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4308 param->fp_time_margin *= (1 + unit / 52);
4309 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4310 case '\0': /* days are default unit if none used */
4311 case 'd': /* fallthrough */
4313 case 'h': /* fallthrough */
4315 case 'm': /* fallthrough */
4317 case 's': /* fallthrough */
4319 /* don't need to multiply by 1 for seconds */
4322 "%s find: bad time string '%s': %s\n",
4323 progname, timebuf, strerror(EINVAL));
4327 if (param->fp_time_margin == 0 ||
4328 (*endptr && unit < param->fp_time_margin))
4329 param->fp_time_margin = unit;
4336 fprintf(stderr, "%s find: bad time '%s': too large\n",
4346 static int str2quotaid(__u32 *id, const char *arg)
4348 unsigned long int projid_tmp = 0;
4349 char *endptr = NULL;
4351 projid_tmp = strtoul(arg, &endptr, 10);
4352 if (*endptr != '\0')
4354 if (projid_tmp >= UINT32_MAX)
4361 static int name2uid(unsigned int *id, const char *name)
4363 struct passwd *passwd;
4365 passwd = getpwnam(name);
4368 *id = passwd->pw_uid;
4373 static int name2gid(unsigned int *id, const char *name)
4375 struct group *group;
4377 group = getgrnam(name);
4380 *id = group->gr_gid;
4385 static inline int name2projid(unsigned int *id, const char *name)
4390 static int uid2name(char **name, unsigned int id)
4392 struct passwd *passwd;
4394 passwd = getpwuid(id);
4397 *name = passwd->pw_name;
4402 static inline int gid2name(char **name, unsigned int id)
4404 struct group *group;
4406 group = getgrgid(id);
4409 *name = group->gr_name;
4414 static int name2layout(__u32 *layout, char *name)
4416 char *ptr, *layout_name;
4419 for (ptr = name; ; ptr = NULL) {
4420 layout_name = strtok(ptr, ",");
4423 if (strcmp(layout_name, "released") == 0)
4424 *layout |= LOV_PATTERN_F_RELEASED;
4425 else if (strcmp(layout_name, "raid0") == 0)
4426 *layout |= LOV_PATTERN_RAID0;
4427 else if (strcmp(layout_name, "mdt") == 0)
4428 *layout |= LOV_PATTERN_MDT;
4429 else if (strcmp(layout_name, "overstriping") == 0)
4430 *layout |= LOV_PATTERN_OVERSTRIPING;
4437 static int lfs_find(int argc, char **argv)
4442 struct find_param param = {
4445 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4447 struct option long_opts[] = {
4448 { .val = 'A', .name = "atime", .has_arg = required_argument },
4449 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4450 { .val = 'B', .name = "btime", .has_arg = required_argument },
4451 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4452 { .val = LFS_COMP_COUNT_OPT,
4453 .name = "comp-count", .has_arg = required_argument },
4454 { .val = LFS_COMP_COUNT_OPT,
4455 .name = "component-count",
4456 .has_arg = required_argument },
4457 { .val = LFS_COMP_FLAGS_OPT,
4458 .name = "comp-flags", .has_arg = required_argument },
4459 { .val = LFS_COMP_FLAGS_OPT,
4460 .name = "component-flags",
4461 .has_arg = required_argument },
4462 { .val = LFS_COMP_START_OPT,
4463 .name = "comp-start", .has_arg = required_argument },
4464 { .val = LFS_COMP_START_OPT,
4465 .name = "component-start",
4466 .has_arg = required_argument },
4467 { .val = LFS_MIRROR_STATE_OPT,
4468 .name = "mirror-state", .has_arg = required_argument },
4469 { .val = LFS_NEWERXY_OPT,
4470 .name = "newer", .has_arg = required_argument},
4471 { .val = LFS_NEWERXY_OPT,
4472 .name = "neweraa", .has_arg = required_argument},
4473 { .val = LFS_NEWERXY_OPT,
4474 .name = "neweram", .has_arg = required_argument},
4475 { .val = LFS_NEWERXY_OPT,
4476 .name = "newerac", .has_arg = required_argument},
4477 { .val = LFS_NEWERXY_OPT,
4478 .name = "newerab", .has_arg = required_argument},
4479 { .val = LFS_NEWERXY_OPT,
4480 .name = "newerma", .has_arg = required_argument},
4481 { .val = LFS_NEWERXY_OPT,
4482 .name = "newermm", .has_arg = required_argument},
4483 { .val = LFS_NEWERXY_OPT,
4484 .name = "newermc", .has_arg = required_argument},
4485 { .val = LFS_NEWERXY_OPT,
4486 .name = "newermb", .has_arg = required_argument},
4487 { .val = LFS_NEWERXY_OPT,
4488 .name = "newerca", .has_arg = required_argument},
4489 { .val = LFS_NEWERXY_OPT,
4490 .name = "newercm", .has_arg = required_argument},
4491 { .val = LFS_NEWERXY_OPT,
4492 .name = "newercc", .has_arg = required_argument},
4493 { .val = LFS_NEWERXY_OPT,
4494 .name = "newercb", .has_arg = required_argument},
4495 { .val = LFS_NEWERXY_OPT,
4496 .name = "newerba", .has_arg = required_argument},
4497 { .val = LFS_NEWERXY_OPT,
4498 .name = "newerbm", .has_arg = required_argument},
4499 { .val = LFS_NEWERXY_OPT,
4500 .name = "newerbc", .has_arg = required_argument},
4501 { .val = LFS_NEWERXY_OPT,
4502 .name = "newerbb", .has_arg = required_argument},
4503 { .val = LFS_NEWERXY_OPT,
4504 .name = "newerBa", .has_arg = required_argument},
4505 { .val = LFS_NEWERXY_OPT,
4506 .name = "newerBm", .has_arg = required_argument},
4507 { .val = LFS_NEWERXY_OPT,
4508 .name = "newerBc", .has_arg = required_argument},
4509 { .val = LFS_NEWERXY_OPT,
4510 .name = "newerBB", .has_arg = required_argument},
4511 { .val = LFS_NEWERXY_OPT,
4512 .name = "newerat", .has_arg = required_argument},
4513 { .val = LFS_NEWERXY_OPT,
4514 .name = "newermt", .has_arg = required_argument},
4515 { .val = LFS_NEWERXY_OPT,
4516 .name = "newerct", .has_arg = required_argument},
4517 { .val = LFS_NEWERXY_OPT,
4518 .name = "newerbt", .has_arg = required_argument},
4519 { .val = LFS_NEWERXY_OPT,
4520 .name = "newerBt", .has_arg = required_argument},
4521 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4522 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4523 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4524 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4525 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4526 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4527 { .val = 'E', .name = "component-end",
4528 .has_arg = required_argument },
4529 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4530 { .val = LFS_LAYOUT_FOREIGN_OPT,
4531 .name = "foreign", .has_arg = optional_argument},
4532 { .val = 'g', .name = "gid", .has_arg = required_argument },
4533 { .val = 'G', .name = "group", .has_arg = required_argument },
4534 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4535 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4536 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4537 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4538 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4539 { .val = 'L', .name = "layout", .has_arg = required_argument },
4540 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4541 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4542 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4543 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4544 { .val = 'n', .name = "name", .has_arg = required_argument },
4545 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4546 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4547 { .val = 'O', .name = "obd", .has_arg = required_argument },
4548 { .val = 'O', .name = "ost", .has_arg = required_argument },
4549 /* no short option for pool yet, can be 'p' after 2.18 */
4550 { .val = LFS_POOL_OPT,
4551 .name = "pool", .has_arg = required_argument },
4552 { .val = '0', .name = "print0", .has_arg = no_argument },
4553 { .val = 'P', .name = "print", .has_arg = no_argument },
4554 { .val = LFS_PROJID_OPT,
4555 .name = "projid", .has_arg = required_argument },
4556 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4557 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4558 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4559 { .val = 's', .name = "size", .has_arg = required_argument },
4560 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4561 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4562 { .val = 't', .name = "type", .has_arg = required_argument },
4563 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4564 { .val = 'u', .name = "uid", .has_arg = required_argument },
4565 { .val = 'U', .name = "user", .has_arg = required_argument },
4566 { .val = 'z', .name = "extension-size",
4567 .has_arg = required_argument },
4568 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4569 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4570 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4584 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4585 while ((c = getopt_long_only(argc, argv,
4586 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4587 long_opts, &optidx)) >= 0) {
4592 /* '!' is part of option */
4594 * when getopt_long_only() finds a string which is not
4595 * an option nor a known option argument it returns 1
4596 * in that case if we already have found pathstart and pathend
4597 * (i.e. we have the list of pathnames),
4598 * the only supported value is "!"
4600 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4601 if (!isoption && pathend != -1) {
4603 "err: %s: filename|dirname must either precede options or follow options\n",
4608 if (!isoption && pathstart == -1)
4609 pathstart = optind - 1;
4610 if (isoption && pathstart != -1 && pathend == -1)
4611 pathend = optind - 2;
4618 * unknown; opt is "!" or path component,
4619 * checking done above.
4621 if (strcmp(optarg, "!") == 0)
4625 xtime = ¶m.fp_atime;
4626 xsign = ¶m.fp_asign;
4627 param.fp_exclude_atime = !!neg_opt;
4628 /* no break, this falls through to 'B' for btime */
4631 xtime = ¶m.fp_btime;
4632 xsign = ¶m.fp_bsign;
4633 param.fp_exclude_btime = !!neg_opt;
4635 /* no break, this falls through to 'C' for ctime */
4638 xtime = ¶m.fp_ctime;
4639 xsign = ¶m.fp_csign;
4640 param.fp_exclude_ctime = !!neg_opt;
4642 /* no break, this falls through to 'M' for mtime */
4645 xtime = ¶m.fp_mtime;
4646 xsign = ¶m.fp_msign;
4647 param.fp_exclude_mtime = !!neg_opt;
4649 rc = set_time(¶m, &t, xtime, optarg);
4650 if (rc == LONG_MAX) {
4658 if (optarg[0] == '+') {
4659 param.fp_blocks_sign = -1;
4661 } else if (optarg[0] == '-') {
4662 param.fp_blocks_sign = 1;
4666 param.fp_blocks_units = 1024;
4667 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4668 ¶m.fp_blocks_units, 0);
4670 fprintf(stderr, "error: bad blocks '%s'\n",
4674 param.fp_check_blocks = 1;
4675 param.fp_exclude_blocks = !!neg_opt;
4677 case LFS_COMP_COUNT_OPT:
4678 if (optarg[0] == '+') {
4679 param.fp_comp_count_sign = -1;
4681 } else if (optarg[0] == '-') {
4682 param.fp_comp_count_sign = 1;
4687 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4688 if (errno != 0 || *endptr != '\0' ||
4689 param.fp_comp_count > UINT32_MAX) {
4691 "error: bad component count '%s'\n",
4695 param.fp_check_comp_count = 1;
4696 param.fp_exclude_comp_count = !!neg_opt;
4698 case LFS_COMP_FLAGS_OPT:
4699 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4700 ¶m.fp_comp_neg_flags);
4703 "error: bad component flags '%s'\n",
4707 param.fp_check_comp_flags = 1;
4709 __u32 flags = param.fp_comp_neg_flags;
4711 param.fp_comp_neg_flags = param.fp_comp_flags;
4712 param.fp_comp_flags = flags;
4715 case LFS_COMP_START_OPT:
4716 if (optarg[0] == '+') {
4717 param.fp_comp_start_sign = -1;
4719 } else if (optarg[0] == '-') {
4720 param.fp_comp_start_sign = 1;
4724 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4725 ¶m.fp_comp_start_units, 0);
4728 "error: bad component start '%s'\n",
4732 param.fp_check_comp_start = 1;
4733 param.fp_exclude_comp_start = !!neg_opt;
4735 case LFS_MIRROR_STATE_OPT:
4736 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4737 ¶m.fp_mirror_neg_state);
4740 "error: bad mirrored file state '%s'\n",
4744 param.fp_check_mirror_state = 1;
4746 __u16 state = param.fp_mirror_neg_state;
4748 param.fp_mirror_neg_state =
4749 param.fp_mirror_state;
4750 param.fp_mirror_state = state;
4754 if (optarg[0] == '+') {
4755 param.fp_stripe_count_sign = -1;
4757 } else if (optarg[0] == '-') {
4758 param.fp_stripe_count_sign = 1;
4763 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4764 if (errno != 0 || *endptr != '\0' ||
4765 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
4767 "error: bad stripe_count '%s'\n",
4772 param.fp_check_stripe_count = 1;
4773 param.fp_exclude_stripe_count = !!neg_opt;
4776 param.fp_max_depth = strtol(optarg, 0, 0);
4779 if (optarg[0] == '+') {
4780 param.fp_comp_end_sign = -1;
4782 } else if (optarg[0] == '-') {
4783 param.fp_comp_end_sign = 1;
4787 if (arg_is_eof(optarg)) {
4788 param.fp_comp_end = LUSTRE_EOF;
4789 param.fp_comp_end_units = 1;
4792 rc = llapi_parse_size(optarg,
4794 ¶m.fp_comp_end_units, 0);
4795 /* assume units of KB if too small */
4796 if (param.fp_comp_end < 4096)
4797 param.fp_comp_end *= 1024;
4801 "error: bad component end '%s'\n",
4805 param.fp_check_comp_end = 1;
4806 param.fp_exclude_comp_end = !!neg_opt;
4808 case LFS_LAYOUT_FOREIGN_OPT: {
4809 /* all types by default */
4810 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4813 /* check pure numeric */
4814 type = strtoul(optarg, &endptr, 0);
4817 type = check_foreign_type_name(optarg);
4818 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4820 "%s %s: unknown foreign type '%s'\n",
4825 } else if (type >= UINT32_MAX) {
4827 "%s %s: invalid foreign type '%s'\n",
4828 progname, argv[0], optarg);
4832 param.fp_foreign_type = type;
4833 param.fp_check_foreign = 1;
4834 param.fp_exclude_foreign = !!neg_opt;
4837 case LFS_NEWERXY_OPT: {
4843 time_t ref = time(NULL);
4845 /* no need to check bad options, they won't get here */
4846 if (strlen(long_opts[optidx].name) == 7) {
4847 x = long_opts[optidx].name[5];
4848 y = long_opts[optidx].name[6];
4852 static const char *const fmts[] = {
4853 "%Y-%m-%d %H:%M:%S",
4856 "%H:%M:%S", /* sometime today */
4865 for (i = 0; fmts[i] != NULL; i++) {
4868 /* Init for times relative to today */
4869 if (strncmp(fmts[i], "%H", 2) == 0)
4870 localtime_r(&ref, &tm);
4872 memset(&tm, 0, sizeof(tm));
4873 ptr = strptime(optarg, fmts[i], &tm);
4875 while (ptr && isspace(*ptr))
4877 if (ptr == optarg + strlen(optarg)) {
4885 "%s: invalid time '%s'\n",
4888 "supported formats are:\n ");
4889 for (i = 0; fmts[i] != NULL; i++)
4890 fprintf(stderr, "'%s', ",
4892 fprintf(stderr, "\n");
4898 } else if (y == 'b' || y == 'B') {
4901 rc = llapi_get_lum_file(optarg, NULL, &stx,
4903 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4904 if (!(stx.stx_mask & STATX_BTIME))
4909 "%s: get btime failed '%s': %s\n",
4915 ref = stx.stx_btime.tv_sec;
4917 struct stat statbuf;
4919 if (stat(optarg, &statbuf) < 0) {
4921 "%s: cannot stat file '%s': %s\n",
4930 ref = statbuf.st_atime;
4933 ref = statbuf.st_mtime;
4936 ref = statbuf.st_ctime;
4940 "%s: invalid Y argument: '%c'\n",
4949 xidx = NEWERXY_ATIME;
4952 xidx = NEWERXY_MTIME;
4955 xidx = NEWERXY_CTIME;
4959 xidx = NEWERXY_BTIME;
4963 "%s: invalid X argument: '%c'\n",
4970 newery = ¶m.fp_newery[xidx][negidx];
4976 *newery = *newery > ref ? ref : *newery;
4978 *newery = *newery > ref ? *newery : ref;
4980 param.fp_newerxy = 1;
4985 rc = name2gid(¶m.fp_gid, optarg);
4987 if (str2quotaid(¶m.fp_gid, optarg)) {
4989 "Group/GID: %s cannot be found.\n",
4995 param.fp_exclude_gid = !!neg_opt;
4996 param.fp_check_gid = 1;
4999 param.fp_hash_type = check_hashtype(optarg);
5000 if (param.fp_hash_type == 0) {
5001 fprintf(stderr, "error: bad hash_type '%s'\n",
5006 param.fp_check_hash_type = 1;
5007 param.fp_exclude_hash_type = !!neg_opt;
5013 ret = name2layout(¶m.fp_layout, optarg);
5016 param.fp_exclude_layout = !!neg_opt;
5017 param.fp_check_layout = 1;
5021 rc = name2uid(¶m.fp_uid, optarg);
5023 if (str2quotaid(¶m.fp_uid, optarg)) {
5025 "User/UID: %s cannot be found.\n",
5031 param.fp_exclude_uid = !!neg_opt;
5032 param.fp_check_uid = 1;
5035 param.fp_pattern = (char *)optarg;
5036 param.fp_exclude_pattern = !!neg_opt;
5039 if (optarg[0] == '+') {
5040 param.fp_mirror_count_sign = -1;
5042 } else if (optarg[0] == '-') {
5043 param.fp_mirror_count_sign = 1;
5048 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5049 if (errno != 0 || *endptr != '\0' ||
5050 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5052 "error: bad mirror count '%s'\n",
5056 param.fp_check_mirror_count = 1;
5057 param.fp_exclude_mirror_count = !!neg_opt;
5062 char *buf, *token, *next, *p;
5066 buf = strdup(optarg);
5072 param.fp_exclude_obd = !!neg_opt;
5075 while (token && *token) {
5076 token = strchr(token, ',');
5083 param.fp_exclude_mdt = !!neg_opt;
5084 param.fp_num_alloc_mdts += len;
5085 tmp = realloc(param.fp_mdt_uuid,
5086 param.fp_num_alloc_mdts *
5087 sizeof(*param.fp_mdt_uuid));
5093 param.fp_mdt_uuid = tmp;
5095 param.fp_exclude_obd = !!neg_opt;
5096 param.fp_num_alloc_obds += len;
5097 tmp = realloc(param.fp_obd_uuid,
5098 param.fp_num_alloc_obds *
5099 sizeof(*param.fp_obd_uuid));
5105 param.fp_obd_uuid = tmp;
5107 for (token = buf; token && *token; token = next) {
5108 struct obd_uuid *puuid;
5112 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5115 ¶m.fp_obd_uuid[param.fp_num_obds++];
5117 p = strchr(token, ',');
5124 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5129 strncpy(puuid->uuid, token,
5130 sizeof(puuid->uuid));
5137 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5141 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5143 "Pool name %s is too long (max %d)\n",
5144 optarg, LOV_MAXPOOLNAME);
5149 * We do check for empty pool because empty pool
5150 * is used to find V1 LOV attributes
5152 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5153 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5154 param.fp_exclude_pool = !!neg_opt;
5155 param.fp_check_pool = 1;
5157 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5158 case 'p': /* want this for --pool, to match getstripe/find */
5160 "warning: -p deprecated, use --print0 or -0\n");
5163 param.fp_zero_end = 1;
5165 case 'P': /* we always print, this option is a no-op */
5167 case LFS_PROJID_OPT:
5168 rc = name2projid(¶m.fp_projid, optarg);
5170 if (str2quotaid(¶m.fp_projid, optarg)) {
5172 "Invalid project ID: %s\n",
5178 param.fp_exclude_projid = !!neg_opt;
5179 param.fp_check_projid = 1;
5182 if (optarg[0] == '+') {
5183 param.fp_size_sign = -1;
5185 } else if (optarg[0] == '-') {
5186 param.fp_size_sign = 1;
5190 ret = llapi_parse_size(optarg, ¶m.fp_size,
5191 ¶m.fp_size_units, 0);
5193 fprintf(stderr, "error: bad file size '%s'\n",
5197 param.fp_check_size = 1;
5198 param.fp_exclude_size = !!neg_opt;
5201 if (optarg[0] == '+') {
5202 param.fp_stripe_size_sign = -1;
5204 } else if (optarg[0] == '-') {
5205 param.fp_stripe_size_sign = 1;
5209 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5210 ¶m.fp_stripe_size_units, 0);
5211 /* assume units of KB if too small to be valid */
5212 if (param.fp_stripe_size < 4096)
5213 param.fp_stripe_size *= 1024;
5215 fprintf(stderr, "error: bad stripe_size '%s'\n",
5219 param.fp_check_stripe_size = 1;
5220 param.fp_exclude_stripe_size = !!neg_opt;
5223 param.fp_exclude_type = !!neg_opt;
5224 switch (optarg[0]) {
5226 param.fp_type = S_IFBLK;
5229 param.fp_type = S_IFCHR;
5232 param.fp_type = S_IFDIR;
5235 param.fp_type = S_IFREG;
5238 param.fp_type = S_IFLNK;
5241 param.fp_type = S_IFIFO;
5244 param.fp_type = S_IFSOCK;
5247 fprintf(stderr, "error: %s: bad type '%s'\n",
5254 if (optarg[0] == '+') {
5255 param.fp_mdt_count_sign = -1;
5257 } else if (optarg[0] == '-') {
5258 param.fp_mdt_count_sign = 1;
5263 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5264 if (errno != 0 || *endptr != '\0' ||
5265 param.fp_mdt_count >= UINT32_MAX) {
5266 fprintf(stderr, "error: bad mdt_count '%s'\n",
5271 param.fp_check_mdt_count = 1;
5272 param.fp_exclude_mdt_count = !!neg_opt;
5275 if (optarg[0] == '+') {
5276 param.fp_ext_size_sign = -1;
5278 } else if (optarg[0] == '-') {
5279 param.fp_ext_size_sign = 1;
5283 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5284 ¶m.fp_ext_size_units, 0);
5286 fprintf(stderr, "error: bad ext-size '%s'\n",
5290 param.fp_ext_size /= SEL_UNIT_SIZE;
5291 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5292 param.fp_check_ext_size = 1;
5293 param.fp_exclude_ext_size = !!neg_opt;
5301 if (pathstart == -1) {
5302 fprintf(stderr, "error: %s: no filename|pathname\n",
5306 } else if (pathend == -1) {
5312 rc = llapi_find(argv[pathstart], ¶m);
5315 pathbad = pathstart;
5317 } while (++pathstart < pathend);
5320 fprintf(stderr, "%s: failed for '%s': %s\n",
5321 progname, argv[pathbad], strerror(-rc));
5324 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5325 free(param.fp_obd_uuid);
5327 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5328 free(param.fp_mdt_uuid);
5333 static int lfs_getstripe_internal(int argc, char **argv,
5334 struct find_param *param)
5336 struct option long_opts[] = {
5337 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5338 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5339 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5340 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5341 { .val = LFS_COMP_COUNT_OPT,
5342 .name = "comp-count", .has_arg = no_argument },
5343 { .val = LFS_COMP_COUNT_OPT,
5344 .name = "component-count", .has_arg = no_argument },
5345 { .val = LFS_COMP_FLAGS_OPT,
5346 .name = "comp-flags", .has_arg = optional_argument },
5347 { .val = LFS_COMP_FLAGS_OPT,
5348 .name = "component-flags", .has_arg = optional_argument },
5349 { .val = LFS_COMP_START_OPT,
5350 .name = "comp-start", .has_arg = optional_argument },
5351 { .val = LFS_COMP_START_OPT,
5352 .name = "component-start", .has_arg = optional_argument },
5353 { .val = LFS_MIRROR_INDEX_OPT,
5354 .name = "mirror-index", .has_arg = required_argument },
5355 { .val = LFS_MIRROR_ID_OPT,
5356 .name = "mirror-id", .has_arg = required_argument },
5357 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5358 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5359 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5360 { .val = 'd', .name = "directory", .has_arg = no_argument },
5361 { .val = 'D', .name = "default", .has_arg = no_argument },
5362 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5363 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5364 { .val = 'F', .name = "fid", .has_arg = no_argument },
5365 { .val = 'g', .name = "generation", .has_arg = no_argument },
5366 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5367 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5368 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5369 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5370 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5371 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5372 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5373 { .val = 'L', .name = "layout", .has_arg = no_argument },
5374 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5375 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5376 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5377 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5378 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5379 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5380 { .val = 'O', .name = "obd", .has_arg = required_argument },
5381 { .val = 'O', .name = "ost", .has_arg = required_argument },
5382 { .val = 'p', .name = "pool", .has_arg = no_argument },
5383 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5384 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5385 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5386 { .val = 'R', .name = "raw", .has_arg = no_argument },
5387 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5388 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5389 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5390 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5391 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5392 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5393 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5394 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5395 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5396 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5400 int pathstart = -1, pathend = -1;
5404 while ((c = getopt_long(argc, argv,
5405 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5406 long_opts, NULL)) != -1) {
5410 /* '!' is part of option */
5411 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5412 if (!isoption && pathend != -1) {
5414 "error: %s: filename|dirname must either precede options or follow options\n",
5418 if (!isoption && pathstart == -1)
5419 pathstart = optind - 1;
5420 if (isoption && pathstart != -1 && pathend == -1)
5421 pathend = optind - 2;
5425 /* unknown: opt is "!" */
5426 if (strcmp(optarg, "!") == 0)
5430 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5431 param->fp_verbose |= VERBOSE_COUNT;
5432 param->fp_max_depth = 0;
5435 case LFS_COMP_COUNT_OPT:
5436 param->fp_verbose |= VERBOSE_COMP_COUNT;
5437 param->fp_max_depth = 0;
5439 case LFS_COMP_FLAGS_OPT:
5441 rc = comp_str2flags(optarg,
5442 ¶m->fp_comp_flags,
5443 ¶m->fp_comp_neg_flags);
5446 "error: %s bad component flags '%s'.\n",
5450 param->fp_check_comp_flags = 1;
5452 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5453 param->fp_max_depth = 0;
5456 case LFS_COMP_START_OPT:
5459 if (tmp[0] == '+') {
5460 param->fp_comp_start_sign = -1;
5462 } else if (tmp[0] == '-') {
5463 param->fp_comp_start_sign = 1;
5466 rc = llapi_parse_size(tmp,
5467 ¶m->fp_comp_start,
5468 ¶m->fp_comp_start_units, 0);
5471 "error: %s bad component start '%s'.\n",
5475 param->fp_check_comp_start = 1;
5477 param->fp_verbose |= VERBOSE_COMP_START;
5478 param->fp_max_depth = 0;
5481 case LFS_MIRROR_INDEX_OPT: {
5482 unsigned long int mirror_index;
5484 if (optarg[0] == '+') {
5485 param->fp_mirror_index_sign = -1;
5487 } else if (optarg[0] == '-') {
5488 param->fp_mirror_index_sign = 1;
5493 mirror_index = strtoul(optarg, &end, 0);
5494 if (errno != 0 || *end != '\0' ||
5495 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5496 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5498 "%s %s: invalid mirror index '%s'\n",
5499 progname, argv[0], optarg);
5503 param->fp_mirror_index = (__u16)mirror_index;
5505 if (param->fp_mirror_id != 0) {
5507 "%s %s: can't specify both mirror index and mirror ID\n",
5511 param->fp_check_mirror_index = 1;
5512 param->fp_exclude_mirror_index = !!neg_opt;
5515 case LFS_MIRROR_ID_OPT: {
5516 unsigned long int mirror_id;
5518 if (optarg[0] == '+') {
5519 param->fp_mirror_id_sign = -1;
5521 } else if (optarg[0] == '-') {
5522 param->fp_mirror_id_sign = 1;
5527 mirror_id = strtoul(optarg, &end, 0);
5528 if (errno != 0 || *end != '\0' ||
5529 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5530 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5532 "%s %s: invalid mirror ID '%s'\n",
5533 progname, argv[0], optarg);
5537 param->fp_mirror_id = (__u16)mirror_id;
5539 if (param->fp_mirror_index != 0) {
5541 "%s %s: can't specify both mirror index and mirror ID\n",
5545 param->fp_check_mirror_id = 1;
5546 param->fp_exclude_mirror_id = !!neg_opt;
5550 param->fp_max_depth = 0;
5553 param->fp_get_default_lmv = 1;
5558 if (tmp[0] == '+') {
5559 param->fp_comp_end_sign = -1;
5561 } else if (tmp[0] == '-') {
5562 param->fp_comp_end_sign = 1;
5566 if (arg_is_eof(tmp)) {
5567 param->fp_comp_end = LUSTRE_EOF;
5568 param->fp_comp_end_units = 1;
5571 rc = llapi_parse_size(tmp,
5572 ¶m->fp_comp_end,
5573 ¶m->fp_comp_end_units, 0);
5574 /* assume units of KB if too small */
5575 if (param->fp_comp_end < 4096)
5576 param->fp_comp_end *= 1024;
5580 "error: %s bad component end '%s'.\n",
5584 param->fp_check_comp_end = 1;
5586 param->fp_verbose |= VERBOSE_COMP_END;
5587 param->fp_max_depth = 0;
5591 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5592 param->fp_verbose |= VERBOSE_DFID;
5593 param->fp_max_depth = 0;
5597 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5598 param->fp_verbose |= VERBOSE_GENERATION;
5599 param->fp_max_depth = 0;
5603 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5604 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5605 param->fp_max_depth = 0;
5610 param->fp_comp_id = strtoul(optarg, &end, 0);
5611 if (*end != '\0' || param->fp_comp_id == 0 ||
5612 param->fp_comp_id > LCME_ID_MAX) {
5614 "error: %s bad component id '%s'\n",
5618 param->fp_check_comp_id = 1;
5620 param->fp_max_depth = 0;
5621 param->fp_verbose |= VERBOSE_COMP_ID;
5625 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5626 param->fp_verbose |= VERBOSE_PATTERN;
5627 param->fp_max_depth = 0;
5630 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5633 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5636 if (!(param->fp_verbose & VERBOSE_DETAIL))
5637 param->fp_max_depth = 0;
5638 param->fp_verbose |= VERBOSE_MDTINDEX;
5641 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5642 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5643 param->fp_max_depth = 0;
5647 if (param->fp_obd_uuid) {
5649 "error: %s: only one obduuid allowed",
5653 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5656 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5657 param->fp_verbose |= VERBOSE_POOL;
5658 param->fp_max_depth = 0;
5665 param->fp_recursive = 1;
5671 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5672 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5673 param->fp_max_depth = 0;
5677 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5683 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5684 param->fp_verbose |= VERBOSE_EXT_SIZE;
5685 param->fp_max_depth = 0;
5693 if (pathstart == -1) {
5694 fprintf(stderr, "error: %s: no filename|pathname\n",
5697 } else if (pathend == -1) {
5705 if (param->fp_recursive)
5706 param->fp_max_depth = -1;
5707 else if (param->fp_verbose & VERBOSE_DETAIL)
5708 param->fp_max_depth = 1;
5710 if (!param->fp_verbose)
5711 param->fp_verbose = VERBOSE_DEFAULT;
5712 if (param->fp_quiet)
5713 param->fp_verbose = VERBOSE_OBJID;
5716 rc = llapi_getstripe(argv[pathstart], param);
5717 } while (++pathstart < pathend && !rc);
5720 fprintf(stderr, "error: %s failed for %s.\n",
5721 argv[0], argv[optind - 1]);
5725 static int lfs_tgts(int argc, char **argv)
5727 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5728 struct find_param param;
5729 int index = 0, rc = 0;
5734 if (argc == 2 && !realpath(argv[1], path)) {
5736 fprintf(stderr, "error: invalid path '%s': %s\n",
5737 argv[1], strerror(-rc));
5741 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5742 /* Check if we have a mount point */
5743 if (mntdir[0] == '\0')
5746 memset(¶m, 0, sizeof(param));
5747 if (!strcmp(argv[0], "mdts"))
5748 param.fp_get_lmv = 1;
5750 rc = llapi_ostlist(mntdir, ¶m);
5752 fprintf(stderr, "error: %s: failed on %s\n",
5755 if (path[0] != '\0')
5757 memset(mntdir, 0, PATH_MAX);
5763 static int lfs_getstripe(int argc, char **argv)
5765 struct find_param param = { 0 };
5767 param.fp_max_depth = 1;
5768 return lfs_getstripe_internal(argc, argv, ¶m);
5772 static int lfs_getdirstripe(int argc, char **argv)
5774 struct find_param param = { 0 };
5775 struct option long_opts[] = {
5776 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5777 { .val = 'D', .name = "default", .has_arg = no_argument },
5778 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5779 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5780 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5781 { .val = 'O', .name = "obd", .has_arg = required_argument },
5782 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5783 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5784 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5785 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5789 param.fp_get_lmv = 1;
5791 while ((c = getopt_long(argc, argv,
5792 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5796 param.fp_verbose |= VERBOSE_COUNT;
5799 param.fp_get_default_lmv = 1;
5803 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5805 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5808 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5811 param.fp_verbose |= VERBOSE_HASH_TYPE;
5814 if (param.fp_obd_uuid) {
5816 "error: %s: only one obduuid allowed",
5820 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5823 param.fp_recursive = 1;
5826 param.fp_verbose |= VERBOSE_DETAIL;
5832 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5833 progname, argv[0], argv[optind - 1]);
5841 if (param.fp_recursive)
5842 param.fp_max_depth = -1;
5844 if (!param.fp_verbose)
5845 param.fp_verbose = VERBOSE_DEFAULT;
5848 rc = llapi_getstripe(argv[optind], ¶m);
5849 } while (++optind < argc && !rc);
5852 fprintf(stderr, "error: %s failed for %s.\n",
5853 argv[0], argv[optind - 1]);
5858 MNTDF_INODES = 0x0001,
5859 MNTDF_COOKED = 0x0002,
5860 MNTDF_LAZY = 0x0004,
5861 MNTDF_VERBOSE = 0x0008,
5862 MNTDF_SHOW = 0x0010,
5863 MNTDF_DECIMAL = 0x0020,
5866 #define COOK(value, base) \
5869 while (value > base) { \
5877 #define CDF "%11llu"
5878 #define HDF "%8.1f%c"
5882 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5884 double avail, used, ratio = 0;
5887 avail = st->os_ffree;
5888 used = st->os_files - st->os_ffree;
5890 avail = st->os_bavail;
5891 used = st->os_blocks - st->os_bfree;
5893 if (avail + used > 0)
5894 ratio = used / (used + avail) * 100;
5896 /* Round up to match df(1) usage percentage */
5897 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5901 * This is to identify various problem states for "lfs df" if .osn_err = true,
5902 * so only show flags reflecting those states by default. Informational states
5903 * are only shown with "-v" and use lower-case names to distinguish them.
5904 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5906 static struct obd_statfs_state_names {
5907 enum obd_statfs_state osn_state;
5908 const char osn_name;
5911 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5912 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5913 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5914 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5915 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5916 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5917 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5918 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5919 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5922 static int showdf(char *mntdir, struct obd_statfs *stat,
5923 char *uuid, enum mntdf_flags flags,
5924 char *type, int index, int rc)
5926 long long avail, used, total;
5928 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
5929 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5930 char tbuf[3 * sizeof(__u64)];
5931 char ubuf[3 * sizeof(__u64)];
5932 char abuf[3 * sizeof(__u64)];
5933 char rbuf[3 * sizeof(__u64)];
5940 if (flags & MNTDF_INODES) {
5941 avail = stat->os_ffree;
5942 used = stat->os_files - stat->os_ffree;
5943 total = stat->os_files;
5945 int shift = flags & MNTDF_COOKED ? 0 : 10;
5947 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5948 used = ((stat->os_blocks - stat->os_bfree) *
5949 stat->os_bsize) >> shift;
5950 total = (stat->os_blocks * stat->os_bsize) >> shift;
5953 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5955 if (flags & MNTDF_COOKED) {
5956 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
5960 cook_val = (double)total;
5961 i = COOK(cook_val, base);
5963 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5966 snprintf(tbuf, sizeof(tbuf), CDF, total);
5968 cook_val = (double)used;
5969 i = COOK(cook_val, base);
5971 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5974 snprintf(ubuf, sizeof(ubuf), CDF, used);
5976 cook_val = (double)avail;
5977 i = COOK(cook_val, base);
5979 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5982 snprintf(abuf, sizeof(abuf), CDF, avail);
5984 snprintf(tbuf, sizeof(tbuf), CDF, total);
5985 snprintf(ubuf, sizeof(tbuf), CDF, used);
5986 snprintf(abuf, sizeof(tbuf), CDF, avail);
5989 sprintf(rbuf, RDF, ratio);
5990 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5991 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5993 printf("[%s:%d]", type, index);
5995 if (stat->os_state) {
5999 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6000 if (oss_names[i].osn_state & stat->os_state &&
6001 (oss_names[i].osn_err ||
6002 flags & MNTDF_VERBOSE))
6003 printf("%c", oss_names[i].osn_name);
6010 printf(UUF": inactive device\n", uuid);
6013 printf(UUF": %s\n", uuid, strerror(-rc));
6020 struct ll_stat_type {
6025 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6027 struct ll_statfs_data {
6029 struct obd_statfs sd_st;
6032 struct ll_statfs_buf {
6034 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6037 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6038 int ops, struct ll_statfs_buf *lsb)
6040 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6041 struct obd_uuid uuid_buf;
6042 char *poolname = NULL;
6043 struct ll_stat_type types[] = {
6044 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6045 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6046 { .st_name = NULL } };
6047 struct ll_stat_type *tp;
6048 __u64 ost_files = 0;
6049 __u64 ost_ffree = 0;
6057 poolname = strchr(pool, '.');
6059 if (strncmp(fsname, pool, strlen(fsname))) {
6060 fprintf(stderr, "filesystem name incorrect\n");
6068 fd = open(mntdir, O_RDONLY);
6071 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6076 if (flags & MNTDF_SHOW) {
6077 if (flags & MNTDF_INODES)
6078 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6079 "UUID", "Inodes", "IUsed", "IFree",
6080 "IUse%", "Mounted on");
6082 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6084 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6085 "Used", "Available", "Use%", "Mounted on");
6088 for (tp = types; tp->st_name != NULL; tp++) {
6089 bool have_ost = false;
6091 if (!(tp->st_op & ops))
6094 for (index = 0; ; index++) {
6095 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6096 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6097 type = flags & MNTDF_LAZY ?
6098 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6099 rc2 = llapi_obd_fstatfs(fd, type, index,
6100 &stat_buf, &uuid_buf);
6105 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6106 if (!(flags & MNTDF_VERBOSE))
6108 } else if (rc2 < 0 && rc == 0) {
6113 * If we have OSTs then don't report MDT block counts.
6114 * For MDT-only filesystems the expectation is that all
6115 * layouts have a DoM component. For filesystems with
6116 * OSTs, files are not necessarily going to store data
6117 * on MDTs, and MDT space is limited to a fraction of
6118 * OST space, so don't include it in the summary.
6120 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6127 if (poolname && tp->st_op == LL_STATFS_LOV &&
6128 llapi_search_ost(fsname, poolname,
6129 obd_uuid2str(&uuid_buf)) != 1)
6133 * the llapi_obd_fstatfs() call may have returned with
6134 * an error, but if it filled in uuid_buf we will at
6135 * lease use that to print out a message for that OBD.
6136 * If we didn't get anything in the uuid_buf, then fill
6137 * it in so that we can print an error message.
6139 if (uuid_buf.uuid[0] == '\0')
6140 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6141 "%s%04x", tp->st_name, index);
6143 lsb->sb_buf[lsb->sb_count].sd_index = index;
6144 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6147 if (flags & MNTDF_SHOW)
6148 showdf(mntdir, &stat_buf,
6149 obd_uuid2str(&uuid_buf), flags,
6150 tp->st_name, index, rc2);
6155 if (tp->st_op == LL_STATFS_LMV) {
6156 sum.os_ffree += stat_buf.os_ffree;
6157 sum.os_files += stat_buf.os_files;
6158 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6159 ost_files += stat_buf.os_files;
6160 ost_ffree += stat_buf.os_ffree;
6162 sum.os_blocks += stat_buf.os_blocks *
6164 sum.os_bfree += stat_buf.os_bfree *
6166 sum.os_bavail += stat_buf.os_bavail *
6174 * If we have _some_ OSTs, but don't have as many free objects on the
6175 * OST as inodes on the MDTs, reduce the reported number of inodes
6176 * to compensate, so that the "inodes in use" number is correct.
6177 * This should be kept in sync with ll_statfs_internal().
6179 if (ost_files && ost_ffree < sum.os_ffree) {
6180 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6181 sum.os_ffree = ost_ffree;
6183 if (flags & MNTDF_SHOW) {
6185 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6193 static int lfs_setdirstripe(int argc, char **argv)
6196 struct lfs_setstripe_args lsa = { 0 };
6197 struct llapi_stripe_param *param = NULL;
6198 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6201 char *mode_opt = NULL;
6202 bool default_stripe = false;
6203 bool delete = false;
6204 bool foreign_mode = false;
6205 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6206 mode_t previous_mode = 0;
6208 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6209 struct option long_opts[] = {
6210 { .val = 'c', .name = "count", .has_arg = required_argument },
6211 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6212 { .val = 'd', .name = "delete", .has_arg = no_argument },
6213 { .val = 'D', .name = "default", .has_arg = no_argument },
6214 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6215 { .val = LFS_LAYOUT_FLAGS_OPT,
6216 .name = "flags", .has_arg = required_argument },
6217 { .val = LFS_LAYOUT_FOREIGN_OPT,
6218 .name = "foreign", .has_arg = optional_argument},
6219 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6220 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6221 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6222 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6224 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6225 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6226 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6228 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6229 { .val = 'i', .name = "index", .has_arg = required_argument },
6231 { .val = 'o', .name = "mode", .has_arg = required_argument },
6232 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6233 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6235 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6236 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6237 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6241 setstripe_args_init(&lsa);
6243 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6244 long_opts, NULL)) >= 0) {
6252 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6253 if (errno != 0 || *end != '\0' ||
6254 lsa.lsa_stripe_count < -1 ||
6255 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6257 "%s %s: invalid stripe count '%s'\n",
6258 progname, argv[0], optarg);
6264 default_stripe = true;
6267 default_stripe = true;
6269 case LFS_LAYOUT_FOREIGN_OPT:
6271 /* check pure numeric */
6272 type = strtoul(optarg, &end, 0);
6275 type = check_foreign_type_name(optarg);
6276 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6278 "%s %s: unknown foreign type '%s'\n",
6283 } else if (type >= UINT32_MAX) {
6285 "%s %s: invalid foreign type '%s'\n",
6286 progname, argv[0], optarg);
6290 foreign_mode = true;
6292 case LFS_LAYOUT_FLAGS_OPT:
6294 flags = strtoul(optarg, &end, 16);
6295 if (errno != 0 || *end != '\0' ||
6296 flags >= UINT32_MAX) {
6298 "%s %s: invalid hex flags '%s'\n",
6299 progname, argv[0], optarg);
6302 if (!foreign_mode) {
6304 "%s %s: hex flags must be specified with --foreign option\n",
6309 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6312 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6315 lsa.lsa_pattern = check_hashtype(optarg);
6316 if (lsa.lsa_pattern == 0) {
6318 "%s %s: bad stripe hash type '%s'\n",
6319 progname, argv[0], optarg);
6324 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6327 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6328 if (strcmp(argv[optind - 1], "--index") == 0)
6330 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6333 lsa.lsa_nr_tgts = parse_targets(mdts,
6334 sizeof(mdts) / sizeof(__u32),
6335 lsa.lsa_nr_tgts, optarg, NULL);
6336 if (lsa.lsa_nr_tgts < 0) {
6338 "%s %s: invalid MDT target(s) '%s'\n",
6339 progname, argv[0], optarg);
6343 lsa.lsa_tgts = mdts;
6344 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6345 lsa.lsa_stripe_off = mdts[0];
6347 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6350 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6359 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6360 progname, argv[0], argv[optind - 1]);
6365 if (optind == argc) {
6366 fprintf(stderr, "%s %s: DIR must be specified\n",
6371 if (xattr && !foreign_mode) {
6373 * only print a warning as this is armless and will be
6377 "%s %s: xattr has been specified for non-foreign layout\n",
6379 } else if (foreign_mode && !xattr) {
6381 "%s %s: xattr must be provided in foreign mode\n",
6386 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6387 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6389 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6394 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6395 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6396 /* if no parameters set, create directory on least-used MDTs */
6397 lsa.lsa_stripe_off = -1;
6398 lsa.lsa_stripe_count = 1;
6402 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6403 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6405 "%s %s: cannot specify -d with -c or -i options\n",
6411 mode = strtoul(mode_opt, &end, 8);
6414 "%s %s: bad MODE '%s'\n",
6415 progname, argv[0], mode_opt);
6418 previous_mode = umask(0);
6421 /* foreign LMV/dir case */
6423 if (argc > optind + 1) {
6425 "%s %s: cannot specify multiple foreign dirs\n",
6430 dname = argv[optind];
6431 result = llapi_dir_create_foreign(dname, mode, type, flags,
6435 "%s mkdir: can't create foreign dir '%s': %s\n",
6436 progname, dname, strerror(-result));
6441 * initialize stripe parameters, in case param is converted to specific,
6442 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6444 param = calloc(1, offsetof(typeof(*param),
6445 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6446 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6449 "%s %s: cannot allocate memory for parameters: %s\n",
6450 progname, argv[0], strerror(ENOMEM));
6454 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6455 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6456 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6457 lsa.lsa_stripe_count == 0))
6458 lsa.lsa_stripe_count = 1;
6459 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6460 param->lsp_stripe_count = lsa.lsa_stripe_count;
6461 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6462 param->lsp_stripe_offset = -1;
6464 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6465 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6466 param->lsp_stripe_pattern = lsa.lsa_pattern;
6468 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6469 param->lsp_pool = lsa.lsa_pool_name;
6470 param->lsp_is_specific = false;
6471 if (lsa.lsa_nr_tgts > 1) {
6472 if (lsa.lsa_stripe_count > 0 &&
6473 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6474 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6476 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6477 argv[0], lsa.lsa_stripe_count,
6483 param->lsp_is_specific = true;
6484 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6485 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6488 dname = argv[optind];
6490 if (default_stripe) {
6491 result = llapi_dir_set_default_lmv(dname, param);
6494 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6495 progname, dname, strerror(-result));
6499 result = llapi_dir_create(dname, mode, param);
6502 "%s setdirstripe: cannot create dir '%s': %s\n",
6503 progname, dname, strerror(-result));
6504 } while (!result && (dname = argv[++optind]));
6507 umask(previous_mode);
6514 static int lfs_rmentry(int argc, char **argv)
6521 fprintf(stderr, "error: %s: missing dirname\n",
6527 dname = argv[index];
6529 result = llapi_direntry_remove(dname);
6532 "error: %s: remove dir entry '%s' failed\n",
6536 dname = argv[++index];
6541 static int lfs_unlink_foreign(int argc, char **argv)
6548 fprintf(stderr, "error: %s: missing pathname\n",
6555 while (name != NULL) {
6556 result = llapi_unlink_foreign(name);
6559 "error: %s: unlink foreign entry '%s' failed\n",
6563 name = argv[++index];
6568 static int lfs_mv(int argc, char **argv)
6570 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6571 struct find_param param = {
6578 struct option long_opts[] = {
6579 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6580 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6581 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6584 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6586 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6589 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6593 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6594 if (errno != 0 || *end != '\0' ||
6595 lmu.lum_stripe_offset >= UINT32_MAX) {
6596 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6602 param.fp_verbose = VERBOSE_DETAIL;
6605 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6606 progname, argv[optind - 1]);
6611 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6612 fprintf(stderr, "%s mv: MDT index must be specified\n",
6617 if (optind >= argc) {
6618 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6622 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6624 /* initialize migrate mdt parameters */
6625 param.fp_lmv_md = &lmu;
6626 param.fp_migrate = 1;
6627 rc = llapi_migrate_mdt(argv[optind], ¶m);
6629 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6630 progname, argv[optind], lmu.lum_stripe_offset,
6635 static int lfs_osts(int argc, char **argv)
6637 return lfs_tgts(argc, argv);
6640 static int lfs_mdts(int argc, char **argv)
6642 return lfs_tgts(argc, argv);
6645 static int lfs_df(int argc, char **argv)
6647 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6648 enum mntdf_flags flags = MNTDF_SHOW;
6649 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6650 int c, rc = 0, index = 0;
6651 char fsname[PATH_MAX] = "", *pool_name = NULL;
6652 struct option long_opts[] = {
6653 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
6654 { .val = 'H', .name = "si", .has_arg = no_argument },
6655 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6656 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6657 { .val = 'p', .name = "pool", .has_arg = required_argument },
6658 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6661 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
6664 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
6667 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
6670 flags |= MNTDF_INODES;
6673 flags |= MNTDF_LAZY;
6679 flags |= MNTDF_VERBOSE;
6685 if (optind < argc && !realpath(argv[optind], path)) {
6687 fprintf(stderr, "error: invalid path '%s': %s\n",
6688 argv[optind], strerror(-rc));
6692 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6693 /* Check if we have a mount point */
6694 if (mntdir[0] == '\0')
6697 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6698 if (rc || path[0] != '\0')
6700 fsname[0] = '\0'; /* avoid matching in next loop */
6701 mntdir[0] = '\0'; /* avoid matching in next loop */
6707 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6708 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6712 if (opt_fsname == opt_instance) { /* both true or both false */
6713 rc = llapi_getname(mntdir, buf, buflen);
6714 } else if (opt_fsname) {
6716 * llapi_search_mounts() fills @buf with fsname, but that is not
6717 * called if explicit paths are specified on the command-line
6720 rc = llapi_get_fsname(mntdir, buf, buflen);
6721 } else /* if (opt_instance) */ {
6722 rc = llapi_get_instance(mntdir, buf, buflen);
6726 fprintf(stderr, "cannot get instance for '%s': %s\n",
6727 mntdir, strerror(-rc));
6732 printf("%s %s\n", buf, mntdir);
6734 printf("%s\n", buf);
6739 static int lfs_getname(int argc, char **argv)
6741 struct option long_opts[] = {
6742 { .val = 'h', .name = "help", .has_arg = no_argument },
6743 { .val = 'i', .name = "instance", .has_arg = no_argument },
6744 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6746 bool opt_instance = false, opt_fsname = false;
6747 char fsname[PATH_MAX] = "";
6750 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6753 opt_instance = true;
6764 if (optind == argc) { /* no paths specified, get all paths. */
6765 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6768 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6769 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6770 opt_instance, opt_fsname, true);
6773 path[0] = fsname[0] = mntdir[0] = '\0';
6775 } else { /* paths specified, only attempt to search these. */
6778 /* if only one path is given, print only requested info */
6779 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6781 for (; optind < argc; optind++) {
6782 rc2 = print_instance(argv[optind], fsname,
6783 sizeof(fsname), opt_instance,
6784 opt_fsname, opt_mntdir);
6794 static int lfs_check(int argc, char **argv)
6796 char mntdir[PATH_MAX] = {'\0'};
6805 fprintf(stderr, "%s check: server type must be specified\n",
6810 obd_types[0] = obd_type1;
6811 obd_types[1] = obd_type2;
6812 obd_types[2] = obd_type3;
6814 if (strcmp(argv[1], "osts") == 0) {
6815 strcpy(obd_types[0], "osc");
6816 } else if (strcmp(argv[1], "mdts") == 0 ||
6817 strcmp(argv[1], "mds") == 0) {
6818 strcpy(obd_types[0], "mdc");
6819 } else if (strcmp(argv[1], "mgts") == 0) {
6820 strcpy(obd_types[0], "mgc");
6821 } else if (strcmp(argv[1], "all") == 0 ||
6822 strcmp(argv[1], "servers") == 0) {
6824 strcpy(obd_types[0], "osc");
6825 strcpy(obd_types[1], "mdc");
6826 strcpy(obd_types[2], "mgc");
6828 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6833 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6834 if (rc < 0 || mntdir[0] == '\0') {
6836 "%s check: cannot find mounted Lustre filesystem: %s\n",
6837 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6841 rc = llapi_target_check(num_types, obd_types, mntdir);
6843 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6844 progname, argv[1], strerror(-rc));
6849 #ifdef HAVE_SYS_QUOTA_H
6850 #define ADD_OVERFLOW(a, b) \
6851 ((((a) + (b)) < (a)) ? \
6852 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6854 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6855 * returns the value or ULONG_MAX on integer overflow or incorrect format
6857 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6858 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6859 * 3. empty integer value is interpreted as 0
6861 static unsigned long str2sec(const char *timestr)
6863 const char spec[] = "smhdw";
6864 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6865 unsigned long val = 0;
6868 if (strpbrk(timestr, spec) == NULL) {
6870 * no specifiers inside the time string,
6871 * should treat it as an integer value
6873 val = strtoul(timestr, &tail, 10);
6874 return *tail ? ULONG_MAX : val;
6877 /* format string is XXwXXdXXhXXmXXs */
6883 v = strtoul(timestr, &tail, 10);
6884 if (v == ULONG_MAX || *tail == '\0')
6886 * value too large (ULONG_MAX or more)
6887 * or missing specifier
6891 ptr = strchr(spec, *tail);
6893 /* unknown specifier */
6898 /* check if product will overflow the type */
6899 if (!(v < ULONG_MAX / mult[ind]))
6902 ADD_OVERFLOW(val, mult[ind] * v);
6903 if (val == ULONG_MAX)
6915 #define ARG2ULL(nr, str, def_units) \
6917 unsigned long long limit, units = def_units; \
6920 rc = llapi_parse_size(str, &limit, &units, 1); \
6922 fprintf(stderr, "%s: invalid limit '%s'\n", \
6929 static inline int has_times_option(int argc, char **argv)
6933 for (i = 1; i < argc; i++)
6934 if (!strcmp(argv[i], "-t"))
6940 static inline int lfs_verify_poolarg(char *pool)
6942 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6944 "Pool name '%.*s' is longer than %d\n",
6945 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6951 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6954 char *mnt, *obd_type = (char *)qctl->obd_type;
6955 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6956 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6957 struct option long_opts[] = {
6958 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6959 { .val = 'g', .name = "group", .has_arg = no_argument },
6960 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6961 { .val = 'p', .name = "projid", .has_arg = no_argument },
6962 { .val = 't', .name = "times", .has_arg = no_argument },
6963 { .val = 'u', .name = "user", .has_arg = no_argument },
6964 { .val = LFS_POOL_OPT,
6965 .name = "pool", .has_arg = required_argument },
6969 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6970 qctl->qc_type = ALLQUOTA;
6972 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6973 long_opts, NULL)) != -1) {
6984 if (qctl->qc_type != ALLQUOTA) {
6986 "error: -u/g/p can't be used more than once\n");
6989 qctl->qc_type = qtype;
6992 if (strncmp(optarg, NOTIFY_GRACE,
6993 strlen(NOTIFY_GRACE)) == 0) {
6994 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6996 dqi->dqi_bgrace = str2sec(optarg);
6997 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6999 "error: bad block-grace: %s\n",
7004 dqb->dqb_valid |= QIF_BTIME;
7007 if (strncmp(optarg, NOTIFY_GRACE,
7008 strlen(NOTIFY_GRACE)) == 0) {
7009 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7011 dqi->dqi_igrace = str2sec(optarg);
7012 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7014 "error: bad inode-grace: %s\n",
7019 dqb->dqb_valid |= QIF_ITIME;
7021 case 't': /* Yes, of course! */
7024 if (lfs_verify_poolarg(optarg))
7027 "Trying to set grace for pool %s\n", optarg);
7028 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7029 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7031 /* getopt prints error message for us when opterr != 0 */
7037 if (qctl->qc_type == ALLQUOTA) {
7038 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
7042 if (optind != argc - 1) {
7043 fprintf(stderr, "error: unexpected parameters encountered\n");
7048 rc = llapi_quotactl(mnt, qctl);
7051 fprintf(stderr, "%s %s ", obd_type,
7052 obd_uuid2str(&qctl->obd_uuid));
7053 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7060 #define BSLIMIT (1 << 0)
7061 #define BHLIMIT (1 << 1)
7062 #define ISLIMIT (1 << 2)
7063 #define IHLIMIT (1 << 3)
7065 int lfs_setquota(int argc, char **argv)
7068 struct if_quotactl *qctl;
7069 char *mnt, *obd_type;
7070 struct obd_dqblk *dqb;
7071 struct option long_opts[] = {
7072 { .val = 'b', .name = "block-softlimit",
7073 .has_arg = required_argument },
7074 { .val = 'B', .name = "block-hardlimit",
7075 .has_arg = required_argument },
7076 { .val = 'd', .name = "default", .has_arg = no_argument },
7077 { .val = 'g', .name = "group", .has_arg = required_argument },
7078 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7079 { .val = 'i', .name = "inode-softlimit",
7080 .has_arg = required_argument },
7081 { .val = 'I', .name = "inode-hardlimit",
7082 .has_arg = required_argument },
7083 { .val = 'p', .name = "projid", .has_arg = required_argument },
7084 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7085 { .val = 'u', .name = "user", .has_arg = required_argument },
7086 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7087 { .val = LFS_POOL_OPT,
7088 .name = "pool", .has_arg = required_argument },
7090 unsigned int limit_mask = 0;
7091 bool use_default = false;
7092 int qtype, qctl_len;
7094 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7095 qctl = malloc(qctl_len);
7099 memset(qctl, 0, qctl_len);
7100 obd_type = (char *)qctl->obd_type;
7101 dqb = &qctl->qc_dqblk;
7103 if (has_times_option(argc, argv)) {
7104 rc = lfs_setquota_times(argc, argv, qctl);
7108 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7109 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7110 * so it can be used as a marker that qc_type
7111 * isn't reinitialized from command line
7114 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7115 long_opts, NULL)) != -1) {
7118 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7121 goto quota_type_def;
7124 rc = name2uid(&qctl->qc_id, optarg);
7127 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7130 goto quota_type_def;
7133 rc = name2gid(&qctl->qc_id, optarg);
7136 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7139 goto quota_type_def;
7142 rc = name2projid(&qctl->qc_id, optarg);
7145 if (str2quotaid(&qctl->qc_id, optarg)) {
7147 "%s setquota: invalid id '%s'\n",
7154 if (qctl->qc_id == 0) {
7156 "%s setquota: can't set quota for root usr/group/project.\n",
7163 if (qctl->qc_type != ALLQUOTA) {
7165 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7170 qctl->qc_type = qtype;
7173 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7177 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7178 dqb->dqb_bsoftlimit >>= 10;
7179 limit_mask |= BSLIMIT;
7180 if (dqb->dqb_bsoftlimit &&
7181 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7183 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7185 (unsigned long long)dqb->dqb_bsoftlimit,
7189 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7190 dqb->dqb_bhardlimit >>= 10;
7191 limit_mask |= BHLIMIT;
7192 if (dqb->dqb_bhardlimit &&
7193 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7195 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7196 "See '%s help setquota' or Lustre manual for details\n",
7198 (unsigned long long)dqb->dqb_bhardlimit,
7202 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7203 limit_mask |= ISLIMIT;
7204 if (dqb->dqb_isoftlimit &&
7205 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7207 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7209 (unsigned long long)dqb->dqb_isoftlimit,
7213 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7214 limit_mask |= IHLIMIT;
7215 if (dqb->dqb_ihardlimit &&
7216 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7218 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7220 (unsigned long long)dqb->dqb_ihardlimit,
7224 if (lfs_verify_poolarg(optarg)) {
7229 "Trying to set quota for pool %s\n", optarg);
7230 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7231 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7235 "%s setquota: unrecognized option '%s'\n",
7236 progname, argv[optind - 1]);
7242 if (qctl->qc_type == ALLQUOTA) {
7244 "%s setquota: either -u or -g must be specified\n",
7250 if (!use_default && limit_mask == 0) {
7252 "%s setquota: at least one limit must be specified\n",
7258 if (use_default && limit_mask != 0) {
7260 "%s setquota: limits should not be specified when using default quota\n",
7266 if (use_default && qctl->qc_id == 0) {
7268 "%s setquota: can not set default quota for root user/group/project\n",
7274 if (optind != argc - 1) {
7276 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7277 progname, argv[optind]);
7285 dqb->dqb_bhardlimit = 0;
7286 dqb->dqb_bsoftlimit = 0;
7287 dqb->dqb_ihardlimit = 0;
7288 dqb->dqb_isoftlimit = 0;
7291 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7292 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7293 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7294 /* sigh, we can't just set blimits/ilimits */
7295 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7296 .qc_type = qctl->qc_type,
7297 .qc_id = qctl->qc_id};
7299 rc = llapi_quotactl(mnt, &tmp_qctl);
7303 if (!(limit_mask & BHLIMIT))
7304 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7305 if (!(limit_mask & BSLIMIT))
7306 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7307 if (!(limit_mask & IHLIMIT))
7308 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7309 if (!(limit_mask & ISLIMIT))
7310 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7312 /* Keep grace times if we have got no softlimit arguments */
7313 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7314 dqb->dqb_valid |= QIF_BTIME;
7315 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7318 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7319 dqb->dqb_valid |= QIF_ITIME;
7320 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7324 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7325 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7327 rc = llapi_quotactl(mnt, qctl);
7331 "%s setquota: cannot quotactl '%s' '%s': %s",
7333 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7340 /* Converts seconds value into format string
7341 * result is returned in buf
7343 * 1. result is in descenting order: 1w2d3h4m5s
7344 * 2. zero fields are not filled (except for p. 3): 5d1s
7345 * 3. zero seconds value is presented as "0s"
7347 static char *__sec2str(time_t seconds, char *buf)
7349 const char spec[] = "smhdw";
7350 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7355 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7356 c = seconds / mult[i];
7358 if (c > 0 || (i == 0 && buf == tail))
7359 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7368 static void sec2str(time_t seconds, char *buf, int rc)
7375 tail = __sec2str(seconds, tail);
7377 if (rc && tail - buf < 39) {
7383 static void diff2str(time_t seconds, char *buf, time_t now)
7388 if (seconds <= now) {
7389 strcpy(buf, "none");
7392 __sec2str(seconds - now, buf);
7395 static void print_quota_title(char *name, struct if_quotactl *qctl,
7396 bool human_readable, bool show_default)
7399 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7400 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7401 "Filesystem", "bquota", "blimit", "bgrace",
7402 "iquota", "ilimit", "igrace");
7404 printf("Disk quotas for %s %s (%cid %u):\n",
7405 qtype_name(qctl->qc_type), name,
7406 *qtype_name(qctl->qc_type), qctl->qc_id);
7407 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7408 "Filesystem", human_readable ? "used" : "kbytes",
7409 "quota", "limit", "grace",
7410 "files", "quota", "limit", "grace");
7414 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7417 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7420 snprintf(buf, buflen, "%5.4gP",
7421 (double)num / ((__u64)1 << 40));
7423 snprintf(buf, buflen, "%5.4gT",
7424 (double)num / (1 << 30));
7426 snprintf(buf, buflen, "%5.4gG",
7427 (double)num / (1 << 20));
7429 snprintf(buf, buflen, "%5.4gM",
7430 (double)num / (1 << 10));
7432 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7436 #define STRBUF_LEN 32
7437 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7438 int rc, bool h, bool show_default)
7444 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7445 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7446 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7447 int bover = 0, iover = 0;
7448 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7449 char numbuf[3][STRBUF_LEN];
7451 char strbuf[STRBUF_LEN];
7453 if (dqb->dqb_bhardlimit &&
7454 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7456 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7457 if (dqb->dqb_btime > now)
7463 if (dqb->dqb_ihardlimit &&
7464 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7466 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7467 if (dqb->dqb_itime > now)
7473 if (strlen(mnt) > 15)
7474 printf("%s\n%15s", mnt, "");
7476 printf("%15s", mnt);
7479 diff2str(dqb->dqb_btime, timebuf, now);
7480 else if (show_default)
7481 snprintf(timebuf, sizeof(timebuf), "%llu",
7482 (unsigned long long)dqb->dqb_btime);
7484 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7485 strbuf, sizeof(strbuf), h);
7486 if (rc == -EREMOTEIO)
7487 sprintf(numbuf[0], "%s*", strbuf);
7489 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7490 "%s" : "[%s]", strbuf);
7492 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7493 if (type == QC_GENERAL)
7494 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7495 "%s" : "[%s]", strbuf);
7497 sprintf(numbuf[1], "%s", "-");
7499 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7500 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7501 "%s" : "[%s]", strbuf);
7504 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7506 printf(" %7s%c %6s %7s %7s",
7507 numbuf[0], bover ? '*' : ' ', numbuf[1],
7508 numbuf[2], bover > 1 ? timebuf : "-");
7511 diff2str(dqb->dqb_itime, timebuf, now);
7512 else if (show_default)
7513 snprintf(timebuf, sizeof(timebuf), "%llu",
7514 (unsigned long long)dqb->dqb_itime);
7516 snprintf(numbuf[0], sizeof(numbuf),
7517 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7518 (uintmax_t)dqb->dqb_curinodes);
7520 if (type == QC_GENERAL)
7521 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7523 (uintmax_t)dqb->dqb_isoftlimit);
7525 sprintf(numbuf[1], "%s", "-");
7527 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7528 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7531 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7532 else if (type != QC_OSTIDX)
7533 printf(" %7s%c %6s %7s %7s",
7534 numbuf[0], iover ? '*' : ' ', numbuf[1],
7535 numbuf[2], iover > 1 ? timebuf : "-");
7537 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7539 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7540 qctl->qc_cmd == Q_GETOINFO) {
7544 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7545 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7547 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7548 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7549 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7551 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7553 printf("Block grace time: %s; Inode grace time: %s\n",
7554 bgtimebuf, igtimebuf);
7558 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7562 /* format is "lustre-OST0001" */
7563 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7565 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7570 *idx = strtoul(dash, &endp, 16);
7571 if (*idx > 0xffff) {
7572 fprintf(stderr, "wrong index %s\n", tgtname);
7579 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7580 bool h, __u64 *total)
7582 int rc = 0, rc1 = 0, count = 0, i = 0;
7583 char **list = NULL, *buffer = NULL;
7584 __u32 valid = qctl->qc_valid;
7586 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7589 /* Is it correct for the case OST0000, OST0002, OST0003 -
7590 * we will ask OST0001 that is absent and won't ask OST0003? */
7591 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7593 fprintf(stderr, "can not get %s count: %s\n",
7594 is_mdt ? "mdt" : "ost", strerror(-rc));
7598 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7599 char fname[PATH_MAX];
7600 char fsname[LUSTRE_MAXFSNAME + 1];
7601 int bufsize = sizeof(struct obd_uuid) * count;
7603 rc = llapi_search_fsname(mnt, fsname);
7605 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7609 buffer = malloc(bufsize + sizeof(*list) * count);
7612 list = (char **)(buffer + bufsize);
7613 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7614 count = llapi_get_poolmembers(fname, list, count,
7620 for (i = 0; i < count; i++) {
7621 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7624 if (tgt_name2index(list[i], &index))
7626 qctl->qc_idx = index;
7631 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7632 rc = llapi_quotactl(mnt, qctl);
7634 /* It is remote client case. */
7635 if (rc == -EOPNOTSUPP) {
7642 fprintf(stderr, "quotactl %s%d failed.\n",
7643 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7647 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7648 qctl->qc_valid, 0, h, false);
7649 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7650 qctl->qc_dqblk.dqb_bhardlimit;
7655 qctl->qc_valid = valid;
7659 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7660 int verbose, int quiet, bool human_readable,
7663 int rc1 = 0, rc2 = 0, rc3 = 0;
7664 char *obd_type = (char *)qctl->obd_type;
7665 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7666 __u64 total_ialloc = 0, total_balloc = 0;
7667 bool use_default_for_blk = false;
7668 bool use_default_for_file = false;
7671 rc1 = llapi_quotactl(mnt, qctl);
7675 fprintf(stderr, "%s quotas are not enabled.\n",
7676 qtype_name(qctl->qc_type));
7679 fprintf(stderr, "Permission denied.\n");
7682 /* We already got error message. */
7685 fprintf(stderr, "Unexpected quotactl error: %s\n",
7690 if (!show_default && qctl->qc_id == 0) {
7691 qctl->qc_dqblk.dqb_bhardlimit = 0;
7692 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7693 qctl->qc_dqblk.dqb_ihardlimit = 0;
7694 qctl->qc_dqblk.dqb_isoftlimit = 0;
7695 qctl->qc_dqblk.dqb_btime = 0;
7696 qctl->qc_dqblk.dqb_itime = 0;
7697 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7700 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7701 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7702 use_default_for_blk = true;
7703 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7706 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7707 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7708 use_default_for_file = true;
7709 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7712 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7713 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7714 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7715 print_quota_title(name, qctl, human_readable, show_default);
7717 if (rc1 && *obd_type)
7718 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7720 if (qctl->qc_valid != QC_GENERAL)
7723 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7724 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7725 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7726 (QIF_LIMITS|QIF_USAGE));
7728 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7730 if (!show_default && verbose &&
7731 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7732 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7733 char strbuf[STRBUF_LEN];
7735 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7737 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7739 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7741 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7742 (uintmax_t)total_ialloc, strbuf);
7745 if (use_default_for_blk)
7746 printf("%cid %u is using default block quota setting\n",
7747 *qtype_name(qctl->qc_type), qctl->qc_id);
7749 if (use_default_for_file)
7750 printf("%cid %u is using default file quota setting\n",
7751 *qtype_name(qctl->qc_type), qctl->qc_id);
7753 if (rc1 || rc2 || rc3 || inacc)
7754 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7768 static int lfs_project(int argc, char **argv)
7770 int ret = 0, err = 0, c, i;
7771 struct project_handle_control phc = { 0 };
7772 enum lfs_project_ops_t op;
7775 phc.assign_projid = false;
7776 /* default action */
7777 op = LFS_PROJECT_LIST;
7779 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7782 if (op != LFS_PROJECT_LIST) {
7784 "%s: cannot specify '-c' '-C' '-s' together\n",
7789 op = LFS_PROJECT_CHECK;
7792 if (op != LFS_PROJECT_LIST) {
7794 "%s: cannot specify '-c' '-C' '-s' together\n",
7799 op = LFS_PROJECT_CLEAR;
7802 if (op != LFS_PROJECT_LIST) {
7804 "%s: cannot specify '-c' '-C' '-s' together\n",
7809 phc.set_inherit = true;
7810 op = LFS_PROJECT_SET;
7816 phc.keep_projid = true;
7819 phc.recursive = true;
7822 if (str2quotaid(&phc.projid, optarg)) {
7824 "Invalid project ID: %s\n",
7829 phc.assign_projid = true;
7833 phc.newline = false;
7836 fprintf(stderr, "%s: invalid option '%c'\n",
7842 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7843 op = LFS_PROJECT_SET;
7844 phc.set_projid = true;
7845 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7846 phc.set_projid = true;
7850 case LFS_PROJECT_CHECK:
7851 if (phc.keep_projid) {
7853 "%s: '-k' is useless together with '-c'\n",
7858 case LFS_PROJECT_CLEAR:
7861 "%s: '-0' is useless together with '-C'\n",
7865 if (phc.assign_projid) {
7867 "%s: '-p' is useless together with '-C'\n",
7872 case LFS_PROJECT_SET:
7875 "%s: '-0' is useless together with '-s'\n",
7879 if (phc.keep_projid) {
7881 "%s: '-k' is useless together with '-s'\n",
7889 "%s: '-0' is useless for list operations\n",
7899 fprintf(stderr, "%s: missing file or directory target(s)\n",
7904 for (i = 0; i < argc; i++) {
7906 case LFS_PROJECT_CHECK:
7907 err = lfs_project_check(argv[i], &phc);
7909 case LFS_PROJECT_LIST:
7910 err = lfs_project_list(argv[i], &phc);
7912 case LFS_PROJECT_CLEAR:
7913 err = lfs_project_clear(argv[i], &phc);
7915 case LFS_PROJECT_SET:
7916 err = lfs_project_set(argv[i], &phc);
7928 static int lfs_quota(int argc, char **argv)
7931 char *mnt, *name = NULL;
7932 struct if_quotactl *qctl;
7934 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7935 __u32 valid = QC_GENERAL, idx = 0;
7936 bool human_readable = false;
7937 bool show_default = false;
7939 struct option long_opts[] = {
7940 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
7943 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7947 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7948 qctl->qc_type = ALLQUOTA;
7949 obd_uuid = (char *)qctl->obd_uuid.uuid;
7951 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7952 long_opts, NULL)) != -1) {
7955 show_default = true;
7960 show_default = true;
7965 show_default = true;
7969 if (qctl->qc_type != ALLQUOTA) {
7971 "%s quota: only one of -u, -g, or -p may be specified\n",
7976 qctl->qc_type = qtype;
7979 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7982 valid = qctl->qc_valid = QC_UUID;
7983 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
7986 valid = qctl->qc_valid = QC_MDTIDX;
7987 idx = qctl->qc_idx = atoi(optarg);
7988 if (idx == 0 && *optarg != '0') {
7990 "%s quota: invalid MDT index '%s'\n",
7997 valid = qctl->qc_valid = QC_OSTIDX;
7998 idx = qctl->qc_idx = atoi(optarg);
7999 if (idx == 0 && *optarg != '0') {
8001 "%s quota: invalid OST index '%s'\n",
8014 human_readable = true;
8017 if (lfs_verify_poolarg(optarg)) {
8021 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8022 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
8023 LUSTRE_Q_GETINFOPOOL :
8024 LUSTRE_Q_GETQUOTAPOOL;
8027 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8028 progname, argv[optind - 1]);
8034 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8035 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8036 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8037 qctl->qc_type == ALLQUOTA &&
8038 optind == argc - 1 && !show_default) {
8041 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8042 qctl->qc_type = qtype;
8043 qctl->qc_valid = valid;
8044 if (qtype == USRQUOTA) {
8045 qctl->qc_id = geteuid();
8046 rc = uid2name(&name, qctl->qc_id);
8048 qctl->qc_id = getegid();
8049 rc = gid2name(&name, qctl->qc_id);
8050 memset(&qctl->qc_dqblk, 0,
8051 sizeof(qctl->qc_dqblk));
8056 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8057 human_readable, show_default);
8062 /* lfs quota -u username /path/to/lustre/mount */
8063 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8064 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8065 /* options should be followed by u/g-name and mntpoint */
8066 if ((!show_default && optind + 2 != argc) ||
8067 (show_default && optind + 1 != argc) ||
8068 qctl->qc_type == ALLQUOTA) {
8070 "%s quota: name and mount point must be specified\n",
8076 if (!show_default) {
8077 name = argv[optind++];
8078 switch (qctl->qc_type) {
8080 rc = name2uid(&qctl->qc_id, name);
8083 rc = name2gid(&qctl->qc_id, name);
8086 rc = name2projid(&qctl->qc_id, name);
8093 qctl->qc_valid = QC_GENERAL;
8094 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
8099 if (str2quotaid(&qctl->qc_id, name)) {
8100 fprintf(stderr, "%s quota: invalid id '%s'\n",
8106 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8107 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8114 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8115 human_readable, show_default);
8120 #endif /* HAVE_SYS_QUOTA_H! */
8122 static int flushctx_ioctl(char *mp)
8126 fd = open(mp, O_RDONLY);
8128 fprintf(stderr, "flushctx: error open %s: %s\n",
8129 mp, strerror(errno));
8133 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8135 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8136 mp, strerror(errno));
8142 static int lfs_flushctx(int argc, char **argv)
8144 int kdestroy = 0, reap = 0, c;
8145 char mntdir[PATH_MAX] = {'\0'};
8149 while ((c = getopt(argc, argv, "kr")) != -1) {
8159 "error: %s: option '-%c' unrecognized\n",
8166 rc = system("kdestroy > /dev/null");
8168 rc = WEXITSTATUS(rc);
8170 "error destroying tickets: %d, continuing\n",
8175 if (optind >= argc) {
8176 /* flush for all mounted lustre fs. */
8177 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8178 /* Check if we have a mount point */
8179 if (mntdir[0] == '\0')
8182 if (flushctx_ioctl(mntdir))
8185 mntdir[0] = '\0'; /* avoid matching in next loop */
8188 /* flush fs as specified */
8189 while (optind < argc) {
8190 if (flushctx_ioctl(argv[optind++]))
8196 rc = system("keyctl reap > /dev/null");
8198 rc = WEXITSTATUS(rc);
8199 fprintf(stderr, "error reaping keyring: %d\n", rc);
8206 static int lfs_changelog(int argc, char **argv)
8208 void *changelog_priv;
8209 struct changelog_rec *rec;
8210 long long startrec = 0, endrec = 0;
8212 struct option long_opts[] = {
8213 { .val = 'f', .name = "follow", .has_arg = no_argument },
8215 char short_opts[] = "f";
8218 while ((rc = getopt_long(argc, argv, short_opts,
8219 long_opts, NULL)) != -1) {
8226 "%s changelog: unrecognized option '%s'\n",
8227 progname, argv[optind - 1]);
8231 if (optind >= argc) {
8232 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8237 mdd = argv[optind++];
8239 startrec = strtoll(argv[optind++], NULL, 10);
8241 endrec = strtoll(argv[optind++], NULL, 10);
8243 rc = llapi_changelog_start(&changelog_priv,
8244 CHANGELOG_FLAG_BLOCK |
8245 CHANGELOG_FLAG_JOBID |
8246 CHANGELOG_FLAG_EXTRA_FLAGS |
8247 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8250 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8251 progname, strerror(errno = -rc));
8255 rc = llapi_changelog_set_xflags(changelog_priv,
8256 CHANGELOG_EXTRA_FLAG_UIDGID |
8257 CHANGELOG_EXTRA_FLAG_NID |
8258 CHANGELOG_EXTRA_FLAG_OMODE |
8259 CHANGELOG_EXTRA_FLAG_XATTR);
8262 "%s changelog: cannot set xflags for changelog: %s\n",
8263 progname, strerror(errno = -rc));
8267 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8271 if (endrec && rec->cr_index > endrec) {
8272 llapi_changelog_free(&rec);
8275 if (rec->cr_index < startrec) {
8276 llapi_changelog_free(&rec);
8280 secs = rec->cr_time >> 30;
8281 gmtime_r(&secs, &ts);
8282 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8283 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8284 changelog_type2str(rec->cr_type),
8285 ts.tm_hour, ts.tm_min, ts.tm_sec,
8286 (int)(rec->cr_time & ((1 << 30) - 1)),
8287 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8288 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8290 if (rec->cr_flags & CLF_JOBID) {
8291 struct changelog_ext_jobid *jid =
8292 changelog_rec_jobid(rec);
8294 if (jid->cr_jobid[0] != '\0')
8295 printf(" j=%s", jid->cr_jobid);
8298 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8299 struct changelog_ext_extra_flags *ef =
8300 changelog_rec_extra_flags(rec);
8302 printf(" ef=0x%llx",
8303 (unsigned long long)ef->cr_extra_flags);
8305 if (ef->cr_extra_flags & CLFE_UIDGID) {
8306 struct changelog_ext_uidgid *uidgid =
8307 changelog_rec_uidgid(rec);
8309 printf(" u=%llu:%llu",
8310 (unsigned long long)uidgid->cr_uid,
8311 (unsigned long long)uidgid->cr_gid);
8313 if (ef->cr_extra_flags & CLFE_NID) {
8314 struct changelog_ext_nid *nid =
8315 changelog_rec_nid(rec);
8318 libcfs_nid2str(nid->cr_nid));
8321 if (ef->cr_extra_flags & CLFE_OPEN) {
8322 struct changelog_ext_openmode *omd =
8323 changelog_rec_openmode(rec);
8324 char mode[] = "---";
8326 /* exec mode must be exclusive */
8327 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8330 if (omd->cr_openflags & MDS_FMODE_READ)
8332 if (omd->cr_openflags &
8339 if (strcmp(mode, "---") != 0)
8340 printf(" m=%s", mode);
8343 if (ef->cr_extra_flags & CLFE_XATTR) {
8344 struct changelog_ext_xattr *xattr =
8345 changelog_rec_xattr(rec);
8347 if (xattr->cr_xattr[0] != '\0')
8348 printf(" x=%s", xattr->cr_xattr);
8352 if (!fid_is_zero(&rec->cr_pfid))
8353 printf(" p="DFID, PFID(&rec->cr_pfid));
8354 if (rec->cr_namelen)
8355 printf(" %.*s", rec->cr_namelen,
8356 changelog_rec_name(rec));
8358 if (rec->cr_flags & CLF_RENAME) {
8359 struct changelog_ext_rename *rnm =
8360 changelog_rec_rename(rec);
8362 if (!fid_is_zero(&rnm->cr_sfid))
8363 printf(" s="DFID" sp="DFID" %.*s",
8364 PFID(&rnm->cr_sfid),
8365 PFID(&rnm->cr_spfid),
8366 (int)changelog_rec_snamelen(rec),
8367 changelog_rec_sname(rec));
8371 llapi_changelog_free(&rec);
8374 llapi_changelog_fini(&changelog_priv);
8377 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8378 progname, strerror(errno = -rc));
8380 return (rc == 1 ? 0 : rc);
8383 static int lfs_changelog_clear(int argc, char **argv)
8391 endrec = strtoll(argv[3], NULL, 10);
8393 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8396 fprintf(stderr, "%s: record out of range: %llu\n",
8398 else if (rc == -ENOENT)
8399 fprintf(stderr, "%s: no changelog user: %s\n",
8402 fprintf(stderr, "%s error: %s\n", argv[0],
8411 static void rstripc(char *str, int c)
8413 char *end = str + strlen(str);
8415 for (; str < end && end[-1] == c; --end)
8419 static int lfs_fid2path(int argc, char **argv)
8421 struct option long_opts[] = {
8422 { .val = 'c', .name = "cur", .has_arg = no_argument },
8423 { .val = 'c', .name = "current", .has_arg = no_argument },
8424 { .val = 'c', .name = "print-link", .has_arg = no_argument },
8425 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
8426 { .val = 'l', .name = "link", .has_arg = required_argument },
8428 char short_opts[] = "cfl:pr:";
8429 bool print_link = false;
8430 bool print_fid = false;
8432 char mnt_dir[PATH_MAX] = "";
8434 char *path_or_fsname;
8435 long long recno = -1;
8437 char *endptr = NULL;
8442 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
8451 linkno = strtol(optarg, &endptr, 10);
8452 if (*endptr != '\0') {
8454 "%s fid2path: invalid linkno '%s'\n",
8460 /* recno is something to do with changelogs
8461 * that was never implemented. We just pass it
8462 * through for the MDT to ignore.
8464 recno = strtoll(optarg, &endptr, 10);
8465 if (*endptr != '\0') {
8467 "%s fid2path: invalid recno '%s'\n",
8474 "%s fid2path: unrecognized option '%s'\n",
8475 progname, argv[optind - 1]);
8480 if (argc - optind < 2) {
8482 "Usage: %s fid2path FSNAME|ROOT FID...\n",
8487 path_or_fsname = argv[optind];
8489 if (*path_or_fsname == '/') {
8490 print_mnt_dir = true;
8491 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
8493 print_mnt_dir = false;
8494 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
8499 "%s fid2path: cannot resolve mount point for '%s': %s\n",
8500 progname, path_or_fsname, strerror(-rc));
8504 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
8507 "%s fid2path: cannot open mount point for '%s': %s\n",
8508 progname, path_or_fsname, strerror(-rc));
8512 /* Strip trailing slashes from mnt_dir. */
8513 rstripc(mnt_dir + 1, '/');
8515 for (i = optind + 1; i < argc; i++) {
8516 const char *fid_str = argv[i];
8520 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
8523 "%s fid2path: invalid FID '%s'\n",
8531 int linktmp = (linkno >= 0) ? linkno : 0;
8533 int oldtmp = linktmp;
8534 long long rectmp = recno;
8535 char path_buf[PATH_MAX];
8537 rc2 = llapi_fid2path_at(mnt_fd, &fid,
8538 path_buf, sizeof(path_buf), &rectmp, &linktmp);
8541 "%s fid2path: cannot find %s %s: %s\n",
8542 progname, path_or_fsname, fid_str,
8550 printf("%s ", fid_str);
8553 printf("%d ", linktmp);
8555 /* You may think this looks wrong or weird (and it is!)
8556 * but we are actually trying to preserve the old quirky
8557 * behaviors (enforced by our old quirky tests!) that
8558 * make lfs so much fun to work on:
8560 * lustre 0x200000007:0x1:0x0 => "/"
8561 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
8563 * Note that llapi_fid2path() returns "" for the root
8567 print_mnt_dir ? mnt_dir : "",
8568 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
8572 /* specified linkno */
8575 if (oldtmp == linktmp)
8587 static int lfs_path2fid(int argc, char **argv)
8589 struct option long_opts[] = {
8590 { .val = 'p', .name = "parents", .has_arg = no_argument },
8593 const char short_opts[] = "p";
8594 const char *sep = "";
8597 bool show_parents = false;
8599 while ((rc = getopt_long(argc, argv, short_opts,
8600 long_opts, NULL)) != -1) {
8603 show_parents = true;
8607 "%s path2fid: unrecognized option '%s'\n",
8608 progname, argv[optind - 1]);
8613 if (optind > argc - 1) {
8614 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8617 } else if (optind < argc - 1) {
8622 for (path = argv + optind; *path != NULL; path++) {
8625 if (!show_parents) {
8626 err = llapi_path2fid(*path, &fid);
8628 printf("%s%s"DFID"\n",
8629 *sep != '\0' ? *path : "", sep,
8632 char name[NAME_MAX + 1];
8633 unsigned int linkno = 0;
8635 while ((err = llapi_path2parent(*path, linkno, &fid,
8636 name, sizeof(name))) == 0) {
8637 if (*sep != '\0' && linkno == 0)
8638 printf("%s%s", *path, sep);
8640 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8645 /* err == -ENODATA is end-of-loop */
8646 if (linkno > 0 && err == -ENODATA) {
8654 "%s path2fid: cannot get %sfid for '%s': %s\n",
8655 progname, show_parents ? "parent " : "", *path,
8667 #define MAX_ERRNO 4095
8668 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8670 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8674 rc = llapi_rmfid(device, fa);
8676 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
8677 progname, strerror(-rc));
8681 for (k = 0; k < fa->fa_nr; k++) {
8682 rc2 = (__s32)fa->fa_fids[k].f_ver;
8683 if (!IS_ERR_VALUE(rc2))
8689 fa->fa_fids[k].f_ver = 0;
8690 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
8691 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
8697 static int lfs_rmfid(int argc, char **argv)
8699 char *fidstr, *device;
8700 int rc = 0, rc2, nr;
8701 struct fid_array *fa;
8703 if (optind > argc - 1) {
8704 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8708 device = argv[optind++];
8711 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8717 while (optind < argc) {
8720 fidstr = argv[optind++];
8721 while (*fidstr == '[')
8723 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8725 fprintf(stderr, "unrecognized FID: %s\n",
8730 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8731 /* start another batch */
8732 rc2 = lfs_rmfid_and_show_errors(device, fa);
8739 rc2 = lfs_rmfid_and_show_errors(device, fa);
8747 static int lfs_data_version(int argc, char **argv)
8754 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8757 fprintf(stderr, "%s data_version: FILE must be specified\n",
8762 while ((c = getopt(argc, argv, "nrw")) != -1) {
8765 data_version_flags = 0;
8768 data_version_flags |= LL_DV_RD_FLUSH;
8771 data_version_flags |= LL_DV_WR_FLUSH;
8775 "%s data_version: unrecognized option '%s'\n",
8776 progname, argv[optind - 1]);
8780 if (optind == argc) {
8781 fprintf(stderr, "%s data_version: FILE must be specified\n",
8786 path = argv[optind];
8787 fd = open(path, O_RDONLY);
8790 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8791 progname, path, strerror(-rc));
8795 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8798 "%s data_version: cannot get version for '%s': %s\n",
8799 progname, path, strerror(-rc));
8801 printf("%ju" "\n", (uintmax_t)data_version);
8807 static int lfs_hsm_state(int argc, char **argv)
8812 struct hsm_user_state hus;
8820 rc = llapi_hsm_state_get(path, &hus);
8822 fprintf(stderr, "can't get hsm state for %s: %s\n",
8823 path, strerror(errno = -rc));
8827 /* Display path name and status flags */
8828 printf("%s: (0x%08x)", path, hus.hus_states);
8830 if (hus.hus_states & HS_RELEASED)
8831 printf(" released");
8832 if (hus.hus_states & HS_EXISTS)
8834 if (hus.hus_states & HS_DIRTY)
8836 if (hus.hus_states & HS_ARCHIVED)
8837 printf(" archived");
8838 /* Display user-settable flags */
8839 if (hus.hus_states & HS_NORELEASE)
8840 printf(" never_release");
8841 if (hus.hus_states & HS_NOARCHIVE)
8842 printf(" never_archive");
8843 if (hus.hus_states & HS_LOST)
8844 printf(" lost_from_hsm");
8846 if (hus.hus_archive_id != 0)
8847 printf(", archive_id:%d", hus.hus_archive_id);
8850 } while (++i < argc);
8855 #define LFS_HSM_SET 0
8856 #define LFS_HSM_CLEAR 1
8859 * Generic function to set or clear HSM flags.
8860 * Used by hsm_set and hsm_clear.
8862 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8864 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8866 struct option long_opts[] = {
8867 { .val = 'A', .name = "archived", .has_arg = no_argument },
8868 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8869 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8870 { .val = 'e', .name = "exists", .has_arg = no_argument },
8871 { .val = 'l', .name = "lost", .has_arg = no_argument },
8872 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8873 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8875 char short_opts[] = "lraAdei:";
8879 __u32 archive_id = 0;
8885 while ((c = getopt_long(argc, argv, short_opts,
8886 long_opts, NULL)) != -1) {
8892 mask |= HS_NOARCHIVE;
8895 mask |= HS_ARCHIVED;
8898 mask |= HS_NORELEASE;
8907 archive_id = strtol(optarg, &end, 10);
8909 fprintf(stderr, "invalid archive_id: '%s'\n",
8917 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8918 argv[0], argv[optind - 1]);
8923 /* User should have specified a flag */
8927 while (optind < argc) {
8928 path = argv[optind];
8930 /* If mode == 0, this means we apply the mask. */
8931 if (mode == LFS_HSM_SET)
8932 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8934 rc = llapi_hsm_state_set(path, 0, mask, 0);
8937 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8938 path, strerror(errno = -rc));
8947 static int lfs_hsm_action(int argc, char **argv)
8952 struct hsm_current_action hca;
8953 struct hsm_extent he;
8954 enum hsm_user_action hua;
8955 enum hsm_progress_states hps;
8963 rc = llapi_hsm_current_action(path, &hca);
8965 fprintf(stderr, "can't get hsm action for %s: %s\n",
8966 path, strerror(errno = -rc));
8969 he = hca.hca_location;
8970 hua = hca.hca_action;
8971 hps = hca.hca_state;
8973 printf("%s: %s", path, hsm_user_action2name(hua));
8975 /* Skip file without action */
8976 if (hca.hca_action == HUA_NONE) {
8981 printf(" %s ", hsm_progress_state2name(hps));
8983 if ((hps == HPS_RUNNING) &&
8984 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8985 printf("(%llu bytes moved)\n",
8986 (unsigned long long)he.length);
8987 else if ((he.offset + he.length) == LUSTRE_EOF)
8988 printf("(from %llu to EOF)\n",
8989 (unsigned long long)he.offset);
8991 printf("(from %llu to %llu)\n",
8992 (unsigned long long)he.offset,
8993 (unsigned long long)(he.offset + he.length));
8995 } while (++i < argc);
9000 static int lfs_hsm_set(int argc, char **argv)
9002 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9005 static int lfs_hsm_clear(int argc, char **argv)
9007 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9011 * Check file state and return its fid, to be used by lfs_hsm_request().
9013 * \param[in] file Path to file to check
9014 * \param[in,out] fid Pointer to allocated lu_fid struct.
9015 * \param[in,out] last_dev Pointer to last device id used.
9017 * \return 0 on success.
9019 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9025 rc = lstat(file, &st);
9027 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9031 * Checking for regular file as archiving as posix copytool
9032 * rejects archiving files other than regular files
9034 if (!S_ISREG(st.st_mode)) {
9035 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9038 /* A request should be ... */
9039 if (*last_dev != st.st_dev && *last_dev != 0) {
9041 "All files should be on the same filesystem: %s\n",
9045 *last_dev = st.st_dev;
9047 rc = llapi_path2fid(file, fid);
9049 fprintf(stderr, "Cannot read FID of %s: %s\n",
9050 file, strerror(-rc));
9056 /* Fill an HSM HUR item with a given file name.
9058 * If mntpath is set, then the filename is actually a FID, and no
9059 * lookup on the filesystem will be performed.
9061 * \param[in] hur the user request to fill
9062 * \param[in] idx index of the item inside the HUR to fill
9063 * \param[in] mntpath mountpoint of Lustre
9064 * \param[in] fname filename (if mtnpath is NULL)
9065 * or FID (if mntpath is set)
9066 * \param[in] last_dev pointer to last device id used
9068 * \retval 0 on success
9069 * \retval CMD_HELP or a negative errno on error
9071 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9072 const char *mntpath, const char *fname,
9075 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9078 hui->hui_extent.length = -1;
9081 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9083 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9086 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9090 hur->hur_request.hr_itemcount++;
9095 static int lfs_hsm_request(int argc, char **argv, int action)
9097 struct option long_opts[] = {
9098 { .val = 'a', .name = "archive", .has_arg = required_argument },
9099 { .val = 'D', .name = "data", .has_arg = required_argument },
9100 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9101 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9104 char short_opts[] = "l:D:a:m:";
9105 struct hsm_user_request *hur, *oldhur;
9110 char *filelist = NULL;
9111 char fullpath[PATH_MAX];
9112 char *opaque = NULL;
9116 int nbfile_alloc = 0;
9117 char *some_file = NULL;
9118 char *mntpath = NULL;
9124 while ((c = getopt_long(argc, argv, short_opts,
9125 long_opts, NULL)) != -1) {
9134 if (action != HUA_ARCHIVE &&
9135 action != HUA_REMOVE) {
9137 "error: -a is supported only when archiving or removing\n");
9140 archive_id = atoi(optarg);
9145 some_file = strdup(optarg);
9151 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9152 argv[0], argv[optind - 1]);
9157 /* All remaining args are files, so we have at least nbfile */
9158 nbfile = argc - optind;
9160 if ((nbfile == 0) && (!filelist))
9164 opaque_len = strlen(opaque);
9167 * Alloc the request structure with enough place to store all files
9168 * from command line.
9170 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9172 fprintf(stderr, "Cannot create the request: %s\n",
9176 nbfile_alloc = nbfile;
9178 hur->hur_request.hr_action = action;
9179 hur->hur_request.hr_archive_id = archive_id;
9180 hur->hur_request.hr_flags = 0;
9182 /* All remaining args are files, add them */
9183 if (nbfile != 0 && some_file == NULL)
9184 some_file = strdup(argv[optind]);
9186 for (i = 0; i < nbfile; i++) {
9187 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9193 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9195 /* If a filelist was specified, read the filelist from it. */
9197 fp = fopen(filelist, "r");
9199 fprintf(stderr, "Cannot read the file list %s: %s\n",
9200 filelist, strerror(errno));
9205 while ((rc = getline(&line, &len, fp)) != -1) {
9207 * If allocated buffer was too small, get something
9210 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9213 nbfile_alloc = nbfile_alloc * 2 + 1;
9215 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9219 "hsm: cannot allocate the request: %s\n",
9226 size = hur_len(oldhur);
9229 "hsm: cannot allocate %u files + %u bytes data\n",
9230 oldhur->hur_request.hr_itemcount,
9231 oldhur->hur_request.hr_data_len);
9238 memcpy(hur, oldhur, size);
9243 if (line[strlen(line) - 1] == '\n')
9244 line[strlen(line) - 1] = '\0';
9246 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9247 mntpath, line, &last_dev);
9263 /* If a --data was used, add it to the request */
9264 hur->hur_request.hr_data_len = opaque_len;
9266 memcpy(hur_data(hur), opaque, opaque_len);
9268 /* Send the HSM request */
9269 if (realpath(some_file, fullpath) == NULL) {
9270 fprintf(stderr, "Could not find path '%s': %s\n",
9271 some_file, strerror(errno));
9273 rc = llapi_hsm_request(fullpath, hur);
9275 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9276 some_file, strerror(-rc));
9286 static int lfs_hsm_archive(int argc, char **argv)
9288 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9291 static int lfs_hsm_restore(int argc, char **argv)
9293 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9296 static int lfs_hsm_release(int argc, char **argv)
9298 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9301 static int lfs_hsm_remove(int argc, char **argv)
9303 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9306 static int lfs_hsm_cancel(int argc, char **argv)
9308 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9311 static int lfs_swap_layouts(int argc, char **argv)
9316 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9317 SWAP_LAYOUTS_KEEP_MTIME |
9318 SWAP_LAYOUTS_KEEP_ATIME);
9321 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9323 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9325 int lfs_get_mode(const char *string)
9327 enum lock_mode_user mode;
9329 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9330 if (lock_mode_names[mode] == NULL)
9332 if (strcmp(string, lock_mode_names[mode]) == 0)
9339 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9341 enum lu_ladvise_type advice;
9344 advice < ARRAY_SIZE(ladvise_names); advice++) {
9345 if (ladvise_names[advice] == NULL)
9347 if (strcmp(string, ladvise_names[advice]) == 0)
9351 return LU_LADVISE_INVALID;
9354 static int lfs_ladvise(int argc, char **argv)
9356 struct option long_opts[] = {
9357 { .val = 'a', .name = "advice", .has_arg = required_argument },
9358 { .val = 'b', .name = "background", .has_arg = no_argument },
9359 { .val = 'e', .name = "end", .has_arg = required_argument },
9360 { .val = 'l', .name = "length", .has_arg = required_argument },
9361 { .val = 'm', .name = "mode", .has_arg = required_argument },
9362 { .val = 's', .name = "start", .has_arg = required_argument },
9363 { .val = 'u', .name = "unset", .has_arg = no_argument },
9365 char short_opts[] = "a:be:l:m:s:u";
9370 struct llapi_lu_ladvise advice;
9371 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9372 unsigned long long start = 0;
9373 unsigned long long end = LUSTRE_EOF;
9374 unsigned long long length = 0;
9375 unsigned long long size_units;
9376 unsigned long long flags = 0;
9380 while ((c = getopt_long(argc, argv, short_opts,
9381 long_opts, NULL)) != -1) {
9384 advice_type = lfs_get_ladvice(optarg);
9385 if (advice_type == LU_LADVISE_INVALID) {
9387 "%s: invalid advice type '%s'\n",
9389 fprintf(stderr, "Valid types:");
9391 for (advice_type = 0;
9392 advice_type < ARRAY_SIZE(ladvise_names);
9394 if (ladvise_names[advice_type] == NULL)
9396 fprintf(stderr, " %s",
9397 ladvise_names[advice_type]);
9399 fprintf(stderr, "\n");
9412 rc = llapi_parse_size(optarg, &end,
9415 fprintf(stderr, "%s: bad end offset '%s'\n",
9422 rc = llapi_parse_size(optarg, &start,
9426 "%s: bad start offset '%s'\n",
9433 rc = llapi_parse_size(optarg, &length,
9436 fprintf(stderr, "%s: bad length '%s'\n",
9442 mode = lfs_get_mode(optarg);
9445 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9453 fprintf(stderr, "%s: option '%s' unrecognized\n",
9454 argv[0], argv[optind - 1]);
9459 if (advice_type == LU_LADVISE_INVALID) {
9460 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9461 fprintf(stderr, "Valid types:");
9462 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9464 if (ladvise_names[advice_type] == NULL)
9466 fprintf(stderr, " %s", ladvise_names[advice_type]);
9468 fprintf(stderr, "\n");
9472 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9474 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9479 if (argc <= optind) {
9480 fprintf(stderr, "%s: please give one or more file names\n",
9485 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9486 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9491 if (end == LUSTRE_EOF && length != 0)
9492 end = start + length;
9495 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9496 argv[0], start, end);
9500 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9501 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9506 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9507 fprintf(stderr, "%s: mode is required with lockahead\n",
9512 while (optind < argc) {
9515 path = argv[optind++];
9517 fd = open(path, O_RDONLY);
9519 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9520 argv[0], path, strerror(errno));
9525 advice.lla_start = start;
9526 advice.lla_end = end;
9527 advice.lla_advice = advice_type;
9528 advice.lla_value1 = 0;
9529 advice.lla_value2 = 0;
9530 advice.lla_value3 = 0;
9531 advice.lla_value4 = 0;
9532 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9533 advice.lla_lockahead_mode = mode;
9534 advice.lla_peradvice_flags = flags;
9537 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9541 "%s: cannot give advice '%s' to file '%s': %s\n",
9542 argv[0], ladvise_names[advice_type],
9543 path, strerror(errno));
9549 if (rc == 0 && rc2 < 0)
9555 static const char *const heat_names[] = LU_HEAT_NAMES;
9557 static int lfs_heat_get(int argc, char **argv)
9559 struct lu_heat *heat;
9568 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9570 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9575 while (optind < argc) {
9576 path = argv[optind++];
9578 fd = open(path, O_RDONLY);
9580 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9581 argv[0], path, strerror(errno));
9586 heat->lh_count = OBD_HEAT_COUNT;
9587 rc2 = llapi_heat_get(fd, heat);
9591 "%s: cannot get heat of file '%s': %s\n",
9592 argv[0], path, strerror(errno));
9596 printf("flags: %x\n", heat->lh_flags);
9597 for (i = 0; i < heat->lh_count; i++)
9598 printf("%s: %llu\n", heat_names[i],
9599 (unsigned long long)heat->lh_heat[i]);
9601 if (rc == 0 && rc2 < 0)
9609 static int lfs_heat_set(int argc, char **argv)
9611 struct option long_opts[] = {
9612 { .val = 'c', .name = "clear", .has_arg = no_argument },
9613 { .val = 'o', .name = "off", .has_arg = no_argument },
9614 { .val = 'O', .name = "on", .has_arg = no_argument },
9616 enum lu_heat_flag flags = 0;
9626 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9629 flags |= LU_HEAT_FLAG_CLEAR;
9632 flags |= LU_HEAT_FLAG_CLEAR;
9633 flags |= LU_HEAT_FLAG_OFF;
9636 flags &= ~LU_HEAT_FLAG_OFF;
9641 fprintf(stderr, "%s: option '%s' unrecognized\n",
9642 argv[0], argv[optind - 1]);
9647 if (argc <= optind) {
9648 fprintf(stderr, "%s: please give one or more file names\n",
9653 while (optind < argc) {
9654 path = argv[optind++];
9656 fd = open(path, O_RDONLY);
9658 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9659 argv[0], path, strerror(errno));
9664 rc2 = llapi_heat_set(fd, flags);
9668 "%s: cannot setflags heat of file '%s': %s\n",
9669 argv[0], path, strerror(errno));
9673 if (rc == 0 && rc2 < 0)
9680 * The input string contains a comma delimited list of component ids and
9681 * ranges, for example "1,2-4,7".
9683 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9685 bool end_of_loop = false;
9693 while (!end_of_loop) {
9697 char *endptr = NULL;
9700 ptr = strchrnul(arg, ',');
9701 end_of_loop = *ptr == '\0';
9704 start_index = strtol(arg, &endptr, 0);
9705 if (endptr == arg) /* no data at all */
9707 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9709 if (start_index < 0)
9712 end_index = start_index;
9713 if (*endptr == '-') {
9714 end_index = strtol(endptr + 1, &endptr, 0);
9715 if (*endptr != '\0')
9717 if (end_index < start_index)
9721 for (i = start_index; i <= end_index && size > 0; i++) {
9724 /* remove duplicate */
9725 for (j = 0; j < nr; j++) {
9729 if (j == nr) { /* no duplicate */
9735 if (size == 0 && i < end_index)
9742 if (!end_of_loop && ptr)
9745 return rc < 0 ? rc : nr;
9749 * struct verify_mirror_id - Mirror id to be verified.
9750 * @mirror_id: A specified mirror id.
9751 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9753 struct verify_mirror_id {
9759 * compare_mirror_ids() - Compare mirror ids.
9760 * @layout: Mirror component list.
9761 * @cbdata: Callback data in verify_mirror_id structure.
9763 * This is a callback function called by llapi_layout_comp_iterate()
9764 * to compare the specified mirror id with the one in the current
9765 * component of @layout. If they are the same, then the specified
9766 * mirror id is valid.
9768 * Return: a negative error code on failure or
9769 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9770 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9773 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9775 struct verify_mirror_id *mirror_id_cbdata =
9776 (struct verify_mirror_id *)cbdata;
9780 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9784 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9785 progname, strerror(errno));
9789 if (mirror_id_cbdata->mirror_id == mirror_id) {
9790 mirror_id_cbdata->is_valid_id = true;
9791 return LLAPI_LAYOUT_ITER_STOP;
9794 return LLAPI_LAYOUT_ITER_CONT;
9798 * verify_mirror_ids() - Verify specified mirror ids.
9799 * @fname: Mirrored file name.
9800 * @mirror_ids: Specified mirror ids to be verified.
9801 * @ids_nr: Number of specified mirror ids.
9803 * This function verifies that specified @mirror_ids are valid
9804 * in the mirrored file @fname.
9806 * Return: 0 on success or a negative error code on failure.
9809 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9811 struct llapi_layout *layout = NULL;
9812 struct verify_mirror_id mirror_id_cbdata = { 0 };
9823 if (stat(fname, &stbuf) < 0) {
9824 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9825 progname, fname, strerror(errno));
9830 if (!S_ISREG(stbuf.st_mode)) {
9831 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9837 fd = open(fname, O_DIRECT | O_RDONLY);
9839 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9840 progname, fname, strerror(errno));
9845 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9847 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9848 progname, fname, strerror(errno));
9852 layout = llapi_layout_get_by_fd(fd, 0);
9854 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9855 progname, fname, strerror(errno));
9857 llapi_lease_release(fd);
9861 rc = llapi_layout_flags_get(layout, &flr_state);
9863 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9864 progname, fname, strerror(errno));
9869 flr_state &= LCM_FL_FLR_MASK;
9870 switch (flr_state) {
9873 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9874 progname, fname, llapi_layout_flags_string(flr_state));
9881 for (i = 0; i < ids_nr; i++) {
9882 mirror_id_cbdata.mirror_id = mirror_ids[i];
9883 mirror_id_cbdata.is_valid_id = false;
9885 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9890 "%s: '%s' failed to verify mirror id: %u.\n",
9891 progname, fname, mirror_ids[i]);
9895 if (!mirror_id_cbdata.is_valid_id) {
9898 "%s: '%s' invalid specified mirror id: %u.\n",
9899 progname, fname, mirror_ids[i]);
9905 llapi_layout_free(layout);
9906 llapi_lease_release(fd);
9914 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9915 __u16 *mirror_ids, int ids_nr)
9917 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9918 struct llapi_layout *layout;
9929 if (stat(fname, &stbuf) < 0) {
9930 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9931 progname, fname, strerror(errno));
9935 if (!S_ISREG(stbuf.st_mode)) {
9936 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9942 fd = open(fname, O_DIRECT | O_RDWR);
9944 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9945 progname, fname, strerror(errno));
9950 layout = llapi_layout_get_by_fd(fd, 0);
9952 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9953 progname, fname, strerror(errno));
9958 rc = llapi_layout_flags_get(layout, &flr_state);
9960 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9961 progname, fname, strerror(errno));
9966 flr_state &= LCM_FL_FLR_MASK;
9967 if (flr_state == LCM_FL_NONE) {
9969 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9974 /* get stale component info */
9975 comp_size = llapi_mirror_find_stale(layout, comp_array,
9976 ARRAY_SIZE(comp_array),
9977 mirror_ids, ids_nr);
9978 if (comp_size <= 0) {
9983 ioc->lil_mode = LL_LEASE_WRLCK;
9984 ioc->lil_flags = LL_LEASE_RESYNC;
9985 rc = llapi_lease_set(fd, ioc);
9987 if (rc == -EALREADY)
9991 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9992 progname, fname, strerror(-rc));
9996 /* get the read range [start, end) */
9997 start = comp_array[0].lrc_start;
9998 end = comp_array[0].lrc_end;
9999 for (idx = 1; idx < comp_size; idx++) {
10000 if (comp_array[idx].lrc_start < start)
10001 start = comp_array[idx].lrc_start;
10002 if (end < comp_array[idx].lrc_end)
10003 end = comp_array[idx].lrc_end;
10006 rc = llapi_lease_check(fd);
10007 if (rc != LL_LEASE_WRLCK) {
10008 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10013 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10016 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10017 progname, fname, strerror(-rc));
10019 /* need to do the lease unlock even resync fails */
10020 ioc->lil_mode = LL_LEASE_UNLCK;
10021 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10022 ioc->lil_count = 0;
10023 for (idx = 0; idx < comp_size; idx++) {
10024 if (comp_array[idx].lrc_synced) {
10025 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10030 rc2 = llapi_lease_set(fd, ioc);
10032 * llapi_lease_set returns lease mode when it request to unlock
10036 /* rc2 == 0 means lost lease lock */
10037 if (rc2 == 0 && rc == 0)
10039 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10041 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10045 llapi_layout_free(layout);
10052 static inline int lfs_mirror_resync(int argc, char **argv)
10054 struct ll_ioc_lease *ioc = NULL;
10055 __u16 mirror_ids[128] = { 0 };
10060 struct option long_opts[] = {
10061 { .val = 'o', .name = "only", .has_arg = required_argument },
10062 { .name = NULL } };
10064 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10067 rc = parse_mirror_ids(mirror_ids,
10068 sizeof(mirror_ids) / sizeof(__u16),
10072 "%s: bad mirror ids '%s'.\n",
10079 fprintf(stderr, "%s: options '%s' unrecognized.\n",
10080 argv[0], argv[optind - 1]);
10086 if (argc == optind) {
10087 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10092 if (ids_nr > 0 && argc > optind + 1) {
10094 "%s: option '--only' cannot be used upon multiple files.\n",
10101 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10106 /* set the lease on the file */
10107 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10109 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10110 argv[0], strerror(errno));
10115 for (; optind < argc; optind++) {
10116 rc = lfs_mirror_resync_file(argv[optind], ioc,
10117 mirror_ids, ids_nr);
10118 /* ignore previous file's error, continue with next file */
10121 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10129 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10131 struct llapi_layout *layout;
10134 layout = llapi_layout_get_by_fd(fd, 0);
10136 fprintf(stderr, "could not get layout.\n");
10140 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10142 fprintf(stderr, "failed to iterate layout\n");
10143 llapi_layout_free(layout);
10146 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10147 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10148 llapi_layout_free(layout);
10152 llapi_layout_free(layout);
10158 * Check whether two files are the same file
10159 * \retval 0 same file
10160 * \retval 1 not the same file
10161 * \retval <0 error code
10163 static inline int check_same_file(int fd, const char *f2)
10165 struct stat stbuf1;
10166 struct stat stbuf2;
10168 if (fstat(fd, &stbuf1) < 0)
10171 if (stat(f2, &stbuf2) < 0)
10174 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10175 stbuf1.st_ino == stbuf2.st_ino)
10181 static inline int lfs_mirror_read(int argc, char **argv)
10184 __u16 mirror_id = 0;
10185 const char *outfile = NULL;
10191 const size_t buflen = 4 << 20;
10193 struct option long_opts[] = {
10194 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10195 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10196 { .name = NULL } };
10198 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10203 unsigned long int id;
10206 id = strtoul(optarg, &end, 0);
10207 if (errno != 0 || *end != '\0' || id == 0 ||
10210 "%s %s: invalid mirror ID '%s'\n",
10211 progname, argv[0], optarg);
10215 mirror_id = (__u16)id;
10222 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10223 progname, argv[optind - 1]);
10228 if (argc == optind) {
10229 fprintf(stderr, "%s %s: no mirrored file provided\n",
10230 progname, argv[0]);
10232 } else if (argc > optind + 1) {
10233 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10237 if (mirror_id == 0) {
10238 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10239 progname, argv[0]);
10243 /* open mirror file */
10244 fname = argv[optind];
10245 fd = open(fname, O_DIRECT | O_RDONLY);
10247 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10248 progname, argv[0], fname, strerror(errno));
10252 /* verify mirror id */
10253 rc = verify_mirror_id_by_fd(fd, mirror_id);
10256 "%s %s: cannot find mirror with ID %u in '%s'\n",
10257 progname, argv[0], mirror_id, fname);
10261 /* open output file - O_EXCL ensures output is not the same as input */
10263 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10265 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10266 progname, argv[0], outfile, strerror(errno));
10271 outfd = STDOUT_FILENO;
10274 /* allocate buffer */
10275 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10277 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10278 progname, argv[0], rc);
10284 ssize_t bytes_read;
10285 ssize_t written = 0;
10287 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10288 if (bytes_read < 0) {
10291 "%s %s: fail to read data from mirror %u: %s\n",
10292 progname, argv[0], mirror_id, strerror(-rc));
10297 if (bytes_read == 0)
10300 while (written < bytes_read) {
10303 written2 = write(outfd, buf + written,
10304 bytes_read - written);
10305 if (written2 < 0) {
10307 "%s %s: fail to write %s: %s\n",
10308 progname, argv[0], outfile ? : "STDOUT",
10313 written += written2;
10316 if (written != bytes_read) {
10318 "%s %s: written %ld bytes does not match with %ld read.\n",
10319 progname, argv[0], written, bytes_read);
10341 static inline int lfs_mirror_write(int argc, char **argv)
10344 __u16 mirror_id = 0;
10345 const char *inputfile = NULL;
10351 const size_t buflen = 4 << 20;
10353 size_t page_size = sysconf(_SC_PAGESIZE);
10354 struct ll_ioc_lease_id ioc;
10356 struct option long_opts[] = {
10357 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10358 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10359 { .name = NULL } };
10361 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10366 unsigned long int id;
10369 id = strtoul(optarg, &end, 0);
10370 if (errno != 0 || *end != '\0' || id == 0 ||
10373 "%s %s: invalid mirror ID '%s'\n",
10374 progname, argv[0], optarg);
10378 mirror_id = (__u16)id;
10382 inputfile = optarg;
10385 fprintf(stderr, "%s: option '%s' unrecognized\n",
10386 progname, argv[optind - 1]);
10391 if (argc == optind) {
10392 fprintf(stderr, "%s %s: no mirrored file provided\n",
10393 progname, argv[0]);
10395 } else if (argc > optind + 1) {
10396 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10400 if (mirror_id == 0) {
10401 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10402 progname, argv[0]);
10406 /* open mirror file */
10407 fname = argv[optind];
10408 fd = open(fname, O_DIRECT | O_WRONLY);
10410 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10411 progname, argv[0], fname, strerror(errno));
10415 /* verify mirror id */
10416 rc = verify_mirror_id_by_fd(fd, mirror_id);
10419 "%s %s: cannot find mirror with ID %u in '%s'\n",
10420 progname, argv[0], mirror_id, fname);
10424 /* open input file */
10426 rc = check_same_file(fd, inputfile);
10429 "%s %s: input file cannot be the mirrored file\n",
10430 progname, argv[0]);
10436 inputfd = open(inputfile, O_RDONLY, 0644);
10438 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10439 progname, argv[0], inputfile, strerror(errno));
10444 inputfd = STDIN_FILENO;
10447 /* allocate buffer */
10448 rc = posix_memalign(&buf, page_size, buflen);
10450 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10451 progname, argv[0], rc);
10452 goto close_inputfd;
10455 /* prepare target mirror components instantiation */
10456 ioc.lil_mode = LL_LEASE_WRLCK;
10457 ioc.lil_flags = LL_LEASE_RESYNC;
10458 ioc.lil_mirror_id = mirror_id;
10459 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10462 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10463 progname, argv[0], fname, strerror(errno));
10469 ssize_t bytes_read;
10473 rc = llapi_lease_check(fd);
10474 if (rc != LL_LEASE_WRLCK) {
10475 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10476 progname, argv[0], fname);
10480 bytes_read = read(inputfd, buf, buflen);
10481 if (bytes_read < 0) {
10484 "%s %s: fail to read data from '%s': %s\n",
10485 progname, argv[0], inputfile ? : "STDIN",
10492 if (bytes_read == 0)
10495 /* round up to page align to make direct IO happy. */
10496 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10498 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10503 "%s %s: fail to write to mirror %u: %s\n",
10504 progname, argv[0], mirror_id,
10512 if (pos & (page_size - 1)) {
10513 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10518 ioc.lil_mode = LL_LEASE_UNLCK;
10519 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10521 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10526 "%s %s: release lease lock of '%s' failed: %s\n",
10527 progname, argv[0], fname, strerror(errno));
10544 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10546 struct llapi_layout *layout;
10547 struct collect_ids_data cid = { .cid_ids = ids,
10549 .cid_exclude = exclude_id, };
10552 layout = llapi_layout_get_by_fd(fd, 0);
10554 fprintf(stderr, "could not get layout\n");
10558 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10560 fprintf(stderr, "failed to iterate layout\n");
10561 llapi_layout_free(layout);
10565 llapi_layout_free(layout);
10567 return cid.cid_count;
10570 static inline int lfs_mirror_copy(int argc, char **argv)
10573 __u16 read_mirror_id = 0;
10574 __u16 ids[128] = { 0 };
10576 struct llapi_layout *layout = NULL;
10577 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10584 struct ll_ioc_lease *ioc = NULL;
10585 struct ll_ioc_lease_id *resync_ioc;
10587 struct option long_opts[] = {
10588 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10589 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10590 { .name = NULL } };
10592 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10597 unsigned long int id;
10600 id = strtoul(optarg, &end, 0);
10601 if (errno != 0 || *end != '\0' || id == 0 ||
10604 "%s %s: invalid read mirror ID '%s'\n",
10605 progname, argv[0], optarg);
10609 read_mirror_id = (__u16)id;
10613 if (!strcmp(optarg, "-1")) {
10614 /* specify all other mirrors */
10615 ids[0] = (__u16)-1;
10618 count = parse_mirror_ids((__u16 *)ids,
10626 fprintf(stderr, "%s: option '%s' unrecognized\n",
10627 progname, argv[optind - 1]);
10632 if (argc == optind) {
10633 fprintf(stderr, "%s %s: no mirrored file provided\n",
10634 progname, argv[0]);
10636 } else if (argc > optind + 1) {
10637 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10641 if (read_mirror_id == 0) {
10643 "%s %s: no valid read mirror ID %d is provided\n",
10644 progname, argv[0], read_mirror_id);
10650 "%s %s: no write mirror ID is provided\n",
10651 progname, argv[0]);
10655 for (i = 0; i < count; i++) {
10656 if (read_mirror_id == ids[i]) {
10658 "%s %s: read and write mirror ID cannot be the same\n",
10659 progname, argv[0]);
10664 /* open mirror file */
10665 fname = argv[optind];
10667 fd = open(fname, O_DIRECT | O_RDWR);
10669 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10670 progname, argv[0], fname, strerror(errno));
10674 /* write to all other mirrors */
10675 if (ids[0] == (__u16)-1) {
10676 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10680 "%s %s: failed to get other mirror ids in '%s': %d\n",
10681 progname, argv[0], fname, rc);
10686 /* verify mirror id */
10687 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10690 "%s %s: cannot find mirror with ID %u in '%s'\n",
10691 progname, argv[0], read_mirror_id, fname);
10695 for (i = 0; i < count; i++) {
10696 rc = verify_mirror_id_by_fd(fd, ids[i]);
10699 "%s %s: cannot find mirror with ID %u in '%s'\n",
10700 progname, argv[0], ids[i], fname);
10705 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10708 "%s %s: cannot alloc comp id array for ioc: %s\n",
10709 progname, argv[0], strerror(errno));
10714 /* get stale component info */
10715 layout = llapi_layout_get_by_fd(fd, 0);
10717 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10718 progname, argv[0], fname, strerror(errno));
10722 comp_size = llapi_mirror_find_stale(layout, comp_array,
10723 ARRAY_SIZE(comp_array),
10725 llapi_layout_free(layout);
10726 if (comp_size < 0) {
10731 /* prepare target mirror components instantiation */
10732 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10733 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10734 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10736 resync_ioc->lil_mirror_id = ids[0];
10738 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10739 rc = llapi_lease_set(fd, ioc);
10742 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10743 progname, argv[0], fname, strerror(errno));
10747 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10750 fprintf(stderr, "%s %s: copy error: %d\n",
10751 progname, argv[0], rc);
10755 fprintf(stdout, "mirror copied successfully: ");
10756 for (i = 0; i < copied; i++)
10757 fprintf(stdout, "%d ", ids[i]);
10758 fprintf(stdout, "\n");
10760 ioc->lil_mode = LL_LEASE_UNLCK;
10761 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10762 ioc->lil_count = 0;
10763 for (i = 0; i < comp_size; i++) {
10766 for (j = 0; j < copied; j++) {
10767 if (comp_array[i].lrc_mirror_id != ids[j])
10770 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10774 rc = llapi_lease_set(fd, ioc);
10779 "%s %s: release lease lock of '%s' failed: %s\n",
10780 progname, argv[0], fname, strerror(errno));
10795 * struct verify_chunk - Mirror chunk to be verified.
10796 * @chunk: [start, end) of the chunk.
10797 * @mirror_count: Number of mirror ids in @mirror_id array.
10798 * @mirror_id: Array of valid mirror ids that cover the chunk.
10800 struct verify_chunk {
10801 struct lu_extent chunk;
10802 unsigned int mirror_count;
10803 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10807 * print_chunks() - Print chunk information.
10808 * @fname: Mirrored file name.
10809 * @chunks: Array of chunks.
10810 * @chunk_count: Number of chunks in @chunks array.
10812 * This function prints [start, end) of each chunk in @chunks
10813 * for mirrored file @fname, and also prints the valid mirror ids
10814 * that cover the chunk.
10819 void print_chunks(const char *fname, struct verify_chunk *chunks,
10825 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10826 for (i = 0; i < chunk_count; i++) {
10827 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10829 if (chunks[i].mirror_count == 0)
10830 fprintf(stdout, "\t[");
10832 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10833 for (j = 1; j < chunks[i].mirror_count; j++)
10834 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10836 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10838 fprintf(stdout, "\n");
10842 * print_checksums() - Print CRC-32 checksum values.
10843 * @chunk: A chunk and its corresponding valid mirror ids.
10844 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10846 * This function prints CRC-32 checksum values on @chunk for
10847 * each valid mirror that covers it.
10852 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10857 "CRC-32 checksum value for chunk "DEXT":\n",
10858 PEXT(&chunk->chunk));
10859 for (i = 0; i < chunk->mirror_count; i++)
10860 fprintf(stdout, "Mirror %u:\t%#lx\n",
10861 chunk->mirror_id[i], crc[i]);
10862 fprintf(stdout, "\n");
10866 * filter_mirror_id() - Filter specified mirror ids.
10867 * @chunks: Array of chunks.
10868 * @chunk_count: Number of chunks in @chunks array.
10869 * @mirror_ids: Specified mirror ids to be verified.
10870 * @ids_nr: Number of specified mirror ids.
10872 * This function scans valid mirror ids that cover each chunk in @chunks
10873 * and filters specified mirror ids.
10878 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10879 __u16 *mirror_ids, int ids_nr)
10884 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10885 unsigned int valid_count = 0;
10887 for (i = 0; i < chunk_count; i++) {
10888 if (chunks[i].mirror_count == 0)
10892 for (j = 0; j < ids_nr; j++) {
10893 for (k = 0; k < chunks[i].mirror_count; k++) {
10894 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10895 valid_id[valid_count] = mirror_ids[j];
10902 memcpy(chunks[i].mirror_id, valid_id,
10903 sizeof(__u16) * valid_count);
10904 chunks[i].mirror_count = valid_count;
10909 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10910 * @layout: Mirror component list.
10911 * @chunks: Array of chunks.
10912 * @chunks_size: Array size of @chunks.
10914 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10915 * to find out chunk segments and store them in @chunks array.
10917 * The @mirror_id array in each element of @chunks will store the valid
10918 * mirror ids that cover the chunk. If a mirror component covering the
10919 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10920 * will not be stored into the @mirror_id array, and the chunk for that
10921 * mirror will not be verified.
10923 * The @mirror_count in each element of @chunks will store the number of
10924 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10925 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10926 * indicates the chunk is valid in only one mirror. In both cases, the
10927 * chunk will not be verified.
10929 * Here is an example:
10931 * 0 1M 2M 3M 4M EOF
10932 * +------+-------------+--------------------+
10933 * | | | S | mirror1
10934 * +------+------+------+------+-------------+
10935 * | | S | S | | mirror2
10936 * +-------------+------+------+-------------+
10938 * prepared @chunks array will contain 5 elements:
10939 * (([0, 1M), [1, 2], 2),
10940 * ([1M, 2M), [1, 2], 2),
10941 * ([2M, 3M), [1], 1),
10942 * ([3M, 4M], [], 0),
10943 * ([4M, EOF), [2], 1))
10945 * Return: the actual array size of @chunks on success
10946 * or a negative error code on failure.
10949 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10950 struct verify_chunk *chunks,
10951 size_t chunks_size)
10955 uint32_t mirror_id;
10961 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10964 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10967 "%s: move to the first layout component: %s.\n",
10968 progname, strerror(errno));
10974 chunks[idx].chunk.e_end = LUSTRE_EOF;
10976 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10979 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10980 progname, strerror(errno));
10984 if (start > chunks[idx].chunk.e_start ||
10985 end <= chunks[idx].chunk.e_start)
10988 if (end < chunks[idx].chunk.e_end)
10989 chunks[idx].chunk.e_end = end;
10991 rc = llapi_layout_comp_flags_get(layout, &flags);
10994 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10995 progname, strerror(errno));
10999 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11002 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11005 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11006 progname, strerror(errno));
11010 chunks[idx].mirror_id[i] = mirror_id;
11012 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11014 "%s: mirror_id array is too small.\n",
11021 rc = llapi_layout_comp_use(layout,
11022 LLAPI_LAYOUT_COMP_USE_NEXT);
11025 "%s: move to the next layout component: %s.\n",
11026 progname, strerror(errno));
11029 } /* loop through all components */
11031 chunks[idx].mirror_count = i;
11033 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11037 if (idx >= chunks_size) {
11038 fprintf(stderr, "%s: chunks array is too small.\n",
11044 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11048 return rc < 0 ? rc : idx + 1;
11052 * lfs_mirror_verify_chunk() - Verify a chunk.
11053 * @fd: File descriptor of the mirrored file.
11054 * @file_size: Size of the mirrored file.
11055 * @chunk: A chunk and its corresponding valid mirror ids.
11056 * @verbose: Verbose mode.
11058 * This function verifies a @chunk contains exactly the same data
11059 * ammong the mirrors that cover it.
11061 * If @verbose is specified, then the function will print where the
11062 * differences are if the data do not match. Otherwise, it will
11063 * just return an error in that case.
11065 * Return: 0 on success or a negative error code on failure.
11068 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11069 struct verify_chunk *chunk, int verbose)
11071 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11073 size_t page_size = sysconf(_SC_PAGESIZE);
11074 ssize_t bytes_read;
11075 ssize_t bytes_done;
11079 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11083 if (file_size == 0)
11086 rc = posix_memalign(&buf, page_size, buflen);
11087 if (rc) /* error code is returned directly */
11091 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11092 PEXT(&chunk->chunk));
11093 for (i = 0; i < chunk->mirror_count; i++)
11094 fprintf(stdout, " %u", chunk->mirror_id[i]);
11095 fprintf(stdout, "\n");
11099 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11100 pos = chunk->chunk.e_start;
11101 while (bytes_done < count) {
11102 /* compute initial CRC-32 checksum */
11103 crc = crc32(0L, Z_NULL, 0);
11104 memset(crc_array, 0, sizeof(crc_array));
11107 for (i = 0; i < chunk->mirror_count; i++) {
11108 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11110 if (bytes_read < 0) {
11113 "%s: failed to read data from mirror %u: %s.\n",
11114 progname, chunk->mirror_id[i],
11119 /* compute new CRC-32 checksum */
11120 crc_array[i] = crc32(crc, buf, bytes_read);
11124 print_checksums(chunk, crc_array);
11126 /* compare CRC-32 checksum values */
11127 for (i = 1; i < chunk->mirror_count; i++) {
11128 if (crc_array[i] != crc_array[0]) {
11132 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11133 progname, PEXT(&chunk->chunk),
11134 chunk->mirror_id[0],
11135 chunk->mirror_id[i]);
11140 bytes_done += bytes_read;
11143 if (verbose > 1 && rc == 0) {
11144 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11145 PEXT(&chunk->chunk));
11146 for (i = 0; i < chunk->mirror_count; i++)
11147 fprintf(stdout, " %u", chunk->mirror_id[i]);
11148 fprintf(stdout, " PASS\n\n");
11157 * lfs_mirror_verify_file() - Verify a mirrored file.
11158 * @fname: Mirrored file name.
11159 * @mirror_ids: Specified mirror ids to be verified.
11160 * @ids_nr: Number of specified mirror ids.
11161 * @verbose: Verbose mode.
11163 * This function verifies that each SYNC mirror of a mirrored file
11164 * specified by @fname contains exactly the same data.
11166 * If @mirror_ids is specified, then the function will verify the
11167 * mirrors specified by @mirror_ids contain exactly the same data.
11169 * If @verbose is specified, then the function will print where the
11170 * differences are if the data do not match. Otherwise, it will
11171 * just return an error in that case.
11173 * Return: 0 on success or a negative error code on failure.
11176 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11179 struct verify_chunk chunks_array[1024] = { };
11180 struct llapi_layout *layout = NULL;
11182 uint32_t flr_state;
11184 int chunk_count = 0;
11190 if (stat(fname, &stbuf) < 0) {
11191 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11192 progname, fname, strerror(errno));
11197 if (!S_ISREG(stbuf.st_mode)) {
11198 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11204 if (stbuf.st_size == 0) {
11206 fprintf(stdout, "%s: '%s' file size is 0.\n",
11212 fd = open(fname, O_DIRECT | O_RDONLY);
11214 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11215 progname, fname, strerror(errno));
11220 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11222 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11223 progname, fname, strerror(errno));
11227 layout = llapi_layout_get_by_fd(fd, 0);
11229 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11230 progname, fname, strerror(errno));
11232 llapi_lease_release(fd);
11236 rc = llapi_layout_flags_get(layout, &flr_state);
11238 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11239 progname, fname, strerror(errno));
11244 flr_state &= LCM_FL_FLR_MASK;
11245 switch (flr_state) {
11248 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11249 progname, fname, llapi_layout_flags_string(flr_state));
11255 /* find out mirror chunks to be verified */
11256 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11257 ARRAY_SIZE(chunks_array));
11258 if (chunk_count < 0) {
11264 /* filter specified mirror ids */
11265 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11268 print_chunks(fname, chunks_array, chunk_count);
11270 for (idx = 0; idx < chunk_count; idx++) {
11271 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11274 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11276 PEXT(&chunks_array[idx].chunk),
11277 (unsigned long long)stbuf.st_size);
11281 if (chunks_array[idx].mirror_count == 0) {
11283 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11285 PEXT(&chunks_array[idx].chunk));
11287 fprintf(stderr, "skipped\n");
11291 fprintf(stderr, "failed\n");
11295 if (chunks_array[idx].mirror_count == 1) {
11298 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11300 PEXT(&chunks_array[idx].chunk),
11301 chunks_array[idx].mirror_id[0]);
11305 rc = llapi_lease_check(fd);
11306 if (rc != LL_LEASE_RDLCK) {
11307 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11312 /* verify one chunk */
11313 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11314 &chunks_array[idx], verbose);
11328 llapi_layout_free(layout);
11329 llapi_lease_release(fd);
11337 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11338 * @argc: The count of lfs mirror verify command line arguments.
11339 * @argv: Array of strings for lfs mirror verify command line arguments.
11341 * This function parses lfs mirror verify command and verifies the
11342 * specified mirrored file(s).
11344 * Return: 0 on success or a negative error code on failure.
11346 static inline int lfs_mirror_verify(int argc, char **argv)
11348 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11354 char cmd[PATH_MAX];
11356 struct option long_opts[] = {
11357 { .val = 'o', .name = "only", .has_arg = required_argument },
11358 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11359 { .name = NULL } };
11361 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11363 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11366 rc = parse_mirror_ids(mirror_ids,
11367 ARRAY_SIZE(mirror_ids),
11371 "%s: bad mirror ids '%s'.\n",
11378 "%s: at least 2 mirror ids needed with '--only' option.\n",
11388 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11389 progname, argv[optind - 1]);
11395 if (argc == optind) {
11396 fprintf(stderr, "%s: no file name given.\n", progname);
11401 if (ids_nr > 0 && argc > optind + 1) {
11403 "%s: '--only' cannot be used upon multiple files.\n",
11410 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11416 for (; optind < argc; optind++) {
11417 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11427 * lfs_mirror() - Parse and execute lfs mirror commands.
11428 * @argc: The count of lfs mirror command line arguments.
11429 * @argv: Array of strings for lfs mirror command line arguments.
11431 * This function parses lfs mirror commands and performs the
11432 * corresponding functions specified in mirror_cmdlist[].
11434 * Return: 0 on success or an error code on failure.
11436 static int lfs_mirror(int argc, char **argv)
11438 char cmd[PATH_MAX];
11441 setlinebuf(stdout);
11443 Parser_init("lfs-mirror > ", mirror_cmdlist);
11445 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11447 program_invocation_short_name = cmd;
11449 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11451 rc = Parser_commands();
11453 return rc < 0 ? -rc : rc;
11456 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11458 #if __BYTE_ORDER == __BIG_ENDIAN
11459 __swab16s(&attrs->lsa_valid);
11460 __swab64s(&attrs->lsa_size);
11461 __swab64s(&attrs->lsa_blocks);
11465 enum lfs_som_type {
11466 LFS_SOM_SIZE = 0x1,
11467 LFS_SOM_BLOCKS = 0x2,
11468 LFS_SOM_FLAGS = 0x4,
11469 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11473 static int lfs_getsom(int argc, char **argv)
11476 struct lustre_som_attrs *attrs;
11477 char buf[sizeof(*attrs) + 64];
11478 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11481 while ((c = getopt(argc, argv, "sbf")) != -1) {
11484 type = LFS_SOM_SIZE;
11487 type = LFS_SOM_BLOCKS;
11490 type = LFS_SOM_FLAGS;
11493 fprintf(stderr, "%s: invalid option '%c'\n",
11503 fprintf(stderr, "%s: %s\n",
11504 progname, argc == 0 ? "miss file target" :
11505 "input more than 2 files");
11510 attrs = (void *)buf;
11511 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11514 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11515 argv[0], strerror(errno), errno);
11519 lustre_som_swab(attrs);
11522 case LFS_SOM_ATTR_ALL:
11523 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11524 path, (unsigned long long)attrs->lsa_size,
11525 (unsigned long long)attrs->lsa_blocks,
11529 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11531 case LFS_SOM_BLOCKS:
11532 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11534 case LFS_SOM_FLAGS:
11535 printf("%x\n", attrs->lsa_valid);
11538 fprintf(stderr, "%s: unknown option\n", progname);
11546 * lfs_mirror_list_commands() - List lfs mirror commands.
11547 * @argc: The count of command line arguments.
11548 * @argv: Array of strings for command line arguments.
11550 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11552 * Return: 0 on success.
11554 static int lfs_mirror_list_commands(int argc, char **argv)
11556 char buffer[81] = "";
11558 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11564 static int lfs_pcc_attach(int argc, char **argv)
11566 struct option long_opts[] = {
11567 { .val = 'i', .name = "id", .has_arg = required_argument },
11568 { .name = NULL } };
11571 __u32 archive_id = 0;
11574 char fullpath[PATH_MAX];
11575 enum lu_pcc_type type = LU_PCC_READWRITE;
11578 while ((c = getopt_long(argc, argv, "i:",
11579 long_opts, NULL)) != -1) {
11583 archive_id = strtoul(optarg, &end, 0);
11584 if (errno != 0 || *end != '\0' ||
11585 archive_id == 0 || archive_id > UINT32_MAX) {
11587 "error: %s: bad archive ID '%s'\n",
11595 fprintf(stderr, "%s: option '%s' unrecognized\n",
11596 argv[0], argv[optind - 1]);
11601 if (archive_id == 0) {
11602 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11606 if (argc <= optind) {
11607 fprintf(stderr, "%s: must specify one or more file names\n",
11612 while (optind < argc) {
11615 path = argv[optind++];
11616 if (!realpath(path, fullpath)) {
11617 fprintf(stderr, "%s: could not find path '%s': %s\n",
11618 argv[0], path, strerror(errno));
11624 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11627 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11628 argv[0], path, archive_id, strerror(-rc2));
11636 static int lfs_pcc_attach_fid(int argc, char **argv)
11638 struct option long_opts[] = {
11639 { .val = 'i', .name = "id", .has_arg = required_argument },
11640 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11641 { .name = NULL } };
11642 char short_opts[] = "i:m:";
11645 __u32 archive_id = 0;
11647 const char *mntpath = NULL;
11648 const char *fidstr;
11649 enum lu_pcc_type type = LU_PCC_READWRITE;
11652 while ((c = getopt_long(argc, argv, short_opts,
11653 long_opts, NULL)) != -1) {
11657 archive_id = strtoul(optarg, &end, 0);
11658 if (errno != 0 || *end != '\0' ||
11659 archive_id > UINT32_MAX) {
11661 "error: %s: bad archive ID '%s'\n",
11672 fprintf(stderr, "%s: option '%s' unrecognized\n",
11673 argv[0], argv[optind - 1]);
11678 if (archive_id == 0) {
11679 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11684 fprintf(stderr, "%s: must specify Lustre mount point\n",
11689 if (argc <= optind) {
11690 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11694 while (optind < argc) {
11697 fidstr = argv[optind++];
11699 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11703 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11704 argv[0], fidstr, mntpath, archive_id,
11707 if (rc == 0 && rc2 < 0)
11713 static int lfs_pcc_detach(int argc, char **argv)
11715 struct option long_opts[] = {
11716 { .val = 'k', .name = "keep", .has_arg = no_argument },
11717 { .name = NULL } };
11718 char short_opts[] = "k";
11722 char fullpath[PATH_MAX];
11723 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11726 while ((c = getopt_long(argc, argv, short_opts,
11727 long_opts, NULL)) != -1) {
11730 detach_opt = PCC_DETACH_OPT_NONE;
11735 fprintf(stderr, "%s: option '%s' unrecognized\n",
11736 argv[0], argv[optind - 1]);
11741 while (optind < argc) {
11744 path = argv[optind++];
11745 if (!realpath(path, fullpath)) {
11746 fprintf(stderr, "%s: could not find path '%s': %s\n",
11747 argv[0], path, strerror(errno));
11753 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11757 "%s: cannot detach '%s' from PCC: %s\n",
11758 argv[0], path, strerror(errno));
11766 static int lfs_pcc_detach_fid(int argc, char **argv)
11768 struct option long_opts[] = {
11769 { .val = 'k', .name = "keep", .has_arg = no_argument },
11770 { .name = NULL } };
11771 char short_opts[] = "k";
11775 const char *mntpath;
11776 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11779 while ((c = getopt_long(argc, argv, short_opts,
11780 long_opts, NULL)) != -1) {
11783 detach_opt = PCC_DETACH_OPT_NONE;
11788 fprintf(stderr, "%s: option '%s' unrecognized\n",
11789 argv[0], argv[optind - 1]);
11794 mntpath = argv[optind++];
11796 while (optind < argc) {
11799 fid = argv[optind++];
11801 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11804 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11805 argv[0], fid, mntpath, strerror(-rc2));
11813 static int lfs_pcc_state(int argc, char **argv)
11817 char fullpath[PATH_MAX];
11818 struct lu_pcc_state state;
11823 fprintf(stderr, "%s: must specify one or more file names\n",
11828 while (optind < argc) {
11831 path = argv[optind++];
11832 if (!realpath(path, fullpath)) {
11833 fprintf(stderr, "%s: could not find path '%s': %s\n",
11834 argv[0], path, strerror(errno));
11840 rc2 = llapi_pcc_state_get(fullpath, &state);
11845 "%s: cannot get PCC state of '%s': %s\n",
11846 argv[0], path, strerror(-rc2));
11850 printf("file: %s", path);
11851 printf(", type: %s", pcc_type2string(state.pccs_type));
11852 if (state.pccs_type == LU_PCC_NONE &&
11853 state.pccs_open_count == 0) {
11858 printf(", PCC file: %s", state.pccs_path);
11859 printf(", user number: %u", state.pccs_open_count);
11860 printf(", flags: %x", state.pccs_flags);
11867 * lfs_pcc_list_commands() - List lfs pcc commands.
11868 * @argc: The count of command line arguments.
11869 * @argv: Array of strings for command line arguments.
11871 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11873 * Return: 0 on success.
11875 static int lfs_pcc_list_commands(int argc, char **argv)
11877 char buffer[81] = "";
11879 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11886 * lfs_pcc() - Parse and execute lfs pcc commands.
11887 * @argc: The count of lfs pcc command line arguments.
11888 * @argv: Array of strings for lfs pcc command line arguments.
11890 * This function parses lfs pcc commands and performs the
11891 * corresponding functions specified in pcc_cmdlist[].
11893 * Return: 0 on success or an error code on failure.
11895 static int lfs_pcc(int argc, char **argv)
11897 char cmd[PATH_MAX];
11900 setlinebuf(stdout);
11902 Parser_init("lfs-pcc > ", pcc_cmdlist);
11904 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11906 program_invocation_short_name = cmd;
11908 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11910 rc = Parser_commands();
11912 return rc < 0 ? -rc : rc;
11915 static int lfs_list_commands(int argc, char **argv)
11917 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11919 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11924 int main(int argc, char **argv)
11928 /* Ensure that liblustreapi constructor has run */
11929 if (!llapi_liblustreapi_initialized())
11930 fprintf(stderr, "liblustreapi was not properly initialized\n");
11932 setlinebuf(stdout);
11935 Parser_init("lfs > ", cmdlist);
11937 progname = program_invocation_short_name; /* Used in error messages */
11939 llapi_set_command_name(argv[1]);
11940 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11941 llapi_clear_command_name();
11943 rc = Parser_commands();
11946 return rc < 0 ? -rc : rc;
11949 #ifdef _LUSTRE_IDL_H_
11950 /* Everything we need here should be included by lustreapi.h. */
11951 # error "lfs should not depend on lustre_idl.h"
11952 #endif /* _LUSTRE_IDL_H_ */