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';
2407 start_index = strtol(arg, &endptr, 0);
2408 if (endptr == arg) /* no data at all */
2410 if (errno != 0 || start_index < -1 ||
2411 (*endptr != '-' && *endptr != '\0'))
2414 end_index = start_index;
2415 if (*endptr == '-') {
2417 end_index = strtol(endptr + 1, &endptr, 0);
2418 if (errno != 0 || *endptr != '\0' || end_index < -1)
2420 if (end_index < start_index)
2424 for (i = start_index; i <= end_index && slots > 0; i++) {
2427 /* remove duplicate */
2428 for (j = 0; j < offset; j++) {
2429 if (tgts[j] == i && pattern &&
2430 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2432 else if (tgts[j] == i)
2438 if (j == offset) { /* check complete */
2444 if (slots == 0 && i < end_index)
2452 if (!end_of_loop && ptr)
2455 if (!overstriped && pattern)
2456 *pattern = LLAPI_LAYOUT_DEFAULT;
2458 return rc < 0 ? rc : nr;
2461 struct lfs_setstripe_args {
2462 unsigned long long lsa_comp_end;
2463 unsigned long long lsa_stripe_size;
2464 unsigned long long lsa_extension_size;
2465 long long lsa_stripe_count;
2466 long long lsa_stripe_off;
2467 __u32 lsa_comp_flags;
2468 __u32 lsa_comp_neg_flags;
2469 unsigned long long lsa_pattern;
2470 unsigned int lsa_mirror_count;
2472 bool lsa_first_comp;
2473 bool lsa_extension_comp;
2475 char *lsa_pool_name;
2478 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2480 unsigned int mirror_count = lsa->lsa_mirror_count;
2481 bool first_comp = lsa->lsa_first_comp;
2483 memset(lsa, 0, sizeof(*lsa));
2485 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2486 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2487 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2488 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2489 lsa->lsa_pool_name = NULL;
2491 lsa->lsa_mirror_count = mirror_count;
2492 lsa->lsa_first_comp = first_comp;
2496 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2497 * @lsa: Stripe options to be initialized and inherited.
2499 * This function initializes stripe options in @lsa and inherit
2500 * stripe_size, stripe_count and OST pool_name options.
2504 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2506 unsigned long long stripe_size;
2507 long long stripe_count;
2508 char *pool_name = NULL;
2510 stripe_size = lsa->lsa_stripe_size;
2511 stripe_count = lsa->lsa_stripe_count;
2512 pool_name = lsa->lsa_pool_name;
2514 setstripe_args_init(lsa);
2516 lsa->lsa_stripe_size = stripe_size;
2517 lsa->lsa_stripe_count = stripe_count;
2518 lsa->lsa_pool_name = pool_name;
2521 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2523 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2524 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2525 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2526 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2527 lsa->lsa_comp_end != 0);
2531 * comp_args_to_layout() - Create or extend a composite layout.
2532 * @composite: Pointer to the composite layout.
2533 * @lsa: Stripe options for the new component.
2535 * This function creates or extends a composite layout by adding a new
2536 * component with stripe options from @lsa.
2538 * Return: 0 on success or an error code on failure.
2540 static int comp_args_to_layout(struct llapi_layout **composite,
2541 struct lfs_setstripe_args *lsa,
2544 struct llapi_layout *layout = *composite;
2545 uint64_t prev_end = 0;
2551 layout = llapi_layout_alloc();
2553 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2558 *composite = layout;
2559 lsa->lsa_first_comp = true;
2564 * Get current component extent, current component
2565 * must be the tail component.
2567 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2569 fprintf(stderr, "Get comp extent failed. %s\n",
2574 if (lsa->lsa_first_comp) {
2576 rc = llapi_layout_add_first_comp(layout);
2578 rc = llapi_layout_comp_add(layout);
2581 fprintf(stderr, "Add component failed. %s\n",
2587 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2589 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2590 lsa->lsa_comp_flags, strerror(errno));
2595 uint64_t comp_end = lsa->lsa_comp_end;
2598 * The extendable component is 0-length, so it can be removed
2599 * if there is insufficient space to extend it.
2601 if (lsa->lsa_extension_comp)
2602 comp_end = prev_end;
2604 rc = llapi_layout_comp_extent_set(layout, prev_end,
2607 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2608 prev_end, comp_end, strerror(errno));
2612 /* reset lsa_first_comp */
2613 lsa->lsa_first_comp = false;
2615 /* Data-on-MDT component setting */
2616 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2618 * In case of Data-on-MDT patterns the only extra option
2619 * applicable is stripe size option.
2621 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2623 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2624 lsa->lsa_stripe_count);
2628 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2630 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2631 lsa->lsa_stripe_size);
2635 if (lsa->lsa_nr_tgts != 0) {
2637 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2642 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2644 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2645 lsa->lsa_stripe_off);
2649 if (lsa->lsa_pool_name != 0) {
2651 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2652 lsa->lsa_pool_name);
2657 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2659 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2664 /* Data-on-MDT component has always single stripe up to end */
2665 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2666 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2667 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2669 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2676 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2677 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2679 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2680 rc = llapi_layout_extension_size_set(layout, size);
2682 rc = llapi_layout_stripe_size_set(layout, size);
2685 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2686 size, strerror(errno));
2690 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2692 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2693 lsa->lsa_stripe_count, strerror(errno));
2697 if (lsa->lsa_pool_name) {
2698 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2700 fprintf(stderr, "Set pool name: %s failed. %s\n",
2701 lsa->lsa_pool_name, strerror(errno));
2705 rc = llapi_layout_pool_name_set(layout, "");
2707 fprintf(stderr, "Clear pool name failed: %s\n",
2713 if (lsa->lsa_nr_tgts > 0) {
2714 if (lsa->lsa_stripe_count > 0 &&
2715 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2716 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2717 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2718 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2719 lsa->lsa_stripe_count,
2724 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2725 rc = llapi_layout_ost_index_set(layout, i,
2730 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2731 lsa->lsa_stripe_off != -1) {
2732 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2735 fprintf(stderr, "Set ost index %d failed. %s\n",
2736 i, strerror(errno));
2740 /* Create the second, virtual component of extension space */
2741 if (lsa->lsa_extension_comp) {
2742 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2743 lsa->lsa_extension_comp = false;
2750 static int build_component(struct llapi_layout **layout,
2751 struct lfs_setstripe_args *lsa, bool set_extent)
2755 rc = comp_args_to_layout(layout, lsa, set_extent);
2759 if (lsa->lsa_mirror_count > 0) {
2760 rc = llapi_layout_mirror_count_set(*layout,
2761 lsa->lsa_mirror_count);
2765 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2768 lsa->lsa_mirror_count = 0;
2774 static int build_prev_component(struct llapi_layout **layout,
2775 struct lfs_setstripe_args *prev,
2776 struct lfs_setstripe_args *lsa,
2779 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2782 if (prev->lsa_stripe_size) {
2784 prev->lsa_comp_end = lsa->lsa_comp_end;
2785 prev->lsa_extension_size = lsa->lsa_extension_size;
2786 prev->lsa_extension_comp = true;
2789 rc = build_component(layout, prev, true);
2795 * Copy lsa to previous lsa;
2796 * if this is an extension component, make the previous invalid;
2799 prev->lsa_stripe_size = 0;
2806 static int build_layout_from_yaml_node(struct cYAML *node,
2807 struct llapi_layout **layout,
2808 struct lfs_setstripe_args *lsa,
2809 struct lfs_setstripe_args *prevp)
2811 struct lfs_setstripe_args prev = { 0 };
2812 __u32 *osts = lsa->lsa_tgts;
2820 string = node->cy_string;
2822 if (node->cy_type == CYAML_TYPE_OBJECT) {
2823 /* go deep to sub blocks */
2824 if (string && !strncmp(string, "component", 9) &&
2825 strncmp(string, "component0", 10) &&
2826 strncmp(string, "components", 10)) {
2827 rc = build_prev_component(layout, prevp, lsa,
2832 /* initialize lsa. */
2833 setstripe_args_init(lsa);
2834 lsa->lsa_first_comp = false;
2835 lsa->lsa_tgts = osts;
2838 rc = build_layout_from_yaml_node(node->cy_child, layout,
2843 if (!node->cy_string)
2846 /* skip leading lmm_ if present, to simplify parsing */
2847 if (strncmp(string, "lmm_", 4) == 0)
2850 if (node->cy_type == CYAML_TYPE_STRING) {
2851 if (!strcmp(string, "lcme_extent.e_end")) {
2852 if (!strcmp(node->cy_valuestring, "EOF") ||
2853 !strcmp(node->cy_valuestring, "eof"))
2854 lsa->lsa_comp_end = LUSTRE_EOF;
2855 } else if (!strcmp(string, "pool")) {
2856 lsa->lsa_pool_name = node->cy_valuestring;
2857 } else if (!strcmp(string, "pattern")) {
2858 if (!strcmp(node->cy_valuestring, "mdt"))
2859 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2860 if (!strcmp(node->cy_valuestring,
2861 "raid0,overstriped"))
2863 LLAPI_LAYOUT_OVERSTRIPING;
2864 } else if (!strcmp(string, "lcme_flags")) {
2865 rc = comp_str2flags(node->cy_valuestring,
2866 &lsa->lsa_comp_flags,
2867 &lsa->lsa_comp_neg_flags);
2871 * Only template flags have meaning in
2872 * the layout for a new file
2874 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2876 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2877 if (!strcmp(string, "lcm_mirror_count")) {
2878 lsa->lsa_mirror_count = node->cy_valueint;
2879 } else if (!strcmp(string, "lcme_extent.e_start")) {
2880 if (node->cy_valueint == 0)
2881 lsa->lsa_first_comp = true;
2882 } else if (!strcmp(string, "lcme_extent.e_end")) {
2883 if (node->cy_valueint == -1)
2884 lsa->lsa_comp_end = LUSTRE_EOF;
2886 lsa->lsa_comp_end = node->cy_valueint;
2887 } else if (!strcmp(string, "stripe_count")) {
2888 lsa->lsa_stripe_count = node->cy_valueint;
2889 } else if (!strcmp(string, "stripe_size")) {
2890 lsa->lsa_stripe_size = node->cy_valueint;
2891 } else if (!strcmp(string, "extension_size")) {
2892 lsa->lsa_extension_size = node->cy_valueint;
2893 lsa->lsa_extension_comp = true;
2894 } else if (!strcmp(string, "stripe_offset")) {
2895 lsa->lsa_stripe_off = node->cy_valueint;
2896 } else if (!strcmp(string, "l_ost_idx")) {
2897 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2902 node = node->cy_next;
2905 if (prevp == &prev) {
2906 rc = build_prev_component(layout, prevp, lsa, true);
2910 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2911 rc = build_component(layout, lsa, *layout != NULL);
2917 static int lfs_comp_create_from_yaml(char *template,
2918 struct llapi_layout **layout,
2919 struct lfs_setstripe_args *lsa,
2922 struct cYAML *tree = NULL, *err_rc = NULL;
2925 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2927 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2928 progname, template);
2929 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2930 "can't parse", &err_rc);
2931 cYAML_print_tree2file(stderr, err_rc);
2932 cYAML_free_tree(err_rc);
2937 /* initialize lsa for plain file */
2938 setstripe_args_init(lsa);
2939 lsa->lsa_tgts = osts;
2941 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2943 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2944 progname, template);
2947 /* clean clean lsa */
2948 setstripe_args_init(lsa);
2952 cYAML_free_tree(tree);
2957 * Get the extension size from the next (SEL) component and extend the
2958 * current component on it. The start of the next component is to be
2961 * \param[in] layout the current layout
2962 * \param[in] start the start of the current component
2963 * \param[in,out] end the end of the current component
2964 * \param[in] offset the offset to adjust the end position to instead of
2967 * \retval 0 - extended successfully
2968 * \retval < 0 - error
2970 static int layout_extend_comp(struct llapi_layout *layout,
2971 uint64_t start, uint64_t *end,
2974 uint64_t size, next_start, next_end;
2977 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2980 "%s setstripe: cannot move component cursor: %s\n",
2981 progname, strerror(errno));
2986 * Even if the @size will not be used below, this will fail if
2987 * this is not a SEL component - a good confirmation we are
2988 * working on right components.
2990 rc = llapi_layout_extension_size_get(layout, &size);
2993 "%s setstripe: cannot get component ext size: %s\n",
2994 progname, strerror(errno));
2998 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3000 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3001 progname, strerror(errno));
3005 next_start += offset ?: size;
3006 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3008 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3009 progname, strerror(errno));
3013 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3016 "%s setstripe: cannot move component cursor: %s\n",
3017 progname, strerror(errno));
3021 *end += offset ?: size;
3022 rc = llapi_layout_comp_extent_set(layout, start, *end);
3024 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3025 progname, strerror(errno));
3033 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3034 * end of the last component in the existing file, and adjust the
3035 * first extent start of the components to be added accordingly.
3037 * In the create mode, we need to check if the first component is an extendable
3038 * SEL component and extend its length to the extension size (first component
3039 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3041 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3044 struct llapi_layout *head;
3045 uint64_t start = 0, prev_end = 0;
3049 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3054 head = llapi_layout_get_by_path(fname, 0);
3057 "%s setstripe: cannot read layout from '%s': %s\n",
3058 progname, fname, strerror(errno));
3060 } else if (errno == ENODATA) {
3062 * file without LOVEA, this component-add will be turned
3063 * into a component-create.
3065 llapi_layout_free(head);
3069 * the new layout will be added to an empty one, it
3070 * still needs to be adjusted below
3074 } else if (!llapi_layout_is_composite(head)) {
3076 "%s setstripe: '%s' not a composite file\n",
3078 llapi_layout_free(head);
3082 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3085 "%s setstripe: cannot get prev extent: %s\n",
3086 progname, strerror(errno));
3087 llapi_layout_free(head);
3091 llapi_layout_free(head);
3095 /* Make sure we use the first component of the layout to be added. */
3096 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3099 "%s setstripe: cannot move component cursor: %s\n",
3100 progname, strerror(errno));
3104 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3106 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3107 progname, strerror(errno));
3111 if (start == 0 && end == 0) {
3112 rc = layout_extend_comp(layout, start, &end,
3113 comp_add ? prev_end : 0);
3118 if (start > prev_end || end < prev_end) {
3120 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3121 progname, start, end, prev_end);
3125 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3128 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3129 progname, prev_end, end, strerror(errno));
3136 static int mirror_adjust_first_extents(struct mirror_args *list)
3144 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3147 list = list->m_next;
3153 static inline bool arg_is_eof(char *arg)
3155 return !strncmp(arg, "-1", strlen("-1")) ||
3156 !strncmp(arg, "EOF", strlen("EOF")) ||
3157 !strncmp(arg, "eof", strlen("eof"));
3161 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3163 * Return: Valid mirror_args pointer on success and
3164 * NULL if memory allocation fails.
3166 static struct mirror_args *lfs_mirror_alloc(void)
3168 struct mirror_args *mirror = NULL;
3171 mirror = calloc(1, sizeof(*mirror));
3173 mirror->m_inherit = false;
3184 * lfs_mirror_free() - Free memory allocated for a mirror argument
3186 * @mirror: Previously allocated mirror argument structure by
3187 * lfs_mirror_alloc().
3189 * Free memory allocated for @mirror.
3193 static void lfs_mirror_free(struct mirror_args *mirror)
3195 if (mirror->m_layout)
3196 llapi_layout_free(mirror->m_layout);
3201 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3202 * @mirror_list: Previously allocated mirror list.
3204 * Free memory allocated for @mirror_list.
3208 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3210 struct mirror_args *next_mirror = NULL;
3212 while (mirror_list) {
3213 next_mirror = mirror_list->m_next;
3214 lfs_mirror_free(mirror_list);
3215 mirror_list = next_mirror;
3227 LFS_COMP_NO_VERIFY_OPT,
3229 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3231 LFS_MIRROR_STATE_OPT,
3233 LFS_MIRROR_INDEX_OPT,
3234 LFS_LAYOUT_FOREIGN_OPT,
3240 static int lfs_setstripe_internal(int argc, char **argv,
3241 enum setstripe_origin opc)
3243 struct lfs_setstripe_args lsa = { 0 };
3244 struct llapi_stripe_param *param = NULL;
3245 struct find_param migrate_mdt_param = {
3255 unsigned long long size_units = 1;
3256 bool migrate_mode = false;
3257 bool migrate_mdt_mode = false;
3258 bool setstripe_mode = false;
3259 bool migration_block = false;
3260 __u64 migration_flags = 0;
3261 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3262 int comp_del = 0, comp_set = 0;
3265 struct llapi_layout *layout = NULL;
3266 struct llapi_layout **lpp = &layout;
3267 bool mirror_mode = false;
3268 bool has_m_file = false;
3269 __u32 mirror_count = 0;
3270 enum mirror_flags mirror_flags = 0;
3271 struct mirror_args *mirror_list = NULL;
3272 struct mirror_args *new_mirror = NULL;
3273 struct mirror_args *last_mirror = NULL;
3274 __u16 mirror_id = 0;
3276 bool from_yaml = false;
3277 bool from_copy = false;
3278 char *template = NULL;
3279 bool foreign_mode = false;
3281 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3282 char *mode_opt = NULL;
3283 mode_t previous_umask = 0;
3284 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3286 struct option long_opts[] = {
3287 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3288 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3289 /* --block is only valid in migrate mode */
3290 { .val = 'b', .name = "block", .has_arg = no_argument },
3291 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3292 { .val = LFS_COMP_ADD_OPT,
3293 .name = "comp-add", .has_arg = no_argument },
3294 { .val = LFS_COMP_ADD_OPT,
3295 .name = "component-add", .has_arg = no_argument },
3296 { .val = LFS_COMP_DEL_OPT,
3297 .name = "comp-del", .has_arg = no_argument },
3298 { .val = LFS_COMP_DEL_OPT,
3299 .name = "component-del", .has_arg = no_argument },
3300 { .val = LFS_COMP_FLAGS_OPT,
3301 .name = "comp-flags", .has_arg = required_argument },
3302 { .val = LFS_COMP_FLAGS_OPT,
3303 .name = "component-flags",
3304 .has_arg = required_argument },
3305 { .val = LFS_COMP_SET_OPT,
3306 .name = "comp-set", .has_arg = no_argument },
3307 { .val = LFS_COMP_SET_OPT,
3308 .name = "component-set",
3309 .has_arg = no_argument},
3310 { .val = LFS_COMP_NO_VERIFY_OPT,
3311 .name = "no-verify", .has_arg = no_argument},
3312 { .val = LFS_LAYOUT_FLAGS_OPT,
3313 .name = "flags", .has_arg = required_argument},
3314 { .val = LFS_LAYOUT_FOREIGN_OPT,
3315 .name = "foreign", .has_arg = optional_argument},
3316 { .val = LFS_MIRROR_ID_OPT,
3317 .name = "mirror-id", .has_arg = required_argument},
3318 { .val = LFS_MODE_OPT,
3319 .name = "mode", .has_arg = required_argument},
3320 { .val = LFS_LAYOUT_COPY,
3321 .name = "copy", .has_arg = required_argument},
3322 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3323 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3324 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3325 { .val = 'C', .name = "overstripe-count",
3326 .has_arg = required_argument},
3327 { .val = 'd', .name = "delete", .has_arg = no_argument},
3328 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3329 /* --non-direct is only valid in migrate mode */
3330 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3331 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3332 { .val = 'E', .name = "component-end",
3333 .has_arg = required_argument},
3334 { .val = 'f', .name = "file", .has_arg = required_argument },
3335 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3336 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3337 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3338 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3339 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3340 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3341 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3342 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3343 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3344 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3345 { .val = 'L', .name = "layout", .has_arg = required_argument },
3346 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3347 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3348 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3349 /* --non-block is only valid in migrate mode */
3350 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3351 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3352 { .val = 'o', .name = "ost", .has_arg = required_argument },
3353 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3354 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3355 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3357 { .val = 'p', .name = "pool", .has_arg = required_argument },
3358 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3359 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3360 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3361 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3362 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3363 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3364 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3365 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3366 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3367 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3368 /* --verbose is only valid in migrate mode */
3369 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3370 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3371 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3372 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3373 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3376 setstripe_args_init(&lsa);
3378 migrate_mode = (opc == SO_MIGRATE);
3379 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3380 setstripe_mode = (opc == SO_SETSTRIPE);
3381 if (opc == SO_MIRROR_DELETE) {
3383 mirror_flags = MF_DESTROY;
3386 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3388 while ((c = getopt_long(argc, argv,
3389 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3390 long_opts, NULL)) >= 0) {
3396 case LFS_COMP_ADD_OPT:
3399 case LFS_COMP_DEL_OPT:
3402 case LFS_COMP_FLAGS_OPT:
3403 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3404 &lsa.lsa_comp_neg_flags);
3407 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3409 "%s: inverted flags are not supported\n",
3414 case LFS_COMP_SET_OPT:
3417 case LFS_COMP_NO_VERIFY_OPT:
3418 mirror_flags |= MF_NO_VERIFY;
3420 case LFS_MIRROR_ID_OPT: {
3421 unsigned long int id;
3424 id = strtoul(optarg, &end, 0);
3425 if (errno != 0 || *end != '\0' || id == 0 ||
3428 "%s %s: invalid mirror ID '%s'\n",
3429 progname, argv[0], optarg);
3433 mirror_id = (__u16)id;
3436 case LFS_LAYOUT_FLAGS_OPT: {
3439 /* check for numeric flags (foreign and mirror cases) */
3440 if (setstripe_mode && !mirror_mode && !last_mirror) {
3442 flags = strtoul(optarg, &end, 16);
3443 if (errno != 0 || *end != '\0' ||
3444 flags >= UINT32_MAX) {
3446 "%s %s: invalid hex flags '%s'\n",
3447 progname, argv[0], optarg);
3450 if (!foreign_mode) {
3452 "%s %s: hex flags must be specified with --foreign option\n",
3459 if (!mirror_mode || !last_mirror) {
3461 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3466 result = comp_str2flags(optarg, &last_mirror->m_flags,
3473 "%s: inverted flags are not supported\n",
3478 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3480 "%s: unsupported mirror flags: %s\n",
3487 case LFS_LAYOUT_FOREIGN_OPT:
3489 /* check pure numeric */
3490 type = strtoul(optarg, &end, 0);
3493 type = check_foreign_type_name(optarg);
3494 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3496 "%s %s: unrecognized foreign type '%s'\n",
3501 } else if (type >= UINT32_MAX) {
3503 "%s %s: invalid foreign type '%s'\n",
3504 progname, argv[0], optarg);
3508 foreign_mode = true;
3513 mode = strtoul(mode_opt, &end, 8);
3516 "%s %s: bad mode '%s'\n",
3517 progname, argv[0], mode_opt);
3520 previous_umask = umask(0);
3523 case LFS_LAYOUT_COPY:
3528 if (!migrate_mode) {
3530 "%s %s: -b|--block valid only for migrate command\n",
3534 migration_block = true;
3537 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3539 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3543 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3547 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3548 if (errno != 0 || *end != '\0'||
3549 lsa.lsa_stripe_count < -1 ||
3550 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3552 "%s %s: invalid stripe count '%s'\n",
3553 progname, argv[0], optarg);
3557 if (lsa.lsa_stripe_count == -1)
3558 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3561 /* delete the default striping pattern */
3563 if (opc == SO_MIRROR_SPLIT) {
3566 "%s %s: -d cannot used with -f\n",
3570 mirror_flags |= MF_DESTROY;
3574 if (!migrate_mode) {
3576 "%s %s: -D|--non-direct is valid only for migrate command\n",
3580 migration_flags |= MIGRATION_NONDIRECT;
3583 if (lsa.lsa_comp_end != 0) {
3584 result = comp_args_to_layout(lpp, &lsa, true);
3586 fprintf(stderr, "%s: invalid layout\n",
3591 setstripe_args_init_inherit(&lsa);
3594 if (arg_is_eof(optarg)) {
3595 lsa.lsa_comp_end = LUSTRE_EOF;
3597 result = llapi_parse_size(optarg,
3600 /* assume units of KB if too small */
3601 if (lsa.lsa_comp_end < 4096)
3602 lsa.lsa_comp_end *= 1024;
3604 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3606 "%s %s: invalid component end '%s'\n",
3607 progname, argv[0], optarg);
3613 if (!migrate_mode) {
3615 "--mdt-hash is valid only for migrate command\n");
3619 lsa.lsa_pattern = check_hashtype(optarg);
3620 if (lsa.lsa_pattern == 0) {
3622 "%s %s: bad stripe hash type '%s'\n",
3623 progname, argv[0], optarg);
3629 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3630 if (errno != 0 || *end != '\0' ||
3631 lsa.lsa_stripe_off < -1 ||
3632 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3634 "%s %s: invalid stripe offset '%s'\n",
3635 progname, argv[0], optarg);
3638 if (lsa.lsa_stripe_off == -1)
3639 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3642 comp_id = strtoul(optarg, &end, 0);
3643 if (*end != '\0' || comp_id == 0 ||
3644 comp_id > LCME_ID_MAX) {
3646 "%s %s: invalid component ID '%s'\n",
3647 progname, argv[0], optarg);
3652 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3654 "error: %s: invalid option: %s\n",
3655 progname, argv[optopt + 1]);
3658 if (opc == SO_MIRROR_EXTEND) {
3661 "error: %s: '-N' must exist in front of '%s'\n",
3662 progname, argv[optopt + 1]);
3665 last_mirror->m_file = optarg;
3666 last_mirror->m_count = 1;
3670 mirror_list = lfs_mirror_alloc();
3671 mirror_list->m_file = optarg;
3676 if (strcmp(argv[optind - 1], "mdt") == 0) {
3677 /* Can be only the first component */
3681 "error: 'mdt' layout can be only the first one\n");
3684 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3687 "error: 'mdt' layout size is too big\n");
3690 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3691 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3694 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3700 if (!migrate_mode) {
3702 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3706 migrate_mdt_mode = true;
3707 lsa.lsa_nr_tgts = parse_targets(tgts,
3708 sizeof(tgts) / sizeof(__u32),
3709 lsa.lsa_nr_tgts, optarg, NULL);
3710 if (lsa.lsa_nr_tgts < 0) {
3712 "%s %s: invalid MDT target(s) '%s'\n",
3713 progname, argv[0], optarg);
3717 lsa.lsa_tgts = tgts;
3718 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3719 lsa.lsa_stripe_off = tgts[0];
3722 if (!migrate_mode) {
3724 "%s %s: -n|--non-block valid only for migrate command\n",
3728 migration_flags |= MIGRATION_NONBLOCK;
3731 if (opc == SO_SETSTRIPE) {
3732 opc = SO_MIRROR_CREATE;
3738 mirror_count = strtoul(optarg, &end, 0);
3739 if (errno != 0 || *end != '\0' ||
3740 mirror_count == 0 ||
3741 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3743 "error: %s: bad mirror count: %s\n",
3750 new_mirror = lfs_mirror_alloc();
3751 new_mirror->m_count = mirror_count;
3754 mirror_list = new_mirror;
3757 /* wrap up last mirror */
3758 if (!setstripe_args_specified(&lsa))
3759 last_mirror->m_inherit = true;
3760 if (lsa.lsa_comp_end == 0)
3761 lsa.lsa_comp_end = LUSTRE_EOF;
3763 result = comp_args_to_layout(lpp, &lsa, true);
3765 lfs_mirror_free(new_mirror);
3769 setstripe_args_init_inherit(&lsa);
3771 last_mirror->m_next = new_mirror;
3774 last_mirror = new_mirror;
3775 lpp = &last_mirror->m_layout;
3778 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3779 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3781 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3783 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3785 "%s %s: -o|--ost incompatible with DoM layout\n",
3790 * -o allows overstriping, and must note it because
3791 * parse_targets is shared with MDT striping, which
3792 * does not allow duplicates
3794 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3795 lsa.lsa_nr_tgts = parse_targets(tgts,
3796 sizeof(tgts) / sizeof(__u32),
3797 lsa.lsa_nr_tgts, optarg,
3799 if (lsa.lsa_nr_tgts < 0) {
3801 "%s %s: invalid OST target(s) '%s'\n",
3802 progname, argv[0], optarg);
3806 lsa.lsa_tgts = tgts;
3807 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3808 lsa.lsa_stripe_off = tgts[0];
3813 lsa.lsa_pool_name = optarg;
3815 if (strlen(lsa.lsa_pool_name) == 0 ||
3816 strncmp(lsa.lsa_pool_name, "none",
3817 LOV_MAXPOOLNAME) == 0)
3818 lsa.lsa_pool_name = NULL;
3821 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3823 /* assume units of KB if too small to be valid */
3824 if (lsa.lsa_stripe_size < 4096)
3825 lsa.lsa_stripe_size *= 1024;
3827 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3829 "%s %s: invalid stripe size '%s'\n",
3830 progname, argv[0], optarg);
3835 if (!migrate_mode) {
3837 "%s %s: -v|--verbose valid only for migrate command\n",
3841 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3842 migration_flags = MIGRATION_VERBOSE;
3852 result = llapi_parse_size(optarg,
3853 &lsa.lsa_extension_size,
3857 "%s %s: invalid extension size '%s'\n",
3858 progname, argv[0], optarg);
3862 lsa.lsa_extension_comp = true;
3865 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3866 progname, argv[0], argv[optind - 1]);
3871 fname = argv[optind];
3873 if (optind == argc) {
3874 fprintf(stderr, "%s %s: FILE must be specified\n",
3879 /* lfs migrate $filename should keep the file's layout by default */
3880 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3884 if (xattr && !foreign_mode) {
3886 * only print a warning as this is harmless and will be ignored
3889 "%s %s: xattr has been specified for non-foreign layout\n",
3891 } else if (foreign_mode && !xattr) {
3893 "%s %s: xattr must be provided in foreign mode\n",
3898 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3899 comp_set || comp_id || delete || from_copy ||
3900 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3903 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3908 if (mirror_mode && mirror_count == 0) {
3910 "error: %s: --mirror-count|-N option is required\n",
3917 if (!setstripe_args_specified(&lsa))
3918 last_mirror->m_inherit = true;
3919 if (lsa.lsa_comp_end == 0)
3920 lsa.lsa_comp_end = LUSTRE_EOF;
3923 if (lsa.lsa_comp_end != 0) {
3924 result = comp_args_to_layout(lpp, &lsa, true);
3926 fprintf(stderr, "error: %s: invalid layout\n",
3933 if (mirror_flags & MF_NO_VERIFY) {
3934 if (opc != SO_MIRROR_EXTEND) {
3936 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3940 } else if (!has_m_file) {
3942 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3949 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
3951 "%s %s: --component-set doesn't have component-id set\n",
3956 if ((delete + comp_set + comp_del + comp_add) > 1) {
3958 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3963 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3964 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3966 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3971 if ((comp_set || comp_del) &&
3972 (setstripe_args_specified(&lsa) || layout != NULL)) {
3974 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3979 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3981 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3986 if (comp_add || comp_del) {
3989 result = lstat(fname, &st);
3990 if (result == 0 && S_ISDIR(st.st_mode)) {
3992 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3999 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4008 "%s %s: option -E must be specified with --component-add\n",
4014 if (from_yaml && from_copy) {
4016 "%s: can't specify --yaml and --copy together\n",
4021 if ((from_yaml || from_copy) &&
4022 (setstripe_args_specified(&lsa) || layout != NULL)) {
4024 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4029 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4031 "%s %s: options --non-block and --block are mutually exclusive\n",
4036 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4037 opc != SO_MIRROR_DELETE && comp_id != 0) {
4039 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4044 if (migrate_mdt_mode) {
4045 struct lmv_user_md *lmu;
4047 /* initialize migrate mdt parameters */
4048 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4049 LMV_USER_MAGIC_SPECIFIC));
4052 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4053 progname, argv[0], strerror(ENOMEM));
4057 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4058 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4059 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4061 "%s %s: migrate should specify MDT index\n",
4066 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4067 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4068 lmu->lum_hash_type = lsa.lsa_pattern;
4070 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4071 if (lsa.lsa_pool_name) {
4072 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4073 sizeof(lmu->lum_pool_name) - 1);
4074 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4076 if (lsa.lsa_nr_tgts > 1) {
4079 if (lsa.lsa_stripe_count > 0 &&
4080 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4081 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4083 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4084 progname, lsa.lsa_stripe_count,
4090 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4091 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4092 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4093 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4095 lmu->lum_magic = LMV_USER_MAGIC;
4098 migrate_mdt_param.fp_lmv_md = lmu;
4099 migrate_mdt_param.fp_migrate = 1;
4100 } else if (!layout) {
4101 /* initialize stripe parameters */
4102 param = calloc(1, offsetof(typeof(*param),
4103 lsp_osts[lsa.lsa_nr_tgts]));
4106 "%s %s: cannot allocate memory for parameters: %s\n",
4107 progname, argv[0], strerror(ENOMEM));
4112 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4113 param->lsp_stripe_size = lsa.lsa_stripe_size;
4114 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4115 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4116 param->lsp_stripe_count = -1;
4118 param->lsp_stripe_count = lsa.lsa_stripe_count;
4120 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4121 param->lsp_stripe_offset = -1;
4123 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4124 param->lsp_stripe_pattern =
4125 llapi_pattern_to_lov(lsa.lsa_pattern);
4126 if (param->lsp_stripe_pattern == EINVAL) {
4127 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4132 param->lsp_pool = lsa.lsa_pool_name;
4133 param->lsp_is_specific = false;
4134 if (lsa.lsa_nr_tgts > 0) {
4135 if (lsa.lsa_stripe_count > 0 &&
4136 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4137 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4138 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4140 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4141 argv[0], lsa.lsa_stripe_count,
4147 param->lsp_is_specific = true;
4148 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4149 memcpy(param->lsp_osts, tgts,
4150 sizeof(*tgts) * lsa.lsa_nr_tgts);
4155 /* generate a layout from a YAML template */
4156 result = lfs_comp_create_from_yaml(template, &layout,
4160 "error: %s: can't create composite layout from template file %s\n",
4166 if (layout != NULL || mirror_list != NULL) {
4168 result = mirror_adjust_first_extents(mirror_list);
4170 result = layout_adjust_first_extent(fname, layout,
4172 if (result == -ENODATA)
4174 else if (result != 0) {
4175 fprintf(stderr, "error: %s: invalid layout\n",
4181 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4183 layout = llapi_layout_get_by_path(template ?: fname, 0);
4186 "%s: can't create composite layout from file %s: %s\n",
4187 progname, template ?: fname,
4194 if (migrate_mdt_mode) {
4195 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4196 } else if (migrate_mode) {
4197 result = lfs_migrate(fname, migration_flags, param,
4199 } else if (comp_set != 0) {
4200 result = lfs_component_set(fname, comp_id,
4203 lsa.lsa_comp_neg_flags);
4204 } else if (comp_del != 0) {
4205 result = lfs_component_del(fname, comp_id,
4207 lsa.lsa_comp_neg_flags);
4208 } else if (comp_add != 0) {
4209 result = lfs_component_add(fname, layout);
4210 } else if (opc == SO_MIRROR_CREATE) {
4211 result = mirror_create(fname, mirror_list);
4212 } else if (opc == SO_MIRROR_EXTEND) {
4213 result = mirror_extend(fname, mirror_list,
4215 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4216 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4218 "%s: no mirror specified to delete from '%s'\n",
4222 if (lsa.lsa_pool_name)
4223 mirror_flags |= MF_COMP_POOL;
4224 else if (mirror_id != 0)
4225 comp_id = mirror_id;
4227 mirror_flags |= MF_COMP_ID;
4228 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4230 has_m_file ? mirror_list->m_file :
4232 } else if (layout) {
4233 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4239 } else if (foreign_mode) {
4240 result = llapi_file_create_foreign(fname, mode, type,
4247 result = llapi_file_open_param(fname,
4256 /* Save the first error encountered. */
4264 umask(previous_umask);
4267 free(migrate_mdt_param.fp_lmv_md);
4268 llapi_layout_free(layout);
4269 lfs_mirror_list_free(mirror_list);
4274 llapi_layout_free(layout);
4275 lfs_mirror_list_free(mirror_list);
4279 static int lfs_poollist(int argc, char **argv)
4284 return llapi_poollist(argv[1]);
4287 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4288 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4293 char *endptr = "AD";
4298 else if (str[0] == '-')
4304 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4305 long long val = strtoll(timebuf, &endptr, 0);
4310 unit *= 52; /* 52 weeks + 1 day below */
4311 case 'w': /* fallthrough */
4313 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4314 param->fp_time_margin *= (1 + unit / 52);
4315 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4316 case '\0': /* days are default unit if none used */
4317 case 'd': /* fallthrough */
4319 case 'h': /* fallthrough */
4321 case 'm': /* fallthrough */
4323 case 's': /* fallthrough */
4325 /* don't need to multiply by 1 for seconds */
4328 "%s find: bad time string '%s': %s\n",
4329 progname, timebuf, strerror(EINVAL));
4333 if (param->fp_time_margin == 0 ||
4334 (*endptr && unit < param->fp_time_margin))
4335 param->fp_time_margin = unit;
4342 fprintf(stderr, "%s find: bad time '%s': too large\n",
4352 static int str2quotaid(__u32 *id, const char *arg)
4354 unsigned long int projid_tmp = 0;
4355 char *endptr = NULL;
4357 projid_tmp = strtoul(arg, &endptr, 10);
4358 if (*endptr != '\0')
4360 if (projid_tmp >= UINT32_MAX)
4367 static int name2uid(unsigned int *id, const char *name)
4369 struct passwd *passwd;
4371 passwd = getpwnam(name);
4374 *id = passwd->pw_uid;
4379 static int name2gid(unsigned int *id, const char *name)
4381 struct group *group;
4383 group = getgrnam(name);
4386 *id = group->gr_gid;
4391 static inline int name2projid(unsigned int *id, const char *name)
4396 static int uid2name(char **name, unsigned int id)
4398 struct passwd *passwd;
4400 passwd = getpwuid(id);
4403 *name = passwd->pw_name;
4408 static inline int gid2name(char **name, unsigned int id)
4410 struct group *group;
4412 group = getgrgid(id);
4415 *name = group->gr_name;
4420 static int name2layout(__u32 *layout, char *name)
4422 char *ptr, *layout_name;
4425 for (ptr = name; ; ptr = NULL) {
4426 layout_name = strtok(ptr, ",");
4429 if (strcmp(layout_name, "released") == 0)
4430 *layout |= LOV_PATTERN_F_RELEASED;
4431 else if (strcmp(layout_name, "raid0") == 0)
4432 *layout |= LOV_PATTERN_RAID0;
4433 else if (strcmp(layout_name, "mdt") == 0)
4434 *layout |= LOV_PATTERN_MDT;
4435 else if (strcmp(layout_name, "overstriping") == 0)
4436 *layout |= LOV_PATTERN_OVERSTRIPING;
4443 static int lfs_find(int argc, char **argv)
4448 struct find_param param = {
4451 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4453 struct option long_opts[] = {
4454 { .val = 'A', .name = "atime", .has_arg = required_argument },
4455 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4456 { .val = 'B', .name = "btime", .has_arg = required_argument },
4457 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4458 { .val = LFS_COMP_COUNT_OPT,
4459 .name = "comp-count", .has_arg = required_argument },
4460 { .val = LFS_COMP_COUNT_OPT,
4461 .name = "component-count",
4462 .has_arg = required_argument },
4463 { .val = LFS_COMP_FLAGS_OPT,
4464 .name = "comp-flags", .has_arg = required_argument },
4465 { .val = LFS_COMP_FLAGS_OPT,
4466 .name = "component-flags",
4467 .has_arg = required_argument },
4468 { .val = LFS_COMP_START_OPT,
4469 .name = "comp-start", .has_arg = required_argument },
4470 { .val = LFS_COMP_START_OPT,
4471 .name = "component-start",
4472 .has_arg = required_argument },
4473 { .val = LFS_MIRROR_STATE_OPT,
4474 .name = "mirror-state", .has_arg = required_argument },
4475 { .val = LFS_NEWERXY_OPT,
4476 .name = "newer", .has_arg = required_argument},
4477 { .val = LFS_NEWERXY_OPT,
4478 .name = "neweraa", .has_arg = required_argument},
4479 { .val = LFS_NEWERXY_OPT,
4480 .name = "neweram", .has_arg = required_argument},
4481 { .val = LFS_NEWERXY_OPT,
4482 .name = "newerac", .has_arg = required_argument},
4483 { .val = LFS_NEWERXY_OPT,
4484 .name = "newerab", .has_arg = required_argument},
4485 { .val = LFS_NEWERXY_OPT,
4486 .name = "newerma", .has_arg = required_argument},
4487 { .val = LFS_NEWERXY_OPT,
4488 .name = "newermm", .has_arg = required_argument},
4489 { .val = LFS_NEWERXY_OPT,
4490 .name = "newermc", .has_arg = required_argument},
4491 { .val = LFS_NEWERXY_OPT,
4492 .name = "newermb", .has_arg = required_argument},
4493 { .val = LFS_NEWERXY_OPT,
4494 .name = "newerca", .has_arg = required_argument},
4495 { .val = LFS_NEWERXY_OPT,
4496 .name = "newercm", .has_arg = required_argument},
4497 { .val = LFS_NEWERXY_OPT,
4498 .name = "newercc", .has_arg = required_argument},
4499 { .val = LFS_NEWERXY_OPT,
4500 .name = "newercb", .has_arg = required_argument},
4501 { .val = LFS_NEWERXY_OPT,
4502 .name = "newerba", .has_arg = required_argument},
4503 { .val = LFS_NEWERXY_OPT,
4504 .name = "newerbm", .has_arg = required_argument},
4505 { .val = LFS_NEWERXY_OPT,
4506 .name = "newerbc", .has_arg = required_argument},
4507 { .val = LFS_NEWERXY_OPT,
4508 .name = "newerbb", .has_arg = required_argument},
4509 { .val = LFS_NEWERXY_OPT,
4510 .name = "newerBa", .has_arg = required_argument},
4511 { .val = LFS_NEWERXY_OPT,
4512 .name = "newerBm", .has_arg = required_argument},
4513 { .val = LFS_NEWERXY_OPT,
4514 .name = "newerBc", .has_arg = required_argument},
4515 { .val = LFS_NEWERXY_OPT,
4516 .name = "newerBB", .has_arg = required_argument},
4517 { .val = LFS_NEWERXY_OPT,
4518 .name = "newerat", .has_arg = required_argument},
4519 { .val = LFS_NEWERXY_OPT,
4520 .name = "newermt", .has_arg = required_argument},
4521 { .val = LFS_NEWERXY_OPT,
4522 .name = "newerct", .has_arg = required_argument},
4523 { .val = LFS_NEWERXY_OPT,
4524 .name = "newerbt", .has_arg = required_argument},
4525 { .val = LFS_NEWERXY_OPT,
4526 .name = "newerBt", .has_arg = required_argument},
4527 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4528 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4529 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4530 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4531 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4532 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4533 { .val = 'E', .name = "component-end",
4534 .has_arg = required_argument },
4535 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4536 { .val = LFS_LAYOUT_FOREIGN_OPT,
4537 .name = "foreign", .has_arg = optional_argument},
4538 { .val = 'g', .name = "gid", .has_arg = required_argument },
4539 { .val = 'G', .name = "group", .has_arg = required_argument },
4540 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4541 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4542 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4543 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4544 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4545 { .val = 'L', .name = "layout", .has_arg = required_argument },
4546 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4547 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4548 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4549 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4550 { .val = 'n', .name = "name", .has_arg = required_argument },
4551 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4552 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4553 { .val = 'O', .name = "obd", .has_arg = required_argument },
4554 { .val = 'O', .name = "ost", .has_arg = required_argument },
4555 /* no short option for pool yet, can be 'p' after 2.18 */
4556 { .val = LFS_POOL_OPT,
4557 .name = "pool", .has_arg = required_argument },
4558 { .val = '0', .name = "print0", .has_arg = no_argument },
4559 { .val = 'P', .name = "print", .has_arg = no_argument },
4560 { .val = LFS_PROJID_OPT,
4561 .name = "projid", .has_arg = required_argument },
4562 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4563 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4564 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4565 { .val = 's', .name = "size", .has_arg = required_argument },
4566 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4567 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4568 { .val = 't', .name = "type", .has_arg = required_argument },
4569 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4570 { .val = 'u', .name = "uid", .has_arg = required_argument },
4571 { .val = 'U', .name = "user", .has_arg = required_argument },
4572 { .val = 'z', .name = "extension-size",
4573 .has_arg = required_argument },
4574 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4575 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4576 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4590 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4591 while ((c = getopt_long_only(argc, argv,
4592 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4593 long_opts, &optidx)) >= 0) {
4598 /* '!' is part of option */
4600 * when getopt_long_only() finds a string which is not
4601 * an option nor a known option argument it returns 1
4602 * in that case if we already have found pathstart and pathend
4603 * (i.e. we have the list of pathnames),
4604 * the only supported value is "!"
4606 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4607 if (!isoption && pathend != -1) {
4609 "err: %s: filename|dirname must either precede options or follow options\n",
4614 if (!isoption && pathstart == -1)
4615 pathstart = optind - 1;
4616 if (isoption && pathstart != -1 && pathend == -1)
4617 pathend = optind - 2;
4624 * unknown; opt is "!" or path component,
4625 * checking done above.
4627 if (strcmp(optarg, "!") == 0)
4631 xtime = ¶m.fp_atime;
4632 xsign = ¶m.fp_asign;
4633 param.fp_exclude_atime = !!neg_opt;
4634 /* no break, this falls through to 'B' for btime */
4637 xtime = ¶m.fp_btime;
4638 xsign = ¶m.fp_bsign;
4639 param.fp_exclude_btime = !!neg_opt;
4641 /* no break, this falls through to 'C' for ctime */
4644 xtime = ¶m.fp_ctime;
4645 xsign = ¶m.fp_csign;
4646 param.fp_exclude_ctime = !!neg_opt;
4648 /* no break, this falls through to 'M' for mtime */
4651 xtime = ¶m.fp_mtime;
4652 xsign = ¶m.fp_msign;
4653 param.fp_exclude_mtime = !!neg_opt;
4655 rc = set_time(¶m, &t, xtime, optarg);
4656 if (rc == LONG_MAX) {
4664 if (optarg[0] == '+') {
4665 param.fp_blocks_sign = -1;
4667 } else if (optarg[0] == '-') {
4668 param.fp_blocks_sign = 1;
4672 param.fp_blocks_units = 1024;
4673 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4674 ¶m.fp_blocks_units, 0);
4676 fprintf(stderr, "error: bad blocks '%s'\n",
4680 param.fp_check_blocks = 1;
4681 param.fp_exclude_blocks = !!neg_opt;
4683 case LFS_COMP_COUNT_OPT:
4684 if (optarg[0] == '+') {
4685 param.fp_comp_count_sign = -1;
4687 } else if (optarg[0] == '-') {
4688 param.fp_comp_count_sign = 1;
4693 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4694 if (errno != 0 || *endptr != '\0' ||
4695 param.fp_comp_count > UINT32_MAX) {
4697 "error: bad component count '%s'\n",
4701 param.fp_check_comp_count = 1;
4702 param.fp_exclude_comp_count = !!neg_opt;
4704 case LFS_COMP_FLAGS_OPT:
4705 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4706 ¶m.fp_comp_neg_flags);
4709 "error: bad component flags '%s'\n",
4713 param.fp_check_comp_flags = 1;
4715 __u32 flags = param.fp_comp_neg_flags;
4717 param.fp_comp_neg_flags = param.fp_comp_flags;
4718 param.fp_comp_flags = flags;
4721 case LFS_COMP_START_OPT:
4722 if (optarg[0] == '+') {
4723 param.fp_comp_start_sign = -1;
4725 } else if (optarg[0] == '-') {
4726 param.fp_comp_start_sign = 1;
4730 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4731 ¶m.fp_comp_start_units, 0);
4734 "error: bad component start '%s'\n",
4738 param.fp_check_comp_start = 1;
4739 param.fp_exclude_comp_start = !!neg_opt;
4741 case LFS_MIRROR_STATE_OPT:
4742 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4743 ¶m.fp_mirror_neg_state);
4746 "error: bad mirrored file state '%s'\n",
4750 param.fp_check_mirror_state = 1;
4752 __u16 state = param.fp_mirror_neg_state;
4754 param.fp_mirror_neg_state =
4755 param.fp_mirror_state;
4756 param.fp_mirror_state = state;
4760 if (optarg[0] == '+') {
4761 param.fp_stripe_count_sign = -1;
4763 } else if (optarg[0] == '-') {
4764 param.fp_stripe_count_sign = 1;
4769 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4770 if (errno != 0 || *endptr != '\0' ||
4771 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
4773 "error: bad stripe_count '%s'\n",
4778 param.fp_check_stripe_count = 1;
4779 param.fp_exclude_stripe_count = !!neg_opt;
4783 param.fp_max_depth = strtol(optarg, 0, 0);
4784 if (errno != 0 || param.fp_max_depth < 0) {
4786 "error: bad maxdepth '%s'\n",
4793 if (optarg[0] == '+') {
4794 param.fp_comp_end_sign = -1;
4796 } else if (optarg[0] == '-') {
4797 param.fp_comp_end_sign = 1;
4801 if (arg_is_eof(optarg)) {
4802 param.fp_comp_end = LUSTRE_EOF;
4803 param.fp_comp_end_units = 1;
4806 rc = llapi_parse_size(optarg,
4808 ¶m.fp_comp_end_units, 0);
4809 /* assume units of KB if too small */
4810 if (param.fp_comp_end < 4096)
4811 param.fp_comp_end *= 1024;
4815 "error: bad component end '%s'\n",
4819 param.fp_check_comp_end = 1;
4820 param.fp_exclude_comp_end = !!neg_opt;
4822 case LFS_LAYOUT_FOREIGN_OPT: {
4823 /* all types by default */
4824 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4827 /* check pure numeric */
4828 type = strtoul(optarg, &endptr, 0);
4831 type = check_foreign_type_name(optarg);
4832 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4834 "%s %s: unknown foreign type '%s'\n",
4839 } else if (type >= UINT32_MAX) {
4841 "%s %s: invalid foreign type '%s'\n",
4842 progname, argv[0], optarg);
4846 param.fp_foreign_type = type;
4847 param.fp_check_foreign = 1;
4848 param.fp_exclude_foreign = !!neg_opt;
4851 case LFS_NEWERXY_OPT: {
4857 time_t ref = time(NULL);
4859 /* no need to check bad options, they won't get here */
4860 if (strlen(long_opts[optidx].name) == 7) {
4861 x = long_opts[optidx].name[5];
4862 y = long_opts[optidx].name[6];
4866 static const char *const fmts[] = {
4867 "%Y-%m-%d %H:%M:%S",
4870 "%H:%M:%S", /* sometime today */
4879 for (i = 0; fmts[i] != NULL; i++) {
4882 /* Init for times relative to today */
4883 if (strncmp(fmts[i], "%H", 2) == 0)
4884 localtime_r(&ref, &tm);
4886 memset(&tm, 0, sizeof(tm));
4887 ptr = strptime(optarg, fmts[i], &tm);
4889 while (ptr && isspace(*ptr))
4891 if (ptr == optarg + strlen(optarg)) {
4899 "%s: invalid time '%s'\n",
4902 "supported formats are:\n ");
4903 for (i = 0; fmts[i] != NULL; i++)
4904 fprintf(stderr, "'%s', ",
4906 fprintf(stderr, "\n");
4912 } else if (y == 'b' || y == 'B') {
4915 rc = llapi_get_lum_file(optarg, NULL, &stx,
4917 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4918 if (!(stx.stx_mask & STATX_BTIME))
4923 "%s: get btime failed '%s': %s\n",
4929 ref = stx.stx_btime.tv_sec;
4931 struct stat statbuf;
4933 if (stat(optarg, &statbuf) < 0) {
4935 "%s: cannot stat file '%s': %s\n",
4944 ref = statbuf.st_atime;
4947 ref = statbuf.st_mtime;
4950 ref = statbuf.st_ctime;
4954 "%s: invalid Y argument: '%c'\n",
4963 xidx = NEWERXY_ATIME;
4966 xidx = NEWERXY_MTIME;
4969 xidx = NEWERXY_CTIME;
4973 xidx = NEWERXY_BTIME;
4977 "%s: invalid X argument: '%c'\n",
4984 newery = ¶m.fp_newery[xidx][negidx];
4990 *newery = *newery > ref ? ref : *newery;
4992 *newery = *newery > ref ? *newery : ref;
4994 param.fp_newerxy = 1;
4999 rc = name2gid(¶m.fp_gid, optarg);
5001 if (str2quotaid(¶m.fp_gid, optarg)) {
5003 "Group/GID: %s cannot be found.\n",
5009 param.fp_exclude_gid = !!neg_opt;
5010 param.fp_check_gid = 1;
5013 param.fp_hash_type = check_hashtype(optarg);
5014 if (param.fp_hash_type == 0) {
5015 fprintf(stderr, "error: bad hash_type '%s'\n",
5020 param.fp_check_hash_type = 1;
5021 param.fp_exclude_hash_type = !!neg_opt;
5027 ret = name2layout(¶m.fp_layout, optarg);
5030 param.fp_exclude_layout = !!neg_opt;
5031 param.fp_check_layout = 1;
5035 rc = name2uid(¶m.fp_uid, optarg);
5037 if (str2quotaid(¶m.fp_uid, optarg)) {
5039 "User/UID: %s cannot be found.\n",
5045 param.fp_exclude_uid = !!neg_opt;
5046 param.fp_check_uid = 1;
5049 param.fp_pattern = (char *)optarg;
5050 param.fp_exclude_pattern = !!neg_opt;
5053 if (optarg[0] == '+') {
5054 param.fp_mirror_count_sign = -1;
5056 } else if (optarg[0] == '-') {
5057 param.fp_mirror_count_sign = 1;
5062 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5063 if (errno != 0 || *endptr != '\0' ||
5064 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5066 "error: bad mirror count '%s'\n",
5070 param.fp_check_mirror_count = 1;
5071 param.fp_exclude_mirror_count = !!neg_opt;
5076 char *buf, *token, *next, *p;
5080 buf = strdup(optarg);
5086 param.fp_exclude_obd = !!neg_opt;
5089 while (token && *token) {
5090 token = strchr(token, ',');
5097 param.fp_exclude_mdt = !!neg_opt;
5098 param.fp_num_alloc_mdts += len;
5099 tmp = realloc(param.fp_mdt_uuid,
5100 param.fp_num_alloc_mdts *
5101 sizeof(*param.fp_mdt_uuid));
5107 param.fp_mdt_uuid = tmp;
5109 param.fp_exclude_obd = !!neg_opt;
5110 param.fp_num_alloc_obds += len;
5111 tmp = realloc(param.fp_obd_uuid,
5112 param.fp_num_alloc_obds *
5113 sizeof(*param.fp_obd_uuid));
5119 param.fp_obd_uuid = tmp;
5121 for (token = buf; token && *token; token = next) {
5122 struct obd_uuid *puuid;
5126 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5129 ¶m.fp_obd_uuid[param.fp_num_obds++];
5131 p = strchr(token, ',');
5138 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5143 strncpy(puuid->uuid, token,
5144 sizeof(puuid->uuid));
5151 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5155 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5157 "Pool name %s is too long (max %d)\n",
5158 optarg, LOV_MAXPOOLNAME);
5163 * We do check for empty pool because empty pool
5164 * is used to find V1 LOV attributes
5166 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5167 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5168 param.fp_exclude_pool = !!neg_opt;
5169 param.fp_check_pool = 1;
5171 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5172 case 'p': /* want this for --pool, to match getstripe/find */
5174 "warning: -p deprecated, use --print0 or -0\n");
5177 param.fp_zero_end = 1;
5179 case 'P': /* we always print, this option is a no-op */
5181 case LFS_PROJID_OPT:
5182 rc = name2projid(¶m.fp_projid, optarg);
5184 if (str2quotaid(¶m.fp_projid, optarg)) {
5186 "Invalid project ID: %s\n",
5192 param.fp_exclude_projid = !!neg_opt;
5193 param.fp_check_projid = 1;
5196 if (optarg[0] == '+') {
5197 param.fp_size_sign = -1;
5199 } else if (optarg[0] == '-') {
5200 param.fp_size_sign = 1;
5204 ret = llapi_parse_size(optarg, ¶m.fp_size,
5205 ¶m.fp_size_units, 0);
5207 fprintf(stderr, "error: bad file size '%s'\n",
5211 param.fp_check_size = 1;
5212 param.fp_exclude_size = !!neg_opt;
5215 if (optarg[0] == '+') {
5216 param.fp_stripe_size_sign = -1;
5218 } else if (optarg[0] == '-') {
5219 param.fp_stripe_size_sign = 1;
5223 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5224 ¶m.fp_stripe_size_units, 0);
5225 /* assume units of KB if too small to be valid */
5226 if (param.fp_stripe_size < 4096)
5227 param.fp_stripe_size *= 1024;
5229 fprintf(stderr, "error: bad stripe_size '%s'\n",
5233 param.fp_check_stripe_size = 1;
5234 param.fp_exclude_stripe_size = !!neg_opt;
5237 param.fp_exclude_type = !!neg_opt;
5238 switch (optarg[0]) {
5240 param.fp_type = S_IFBLK;
5243 param.fp_type = S_IFCHR;
5246 param.fp_type = S_IFDIR;
5249 param.fp_type = S_IFREG;
5252 param.fp_type = S_IFLNK;
5255 param.fp_type = S_IFIFO;
5258 param.fp_type = S_IFSOCK;
5261 fprintf(stderr, "error: %s: bad type '%s'\n",
5268 if (optarg[0] == '+') {
5269 param.fp_mdt_count_sign = -1;
5271 } else if (optarg[0] == '-') {
5272 param.fp_mdt_count_sign = 1;
5277 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5278 if (errno != 0 || *endptr != '\0' ||
5279 param.fp_mdt_count >= UINT32_MAX) {
5280 fprintf(stderr, "error: bad mdt_count '%s'\n",
5285 param.fp_check_mdt_count = 1;
5286 param.fp_exclude_mdt_count = !!neg_opt;
5289 if (optarg[0] == '+') {
5290 param.fp_ext_size_sign = -1;
5292 } else if (optarg[0] == '-') {
5293 param.fp_ext_size_sign = 1;
5297 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5298 ¶m.fp_ext_size_units, 0);
5300 fprintf(stderr, "error: bad ext-size '%s'\n",
5304 param.fp_ext_size /= SEL_UNIT_SIZE;
5305 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5306 param.fp_check_ext_size = 1;
5307 param.fp_exclude_ext_size = !!neg_opt;
5315 if (pathstart == -1) {
5316 fprintf(stderr, "error: %s: no filename|pathname\n",
5320 } else if (pathend == -1) {
5326 rc = llapi_find(argv[pathstart], ¶m);
5329 pathbad = pathstart;
5331 } while (++pathstart < pathend);
5334 fprintf(stderr, "%s: failed for '%s': %s\n",
5335 progname, argv[pathbad], strerror(-rc));
5338 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5339 free(param.fp_obd_uuid);
5341 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5342 free(param.fp_mdt_uuid);
5347 static int lfs_getstripe_internal(int argc, char **argv,
5348 struct find_param *param)
5350 struct option long_opts[] = {
5351 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5352 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5353 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5354 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5355 { .val = LFS_COMP_COUNT_OPT,
5356 .name = "comp-count", .has_arg = no_argument },
5357 { .val = LFS_COMP_COUNT_OPT,
5358 .name = "component-count", .has_arg = no_argument },
5359 { .val = LFS_COMP_FLAGS_OPT,
5360 .name = "comp-flags", .has_arg = optional_argument },
5361 { .val = LFS_COMP_FLAGS_OPT,
5362 .name = "component-flags", .has_arg = optional_argument },
5363 { .val = LFS_COMP_START_OPT,
5364 .name = "comp-start", .has_arg = optional_argument },
5365 { .val = LFS_COMP_START_OPT,
5366 .name = "component-start", .has_arg = optional_argument },
5367 { .val = LFS_MIRROR_INDEX_OPT,
5368 .name = "mirror-index", .has_arg = required_argument },
5369 { .val = LFS_MIRROR_ID_OPT,
5370 .name = "mirror-id", .has_arg = required_argument },
5371 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5372 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5373 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5374 { .val = 'd', .name = "directory", .has_arg = no_argument },
5375 { .val = 'D', .name = "default", .has_arg = no_argument },
5376 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5377 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5378 { .val = 'F', .name = "fid", .has_arg = no_argument },
5379 { .val = 'g', .name = "generation", .has_arg = no_argument },
5380 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5381 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5382 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5383 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5384 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5385 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5386 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5387 { .val = 'L', .name = "layout", .has_arg = no_argument },
5388 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5389 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5390 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5391 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5392 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5393 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5394 { .val = 'O', .name = "obd", .has_arg = required_argument },
5395 { .val = 'O', .name = "ost", .has_arg = required_argument },
5396 { .val = 'p', .name = "pool", .has_arg = no_argument },
5397 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5398 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5399 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5400 { .val = 'R', .name = "raw", .has_arg = no_argument },
5401 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5402 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5403 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5404 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5405 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5406 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5407 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5408 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5409 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5410 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5414 int pathstart = -1, pathend = -1;
5418 while ((c = getopt_long(argc, argv,
5419 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5420 long_opts, NULL)) != -1) {
5424 /* '!' is part of option */
5425 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5426 if (!isoption && pathend != -1) {
5428 "error: %s: filename|dirname must either precede options or follow options\n",
5432 if (!isoption && pathstart == -1)
5433 pathstart = optind - 1;
5434 if (isoption && pathstart != -1 && pathend == -1)
5435 pathend = optind - 2;
5439 /* unknown: opt is "!" */
5440 if (strcmp(optarg, "!") == 0)
5444 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5445 param->fp_verbose |= VERBOSE_COUNT;
5446 param->fp_max_depth = 0;
5449 case LFS_COMP_COUNT_OPT:
5450 param->fp_verbose |= VERBOSE_COMP_COUNT;
5451 param->fp_max_depth = 0;
5453 case LFS_COMP_FLAGS_OPT:
5455 rc = comp_str2flags(optarg,
5456 ¶m->fp_comp_flags,
5457 ¶m->fp_comp_neg_flags);
5460 "error: %s bad component flags '%s'.\n",
5464 param->fp_check_comp_flags = 1;
5466 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5467 param->fp_max_depth = 0;
5470 case LFS_COMP_START_OPT:
5473 if (tmp[0] == '+') {
5474 param->fp_comp_start_sign = -1;
5476 } else if (tmp[0] == '-') {
5477 param->fp_comp_start_sign = 1;
5480 rc = llapi_parse_size(tmp,
5481 ¶m->fp_comp_start,
5482 ¶m->fp_comp_start_units, 0);
5485 "error: %s bad component start '%s'.\n",
5489 param->fp_check_comp_start = 1;
5491 param->fp_verbose |= VERBOSE_COMP_START;
5492 param->fp_max_depth = 0;
5495 case LFS_MIRROR_INDEX_OPT: {
5496 unsigned long int mirror_index;
5498 if (optarg[0] == '+') {
5499 param->fp_mirror_index_sign = -1;
5501 } else if (optarg[0] == '-') {
5502 param->fp_mirror_index_sign = 1;
5507 mirror_index = strtoul(optarg, &end, 0);
5508 if (errno != 0 || *end != '\0' ||
5509 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5510 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5512 "%s %s: invalid mirror index '%s'\n",
5513 progname, argv[0], optarg);
5517 param->fp_mirror_index = (__u16)mirror_index;
5519 if (param->fp_mirror_id != 0) {
5521 "%s %s: can't specify both mirror index and mirror ID\n",
5525 param->fp_check_mirror_index = 1;
5526 param->fp_exclude_mirror_index = !!neg_opt;
5529 case LFS_MIRROR_ID_OPT: {
5530 unsigned long int mirror_id;
5532 if (optarg[0] == '+') {
5533 param->fp_mirror_id_sign = -1;
5535 } else if (optarg[0] == '-') {
5536 param->fp_mirror_id_sign = 1;
5541 mirror_id = strtoul(optarg, &end, 0);
5542 if (errno != 0 || *end != '\0' ||
5543 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5544 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5546 "%s %s: invalid mirror ID '%s'\n",
5547 progname, argv[0], optarg);
5551 param->fp_mirror_id = (__u16)mirror_id;
5553 if (param->fp_mirror_index != 0) {
5555 "%s %s: can't specify both mirror index and mirror ID\n",
5559 param->fp_check_mirror_id = 1;
5560 param->fp_exclude_mirror_id = !!neg_opt;
5564 param->fp_max_depth = 0;
5567 param->fp_get_default_lmv = 1;
5572 if (tmp[0] == '+') {
5573 param->fp_comp_end_sign = -1;
5575 } else if (tmp[0] == '-') {
5576 param->fp_comp_end_sign = 1;
5580 if (arg_is_eof(tmp)) {
5581 param->fp_comp_end = LUSTRE_EOF;
5582 param->fp_comp_end_units = 1;
5585 rc = llapi_parse_size(tmp,
5586 ¶m->fp_comp_end,
5587 ¶m->fp_comp_end_units, 0);
5588 /* assume units of KB if too small */
5589 if (param->fp_comp_end < 4096)
5590 param->fp_comp_end *= 1024;
5594 "error: %s bad component end '%s'.\n",
5598 param->fp_check_comp_end = 1;
5600 param->fp_verbose |= VERBOSE_COMP_END;
5601 param->fp_max_depth = 0;
5605 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5606 param->fp_verbose |= VERBOSE_DFID;
5607 param->fp_max_depth = 0;
5611 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5612 param->fp_verbose |= VERBOSE_GENERATION;
5613 param->fp_max_depth = 0;
5617 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5618 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5619 param->fp_max_depth = 0;
5624 param->fp_comp_id = strtoul(optarg, &end, 0);
5625 if (*end != '\0' || param->fp_comp_id == 0 ||
5626 param->fp_comp_id > LCME_ID_MAX) {
5628 "error: %s bad component id '%s'\n",
5632 param->fp_check_comp_id = 1;
5634 param->fp_max_depth = 0;
5635 param->fp_verbose |= VERBOSE_COMP_ID;
5639 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5640 param->fp_verbose |= VERBOSE_PATTERN;
5641 param->fp_max_depth = 0;
5644 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5647 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5650 if (!(param->fp_verbose & VERBOSE_DETAIL))
5651 param->fp_max_depth = 0;
5652 param->fp_verbose |= VERBOSE_MDTINDEX;
5655 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5656 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5657 param->fp_max_depth = 0;
5661 if (param->fp_obd_uuid) {
5663 "error: %s: only one obduuid allowed",
5667 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5670 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5671 param->fp_verbose |= VERBOSE_POOL;
5672 param->fp_max_depth = 0;
5679 param->fp_recursive = 1;
5685 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5686 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5687 param->fp_max_depth = 0;
5691 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5697 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5698 param->fp_verbose |= VERBOSE_EXT_SIZE;
5699 param->fp_max_depth = 0;
5707 if (pathstart == -1) {
5708 fprintf(stderr, "error: %s: no filename|pathname\n",
5711 } else if (pathend == -1) {
5719 if (param->fp_recursive)
5720 param->fp_max_depth = -1;
5721 else if (param->fp_verbose & VERBOSE_DETAIL)
5722 param->fp_max_depth = 1;
5724 if (!param->fp_verbose)
5725 param->fp_verbose = VERBOSE_DEFAULT;
5726 if (param->fp_quiet)
5727 param->fp_verbose = VERBOSE_OBJID;
5730 rc = llapi_getstripe(argv[pathstart], param);
5731 } while (++pathstart < pathend && !rc);
5734 fprintf(stderr, "error: %s failed for %s.\n",
5735 argv[0], argv[optind - 1]);
5739 static int lfs_tgts(int argc, char **argv)
5741 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5742 struct find_param param;
5743 int index = 0, rc = 0;
5748 if (argc == 2 && !realpath(argv[1], path)) {
5750 fprintf(stderr, "error: invalid path '%s': %s\n",
5751 argv[1], strerror(-rc));
5755 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5756 /* Check if we have a mount point */
5757 if (mntdir[0] == '\0')
5760 memset(¶m, 0, sizeof(param));
5761 if (!strcmp(argv[0], "mdts"))
5762 param.fp_get_lmv = 1;
5764 rc = llapi_ostlist(mntdir, ¶m);
5766 fprintf(stderr, "error: %s: failed on %s\n",
5769 if (path[0] != '\0')
5771 memset(mntdir, 0, PATH_MAX);
5777 static int lfs_getstripe(int argc, char **argv)
5779 struct find_param param = { 0 };
5781 param.fp_max_depth = 1;
5782 return lfs_getstripe_internal(argc, argv, ¶m);
5786 static int lfs_getdirstripe(int argc, char **argv)
5788 struct find_param param = { 0 };
5789 struct option long_opts[] = {
5790 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5791 { .val = 'D', .name = "default", .has_arg = no_argument },
5792 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5793 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5794 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5795 { .val = 'O', .name = "obd", .has_arg = required_argument },
5796 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5797 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5798 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5799 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5803 param.fp_get_lmv = 1;
5805 while ((c = getopt_long(argc, argv,
5806 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5810 param.fp_verbose |= VERBOSE_COUNT;
5813 param.fp_get_default_lmv = 1;
5817 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5819 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5822 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5825 param.fp_verbose |= VERBOSE_HASH_TYPE;
5828 if (param.fp_obd_uuid) {
5830 "error: %s: only one obduuid allowed",
5834 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5837 param.fp_recursive = 1;
5840 param.fp_verbose |= VERBOSE_DETAIL;
5846 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5847 progname, argv[0], argv[optind - 1]);
5855 if (param.fp_recursive)
5856 param.fp_max_depth = -1;
5858 if (!param.fp_verbose)
5859 param.fp_verbose = VERBOSE_DEFAULT;
5862 rc = llapi_getstripe(argv[optind], ¶m);
5863 } while (++optind < argc && !rc);
5866 fprintf(stderr, "error: %s failed for %s.\n",
5867 argv[0], argv[optind - 1]);
5872 MNTDF_INODES = 0x0001,
5873 MNTDF_COOKED = 0x0002,
5874 MNTDF_LAZY = 0x0004,
5875 MNTDF_VERBOSE = 0x0008,
5876 MNTDF_SHOW = 0x0010,
5877 MNTDF_DECIMAL = 0x0020,
5880 #define COOK(value, base) \
5883 while (value > base) { \
5891 #define CDF "%11llu"
5892 #define HDF "%8.1f%c"
5896 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5898 double avail, used, ratio = 0;
5901 avail = st->os_ffree;
5902 used = st->os_files - st->os_ffree;
5904 avail = st->os_bavail;
5905 used = st->os_blocks - st->os_bfree;
5907 if (avail + used > 0)
5908 ratio = used / (used + avail) * 100;
5910 /* Round up to match df(1) usage percentage */
5911 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5915 * This is to identify various problem states for "lfs df" if .osn_err = true,
5916 * so only show flags reflecting those states by default. Informational states
5917 * are only shown with "-v" and use lower-case names to distinguish them.
5918 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5920 static struct obd_statfs_state_names {
5921 enum obd_statfs_state osn_state;
5922 const char osn_name;
5925 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5926 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5927 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5928 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5929 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5930 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5931 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5932 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5933 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5936 static int showdf(char *mntdir, struct obd_statfs *stat,
5937 char *uuid, enum mntdf_flags flags,
5938 char *type, int index, int rc)
5940 long long avail, used, total;
5942 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
5943 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5944 char tbuf[3 * sizeof(__u64)];
5945 char ubuf[3 * sizeof(__u64)];
5946 char abuf[3 * sizeof(__u64)];
5947 char rbuf[3 * sizeof(__u64)];
5954 if (flags & MNTDF_INODES) {
5955 avail = stat->os_ffree;
5956 used = stat->os_files - stat->os_ffree;
5957 total = stat->os_files;
5959 int shift = flags & MNTDF_COOKED ? 0 : 10;
5961 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5962 used = ((stat->os_blocks - stat->os_bfree) *
5963 stat->os_bsize) >> shift;
5964 total = (stat->os_blocks * stat->os_bsize) >> shift;
5967 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5969 if (flags & MNTDF_COOKED) {
5970 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
5974 cook_val = (double)total;
5975 i = COOK(cook_val, base);
5977 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5980 snprintf(tbuf, sizeof(tbuf), CDF, total);
5982 cook_val = (double)used;
5983 i = COOK(cook_val, base);
5985 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5988 snprintf(ubuf, sizeof(ubuf), CDF, used);
5990 cook_val = (double)avail;
5991 i = COOK(cook_val, base);
5993 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5996 snprintf(abuf, sizeof(abuf), CDF, avail);
5998 snprintf(tbuf, sizeof(tbuf), CDF, total);
5999 snprintf(ubuf, sizeof(tbuf), CDF, used);
6000 snprintf(abuf, sizeof(tbuf), CDF, avail);
6003 sprintf(rbuf, RDF, ratio);
6004 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6005 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6007 printf("[%s:%d]", type, index);
6009 if (stat->os_state) {
6013 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6014 if (oss_names[i].osn_state & stat->os_state &&
6015 (oss_names[i].osn_err ||
6016 flags & MNTDF_VERBOSE))
6017 printf("%c", oss_names[i].osn_name);
6024 printf(UUF": inactive device\n", uuid);
6027 printf(UUF": %s\n", uuid, strerror(-rc));
6034 struct ll_stat_type {
6039 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6041 struct ll_statfs_data {
6043 struct obd_statfs sd_st;
6046 struct ll_statfs_buf {
6048 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6051 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6052 int ops, struct ll_statfs_buf *lsb)
6054 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6055 struct obd_uuid uuid_buf;
6056 char *poolname = NULL;
6057 struct ll_stat_type types[] = {
6058 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6059 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6060 { .st_name = NULL } };
6061 struct ll_stat_type *tp;
6062 __u64 ost_files = 0;
6063 __u64 ost_ffree = 0;
6071 poolname = strchr(pool, '.');
6073 if (strncmp(fsname, pool, strlen(fsname))) {
6074 fprintf(stderr, "filesystem name incorrect\n");
6082 fd = open(mntdir, O_RDONLY);
6085 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6090 if (flags & MNTDF_SHOW) {
6091 if (flags & MNTDF_INODES)
6092 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6093 "UUID", "Inodes", "IUsed", "IFree",
6094 "IUse%", "Mounted on");
6096 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6098 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6099 "Used", "Available", "Use%", "Mounted on");
6102 for (tp = types; tp->st_name != NULL; tp++) {
6103 bool have_ost = false;
6105 if (!(tp->st_op & ops))
6108 for (index = 0; ; index++) {
6109 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6110 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6111 type = flags & MNTDF_LAZY ?
6112 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6113 rc2 = llapi_obd_fstatfs(fd, type, index,
6114 &stat_buf, &uuid_buf);
6119 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6120 if (!(flags & MNTDF_VERBOSE))
6122 } else if (rc2 < 0 && rc == 0) {
6127 * If we have OSTs then don't report MDT block counts.
6128 * For MDT-only filesystems the expectation is that all
6129 * layouts have a DoM component. For filesystems with
6130 * OSTs, files are not necessarily going to store data
6131 * on MDTs, and MDT space is limited to a fraction of
6132 * OST space, so don't include it in the summary.
6134 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6141 if (poolname && tp->st_op == LL_STATFS_LOV &&
6142 llapi_search_ost(fsname, poolname,
6143 obd_uuid2str(&uuid_buf)) != 1)
6147 * the llapi_obd_fstatfs() call may have returned with
6148 * an error, but if it filled in uuid_buf we will at
6149 * lease use that to print out a message for that OBD.
6150 * If we didn't get anything in the uuid_buf, then fill
6151 * it in so that we can print an error message.
6153 if (uuid_buf.uuid[0] == '\0')
6154 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6155 "%s%04x", tp->st_name, index);
6157 lsb->sb_buf[lsb->sb_count].sd_index = index;
6158 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6161 if (flags & MNTDF_SHOW)
6162 showdf(mntdir, &stat_buf,
6163 obd_uuid2str(&uuid_buf), flags,
6164 tp->st_name, index, rc2);
6169 if (tp->st_op == LL_STATFS_LMV) {
6170 sum.os_ffree += stat_buf.os_ffree;
6171 sum.os_files += stat_buf.os_files;
6172 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6173 ost_files += stat_buf.os_files;
6174 ost_ffree += stat_buf.os_ffree;
6176 sum.os_blocks += stat_buf.os_blocks *
6178 sum.os_bfree += stat_buf.os_bfree *
6180 sum.os_bavail += stat_buf.os_bavail *
6188 * If we have _some_ OSTs, but don't have as many free objects on the
6189 * OST as inodes on the MDTs, reduce the reported number of inodes
6190 * to compensate, so that the "inodes in use" number is correct.
6191 * This should be kept in sync with ll_statfs_internal().
6193 if (ost_files && ost_ffree < sum.os_ffree) {
6194 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6195 sum.os_ffree = ost_ffree;
6197 if (flags & MNTDF_SHOW) {
6199 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6207 static int lfs_setdirstripe(int argc, char **argv)
6210 struct lfs_setstripe_args lsa = { 0 };
6211 struct llapi_stripe_param *param = NULL;
6212 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6215 char *mode_opt = NULL;
6216 bool default_stripe = false;
6217 bool delete = false;
6218 bool foreign_mode = false;
6219 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6220 mode_t previous_mode = 0;
6222 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6223 struct option long_opts[] = {
6224 { .val = 'c', .name = "count", .has_arg = required_argument },
6225 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6226 { .val = 'd', .name = "delete", .has_arg = no_argument },
6227 { .val = 'D', .name = "default", .has_arg = no_argument },
6228 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6229 { .val = LFS_LAYOUT_FLAGS_OPT,
6230 .name = "flags", .has_arg = required_argument },
6231 { .val = LFS_LAYOUT_FOREIGN_OPT,
6232 .name = "foreign", .has_arg = optional_argument},
6233 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6234 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6235 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6236 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6238 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6239 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6240 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6242 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6243 { .val = 'i', .name = "index", .has_arg = required_argument },
6245 { .val = 'o', .name = "mode", .has_arg = required_argument },
6246 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6247 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6249 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6250 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6251 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6255 setstripe_args_init(&lsa);
6257 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6258 long_opts, NULL)) >= 0) {
6266 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6267 if (errno != 0 || *end != '\0' ||
6268 lsa.lsa_stripe_count < -1 ||
6269 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6271 "%s %s: invalid stripe count '%s'\n",
6272 progname, argv[0], optarg);
6278 default_stripe = true;
6281 default_stripe = true;
6283 case LFS_LAYOUT_FOREIGN_OPT:
6285 /* check pure numeric */
6286 type = strtoul(optarg, &end, 0);
6289 type = check_foreign_type_name(optarg);
6290 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6292 "%s %s: unknown foreign type '%s'\n",
6297 } else if (type >= UINT32_MAX) {
6299 "%s %s: invalid foreign type '%s'\n",
6300 progname, argv[0], optarg);
6304 foreign_mode = true;
6306 case LFS_LAYOUT_FLAGS_OPT:
6308 flags = strtoul(optarg, &end, 16);
6309 if (errno != 0 || *end != '\0' ||
6310 flags >= UINT32_MAX) {
6312 "%s %s: invalid hex flags '%s'\n",
6313 progname, argv[0], optarg);
6316 if (!foreign_mode) {
6318 "%s %s: hex flags must be specified with --foreign option\n",
6323 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6326 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6329 lsa.lsa_pattern = check_hashtype(optarg);
6330 if (lsa.lsa_pattern == 0) {
6332 "%s %s: bad stripe hash type '%s'\n",
6333 progname, argv[0], optarg);
6338 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6341 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6342 if (strcmp(argv[optind - 1], "--index") == 0)
6344 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6347 lsa.lsa_nr_tgts = parse_targets(mdts,
6348 sizeof(mdts) / sizeof(__u32),
6349 lsa.lsa_nr_tgts, optarg, NULL);
6350 if (lsa.lsa_nr_tgts < 0) {
6352 "%s %s: invalid MDT target(s) '%s'\n",
6353 progname, argv[0], optarg);
6357 lsa.lsa_tgts = mdts;
6358 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6359 lsa.lsa_stripe_off = mdts[0];
6361 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6364 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6373 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6374 progname, argv[0], argv[optind - 1]);
6379 if (optind == argc) {
6380 fprintf(stderr, "%s %s: DIR must be specified\n",
6385 if (xattr && !foreign_mode) {
6387 * only print a warning as this is armless and will be
6391 "%s %s: xattr has been specified for non-foreign layout\n",
6393 } else if (foreign_mode && !xattr) {
6395 "%s %s: xattr must be provided in foreign mode\n",
6400 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6401 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6403 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6408 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6409 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6410 /* if no parameters set, create directory on least-used MDTs */
6411 lsa.lsa_stripe_off = -1;
6412 lsa.lsa_stripe_count = 1;
6416 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6417 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6419 "%s %s: cannot specify -d with -c or -i options\n",
6425 mode = strtoul(mode_opt, &end, 8);
6428 "%s %s: bad MODE '%s'\n",
6429 progname, argv[0], mode_opt);
6432 previous_mode = umask(0);
6435 /* foreign LMV/dir case */
6437 if (argc > optind + 1) {
6439 "%s %s: cannot specify multiple foreign dirs\n",
6444 dname = argv[optind];
6445 result = llapi_dir_create_foreign(dname, mode, type, flags,
6449 "%s mkdir: can't create foreign dir '%s': %s\n",
6450 progname, dname, strerror(-result));
6455 * initialize stripe parameters, in case param is converted to specific,
6456 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6458 param = calloc(1, offsetof(typeof(*param),
6459 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6460 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6463 "%s %s: cannot allocate memory for parameters: %s\n",
6464 progname, argv[0], strerror(ENOMEM));
6468 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6469 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6470 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6471 lsa.lsa_stripe_count == 0))
6472 lsa.lsa_stripe_count = 1;
6473 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6474 param->lsp_stripe_count = lsa.lsa_stripe_count;
6475 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6476 param->lsp_stripe_offset = -1;
6478 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6479 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6480 param->lsp_stripe_pattern = lsa.lsa_pattern;
6482 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6483 param->lsp_pool = lsa.lsa_pool_name;
6484 param->lsp_is_specific = false;
6485 if (lsa.lsa_nr_tgts > 1) {
6486 if (lsa.lsa_stripe_count > 0 &&
6487 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6488 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6490 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6491 argv[0], lsa.lsa_stripe_count,
6497 param->lsp_is_specific = true;
6498 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6499 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6502 dname = argv[optind];
6504 if (default_stripe) {
6505 result = llapi_dir_set_default_lmv(dname, param);
6508 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6509 progname, dname, strerror(-result));
6513 result = llapi_dir_create(dname, mode, param);
6516 "%s setdirstripe: cannot create dir '%s': %s\n",
6517 progname, dname, strerror(-result));
6518 } while (!result && (dname = argv[++optind]));
6521 umask(previous_mode);
6528 static int lfs_rmentry(int argc, char **argv)
6535 fprintf(stderr, "error: %s: missing dirname\n",
6541 dname = argv[index];
6543 result = llapi_direntry_remove(dname);
6546 "error: %s: remove dir entry '%s' failed\n",
6550 dname = argv[++index];
6555 static int lfs_unlink_foreign(int argc, char **argv)
6562 fprintf(stderr, "error: %s: missing pathname\n",
6569 while (name != NULL) {
6570 result = llapi_unlink_foreign(name);
6573 "error: %s: unlink foreign entry '%s' failed\n",
6577 name = argv[++index];
6582 static int lfs_mv(int argc, char **argv)
6584 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6585 struct find_param param = {
6592 struct option long_opts[] = {
6593 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6594 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6595 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6598 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6600 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6603 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6607 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6608 if (errno != 0 || *end != '\0' ||
6609 lmu.lum_stripe_offset >= UINT32_MAX) {
6610 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6616 param.fp_verbose = VERBOSE_DETAIL;
6619 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6620 progname, argv[optind - 1]);
6625 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6626 fprintf(stderr, "%s mv: MDT index must be specified\n",
6631 if (optind >= argc) {
6632 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6636 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6638 /* initialize migrate mdt parameters */
6639 param.fp_lmv_md = &lmu;
6640 param.fp_migrate = 1;
6641 rc = llapi_migrate_mdt(argv[optind], ¶m);
6643 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6644 progname, argv[optind], lmu.lum_stripe_offset,
6649 static int lfs_osts(int argc, char **argv)
6651 return lfs_tgts(argc, argv);
6654 static int lfs_mdts(int argc, char **argv)
6656 return lfs_tgts(argc, argv);
6659 static int lfs_df(int argc, char **argv)
6661 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6662 enum mntdf_flags flags = MNTDF_SHOW;
6663 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6664 int c, rc = 0, index = 0;
6665 char fsname[PATH_MAX] = "", *pool_name = NULL;
6666 struct option long_opts[] = {
6667 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
6668 { .val = 'H', .name = "si", .has_arg = no_argument },
6669 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6670 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6671 { .val = 'p', .name = "pool", .has_arg = required_argument },
6672 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6675 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
6678 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
6681 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
6684 flags |= MNTDF_INODES;
6687 flags |= MNTDF_LAZY;
6693 flags |= MNTDF_VERBOSE;
6699 if (optind < argc && !realpath(argv[optind], path)) {
6701 fprintf(stderr, "error: invalid path '%s': %s\n",
6702 argv[optind], strerror(-rc));
6706 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6707 /* Check if we have a mount point */
6708 if (mntdir[0] == '\0')
6711 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6712 if (rc || path[0] != '\0')
6714 fsname[0] = '\0'; /* avoid matching in next loop */
6715 mntdir[0] = '\0'; /* avoid matching in next loop */
6721 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6722 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6726 if (opt_fsname == opt_instance) { /* both true or both false */
6727 rc = llapi_getname(mntdir, buf, buflen);
6728 } else if (opt_fsname) {
6730 * llapi_search_mounts() fills @buf with fsname, but that is not
6731 * called if explicit paths are specified on the command-line
6734 rc = llapi_get_fsname(mntdir, buf, buflen);
6735 } else /* if (opt_instance) */ {
6736 rc = llapi_get_instance(mntdir, buf, buflen);
6740 fprintf(stderr, "cannot get instance for '%s': %s\n",
6741 mntdir, strerror(-rc));
6746 printf("%s %s\n", buf, mntdir);
6748 printf("%s\n", buf);
6753 static int lfs_getname(int argc, char **argv)
6755 struct option long_opts[] = {
6756 { .val = 'h', .name = "help", .has_arg = no_argument },
6757 { .val = 'i', .name = "instance", .has_arg = no_argument },
6758 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6760 bool opt_instance = false, opt_fsname = false;
6761 char fsname[PATH_MAX] = "";
6764 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6767 opt_instance = true;
6778 if (optind == argc) { /* no paths specified, get all paths. */
6779 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6782 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6783 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6784 opt_instance, opt_fsname, true);
6787 path[0] = fsname[0] = mntdir[0] = '\0';
6789 } else { /* paths specified, only attempt to search these. */
6792 /* if only one path is given, print only requested info */
6793 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6795 for (; optind < argc; optind++) {
6796 rc2 = print_instance(argv[optind], fsname,
6797 sizeof(fsname), opt_instance,
6798 opt_fsname, opt_mntdir);
6808 static int lfs_check(int argc, char **argv)
6810 char mntdir[PATH_MAX] = {'\0'};
6819 fprintf(stderr, "%s check: server type must be specified\n",
6824 obd_types[0] = obd_type1;
6825 obd_types[1] = obd_type2;
6826 obd_types[2] = obd_type3;
6828 if (strcmp(argv[1], "osts") == 0) {
6829 strcpy(obd_types[0], "osc");
6830 } else if (strcmp(argv[1], "mdts") == 0 ||
6831 strcmp(argv[1], "mds") == 0) {
6832 strcpy(obd_types[0], "mdc");
6833 } else if (strcmp(argv[1], "mgts") == 0) {
6834 strcpy(obd_types[0], "mgc");
6835 } else if (strcmp(argv[1], "all") == 0 ||
6836 strcmp(argv[1], "servers") == 0) {
6838 strcpy(obd_types[0], "osc");
6839 strcpy(obd_types[1], "mdc");
6840 strcpy(obd_types[2], "mgc");
6842 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6847 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6848 if (rc < 0 || mntdir[0] == '\0') {
6850 "%s check: cannot find mounted Lustre filesystem: %s\n",
6851 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6855 rc = llapi_target_check(num_types, obd_types, mntdir);
6857 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6858 progname, argv[1], strerror(-rc));
6863 #ifdef HAVE_SYS_QUOTA_H
6864 #define ADD_OVERFLOW(a, b) \
6865 ((((a) + (b)) < (a)) ? \
6866 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6868 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6869 * returns the value or ULONG_MAX on integer overflow or incorrect format
6871 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6872 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6873 * 3. empty integer value is interpreted as 0
6875 static unsigned long str2sec(const char *timestr)
6877 const char spec[] = "smhdw";
6878 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6879 unsigned long val = 0;
6882 if (strpbrk(timestr, spec) == NULL) {
6884 * no specifiers inside the time string,
6885 * should treat it as an integer value
6887 val = strtoul(timestr, &tail, 10);
6888 return *tail ? ULONG_MAX : val;
6891 /* format string is XXwXXdXXhXXmXXs */
6897 v = strtoul(timestr, &tail, 10);
6898 if (v == ULONG_MAX || *tail == '\0')
6900 * value too large (ULONG_MAX or more)
6901 * or missing specifier
6905 ptr = strchr(spec, *tail);
6907 /* unknown specifier */
6912 /* check if product will overflow the type */
6913 if (!(v < ULONG_MAX / mult[ind]))
6916 ADD_OVERFLOW(val, mult[ind] * v);
6917 if (val == ULONG_MAX)
6929 #define ARG2ULL(nr, str, def_units) \
6931 unsigned long long limit, units = def_units; \
6934 rc = llapi_parse_size(str, &limit, &units, 1); \
6936 fprintf(stderr, "%s: invalid limit '%s'\n", \
6943 static inline int has_times_option(int argc, char **argv)
6947 for (i = 1; i < argc; i++)
6948 if (!strcmp(argv[i], "-t"))
6954 static inline int lfs_verify_poolarg(char *pool)
6956 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6958 "Pool name '%.*s' is longer than %d\n",
6959 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6965 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6968 char *mnt, *obd_type = (char *)qctl->obd_type;
6969 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6970 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6971 struct option long_opts[] = {
6972 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6973 { .val = 'g', .name = "group", .has_arg = no_argument },
6974 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6975 { .val = 'p', .name = "projid", .has_arg = no_argument },
6976 { .val = 't', .name = "times", .has_arg = no_argument },
6977 { .val = 'u', .name = "user", .has_arg = no_argument },
6978 { .val = LFS_POOL_OPT,
6979 .name = "pool", .has_arg = required_argument },
6983 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6984 qctl->qc_type = ALLQUOTA;
6986 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6987 long_opts, NULL)) != -1) {
6998 if (qctl->qc_type != ALLQUOTA) {
7000 "error: -u/g/p can't be used more than once\n");
7003 qctl->qc_type = qtype;
7006 if (strncmp(optarg, NOTIFY_GRACE,
7007 strlen(NOTIFY_GRACE)) == 0) {
7008 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7010 dqi->dqi_bgrace = str2sec(optarg);
7011 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7013 "error: bad block-grace: %s\n",
7018 dqb->dqb_valid |= QIF_BTIME;
7021 if (strncmp(optarg, NOTIFY_GRACE,
7022 strlen(NOTIFY_GRACE)) == 0) {
7023 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7025 dqi->dqi_igrace = str2sec(optarg);
7026 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7028 "error: bad inode-grace: %s\n",
7033 dqb->dqb_valid |= QIF_ITIME;
7035 case 't': /* Yes, of course! */
7038 if (lfs_verify_poolarg(optarg))
7041 "Trying to set grace for pool %s\n", optarg);
7042 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7043 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7045 /* getopt prints error message for us when opterr != 0 */
7051 if (qctl->qc_type == ALLQUOTA) {
7052 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
7056 if (optind != argc - 1) {
7057 fprintf(stderr, "error: unexpected parameters encountered\n");
7062 rc = llapi_quotactl(mnt, qctl);
7065 fprintf(stderr, "%s %s ", obd_type,
7066 obd_uuid2str(&qctl->obd_uuid));
7067 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7074 #define BSLIMIT (1 << 0)
7075 #define BHLIMIT (1 << 1)
7076 #define ISLIMIT (1 << 2)
7077 #define IHLIMIT (1 << 3)
7079 int lfs_setquota(int argc, char **argv)
7082 struct if_quotactl *qctl;
7083 char *mnt, *obd_type;
7084 struct obd_dqblk *dqb;
7085 struct option long_opts[] = {
7086 { .val = 'b', .name = "block-softlimit",
7087 .has_arg = required_argument },
7088 { .val = 'B', .name = "block-hardlimit",
7089 .has_arg = required_argument },
7090 { .val = 'd', .name = "default", .has_arg = no_argument },
7091 { .val = 'g', .name = "group", .has_arg = required_argument },
7092 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7093 { .val = 'i', .name = "inode-softlimit",
7094 .has_arg = required_argument },
7095 { .val = 'I', .name = "inode-hardlimit",
7096 .has_arg = required_argument },
7097 { .val = 'p', .name = "projid", .has_arg = required_argument },
7098 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7099 { .val = 'u', .name = "user", .has_arg = required_argument },
7100 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7101 { .val = LFS_POOL_OPT,
7102 .name = "pool", .has_arg = required_argument },
7104 unsigned int limit_mask = 0;
7105 bool use_default = false;
7106 int qtype, qctl_len;
7108 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7109 qctl = malloc(qctl_len);
7113 memset(qctl, 0, qctl_len);
7114 obd_type = (char *)qctl->obd_type;
7115 dqb = &qctl->qc_dqblk;
7117 if (has_times_option(argc, argv)) {
7118 rc = lfs_setquota_times(argc, argv, qctl);
7122 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7123 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7124 * so it can be used as a marker that qc_type
7125 * isn't reinitialized from command line
7128 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7129 long_opts, NULL)) != -1) {
7132 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7135 goto quota_type_def;
7138 rc = name2uid(&qctl->qc_id, optarg);
7141 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7144 goto quota_type_def;
7147 rc = name2gid(&qctl->qc_id, optarg);
7150 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7153 goto quota_type_def;
7156 rc = name2projid(&qctl->qc_id, optarg);
7159 if (str2quotaid(&qctl->qc_id, optarg)) {
7161 "%s setquota: invalid id '%s'\n",
7168 if (qctl->qc_id == 0) {
7170 "%s setquota: can't set quota for root usr/group/project.\n",
7177 if (qctl->qc_type != ALLQUOTA) {
7179 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7184 qctl->qc_type = qtype;
7187 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7191 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7192 dqb->dqb_bsoftlimit >>= 10;
7193 limit_mask |= BSLIMIT;
7194 if (dqb->dqb_bsoftlimit &&
7195 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7197 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7199 (unsigned long long)dqb->dqb_bsoftlimit,
7203 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7204 dqb->dqb_bhardlimit >>= 10;
7205 limit_mask |= BHLIMIT;
7206 if (dqb->dqb_bhardlimit &&
7207 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7209 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7210 "See '%s help setquota' or Lustre manual for details\n",
7212 (unsigned long long)dqb->dqb_bhardlimit,
7216 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7217 limit_mask |= ISLIMIT;
7218 if (dqb->dqb_isoftlimit &&
7219 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7221 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7223 (unsigned long long)dqb->dqb_isoftlimit,
7227 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7228 limit_mask |= IHLIMIT;
7229 if (dqb->dqb_ihardlimit &&
7230 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7232 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7234 (unsigned long long)dqb->dqb_ihardlimit,
7238 if (lfs_verify_poolarg(optarg)) {
7243 "Trying to set quota for pool %s\n", optarg);
7244 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7245 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7249 "%s setquota: unrecognized option '%s'\n",
7250 progname, argv[optind - 1]);
7256 if (qctl->qc_type == ALLQUOTA) {
7258 "%s setquota: either -u or -g must be specified\n",
7264 if (!use_default && limit_mask == 0) {
7266 "%s setquota: at least one limit must be specified\n",
7272 if (use_default && limit_mask != 0) {
7274 "%s setquota: limits should not be specified when using default quota\n",
7280 if (use_default && qctl->qc_id == 0) {
7282 "%s setquota: can not set default quota for root user/group/project\n",
7288 if (optind != argc - 1) {
7290 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7291 progname, argv[optind]);
7299 dqb->dqb_bhardlimit = 0;
7300 dqb->dqb_bsoftlimit = 0;
7301 dqb->dqb_ihardlimit = 0;
7302 dqb->dqb_isoftlimit = 0;
7305 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7306 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7307 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7308 /* sigh, we can't just set blimits/ilimits */
7309 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7310 .qc_type = qctl->qc_type,
7311 .qc_id = qctl->qc_id};
7313 rc = llapi_quotactl(mnt, &tmp_qctl);
7317 if (!(limit_mask & BHLIMIT))
7318 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7319 if (!(limit_mask & BSLIMIT))
7320 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7321 if (!(limit_mask & IHLIMIT))
7322 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7323 if (!(limit_mask & ISLIMIT))
7324 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7326 /* Keep grace times if we have got no softlimit arguments */
7327 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7328 dqb->dqb_valid |= QIF_BTIME;
7329 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7332 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7333 dqb->dqb_valid |= QIF_ITIME;
7334 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7338 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7339 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7341 rc = llapi_quotactl(mnt, qctl);
7345 "%s setquota: cannot quotactl '%s' '%s': %s",
7347 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7354 /* Converts seconds value into format string
7355 * result is returned in buf
7357 * 1. result is in descenting order: 1w2d3h4m5s
7358 * 2. zero fields are not filled (except for p. 3): 5d1s
7359 * 3. zero seconds value is presented as "0s"
7361 static char *__sec2str(time_t seconds, char *buf)
7363 const char spec[] = "smhdw";
7364 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7369 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7370 c = seconds / mult[i];
7372 if (c > 0 || (i == 0 && buf == tail))
7373 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7382 static void sec2str(time_t seconds, char *buf, int rc)
7389 tail = __sec2str(seconds, tail);
7391 if (rc && tail - buf < 39) {
7397 static void diff2str(time_t seconds, char *buf, time_t now)
7402 if (seconds <= now) {
7403 strcpy(buf, "none");
7406 __sec2str(seconds - now, buf);
7409 static void print_quota_title(char *name, struct if_quotactl *qctl,
7410 bool human_readable, bool show_default)
7413 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7414 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7415 "Filesystem", "bquota", "blimit", "bgrace",
7416 "iquota", "ilimit", "igrace");
7418 printf("Disk quotas for %s %s (%cid %u):\n",
7419 qtype_name(qctl->qc_type), name,
7420 *qtype_name(qctl->qc_type), qctl->qc_id);
7421 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7422 "Filesystem", human_readable ? "used" : "kbytes",
7423 "quota", "limit", "grace",
7424 "files", "quota", "limit", "grace");
7428 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7431 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7434 snprintf(buf, buflen, "%5.4gP",
7435 (double)num / ((__u64)1 << 40));
7437 snprintf(buf, buflen, "%5.4gT",
7438 (double)num / (1 << 30));
7440 snprintf(buf, buflen, "%5.4gG",
7441 (double)num / (1 << 20));
7443 snprintf(buf, buflen, "%5.4gM",
7444 (double)num / (1 << 10));
7446 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7450 #define STRBUF_LEN 32
7451 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7452 int rc, bool h, bool show_default)
7458 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7459 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7460 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7461 int bover = 0, iover = 0;
7462 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7463 char numbuf[3][STRBUF_LEN];
7465 char strbuf[STRBUF_LEN];
7467 if (dqb->dqb_bhardlimit &&
7468 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7470 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7471 if (dqb->dqb_btime > now)
7477 if (dqb->dqb_ihardlimit &&
7478 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7480 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7481 if (dqb->dqb_itime > now)
7487 if (strlen(mnt) > 15)
7488 printf("%s\n%15s", mnt, "");
7490 printf("%15s", mnt);
7493 diff2str(dqb->dqb_btime, timebuf, now);
7494 else if (show_default)
7495 snprintf(timebuf, sizeof(timebuf), "%llu",
7496 (unsigned long long)dqb->dqb_btime);
7498 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7499 strbuf, sizeof(strbuf), h);
7500 if (rc == -EREMOTEIO)
7501 sprintf(numbuf[0], "%s*", strbuf);
7503 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7504 "%s" : "[%s]", strbuf);
7506 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7507 if (type == QC_GENERAL)
7508 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7509 "%s" : "[%s]", strbuf);
7511 sprintf(numbuf[1], "%s", "-");
7513 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7514 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7515 "%s" : "[%s]", strbuf);
7518 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7520 printf(" %7s%c %6s %7s %7s",
7521 numbuf[0], bover ? '*' : ' ', numbuf[1],
7522 numbuf[2], bover > 1 ? timebuf : "-");
7525 diff2str(dqb->dqb_itime, timebuf, now);
7526 else if (show_default)
7527 snprintf(timebuf, sizeof(timebuf), "%llu",
7528 (unsigned long long)dqb->dqb_itime);
7530 snprintf(numbuf[0], sizeof(numbuf),
7531 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7532 (uintmax_t)dqb->dqb_curinodes);
7534 if (type == QC_GENERAL)
7535 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7537 (uintmax_t)dqb->dqb_isoftlimit);
7539 sprintf(numbuf[1], "%s", "-");
7541 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7542 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7545 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7546 else if (type != QC_OSTIDX)
7547 printf(" %7s%c %6s %7s %7s",
7548 numbuf[0], iover ? '*' : ' ', numbuf[1],
7549 numbuf[2], iover > 1 ? timebuf : "-");
7551 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7553 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7554 qctl->qc_cmd == Q_GETOINFO) {
7558 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7559 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7561 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7562 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7563 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7565 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7567 printf("Block grace time: %s; Inode grace time: %s\n",
7568 bgtimebuf, igtimebuf);
7572 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7576 /* format is "lustre-OST0001" */
7577 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7579 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7584 *idx = strtoul(dash, &endp, 16);
7585 if (*idx > 0xffff) {
7586 fprintf(stderr, "wrong index %s\n", tgtname);
7593 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7594 bool h, __u64 *total)
7596 int rc = 0, rc1 = 0, count = 0, i = 0;
7597 char **list = NULL, *buffer = NULL;
7598 __u32 valid = qctl->qc_valid;
7600 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7603 /* Is it correct for the case OST0000, OST0002, OST0003 -
7604 * we will ask OST0001 that is absent and won't ask OST0003? */
7605 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7607 fprintf(stderr, "can not get %s count: %s\n",
7608 is_mdt ? "mdt" : "ost", strerror(-rc));
7612 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7613 char fname[PATH_MAX];
7614 char fsname[LUSTRE_MAXFSNAME + 1];
7615 int bufsize = sizeof(struct obd_uuid) * count;
7617 rc = llapi_search_fsname(mnt, fsname);
7619 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7623 buffer = malloc(bufsize + sizeof(*list) * count);
7626 list = (char **)(buffer + bufsize);
7627 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7628 count = llapi_get_poolmembers(fname, list, count,
7634 for (i = 0; i < count; i++) {
7635 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7638 if (tgt_name2index(list[i], &index))
7640 qctl->qc_idx = index;
7645 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7646 rc = llapi_quotactl(mnt, qctl);
7648 /* It is remote client case. */
7649 if (rc == -EOPNOTSUPP) {
7656 fprintf(stderr, "quotactl %s%d failed.\n",
7657 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7661 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7662 qctl->qc_valid, 0, h, false);
7663 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7664 qctl->qc_dqblk.dqb_bhardlimit;
7669 qctl->qc_valid = valid;
7673 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7674 int verbose, int quiet, bool human_readable,
7677 int rc1 = 0, rc2 = 0, rc3 = 0;
7678 char *obd_type = (char *)qctl->obd_type;
7679 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7680 __u64 total_ialloc = 0, total_balloc = 0;
7681 bool use_default_for_blk = false;
7682 bool use_default_for_file = false;
7685 rc1 = llapi_quotactl(mnt, qctl);
7689 fprintf(stderr, "%s quotas are not enabled.\n",
7690 qtype_name(qctl->qc_type));
7693 fprintf(stderr, "Permission denied.\n");
7696 /* We already got error message. */
7699 fprintf(stderr, "Unexpected quotactl error: %s\n",
7704 if (!show_default && qctl->qc_id == 0) {
7705 qctl->qc_dqblk.dqb_bhardlimit = 0;
7706 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7707 qctl->qc_dqblk.dqb_ihardlimit = 0;
7708 qctl->qc_dqblk.dqb_isoftlimit = 0;
7709 qctl->qc_dqblk.dqb_btime = 0;
7710 qctl->qc_dqblk.dqb_itime = 0;
7711 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7714 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7715 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7716 use_default_for_blk = true;
7717 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7720 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7721 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7722 use_default_for_file = true;
7723 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7726 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7727 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7728 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7729 print_quota_title(name, qctl, human_readable, show_default);
7731 if (rc1 && *obd_type)
7732 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7734 if (qctl->qc_valid != QC_GENERAL)
7737 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7738 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7739 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7740 (QIF_LIMITS|QIF_USAGE));
7742 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7744 if (!show_default && verbose &&
7745 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7746 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7747 char strbuf[STRBUF_LEN];
7749 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7751 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7753 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7755 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7756 (uintmax_t)total_ialloc, strbuf);
7759 if (use_default_for_blk)
7760 printf("%cid %u is using default block quota setting\n",
7761 *qtype_name(qctl->qc_type), qctl->qc_id);
7763 if (use_default_for_file)
7764 printf("%cid %u is using default file quota setting\n",
7765 *qtype_name(qctl->qc_type), qctl->qc_id);
7767 if (rc1 || rc2 || rc3 || inacc)
7768 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7782 static int lfs_project(int argc, char **argv)
7784 int ret = 0, err = 0, c, i;
7785 struct project_handle_control phc = { 0 };
7786 enum lfs_project_ops_t op;
7789 phc.assign_projid = false;
7790 /* default action */
7791 op = LFS_PROJECT_LIST;
7793 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7796 if (op != LFS_PROJECT_LIST) {
7798 "%s: cannot specify '-c' '-C' '-s' together\n",
7803 op = LFS_PROJECT_CHECK;
7806 if (op != LFS_PROJECT_LIST) {
7808 "%s: cannot specify '-c' '-C' '-s' together\n",
7813 op = LFS_PROJECT_CLEAR;
7816 if (op != LFS_PROJECT_LIST) {
7818 "%s: cannot specify '-c' '-C' '-s' together\n",
7823 phc.set_inherit = true;
7824 op = LFS_PROJECT_SET;
7830 phc.keep_projid = true;
7833 phc.recursive = true;
7836 if (str2quotaid(&phc.projid, optarg)) {
7838 "Invalid project ID: %s\n",
7843 phc.assign_projid = true;
7847 phc.newline = false;
7850 fprintf(stderr, "%s: invalid option '%c'\n",
7856 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7857 op = LFS_PROJECT_SET;
7858 phc.set_projid = true;
7859 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7860 phc.set_projid = true;
7864 case LFS_PROJECT_CHECK:
7865 if (phc.keep_projid) {
7867 "%s: '-k' is useless together with '-c'\n",
7872 case LFS_PROJECT_CLEAR:
7875 "%s: '-0' is useless together with '-C'\n",
7879 if (phc.assign_projid) {
7881 "%s: '-p' is useless together with '-C'\n",
7886 case LFS_PROJECT_SET:
7889 "%s: '-0' is useless together with '-s'\n",
7893 if (phc.keep_projid) {
7895 "%s: '-k' is useless together with '-s'\n",
7903 "%s: '-0' is useless for list operations\n",
7913 fprintf(stderr, "%s: missing file or directory target(s)\n",
7918 for (i = 0; i < argc; i++) {
7920 case LFS_PROJECT_CHECK:
7921 err = lfs_project_check(argv[i], &phc);
7923 case LFS_PROJECT_LIST:
7924 err = lfs_project_list(argv[i], &phc);
7926 case LFS_PROJECT_CLEAR:
7927 err = lfs_project_clear(argv[i], &phc);
7929 case LFS_PROJECT_SET:
7930 err = lfs_project_set(argv[i], &phc);
7942 static int lfs_quota(int argc, char **argv)
7945 char *mnt, *name = NULL;
7946 struct if_quotactl *qctl;
7948 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7949 __u32 valid = QC_GENERAL, idx = 0;
7950 bool human_readable = false;
7951 bool show_default = false;
7953 struct option long_opts[] = {
7954 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
7957 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7961 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7962 qctl->qc_type = ALLQUOTA;
7963 obd_uuid = (char *)qctl->obd_uuid.uuid;
7965 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7966 long_opts, NULL)) != -1) {
7969 show_default = true;
7974 show_default = true;
7979 show_default = true;
7983 if (qctl->qc_type != ALLQUOTA) {
7985 "%s quota: only one of -u, -g, or -p may be specified\n",
7990 qctl->qc_type = qtype;
7993 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7996 valid = qctl->qc_valid = QC_UUID;
7997 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8000 valid = qctl->qc_valid = QC_MDTIDX;
8001 idx = qctl->qc_idx = atoi(optarg);
8002 if (idx == 0 && *optarg != '0') {
8004 "%s quota: invalid MDT index '%s'\n",
8011 valid = qctl->qc_valid = QC_OSTIDX;
8012 idx = qctl->qc_idx = atoi(optarg);
8013 if (idx == 0 && *optarg != '0') {
8015 "%s quota: invalid OST index '%s'\n",
8028 human_readable = true;
8031 if (lfs_verify_poolarg(optarg)) {
8035 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8036 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
8037 LUSTRE_Q_GETINFOPOOL :
8038 LUSTRE_Q_GETQUOTAPOOL;
8041 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8042 progname, argv[optind - 1]);
8048 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8049 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8050 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8051 qctl->qc_type == ALLQUOTA &&
8052 optind == argc - 1 && !show_default) {
8055 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8056 qctl->qc_type = qtype;
8057 qctl->qc_valid = valid;
8058 if (qtype == USRQUOTA) {
8059 qctl->qc_id = geteuid();
8060 rc = uid2name(&name, qctl->qc_id);
8062 qctl->qc_id = getegid();
8063 rc = gid2name(&name, qctl->qc_id);
8064 memset(&qctl->qc_dqblk, 0,
8065 sizeof(qctl->qc_dqblk));
8070 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8071 human_readable, show_default);
8076 /* lfs quota -u username /path/to/lustre/mount */
8077 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8078 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8079 /* options should be followed by u/g-name and mntpoint */
8080 if ((!show_default && optind + 2 != argc) ||
8081 (show_default && optind + 1 != argc) ||
8082 qctl->qc_type == ALLQUOTA) {
8084 "%s quota: name and mount point must be specified\n",
8090 if (!show_default) {
8091 name = argv[optind++];
8092 switch (qctl->qc_type) {
8094 rc = name2uid(&qctl->qc_id, name);
8097 rc = name2gid(&qctl->qc_id, name);
8100 rc = name2projid(&qctl->qc_id, name);
8107 qctl->qc_valid = QC_GENERAL;
8108 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
8113 if (str2quotaid(&qctl->qc_id, name)) {
8114 fprintf(stderr, "%s quota: invalid id '%s'\n",
8120 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8121 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8128 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8129 human_readable, show_default);
8134 #endif /* HAVE_SYS_QUOTA_H! */
8136 static int flushctx_ioctl(char *mp)
8140 fd = open(mp, O_RDONLY);
8142 fprintf(stderr, "flushctx: error open %s: %s\n",
8143 mp, strerror(errno));
8147 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8149 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8150 mp, strerror(errno));
8156 static int lfs_flushctx(int argc, char **argv)
8158 int kdestroy = 0, reap = 0, c;
8159 char mntdir[PATH_MAX] = {'\0'};
8163 while ((c = getopt(argc, argv, "kr")) != -1) {
8173 "error: %s: option '-%c' unrecognized\n",
8180 rc = system("kdestroy > /dev/null");
8182 rc = WEXITSTATUS(rc);
8184 "error destroying tickets: %d, continuing\n",
8189 if (optind >= argc) {
8190 /* flush for all mounted lustre fs. */
8191 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8192 /* Check if we have a mount point */
8193 if (mntdir[0] == '\0')
8196 if (flushctx_ioctl(mntdir))
8199 mntdir[0] = '\0'; /* avoid matching in next loop */
8202 /* flush fs as specified */
8203 while (optind < argc) {
8204 if (flushctx_ioctl(argv[optind++]))
8210 rc = system("keyctl reap > /dev/null");
8212 rc = WEXITSTATUS(rc);
8213 fprintf(stderr, "error reaping keyring: %d\n", rc);
8220 static int lfs_changelog(int argc, char **argv)
8222 void *changelog_priv;
8223 struct changelog_rec *rec;
8224 long long startrec = 0, endrec = 0;
8226 struct option long_opts[] = {
8227 { .val = 'f', .name = "follow", .has_arg = no_argument },
8229 char short_opts[] = "f";
8232 while ((rc = getopt_long(argc, argv, short_opts,
8233 long_opts, NULL)) != -1) {
8240 "%s changelog: unrecognized option '%s'\n",
8241 progname, argv[optind - 1]);
8245 if (optind >= argc) {
8246 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8251 mdd = argv[optind++];
8252 if (argc > optind) {
8254 startrec = strtoll(argv[optind++], NULL, 10);
8255 if (errno != 0 || startrec < 0) {
8257 "%s changelog: bad startrec\n",
8263 if (argc > optind) {
8265 endrec = strtoll(argv[optind++], NULL, 10);
8266 if (errno != 0 || endrec < 0) {
8268 "%s changelog: bad endrec\n",
8274 rc = llapi_changelog_start(&changelog_priv,
8275 CHANGELOG_FLAG_BLOCK |
8276 CHANGELOG_FLAG_JOBID |
8277 CHANGELOG_FLAG_EXTRA_FLAGS |
8278 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8281 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8282 progname, strerror(errno = -rc));
8286 rc = llapi_changelog_set_xflags(changelog_priv,
8287 CHANGELOG_EXTRA_FLAG_UIDGID |
8288 CHANGELOG_EXTRA_FLAG_NID |
8289 CHANGELOG_EXTRA_FLAG_OMODE |
8290 CHANGELOG_EXTRA_FLAG_XATTR);
8293 "%s changelog: cannot set xflags for changelog: %s\n",
8294 progname, strerror(errno = -rc));
8298 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8302 if (endrec && rec->cr_index > endrec) {
8303 llapi_changelog_free(&rec);
8306 if (rec->cr_index < startrec) {
8307 llapi_changelog_free(&rec);
8311 secs = rec->cr_time >> 30;
8312 gmtime_r(&secs, &ts);
8313 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8314 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8315 changelog_type2str(rec->cr_type),
8316 ts.tm_hour, ts.tm_min, ts.tm_sec,
8317 (int)(rec->cr_time & ((1 << 30) - 1)),
8318 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8319 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8321 if (rec->cr_flags & CLF_JOBID) {
8322 struct changelog_ext_jobid *jid =
8323 changelog_rec_jobid(rec);
8325 if (jid->cr_jobid[0] != '\0')
8326 printf(" j=%s", jid->cr_jobid);
8329 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8330 struct changelog_ext_extra_flags *ef =
8331 changelog_rec_extra_flags(rec);
8333 printf(" ef=0x%llx",
8334 (unsigned long long)ef->cr_extra_flags);
8336 if (ef->cr_extra_flags & CLFE_UIDGID) {
8337 struct changelog_ext_uidgid *uidgid =
8338 changelog_rec_uidgid(rec);
8340 printf(" u=%llu:%llu",
8341 (unsigned long long)uidgid->cr_uid,
8342 (unsigned long long)uidgid->cr_gid);
8344 if (ef->cr_extra_flags & CLFE_NID) {
8345 struct changelog_ext_nid *nid =
8346 changelog_rec_nid(rec);
8349 libcfs_nid2str(nid->cr_nid));
8352 if (ef->cr_extra_flags & CLFE_OPEN) {
8353 struct changelog_ext_openmode *omd =
8354 changelog_rec_openmode(rec);
8355 char mode[] = "---";
8357 /* exec mode must be exclusive */
8358 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8361 if (omd->cr_openflags & MDS_FMODE_READ)
8363 if (omd->cr_openflags &
8370 if (strcmp(mode, "---") != 0)
8371 printf(" m=%s", mode);
8374 if (ef->cr_extra_flags & CLFE_XATTR) {
8375 struct changelog_ext_xattr *xattr =
8376 changelog_rec_xattr(rec);
8378 if (xattr->cr_xattr[0] != '\0')
8379 printf(" x=%s", xattr->cr_xattr);
8383 if (!fid_is_zero(&rec->cr_pfid))
8384 printf(" p="DFID, PFID(&rec->cr_pfid));
8385 if (rec->cr_namelen)
8386 printf(" %.*s", rec->cr_namelen,
8387 changelog_rec_name(rec));
8389 if (rec->cr_flags & CLF_RENAME) {
8390 struct changelog_ext_rename *rnm =
8391 changelog_rec_rename(rec);
8393 if (!fid_is_zero(&rnm->cr_sfid))
8394 printf(" s="DFID" sp="DFID" %.*s",
8395 PFID(&rnm->cr_sfid),
8396 PFID(&rnm->cr_spfid),
8397 (int)changelog_rec_snamelen(rec),
8398 changelog_rec_sname(rec));
8402 llapi_changelog_free(&rec);
8405 llapi_changelog_fini(&changelog_priv);
8408 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8409 progname, strerror(errno = -rc));
8411 return (rc == 1 ? 0 : rc);
8414 static int lfs_changelog_clear(int argc, char **argv)
8423 endrec = strtoll(argv[3], NULL, 10);
8424 if (errno != 0 || endrec < 0) {
8426 "%s: bad endrec '%s'\n",
8431 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8434 fprintf(stderr, "%s: record out of range: %llu\n",
8436 else if (rc == -ENOENT)
8437 fprintf(stderr, "%s: no changelog user: %s\n",
8440 fprintf(stderr, "%s error: %s\n", argv[0],
8449 static void rstripc(char *str, int c)
8451 char *end = str + strlen(str);
8453 for (; str < end && end[-1] == c; --end)
8457 static int lfs_fid2path(int argc, char **argv)
8459 struct option long_opts[] = {
8460 { .val = 'c', .name = "cur", .has_arg = no_argument },
8461 { .val = 'c', .name = "current", .has_arg = no_argument },
8462 { .val = 'c', .name = "print-link", .has_arg = no_argument },
8463 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
8464 { .val = 'l', .name = "link", .has_arg = required_argument },
8466 char short_opts[] = "cfl:pr:";
8467 bool print_link = false;
8468 bool print_fid = false;
8470 char mnt_dir[PATH_MAX] = "";
8472 char *path_or_fsname;
8473 long long recno = -1;
8475 char *endptr = NULL;
8480 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
8490 linkno = strtol(optarg, &endptr, 10);
8491 if (errno != 0 || *endptr != '\0' || linkno < 0) {
8493 "%s fid2path: invalid linkno '%s'\n",
8499 /* recno is something to do with changelogs
8500 * that was never implemented. We just pass it
8501 * through for the MDT to ignore.
8504 recno = strtoll(optarg, &endptr, 10);
8505 if (errno != 0 || *endptr != '\0' || recno < 0) {
8507 "%s fid2path: invalid recno '%s'\n",
8514 "%s fid2path: unrecognized option '%s'\n",
8515 progname, argv[optind - 1]);
8520 if (argc - optind < 2) {
8522 "Usage: %s fid2path FSNAME|ROOT FID...\n",
8527 path_or_fsname = argv[optind];
8529 if (*path_or_fsname == '/') {
8530 print_mnt_dir = true;
8531 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
8533 print_mnt_dir = false;
8534 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
8539 "%s fid2path: cannot resolve mount point for '%s': %s\n",
8540 progname, path_or_fsname, strerror(-rc));
8544 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
8547 "%s fid2path: cannot open mount point for '%s': %s\n",
8548 progname, path_or_fsname, strerror(-rc));
8552 /* Strip trailing slashes from mnt_dir. */
8553 rstripc(mnt_dir + 1, '/');
8555 for (i = optind + 1; i < argc; i++) {
8556 const char *fid_str = argv[i];
8560 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
8563 "%s fid2path: invalid FID '%s'\n",
8571 int linktmp = (linkno >= 0) ? linkno : 0;
8573 int oldtmp = linktmp;
8574 long long rectmp = recno;
8575 char path_buf[PATH_MAX];
8577 rc2 = llapi_fid2path_at(mnt_fd, &fid,
8578 path_buf, sizeof(path_buf), &rectmp, &linktmp);
8581 "%s fid2path: cannot find %s %s: %s\n",
8582 progname, path_or_fsname, fid_str,
8590 printf("%s ", fid_str);
8593 printf("%d ", linktmp);
8595 /* You may think this looks wrong or weird (and it is!)
8596 * but we are actually trying to preserve the old quirky
8597 * behaviors (enforced by our old quirky tests!) that
8598 * make lfs so much fun to work on:
8600 * lustre 0x200000007:0x1:0x0 => "/"
8601 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
8603 * Note that llapi_fid2path() returns "" for the root
8607 print_mnt_dir ? mnt_dir : "",
8608 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
8612 /* specified linkno */
8615 if (oldtmp == linktmp)
8627 static int lfs_path2fid(int argc, char **argv)
8629 struct option long_opts[] = {
8630 { .val = 'p', .name = "parents", .has_arg = no_argument },
8633 const char short_opts[] = "p";
8634 const char *sep = "";
8637 bool show_parents = false;
8639 while ((rc = getopt_long(argc, argv, short_opts,
8640 long_opts, NULL)) != -1) {
8643 show_parents = true;
8647 "%s path2fid: unrecognized option '%s'\n",
8648 progname, argv[optind - 1]);
8653 if (optind > argc - 1) {
8654 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8657 } else if (optind < argc - 1) {
8662 for (path = argv + optind; *path != NULL; path++) {
8665 if (!show_parents) {
8666 err = llapi_path2fid(*path, &fid);
8668 printf("%s%s"DFID"\n",
8669 *sep != '\0' ? *path : "", sep,
8672 char name[NAME_MAX + 1];
8673 unsigned int linkno = 0;
8675 while ((err = llapi_path2parent(*path, linkno, &fid,
8676 name, sizeof(name))) == 0) {
8677 if (*sep != '\0' && linkno == 0)
8678 printf("%s%s", *path, sep);
8680 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8685 /* err == -ENODATA is end-of-loop */
8686 if (linkno > 0 && err == -ENODATA) {
8694 "%s path2fid: cannot get %sfid for '%s': %s\n",
8695 progname, show_parents ? "parent " : "", *path,
8707 #define MAX_ERRNO 4095
8708 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8710 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8714 rc = llapi_rmfid(device, fa);
8716 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
8717 progname, strerror(-rc));
8721 for (k = 0; k < fa->fa_nr; k++) {
8722 rc2 = (__s32)fa->fa_fids[k].f_ver;
8723 if (!IS_ERR_VALUE(rc2))
8729 fa->fa_fids[k].f_ver = 0;
8730 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
8731 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
8737 static int lfs_rmfid(int argc, char **argv)
8739 char *fidstr, *device;
8740 int rc = 0, rc2, nr;
8741 struct fid_array *fa;
8743 if (optind > argc - 1) {
8744 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8748 device = argv[optind++];
8751 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8757 while (optind < argc) {
8760 fidstr = argv[optind++];
8761 while (*fidstr == '[')
8763 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8765 fprintf(stderr, "unrecognized FID: %s\n",
8770 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8771 /* start another batch */
8772 rc2 = lfs_rmfid_and_show_errors(device, fa);
8779 rc2 = lfs_rmfid_and_show_errors(device, fa);
8787 static int lfs_data_version(int argc, char **argv)
8794 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8797 fprintf(stderr, "%s data_version: FILE must be specified\n",
8802 while ((c = getopt(argc, argv, "nrw")) != -1) {
8805 data_version_flags = 0;
8808 data_version_flags |= LL_DV_RD_FLUSH;
8811 data_version_flags |= LL_DV_WR_FLUSH;
8815 "%s data_version: unrecognized option '%s'\n",
8816 progname, argv[optind - 1]);
8820 if (optind == argc) {
8821 fprintf(stderr, "%s data_version: FILE must be specified\n",
8826 path = argv[optind];
8827 fd = open(path, O_RDONLY);
8830 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8831 progname, path, strerror(-rc));
8835 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8838 "%s data_version: cannot get version for '%s': %s\n",
8839 progname, path, strerror(-rc));
8841 printf("%ju" "\n", (uintmax_t)data_version);
8847 static int lfs_hsm_state(int argc, char **argv)
8852 struct hsm_user_state hus;
8860 rc = llapi_hsm_state_get(path, &hus);
8862 fprintf(stderr, "can't get hsm state for %s: %s\n",
8863 path, strerror(errno = -rc));
8867 /* Display path name and status flags */
8868 printf("%s: (0x%08x)", path, hus.hus_states);
8870 if (hus.hus_states & HS_RELEASED)
8871 printf(" released");
8872 if (hus.hus_states & HS_EXISTS)
8874 if (hus.hus_states & HS_DIRTY)
8876 if (hus.hus_states & HS_ARCHIVED)
8877 printf(" archived");
8878 /* Display user-settable flags */
8879 if (hus.hus_states & HS_NORELEASE)
8880 printf(" never_release");
8881 if (hus.hus_states & HS_NOARCHIVE)
8882 printf(" never_archive");
8883 if (hus.hus_states & HS_LOST)
8884 printf(" lost_from_hsm");
8886 if (hus.hus_archive_id != 0)
8887 printf(", archive_id:%d", hus.hus_archive_id);
8890 } while (++i < argc);
8895 #define LFS_HSM_SET 0
8896 #define LFS_HSM_CLEAR 1
8899 * Generic function to set or clear HSM flags.
8900 * Used by hsm_set and hsm_clear.
8902 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8904 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8906 struct option long_opts[] = {
8907 { .val = 'A', .name = "archived", .has_arg = no_argument },
8908 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8909 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8910 { .val = 'e', .name = "exists", .has_arg = no_argument },
8911 { .val = 'l', .name = "lost", .has_arg = no_argument },
8912 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8913 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8915 char short_opts[] = "lraAdei:";
8919 __u32 archive_id = 0;
8925 while ((c = getopt_long(argc, argv, short_opts,
8926 long_opts, NULL)) != -1) {
8932 mask |= HS_NOARCHIVE;
8935 mask |= HS_ARCHIVED;
8938 mask |= HS_NORELEASE;
8948 archive_id = strtol(optarg, &end, 10);
8949 if (errno != 0 || *end != '\0' || archive_id < 0) {
8950 fprintf(stderr, "invalid archive_id: '%s'\n",
8958 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8959 argv[0], argv[optind - 1]);
8964 /* User should have specified a flag */
8968 while (optind < argc) {
8969 path = argv[optind];
8971 /* If mode == 0, this means we apply the mask. */
8972 if (mode == LFS_HSM_SET)
8973 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8975 rc = llapi_hsm_state_set(path, 0, mask, 0);
8978 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8979 path, strerror(errno = -rc));
8988 static int lfs_hsm_action(int argc, char **argv)
8993 struct hsm_current_action hca;
8994 struct hsm_extent he;
8995 enum hsm_user_action hua;
8996 enum hsm_progress_states hps;
9004 rc = llapi_hsm_current_action(path, &hca);
9006 fprintf(stderr, "can't get hsm action for %s: %s\n",
9007 path, strerror(errno = -rc));
9010 he = hca.hca_location;
9011 hua = hca.hca_action;
9012 hps = hca.hca_state;
9014 printf("%s: %s", path, hsm_user_action2name(hua));
9016 /* Skip file without action */
9017 if (hca.hca_action == HUA_NONE) {
9022 printf(" %s ", hsm_progress_state2name(hps));
9024 if ((hps == HPS_RUNNING) &&
9025 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9026 printf("(%llu bytes moved)\n",
9027 (unsigned long long)he.length);
9028 else if ((he.offset + he.length) == LUSTRE_EOF)
9029 printf("(from %llu to EOF)\n",
9030 (unsigned long long)he.offset);
9032 printf("(from %llu to %llu)\n",
9033 (unsigned long long)he.offset,
9034 (unsigned long long)(he.offset + he.length));
9036 } while (++i < argc);
9041 static int lfs_hsm_set(int argc, char **argv)
9043 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9046 static int lfs_hsm_clear(int argc, char **argv)
9048 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9052 * Check file state and return its fid, to be used by lfs_hsm_request().
9054 * \param[in] file Path to file to check
9055 * \param[in,out] fid Pointer to allocated lu_fid struct.
9056 * \param[in,out] last_dev Pointer to last device id used.
9058 * \return 0 on success.
9060 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9066 rc = lstat(file, &st);
9068 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9072 * Checking for regular file as archiving as posix copytool
9073 * rejects archiving files other than regular files
9075 if (!S_ISREG(st.st_mode)) {
9076 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9079 /* A request should be ... */
9080 if (*last_dev != st.st_dev && *last_dev != 0) {
9082 "All files should be on the same filesystem: %s\n",
9086 *last_dev = st.st_dev;
9088 rc = llapi_path2fid(file, fid);
9090 fprintf(stderr, "Cannot read FID of %s: %s\n",
9091 file, strerror(-rc));
9097 /* Fill an HSM HUR item with a given file name.
9099 * If mntpath is set, then the filename is actually a FID, and no
9100 * lookup on the filesystem will be performed.
9102 * \param[in] hur the user request to fill
9103 * \param[in] idx index of the item inside the HUR to fill
9104 * \param[in] mntpath mountpoint of Lustre
9105 * \param[in] fname filename (if mtnpath is NULL)
9106 * or FID (if mntpath is set)
9107 * \param[in] last_dev pointer to last device id used
9109 * \retval 0 on success
9110 * \retval CMD_HELP or a negative errno on error
9112 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9113 const char *mntpath, const char *fname,
9116 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9119 hui->hui_extent.length = -1;
9122 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9124 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9127 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9131 hur->hur_request.hr_itemcount++;
9136 static int lfs_hsm_request(int argc, char **argv, int action)
9138 struct option long_opts[] = {
9139 { .val = 'a', .name = "archive", .has_arg = required_argument },
9140 { .val = 'D', .name = "data", .has_arg = required_argument },
9141 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9142 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9145 char short_opts[] = "l:D:a:m:";
9146 struct hsm_user_request *hur, *oldhur;
9151 char *filelist = NULL;
9152 char fullpath[PATH_MAX];
9153 char *opaque = NULL;
9157 int nbfile_alloc = 0;
9158 char *some_file = NULL;
9159 char *mntpath = NULL;
9165 while ((c = getopt_long(argc, argv, short_opts,
9166 long_opts, NULL)) != -1) {
9175 if (action != HUA_ARCHIVE &&
9176 action != HUA_REMOVE) {
9178 "error: -a is supported only when archiving or removing\n");
9181 archive_id = atoi(optarg);
9186 some_file = strdup(optarg);
9192 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9193 argv[0], argv[optind - 1]);
9198 /* All remaining args are files, so we have at least nbfile */
9199 nbfile = argc - optind;
9201 if ((nbfile == 0) && (!filelist))
9205 opaque_len = strlen(opaque);
9208 * Alloc the request structure with enough place to store all files
9209 * from command line.
9211 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9213 fprintf(stderr, "Cannot create the request: %s\n",
9217 nbfile_alloc = nbfile;
9219 hur->hur_request.hr_action = action;
9220 hur->hur_request.hr_archive_id = archive_id;
9221 hur->hur_request.hr_flags = 0;
9223 /* All remaining args are files, add them */
9224 if (nbfile != 0 && some_file == NULL)
9225 some_file = strdup(argv[optind]);
9227 for (i = 0; i < nbfile; i++) {
9228 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9234 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9236 /* If a filelist was specified, read the filelist from it. */
9238 fp = fopen(filelist, "r");
9240 fprintf(stderr, "Cannot read the file list %s: %s\n",
9241 filelist, strerror(errno));
9246 while ((rc = getline(&line, &len, fp)) != -1) {
9248 * If allocated buffer was too small, get something
9251 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9254 nbfile_alloc = nbfile_alloc * 2 + 1;
9256 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9260 "hsm: cannot allocate the request: %s\n",
9267 size = hur_len(oldhur);
9270 "hsm: cannot allocate %u files + %u bytes data\n",
9271 oldhur->hur_request.hr_itemcount,
9272 oldhur->hur_request.hr_data_len);
9279 memcpy(hur, oldhur, size);
9284 if (line[strlen(line) - 1] == '\n')
9285 line[strlen(line) - 1] = '\0';
9287 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9288 mntpath, line, &last_dev);
9304 /* If a --data was used, add it to the request */
9305 hur->hur_request.hr_data_len = opaque_len;
9307 memcpy(hur_data(hur), opaque, opaque_len);
9309 /* Send the HSM request */
9310 if (realpath(some_file, fullpath) == NULL) {
9311 fprintf(stderr, "Could not find path '%s': %s\n",
9312 some_file, strerror(errno));
9314 rc = llapi_hsm_request(fullpath, hur);
9316 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9317 some_file, strerror(-rc));
9327 static int lfs_hsm_archive(int argc, char **argv)
9329 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9332 static int lfs_hsm_restore(int argc, char **argv)
9334 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9337 static int lfs_hsm_release(int argc, char **argv)
9339 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9342 static int lfs_hsm_remove(int argc, char **argv)
9344 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9347 static int lfs_hsm_cancel(int argc, char **argv)
9349 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9352 static int lfs_swap_layouts(int argc, char **argv)
9357 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9358 SWAP_LAYOUTS_KEEP_MTIME |
9359 SWAP_LAYOUTS_KEEP_ATIME);
9362 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9364 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9366 int lfs_get_mode(const char *string)
9368 enum lock_mode_user mode;
9370 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9371 if (lock_mode_names[mode] == NULL)
9373 if (strcmp(string, lock_mode_names[mode]) == 0)
9380 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9382 enum lu_ladvise_type advice;
9385 advice < ARRAY_SIZE(ladvise_names); advice++) {
9386 if (ladvise_names[advice] == NULL)
9388 if (strcmp(string, ladvise_names[advice]) == 0)
9392 return LU_LADVISE_INVALID;
9395 static int lfs_ladvise(int argc, char **argv)
9397 struct option long_opts[] = {
9398 { .val = 'a', .name = "advice", .has_arg = required_argument },
9399 { .val = 'b', .name = "background", .has_arg = no_argument },
9400 { .val = 'e', .name = "end", .has_arg = required_argument },
9401 { .val = 'l', .name = "length", .has_arg = required_argument },
9402 { .val = 'm', .name = "mode", .has_arg = required_argument },
9403 { .val = 's', .name = "start", .has_arg = required_argument },
9404 { .val = 'u', .name = "unset", .has_arg = no_argument },
9406 char short_opts[] = "a:be:l:m:s:u";
9411 struct llapi_lu_ladvise advice;
9412 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9413 unsigned long long start = 0;
9414 unsigned long long end = LUSTRE_EOF;
9415 unsigned long long length = 0;
9416 unsigned long long size_units;
9417 unsigned long long flags = 0;
9421 while ((c = getopt_long(argc, argv, short_opts,
9422 long_opts, NULL)) != -1) {
9425 advice_type = lfs_get_ladvice(optarg);
9426 if (advice_type == LU_LADVISE_INVALID) {
9428 "%s: invalid advice type '%s'\n",
9430 fprintf(stderr, "Valid types:");
9432 for (advice_type = 0;
9433 advice_type < ARRAY_SIZE(ladvise_names);
9435 if (ladvise_names[advice_type] == NULL)
9437 fprintf(stderr, " %s",
9438 ladvise_names[advice_type]);
9440 fprintf(stderr, "\n");
9453 rc = llapi_parse_size(optarg, &end,
9456 fprintf(stderr, "%s: bad end offset '%s'\n",
9463 rc = llapi_parse_size(optarg, &start,
9467 "%s: bad start offset '%s'\n",
9474 rc = llapi_parse_size(optarg, &length,
9477 fprintf(stderr, "%s: bad length '%s'\n",
9483 mode = lfs_get_mode(optarg);
9486 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9494 fprintf(stderr, "%s: option '%s' unrecognized\n",
9495 argv[0], argv[optind - 1]);
9500 if (advice_type == LU_LADVISE_INVALID) {
9501 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9502 fprintf(stderr, "Valid types:");
9503 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9505 if (ladvise_names[advice_type] == NULL)
9507 fprintf(stderr, " %s", ladvise_names[advice_type]);
9509 fprintf(stderr, "\n");
9513 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9515 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9520 if (argc <= optind) {
9521 fprintf(stderr, "%s: please give one or more file names\n",
9526 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9527 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9532 if (end == LUSTRE_EOF && length != 0)
9533 end = start + length;
9536 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9537 argv[0], start, end);
9541 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9542 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9547 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9548 fprintf(stderr, "%s: mode is required with lockahead\n",
9553 while (optind < argc) {
9556 path = argv[optind++];
9558 fd = open(path, O_RDONLY);
9560 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9561 argv[0], path, strerror(errno));
9566 advice.lla_start = start;
9567 advice.lla_end = end;
9568 advice.lla_advice = advice_type;
9569 advice.lla_value1 = 0;
9570 advice.lla_value2 = 0;
9571 advice.lla_value3 = 0;
9572 advice.lla_value4 = 0;
9573 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9574 advice.lla_lockahead_mode = mode;
9575 advice.lla_peradvice_flags = flags;
9578 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9582 "%s: cannot give advice '%s' to file '%s': %s\n",
9583 argv[0], ladvise_names[advice_type],
9584 path, strerror(errno));
9590 if (rc == 0 && rc2 < 0)
9596 static const char *const heat_names[] = LU_HEAT_NAMES;
9598 static int lfs_heat_get(int argc, char **argv)
9600 struct lu_heat *heat;
9609 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9611 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9616 while (optind < argc) {
9617 path = argv[optind++];
9619 fd = open(path, O_RDONLY);
9621 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9622 argv[0], path, strerror(errno));
9627 heat->lh_count = OBD_HEAT_COUNT;
9628 rc2 = llapi_heat_get(fd, heat);
9632 "%s: cannot get heat of file '%s': %s\n",
9633 argv[0], path, strerror(errno));
9637 printf("flags: %x\n", heat->lh_flags);
9638 for (i = 0; i < heat->lh_count; i++)
9639 printf("%s: %llu\n", heat_names[i],
9640 (unsigned long long)heat->lh_heat[i]);
9642 if (rc == 0 && rc2 < 0)
9650 static int lfs_heat_set(int argc, char **argv)
9652 struct option long_opts[] = {
9653 { .val = 'c', .name = "clear", .has_arg = no_argument },
9654 { .val = 'o', .name = "off", .has_arg = no_argument },
9655 { .val = 'O', .name = "on", .has_arg = no_argument },
9657 enum lu_heat_flag flags = 0;
9667 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9670 flags |= LU_HEAT_FLAG_CLEAR;
9673 flags |= LU_HEAT_FLAG_CLEAR;
9674 flags |= LU_HEAT_FLAG_OFF;
9677 flags &= ~LU_HEAT_FLAG_OFF;
9682 fprintf(stderr, "%s: option '%s' unrecognized\n",
9683 argv[0], argv[optind - 1]);
9688 if (argc <= optind) {
9689 fprintf(stderr, "%s: please give one or more file names\n",
9694 while (optind < argc) {
9695 path = argv[optind++];
9697 fd = open(path, O_RDONLY);
9699 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9700 argv[0], path, strerror(errno));
9705 rc2 = llapi_heat_set(fd, flags);
9709 "%s: cannot setflags heat of file '%s': %s\n",
9710 argv[0], path, strerror(errno));
9714 if (rc == 0 && rc2 < 0)
9721 * The input string contains a comma delimited list of component ids and
9722 * ranges, for example "1,2-4,7".
9724 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9726 bool end_of_loop = false;
9734 while (!end_of_loop) {
9738 char *endptr = NULL;
9741 ptr = strchrnul(arg, ',');
9742 end_of_loop = *ptr == '\0';
9745 start_index = strtol(arg, &endptr, 0);
9746 if (endptr == arg) /* no data at all */
9748 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9750 if (start_index < 0)
9753 end_index = start_index;
9754 if (*endptr == '-') {
9755 end_index = strtol(endptr + 1, &endptr, 0);
9756 if (*endptr != '\0')
9758 if (end_index < start_index)
9762 for (i = start_index; i <= end_index && size > 0; i++) {
9765 /* remove duplicate */
9766 for (j = 0; j < nr; j++) {
9770 if (j == nr) { /* no duplicate */
9776 if (size == 0 && i < end_index)
9783 if (!end_of_loop && ptr)
9786 return rc < 0 ? rc : nr;
9790 * struct verify_mirror_id - Mirror id to be verified.
9791 * @mirror_id: A specified mirror id.
9792 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9794 struct verify_mirror_id {
9800 * compare_mirror_ids() - Compare mirror ids.
9801 * @layout: Mirror component list.
9802 * @cbdata: Callback data in verify_mirror_id structure.
9804 * This is a callback function called by llapi_layout_comp_iterate()
9805 * to compare the specified mirror id with the one in the current
9806 * component of @layout. If they are the same, then the specified
9807 * mirror id is valid.
9809 * Return: a negative error code on failure or
9810 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9811 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9814 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9816 struct verify_mirror_id *mirror_id_cbdata =
9817 (struct verify_mirror_id *)cbdata;
9821 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9825 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9826 progname, strerror(errno));
9830 if (mirror_id_cbdata->mirror_id == mirror_id) {
9831 mirror_id_cbdata->is_valid_id = true;
9832 return LLAPI_LAYOUT_ITER_STOP;
9835 return LLAPI_LAYOUT_ITER_CONT;
9839 * verify_mirror_ids() - Verify specified mirror ids.
9840 * @fname: Mirrored file name.
9841 * @mirror_ids: Specified mirror ids to be verified.
9842 * @ids_nr: Number of specified mirror ids.
9844 * This function verifies that specified @mirror_ids are valid
9845 * in the mirrored file @fname.
9847 * Return: 0 on success or a negative error code on failure.
9850 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9852 struct llapi_layout *layout = NULL;
9853 struct verify_mirror_id mirror_id_cbdata = { 0 };
9864 if (stat(fname, &stbuf) < 0) {
9865 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9866 progname, fname, strerror(errno));
9871 if (!S_ISREG(stbuf.st_mode)) {
9872 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9878 fd = open(fname, O_DIRECT | O_RDONLY);
9880 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9881 progname, fname, strerror(errno));
9886 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9888 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9889 progname, fname, strerror(errno));
9893 layout = llapi_layout_get_by_fd(fd, 0);
9895 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9896 progname, fname, strerror(errno));
9898 llapi_lease_release(fd);
9902 rc = llapi_layout_flags_get(layout, &flr_state);
9904 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9905 progname, fname, strerror(errno));
9910 flr_state &= LCM_FL_FLR_MASK;
9911 switch (flr_state) {
9914 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9915 progname, fname, llapi_layout_flags_string(flr_state));
9922 for (i = 0; i < ids_nr; i++) {
9923 mirror_id_cbdata.mirror_id = mirror_ids[i];
9924 mirror_id_cbdata.is_valid_id = false;
9926 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9931 "%s: '%s' failed to verify mirror id: %u.\n",
9932 progname, fname, mirror_ids[i]);
9936 if (!mirror_id_cbdata.is_valid_id) {
9939 "%s: '%s' invalid specified mirror id: %u.\n",
9940 progname, fname, mirror_ids[i]);
9946 llapi_layout_free(layout);
9947 llapi_lease_release(fd);
9955 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9956 __u16 *mirror_ids, int ids_nr)
9958 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9959 struct llapi_layout *layout;
9970 if (stat(fname, &stbuf) < 0) {
9971 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9972 progname, fname, strerror(errno));
9976 if (!S_ISREG(stbuf.st_mode)) {
9977 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9983 fd = open(fname, O_DIRECT | O_RDWR);
9985 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9986 progname, fname, strerror(errno));
9991 layout = llapi_layout_get_by_fd(fd, 0);
9993 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9994 progname, fname, strerror(errno));
9999 rc = llapi_layout_flags_get(layout, &flr_state);
10001 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10002 progname, fname, strerror(errno));
10007 flr_state &= LCM_FL_FLR_MASK;
10008 if (flr_state == LCM_FL_NONE) {
10010 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10015 /* get stale component info */
10016 comp_size = llapi_mirror_find_stale(layout, comp_array,
10017 ARRAY_SIZE(comp_array),
10018 mirror_ids, ids_nr);
10019 if (comp_size <= 0) {
10024 ioc->lil_mode = LL_LEASE_WRLCK;
10025 ioc->lil_flags = LL_LEASE_RESYNC;
10026 rc = llapi_lease_set(fd, ioc);
10028 if (rc == -EALREADY)
10032 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10033 progname, fname, strerror(-rc));
10037 /* get the read range [start, end) */
10038 start = comp_array[0].lrc_start;
10039 end = comp_array[0].lrc_end;
10040 for (idx = 1; idx < comp_size; idx++) {
10041 if (comp_array[idx].lrc_start < start)
10042 start = comp_array[idx].lrc_start;
10043 if (end < comp_array[idx].lrc_end)
10044 end = comp_array[idx].lrc_end;
10047 rc = llapi_lease_check(fd);
10048 if (rc != LL_LEASE_WRLCK) {
10049 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10054 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10057 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10058 progname, fname, strerror(-rc));
10060 /* need to do the lease unlock even resync fails */
10061 ioc->lil_mode = LL_LEASE_UNLCK;
10062 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10063 ioc->lil_count = 0;
10064 for (idx = 0; idx < comp_size; idx++) {
10065 if (comp_array[idx].lrc_synced) {
10066 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10071 rc2 = llapi_lease_set(fd, ioc);
10073 * llapi_lease_set returns lease mode when it request to unlock
10077 /* rc2 == 0 means lost lease lock */
10078 if (rc2 == 0 && rc == 0)
10080 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10082 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10086 llapi_layout_free(layout);
10093 static inline int lfs_mirror_resync(int argc, char **argv)
10095 struct ll_ioc_lease *ioc = NULL;
10096 __u16 mirror_ids[128] = { 0 };
10101 struct option long_opts[] = {
10102 { .val = 'o', .name = "only", .has_arg = required_argument },
10103 { .name = NULL } };
10105 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10108 rc = parse_mirror_ids(mirror_ids,
10109 sizeof(mirror_ids) / sizeof(__u16),
10113 "%s: bad mirror ids '%s'.\n",
10120 fprintf(stderr, "%s: options '%s' unrecognized.\n",
10121 argv[0], argv[optind - 1]);
10127 if (argc == optind) {
10128 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10133 if (ids_nr > 0 && argc > optind + 1) {
10135 "%s: option '--only' cannot be used upon multiple files.\n",
10142 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10147 /* set the lease on the file */
10148 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10150 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10151 argv[0], strerror(errno));
10156 for (; optind < argc; optind++) {
10157 rc = lfs_mirror_resync_file(argv[optind], ioc,
10158 mirror_ids, ids_nr);
10159 /* ignore previous file's error, continue with next file */
10162 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10170 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10172 struct llapi_layout *layout;
10175 layout = llapi_layout_get_by_fd(fd, 0);
10177 fprintf(stderr, "could not get layout.\n");
10181 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10183 fprintf(stderr, "failed to iterate layout\n");
10184 llapi_layout_free(layout);
10187 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10188 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10189 llapi_layout_free(layout);
10193 llapi_layout_free(layout);
10199 * Check whether two files are the same file
10200 * \retval 0 same file
10201 * \retval 1 not the same file
10202 * \retval <0 error code
10204 static inline int check_same_file(int fd, const char *f2)
10206 struct stat stbuf1;
10207 struct stat stbuf2;
10209 if (fstat(fd, &stbuf1) < 0)
10212 if (stat(f2, &stbuf2) < 0)
10215 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10216 stbuf1.st_ino == stbuf2.st_ino)
10222 static inline int lfs_mirror_read(int argc, char **argv)
10225 __u16 mirror_id = 0;
10226 const char *outfile = NULL;
10232 const size_t buflen = 4 << 20;
10234 struct option long_opts[] = {
10235 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10236 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10237 { .name = NULL } };
10239 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10244 unsigned long int id;
10247 id = strtoul(optarg, &end, 0);
10248 if (errno != 0 || *end != '\0' || id == 0 ||
10251 "%s %s: invalid mirror ID '%s'\n",
10252 progname, argv[0], optarg);
10256 mirror_id = (__u16)id;
10263 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10264 progname, argv[optind - 1]);
10269 if (argc == optind) {
10270 fprintf(stderr, "%s %s: no mirrored file provided\n",
10271 progname, argv[0]);
10273 } else if (argc > optind + 1) {
10274 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10278 if (mirror_id == 0) {
10279 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10280 progname, argv[0]);
10284 /* open mirror file */
10285 fname = argv[optind];
10286 fd = open(fname, O_DIRECT | O_RDONLY);
10288 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10289 progname, argv[0], fname, strerror(errno));
10293 /* verify mirror id */
10294 rc = verify_mirror_id_by_fd(fd, mirror_id);
10297 "%s %s: cannot find mirror with ID %u in '%s'\n",
10298 progname, argv[0], mirror_id, fname);
10302 /* open output file - O_EXCL ensures output is not the same as input */
10304 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10306 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10307 progname, argv[0], outfile, strerror(errno));
10312 outfd = STDOUT_FILENO;
10315 /* allocate buffer */
10316 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10318 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10319 progname, argv[0], rc);
10325 ssize_t bytes_read;
10326 ssize_t written = 0;
10328 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10329 if (bytes_read < 0) {
10332 "%s %s: fail to read data from mirror %u: %s\n",
10333 progname, argv[0], mirror_id, strerror(-rc));
10338 if (bytes_read == 0)
10341 while (written < bytes_read) {
10344 written2 = write(outfd, buf + written,
10345 bytes_read - written);
10346 if (written2 < 0) {
10348 "%s %s: fail to write %s: %s\n",
10349 progname, argv[0], outfile ? : "STDOUT",
10354 written += written2;
10357 if (written != bytes_read) {
10359 "%s %s: written %ld bytes does not match with %ld read.\n",
10360 progname, argv[0], written, bytes_read);
10382 static inline int lfs_mirror_write(int argc, char **argv)
10385 __u16 mirror_id = 0;
10386 const char *inputfile = NULL;
10392 const size_t buflen = 4 << 20;
10394 size_t page_size = sysconf(_SC_PAGESIZE);
10395 struct ll_ioc_lease_id ioc;
10397 struct option long_opts[] = {
10398 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10399 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10400 { .name = NULL } };
10402 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10407 unsigned long int id;
10410 id = strtoul(optarg, &end, 0);
10411 if (errno != 0 || *end != '\0' || id == 0 ||
10414 "%s %s: invalid mirror ID '%s'\n",
10415 progname, argv[0], optarg);
10419 mirror_id = (__u16)id;
10423 inputfile = optarg;
10426 fprintf(stderr, "%s: option '%s' unrecognized\n",
10427 progname, argv[optind - 1]);
10432 if (argc == optind) {
10433 fprintf(stderr, "%s %s: no mirrored file provided\n",
10434 progname, argv[0]);
10436 } else if (argc > optind + 1) {
10437 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10441 if (mirror_id == 0) {
10442 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10443 progname, argv[0]);
10447 /* open mirror file */
10448 fname = argv[optind];
10449 fd = open(fname, O_DIRECT | O_WRONLY);
10451 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10452 progname, argv[0], fname, strerror(errno));
10456 /* verify mirror id */
10457 rc = verify_mirror_id_by_fd(fd, mirror_id);
10460 "%s %s: cannot find mirror with ID %u in '%s'\n",
10461 progname, argv[0], mirror_id, fname);
10465 /* open input file */
10467 rc = check_same_file(fd, inputfile);
10470 "%s %s: input file cannot be the mirrored file\n",
10471 progname, argv[0]);
10477 inputfd = open(inputfile, O_RDONLY, 0644);
10479 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10480 progname, argv[0], inputfile, strerror(errno));
10485 inputfd = STDIN_FILENO;
10488 /* allocate buffer */
10489 rc = posix_memalign(&buf, page_size, buflen);
10491 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10492 progname, argv[0], rc);
10493 goto close_inputfd;
10496 /* prepare target mirror components instantiation */
10497 ioc.lil_mode = LL_LEASE_WRLCK;
10498 ioc.lil_flags = LL_LEASE_RESYNC;
10499 ioc.lil_mirror_id = mirror_id;
10500 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10503 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10504 progname, argv[0], fname, strerror(errno));
10510 ssize_t bytes_read;
10514 rc = llapi_lease_check(fd);
10515 if (rc != LL_LEASE_WRLCK) {
10516 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10517 progname, argv[0], fname);
10521 bytes_read = read(inputfd, buf, buflen);
10522 if (bytes_read < 0) {
10525 "%s %s: fail to read data from '%s': %s\n",
10526 progname, argv[0], inputfile ? : "STDIN",
10533 if (bytes_read == 0)
10536 /* round up to page align to make direct IO happy. */
10537 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10539 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10544 "%s %s: fail to write to mirror %u: %s\n",
10545 progname, argv[0], mirror_id,
10553 if (pos & (page_size - 1)) {
10554 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10559 ioc.lil_mode = LL_LEASE_UNLCK;
10560 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10562 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10567 "%s %s: release lease lock of '%s' failed: %s\n",
10568 progname, argv[0], fname, strerror(errno));
10585 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10587 struct llapi_layout *layout;
10588 struct collect_ids_data cid = { .cid_ids = ids,
10590 .cid_exclude = exclude_id, };
10593 layout = llapi_layout_get_by_fd(fd, 0);
10595 fprintf(stderr, "could not get layout\n");
10599 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10601 fprintf(stderr, "failed to iterate layout\n");
10602 llapi_layout_free(layout);
10606 llapi_layout_free(layout);
10608 return cid.cid_count;
10611 static inline int lfs_mirror_copy(int argc, char **argv)
10614 __u16 read_mirror_id = 0;
10615 __u16 ids[128] = { 0 };
10617 struct llapi_layout *layout = NULL;
10618 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10625 struct ll_ioc_lease *ioc = NULL;
10626 struct ll_ioc_lease_id *resync_ioc;
10628 struct option long_opts[] = {
10629 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10630 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10631 { .name = NULL } };
10633 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10638 unsigned long int id;
10641 id = strtoul(optarg, &end, 0);
10642 if (errno != 0 || *end != '\0' || id == 0 ||
10645 "%s %s: invalid read mirror ID '%s'\n",
10646 progname, argv[0], optarg);
10650 read_mirror_id = (__u16)id;
10654 if (!strcmp(optarg, "-1")) {
10655 /* specify all other mirrors */
10656 ids[0] = (__u16)-1;
10659 count = parse_mirror_ids((__u16 *)ids,
10667 fprintf(stderr, "%s: option '%s' unrecognized\n",
10668 progname, argv[optind - 1]);
10673 if (argc == optind) {
10674 fprintf(stderr, "%s %s: no mirrored file provided\n",
10675 progname, argv[0]);
10677 } else if (argc > optind + 1) {
10678 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10682 if (read_mirror_id == 0) {
10684 "%s %s: no valid read mirror ID %d is provided\n",
10685 progname, argv[0], read_mirror_id);
10691 "%s %s: no write mirror ID is provided\n",
10692 progname, argv[0]);
10696 for (i = 0; i < count; i++) {
10697 if (read_mirror_id == ids[i]) {
10699 "%s %s: read and write mirror ID cannot be the same\n",
10700 progname, argv[0]);
10705 /* open mirror file */
10706 fname = argv[optind];
10708 fd = open(fname, O_DIRECT | O_RDWR);
10710 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10711 progname, argv[0], fname, strerror(errno));
10715 /* write to all other mirrors */
10716 if (ids[0] == (__u16)-1) {
10717 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10721 "%s %s: failed to get other mirror ids in '%s': %d\n",
10722 progname, argv[0], fname, rc);
10727 /* verify mirror id */
10728 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10731 "%s %s: cannot find mirror with ID %u in '%s'\n",
10732 progname, argv[0], read_mirror_id, fname);
10736 for (i = 0; i < count; i++) {
10737 rc = verify_mirror_id_by_fd(fd, ids[i]);
10740 "%s %s: cannot find mirror with ID %u in '%s'\n",
10741 progname, argv[0], ids[i], fname);
10746 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10749 "%s %s: cannot alloc comp id array for ioc: %s\n",
10750 progname, argv[0], strerror(errno));
10755 /* get stale component info */
10756 layout = llapi_layout_get_by_fd(fd, 0);
10758 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10759 progname, argv[0], fname, strerror(errno));
10763 comp_size = llapi_mirror_find_stale(layout, comp_array,
10764 ARRAY_SIZE(comp_array),
10766 llapi_layout_free(layout);
10767 if (comp_size < 0) {
10772 /* prepare target mirror components instantiation */
10773 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10774 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10775 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10777 resync_ioc->lil_mirror_id = ids[0];
10779 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10780 rc = llapi_lease_set(fd, ioc);
10783 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10784 progname, argv[0], fname, strerror(errno));
10788 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10791 fprintf(stderr, "%s %s: copy error: %d\n",
10792 progname, argv[0], rc);
10796 fprintf(stdout, "mirror copied successfully: ");
10797 for (i = 0; i < copied; i++)
10798 fprintf(stdout, "%d ", ids[i]);
10799 fprintf(stdout, "\n");
10801 ioc->lil_mode = LL_LEASE_UNLCK;
10802 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10803 ioc->lil_count = 0;
10804 for (i = 0; i < comp_size; i++) {
10807 for (j = 0; j < copied; j++) {
10808 if (comp_array[i].lrc_mirror_id != ids[j])
10811 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10815 rc = llapi_lease_set(fd, ioc);
10820 "%s %s: release lease lock of '%s' failed: %s\n",
10821 progname, argv[0], fname, strerror(errno));
10836 * struct verify_chunk - Mirror chunk to be verified.
10837 * @chunk: [start, end) of the chunk.
10838 * @mirror_count: Number of mirror ids in @mirror_id array.
10839 * @mirror_id: Array of valid mirror ids that cover the chunk.
10841 struct verify_chunk {
10842 struct lu_extent chunk;
10843 unsigned int mirror_count;
10844 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10848 * print_chunks() - Print chunk information.
10849 * @fname: Mirrored file name.
10850 * @chunks: Array of chunks.
10851 * @chunk_count: Number of chunks in @chunks array.
10853 * This function prints [start, end) of each chunk in @chunks
10854 * for mirrored file @fname, and also prints the valid mirror ids
10855 * that cover the chunk.
10860 void print_chunks(const char *fname, struct verify_chunk *chunks,
10866 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10867 for (i = 0; i < chunk_count; i++) {
10868 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10870 if (chunks[i].mirror_count == 0)
10871 fprintf(stdout, "\t[");
10873 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10874 for (j = 1; j < chunks[i].mirror_count; j++)
10875 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10877 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10879 fprintf(stdout, "\n");
10883 * print_checksums() - Print CRC-32 checksum values.
10884 * @chunk: A chunk and its corresponding valid mirror ids.
10885 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10887 * This function prints CRC-32 checksum values on @chunk for
10888 * each valid mirror that covers it.
10893 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10898 "CRC-32 checksum value for chunk "DEXT":\n",
10899 PEXT(&chunk->chunk));
10900 for (i = 0; i < chunk->mirror_count; i++)
10901 fprintf(stdout, "Mirror %u:\t%#lx\n",
10902 chunk->mirror_id[i], crc[i]);
10903 fprintf(stdout, "\n");
10907 * filter_mirror_id() - Filter specified mirror ids.
10908 * @chunks: Array of chunks.
10909 * @chunk_count: Number of chunks in @chunks array.
10910 * @mirror_ids: Specified mirror ids to be verified.
10911 * @ids_nr: Number of specified mirror ids.
10913 * This function scans valid mirror ids that cover each chunk in @chunks
10914 * and filters specified mirror ids.
10919 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10920 __u16 *mirror_ids, int ids_nr)
10925 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10926 unsigned int valid_count = 0;
10928 for (i = 0; i < chunk_count; i++) {
10929 if (chunks[i].mirror_count == 0)
10933 for (j = 0; j < ids_nr; j++) {
10934 for (k = 0; k < chunks[i].mirror_count; k++) {
10935 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10936 valid_id[valid_count] = mirror_ids[j];
10943 memcpy(chunks[i].mirror_id, valid_id,
10944 sizeof(__u16) * valid_count);
10945 chunks[i].mirror_count = valid_count;
10950 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10951 * @layout: Mirror component list.
10952 * @chunks: Array of chunks.
10953 * @chunks_size: Array size of @chunks.
10955 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10956 * to find out chunk segments and store them in @chunks array.
10958 * The @mirror_id array in each element of @chunks will store the valid
10959 * mirror ids that cover the chunk. If a mirror component covering the
10960 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10961 * will not be stored into the @mirror_id array, and the chunk for that
10962 * mirror will not be verified.
10964 * The @mirror_count in each element of @chunks will store the number of
10965 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10966 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10967 * indicates the chunk is valid in only one mirror. In both cases, the
10968 * chunk will not be verified.
10970 * Here is an example:
10972 * 0 1M 2M 3M 4M EOF
10973 * +------+-------------+--------------------+
10974 * | | | S | mirror1
10975 * +------+------+------+------+-------------+
10976 * | | S | S | | mirror2
10977 * +-------------+------+------+-------------+
10979 * prepared @chunks array will contain 5 elements:
10980 * (([0, 1M), [1, 2], 2),
10981 * ([1M, 2M), [1, 2], 2),
10982 * ([2M, 3M), [1], 1),
10983 * ([3M, 4M], [], 0),
10984 * ([4M, EOF), [2], 1))
10986 * Return: the actual array size of @chunks on success
10987 * or a negative error code on failure.
10990 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10991 struct verify_chunk *chunks,
10992 size_t chunks_size)
10996 uint32_t mirror_id;
11002 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11005 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11008 "%s: move to the first layout component: %s.\n",
11009 progname, strerror(errno));
11015 chunks[idx].chunk.e_end = LUSTRE_EOF;
11017 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11020 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11021 progname, strerror(errno));
11025 if (start > chunks[idx].chunk.e_start ||
11026 end <= chunks[idx].chunk.e_start)
11029 if (end < chunks[idx].chunk.e_end)
11030 chunks[idx].chunk.e_end = end;
11032 rc = llapi_layout_comp_flags_get(layout, &flags);
11035 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11036 progname, strerror(errno));
11040 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11043 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11046 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11047 progname, strerror(errno));
11051 chunks[idx].mirror_id[i] = mirror_id;
11053 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11055 "%s: mirror_id array is too small.\n",
11062 rc = llapi_layout_comp_use(layout,
11063 LLAPI_LAYOUT_COMP_USE_NEXT);
11066 "%s: move to the next layout component: %s.\n",
11067 progname, strerror(errno));
11070 } /* loop through all components */
11072 chunks[idx].mirror_count = i;
11074 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11078 if (idx >= chunks_size) {
11079 fprintf(stderr, "%s: chunks array is too small.\n",
11085 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11089 return rc < 0 ? rc : idx + 1;
11093 * lfs_mirror_verify_chunk() - Verify a chunk.
11094 * @fd: File descriptor of the mirrored file.
11095 * @file_size: Size of the mirrored file.
11096 * @chunk: A chunk and its corresponding valid mirror ids.
11097 * @verbose: Verbose mode.
11099 * This function verifies a @chunk contains exactly the same data
11100 * ammong the mirrors that cover it.
11102 * If @verbose is specified, then the function will print where the
11103 * differences are if the data do not match. Otherwise, it will
11104 * just return an error in that case.
11106 * Return: 0 on success or a negative error code on failure.
11109 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11110 struct verify_chunk *chunk, int verbose)
11112 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11114 size_t page_size = sysconf(_SC_PAGESIZE);
11115 ssize_t bytes_read;
11116 ssize_t bytes_done;
11120 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11124 if (file_size == 0)
11127 rc = posix_memalign(&buf, page_size, buflen);
11128 if (rc) /* error code is returned directly */
11132 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11133 PEXT(&chunk->chunk));
11134 for (i = 0; i < chunk->mirror_count; i++)
11135 fprintf(stdout, " %u", chunk->mirror_id[i]);
11136 fprintf(stdout, "\n");
11140 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11141 pos = chunk->chunk.e_start;
11142 while (bytes_done < count) {
11143 /* compute initial CRC-32 checksum */
11144 crc = crc32(0L, Z_NULL, 0);
11145 memset(crc_array, 0, sizeof(crc_array));
11148 for (i = 0; i < chunk->mirror_count; i++) {
11149 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11151 if (bytes_read < 0) {
11154 "%s: failed to read data from mirror %u: %s.\n",
11155 progname, chunk->mirror_id[i],
11160 /* compute new CRC-32 checksum */
11161 crc_array[i] = crc32(crc, buf, bytes_read);
11165 print_checksums(chunk, crc_array);
11167 /* compare CRC-32 checksum values */
11168 for (i = 1; i < chunk->mirror_count; i++) {
11169 if (crc_array[i] != crc_array[0]) {
11173 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11174 progname, PEXT(&chunk->chunk),
11175 chunk->mirror_id[0],
11176 chunk->mirror_id[i]);
11181 bytes_done += bytes_read;
11184 if (verbose > 1 && rc == 0) {
11185 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11186 PEXT(&chunk->chunk));
11187 for (i = 0; i < chunk->mirror_count; i++)
11188 fprintf(stdout, " %u", chunk->mirror_id[i]);
11189 fprintf(stdout, " PASS\n\n");
11198 * lfs_mirror_verify_file() - Verify a mirrored file.
11199 * @fname: Mirrored file name.
11200 * @mirror_ids: Specified mirror ids to be verified.
11201 * @ids_nr: Number of specified mirror ids.
11202 * @verbose: Verbose mode.
11204 * This function verifies that each SYNC mirror of a mirrored file
11205 * specified by @fname contains exactly the same data.
11207 * If @mirror_ids is specified, then the function will verify the
11208 * mirrors specified by @mirror_ids contain exactly the same data.
11210 * If @verbose is specified, then the function will print where the
11211 * differences are if the data do not match. Otherwise, it will
11212 * just return an error in that case.
11214 * Return: 0 on success or a negative error code on failure.
11217 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11220 struct verify_chunk chunks_array[1024] = { };
11221 struct llapi_layout *layout = NULL;
11223 uint32_t flr_state;
11225 int chunk_count = 0;
11231 if (stat(fname, &stbuf) < 0) {
11232 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11233 progname, fname, strerror(errno));
11238 if (!S_ISREG(stbuf.st_mode)) {
11239 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11245 if (stbuf.st_size == 0) {
11247 fprintf(stdout, "%s: '%s' file size is 0.\n",
11253 fd = open(fname, O_DIRECT | O_RDONLY);
11255 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11256 progname, fname, strerror(errno));
11261 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11263 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11264 progname, fname, strerror(errno));
11268 layout = llapi_layout_get_by_fd(fd, 0);
11270 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11271 progname, fname, strerror(errno));
11273 llapi_lease_release(fd);
11277 rc = llapi_layout_flags_get(layout, &flr_state);
11279 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11280 progname, fname, strerror(errno));
11285 flr_state &= LCM_FL_FLR_MASK;
11286 switch (flr_state) {
11289 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11290 progname, fname, llapi_layout_flags_string(flr_state));
11296 /* find out mirror chunks to be verified */
11297 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11298 ARRAY_SIZE(chunks_array));
11299 if (chunk_count < 0) {
11305 /* filter specified mirror ids */
11306 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11309 print_chunks(fname, chunks_array, chunk_count);
11311 for (idx = 0; idx < chunk_count; idx++) {
11312 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11315 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11317 PEXT(&chunks_array[idx].chunk),
11318 (unsigned long long)stbuf.st_size);
11322 if (chunks_array[idx].mirror_count == 0) {
11324 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11326 PEXT(&chunks_array[idx].chunk));
11328 fprintf(stderr, "skipped\n");
11332 fprintf(stderr, "failed\n");
11336 if (chunks_array[idx].mirror_count == 1) {
11339 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11341 PEXT(&chunks_array[idx].chunk),
11342 chunks_array[idx].mirror_id[0]);
11346 rc = llapi_lease_check(fd);
11347 if (rc != LL_LEASE_RDLCK) {
11348 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11353 /* verify one chunk */
11354 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11355 &chunks_array[idx], verbose);
11369 llapi_layout_free(layout);
11370 llapi_lease_release(fd);
11378 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11379 * @argc: The count of lfs mirror verify command line arguments.
11380 * @argv: Array of strings for lfs mirror verify command line arguments.
11382 * This function parses lfs mirror verify command and verifies the
11383 * specified mirrored file(s).
11385 * Return: 0 on success or a negative error code on failure.
11387 static inline int lfs_mirror_verify(int argc, char **argv)
11389 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11395 char cmd[PATH_MAX];
11397 struct option long_opts[] = {
11398 { .val = 'o', .name = "only", .has_arg = required_argument },
11399 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11400 { .name = NULL } };
11402 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11404 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11407 rc = parse_mirror_ids(mirror_ids,
11408 ARRAY_SIZE(mirror_ids),
11412 "%s: bad mirror ids '%s'.\n",
11419 "%s: at least 2 mirror ids needed with '--only' option.\n",
11429 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11430 progname, argv[optind - 1]);
11436 if (argc == optind) {
11437 fprintf(stderr, "%s: no file name given.\n", progname);
11442 if (ids_nr > 0 && argc > optind + 1) {
11444 "%s: '--only' cannot be used upon multiple files.\n",
11451 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11457 for (; optind < argc; optind++) {
11458 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11468 * lfs_mirror() - Parse and execute lfs mirror commands.
11469 * @argc: The count of lfs mirror command line arguments.
11470 * @argv: Array of strings for lfs mirror command line arguments.
11472 * This function parses lfs mirror commands and performs the
11473 * corresponding functions specified in mirror_cmdlist[].
11475 * Return: 0 on success or an error code on failure.
11477 static int lfs_mirror(int argc, char **argv)
11479 char cmd[PATH_MAX];
11482 setlinebuf(stdout);
11484 Parser_init("lfs-mirror > ", mirror_cmdlist);
11486 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11488 program_invocation_short_name = cmd;
11490 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11492 rc = Parser_commands();
11494 return rc < 0 ? -rc : rc;
11497 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11499 #if __BYTE_ORDER == __BIG_ENDIAN
11500 __swab16s(&attrs->lsa_valid);
11501 __swab64s(&attrs->lsa_size);
11502 __swab64s(&attrs->lsa_blocks);
11506 enum lfs_som_type {
11507 LFS_SOM_SIZE = 0x1,
11508 LFS_SOM_BLOCKS = 0x2,
11509 LFS_SOM_FLAGS = 0x4,
11510 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11514 static int lfs_getsom(int argc, char **argv)
11517 struct lustre_som_attrs *attrs;
11518 char buf[sizeof(*attrs) + 64];
11519 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11522 while ((c = getopt(argc, argv, "sbf")) != -1) {
11525 type = LFS_SOM_SIZE;
11528 type = LFS_SOM_BLOCKS;
11531 type = LFS_SOM_FLAGS;
11534 fprintf(stderr, "%s: invalid option '%c'\n",
11544 fprintf(stderr, "%s: %s\n",
11545 progname, argc == 0 ? "miss file target" :
11546 "input more than 2 files");
11551 attrs = (void *)buf;
11552 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11555 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11556 argv[0], strerror(errno), errno);
11560 lustre_som_swab(attrs);
11563 case LFS_SOM_ATTR_ALL:
11564 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11565 path, (unsigned long long)attrs->lsa_size,
11566 (unsigned long long)attrs->lsa_blocks,
11570 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11572 case LFS_SOM_BLOCKS:
11573 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11575 case LFS_SOM_FLAGS:
11576 printf("%x\n", attrs->lsa_valid);
11579 fprintf(stderr, "%s: unknown option\n", progname);
11587 * lfs_mirror_list_commands() - List lfs mirror commands.
11588 * @argc: The count of command line arguments.
11589 * @argv: Array of strings for command line arguments.
11591 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11593 * Return: 0 on success.
11595 static int lfs_mirror_list_commands(int argc, char **argv)
11597 char buffer[81] = "";
11599 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11605 static int lfs_pcc_attach(int argc, char **argv)
11607 struct option long_opts[] = {
11608 { .val = 'i', .name = "id", .has_arg = required_argument },
11609 { .name = NULL } };
11612 __u32 archive_id = 0;
11615 char fullpath[PATH_MAX];
11616 enum lu_pcc_type type = LU_PCC_READWRITE;
11619 while ((c = getopt_long(argc, argv, "i:",
11620 long_opts, NULL)) != -1) {
11624 archive_id = strtoul(optarg, &end, 0);
11625 if (errno != 0 || *end != '\0' ||
11626 archive_id == 0 || archive_id > UINT32_MAX) {
11628 "error: %s: bad archive ID '%s'\n",
11636 fprintf(stderr, "%s: option '%s' unrecognized\n",
11637 argv[0], argv[optind - 1]);
11642 if (archive_id == 0) {
11643 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11647 if (argc <= optind) {
11648 fprintf(stderr, "%s: must specify one or more file names\n",
11653 while (optind < argc) {
11656 path = argv[optind++];
11657 if (!realpath(path, fullpath)) {
11658 fprintf(stderr, "%s: could not find path '%s': %s\n",
11659 argv[0], path, strerror(errno));
11665 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11668 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11669 argv[0], path, archive_id, strerror(-rc2));
11677 static int lfs_pcc_attach_fid(int argc, char **argv)
11679 struct option long_opts[] = {
11680 { .val = 'i', .name = "id", .has_arg = required_argument },
11681 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11682 { .name = NULL } };
11683 char short_opts[] = "i:m:";
11686 __u32 archive_id = 0;
11688 const char *mntpath = NULL;
11689 const char *fidstr;
11690 enum lu_pcc_type type = LU_PCC_READWRITE;
11693 while ((c = getopt_long(argc, argv, short_opts,
11694 long_opts, NULL)) != -1) {
11698 archive_id = strtoul(optarg, &end, 0);
11699 if (errno != 0 || *end != '\0' ||
11700 archive_id > UINT32_MAX) {
11702 "error: %s: bad archive ID '%s'\n",
11713 fprintf(stderr, "%s: option '%s' unrecognized\n",
11714 argv[0], argv[optind - 1]);
11719 if (archive_id == 0) {
11720 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11725 fprintf(stderr, "%s: must specify Lustre mount point\n",
11730 if (argc <= optind) {
11731 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11735 while (optind < argc) {
11738 fidstr = argv[optind++];
11740 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11744 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11745 argv[0], fidstr, mntpath, archive_id,
11748 if (rc == 0 && rc2 < 0)
11754 static int lfs_pcc_detach(int argc, char **argv)
11756 struct option long_opts[] = {
11757 { .val = 'k', .name = "keep", .has_arg = no_argument },
11758 { .name = NULL } };
11759 char short_opts[] = "k";
11763 char fullpath[PATH_MAX];
11764 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11767 while ((c = getopt_long(argc, argv, short_opts,
11768 long_opts, NULL)) != -1) {
11771 detach_opt = PCC_DETACH_OPT_NONE;
11776 fprintf(stderr, "%s: option '%s' unrecognized\n",
11777 argv[0], argv[optind - 1]);
11782 while (optind < argc) {
11785 path = argv[optind++];
11786 if (!realpath(path, fullpath)) {
11787 fprintf(stderr, "%s: could not find path '%s': %s\n",
11788 argv[0], path, strerror(errno));
11794 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11798 "%s: cannot detach '%s' from PCC: %s\n",
11799 argv[0], path, strerror(errno));
11807 static int lfs_pcc_detach_fid(int argc, char **argv)
11809 struct option long_opts[] = {
11810 { .val = 'k', .name = "keep", .has_arg = no_argument },
11811 { .name = NULL } };
11812 char short_opts[] = "k";
11816 const char *mntpath;
11817 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11820 while ((c = getopt_long(argc, argv, short_opts,
11821 long_opts, NULL)) != -1) {
11824 detach_opt = PCC_DETACH_OPT_NONE;
11829 fprintf(stderr, "%s: option '%s' unrecognized\n",
11830 argv[0], argv[optind - 1]);
11835 mntpath = argv[optind++];
11837 while (optind < argc) {
11840 fid = argv[optind++];
11842 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11845 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11846 argv[0], fid, mntpath, strerror(-rc2));
11854 static int lfs_pcc_state(int argc, char **argv)
11858 char fullpath[PATH_MAX];
11859 struct lu_pcc_state state;
11864 fprintf(stderr, "%s: must specify one or more file names\n",
11869 while (optind < argc) {
11872 path = argv[optind++];
11873 if (!realpath(path, fullpath)) {
11874 fprintf(stderr, "%s: could not find path '%s': %s\n",
11875 argv[0], path, strerror(errno));
11881 rc2 = llapi_pcc_state_get(fullpath, &state);
11886 "%s: cannot get PCC state of '%s': %s\n",
11887 argv[0], path, strerror(-rc2));
11891 printf("file: %s", path);
11892 printf(", type: %s", pcc_type2string(state.pccs_type));
11893 if (state.pccs_type == LU_PCC_NONE &&
11894 state.pccs_open_count == 0) {
11899 printf(", PCC file: %s", state.pccs_path);
11900 printf(", user number: %u", state.pccs_open_count);
11901 printf(", flags: %x", state.pccs_flags);
11908 * lfs_pcc_list_commands() - List lfs pcc commands.
11909 * @argc: The count of command line arguments.
11910 * @argv: Array of strings for command line arguments.
11912 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11914 * Return: 0 on success.
11916 static int lfs_pcc_list_commands(int argc, char **argv)
11918 char buffer[81] = "";
11920 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11927 * lfs_pcc() - Parse and execute lfs pcc commands.
11928 * @argc: The count of lfs pcc command line arguments.
11929 * @argv: Array of strings for lfs pcc command line arguments.
11931 * This function parses lfs pcc commands and performs the
11932 * corresponding functions specified in pcc_cmdlist[].
11934 * Return: 0 on success or an error code on failure.
11936 static int lfs_pcc(int argc, char **argv)
11938 char cmd[PATH_MAX];
11941 setlinebuf(stdout);
11943 Parser_init("lfs-pcc > ", pcc_cmdlist);
11945 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11947 program_invocation_short_name = cmd;
11949 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11951 rc = Parser_commands();
11953 return rc < 0 ? -rc : rc;
11956 static int lfs_list_commands(int argc, char **argv)
11958 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11960 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11965 int main(int argc, char **argv)
11969 /* Ensure that liblustreapi constructor has run */
11970 if (!llapi_liblustreapi_initialized())
11971 fprintf(stderr, "liblustreapi was not properly initialized\n");
11973 setlinebuf(stdout);
11976 Parser_init("lfs > ", cmdlist);
11978 progname = program_invocation_short_name; /* Used in error messages */
11980 llapi_set_command_name(argv[1]);
11981 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11982 llapi_clear_command_name();
11984 rc = Parser_commands();
11987 return rc < 0 ? -rc : rc;
11990 #ifdef _LUSTRE_IDL_H_
11991 /* Everything we need here should be included by lustreapi.h. */
11992 # error "lfs should not depend on lustre_idl.h"
11993 #endif /* _LUSTRE_IDL_H_ */