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 enum setstripe_origin {
152 static int lfs_setstripe_internal(int argc, char **argv,
153 enum setstripe_origin opc);
155 static inline int lfs_setstripe(int argc, char **argv)
157 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
160 static inline int lfs_setstripe_migrate(int argc, char **argv)
162 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
165 static inline int lfs_mirror_create(int argc, char **argv)
167 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
170 static inline int lfs_mirror_extend(int argc, char **argv)
172 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
175 static inline int lfs_mirror_split(int argc, char **argv)
177 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
180 static inline int lfs_mirror_delete(int argc, char **argv)
182 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
185 /* Setstripe and migrate share mostly the same parameters */
186 #define SSM_CMD_COMMON(cmd) \
187 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
188 " [--stripe-count|-c <stripe_count>]\n" \
189 " [--overstripe-count|-C <stripe_count>]\n" \
190 " [--stripe-index|-i <start_ost_idx>]\n" \
191 " [--stripe-size|-S <stripe_size>]\n" \
192 " [--extension-size|--ext-size|-z]\n" \
193 " [--layout|-L <pattern>]\n" \
194 " [--mirror-count|-N[mirror_count]]\n" \
195 " [--ost|-o <ost_indices>]\n" \
196 " [--pool|-p <pool_name>]\n" \
197 " [--yaml|-y <yaml_template_file>]\n" \
198 " [--copy=<lustre_src>]\n"
200 #define SSM_HELP_COMMON \
201 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
202 "\t Using -C instead of -c allows overstriping, which\n" \
203 "\t will place more than one stripe per OST if\n" \
204 "\t stripe_count is greater than the number of OSTs\n" \
205 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
206 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
207 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
208 "\t respectively)\n" \
209 "\textension_size:\n" \
210 "\t Number of bytes the previous component is extended\n" \
211 "\t each time. Can be specified with K, M, G (for KB,\n" \
212 "\t MB, GB respectively)\n" \
213 "\tpool_name: Name of OST pool to use (default none)\n" \
214 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
215 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
216 "\t Indices be specified in a format of:\n" \
217 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
219 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
220 "\t If --pool is set with --ost then the OSTs\n" \
221 "\t must be the members of the pool.\n" \
222 "\tcomp_end: Extent end of component, start after previous end.\n"\
223 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
224 "\t respectively, -1 for EOF). Must be a multiple of\n"\
225 "\t stripe_size.\n" \
226 "\tyaml_template_file:\n" \
227 "\t YAML layout template file, can't be used with -c,\n" \
228 "\t -i, -S, -p, -o, or -E arguments.\n" \
229 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
230 "\t another lustre file or directory, can't used with\n" \
231 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
233 #define MIRROR_CREATE_HELP \
234 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
235 "\t setstripe layout options\n" \
236 "\t It defaults to 1 if not specified; if specified,\n" \
237 "\t it must follow the option without a space.\n" \
238 "\t The option can also be repeated multiple times to\n" \
239 "\t separate mirrors that have different layouts.\n" \
240 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
241 "\t It can be a plain layout or a composite layout.\n" \
242 "\t If not specified, the stripe options inherited\n" \
243 "\t from the previous component will be used.\n" \
244 "\tflags: set flags to the component of the current mirror.\n" \
245 "\t Only \"prefer\" flag is supported so far.\n"
247 #define MIRROR_EXTEND_HELP \
249 "\tvictim_file: The layout of victim_file will be split and used\n" \
250 "\t as a mirror added to the mirrored file.\n" \
251 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
252 "\t from victim file(s) in case the victim file(s)\n" \
253 "\t contains the same data as the original mirrored\n" \
256 #define MIRROR_EXTEND_USAGE \
257 " {--mirror-count|-N[mirror_count]}\n" \
258 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
261 #define SETSTRIPE_USAGE \
262 SSM_CMD_COMMON("setstripe") \
263 MIRROR_EXTEND_USAGE \
264 " <directory|filename>\n" \
268 #define MIGRATE_USAGE \
269 SSM_CMD_COMMON("migrate ") \
270 " [--block|-b] [--non-block|-n]\n" \
271 " [--non-direct|-D] [--verbose|-v]\n" \
275 "\tblock: Block file access during data migration (default)\n" \
276 "\tnon-block: Abort migrations if concurrent access is detected\n" \
277 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
278 "\tverbose: Print each filename as it is migrated\n" \
280 #define SETDIRSTRIPE_USAGE \
281 " [--mdt-count|-c stripe_count>\n" \
282 " [--mdt-hash|-H mdt_hash]\n" \
283 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
284 " [--default|-D] [--mode|-o mode] <dir>\n" \
285 "\tstripe_count: stripe count of the striped directory\n" \
286 "\tmdt_index: MDT index of first stripe\n" \
287 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
288 " crush CRUSH hash algorithm (default)\n" \
289 " fnv_1a_64 FNV-1a hash algorithm\n" \
290 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
291 "\tdefault_stripe: set default dirstripe of the directory\n" \
292 "\tmode: the file access permission of the directory (octal)\n" \
293 "To create dir with a foreign (free format) layout :\n" \
294 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
295 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
296 "\tmode: the mode of the directory\n" \
297 "\tforeign_type: none or symlink\n"
300 * command_t mirror_cmdlist - lfs mirror commands.
302 command_t mirror_cmdlist[] = {
303 { .pc_name = "create", .pc_func = lfs_mirror_create,
304 .pc_help = "Create a mirrored file.\n"
305 "usage: lfs mirror create "
306 "<--mirror-count|-N[mirror_count]> "
307 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
308 MIRROR_CREATE_HELP },
309 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
310 .pc_help = "Delete a mirror from a file.\n"
311 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
312 "\t --component-id|--comp-id|-I <comp_id> |\n"
313 "\t -p <pool>} <mirrored_file> ...\n"
315 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
316 .pc_help = "Extend a mirrored file.\n"
317 "usage: lfs mirror extend "
318 "<--mirror-count|-N[mirror_count]> [--no-verify] "
319 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
320 MIRROR_EXTEND_HELP },
321 { .pc_name = "split", .pc_func = lfs_mirror_split,
322 .pc_help = "Split a mirrored file.\n"
323 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
324 "\t --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
325 "\t [-f <new_file>] <mirrored_file> ...\n"
326 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
327 "\t can be fetched by lfs getstripe command.\n"
328 "\tcomp_id: Unique component ID within a mirror.\n"
329 "\tpool: Components on specified pool.\n"
330 "\tnew_file: This option indicates the layout of the split\n"
331 "\t mirror will be stored into. If not specified,\n"
332 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
333 "\t will be used.\n" },
334 { .pc_name = "read", .pc_func = lfs_mirror_read,
335 .pc_help = "Read the content of a specified mirror of a file.\n"
336 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
337 "[--outfile|-o <output_file>] <mirrored_file>\n" },
338 { .pc_name = "write", .pc_func = lfs_mirror_write,
339 .pc_help = "Write to a specified mirror of a file.\n"
340 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
341 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
342 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
343 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
344 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
345 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
346 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
347 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
348 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
349 "<mirrored file> [<mirrored file2>...]\n"},
350 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
351 .pc_help = "Verify mirrored file(s).\n"
352 "usage: lfs mirror verify "
353 "[--only <mirror_id,mirror_id2[,...]>] "
354 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
355 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
356 .pc_help = "list commands supported by lfs mirror"},
357 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
358 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
359 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
364 * command_t pcc_cmdlist - lfs pcc commands.
366 command_t pcc_cmdlist[] = {
367 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
368 .pc_help = "Attach given files to the Persistent Client Cache.\n"
369 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
370 "\t-i: archive id for RW-PCC\n" },
371 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
372 .pc_help = "Attach given files into PCC by FID(s).\n"
373 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
375 "\t-i: archive id for RW-PCC\n"
376 "\t-m: Lustre mount point\n" },
377 { .pc_name = "state", .pc_func = lfs_pcc_state,
378 .pc_help = "Display the PCC state for given files.\n"
379 "usage: lfs pcc state <file> ...\n" },
380 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
381 .pc_help = "Detach given files from the Persistent Client Cache.\n"
382 "usage: lfs pcc detach <file> ...\n" },
383 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
384 .pc_help = "Detach given files from PCC by FID(s).\n"
385 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
386 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
387 .pc_help = "list commands supported by lfs pcc"},
388 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
389 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
390 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
394 /* all available commands */
395 command_t cmdlist[] = {
396 {"setstripe", lfs_setstripe, 0,
397 "To create a file with specified striping/composite layout, or\n"
398 "create/replace the default layout on an existing directory:\n"
399 SSM_CMD_COMMON("setstripe")
401 " <directory|filename>\n"
403 "To add component(s) to an existing composite file:\n"
404 SSM_CMD_COMMON("setstripe --component-add")
406 "To totally delete the default striping from an existing directory:\n"
407 "usage: setstripe [--delete|-d] <directory>\n"
409 "To create a mirrored file or set s default mirror layout on a directory:\n"
410 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
412 "To delete the last component(s) from an existing composite file\n"
413 "(note that this will also delete any data in those components):\n"
414 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
415 " [--component-flags|-F <comp_flags>]\n"
417 "\tcomp_id: Unique component ID to delete\n"
418 "\tcomp_flags: 'init' indicating all instantiated components\n"
419 "\t '^init' indicating all uninstantiated components\n"
420 "\t-I and -F cannot be specified at the same time\n"
421 "To create a file with a foreign (free format) layout:\n"
422 "usage: setstripe --foreign[=<foreign_type>]\n"
423 " --xattr|-x <layout_string> [--flags <hex>]\n"
424 " [--mode <mode>] <filename>\n"},
425 {"getstripe", lfs_getstripe, 0,
426 "To list the layout pattern for a given file or files in a\n"
427 "directory or recursively for all files in a directory tree.\n"
428 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
429 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
430 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
431 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
432 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
433 " [--component-id[=comp_id]|-I[comp_id]]\n"
434 " [--component-flags[=comp_flags]]\n"
435 " [--component-count]\n"
436 " [--extension-size|--ext-size|-z]\n"
437 " [--component-start[=[+-]comp_start]]\n"
438 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
439 " [[!] --mirror-index=[+-]<index> |\n"
440 " [!] --mirror-id=[+-]<id>] [--mirror-count|-N]\n"
441 " <directory|filename> ..."},
442 {"setdirstripe", lfs_setdirstripe, 0,
443 "Create striped directory on specified MDT, same as mkdir.\n"
444 "May be restricted to root or group users, depending on settings.\n"
445 "usage: setdirstripe [OPTION] <directory>\n"
447 {"getdirstripe", lfs_getdirstripe, 0,
448 "To list the layout pattern info for a given directory\n"
449 "or recursively for all directories in a directory tree.\n"
450 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
451 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
452 " [--recursive|-r] [--yaml|-y]\n"
453 " [--verbose|-v] [--default|-D] <dir> ..."},
454 {"mkdir", lfs_setdirstripe, 0,
455 "Create striped directory on specified MDT, same as setdirstripe.\n"
456 "usage: mkdir [OPTION] <directory>\n"
458 {"rm_entry", lfs_rmentry, 0,
459 "To remove the name entry of the remote directory. Note: This\n"
460 "command will only delete the name entry, i.e. the remote directory\n"
461 "will become inaccessable after this command. This can only be done\n"
462 "by the administrator\n"
463 "usage: rm_entry <dir>\n"},
464 {"unlink_foreign", lfs_unlink_foreign, 0,
465 "To remove the foreign file/dir.\n"
466 "Note: This is for files/dirs prevented to be removed using\n"
467 "unlink/rmdir, but works also for regular ones\n"
468 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
469 {"pool_list", lfs_poollist, 0,
470 "List pools or pool OSTs\n"
471 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
472 {"find", lfs_find, 0,
473 "find files matching given attributes recursively in directory tree.\n"
474 "usage: find <directory|filename> ...\n"
475 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
476 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
477 " [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
478 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
479 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
480 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
481 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
482 " [[!] --stripe-count|-c [+-]<stripes>]\n"
483 " [[!] --stripe-index|-i <index,...>]\n"
484 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
485 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
486 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
487 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
488 " [[!] --projid <projid>]\n"
489 " [[!] --foreign[=<foreign_type>]]\n"
490 " [[!] --layout|-L released,raid0,mdt]\n"
491 " [[!] --foreign[=<foreign_type>]]\n"
492 " [[!] --component-count [+-]<comp_cnt>]\n"
493 " [[!] --component-start [+-]N[kMGTPE]]\n"
494 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
495 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
496 " [[!] --mirror-count|-N [+-]<n>]\n"
497 " [[!] --mirror-state <[^]state>]\n"
498 " [[!] --mdt-count|-T [+-]<stripes>]\n"
499 " [[!] --mdt-hash|-H <hashtype>\n"
500 " [[!] --mdt-index|-m <uuid|index,...>]\n"
501 "\t !: used before an option indicates 'NOT' requested attribute\n"
502 "\t -: used before a value indicates less than requested value\n"
503 "\t +: used before a value indicates more than requested value\n"
504 "\thashtype: hash type of the striped directory.\n"
505 "\t fnv_1a_64 FNV-1a hash algorithm\n"
506 "\t all_char sum of characters % MDT_COUNT\n"},
507 {"check", lfs_check, 0,
508 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
509 "or all the servers (MGTs, MDTs and OSTs).\n"
510 "usage: check <mgts|osts|mdts|all>"},
511 {"osts", lfs_osts, 0, "list OSTs connected to client "
512 "[for specified path only]\n" "usage: osts [path]"},
513 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
514 "[for specified path only]\n" "usage: mdts [path]"},
516 "report filesystem disk space usage or inodes usage "
517 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
518 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
519 " [--pool|-p <fsname>[.<pool>]] [path]"},
520 {"getname", lfs_getname, 0,
521 "list instances and specified mount points [for specified path only]\n"
522 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
523 #ifdef HAVE_SYS_QUOTA_H
524 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
525 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
526 " -b <block-softlimit> -B <block-hardlimit>\n"
527 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
528 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
529 " [--block-softlimit <block-softlimit>]\n"
530 " [--block-hardlimit <block-hardlimit>]\n"
531 " [--inode-softlimit <inode-softlimit>]\n"
532 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
533 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
534 " [--block-grace 'notify'|<block-grace>]\n"
535 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
536 " setquota <-U|-G|-P>\n"
537 " -b <block-softlimit> -B <block-hardlimit>\n"
538 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
539 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
540 " [--block-softlimit <block-softlimit>]\n"
541 " [--block-hardlimit <block-hardlimit>]\n"
542 " [--inode-softlimit <inode-softlimit>]\n"
543 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
544 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
546 " -b can be used instead of --block-softlimit/--block-grace\n"
547 " -B can be used instead of --block-hardlimit\n"
548 " -i can be used instead of --inode-softlimit/--inode-grace\n"
549 " -I can be used instead of --inode-hardlimit\n"
550 " -d can be used instead of --default\n\n"
551 "Note: The total quota space will be split into many qunits and\n"
552 " balanced over all server targets, the minimal qunit size is\n"
553 " 1M bytes for block space and 1K inodes for inode space.\n\n"
554 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
555 " Quota space rebalancing process will stop when this mininum\n"
556 " value is reached. As a result, quota exceeded can be returned\n"
557 " while many targets still have 1MB or 1K inodes of spare\n"
559 " When setting the grace time, 'notify' can be used as grace to\n"
560 " be notified after the quota is over soft limit but prevents\n"
561 " the soft limit from becoming the hard limit."},
562 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
563 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
565 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
566 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
567 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
568 {"project", lfs_project, 0,
569 "Change or list project attribute for specified file or directory.\n"
570 "usage: project [-d|-r] <file|directory...>\n"
571 " list project ID and flags on file(s) or directories\n"
572 " project [-p id] [-s] [-r] <file|directory...>\n"
573 " set project ID and/or inherit flag for specified file(s) or directories\n"
574 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
575 " check project ID and flags on file(s) or directories, print outliers\n"
576 " project -C [-r] [-k] <file|directory...>\n"
577 " clear the project inherit flag and ID on the file or directory\n"
580 {"flushctx", lfs_flushctx, 0,
581 "Flush security context for current user.\n"
582 "usage: flushctx [-k] [-r] [mountpoint...]"},
583 {"changelog", lfs_changelog, 0,
584 "Show the metadata changes on an MDT."
585 "\nusage: changelog <mdtname> [startrec [endrec]]"},
586 {"changelog_clear", lfs_changelog_clear, 0,
587 "Indicate that old changelog records up to <endrec> are no longer of "
588 "interest to consumer <id>, allowing the system to free up space.\n"
589 "An <endrec> of 0 means all records.\n"
590 "usage: changelog_clear <mdtname> <id> <endrec>"},
591 {"fid2path", lfs_fid2path, 0,
592 "Resolve the full path(s) for given FID(s). For a specific hardlink "
593 "specify link number <linkno>.\n"
594 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
595 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
596 "usage: path2fid [--parents] <path> ..."},
597 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
598 "usage: rmfid <fsname|rootpath> <fid> ..."},
599 {"data_version", lfs_data_version, 0, "Display file data version for "
600 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
601 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
602 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
603 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
604 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
605 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
606 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
608 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
609 "[--archived] [--lost] <file> ..."},
610 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
611 "given files.\n" "usage: hsm_action <file> ..."},
612 {"hsm_archive", lfs_hsm_archive, 0,
613 "Archive file to external storage.\n"
614 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
616 {"hsm_restore", lfs_hsm_restore, 0,
617 "Restore file from external storage.\n"
618 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
619 {"hsm_release", lfs_hsm_release, 0,
620 "Release files from Lustre.\n"
621 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
622 {"hsm_remove", lfs_hsm_remove, 0,
623 "Remove file copy from external storage.\n"
624 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
626 " (FILE [FILE ...] | "
627 "--mntpath MOUNTPATH FID [FID ...])\n"
629 "Note: To remove an archived copy of a file already deleted from a "
631 "--mntpath option and a list of FIDs must be specified"
633 {"hsm_cancel", lfs_hsm_cancel, 0,
634 "Cancel requests related to specified files.\n"
635 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
636 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
637 "usage: swap_layouts <path1> <path2>"},
638 {"migrate", lfs_setstripe_migrate, 0,
639 "migrate a directory between MDTs.\n"
640 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
641 " [--mdt-hash|-H] <hash_type>\n"
642 " [--mdt-index|-m] <start_mdt_index>\n"
645 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
646 " it's the MDT index of first stripe\n"
647 "\tmdt_count: number of MDTs to stripe a directory over\n"
648 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
649 " all_char (type 1)sum of characters % MDT_COUNT\n"
650 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
651 " crush (type 3)CRUSH hash algorithm\n"
653 "migrate file objects from one OST "
654 "layout\nto another (may be not safe with concurent writes).\n"
656 "[--stripe-count|-c] <stripe_count>\n"
657 "[--overstripe-count|-C] <stripe_count>\n"
658 " [--stripe-index|-i] <start_ost_index>\n"
659 " [--stripe-size|-S] <stripe_size>\n"
660 " [--pool|-p] <pool_name>\n"
661 " [--ost|-o] <ost_indices>\n"
663 " [--non-block|-n]\n"
664 " [--non-direct|-D]\n"
665 " <file|directory>\n"
666 "\tstripe_count: number of OSTs to stripe a file over\n"
667 "\t Using -C instead of -c allows overstriping, which\n"
668 "\t will place more than one stripe per OST if\n"
669 "\t stripe_count is greater than the number of OSTs\n"
670 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
671 "\tstripe_size: number of bytes to store before moving to the next OST\n"
672 "\tpool_name: name of the predefined pool of OSTs\n"
673 "\tost_indices: OSTs to stripe over, in order\n"
674 "\tblock: Block file access during data migration (default)\n"
675 "\tnon-block: Abort migrations if concurrent access is detected\n"
676 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
678 "To move directories between MDTs. This command is deprecated, "
679 "use \"migrate\" instead.\n"
680 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
682 {"ladvise", lfs_ladvise, 0,
683 "Provide servers with advice about access patterns for a file.\n"
684 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
685 " [--background|-b] [--unset|-u]\n\n"
686 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
687 " {[--mode|-m [READ,WRITE]}\n"
689 {"mirror", lfs_mirror, mirror_cmdlist,
690 "lfs commands used to manage files with mirrored components:\n"
691 "lfs mirror create - create a mirrored file or directory\n"
692 "lfs mirror extend - add mirror(s) to an existing file\n"
693 "lfs mirror split - split a mirror from an existing mirrored file\n"
694 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
695 "lfs mirror read - read a mirror content of a mirrored file\n"
696 "lfs mirror write - write to a mirror of a mirrored file\n"
697 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
698 "lfs mirror verify - verify mirrored file(s)\n"},
699 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
700 "usage: getsom [-s] [-b] [-f] <path>\n"
701 "\t-s: Only show the size value of the SOM data for a given file\n"
702 "\t-b: Only show the blocks value of the SOM data for a given file\n"
703 "\t-f: Only show the flags value of the SOM data for a given file\n"},
704 {"heat_get", lfs_heat_get, 0,
705 "To get heat of files.\n"
706 "usage: heat_get <file> ...\n"},
707 {"heat_set", lfs_heat_set, 0,
708 "To set heat flags of files.\n"
709 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
710 "\t--clear|-c: Clear file heat for given files\n"
711 "\t--off|-o: Turn off file heat for given files\n"
712 "\t--on|-O: Turn on file heat for given files\n"},
713 {"pcc", lfs_pcc, pcc_cmdlist,
714 "lfs commands used to interact with PCC features:\n"
715 "lfs pcc attach - attach given files to Persistent Client Cache\n"
716 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
717 "lfs pcc state - display the PCC state for given files\n"
718 "lfs pcc detach - detach given files from Persistent Client Cache\n"
719 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
720 {"help", Parser_help, 0, "help"},
721 {"exit", Parser_quit, 0, "quit"},
722 {"quit", Parser_quit, 0, "quit"},
723 {"--version", Parser_version, 0,
724 "output build version of the utility and exit"},
725 {"--list-commands", lfs_list_commands, 0,
726 "list commands supported by the utility and exit"},
730 static int check_hashtype(const char *hashtype)
732 int type_num = atoi(hashtype);
735 /* numeric hash type */
736 if (hashtype && strlen(hashtype) == 1 &&
737 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
739 /* string hash type */
740 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
741 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
747 static uint32_t check_foreign_type_name(const char *foreign_type_name)
751 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
752 if (!lu_foreign_types[i].lft_name)
754 if (strcmp(foreign_type_name,
755 lu_foreign_types[i].lft_name) == 0)
756 return lu_foreign_types[i].lft_type;
759 return LU_FOREIGN_TYPE_UNKNOWN;
762 static const char *error_loc = "syserror";
765 MIGRATION_NONBLOCK = 0x0001,
766 MIGRATION_MIRROR = 0x0002,
767 MIGRATION_NONDIRECT = 0x0004,
768 MIGRATION_VERBOSE = 0x0008,
772 migrate_open_files(const char *name, __u64 migration_flags,
773 const struct llapi_stripe_param *param,
774 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
781 char parent[PATH_MAX];
782 char volatile_file[PATH_MAX];
788 if (!param && !layout) {
789 error_loc = "layout information";
793 /* search for file directory pathname */
794 if (strlen(name) > sizeof(parent) - 1) {
795 error_loc = "source file name";
799 strncpy(parent, name, sizeof(parent));
800 ptr = strrchr(parent, '/');
802 if (!getcwd(parent, sizeof(parent))) {
803 error_loc = "getcwd";
807 if (ptr == parent) /* leading '/' */
812 /* open file, direct io */
813 /* even if the file is only read, WR mode is nedeed to allow
817 if (!(migration_flags & MIGRATION_NONDIRECT))
819 fd = open(name, rflags);
822 error_loc = "cannot open source file";
826 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
828 error_loc = "cannot get MDT index";
833 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
834 mode_t open_mode = S_IRUSR | S_IWUSR;
836 random_value = random();
837 rc = snprintf(volatile_file, sizeof(volatile_file),
838 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
839 mdt_index, random_value);
840 if (rc >= sizeof(volatile_file)) {
845 /* create, open a volatile file, use caching (ie no directio) */
847 /* Returns -1 and sets errno on error: */
848 fdv = llapi_layout_file_open(volatile_file, open_flags,
853 /* Does the right thing on error: */
854 fdv = llapi_file_open_param(volatile_file, open_flags,
857 } while (fdv < 0 && (rc = fdv) == -EEXIST);
860 error_loc = "cannot create volatile file";
865 * In case the MDT does not support creation of volatile files
866 * we should try to unlink it.
868 (void)unlink(volatile_file);
871 * Not-owner (root?) special case.
872 * Need to set owner/group of volatile file like original.
873 * This will allow to pass related check during layout_swap.
878 error_loc = "cannot stat source file";
882 rc = fstat(fdv, &stv);
885 error_loc = "cannot stat volatile";
889 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
890 rc = fchown(fdv, st.st_uid, st.st_gid);
893 error_loc = "cannot change ownwership of volatile";
912 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
914 struct llapi_layout *layout;
915 size_t buf_size = 4 * 1024 * 1024;
924 layout = llapi_layout_get_by_fd(fd_src, 0);
926 uint64_t stripe_size;
928 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
930 buf_size = stripe_size;
932 llapi_layout_free(layout);
935 /* Use a page-aligned buffer for direct I/O */
936 rc = posix_memalign(&buf, getpagesize(), buf_size);
942 * read new data only if we have written all
943 * previously read data
947 rc = check_file(fd_src);
952 rsize = read(fd_src, buf, buf_size);
966 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
979 /* Try to avoid page cache pollution after migration. */
980 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
981 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
987 static int migrate_copy_timestamps(int fd, int fdv)
991 if (fstat(fd, &st) == 0) {
992 struct timeval tv[2] = {
993 {.tv_sec = st.st_atime},
994 {.tv_sec = st.st_mtime}
997 return futimes(fdv, tv);
1003 static int migrate_block(int fd, int fdv)
1010 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1012 error_loc = "cannot get dataversion";
1021 * The grouplock blocks all concurrent accesses to the file.
1022 * It has to be taken after llapi_get_data_version as it would
1025 rc = llapi_group_lock(fd, gid);
1027 error_loc = "cannot get group lock";
1031 rc = migrate_copy_data(fd, fdv, NULL);
1033 error_loc = "data copy failed";
1037 /* Make sure we keep original atime/mtime values */
1038 rc = migrate_copy_timestamps(fd, fdv);
1040 error_loc = "timestamp copy failed";
1046 * for a migration we need to check data version on file did
1049 * Pass in gid=0 since we already own grouplock.
1051 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1052 SWAP_LAYOUTS_CHECK_DV1);
1053 if (rc == -EAGAIN) {
1054 error_loc = "file changed";
1056 } else if (rc < 0) {
1057 error_loc = "cannot swap layout";
1062 rc2 = llapi_group_unlock(fd, gid);
1063 if (rc2 < 0 && rc == 0) {
1064 error_loc = "unlock group lock";
1072 * Internal helper for migrate_copy_data(). Check lease and report error if
1075 * \param[in] fd File descriptor on which to check the lease.
1077 * \retval 0 Migration can keep on going.
1078 * \retval -errno Error occurred, abort migration.
1080 static int check_lease(int fd)
1084 rc = llapi_lease_check(fd);
1086 return 0; /* llapi_check_lease returns > 0 on success. */
1091 static int migrate_nonblock(int fd, int fdv)
1097 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1099 error_loc = "cannot get data version";
1103 rc = migrate_copy_data(fd, fdv, check_lease);
1105 error_loc = "data copy failed";
1109 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1111 error_loc = "cannot get data version";
1117 error_loc = "source file changed";
1121 /* Make sure we keep original atime/mtime values */
1122 rc = migrate_copy_timestamps(fd, fdv);
1124 error_loc = "timestamp copy failed";
1131 static int lfs_component_set(char *fname, int comp_id,
1132 __u32 flags, __u32 neg_flags)
1135 __u32 flags_array[2];
1140 ids[count] = comp_id;
1141 flags_array[count] = flags;
1146 if (neg_flags & LCME_FL_STALE) {
1148 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1153 ids[count] = comp_id;
1154 flags_array[count] = neg_flags | LCME_FL_NEG;
1158 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1160 if (errno == EUCLEAN) {
1163 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1164 progname, comp_id, fname);
1167 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1168 progname, comp_id, fname, flags, neg_flags);
1175 static int lfs_component_del(char *fname, __u32 comp_id,
1176 __u32 flags, __u32 neg_flags)
1180 if (flags && neg_flags)
1183 if (!flags && neg_flags)
1184 flags = neg_flags | LCME_FL_NEG;
1186 if ((flags && comp_id) || (!flags && !comp_id))
1190 if (flags & ~LCME_KNOWN_FLAGS) {
1192 "%s setstripe: unknown flags %#x\n",
1196 } else if (comp_id > LCME_ID_MAX) {
1197 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1202 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1205 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1206 progname, comp_id, fname, strerror(errno));
1210 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1217 rc = llapi_layout_file_comp_add(fname, layout);
1219 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1220 fname, strerror(errno));
1224 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1225 struct llapi_layout *layout)
1233 fd = lstat(fname, &st);
1234 if (fd == 0 && S_ISDIR(st.st_mode))
1235 open_flags = O_DIRECTORY | O_RDONLY;
1237 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1239 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1240 S_ISDIR(st.st_mode) ?
1241 "set default composite layout for" :
1242 "create composite file",
1243 fname, strerror(errno));
1247 static int lfs_migrate(char *name, __u64 migration_flags,
1248 struct llapi_stripe_param *param,
1249 struct llapi_layout *layout)
1251 struct llapi_layout *existing;
1252 uint64_t dom_new, dom_cur;
1257 rc = migrate_open_files(name, migration_flags, param, layout,
1262 rc = llapi_layout_dom_size(layout, &dom_new);
1264 error_loc = "cannot get new layout DoM size";
1267 /* special case for migration to DOM layout*/
1268 existing = llapi_layout_get_by_fd(fd, 0);
1270 error_loc = "cannot get existing layout";
1274 rc = llapi_layout_dom_size(existing, &dom_cur);
1276 error_loc = "cannot get current layout DoM size";
1281 * if file has DoM layout already then migration is possible to
1282 * the new layout with the same DoM component via swap layout,
1283 * if new layout used bigger DOM size, then mirroring is used
1285 if (dom_new > dom_cur) {
1286 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1289 error_loc = "cannot migrate to DOM layout";
1293 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1295 * Blocking mode (forced if servers do not support file lease).
1296 * It is also the default mode, since we cannot distinguish
1297 * between a broken lease and a server that does not support
1298 * atomic swap/close (LU-6785)
1300 rc = migrate_block(fd, fdv);
1304 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1306 error_loc = "cannot get lease";
1310 rc = migrate_nonblock(fd, fdv);
1312 llapi_lease_release(fd);
1317 * Atomically put lease, swap layouts and close.
1318 * for a migration we need to check data version on file did
1321 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1323 error_loc = "cannot swap layout";
1335 fprintf(stderr, "error: %s: %s: %s: %s\n",
1336 progname, name, error_loc, strerror(-rc));
1337 else if (migration_flags & MIGRATION_VERBOSE)
1338 printf("%s\n", name);
1343 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1352 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1356 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1357 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1358 const char *comp_name = comp_flags_table[i].cfn_name;
1360 if (strcmp(name, comp_name) == 0) {
1361 *flags |= comp_flag;
1363 } else if (strncmp(name, "^", 1) == 0 &&
1364 strcmp(name + 1, comp_name) == 0) {
1365 *neg_flags |= comp_flag;
1370 llapi_printf(LLAPI_MSG_ERROR,
1371 "%s: component flag '%s' not supported\n",
1377 if (!*flags && !*neg_flags)
1380 /* don't allow to set and exclude the same flag */
1381 if (*flags & *neg_flags)
1387 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1395 if (strncmp(string, "^", 1) == 0) {
1396 *neg_state = llapi_layout_string_flags(string + 1);
1397 if (*neg_state != 0)
1400 *state = llapi_layout_string_flags(string);
1405 llapi_printf(LLAPI_MSG_ERROR,
1406 "%s: mirrored file state '%s' not supported\n",
1412 * struct mirror_args - Command-line arguments for mirror(s).
1413 * @m_count: Number of mirrors to be created with this layout.
1414 * @m_flags: Mirror level flags, only 'prefer' is supported.
1415 * @m_layout: Mirror layout.
1416 * @m_file: A victim file. Its layout will be split and used as a mirror.
1417 * @m_next: Point to the next node of the list.
1419 * Command-line arguments for mirror(s) will be parsed and stored in
1420 * a linked list that consists of this structure.
1422 struct mirror_args {
1425 struct llapi_layout *m_layout;
1427 struct mirror_args *m_next;
1432 * enum mirror_flags - Flags for extending a mirrored file.
1433 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1434 * in case the victim file(s) contains the same data as the
1435 * original mirrored file.
1436 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1437 * @MF_COMP_ID: specified component id instead of mirror id
1439 * Flags for extending a mirrored file.
1449 * mirror_create_sanity_check() - Check mirror list.
1450 * @list: A linked list that stores the mirror arguments.
1452 * This function does a sanity check on @list for creating
1455 * Return: 0 on success or a negative error code on failure.
1457 static int mirror_create_sanity_check(const char *fname,
1458 struct mirror_args *list)
1461 bool has_m_file = false;
1462 bool has_m_layout = false;
1468 struct llapi_layout *layout;
1470 layout = llapi_layout_get_by_path(fname, 0);
1473 "error: %s: file '%s' couldn't get layout\n",
1478 rc = llapi_layout_sanity(layout, false, true);
1480 llapi_layout_free(layout);
1483 llapi_layout_sanity_perror(rc);
1491 llapi_layout_free(list->m_layout);
1494 llapi_layout_get_by_path(list->m_file, 0);
1495 if (!list->m_layout) {
1497 "error: %s: file '%s' has no layout\n",
1498 progname, list->m_file);
1502 has_m_layout = true;
1503 if (!list->m_layout) {
1504 fprintf(stderr, "error: %s: no mirror layout\n",
1510 rc = llapi_layout_sanity(list->m_layout, false, true);
1512 llapi_layout_sanity_perror(rc);
1516 list = list->m_next;
1519 if (has_m_file && has_m_layout) {
1521 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1529 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1531 __u32 mirror_flags = *(__u32 *)cbdata;
1535 rc = llapi_layout_comp_flags_get(layout, &flags);
1540 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1545 return LLAPI_LAYOUT_ITER_CONT;
1549 * mirror_create() - Create a mirrored file.
1550 * @fname: The file to be created.
1551 * @mirror_list: A linked list that stores the mirror arguments.
1553 * This function creates a mirrored file @fname with the mirror(s)
1554 * from @mirror_list.
1556 * Return: 0 on success or a negative error code on failure.
1558 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1560 struct llapi_layout *layout = NULL;
1561 struct mirror_args *cur_mirror = NULL;
1562 uint16_t mirror_count = 0;
1566 rc = mirror_create_sanity_check(NULL, mirror_list);
1570 cur_mirror = mirror_list;
1571 while (cur_mirror) {
1572 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1574 &cur_mirror->m_flags);
1577 fprintf(stderr, "%s: failed to set mirror flags\n",
1582 for (i = 0; i < cur_mirror->m_count; i++) {
1583 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1587 "error: %s: merge layout failed: %s\n",
1588 progname, strerror(errno));
1592 mirror_count += cur_mirror->m_count;
1593 cur_mirror = cur_mirror->m_next;
1597 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1601 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1604 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1605 progname, strerror(errno));
1609 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1617 llapi_layout_free(layout);
1622 * Compare files and check lease on @fd.
1624 * \retval bytes number of bytes are the same
1626 static ssize_t mirror_file_compare(int fd, int fdv)
1628 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1630 ssize_t bytes_done = 0;
1631 ssize_t bytes_read = 0;
1633 buf = malloc(buflen * 2);
1638 if (!llapi_lease_check(fd)) {
1639 bytes_done = -EBUSY;
1643 bytes_read = read(fd, buf, buflen);
1644 if (bytes_read <= 0)
1647 if (bytes_read != read(fdv, buf + buflen, buflen))
1651 * XXX: should compute the checksum on each buffer and then
1652 * compare checksum to avoid cache collision
1654 if (memcmp(buf, buf + buflen, bytes_read))
1657 bytes_done += bytes_read;
1665 static int mirror_extend_file(const char *fname, const char *victim_file,
1666 enum mirror_flags mirror_flags)
1671 struct stat stbuf_v;
1672 struct ll_ioc_lease *data = NULL;
1675 fd = open(fname, O_RDWR);
1677 error_loc = "open source file";
1682 fdv = open(victim_file, O_RDWR);
1684 error_loc = "open target file";
1689 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1690 error_loc = "stat source or target file";
1695 if (stbuf.st_dev != stbuf_v.st_dev) {
1696 error_loc = "stat source and target file";
1701 /* mirrors should be of the same size */
1702 if (stbuf.st_size != stbuf_v.st_size) {
1703 error_loc = "file sizes don't match";
1708 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1710 error_loc = "cannot get lease";
1714 if (!(mirror_flags & MF_NO_VERIFY)) {
1716 /* mirrors should have the same contents */
1717 ret = mirror_file_compare(fd, fdv);
1718 if (ret != stbuf.st_size) {
1719 error_loc = "file busy or contents don't match";
1720 rc = ret < 0 ? ret : -EINVAL;
1725 /* Get rid of caching pages from clients */
1726 rc = llapi_file_flush(fd);
1728 error_loc = "cannot get data version";
1732 rc = llapi_file_flush(fdv);
1734 error_loc = "cannot get data version";
1738 /* Make sure we keep original atime/mtime values */
1739 rc = migrate_copy_timestamps(fd, fdv);
1741 error_loc = "cannot copy timestamp";
1745 /* Atomically put lease, merge layouts and close. */
1746 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1748 error_loc = "memory allocation";
1751 data->lil_mode = LL_LEASE_UNLCK;
1752 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1753 data->lil_count = 1;
1754 data->lil_ids[0] = fdv;
1755 rc = llapi_lease_set(fd, data);
1757 error_loc = "cannot merge layout";
1759 } else if (rc == 0) {
1761 error_loc = "lost lease lock";
1774 (void) unlink(victim_file);
1776 fprintf(stderr, "error: %s: %s: %s: %s\n",
1777 progname, fname, error_loc, strerror(-rc));
1781 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1782 bool inherit, uint32_t flags)
1784 struct llapi_layout *f_layout = NULL;
1785 struct ll_ioc_lease *data = NULL;
1791 f_layout = llapi_layout_get_by_path(name, 0);
1793 fprintf(stderr, "%s: cannot get layout\n", progname);
1796 rc = llapi_layout_get_last_init_comp(f_layout);
1798 fprintf(stderr, "%s: cannot get the last init comp\n",
1802 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1805 "%s: cannot inherit from the last init comp\n",
1810 llapi_layout_comp_flags_set(m_layout, flags);
1811 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1816 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1818 error_loc = "cannot get lease";
1822 rc = migrate_nonblock(fd, fdv);
1824 llapi_lease_release(fd);
1828 /* Atomically put lease, merge layouts and close. */
1829 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1831 error_loc = "memory allocation";
1834 data->lil_mode = LL_LEASE_UNLCK;
1835 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1836 data->lil_count = 1;
1837 data->lil_ids[0] = fdv;
1838 rc = llapi_lease_set(fd, data);
1840 error_loc = "cannot merge layout";
1842 } else if (rc == 0) {
1844 error_loc = "lost lease lock";
1857 fprintf(stderr, "error: %s: %s: %s: %s\n",
1858 progname, name, error_loc, strerror(-rc));
1862 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1863 enum mirror_flags mirror_flags)
1867 rc = mirror_create_sanity_check(fname, mirror_list);
1871 while (mirror_list) {
1872 if (mirror_list->m_file) {
1873 rc = mirror_extend_file(fname, mirror_list->m_file,
1876 __u32 mirror_count = mirror_list->m_count;
1878 while (mirror_count > 0) {
1879 rc = mirror_extend_layout(fname,
1880 mirror_list->m_layout,
1881 mirror_list->m_inherit,
1882 mirror_list->m_flags);
1892 mirror_list = mirror_list->m_next;
1898 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1903 rc = llapi_layout_mirror_id_get(layout, &id);
1907 if ((__u16)id == *(__u16 *)cbdata)
1908 return LLAPI_LAYOUT_ITER_STOP;
1910 return LLAPI_LAYOUT_ITER_CONT;
1913 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1918 rc = llapi_layout_comp_id_get(layout, &id);
1922 if (id == *(__u32 *)cbdata)
1923 return LLAPI_LAYOUT_ITER_STOP;
1925 return LLAPI_LAYOUT_ITER_CONT;
1928 struct pool_to_id_cbdata {
1933 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1935 char buf[LOV_MAXPOOLNAME + 1];
1936 struct pool_to_id_cbdata *d = (void *)cbdata;
1940 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1943 if (strcmp(d->pool, buf))
1944 return LLAPI_LAYOUT_ITER_CONT;
1946 rc = llapi_layout_mirror_id_get(layout, &id);
1951 return LLAPI_LAYOUT_ITER_STOP;
1954 struct collect_ids_data {
1960 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1962 struct collect_ids_data *cid = cbdata;
1966 rc = llapi_layout_mirror_id_get(layout, &id);
1970 if ((__u16)id != cid->cid_exclude) {
1973 for (i = 0; i < cid->cid_count; i++) {
1974 /* already collected the mirror id */
1975 if (id == cid->cid_ids[i])
1976 return LLAPI_LAYOUT_ITER_CONT;
1978 cid->cid_ids[cid->cid_count] = id;
1982 return LLAPI_LAYOUT_ITER_CONT;
1986 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1987 * @mirror_id: Mirror id to be checked.
1988 * @layout: Mirror component list.
1990 * This function checks if a mirror with specified @mirror_id is the last
1991 * non-stale mirror of a layout @layout.
1993 * Return: true or false.
1996 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1998 __u16 mirror_ids[128] = { 0 };
1999 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2001 .cid_exclude = mirror_id, };
2004 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2006 for (i = 0; i < cid.cid_count; i++) {
2007 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2010 comp_size = llapi_mirror_find_stale(layout, comp_array,
2011 ARRAY_SIZE(comp_array),
2020 static int mirror_split(const char *fname, __u32 id, const char *pool,
2021 enum mirror_flags mflags, const char *victim_file)
2023 struct llapi_layout *layout;
2024 char parent[PATH_MAX];
2025 char victim[PATH_MAX];
2026 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2028 struct ll_ioc_lease *data;
2029 uint16_t mirror_count;
2035 /* check fname contains mirror with mirror_id/comp_id */
2036 layout = llapi_layout_get_by_path(fname, 0);
2039 "error %s: file '%s' couldn't get layout\n",
2044 rc = llapi_layout_sanity(layout, false, true);
2046 llapi_layout_sanity_perror(rc);
2050 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2053 "error %s: file '%s' couldn't get mirror count\n",
2057 if (mirror_count < 2) {
2059 "error %s: file '%s' has %d component, cannot split\n",
2060 progname, fname, mirror_count);
2064 if (mflags & MF_COMP_POOL) {
2065 struct pool_to_id_cbdata data = { .pool = pool };
2067 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
2069 mirror_id = data.id;
2070 } else if (mflags & MF_COMP_ID) {
2071 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2072 mirror_id = mirror_id_of(id);
2074 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2078 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2081 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2082 if (mflags & MF_COMP_POOL) {
2084 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2085 progname, fname, pool);
2087 } else if (mflags & MF_COMP_ID) {
2089 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2090 progname, fname, id);
2094 "error %s: file '%s' does not contain mirror with id %u\n",
2095 progname, fname, id);
2100 fd = open(fname, O_RDWR);
2103 "error %s: open file '%s' failed: %s\n",
2104 progname, fname, strerror(errno));
2108 /* get victim file directory pathname */
2109 if (strlen(fname) > sizeof(parent) - 1) {
2110 fprintf(stderr, "error %s: file name of '%s' too long\n",
2115 strncpy(parent, fname, sizeof(parent));
2116 ptr = strrchr(parent, '/');
2118 if (!getcwd(parent, sizeof(parent))) {
2119 fprintf(stderr, "error %s: getcwd failed: %s\n",
2120 progname, strerror(errno));
2130 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2132 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2138 /* use a temp file to store the splitted layout */
2139 if (mflags & MF_DESTROY) {
2140 if (last_non_stale_mirror(mirror_id, layout)) {
2143 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2148 fdv = llapi_create_volatile_idx(parent, mdt_index,
2149 O_LOV_DELAY_CREATE);
2151 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2153 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2156 /* user specified victim file */
2157 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2162 "error %s: create victim file failed: %s\n",
2163 progname, strerror(errno));
2167 /* get lease lock of fname */
2168 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2171 "error %s: cannot get lease of file '%s': %d\n",
2172 progname, fname, rc);
2176 /* Atomatically put lease, split layouts and close. */
2177 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2183 data->lil_mode = LL_LEASE_UNLCK;
2184 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2185 data->lil_count = 2;
2186 data->lil_ids[0] = fdv;
2187 data->lil_ids[1] = mirror_id;
2188 rc = llapi_lease_set(fd, data);
2190 if (rc == 0) /* lost lease lock */
2193 "error %s: cannot split '%s': %s\n",
2194 progname, fname, strerror(-rc));
2205 llapi_layout_free(layout);
2210 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2211 __u16 *mirror_ids, int ids_nr);
2213 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2214 __u64 migration_flags,
2215 struct llapi_stripe_param *param,
2216 struct llapi_layout *layout)
2218 struct ll_ioc_lease *data = NULL;
2221 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2223 error_loc = "cannot get lease";
2227 /* Atomically put lease, merge layouts, resync and close. */
2228 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2230 error_loc = "memory allocation";
2233 data->lil_mode = LL_LEASE_UNLCK;
2234 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2235 data->lil_count = 1;
2236 data->lil_ids[0] = fdv;
2237 rc = llapi_lease_set(fd, data);
2239 error_loc = "cannot merge layout";
2241 } else if (rc == 0) {
2243 error_loc = "lost lease lock";
2249 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2251 error_loc = "cannot resync file";
2255 /* delete first mirror now */
2256 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2258 error_loc = "cannot delete old layout";
2266 fprintf(stderr, "error: %s: %s: %s: %s\n",
2267 progname, name, error_loc, strerror(-rc));
2268 else if (migration_flags & MIGRATION_VERBOSE)
2269 printf("%s\n", name);
2276 * Parse a string containing an target index list into an array of integers.
2278 * The input string contains a comma delimited list of individual
2279 * indices and ranges, for example "1,2-4,7". Add the indices into the
2280 * \a tgts array and remove duplicates.
2282 * \param[out] tgts array to store indices in
2283 * \param[in] size size of \a tgts array
2284 * \param[in] offset starting index in \a tgts
2285 * \param[in] arg string containing OST index list
2286 * \param[in/out] overstriping index list may contain duplicates
2288 * \retval positive number of indices in \a tgts
2289 * \retval -EINVAL unable to parse \a arg
2291 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2292 unsigned long long *pattern)
2296 int slots = size - offset;
2298 bool overstriped = false;
2304 end_of_loop = false;
2305 while (!end_of_loop) {
2306 int start_index = 0;
2309 char *endptr = NULL;
2313 ptr = strchrnul(arg, ',');
2315 end_of_loop = *ptr == '\0';
2318 start_index = strtol(arg, &endptr, 0);
2319 if (endptr == arg) /* no data at all */
2321 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2324 end_index = start_index;
2325 if (*endptr == '-') {
2326 end_index = strtol(endptr + 1, &endptr, 0);
2327 if (*endptr != '\0')
2329 if (end_index < start_index)
2333 for (i = start_index; i <= end_index && slots > 0; i++) {
2336 /* remove duplicate */
2337 for (j = 0; j < offset; j++) {
2338 if (tgts[j] == i && pattern &&
2339 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2341 else if (tgts[j] == i)
2347 if (j == offset) { /* check complete */
2353 if (slots == 0 && i < end_index)
2361 if (!end_of_loop && ptr)
2364 if (!overstriped && pattern)
2365 *pattern = LLAPI_LAYOUT_DEFAULT;
2367 return rc < 0 ? rc : nr;
2370 struct lfs_setstripe_args {
2371 unsigned long long lsa_comp_end;
2372 unsigned long long lsa_stripe_size;
2373 unsigned long long lsa_extension_size;
2374 long long lsa_stripe_count;
2375 long long lsa_stripe_off;
2376 __u32 lsa_comp_flags;
2377 __u32 lsa_comp_neg_flags;
2378 unsigned long long lsa_pattern;
2379 unsigned int lsa_mirror_count;
2381 bool lsa_first_comp;
2382 bool lsa_extension_comp;
2384 char *lsa_pool_name;
2387 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2389 unsigned int mirror_count = lsa->lsa_mirror_count;
2390 bool first_comp = lsa->lsa_first_comp;
2392 memset(lsa, 0, sizeof(*lsa));
2394 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2395 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2396 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2397 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2398 lsa->lsa_pool_name = NULL;
2400 lsa->lsa_mirror_count = mirror_count;
2401 lsa->lsa_first_comp = first_comp;
2405 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2406 * @lsa: Stripe options to be initialized and inherited.
2408 * This function initializes stripe options in @lsa and inherit
2409 * stripe_size, stripe_count and OST pool_name options.
2413 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2415 unsigned long long stripe_size;
2416 long long stripe_count;
2417 char *pool_name = NULL;
2419 stripe_size = lsa->lsa_stripe_size;
2420 stripe_count = lsa->lsa_stripe_count;
2421 pool_name = lsa->lsa_pool_name;
2423 setstripe_args_init(lsa);
2425 lsa->lsa_stripe_size = stripe_size;
2426 lsa->lsa_stripe_count = stripe_count;
2427 lsa->lsa_pool_name = pool_name;
2430 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2432 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2433 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2434 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2435 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2436 lsa->lsa_comp_end != 0);
2440 * comp_args_to_layout() - Create or extend a composite layout.
2441 * @composite: Pointer to the composite layout.
2442 * @lsa: Stripe options for the new component.
2444 * This function creates or extends a composite layout by adding a new
2445 * component with stripe options from @lsa.
2447 * Return: 0 on success or an error code on failure.
2449 static int comp_args_to_layout(struct llapi_layout **composite,
2450 struct lfs_setstripe_args *lsa,
2453 struct llapi_layout *layout = *composite;
2454 uint64_t prev_end = 0;
2460 layout = llapi_layout_alloc();
2462 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2467 *composite = layout;
2468 lsa->lsa_first_comp = true;
2473 * Get current component extent, current component
2474 * must be the tail component.
2476 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2478 fprintf(stderr, "Get comp extent failed. %s\n",
2483 if (lsa->lsa_first_comp) {
2485 rc = llapi_layout_add_first_comp(layout);
2487 rc = llapi_layout_comp_add(layout);
2490 fprintf(stderr, "Add component failed. %s\n",
2496 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2498 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2499 lsa->lsa_comp_flags, strerror(errno));
2504 uint64_t comp_end = lsa->lsa_comp_end;
2507 * The extendable component is 0-length, so it can be removed
2508 * if there is insufficient space to extend it.
2510 if (lsa->lsa_extension_comp)
2511 comp_end = prev_end;
2513 rc = llapi_layout_comp_extent_set(layout, prev_end,
2516 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2517 prev_end, comp_end, strerror(errno));
2521 /* reset lsa_first_comp */
2522 lsa->lsa_first_comp = false;
2524 /* Data-on-MDT component setting */
2525 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2527 * In case of Data-on-MDT patterns the only extra option
2528 * applicable is stripe size option.
2530 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2532 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2533 lsa->lsa_stripe_count);
2537 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2539 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2540 lsa->lsa_stripe_size);
2544 if (lsa->lsa_nr_tgts != 0) {
2546 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2551 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2553 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2554 lsa->lsa_stripe_off);
2558 if (lsa->lsa_pool_name != 0) {
2560 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2561 lsa->lsa_pool_name);
2566 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2568 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2573 /* Data-on-MDT component has always single stripe up to end */
2574 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2575 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2576 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2578 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2585 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2586 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2588 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2589 rc = llapi_layout_extension_size_set(layout, size);
2591 rc = llapi_layout_stripe_size_set(layout, size);
2594 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2595 size, strerror(errno));
2599 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2601 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2602 lsa->lsa_stripe_count, strerror(errno));
2606 if (lsa->lsa_pool_name) {
2607 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2609 fprintf(stderr, "Set pool name: %s failed. %s\n",
2610 lsa->lsa_pool_name, strerror(errno));
2614 rc = llapi_layout_pool_name_set(layout, "");
2616 fprintf(stderr, "Clear pool name failed: %s\n",
2622 if (lsa->lsa_nr_tgts > 0) {
2623 if (lsa->lsa_stripe_count > 0 &&
2624 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2625 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2626 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2627 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2628 lsa->lsa_stripe_count,
2633 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2634 rc = llapi_layout_ost_index_set(layout, i,
2639 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2640 lsa->lsa_stripe_off != -1) {
2641 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2644 fprintf(stderr, "Set ost index %d failed. %s\n",
2645 i, strerror(errno));
2649 /* Create the second, virtual component of extension space */
2650 if (lsa->lsa_extension_comp) {
2651 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2652 lsa->lsa_extension_comp = false;
2659 static int build_component(struct llapi_layout **layout,
2660 struct lfs_setstripe_args *lsa, bool set_extent)
2664 rc = comp_args_to_layout(layout, lsa, set_extent);
2668 if (lsa->lsa_mirror_count > 0) {
2669 rc = llapi_layout_mirror_count_set(*layout,
2670 lsa->lsa_mirror_count);
2674 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2677 lsa->lsa_mirror_count = 0;
2683 static int build_prev_component(struct llapi_layout **layout,
2684 struct lfs_setstripe_args *prev,
2685 struct lfs_setstripe_args *lsa,
2688 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2691 if (prev->lsa_stripe_size) {
2693 prev->lsa_comp_end = lsa->lsa_comp_end;
2694 prev->lsa_extension_size = lsa->lsa_extension_size;
2695 prev->lsa_extension_comp = true;
2698 rc = build_component(layout, prev, true);
2704 * Copy lsa to previous lsa;
2705 * if this is an extension component, make the previous invalid;
2708 prev->lsa_stripe_size = 0;
2715 static int build_layout_from_yaml_node(struct cYAML *node,
2716 struct llapi_layout **layout,
2717 struct lfs_setstripe_args *lsa,
2718 struct lfs_setstripe_args *prevp)
2720 struct lfs_setstripe_args prev = { 0 };
2721 __u32 *osts = lsa->lsa_tgts;
2729 string = node->cy_string;
2731 if (node->cy_type == CYAML_TYPE_OBJECT) {
2732 /* go deep to sub blocks */
2733 if (string && !strncmp(string, "component", 9) &&
2734 strncmp(string, "component0", 10) &&
2735 strncmp(string, "components", 10)) {
2736 rc = build_prev_component(layout, prevp, lsa,
2741 /* initialize lsa. */
2742 setstripe_args_init(lsa);
2743 lsa->lsa_first_comp = false;
2744 lsa->lsa_tgts = osts;
2747 rc = build_layout_from_yaml_node(node->cy_child, layout,
2752 if (!node->cy_string)
2755 /* skip leading lmm_ if present, to simplify parsing */
2756 if (strncmp(string, "lmm_", 4) == 0)
2759 if (node->cy_type == CYAML_TYPE_STRING) {
2760 if (!strcmp(string, "lcme_extent.e_end")) {
2761 if (!strcmp(node->cy_valuestring, "EOF") ||
2762 !strcmp(node->cy_valuestring, "eof"))
2763 lsa->lsa_comp_end = LUSTRE_EOF;
2764 } else if (!strcmp(string, "pool")) {
2765 lsa->lsa_pool_name = node->cy_valuestring;
2766 } else if (!strcmp(string, "pattern")) {
2767 if (!strcmp(node->cy_valuestring, "mdt"))
2768 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2769 if (!strcmp(node->cy_valuestring,
2770 "raid0,overstriped"))
2772 LLAPI_LAYOUT_OVERSTRIPING;
2773 } else if (!strcmp(string, "lcme_flags")) {
2774 rc = comp_str2flags(node->cy_valuestring,
2775 &lsa->lsa_comp_flags,
2776 &lsa->lsa_comp_neg_flags);
2780 * Only template flags have meaning in
2781 * the layout for a new file
2783 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2785 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2786 if (!strcmp(string, "lcm_mirror_count")) {
2787 lsa->lsa_mirror_count = node->cy_valueint;
2788 } else if (!strcmp(string, "lcme_extent.e_start")) {
2789 if (node->cy_valueint == 0)
2790 lsa->lsa_first_comp = true;
2791 } else if (!strcmp(string, "lcme_extent.e_end")) {
2792 if (node->cy_valueint == -1)
2793 lsa->lsa_comp_end = LUSTRE_EOF;
2795 lsa->lsa_comp_end = node->cy_valueint;
2796 } else if (!strcmp(string, "stripe_count")) {
2797 lsa->lsa_stripe_count = node->cy_valueint;
2798 } else if (!strcmp(string, "stripe_size")) {
2799 lsa->lsa_stripe_size = node->cy_valueint;
2800 } else if (!strcmp(string, "extension_size")) {
2801 lsa->lsa_extension_size = node->cy_valueint;
2802 lsa->lsa_extension_comp = true;
2803 } else if (!strcmp(string, "stripe_offset")) {
2804 lsa->lsa_stripe_off = node->cy_valueint;
2805 } else if (!strcmp(string, "l_ost_idx")) {
2806 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2811 node = node->cy_next;
2814 if (prevp == &prev) {
2815 rc = build_prev_component(layout, prevp, lsa, true);
2819 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2820 rc = build_component(layout, lsa, *layout != NULL);
2826 static int lfs_comp_create_from_yaml(char *template,
2827 struct llapi_layout **layout,
2828 struct lfs_setstripe_args *lsa,
2831 struct cYAML *tree = NULL, *err_rc = NULL;
2834 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2836 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2837 progname, template);
2838 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2839 "can't parse", &err_rc);
2840 cYAML_print_tree2file(stderr, err_rc);
2841 cYAML_free_tree(err_rc);
2846 /* initialize lsa for plain file */
2847 setstripe_args_init(lsa);
2848 lsa->lsa_tgts = osts;
2850 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2852 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2853 progname, template);
2856 /* clean clean lsa */
2857 setstripe_args_init(lsa);
2861 cYAML_free_tree(tree);
2866 * Get the extension size from the next (SEL) component and extend the
2867 * current component on it. The start of the next component is to be
2870 * \param[in] layout the current layout
2871 * \param[in] start the start of the current component
2872 * \param[in,out] end the end of the current component
2873 * \param[in] offset the offset to adjust the end position to instead of
2876 * \retval 0 - extended successfully
2877 * \retval < 0 - error
2879 static int layout_extend_comp(struct llapi_layout *layout,
2880 uint64_t start, uint64_t *end,
2883 uint64_t size, next_start, next_end;
2886 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2889 "%s setstripe: cannot move component cursor: %s\n",
2890 progname, strerror(errno));
2895 * Even if the @size will not be used below, this will fail if
2896 * this is not a SEL component - a good confirmation we are
2897 * working on right components.
2899 rc = llapi_layout_extension_size_get(layout, &size);
2902 "%s setstripe: cannot get component ext size: %s\n",
2903 progname, strerror(errno));
2907 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2909 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2910 progname, strerror(errno));
2914 next_start += offset ?: size;
2915 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2917 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2918 progname, strerror(errno));
2922 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2925 "%s setstripe: cannot move component cursor: %s\n",
2926 progname, strerror(errno));
2930 *end += offset ?: size;
2931 rc = llapi_layout_comp_extent_set(layout, start, *end);
2933 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2934 progname, strerror(errno));
2942 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2943 * end of the last component in the existing file, and adjust the
2944 * first extent start of the components to be added accordingly.
2946 * In the create mode, we need to check if the first component is an extendable
2947 * SEL component and extend its length to the extension size (first component
2948 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2950 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2953 struct llapi_layout *head;
2954 uint64_t start = 0, prev_end = 0;
2958 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
2963 head = llapi_layout_get_by_path(fname, 0);
2966 "%s setstripe: cannot read layout from '%s': %s\n",
2967 progname, fname, strerror(errno));
2969 } else if (errno == ENODATA) {
2971 * file without LOVEA, this component-add will be turned
2972 * into a component-create.
2974 llapi_layout_free(head);
2978 * the new layout will be added to an empty one, it
2979 * still needs to be adjusted below
2983 } else if (!llapi_layout_is_composite(head)) {
2985 "%s setstripe: '%s' not a composite file\n",
2987 llapi_layout_free(head);
2991 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2994 "%s setstripe: cannot get prev extent: %s\n",
2995 progname, strerror(errno));
2996 llapi_layout_free(head);
3000 llapi_layout_free(head);
3004 /* Make sure we use the first component of the layout to be added. */
3005 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3008 "%s setstripe: cannot move component cursor: %s\n",
3009 progname, strerror(errno));
3013 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3015 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3016 progname, strerror(errno));
3020 if (start == 0 && end == 0) {
3021 rc = layout_extend_comp(layout, start, &end,
3022 comp_add ? prev_end : 0);
3027 if (start > prev_end || end < prev_end) {
3029 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3030 progname, start, end, prev_end);
3034 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3037 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3038 progname, prev_end, end, strerror(errno));
3045 static int mirror_adjust_first_extents(struct mirror_args *list)
3053 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3056 list = list->m_next;
3062 static inline bool arg_is_eof(char *arg)
3064 return !strncmp(arg, "-1", strlen("-1")) ||
3065 !strncmp(arg, "EOF", strlen("EOF")) ||
3066 !strncmp(arg, "eof", strlen("eof"));
3070 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3072 * Return: Valid mirror_args pointer on success and
3073 * NULL if memory allocation fails.
3075 static struct mirror_args *lfs_mirror_alloc(void)
3077 struct mirror_args *mirror = NULL;
3080 mirror = calloc(1, sizeof(*mirror));
3082 mirror->m_inherit = false;
3093 * lfs_mirror_free() - Free memory allocated for a mirror argument
3095 * @mirror: Previously allocated mirror argument structure by
3096 * lfs_mirror_alloc().
3098 * Free memory allocated for @mirror.
3102 static void lfs_mirror_free(struct mirror_args *mirror)
3104 if (mirror->m_layout)
3105 llapi_layout_free(mirror->m_layout);
3110 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3111 * @mirror_list: Previously allocated mirror list.
3113 * Free memory allocated for @mirror_list.
3117 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3119 struct mirror_args *next_mirror = NULL;
3121 while (mirror_list) {
3122 next_mirror = mirror_list->m_next;
3123 lfs_mirror_free(mirror_list);
3124 mirror_list = next_mirror;
3136 LFS_COMP_NO_VERIFY_OPT,
3138 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3140 LFS_MIRROR_STATE_OPT,
3142 LFS_MIRROR_INDEX_OPT,
3143 LFS_LAYOUT_FOREIGN_OPT,
3149 static int lfs_setstripe_internal(int argc, char **argv,
3150 enum setstripe_origin opc)
3152 struct lfs_setstripe_args lsa = { 0 };
3153 struct llapi_stripe_param *param = NULL;
3154 struct find_param migrate_mdt_param = {
3164 unsigned long long size_units = 1;
3165 bool migrate_mode = false;
3166 bool migrate_mdt_mode = false;
3167 bool setstripe_mode = false;
3168 bool migration_block = false;
3169 __u64 migration_flags = 0;
3170 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3171 int comp_del = 0, comp_set = 0;
3174 struct llapi_layout *layout = NULL;
3175 struct llapi_layout **lpp = &layout;
3176 bool mirror_mode = false;
3177 bool has_m_file = false;
3178 __u32 mirror_count = 0;
3179 enum mirror_flags mirror_flags = 0;
3180 struct mirror_args *mirror_list = NULL;
3181 struct mirror_args *new_mirror = NULL;
3182 struct mirror_args *last_mirror = NULL;
3183 __u16 mirror_id = 0;
3185 bool from_yaml = false;
3186 bool from_copy = false;
3187 char *template = NULL;
3188 bool foreign_mode = false;
3190 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3191 char *mode_opt = NULL;
3192 mode_t previous_umask = 0;
3193 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3195 struct option long_opts[] = {
3196 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3197 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3198 /* --block is only valid in migrate mode */
3199 { .val = 'b', .name = "block", .has_arg = no_argument },
3200 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3201 { .val = LFS_COMP_ADD_OPT,
3202 .name = "comp-add", .has_arg = no_argument },
3203 { .val = LFS_COMP_ADD_OPT,
3204 .name = "component-add", .has_arg = no_argument },
3205 { .val = LFS_COMP_DEL_OPT,
3206 .name = "comp-del", .has_arg = no_argument },
3207 { .val = LFS_COMP_DEL_OPT,
3208 .name = "component-del", .has_arg = no_argument },
3209 { .val = LFS_COMP_FLAGS_OPT,
3210 .name = "comp-flags", .has_arg = required_argument },
3211 { .val = LFS_COMP_FLAGS_OPT,
3212 .name = "component-flags",
3213 .has_arg = required_argument },
3214 { .val = LFS_COMP_SET_OPT,
3215 .name = "comp-set", .has_arg = no_argument },
3216 { .val = LFS_COMP_SET_OPT,
3217 .name = "component-set",
3218 .has_arg = no_argument},
3219 { .val = LFS_COMP_NO_VERIFY_OPT,
3220 .name = "no-verify", .has_arg = no_argument},
3221 { .val = LFS_LAYOUT_FLAGS_OPT,
3222 .name = "flags", .has_arg = required_argument},
3223 { .val = LFS_LAYOUT_FOREIGN_OPT,
3224 .name = "foreign", .has_arg = optional_argument},
3225 { .val = LFS_MIRROR_ID_OPT,
3226 .name = "mirror-id", .has_arg = required_argument},
3227 { .val = LFS_MODE_OPT,
3228 .name = "mode", .has_arg = required_argument},
3229 { .val = LFS_LAYOUT_COPY,
3230 .name = "copy", .has_arg = required_argument},
3231 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3232 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3233 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3234 { .val = 'C', .name = "overstripe-count",
3235 .has_arg = required_argument},
3236 { .val = 'd', .name = "delete", .has_arg = no_argument},
3237 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3238 /* --non-direct is only valid in migrate mode */
3239 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3240 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3241 { .val = 'E', .name = "component-end",
3242 .has_arg = required_argument},
3243 { .val = 'f', .name = "file", .has_arg = required_argument },
3244 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3245 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3246 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3247 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3248 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3249 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3250 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3251 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3252 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3253 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3254 { .val = 'L', .name = "layout", .has_arg = required_argument },
3255 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3256 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3257 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3258 /* --non-block is only valid in migrate mode */
3259 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3260 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3261 { .val = 'o', .name = "ost", .has_arg = required_argument },
3262 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3263 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3264 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3266 { .val = 'p', .name = "pool", .has_arg = required_argument },
3267 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3268 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3269 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3270 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3271 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3272 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3273 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3274 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3275 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3276 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3277 /* --verbose is only valid in migrate mode */
3278 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3279 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3280 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3281 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3282 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3285 setstripe_args_init(&lsa);
3287 migrate_mode = (opc == SO_MIGRATE);
3288 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3289 setstripe_mode = (opc == SO_SETSTRIPE);
3290 if (opc == SO_MIRROR_DELETE) {
3292 mirror_flags = MF_DESTROY;
3295 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3297 while ((c = getopt_long(argc, argv,
3298 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3299 long_opts, NULL)) >= 0) {
3305 case LFS_COMP_ADD_OPT:
3308 case LFS_COMP_DEL_OPT:
3311 case LFS_COMP_FLAGS_OPT:
3312 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3313 &lsa.lsa_comp_neg_flags);
3316 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3318 "%s: inverted flags are not supported\n",
3323 case LFS_COMP_SET_OPT:
3326 case LFS_COMP_NO_VERIFY_OPT:
3327 mirror_flags |= MF_NO_VERIFY;
3329 case LFS_MIRROR_ID_OPT:
3330 mirror_id = strtoul(optarg, &end, 0);
3331 if (*end != '\0' || mirror_id == 0) {
3333 "%s %s: invalid mirror ID '%s'\n",
3334 progname, argv[0], optarg);
3338 case LFS_LAYOUT_FLAGS_OPT: {
3341 /* check for numeric flags (foreign and mirror cases) */
3342 if (setstripe_mode && !mirror_mode && !last_mirror) {
3343 flags = strtoul(optarg, &end, 16);
3346 "%s %s: bad flags '%s'\n",
3347 progname, argv[0], optarg);
3353 if (!mirror_mode || !last_mirror) {
3355 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3360 result = comp_str2flags(optarg, &last_mirror->m_flags,
3367 "%s: inverted flags are not supported\n",
3372 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3374 "%s: unsupported mirror flags: %s\n",
3381 case LFS_LAYOUT_FOREIGN_OPT:
3383 /* check pure numeric */
3384 type = strtoul(optarg, &end, 0);
3387 type = check_foreign_type_name(optarg);
3388 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3390 "%s %s: unrecognized foreign type '%s'\n",
3397 foreign_mode = true;
3402 mode = strtoul(mode_opt, &end, 8);
3405 "%s %s: bad mode '%s'\n",
3406 progname, argv[0], mode_opt);
3409 previous_umask = umask(0);
3412 case LFS_LAYOUT_COPY:
3417 if (!migrate_mode) {
3419 "%s %s: -b|--block valid only for migrate command\n",
3423 migration_block = true;
3426 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3428 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3432 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3435 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3438 "%s %s: invalid stripe count '%s'\n",
3439 progname, argv[0], optarg);
3443 if (lsa.lsa_stripe_count == -1)
3444 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3447 /* delete the default striping pattern */
3449 if (opc == SO_MIRROR_SPLIT) {
3452 "%s %s: -d cannot used with -f\n",
3456 mirror_flags |= MF_DESTROY;
3460 if (!migrate_mode) {
3462 "%s %s: -D|--non-direct is valid only for migrate command\n",
3466 migration_flags |= MIGRATION_NONDIRECT;
3469 if (lsa.lsa_comp_end != 0) {
3470 result = comp_args_to_layout(lpp, &lsa, true);
3472 fprintf(stderr, "%s: invalid layout\n",
3477 setstripe_args_init_inherit(&lsa);
3480 if (arg_is_eof(optarg)) {
3481 lsa.lsa_comp_end = LUSTRE_EOF;
3483 result = llapi_parse_size(optarg,
3488 "%s %s: invalid component end '%s'\n",
3489 progname, argv[0], optarg);
3495 if (!migrate_mode) {
3497 "--mdt-hash is valid only for migrate command\n");
3501 lsa.lsa_pattern = check_hashtype(optarg);
3502 if (lsa.lsa_pattern == 0) {
3504 "%s %s: bad stripe hash type '%s'\n",
3505 progname, argv[0], optarg);
3510 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3513 "%s %s: invalid stripe offset '%s'\n",
3514 progname, argv[0], optarg);
3517 if (lsa.lsa_stripe_off == -1)
3518 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3521 comp_id = strtoul(optarg, &end, 0);
3522 if (*end != '\0' || comp_id == 0 ||
3523 comp_id > LCME_ID_MAX) {
3525 "%s %s: invalid component ID '%s'\n",
3526 progname, argv[0], optarg);
3531 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3533 "error: %s: invalid option: %s\n",
3534 progname, argv[optopt + 1]);
3537 if (opc == SO_MIRROR_EXTEND) {
3540 "error: %s: '-N' must exist in front of '%s'\n",
3541 progname, argv[optopt + 1]);
3544 last_mirror->m_file = optarg;
3545 last_mirror->m_count = 1;
3549 mirror_list = lfs_mirror_alloc();
3550 mirror_list->m_file = optarg;
3555 if (strcmp(argv[optind - 1], "mdt") == 0) {
3556 /* Can be only the first component */
3560 "error: 'mdt' layout can be only the first one\n");
3563 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3566 "error: 'mdt' layout size is too big\n");
3569 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3570 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3573 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3579 if (!migrate_mode) {
3581 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3585 migrate_mdt_mode = true;
3586 lsa.lsa_nr_tgts = parse_targets(tgts,
3587 sizeof(tgts) / sizeof(__u32),
3588 lsa.lsa_nr_tgts, optarg, NULL);
3589 if (lsa.lsa_nr_tgts < 0) {
3591 "%s %s: invalid MDT target(s) '%s'\n",
3592 progname, argv[0], optarg);
3596 lsa.lsa_tgts = tgts;
3597 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3598 lsa.lsa_stripe_off = tgts[0];
3601 if (!migrate_mode) {
3603 "%s %s: -n|--non-block valid only for migrate command\n",
3607 migration_flags |= MIGRATION_NONBLOCK;
3610 if (opc == SO_SETSTRIPE) {
3611 opc = SO_MIRROR_CREATE;
3616 mirror_count = strtoul(optarg, &end, 0);
3617 if (*end != '\0' || mirror_count == 0) {
3619 "error: %s: bad mirror count: %s\n",
3626 new_mirror = lfs_mirror_alloc();
3627 new_mirror->m_count = mirror_count;
3630 mirror_list = new_mirror;
3633 /* wrap up last mirror */
3634 if (!setstripe_args_specified(&lsa))
3635 last_mirror->m_inherit = true;
3636 if (lsa.lsa_comp_end == 0)
3637 lsa.lsa_comp_end = LUSTRE_EOF;
3639 result = comp_args_to_layout(lpp, &lsa, true);
3641 lfs_mirror_free(new_mirror);
3645 setstripe_args_init_inherit(&lsa);
3647 last_mirror->m_next = new_mirror;
3650 last_mirror = new_mirror;
3651 lpp = &last_mirror->m_layout;
3654 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3655 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3657 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3659 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3661 "%s %s: -o|--ost incompatible with DoM layout\n",
3666 * -o allows overstriping, and must note it because
3667 * parse_targets is shared with MDT striping, which
3668 * does not allow duplicates
3670 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3671 lsa.lsa_nr_tgts = parse_targets(tgts,
3672 sizeof(tgts) / sizeof(__u32),
3673 lsa.lsa_nr_tgts, optarg,
3675 if (lsa.lsa_nr_tgts < 0) {
3677 "%s %s: invalid OST target(s) '%s'\n",
3678 progname, argv[0], optarg);
3682 lsa.lsa_tgts = tgts;
3683 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3684 lsa.lsa_stripe_off = tgts[0];
3689 lsa.lsa_pool_name = optarg;
3691 if (strlen(lsa.lsa_pool_name) == 0 ||
3692 strncmp(lsa.lsa_pool_name, "none",
3693 LOV_MAXPOOLNAME) == 0)
3694 lsa.lsa_pool_name = NULL;
3697 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3701 "%s %s: invalid stripe size '%s'\n",
3702 progname, argv[0], optarg);
3707 if (!migrate_mode) {
3709 "%s %s: -v|--verbose valid only for migrate command\n",
3713 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3714 migration_flags = MIGRATION_VERBOSE;
3724 result = llapi_parse_size(optarg,
3725 &lsa.lsa_extension_size,
3729 "%s %s: invalid extension size '%s'\n",
3730 progname, argv[0], optarg);
3734 lsa.lsa_extension_comp = true;
3737 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3738 progname, argv[0], argv[optind - 1]);
3743 fname = argv[optind];
3745 if (optind == argc) {
3746 fprintf(stderr, "%s %s: FILE must be specified\n",
3751 /* lfs migrate $filename should keep the file's layout by default */
3752 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3756 if (xattr && !foreign_mode) {
3758 * only print a warning as this is harmless and will be ignored
3761 "%s %s: xattr has been specified for non-foreign layout\n",
3763 } else if (foreign_mode && !xattr) {
3765 "%s %s: xattr must be provided in foreign mode\n",
3770 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3771 comp_set || comp_id || delete || from_copy ||
3772 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3775 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3780 if (mirror_mode && mirror_count == 0) {
3782 "error: %s: --mirror-count|-N option is required\n",
3789 if (!setstripe_args_specified(&lsa))
3790 last_mirror->m_inherit = true;
3791 if (lsa.lsa_comp_end == 0)
3792 lsa.lsa_comp_end = LUSTRE_EOF;
3795 if (lsa.lsa_comp_end != 0) {
3796 result = comp_args_to_layout(lpp, &lsa, true);
3798 fprintf(stderr, "error: %s: invalid layout\n",
3805 if (mirror_flags & MF_NO_VERIFY) {
3806 if (opc != SO_MIRROR_EXTEND) {
3808 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3812 } else if (!has_m_file) {
3814 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3821 if (comp_set && !comp_id) {
3823 "%s %s: --component-set doesn't have component-id set\n",
3828 if ((delete + comp_set + comp_del + comp_add) > 1) {
3830 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3835 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3836 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3838 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3843 if ((comp_set || comp_del) &&
3844 (setstripe_args_specified(&lsa) || layout != NULL)) {
3846 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3851 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3853 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3858 if (comp_add || comp_del) {
3861 result = lstat(fname, &st);
3862 if (result == 0 && S_ISDIR(st.st_mode)) {
3864 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3871 "error: %s: can't use --component-add or --component-del for mirror operation\n",
3880 "%s %s: option -E must be specified with --component-add\n",
3886 if (from_yaml && from_copy) {
3888 "%s: can't specify --yaml and --copy together\n",
3893 if ((from_yaml || from_copy) &&
3894 (setstripe_args_specified(&lsa) || layout != NULL)) {
3896 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
3901 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3903 "%s %s: options --non-block and --block are mutually exclusive\n",
3908 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3909 opc != SO_MIRROR_DELETE && comp_id != 0) {
3911 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3916 if (migrate_mdt_mode) {
3917 struct lmv_user_md *lmu;
3919 /* initialize migrate mdt parameters */
3920 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3921 LMV_USER_MAGIC_SPECIFIC));
3924 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3925 progname, argv[0], strerror(ENOMEM));
3929 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3930 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3931 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3933 "%s %s: migrate should specify MDT index\n",
3938 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3939 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3940 lmu->lum_hash_type = lsa.lsa_pattern;
3942 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
3943 if (lsa.lsa_pool_name) {
3944 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3945 sizeof(lmu->lum_pool_name) - 1);
3946 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3948 if (lsa.lsa_nr_tgts > 1) {
3951 if (lsa.lsa_stripe_count > 0 &&
3952 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3953 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3955 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3956 progname, lsa.lsa_stripe_count,
3962 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3963 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3964 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3965 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3967 lmu->lum_magic = LMV_USER_MAGIC;
3970 migrate_mdt_param.fp_lmv_md = lmu;
3971 migrate_mdt_param.fp_migrate = 1;
3972 } else if (!layout) {
3973 /* initialize stripe parameters */
3974 param = calloc(1, offsetof(typeof(*param),
3975 lsp_osts[lsa.lsa_nr_tgts]));
3978 "%s %s: cannot allocate memory for parameters: %s\n",
3979 progname, argv[0], strerror(ENOMEM));
3984 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3985 param->lsp_stripe_size = lsa.lsa_stripe_size;
3986 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3987 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3988 param->lsp_stripe_count = -1;
3990 param->lsp_stripe_count = lsa.lsa_stripe_count;
3992 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3993 param->lsp_stripe_offset = -1;
3995 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3996 param->lsp_stripe_pattern =
3997 llapi_pattern_to_lov(lsa.lsa_pattern);
3998 if (param->lsp_stripe_pattern == EINVAL) {
3999 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4004 param->lsp_pool = lsa.lsa_pool_name;
4005 param->lsp_is_specific = false;
4006 if (lsa.lsa_nr_tgts > 0) {
4007 if (lsa.lsa_stripe_count > 0 &&
4008 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4009 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4010 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4012 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4013 argv[0], lsa.lsa_stripe_count,
4019 param->lsp_is_specific = true;
4020 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4021 memcpy(param->lsp_osts, tgts,
4022 sizeof(*tgts) * lsa.lsa_nr_tgts);
4027 /* generate a layout from a YAML template */
4028 result = lfs_comp_create_from_yaml(template, &layout,
4032 "error: %s: can't create composite layout from template file %s\n",
4038 if (layout != NULL || mirror_list != NULL) {
4040 result = mirror_adjust_first_extents(mirror_list);
4042 result = layout_adjust_first_extent(fname, layout,
4044 if (result == -ENODATA)
4046 else if (result != 0) {
4047 fprintf(stderr, "error: %s: invalid layout\n",
4053 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4055 layout = llapi_layout_get_by_path(template ?: fname, 0);
4058 "%s: can't create composite layout from file %s: %s\n",
4059 progname, template ?: fname,
4066 if (migrate_mdt_mode) {
4067 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4068 } else if (migrate_mode) {
4069 result = lfs_migrate(fname, migration_flags, param,
4071 } else if (comp_set != 0) {
4072 result = lfs_component_set(fname, comp_id,
4074 lsa.lsa_comp_neg_flags);
4075 } else if (comp_del != 0) {
4076 result = lfs_component_del(fname, comp_id,
4078 lsa.lsa_comp_neg_flags);
4079 } else if (comp_add != 0) {
4080 result = lfs_component_add(fname, layout);
4081 } else if (opc == SO_MIRROR_CREATE) {
4082 result = mirror_create(fname, mirror_list);
4083 } else if (opc == SO_MIRROR_EXTEND) {
4084 result = mirror_extend(fname, mirror_list,
4086 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4087 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4089 "%s: no mirror specified to delete from '%s'\n",
4093 if (lsa.lsa_pool_name)
4094 mirror_flags |= MF_COMP_POOL;
4095 else if (mirror_id != 0)
4096 comp_id = mirror_id;
4098 mirror_flags |= MF_COMP_ID;
4099 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4101 has_m_file ? mirror_list->m_file :
4103 } else if (layout) {
4104 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4110 } else if (foreign_mode) {
4111 result = llapi_file_create_foreign(fname, mode, type,
4118 result = llapi_file_open_param(fname,
4127 /* Save the first error encountered. */
4135 umask(previous_umask);
4138 free(migrate_mdt_param.fp_lmv_md);
4139 llapi_layout_free(layout);
4140 lfs_mirror_list_free(mirror_list);
4145 llapi_layout_free(layout);
4146 lfs_mirror_list_free(mirror_list);
4150 static int lfs_poollist(int argc, char **argv)
4155 return llapi_poollist(argv[1]);
4158 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4159 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4164 char *endptr = "AD";
4169 else if (str[0] == '-')
4175 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4176 long long val = strtoll(timebuf, &endptr, 0);
4181 unit *= 52; /* 52 weeks + 1 day below */
4182 case 'w': /* fallthrough */
4184 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4185 param->fp_time_margin *= (1 + unit / 52);
4186 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4187 case '\0': /* days are default unit if none used */
4188 case 'd': /* fallthrough */
4190 case 'h': /* fallthrough */
4192 case 'm': /* fallthrough */
4194 case 's': /* fallthrough */
4196 /* don't need to multiply by 1 for seconds */
4199 "%s find: bad time string '%s': %s\n",
4200 progname, timebuf, strerror(EINVAL));
4204 if (param->fp_time_margin == 0 ||
4205 (*endptr && unit < param->fp_time_margin))
4206 param->fp_time_margin = unit;
4213 fprintf(stderr, "%s find: bad time '%s': too large\n",
4223 static int str2quotaid(__u32 *id, const char *arg)
4225 unsigned long int projid_tmp = 0;
4226 char *endptr = NULL;
4228 projid_tmp = strtoul(arg, &endptr, 10);
4229 if (*endptr != '\0')
4231 if (projid_tmp >= UINT32_MAX)
4238 static int name2uid(unsigned int *id, const char *name)
4240 struct passwd *passwd;
4242 passwd = getpwnam(name);
4245 *id = passwd->pw_uid;
4250 static int name2gid(unsigned int *id, const char *name)
4252 struct group *group;
4254 group = getgrnam(name);
4257 *id = group->gr_gid;
4262 static inline int name2projid(unsigned int *id, const char *name)
4267 static int uid2name(char **name, unsigned int id)
4269 struct passwd *passwd;
4271 passwd = getpwuid(id);
4274 *name = passwd->pw_name;
4279 static inline int gid2name(char **name, unsigned int id)
4281 struct group *group;
4283 group = getgrgid(id);
4286 *name = group->gr_name;
4291 static int name2layout(__u32 *layout, char *name)
4293 char *ptr, *layout_name;
4296 for (ptr = name; ; ptr = NULL) {
4297 layout_name = strtok(ptr, ",");
4300 if (strcmp(layout_name, "released") == 0)
4301 *layout |= LOV_PATTERN_F_RELEASED;
4302 else if (strcmp(layout_name, "raid0") == 0)
4303 *layout |= LOV_PATTERN_RAID0;
4304 else if (strcmp(layout_name, "mdt") == 0)
4305 *layout |= LOV_PATTERN_MDT;
4306 else if (strcmp(layout_name, "overstriping") == 0)
4307 *layout |= LOV_PATTERN_OVERSTRIPING;
4314 static int lfs_find(int argc, char **argv)
4319 struct find_param param = {
4322 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4324 struct option long_opts[] = {
4325 { .val = 'A', .name = "atime", .has_arg = required_argument },
4326 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4327 { .val = 'B', .name = "btime", .has_arg = required_argument },
4328 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4329 { .val = LFS_COMP_COUNT_OPT,
4330 .name = "comp-count", .has_arg = required_argument },
4331 { .val = LFS_COMP_COUNT_OPT,
4332 .name = "component-count",
4333 .has_arg = required_argument },
4334 { .val = LFS_COMP_FLAGS_OPT,
4335 .name = "comp-flags", .has_arg = required_argument },
4336 { .val = LFS_COMP_FLAGS_OPT,
4337 .name = "component-flags",
4338 .has_arg = required_argument },
4339 { .val = LFS_COMP_START_OPT,
4340 .name = "comp-start", .has_arg = required_argument },
4341 { .val = LFS_COMP_START_OPT,
4342 .name = "component-start",
4343 .has_arg = required_argument },
4344 { .val = LFS_MIRROR_STATE_OPT,
4345 .name = "mirror-state", .has_arg = required_argument },
4346 { .val = LFS_LAYOUT_FOREIGN_OPT,
4347 .name = "foreign", .has_arg = optional_argument},
4348 { .val = LFS_NEWERXY_OPT,
4349 .name = "newer", .has_arg = required_argument},
4350 { .val = LFS_NEWERXY_OPT,
4351 .name = "neweraa", .has_arg = required_argument},
4352 { .val = LFS_NEWERXY_OPT,
4353 .name = "neweram", .has_arg = required_argument},
4354 { .val = LFS_NEWERXY_OPT,
4355 .name = "newerac", .has_arg = required_argument},
4356 { .val = LFS_NEWERXY_OPT,
4357 .name = "newerab", .has_arg = required_argument},
4358 { .val = LFS_NEWERXY_OPT,
4359 .name = "newerma", .has_arg = required_argument},
4360 { .val = LFS_NEWERXY_OPT,
4361 .name = "newermm", .has_arg = required_argument},
4362 { .val = LFS_NEWERXY_OPT,
4363 .name = "newermc", .has_arg = required_argument},
4364 { .val = LFS_NEWERXY_OPT,
4365 .name = "newermb", .has_arg = required_argument},
4366 { .val = LFS_NEWERXY_OPT,
4367 .name = "newerca", .has_arg = required_argument},
4368 { .val = LFS_NEWERXY_OPT,
4369 .name = "newercm", .has_arg = required_argument},
4370 { .val = LFS_NEWERXY_OPT,
4371 .name = "newercc", .has_arg = required_argument},
4372 { .val = LFS_NEWERXY_OPT,
4373 .name = "newercb", .has_arg = required_argument},
4374 { .val = LFS_NEWERXY_OPT,
4375 .name = "newerba", .has_arg = required_argument},
4376 { .val = LFS_NEWERXY_OPT,
4377 .name = "newerbm", .has_arg = required_argument},
4378 { .val = LFS_NEWERXY_OPT,
4379 .name = "newerbc", .has_arg = required_argument},
4380 { .val = LFS_NEWERXY_OPT,
4381 .name = "newerbb", .has_arg = required_argument},
4382 { .val = LFS_NEWERXY_OPT,
4383 .name = "newerBa", .has_arg = required_argument},
4384 { .val = LFS_NEWERXY_OPT,
4385 .name = "newerBm", .has_arg = required_argument},
4386 { .val = LFS_NEWERXY_OPT,
4387 .name = "newerBc", .has_arg = required_argument},
4388 { .val = LFS_NEWERXY_OPT,
4389 .name = "newerBB", .has_arg = required_argument},
4390 { .val = LFS_NEWERXY_OPT,
4391 .name = "newerat", .has_arg = required_argument},
4392 { .val = LFS_NEWERXY_OPT,
4393 .name = "newermt", .has_arg = required_argument},
4394 { .val = LFS_NEWERXY_OPT,
4395 .name = "newerct", .has_arg = required_argument},
4396 { .val = LFS_NEWERXY_OPT,
4397 .name = "newerbt", .has_arg = required_argument},
4398 { .val = LFS_NEWERXY_OPT,
4399 .name = "newerBt", .has_arg = required_argument},
4400 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4401 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4402 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4403 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4404 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4405 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4406 { .val = 'E', .name = "component-end",
4407 .has_arg = required_argument },
4408 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4409 { .val = LFS_LAYOUT_FOREIGN_OPT,
4410 .name = "foreign", .has_arg = optional_argument},
4411 { .val = 'g', .name = "gid", .has_arg = required_argument },
4412 { .val = 'G', .name = "group", .has_arg = required_argument },
4413 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4414 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4415 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4416 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4417 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4418 { .val = 'L', .name = "layout", .has_arg = required_argument },
4419 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4420 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4421 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4422 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4423 { .val = 'n', .name = "name", .has_arg = required_argument },
4424 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4425 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4426 { .val = 'O', .name = "obd", .has_arg = required_argument },
4427 { .val = 'O', .name = "ost", .has_arg = required_argument },
4428 /* no short option for pool yet, can be 'p' after 2.18 */
4429 { .val = LFS_POOL_OPT,
4430 .name = "pool", .has_arg = required_argument },
4431 { .val = '0', .name = "print0", .has_arg = no_argument },
4432 { .val = 'P', .name = "print", .has_arg = no_argument },
4433 { .val = LFS_PROJID_OPT,
4434 .name = "projid", .has_arg = required_argument },
4435 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4436 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4437 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4438 { .val = 's', .name = "size", .has_arg = required_argument },
4439 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4440 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4441 { .val = 't', .name = "type", .has_arg = required_argument },
4442 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4443 { .val = 'u', .name = "uid", .has_arg = required_argument },
4444 { .val = 'U', .name = "user", .has_arg = required_argument },
4445 { .val = 'z', .name = "extension-size",
4446 .has_arg = required_argument },
4447 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4448 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4449 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4463 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4464 while ((c = getopt_long_only(argc, argv,
4465 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4466 long_opts, &optidx)) >= 0) {
4471 /* '!' is part of option */
4473 * when getopt_long_only() finds a string which is not
4474 * an option nor a known option argument it returns 1
4475 * in that case if we already have found pathstart and pathend
4476 * (i.e. we have the list of pathnames),
4477 * the only supported value is "!"
4479 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4480 if (!isoption && pathend != -1) {
4482 "err: %s: filename|dirname must either precede options or follow options\n",
4487 if (!isoption && pathstart == -1)
4488 pathstart = optind - 1;
4489 if (isoption && pathstart != -1 && pathend == -1)
4490 pathend = optind - 2;
4497 * unknown; opt is "!" or path component,
4498 * checking done above.
4500 if (strcmp(optarg, "!") == 0)
4504 xtime = ¶m.fp_atime;
4505 xsign = ¶m.fp_asign;
4506 param.fp_exclude_atime = !!neg_opt;
4507 /* no break, this falls through to 'B' for btime */
4510 xtime = ¶m.fp_btime;
4511 xsign = ¶m.fp_bsign;
4512 param.fp_exclude_btime = !!neg_opt;
4514 /* no break, this falls through to 'C' for ctime */
4517 xtime = ¶m.fp_ctime;
4518 xsign = ¶m.fp_csign;
4519 param.fp_exclude_ctime = !!neg_opt;
4521 /* no break, this falls through to 'M' for mtime */
4524 xtime = ¶m.fp_mtime;
4525 xsign = ¶m.fp_msign;
4526 param.fp_exclude_mtime = !!neg_opt;
4528 rc = set_time(¶m, &t, xtime, optarg);
4529 if (rc == LONG_MAX) {
4537 if (optarg[0] == '+') {
4538 param.fp_blocks_sign = -1;
4540 } else if (optarg[0] == '-') {
4541 param.fp_blocks_sign = 1;
4545 param.fp_blocks_units = 1024;
4546 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4547 ¶m.fp_blocks_units, 0);
4549 fprintf(stderr, "error: bad blocks '%s'\n",
4553 param.fp_check_blocks = 1;
4554 param.fp_exclude_blocks = !!neg_opt;
4556 case LFS_COMP_COUNT_OPT:
4557 if (optarg[0] == '+') {
4558 param.fp_comp_count_sign = -1;
4560 } else if (optarg[0] == '-') {
4561 param.fp_comp_count_sign = 1;
4565 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4566 if (*endptr != '\0') {
4568 "error: bad component count '%s'\n",
4572 param.fp_check_comp_count = 1;
4573 param.fp_exclude_comp_count = !!neg_opt;
4575 case LFS_COMP_FLAGS_OPT:
4576 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4577 ¶m.fp_comp_neg_flags);
4580 "error: bad component flags '%s'\n",
4584 param.fp_check_comp_flags = 1;
4586 __u32 flags = param.fp_comp_neg_flags;
4588 param.fp_comp_neg_flags = param.fp_comp_flags;
4589 param.fp_comp_flags = flags;
4592 case LFS_COMP_START_OPT:
4593 if (optarg[0] == '+') {
4594 param.fp_comp_start_sign = -1;
4596 } else if (optarg[0] == '-') {
4597 param.fp_comp_start_sign = 1;
4601 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4602 ¶m.fp_comp_start_units, 0);
4605 "error: bad component start '%s'\n",
4609 param.fp_check_comp_start = 1;
4610 param.fp_exclude_comp_start = !!neg_opt;
4612 case LFS_MIRROR_STATE_OPT:
4613 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4614 ¶m.fp_mirror_neg_state);
4617 "error: bad mirrored file state '%s'\n",
4621 param.fp_check_mirror_state = 1;
4623 __u16 state = param.fp_mirror_neg_state;
4625 param.fp_mirror_neg_state =
4626 param.fp_mirror_state;
4627 param.fp_mirror_state = state;
4631 if (optarg[0] == '+') {
4632 param.fp_stripe_count_sign = -1;
4634 } else if (optarg[0] == '-') {
4635 param.fp_stripe_count_sign = 1;
4639 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4640 if (*endptr != '\0') {
4642 "error: bad stripe_count '%s'\n",
4647 param.fp_check_stripe_count = 1;
4648 param.fp_exclude_stripe_count = !!neg_opt;
4651 param.fp_max_depth = strtol(optarg, 0, 0);
4654 if (optarg[0] == '+') {
4655 param.fp_comp_end_sign = -1;
4657 } else if (optarg[0] == '-') {
4658 param.fp_comp_end_sign = 1;
4662 if (arg_is_eof(optarg)) {
4663 param.fp_comp_end = LUSTRE_EOF;
4664 param.fp_comp_end_units = 1;
4667 rc = llapi_parse_size(optarg,
4669 ¶m.fp_comp_end_units, 0);
4673 "error: bad component end '%s'\n",
4677 param.fp_check_comp_end = 1;
4678 param.fp_exclude_comp_end = !!neg_opt;
4680 case LFS_LAYOUT_FOREIGN_OPT: {
4681 /* all types by default */
4682 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4685 /* check pure numeric */
4686 type = strtoul(optarg, &endptr, 0);
4689 type = check_foreign_type_name(optarg);
4690 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4692 "%s %s: unknown foreign type '%s'\n",
4699 param.fp_foreign_type = type;
4700 param.fp_check_foreign = 1;
4701 param.fp_exclude_foreign = !!neg_opt;
4704 case LFS_NEWERXY_OPT: {
4710 time_t ref = time(NULL);
4712 /* no need to check bad options, they won't get here */
4713 if (strlen(long_opts[optidx].name) == 7) {
4714 x = long_opts[optidx].name[5];
4715 y = long_opts[optidx].name[6];
4719 static const char *const fmts[] = {
4720 "%Y-%m-%d %H:%M:%S",
4723 "%H:%M:%S", /* sometime today */
4732 for (i = 0; fmts[i] != NULL; i++) {
4735 /* Init for times relative to today */
4736 if (strncmp(fmts[i], "%H", 2) == 0)
4737 localtime_r(&ref, &tm);
4739 memset(&tm, 0, sizeof(tm));
4740 ptr = strptime(optarg, fmts[i], &tm);
4742 while (ptr && isspace(*ptr))
4744 if (ptr == optarg + strlen(optarg)) {
4752 "%s: invalid time '%s'\n",
4755 "supported formats are:\n ");
4756 for (i = 0; fmts[i] != NULL; i++)
4757 fprintf(stderr, "'%s', ",
4759 fprintf(stderr, "\n");
4765 } else if (y == 'b' || y == 'B') {
4768 rc = llapi_get_lum_file(optarg, NULL, &stx,
4770 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4771 if (!(stx.stx_mask & STATX_BTIME))
4776 "%s: get btime failed '%s': %s\n",
4782 ref = stx.stx_btime.tv_sec;
4784 struct stat statbuf;
4786 if (stat(optarg, &statbuf) < 0) {
4788 "%s: cannot stat file '%s': %s\n",
4797 ref = statbuf.st_atime;
4800 ref = statbuf.st_mtime;
4803 ref = statbuf.st_ctime;
4807 "%s: invalid Y argument: '%c'\n",
4816 xidx = NEWERXY_ATIME;
4819 xidx = NEWERXY_MTIME;
4822 xidx = NEWERXY_CTIME;
4826 xidx = NEWERXY_BTIME;
4830 "%s: invalid X argument: '%c'\n",
4837 newery = ¶m.fp_newery[xidx][negidx];
4843 *newery = *newery > ref ? ref : *newery;
4845 *newery = *newery > ref ? *newery : ref;
4847 param.fp_newerxy = 1;
4852 rc = name2gid(¶m.fp_gid, optarg);
4854 if (str2quotaid(¶m.fp_gid, optarg)) {
4856 "Group/GID: %s cannot be found.\n",
4862 param.fp_exclude_gid = !!neg_opt;
4863 param.fp_check_gid = 1;
4866 param.fp_hash_type = check_hashtype(optarg);
4867 if (param.fp_hash_type == 0) {
4868 fprintf(stderr, "error: bad hash_type '%s'\n",
4873 param.fp_check_hash_type = 1;
4874 param.fp_exclude_hash_type = !!neg_opt;
4880 ret = name2layout(¶m.fp_layout, optarg);
4883 param.fp_exclude_layout = !!neg_opt;
4884 param.fp_check_layout = 1;
4888 rc = name2uid(¶m.fp_uid, optarg);
4890 if (str2quotaid(¶m.fp_uid, optarg)) {
4892 "User/UID: %s cannot be found.\n",
4898 param.fp_exclude_uid = !!neg_opt;
4899 param.fp_check_uid = 1;
4902 param.fp_pattern = (char *)optarg;
4903 param.fp_exclude_pattern = !!neg_opt;
4906 if (optarg[0] == '+') {
4907 param.fp_mirror_count_sign = -1;
4909 } else if (optarg[0] == '-') {
4910 param.fp_mirror_count_sign = 1;
4914 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4915 if (*endptr != '\0') {
4917 "error: bad mirror count '%s'\n",
4921 param.fp_check_mirror_count = 1;
4922 param.fp_exclude_mirror_count = !!neg_opt;
4927 char *buf, *token, *next, *p;
4931 buf = strdup(optarg);
4937 param.fp_exclude_obd = !!neg_opt;
4940 while (token && *token) {
4941 token = strchr(token, ',');
4948 param.fp_exclude_mdt = !!neg_opt;
4949 param.fp_num_alloc_mdts += len;
4950 tmp = realloc(param.fp_mdt_uuid,
4951 param.fp_num_alloc_mdts *
4952 sizeof(*param.fp_mdt_uuid));
4958 param.fp_mdt_uuid = tmp;
4960 param.fp_exclude_obd = !!neg_opt;
4961 param.fp_num_alloc_obds += len;
4962 tmp = realloc(param.fp_obd_uuid,
4963 param.fp_num_alloc_obds *
4964 sizeof(*param.fp_obd_uuid));
4970 param.fp_obd_uuid = tmp;
4972 for (token = buf; token && *token; token = next) {
4973 struct obd_uuid *puuid;
4977 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4980 ¶m.fp_obd_uuid[param.fp_num_obds++];
4982 p = strchr(token, ',');
4989 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4994 strncpy(puuid->uuid, token,
4995 sizeof(puuid->uuid));
5002 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5006 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5008 "Pool name %s is too long (max %d)\n",
5009 optarg, LOV_MAXPOOLNAME);
5014 * We do check for empty pool because empty pool
5015 * is used to find V1 LOV attributes
5017 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5018 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5019 param.fp_exclude_pool = !!neg_opt;
5020 param.fp_check_pool = 1;
5022 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5023 case 'p': /* want this for --pool, to match getstripe/find */
5025 "warning: -p deprecated, use --print0 or -0\n");
5028 param.fp_zero_end = 1;
5030 case 'P': /* we always print, this option is a no-op */
5032 case LFS_PROJID_OPT:
5033 rc = name2projid(¶m.fp_projid, optarg);
5035 if (str2quotaid(¶m.fp_projid, optarg)) {
5037 "Invalid project ID: %s\n",
5043 param.fp_exclude_projid = !!neg_opt;
5044 param.fp_check_projid = 1;
5047 if (optarg[0] == '+') {
5048 param.fp_size_sign = -1;
5050 } else if (optarg[0] == '-') {
5051 param.fp_size_sign = 1;
5055 ret = llapi_parse_size(optarg, ¶m.fp_size,
5056 ¶m.fp_size_units, 0);
5058 fprintf(stderr, "error: bad file size '%s'\n",
5062 param.fp_check_size = 1;
5063 param.fp_exclude_size = !!neg_opt;
5066 if (optarg[0] == '+') {
5067 param.fp_stripe_size_sign = -1;
5069 } else if (optarg[0] == '-') {
5070 param.fp_stripe_size_sign = 1;
5074 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5075 ¶m.fp_stripe_size_units, 0);
5077 fprintf(stderr, "error: bad stripe_size '%s'\n",
5081 param.fp_check_stripe_size = 1;
5082 param.fp_exclude_stripe_size = !!neg_opt;
5085 param.fp_exclude_type = !!neg_opt;
5086 switch (optarg[0]) {
5088 param.fp_type = S_IFBLK;
5091 param.fp_type = S_IFCHR;
5094 param.fp_type = S_IFDIR;
5097 param.fp_type = S_IFREG;
5100 param.fp_type = S_IFLNK;
5103 param.fp_type = S_IFIFO;
5106 param.fp_type = S_IFSOCK;
5109 fprintf(stderr, "error: %s: bad type '%s'\n",
5116 if (optarg[0] == '+') {
5117 param.fp_mdt_count_sign = -1;
5119 } else if (optarg[0] == '-') {
5120 param.fp_mdt_count_sign = 1;
5124 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5125 if (*endptr != '\0') {
5126 fprintf(stderr, "error: bad mdt_count '%s'\n",
5131 param.fp_check_mdt_count = 1;
5132 param.fp_exclude_mdt_count = !!neg_opt;
5135 if (optarg[0] == '+') {
5136 param.fp_ext_size_sign = -1;
5138 } else if (optarg[0] == '-') {
5139 param.fp_ext_size_sign = 1;
5143 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5144 ¶m.fp_ext_size_units, 0);
5146 fprintf(stderr, "error: bad ext-size '%s'\n",
5150 param.fp_ext_size /= SEL_UNIT_SIZE;
5151 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5152 param.fp_check_ext_size = 1;
5153 param.fp_exclude_ext_size = !!neg_opt;
5161 if (pathstart == -1) {
5162 fprintf(stderr, "error: %s: no filename|pathname\n",
5166 } else if (pathend == -1) {
5172 rc = llapi_find(argv[pathstart], ¶m);
5175 pathbad = pathstart;
5177 } while (++pathstart < pathend);
5180 fprintf(stderr, "%s: failed for '%s': %s\n",
5181 progname, argv[pathbad], strerror(-rc));
5184 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5185 free(param.fp_obd_uuid);
5187 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5188 free(param.fp_mdt_uuid);
5193 static int lfs_getstripe_internal(int argc, char **argv,
5194 struct find_param *param)
5196 struct option long_opts[] = {
5197 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5198 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5199 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5200 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5201 { .val = LFS_COMP_COUNT_OPT,
5202 .name = "comp-count", .has_arg = no_argument },
5203 { .val = LFS_COMP_COUNT_OPT,
5204 .name = "component-count", .has_arg = no_argument },
5205 { .val = LFS_COMP_FLAGS_OPT,
5206 .name = "comp-flags", .has_arg = optional_argument },
5207 { .val = LFS_COMP_FLAGS_OPT,
5208 .name = "component-flags", .has_arg = optional_argument },
5209 { .val = LFS_COMP_START_OPT,
5210 .name = "comp-start", .has_arg = optional_argument },
5211 { .val = LFS_COMP_START_OPT,
5212 .name = "component-start", .has_arg = optional_argument },
5213 { .val = LFS_MIRROR_INDEX_OPT,
5214 .name = "mirror-index", .has_arg = required_argument },
5215 { .val = LFS_MIRROR_ID_OPT,
5216 .name = "mirror-id", .has_arg = required_argument },
5217 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5218 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5219 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5220 { .val = 'd', .name = "directory", .has_arg = no_argument },
5221 { .val = 'D', .name = "default", .has_arg = no_argument },
5222 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5223 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5224 { .val = 'F', .name = "fid", .has_arg = no_argument },
5225 { .val = 'g', .name = "generation", .has_arg = no_argument },
5226 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5227 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5228 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5229 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5230 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5231 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5232 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5233 { .val = 'L', .name = "layout", .has_arg = no_argument },
5234 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5235 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5236 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5237 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5238 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5239 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5240 { .val = 'O', .name = "obd", .has_arg = required_argument },
5241 { .val = 'O', .name = "ost", .has_arg = required_argument },
5242 { .val = 'p', .name = "pool", .has_arg = no_argument },
5243 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5244 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5245 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5246 { .val = 'R', .name = "raw", .has_arg = no_argument },
5247 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5248 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5249 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5250 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5251 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5252 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5253 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5254 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5255 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5256 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5260 int pathstart = -1, pathend = -1;
5264 while ((c = getopt_long(argc, argv,
5265 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5266 long_opts, NULL)) != -1) {
5270 /* '!' is part of option */
5271 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5272 if (!isoption && pathend != -1) {
5274 "error: %s: filename|dirname must either precede options or follow options\n",
5278 if (!isoption && pathstart == -1)
5279 pathstart = optind - 1;
5280 if (isoption && pathstart != -1 && pathend == -1)
5281 pathend = optind - 2;
5285 /* unknown: opt is "!" */
5286 if (strcmp(optarg, "!") == 0)
5290 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5291 param->fp_verbose |= VERBOSE_COUNT;
5292 param->fp_max_depth = 0;
5295 case LFS_COMP_COUNT_OPT:
5296 param->fp_verbose |= VERBOSE_COMP_COUNT;
5297 param->fp_max_depth = 0;
5299 case LFS_COMP_FLAGS_OPT:
5301 rc = comp_str2flags(optarg,
5302 ¶m->fp_comp_flags,
5303 ¶m->fp_comp_neg_flags);
5306 "error: %s bad component flags '%s'.\n",
5310 param->fp_check_comp_flags = 1;
5312 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5313 param->fp_max_depth = 0;
5316 case LFS_COMP_START_OPT:
5319 if (tmp[0] == '+') {
5320 param->fp_comp_start_sign = -1;
5322 } else if (tmp[0] == '-') {
5323 param->fp_comp_start_sign = 1;
5326 rc = llapi_parse_size(tmp,
5327 ¶m->fp_comp_start,
5328 ¶m->fp_comp_start_units, 0);
5331 "error: %s bad component start '%s'.\n",
5335 param->fp_check_comp_start = 1;
5337 param->fp_verbose |= VERBOSE_COMP_START;
5338 param->fp_max_depth = 0;
5341 case LFS_MIRROR_INDEX_OPT:
5342 if (optarg[0] == '+') {
5343 param->fp_mirror_index_sign = -1;
5345 } else if (optarg[0] == '-') {
5346 param->fp_mirror_index_sign = 1;
5350 param->fp_mirror_index = strtoul(optarg, &end, 0);
5351 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5352 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5354 "%s %s: invalid mirror index '%s'\n",
5355 progname, argv[0], optarg);
5358 if (param->fp_mirror_id != 0) {
5360 "%s %s: can't specify both mirror index and mirror ID\n",
5364 param->fp_check_mirror_index = 1;
5365 param->fp_exclude_mirror_index = !!neg_opt;
5367 case LFS_MIRROR_ID_OPT:
5368 if (optarg[0] == '+') {
5369 param->fp_mirror_id_sign = -1;
5371 } else if (optarg[0] == '-') {
5372 param->fp_mirror_id_sign = 1;
5376 param->fp_mirror_id = strtoul(optarg, &end, 0);
5377 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5378 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5380 "%s %s: invalid mirror ID '%s'\n",
5381 progname, argv[0], optarg);
5384 if (param->fp_mirror_index != 0) {
5386 "%s %s: can't specify both mirror index and mirror ID\n",
5390 param->fp_check_mirror_id = 1;
5391 param->fp_exclude_mirror_id = !!neg_opt;
5394 param->fp_max_depth = 0;
5397 param->fp_get_default_lmv = 1;
5402 if (tmp[0] == '+') {
5403 param->fp_comp_end_sign = -1;
5405 } else if (tmp[0] == '-') {
5406 param->fp_comp_end_sign = 1;
5410 if (arg_is_eof(tmp)) {
5411 param->fp_comp_end = LUSTRE_EOF;
5412 param->fp_comp_end_units = 1;
5415 rc = llapi_parse_size(tmp,
5416 ¶m->fp_comp_end,
5417 ¶m->fp_comp_end_units, 0);
5421 "error: %s bad component end '%s'.\n",
5425 param->fp_check_comp_end = 1;
5427 param->fp_verbose |= VERBOSE_COMP_END;
5428 param->fp_max_depth = 0;
5432 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5433 param->fp_verbose |= VERBOSE_DFID;
5434 param->fp_max_depth = 0;
5438 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5439 param->fp_verbose |= VERBOSE_GENERATION;
5440 param->fp_max_depth = 0;
5444 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5445 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5446 param->fp_max_depth = 0;
5451 param->fp_comp_id = strtoul(optarg, &end, 0);
5452 if (*end != '\0' || param->fp_comp_id == 0 ||
5453 param->fp_comp_id > LCME_ID_MAX) {
5455 "error: %s bad component id '%s'\n",
5459 param->fp_check_comp_id = 1;
5461 param->fp_max_depth = 0;
5462 param->fp_verbose |= VERBOSE_COMP_ID;
5466 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5467 param->fp_verbose |= VERBOSE_PATTERN;
5468 param->fp_max_depth = 0;
5471 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5474 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5477 if (!(param->fp_verbose & VERBOSE_DETAIL))
5478 param->fp_max_depth = 0;
5479 param->fp_verbose |= VERBOSE_MDTINDEX;
5482 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5483 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5484 param->fp_max_depth = 0;
5488 if (param->fp_obd_uuid) {
5490 "error: %s: only one obduuid allowed",
5494 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5497 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5498 param->fp_verbose |= VERBOSE_POOL;
5499 param->fp_max_depth = 0;
5506 param->fp_recursive = 1;
5512 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5513 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5514 param->fp_max_depth = 0;
5518 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5524 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5525 param->fp_verbose |= VERBOSE_EXT_SIZE;
5526 param->fp_max_depth = 0;
5534 if (pathstart == -1) {
5535 fprintf(stderr, "error: %s: no filename|pathname\n",
5538 } else if (pathend == -1) {
5546 if (param->fp_recursive)
5547 param->fp_max_depth = -1;
5548 else if (param->fp_verbose & VERBOSE_DETAIL)
5549 param->fp_max_depth = 1;
5551 if (!param->fp_verbose)
5552 param->fp_verbose = VERBOSE_DEFAULT;
5553 if (param->fp_quiet)
5554 param->fp_verbose = VERBOSE_OBJID;
5557 rc = llapi_getstripe(argv[pathstart], param);
5558 } while (++pathstart < pathend && !rc);
5561 fprintf(stderr, "error: %s failed for %s.\n",
5562 argv[0], argv[optind - 1]);
5566 static int lfs_tgts(int argc, char **argv)
5568 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5569 struct find_param param;
5570 int index = 0, rc = 0;
5575 if (argc == 2 && !realpath(argv[1], path)) {
5577 fprintf(stderr, "error: invalid path '%s': %s\n",
5578 argv[1], strerror(-rc));
5582 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5583 /* Check if we have a mount point */
5584 if (mntdir[0] == '\0')
5587 memset(¶m, 0, sizeof(param));
5588 if (!strcmp(argv[0], "mdts"))
5589 param.fp_get_lmv = 1;
5591 rc = llapi_ostlist(mntdir, ¶m);
5593 fprintf(stderr, "error: %s: failed on %s\n",
5596 if (path[0] != '\0')
5598 memset(mntdir, 0, PATH_MAX);
5604 static int lfs_getstripe(int argc, char **argv)
5606 struct find_param param = { 0 };
5608 param.fp_max_depth = 1;
5609 return lfs_getstripe_internal(argc, argv, ¶m);
5613 static int lfs_getdirstripe(int argc, char **argv)
5615 struct find_param param = { 0 };
5616 struct option long_opts[] = {
5617 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5618 { .val = 'D', .name = "default", .has_arg = no_argument },
5619 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5620 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5621 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5622 { .val = 'O', .name = "obd", .has_arg = required_argument },
5623 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5624 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5625 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5626 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5630 param.fp_get_lmv = 1;
5632 while ((c = getopt_long(argc, argv,
5633 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5637 param.fp_verbose |= VERBOSE_COUNT;
5640 param.fp_get_default_lmv = 1;
5644 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5646 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5649 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5652 param.fp_verbose |= VERBOSE_HASH_TYPE;
5655 if (param.fp_obd_uuid) {
5657 "error: %s: only one obduuid allowed",
5661 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5664 param.fp_recursive = 1;
5667 param.fp_verbose |= VERBOSE_DETAIL;
5673 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5674 progname, argv[0], argv[optind - 1]);
5682 if (param.fp_recursive)
5683 param.fp_max_depth = -1;
5685 if (!param.fp_verbose)
5686 param.fp_verbose = VERBOSE_DEFAULT;
5689 rc = llapi_getstripe(argv[optind], ¶m);
5690 } while (++optind < argc && !rc);
5693 fprintf(stderr, "error: %s failed for %s.\n",
5694 argv[0], argv[optind - 1]);
5699 MNTDF_INODES = 0x0001,
5700 MNTDF_COOKED = 0x0002,
5701 MNTDF_LAZY = 0x0004,
5702 MNTDF_VERBOSE = 0x0008,
5703 MNTDF_SHOW = 0x0010,
5706 #define COOK(value) \
5709 while (value > 1024) { \
5717 #define CDF "%11llu"
5718 #define HDF "%8.1f%c"
5722 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5724 double avail, used, ratio = 0;
5727 avail = st->os_ffree;
5728 used = st->os_files - st->os_ffree;
5730 avail = st->os_bavail;
5731 used = st->os_blocks - st->os_bfree;
5733 if (avail + used > 0)
5734 ratio = used / (used + avail) * 100;
5736 /* Round up to match df(1) usage percentage */
5737 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5741 * This is to identify various problem states for "lfs df" if .osn_err = true,
5742 * so only show flags reflecting those states by default. Informational states
5743 * are only shown with "-v" and use lower-case names to distinguish them.
5744 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5746 static struct obd_statfs_state_names {
5747 enum obd_statfs_state osn_state;
5748 const char osn_name;
5751 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5752 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5753 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5754 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5755 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5756 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5757 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5758 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5759 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5762 static int showdf(char *mntdir, struct obd_statfs *stat,
5763 char *uuid, enum mntdf_flags flags,
5764 char *type, int index, int rc)
5766 long long avail, used, total;
5768 char *suffix = "KMGTPEZY";
5769 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5770 char tbuf[3 * sizeof(__u64)];
5771 char ubuf[3 * sizeof(__u64)];
5772 char abuf[3 * sizeof(__u64)];
5773 char rbuf[3 * sizeof(__u64)];
5780 if (flags & MNTDF_INODES) {
5781 avail = stat->os_ffree;
5782 used = stat->os_files - stat->os_ffree;
5783 total = stat->os_files;
5785 int shift = flags & MNTDF_COOKED ? 0 : 10;
5787 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5788 used = ((stat->os_blocks - stat->os_bfree) *
5789 stat->os_bsize) >> shift;
5790 total = (stat->os_blocks * stat->os_bsize) >> shift;
5793 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5795 if (flags & MNTDF_COOKED) {
5799 cook_val = (double)total;
5802 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5805 snprintf(tbuf, sizeof(tbuf), CDF, total);
5807 cook_val = (double)used;
5810 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5813 snprintf(ubuf, sizeof(ubuf), CDF, used);
5815 cook_val = (double)avail;
5818 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5821 snprintf(abuf, sizeof(abuf), CDF, avail);
5823 snprintf(tbuf, sizeof(tbuf), CDF, total);
5824 snprintf(ubuf, sizeof(tbuf), CDF, used);
5825 snprintf(abuf, sizeof(tbuf), CDF, avail);
5828 sprintf(rbuf, RDF, ratio);
5829 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5830 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5832 printf("[%s:%d]", type, index);
5834 if (stat->os_state) {
5838 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5839 if (oss_names[i].osn_state & stat->os_state &&
5840 (oss_names[i].osn_err ||
5841 flags & MNTDF_VERBOSE))
5842 printf("%c", oss_names[i].osn_name);
5849 printf(UUF": inactive device\n", uuid);
5852 printf(UUF": %s\n", uuid, strerror(-rc));
5859 struct ll_stat_type {
5864 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5866 struct ll_statfs_data {
5868 struct obd_statfs sd_st;
5871 struct ll_statfs_buf {
5873 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5876 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5877 int ops, struct ll_statfs_buf *lsb)
5879 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5880 struct obd_uuid uuid_buf;
5881 char *poolname = NULL;
5882 struct ll_stat_type types[] = {
5883 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5884 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5885 { .st_name = NULL } };
5886 struct ll_stat_type *tp;
5887 __u64 ost_files = 0;
5888 __u64 ost_ffree = 0;
5896 poolname = strchr(pool, '.');
5898 if (strncmp(fsname, pool, strlen(fsname))) {
5899 fprintf(stderr, "filesystem name incorrect\n");
5907 fd = open(mntdir, O_RDONLY);
5910 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5915 if (flags & MNTDF_SHOW) {
5916 if (flags & MNTDF_INODES)
5917 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5918 "UUID", "Inodes", "IUsed", "IFree",
5919 "IUse%", "Mounted on");
5921 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5923 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5924 "Used", "Available", "Use%", "Mounted on");
5927 for (tp = types; tp->st_name != NULL; tp++) {
5928 bool have_ost = false;
5930 if (!(tp->st_op & ops))
5933 for (index = 0; ; index++) {
5934 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5935 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5936 type = flags & MNTDF_LAZY ?
5937 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5938 rc2 = llapi_obd_fstatfs(fd, type, index,
5939 &stat_buf, &uuid_buf);
5944 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5945 if (!(flags & MNTDF_VERBOSE))
5947 } else if (rc2 < 0 && rc == 0) {
5952 * If we have OSTs then don't report MDT block counts.
5953 * For MDT-only filesystems the expectation is that all
5954 * layouts have a DoM component. For filesystems with
5955 * OSTs, files are not necessarily going to store data
5956 * on MDTs, and MDT space is limited to a fraction of
5957 * OST space, so don't include it in the summary.
5959 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5966 if (poolname && tp->st_op == LL_STATFS_LOV &&
5967 llapi_search_ost(fsname, poolname,
5968 obd_uuid2str(&uuid_buf)) != 1)
5972 * the llapi_obd_fstatfs() call may have returned with
5973 * an error, but if it filled in uuid_buf we will at
5974 * lease use that to print out a message for that OBD.
5975 * If we didn't get anything in the uuid_buf, then fill
5976 * it in so that we can print an error message.
5978 if (uuid_buf.uuid[0] == '\0')
5979 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5980 "%s%04x", tp->st_name, index);
5982 lsb->sb_buf[lsb->sb_count].sd_index = index;
5983 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5986 if (flags & MNTDF_SHOW)
5987 showdf(mntdir, &stat_buf,
5988 obd_uuid2str(&uuid_buf), flags,
5989 tp->st_name, index, rc2);
5994 if (tp->st_op == LL_STATFS_LMV) {
5995 sum.os_ffree += stat_buf.os_ffree;
5996 sum.os_files += stat_buf.os_files;
5997 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5998 ost_files += stat_buf.os_files;
5999 ost_ffree += stat_buf.os_ffree;
6001 sum.os_blocks += stat_buf.os_blocks *
6003 sum.os_bfree += stat_buf.os_bfree *
6005 sum.os_bavail += stat_buf.os_bavail *
6013 * If we have _some_ OSTs, but don't have as many free objects on the
6014 * OST as inodes on the MDTs, reduce the reported number of inodes
6015 * to compensate, so that the "inodes in use" number is correct.
6016 * This should be kept in sync with ll_statfs_internal().
6018 if (ost_files && ost_ffree < sum.os_ffree) {
6019 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6020 sum.os_ffree = ost_ffree;
6022 if (flags & MNTDF_SHOW) {
6024 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6032 static int lfs_setdirstripe(int argc, char **argv)
6035 struct lfs_setstripe_args lsa = { 0 };
6036 struct llapi_stripe_param *param = NULL;
6037 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6040 char *mode_opt = NULL;
6041 bool default_stripe = false;
6042 bool delete = false;
6043 bool foreign_mode = false;
6044 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6045 mode_t previous_mode = 0;
6047 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6048 struct option long_opts[] = {
6049 { .val = 'c', .name = "count", .has_arg = required_argument },
6050 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6051 { .val = 'd', .name = "delete", .has_arg = no_argument },
6052 { .val = 'D', .name = "default", .has_arg = no_argument },
6053 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6054 { .val = LFS_LAYOUT_FLAGS_OPT,
6055 .name = "flags", .has_arg = required_argument },
6056 { .val = LFS_LAYOUT_FOREIGN_OPT,
6057 .name = "foreign", .has_arg = optional_argument},
6058 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6059 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6060 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6061 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6063 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6064 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6065 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6067 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6068 { .val = 'i', .name = "index", .has_arg = required_argument },
6070 { .val = 'o', .name = "mode", .has_arg = required_argument },
6071 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6072 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6074 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6075 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6076 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6080 setstripe_args_init(&lsa);
6082 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6083 long_opts, NULL)) >= 0) {
6090 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6093 "%s %s: invalid stripe count '%s'\n",
6094 progname, argv[0], optarg);
6100 default_stripe = true;
6103 default_stripe = true;
6105 case LFS_LAYOUT_FOREIGN_OPT:
6107 /* check pure numeric */
6108 type = strtoul(optarg, &end, 0);
6111 type = check_foreign_type_name(optarg);
6112 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6114 "%s %s: unknown foreign type '%s'\n",
6121 foreign_mode = true;
6123 case LFS_LAYOUT_FLAGS_OPT:
6124 flags = strtoul(optarg, &end, 16);
6127 "%s %s: bad flags '%s'\n",
6128 progname, argv[0], optarg);
6132 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6135 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6138 lsa.lsa_pattern = check_hashtype(optarg);
6139 if (lsa.lsa_pattern == 0) {
6141 "%s %s: bad stripe hash type '%s'\n",
6142 progname, argv[0], optarg);
6147 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6150 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6151 if (strcmp(argv[optind - 1], "--index") == 0)
6153 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6156 lsa.lsa_nr_tgts = parse_targets(mdts,
6157 sizeof(mdts) / sizeof(__u32),
6158 lsa.lsa_nr_tgts, optarg, NULL);
6159 if (lsa.lsa_nr_tgts < 0) {
6161 "%s %s: invalid MDT target(s) '%s'\n",
6162 progname, argv[0], optarg);
6166 lsa.lsa_tgts = mdts;
6167 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6168 lsa.lsa_stripe_off = mdts[0];
6170 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6173 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6182 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6183 progname, argv[0], argv[optind - 1]);
6188 if (optind == argc) {
6189 fprintf(stderr, "%s %s: DIR must be specified\n",
6194 if (xattr && !foreign_mode) {
6196 * only print a warning as this is armless and will be
6200 "%s %s: xattr has been specified for non-foreign layout\n",
6202 } else if (foreign_mode && !xattr) {
6204 "%s %s: xattr must be provided in foreign mode\n",
6209 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6210 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6212 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6217 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6218 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6219 /* if no parameters set, create directory on least-used MDTs */
6220 lsa.lsa_stripe_off = -1;
6221 lsa.lsa_stripe_count = 1;
6225 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6226 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6228 "%s %s: cannot specify -d with -c or -i options\n",
6234 mode = strtoul(mode_opt, &end, 8);
6237 "%s %s: bad MODE '%s'\n",
6238 progname, argv[0], mode_opt);
6241 previous_mode = umask(0);
6244 /* foreign LMV/dir case */
6246 if (argc > optind + 1) {
6248 "%s %s: cannot specify multiple foreign dirs\n",
6253 dname = argv[optind];
6254 result = llapi_dir_create_foreign(dname, mode, type, flags,
6258 "%s mkdir: can't create foreign dir '%s': %s\n",
6259 progname, dname, strerror(-result));
6264 * initialize stripe parameters, in case param is converted to specific,
6265 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6267 param = calloc(1, offsetof(typeof(*param),
6268 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6269 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6272 "%s %s: cannot allocate memory for parameters: %s\n",
6273 progname, argv[0], strerror(ENOMEM));
6277 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6278 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6279 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6280 lsa.lsa_stripe_count == 0))
6281 lsa.lsa_stripe_count = 1;
6282 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6283 param->lsp_stripe_count = lsa.lsa_stripe_count;
6284 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6285 param->lsp_stripe_offset = -1;
6287 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6288 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6289 param->lsp_stripe_pattern = lsa.lsa_pattern;
6291 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6292 param->lsp_pool = lsa.lsa_pool_name;
6293 param->lsp_is_specific = false;
6294 if (lsa.lsa_nr_tgts > 1) {
6295 if (lsa.lsa_stripe_count > 0 &&
6296 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6297 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6299 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6300 argv[0], lsa.lsa_stripe_count,
6306 param->lsp_is_specific = true;
6307 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6308 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6311 dname = argv[optind];
6313 if (default_stripe) {
6314 result = llapi_dir_set_default_lmv(dname, param);
6317 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6318 progname, dname, strerror(-result));
6322 result = llapi_dir_create(dname, mode, param);
6325 "%s setdirstripe: cannot create dir '%s': %s\n",
6326 progname, dname, strerror(-result));
6327 } while (!result && (dname = argv[++optind]));
6330 umask(previous_mode);
6337 static int lfs_rmentry(int argc, char **argv)
6344 fprintf(stderr, "error: %s: missing dirname\n",
6350 dname = argv[index];
6352 result = llapi_direntry_remove(dname);
6355 "error: %s: remove dir entry '%s' failed\n",
6359 dname = argv[++index];
6364 static int lfs_unlink_foreign(int argc, char **argv)
6371 fprintf(stderr, "error: %s: missing pathname\n",
6378 while (name != NULL) {
6379 result = llapi_unlink_foreign(name);
6382 "error: %s: unlink foreign entry '%s' failed\n",
6386 name = argv[++index];
6391 static int lfs_mv(int argc, char **argv)
6393 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6394 struct find_param param = {
6401 struct option long_opts[] = {
6402 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6403 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6404 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6407 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6409 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6412 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6415 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6417 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6423 param.fp_verbose = VERBOSE_DETAIL;
6426 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6427 progname, argv[optind - 1]);
6432 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6433 fprintf(stderr, "%s mv: MDT index must be specified\n",
6438 if (optind >= argc) {
6439 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6443 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6445 /* initialize migrate mdt parameters */
6446 param.fp_lmv_md = &lmu;
6447 param.fp_migrate = 1;
6448 rc = llapi_migrate_mdt(argv[optind], ¶m);
6450 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6451 progname, argv[optind], lmu.lum_stripe_offset,
6456 static int lfs_osts(int argc, char **argv)
6458 return lfs_tgts(argc, argv);
6461 static int lfs_mdts(int argc, char **argv)
6463 return lfs_tgts(argc, argv);
6466 static int lfs_df(int argc, char **argv)
6468 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6469 enum mntdf_flags flags = MNTDF_SHOW;
6470 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6471 int c, rc = 0, index = 0;
6472 char fsname[PATH_MAX] = "", *pool_name = NULL;
6473 struct option long_opts[] = {
6474 { .val = 'h', .name = "human-readable",
6475 .has_arg = no_argument },
6476 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6477 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6478 { .val = 'p', .name = "pool", .has_arg = required_argument },
6479 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6482 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6485 flags |= MNTDF_COOKED;
6488 flags |= MNTDF_INODES;
6491 flags |= MNTDF_LAZY;
6497 flags |= MNTDF_VERBOSE;
6503 if (optind < argc && !realpath(argv[optind], path)) {
6505 fprintf(stderr, "error: invalid path '%s': %s\n",
6506 argv[optind], strerror(-rc));
6510 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6511 /* Check if we have a mount point */
6512 if (mntdir[0] == '\0')
6515 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6516 if (rc || path[0] != '\0')
6518 fsname[0] = '\0'; /* avoid matching in next loop */
6519 mntdir[0] = '\0'; /* avoid matching in next loop */
6525 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6526 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6530 if (opt_fsname == opt_instance) { /* both true or both false */
6531 rc = llapi_getname(mntdir, buf, buflen);
6532 } else if (opt_fsname) {
6534 * llapi_search_mounts() fills @buf with fsname, but that is not
6535 * called if explicit paths are specified on the command-line
6538 rc = llapi_get_fsname(mntdir, buf, buflen);
6539 } else /* if (opt_instance) */ {
6540 rc = llapi_get_instance(mntdir, buf, buflen);
6544 fprintf(stderr, "cannot get instance for '%s': %s\n",
6545 mntdir, strerror(-rc));
6550 printf("%s %s\n", buf, mntdir);
6552 printf("%s\n", buf);
6557 static int lfs_getname(int argc, char **argv)
6559 struct option long_opts[] = {
6560 { .val = 'h', .name = "help", .has_arg = no_argument },
6561 { .val = 'i', .name = "instance", .has_arg = no_argument },
6562 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6564 bool opt_instance = false, opt_fsname = false;
6565 char fsname[PATH_MAX] = "";
6568 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6571 opt_instance = true;
6582 if (optind == argc) { /* no paths specified, get all paths. */
6583 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6586 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6587 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6588 opt_instance, opt_fsname, true);
6591 path[0] = fsname[0] = mntdir[0] = '\0';
6593 } else { /* paths specified, only attempt to search these. */
6596 /* if only one path is given, print only requested info */
6597 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6599 for (; optind < argc; optind++) {
6600 rc2 = print_instance(argv[optind], fsname,
6601 sizeof(fsname), opt_instance,
6602 opt_fsname, opt_mntdir);
6612 static int lfs_check(int argc, char **argv)
6614 char mntdir[PATH_MAX] = {'\0'};
6623 fprintf(stderr, "%s check: server type must be specified\n",
6628 obd_types[0] = obd_type1;
6629 obd_types[1] = obd_type2;
6630 obd_types[2] = obd_type3;
6632 if (strcmp(argv[1], "osts") == 0) {
6633 strcpy(obd_types[0], "osc");
6634 } else if (strcmp(argv[1], "mdts") == 0 ||
6635 strcmp(argv[1], "mds") == 0) {
6636 strcpy(obd_types[0], "mdc");
6637 } else if (strcmp(argv[1], "mgts") == 0) {
6638 strcpy(obd_types[0], "mgc");
6639 } else if (strcmp(argv[1], "all") == 0 ||
6640 strcmp(argv[1], "servers") == 0) {
6642 strcpy(obd_types[0], "osc");
6643 strcpy(obd_types[1], "mdc");
6644 strcpy(obd_types[2], "mgc");
6646 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6651 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6652 if (rc < 0 || mntdir[0] == '\0') {
6654 "%s check: cannot find mounted Lustre filesystem: %s\n",
6655 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6659 rc = llapi_target_check(num_types, obd_types, mntdir);
6661 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6662 progname, argv[1], strerror(-rc));
6667 #ifdef HAVE_SYS_QUOTA_H
6668 #define ADD_OVERFLOW(a, b) \
6669 ((((a) + (b)) < (a)) ? \
6670 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6672 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6673 * returns the value or ULONG_MAX on integer overflow or incorrect format
6675 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6676 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6677 * 3. empty integer value is interpreted as 0
6679 static unsigned long str2sec(const char *timestr)
6681 const char spec[] = "smhdw";
6682 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6683 unsigned long val = 0;
6686 if (strpbrk(timestr, spec) == NULL) {
6688 * no specifiers inside the time string,
6689 * should treat it as an integer value
6691 val = strtoul(timestr, &tail, 10);
6692 return *tail ? ULONG_MAX : val;
6695 /* format string is XXwXXdXXhXXmXXs */
6701 v = strtoul(timestr, &tail, 10);
6702 if (v == ULONG_MAX || *tail == '\0')
6704 * value too large (ULONG_MAX or more)
6705 * or missing specifier
6709 ptr = strchr(spec, *tail);
6711 /* unknown specifier */
6716 /* check if product will overflow the type */
6717 if (!(v < ULONG_MAX / mult[ind]))
6720 ADD_OVERFLOW(val, mult[ind] * v);
6721 if (val == ULONG_MAX)
6733 #define ARG2ULL(nr, str, def_units) \
6735 unsigned long long limit, units = def_units; \
6738 rc = llapi_parse_size(str, &limit, &units, 1); \
6740 fprintf(stderr, "%s: invalid limit '%s'\n", \
6747 static inline int has_times_option(int argc, char **argv)
6751 for (i = 1; i < argc; i++)
6752 if (!strcmp(argv[i], "-t"))
6758 static inline int lfs_verify_poolarg(char *pool)
6760 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6762 "Pool name '%.*s' is longer than %d\n",
6763 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6769 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6772 char *mnt, *obd_type = (char *)qctl->obd_type;
6773 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6774 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6775 struct option long_opts[] = {
6776 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6777 { .val = 'g', .name = "group", .has_arg = no_argument },
6778 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6779 { .val = 'p', .name = "projid", .has_arg = no_argument },
6780 { .val = 't', .name = "times", .has_arg = no_argument },
6781 { .val = 'u', .name = "user", .has_arg = no_argument },
6782 { .val = LFS_POOL_OPT,
6783 .name = "pool", .has_arg = required_argument },
6787 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6788 qctl->qc_type = ALLQUOTA;
6790 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6791 long_opts, NULL)) != -1) {
6802 if (qctl->qc_type != ALLQUOTA) {
6804 "error: -u/g/p can't be used more than once\n");
6807 qctl->qc_type = qtype;
6810 if (strncmp(optarg, NOTIFY_GRACE,
6811 strlen(NOTIFY_GRACE)) == 0) {
6812 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6814 dqi->dqi_bgrace = str2sec(optarg);
6815 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6817 "error: bad block-grace: %s\n",
6822 dqb->dqb_valid |= QIF_BTIME;
6825 if (strncmp(optarg, NOTIFY_GRACE,
6826 strlen(NOTIFY_GRACE)) == 0) {
6827 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6829 dqi->dqi_igrace = str2sec(optarg);
6830 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6832 "error: bad inode-grace: %s\n",
6837 dqb->dqb_valid |= QIF_ITIME;
6839 case 't': /* Yes, of course! */
6842 if (lfs_verify_poolarg(optarg))
6845 "Trying to set grace for pool %s\n", optarg);
6846 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
6847 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
6849 /* getopt prints error message for us when opterr != 0 */
6855 if (qctl->qc_type == ALLQUOTA) {
6856 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6860 if (optind != argc - 1) {
6861 fprintf(stderr, "error: unexpected parameters encountered\n");
6866 rc = llapi_quotactl(mnt, qctl);
6869 fprintf(stderr, "%s %s ", obd_type,
6870 obd_uuid2str(&qctl->obd_uuid));
6871 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6878 #define BSLIMIT (1 << 0)
6879 #define BHLIMIT (1 << 1)
6880 #define ISLIMIT (1 << 2)
6881 #define IHLIMIT (1 << 3)
6883 int lfs_setquota(int argc, char **argv)
6886 struct if_quotactl *qctl;
6887 char *mnt, *obd_type;
6888 struct obd_dqblk *dqb;
6889 struct option long_opts[] = {
6890 { .val = 'b', .name = "block-softlimit",
6891 .has_arg = required_argument },
6892 { .val = 'B', .name = "block-hardlimit",
6893 .has_arg = required_argument },
6894 { .val = 'd', .name = "default", .has_arg = no_argument },
6895 { .val = 'g', .name = "group", .has_arg = required_argument },
6896 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6897 { .val = 'i', .name = "inode-softlimit",
6898 .has_arg = required_argument },
6899 { .val = 'I', .name = "inode-hardlimit",
6900 .has_arg = required_argument },
6901 { .val = 'p', .name = "projid", .has_arg = required_argument },
6902 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6903 { .val = 'u', .name = "user", .has_arg = required_argument },
6904 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6905 { .val = LFS_POOL_OPT,
6906 .name = "pool", .has_arg = required_argument },
6908 unsigned int limit_mask = 0;
6909 bool use_default = false;
6910 int qtype, qctl_len;
6912 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
6913 qctl = malloc(qctl_len);
6917 memset(qctl, 0, qctl_len);
6918 obd_type = (char *)qctl->obd_type;
6919 dqb = &qctl->qc_dqblk;
6921 if (has_times_option(argc, argv)) {
6922 rc = lfs_setquota_times(argc, argv, qctl);
6926 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
6927 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6928 * so it can be used as a marker that qc_type
6929 * isn't reinitialized from command line
6932 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6933 long_opts, NULL)) != -1) {
6936 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6939 goto quota_type_def;
6942 rc = name2uid(&qctl->qc_id, optarg);
6945 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6948 goto quota_type_def;
6951 rc = name2gid(&qctl->qc_id, optarg);
6954 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6957 goto quota_type_def;
6960 rc = name2projid(&qctl->qc_id, optarg);
6963 if (str2quotaid(&qctl->qc_id, optarg)) {
6965 "%s setquota: invalid id '%s'\n",
6972 if (qctl->qc_id == 0) {
6974 "%s setquota: can't set quota for root usr/group/project.\n",
6981 if (qctl->qc_type != ALLQUOTA) {
6983 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
6988 qctl->qc_type = qtype;
6991 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6995 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6996 dqb->dqb_bsoftlimit >>= 10;
6997 limit_mask |= BSLIMIT;
6998 if (dqb->dqb_bsoftlimit &&
6999 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7001 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7003 (unsigned long long)dqb->dqb_bsoftlimit,
7007 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7008 dqb->dqb_bhardlimit >>= 10;
7009 limit_mask |= BHLIMIT;
7010 if (dqb->dqb_bhardlimit &&
7011 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7013 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7014 "See '%s help setquota' or Lustre manual for details\n",
7016 (unsigned long long)dqb->dqb_bhardlimit,
7020 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7021 limit_mask |= ISLIMIT;
7022 if (dqb->dqb_isoftlimit &&
7023 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7025 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7027 (unsigned long long)dqb->dqb_isoftlimit,
7031 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7032 limit_mask |= IHLIMIT;
7033 if (dqb->dqb_ihardlimit &&
7034 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7036 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7038 (unsigned long long)dqb->dqb_ihardlimit,
7042 if (lfs_verify_poolarg(optarg)) {
7047 "Trying to set quota for pool %s\n", optarg);
7048 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7049 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7053 "%s setquota: unrecognized option '%s'\n",
7054 progname, argv[optind - 1]);
7060 if (qctl->qc_type == ALLQUOTA) {
7062 "%s setquota: either -u or -g must be specified\n",
7068 if (!use_default && limit_mask == 0) {
7070 "%s setquota: at least one limit must be specified\n",
7076 if (use_default && limit_mask != 0) {
7078 "%s setquota: limits should not be specified when using default quota\n",
7084 if (use_default && qctl->qc_id == 0) {
7086 "%s setquota: can not set default quota for root user/group/project\n",
7092 if (optind != argc - 1) {
7094 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7095 progname, argv[optind]);
7103 dqb->dqb_bhardlimit = 0;
7104 dqb->dqb_bsoftlimit = 0;
7105 dqb->dqb_ihardlimit = 0;
7106 dqb->dqb_isoftlimit = 0;
7109 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7110 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7111 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7112 /* sigh, we can't just set blimits/ilimits */
7113 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7114 .qc_type = qctl->qc_type,
7115 .qc_id = qctl->qc_id};
7117 rc = llapi_quotactl(mnt, &tmp_qctl);
7121 if (!(limit_mask & BHLIMIT))
7122 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7123 if (!(limit_mask & BSLIMIT))
7124 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7125 if (!(limit_mask & IHLIMIT))
7126 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7127 if (!(limit_mask & ISLIMIT))
7128 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7130 /* Keep grace times if we have got no softlimit arguments */
7131 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7132 dqb->dqb_valid |= QIF_BTIME;
7133 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7136 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7137 dqb->dqb_valid |= QIF_ITIME;
7138 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7142 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7143 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7145 rc = llapi_quotactl(mnt, qctl);
7149 "%s setquota: cannot quotactl '%s' '%s': %s",
7151 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7158 /* Converts seconds value into format string
7159 * result is returned in buf
7161 * 1. result is in descenting order: 1w2d3h4m5s
7162 * 2. zero fields are not filled (except for p. 3): 5d1s
7163 * 3. zero seconds value is presented as "0s"
7165 static char *__sec2str(time_t seconds, char *buf)
7167 const char spec[] = "smhdw";
7168 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7173 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7174 c = seconds / mult[i];
7176 if (c > 0 || (i == 0 && buf == tail))
7177 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7186 static void sec2str(time_t seconds, char *buf, int rc)
7193 tail = __sec2str(seconds, tail);
7195 if (rc && tail - buf < 39) {
7201 static void diff2str(time_t seconds, char *buf, time_t now)
7206 if (seconds <= now) {
7207 strcpy(buf, "none");
7210 __sec2str(seconds - now, buf);
7213 static void print_quota_title(char *name, struct if_quotactl *qctl,
7214 bool human_readable, bool show_default)
7217 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7218 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7219 "Filesystem", "bquota", "blimit", "bgrace",
7220 "iquota", "ilimit", "igrace");
7222 printf("Disk quotas for %s %s (%cid %u):\n",
7223 qtype_name(qctl->qc_type), name,
7224 *qtype_name(qctl->qc_type), qctl->qc_id);
7225 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7226 "Filesystem", human_readable ? "used" : "kbytes",
7227 "quota", "limit", "grace",
7228 "files", "quota", "limit", "grace");
7232 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7235 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7238 snprintf(buf, buflen, "%5.4gP",
7239 (double)num / ((__u64)1 << 40));
7241 snprintf(buf, buflen, "%5.4gT",
7242 (double)num / (1 << 30));
7244 snprintf(buf, buflen, "%5.4gG",
7245 (double)num / (1 << 20));
7247 snprintf(buf, buflen, "%5.4gM",
7248 (double)num / (1 << 10));
7250 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7254 #define STRBUF_LEN 32
7255 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7256 int rc, bool h, bool show_default)
7262 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7263 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7264 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7265 int bover = 0, iover = 0;
7266 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7267 char numbuf[3][STRBUF_LEN];
7269 char strbuf[STRBUF_LEN];
7271 if (dqb->dqb_bhardlimit &&
7272 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7274 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7275 if (dqb->dqb_btime > now)
7281 if (dqb->dqb_ihardlimit &&
7282 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7284 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7285 if (dqb->dqb_itime > now)
7291 if (strlen(mnt) > 15)
7292 printf("%s\n%15s", mnt, "");
7294 printf("%15s", mnt);
7297 diff2str(dqb->dqb_btime, timebuf, now);
7298 else if (show_default)
7299 snprintf(timebuf, sizeof(timebuf), "%llu",
7300 (unsigned long long)dqb->dqb_btime);
7302 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7303 strbuf, sizeof(strbuf), h);
7304 if (rc == -EREMOTEIO)
7305 sprintf(numbuf[0], "%s*", strbuf);
7307 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7308 "%s" : "[%s]", strbuf);
7310 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7311 if (type == QC_GENERAL)
7312 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7313 "%s" : "[%s]", strbuf);
7315 sprintf(numbuf[1], "%s", "-");
7317 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7318 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7319 "%s" : "[%s]", strbuf);
7322 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7324 printf(" %7s%c %6s %7s %7s",
7325 numbuf[0], bover ? '*' : ' ', numbuf[1],
7326 numbuf[2], bover > 1 ? timebuf : "-");
7329 diff2str(dqb->dqb_itime, timebuf, now);
7330 else if (show_default)
7331 snprintf(timebuf, sizeof(timebuf), "%llu",
7332 (unsigned long long)dqb->dqb_itime);
7334 snprintf(numbuf[0], sizeof(numbuf),
7335 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7336 (uintmax_t)dqb->dqb_curinodes);
7338 if (type == QC_GENERAL)
7339 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7341 (uintmax_t)dqb->dqb_isoftlimit);
7343 sprintf(numbuf[1], "%s", "-");
7345 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7346 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7349 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7350 else if (type != QC_OSTIDX)
7351 printf(" %7s%c %6s %7s %7s",
7352 numbuf[0], iover ? '*' : ' ', numbuf[1],
7353 numbuf[2], iover > 1 ? timebuf : "-");
7355 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7357 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7358 qctl->qc_cmd == Q_GETOINFO) {
7362 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7363 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7365 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7366 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7367 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7369 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7371 printf("Block grace time: %s; Inode grace time: %s\n",
7372 bgtimebuf, igtimebuf);
7376 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7380 /* format is "lustre-OST0001" */
7381 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7383 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7388 *idx = strtoul(dash, &endp, 16);
7389 if (*idx > 0xffff) {
7390 fprintf(stderr, "wrong index %s\n", tgtname);
7397 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7398 bool h, __u64 *total)
7400 int rc = 0, rc1 = 0, count = 0, i = 0;
7401 char **list = NULL, *buffer = NULL;
7402 __u32 valid = qctl->qc_valid;
7404 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7407 /* Is it correct for the case OST0000, OST0002, OST0003 -
7408 * we will ask OST0001 that is absent and won't ask OST0003? */
7409 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7411 fprintf(stderr, "can not get %s count: %s\n",
7412 is_mdt ? "mdt" : "ost", strerror(-rc));
7416 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7417 char fname[PATH_MAX];
7418 char fsname[LUSTRE_MAXFSNAME + 1];
7419 int bufsize = sizeof(struct obd_uuid) * count;
7421 rc = llapi_search_fsname(mnt, fsname);
7423 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7427 buffer = malloc(bufsize + sizeof(*list) * count);
7430 list = (char **)(buffer + bufsize);
7431 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7432 count = llapi_get_poolmembers(fname, list, count,
7438 for (i = 0; i < count; i++) {
7439 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7442 if (tgt_name2index(list[i], &index))
7444 qctl->qc_idx = index;
7449 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7450 rc = llapi_quotactl(mnt, qctl);
7452 /* It is remote client case. */
7453 if (rc == -EOPNOTSUPP) {
7460 fprintf(stderr, "quotactl %s%d failed.\n",
7461 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7465 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7466 qctl->qc_valid, 0, h, false);
7467 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7468 qctl->qc_dqblk.dqb_bhardlimit;
7473 qctl->qc_valid = valid;
7477 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7478 int verbose, int quiet, bool human_readable,
7481 int rc1 = 0, rc2 = 0, rc3 = 0;
7482 char *obd_type = (char *)qctl->obd_type;
7483 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7484 __u64 total_ialloc = 0, total_balloc = 0;
7485 bool use_default_for_blk = false;
7486 bool use_default_for_file = false;
7489 rc1 = llapi_quotactl(mnt, qctl);
7493 fprintf(stderr, "%s quotas are not enabled.\n",
7494 qtype_name(qctl->qc_type));
7497 fprintf(stderr, "Permission denied.\n");
7500 /* We already got error message. */
7503 fprintf(stderr, "Unexpected quotactl error: %s\n",
7508 if (!show_default && qctl->qc_id == 0) {
7509 qctl->qc_dqblk.dqb_bhardlimit = 0;
7510 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7511 qctl->qc_dqblk.dqb_ihardlimit = 0;
7512 qctl->qc_dqblk.dqb_isoftlimit = 0;
7513 qctl->qc_dqblk.dqb_btime = 0;
7514 qctl->qc_dqblk.dqb_itime = 0;
7515 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7518 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7519 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7520 use_default_for_blk = true;
7521 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7524 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7525 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7526 use_default_for_file = true;
7527 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7530 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7531 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7532 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7533 print_quota_title(name, qctl, human_readable, show_default);
7535 if (rc1 && *obd_type)
7536 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7538 if (qctl->qc_valid != QC_GENERAL)
7541 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7542 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7543 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7544 (QIF_LIMITS|QIF_USAGE));
7546 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7548 if (!show_default && verbose &&
7549 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7550 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7551 char strbuf[STRBUF_LEN];
7553 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7555 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7557 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7559 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7560 (uintmax_t)total_ialloc, strbuf);
7563 if (use_default_for_blk)
7564 printf("%cid %u is using default block quota setting\n",
7565 *qtype_name(qctl->qc_type), qctl->qc_id);
7567 if (use_default_for_file)
7568 printf("%cid %u is using default file quota setting\n",
7569 *qtype_name(qctl->qc_type), qctl->qc_id);
7571 if (rc1 || rc2 || rc3 || inacc)
7572 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7586 static int lfs_project(int argc, char **argv)
7588 int ret = 0, err = 0, c, i;
7589 struct project_handle_control phc = { 0 };
7590 enum lfs_project_ops_t op;
7593 phc.assign_projid = false;
7594 /* default action */
7595 op = LFS_PROJECT_LIST;
7597 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7600 if (op != LFS_PROJECT_LIST) {
7602 "%s: cannot specify '-c' '-C' '-s' together\n",
7607 op = LFS_PROJECT_CHECK;
7610 if (op != LFS_PROJECT_LIST) {
7612 "%s: cannot specify '-c' '-C' '-s' together\n",
7617 op = LFS_PROJECT_CLEAR;
7620 if (op != LFS_PROJECT_LIST) {
7622 "%s: cannot specify '-c' '-C' '-s' together\n",
7627 phc.set_inherit = true;
7628 op = LFS_PROJECT_SET;
7634 phc.keep_projid = true;
7637 phc.recursive = true;
7640 if (str2quotaid(&phc.projid, optarg)) {
7642 "Invalid project ID: %s\n",
7647 phc.assign_projid = true;
7651 phc.newline = false;
7654 fprintf(stderr, "%s: invalid option '%c'\n",
7660 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7661 op = LFS_PROJECT_SET;
7662 phc.set_projid = true;
7663 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7664 phc.set_projid = true;
7668 case LFS_PROJECT_CHECK:
7669 if (phc.keep_projid) {
7671 "%s: '-k' is useless together with '-c'\n",
7676 case LFS_PROJECT_CLEAR:
7679 "%s: '-0' is useless together with '-C'\n",
7683 if (phc.assign_projid) {
7685 "%s: '-p' is useless together with '-C'\n",
7690 case LFS_PROJECT_SET:
7693 "%s: '-0' is useless together with '-s'\n",
7697 if (phc.keep_projid) {
7699 "%s: '-k' is useless together with '-s'\n",
7707 "%s: '-0' is useless for list operations\n",
7717 fprintf(stderr, "%s: missing file or directory target(s)\n",
7722 for (i = 0; i < argc; i++) {
7724 case LFS_PROJECT_CHECK:
7725 err = lfs_project_check(argv[i], &phc);
7727 case LFS_PROJECT_LIST:
7728 err = lfs_project_list(argv[i], &phc);
7730 case LFS_PROJECT_CLEAR:
7731 err = lfs_project_clear(argv[i], &phc);
7733 case LFS_PROJECT_SET:
7734 err = lfs_project_set(argv[i], &phc);
7746 static int lfs_quota(int argc, char **argv)
7749 char *mnt, *name = NULL;
7750 struct if_quotactl *qctl;
7752 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7753 __u32 valid = QC_GENERAL, idx = 0;
7754 bool human_readable = false;
7755 bool show_default = false;
7757 struct option long_opts[] = {
7758 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
7761 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7765 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7766 qctl->qc_type = ALLQUOTA;
7767 obd_uuid = (char *)qctl->obd_uuid.uuid;
7769 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7770 long_opts, NULL)) != -1) {
7773 show_default = true;
7778 show_default = true;
7783 show_default = true;
7787 if (qctl->qc_type != ALLQUOTA) {
7789 "%s quota: only one of -u, -g, or -p may be specified\n",
7794 qctl->qc_type = qtype;
7797 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7800 valid = qctl->qc_valid = QC_UUID;
7801 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
7804 valid = qctl->qc_valid = QC_MDTIDX;
7805 idx = qctl->qc_idx = atoi(optarg);
7806 if (idx == 0 && *optarg != '0') {
7808 "%s quota: invalid MDT index '%s'\n",
7815 valid = qctl->qc_valid = QC_OSTIDX;
7816 idx = qctl->qc_idx = atoi(optarg);
7817 if (idx == 0 && *optarg != '0') {
7819 "%s quota: invalid OST index '%s'\n",
7832 human_readable = true;
7835 if (lfs_verify_poolarg(optarg)) {
7839 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7840 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
7841 LUSTRE_Q_GETINFOPOOL :
7842 LUSTRE_Q_GETQUOTAPOOL;
7845 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7846 progname, argv[optind - 1]);
7852 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7853 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7854 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7855 qctl->qc_type == ALLQUOTA &&
7856 optind == argc - 1 && !show_default) {
7859 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7860 qctl->qc_type = qtype;
7861 qctl->qc_valid = valid;
7862 if (qtype == USRQUOTA) {
7863 qctl->qc_id = geteuid();
7864 rc = uid2name(&name, qctl->qc_id);
7866 qctl->qc_id = getegid();
7867 rc = gid2name(&name, qctl->qc_id);
7868 memset(&qctl->qc_dqblk, 0,
7869 sizeof(qctl->qc_dqblk));
7874 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
7875 human_readable, show_default);
7880 /* lfs quota -u username /path/to/lustre/mount */
7881 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7882 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7883 /* options should be followed by u/g-name and mntpoint */
7884 if ((!show_default && optind + 2 != argc) ||
7885 (show_default && optind + 1 != argc) ||
7886 qctl->qc_type == ALLQUOTA) {
7888 "%s quota: name and mount point must be specified\n",
7894 if (!show_default) {
7895 name = argv[optind++];
7896 switch (qctl->qc_type) {
7898 rc = name2uid(&qctl->qc_id, name);
7901 rc = name2gid(&qctl->qc_id, name);
7904 rc = name2projid(&qctl->qc_id, name);
7911 qctl->qc_valid = QC_GENERAL;
7912 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
7917 if (str2quotaid(&qctl->qc_id, name)) {
7918 fprintf(stderr, "%s quota: invalid id '%s'\n",
7924 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
7925 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7932 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
7933 human_readable, show_default);
7938 #endif /* HAVE_SYS_QUOTA_H! */
7940 static int flushctx_ioctl(char *mp)
7944 fd = open(mp, O_RDONLY);
7946 fprintf(stderr, "flushctx: error open %s: %s\n",
7947 mp, strerror(errno));
7951 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7953 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7954 mp, strerror(errno));
7960 static int lfs_flushctx(int argc, char **argv)
7962 int kdestroy = 0, reap = 0, c;
7963 char mntdir[PATH_MAX] = {'\0'};
7967 while ((c = getopt(argc, argv, "kr")) != -1) {
7977 "error: %s: option '-%c' unrecognized\n",
7984 rc = system("kdestroy > /dev/null");
7986 rc = WEXITSTATUS(rc);
7988 "error destroying tickets: %d, continuing\n",
7993 if (optind >= argc) {
7994 /* flush for all mounted lustre fs. */
7995 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7996 /* Check if we have a mount point */
7997 if (mntdir[0] == '\0')
8000 if (flushctx_ioctl(mntdir))
8003 mntdir[0] = '\0'; /* avoid matching in next loop */
8006 /* flush fs as specified */
8007 while (optind < argc) {
8008 if (flushctx_ioctl(argv[optind++]))
8014 rc = system("keyctl reap > /dev/null");
8016 rc = WEXITSTATUS(rc);
8017 fprintf(stderr, "error reaping keyring: %d\n", rc);
8024 static int lfs_changelog(int argc, char **argv)
8026 void *changelog_priv;
8027 struct changelog_rec *rec;
8028 long long startrec = 0, endrec = 0;
8030 struct option long_opts[] = {
8031 { .val = 'f', .name = "follow", .has_arg = no_argument },
8033 char short_opts[] = "f";
8036 while ((rc = getopt_long(argc, argv, short_opts,
8037 long_opts, NULL)) != -1) {
8044 "%s changelog: unrecognized option '%s'\n",
8045 progname, argv[optind - 1]);
8049 if (optind >= argc) {
8050 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8055 mdd = argv[optind++];
8057 startrec = strtoll(argv[optind++], NULL, 10);
8059 endrec = strtoll(argv[optind++], NULL, 10);
8061 rc = llapi_changelog_start(&changelog_priv,
8062 CHANGELOG_FLAG_BLOCK |
8063 CHANGELOG_FLAG_JOBID |
8064 CHANGELOG_FLAG_EXTRA_FLAGS |
8065 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8068 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8069 progname, strerror(errno = -rc));
8073 rc = llapi_changelog_set_xflags(changelog_priv,
8074 CHANGELOG_EXTRA_FLAG_UIDGID |
8075 CHANGELOG_EXTRA_FLAG_NID |
8076 CHANGELOG_EXTRA_FLAG_OMODE |
8077 CHANGELOG_EXTRA_FLAG_XATTR);
8080 "%s changelog: cannot set xflags for changelog: %s\n",
8081 progname, strerror(errno = -rc));
8085 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8089 if (endrec && rec->cr_index > endrec) {
8090 llapi_changelog_free(&rec);
8093 if (rec->cr_index < startrec) {
8094 llapi_changelog_free(&rec);
8098 secs = rec->cr_time >> 30;
8099 gmtime_r(&secs, &ts);
8100 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8101 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8102 changelog_type2str(rec->cr_type),
8103 ts.tm_hour, ts.tm_min, ts.tm_sec,
8104 (int)(rec->cr_time & ((1 << 30) - 1)),
8105 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8106 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8108 if (rec->cr_flags & CLF_JOBID) {
8109 struct changelog_ext_jobid *jid =
8110 changelog_rec_jobid(rec);
8112 if (jid->cr_jobid[0] != '\0')
8113 printf(" j=%s", jid->cr_jobid);
8116 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8117 struct changelog_ext_extra_flags *ef =
8118 changelog_rec_extra_flags(rec);
8120 printf(" ef=0x%llx",
8121 (unsigned long long)ef->cr_extra_flags);
8123 if (ef->cr_extra_flags & CLFE_UIDGID) {
8124 struct changelog_ext_uidgid *uidgid =
8125 changelog_rec_uidgid(rec);
8127 printf(" u=%llu:%llu",
8128 (unsigned long long)uidgid->cr_uid,
8129 (unsigned long long)uidgid->cr_gid);
8131 if (ef->cr_extra_flags & CLFE_NID) {
8132 struct changelog_ext_nid *nid =
8133 changelog_rec_nid(rec);
8136 libcfs_nid2str(nid->cr_nid));
8139 if (ef->cr_extra_flags & CLFE_OPEN) {
8140 struct changelog_ext_openmode *omd =
8141 changelog_rec_openmode(rec);
8142 char mode[] = "---";
8144 /* exec mode must be exclusive */
8145 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8148 if (omd->cr_openflags & MDS_FMODE_READ)
8150 if (omd->cr_openflags &
8157 if (strcmp(mode, "---") != 0)
8158 printf(" m=%s", mode);
8161 if (ef->cr_extra_flags & CLFE_XATTR) {
8162 struct changelog_ext_xattr *xattr =
8163 changelog_rec_xattr(rec);
8165 if (xattr->cr_xattr[0] != '\0')
8166 printf(" x=%s", xattr->cr_xattr);
8170 if (!fid_is_zero(&rec->cr_pfid))
8171 printf(" p="DFID, PFID(&rec->cr_pfid));
8172 if (rec->cr_namelen)
8173 printf(" %.*s", rec->cr_namelen,
8174 changelog_rec_name(rec));
8176 if (rec->cr_flags & CLF_RENAME) {
8177 struct changelog_ext_rename *rnm =
8178 changelog_rec_rename(rec);
8180 if (!fid_is_zero(&rnm->cr_sfid))
8181 printf(" s="DFID" sp="DFID" %.*s",
8182 PFID(&rnm->cr_sfid),
8183 PFID(&rnm->cr_spfid),
8184 (int)changelog_rec_snamelen(rec),
8185 changelog_rec_sname(rec));
8189 llapi_changelog_free(&rec);
8192 llapi_changelog_fini(&changelog_priv);
8195 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8196 progname, strerror(errno = -rc));
8198 return (rc == 1 ? 0 : rc);
8201 static int lfs_changelog_clear(int argc, char **argv)
8209 endrec = strtoll(argv[3], NULL, 10);
8211 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8214 fprintf(stderr, "%s: record out of range: %llu\n",
8216 else if (rc == -ENOENT)
8217 fprintf(stderr, "%s: no changelog user: %s\n",
8220 fprintf(stderr, "%s error: %s\n", argv[0],
8229 static int lfs_fid2path(int argc, char **argv)
8231 struct option long_opts[] = {
8232 { .val = 'c', .name = "cur", .has_arg = no_argument },
8233 { .val = 'l', .name = "link", .has_arg = required_argument },
8234 { .val = 'r', .name = "rec", .has_arg = required_argument },
8236 char short_opts[] = "cl:r:";
8237 char mntdir[PATH_MAX];
8238 char *device, *fid, *path, *rootpath;
8239 long long recno = -1;
8244 char *endptr = NULL;
8246 while ((rc = getopt_long(argc, argv, short_opts,
8247 long_opts, NULL)) != -1) {
8253 linkno = strtol(optarg, &endptr, 10);
8254 if (*endptr != '\0') {
8256 "%s fid2path: invalid linkno '%s'\n",
8262 recno = strtoll(optarg, &endptr, 10);
8263 if (*endptr != '\0') {
8265 "%s fid2path: invalid recno '%s'\n",
8272 "%s fid2path: unrecognized option '%s'\n",
8273 progname, argv[optind - 1]);
8280 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
8285 device = argv[optind++];
8286 path = calloc(1, PATH_MAX);
8290 "%s fid2path: cannot allocate memory for path: %s\n",
8291 progname, strerror(-rc));
8296 /* in case that device is not the mountpoint */
8297 if (device[0] == '/') {
8298 rc = llapi_search_mounts(device, 0, mntdir, NULL);
8303 "%s fid2path: %s has no mountpoint: %s\n",
8304 progname, device, strerror(-rc));
8308 while (optind < argc) {
8309 fid = argv[optind++];
8311 lnktmp = (linkno >= 0) ? linkno : 0;
8313 int oldtmp = lnktmp;
8314 long long rectmp = recno;
8317 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
8321 "%s fid2path: cannot find %s %s: %s\n",
8322 progname, device, fid,
8323 strerror(errno = -rc2));
8330 fprintf(stdout, "%lld ", rectmp);
8331 if (device[0] == '/') {
8332 fprintf(stdout, "%s", rootpath);
8333 if (rootpath[strlen(rootpath) - 1] != '/')
8334 fprintf(stdout, "/");
8335 } else if (path[0] == '\0') {
8336 fprintf(stdout, "/");
8338 fprintf(stdout, "%s\n", path);
8341 /* specified linkno */
8343 if (oldtmp == lnktmp)
8353 static int lfs_path2fid(int argc, char **argv)
8355 struct option long_opts[] = {
8356 { .val = 'p', .name = "parents", .has_arg = no_argument },
8359 const char short_opts[] = "p";
8360 const char *sep = "";
8363 bool show_parents = false;
8365 while ((rc = getopt_long(argc, argv, short_opts,
8366 long_opts, NULL)) != -1) {
8369 show_parents = true;
8373 "%s path2fid: unrecognized option '%s'\n",
8374 progname, argv[optind - 1]);
8379 if (optind > argc - 1) {
8380 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8383 } else if (optind < argc - 1) {
8388 for (path = argv + optind; *path != NULL; path++) {
8391 if (!show_parents) {
8392 err = llapi_path2fid(*path, &fid);
8394 printf("%s%s"DFID"\n",
8395 *sep != '\0' ? *path : "", sep,
8398 char name[NAME_MAX + 1];
8399 unsigned int linkno = 0;
8401 while ((err = llapi_path2parent(*path, linkno, &fid,
8402 name, sizeof(name))) == 0) {
8403 if (*sep != '\0' && linkno == 0)
8404 printf("%s%s", *path, sep);
8406 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8411 /* err == -ENODATA is end-of-loop */
8412 if (linkno > 0 && err == -ENODATA) {
8420 "%s path2fid: cannot get %sfid for '%s': %s\n",
8421 progname, show_parents ? "parent " : "", *path,
8433 #define MAX_ERRNO 4095
8434 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8436 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8440 rc = llapi_rmfid(device, fa);
8442 fprintf(stderr, "rmfid(): rc = %d\n", rc);
8446 for (k = 0; k < fa->fa_nr; k++) {
8447 rc = (__s32)fa->fa_fids[k].f_ver;
8448 if (!IS_ERR_VALUE(rc))
8454 fa->fa_fids[k].f_ver = 0;
8455 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
8456 PFID(&fa->fa_fids[k]), rc);
8462 static int lfs_rmfid(int argc, char **argv)
8464 char *fidstr, *device;
8465 int rc = 0, rc2, nr;
8466 struct fid_array *fa;
8468 if (optind > argc - 1) {
8469 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8473 device = argv[optind++];
8476 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8482 while (optind < argc) {
8485 fidstr = argv[optind++];
8486 while (*fidstr == '[')
8488 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8490 fprintf(stderr, "unrecognized FID: %s\n",
8495 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8496 /* start another batch */
8497 rc2 = lfs_rmfid_and_show_errors(device, fa);
8504 rc2 = lfs_rmfid_and_show_errors(device, fa);
8512 static int lfs_data_version(int argc, char **argv)
8519 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8522 fprintf(stderr, "%s data_version: FILE must be specified\n",
8527 while ((c = getopt(argc, argv, "nrw")) != -1) {
8530 data_version_flags = 0;
8533 data_version_flags |= LL_DV_RD_FLUSH;
8536 data_version_flags |= LL_DV_WR_FLUSH;
8540 "%s data_version: unrecognized option '%s'\n",
8541 progname, argv[optind - 1]);
8545 if (optind == argc) {
8546 fprintf(stderr, "%s data_version: FILE must be specified\n",
8551 path = argv[optind];
8552 fd = open(path, O_RDONLY);
8555 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8556 progname, path, strerror(-rc));
8560 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8563 "%s data_version: cannot get version for '%s': %s\n",
8564 progname, path, strerror(-rc));
8566 printf("%ju" "\n", (uintmax_t)data_version);
8572 static int lfs_hsm_state(int argc, char **argv)
8577 struct hsm_user_state hus;
8585 rc = llapi_hsm_state_get(path, &hus);
8587 fprintf(stderr, "can't get hsm state for %s: %s\n",
8588 path, strerror(errno = -rc));
8592 /* Display path name and status flags */
8593 printf("%s: (0x%08x)", path, hus.hus_states);
8595 if (hus.hus_states & HS_RELEASED)
8596 printf(" released");
8597 if (hus.hus_states & HS_EXISTS)
8599 if (hus.hus_states & HS_DIRTY)
8601 if (hus.hus_states & HS_ARCHIVED)
8602 printf(" archived");
8603 /* Display user-settable flags */
8604 if (hus.hus_states & HS_NORELEASE)
8605 printf(" never_release");
8606 if (hus.hus_states & HS_NOARCHIVE)
8607 printf(" never_archive");
8608 if (hus.hus_states & HS_LOST)
8609 printf(" lost_from_hsm");
8611 if (hus.hus_archive_id != 0)
8612 printf(", archive_id:%d", hus.hus_archive_id);
8615 } while (++i < argc);
8620 #define LFS_HSM_SET 0
8621 #define LFS_HSM_CLEAR 1
8624 * Generic function to set or clear HSM flags.
8625 * Used by hsm_set and hsm_clear.
8627 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8629 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8631 struct option long_opts[] = {
8632 { .val = 'A', .name = "archived", .has_arg = no_argument },
8633 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8634 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8635 { .val = 'e', .name = "exists", .has_arg = no_argument },
8636 { .val = 'l', .name = "lost", .has_arg = no_argument },
8637 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8638 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8640 char short_opts[] = "lraAdei:";
8644 __u32 archive_id = 0;
8650 while ((c = getopt_long(argc, argv, short_opts,
8651 long_opts, NULL)) != -1) {
8657 mask |= HS_NOARCHIVE;
8660 mask |= HS_ARCHIVED;
8663 mask |= HS_NORELEASE;
8672 archive_id = strtol(optarg, &end, 10);
8674 fprintf(stderr, "invalid archive_id: '%s'\n",
8682 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8683 argv[0], argv[optind - 1]);
8688 /* User should have specified a flag */
8692 while (optind < argc) {
8693 path = argv[optind];
8695 /* If mode == 0, this means we apply the mask. */
8696 if (mode == LFS_HSM_SET)
8697 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8699 rc = llapi_hsm_state_set(path, 0, mask, 0);
8702 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8703 path, strerror(errno = -rc));
8712 static int lfs_hsm_action(int argc, char **argv)
8717 struct hsm_current_action hca;
8718 struct hsm_extent he;
8719 enum hsm_user_action hua;
8720 enum hsm_progress_states hps;
8728 rc = llapi_hsm_current_action(path, &hca);
8730 fprintf(stderr, "can't get hsm action for %s: %s\n",
8731 path, strerror(errno = -rc));
8734 he = hca.hca_location;
8735 hua = hca.hca_action;
8736 hps = hca.hca_state;
8738 printf("%s: %s", path, hsm_user_action2name(hua));
8740 /* Skip file without action */
8741 if (hca.hca_action == HUA_NONE) {
8746 printf(" %s ", hsm_progress_state2name(hps));
8748 if ((hps == HPS_RUNNING) &&
8749 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8750 printf("(%llu bytes moved)\n",
8751 (unsigned long long)he.length);
8752 else if ((he.offset + he.length) == LUSTRE_EOF)
8753 printf("(from %llu to EOF)\n",
8754 (unsigned long long)he.offset);
8756 printf("(from %llu to %llu)\n",
8757 (unsigned long long)he.offset,
8758 (unsigned long long)(he.offset + he.length));
8760 } while (++i < argc);
8765 static int lfs_hsm_set(int argc, char **argv)
8767 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8770 static int lfs_hsm_clear(int argc, char **argv)
8772 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8776 * Check file state and return its fid, to be used by lfs_hsm_request().
8778 * \param[in] file Path to file to check
8779 * \param[in,out] fid Pointer to allocated lu_fid struct.
8780 * \param[in,out] last_dev Pointer to last device id used.
8782 * \return 0 on success.
8784 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8790 rc = lstat(file, &st);
8792 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8796 * Checking for regular file as archiving as posix copytool
8797 * rejects archiving files other than regular files
8799 if (!S_ISREG(st.st_mode)) {
8800 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8803 /* A request should be ... */
8804 if (*last_dev != st.st_dev && *last_dev != 0) {
8806 "All files should be on the same filesystem: %s\n",
8810 *last_dev = st.st_dev;
8812 rc = llapi_path2fid(file, fid);
8814 fprintf(stderr, "Cannot read FID of %s: %s\n",
8815 file, strerror(-rc));
8821 /* Fill an HSM HUR item with a given file name.
8823 * If mntpath is set, then the filename is actually a FID, and no
8824 * lookup on the filesystem will be performed.
8826 * \param[in] hur the user request to fill
8827 * \param[in] idx index of the item inside the HUR to fill
8828 * \param[in] mntpath mountpoint of Lustre
8829 * \param[in] fname filename (if mtnpath is NULL)
8830 * or FID (if mntpath is set)
8831 * \param[in] last_dev pointer to last device id used
8833 * \retval 0 on success
8834 * \retval CMD_HELP or a negative errno on error
8836 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8837 const char *mntpath, const char *fname,
8840 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8843 hui->hui_extent.length = -1;
8846 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8848 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8851 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8855 hur->hur_request.hr_itemcount++;
8860 static int lfs_hsm_request(int argc, char **argv, int action)
8862 struct option long_opts[] = {
8863 { .val = 'a', .name = "archive", .has_arg = required_argument },
8864 { .val = 'D', .name = "data", .has_arg = required_argument },
8865 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8866 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8869 char short_opts[] = "l:D:a:m:";
8870 struct hsm_user_request *hur, *oldhur;
8875 char *filelist = NULL;
8876 char fullpath[PATH_MAX];
8877 char *opaque = NULL;
8881 int nbfile_alloc = 0;
8882 char *some_file = NULL;
8883 char *mntpath = NULL;
8889 while ((c = getopt_long(argc, argv, short_opts,
8890 long_opts, NULL)) != -1) {
8899 if (action != HUA_ARCHIVE &&
8900 action != HUA_REMOVE) {
8902 "error: -a is supported only when archiving or removing\n");
8905 archive_id = atoi(optarg);
8910 some_file = strdup(optarg);
8916 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8917 argv[0], argv[optind - 1]);
8922 /* All remaining args are files, so we have at least nbfile */
8923 nbfile = argc - optind;
8925 if ((nbfile == 0) && (!filelist))
8929 opaque_len = strlen(opaque);
8932 * Alloc the request structure with enough place to store all files
8933 * from command line.
8935 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8937 fprintf(stderr, "Cannot create the request: %s\n",
8941 nbfile_alloc = nbfile;
8943 hur->hur_request.hr_action = action;
8944 hur->hur_request.hr_archive_id = archive_id;
8945 hur->hur_request.hr_flags = 0;
8947 /* All remaining args are files, add them */
8948 if (nbfile != 0 && some_file == NULL)
8949 some_file = strdup(argv[optind]);
8951 for (i = 0; i < nbfile; i++) {
8952 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8958 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8960 /* If a filelist was specified, read the filelist from it. */
8962 fp = fopen(filelist, "r");
8964 fprintf(stderr, "Cannot read the file list %s: %s\n",
8965 filelist, strerror(errno));
8970 while ((rc = getline(&line, &len, fp)) != -1) {
8972 * If allocated buffer was too small, get something
8975 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8978 nbfile_alloc = nbfile_alloc * 2 + 1;
8980 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8984 "hsm: cannot allocate the request: %s\n",
8991 size = hur_len(oldhur);
8994 "hsm: cannot allocate %u files + %u bytes data\n",
8995 oldhur->hur_request.hr_itemcount,
8996 oldhur->hur_request.hr_data_len);
9003 memcpy(hur, oldhur, size);
9008 if (line[strlen(line) - 1] == '\n')
9009 line[strlen(line) - 1] = '\0';
9011 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9012 mntpath, line, &last_dev);
9028 /* If a --data was used, add it to the request */
9029 hur->hur_request.hr_data_len = opaque_len;
9031 memcpy(hur_data(hur), opaque, opaque_len);
9033 /* Send the HSM request */
9034 if (realpath(some_file, fullpath) == NULL) {
9035 fprintf(stderr, "Could not find path '%s': %s\n",
9036 some_file, strerror(errno));
9038 rc = llapi_hsm_request(fullpath, hur);
9040 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9041 some_file, strerror(-rc));
9051 static int lfs_hsm_archive(int argc, char **argv)
9053 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9056 static int lfs_hsm_restore(int argc, char **argv)
9058 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9061 static int lfs_hsm_release(int argc, char **argv)
9063 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9066 static int lfs_hsm_remove(int argc, char **argv)
9068 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9071 static int lfs_hsm_cancel(int argc, char **argv)
9073 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9076 static int lfs_swap_layouts(int argc, char **argv)
9081 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9082 SWAP_LAYOUTS_KEEP_MTIME |
9083 SWAP_LAYOUTS_KEEP_ATIME);
9086 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9088 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9090 int lfs_get_mode(const char *string)
9092 enum lock_mode_user mode;
9094 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9095 if (lock_mode_names[mode] == NULL)
9097 if (strcmp(string, lock_mode_names[mode]) == 0)
9104 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9106 enum lu_ladvise_type advice;
9109 advice < ARRAY_SIZE(ladvise_names); advice++) {
9110 if (ladvise_names[advice] == NULL)
9112 if (strcmp(string, ladvise_names[advice]) == 0)
9116 return LU_LADVISE_INVALID;
9119 static int lfs_ladvise(int argc, char **argv)
9121 struct option long_opts[] = {
9122 { .val = 'a', .name = "advice", .has_arg = required_argument },
9123 { .val = 'b', .name = "background", .has_arg = no_argument },
9124 { .val = 'e', .name = "end", .has_arg = required_argument },
9125 { .val = 'l', .name = "length", .has_arg = required_argument },
9126 { .val = 'm', .name = "mode", .has_arg = required_argument },
9127 { .val = 's', .name = "start", .has_arg = required_argument },
9128 { .val = 'u', .name = "unset", .has_arg = no_argument },
9130 char short_opts[] = "a:be:l:m:s:u";
9135 struct llapi_lu_ladvise advice;
9136 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9137 unsigned long long start = 0;
9138 unsigned long long end = LUSTRE_EOF;
9139 unsigned long long length = 0;
9140 unsigned long long size_units;
9141 unsigned long long flags = 0;
9145 while ((c = getopt_long(argc, argv, short_opts,
9146 long_opts, NULL)) != -1) {
9149 advice_type = lfs_get_ladvice(optarg);
9150 if (advice_type == LU_LADVISE_INVALID) {
9152 "%s: invalid advice type '%s'\n",
9154 fprintf(stderr, "Valid types:");
9156 for (advice_type = 0;
9157 advice_type < ARRAY_SIZE(ladvise_names);
9159 if (ladvise_names[advice_type] == NULL)
9161 fprintf(stderr, " %s",
9162 ladvise_names[advice_type]);
9164 fprintf(stderr, "\n");
9177 rc = llapi_parse_size(optarg, &end,
9180 fprintf(stderr, "%s: bad end offset '%s'\n",
9187 rc = llapi_parse_size(optarg, &start,
9191 "%s: bad start offset '%s'\n",
9198 rc = llapi_parse_size(optarg, &length,
9201 fprintf(stderr, "%s: bad length '%s'\n",
9207 mode = lfs_get_mode(optarg);
9210 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9218 fprintf(stderr, "%s: option '%s' unrecognized\n",
9219 argv[0], argv[optind - 1]);
9224 if (advice_type == LU_LADVISE_INVALID) {
9225 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9226 fprintf(stderr, "Valid types:");
9227 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9229 if (ladvise_names[advice_type] == NULL)
9231 fprintf(stderr, " %s", ladvise_names[advice_type]);
9233 fprintf(stderr, "\n");
9237 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9239 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9244 if (argc <= optind) {
9245 fprintf(stderr, "%s: please give one or more file names\n",
9250 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9251 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9256 if (end == LUSTRE_EOF && length != 0)
9257 end = start + length;
9260 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9261 argv[0], start, end);
9265 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9266 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9271 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9272 fprintf(stderr, "%s: mode is required with lockahead\n",
9277 while (optind < argc) {
9280 path = argv[optind++];
9282 fd = open(path, O_RDONLY);
9284 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9285 argv[0], path, strerror(errno));
9290 advice.lla_start = start;
9291 advice.lla_end = end;
9292 advice.lla_advice = advice_type;
9293 advice.lla_value1 = 0;
9294 advice.lla_value2 = 0;
9295 advice.lla_value3 = 0;
9296 advice.lla_value4 = 0;
9297 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9298 advice.lla_lockahead_mode = mode;
9299 advice.lla_peradvice_flags = flags;
9302 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9306 "%s: cannot give advice '%s' to file '%s': %s\n",
9307 argv[0], ladvise_names[advice_type],
9308 path, strerror(errno));
9314 if (rc == 0 && rc2 < 0)
9320 static const char *const heat_names[] = LU_HEAT_NAMES;
9322 static int lfs_heat_get(int argc, char **argv)
9324 struct lu_heat *heat;
9333 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9335 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9340 while (optind < argc) {
9341 path = argv[optind++];
9343 fd = open(path, O_RDONLY);
9345 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9346 argv[0], path, strerror(errno));
9351 heat->lh_count = OBD_HEAT_COUNT;
9352 rc2 = llapi_heat_get(fd, heat);
9356 "%s: cannot get heat of file '%s': %s\n",
9357 argv[0], path, strerror(errno));
9361 printf("flags: %x\n", heat->lh_flags);
9362 for (i = 0; i < heat->lh_count; i++)
9363 printf("%s: %llu\n", heat_names[i],
9364 (unsigned long long)heat->lh_heat[i]);
9366 if (rc == 0 && rc2 < 0)
9374 static int lfs_heat_set(int argc, char **argv)
9376 struct option long_opts[] = {
9377 { .val = 'c', .name = "clear", .has_arg = no_argument },
9378 { .val = 'o', .name = "off", .has_arg = no_argument },
9379 { .val = 'O', .name = "on", .has_arg = no_argument },
9381 enum lu_heat_flag flags = 0;
9391 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9394 flags |= LU_HEAT_FLAG_CLEAR;
9397 flags |= LU_HEAT_FLAG_CLEAR;
9398 flags |= LU_HEAT_FLAG_OFF;
9401 flags &= ~LU_HEAT_FLAG_OFF;
9406 fprintf(stderr, "%s: option '%s' unrecognized\n",
9407 argv[0], argv[optind - 1]);
9412 if (argc <= optind) {
9413 fprintf(stderr, "%s: please give one or more file names\n",
9418 while (optind < argc) {
9419 path = argv[optind++];
9421 fd = open(path, O_RDONLY);
9423 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9424 argv[0], path, strerror(errno));
9429 rc2 = llapi_heat_set(fd, flags);
9433 "%s: cannot setflags heat of file '%s': %s\n",
9434 argv[0], path, strerror(errno));
9438 if (rc == 0 && rc2 < 0)
9445 * The input string contains a comma delimited list of component ids and
9446 * ranges, for example "1,2-4,7".
9448 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9450 bool end_of_loop = false;
9458 while (!end_of_loop) {
9462 char *endptr = NULL;
9465 ptr = strchrnul(arg, ',');
9466 end_of_loop = *ptr == '\0';
9469 start_index = strtol(arg, &endptr, 0);
9470 if (endptr == arg) /* no data at all */
9472 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9474 if (start_index < 0)
9477 end_index = start_index;
9478 if (*endptr == '-') {
9479 end_index = strtol(endptr + 1, &endptr, 0);
9480 if (*endptr != '\0')
9482 if (end_index < start_index)
9486 for (i = start_index; i <= end_index && size > 0; i++) {
9489 /* remove duplicate */
9490 for (j = 0; j < nr; j++) {
9494 if (j == nr) { /* no duplicate */
9500 if (size == 0 && i < end_index)
9507 if (!end_of_loop && ptr)
9510 return rc < 0 ? rc : nr;
9514 * struct verify_mirror_id - Mirror id to be verified.
9515 * @mirror_id: A specified mirror id.
9516 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9518 struct verify_mirror_id {
9524 * compare_mirror_ids() - Compare mirror ids.
9525 * @layout: Mirror component list.
9526 * @cbdata: Callback data in verify_mirror_id structure.
9528 * This is a callback function called by llapi_layout_comp_iterate()
9529 * to compare the specified mirror id with the one in the current
9530 * component of @layout. If they are the same, then the specified
9531 * mirror id is valid.
9533 * Return: a negative error code on failure or
9534 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9535 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9538 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9540 struct verify_mirror_id *mirror_id_cbdata =
9541 (struct verify_mirror_id *)cbdata;
9545 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9549 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9550 progname, strerror(errno));
9554 if (mirror_id_cbdata->mirror_id == mirror_id) {
9555 mirror_id_cbdata->is_valid_id = true;
9556 return LLAPI_LAYOUT_ITER_STOP;
9559 return LLAPI_LAYOUT_ITER_CONT;
9563 * verify_mirror_ids() - Verify specified mirror ids.
9564 * @fname: Mirrored file name.
9565 * @mirror_ids: Specified mirror ids to be verified.
9566 * @ids_nr: Number of specified mirror ids.
9568 * This function verifies that specified @mirror_ids are valid
9569 * in the mirrored file @fname.
9571 * Return: 0 on success or a negative error code on failure.
9574 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9576 struct llapi_layout *layout = NULL;
9577 struct verify_mirror_id mirror_id_cbdata = { 0 };
9588 if (stat(fname, &stbuf) < 0) {
9589 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9590 progname, fname, strerror(errno));
9595 if (!S_ISREG(stbuf.st_mode)) {
9596 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9602 fd = open(fname, O_DIRECT | O_RDONLY);
9604 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9605 progname, fname, strerror(errno));
9610 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9612 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9613 progname, fname, strerror(errno));
9617 layout = llapi_layout_get_by_fd(fd, 0);
9619 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9620 progname, fname, strerror(errno));
9622 llapi_lease_release(fd);
9626 rc = llapi_layout_flags_get(layout, &flr_state);
9628 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9629 progname, fname, strerror(errno));
9634 flr_state &= LCM_FL_FLR_MASK;
9635 switch (flr_state) {
9638 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9639 progname, fname, llapi_layout_flags_string(flr_state));
9646 for (i = 0; i < ids_nr; i++) {
9647 mirror_id_cbdata.mirror_id = mirror_ids[i];
9648 mirror_id_cbdata.is_valid_id = false;
9650 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9655 "%s: '%s' failed to verify mirror id: %u.\n",
9656 progname, fname, mirror_ids[i]);
9660 if (!mirror_id_cbdata.is_valid_id) {
9663 "%s: '%s' invalid specified mirror id: %u.\n",
9664 progname, fname, mirror_ids[i]);
9670 llapi_layout_free(layout);
9671 llapi_lease_release(fd);
9679 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9680 __u16 *mirror_ids, int ids_nr)
9682 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9683 struct llapi_layout *layout;
9694 if (stat(fname, &stbuf) < 0) {
9695 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9696 progname, fname, strerror(errno));
9700 if (!S_ISREG(stbuf.st_mode)) {
9701 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9707 fd = open(fname, O_DIRECT | O_RDWR);
9709 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9710 progname, fname, strerror(errno));
9715 layout = llapi_layout_get_by_fd(fd, 0);
9717 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9718 progname, fname, strerror(errno));
9723 rc = llapi_layout_flags_get(layout, &flr_state);
9725 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9726 progname, fname, strerror(errno));
9731 flr_state &= LCM_FL_FLR_MASK;
9732 if (flr_state == LCM_FL_NONE) {
9734 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9739 /* get stale component info */
9740 comp_size = llapi_mirror_find_stale(layout, comp_array,
9741 ARRAY_SIZE(comp_array),
9742 mirror_ids, ids_nr);
9743 if (comp_size <= 0) {
9748 ioc->lil_mode = LL_LEASE_WRLCK;
9749 ioc->lil_flags = LL_LEASE_RESYNC;
9750 rc = llapi_lease_set(fd, ioc);
9752 if (rc == -EALREADY)
9756 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9757 progname, fname, strerror(-rc));
9761 /* get the read range [start, end) */
9762 start = comp_array[0].lrc_start;
9763 end = comp_array[0].lrc_end;
9764 for (idx = 1; idx < comp_size; idx++) {
9765 if (comp_array[idx].lrc_start < start)
9766 start = comp_array[idx].lrc_start;
9767 if (end < comp_array[idx].lrc_end)
9768 end = comp_array[idx].lrc_end;
9771 rc = llapi_lease_check(fd);
9772 if (rc != LL_LEASE_WRLCK) {
9773 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9778 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9781 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9782 progname, fname, strerror(-rc));
9784 /* need to do the lease unlock even resync fails */
9785 ioc->lil_mode = LL_LEASE_UNLCK;
9786 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9788 for (idx = 0; idx < comp_size; idx++) {
9789 if (comp_array[idx].lrc_synced) {
9790 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9795 rc2 = llapi_lease_set(fd, ioc);
9797 * llapi_lease_set returns lease mode when it request to unlock
9801 /* rc2 == 0 means lost lease lock */
9802 if (rc2 == 0 && rc == 0)
9804 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9806 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9810 llapi_layout_free(layout);
9817 static inline int lfs_mirror_resync(int argc, char **argv)
9819 struct ll_ioc_lease *ioc = NULL;
9820 __u16 mirror_ids[128] = { 0 };
9825 struct option long_opts[] = {
9826 { .val = 'o', .name = "only", .has_arg = required_argument },
9829 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9832 rc = parse_mirror_ids(mirror_ids,
9833 sizeof(mirror_ids) / sizeof(__u16),
9837 "%s: bad mirror ids '%s'.\n",
9844 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9845 argv[0], argv[optind - 1]);
9851 if (argc == optind) {
9852 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9857 if (ids_nr > 0 && argc > optind + 1) {
9859 "%s: option '--only' cannot be used upon multiple files.\n",
9866 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9871 /* set the lease on the file */
9872 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9874 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9875 argv[0], strerror(errno));
9880 for (; optind < argc; optind++) {
9881 rc = lfs_mirror_resync_file(argv[optind], ioc,
9882 mirror_ids, ids_nr);
9883 /* ignore previous file's error, continue with next file */
9886 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9894 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9896 struct llapi_layout *layout;
9899 layout = llapi_layout_get_by_fd(fd, 0);
9901 fprintf(stderr, "could not get layout.\n");
9905 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9907 fprintf(stderr, "failed to iterate layout\n");
9908 llapi_layout_free(layout);
9911 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9912 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9913 llapi_layout_free(layout);
9917 llapi_layout_free(layout);
9923 * Check whether two files are the same file
9924 * \retval 0 same file
9925 * \retval 1 not the same file
9926 * \retval <0 error code
9928 static inline int check_same_file(int fd, const char *f2)
9933 if (fstat(fd, &stbuf1) < 0)
9936 if (stat(f2, &stbuf2) < 0)
9939 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9940 stbuf1.st_ino == stbuf2.st_ino)
9946 static inline int lfs_mirror_read(int argc, char **argv)
9949 __u16 mirror_id = 0;
9950 const char *outfile = NULL;
9956 const size_t buflen = 4 << 20;
9958 struct option long_opts[] = {
9959 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9960 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9963 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9968 mirror_id = strtoul(optarg, &end, 0);
9969 if (*end != '\0' || mirror_id == 0) {
9971 "%s %s: invalid mirror ID '%s'\n",
9972 progname, argv[0], optarg);
9980 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9981 progname, argv[optind - 1]);
9986 if (argc == optind) {
9987 fprintf(stderr, "%s %s: no mirrored file provided\n",
9990 } else if (argc > optind + 1) {
9991 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9995 if (mirror_id == 0) {
9996 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10001 /* open mirror file */
10002 fname = argv[optind];
10003 fd = open(fname, O_DIRECT | O_RDONLY);
10005 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10006 progname, argv[0], fname, strerror(errno));
10010 /* verify mirror id */
10011 rc = verify_mirror_id_by_fd(fd, mirror_id);
10014 "%s %s: cannot find mirror with ID %u in '%s'\n",
10015 progname, argv[0], mirror_id, fname);
10019 /* open output file - O_EXCL ensures output is not the same as input */
10021 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10023 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10024 progname, argv[0], outfile, strerror(errno));
10029 outfd = STDOUT_FILENO;
10032 /* allocate buffer */
10033 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10035 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10036 progname, argv[0], rc);
10042 ssize_t bytes_read;
10043 ssize_t written = 0;
10045 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10046 if (bytes_read < 0) {
10049 "%s %s: fail to read data from mirror %u: %s\n",
10050 progname, argv[0], mirror_id, strerror(-rc));
10055 if (bytes_read == 0)
10058 while (written < bytes_read) {
10061 written2 = write(outfd, buf + written,
10062 bytes_read - written);
10063 if (written2 < 0) {
10065 "%s %s: fail to write %s: %s\n",
10066 progname, argv[0], outfile ? : "STDOUT",
10071 written += written2;
10074 if (written != bytes_read) {
10076 "%s %s: written %ld bytes does not match with %ld read.\n",
10077 progname, argv[0], written, bytes_read);
10099 static inline int lfs_mirror_write(int argc, char **argv)
10102 __u16 mirror_id = 0;
10103 const char *inputfile = NULL;
10109 const size_t buflen = 4 << 20;
10111 size_t page_size = sysconf(_SC_PAGESIZE);
10112 struct ll_ioc_lease_id ioc;
10114 struct option long_opts[] = {
10115 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10116 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10117 { .name = NULL } };
10119 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10124 mirror_id = strtoul(optarg, &end, 0);
10125 if (*end != '\0' || mirror_id == 0) {
10127 "%s %s: invalid mirror ID '%s'\n",
10128 progname, argv[0], optarg);
10133 inputfile = optarg;
10136 fprintf(stderr, "%s: option '%s' unrecognized\n",
10137 progname, argv[optind - 1]);
10142 if (argc == optind) {
10143 fprintf(stderr, "%s %s: no mirrored file provided\n",
10144 progname, argv[0]);
10146 } else if (argc > optind + 1) {
10147 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10151 if (mirror_id == 0) {
10152 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10153 progname, argv[0]);
10157 /* open mirror file */
10158 fname = argv[optind];
10159 fd = open(fname, O_DIRECT | O_WRONLY);
10161 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10162 progname, argv[0], fname, strerror(errno));
10166 /* verify mirror id */
10167 rc = verify_mirror_id_by_fd(fd, mirror_id);
10170 "%s %s: cannot find mirror with ID %u in '%s'\n",
10171 progname, argv[0], mirror_id, fname);
10175 /* open input file */
10177 rc = check_same_file(fd, inputfile);
10180 "%s %s: input file cannot be the mirrored file\n",
10181 progname, argv[0]);
10187 inputfd = open(inputfile, O_RDONLY, 0644);
10189 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10190 progname, argv[0], inputfile, strerror(errno));
10195 inputfd = STDIN_FILENO;
10198 /* allocate buffer */
10199 rc = posix_memalign(&buf, page_size, buflen);
10201 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10202 progname, argv[0], rc);
10203 goto close_inputfd;
10206 /* prepare target mirror components instantiation */
10207 ioc.lil_mode = LL_LEASE_WRLCK;
10208 ioc.lil_flags = LL_LEASE_RESYNC;
10209 ioc.lil_mirror_id = mirror_id;
10210 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10213 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10214 progname, argv[0], fname, strerror(errno));
10220 ssize_t bytes_read;
10224 rc = llapi_lease_check(fd);
10225 if (rc != LL_LEASE_WRLCK) {
10226 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10227 progname, argv[0], fname);
10231 bytes_read = read(inputfd, buf, buflen);
10232 if (bytes_read < 0) {
10235 "%s %s: fail to read data from '%s': %s\n",
10236 progname, argv[0], inputfile ? : "STDIN",
10243 if (bytes_read == 0)
10246 /* round up to page align to make direct IO happy. */
10247 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10249 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10254 "%s %s: fail to write to mirror %u: %s\n",
10255 progname, argv[0], mirror_id,
10263 if (pos & (page_size - 1)) {
10264 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10269 ioc.lil_mode = LL_LEASE_UNLCK;
10270 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10272 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10277 "%s %s: release lease lock of '%s' failed: %s\n",
10278 progname, argv[0], fname, strerror(errno));
10295 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10297 struct llapi_layout *layout;
10298 struct collect_ids_data cid = { .cid_ids = ids,
10300 .cid_exclude = exclude_id, };
10303 layout = llapi_layout_get_by_fd(fd, 0);
10305 fprintf(stderr, "could not get layout\n");
10309 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10311 fprintf(stderr, "failed to iterate layout\n");
10312 llapi_layout_free(layout);
10316 llapi_layout_free(layout);
10318 return cid.cid_count;
10321 static inline int lfs_mirror_copy(int argc, char **argv)
10324 __u16 read_mirror_id = 0;
10325 __u16 ids[128] = { 0 };
10327 struct llapi_layout *layout = NULL;
10328 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10335 struct ll_ioc_lease *ioc = NULL;
10336 struct ll_ioc_lease_id *resync_ioc;
10338 struct option long_opts[] = {
10339 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10340 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10341 { .name = NULL } };
10343 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10348 read_mirror_id = strtoul(optarg, &end, 0);
10349 if (*end != '\0' || read_mirror_id == 0) {
10351 "%s %s: invalid read mirror ID '%s'\n",
10352 progname, argv[0], optarg);
10357 if (!strcmp(optarg, "-1")) {
10358 /* specify all other mirrors */
10359 ids[0] = (__u16)-1;
10362 count = parse_mirror_ids((__u16 *)ids,
10370 fprintf(stderr, "%s: option '%s' unrecognized\n",
10371 progname, argv[optind - 1]);
10376 if (argc == optind) {
10377 fprintf(stderr, "%s %s: no mirrored file provided\n",
10378 progname, argv[0]);
10380 } else if (argc > optind + 1) {
10381 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10385 if (read_mirror_id == 0) {
10387 "%s %s: no valid read mirror ID %d is provided\n",
10388 progname, argv[0], read_mirror_id);
10394 "%s %s: no write mirror ID is provided\n",
10395 progname, argv[0]);
10399 for (i = 0; i < count; i++) {
10400 if (read_mirror_id == ids[i]) {
10402 "%s %s: read and write mirror ID cannot be the same\n",
10403 progname, argv[0]);
10408 /* open mirror file */
10409 fname = argv[optind];
10411 fd = open(fname, O_DIRECT | O_RDWR);
10413 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10414 progname, argv[0], fname, strerror(errno));
10418 /* write to all other mirrors */
10419 if (ids[0] == (__u16)-1) {
10420 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10424 "%s %s: failed to get other mirror ids in '%s': %d\n",
10425 progname, argv[0], fname, rc);
10430 /* verify mirror id */
10431 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10434 "%s %s: cannot find mirror with ID %u in '%s'\n",
10435 progname, argv[0], read_mirror_id, fname);
10439 for (i = 0; i < count; i++) {
10440 rc = verify_mirror_id_by_fd(fd, ids[i]);
10443 "%s %s: cannot find mirror with ID %u in '%s'\n",
10444 progname, argv[0], ids[i], fname);
10449 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10452 "%s %s: cannot alloc comp id array for ioc: %s\n",
10453 progname, argv[0], strerror(errno));
10458 /* get stale component info */
10459 layout = llapi_layout_get_by_fd(fd, 0);
10461 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10462 progname, argv[0], fname, strerror(errno));
10466 comp_size = llapi_mirror_find_stale(layout, comp_array,
10467 ARRAY_SIZE(comp_array),
10469 llapi_layout_free(layout);
10470 if (comp_size < 0) {
10475 /* prepare target mirror components instantiation */
10476 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10477 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10478 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10480 resync_ioc->lil_mirror_id = ids[0];
10482 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10483 rc = llapi_lease_set(fd, ioc);
10486 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10487 progname, argv[0], fname, strerror(errno));
10491 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10494 fprintf(stderr, "%s %s: copy error: %d\n",
10495 progname, argv[0], rc);
10499 fprintf(stdout, "mirror copied successfully: ");
10500 for (i = 0; i < copied; i++)
10501 fprintf(stdout, "%d ", ids[i]);
10502 fprintf(stdout, "\n");
10504 ioc->lil_mode = LL_LEASE_UNLCK;
10505 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10506 ioc->lil_count = 0;
10507 for (i = 0; i < comp_size; i++) {
10510 for (j = 0; j < copied; j++) {
10511 if (comp_array[i].lrc_mirror_id != ids[j])
10514 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10518 rc = llapi_lease_set(fd, ioc);
10523 "%s %s: release lease lock of '%s' failed: %s\n",
10524 progname, argv[0], fname, strerror(errno));
10539 * struct verify_chunk - Mirror chunk to be verified.
10540 * @chunk: [start, end) of the chunk.
10541 * @mirror_count: Number of mirror ids in @mirror_id array.
10542 * @mirror_id: Array of valid mirror ids that cover the chunk.
10544 struct verify_chunk {
10545 struct lu_extent chunk;
10546 unsigned int mirror_count;
10547 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10551 * print_chunks() - Print chunk information.
10552 * @fname: Mirrored file name.
10553 * @chunks: Array of chunks.
10554 * @chunk_count: Number of chunks in @chunks array.
10556 * This function prints [start, end) of each chunk in @chunks
10557 * for mirrored file @fname, and also prints the valid mirror ids
10558 * that cover the chunk.
10563 void print_chunks(const char *fname, struct verify_chunk *chunks,
10569 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10570 for (i = 0; i < chunk_count; i++) {
10571 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10573 if (chunks[i].mirror_count == 0)
10574 fprintf(stdout, "\t[");
10576 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10577 for (j = 1; j < chunks[i].mirror_count; j++)
10578 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10580 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10582 fprintf(stdout, "\n");
10586 * print_checksums() - Print CRC-32 checksum values.
10587 * @chunk: A chunk and its corresponding valid mirror ids.
10588 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10590 * This function prints CRC-32 checksum values on @chunk for
10591 * each valid mirror that covers it.
10596 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10601 "CRC-32 checksum value for chunk "DEXT":\n",
10602 PEXT(&chunk->chunk));
10603 for (i = 0; i < chunk->mirror_count; i++)
10604 fprintf(stdout, "Mirror %u:\t%#lx\n",
10605 chunk->mirror_id[i], crc[i]);
10606 fprintf(stdout, "\n");
10610 * filter_mirror_id() - Filter specified mirror ids.
10611 * @chunks: Array of chunks.
10612 * @chunk_count: Number of chunks in @chunks array.
10613 * @mirror_ids: Specified mirror ids to be verified.
10614 * @ids_nr: Number of specified mirror ids.
10616 * This function scans valid mirror ids that cover each chunk in @chunks
10617 * and filters specified mirror ids.
10622 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10623 __u16 *mirror_ids, int ids_nr)
10628 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10629 unsigned int valid_count = 0;
10631 for (i = 0; i < chunk_count; i++) {
10632 if (chunks[i].mirror_count == 0)
10636 for (j = 0; j < ids_nr; j++) {
10637 for (k = 0; k < chunks[i].mirror_count; k++) {
10638 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10639 valid_id[valid_count] = mirror_ids[j];
10646 memcpy(chunks[i].mirror_id, valid_id,
10647 sizeof(__u16) * valid_count);
10648 chunks[i].mirror_count = valid_count;
10653 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10654 * @layout: Mirror component list.
10655 * @chunks: Array of chunks.
10656 * @chunks_size: Array size of @chunks.
10658 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10659 * to find out chunk segments and store them in @chunks array.
10661 * The @mirror_id array in each element of @chunks will store the valid
10662 * mirror ids that cover the chunk. If a mirror component covering the
10663 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10664 * will not be stored into the @mirror_id array, and the chunk for that
10665 * mirror will not be verified.
10667 * The @mirror_count in each element of @chunks will store the number of
10668 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10669 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10670 * indicates the chunk is valid in only one mirror. In both cases, the
10671 * chunk will not be verified.
10673 * Here is an example:
10675 * 0 1M 2M 3M 4M EOF
10676 * +------+-------------+--------------------+
10677 * | | | S | mirror1
10678 * +------+------+------+------+-------------+
10679 * | | S | S | | mirror2
10680 * +-------------+------+------+-------------+
10682 * prepared @chunks array will contain 5 elements:
10683 * (([0, 1M), [1, 2], 2),
10684 * ([1M, 2M), [1, 2], 2),
10685 * ([2M, 3M), [1], 1),
10686 * ([3M, 4M], [], 0),
10687 * ([4M, EOF), [2], 1))
10689 * Return: the actual array size of @chunks on success
10690 * or a negative error code on failure.
10693 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10694 struct verify_chunk *chunks,
10695 size_t chunks_size)
10699 uint32_t mirror_id;
10705 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10708 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10711 "%s: move to the first layout component: %s.\n",
10712 progname, strerror(errno));
10718 chunks[idx].chunk.e_end = LUSTRE_EOF;
10720 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10723 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10724 progname, strerror(errno));
10728 if (start > chunks[idx].chunk.e_start ||
10729 end <= chunks[idx].chunk.e_start)
10732 if (end < chunks[idx].chunk.e_end)
10733 chunks[idx].chunk.e_end = end;
10735 rc = llapi_layout_comp_flags_get(layout, &flags);
10738 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10739 progname, strerror(errno));
10743 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10746 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10749 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10750 progname, strerror(errno));
10754 chunks[idx].mirror_id[i] = mirror_id;
10756 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10758 "%s: mirror_id array is too small.\n",
10765 rc = llapi_layout_comp_use(layout,
10766 LLAPI_LAYOUT_COMP_USE_NEXT);
10769 "%s: move to the next layout component: %s.\n",
10770 progname, strerror(errno));
10773 } /* loop through all components */
10775 chunks[idx].mirror_count = i;
10777 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10781 if (idx >= chunks_size) {
10782 fprintf(stderr, "%s: chunks array is too small.\n",
10788 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10792 return rc < 0 ? rc : idx + 1;
10796 * lfs_mirror_verify_chunk() - Verify a chunk.
10797 * @fd: File descriptor of the mirrored file.
10798 * @file_size: Size of the mirrored file.
10799 * @chunk: A chunk and its corresponding valid mirror ids.
10800 * @verbose: Verbose mode.
10802 * This function verifies a @chunk contains exactly the same data
10803 * ammong the mirrors that cover it.
10805 * If @verbose is specified, then the function will print where the
10806 * differences are if the data do not match. Otherwise, it will
10807 * just return an error in that case.
10809 * Return: 0 on success or a negative error code on failure.
10812 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10813 struct verify_chunk *chunk, int verbose)
10815 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10817 size_t page_size = sysconf(_SC_PAGESIZE);
10818 ssize_t bytes_read;
10819 ssize_t bytes_done;
10823 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10827 if (file_size == 0)
10830 rc = posix_memalign(&buf, page_size, buflen);
10831 if (rc) /* error code is returned directly */
10835 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10836 PEXT(&chunk->chunk));
10837 for (i = 0; i < chunk->mirror_count; i++)
10838 fprintf(stdout, " %u", chunk->mirror_id[i]);
10839 fprintf(stdout, "\n");
10843 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10844 pos = chunk->chunk.e_start;
10845 while (bytes_done < count) {
10846 /* compute initial CRC-32 checksum */
10847 crc = crc32(0L, Z_NULL, 0);
10848 memset(crc_array, 0, sizeof(crc_array));
10851 for (i = 0; i < chunk->mirror_count; i++) {
10852 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10854 if (bytes_read < 0) {
10857 "%s: failed to read data from mirror %u: %s.\n",
10858 progname, chunk->mirror_id[i],
10863 /* compute new CRC-32 checksum */
10864 crc_array[i] = crc32(crc, buf, bytes_read);
10868 print_checksums(chunk, crc_array);
10870 /* compare CRC-32 checksum values */
10871 for (i = 1; i < chunk->mirror_count; i++) {
10872 if (crc_array[i] != crc_array[0]) {
10876 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10877 progname, PEXT(&chunk->chunk),
10878 chunk->mirror_id[0],
10879 chunk->mirror_id[i]);
10884 bytes_done += bytes_read;
10887 if (verbose > 1 && rc == 0) {
10888 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10889 PEXT(&chunk->chunk));
10890 for (i = 0; i < chunk->mirror_count; i++)
10891 fprintf(stdout, " %u", chunk->mirror_id[i]);
10892 fprintf(stdout, " PASS\n\n");
10901 * lfs_mirror_verify_file() - Verify a mirrored file.
10902 * @fname: Mirrored file name.
10903 * @mirror_ids: Specified mirror ids to be verified.
10904 * @ids_nr: Number of specified mirror ids.
10905 * @verbose: Verbose mode.
10907 * This function verifies that each SYNC mirror of a mirrored file
10908 * specified by @fname contains exactly the same data.
10910 * If @mirror_ids is specified, then the function will verify the
10911 * mirrors specified by @mirror_ids contain exactly the same data.
10913 * If @verbose is specified, then the function will print where the
10914 * differences are if the data do not match. Otherwise, it will
10915 * just return an error in that case.
10917 * Return: 0 on success or a negative error code on failure.
10920 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10923 struct verify_chunk chunks_array[1024] = { };
10924 struct llapi_layout *layout = NULL;
10926 uint32_t flr_state;
10928 int chunk_count = 0;
10934 if (stat(fname, &stbuf) < 0) {
10935 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10936 progname, fname, strerror(errno));
10941 if (!S_ISREG(stbuf.st_mode)) {
10942 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10948 if (stbuf.st_size == 0) {
10950 fprintf(stdout, "%s: '%s' file size is 0.\n",
10956 fd = open(fname, O_DIRECT | O_RDONLY);
10958 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10959 progname, fname, strerror(errno));
10964 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10966 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10967 progname, fname, strerror(errno));
10971 layout = llapi_layout_get_by_fd(fd, 0);
10973 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10974 progname, fname, strerror(errno));
10976 llapi_lease_release(fd);
10980 rc = llapi_layout_flags_get(layout, &flr_state);
10982 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10983 progname, fname, strerror(errno));
10988 flr_state &= LCM_FL_FLR_MASK;
10989 switch (flr_state) {
10992 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10993 progname, fname, llapi_layout_flags_string(flr_state));
10999 /* find out mirror chunks to be verified */
11000 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11001 ARRAY_SIZE(chunks_array));
11002 if (chunk_count < 0) {
11008 /* filter specified mirror ids */
11009 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11012 print_chunks(fname, chunks_array, chunk_count);
11014 for (idx = 0; idx < chunk_count; idx++) {
11015 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11018 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11020 PEXT(&chunks_array[idx].chunk),
11021 (unsigned long long)stbuf.st_size);
11025 if (chunks_array[idx].mirror_count == 0) {
11027 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11029 PEXT(&chunks_array[idx].chunk));
11031 fprintf(stderr, "skipped\n");
11035 fprintf(stderr, "failed\n");
11039 if (chunks_array[idx].mirror_count == 1) {
11042 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11044 PEXT(&chunks_array[idx].chunk),
11045 chunks_array[idx].mirror_id[0]);
11049 rc = llapi_lease_check(fd);
11050 if (rc != LL_LEASE_RDLCK) {
11051 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11056 /* verify one chunk */
11057 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11058 &chunks_array[idx], verbose);
11072 llapi_layout_free(layout);
11073 llapi_lease_release(fd);
11081 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11082 * @argc: The count of lfs mirror verify command line arguments.
11083 * @argv: Array of strings for lfs mirror verify command line arguments.
11085 * This function parses lfs mirror verify command and verifies the
11086 * specified mirrored file(s).
11088 * Return: 0 on success or a negative error code on failure.
11090 static inline int lfs_mirror_verify(int argc, char **argv)
11092 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11098 char cmd[PATH_MAX];
11100 struct option long_opts[] = {
11101 { .val = 'o', .name = "only", .has_arg = required_argument },
11102 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11103 { .name = NULL } };
11105 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11107 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11110 rc = parse_mirror_ids(mirror_ids,
11111 ARRAY_SIZE(mirror_ids),
11115 "%s: bad mirror ids '%s'.\n",
11122 "%s: at least 2 mirror ids needed with '--only' option.\n",
11132 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11133 progname, argv[optind - 1]);
11139 if (argc == optind) {
11140 fprintf(stderr, "%s: no file name given.\n", progname);
11145 if (ids_nr > 0 && argc > optind + 1) {
11147 "%s: '--only' cannot be used upon multiple files.\n",
11154 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11160 for (; optind < argc; optind++) {
11161 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11171 * lfs_mirror() - Parse and execute lfs mirror commands.
11172 * @argc: The count of lfs mirror command line arguments.
11173 * @argv: Array of strings for lfs mirror command line arguments.
11175 * This function parses lfs mirror commands and performs the
11176 * corresponding functions specified in mirror_cmdlist[].
11178 * Return: 0 on success or an error code on failure.
11180 static int lfs_mirror(int argc, char **argv)
11182 char cmd[PATH_MAX];
11185 setlinebuf(stdout);
11187 Parser_init("lfs-mirror > ", mirror_cmdlist);
11189 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11191 program_invocation_short_name = cmd;
11193 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11195 rc = Parser_commands();
11197 return rc < 0 ? -rc : rc;
11200 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11202 #if __BYTE_ORDER == __BIG_ENDIAN
11203 __swab16s(&attrs->lsa_valid);
11204 __swab64s(&attrs->lsa_size);
11205 __swab64s(&attrs->lsa_blocks);
11209 enum lfs_som_type {
11210 LFS_SOM_SIZE = 0x1,
11211 LFS_SOM_BLOCKS = 0x2,
11212 LFS_SOM_FLAGS = 0x4,
11213 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11217 static int lfs_getsom(int argc, char **argv)
11220 struct lustre_som_attrs *attrs;
11221 char buf[sizeof(*attrs) + 64];
11222 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11225 while ((c = getopt(argc, argv, "sbf")) != -1) {
11228 type = LFS_SOM_SIZE;
11231 type = LFS_SOM_BLOCKS;
11234 type = LFS_SOM_FLAGS;
11237 fprintf(stderr, "%s: invalid option '%c'\n",
11247 fprintf(stderr, "%s: %s\n",
11248 progname, argc == 0 ? "miss file target" :
11249 "input more than 2 files");
11254 attrs = (void *)buf;
11255 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11258 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11259 argv[0], strerror(errno), errno);
11263 lustre_som_swab(attrs);
11266 case LFS_SOM_ATTR_ALL:
11267 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11268 path, (unsigned long long)attrs->lsa_size,
11269 (unsigned long long)attrs->lsa_blocks,
11273 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11275 case LFS_SOM_BLOCKS:
11276 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11278 case LFS_SOM_FLAGS:
11279 printf("%x\n", attrs->lsa_valid);
11282 fprintf(stderr, "%s: unknown option\n", progname);
11290 * lfs_mirror_list_commands() - List lfs mirror commands.
11291 * @argc: The count of command line arguments.
11292 * @argv: Array of strings for command line arguments.
11294 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11296 * Return: 0 on success.
11298 static int lfs_mirror_list_commands(int argc, char **argv)
11300 char buffer[81] = "";
11302 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11308 static int lfs_pcc_attach(int argc, char **argv)
11310 struct option long_opts[] = {
11311 { .val = 'i', .name = "id", .has_arg = required_argument },
11312 { .name = NULL } };
11315 __u32 archive_id = 0;
11318 char fullpath[PATH_MAX];
11319 enum lu_pcc_type type = LU_PCC_READWRITE;
11322 while ((c = getopt_long(argc, argv, "i:",
11323 long_opts, NULL)) != -1) {
11326 archive_id = strtoul(optarg, &end, 0);
11327 if (*end != '\0' || archive_id == 0) {
11329 "error: %s: bad archive ID '%s'\n",
11337 fprintf(stderr, "%s: option '%s' unrecognized\n",
11338 argv[0], argv[optind - 1]);
11343 if (archive_id == 0) {
11344 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11348 if (argc <= optind) {
11349 fprintf(stderr, "%s: must specify one or more file names\n",
11354 while (optind < argc) {
11357 path = argv[optind++];
11358 if (!realpath(path, fullpath)) {
11359 fprintf(stderr, "%s: could not find path '%s': %s\n",
11360 argv[0], path, strerror(errno));
11366 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11369 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11370 argv[0], path, archive_id, strerror(-rc2));
11378 static int lfs_pcc_attach_fid(int argc, char **argv)
11380 struct option long_opts[] = {
11381 { .val = 'i', .name = "id", .has_arg = required_argument },
11382 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11383 { .name = NULL } };
11384 char short_opts[] = "i:m:";
11387 __u32 archive_id = 0;
11389 const char *mntpath = NULL;
11390 const char *fidstr;
11391 enum lu_pcc_type type = LU_PCC_READWRITE;
11394 while ((c = getopt_long(argc, argv, short_opts,
11395 long_opts, NULL)) != -1) {
11398 archive_id = strtoul(optarg, &end, 0);
11399 if (*end != '\0') {
11401 "error: %s: bad archive ID '%s'\n",
11412 fprintf(stderr, "%s: option '%s' unrecognized\n",
11413 argv[0], argv[optind - 1]);
11418 if (archive_id == 0) {
11419 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11424 fprintf(stderr, "%s: must specify Lustre mount point\n",
11429 if (argc <= optind) {
11430 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11434 while (optind < argc) {
11437 fidstr = argv[optind++];
11439 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11443 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11444 argv[0], fidstr, mntpath, archive_id,
11447 if (rc == 0 && rc2 < 0)
11453 static int lfs_pcc_detach(int argc, char **argv)
11455 struct option long_opts[] = {
11456 { .val = 'k', .name = "keep", .has_arg = no_argument },
11457 { .name = NULL } };
11458 char short_opts[] = "k";
11462 char fullpath[PATH_MAX];
11463 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11466 while ((c = getopt_long(argc, argv, short_opts,
11467 long_opts, NULL)) != -1) {
11470 detach_opt = PCC_DETACH_OPT_NONE;
11475 fprintf(stderr, "%s: option '%s' unrecognized\n",
11476 argv[0], argv[optind - 1]);
11481 while (optind < argc) {
11484 path = argv[optind++];
11485 if (!realpath(path, fullpath)) {
11486 fprintf(stderr, "%s: could not find path '%s': %s\n",
11487 argv[0], path, strerror(errno));
11493 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11497 "%s: cannot detach '%s' from PCC: %s\n",
11498 argv[0], path, strerror(errno));
11506 static int lfs_pcc_detach_fid(int argc, char **argv)
11508 struct option long_opts[] = {
11509 { .val = 'k', .name = "keep", .has_arg = no_argument },
11510 { .name = NULL } };
11511 char short_opts[] = "k";
11515 const char *mntpath;
11516 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11519 while ((c = getopt_long(argc, argv, short_opts,
11520 long_opts, NULL)) != -1) {
11523 detach_opt = PCC_DETACH_OPT_NONE;
11528 fprintf(stderr, "%s: option '%s' unrecognized\n",
11529 argv[0], argv[optind - 1]);
11534 mntpath = argv[optind++];
11536 while (optind < argc) {
11539 fid = argv[optind++];
11541 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11544 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11545 argv[0], fid, mntpath, strerror(-rc2));
11553 static int lfs_pcc_state(int argc, char **argv)
11557 char fullpath[PATH_MAX];
11558 struct lu_pcc_state state;
11563 fprintf(stderr, "%s: must specify one or more file names\n",
11568 while (optind < argc) {
11571 path = argv[optind++];
11572 if (!realpath(path, fullpath)) {
11573 fprintf(stderr, "%s: could not find path '%s': %s\n",
11574 argv[0], path, strerror(errno));
11580 rc2 = llapi_pcc_state_get(fullpath, &state);
11585 "%s: cannot get PCC state of '%s': %s\n",
11586 argv[0], path, strerror(-rc2));
11590 printf("file: %s", path);
11591 printf(", type: %s", pcc_type2string(state.pccs_type));
11592 if (state.pccs_type == LU_PCC_NONE &&
11593 state.pccs_open_count == 0) {
11598 printf(", PCC file: %s", state.pccs_path);
11599 printf(", user number: %u", state.pccs_open_count);
11600 printf(", flags: %x", state.pccs_flags);
11607 * lfs_pcc_list_commands() - List lfs pcc commands.
11608 * @argc: The count of command line arguments.
11609 * @argv: Array of strings for command line arguments.
11611 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11613 * Return: 0 on success.
11615 static int lfs_pcc_list_commands(int argc, char **argv)
11617 char buffer[81] = "";
11619 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11626 * lfs_pcc() - Parse and execute lfs pcc commands.
11627 * @argc: The count of lfs pcc command line arguments.
11628 * @argv: Array of strings for lfs pcc command line arguments.
11630 * This function parses lfs pcc commands and performs the
11631 * corresponding functions specified in pcc_cmdlist[].
11633 * Return: 0 on success or an error code on failure.
11635 static int lfs_pcc(int argc, char **argv)
11637 char cmd[PATH_MAX];
11640 setlinebuf(stdout);
11642 Parser_init("lfs-pcc > ", pcc_cmdlist);
11644 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11646 program_invocation_short_name = cmd;
11648 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11650 rc = Parser_commands();
11652 return rc < 0 ? -rc : rc;
11655 static int lfs_list_commands(int argc, char **argv)
11657 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11659 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11664 int main(int argc, char **argv)
11668 /* Ensure that liblustreapi constructor has run */
11669 if (!llapi_liblustreapi_initialized())
11670 fprintf(stderr, "liblustreapi was not properly initialized\n");
11672 setlinebuf(stdout);
11675 Parser_init("lfs > ", cmdlist);
11677 progname = program_invocation_short_name; /* Used in error messages */
11679 llapi_set_command_name(argv[1]);
11680 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11681 llapi_clear_command_name();
11683 rc = Parser_commands();
11686 return rc < 0 ? -rc : rc;
11689 #ifdef _LUSTRE_IDL_H_
11690 /* Everything we need here should be included by lustreapi.h. */
11691 # error "lfs should not depend on lustre_idl.h"
11692 #endif /* _LUSTRE_IDL_H_ */