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;
932 size_t page_size = sysconf(_SC_PAGESIZE);
936 layout = llapi_layout_get_by_fd(fd_src, 0);
938 uint64_t stripe_size;
940 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
942 buf_size = stripe_size;
944 llapi_layout_free(layout);
947 /* Use a page-aligned buffer for direct I/O */
948 rc = posix_memalign(&buf, page_size, buf_size);
952 sparse = llapi_file_is_sparse(fd_src);
954 rc = ftruncate(fd_dst, pos);
963 size_t to_read, to_write;
966 if (sparse && pos >= data_end) {
969 data_off = llapi_data_seek(fd_src, pos, &data_size);
971 /* Non-fatal, switch to full copy */
975 /* hole at the end of file, truncate up to it */
977 rc = ftruncate(fd_dst, data_off);
981 pos = data_off & ~(page_size - 1);
982 data_end = data_off + data_size;
983 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
984 to_read = MIN(to_read, buf_size);
990 rc = check_file(fd_src);
995 rsize = pread(fd_src, buf, to_read, pos);
1005 while (to_write > 0) {
1008 written = pwrite(fd_dst, buf, to_write, pos);
1014 to_write -= written;
1016 if (rc || rsize < to_read)
1024 /* Try to avoid page cache pollution after migration. */
1025 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
1026 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1032 static int migrate_copy_timestamps(int fd, int fdv)
1036 if (fstat(fd, &st) == 0) {
1037 struct timeval tv[2] = {
1038 {.tv_sec = st.st_atime},
1039 {.tv_sec = st.st_mtime}
1042 return futimes(fdv, tv);
1048 static int migrate_block(int fd, int fdv)
1055 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1057 error_loc = "cannot get dataversion";
1066 * The grouplock blocks all concurrent accesses to the file.
1067 * It has to be taken after llapi_get_data_version as it would
1070 rc = llapi_group_lock(fd, gid);
1072 error_loc = "cannot get group lock";
1076 rc = migrate_copy_data(fd, fdv, NULL);
1078 error_loc = "data copy failed";
1082 /* Make sure we keep original atime/mtime values */
1083 rc = migrate_copy_timestamps(fd, fdv);
1085 error_loc = "timestamp copy failed";
1091 * for a migration we need to check data version on file did
1094 * Pass in gid=0 since we already own grouplock.
1096 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1097 SWAP_LAYOUTS_CHECK_DV1);
1098 if (rc == -EAGAIN) {
1099 error_loc = "file changed";
1101 } else if (rc < 0) {
1102 error_loc = "cannot swap layout";
1107 rc2 = llapi_group_unlock(fd, gid);
1108 if (rc2 < 0 && rc == 0) {
1109 error_loc = "unlock group lock";
1117 * Internal helper for migrate_copy_data(). Check lease and report error if
1120 * \param[in] fd File descriptor on which to check the lease.
1122 * \retval 0 Migration can keep on going.
1123 * \retval -errno Error occurred, abort migration.
1125 static int check_lease(int fd)
1129 rc = llapi_lease_check(fd);
1131 return 0; /* llapi_check_lease returns > 0 on success. */
1136 static int migrate_nonblock(int fd, int fdv)
1142 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1144 error_loc = "cannot get data version";
1148 rc = migrate_copy_data(fd, fdv, check_lease);
1150 error_loc = "data copy failed";
1154 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1156 error_loc = "cannot get data version";
1162 error_loc = "source file changed";
1166 /* Make sure we keep original atime/mtime values */
1167 rc = migrate_copy_timestamps(fd, fdv);
1169 error_loc = "timestamp copy failed";
1177 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1179 struct pool_to_id_cbdata data = { .pool = pool };
1180 struct llapi_layout *layout = NULL;
1183 layout = llapi_layout_get_by_path(fname, 0);
1186 "error %s: file '%s' couldn't get layout: rc=%d\n",
1187 progname, fname, errno);
1191 rc = llapi_layout_sanity(layout, false, true);
1193 llapi_layout_sanity_perror(errno);
1196 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1205 llapi_layout_free(layout);
1209 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1210 __u32 flags, __u32 neg_flags)
1213 __u32 flags_array[2];
1220 "error %s: neither component id nor pool is specified\n",
1224 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1230 ids[count] = comp_id;
1231 flags_array[count] = flags;
1236 if (neg_flags & LCME_FL_STALE) {
1238 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1243 ids[count] = comp_id;
1244 flags_array[count] = neg_flags | LCME_FL_NEG;
1248 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1250 if (errno == EUCLEAN) {
1253 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1254 progname, comp_id, fname);
1257 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1258 progname, comp_id, fname, flags, neg_flags);
1265 static int lfs_component_del(char *fname, __u32 comp_id,
1266 __u32 flags, __u32 neg_flags)
1270 if (flags && neg_flags)
1273 if (!flags && neg_flags)
1274 flags = neg_flags | LCME_FL_NEG;
1276 if ((flags && comp_id) || (!flags && !comp_id))
1280 if (flags & ~LCME_KNOWN_FLAGS) {
1282 "%s setstripe: unknown flags %#x\n",
1286 } else if (comp_id > LCME_ID_MAX) {
1287 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1292 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1295 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1296 progname, comp_id, fname, strerror(errno));
1300 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1307 rc = llapi_layout_file_comp_add(fname, layout);
1309 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1310 fname, strerror(errno));
1314 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1315 struct llapi_layout *layout)
1323 fd = lstat(fname, &st);
1324 if (fd == 0 && S_ISDIR(st.st_mode))
1325 open_flags = O_DIRECTORY | O_RDONLY;
1327 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1329 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1330 S_ISDIR(st.st_mode) ?
1331 "set default composite layout for" :
1332 "create composite file",
1333 fname, strerror(errno));
1337 static int lfs_migrate(char *name, __u64 migration_flags,
1338 struct llapi_stripe_param *param,
1339 struct llapi_layout *layout)
1341 struct llapi_layout *existing;
1342 uint64_t dom_new, dom_cur;
1347 rc = migrate_open_files(name, migration_flags, param, layout,
1352 rc = llapi_layout_dom_size(layout, &dom_new);
1354 error_loc = "cannot get new layout DoM size";
1357 /* special case for migration to DOM layout*/
1358 existing = llapi_layout_get_by_fd(fd, 0);
1360 error_loc = "cannot get existing layout";
1364 rc = llapi_layout_dom_size(existing, &dom_cur);
1366 error_loc = "cannot get current layout DoM size";
1371 * if file has DoM layout already then migration is possible to
1372 * the new layout with the same DoM component via swap layout,
1373 * if new layout used bigger DOM size, then mirroring is used
1375 if (dom_new > dom_cur) {
1376 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1379 error_loc = "cannot migrate to DOM layout";
1383 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1385 * Blocking mode (forced if servers do not support file lease).
1386 * It is also the default mode, since we cannot distinguish
1387 * between a broken lease and a server that does not support
1388 * atomic swap/close (LU-6785)
1390 rc = migrate_block(fd, fdv);
1394 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1396 error_loc = "cannot get lease";
1400 rc = migrate_nonblock(fd, fdv);
1402 llapi_lease_release(fd);
1407 * Atomically put lease, swap layouts and close.
1408 * for a migration we need to check data version on file did
1411 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1413 error_loc = "cannot swap layout";
1425 fprintf(stderr, "error: %s: %s: %s: %s\n",
1426 progname, name, error_loc, strerror(-rc));
1427 else if (migration_flags & MIGRATION_VERBOSE)
1428 printf("%s\n", name);
1433 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1436 char *dup_string = NULL;
1442 if (!string || !string[0])
1445 dup_string = strdup(string);
1447 llapi_printf(LLAPI_MSG_ERROR,
1448 "%s: insufficient memory\n",
1453 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1457 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1458 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1459 const char *comp_name = comp_flags_table[i].cfn_name;
1461 if (strcmp(name, comp_name) == 0) {
1462 *flags |= comp_flag;
1464 } else if (strncmp(name, "^", 1) == 0 &&
1465 strcmp(name + 1, comp_name) == 0) {
1466 *neg_flags |= comp_flag;
1471 llapi_printf(LLAPI_MSG_ERROR,
1472 "%s: component flag '%s' not supported\n",
1479 if (!*flags && !*neg_flags)
1482 /* don't allow to set and exclude the same flag */
1483 if (*flags & *neg_flags)
1491 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1499 if (strncmp(string, "^", 1) == 0) {
1500 *neg_state = llapi_layout_string_flags(string + 1);
1501 if (*neg_state != 0)
1504 *state = llapi_layout_string_flags(string);
1509 llapi_printf(LLAPI_MSG_ERROR,
1510 "%s: mirrored file state '%s' not supported\n",
1516 * struct mirror_args - Command-line arguments for mirror(s).
1517 * @m_count: Number of mirrors to be created with this layout.
1518 * @m_flags: Mirror level flags, only 'prefer' is supported.
1519 * @m_layout: Mirror layout.
1520 * @m_file: A victim file. Its layout will be split and used as a mirror.
1521 * @m_next: Point to the next node of the list.
1523 * Command-line arguments for mirror(s) will be parsed and stored in
1524 * a linked list that consists of this structure.
1526 struct mirror_args {
1529 struct llapi_layout *m_layout;
1531 struct mirror_args *m_next;
1536 * enum mirror_flags - Flags for extending a mirrored file.
1537 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1538 * in case the victim file(s) contains the same data as the
1539 * original mirrored file.
1540 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1541 * @MF_COMP_ID: specified component id instead of mirror id
1543 * Flags for extending a mirrored file.
1553 * mirror_create_sanity_check() - Check mirror list.
1554 * @list: A linked list that stores the mirror arguments.
1556 * This function does a sanity check on @list for creating
1559 * Return: 0 on success or a negative error code on failure.
1561 static int mirror_create_sanity_check(const char *fname,
1562 struct mirror_args *list)
1565 bool has_m_file = false;
1566 bool has_m_layout = false;
1572 struct llapi_layout *layout;
1574 layout = llapi_layout_get_by_path(fname, 0);
1577 "error: %s: file '%s' couldn't get layout\n",
1582 rc = llapi_layout_sanity(layout, false, true);
1584 llapi_layout_free(layout);
1587 llapi_layout_sanity_perror(rc);
1595 llapi_layout_free(list->m_layout);
1598 llapi_layout_get_by_path(list->m_file, 0);
1599 if (!list->m_layout) {
1601 "error: %s: file '%s' has no layout\n",
1602 progname, list->m_file);
1606 has_m_layout = true;
1607 if (!list->m_layout) {
1608 fprintf(stderr, "error: %s: no mirror layout\n",
1614 rc = llapi_layout_sanity(list->m_layout, false, true);
1616 llapi_layout_sanity_perror(rc);
1620 list = list->m_next;
1623 if (has_m_file && has_m_layout) {
1625 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1633 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1635 __u32 mirror_flags = *(__u32 *)cbdata;
1639 rc = llapi_layout_comp_flags_get(layout, &flags);
1644 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1649 return LLAPI_LAYOUT_ITER_CONT;
1653 * mirror_create() - Create a mirrored file.
1654 * @fname: The file to be created.
1655 * @mirror_list: A linked list that stores the mirror arguments.
1657 * This function creates a mirrored file @fname with the mirror(s)
1658 * from @mirror_list.
1660 * Return: 0 on success or a negative error code on failure.
1662 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1664 struct llapi_layout *layout = NULL;
1665 struct mirror_args *cur_mirror = NULL;
1666 uint16_t mirror_count = 0;
1670 rc = mirror_create_sanity_check(NULL, mirror_list);
1674 cur_mirror = mirror_list;
1675 while (cur_mirror) {
1676 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1678 &cur_mirror->m_flags);
1681 fprintf(stderr, "%s: failed to set mirror flags\n",
1686 for (i = 0; i < cur_mirror->m_count; i++) {
1687 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1691 "error: %s: merge layout failed: %s\n",
1692 progname, strerror(errno));
1696 mirror_count += cur_mirror->m_count;
1697 cur_mirror = cur_mirror->m_next;
1701 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1705 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1708 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1709 progname, strerror(errno));
1713 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1721 llapi_layout_free(layout);
1726 * Compare files and check lease on @fd.
1728 * \retval bytes number of bytes are the same
1730 static ssize_t mirror_file_compare(int fd, int fdv)
1732 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1734 ssize_t bytes_done = 0;
1735 ssize_t bytes_read = 0;
1737 buf = malloc(buflen * 2);
1742 if (!llapi_lease_check(fd)) {
1743 bytes_done = -EBUSY;
1747 bytes_read = read(fd, buf, buflen);
1748 if (bytes_read <= 0)
1751 if (bytes_read != read(fdv, buf + buflen, buflen))
1755 * XXX: should compute the checksum on each buffer and then
1756 * compare checksum to avoid cache collision
1758 if (memcmp(buf, buf + buflen, bytes_read))
1761 bytes_done += bytes_read;
1769 static int mirror_extend_file(const char *fname, const char *victim_file,
1770 enum mirror_flags mirror_flags)
1775 struct stat stbuf_v;
1776 struct ll_ioc_lease *data = NULL;
1779 fd = open(fname, O_RDWR);
1781 error_loc = "open source file";
1786 fdv = open(victim_file, O_RDWR);
1788 error_loc = "open target file";
1793 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1794 error_loc = "stat source or target file";
1799 if (stbuf.st_dev != stbuf_v.st_dev) {
1800 error_loc = "stat source and target file";
1805 /* mirrors should be of the same size */
1806 if (stbuf.st_size != stbuf_v.st_size) {
1807 error_loc = "file sizes don't match";
1812 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1814 error_loc = "cannot get lease";
1818 if (!(mirror_flags & MF_NO_VERIFY)) {
1820 /* mirrors should have the same contents */
1821 ret = mirror_file_compare(fd, fdv);
1822 if (ret != stbuf.st_size) {
1823 error_loc = "file busy or contents don't match";
1824 rc = ret < 0 ? ret : -EINVAL;
1829 /* Get rid of caching pages from clients */
1830 rc = llapi_file_flush(fd);
1832 error_loc = "cannot get data version";
1836 rc = llapi_file_flush(fdv);
1838 error_loc = "cannot get data version";
1842 /* Make sure we keep original atime/mtime values */
1843 rc = migrate_copy_timestamps(fd, fdv);
1845 error_loc = "cannot copy timestamp";
1849 /* Atomically put lease, merge layouts and close. */
1850 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1852 error_loc = "memory allocation";
1855 data->lil_mode = LL_LEASE_UNLCK;
1856 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1857 data->lil_count = 1;
1858 data->lil_ids[0] = fdv;
1859 rc = llapi_lease_set(fd, data);
1861 error_loc = "cannot merge layout";
1863 } else if (rc == 0) {
1865 error_loc = "lost lease lock";
1878 (void) unlink(victim_file);
1880 fprintf(stderr, "error: %s: %s: %s: %s\n",
1881 progname, fname, error_loc, strerror(-rc));
1885 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1886 bool inherit, uint32_t flags)
1888 struct llapi_layout *f_layout = NULL;
1889 struct ll_ioc_lease *data = NULL;
1895 f_layout = llapi_layout_get_by_path(name, 0);
1897 fprintf(stderr, "%s: cannot get layout\n", progname);
1900 rc = llapi_layout_get_last_init_comp(f_layout);
1902 fprintf(stderr, "%s: cannot get the last init comp\n",
1906 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1909 "%s: cannot inherit from the last init comp\n",
1914 llapi_layout_comp_flags_set(m_layout, flags);
1915 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1920 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1922 error_loc = "cannot get lease";
1926 rc = migrate_nonblock(fd, fdv);
1928 llapi_lease_release(fd);
1932 /* Atomically put lease, merge layouts and close. */
1933 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1935 error_loc = "memory allocation";
1938 data->lil_mode = LL_LEASE_UNLCK;
1939 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1940 data->lil_count = 1;
1941 data->lil_ids[0] = fdv;
1942 rc = llapi_lease_set(fd, data);
1944 error_loc = "cannot merge layout";
1946 } else if (rc == 0) {
1948 error_loc = "lost lease lock";
1961 fprintf(stderr, "error: %s: %s: %s: %s\n",
1962 progname, name, error_loc, strerror(-rc));
1966 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1967 enum mirror_flags mirror_flags)
1971 rc = mirror_create_sanity_check(fname, mirror_list);
1975 while (mirror_list) {
1976 if (mirror_list->m_file) {
1977 rc = mirror_extend_file(fname, mirror_list->m_file,
1980 __u32 mirror_count = mirror_list->m_count;
1982 while (mirror_count > 0) {
1983 rc = mirror_extend_layout(fname,
1984 mirror_list->m_layout,
1985 mirror_list->m_inherit,
1986 mirror_list->m_flags);
1996 mirror_list = mirror_list->m_next;
2002 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2007 rc = llapi_layout_mirror_id_get(layout, &id);
2011 if ((__u16)id == *(__u16 *)cbdata)
2012 return LLAPI_LAYOUT_ITER_STOP;
2014 return LLAPI_LAYOUT_ITER_CONT;
2017 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2022 rc = llapi_layout_comp_id_get(layout, &id);
2026 if (id == *(__u32 *)cbdata)
2027 return LLAPI_LAYOUT_ITER_STOP;
2029 return LLAPI_LAYOUT_ITER_CONT;
2032 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2034 char buf[LOV_MAXPOOLNAME + 1];
2035 struct pool_to_id_cbdata *d = (void *)cbdata;
2039 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2042 if (strcmp(d->pool, buf))
2043 return LLAPI_LAYOUT_ITER_CONT;
2045 rc = llapi_layout_mirror_id_get(layout, &id);
2050 return LLAPI_LAYOUT_ITER_STOP;
2053 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2055 char buf[LOV_MAXPOOLNAME + 1];
2056 struct pool_to_id_cbdata *d = (void *)cbdata;
2060 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2063 if (strcmp(d->pool, buf))
2064 return LLAPI_LAYOUT_ITER_CONT;
2066 rc = llapi_layout_comp_id_get(layout, &id);
2071 return LLAPI_LAYOUT_ITER_STOP;
2074 struct collect_ids_data {
2080 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2082 struct collect_ids_data *cid = cbdata;
2086 rc = llapi_layout_mirror_id_get(layout, &id);
2090 if ((__u16)id != cid->cid_exclude) {
2093 for (i = 0; i < cid->cid_count; i++) {
2094 /* already collected the mirror id */
2095 if (id == cid->cid_ids[i])
2096 return LLAPI_LAYOUT_ITER_CONT;
2098 cid->cid_ids[cid->cid_count] = id;
2102 return LLAPI_LAYOUT_ITER_CONT;
2106 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2107 * @mirror_id: Mirror id to be checked.
2108 * @layout: Mirror component list.
2110 * This function checks if a mirror with specified @mirror_id is the last
2111 * non-stale mirror of a layout @layout.
2113 * Return: true or false.
2116 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2118 __u16 mirror_ids[128] = { 0 };
2119 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2121 .cid_exclude = mirror_id, };
2124 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2126 for (i = 0; i < cid.cid_count; i++) {
2127 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2130 comp_size = llapi_mirror_find_stale(layout, comp_array,
2131 ARRAY_SIZE(comp_array),
2140 static int mirror_split(const char *fname, __u32 id, const char *pool,
2141 enum mirror_flags mflags, const char *victim_file)
2143 struct llapi_layout *layout;
2144 char parent[PATH_MAX];
2145 char victim[PATH_MAX];
2146 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2148 struct ll_ioc_lease *data;
2149 uint16_t mirror_count;
2155 /* check fname contains mirror with mirror_id/comp_id */
2156 layout = llapi_layout_get_by_path(fname, 0);
2159 "error %s: file '%s' couldn't get layout\n",
2164 rc = llapi_layout_sanity(layout, false, true);
2166 llapi_layout_sanity_perror(rc);
2170 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2173 "error %s: file '%s' couldn't get mirror count\n",
2177 if (mirror_count < 2) {
2179 "error %s: file '%s' has %d component, cannot split\n",
2180 progname, fname, mirror_count);
2184 if (mflags & MF_COMP_POOL) {
2185 struct pool_to_id_cbdata data = { .pool = pool };
2187 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2189 mirror_id = data.id;
2190 } else if (mflags & MF_COMP_ID) {
2191 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2192 mirror_id = mirror_id_of(id);
2194 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2198 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2201 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2202 if (mflags & MF_COMP_POOL) {
2204 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2205 progname, fname, pool);
2207 } else if (mflags & MF_COMP_ID) {
2209 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2210 progname, fname, id);
2214 "error %s: file '%s' does not contain mirror with id %u\n",
2215 progname, fname, id);
2220 fd = open(fname, O_RDWR);
2223 "error %s: open file '%s' failed: %s\n",
2224 progname, fname, strerror(errno));
2228 /* get victim file directory pathname */
2229 if (strlen(fname) > sizeof(parent) - 1) {
2230 fprintf(stderr, "error %s: file name of '%s' too long\n",
2235 strncpy(parent, fname, sizeof(parent));
2236 ptr = strrchr(parent, '/');
2238 if (!getcwd(parent, sizeof(parent))) {
2239 fprintf(stderr, "error %s: getcwd failed: %s\n",
2240 progname, strerror(errno));
2250 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2252 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2258 /* use a temp file to store the splitted layout */
2259 if (mflags & MF_DESTROY) {
2260 if (last_non_stale_mirror(mirror_id, layout)) {
2263 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2268 fdv = llapi_create_volatile_idx(parent, mdt_index,
2269 O_LOV_DELAY_CREATE);
2271 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2273 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2276 /* user specified victim file */
2277 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2282 "error %s: create victim file failed: %s\n",
2283 progname, strerror(errno));
2287 /* get lease lock of fname */
2288 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2291 "error %s: cannot get lease of file '%s': %d\n",
2292 progname, fname, rc);
2296 /* Atomatically put lease, split layouts and close. */
2297 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2303 data->lil_mode = LL_LEASE_UNLCK;
2304 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2305 data->lil_count = 2;
2306 data->lil_ids[0] = fdv;
2307 data->lil_ids[1] = mirror_id;
2308 rc = llapi_lease_set(fd, data);
2310 if (rc == 0) /* lost lease lock */
2313 "error %s: cannot split '%s': %s\n",
2314 progname, fname, strerror(-rc));
2325 llapi_layout_free(layout);
2330 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2331 __u16 *mirror_ids, int ids_nr);
2333 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2334 __u64 migration_flags,
2335 struct llapi_stripe_param *param,
2336 struct llapi_layout *layout)
2338 struct ll_ioc_lease *data = NULL;
2341 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2343 error_loc = "cannot get lease";
2347 /* Atomically put lease, merge layouts, resync and close. */
2348 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2350 error_loc = "memory allocation";
2353 data->lil_mode = LL_LEASE_UNLCK;
2354 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2355 data->lil_count = 1;
2356 data->lil_ids[0] = fdv;
2357 rc = llapi_lease_set(fd, data);
2359 error_loc = "cannot merge layout";
2361 } else if (rc == 0) {
2363 error_loc = "lost lease lock";
2369 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2371 error_loc = "cannot resync file";
2375 /* delete first mirror now */
2376 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2378 error_loc = "cannot delete old layout";
2386 fprintf(stderr, "error: %s: %s: %s: %s\n",
2387 progname, name, error_loc, strerror(-rc));
2388 else if (migration_flags & MIGRATION_VERBOSE)
2389 printf("%s\n", name);
2396 * Parse a string containing an target index list into an array of integers.
2398 * The input string contains a comma delimited list of individual
2399 * indices and ranges, for example "1,2-4,7". Add the indices into the
2400 * \a tgts array and remove duplicates.
2402 * \param[out] tgts array to store indices in
2403 * \param[in] size size of \a tgts array
2404 * \param[in] offset starting index in \a tgts
2405 * \param[in] arg string containing OST index list
2406 * \param[in/out] overstriping index list may contain duplicates
2408 * \retval positive number of indices in \a tgts
2409 * \retval -EINVAL unable to parse \a arg
2411 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2412 unsigned long long *pattern)
2416 int slots = size - offset;
2418 bool overstriped = false;
2424 end_of_loop = false;
2425 while (!end_of_loop) {
2426 int start_index = 0;
2429 char *endptr = NULL;
2433 ptr = strchrnul(arg, ',');
2435 end_of_loop = *ptr == '\0';
2439 start_index = strtol(arg, &endptr, 0);
2440 if (endptr == arg) /* no data at all */
2442 if (errno != 0 || start_index < -1 ||
2443 (*endptr != '-' && *endptr != '\0'))
2446 end_index = start_index;
2447 if (*endptr == '-') {
2449 end_index = strtol(endptr + 1, &endptr, 0);
2450 if (errno != 0 || *endptr != '\0' || end_index < -1)
2452 if (end_index < start_index)
2456 for (i = start_index; i <= end_index && slots > 0; i++) {
2459 /* remove duplicate */
2460 for (j = 0; j < offset; j++) {
2461 if (tgts[j] == i && pattern &&
2462 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2464 else if (tgts[j] == i)
2470 if (j == offset) { /* check complete */
2476 if (slots == 0 && i < end_index)
2484 if (!end_of_loop && ptr)
2487 if (!overstriped && pattern)
2488 *pattern = LLAPI_LAYOUT_DEFAULT;
2490 return rc < 0 ? rc : nr;
2493 struct lfs_setstripe_args {
2494 unsigned long long lsa_comp_end;
2495 unsigned long long lsa_stripe_size;
2496 unsigned long long lsa_extension_size;
2497 long long lsa_stripe_count;
2498 long long lsa_stripe_off;
2499 __u32 lsa_comp_flags;
2500 __u32 lsa_comp_neg_flags;
2501 unsigned long long lsa_pattern;
2502 unsigned int lsa_mirror_count;
2504 bool lsa_first_comp;
2505 bool lsa_extension_comp;
2507 char *lsa_pool_name;
2510 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2512 unsigned int mirror_count = lsa->lsa_mirror_count;
2513 bool first_comp = lsa->lsa_first_comp;
2515 memset(lsa, 0, sizeof(*lsa));
2517 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2518 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2519 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2520 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2521 lsa->lsa_pool_name = NULL;
2523 lsa->lsa_mirror_count = mirror_count;
2524 lsa->lsa_first_comp = first_comp;
2528 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2529 * @lsa: Stripe options to be initialized and inherited.
2531 * This function initializes stripe options in @lsa and inherit
2532 * stripe_size, stripe_count and OST pool_name options.
2536 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2538 unsigned long long stripe_size;
2539 long long stripe_count;
2540 char *pool_name = NULL;
2542 stripe_size = lsa->lsa_stripe_size;
2543 stripe_count = lsa->lsa_stripe_count;
2544 pool_name = lsa->lsa_pool_name;
2546 setstripe_args_init(lsa);
2548 lsa->lsa_stripe_size = stripe_size;
2549 lsa->lsa_stripe_count = stripe_count;
2550 lsa->lsa_pool_name = pool_name;
2553 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2555 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2556 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2557 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2558 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2559 lsa->lsa_comp_end != 0);
2563 * comp_args_to_layout() - Create or extend a composite layout.
2564 * @composite: Pointer to the composite layout.
2565 * @lsa: Stripe options for the new component.
2567 * This function creates or extends a composite layout by adding a new
2568 * component with stripe options from @lsa.
2570 * Return: 0 on success or an error code on failure.
2572 static int comp_args_to_layout(struct llapi_layout **composite,
2573 struct lfs_setstripe_args *lsa,
2576 struct llapi_layout *layout = *composite;
2577 uint64_t prev_end = 0;
2583 layout = llapi_layout_alloc();
2585 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2590 *composite = layout;
2591 lsa->lsa_first_comp = true;
2596 * Get current component extent, current component
2597 * must be the tail component.
2599 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2601 fprintf(stderr, "Get comp extent failed. %s\n",
2606 if (lsa->lsa_first_comp) {
2608 rc = llapi_layout_add_first_comp(layout);
2610 rc = llapi_layout_comp_add(layout);
2613 fprintf(stderr, "Add component failed. %s\n",
2619 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2621 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2622 lsa->lsa_comp_flags, strerror(errno));
2627 uint64_t comp_end = lsa->lsa_comp_end;
2630 * The extendable component is 0-length, so it can be removed
2631 * if there is insufficient space to extend it.
2633 if (lsa->lsa_extension_comp)
2634 comp_end = prev_end;
2636 rc = llapi_layout_comp_extent_set(layout, prev_end,
2639 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2640 prev_end, comp_end, strerror(errno));
2644 /* reset lsa_first_comp */
2645 lsa->lsa_first_comp = false;
2647 /* Data-on-MDT component setting */
2648 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2650 * In case of Data-on-MDT patterns the only extra option
2651 * applicable is stripe size option.
2653 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2655 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2656 lsa->lsa_stripe_count);
2660 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2662 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2663 lsa->lsa_stripe_size);
2667 if (lsa->lsa_nr_tgts != 0) {
2669 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2674 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2676 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2677 lsa->lsa_stripe_off);
2681 if (lsa->lsa_pool_name != 0) {
2683 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2684 lsa->lsa_pool_name);
2689 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2691 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2696 /* Data-on-MDT component has always single stripe up to end */
2697 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2698 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2699 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2701 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2708 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2709 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2711 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2712 rc = llapi_layout_extension_size_set(layout, size);
2714 rc = llapi_layout_stripe_size_set(layout, size);
2717 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2718 size, strerror(errno));
2722 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2724 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2725 lsa->lsa_stripe_count, strerror(errno));
2729 if (lsa->lsa_pool_name) {
2730 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2732 fprintf(stderr, "Set pool name: %s failed. %s\n",
2733 lsa->lsa_pool_name, strerror(errno));
2737 rc = llapi_layout_pool_name_set(layout, "");
2739 fprintf(stderr, "Clear pool name failed: %s\n",
2745 if (lsa->lsa_nr_tgts > 0) {
2746 if (lsa->lsa_stripe_count > 0 &&
2747 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2748 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2749 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2750 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2751 lsa->lsa_stripe_count,
2756 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2757 rc = llapi_layout_ost_index_set(layout, i,
2762 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2763 lsa->lsa_stripe_off != -1) {
2764 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2767 fprintf(stderr, "Set ost index %d failed. %s\n",
2768 i, strerror(errno));
2772 /* Create the second, virtual component of extension space */
2773 if (lsa->lsa_extension_comp) {
2774 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2775 lsa->lsa_extension_comp = false;
2782 static int build_component(struct llapi_layout **layout,
2783 struct lfs_setstripe_args *lsa, bool set_extent)
2787 rc = comp_args_to_layout(layout, lsa, set_extent);
2791 if (lsa->lsa_mirror_count > 0) {
2792 rc = llapi_layout_mirror_count_set(*layout,
2793 lsa->lsa_mirror_count);
2797 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2800 lsa->lsa_mirror_count = 0;
2806 static int build_prev_component(struct llapi_layout **layout,
2807 struct lfs_setstripe_args *prev,
2808 struct lfs_setstripe_args *lsa,
2811 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2814 if (prev->lsa_stripe_size) {
2816 prev->lsa_comp_end = lsa->lsa_comp_end;
2817 prev->lsa_extension_size = lsa->lsa_extension_size;
2818 prev->lsa_extension_comp = true;
2821 rc = build_component(layout, prev, true);
2827 * Copy lsa to previous lsa;
2828 * if this is an extension component, make the previous invalid;
2831 prev->lsa_stripe_size = 0;
2838 static int build_layout_from_yaml_node(struct cYAML *node,
2839 struct llapi_layout **layout,
2840 struct lfs_setstripe_args *lsa,
2841 struct lfs_setstripe_args *prevp)
2843 struct lfs_setstripe_args prev = { 0 };
2844 __u32 *osts = lsa->lsa_tgts;
2852 string = node->cy_string;
2854 if (node->cy_type == CYAML_TYPE_OBJECT) {
2855 /* go deep to sub blocks */
2856 if (string && !strncmp(string, "component", 9) &&
2857 strncmp(string, "component0", 10) &&
2858 strncmp(string, "components", 10)) {
2859 rc = build_prev_component(layout, prevp, lsa,
2864 /* initialize lsa. */
2865 setstripe_args_init(lsa);
2866 lsa->lsa_first_comp = false;
2867 lsa->lsa_tgts = osts;
2870 rc = build_layout_from_yaml_node(node->cy_child, layout,
2875 if (!node->cy_string)
2878 /* skip leading lmm_ if present, to simplify parsing */
2879 if (strncmp(string, "lmm_", 4) == 0)
2882 if (node->cy_type == CYAML_TYPE_STRING) {
2883 if (!strcmp(string, "lcme_extent.e_end")) {
2884 if (!strcmp(node->cy_valuestring, "EOF") ||
2885 !strcmp(node->cy_valuestring, "eof"))
2886 lsa->lsa_comp_end = LUSTRE_EOF;
2887 } else if (!strcmp(string, "pool")) {
2888 lsa->lsa_pool_name = node->cy_valuestring;
2889 } else if (!strcmp(string, "pattern")) {
2890 if (!strcmp(node->cy_valuestring, "mdt"))
2891 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2892 if (!strcmp(node->cy_valuestring,
2893 "raid0,overstriped"))
2895 LLAPI_LAYOUT_OVERSTRIPING;
2896 } else if (!strcmp(string, "lcme_flags")) {
2897 rc = comp_str2flags(node->cy_valuestring,
2898 &lsa->lsa_comp_flags,
2899 &lsa->lsa_comp_neg_flags);
2903 * Only template flags have meaning in
2904 * the layout for a new file
2906 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2908 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2909 if (!strcmp(string, "lcm_mirror_count")) {
2910 lsa->lsa_mirror_count = node->cy_valueint;
2911 } else if (!strcmp(string, "lcme_extent.e_start")) {
2912 if (node->cy_valueint == 0)
2913 lsa->lsa_first_comp = true;
2914 } else if (!strcmp(string, "lcme_extent.e_end")) {
2915 if (node->cy_valueint == -1)
2916 lsa->lsa_comp_end = LUSTRE_EOF;
2918 lsa->lsa_comp_end = node->cy_valueint;
2919 } else if (!strcmp(string, "stripe_count")) {
2920 lsa->lsa_stripe_count = node->cy_valueint;
2921 } else if (!strcmp(string, "stripe_size")) {
2922 lsa->lsa_stripe_size = node->cy_valueint;
2923 } else if (!strcmp(string, "extension_size")) {
2924 lsa->lsa_extension_size = node->cy_valueint;
2925 lsa->lsa_extension_comp = true;
2926 } else if (!strcmp(string, "stripe_offset")) {
2927 lsa->lsa_stripe_off = node->cy_valueint;
2928 } else if (!strcmp(string, "l_ost_idx")) {
2929 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2934 node = node->cy_next;
2937 if (prevp == &prev) {
2938 rc = build_prev_component(layout, prevp, lsa, true);
2942 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2943 rc = build_component(layout, lsa, *layout != NULL);
2949 static int lfs_comp_create_from_yaml(char *template,
2950 struct llapi_layout **layout,
2951 struct lfs_setstripe_args *lsa,
2954 struct cYAML *tree = NULL, *err_rc = NULL;
2957 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2959 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2960 progname, template);
2961 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2962 "can't parse", &err_rc);
2963 cYAML_print_tree2file(stderr, err_rc);
2964 cYAML_free_tree(err_rc);
2969 /* initialize lsa for plain file */
2970 setstripe_args_init(lsa);
2971 lsa->lsa_tgts = osts;
2973 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2975 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2976 progname, template);
2979 /* clean clean lsa */
2980 setstripe_args_init(lsa);
2984 cYAML_free_tree(tree);
2989 * Get the extension size from the next (SEL) component and extend the
2990 * current component on it. The start of the next component is to be
2993 * \param[in] layout the current layout
2994 * \param[in] start the start of the current component
2995 * \param[in,out] end the end of the current component
2996 * \param[in] offset the offset to adjust the end position to instead of
2999 * \retval 0 - extended successfully
3000 * \retval < 0 - error
3002 static int layout_extend_comp(struct llapi_layout *layout,
3003 uint64_t start, uint64_t *end,
3006 uint64_t size, next_start, next_end;
3009 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3012 "%s setstripe: cannot move component cursor: %s\n",
3013 progname, strerror(errno));
3018 * Even if the @size will not be used below, this will fail if
3019 * this is not a SEL component - a good confirmation we are
3020 * working on right components.
3022 rc = llapi_layout_extension_size_get(layout, &size);
3025 "%s setstripe: cannot get component ext size: %s\n",
3026 progname, strerror(errno));
3030 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3032 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3033 progname, strerror(errno));
3037 next_start += offset ?: size;
3038 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3040 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3041 progname, strerror(errno));
3045 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3048 "%s setstripe: cannot move component cursor: %s\n",
3049 progname, strerror(errno));
3053 *end += offset ?: size;
3054 rc = llapi_layout_comp_extent_set(layout, start, *end);
3056 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3057 progname, strerror(errno));
3065 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3066 * end of the last component in the existing file, and adjust the
3067 * first extent start of the components to be added accordingly.
3069 * In the create mode, we need to check if the first component is an extendable
3070 * SEL component and extend its length to the extension size (first component
3071 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3073 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3076 struct llapi_layout *head;
3077 uint64_t start = 0, prev_end = 0;
3081 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3086 head = llapi_layout_get_by_path(fname, 0);
3089 "%s setstripe: cannot read layout from '%s': %s\n",
3090 progname, fname, strerror(errno));
3092 } else if (errno == ENODATA) {
3094 * file without LOVEA, this component-add will be turned
3095 * into a component-create.
3097 llapi_layout_free(head);
3101 * the new layout will be added to an empty one, it
3102 * still needs to be adjusted below
3106 } else if (!llapi_layout_is_composite(head)) {
3108 "%s setstripe: '%s' not a composite file\n",
3110 llapi_layout_free(head);
3114 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3117 "%s setstripe: cannot get prev extent: %s\n",
3118 progname, strerror(errno));
3119 llapi_layout_free(head);
3123 llapi_layout_free(head);
3127 /* Make sure we use the first component of the layout to be added. */
3128 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3131 "%s setstripe: cannot move component cursor: %s\n",
3132 progname, strerror(errno));
3136 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3138 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3139 progname, strerror(errno));
3143 if (start == 0 && end == 0) {
3144 rc = layout_extend_comp(layout, start, &end,
3145 comp_add ? prev_end : 0);
3150 if (start > prev_end || end < prev_end) {
3152 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3153 progname, start, end, prev_end);
3157 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3160 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3161 progname, prev_end, end, strerror(errno));
3168 static int mirror_adjust_first_extents(struct mirror_args *list)
3176 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3179 list = list->m_next;
3185 static inline bool arg_is_eof(char *arg)
3187 return !strncmp(arg, "-1", strlen("-1")) ||
3188 !strncmp(arg, "EOF", strlen("EOF")) ||
3189 !strncmp(arg, "eof", strlen("eof"));
3193 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3195 * Return: Valid mirror_args pointer on success and
3196 * NULL if memory allocation fails.
3198 static struct mirror_args *lfs_mirror_alloc(void)
3200 struct mirror_args *mirror = NULL;
3203 mirror = calloc(1, sizeof(*mirror));
3205 mirror->m_inherit = false;
3216 * lfs_mirror_free() - Free memory allocated for a mirror argument
3218 * @mirror: Previously allocated mirror argument structure by
3219 * lfs_mirror_alloc().
3221 * Free memory allocated for @mirror.
3225 static void lfs_mirror_free(struct mirror_args *mirror)
3227 if (mirror->m_layout)
3228 llapi_layout_free(mirror->m_layout);
3233 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3234 * @mirror_list: Previously allocated mirror list.
3236 * Free memory allocated for @mirror_list.
3240 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3242 struct mirror_args *next_mirror = NULL;
3244 while (mirror_list) {
3245 next_mirror = mirror_list->m_next;
3246 lfs_mirror_free(mirror_list);
3247 mirror_list = next_mirror;
3259 LFS_COMP_NO_VERIFY_OPT,
3261 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3263 LFS_MIRROR_STATE_OPT,
3265 LFS_MIRROR_INDEX_OPT,
3266 LFS_LAYOUT_FOREIGN_OPT,
3272 static int lfs_setstripe_internal(int argc, char **argv,
3273 enum setstripe_origin opc)
3275 struct lfs_setstripe_args lsa = { 0 };
3276 struct llapi_stripe_param *param = NULL;
3277 struct find_param migrate_mdt_param = {
3287 unsigned long long size_units = 1;
3288 bool migrate_mode = false;
3289 bool migrate_mdt_mode = false;
3290 bool setstripe_mode = false;
3291 bool migration_block = false;
3292 __u64 migration_flags = 0;
3293 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3294 int comp_del = 0, comp_set = 0;
3297 struct llapi_layout *layout = NULL;
3298 struct llapi_layout **lpp = &layout;
3299 bool mirror_mode = false;
3300 bool has_m_file = false;
3301 __u32 mirror_count = 0;
3302 enum mirror_flags mirror_flags = 0;
3303 struct mirror_args *mirror_list = NULL;
3304 struct mirror_args *new_mirror = NULL;
3305 struct mirror_args *last_mirror = NULL;
3306 __u16 mirror_id = 0;
3308 bool from_yaml = false;
3309 bool from_copy = false;
3310 char *template = NULL;
3311 bool foreign_mode = false;
3313 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3314 char *mode_opt = NULL;
3315 mode_t previous_umask = 0;
3316 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3318 struct option long_opts[] = {
3319 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3320 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3321 /* --block is only valid in migrate mode */
3322 { .val = 'b', .name = "block", .has_arg = no_argument },
3323 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3324 { .val = LFS_COMP_ADD_OPT,
3325 .name = "comp-add", .has_arg = no_argument },
3326 { .val = LFS_COMP_ADD_OPT,
3327 .name = "component-add", .has_arg = no_argument },
3328 { .val = LFS_COMP_DEL_OPT,
3329 .name = "comp-del", .has_arg = no_argument },
3330 { .val = LFS_COMP_DEL_OPT,
3331 .name = "component-del", .has_arg = no_argument },
3332 { .val = LFS_COMP_FLAGS_OPT,
3333 .name = "comp-flags", .has_arg = required_argument },
3334 { .val = LFS_COMP_FLAGS_OPT,
3335 .name = "component-flags",
3336 .has_arg = required_argument },
3337 { .val = LFS_COMP_SET_OPT,
3338 .name = "comp-set", .has_arg = no_argument },
3339 { .val = LFS_COMP_SET_OPT,
3340 .name = "component-set",
3341 .has_arg = no_argument},
3342 { .val = LFS_COMP_NO_VERIFY_OPT,
3343 .name = "no-verify", .has_arg = no_argument},
3344 { .val = LFS_LAYOUT_FLAGS_OPT,
3345 .name = "flags", .has_arg = required_argument},
3346 { .val = LFS_LAYOUT_FOREIGN_OPT,
3347 .name = "foreign", .has_arg = optional_argument},
3348 { .val = LFS_MIRROR_ID_OPT,
3349 .name = "mirror-id", .has_arg = required_argument},
3350 { .val = LFS_MODE_OPT,
3351 .name = "mode", .has_arg = required_argument},
3352 { .val = LFS_LAYOUT_COPY,
3353 .name = "copy", .has_arg = required_argument},
3354 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3355 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3356 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3357 { .val = 'C', .name = "overstripe-count",
3358 .has_arg = required_argument},
3359 { .val = 'd', .name = "delete", .has_arg = no_argument},
3360 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3361 /* --non-direct is only valid in migrate mode */
3362 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3363 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3364 { .val = 'E', .name = "component-end",
3365 .has_arg = required_argument},
3366 { .val = 'f', .name = "file", .has_arg = required_argument },
3367 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3368 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3369 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3370 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3371 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3372 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3373 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3374 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3375 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3376 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3377 { .val = 'L', .name = "layout", .has_arg = required_argument },
3378 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3379 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3380 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3381 /* --non-block is only valid in migrate mode */
3382 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3383 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3384 { .val = 'o', .name = "ost", .has_arg = required_argument },
3385 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3386 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3387 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3389 { .val = 'p', .name = "pool", .has_arg = required_argument },
3390 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3391 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3392 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3393 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3394 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3395 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3396 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3397 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3398 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3399 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3400 /* --verbose is only valid in migrate mode */
3401 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3402 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3403 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3404 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3405 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3408 setstripe_args_init(&lsa);
3410 migrate_mode = (opc == SO_MIGRATE);
3411 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3412 setstripe_mode = (opc == SO_SETSTRIPE);
3413 if (opc == SO_MIRROR_DELETE) {
3415 mirror_flags = MF_DESTROY;
3418 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3420 while ((c = getopt_long(argc, argv,
3421 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3422 long_opts, NULL)) >= 0) {
3428 case LFS_COMP_ADD_OPT:
3431 case LFS_COMP_DEL_OPT:
3434 case LFS_COMP_FLAGS_OPT:
3435 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3436 &lsa.lsa_comp_neg_flags);
3439 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3441 "%s: inverted flags are not supported\n",
3446 case LFS_COMP_SET_OPT:
3449 case LFS_COMP_NO_VERIFY_OPT:
3450 mirror_flags |= MF_NO_VERIFY;
3452 case LFS_MIRROR_ID_OPT: {
3453 unsigned long int id;
3456 id = strtoul(optarg, &end, 0);
3457 if (errno != 0 || *end != '\0' || id == 0 ||
3460 "%s %s: invalid mirror ID '%s'\n",
3461 progname, argv[0], optarg);
3465 mirror_id = (__u16)id;
3468 case LFS_LAYOUT_FLAGS_OPT: {
3471 /* check for numeric flags (foreign and mirror cases) */
3472 if (setstripe_mode && !mirror_mode && !last_mirror) {
3474 flags = strtoul(optarg, &end, 16);
3475 if (errno != 0 || *end != '\0' ||
3476 flags >= UINT32_MAX) {
3478 "%s %s: invalid hex flags '%s'\n",
3479 progname, argv[0], optarg);
3482 if (!foreign_mode) {
3484 "%s %s: hex flags must be specified with --foreign option\n",
3491 if (!mirror_mode || !last_mirror) {
3493 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3498 result = comp_str2flags(optarg, &last_mirror->m_flags,
3505 "%s: inverted flags are not supported\n",
3510 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3512 "%s: unsupported mirror flags: %s\n",
3519 case LFS_LAYOUT_FOREIGN_OPT:
3521 /* check pure numeric */
3522 type = strtoul(optarg, &end, 0);
3525 type = check_foreign_type_name(optarg);
3526 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3528 "%s %s: unrecognized foreign type '%s'\n",
3533 } else if (type >= UINT32_MAX) {
3535 "%s %s: invalid foreign type '%s'\n",
3536 progname, argv[0], optarg);
3540 foreign_mode = true;
3545 mode = strtoul(mode_opt, &end, 8);
3548 "%s %s: bad mode '%s'\n",
3549 progname, argv[0], mode_opt);
3552 previous_umask = umask(0);
3555 case LFS_LAYOUT_COPY:
3560 if (!migrate_mode) {
3562 "%s %s: -b|--block valid only for migrate command\n",
3566 migration_block = true;
3569 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3571 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3575 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3579 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3580 if (errno != 0 || *end != '\0'||
3581 lsa.lsa_stripe_count < -1 ||
3582 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3584 "%s %s: invalid stripe count '%s'\n",
3585 progname, argv[0], optarg);
3589 if (lsa.lsa_stripe_count == -1)
3590 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3593 /* delete the default striping pattern */
3595 if (opc == SO_MIRROR_SPLIT) {
3598 "%s %s: -d cannot used with -f\n",
3602 mirror_flags |= MF_DESTROY;
3606 if (!migrate_mode) {
3608 "%s %s: -D|--non-direct is valid only for migrate command\n",
3612 migration_flags |= MIGRATION_NONDIRECT;
3615 if (lsa.lsa_comp_end != 0) {
3616 result = comp_args_to_layout(lpp, &lsa, true);
3618 fprintf(stderr, "%s: invalid layout\n",
3623 setstripe_args_init_inherit(&lsa);
3626 if (arg_is_eof(optarg)) {
3627 lsa.lsa_comp_end = LUSTRE_EOF;
3629 result = llapi_parse_size(optarg,
3632 /* assume units of KB if too small */
3633 if (lsa.lsa_comp_end < 4096)
3634 lsa.lsa_comp_end *= 1024;
3636 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3638 "%s %s: invalid component end '%s'\n",
3639 progname, argv[0], optarg);
3645 if (!migrate_mode) {
3647 "--mdt-hash is valid only for migrate command\n");
3651 lsa.lsa_pattern = check_hashtype(optarg);
3652 if (lsa.lsa_pattern == 0) {
3654 "%s %s: bad stripe hash type '%s'\n",
3655 progname, argv[0], optarg);
3661 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3662 if (errno != 0 || *end != '\0' ||
3663 lsa.lsa_stripe_off < -1 ||
3664 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3666 "%s %s: invalid stripe offset '%s'\n",
3667 progname, argv[0], optarg);
3670 if (lsa.lsa_stripe_off == -1)
3671 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3674 comp_id = strtoul(optarg, &end, 0);
3675 if (*end != '\0' || comp_id == 0 ||
3676 comp_id > LCME_ID_MAX) {
3678 "%s %s: invalid component ID '%s'\n",
3679 progname, argv[0], optarg);
3684 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3686 "error: %s: invalid option: %s\n",
3687 progname, argv[optopt + 1]);
3690 if (opc == SO_MIRROR_EXTEND) {
3693 "error: %s: '-N' must exist in front of '%s'\n",
3694 progname, argv[optopt + 1]);
3697 last_mirror->m_file = optarg;
3698 last_mirror->m_count = 1;
3702 mirror_list = lfs_mirror_alloc();
3703 mirror_list->m_file = optarg;
3708 if (strcmp(argv[optind - 1], "mdt") == 0) {
3709 /* Can be only the first component */
3713 "error: 'mdt' layout can be only the first one\n");
3716 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3719 "error: 'mdt' layout size is too big\n");
3722 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3723 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3726 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3732 if (!migrate_mode) {
3734 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3738 migrate_mdt_mode = true;
3739 lsa.lsa_nr_tgts = parse_targets(tgts,
3740 sizeof(tgts) / sizeof(__u32),
3741 lsa.lsa_nr_tgts, optarg, NULL);
3742 if (lsa.lsa_nr_tgts < 0) {
3744 "%s %s: invalid MDT target(s) '%s'\n",
3745 progname, argv[0], optarg);
3749 lsa.lsa_tgts = tgts;
3750 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3751 lsa.lsa_stripe_off = tgts[0];
3754 if (!migrate_mode) {
3756 "%s %s: -n|--non-block valid only for migrate command\n",
3760 migration_flags |= MIGRATION_NONBLOCK;
3763 if (opc == SO_SETSTRIPE) {
3764 opc = SO_MIRROR_CREATE;
3770 mirror_count = strtoul(optarg, &end, 0);
3771 if (errno != 0 || *end != '\0' ||
3772 mirror_count == 0 ||
3773 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3775 "error: %s: bad mirror count: %s\n",
3782 new_mirror = lfs_mirror_alloc();
3783 new_mirror->m_count = mirror_count;
3786 mirror_list = new_mirror;
3789 /* wrap up last mirror */
3790 if (!setstripe_args_specified(&lsa))
3791 last_mirror->m_inherit = true;
3792 if (lsa.lsa_comp_end == 0)
3793 lsa.lsa_comp_end = LUSTRE_EOF;
3795 result = comp_args_to_layout(lpp, &lsa, true);
3797 lfs_mirror_free(new_mirror);
3801 setstripe_args_init_inherit(&lsa);
3803 last_mirror->m_next = new_mirror;
3806 last_mirror = new_mirror;
3807 lpp = &last_mirror->m_layout;
3810 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3811 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3813 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3815 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3817 "%s %s: -o|--ost incompatible with DoM layout\n",
3822 * -o allows overstriping, and must note it because
3823 * parse_targets is shared with MDT striping, which
3824 * does not allow duplicates
3826 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3827 lsa.lsa_nr_tgts = parse_targets(tgts,
3828 sizeof(tgts) / sizeof(__u32),
3829 lsa.lsa_nr_tgts, optarg,
3831 if (lsa.lsa_nr_tgts < 0) {
3833 "%s %s: invalid OST target(s) '%s'\n",
3834 progname, argv[0], optarg);
3838 lsa.lsa_tgts = tgts;
3839 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3840 lsa.lsa_stripe_off = tgts[0];
3845 lsa.lsa_pool_name = optarg;
3847 if (strlen(lsa.lsa_pool_name) == 0 ||
3848 strncmp(lsa.lsa_pool_name, "none",
3849 LOV_MAXPOOLNAME) == 0)
3850 lsa.lsa_pool_name = NULL;
3853 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3855 /* assume units of KB if too small to be valid */
3856 if (lsa.lsa_stripe_size < 4096)
3857 lsa.lsa_stripe_size *= 1024;
3859 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3861 "%s %s: invalid stripe size '%s'\n",
3862 progname, argv[0], optarg);
3867 if (!migrate_mode) {
3869 "%s %s: -v|--verbose valid only for migrate command\n",
3873 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3874 migration_flags = MIGRATION_VERBOSE;
3884 result = llapi_parse_size(optarg,
3885 &lsa.lsa_extension_size,
3889 "%s %s: invalid extension size '%s'\n",
3890 progname, argv[0], optarg);
3894 lsa.lsa_extension_comp = true;
3897 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3898 progname, argv[0], argv[optind - 1]);
3903 fname = argv[optind];
3905 if (optind == argc) {
3906 fprintf(stderr, "%s %s: FILE must be specified\n",
3911 /* lfs migrate $filename should keep the file's layout by default */
3912 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3916 if (xattr && !foreign_mode) {
3918 * only print a warning as this is harmless and will be ignored
3921 "%s %s: xattr has been specified for non-foreign layout\n",
3923 } else if (foreign_mode && !xattr) {
3925 "%s %s: xattr must be provided in foreign mode\n",
3930 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3931 comp_set || comp_id || delete || from_copy ||
3932 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3935 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3940 if (mirror_mode && mirror_count == 0) {
3942 "error: %s: --mirror-count|-N option is required\n",
3949 if (!setstripe_args_specified(&lsa))
3950 last_mirror->m_inherit = true;
3951 if (lsa.lsa_comp_end == 0)
3952 lsa.lsa_comp_end = LUSTRE_EOF;
3955 if (lsa.lsa_comp_end != 0) {
3956 result = comp_args_to_layout(lpp, &lsa, true);
3958 fprintf(stderr, "error: %s: invalid layout\n",
3965 if (mirror_flags & MF_NO_VERIFY) {
3966 if (opc != SO_MIRROR_EXTEND) {
3968 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3972 } else if (!has_m_file) {
3974 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3981 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
3983 "%s %s: --component-set doesn't have component-id set\n",
3988 if ((delete + comp_set + comp_del + comp_add) > 1) {
3990 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3995 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3996 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3998 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4003 if ((comp_set || comp_del) &&
4004 (setstripe_args_specified(&lsa) || layout != NULL)) {
4006 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4011 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4013 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4018 if (comp_add || comp_del) {
4021 result = lstat(fname, &st);
4022 if (result == 0 && S_ISDIR(st.st_mode)) {
4024 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4031 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4040 "%s %s: option -E must be specified with --component-add\n",
4046 if (from_yaml && from_copy) {
4048 "%s: can't specify --yaml and --copy together\n",
4053 if ((from_yaml || from_copy) &&
4054 (setstripe_args_specified(&lsa) || layout != NULL)) {
4056 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4061 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4063 "%s %s: options --non-block and --block are mutually exclusive\n",
4068 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4069 opc != SO_MIRROR_DELETE && comp_id != 0) {
4071 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4076 if (migrate_mdt_mode) {
4077 struct lmv_user_md *lmu;
4079 /* initialize migrate mdt parameters */
4080 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4081 LMV_USER_MAGIC_SPECIFIC));
4084 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4085 progname, argv[0], strerror(ENOMEM));
4089 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4090 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4091 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4093 "%s %s: migrate should specify MDT index\n",
4098 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4099 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4100 lmu->lum_hash_type = lsa.lsa_pattern;
4102 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4103 if (lsa.lsa_pool_name) {
4104 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4105 sizeof(lmu->lum_pool_name) - 1);
4106 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4108 if (lsa.lsa_nr_tgts > 1) {
4111 if (lsa.lsa_stripe_count > 0 &&
4112 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4113 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4115 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4116 progname, lsa.lsa_stripe_count,
4122 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4123 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4124 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4125 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4127 lmu->lum_magic = LMV_USER_MAGIC;
4130 migrate_mdt_param.fp_lmv_md = lmu;
4131 migrate_mdt_param.fp_migrate = 1;
4132 } else if (!layout) {
4133 /* initialize stripe parameters */
4134 param = calloc(1, offsetof(typeof(*param),
4135 lsp_osts[lsa.lsa_nr_tgts]));
4138 "%s %s: cannot allocate memory for parameters: %s\n",
4139 progname, argv[0], strerror(ENOMEM));
4144 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4145 param->lsp_stripe_size = lsa.lsa_stripe_size;
4146 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4147 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4148 param->lsp_stripe_count = -1;
4150 param->lsp_stripe_count = lsa.lsa_stripe_count;
4152 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4153 param->lsp_stripe_offset = -1;
4155 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4156 param->lsp_stripe_pattern =
4157 llapi_pattern_to_lov(lsa.lsa_pattern);
4158 if (param->lsp_stripe_pattern == EINVAL) {
4159 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4164 param->lsp_pool = lsa.lsa_pool_name;
4165 param->lsp_is_specific = false;
4166 if (lsa.lsa_nr_tgts > 0) {
4167 if (lsa.lsa_stripe_count > 0 &&
4168 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4169 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4170 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4172 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4173 argv[0], lsa.lsa_stripe_count,
4179 param->lsp_is_specific = true;
4180 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4181 memcpy(param->lsp_osts, tgts,
4182 sizeof(*tgts) * lsa.lsa_nr_tgts);
4187 /* generate a layout from a YAML template */
4188 result = lfs_comp_create_from_yaml(template, &layout,
4192 "error: %s: can't create composite layout from template file %s\n",
4198 if (layout != NULL || mirror_list != NULL) {
4200 result = mirror_adjust_first_extents(mirror_list);
4202 result = layout_adjust_first_extent(fname, layout,
4204 if (result == -ENODATA)
4206 else if (result != 0) {
4207 fprintf(stderr, "error: %s: invalid layout\n",
4213 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4215 layout = llapi_layout_get_by_path(template ?: fname, 0);
4218 "%s: can't create composite layout from file %s: %s\n",
4219 progname, template ?: fname,
4226 if (migrate_mdt_mode) {
4227 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4228 } else if (migrate_mode) {
4229 result = lfs_migrate(fname, migration_flags, param,
4231 } else if (comp_set != 0) {
4232 result = lfs_component_set(fname, comp_id,
4235 lsa.lsa_comp_neg_flags);
4236 } else if (comp_del != 0) {
4237 result = lfs_component_del(fname, comp_id,
4239 lsa.lsa_comp_neg_flags);
4240 } else if (comp_add != 0) {
4241 result = lfs_component_add(fname, layout);
4242 } else if (opc == SO_MIRROR_CREATE) {
4243 result = mirror_create(fname, mirror_list);
4244 } else if (opc == SO_MIRROR_EXTEND) {
4245 result = mirror_extend(fname, mirror_list,
4247 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4248 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4250 "%s: no mirror specified to delete from '%s'\n",
4254 if (lsa.lsa_pool_name)
4255 mirror_flags |= MF_COMP_POOL;
4256 else if (mirror_id != 0)
4257 comp_id = mirror_id;
4259 mirror_flags |= MF_COMP_ID;
4260 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4262 has_m_file ? mirror_list->m_file :
4264 } else if (layout) {
4265 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4271 } else if (foreign_mode) {
4272 result = llapi_file_create_foreign(fname, mode, type,
4279 result = llapi_file_open_param(fname,
4288 /* Save the first error encountered. */
4296 umask(previous_umask);
4299 free(migrate_mdt_param.fp_lmv_md);
4300 llapi_layout_free(layout);
4301 lfs_mirror_list_free(mirror_list);
4306 llapi_layout_free(layout);
4307 lfs_mirror_list_free(mirror_list);
4311 static int lfs_poollist(int argc, char **argv)
4316 return llapi_poollist(argv[1]);
4319 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4320 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4325 char *endptr = "AD";
4330 else if (str[0] == '-')
4336 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4337 long long val = strtoll(timebuf, &endptr, 0);
4342 unit *= 52; /* 52 weeks + 1 day below */
4343 case 'w': /* fallthrough */
4345 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4346 param->fp_time_margin *= (1 + unit / 52);
4347 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4348 case '\0': /* days are default unit if none used */
4349 case 'd': /* fallthrough */
4351 case 'h': /* fallthrough */
4353 case 'm': /* fallthrough */
4355 case 's': /* fallthrough */
4357 /* don't need to multiply by 1 for seconds */
4360 "%s find: bad time string '%s': %s\n",
4361 progname, timebuf, strerror(EINVAL));
4365 if (param->fp_time_margin == 0 ||
4366 (*endptr && unit < param->fp_time_margin))
4367 param->fp_time_margin = unit;
4374 fprintf(stderr, "%s find: bad time '%s': too large\n",
4384 static int str2quotaid(__u32 *id, const char *arg)
4386 unsigned long int projid_tmp = 0;
4387 char *endptr = NULL;
4389 projid_tmp = strtoul(arg, &endptr, 10);
4390 if (*endptr != '\0')
4392 if (projid_tmp >= UINT32_MAX)
4399 static int name2uid(unsigned int *id, const char *name)
4401 struct passwd *passwd;
4403 passwd = getpwnam(name);
4406 *id = passwd->pw_uid;
4411 static int name2gid(unsigned int *id, const char *name)
4413 struct group *group;
4415 group = getgrnam(name);
4418 *id = group->gr_gid;
4423 static inline int name2projid(unsigned int *id, const char *name)
4428 static int uid2name(char **name, unsigned int id)
4430 struct passwd *passwd;
4432 passwd = getpwuid(id);
4435 *name = passwd->pw_name;
4440 static inline int gid2name(char **name, unsigned int id)
4442 struct group *group;
4444 group = getgrgid(id);
4447 *name = group->gr_name;
4452 static int name2layout(__u32 *layout, char *name)
4454 char *ptr, *layout_name;
4457 for (ptr = name; ; ptr = NULL) {
4458 layout_name = strtok(ptr, ",");
4461 if (strcmp(layout_name, "released") == 0)
4462 *layout |= LOV_PATTERN_F_RELEASED;
4463 else if (strcmp(layout_name, "raid0") == 0)
4464 *layout |= LOV_PATTERN_RAID0;
4465 else if (strcmp(layout_name, "mdt") == 0)
4466 *layout |= LOV_PATTERN_MDT;
4467 else if (strcmp(layout_name, "overstriping") == 0)
4468 *layout |= LOV_PATTERN_OVERSTRIPING;
4475 static int lfs_find(int argc, char **argv)
4480 struct find_param param = {
4483 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4485 struct option long_opts[] = {
4486 { .val = 'A', .name = "atime", .has_arg = required_argument },
4487 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4488 { .val = 'B', .name = "btime", .has_arg = required_argument },
4489 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4490 { .val = LFS_COMP_COUNT_OPT,
4491 .name = "comp-count", .has_arg = required_argument },
4492 { .val = LFS_COMP_COUNT_OPT,
4493 .name = "component-count",
4494 .has_arg = required_argument },
4495 { .val = LFS_COMP_FLAGS_OPT,
4496 .name = "comp-flags", .has_arg = required_argument },
4497 { .val = LFS_COMP_FLAGS_OPT,
4498 .name = "component-flags",
4499 .has_arg = required_argument },
4500 { .val = LFS_COMP_START_OPT,
4501 .name = "comp-start", .has_arg = required_argument },
4502 { .val = LFS_COMP_START_OPT,
4503 .name = "component-start",
4504 .has_arg = required_argument },
4505 { .val = LFS_MIRROR_STATE_OPT,
4506 .name = "mirror-state", .has_arg = required_argument },
4507 { .val = LFS_NEWERXY_OPT,
4508 .name = "newer", .has_arg = required_argument},
4509 { .val = LFS_NEWERXY_OPT,
4510 .name = "neweraa", .has_arg = required_argument},
4511 { .val = LFS_NEWERXY_OPT,
4512 .name = "neweram", .has_arg = required_argument},
4513 { .val = LFS_NEWERXY_OPT,
4514 .name = "newerac", .has_arg = required_argument},
4515 { .val = LFS_NEWERXY_OPT,
4516 .name = "newerab", .has_arg = required_argument},
4517 { .val = LFS_NEWERXY_OPT,
4518 .name = "newerma", .has_arg = required_argument},
4519 { .val = LFS_NEWERXY_OPT,
4520 .name = "newermm", .has_arg = required_argument},
4521 { .val = LFS_NEWERXY_OPT,
4522 .name = "newermc", .has_arg = required_argument},
4523 { .val = LFS_NEWERXY_OPT,
4524 .name = "newermb", .has_arg = required_argument},
4525 { .val = LFS_NEWERXY_OPT,
4526 .name = "newerca", .has_arg = required_argument},
4527 { .val = LFS_NEWERXY_OPT,
4528 .name = "newercm", .has_arg = required_argument},
4529 { .val = LFS_NEWERXY_OPT,
4530 .name = "newercc", .has_arg = required_argument},
4531 { .val = LFS_NEWERXY_OPT,
4532 .name = "newercb", .has_arg = required_argument},
4533 { .val = LFS_NEWERXY_OPT,
4534 .name = "newerba", .has_arg = required_argument},
4535 { .val = LFS_NEWERXY_OPT,
4536 .name = "newerbm", .has_arg = required_argument},
4537 { .val = LFS_NEWERXY_OPT,
4538 .name = "newerbc", .has_arg = required_argument},
4539 { .val = LFS_NEWERXY_OPT,
4540 .name = "newerbb", .has_arg = required_argument},
4541 { .val = LFS_NEWERXY_OPT,
4542 .name = "newerBa", .has_arg = required_argument},
4543 { .val = LFS_NEWERXY_OPT,
4544 .name = "newerBm", .has_arg = required_argument},
4545 { .val = LFS_NEWERXY_OPT,
4546 .name = "newerBc", .has_arg = required_argument},
4547 { .val = LFS_NEWERXY_OPT,
4548 .name = "newerBB", .has_arg = required_argument},
4549 { .val = LFS_NEWERXY_OPT,
4550 .name = "newerat", .has_arg = required_argument},
4551 { .val = LFS_NEWERXY_OPT,
4552 .name = "newermt", .has_arg = required_argument},
4553 { .val = LFS_NEWERXY_OPT,
4554 .name = "newerct", .has_arg = required_argument},
4555 { .val = LFS_NEWERXY_OPT,
4556 .name = "newerbt", .has_arg = required_argument},
4557 { .val = LFS_NEWERXY_OPT,
4558 .name = "newerBt", .has_arg = required_argument},
4559 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4560 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4561 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4562 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4563 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4564 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4565 { .val = 'E', .name = "component-end",
4566 .has_arg = required_argument },
4567 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4568 { .val = LFS_LAYOUT_FOREIGN_OPT,
4569 .name = "foreign", .has_arg = optional_argument},
4570 { .val = 'g', .name = "gid", .has_arg = required_argument },
4571 { .val = 'G', .name = "group", .has_arg = required_argument },
4572 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4573 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4574 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4575 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4576 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4577 { .val = 'L', .name = "layout", .has_arg = required_argument },
4578 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4579 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4580 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4581 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4582 { .val = 'n', .name = "name", .has_arg = required_argument },
4583 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4584 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4585 { .val = 'O', .name = "obd", .has_arg = required_argument },
4586 { .val = 'O', .name = "ost", .has_arg = required_argument },
4587 /* no short option for pool yet, can be 'p' after 2.18 */
4588 { .val = LFS_POOL_OPT,
4589 .name = "pool", .has_arg = required_argument },
4590 { .val = '0', .name = "print0", .has_arg = no_argument },
4591 { .val = 'P', .name = "print", .has_arg = no_argument },
4592 { .val = LFS_PROJID_OPT,
4593 .name = "projid", .has_arg = required_argument },
4594 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4595 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4596 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4597 { .val = 's', .name = "size", .has_arg = required_argument },
4598 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4599 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4600 { .val = 't', .name = "type", .has_arg = required_argument },
4601 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4602 { .val = 'u', .name = "uid", .has_arg = required_argument },
4603 { .val = 'U', .name = "user", .has_arg = required_argument },
4604 { .val = 'z', .name = "extension-size",
4605 .has_arg = required_argument },
4606 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4607 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4608 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4622 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4623 while ((c = getopt_long_only(argc, argv,
4624 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4625 long_opts, &optidx)) >= 0) {
4630 /* '!' is part of option */
4632 * when getopt_long_only() finds a string which is not
4633 * an option nor a known option argument it returns 1
4634 * in that case if we already have found pathstart and pathend
4635 * (i.e. we have the list of pathnames),
4636 * the only supported value is "!"
4638 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4639 if (!isoption && pathend != -1) {
4641 "err: %s: filename|dirname must either precede options or follow options\n",
4646 if (!isoption && pathstart == -1)
4647 pathstart = optind - 1;
4648 if (isoption && pathstart != -1 && pathend == -1)
4649 pathend = optind - 2;
4656 * unknown; opt is "!" or path component,
4657 * checking done above.
4659 if (strcmp(optarg, "!") == 0)
4663 xtime = ¶m.fp_atime;
4664 xsign = ¶m.fp_asign;
4665 param.fp_exclude_atime = !!neg_opt;
4666 /* no break, this falls through to 'B' for btime */
4669 xtime = ¶m.fp_btime;
4670 xsign = ¶m.fp_bsign;
4671 param.fp_exclude_btime = !!neg_opt;
4673 /* no break, this falls through to 'C' for ctime */
4676 xtime = ¶m.fp_ctime;
4677 xsign = ¶m.fp_csign;
4678 param.fp_exclude_ctime = !!neg_opt;
4680 /* no break, this falls through to 'M' for mtime */
4683 xtime = ¶m.fp_mtime;
4684 xsign = ¶m.fp_msign;
4685 param.fp_exclude_mtime = !!neg_opt;
4687 rc = set_time(¶m, &t, xtime, optarg);
4688 if (rc == LONG_MAX) {
4696 if (optarg[0] == '+') {
4697 param.fp_blocks_sign = -1;
4699 } else if (optarg[0] == '-') {
4700 param.fp_blocks_sign = 1;
4704 param.fp_blocks_units = 1024;
4705 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4706 ¶m.fp_blocks_units, 0);
4708 fprintf(stderr, "error: bad blocks '%s'\n",
4712 param.fp_check_blocks = 1;
4713 param.fp_exclude_blocks = !!neg_opt;
4715 case LFS_COMP_COUNT_OPT:
4716 if (optarg[0] == '+') {
4717 param.fp_comp_count_sign = -1;
4719 } else if (optarg[0] == '-') {
4720 param.fp_comp_count_sign = 1;
4725 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4726 if (errno != 0 || *endptr != '\0' ||
4727 param.fp_comp_count > UINT32_MAX) {
4729 "error: bad component count '%s'\n",
4733 param.fp_check_comp_count = 1;
4734 param.fp_exclude_comp_count = !!neg_opt;
4736 case LFS_COMP_FLAGS_OPT:
4737 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4738 ¶m.fp_comp_neg_flags);
4741 "error: bad component flags '%s'\n",
4745 param.fp_check_comp_flags = 1;
4747 __u32 flags = param.fp_comp_neg_flags;
4749 param.fp_comp_neg_flags = param.fp_comp_flags;
4750 param.fp_comp_flags = flags;
4753 case LFS_COMP_START_OPT:
4754 if (optarg[0] == '+') {
4755 param.fp_comp_start_sign = -1;
4757 } else if (optarg[0] == '-') {
4758 param.fp_comp_start_sign = 1;
4762 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4763 ¶m.fp_comp_start_units, 0);
4766 "error: bad component start '%s'\n",
4770 param.fp_check_comp_start = 1;
4771 param.fp_exclude_comp_start = !!neg_opt;
4773 case LFS_MIRROR_STATE_OPT:
4774 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4775 ¶m.fp_mirror_neg_state);
4778 "error: bad mirrored file state '%s'\n",
4782 param.fp_check_mirror_state = 1;
4784 __u16 state = param.fp_mirror_neg_state;
4786 param.fp_mirror_neg_state =
4787 param.fp_mirror_state;
4788 param.fp_mirror_state = state;
4792 if (optarg[0] == '+') {
4793 param.fp_stripe_count_sign = -1;
4795 } else if (optarg[0] == '-') {
4796 param.fp_stripe_count_sign = 1;
4801 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4802 if (errno != 0 || *endptr != '\0' ||
4803 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
4805 "error: bad stripe_count '%s'\n",
4810 param.fp_check_stripe_count = 1;
4811 param.fp_exclude_stripe_count = !!neg_opt;
4815 param.fp_max_depth = strtol(optarg, 0, 0);
4816 if (errno != 0 || param.fp_max_depth < 0) {
4818 "error: bad maxdepth '%s'\n",
4825 if (optarg[0] == '+') {
4826 param.fp_comp_end_sign = -1;
4828 } else if (optarg[0] == '-') {
4829 param.fp_comp_end_sign = 1;
4833 if (arg_is_eof(optarg)) {
4834 param.fp_comp_end = LUSTRE_EOF;
4835 param.fp_comp_end_units = 1;
4838 rc = llapi_parse_size(optarg,
4840 ¶m.fp_comp_end_units, 0);
4841 /* assume units of KB if too small */
4842 if (param.fp_comp_end < 4096)
4843 param.fp_comp_end *= 1024;
4847 "error: bad component end '%s'\n",
4851 param.fp_check_comp_end = 1;
4852 param.fp_exclude_comp_end = !!neg_opt;
4854 case LFS_LAYOUT_FOREIGN_OPT: {
4855 /* all types by default */
4856 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4859 /* check pure numeric */
4860 type = strtoul(optarg, &endptr, 0);
4863 type = check_foreign_type_name(optarg);
4864 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4866 "%s %s: unknown foreign type '%s'\n",
4871 } else if (type >= UINT32_MAX) {
4873 "%s %s: invalid foreign type '%s'\n",
4874 progname, argv[0], optarg);
4878 param.fp_foreign_type = type;
4879 param.fp_check_foreign = 1;
4880 param.fp_exclude_foreign = !!neg_opt;
4883 case LFS_NEWERXY_OPT: {
4889 time_t ref = time(NULL);
4891 /* no need to check bad options, they won't get here */
4892 if (strlen(long_opts[optidx].name) == 7) {
4893 x = long_opts[optidx].name[5];
4894 y = long_opts[optidx].name[6];
4898 static const char *const fmts[] = {
4899 "%Y-%m-%d %H:%M:%S",
4902 "%H:%M:%S", /* sometime today */
4911 for (i = 0; fmts[i] != NULL; i++) {
4914 /* Init for times relative to today */
4915 if (strncmp(fmts[i], "%H", 2) == 0)
4916 localtime_r(&ref, &tm);
4918 memset(&tm, 0, sizeof(tm));
4919 ptr = strptime(optarg, fmts[i], &tm);
4921 while (ptr && isspace(*ptr))
4923 if (ptr == optarg + strlen(optarg)) {
4931 "%s: invalid time '%s'\n",
4934 "supported formats are:\n ");
4935 for (i = 0; fmts[i] != NULL; i++)
4936 fprintf(stderr, "'%s', ",
4938 fprintf(stderr, "\n");
4944 } else if (y == 'b' || y == 'B') {
4947 rc = llapi_get_lum_file(optarg, NULL, &stx,
4949 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4950 if (!(stx.stx_mask & STATX_BTIME))
4955 "%s: get btime failed '%s': %s\n",
4961 ref = stx.stx_btime.tv_sec;
4963 struct stat statbuf;
4965 if (stat(optarg, &statbuf) < 0) {
4967 "%s: cannot stat file '%s': %s\n",
4976 ref = statbuf.st_atime;
4979 ref = statbuf.st_mtime;
4982 ref = statbuf.st_ctime;
4986 "%s: invalid Y argument: '%c'\n",
4995 xidx = NEWERXY_ATIME;
4998 xidx = NEWERXY_MTIME;
5001 xidx = NEWERXY_CTIME;
5005 xidx = NEWERXY_BTIME;
5009 "%s: invalid X argument: '%c'\n",
5016 newery = ¶m.fp_newery[xidx][negidx];
5022 *newery = *newery > ref ? ref : *newery;
5024 *newery = *newery > ref ? *newery : ref;
5026 param.fp_newerxy = 1;
5031 rc = name2gid(¶m.fp_gid, optarg);
5033 if (str2quotaid(¶m.fp_gid, optarg)) {
5035 "Group/GID: %s cannot be found.\n",
5041 param.fp_exclude_gid = !!neg_opt;
5042 param.fp_check_gid = 1;
5045 param.fp_hash_type = check_hashtype(optarg);
5046 if (param.fp_hash_type == 0) {
5047 fprintf(stderr, "error: bad hash_type '%s'\n",
5052 param.fp_check_hash_type = 1;
5053 param.fp_exclude_hash_type = !!neg_opt;
5059 ret = name2layout(¶m.fp_layout, optarg);
5062 param.fp_exclude_layout = !!neg_opt;
5063 param.fp_check_layout = 1;
5067 rc = name2uid(¶m.fp_uid, optarg);
5069 if (str2quotaid(¶m.fp_uid, optarg)) {
5071 "User/UID: %s cannot be found.\n",
5077 param.fp_exclude_uid = !!neg_opt;
5078 param.fp_check_uid = 1;
5081 param.fp_pattern = (char *)optarg;
5082 param.fp_exclude_pattern = !!neg_opt;
5085 if (optarg[0] == '+') {
5086 param.fp_mirror_count_sign = -1;
5088 } else if (optarg[0] == '-') {
5089 param.fp_mirror_count_sign = 1;
5094 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5095 if (errno != 0 || *endptr != '\0' ||
5096 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5098 "error: bad mirror count '%s'\n",
5102 param.fp_check_mirror_count = 1;
5103 param.fp_exclude_mirror_count = !!neg_opt;
5108 char *buf, *token, *next, *p;
5112 buf = strdup(optarg);
5118 param.fp_exclude_obd = !!neg_opt;
5121 while (token && *token) {
5122 token = strchr(token, ',');
5129 param.fp_exclude_mdt = !!neg_opt;
5130 param.fp_num_alloc_mdts += len;
5131 tmp = realloc(param.fp_mdt_uuid,
5132 param.fp_num_alloc_mdts *
5133 sizeof(*param.fp_mdt_uuid));
5139 param.fp_mdt_uuid = tmp;
5141 param.fp_exclude_obd = !!neg_opt;
5142 param.fp_num_alloc_obds += len;
5143 tmp = realloc(param.fp_obd_uuid,
5144 param.fp_num_alloc_obds *
5145 sizeof(*param.fp_obd_uuid));
5151 param.fp_obd_uuid = tmp;
5153 for (token = buf; token && *token; token = next) {
5154 struct obd_uuid *puuid;
5158 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5161 ¶m.fp_obd_uuid[param.fp_num_obds++];
5163 p = strchr(token, ',');
5170 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5175 strncpy(puuid->uuid, token,
5176 sizeof(puuid->uuid));
5183 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5187 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5189 "Pool name %s is too long (max %d)\n",
5190 optarg, LOV_MAXPOOLNAME);
5195 * We do check for empty pool because empty pool
5196 * is used to find V1 LOV attributes
5198 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5199 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5200 param.fp_exclude_pool = !!neg_opt;
5201 param.fp_check_pool = 1;
5203 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5204 case 'p': /* want this for --pool, to match getstripe/find */
5206 "warning: -p deprecated, use --print0 or -0\n");
5209 param.fp_zero_end = 1;
5211 case 'P': /* we always print, this option is a no-op */
5213 case LFS_PROJID_OPT:
5214 rc = name2projid(¶m.fp_projid, optarg);
5216 if (str2quotaid(¶m.fp_projid, optarg)) {
5218 "Invalid project ID: %s\n",
5224 param.fp_exclude_projid = !!neg_opt;
5225 param.fp_check_projid = 1;
5228 if (optarg[0] == '+') {
5229 param.fp_size_sign = -1;
5231 } else if (optarg[0] == '-') {
5232 param.fp_size_sign = 1;
5236 ret = llapi_parse_size(optarg, ¶m.fp_size,
5237 ¶m.fp_size_units, 0);
5239 fprintf(stderr, "error: bad file size '%s'\n",
5243 param.fp_check_size = 1;
5244 param.fp_exclude_size = !!neg_opt;
5247 if (optarg[0] == '+') {
5248 param.fp_stripe_size_sign = -1;
5250 } else if (optarg[0] == '-') {
5251 param.fp_stripe_size_sign = 1;
5255 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5256 ¶m.fp_stripe_size_units, 0);
5257 /* assume units of KB if too small to be valid */
5258 if (param.fp_stripe_size < 4096)
5259 param.fp_stripe_size *= 1024;
5261 fprintf(stderr, "error: bad stripe_size '%s'\n",
5265 param.fp_check_stripe_size = 1;
5266 param.fp_exclude_stripe_size = !!neg_opt;
5269 param.fp_exclude_type = !!neg_opt;
5270 switch (optarg[0]) {
5272 param.fp_type = S_IFBLK;
5275 param.fp_type = S_IFCHR;
5278 param.fp_type = S_IFDIR;
5281 param.fp_type = S_IFREG;
5284 param.fp_type = S_IFLNK;
5287 param.fp_type = S_IFIFO;
5290 param.fp_type = S_IFSOCK;
5293 fprintf(stderr, "error: %s: bad type '%s'\n",
5300 if (optarg[0] == '+') {
5301 param.fp_mdt_count_sign = -1;
5303 } else if (optarg[0] == '-') {
5304 param.fp_mdt_count_sign = 1;
5309 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5310 if (errno != 0 || *endptr != '\0' ||
5311 param.fp_mdt_count >= UINT32_MAX) {
5312 fprintf(stderr, "error: bad mdt_count '%s'\n",
5317 param.fp_check_mdt_count = 1;
5318 param.fp_exclude_mdt_count = !!neg_opt;
5321 if (optarg[0] == '+') {
5322 param.fp_ext_size_sign = -1;
5324 } else if (optarg[0] == '-') {
5325 param.fp_ext_size_sign = 1;
5329 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5330 ¶m.fp_ext_size_units, 0);
5332 fprintf(stderr, "error: bad ext-size '%s'\n",
5336 param.fp_ext_size /= SEL_UNIT_SIZE;
5337 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5338 param.fp_check_ext_size = 1;
5339 param.fp_exclude_ext_size = !!neg_opt;
5347 if (pathstart == -1) {
5348 fprintf(stderr, "error: %s: no filename|pathname\n",
5352 } else if (pathend == -1) {
5358 rc = llapi_find(argv[pathstart], ¶m);
5361 pathbad = pathstart;
5363 } while (++pathstart < pathend);
5366 fprintf(stderr, "%s: failed for '%s': %s\n",
5367 progname, argv[pathbad], strerror(-rc));
5370 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5371 free(param.fp_obd_uuid);
5373 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5374 free(param.fp_mdt_uuid);
5379 static int lfs_getstripe_internal(int argc, char **argv,
5380 struct find_param *param)
5382 struct option long_opts[] = {
5383 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5384 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5385 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5386 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5387 { .val = LFS_COMP_COUNT_OPT,
5388 .name = "comp-count", .has_arg = no_argument },
5389 { .val = LFS_COMP_COUNT_OPT,
5390 .name = "component-count", .has_arg = no_argument },
5391 { .val = LFS_COMP_FLAGS_OPT,
5392 .name = "comp-flags", .has_arg = optional_argument },
5393 { .val = LFS_COMP_FLAGS_OPT,
5394 .name = "component-flags", .has_arg = optional_argument },
5395 { .val = LFS_COMP_START_OPT,
5396 .name = "comp-start", .has_arg = optional_argument },
5397 { .val = LFS_COMP_START_OPT,
5398 .name = "component-start", .has_arg = optional_argument },
5399 { .val = LFS_MIRROR_INDEX_OPT,
5400 .name = "mirror-index", .has_arg = required_argument },
5401 { .val = LFS_MIRROR_ID_OPT,
5402 .name = "mirror-id", .has_arg = required_argument },
5403 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5404 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5405 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5406 { .val = 'd', .name = "directory", .has_arg = no_argument },
5407 { .val = 'D', .name = "default", .has_arg = no_argument },
5408 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5409 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5410 { .val = 'F', .name = "fid", .has_arg = no_argument },
5411 { .val = 'g', .name = "generation", .has_arg = no_argument },
5412 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5413 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5414 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5415 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5416 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5417 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5418 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5419 { .val = 'L', .name = "layout", .has_arg = no_argument },
5420 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5421 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5422 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5423 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5424 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5425 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5426 { .val = 'O', .name = "obd", .has_arg = required_argument },
5427 { .val = 'O', .name = "ost", .has_arg = required_argument },
5428 { .val = 'p', .name = "pool", .has_arg = no_argument },
5429 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5430 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5431 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5432 { .val = 'R', .name = "raw", .has_arg = no_argument },
5433 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5434 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5435 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5436 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5437 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5438 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5439 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5440 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5441 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5442 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5446 int pathstart = -1, pathend = -1;
5450 while ((c = getopt_long(argc, argv,
5451 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5452 long_opts, NULL)) != -1) {
5456 /* '!' is part of option */
5457 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5458 if (!isoption && pathend != -1) {
5460 "error: %s: filename|dirname must either precede options or follow options\n",
5464 if (!isoption && pathstart == -1)
5465 pathstart = optind - 1;
5466 if (isoption && pathstart != -1 && pathend == -1)
5467 pathend = optind - 2;
5471 /* unknown: opt is "!" */
5472 if (strcmp(optarg, "!") == 0)
5476 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5477 param->fp_verbose |= VERBOSE_COUNT;
5478 param->fp_max_depth = 0;
5481 case LFS_COMP_COUNT_OPT:
5482 param->fp_verbose |= VERBOSE_COMP_COUNT;
5483 param->fp_max_depth = 0;
5485 case LFS_COMP_FLAGS_OPT:
5487 rc = comp_str2flags(optarg,
5488 ¶m->fp_comp_flags,
5489 ¶m->fp_comp_neg_flags);
5492 "error: %s bad component flags '%s'.\n",
5496 param->fp_check_comp_flags = 1;
5498 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5499 param->fp_max_depth = 0;
5502 case LFS_COMP_START_OPT:
5505 if (tmp[0] == '+') {
5506 param->fp_comp_start_sign = -1;
5508 } else if (tmp[0] == '-') {
5509 param->fp_comp_start_sign = 1;
5512 rc = llapi_parse_size(tmp,
5513 ¶m->fp_comp_start,
5514 ¶m->fp_comp_start_units, 0);
5517 "error: %s bad component start '%s'.\n",
5521 param->fp_check_comp_start = 1;
5523 param->fp_verbose |= VERBOSE_COMP_START;
5524 param->fp_max_depth = 0;
5527 case LFS_MIRROR_INDEX_OPT: {
5528 unsigned long int mirror_index;
5530 if (optarg[0] == '+') {
5531 param->fp_mirror_index_sign = -1;
5533 } else if (optarg[0] == '-') {
5534 param->fp_mirror_index_sign = 1;
5539 mirror_index = strtoul(optarg, &end, 0);
5540 if (errno != 0 || *end != '\0' ||
5541 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5542 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5544 "%s %s: invalid mirror index '%s'\n",
5545 progname, argv[0], optarg);
5549 param->fp_mirror_index = (__u16)mirror_index;
5551 if (param->fp_mirror_id != 0) {
5553 "%s %s: can't specify both mirror index and mirror ID\n",
5557 param->fp_check_mirror_index = 1;
5558 param->fp_exclude_mirror_index = !!neg_opt;
5561 case LFS_MIRROR_ID_OPT: {
5562 unsigned long int mirror_id;
5564 if (optarg[0] == '+') {
5565 param->fp_mirror_id_sign = -1;
5567 } else if (optarg[0] == '-') {
5568 param->fp_mirror_id_sign = 1;
5573 mirror_id = strtoul(optarg, &end, 0);
5574 if (errno != 0 || *end != '\0' ||
5575 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5576 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5578 "%s %s: invalid mirror ID '%s'\n",
5579 progname, argv[0], optarg);
5583 param->fp_mirror_id = (__u16)mirror_id;
5585 if (param->fp_mirror_index != 0) {
5587 "%s %s: can't specify both mirror index and mirror ID\n",
5591 param->fp_check_mirror_id = 1;
5592 param->fp_exclude_mirror_id = !!neg_opt;
5596 param->fp_max_depth = 0;
5599 param->fp_get_default_lmv = 1;
5604 if (tmp[0] == '+') {
5605 param->fp_comp_end_sign = -1;
5607 } else if (tmp[0] == '-') {
5608 param->fp_comp_end_sign = 1;
5612 if (arg_is_eof(tmp)) {
5613 param->fp_comp_end = LUSTRE_EOF;
5614 param->fp_comp_end_units = 1;
5617 rc = llapi_parse_size(tmp,
5618 ¶m->fp_comp_end,
5619 ¶m->fp_comp_end_units, 0);
5620 /* assume units of KB if too small */
5621 if (param->fp_comp_end < 4096)
5622 param->fp_comp_end *= 1024;
5626 "error: %s bad component end '%s'.\n",
5630 param->fp_check_comp_end = 1;
5632 param->fp_verbose |= VERBOSE_COMP_END;
5633 param->fp_max_depth = 0;
5637 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5638 param->fp_verbose |= VERBOSE_DFID;
5639 param->fp_max_depth = 0;
5643 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5644 param->fp_verbose |= VERBOSE_GENERATION;
5645 param->fp_max_depth = 0;
5649 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5650 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5651 param->fp_max_depth = 0;
5656 param->fp_comp_id = strtoul(optarg, &end, 0);
5657 if (*end != '\0' || param->fp_comp_id == 0 ||
5658 param->fp_comp_id > LCME_ID_MAX) {
5660 "error: %s bad component id '%s'\n",
5664 param->fp_check_comp_id = 1;
5666 param->fp_max_depth = 0;
5667 param->fp_verbose |= VERBOSE_COMP_ID;
5671 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5672 param->fp_verbose |= VERBOSE_PATTERN;
5673 param->fp_max_depth = 0;
5676 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5679 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5682 if (!(param->fp_verbose & VERBOSE_DETAIL))
5683 param->fp_max_depth = 0;
5684 param->fp_verbose |= VERBOSE_MDTINDEX;
5687 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5688 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5689 param->fp_max_depth = 0;
5693 if (param->fp_obd_uuid) {
5695 "error: %s: only one obduuid allowed",
5699 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5702 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5703 param->fp_verbose |= VERBOSE_POOL;
5704 param->fp_max_depth = 0;
5711 param->fp_recursive = 1;
5717 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5718 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5719 param->fp_max_depth = 0;
5723 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5729 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5730 param->fp_verbose |= VERBOSE_EXT_SIZE;
5731 param->fp_max_depth = 0;
5739 if (pathstart == -1) {
5740 fprintf(stderr, "error: %s: no filename|pathname\n",
5743 } else if (pathend == -1) {
5751 if (param->fp_recursive)
5752 param->fp_max_depth = -1;
5753 else if (param->fp_verbose & VERBOSE_DETAIL)
5754 param->fp_max_depth = 1;
5756 if (!param->fp_verbose)
5757 param->fp_verbose = VERBOSE_DEFAULT;
5758 if (param->fp_quiet)
5759 param->fp_verbose = VERBOSE_OBJID;
5762 rc = llapi_getstripe(argv[pathstart], param);
5763 } while (++pathstart < pathend && !rc);
5766 fprintf(stderr, "error: %s failed for %s.\n",
5767 argv[0], argv[optind - 1]);
5771 static int lfs_tgts(int argc, char **argv)
5773 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5774 struct find_param param;
5775 int index = 0, rc = 0;
5780 if (argc == 2 && !realpath(argv[1], path)) {
5782 fprintf(stderr, "error: invalid path '%s': %s\n",
5783 argv[1], strerror(-rc));
5787 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5788 /* Check if we have a mount point */
5789 if (mntdir[0] == '\0')
5792 memset(¶m, 0, sizeof(param));
5793 if (!strcmp(argv[0], "mdts"))
5794 param.fp_get_lmv = 1;
5796 rc = llapi_ostlist(mntdir, ¶m);
5798 fprintf(stderr, "error: %s: failed on %s\n",
5801 if (path[0] != '\0')
5803 memset(mntdir, 0, PATH_MAX);
5809 static int lfs_getstripe(int argc, char **argv)
5811 struct find_param param = { 0 };
5813 param.fp_max_depth = 1;
5814 return lfs_getstripe_internal(argc, argv, ¶m);
5818 static int lfs_getdirstripe(int argc, char **argv)
5820 struct find_param param = { 0 };
5821 struct option long_opts[] = {
5822 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5823 { .val = 'D', .name = "default", .has_arg = no_argument },
5824 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5825 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5826 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5827 { .val = 'O', .name = "obd", .has_arg = required_argument },
5828 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5829 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5830 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5831 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5835 param.fp_get_lmv = 1;
5837 while ((c = getopt_long(argc, argv,
5838 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5842 param.fp_verbose |= VERBOSE_COUNT;
5845 param.fp_get_default_lmv = 1;
5849 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5851 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5854 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5857 param.fp_verbose |= VERBOSE_HASH_TYPE;
5860 if (param.fp_obd_uuid) {
5862 "error: %s: only one obduuid allowed",
5866 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5869 param.fp_recursive = 1;
5872 param.fp_verbose |= VERBOSE_DETAIL;
5878 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5879 progname, argv[0], argv[optind - 1]);
5887 if (param.fp_recursive)
5888 param.fp_max_depth = -1;
5890 if (!param.fp_verbose)
5891 param.fp_verbose = VERBOSE_DEFAULT;
5894 rc = llapi_getstripe(argv[optind], ¶m);
5895 } while (++optind < argc && !rc);
5898 fprintf(stderr, "error: %s failed for %s.\n",
5899 argv[0], argv[optind - 1]);
5904 MNTDF_INODES = 0x0001,
5905 MNTDF_COOKED = 0x0002,
5906 MNTDF_LAZY = 0x0004,
5907 MNTDF_VERBOSE = 0x0008,
5908 MNTDF_SHOW = 0x0010,
5909 MNTDF_DECIMAL = 0x0020,
5912 #define COOK(value, base) \
5915 while (value > base) { \
5923 #define CDF "%11llu"
5924 #define HDF "%8.1f%c"
5928 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5930 double avail, used, ratio = 0;
5933 avail = st->os_ffree;
5934 used = st->os_files - st->os_ffree;
5936 avail = st->os_bavail;
5937 used = st->os_blocks - st->os_bfree;
5939 if (avail + used > 0)
5940 ratio = used / (used + avail) * 100;
5942 /* Round up to match df(1) usage percentage */
5943 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5947 * This is to identify various problem states for "lfs df" if .osn_err = true,
5948 * so only show flags reflecting those states by default. Informational states
5949 * are only shown with "-v" and use lower-case names to distinguish them.
5950 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5952 static struct obd_statfs_state_names {
5953 enum obd_statfs_state osn_state;
5954 const char osn_name;
5957 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5958 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5959 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5960 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5961 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5962 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5963 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5964 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5965 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5968 static int showdf(char *mntdir, struct obd_statfs *stat,
5969 char *uuid, enum mntdf_flags flags,
5970 char *type, int index, int rc)
5972 long long avail, used, total;
5974 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
5975 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5976 char tbuf[3 * sizeof(__u64)];
5977 char ubuf[3 * sizeof(__u64)];
5978 char abuf[3 * sizeof(__u64)];
5979 char rbuf[3 * sizeof(__u64)];
5986 if (flags & MNTDF_INODES) {
5987 avail = stat->os_ffree;
5988 used = stat->os_files - stat->os_ffree;
5989 total = stat->os_files;
5991 int shift = flags & MNTDF_COOKED ? 0 : 10;
5993 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5994 used = ((stat->os_blocks - stat->os_bfree) *
5995 stat->os_bsize) >> shift;
5996 total = (stat->os_blocks * stat->os_bsize) >> shift;
5999 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6001 if (flags & MNTDF_COOKED) {
6002 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6006 cook_val = (double)total;
6007 i = COOK(cook_val, base);
6009 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6012 snprintf(tbuf, sizeof(tbuf), CDF, total);
6014 cook_val = (double)used;
6015 i = COOK(cook_val, base);
6017 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6020 snprintf(ubuf, sizeof(ubuf), CDF, used);
6022 cook_val = (double)avail;
6023 i = COOK(cook_val, base);
6025 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6028 snprintf(abuf, sizeof(abuf), CDF, avail);
6030 snprintf(tbuf, sizeof(tbuf), CDF, total);
6031 snprintf(ubuf, sizeof(tbuf), CDF, used);
6032 snprintf(abuf, sizeof(tbuf), CDF, avail);
6035 sprintf(rbuf, RDF, ratio);
6036 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6037 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6039 printf("[%s:%d]", type, index);
6041 if (stat->os_state) {
6045 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6046 if (oss_names[i].osn_state & stat->os_state &&
6047 (oss_names[i].osn_err ||
6048 flags & MNTDF_VERBOSE))
6049 printf("%c", oss_names[i].osn_name);
6056 printf(UUF": inactive device\n", uuid);
6059 printf(UUF": %s\n", uuid, strerror(-rc));
6066 struct ll_stat_type {
6071 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6073 struct ll_statfs_data {
6075 struct obd_statfs sd_st;
6078 struct ll_statfs_buf {
6080 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6083 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6084 int ops, struct ll_statfs_buf *lsb)
6086 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6087 struct obd_uuid uuid_buf;
6088 char *poolname = NULL;
6089 struct ll_stat_type types[] = {
6090 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6091 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6092 { .st_name = NULL } };
6093 struct ll_stat_type *tp;
6094 __u64 ost_files = 0;
6095 __u64 ost_ffree = 0;
6103 poolname = strchr(pool, '.');
6105 if (strncmp(fsname, pool, strlen(fsname))) {
6106 fprintf(stderr, "filesystem name incorrect\n");
6114 fd = open(mntdir, O_RDONLY);
6117 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6122 if (flags & MNTDF_SHOW) {
6123 if (flags & MNTDF_INODES)
6124 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6125 "UUID", "Inodes", "IUsed", "IFree",
6126 "IUse%", "Mounted on");
6128 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6130 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6131 "Used", "Available", "Use%", "Mounted on");
6134 for (tp = types; tp->st_name != NULL; tp++) {
6135 bool have_ost = false;
6137 if (!(tp->st_op & ops))
6140 for (index = 0; ; index++) {
6141 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6142 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6143 type = flags & MNTDF_LAZY ?
6144 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6145 rc2 = llapi_obd_fstatfs(fd, type, index,
6146 &stat_buf, &uuid_buf);
6151 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6152 if (!(flags & MNTDF_VERBOSE))
6154 } else if (rc2 < 0 && rc == 0) {
6159 * If we have OSTs then don't report MDT block counts.
6160 * For MDT-only filesystems the expectation is that all
6161 * layouts have a DoM component. For filesystems with
6162 * OSTs, files are not necessarily going to store data
6163 * on MDTs, and MDT space is limited to a fraction of
6164 * OST space, so don't include it in the summary.
6166 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6173 if (poolname && tp->st_op == LL_STATFS_LOV &&
6174 llapi_search_ost(fsname, poolname,
6175 obd_uuid2str(&uuid_buf)) != 1)
6179 * the llapi_obd_fstatfs() call may have returned with
6180 * an error, but if it filled in uuid_buf we will at
6181 * lease use that to print out a message for that OBD.
6182 * If we didn't get anything in the uuid_buf, then fill
6183 * it in so that we can print an error message.
6185 if (uuid_buf.uuid[0] == '\0')
6186 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6187 "%s%04x", tp->st_name, index);
6189 lsb->sb_buf[lsb->sb_count].sd_index = index;
6190 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6193 if (flags & MNTDF_SHOW)
6194 showdf(mntdir, &stat_buf,
6195 obd_uuid2str(&uuid_buf), flags,
6196 tp->st_name, index, rc2);
6201 if (tp->st_op == LL_STATFS_LMV) {
6202 sum.os_ffree += stat_buf.os_ffree;
6203 sum.os_files += stat_buf.os_files;
6204 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6205 ost_files += stat_buf.os_files;
6206 ost_ffree += stat_buf.os_ffree;
6208 sum.os_blocks += stat_buf.os_blocks *
6210 sum.os_bfree += stat_buf.os_bfree *
6212 sum.os_bavail += stat_buf.os_bavail *
6220 * If we have _some_ OSTs, but don't have as many free objects on the
6221 * OST as inodes on the MDTs, reduce the reported number of inodes
6222 * to compensate, so that the "inodes in use" number is correct.
6223 * This should be kept in sync with ll_statfs_internal().
6225 if (ost_files && ost_ffree < sum.os_ffree) {
6226 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6227 sum.os_ffree = ost_ffree;
6229 if (flags & MNTDF_SHOW) {
6231 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6239 static int lfs_setdirstripe(int argc, char **argv)
6242 struct lfs_setstripe_args lsa = { 0 };
6243 struct llapi_stripe_param *param = NULL;
6244 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6247 char *mode_opt = NULL;
6248 bool default_stripe = false;
6249 bool delete = false;
6250 bool foreign_mode = false;
6251 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6252 mode_t previous_mode = 0;
6254 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6255 struct option long_opts[] = {
6256 { .val = 'c', .name = "count", .has_arg = required_argument },
6257 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6258 { .val = 'd', .name = "delete", .has_arg = no_argument },
6259 { .val = 'D', .name = "default", .has_arg = no_argument },
6260 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6261 { .val = LFS_LAYOUT_FLAGS_OPT,
6262 .name = "flags", .has_arg = required_argument },
6263 { .val = LFS_LAYOUT_FOREIGN_OPT,
6264 .name = "foreign", .has_arg = optional_argument},
6265 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6266 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6267 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6268 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6270 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6271 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6272 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6274 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6275 { .val = 'i', .name = "index", .has_arg = required_argument },
6277 { .val = 'o', .name = "mode", .has_arg = required_argument },
6278 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6279 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6281 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6282 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6283 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6287 setstripe_args_init(&lsa);
6289 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6290 long_opts, NULL)) >= 0) {
6298 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6299 if (errno != 0 || *end != '\0' ||
6300 lsa.lsa_stripe_count < -1 ||
6301 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6303 "%s %s: invalid stripe count '%s'\n",
6304 progname, argv[0], optarg);
6310 default_stripe = true;
6313 default_stripe = true;
6315 case LFS_LAYOUT_FOREIGN_OPT:
6317 /* check pure numeric */
6318 type = strtoul(optarg, &end, 0);
6321 type = check_foreign_type_name(optarg);
6322 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6324 "%s %s: unknown foreign type '%s'\n",
6329 } else if (type >= UINT32_MAX) {
6331 "%s %s: invalid foreign type '%s'\n",
6332 progname, argv[0], optarg);
6336 foreign_mode = true;
6338 case LFS_LAYOUT_FLAGS_OPT:
6340 flags = strtoul(optarg, &end, 16);
6341 if (errno != 0 || *end != '\0' ||
6342 flags >= UINT32_MAX) {
6344 "%s %s: invalid hex flags '%s'\n",
6345 progname, argv[0], optarg);
6348 if (!foreign_mode) {
6350 "%s %s: hex flags must be specified with --foreign option\n",
6355 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6358 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6361 lsa.lsa_pattern = check_hashtype(optarg);
6362 if (lsa.lsa_pattern == 0) {
6364 "%s %s: bad stripe hash type '%s'\n",
6365 progname, argv[0], optarg);
6370 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6373 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6374 if (strcmp(argv[optind - 1], "--index") == 0)
6376 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6379 lsa.lsa_nr_tgts = parse_targets(mdts,
6380 sizeof(mdts) / sizeof(__u32),
6381 lsa.lsa_nr_tgts, optarg, NULL);
6382 if (lsa.lsa_nr_tgts < 0) {
6384 "%s %s: invalid MDT target(s) '%s'\n",
6385 progname, argv[0], optarg);
6389 lsa.lsa_tgts = mdts;
6390 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6391 lsa.lsa_stripe_off = mdts[0];
6393 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6396 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6405 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6406 progname, argv[0], argv[optind - 1]);
6411 if (optind == argc) {
6412 fprintf(stderr, "%s %s: DIR must be specified\n",
6417 if (xattr && !foreign_mode) {
6419 * only print a warning as this is armless and will be
6423 "%s %s: xattr has been specified for non-foreign layout\n",
6425 } else if (foreign_mode && !xattr) {
6427 "%s %s: xattr must be provided in foreign mode\n",
6432 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6433 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6435 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6440 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6441 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6442 /* if no parameters set, create directory on least-used MDTs */
6443 lsa.lsa_stripe_off = -1;
6444 lsa.lsa_stripe_count = 1;
6448 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6449 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6451 "%s %s: cannot specify -d with -c or -i options\n",
6457 mode = strtoul(mode_opt, &end, 8);
6460 "%s %s: bad MODE '%s'\n",
6461 progname, argv[0], mode_opt);
6464 previous_mode = umask(0);
6467 /* foreign LMV/dir case */
6469 if (argc > optind + 1) {
6471 "%s %s: cannot specify multiple foreign dirs\n",
6476 dname = argv[optind];
6477 result = llapi_dir_create_foreign(dname, mode, type, flags,
6481 "%s mkdir: can't create foreign dir '%s': %s\n",
6482 progname, dname, strerror(-result));
6487 * initialize stripe parameters, in case param is converted to specific,
6488 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6490 param = calloc(1, offsetof(typeof(*param),
6491 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6492 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6495 "%s %s: cannot allocate memory for parameters: %s\n",
6496 progname, argv[0], strerror(ENOMEM));
6500 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6501 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6502 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6503 lsa.lsa_stripe_count == 0))
6504 lsa.lsa_stripe_count = 1;
6505 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6506 param->lsp_stripe_count = lsa.lsa_stripe_count;
6507 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6508 param->lsp_stripe_offset = -1;
6510 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6511 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6512 param->lsp_stripe_pattern = lsa.lsa_pattern;
6514 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6515 param->lsp_pool = lsa.lsa_pool_name;
6516 param->lsp_is_specific = false;
6517 if (lsa.lsa_nr_tgts > 1) {
6518 if (lsa.lsa_stripe_count > 0 &&
6519 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6520 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6522 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6523 argv[0], lsa.lsa_stripe_count,
6529 param->lsp_is_specific = true;
6530 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6531 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6534 dname = argv[optind];
6536 if (default_stripe) {
6537 result = llapi_dir_set_default_lmv(dname, param);
6540 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6541 progname, dname, strerror(-result));
6545 result = llapi_dir_create(dname, mode, param);
6548 "%s setdirstripe: cannot create dir '%s': %s\n",
6549 progname, dname, strerror(-result));
6550 } while (!result && (dname = argv[++optind]));
6553 umask(previous_mode);
6560 static int lfs_rmentry(int argc, char **argv)
6567 fprintf(stderr, "error: %s: missing dirname\n",
6573 dname = argv[index];
6575 result = llapi_direntry_remove(dname);
6578 "error: %s: remove dir entry '%s' failed\n",
6582 dname = argv[++index];
6587 static int lfs_unlink_foreign(int argc, char **argv)
6594 fprintf(stderr, "error: %s: missing pathname\n",
6601 while (name != NULL) {
6602 result = llapi_unlink_foreign(name);
6605 "error: %s: unlink foreign entry '%s' failed\n",
6609 name = argv[++index];
6614 static int lfs_mv(int argc, char **argv)
6616 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6617 struct find_param param = {
6624 struct option long_opts[] = {
6625 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6626 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6627 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6630 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6632 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6635 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6639 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6640 if (errno != 0 || *end != '\0' ||
6641 lmu.lum_stripe_offset >= UINT32_MAX) {
6642 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6648 param.fp_verbose = VERBOSE_DETAIL;
6651 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6652 progname, argv[optind - 1]);
6657 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6658 fprintf(stderr, "%s mv: MDT index must be specified\n",
6663 if (optind >= argc) {
6664 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6668 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6670 /* initialize migrate mdt parameters */
6671 param.fp_lmv_md = &lmu;
6672 param.fp_migrate = 1;
6673 rc = llapi_migrate_mdt(argv[optind], ¶m);
6675 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6676 progname, argv[optind], lmu.lum_stripe_offset,
6681 static int lfs_osts(int argc, char **argv)
6683 return lfs_tgts(argc, argv);
6686 static int lfs_mdts(int argc, char **argv)
6688 return lfs_tgts(argc, argv);
6691 static int lfs_df(int argc, char **argv)
6693 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6694 enum mntdf_flags flags = MNTDF_SHOW;
6695 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6696 int c, rc = 0, index = 0;
6697 char fsname[PATH_MAX] = "", *pool_name = NULL;
6698 struct option long_opts[] = {
6699 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
6700 { .val = 'H', .name = "si", .has_arg = no_argument },
6701 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6702 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6703 { .val = 'p', .name = "pool", .has_arg = required_argument },
6704 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6707 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
6710 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
6713 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
6716 flags |= MNTDF_INODES;
6719 flags |= MNTDF_LAZY;
6725 flags |= MNTDF_VERBOSE;
6731 if (optind < argc && !realpath(argv[optind], path)) {
6733 fprintf(stderr, "error: invalid path '%s': %s\n",
6734 argv[optind], strerror(-rc));
6738 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6739 /* Check if we have a mount point */
6740 if (mntdir[0] == '\0')
6743 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6744 if (rc || path[0] != '\0')
6746 fsname[0] = '\0'; /* avoid matching in next loop */
6747 mntdir[0] = '\0'; /* avoid matching in next loop */
6753 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6754 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6758 if (opt_fsname == opt_instance) { /* both true or both false */
6759 rc = llapi_getname(mntdir, buf, buflen);
6760 } else if (opt_fsname) {
6762 * llapi_search_mounts() fills @buf with fsname, but that is not
6763 * called if explicit paths are specified on the command-line
6766 rc = llapi_get_fsname(mntdir, buf, buflen);
6767 } else /* if (opt_instance) */ {
6768 rc = llapi_get_instance(mntdir, buf, buflen);
6772 fprintf(stderr, "cannot get instance for '%s': %s\n",
6773 mntdir, strerror(-rc));
6778 printf("%s %s\n", buf, mntdir);
6780 printf("%s\n", buf);
6785 static int lfs_getname(int argc, char **argv)
6787 struct option long_opts[] = {
6788 { .val = 'h', .name = "help", .has_arg = no_argument },
6789 { .val = 'i', .name = "instance", .has_arg = no_argument },
6790 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6792 bool opt_instance = false, opt_fsname = false;
6793 char fsname[PATH_MAX] = "";
6796 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6799 opt_instance = true;
6810 if (optind == argc) { /* no paths specified, get all paths. */
6811 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6814 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6815 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6816 opt_instance, opt_fsname, true);
6819 path[0] = fsname[0] = mntdir[0] = '\0';
6821 } else { /* paths specified, only attempt to search these. */
6824 /* if only one path is given, print only requested info */
6825 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6827 for (; optind < argc; optind++) {
6828 rc2 = print_instance(argv[optind], fsname,
6829 sizeof(fsname), opt_instance,
6830 opt_fsname, opt_mntdir);
6840 static int lfs_check(int argc, char **argv)
6842 char mntdir[PATH_MAX] = {'\0'};
6851 fprintf(stderr, "%s check: server type must be specified\n",
6856 obd_types[0] = obd_type1;
6857 obd_types[1] = obd_type2;
6858 obd_types[2] = obd_type3;
6860 if (strcmp(argv[1], "osts") == 0) {
6861 strcpy(obd_types[0], "osc");
6862 } else if (strcmp(argv[1], "mdts") == 0 ||
6863 strcmp(argv[1], "mds") == 0) {
6864 strcpy(obd_types[0], "mdc");
6865 } else if (strcmp(argv[1], "mgts") == 0) {
6866 strcpy(obd_types[0], "mgc");
6867 } else if (strcmp(argv[1], "all") == 0 ||
6868 strcmp(argv[1], "servers") == 0) {
6870 strcpy(obd_types[0], "osc");
6871 strcpy(obd_types[1], "mdc");
6872 strcpy(obd_types[2], "mgc");
6874 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6879 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6880 if (rc < 0 || mntdir[0] == '\0') {
6882 "%s check: cannot find mounted Lustre filesystem: %s\n",
6883 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6887 rc = llapi_target_check(num_types, obd_types, mntdir);
6889 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6890 progname, argv[1], strerror(-rc));
6895 #ifdef HAVE_SYS_QUOTA_H
6896 #define ADD_OVERFLOW(a, b) \
6897 ((((a) + (b)) < (a)) ? \
6898 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6900 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6901 * returns the value or ULONG_MAX on integer overflow or incorrect format
6903 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6904 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6905 * 3. empty integer value is interpreted as 0
6907 static unsigned long str2sec(const char *timestr)
6909 const char spec[] = "smhdw";
6910 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6911 unsigned long val = 0;
6914 if (strpbrk(timestr, spec) == NULL) {
6916 * no specifiers inside the time string,
6917 * should treat it as an integer value
6919 val = strtoul(timestr, &tail, 10);
6920 return *tail ? ULONG_MAX : val;
6923 /* format string is XXwXXdXXhXXmXXs */
6929 v = strtoul(timestr, &tail, 10);
6930 if (v == ULONG_MAX || *tail == '\0')
6932 * value too large (ULONG_MAX or more)
6933 * or missing specifier
6937 ptr = strchr(spec, *tail);
6939 /* unknown specifier */
6944 /* check if product will overflow the type */
6945 if (!(v < ULONG_MAX / mult[ind]))
6948 ADD_OVERFLOW(val, mult[ind] * v);
6949 if (val == ULONG_MAX)
6961 #define ARG2ULL(nr, str, def_units) \
6963 unsigned long long limit, units = def_units; \
6966 rc = llapi_parse_size(str, &limit, &units, 1); \
6968 fprintf(stderr, "%s: invalid limit '%s'\n", \
6975 static inline int has_times_option(int argc, char **argv)
6979 for (i = 1; i < argc; i++)
6980 if (!strcmp(argv[i], "-t"))
6986 static inline int lfs_verify_poolarg(char *pool)
6988 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6990 "Pool name '%.*s' is longer than %d\n",
6991 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6997 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7000 char *mnt, *obd_type = (char *)qctl->obd_type;
7001 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7002 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7003 struct option long_opts[] = {
7004 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7005 { .val = 'g', .name = "group", .has_arg = no_argument },
7006 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7007 { .val = 'p', .name = "projid", .has_arg = no_argument },
7008 { .val = 't', .name = "times", .has_arg = no_argument },
7009 { .val = 'u', .name = "user", .has_arg = no_argument },
7010 { .val = LFS_POOL_OPT,
7011 .name = "pool", .has_arg = required_argument },
7015 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7016 qctl->qc_type = ALLQUOTA;
7018 while ((c = getopt_long(argc, argv, "b:gi:ptu",
7019 long_opts, NULL)) != -1) {
7030 if (qctl->qc_type != ALLQUOTA) {
7032 "error: -u/g/p can't be used more than once\n");
7035 qctl->qc_type = qtype;
7038 if (strncmp(optarg, NOTIFY_GRACE,
7039 strlen(NOTIFY_GRACE)) == 0) {
7040 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7042 dqi->dqi_bgrace = str2sec(optarg);
7043 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7045 "error: bad block-grace: %s\n",
7050 dqb->dqb_valid |= QIF_BTIME;
7053 if (strncmp(optarg, NOTIFY_GRACE,
7054 strlen(NOTIFY_GRACE)) == 0) {
7055 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7057 dqi->dqi_igrace = str2sec(optarg);
7058 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7060 "error: bad inode-grace: %s\n",
7065 dqb->dqb_valid |= QIF_ITIME;
7067 case 't': /* Yes, of course! */
7070 if (lfs_verify_poolarg(optarg))
7073 "Trying to set grace for pool %s\n", optarg);
7074 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7075 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7077 /* getopt prints error message for us when opterr != 0 */
7083 if (qctl->qc_type == ALLQUOTA) {
7084 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
7088 if (optind != argc - 1) {
7089 fprintf(stderr, "error: unexpected parameters encountered\n");
7094 rc = llapi_quotactl(mnt, qctl);
7097 fprintf(stderr, "%s %s ", obd_type,
7098 obd_uuid2str(&qctl->obd_uuid));
7099 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7106 #define BSLIMIT (1 << 0)
7107 #define BHLIMIT (1 << 1)
7108 #define ISLIMIT (1 << 2)
7109 #define IHLIMIT (1 << 3)
7111 int lfs_setquota(int argc, char **argv)
7114 struct if_quotactl *qctl;
7115 char *mnt, *obd_type;
7116 struct obd_dqblk *dqb;
7117 struct option long_opts[] = {
7118 { .val = 'b', .name = "block-softlimit",
7119 .has_arg = required_argument },
7120 { .val = 'B', .name = "block-hardlimit",
7121 .has_arg = required_argument },
7122 { .val = 'd', .name = "default", .has_arg = no_argument },
7123 { .val = 'g', .name = "group", .has_arg = required_argument },
7124 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7125 { .val = 'i', .name = "inode-softlimit",
7126 .has_arg = required_argument },
7127 { .val = 'I', .name = "inode-hardlimit",
7128 .has_arg = required_argument },
7129 { .val = 'p', .name = "projid", .has_arg = required_argument },
7130 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7131 { .val = 'u', .name = "user", .has_arg = required_argument },
7132 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7133 { .val = LFS_POOL_OPT,
7134 .name = "pool", .has_arg = required_argument },
7136 unsigned int limit_mask = 0;
7137 bool use_default = false;
7138 int qtype, qctl_len;
7140 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7141 qctl = malloc(qctl_len);
7145 memset(qctl, 0, qctl_len);
7146 obd_type = (char *)qctl->obd_type;
7147 dqb = &qctl->qc_dqblk;
7149 if (has_times_option(argc, argv)) {
7150 rc = lfs_setquota_times(argc, argv, qctl);
7154 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7155 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7156 * so it can be used as a marker that qc_type
7157 * isn't reinitialized from command line
7160 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7161 long_opts, NULL)) != -1) {
7164 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7167 goto quota_type_def;
7170 rc = name2uid(&qctl->qc_id, optarg);
7173 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7176 goto quota_type_def;
7179 rc = name2gid(&qctl->qc_id, optarg);
7182 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7185 goto quota_type_def;
7188 rc = name2projid(&qctl->qc_id, optarg);
7191 if (str2quotaid(&qctl->qc_id, optarg)) {
7193 "%s setquota: invalid id '%s'\n",
7200 if (qctl->qc_id == 0) {
7202 "%s setquota: can't set quota for root usr/group/project.\n",
7209 if (qctl->qc_type != ALLQUOTA) {
7211 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7216 qctl->qc_type = qtype;
7219 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7223 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7224 dqb->dqb_bsoftlimit >>= 10;
7225 limit_mask |= BSLIMIT;
7226 if (dqb->dqb_bsoftlimit &&
7227 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7229 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7231 (unsigned long long)dqb->dqb_bsoftlimit,
7235 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7236 dqb->dqb_bhardlimit >>= 10;
7237 limit_mask |= BHLIMIT;
7238 if (dqb->dqb_bhardlimit &&
7239 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7241 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7242 "See '%s help setquota' or Lustre manual for details\n",
7244 (unsigned long long)dqb->dqb_bhardlimit,
7248 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7249 limit_mask |= ISLIMIT;
7250 if (dqb->dqb_isoftlimit &&
7251 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7253 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7255 (unsigned long long)dqb->dqb_isoftlimit,
7259 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7260 limit_mask |= IHLIMIT;
7261 if (dqb->dqb_ihardlimit &&
7262 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7264 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7266 (unsigned long long)dqb->dqb_ihardlimit,
7270 if (lfs_verify_poolarg(optarg)) {
7275 "Trying to set quota for pool %s\n", optarg);
7276 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7277 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7281 "%s setquota: unrecognized option '%s'\n",
7282 progname, argv[optind - 1]);
7288 if (qctl->qc_type == ALLQUOTA) {
7290 "%s setquota: either -u or -g must be specified\n",
7296 if (!use_default && limit_mask == 0) {
7298 "%s setquota: at least one limit must be specified\n",
7304 if (use_default && limit_mask != 0) {
7306 "%s setquota: limits should not be specified when using default quota\n",
7312 if (use_default && qctl->qc_id == 0) {
7314 "%s setquota: can not set default quota for root user/group/project\n",
7320 if (optind != argc - 1) {
7322 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7323 progname, argv[optind]);
7331 dqb->dqb_bhardlimit = 0;
7332 dqb->dqb_bsoftlimit = 0;
7333 dqb->dqb_ihardlimit = 0;
7334 dqb->dqb_isoftlimit = 0;
7337 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7338 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7339 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7340 /* sigh, we can't just set blimits/ilimits */
7341 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7342 .qc_type = qctl->qc_type,
7343 .qc_id = qctl->qc_id};
7345 rc = llapi_quotactl(mnt, &tmp_qctl);
7349 if (!(limit_mask & BHLIMIT))
7350 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7351 if (!(limit_mask & BSLIMIT))
7352 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7353 if (!(limit_mask & IHLIMIT))
7354 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7355 if (!(limit_mask & ISLIMIT))
7356 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7358 /* Keep grace times if we have got no softlimit arguments */
7359 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7360 dqb->dqb_valid |= QIF_BTIME;
7361 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7364 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7365 dqb->dqb_valid |= QIF_ITIME;
7366 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7370 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7371 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7373 rc = llapi_quotactl(mnt, qctl);
7377 "%s setquota: cannot quotactl '%s' '%s': %s",
7379 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7386 /* Converts seconds value into format string
7387 * result is returned in buf
7389 * 1. result is in descenting order: 1w2d3h4m5s
7390 * 2. zero fields are not filled (except for p. 3): 5d1s
7391 * 3. zero seconds value is presented as "0s"
7393 static char *__sec2str(time_t seconds, char *buf)
7395 const char spec[] = "smhdw";
7396 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7401 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7402 c = seconds / mult[i];
7404 if (c > 0 || (i == 0 && buf == tail))
7405 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7414 static void sec2str(time_t seconds, char *buf, int rc)
7421 tail = __sec2str(seconds, tail);
7423 if (rc && tail - buf < 39) {
7429 static void diff2str(time_t seconds, char *buf, time_t now)
7434 if (seconds <= now) {
7435 strcpy(buf, "none");
7438 __sec2str(seconds - now, buf);
7441 static void print_quota_title(char *name, struct if_quotactl *qctl,
7442 bool human_readable, bool show_default)
7445 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7446 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7447 "Filesystem", "bquota", "blimit", "bgrace",
7448 "iquota", "ilimit", "igrace");
7450 printf("Disk quotas for %s %s (%cid %u):\n",
7451 qtype_name(qctl->qc_type), name,
7452 *qtype_name(qctl->qc_type), qctl->qc_id);
7453 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7454 "Filesystem", human_readable ? "used" : "kbytes",
7455 "quota", "limit", "grace",
7456 "files", "quota", "limit", "grace");
7460 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7463 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7466 snprintf(buf, buflen, "%5.4gP",
7467 (double)num / ((__u64)1 << 40));
7469 snprintf(buf, buflen, "%5.4gT",
7470 (double)num / (1 << 30));
7472 snprintf(buf, buflen, "%5.4gG",
7473 (double)num / (1 << 20));
7475 snprintf(buf, buflen, "%5.4gM",
7476 (double)num / (1 << 10));
7478 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7482 #define STRBUF_LEN 32
7483 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7484 int rc, bool h, bool show_default)
7490 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7491 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7492 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7493 int bover = 0, iover = 0;
7494 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7495 char numbuf[3][STRBUF_LEN];
7497 char strbuf[STRBUF_LEN];
7499 if (dqb->dqb_bhardlimit &&
7500 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7502 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7503 if (dqb->dqb_btime > now)
7509 if (dqb->dqb_ihardlimit &&
7510 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7512 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7513 if (dqb->dqb_itime > now)
7519 if (strlen(mnt) > 15)
7520 printf("%s\n%15s", mnt, "");
7522 printf("%15s", mnt);
7525 diff2str(dqb->dqb_btime, timebuf, now);
7526 else if (show_default)
7527 snprintf(timebuf, sizeof(timebuf), "%llu",
7528 (unsigned long long)dqb->dqb_btime);
7530 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7531 strbuf, sizeof(strbuf), h);
7532 if (rc == -EREMOTEIO)
7533 sprintf(numbuf[0], "%s*", strbuf);
7535 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7536 "%s" : "[%s]", strbuf);
7538 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7539 if (type == QC_GENERAL)
7540 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7541 "%s" : "[%s]", strbuf);
7543 sprintf(numbuf[1], "%s", "-");
7545 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7546 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7547 "%s" : "[%s]", strbuf);
7550 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7552 printf(" %7s%c %6s %7s %7s",
7553 numbuf[0], bover ? '*' : ' ', numbuf[1],
7554 numbuf[2], bover > 1 ? timebuf : "-");
7557 diff2str(dqb->dqb_itime, timebuf, now);
7558 else if (show_default)
7559 snprintf(timebuf, sizeof(timebuf), "%llu",
7560 (unsigned long long)dqb->dqb_itime);
7562 snprintf(numbuf[0], sizeof(numbuf),
7563 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7564 (uintmax_t)dqb->dqb_curinodes);
7566 if (type == QC_GENERAL)
7567 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7569 (uintmax_t)dqb->dqb_isoftlimit);
7571 sprintf(numbuf[1], "%s", "-");
7573 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7574 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7577 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7578 else if (type != QC_OSTIDX)
7579 printf(" %7s%c %6s %7s %7s",
7580 numbuf[0], iover ? '*' : ' ', numbuf[1],
7581 numbuf[2], iover > 1 ? timebuf : "-");
7583 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7585 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7586 qctl->qc_cmd == Q_GETOINFO) {
7590 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7591 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7593 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7594 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7595 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7597 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7599 printf("Block grace time: %s; Inode grace time: %s\n",
7600 bgtimebuf, igtimebuf);
7604 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7608 /* format is "lustre-OST0001" */
7609 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7611 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7616 *idx = strtoul(dash, &endp, 16);
7617 if (*idx > 0xffff) {
7618 fprintf(stderr, "wrong index %s\n", tgtname);
7625 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7626 bool h, __u64 *total)
7628 int rc = 0, rc1 = 0, count = 0, i = 0;
7629 char **list = NULL, *buffer = NULL;
7630 __u32 valid = qctl->qc_valid;
7632 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7635 /* Is it correct for the case OST0000, OST0002, OST0003 -
7636 * we will ask OST0001 that is absent and won't ask OST0003? */
7637 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7639 fprintf(stderr, "can not get %s count: %s\n",
7640 is_mdt ? "mdt" : "ost", strerror(-rc));
7644 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7645 char fname[PATH_MAX];
7646 char fsname[LUSTRE_MAXFSNAME + 1];
7647 int bufsize = sizeof(struct obd_uuid) * count;
7649 rc = llapi_search_fsname(mnt, fsname);
7651 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7655 buffer = malloc(bufsize + sizeof(*list) * count);
7658 list = (char **)(buffer + bufsize);
7659 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7660 count = llapi_get_poolmembers(fname, list, count,
7666 for (i = 0; i < count; i++) {
7667 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7670 if (tgt_name2index(list[i], &index))
7672 qctl->qc_idx = index;
7677 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7678 rc = llapi_quotactl(mnt, qctl);
7680 /* It is remote client case. */
7681 if (rc == -EOPNOTSUPP) {
7688 fprintf(stderr, "quotactl %s%d failed.\n",
7689 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7693 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7694 qctl->qc_valid, 0, h, false);
7695 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7696 qctl->qc_dqblk.dqb_bhardlimit;
7701 qctl->qc_valid = valid;
7705 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7706 int verbose, int quiet, bool human_readable,
7709 int rc1 = 0, rc2 = 0, rc3 = 0;
7710 char *obd_type = (char *)qctl->obd_type;
7711 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7712 __u64 total_ialloc = 0, total_balloc = 0;
7713 bool use_default_for_blk = false;
7714 bool use_default_for_file = false;
7717 rc1 = llapi_quotactl(mnt, qctl);
7721 fprintf(stderr, "%s quotas are not enabled.\n",
7722 qtype_name(qctl->qc_type));
7725 fprintf(stderr, "Permission denied.\n");
7728 /* We already got error message. */
7731 fprintf(stderr, "Unexpected quotactl error: %s\n",
7736 if (!show_default && qctl->qc_id == 0) {
7737 qctl->qc_dqblk.dqb_bhardlimit = 0;
7738 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7739 qctl->qc_dqblk.dqb_ihardlimit = 0;
7740 qctl->qc_dqblk.dqb_isoftlimit = 0;
7741 qctl->qc_dqblk.dqb_btime = 0;
7742 qctl->qc_dqblk.dqb_itime = 0;
7743 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7746 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7747 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7748 use_default_for_blk = true;
7749 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7752 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7753 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7754 use_default_for_file = true;
7755 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7758 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7759 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7760 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7761 print_quota_title(name, qctl, human_readable, show_default);
7763 if (rc1 && *obd_type)
7764 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7766 if (qctl->qc_valid != QC_GENERAL)
7769 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7770 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7771 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7772 (QIF_LIMITS|QIF_USAGE));
7774 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7776 if (!show_default && verbose &&
7777 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7778 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7779 char strbuf[STRBUF_LEN];
7781 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7783 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7785 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7787 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7788 (uintmax_t)total_ialloc, strbuf);
7791 if (use_default_for_blk)
7792 printf("%cid %u is using default block quota setting\n",
7793 *qtype_name(qctl->qc_type), qctl->qc_id);
7795 if (use_default_for_file)
7796 printf("%cid %u is using default file quota setting\n",
7797 *qtype_name(qctl->qc_type), qctl->qc_id);
7799 if (rc1 || rc2 || rc3 || inacc)
7800 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7814 static int lfs_project(int argc, char **argv)
7816 int ret = 0, err = 0, c, i;
7817 struct project_handle_control phc = { 0 };
7818 enum lfs_project_ops_t op;
7821 phc.assign_projid = false;
7822 /* default action */
7823 op = LFS_PROJECT_LIST;
7825 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7828 if (op != LFS_PROJECT_LIST) {
7830 "%s: cannot specify '-c' '-C' '-s' together\n",
7835 op = LFS_PROJECT_CHECK;
7838 if (op != LFS_PROJECT_LIST) {
7840 "%s: cannot specify '-c' '-C' '-s' together\n",
7845 op = LFS_PROJECT_CLEAR;
7848 if (op != LFS_PROJECT_LIST) {
7850 "%s: cannot specify '-c' '-C' '-s' together\n",
7855 phc.set_inherit = true;
7856 op = LFS_PROJECT_SET;
7862 phc.keep_projid = true;
7865 phc.recursive = true;
7868 if (str2quotaid(&phc.projid, optarg)) {
7870 "Invalid project ID: %s\n",
7875 phc.assign_projid = true;
7879 phc.newline = false;
7882 fprintf(stderr, "%s: invalid option '%c'\n",
7888 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7889 op = LFS_PROJECT_SET;
7890 phc.set_projid = true;
7891 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7892 phc.set_projid = true;
7896 case LFS_PROJECT_CHECK:
7897 if (phc.keep_projid) {
7899 "%s: '-k' is useless together with '-c'\n",
7904 case LFS_PROJECT_CLEAR:
7907 "%s: '-0' is useless together with '-C'\n",
7911 if (phc.assign_projid) {
7913 "%s: '-p' is useless together with '-C'\n",
7918 case LFS_PROJECT_SET:
7921 "%s: '-0' is useless together with '-s'\n",
7925 if (phc.keep_projid) {
7927 "%s: '-k' is useless together with '-s'\n",
7935 "%s: '-0' is useless for list operations\n",
7945 fprintf(stderr, "%s: missing file or directory target(s)\n",
7950 for (i = 0; i < argc; i++) {
7952 case LFS_PROJECT_CHECK:
7953 err = lfs_project_check(argv[i], &phc);
7955 case LFS_PROJECT_LIST:
7956 err = lfs_project_list(argv[i], &phc);
7958 case LFS_PROJECT_CLEAR:
7959 err = lfs_project_clear(argv[i], &phc);
7961 case LFS_PROJECT_SET:
7962 err = lfs_project_set(argv[i], &phc);
7974 static int lfs_quota(int argc, char **argv)
7977 char *mnt, *name = NULL;
7978 struct if_quotactl *qctl;
7980 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7981 __u32 valid = QC_GENERAL, idx = 0;
7982 bool human_readable = false;
7983 bool show_default = false;
7985 struct option long_opts[] = {
7986 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
7989 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7993 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7994 qctl->qc_type = ALLQUOTA;
7995 obd_uuid = (char *)qctl->obd_uuid.uuid;
7997 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7998 long_opts, NULL)) != -1) {
8001 show_default = true;
8006 show_default = true;
8011 show_default = true;
8015 if (qctl->qc_type != ALLQUOTA) {
8017 "%s quota: only one of -u, -g, or -p may be specified\n",
8022 qctl->qc_type = qtype;
8025 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8028 valid = qctl->qc_valid = QC_UUID;
8029 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8032 valid = qctl->qc_valid = QC_MDTIDX;
8033 idx = qctl->qc_idx = atoi(optarg);
8034 if (idx == 0 && *optarg != '0') {
8036 "%s quota: invalid MDT index '%s'\n",
8043 valid = qctl->qc_valid = QC_OSTIDX;
8044 idx = qctl->qc_idx = atoi(optarg);
8045 if (idx == 0 && *optarg != '0') {
8047 "%s quota: invalid OST index '%s'\n",
8060 human_readable = true;
8063 if (lfs_verify_poolarg(optarg)) {
8067 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8068 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
8069 LUSTRE_Q_GETINFOPOOL :
8070 LUSTRE_Q_GETQUOTAPOOL;
8073 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8074 progname, argv[optind - 1]);
8080 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8081 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8082 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8083 qctl->qc_type == ALLQUOTA &&
8084 optind == argc - 1 && !show_default) {
8087 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8088 qctl->qc_type = qtype;
8089 qctl->qc_valid = valid;
8090 if (qtype == USRQUOTA) {
8091 qctl->qc_id = geteuid();
8092 rc = uid2name(&name, qctl->qc_id);
8094 qctl->qc_id = getegid();
8095 rc = gid2name(&name, qctl->qc_id);
8096 memset(&qctl->qc_dqblk, 0,
8097 sizeof(qctl->qc_dqblk));
8102 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8103 human_readable, show_default);
8108 /* lfs quota -u username /path/to/lustre/mount */
8109 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8110 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8111 /* options should be followed by u/g-name and mntpoint */
8112 if ((!show_default && optind + 2 != argc) ||
8113 (show_default && optind + 1 != argc) ||
8114 qctl->qc_type == ALLQUOTA) {
8116 "%s quota: name and mount point must be specified\n",
8122 if (!show_default) {
8123 name = argv[optind++];
8124 switch (qctl->qc_type) {
8126 rc = name2uid(&qctl->qc_id, name);
8129 rc = name2gid(&qctl->qc_id, name);
8132 rc = name2projid(&qctl->qc_id, name);
8139 qctl->qc_valid = QC_GENERAL;
8140 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
8145 if (str2quotaid(&qctl->qc_id, name)) {
8146 fprintf(stderr, "%s quota: invalid id '%s'\n",
8152 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8153 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8160 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8161 human_readable, show_default);
8166 #endif /* HAVE_SYS_QUOTA_H! */
8168 static int flushctx_ioctl(char *mp)
8172 fd = open(mp, O_RDONLY);
8174 fprintf(stderr, "flushctx: error open %s: %s\n",
8175 mp, strerror(errno));
8179 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8181 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8182 mp, strerror(errno));
8188 static int lfs_flushctx(int argc, char **argv)
8190 int kdestroy = 0, reap = 0, c;
8191 char mntdir[PATH_MAX] = {'\0'};
8195 while ((c = getopt(argc, argv, "kr")) != -1) {
8205 "error: %s: option '-%c' unrecognized\n",
8212 rc = system("kdestroy > /dev/null");
8214 rc = WEXITSTATUS(rc);
8216 "error destroying tickets: %d, continuing\n",
8221 if (optind >= argc) {
8222 /* flush for all mounted lustre fs. */
8223 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8224 /* Check if we have a mount point */
8225 if (mntdir[0] == '\0')
8228 if (flushctx_ioctl(mntdir))
8231 mntdir[0] = '\0'; /* avoid matching in next loop */
8234 /* flush fs as specified */
8235 while (optind < argc) {
8236 if (flushctx_ioctl(argv[optind++]))
8242 rc = system("keyctl reap > /dev/null");
8244 rc = WEXITSTATUS(rc);
8245 fprintf(stderr, "error reaping keyring: %d\n", rc);
8252 static int lfs_changelog(int argc, char **argv)
8254 void *changelog_priv;
8255 struct changelog_rec *rec;
8256 long long startrec = 0, endrec = 0;
8258 struct option long_opts[] = {
8259 { .val = 'f', .name = "follow", .has_arg = no_argument },
8261 char short_opts[] = "f";
8264 while ((rc = getopt_long(argc, argv, short_opts,
8265 long_opts, NULL)) != -1) {
8272 "%s changelog: unrecognized option '%s'\n",
8273 progname, argv[optind - 1]);
8277 if (optind >= argc) {
8278 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8283 mdd = argv[optind++];
8284 if (argc > optind) {
8286 startrec = strtoll(argv[optind++], NULL, 10);
8287 if (errno != 0 || startrec < 0) {
8289 "%s changelog: bad startrec\n",
8295 if (argc > optind) {
8297 endrec = strtoll(argv[optind++], NULL, 10);
8298 if (errno != 0 || endrec < 0) {
8300 "%s changelog: bad endrec\n",
8306 rc = llapi_changelog_start(&changelog_priv,
8307 CHANGELOG_FLAG_BLOCK |
8308 CHANGELOG_FLAG_JOBID |
8309 CHANGELOG_FLAG_EXTRA_FLAGS |
8310 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8313 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8314 progname, strerror(errno = -rc));
8318 rc = llapi_changelog_set_xflags(changelog_priv,
8319 CHANGELOG_EXTRA_FLAG_UIDGID |
8320 CHANGELOG_EXTRA_FLAG_NID |
8321 CHANGELOG_EXTRA_FLAG_OMODE |
8322 CHANGELOG_EXTRA_FLAG_XATTR);
8325 "%s changelog: cannot set xflags for changelog: %s\n",
8326 progname, strerror(errno = -rc));
8330 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8334 if (endrec && rec->cr_index > endrec) {
8335 llapi_changelog_free(&rec);
8338 if (rec->cr_index < startrec) {
8339 llapi_changelog_free(&rec);
8343 secs = rec->cr_time >> 30;
8344 gmtime_r(&secs, &ts);
8345 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8346 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8347 changelog_type2str(rec->cr_type),
8348 ts.tm_hour, ts.tm_min, ts.tm_sec,
8349 (int)(rec->cr_time & ((1 << 30) - 1)),
8350 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8351 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8353 if (rec->cr_flags & CLF_JOBID) {
8354 struct changelog_ext_jobid *jid =
8355 changelog_rec_jobid(rec);
8357 if (jid->cr_jobid[0] != '\0')
8358 printf(" j=%s", jid->cr_jobid);
8361 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8362 struct changelog_ext_extra_flags *ef =
8363 changelog_rec_extra_flags(rec);
8365 printf(" ef=0x%llx",
8366 (unsigned long long)ef->cr_extra_flags);
8368 if (ef->cr_extra_flags & CLFE_UIDGID) {
8369 struct changelog_ext_uidgid *uidgid =
8370 changelog_rec_uidgid(rec);
8372 printf(" u=%llu:%llu",
8373 (unsigned long long)uidgid->cr_uid,
8374 (unsigned long long)uidgid->cr_gid);
8376 if (ef->cr_extra_flags & CLFE_NID) {
8377 struct changelog_ext_nid *nid =
8378 changelog_rec_nid(rec);
8381 libcfs_nid2str(nid->cr_nid));
8384 if (ef->cr_extra_flags & CLFE_OPEN) {
8385 struct changelog_ext_openmode *omd =
8386 changelog_rec_openmode(rec);
8387 char mode[] = "---";
8389 /* exec mode must be exclusive */
8390 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8393 if (omd->cr_openflags & MDS_FMODE_READ)
8395 if (omd->cr_openflags &
8402 if (strcmp(mode, "---") != 0)
8403 printf(" m=%s", mode);
8406 if (ef->cr_extra_flags & CLFE_XATTR) {
8407 struct changelog_ext_xattr *xattr =
8408 changelog_rec_xattr(rec);
8410 if (xattr->cr_xattr[0] != '\0')
8411 printf(" x=%s", xattr->cr_xattr);
8415 if (!fid_is_zero(&rec->cr_pfid))
8416 printf(" p="DFID, PFID(&rec->cr_pfid));
8417 if (rec->cr_namelen)
8418 printf(" %.*s", rec->cr_namelen,
8419 changelog_rec_name(rec));
8421 if (rec->cr_flags & CLF_RENAME) {
8422 struct changelog_ext_rename *rnm =
8423 changelog_rec_rename(rec);
8425 if (!fid_is_zero(&rnm->cr_sfid))
8426 printf(" s="DFID" sp="DFID" %.*s",
8427 PFID(&rnm->cr_sfid),
8428 PFID(&rnm->cr_spfid),
8429 (int)changelog_rec_snamelen(rec),
8430 changelog_rec_sname(rec));
8434 llapi_changelog_free(&rec);
8437 llapi_changelog_fini(&changelog_priv);
8440 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8441 progname, strerror(errno = -rc));
8443 return (rc == 1 ? 0 : rc);
8446 static int lfs_changelog_clear(int argc, char **argv)
8455 endrec = strtoll(argv[3], NULL, 10);
8456 if (errno != 0 || endrec < 0) {
8458 "%s: bad endrec '%s'\n",
8463 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8466 fprintf(stderr, "%s: record out of range: %llu\n",
8468 else if (rc == -ENOENT)
8469 fprintf(stderr, "%s: no changelog user: %s\n",
8472 fprintf(stderr, "%s error: %s\n", argv[0],
8481 static void rstripc(char *str, int c)
8483 char *end = str + strlen(str);
8485 for (; str < end && end[-1] == c; --end)
8489 static int lfs_fid2path(int argc, char **argv)
8491 struct option long_opts[] = {
8492 { .val = 'c', .name = "cur", .has_arg = no_argument },
8493 { .val = 'c', .name = "current", .has_arg = no_argument },
8494 { .val = 'c', .name = "print-link", .has_arg = no_argument },
8495 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
8496 { .val = 'l', .name = "link", .has_arg = required_argument },
8498 char short_opts[] = "cfl:pr:";
8499 bool print_link = false;
8500 bool print_fid = false;
8502 char mnt_dir[PATH_MAX] = "";
8504 char *path_or_fsname;
8505 long long recno = -1;
8507 char *endptr = NULL;
8512 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
8522 linkno = strtol(optarg, &endptr, 10);
8523 if (errno != 0 || *endptr != '\0' || linkno < 0) {
8525 "%s fid2path: invalid linkno '%s'\n",
8531 /* recno is something to do with changelogs
8532 * that was never implemented. We just pass it
8533 * through for the MDT to ignore.
8536 recno = strtoll(optarg, &endptr, 10);
8537 if (errno != 0 || *endptr != '\0' || recno < 0) {
8539 "%s fid2path: invalid recno '%s'\n",
8546 "%s fid2path: unrecognized option '%s'\n",
8547 progname, argv[optind - 1]);
8552 if (argc - optind < 2) {
8554 "Usage: %s fid2path FSNAME|ROOT FID...\n",
8559 path_or_fsname = argv[optind];
8561 if (*path_or_fsname == '/') {
8562 print_mnt_dir = true;
8563 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
8565 print_mnt_dir = false;
8566 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
8571 "%s fid2path: cannot resolve mount point for '%s': %s\n",
8572 progname, path_or_fsname, strerror(-rc));
8576 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
8579 "%s fid2path: cannot open mount point for '%s': %s\n",
8580 progname, path_or_fsname, strerror(-rc));
8584 /* Strip trailing slashes from mnt_dir. */
8585 rstripc(mnt_dir + 1, '/');
8587 for (i = optind + 1; i < argc; i++) {
8588 const char *fid_str = argv[i];
8592 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
8595 "%s fid2path: invalid FID '%s'\n",
8603 int linktmp = (linkno >= 0) ? linkno : 0;
8605 int oldtmp = linktmp;
8606 long long rectmp = recno;
8607 char path_buf[PATH_MAX];
8609 rc2 = llapi_fid2path_at(mnt_fd, &fid,
8610 path_buf, sizeof(path_buf), &rectmp, &linktmp);
8613 "%s fid2path: cannot find %s %s: %s\n",
8614 progname, path_or_fsname, fid_str,
8622 printf("%s ", fid_str);
8625 printf("%d ", linktmp);
8627 /* You may think this looks wrong or weird (and it is!)
8628 * but we are actually trying to preserve the old quirky
8629 * behaviors (enforced by our old quirky tests!) that
8630 * make lfs so much fun to work on:
8632 * lustre 0x200000007:0x1:0x0 => "/"
8633 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
8635 * Note that llapi_fid2path() returns "" for the root
8639 print_mnt_dir ? mnt_dir : "",
8640 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
8644 /* specified linkno */
8647 if (oldtmp == linktmp)
8659 static int lfs_path2fid(int argc, char **argv)
8661 struct option long_opts[] = {
8662 { .val = 'p', .name = "parents", .has_arg = no_argument },
8665 const char short_opts[] = "p";
8666 const char *sep = "";
8669 bool show_parents = false;
8671 while ((rc = getopt_long(argc, argv, short_opts,
8672 long_opts, NULL)) != -1) {
8675 show_parents = true;
8679 "%s path2fid: unrecognized option '%s'\n",
8680 progname, argv[optind - 1]);
8685 if (optind > argc - 1) {
8686 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8689 } else if (optind < argc - 1) {
8694 for (path = argv + optind; *path != NULL; path++) {
8697 if (!show_parents) {
8698 err = llapi_path2fid(*path, &fid);
8700 printf("%s%s"DFID"\n",
8701 *sep != '\0' ? *path : "", sep,
8704 char name[NAME_MAX + 1];
8705 unsigned int linkno = 0;
8707 while ((err = llapi_path2parent(*path, linkno, &fid,
8708 name, sizeof(name))) == 0) {
8709 if (*sep != '\0' && linkno == 0)
8710 printf("%s%s", *path, sep);
8712 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8717 /* err == -ENODATA is end-of-loop */
8718 if (linkno > 0 && err == -ENODATA) {
8726 "%s path2fid: cannot get %sfid for '%s': %s\n",
8727 progname, show_parents ? "parent " : "", *path,
8739 #define MAX_ERRNO 4095
8740 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8742 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8746 rc = llapi_rmfid(device, fa);
8748 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
8749 progname, strerror(-rc));
8753 for (k = 0; k < fa->fa_nr; k++) {
8754 rc2 = (__s32)fa->fa_fids[k].f_ver;
8755 if (!IS_ERR_VALUE(rc2))
8761 fa->fa_fids[k].f_ver = 0;
8762 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
8763 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
8769 static int lfs_rmfid(int argc, char **argv)
8771 char *fidstr, *device;
8772 int rc = 0, rc2, nr;
8773 struct fid_array *fa;
8775 if (optind > argc - 1) {
8776 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8780 device = argv[optind++];
8783 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8789 while (optind < argc) {
8792 fidstr = argv[optind++];
8793 while (*fidstr == '[')
8795 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8797 fprintf(stderr, "unrecognized FID: %s\n",
8802 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8803 /* start another batch */
8804 rc2 = lfs_rmfid_and_show_errors(device, fa);
8811 rc2 = lfs_rmfid_and_show_errors(device, fa);
8819 static int lfs_data_version(int argc, char **argv)
8826 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8829 fprintf(stderr, "%s data_version: FILE must be specified\n",
8834 while ((c = getopt(argc, argv, "nrw")) != -1) {
8837 data_version_flags = 0;
8840 data_version_flags |= LL_DV_RD_FLUSH;
8843 data_version_flags |= LL_DV_WR_FLUSH;
8847 "%s data_version: unrecognized option '%s'\n",
8848 progname, argv[optind - 1]);
8852 if (optind == argc) {
8853 fprintf(stderr, "%s data_version: FILE must be specified\n",
8858 path = argv[optind];
8859 fd = open(path, O_RDONLY);
8862 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8863 progname, path, strerror(-rc));
8867 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8870 "%s data_version: cannot get version for '%s': %s\n",
8871 progname, path, strerror(-rc));
8873 printf("%ju" "\n", (uintmax_t)data_version);
8879 static int lfs_hsm_state(int argc, char **argv)
8884 struct hsm_user_state hus;
8892 rc = llapi_hsm_state_get(path, &hus);
8894 fprintf(stderr, "can't get hsm state for %s: %s\n",
8895 path, strerror(errno = -rc));
8899 /* Display path name and status flags */
8900 printf("%s: (0x%08x)", path, hus.hus_states);
8902 if (hus.hus_states & HS_RELEASED)
8903 printf(" released");
8904 if (hus.hus_states & HS_EXISTS)
8906 if (hus.hus_states & HS_DIRTY)
8908 if (hus.hus_states & HS_ARCHIVED)
8909 printf(" archived");
8910 /* Display user-settable flags */
8911 if (hus.hus_states & HS_NORELEASE)
8912 printf(" never_release");
8913 if (hus.hus_states & HS_NOARCHIVE)
8914 printf(" never_archive");
8915 if (hus.hus_states & HS_LOST)
8916 printf(" lost_from_hsm");
8918 if (hus.hus_archive_id != 0)
8919 printf(", archive_id:%d", hus.hus_archive_id);
8922 } while (++i < argc);
8927 #define LFS_HSM_SET 0
8928 #define LFS_HSM_CLEAR 1
8931 * Generic function to set or clear HSM flags.
8932 * Used by hsm_set and hsm_clear.
8934 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8936 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8938 struct option long_opts[] = {
8939 { .val = 'A', .name = "archived", .has_arg = no_argument },
8940 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8941 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8942 { .val = 'e', .name = "exists", .has_arg = no_argument },
8943 { .val = 'l', .name = "lost", .has_arg = no_argument },
8944 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8945 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8947 char short_opts[] = "lraAdei:";
8951 __u32 archive_id = 0;
8957 while ((c = getopt_long(argc, argv, short_opts,
8958 long_opts, NULL)) != -1) {
8964 mask |= HS_NOARCHIVE;
8967 mask |= HS_ARCHIVED;
8970 mask |= HS_NORELEASE;
8980 archive_id = strtol(optarg, &end, 10);
8981 if (errno != 0 || *end != '\0' || archive_id < 0) {
8982 fprintf(stderr, "invalid archive_id: '%s'\n",
8990 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8991 argv[0], argv[optind - 1]);
8996 /* User should have specified a flag */
9000 while (optind < argc) {
9001 path = argv[optind];
9003 /* If mode == 0, this means we apply the mask. */
9004 if (mode == LFS_HSM_SET)
9005 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9007 rc = llapi_hsm_state_set(path, 0, mask, 0);
9010 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9011 path, strerror(errno = -rc));
9020 static int lfs_hsm_action(int argc, char **argv)
9025 struct hsm_current_action hca;
9026 struct hsm_extent he;
9027 enum hsm_user_action hua;
9028 enum hsm_progress_states hps;
9036 rc = llapi_hsm_current_action(path, &hca);
9038 fprintf(stderr, "can't get hsm action for %s: %s\n",
9039 path, strerror(errno = -rc));
9042 he = hca.hca_location;
9043 hua = hca.hca_action;
9044 hps = hca.hca_state;
9046 printf("%s: %s", path, hsm_user_action2name(hua));
9048 /* Skip file without action */
9049 if (hca.hca_action == HUA_NONE) {
9054 printf(" %s ", hsm_progress_state2name(hps));
9056 if ((hps == HPS_RUNNING) &&
9057 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9058 printf("(%llu bytes moved)\n",
9059 (unsigned long long)he.length);
9060 else if ((he.offset + he.length) == LUSTRE_EOF)
9061 printf("(from %llu to EOF)\n",
9062 (unsigned long long)he.offset);
9064 printf("(from %llu to %llu)\n",
9065 (unsigned long long)he.offset,
9066 (unsigned long long)(he.offset + he.length));
9068 } while (++i < argc);
9073 static int lfs_hsm_set(int argc, char **argv)
9075 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9078 static int lfs_hsm_clear(int argc, char **argv)
9080 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9084 * Check file state and return its fid, to be used by lfs_hsm_request().
9086 * \param[in] file Path to file to check
9087 * \param[in,out] fid Pointer to allocated lu_fid struct.
9088 * \param[in,out] last_dev Pointer to last device id used.
9090 * \return 0 on success.
9092 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9098 rc = lstat(file, &st);
9100 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9104 * Checking for regular file as archiving as posix copytool
9105 * rejects archiving files other than regular files
9107 if (!S_ISREG(st.st_mode)) {
9108 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9111 /* A request should be ... */
9112 if (*last_dev != st.st_dev && *last_dev != 0) {
9114 "All files should be on the same filesystem: %s\n",
9118 *last_dev = st.st_dev;
9120 rc = llapi_path2fid(file, fid);
9122 fprintf(stderr, "Cannot read FID of %s: %s\n",
9123 file, strerror(-rc));
9129 /* Fill an HSM HUR item with a given file name.
9131 * If mntpath is set, then the filename is actually a FID, and no
9132 * lookup on the filesystem will be performed.
9134 * \param[in] hur the user request to fill
9135 * \param[in] idx index of the item inside the HUR to fill
9136 * \param[in] mntpath mountpoint of Lustre
9137 * \param[in] fname filename (if mtnpath is NULL)
9138 * or FID (if mntpath is set)
9139 * \param[in] last_dev pointer to last device id used
9141 * \retval 0 on success
9142 * \retval CMD_HELP or a negative errno on error
9144 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9145 const char *mntpath, const char *fname,
9148 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9151 hui->hui_extent.length = -1;
9154 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9156 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9159 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9163 hur->hur_request.hr_itemcount++;
9168 static int lfs_hsm_request(int argc, char **argv, int action)
9170 struct option long_opts[] = {
9171 { .val = 'a', .name = "archive", .has_arg = required_argument },
9172 { .val = 'D', .name = "data", .has_arg = required_argument },
9173 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9174 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9177 char short_opts[] = "l:D:a:m:";
9178 struct hsm_user_request *hur, *oldhur;
9183 char *filelist = NULL;
9184 char fullpath[PATH_MAX];
9185 char *opaque = NULL;
9189 int nbfile_alloc = 0;
9190 char *some_file = NULL;
9191 char *mntpath = NULL;
9197 while ((c = getopt_long(argc, argv, short_opts,
9198 long_opts, NULL)) != -1) {
9207 if (action != HUA_ARCHIVE &&
9208 action != HUA_REMOVE) {
9210 "error: -a is supported only when archiving or removing\n");
9213 archive_id = atoi(optarg);
9218 some_file = strdup(optarg);
9224 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9225 argv[0], argv[optind - 1]);
9230 /* All remaining args are files, so we have at least nbfile */
9231 nbfile = argc - optind;
9233 if ((nbfile == 0) && (!filelist))
9237 opaque_len = strlen(opaque);
9240 * Alloc the request structure with enough place to store all files
9241 * from command line.
9243 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9245 fprintf(stderr, "Cannot create the request: %s\n",
9249 nbfile_alloc = nbfile;
9251 hur->hur_request.hr_action = action;
9252 hur->hur_request.hr_archive_id = archive_id;
9253 hur->hur_request.hr_flags = 0;
9255 /* All remaining args are files, add them */
9256 if (nbfile != 0 && some_file == NULL)
9257 some_file = strdup(argv[optind]);
9259 for (i = 0; i < nbfile; i++) {
9260 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9266 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9268 /* If a filelist was specified, read the filelist from it. */
9270 fp = fopen(filelist, "r");
9272 fprintf(stderr, "Cannot read the file list %s: %s\n",
9273 filelist, strerror(errno));
9278 while ((rc = getline(&line, &len, fp)) != -1) {
9280 * If allocated buffer was too small, get something
9283 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9286 nbfile_alloc = nbfile_alloc * 2 + 1;
9288 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9292 "hsm: cannot allocate the request: %s\n",
9299 size = hur_len(oldhur);
9302 "hsm: cannot allocate %u files + %u bytes data\n",
9303 oldhur->hur_request.hr_itemcount,
9304 oldhur->hur_request.hr_data_len);
9311 memcpy(hur, oldhur, size);
9316 if (line[strlen(line) - 1] == '\n')
9317 line[strlen(line) - 1] = '\0';
9319 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9320 mntpath, line, &last_dev);
9336 /* If a --data was used, add it to the request */
9337 hur->hur_request.hr_data_len = opaque_len;
9339 memcpy(hur_data(hur), opaque, opaque_len);
9341 /* Send the HSM request */
9342 if (realpath(some_file, fullpath) == NULL) {
9343 fprintf(stderr, "Could not find path '%s': %s\n",
9344 some_file, strerror(errno));
9346 rc = llapi_hsm_request(fullpath, hur);
9348 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9349 some_file, strerror(-rc));
9359 static int lfs_hsm_archive(int argc, char **argv)
9361 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9364 static int lfs_hsm_restore(int argc, char **argv)
9366 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9369 static int lfs_hsm_release(int argc, char **argv)
9371 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9374 static int lfs_hsm_remove(int argc, char **argv)
9376 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9379 static int lfs_hsm_cancel(int argc, char **argv)
9381 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9384 static int lfs_swap_layouts(int argc, char **argv)
9389 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9390 SWAP_LAYOUTS_KEEP_MTIME |
9391 SWAP_LAYOUTS_KEEP_ATIME);
9394 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9396 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9398 int lfs_get_mode(const char *string)
9400 enum lock_mode_user mode;
9402 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9403 if (lock_mode_names[mode] == NULL)
9405 if (strcmp(string, lock_mode_names[mode]) == 0)
9412 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9414 enum lu_ladvise_type advice;
9417 advice < ARRAY_SIZE(ladvise_names); advice++) {
9418 if (ladvise_names[advice] == NULL)
9420 if (strcmp(string, ladvise_names[advice]) == 0)
9424 return LU_LADVISE_INVALID;
9427 static int lfs_ladvise(int argc, char **argv)
9429 struct option long_opts[] = {
9430 { .val = 'a', .name = "advice", .has_arg = required_argument },
9431 { .val = 'b', .name = "background", .has_arg = no_argument },
9432 { .val = 'e', .name = "end", .has_arg = required_argument },
9433 { .val = 'l', .name = "length", .has_arg = required_argument },
9434 { .val = 'm', .name = "mode", .has_arg = required_argument },
9435 { .val = 's', .name = "start", .has_arg = required_argument },
9436 { .val = 'u', .name = "unset", .has_arg = no_argument },
9438 char short_opts[] = "a:be:l:m:s:u";
9443 struct llapi_lu_ladvise advice;
9444 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9445 unsigned long long start = 0;
9446 unsigned long long end = LUSTRE_EOF;
9447 unsigned long long length = 0;
9448 unsigned long long size_units;
9449 unsigned long long flags = 0;
9453 while ((c = getopt_long(argc, argv, short_opts,
9454 long_opts, NULL)) != -1) {
9457 advice_type = lfs_get_ladvice(optarg);
9458 if (advice_type == LU_LADVISE_INVALID) {
9460 "%s: invalid advice type '%s'\n",
9462 fprintf(stderr, "Valid types:");
9464 for (advice_type = 0;
9465 advice_type < ARRAY_SIZE(ladvise_names);
9467 if (ladvise_names[advice_type] == NULL)
9469 fprintf(stderr, " %s",
9470 ladvise_names[advice_type]);
9472 fprintf(stderr, "\n");
9485 rc = llapi_parse_size(optarg, &end,
9488 fprintf(stderr, "%s: bad end offset '%s'\n",
9495 rc = llapi_parse_size(optarg, &start,
9499 "%s: bad start offset '%s'\n",
9506 rc = llapi_parse_size(optarg, &length,
9509 fprintf(stderr, "%s: bad length '%s'\n",
9515 mode = lfs_get_mode(optarg);
9518 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9526 fprintf(stderr, "%s: option '%s' unrecognized\n",
9527 argv[0], argv[optind - 1]);
9532 if (advice_type == LU_LADVISE_INVALID) {
9533 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9534 fprintf(stderr, "Valid types:");
9535 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9537 if (ladvise_names[advice_type] == NULL)
9539 fprintf(stderr, " %s", ladvise_names[advice_type]);
9541 fprintf(stderr, "\n");
9545 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9547 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9552 if (argc <= optind) {
9553 fprintf(stderr, "%s: please give one or more file names\n",
9558 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9559 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9564 if (end == LUSTRE_EOF && length != 0)
9565 end = start + length;
9568 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9569 argv[0], start, end);
9573 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9574 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9579 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9580 fprintf(stderr, "%s: mode is required with lockahead\n",
9585 while (optind < argc) {
9588 path = argv[optind++];
9590 fd = open(path, O_RDONLY);
9592 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9593 argv[0], path, strerror(errno));
9598 advice.lla_start = start;
9599 advice.lla_end = end;
9600 advice.lla_advice = advice_type;
9601 advice.lla_value1 = 0;
9602 advice.lla_value2 = 0;
9603 advice.lla_value3 = 0;
9604 advice.lla_value4 = 0;
9605 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9606 advice.lla_lockahead_mode = mode;
9607 advice.lla_peradvice_flags = flags;
9610 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9614 "%s: cannot give advice '%s' to file '%s': %s\n",
9615 argv[0], ladvise_names[advice_type],
9616 path, strerror(errno));
9622 if (rc == 0 && rc2 < 0)
9628 static const char *const heat_names[] = LU_HEAT_NAMES;
9630 static int lfs_heat_get(int argc, char **argv)
9632 struct lu_heat *heat;
9641 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9643 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9648 while (optind < argc) {
9649 path = argv[optind++];
9651 fd = open(path, O_RDONLY);
9653 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9654 argv[0], path, strerror(errno));
9659 heat->lh_count = OBD_HEAT_COUNT;
9660 rc2 = llapi_heat_get(fd, heat);
9664 "%s: cannot get heat of file '%s': %s\n",
9665 argv[0], path, strerror(errno));
9669 printf("flags: %x\n", heat->lh_flags);
9670 for (i = 0; i < heat->lh_count; i++)
9671 printf("%s: %llu\n", heat_names[i],
9672 (unsigned long long)heat->lh_heat[i]);
9674 if (rc == 0 && rc2 < 0)
9682 static int lfs_heat_set(int argc, char **argv)
9684 struct option long_opts[] = {
9685 { .val = 'c', .name = "clear", .has_arg = no_argument },
9686 { .val = 'o', .name = "off", .has_arg = no_argument },
9687 { .val = 'O', .name = "on", .has_arg = no_argument },
9689 enum lu_heat_flag flags = 0;
9699 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9702 flags |= LU_HEAT_FLAG_CLEAR;
9705 flags |= LU_HEAT_FLAG_CLEAR;
9706 flags |= LU_HEAT_FLAG_OFF;
9709 flags &= ~LU_HEAT_FLAG_OFF;
9714 fprintf(stderr, "%s: option '%s' unrecognized\n",
9715 argv[0], argv[optind - 1]);
9720 if (argc <= optind) {
9721 fprintf(stderr, "%s: please give one or more file names\n",
9726 while (optind < argc) {
9727 path = argv[optind++];
9729 fd = open(path, O_RDONLY);
9731 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9732 argv[0], path, strerror(errno));
9737 rc2 = llapi_heat_set(fd, flags);
9741 "%s: cannot setflags heat of file '%s': %s\n",
9742 argv[0], path, strerror(errno));
9746 if (rc == 0 && rc2 < 0)
9753 * The input string contains a comma delimited list of component ids and
9754 * ranges, for example "1,2-4,7".
9756 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9758 bool end_of_loop = false;
9766 while (!end_of_loop) {
9770 char *endptr = NULL;
9773 ptr = strchrnul(arg, ',');
9774 end_of_loop = *ptr == '\0';
9777 start_index = strtol(arg, &endptr, 0);
9778 if (endptr == arg) /* no data at all */
9780 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9782 if (start_index < 0)
9785 end_index = start_index;
9786 if (*endptr == '-') {
9787 end_index = strtol(endptr + 1, &endptr, 0);
9788 if (*endptr != '\0')
9790 if (end_index < start_index)
9794 for (i = start_index; i <= end_index && size > 0; i++) {
9797 /* remove duplicate */
9798 for (j = 0; j < nr; j++) {
9802 if (j == nr) { /* no duplicate */
9808 if (size == 0 && i < end_index)
9815 if (!end_of_loop && ptr)
9818 return rc < 0 ? rc : nr;
9822 * struct verify_mirror_id - Mirror id to be verified.
9823 * @mirror_id: A specified mirror id.
9824 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9826 struct verify_mirror_id {
9832 * compare_mirror_ids() - Compare mirror ids.
9833 * @layout: Mirror component list.
9834 * @cbdata: Callback data in verify_mirror_id structure.
9836 * This is a callback function called by llapi_layout_comp_iterate()
9837 * to compare the specified mirror id with the one in the current
9838 * component of @layout. If they are the same, then the specified
9839 * mirror id is valid.
9841 * Return: a negative error code on failure or
9842 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9843 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9846 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9848 struct verify_mirror_id *mirror_id_cbdata =
9849 (struct verify_mirror_id *)cbdata;
9853 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9857 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9858 progname, strerror(errno));
9862 if (mirror_id_cbdata->mirror_id == mirror_id) {
9863 mirror_id_cbdata->is_valid_id = true;
9864 return LLAPI_LAYOUT_ITER_STOP;
9867 return LLAPI_LAYOUT_ITER_CONT;
9871 * verify_mirror_ids() - Verify specified mirror ids.
9872 * @fname: Mirrored file name.
9873 * @mirror_ids: Specified mirror ids to be verified.
9874 * @ids_nr: Number of specified mirror ids.
9876 * This function verifies that specified @mirror_ids are valid
9877 * in the mirrored file @fname.
9879 * Return: 0 on success or a negative error code on failure.
9882 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9884 struct llapi_layout *layout = NULL;
9885 struct verify_mirror_id mirror_id_cbdata = { 0 };
9896 if (stat(fname, &stbuf) < 0) {
9897 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9898 progname, fname, strerror(errno));
9903 if (!S_ISREG(stbuf.st_mode)) {
9904 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9910 fd = open(fname, O_DIRECT | O_RDONLY);
9912 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9913 progname, fname, strerror(errno));
9918 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9920 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9921 progname, fname, strerror(errno));
9925 layout = llapi_layout_get_by_fd(fd, 0);
9927 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9928 progname, fname, strerror(errno));
9930 llapi_lease_release(fd);
9934 rc = llapi_layout_flags_get(layout, &flr_state);
9936 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9937 progname, fname, strerror(errno));
9942 flr_state &= LCM_FL_FLR_MASK;
9943 switch (flr_state) {
9946 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9947 progname, fname, llapi_layout_flags_string(flr_state));
9954 for (i = 0; i < ids_nr; i++) {
9955 mirror_id_cbdata.mirror_id = mirror_ids[i];
9956 mirror_id_cbdata.is_valid_id = false;
9958 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9963 "%s: '%s' failed to verify mirror id: %u.\n",
9964 progname, fname, mirror_ids[i]);
9968 if (!mirror_id_cbdata.is_valid_id) {
9971 "%s: '%s' invalid specified mirror id: %u.\n",
9972 progname, fname, mirror_ids[i]);
9978 llapi_layout_free(layout);
9979 llapi_lease_release(fd);
9987 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9988 __u16 *mirror_ids, int ids_nr)
9990 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9991 struct llapi_layout *layout;
10002 if (stat(fname, &stbuf) < 0) {
10003 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10004 progname, fname, strerror(errno));
10008 if (!S_ISREG(stbuf.st_mode)) {
10009 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10015 fd = open(fname, O_DIRECT | O_RDWR);
10017 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10018 progname, fname, strerror(errno));
10023 layout = llapi_layout_get_by_fd(fd, 0);
10025 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10026 progname, fname, strerror(errno));
10031 rc = llapi_layout_flags_get(layout, &flr_state);
10033 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10034 progname, fname, strerror(errno));
10039 flr_state &= LCM_FL_FLR_MASK;
10040 if (flr_state == LCM_FL_NONE) {
10042 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10047 /* get stale component info */
10048 comp_size = llapi_mirror_find_stale(layout, comp_array,
10049 ARRAY_SIZE(comp_array),
10050 mirror_ids, ids_nr);
10051 if (comp_size <= 0) {
10056 ioc->lil_mode = LL_LEASE_WRLCK;
10057 ioc->lil_flags = LL_LEASE_RESYNC;
10058 rc = llapi_lease_set(fd, ioc);
10060 if (rc == -EALREADY)
10064 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10065 progname, fname, strerror(-rc));
10069 /* get the read range [start, end) */
10070 start = comp_array[0].lrc_start;
10071 end = comp_array[0].lrc_end;
10072 for (idx = 1; idx < comp_size; idx++) {
10073 if (comp_array[idx].lrc_start < start)
10074 start = comp_array[idx].lrc_start;
10075 if (end < comp_array[idx].lrc_end)
10076 end = comp_array[idx].lrc_end;
10079 rc = llapi_lease_check(fd);
10080 if (rc != LL_LEASE_WRLCK) {
10081 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10086 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10089 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10090 progname, fname, strerror(-rc));
10092 /* need to do the lease unlock even resync fails */
10093 ioc->lil_mode = LL_LEASE_UNLCK;
10094 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10095 ioc->lil_count = 0;
10096 for (idx = 0; idx < comp_size; idx++) {
10097 if (comp_array[idx].lrc_synced) {
10098 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10103 rc2 = llapi_lease_set(fd, ioc);
10105 * llapi_lease_set returns lease mode when it request to unlock
10109 /* rc2 == 0 means lost lease lock */
10110 if (rc2 == 0 && rc == 0)
10112 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10114 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10118 llapi_layout_free(layout);
10125 static inline int lfs_mirror_resync(int argc, char **argv)
10127 struct ll_ioc_lease *ioc = NULL;
10128 __u16 mirror_ids[128] = { 0 };
10133 struct option long_opts[] = {
10134 { .val = 'o', .name = "only", .has_arg = required_argument },
10135 { .name = NULL } };
10137 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10140 rc = parse_mirror_ids(mirror_ids,
10141 sizeof(mirror_ids) / sizeof(__u16),
10145 "%s: bad mirror ids '%s'.\n",
10152 fprintf(stderr, "%s: options '%s' unrecognized.\n",
10153 argv[0], argv[optind - 1]);
10159 if (argc == optind) {
10160 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10165 if (ids_nr > 0 && argc > optind + 1) {
10167 "%s: option '--only' cannot be used upon multiple files.\n",
10174 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10179 /* set the lease on the file */
10180 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10182 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10183 argv[0], strerror(errno));
10188 for (; optind < argc; optind++) {
10189 rc = lfs_mirror_resync_file(argv[optind], ioc,
10190 mirror_ids, ids_nr);
10191 /* ignore previous file's error, continue with next file */
10194 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10202 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10204 struct llapi_layout *layout;
10207 layout = llapi_layout_get_by_fd(fd, 0);
10209 fprintf(stderr, "could not get layout.\n");
10213 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10215 fprintf(stderr, "failed to iterate layout\n");
10216 llapi_layout_free(layout);
10219 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10220 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10221 llapi_layout_free(layout);
10225 llapi_layout_free(layout);
10231 * Check whether two files are the same file
10232 * \retval 0 same file
10233 * \retval 1 not the same file
10234 * \retval <0 error code
10236 static inline int check_same_file(int fd, const char *f2)
10238 struct stat stbuf1;
10239 struct stat stbuf2;
10241 if (fstat(fd, &stbuf1) < 0)
10244 if (stat(f2, &stbuf2) < 0)
10247 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10248 stbuf1.st_ino == stbuf2.st_ino)
10254 static inline int lfs_mirror_read(int argc, char **argv)
10257 __u16 mirror_id = 0;
10258 const char *outfile = NULL;
10264 const size_t buflen = 4 << 20;
10266 struct option long_opts[] = {
10267 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10268 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10269 { .name = NULL } };
10271 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10276 unsigned long int id;
10279 id = strtoul(optarg, &end, 0);
10280 if (errno != 0 || *end != '\0' || id == 0 ||
10283 "%s %s: invalid mirror ID '%s'\n",
10284 progname, argv[0], optarg);
10288 mirror_id = (__u16)id;
10295 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10296 progname, argv[optind - 1]);
10301 if (argc == optind) {
10302 fprintf(stderr, "%s %s: no mirrored file provided\n",
10303 progname, argv[0]);
10305 } else if (argc > optind + 1) {
10306 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10310 if (mirror_id == 0) {
10311 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10312 progname, argv[0]);
10316 /* open mirror file */
10317 fname = argv[optind];
10318 fd = open(fname, O_DIRECT | O_RDONLY);
10320 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10321 progname, argv[0], fname, strerror(errno));
10325 /* verify mirror id */
10326 rc = verify_mirror_id_by_fd(fd, mirror_id);
10329 "%s %s: cannot find mirror with ID %u in '%s'\n",
10330 progname, argv[0], mirror_id, fname);
10334 /* open output file - O_EXCL ensures output is not the same as input */
10336 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10338 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10339 progname, argv[0], outfile, strerror(errno));
10344 outfd = STDOUT_FILENO;
10347 /* allocate buffer */
10348 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10350 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10351 progname, argv[0], rc);
10357 ssize_t bytes_read;
10358 ssize_t written = 0;
10360 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10361 if (bytes_read < 0) {
10364 "%s %s: fail to read data from mirror %u: %s\n",
10365 progname, argv[0], mirror_id, strerror(-rc));
10370 if (bytes_read == 0)
10373 while (written < bytes_read) {
10376 written2 = write(outfd, buf + written,
10377 bytes_read - written);
10378 if (written2 < 0) {
10380 "%s %s: fail to write %s: %s\n",
10381 progname, argv[0], outfile ? : "STDOUT",
10386 written += written2;
10389 if (written != bytes_read) {
10391 "%s %s: written %ld bytes does not match with %ld read.\n",
10392 progname, argv[0], written, bytes_read);
10414 static inline int lfs_mirror_write(int argc, char **argv)
10417 __u16 mirror_id = 0;
10418 const char *inputfile = NULL;
10424 const size_t buflen = 4 << 20;
10426 size_t page_size = sysconf(_SC_PAGESIZE);
10427 struct ll_ioc_lease_id ioc;
10429 struct option long_opts[] = {
10430 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10431 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10432 { .name = NULL } };
10434 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10439 unsigned long int id;
10442 id = strtoul(optarg, &end, 0);
10443 if (errno != 0 || *end != '\0' || id == 0 ||
10446 "%s %s: invalid mirror ID '%s'\n",
10447 progname, argv[0], optarg);
10451 mirror_id = (__u16)id;
10455 inputfile = optarg;
10458 fprintf(stderr, "%s: option '%s' unrecognized\n",
10459 progname, argv[optind - 1]);
10464 if (argc == optind) {
10465 fprintf(stderr, "%s %s: no mirrored file provided\n",
10466 progname, argv[0]);
10468 } else if (argc > optind + 1) {
10469 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10473 if (mirror_id == 0) {
10474 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10475 progname, argv[0]);
10479 /* open mirror file */
10480 fname = argv[optind];
10481 fd = open(fname, O_DIRECT | O_WRONLY);
10483 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10484 progname, argv[0], fname, strerror(errno));
10488 /* verify mirror id */
10489 rc = verify_mirror_id_by_fd(fd, mirror_id);
10492 "%s %s: cannot find mirror with ID %u in '%s'\n",
10493 progname, argv[0], mirror_id, fname);
10497 /* open input file */
10499 rc = check_same_file(fd, inputfile);
10502 "%s %s: input file cannot be the mirrored file\n",
10503 progname, argv[0]);
10509 inputfd = open(inputfile, O_RDONLY, 0644);
10511 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10512 progname, argv[0], inputfile, strerror(errno));
10517 inputfd = STDIN_FILENO;
10520 /* allocate buffer */
10521 rc = posix_memalign(&buf, page_size, buflen);
10523 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10524 progname, argv[0], rc);
10525 goto close_inputfd;
10528 /* prepare target mirror components instantiation */
10529 ioc.lil_mode = LL_LEASE_WRLCK;
10530 ioc.lil_flags = LL_LEASE_RESYNC;
10531 ioc.lil_mirror_id = mirror_id;
10532 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10535 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10536 progname, argv[0], fname, strerror(errno));
10542 ssize_t bytes_read;
10546 rc = llapi_lease_check(fd);
10547 if (rc != LL_LEASE_WRLCK) {
10548 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10549 progname, argv[0], fname);
10553 bytes_read = read(inputfd, buf, buflen);
10554 if (bytes_read < 0) {
10557 "%s %s: fail to read data from '%s': %s\n",
10558 progname, argv[0], inputfile ? : "STDIN",
10565 if (bytes_read == 0)
10568 /* round up to page align to make direct IO happy. */
10569 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10571 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10576 "%s %s: fail to write to mirror %u: %s\n",
10577 progname, argv[0], mirror_id,
10585 if (pos & (page_size - 1)) {
10586 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10591 ioc.lil_mode = LL_LEASE_UNLCK;
10592 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10594 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10599 "%s %s: release lease lock of '%s' failed: %s\n",
10600 progname, argv[0], fname, strerror(errno));
10617 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10619 struct llapi_layout *layout;
10620 struct collect_ids_data cid = { .cid_ids = ids,
10622 .cid_exclude = exclude_id, };
10625 layout = llapi_layout_get_by_fd(fd, 0);
10627 fprintf(stderr, "could not get layout\n");
10631 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10633 fprintf(stderr, "failed to iterate layout\n");
10634 llapi_layout_free(layout);
10638 llapi_layout_free(layout);
10640 return cid.cid_count;
10643 static inline int lfs_mirror_copy(int argc, char **argv)
10646 __u16 read_mirror_id = 0;
10647 __u16 ids[128] = { 0 };
10649 struct llapi_layout *layout = NULL;
10650 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10657 struct ll_ioc_lease *ioc = NULL;
10658 struct ll_ioc_lease_id *resync_ioc;
10660 struct option long_opts[] = {
10661 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10662 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10663 { .name = NULL } };
10665 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10670 unsigned long int id;
10673 id = strtoul(optarg, &end, 0);
10674 if (errno != 0 || *end != '\0' || id == 0 ||
10677 "%s %s: invalid read mirror ID '%s'\n",
10678 progname, argv[0], optarg);
10682 read_mirror_id = (__u16)id;
10686 if (!strcmp(optarg, "-1")) {
10687 /* specify all other mirrors */
10688 ids[0] = (__u16)-1;
10691 count = parse_mirror_ids((__u16 *)ids,
10699 fprintf(stderr, "%s: option '%s' unrecognized\n",
10700 progname, argv[optind - 1]);
10705 if (argc == optind) {
10706 fprintf(stderr, "%s %s: no mirrored file provided\n",
10707 progname, argv[0]);
10709 } else if (argc > optind + 1) {
10710 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10714 if (read_mirror_id == 0) {
10716 "%s %s: no valid read mirror ID %d is provided\n",
10717 progname, argv[0], read_mirror_id);
10723 "%s %s: no write mirror ID is provided\n",
10724 progname, argv[0]);
10728 for (i = 0; i < count; i++) {
10729 if (read_mirror_id == ids[i]) {
10731 "%s %s: read and write mirror ID cannot be the same\n",
10732 progname, argv[0]);
10737 /* open mirror file */
10738 fname = argv[optind];
10740 fd = open(fname, O_DIRECT | O_RDWR);
10742 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10743 progname, argv[0], fname, strerror(errno));
10747 /* write to all other mirrors */
10748 if (ids[0] == (__u16)-1) {
10749 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10753 "%s %s: failed to get other mirror ids in '%s': %d\n",
10754 progname, argv[0], fname, rc);
10759 /* verify mirror id */
10760 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10763 "%s %s: cannot find mirror with ID %u in '%s'\n",
10764 progname, argv[0], read_mirror_id, fname);
10768 for (i = 0; i < count; i++) {
10769 rc = verify_mirror_id_by_fd(fd, ids[i]);
10772 "%s %s: cannot find mirror with ID %u in '%s'\n",
10773 progname, argv[0], ids[i], fname);
10778 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10781 "%s %s: cannot alloc comp id array for ioc: %s\n",
10782 progname, argv[0], strerror(errno));
10787 /* get stale component info */
10788 layout = llapi_layout_get_by_fd(fd, 0);
10790 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10791 progname, argv[0], fname, strerror(errno));
10795 comp_size = llapi_mirror_find_stale(layout, comp_array,
10796 ARRAY_SIZE(comp_array),
10798 llapi_layout_free(layout);
10799 if (comp_size < 0) {
10804 /* prepare target mirror components instantiation */
10805 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10806 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10807 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10809 resync_ioc->lil_mirror_id = ids[0];
10811 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10812 rc = llapi_lease_set(fd, ioc);
10815 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10816 progname, argv[0], fname, strerror(errno));
10820 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10823 fprintf(stderr, "%s %s: copy error: %d\n",
10824 progname, argv[0], rc);
10828 fprintf(stdout, "mirror copied successfully: ");
10829 for (i = 0; i < copied; i++)
10830 fprintf(stdout, "%d ", ids[i]);
10831 fprintf(stdout, "\n");
10833 ioc->lil_mode = LL_LEASE_UNLCK;
10834 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10835 ioc->lil_count = 0;
10836 for (i = 0; i < comp_size; i++) {
10839 for (j = 0; j < copied; j++) {
10840 if (comp_array[i].lrc_mirror_id != ids[j])
10843 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10847 rc = llapi_lease_set(fd, ioc);
10852 "%s %s: release lease lock of '%s' failed: %s\n",
10853 progname, argv[0], fname, strerror(errno));
10868 * struct verify_chunk - Mirror chunk to be verified.
10869 * @chunk: [start, end) of the chunk.
10870 * @mirror_count: Number of mirror ids in @mirror_id array.
10871 * @mirror_id: Array of valid mirror ids that cover the chunk.
10873 struct verify_chunk {
10874 struct lu_extent chunk;
10875 unsigned int mirror_count;
10876 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10880 * print_chunks() - Print chunk information.
10881 * @fname: Mirrored file name.
10882 * @chunks: Array of chunks.
10883 * @chunk_count: Number of chunks in @chunks array.
10885 * This function prints [start, end) of each chunk in @chunks
10886 * for mirrored file @fname, and also prints the valid mirror ids
10887 * that cover the chunk.
10892 void print_chunks(const char *fname, struct verify_chunk *chunks,
10898 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10899 for (i = 0; i < chunk_count; i++) {
10900 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10902 if (chunks[i].mirror_count == 0)
10903 fprintf(stdout, "\t[");
10905 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10906 for (j = 1; j < chunks[i].mirror_count; j++)
10907 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10909 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10911 fprintf(stdout, "\n");
10915 * print_checksums() - Print CRC-32 checksum values.
10916 * @chunk: A chunk and its corresponding valid mirror ids.
10917 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10919 * This function prints CRC-32 checksum values on @chunk for
10920 * each valid mirror that covers it.
10925 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10930 "CRC-32 checksum value for chunk "DEXT":\n",
10931 PEXT(&chunk->chunk));
10932 for (i = 0; i < chunk->mirror_count; i++)
10933 fprintf(stdout, "Mirror %u:\t%#lx\n",
10934 chunk->mirror_id[i], crc[i]);
10935 fprintf(stdout, "\n");
10939 * filter_mirror_id() - Filter specified mirror ids.
10940 * @chunks: Array of chunks.
10941 * @chunk_count: Number of chunks in @chunks array.
10942 * @mirror_ids: Specified mirror ids to be verified.
10943 * @ids_nr: Number of specified mirror ids.
10945 * This function scans valid mirror ids that cover each chunk in @chunks
10946 * and filters specified mirror ids.
10951 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10952 __u16 *mirror_ids, int ids_nr)
10957 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10958 unsigned int valid_count = 0;
10960 for (i = 0; i < chunk_count; i++) {
10961 if (chunks[i].mirror_count == 0)
10965 for (j = 0; j < ids_nr; j++) {
10966 for (k = 0; k < chunks[i].mirror_count; k++) {
10967 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10968 valid_id[valid_count] = mirror_ids[j];
10975 memcpy(chunks[i].mirror_id, valid_id,
10976 sizeof(__u16) * valid_count);
10977 chunks[i].mirror_count = valid_count;
10982 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10983 * @layout: Mirror component list.
10984 * @chunks: Array of chunks.
10985 * @chunks_size: Array size of @chunks.
10987 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10988 * to find out chunk segments and store them in @chunks array.
10990 * The @mirror_id array in each element of @chunks will store the valid
10991 * mirror ids that cover the chunk. If a mirror component covering the
10992 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10993 * will not be stored into the @mirror_id array, and the chunk for that
10994 * mirror will not be verified.
10996 * The @mirror_count in each element of @chunks will store the number of
10997 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10998 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10999 * indicates the chunk is valid in only one mirror. In both cases, the
11000 * chunk will not be verified.
11002 * Here is an example:
11004 * 0 1M 2M 3M 4M EOF
11005 * +------+-------------+--------------------+
11006 * | | | S | mirror1
11007 * +------+------+------+------+-------------+
11008 * | | S | S | | mirror2
11009 * +-------------+------+------+-------------+
11011 * prepared @chunks array will contain 5 elements:
11012 * (([0, 1M), [1, 2], 2),
11013 * ([1M, 2M), [1, 2], 2),
11014 * ([2M, 3M), [1], 1),
11015 * ([3M, 4M], [], 0),
11016 * ([4M, EOF), [2], 1))
11018 * Return: the actual array size of @chunks on success
11019 * or a negative error code on failure.
11022 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11023 struct verify_chunk *chunks,
11024 size_t chunks_size)
11028 uint32_t mirror_id;
11034 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11037 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11040 "%s: move to the first layout component: %s.\n",
11041 progname, strerror(errno));
11047 chunks[idx].chunk.e_end = LUSTRE_EOF;
11049 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11052 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11053 progname, strerror(errno));
11057 if (start > chunks[idx].chunk.e_start ||
11058 end <= chunks[idx].chunk.e_start)
11061 if (end < chunks[idx].chunk.e_end)
11062 chunks[idx].chunk.e_end = end;
11064 rc = llapi_layout_comp_flags_get(layout, &flags);
11067 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11068 progname, strerror(errno));
11072 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11075 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11078 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11079 progname, strerror(errno));
11083 chunks[idx].mirror_id[i] = mirror_id;
11085 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11087 "%s: mirror_id array is too small.\n",
11094 rc = llapi_layout_comp_use(layout,
11095 LLAPI_LAYOUT_COMP_USE_NEXT);
11098 "%s: move to the next layout component: %s.\n",
11099 progname, strerror(errno));
11102 } /* loop through all components */
11104 chunks[idx].mirror_count = i;
11106 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11110 if (idx >= chunks_size) {
11111 fprintf(stderr, "%s: chunks array is too small.\n",
11117 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11121 return rc < 0 ? rc : idx + 1;
11125 * lfs_mirror_verify_chunk() - Verify a chunk.
11126 * @fd: File descriptor of the mirrored file.
11127 * @file_size: Size of the mirrored file.
11128 * @chunk: A chunk and its corresponding valid mirror ids.
11129 * @verbose: Verbose mode.
11131 * This function verifies a @chunk contains exactly the same data
11132 * ammong the mirrors that cover it.
11134 * If @verbose is specified, then the function will print where the
11135 * differences are if the data do not match. Otherwise, it will
11136 * just return an error in that case.
11138 * Return: 0 on success or a negative error code on failure.
11141 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11142 struct verify_chunk *chunk, int verbose)
11144 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11146 size_t page_size = sysconf(_SC_PAGESIZE);
11147 ssize_t bytes_read;
11148 ssize_t bytes_done;
11152 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11156 if (file_size == 0)
11159 rc = posix_memalign(&buf, page_size, buflen);
11160 if (rc) /* error code is returned directly */
11164 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11165 PEXT(&chunk->chunk));
11166 for (i = 0; i < chunk->mirror_count; i++)
11167 fprintf(stdout, " %u", chunk->mirror_id[i]);
11168 fprintf(stdout, "\n");
11172 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11173 pos = chunk->chunk.e_start;
11174 while (bytes_done < count) {
11175 /* compute initial CRC-32 checksum */
11176 crc = crc32(0L, Z_NULL, 0);
11177 memset(crc_array, 0, sizeof(crc_array));
11180 for (i = 0; i < chunk->mirror_count; i++) {
11181 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11183 if (bytes_read < 0) {
11186 "%s: failed to read data from mirror %u: %s.\n",
11187 progname, chunk->mirror_id[i],
11192 /* compute new CRC-32 checksum */
11193 crc_array[i] = crc32(crc, buf, bytes_read);
11197 print_checksums(chunk, crc_array);
11199 /* compare CRC-32 checksum values */
11200 for (i = 1; i < chunk->mirror_count; i++) {
11201 if (crc_array[i] != crc_array[0]) {
11205 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11206 progname, PEXT(&chunk->chunk),
11207 chunk->mirror_id[0],
11208 chunk->mirror_id[i]);
11213 bytes_done += bytes_read;
11216 if (verbose > 1 && rc == 0) {
11217 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11218 PEXT(&chunk->chunk));
11219 for (i = 0; i < chunk->mirror_count; i++)
11220 fprintf(stdout, " %u", chunk->mirror_id[i]);
11221 fprintf(stdout, " PASS\n\n");
11230 * lfs_mirror_verify_file() - Verify a mirrored file.
11231 * @fname: Mirrored file name.
11232 * @mirror_ids: Specified mirror ids to be verified.
11233 * @ids_nr: Number of specified mirror ids.
11234 * @verbose: Verbose mode.
11236 * This function verifies that each SYNC mirror of a mirrored file
11237 * specified by @fname contains exactly the same data.
11239 * If @mirror_ids is specified, then the function will verify the
11240 * mirrors specified by @mirror_ids contain exactly the same data.
11242 * If @verbose is specified, then the function will print where the
11243 * differences are if the data do not match. Otherwise, it will
11244 * just return an error in that case.
11246 * Return: 0 on success or a negative error code on failure.
11249 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11252 struct verify_chunk chunks_array[1024] = { };
11253 struct llapi_layout *layout = NULL;
11255 uint32_t flr_state;
11257 int chunk_count = 0;
11263 if (stat(fname, &stbuf) < 0) {
11264 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11265 progname, fname, strerror(errno));
11270 if (!S_ISREG(stbuf.st_mode)) {
11271 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11277 if (stbuf.st_size == 0) {
11279 fprintf(stdout, "%s: '%s' file size is 0.\n",
11285 fd = open(fname, O_DIRECT | O_RDONLY);
11287 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11288 progname, fname, strerror(errno));
11293 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11295 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11296 progname, fname, strerror(errno));
11300 layout = llapi_layout_get_by_fd(fd, 0);
11302 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11303 progname, fname, strerror(errno));
11305 llapi_lease_release(fd);
11309 rc = llapi_layout_flags_get(layout, &flr_state);
11311 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11312 progname, fname, strerror(errno));
11317 flr_state &= LCM_FL_FLR_MASK;
11318 switch (flr_state) {
11321 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11322 progname, fname, llapi_layout_flags_string(flr_state));
11328 /* find out mirror chunks to be verified */
11329 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11330 ARRAY_SIZE(chunks_array));
11331 if (chunk_count < 0) {
11337 /* filter specified mirror ids */
11338 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11341 print_chunks(fname, chunks_array, chunk_count);
11343 for (idx = 0; idx < chunk_count; idx++) {
11344 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11347 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11349 PEXT(&chunks_array[idx].chunk),
11350 (unsigned long long)stbuf.st_size);
11354 if (chunks_array[idx].mirror_count == 0) {
11356 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11358 PEXT(&chunks_array[idx].chunk));
11360 fprintf(stderr, "skipped\n");
11364 fprintf(stderr, "failed\n");
11368 if (chunks_array[idx].mirror_count == 1) {
11371 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11373 PEXT(&chunks_array[idx].chunk),
11374 chunks_array[idx].mirror_id[0]);
11378 rc = llapi_lease_check(fd);
11379 if (rc != LL_LEASE_RDLCK) {
11380 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11385 /* verify one chunk */
11386 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11387 &chunks_array[idx], verbose);
11401 llapi_layout_free(layout);
11402 llapi_lease_release(fd);
11410 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11411 * @argc: The count of lfs mirror verify command line arguments.
11412 * @argv: Array of strings for lfs mirror verify command line arguments.
11414 * This function parses lfs mirror verify command and verifies the
11415 * specified mirrored file(s).
11417 * Return: 0 on success or a negative error code on failure.
11419 static inline int lfs_mirror_verify(int argc, char **argv)
11421 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11427 char cmd[PATH_MAX];
11429 struct option long_opts[] = {
11430 { .val = 'o', .name = "only", .has_arg = required_argument },
11431 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11432 { .name = NULL } };
11434 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11436 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11439 rc = parse_mirror_ids(mirror_ids,
11440 ARRAY_SIZE(mirror_ids),
11444 "%s: bad mirror ids '%s'.\n",
11451 "%s: at least 2 mirror ids needed with '--only' option.\n",
11461 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11462 progname, argv[optind - 1]);
11468 if (argc == optind) {
11469 fprintf(stderr, "%s: no file name given.\n", progname);
11474 if (ids_nr > 0 && argc > optind + 1) {
11476 "%s: '--only' cannot be used upon multiple files.\n",
11483 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11489 for (; optind < argc; optind++) {
11490 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11500 * lfs_mirror() - Parse and execute lfs mirror commands.
11501 * @argc: The count of lfs mirror command line arguments.
11502 * @argv: Array of strings for lfs mirror command line arguments.
11504 * This function parses lfs mirror commands and performs the
11505 * corresponding functions specified in mirror_cmdlist[].
11507 * Return: 0 on success or an error code on failure.
11509 static int lfs_mirror(int argc, char **argv)
11511 char cmd[PATH_MAX];
11514 setlinebuf(stdout);
11516 Parser_init("lfs-mirror > ", mirror_cmdlist);
11518 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11520 program_invocation_short_name = cmd;
11522 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11524 rc = Parser_commands();
11526 return rc < 0 ? -rc : rc;
11529 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11531 #if __BYTE_ORDER == __BIG_ENDIAN
11532 __swab16s(&attrs->lsa_valid);
11533 __swab64s(&attrs->lsa_size);
11534 __swab64s(&attrs->lsa_blocks);
11538 enum lfs_som_type {
11539 LFS_SOM_SIZE = 0x1,
11540 LFS_SOM_BLOCKS = 0x2,
11541 LFS_SOM_FLAGS = 0x4,
11542 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11546 static int lfs_getsom(int argc, char **argv)
11549 struct lustre_som_attrs *attrs;
11550 char buf[sizeof(*attrs) + 64];
11551 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11554 while ((c = getopt(argc, argv, "sbf")) != -1) {
11557 type = LFS_SOM_SIZE;
11560 type = LFS_SOM_BLOCKS;
11563 type = LFS_SOM_FLAGS;
11566 fprintf(stderr, "%s: invalid option '%c'\n",
11576 fprintf(stderr, "%s: %s\n",
11577 progname, argc == 0 ? "miss file target" :
11578 "input more than 2 files");
11583 attrs = (void *)buf;
11584 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11587 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11588 argv[0], strerror(errno), errno);
11592 lustre_som_swab(attrs);
11595 case LFS_SOM_ATTR_ALL:
11596 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11597 path, (unsigned long long)attrs->lsa_size,
11598 (unsigned long long)attrs->lsa_blocks,
11602 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11604 case LFS_SOM_BLOCKS:
11605 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11607 case LFS_SOM_FLAGS:
11608 printf("%x\n", attrs->lsa_valid);
11611 fprintf(stderr, "%s: unknown option\n", progname);
11619 * lfs_mirror_list_commands() - List lfs mirror commands.
11620 * @argc: The count of command line arguments.
11621 * @argv: Array of strings for command line arguments.
11623 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11625 * Return: 0 on success.
11627 static int lfs_mirror_list_commands(int argc, char **argv)
11629 char buffer[81] = "";
11631 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11637 static int lfs_pcc_attach(int argc, char **argv)
11639 struct option long_opts[] = {
11640 { .val = 'i', .name = "id", .has_arg = required_argument },
11641 { .name = NULL } };
11644 __u32 archive_id = 0;
11647 char fullpath[PATH_MAX];
11648 enum lu_pcc_type type = LU_PCC_READWRITE;
11651 while ((c = getopt_long(argc, argv, "i:",
11652 long_opts, NULL)) != -1) {
11656 archive_id = strtoul(optarg, &end, 0);
11657 if (errno != 0 || *end != '\0' ||
11658 archive_id == 0 || archive_id > UINT32_MAX) {
11660 "error: %s: bad archive ID '%s'\n",
11668 fprintf(stderr, "%s: option '%s' unrecognized\n",
11669 argv[0], argv[optind - 1]);
11674 if (archive_id == 0) {
11675 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11679 if (argc <= optind) {
11680 fprintf(stderr, "%s: must specify one or more file names\n",
11685 while (optind < argc) {
11688 path = argv[optind++];
11689 if (!realpath(path, fullpath)) {
11690 fprintf(stderr, "%s: could not find path '%s': %s\n",
11691 argv[0], path, strerror(errno));
11697 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11700 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11701 argv[0], path, archive_id, strerror(-rc2));
11709 static int lfs_pcc_attach_fid(int argc, char **argv)
11711 struct option long_opts[] = {
11712 { .val = 'i', .name = "id", .has_arg = required_argument },
11713 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11714 { .name = NULL } };
11715 char short_opts[] = "i:m:";
11718 __u32 archive_id = 0;
11720 const char *mntpath = NULL;
11721 const char *fidstr;
11722 enum lu_pcc_type type = LU_PCC_READWRITE;
11725 while ((c = getopt_long(argc, argv, short_opts,
11726 long_opts, NULL)) != -1) {
11730 archive_id = strtoul(optarg, &end, 0);
11731 if (errno != 0 || *end != '\0' ||
11732 archive_id > UINT32_MAX) {
11734 "error: %s: bad archive ID '%s'\n",
11745 fprintf(stderr, "%s: option '%s' unrecognized\n",
11746 argv[0], argv[optind - 1]);
11751 if (archive_id == 0) {
11752 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11757 fprintf(stderr, "%s: must specify Lustre mount point\n",
11762 if (argc <= optind) {
11763 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11767 while (optind < argc) {
11770 fidstr = argv[optind++];
11772 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11776 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11777 argv[0], fidstr, mntpath, archive_id,
11780 if (rc == 0 && rc2 < 0)
11786 static int lfs_pcc_detach(int argc, char **argv)
11788 struct option long_opts[] = {
11789 { .val = 'k', .name = "keep", .has_arg = no_argument },
11790 { .name = NULL } };
11791 char short_opts[] = "k";
11795 char fullpath[PATH_MAX];
11796 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11799 while ((c = getopt_long(argc, argv, short_opts,
11800 long_opts, NULL)) != -1) {
11803 detach_opt = PCC_DETACH_OPT_NONE;
11808 fprintf(stderr, "%s: option '%s' unrecognized\n",
11809 argv[0], argv[optind - 1]);
11814 while (optind < argc) {
11817 path = argv[optind++];
11818 if (!realpath(path, fullpath)) {
11819 fprintf(stderr, "%s: could not find path '%s': %s\n",
11820 argv[0], path, strerror(errno));
11826 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11830 "%s: cannot detach '%s' from PCC: %s\n",
11831 argv[0], path, strerror(errno));
11839 static int lfs_pcc_detach_fid(int argc, char **argv)
11841 struct option long_opts[] = {
11842 { .val = 'k', .name = "keep", .has_arg = no_argument },
11843 { .name = NULL } };
11844 char short_opts[] = "k";
11848 const char *mntpath;
11849 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11852 while ((c = getopt_long(argc, argv, short_opts,
11853 long_opts, NULL)) != -1) {
11856 detach_opt = PCC_DETACH_OPT_NONE;
11861 fprintf(stderr, "%s: option '%s' unrecognized\n",
11862 argv[0], argv[optind - 1]);
11867 mntpath = argv[optind++];
11869 while (optind < argc) {
11872 fid = argv[optind++];
11874 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11877 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11878 argv[0], fid, mntpath, strerror(-rc2));
11886 static int lfs_pcc_state(int argc, char **argv)
11890 char fullpath[PATH_MAX];
11891 struct lu_pcc_state state;
11896 fprintf(stderr, "%s: must specify one or more file names\n",
11901 while (optind < argc) {
11904 path = argv[optind++];
11905 if (!realpath(path, fullpath)) {
11906 fprintf(stderr, "%s: could not find path '%s': %s\n",
11907 argv[0], path, strerror(errno));
11913 rc2 = llapi_pcc_state_get(fullpath, &state);
11918 "%s: cannot get PCC state of '%s': %s\n",
11919 argv[0], path, strerror(-rc2));
11923 printf("file: %s", path);
11924 printf(", type: %s", pcc_type2string(state.pccs_type));
11925 if (state.pccs_type == LU_PCC_NONE &&
11926 state.pccs_open_count == 0) {
11931 printf(", PCC file: %s", state.pccs_path);
11932 printf(", user number: %u", state.pccs_open_count);
11933 printf(", flags: %x", state.pccs_flags);
11940 * lfs_pcc_list_commands() - List lfs pcc commands.
11941 * @argc: The count of command line arguments.
11942 * @argv: Array of strings for command line arguments.
11944 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11946 * Return: 0 on success.
11948 static int lfs_pcc_list_commands(int argc, char **argv)
11950 char buffer[81] = "";
11952 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11959 * lfs_pcc() - Parse and execute lfs pcc commands.
11960 * @argc: The count of lfs pcc command line arguments.
11961 * @argv: Array of strings for lfs pcc command line arguments.
11963 * This function parses lfs pcc commands and performs the
11964 * corresponding functions specified in pcc_cmdlist[].
11966 * Return: 0 on success or an error code on failure.
11968 static int lfs_pcc(int argc, char **argv)
11970 char cmd[PATH_MAX];
11973 setlinebuf(stdout);
11975 Parser_init("lfs-pcc > ", pcc_cmdlist);
11977 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11979 program_invocation_short_name = cmd;
11981 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11983 rc = Parser_commands();
11985 return rc < 0 ? -rc : rc;
11988 static int lfs_list_commands(int argc, char **argv)
11990 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11992 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11997 int main(int argc, char **argv)
12001 /* Ensure that liblustreapi constructor has run */
12002 if (!llapi_liblustreapi_initialized())
12003 fprintf(stderr, "liblustreapi was not properly initialized\n");
12005 setlinebuf(stdout);
12008 Parser_init("lfs > ", cmdlist);
12010 progname = program_invocation_short_name; /* Used in error messages */
12012 llapi_set_command_name(argv[1]);
12013 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12014 llapi_clear_command_name();
12016 rc = Parser_commands();
12019 return rc < 0 ? -rc : rc;
12022 #ifdef _LUSTRE_IDL_H_
12023 /* Everything we need here should be included by lustreapi.h. */
12024 # error "lfs should not depend on lustre_idl.h"
12025 #endif /* _LUSTRE_IDL_H_ */