4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
61 #include <sys/xattr.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <libcfs/util/string.h>
75 #include <lustre/lustreapi.h>
76 #include <linux/lustre/lustre_ver.h>
77 #include <linux/lustre/lustre_param.h>
78 #include <linux/lnet/nidstr.h>
79 #include <lnetconfig/cyaml.h>
83 static int lfs_find(int argc, char **argv);
84 static int lfs_getstripe(int argc, char **argv);
85 static int lfs_getdirstripe(int argc, char **argv);
86 static int lfs_setdirstripe(int argc, char **argv);
87 static int lfs_rmentry(int argc, char **argv);
88 static int lfs_unlink_foreign(int argc, char **argv);
89 static int lfs_osts(int argc, char **argv);
90 static int lfs_mdts(int argc, char **argv);
91 static int lfs_df(int argc, char **argv);
92 static int lfs_getname(int argc, char **argv);
93 static int lfs_check(int argc, char **argv);
94 #ifdef HAVE_SYS_QUOTA_H
95 static int lfs_setquota(int argc, char **argv);
96 static int lfs_quota(int argc, char **argv);
97 static int lfs_project(int argc, char **argv);
99 static int lfs_flushctx(int argc, char **argv);
100 static int lfs_poollist(int argc, char **argv);
101 static int lfs_changelog(int argc, char **argv);
102 static int lfs_changelog_clear(int argc, char **argv);
103 static int lfs_fid2path(int argc, char **argv);
104 static int lfs_path2fid(int argc, char **argv);
105 static int lfs_rmfid(int argc, char **argv);
106 static int lfs_data_version(int argc, char **argv);
107 static int lfs_hsm_state(int argc, char **argv);
108 static int lfs_hsm_set(int argc, char **argv);
109 static int lfs_hsm_clear(int argc, char **argv);
110 static int lfs_hsm_action(int argc, char **argv);
111 static int lfs_hsm_archive(int argc, char **argv);
112 static int lfs_hsm_restore(int argc, char **argv);
113 static int lfs_hsm_release(int argc, char **argv);
114 static int lfs_hsm_remove(int argc, char **argv);
115 static int lfs_hsm_cancel(int argc, char **argv);
116 static int lfs_swap_layouts(int argc, char **argv);
117 static int lfs_mv(int argc, char **argv);
118 static int lfs_ladvise(int argc, char **argv);
119 static int lfs_getsom(int argc, char **argv);
120 static int lfs_heat_get(int argc, char **argv);
121 static int lfs_heat_set(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_read(int argc, char **argv);
128 static inline int lfs_mirror_write(int argc, char **argv);
129 static inline int lfs_mirror_copy(int argc, char **argv);
130 static int lfs_pcc_attach(int argc, char **argv);
131 static int lfs_pcc_attach_fid(int argc, char **argv);
132 static int lfs_pcc_detach(int argc, char **argv);
133 static int lfs_pcc_detach_fid(int argc, char **argv);
134 static int lfs_pcc_state(int argc, char **argv);
135 static int lfs_pcc(int argc, char **argv);
136 static int lfs_pcc_list_commands(int argc, char **argv);
137 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
138 __u64 migration_flags,
139 struct llapi_stripe_param *param,
140 struct llapi_layout *layout);
142 struct pool_to_id_cbdata {
146 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
147 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
149 enum setstripe_origin {
159 static int lfs_setstripe_internal(int argc, char **argv,
160 enum setstripe_origin opc);
162 static inline int lfs_setstripe(int argc, char **argv)
164 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
167 static inline int lfs_setstripe_migrate(int argc, char **argv)
169 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
172 static inline int lfs_mirror_create(int argc, char **argv)
174 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
177 static inline int lfs_mirror_extend(int argc, char **argv)
179 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
182 static inline int lfs_mirror_split(int argc, char **argv)
184 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
187 static inline int lfs_mirror_delete(int argc, char **argv)
189 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
192 /* Setstripe and migrate share mostly the same parameters */
193 #define SSM_CMD_COMMON(cmd) \
194 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
195 " [--stripe-count|-c <stripe_count>]\n" \
196 " [--overstripe-count|-C <stripe_count>]\n" \
197 " [--stripe-index|-i <start_ost_idx>]\n" \
198 " [--stripe-size|-S <stripe_size>]\n" \
199 " [--extension-size|--ext-size|-z]\n" \
200 " [--layout|-L <pattern>]\n" \
201 " [--mirror-count|-N[mirror_count]]\n" \
202 " [--ost|-o <ost_indices>]\n" \
203 " [--pool|-p <pool_name>]\n" \
204 " [--yaml|-y <yaml_template_file>]\n" \
205 " [--copy=<lustre_src>]\n"
207 #define SSM_HELP_COMMON \
208 "\tstripe_count: Number of OSTs to stripe on (0=fs default, -1 all)\n" \
209 "\t Using -C instead of -c allows overstriping, which\n" \
210 "\t will place more than one stripe per OST if\n" \
211 "\t stripe_count is greater than the number of OSTs.\n" \
212 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
213 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
214 "\t Optional K, M, or G suffix (for KB, MB, GB\n" \
215 "\t respectively). Must be a multiple of 64KiB.\n" \
216 "\textension_size:\n" \
217 "\t Number of bytes the previous component is extended\n" \
218 "\t each time. Optional K, M, or G suffix (for KB,\n" \
219 "\t MB, GB respectively)\n" \
220 "\tpool_name: Name of OST pool to use (default none)\n" \
221 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n" \
222 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
223 "\t Indices be specified in a format of:\n" \
224 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
226 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
227 "\t If --pool is set with --ost then the OSTs\n" \
228 "\t must be the members of the pool.\n" \
229 "\tcomp_end: Extent end of component, start after previous end.\n" \
230 "\t Optional K, M, or G suffix (for KiB, MiB, GiB), or\n" \
231 "\t -1 or 'eof' for max file size). Must be a multiple\n" \
232 "\t of stripe_size and a multiple of 64KiB.\n" \
233 "\tyaml_template_file:\n" \
234 "\t YAML layout template file, can't be used with -c,\n" \
235 "\t -i, -S, -p, -o, or -E arguments.\n" \
236 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
237 "\t another lustre file or directory, can't used with\n" \
238 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
240 #define MIRROR_CREATE_HELP \
241 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
242 "\t setstripe layout options\n" \
243 "\t It defaults to 1 if not specified; if specified,\n" \
244 "\t it must follow the option without a space.\n" \
245 "\t The option can also be repeated multiple times to\n" \
246 "\t separate mirrors that have different layouts.\n" \
247 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
248 "\t It can be a plain layout or a composite layout.\n" \
249 "\t If not specified, the stripe options inherited\n" \
250 "\t from the previous component will be used.\n" \
251 "\tflags: set flags to the component of the current mirror.\n" \
252 "\t Only \"prefer\" flag is supported so far.\n"
254 #define MIRROR_EXTEND_HELP \
256 "\tvictim_file: The layout of victim_file will be split and used\n" \
257 "\t as a mirror added to the mirrored file.\n" \
258 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
259 "\t from victim file(s) in case the victim file(s)\n" \
260 "\t contains the same data as the original mirrored\n" \
263 #define MIRROR_EXTEND_USAGE \
264 " {--mirror-count|-N[mirror_count]}\n" \
265 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
268 #define SETSTRIPE_USAGE \
269 SSM_CMD_COMMON("setstripe") \
270 MIRROR_EXTEND_USAGE \
271 " <directory|filename>\n" \
275 #define MIGRATE_USAGE \
276 SSM_CMD_COMMON("migrate ") \
277 " [--block|-b] [--non-block|-n]\n" \
278 " [--non-direct|-D] [--verbose|-v]\n" \
282 "\tblock: Block file access during data migration (default)\n" \
283 "\tnon-block: Abort migrations if concurrent access is detected\n" \
284 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
285 "\tverbose: Print each filename as it is migrated\n" \
287 #define SETDIRSTRIPE_USAGE \
288 " [--mdt-count|-c stripe_count>\n" \
289 " [--mdt-hash|-H mdt_hash]\n" \
290 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
291 " [--default|-D] [--mode|-o mode] <dir>\n" \
292 "\tstripe_count: stripe count of the striped directory\n" \
293 "\tmdt_index: MDT index of first stripe\n" \
294 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
295 " crush CRUSH hash algorithm (default)\n" \
296 " fnv_1a_64 FNV-1a hash algorithm\n" \
297 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
298 "\tdefault_stripe: set default dirstripe of the directory\n" \
299 "\tmode: the file access permission of the directory (octal)\n" \
300 "To create dir with a foreign (free format) layout :\n" \
301 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
302 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
303 "\tmode: the mode of the directory\n" \
304 "\tforeign_type: none or symlink\n"
307 * command_t mirror_cmdlist - lfs mirror commands.
309 command_t mirror_cmdlist[] = {
310 { .pc_name = "create", .pc_func = lfs_mirror_create,
311 .pc_help = "Create a mirrored file.\n"
312 "usage: lfs mirror create "
313 "<--mirror-count|-N[mirror_count]> "
314 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
315 MIRROR_CREATE_HELP },
316 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
317 .pc_help = "Delete a mirror from a file.\n"
318 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
319 "\t --component-id|--comp-id|-I <comp_id> |\n"
320 "\t -p <pool>} <mirrored_file> ...\n"
322 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
323 .pc_help = "Extend a mirrored file.\n"
324 "usage: lfs mirror extend "
325 "<--mirror-count|-N[mirror_count]> [--no-verify] "
326 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
327 MIRROR_EXTEND_HELP },
328 { .pc_name = "split", .pc_func = lfs_mirror_split,
329 .pc_help = "Split a mirrored file.\n"
330 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
331 "\t --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
332 "\t [-f <new_file>] <mirrored_file> ...\n"
333 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
334 "\t can be fetched by lfs getstripe command.\n"
335 "\tcomp_id: Unique component ID within a mirror.\n"
336 "\tpool: Components on specified pool.\n"
337 "\tnew_file: This option indicates the layout of the split\n"
338 "\t mirror will be stored into. If not specified,\n"
339 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
340 "\t will be used.\n" },
341 { .pc_name = "read", .pc_func = lfs_mirror_read,
342 .pc_help = "Read the content of a specified mirror of a file.\n"
343 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
344 "[--outfile|-o <output_file>] <mirrored_file>\n" },
345 { .pc_name = "write", .pc_func = lfs_mirror_write,
346 .pc_help = "Write to a specified mirror of a file.\n"
347 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
348 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
349 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
350 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
351 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
352 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
353 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
354 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
355 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
356 "<mirrored file> [<mirrored file2>...]\n"},
357 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
358 .pc_help = "Verify mirrored file(s).\n"
359 "usage: lfs mirror verify "
360 "[--only <mirror_id,mirror_id2[,...]>] "
361 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
362 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
363 .pc_help = "list commands supported by lfs mirror"},
364 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
365 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
366 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
371 * command_t pcc_cmdlist - lfs pcc commands.
373 command_t pcc_cmdlist[] = {
374 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
375 .pc_help = "Attach given files to the Persistent Client Cache.\n"
376 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
377 "\t-i: archive id for RW-PCC\n" },
378 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
379 .pc_help = "Attach given files into PCC by FID(s).\n"
380 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
382 "\t-i: archive id for RW-PCC\n"
383 "\t-m: Lustre mount point\n" },
384 { .pc_name = "state", .pc_func = lfs_pcc_state,
385 .pc_help = "Display the PCC state for given files.\n"
386 "usage: lfs pcc state <file> ...\n" },
387 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
388 .pc_help = "Detach given files from the Persistent Client Cache.\n"
389 "usage: lfs pcc detach <file> ...\n" },
390 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
391 .pc_help = "Detach given files from PCC by FID(s).\n"
392 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
393 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
394 .pc_help = "list commands supported by lfs pcc"},
395 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
396 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
397 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
401 /* all available commands */
402 command_t cmdlist[] = {
403 {"setstripe", lfs_setstripe, 0,
404 "To create a file with specified striping/composite layout, or\n"
405 "create/replace the default layout on an existing directory:\n"
406 SSM_CMD_COMMON("setstripe")
408 " <directory|filename>\n"
410 "To add component(s) to an existing composite file:\n"
411 SSM_CMD_COMMON("setstripe --component-add")
413 "To totally delete the default striping from an existing directory:\n"
414 "usage: setstripe [--delete|-d] <directory>\n"
416 "To create a mirrored file or set s default mirror layout on a directory:\n"
417 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
419 "To delete the last component(s) from an existing composite file\n"
420 "(note that this will also delete any data in those components):\n"
421 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
422 " [--component-flags|-F <comp_flags>]\n"
424 "\tcomp_id: Unique component ID to delete\n"
425 "\tcomp_flags: 'init' indicating all instantiated components\n"
426 "\t '^init' indicating all uninstantiated components\n"
427 "\t-I and -F cannot be specified at the same time\n"
428 "To create a file with a foreign (free format) layout:\n"
429 "usage: setstripe --foreign[=<foreign_type>]\n"
430 " --xattr|-x <layout_string> [--flags <hex>]\n"
431 " [--mode <mode>] <filename>\n"},
432 {"getstripe", lfs_getstripe, 0,
433 "To list the layout pattern for a given file or files in a\n"
434 "directory or recursively for all files in a directory tree.\n"
435 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
436 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
437 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
438 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
439 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
440 " [--component-id[=comp_id]|-I[comp_id]]\n"
441 " [--component-flags[=comp_flags]]\n"
442 " [--component-count]\n"
443 " [--extension-size|--ext-size|-z]\n"
444 " [--component-start[=[+-]comp_start]]\n"
445 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
446 " [[!] --mirror-index=[+-]<index> |\n"
447 " [!] --mirror-id=[+-]<id>] [--mirror-count|-N]\n"
448 " <directory|filename> ..."},
449 {"setdirstripe", lfs_setdirstripe, 0,
450 "Create striped directory on specified MDT, same as mkdir.\n"
451 "May be restricted to root or group users, depending on settings.\n"
452 "usage: setdirstripe [OPTION] <directory>\n"
454 {"getdirstripe", lfs_getdirstripe, 0,
455 "To list the layout pattern info for a given directory\n"
456 "or recursively for all directories in a directory tree.\n"
457 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
458 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
459 " [--recursive|-r] [--yaml|-y]\n"
460 " [--verbose|-v] [--default|-D] <dir> ..."},
461 {"mkdir", lfs_setdirstripe, 0,
462 "Create striped directory on specified MDT, same as setdirstripe.\n"
463 "usage: mkdir [OPTION] <directory>\n"
465 {"rm_entry", lfs_rmentry, 0,
466 "To remove the name entry of the remote directory. Note: This\n"
467 "command will only delete the name entry, i.e. the remote directory\n"
468 "will become inaccessable after this command. This can only be done\n"
469 "by the administrator\n"
470 "usage: rm_entry <dir>\n"},
471 {"unlink_foreign", lfs_unlink_foreign, 0,
472 "To remove the foreign file/dir.\n"
473 "Note: This is for files/dirs prevented to be removed using\n"
474 "unlink/rmdir, but works also for regular ones\n"
475 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
476 {"pool_list", lfs_poollist, 0,
477 "List pools or pool OSTs\n"
478 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
479 {"find", lfs_find, 0,
480 "find files matching given attributes recursively in directory tree.\n"
481 "usage: find <directory|filename> ...\n"
482 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
483 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
484 " [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
485 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
486 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
487 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
488 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
489 " [[!] --stripe-count|-c [+-]<stripes>]\n"
490 " [[!] --stripe-index|-i <index,...>]\n"
491 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
492 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
493 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
494 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
495 " [[!] --projid <projid>]\n"
496 " [[!] --foreign[=<foreign_type>]]\n"
497 " [[!] --layout|-L released,raid0,mdt]\n"
498 " [[!] --foreign[=<foreign_type>]]\n"
499 " [[!] --component-count [+-]<comp_cnt>]\n"
500 " [[!] --component-start [+-]N[kMGTPE]]\n"
501 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
502 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
503 " [[!] --mirror-count|-N [+-]<n>]\n"
504 " [[!] --mirror-state <[^]state>]\n"
505 " [[!] --mdt-count|-T [+-]<stripes>]\n"
506 " [[!] --mdt-hash|-H <hashtype>\n"
507 " [[!] --mdt-index|-m <uuid|index,...>]\n"
508 "\t !: used before an option indicates 'NOT' requested attribute\n"
509 "\t -: used before a value indicates less than requested value\n"
510 "\t +: used before a value indicates more than requested value\n"
511 "\thashtype: hash type of the striped directory.\n"
512 "\t fnv_1a_64 FNV-1a hash algorithm\n"
513 "\t all_char sum of characters % MDT_COUNT\n"},
514 {"check", lfs_check, 0,
515 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
516 "or all the servers (MGTs, MDTs and OSTs).\n"
517 "usage: check <mgts|osts|mdts|all>"},
518 {"osts", lfs_osts, 0, "list OSTs connected to client "
519 "[for specified path only]\n" "usage: osts [path]"},
520 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
521 "[for specified path only]\n" "usage: mdts [path]"},
523 "report filesystem disk space usage or inodes usage "
524 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
525 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
526 " [--pool|-p <fsname>[.<pool>]] [path]"},
527 {"getname", lfs_getname, 0,
528 "list instances and specified mount points [for specified path only]\n"
529 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
530 #ifdef HAVE_SYS_QUOTA_H
531 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
532 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
533 " -b <block-softlimit> -B <block-hardlimit>\n"
534 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
535 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
536 " [--block-softlimit <block-softlimit>]\n"
537 " [--block-hardlimit <block-hardlimit>]\n"
538 " [--inode-softlimit <inode-softlimit>]\n"
539 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
540 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
541 " [--block-grace 'notify'|<block-grace>]\n"
542 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
543 " setquota <-U|-G|-P>\n"
544 " -b <block-softlimit> -B <block-hardlimit>\n"
545 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
546 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
547 " [--block-softlimit <block-softlimit>]\n"
548 " [--block-hardlimit <block-hardlimit>]\n"
549 " [--inode-softlimit <inode-softlimit>]\n"
550 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
551 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
553 " -b can be used instead of --block-softlimit/--block-grace\n"
554 " -B can be used instead of --block-hardlimit\n"
555 " -i can be used instead of --inode-softlimit/--inode-grace\n"
556 " -I can be used instead of --inode-hardlimit\n"
557 " -d can be used instead of --default\n\n"
558 "Note: The total quota space will be split into many qunits and\n"
559 " balanced over all server targets, the minimal qunit size is\n"
560 " 1M bytes for block space and 1K inodes for inode space.\n\n"
561 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
562 " Quota space rebalancing process will stop when this mininum\n"
563 " value is reached. As a result, quota exceeded can be returned\n"
564 " while many targets still have 1MB or 1K inodes of spare\n"
566 " When setting the grace time, 'notify' can be used as grace to\n"
567 " be notified after the quota is over soft limit but prevents\n"
568 " the soft limit from becoming the hard limit."},
569 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
570 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
572 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
573 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
574 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
575 {"project", lfs_project, 0,
576 "Change or list project attribute for specified file or directory.\n"
577 "usage: project [-d|-r] <file|directory...>\n"
578 " list project ID and flags on file(s) or directories\n"
579 " project [-p id] [-s] [-r] <file|directory...>\n"
580 " set project ID and/or inherit flag for specified file(s) or directories\n"
581 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
582 " check project ID and flags on file(s) or directories, print outliers\n"
583 " project -C [-r] [-k] <file|directory...>\n"
584 " clear the project inherit flag and ID on the file or directory\n"
587 {"flushctx", lfs_flushctx, 0,
588 "Flush security context for current user.\n"
589 "usage: flushctx [-k] [-r] [mountpoint...]"},
590 {"changelog", lfs_changelog, 0,
591 "Show the metadata changes on an MDT."
592 "\nusage: changelog <mdtname> [startrec [endrec]]"},
593 {"changelog_clear", lfs_changelog_clear, 0,
594 "Indicate that old changelog records up to <endrec> are no longer of "
595 "interest to consumer <id>, allowing the system to free up space.\n"
596 "An <endrec> of 0 means all records.\n"
597 "usage: changelog_clear <mdtname> <id> <endrec>"},
598 {"fid2path", lfs_fid2path, 0,
599 "Resolve the full path(s) for given FID(s). For a specific hardlink "
600 "specify link number <linkno>.\n"
601 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
602 "<fsname|root> <fid>..."},
603 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
604 "usage: path2fid [--parents] <path> ..."},
605 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
606 "usage: rmfid <fsname|rootpath> <fid> ..."},
607 {"data_version", lfs_data_version, 0, "Display file data version for "
608 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
609 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
610 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
611 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
612 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
613 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
614 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
616 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
617 "[--archived] [--lost] <file> ..."},
618 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
619 "given files.\n" "usage: hsm_action <file> ..."},
620 {"hsm_archive", lfs_hsm_archive, 0,
621 "Archive file to external storage.\n"
622 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
624 {"hsm_restore", lfs_hsm_restore, 0,
625 "Restore file from external storage.\n"
626 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
627 {"hsm_release", lfs_hsm_release, 0,
628 "Release files from Lustre.\n"
629 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
630 {"hsm_remove", lfs_hsm_remove, 0,
631 "Remove file copy from external storage.\n"
632 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
634 " (FILE [FILE ...] | "
635 "--mntpath MOUNTPATH FID [FID ...])\n"
637 "Note: To remove an archived copy of a file already deleted from a "
639 "--mntpath option and a list of FIDs must be specified"
641 {"hsm_cancel", lfs_hsm_cancel, 0,
642 "Cancel requests related to specified files.\n"
643 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
644 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
645 "usage: swap_layouts <path1> <path2>"},
646 {"migrate", lfs_setstripe_migrate, 0,
647 "migrate a directory between MDTs.\n"
648 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
649 " [--mdt-hash|-H] <hash_type>\n"
650 " [--mdt-index|-m] <start_mdt_index>\n"
653 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
654 " it's the MDT index of first stripe\n"
655 "\tmdt_count: number of MDTs to stripe a directory over\n"
656 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
657 " all_char (type 1)sum of characters % MDT_COUNT\n"
658 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
659 " crush (type 3)CRUSH hash algorithm\n"
661 "migrate file objects from one OST "
662 "layout\nto another (may be not safe with concurent writes).\n"
664 "[--stripe-count|-c] <stripe_count>\n"
665 "[--overstripe-count|-C] <stripe_count>\n"
666 " [--stripe-index|-i] <start_ost_index>\n"
667 " [--stripe-size|-S] <stripe_size>\n"
668 " [--pool|-p] <pool_name>\n"
669 " [--ost|-o] <ost_indices>\n"
671 " [--non-block|-n]\n"
672 " [--non-direct|-D]\n"
673 " <file|directory>\n"
674 "\tstripe_count: number of OSTs to stripe a file over\n"
675 "\t Using -C instead of -c allows overstriping, which\n"
676 "\t will place more than one stripe per OST if\n"
677 "\t stripe_count is greater than the number of OSTs\n"
678 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
679 "\tstripe_size: number of bytes to store before moving to the next OST\n"
680 "\tpool_name: name of the predefined pool of OSTs\n"
681 "\tost_indices: OSTs to stripe over, in order\n"
682 "\tblock: Block file access during data migration (default)\n"
683 "\tnon-block: Abort migrations if concurrent access is detected\n"
684 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
686 "To move directories between MDTs. This command is deprecated, "
687 "use \"migrate\" instead.\n"
688 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
690 {"ladvise", lfs_ladvise, 0,
691 "Provide servers with advice about access patterns for a file.\n"
692 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
693 " [--background|-b] [--unset|-u]\n\n"
694 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
695 " {[--mode|-m [READ,WRITE]}\n"
697 {"mirror", lfs_mirror, mirror_cmdlist,
698 "lfs commands used to manage files with mirrored components:\n"
699 "lfs mirror create - create a mirrored file or directory\n"
700 "lfs mirror extend - add mirror(s) to an existing file\n"
701 "lfs mirror split - split a mirror from an existing mirrored file\n"
702 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
703 "lfs mirror read - read a mirror content of a mirrored file\n"
704 "lfs mirror write - write to a mirror of a mirrored file\n"
705 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
706 "lfs mirror verify - verify mirrored file(s)\n"},
707 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
708 "usage: getsom [-s] [-b] [-f] <path>\n"
709 "\t-s: Only show the size value of the SOM data for a given file\n"
710 "\t-b: Only show the blocks value of the SOM data for a given file\n"
711 "\t-f: Only show the flags value of the SOM data for a given file\n"},
712 {"heat_get", lfs_heat_get, 0,
713 "To get heat of files.\n"
714 "usage: heat_get <file> ...\n"},
715 {"heat_set", lfs_heat_set, 0,
716 "To set heat flags of files.\n"
717 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
718 "\t--clear|-c: Clear file heat for given files\n"
719 "\t--off|-o: Turn off file heat for given files\n"
720 "\t--on|-O: Turn on file heat for given files\n"},
721 {"pcc", lfs_pcc, pcc_cmdlist,
722 "lfs commands used to interact with PCC features:\n"
723 "lfs pcc attach - attach given files to Persistent Client Cache\n"
724 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
725 "lfs pcc state - display the PCC state for given files\n"
726 "lfs pcc detach - detach given files from Persistent Client Cache\n"
727 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
728 {"help", Parser_help, 0, "help"},
729 {"exit", Parser_quit, 0, "quit"},
730 {"quit", Parser_quit, 0, "quit"},
731 {"--version", Parser_version, 0,
732 "output build version of the utility and exit"},
733 {"--list-commands", lfs_list_commands, 0,
734 "list commands supported by the utility and exit"},
738 static int check_hashtype(const char *hashtype)
740 int type_num = atoi(hashtype);
743 /* numeric hash type */
744 if (hashtype && strlen(hashtype) == 1 &&
745 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
747 /* string hash type */
748 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
749 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
755 static uint32_t check_foreign_type_name(const char *foreign_type_name)
759 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
760 if (!lu_foreign_types[i].lft_name)
762 if (strcmp(foreign_type_name,
763 lu_foreign_types[i].lft_name) == 0)
764 return lu_foreign_types[i].lft_type;
767 return LU_FOREIGN_TYPE_UNKNOWN;
770 static const char *error_loc = "syserror";
773 MIGRATION_NONBLOCK = 0x0001,
774 MIGRATION_MIRROR = 0x0002,
775 MIGRATION_NONDIRECT = 0x0004,
776 MIGRATION_VERBOSE = 0x0008,
780 migrate_open_files(const char *name, __u64 migration_flags,
781 const struct llapi_stripe_param *param,
782 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
789 char parent[PATH_MAX];
790 char volatile_file[PATH_MAX];
796 if (!param && !layout) {
797 error_loc = "layout information";
801 /* search for file directory pathname */
802 if (strlen(name) > sizeof(parent) - 1) {
803 error_loc = "source file name";
807 strncpy(parent, name, sizeof(parent));
808 ptr = strrchr(parent, '/');
810 if (!getcwd(parent, sizeof(parent))) {
811 error_loc = "getcwd";
815 if (ptr == parent) /* leading '/' */
820 /* open file, direct io */
821 /* even if the file is only read, WR mode is nedeed to allow
825 if (!(migration_flags & MIGRATION_NONDIRECT))
827 fd = open(name, rflags);
830 error_loc = "cannot open source file";
834 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
836 error_loc = "cannot get MDT index";
841 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
842 mode_t open_mode = S_IRUSR | S_IWUSR;
844 random_value = random();
845 rc = snprintf(volatile_file, sizeof(volatile_file),
846 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
847 mdt_index, random_value);
848 if (rc >= sizeof(volatile_file)) {
853 /* create, open a volatile file, use caching (ie no directio) */
855 /* Returns -1 and sets errno on error: */
856 fdv = llapi_layout_file_open(volatile_file, open_flags,
861 /* Does the right thing on error: */
862 fdv = llapi_file_open_param(volatile_file, open_flags,
865 } while (fdv < 0 && (rc = fdv) == -EEXIST);
868 error_loc = "cannot create volatile file";
873 * In case the MDT does not support creation of volatile files
874 * we should try to unlink it.
876 (void)unlink(volatile_file);
879 * Not-owner (root?) special case.
880 * Need to set owner/group of volatile file like original.
881 * This will allow to pass related check during layout_swap.
886 error_loc = "cannot stat source file";
890 rc = fstat(fdv, &stv);
893 error_loc = "cannot stat volatile";
897 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
898 rc = fchown(fdv, st.st_uid, st.st_gid);
901 error_loc = "cannot change ownwership of volatile";
920 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
922 struct llapi_layout *layout;
923 size_t buf_size = 4 * 1024 * 1024;
932 layout = llapi_layout_get_by_fd(fd_src, 0);
934 uint64_t stripe_size;
936 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
938 buf_size = stripe_size;
940 llapi_layout_free(layout);
943 /* Use a page-aligned buffer for direct I/O */
944 rc = posix_memalign(&buf, getpagesize(), buf_size);
950 * read new data only if we have written all
951 * previously read data
955 rc = check_file(fd_src);
960 rsize = read(fd_src, buf, buf_size);
974 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
987 /* Try to avoid page cache pollution after migration. */
988 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
989 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
995 static int migrate_copy_timestamps(int fd, int fdv)
999 if (fstat(fd, &st) == 0) {
1000 struct timeval tv[2] = {
1001 {.tv_sec = st.st_atime},
1002 {.tv_sec = st.st_mtime}
1005 return futimes(fdv, tv);
1011 static int migrate_block(int fd, int fdv)
1018 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1020 error_loc = "cannot get dataversion";
1029 * The grouplock blocks all concurrent accesses to the file.
1030 * It has to be taken after llapi_get_data_version as it would
1033 rc = llapi_group_lock(fd, gid);
1035 error_loc = "cannot get group lock";
1039 rc = migrate_copy_data(fd, fdv, NULL);
1041 error_loc = "data copy failed";
1045 /* Make sure we keep original atime/mtime values */
1046 rc = migrate_copy_timestamps(fd, fdv);
1048 error_loc = "timestamp copy failed";
1054 * for a migration we need to check data version on file did
1057 * Pass in gid=0 since we already own grouplock.
1059 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1060 SWAP_LAYOUTS_CHECK_DV1);
1061 if (rc == -EAGAIN) {
1062 error_loc = "file changed";
1064 } else if (rc < 0) {
1065 error_loc = "cannot swap layout";
1070 rc2 = llapi_group_unlock(fd, gid);
1071 if (rc2 < 0 && rc == 0) {
1072 error_loc = "unlock group lock";
1080 * Internal helper for migrate_copy_data(). Check lease and report error if
1083 * \param[in] fd File descriptor on which to check the lease.
1085 * \retval 0 Migration can keep on going.
1086 * \retval -errno Error occurred, abort migration.
1088 static int check_lease(int fd)
1092 rc = llapi_lease_check(fd);
1094 return 0; /* llapi_check_lease returns > 0 on success. */
1099 static int migrate_nonblock(int fd, int fdv)
1105 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1107 error_loc = "cannot get data version";
1111 rc = migrate_copy_data(fd, fdv, check_lease);
1113 error_loc = "data copy failed";
1117 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1119 error_loc = "cannot get data version";
1125 error_loc = "source file changed";
1129 /* Make sure we keep original atime/mtime values */
1130 rc = migrate_copy_timestamps(fd, fdv);
1132 error_loc = "timestamp copy failed";
1140 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1142 struct pool_to_id_cbdata data = { .pool = pool };
1143 struct llapi_layout *layout = NULL;
1146 layout = llapi_layout_get_by_path(fname, 0);
1149 "error %s: file '%s' couldn't get layout: rc=%d\n",
1150 progname, fname, errno);
1154 rc = llapi_layout_sanity(layout, false, true);
1156 llapi_layout_sanity_perror(errno);
1159 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1168 llapi_layout_free(layout);
1172 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1173 __u32 flags, __u32 neg_flags)
1176 __u32 flags_array[2];
1183 "error %s: neither component id nor pool is specified\n",
1187 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1193 ids[count] = comp_id;
1194 flags_array[count] = flags;
1199 if (neg_flags & LCME_FL_STALE) {
1201 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1206 ids[count] = comp_id;
1207 flags_array[count] = neg_flags | LCME_FL_NEG;
1211 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1213 if (errno == EUCLEAN) {
1216 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1217 progname, comp_id, fname);
1220 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1221 progname, comp_id, fname, flags, neg_flags);
1228 static int lfs_component_del(char *fname, __u32 comp_id,
1229 __u32 flags, __u32 neg_flags)
1233 if (flags && neg_flags)
1236 if (!flags && neg_flags)
1237 flags = neg_flags | LCME_FL_NEG;
1239 if ((flags && comp_id) || (!flags && !comp_id))
1243 if (flags & ~LCME_KNOWN_FLAGS) {
1245 "%s setstripe: unknown flags %#x\n",
1249 } else if (comp_id > LCME_ID_MAX) {
1250 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1255 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1258 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1259 progname, comp_id, fname, strerror(errno));
1263 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1270 rc = llapi_layout_file_comp_add(fname, layout);
1272 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1273 fname, strerror(errno));
1277 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1278 struct llapi_layout *layout)
1286 fd = lstat(fname, &st);
1287 if (fd == 0 && S_ISDIR(st.st_mode))
1288 open_flags = O_DIRECTORY | O_RDONLY;
1290 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1292 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1293 S_ISDIR(st.st_mode) ?
1294 "set default composite layout for" :
1295 "create composite file",
1296 fname, strerror(errno));
1300 static int lfs_migrate(char *name, __u64 migration_flags,
1301 struct llapi_stripe_param *param,
1302 struct llapi_layout *layout)
1304 struct llapi_layout *existing;
1305 uint64_t dom_new, dom_cur;
1310 rc = migrate_open_files(name, migration_flags, param, layout,
1315 rc = llapi_layout_dom_size(layout, &dom_new);
1317 error_loc = "cannot get new layout DoM size";
1320 /* special case for migration to DOM layout*/
1321 existing = llapi_layout_get_by_fd(fd, 0);
1323 error_loc = "cannot get existing layout";
1327 rc = llapi_layout_dom_size(existing, &dom_cur);
1329 error_loc = "cannot get current layout DoM size";
1334 * if file has DoM layout already then migration is possible to
1335 * the new layout with the same DoM component via swap layout,
1336 * if new layout used bigger DOM size, then mirroring is used
1338 if (dom_new > dom_cur) {
1339 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1342 error_loc = "cannot migrate to DOM layout";
1346 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1348 * Blocking mode (forced if servers do not support file lease).
1349 * It is also the default mode, since we cannot distinguish
1350 * between a broken lease and a server that does not support
1351 * atomic swap/close (LU-6785)
1353 rc = migrate_block(fd, fdv);
1357 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1359 error_loc = "cannot get lease";
1363 rc = migrate_nonblock(fd, fdv);
1365 llapi_lease_release(fd);
1370 * Atomically put lease, swap layouts and close.
1371 * for a migration we need to check data version on file did
1374 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1376 error_loc = "cannot swap layout";
1388 fprintf(stderr, "error: %s: %s: %s: %s\n",
1389 progname, name, error_loc, strerror(-rc));
1390 else if (migration_flags & MIGRATION_VERBOSE)
1391 printf("%s\n", name);
1396 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1405 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1409 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1410 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1411 const char *comp_name = comp_flags_table[i].cfn_name;
1413 if (strcmp(name, comp_name) == 0) {
1414 *flags |= comp_flag;
1416 } else if (strncmp(name, "^", 1) == 0 &&
1417 strcmp(name + 1, comp_name) == 0) {
1418 *neg_flags |= comp_flag;
1423 llapi_printf(LLAPI_MSG_ERROR,
1424 "%s: component flag '%s' not supported\n",
1430 if (!*flags && !*neg_flags)
1433 /* don't allow to set and exclude the same flag */
1434 if (*flags & *neg_flags)
1440 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1448 if (strncmp(string, "^", 1) == 0) {
1449 *neg_state = llapi_layout_string_flags(string + 1);
1450 if (*neg_state != 0)
1453 *state = llapi_layout_string_flags(string);
1458 llapi_printf(LLAPI_MSG_ERROR,
1459 "%s: mirrored file state '%s' not supported\n",
1465 * struct mirror_args - Command-line arguments for mirror(s).
1466 * @m_count: Number of mirrors to be created with this layout.
1467 * @m_flags: Mirror level flags, only 'prefer' is supported.
1468 * @m_layout: Mirror layout.
1469 * @m_file: A victim file. Its layout will be split and used as a mirror.
1470 * @m_next: Point to the next node of the list.
1472 * Command-line arguments for mirror(s) will be parsed and stored in
1473 * a linked list that consists of this structure.
1475 struct mirror_args {
1478 struct llapi_layout *m_layout;
1480 struct mirror_args *m_next;
1485 * enum mirror_flags - Flags for extending a mirrored file.
1486 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1487 * in case the victim file(s) contains the same data as the
1488 * original mirrored file.
1489 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1490 * @MF_COMP_ID: specified component id instead of mirror id
1492 * Flags for extending a mirrored file.
1502 * mirror_create_sanity_check() - Check mirror list.
1503 * @list: A linked list that stores the mirror arguments.
1505 * This function does a sanity check on @list for creating
1508 * Return: 0 on success or a negative error code on failure.
1510 static int mirror_create_sanity_check(const char *fname,
1511 struct mirror_args *list)
1514 bool has_m_file = false;
1515 bool has_m_layout = false;
1521 struct llapi_layout *layout;
1523 layout = llapi_layout_get_by_path(fname, 0);
1526 "error: %s: file '%s' couldn't get layout\n",
1531 rc = llapi_layout_sanity(layout, false, true);
1533 llapi_layout_free(layout);
1536 llapi_layout_sanity_perror(rc);
1544 llapi_layout_free(list->m_layout);
1547 llapi_layout_get_by_path(list->m_file, 0);
1548 if (!list->m_layout) {
1550 "error: %s: file '%s' has no layout\n",
1551 progname, list->m_file);
1555 has_m_layout = true;
1556 if (!list->m_layout) {
1557 fprintf(stderr, "error: %s: no mirror layout\n",
1563 rc = llapi_layout_sanity(list->m_layout, false, true);
1565 llapi_layout_sanity_perror(rc);
1569 list = list->m_next;
1572 if (has_m_file && has_m_layout) {
1574 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1582 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1584 __u32 mirror_flags = *(__u32 *)cbdata;
1588 rc = llapi_layout_comp_flags_get(layout, &flags);
1593 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1598 return LLAPI_LAYOUT_ITER_CONT;
1602 * mirror_create() - Create a mirrored file.
1603 * @fname: The file to be created.
1604 * @mirror_list: A linked list that stores the mirror arguments.
1606 * This function creates a mirrored file @fname with the mirror(s)
1607 * from @mirror_list.
1609 * Return: 0 on success or a negative error code on failure.
1611 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1613 struct llapi_layout *layout = NULL;
1614 struct mirror_args *cur_mirror = NULL;
1615 uint16_t mirror_count = 0;
1619 rc = mirror_create_sanity_check(NULL, mirror_list);
1623 cur_mirror = mirror_list;
1624 while (cur_mirror) {
1625 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1627 &cur_mirror->m_flags);
1630 fprintf(stderr, "%s: failed to set mirror flags\n",
1635 for (i = 0; i < cur_mirror->m_count; i++) {
1636 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1640 "error: %s: merge layout failed: %s\n",
1641 progname, strerror(errno));
1645 mirror_count += cur_mirror->m_count;
1646 cur_mirror = cur_mirror->m_next;
1650 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1654 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1657 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1658 progname, strerror(errno));
1662 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1670 llapi_layout_free(layout);
1675 * Compare files and check lease on @fd.
1677 * \retval bytes number of bytes are the same
1679 static ssize_t mirror_file_compare(int fd, int fdv)
1681 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1683 ssize_t bytes_done = 0;
1684 ssize_t bytes_read = 0;
1686 buf = malloc(buflen * 2);
1691 if (!llapi_lease_check(fd)) {
1692 bytes_done = -EBUSY;
1696 bytes_read = read(fd, buf, buflen);
1697 if (bytes_read <= 0)
1700 if (bytes_read != read(fdv, buf + buflen, buflen))
1704 * XXX: should compute the checksum on each buffer and then
1705 * compare checksum to avoid cache collision
1707 if (memcmp(buf, buf + buflen, bytes_read))
1710 bytes_done += bytes_read;
1718 static int mirror_extend_file(const char *fname, const char *victim_file,
1719 enum mirror_flags mirror_flags)
1724 struct stat stbuf_v;
1725 struct ll_ioc_lease *data = NULL;
1728 fd = open(fname, O_RDWR);
1730 error_loc = "open source file";
1735 fdv = open(victim_file, O_RDWR);
1737 error_loc = "open target file";
1742 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1743 error_loc = "stat source or target file";
1748 if (stbuf.st_dev != stbuf_v.st_dev) {
1749 error_loc = "stat source and target file";
1754 /* mirrors should be of the same size */
1755 if (stbuf.st_size != stbuf_v.st_size) {
1756 error_loc = "file sizes don't match";
1761 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1763 error_loc = "cannot get lease";
1767 if (!(mirror_flags & MF_NO_VERIFY)) {
1769 /* mirrors should have the same contents */
1770 ret = mirror_file_compare(fd, fdv);
1771 if (ret != stbuf.st_size) {
1772 error_loc = "file busy or contents don't match";
1773 rc = ret < 0 ? ret : -EINVAL;
1778 /* Get rid of caching pages from clients */
1779 rc = llapi_file_flush(fd);
1781 error_loc = "cannot get data version";
1785 rc = llapi_file_flush(fdv);
1787 error_loc = "cannot get data version";
1791 /* Make sure we keep original atime/mtime values */
1792 rc = migrate_copy_timestamps(fd, fdv);
1794 error_loc = "cannot copy timestamp";
1798 /* Atomically put lease, merge layouts and close. */
1799 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1801 error_loc = "memory allocation";
1804 data->lil_mode = LL_LEASE_UNLCK;
1805 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1806 data->lil_count = 1;
1807 data->lil_ids[0] = fdv;
1808 rc = llapi_lease_set(fd, data);
1810 error_loc = "cannot merge layout";
1812 } else if (rc == 0) {
1814 error_loc = "lost lease lock";
1827 (void) unlink(victim_file);
1829 fprintf(stderr, "error: %s: %s: %s: %s\n",
1830 progname, fname, error_loc, strerror(-rc));
1834 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1835 bool inherit, uint32_t flags)
1837 struct llapi_layout *f_layout = NULL;
1838 struct ll_ioc_lease *data = NULL;
1844 f_layout = llapi_layout_get_by_path(name, 0);
1846 fprintf(stderr, "%s: cannot get layout\n", progname);
1849 rc = llapi_layout_get_last_init_comp(f_layout);
1851 fprintf(stderr, "%s: cannot get the last init comp\n",
1855 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1858 "%s: cannot inherit from the last init comp\n",
1863 llapi_layout_comp_flags_set(m_layout, flags);
1864 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1869 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1871 error_loc = "cannot get lease";
1875 rc = migrate_nonblock(fd, fdv);
1877 llapi_lease_release(fd);
1881 /* Atomically put lease, merge layouts and close. */
1882 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1884 error_loc = "memory allocation";
1887 data->lil_mode = LL_LEASE_UNLCK;
1888 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1889 data->lil_count = 1;
1890 data->lil_ids[0] = fdv;
1891 rc = llapi_lease_set(fd, data);
1893 error_loc = "cannot merge layout";
1895 } else if (rc == 0) {
1897 error_loc = "lost lease lock";
1910 fprintf(stderr, "error: %s: %s: %s: %s\n",
1911 progname, name, error_loc, strerror(-rc));
1915 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1916 enum mirror_flags mirror_flags)
1920 rc = mirror_create_sanity_check(fname, mirror_list);
1924 while (mirror_list) {
1925 if (mirror_list->m_file) {
1926 rc = mirror_extend_file(fname, mirror_list->m_file,
1929 __u32 mirror_count = mirror_list->m_count;
1931 while (mirror_count > 0) {
1932 rc = mirror_extend_layout(fname,
1933 mirror_list->m_layout,
1934 mirror_list->m_inherit,
1935 mirror_list->m_flags);
1945 mirror_list = mirror_list->m_next;
1951 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1956 rc = llapi_layout_mirror_id_get(layout, &id);
1960 if ((__u16)id == *(__u16 *)cbdata)
1961 return LLAPI_LAYOUT_ITER_STOP;
1963 return LLAPI_LAYOUT_ITER_CONT;
1966 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1971 rc = llapi_layout_comp_id_get(layout, &id);
1975 if (id == *(__u32 *)cbdata)
1976 return LLAPI_LAYOUT_ITER_STOP;
1978 return LLAPI_LAYOUT_ITER_CONT;
1981 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
1983 char buf[LOV_MAXPOOLNAME + 1];
1984 struct pool_to_id_cbdata *d = (void *)cbdata;
1988 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1991 if (strcmp(d->pool, buf))
1992 return LLAPI_LAYOUT_ITER_CONT;
1994 rc = llapi_layout_mirror_id_get(layout, &id);
1999 return LLAPI_LAYOUT_ITER_STOP;
2002 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2004 char buf[LOV_MAXPOOLNAME + 1];
2005 struct pool_to_id_cbdata *d = (void *)cbdata;
2009 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2012 if (strcmp(d->pool, buf))
2013 return LLAPI_LAYOUT_ITER_CONT;
2015 rc = llapi_layout_comp_id_get(layout, &id);
2020 return LLAPI_LAYOUT_ITER_STOP;
2023 struct collect_ids_data {
2029 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2031 struct collect_ids_data *cid = cbdata;
2035 rc = llapi_layout_mirror_id_get(layout, &id);
2039 if ((__u16)id != cid->cid_exclude) {
2042 for (i = 0; i < cid->cid_count; i++) {
2043 /* already collected the mirror id */
2044 if (id == cid->cid_ids[i])
2045 return LLAPI_LAYOUT_ITER_CONT;
2047 cid->cid_ids[cid->cid_count] = id;
2051 return LLAPI_LAYOUT_ITER_CONT;
2055 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2056 * @mirror_id: Mirror id to be checked.
2057 * @layout: Mirror component list.
2059 * This function checks if a mirror with specified @mirror_id is the last
2060 * non-stale mirror of a layout @layout.
2062 * Return: true or false.
2065 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2067 __u16 mirror_ids[128] = { 0 };
2068 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2070 .cid_exclude = mirror_id, };
2073 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2075 for (i = 0; i < cid.cid_count; i++) {
2076 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2079 comp_size = llapi_mirror_find_stale(layout, comp_array,
2080 ARRAY_SIZE(comp_array),
2089 static int mirror_split(const char *fname, __u32 id, const char *pool,
2090 enum mirror_flags mflags, const char *victim_file)
2092 struct llapi_layout *layout;
2093 char parent[PATH_MAX];
2094 char victim[PATH_MAX];
2095 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2097 struct ll_ioc_lease *data;
2098 uint16_t mirror_count;
2104 /* check fname contains mirror with mirror_id/comp_id */
2105 layout = llapi_layout_get_by_path(fname, 0);
2108 "error %s: file '%s' couldn't get layout\n",
2113 rc = llapi_layout_sanity(layout, false, true);
2115 llapi_layout_sanity_perror(rc);
2119 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2122 "error %s: file '%s' couldn't get mirror count\n",
2126 if (mirror_count < 2) {
2128 "error %s: file '%s' has %d component, cannot split\n",
2129 progname, fname, mirror_count);
2133 if (mflags & MF_COMP_POOL) {
2134 struct pool_to_id_cbdata data = { .pool = pool };
2136 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2138 mirror_id = data.id;
2139 } else if (mflags & MF_COMP_ID) {
2140 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2141 mirror_id = mirror_id_of(id);
2143 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2147 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2150 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2151 if (mflags & MF_COMP_POOL) {
2153 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2154 progname, fname, pool);
2156 } else if (mflags & MF_COMP_ID) {
2158 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2159 progname, fname, id);
2163 "error %s: file '%s' does not contain mirror with id %u\n",
2164 progname, fname, id);
2169 fd = open(fname, O_RDWR);
2172 "error %s: open file '%s' failed: %s\n",
2173 progname, fname, strerror(errno));
2177 /* get victim file directory pathname */
2178 if (strlen(fname) > sizeof(parent) - 1) {
2179 fprintf(stderr, "error %s: file name of '%s' too long\n",
2184 strncpy(parent, fname, sizeof(parent));
2185 ptr = strrchr(parent, '/');
2187 if (!getcwd(parent, sizeof(parent))) {
2188 fprintf(stderr, "error %s: getcwd failed: %s\n",
2189 progname, strerror(errno));
2199 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2201 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2207 /* use a temp file to store the splitted layout */
2208 if (mflags & MF_DESTROY) {
2209 if (last_non_stale_mirror(mirror_id, layout)) {
2212 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2217 fdv = llapi_create_volatile_idx(parent, mdt_index,
2218 O_LOV_DELAY_CREATE);
2220 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2222 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2225 /* user specified victim file */
2226 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2231 "error %s: create victim file failed: %s\n",
2232 progname, strerror(errno));
2236 /* get lease lock of fname */
2237 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2240 "error %s: cannot get lease of file '%s': %d\n",
2241 progname, fname, rc);
2245 /* Atomatically put lease, split layouts and close. */
2246 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2252 data->lil_mode = LL_LEASE_UNLCK;
2253 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2254 data->lil_count = 2;
2255 data->lil_ids[0] = fdv;
2256 data->lil_ids[1] = mirror_id;
2257 rc = llapi_lease_set(fd, data);
2259 if (rc == 0) /* lost lease lock */
2262 "error %s: cannot split '%s': %s\n",
2263 progname, fname, strerror(-rc));
2274 llapi_layout_free(layout);
2279 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2280 __u16 *mirror_ids, int ids_nr);
2282 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2283 __u64 migration_flags,
2284 struct llapi_stripe_param *param,
2285 struct llapi_layout *layout)
2287 struct ll_ioc_lease *data = NULL;
2290 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2292 error_loc = "cannot get lease";
2296 /* Atomically put lease, merge layouts, resync and close. */
2297 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2299 error_loc = "memory allocation";
2302 data->lil_mode = LL_LEASE_UNLCK;
2303 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2304 data->lil_count = 1;
2305 data->lil_ids[0] = fdv;
2306 rc = llapi_lease_set(fd, data);
2308 error_loc = "cannot merge layout";
2310 } else if (rc == 0) {
2312 error_loc = "lost lease lock";
2318 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2320 error_loc = "cannot resync file";
2324 /* delete first mirror now */
2325 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2327 error_loc = "cannot delete old layout";
2335 fprintf(stderr, "error: %s: %s: %s: %s\n",
2336 progname, name, error_loc, strerror(-rc));
2337 else if (migration_flags & MIGRATION_VERBOSE)
2338 printf("%s\n", name);
2345 * Parse a string containing an target index list into an array of integers.
2347 * The input string contains a comma delimited list of individual
2348 * indices and ranges, for example "1,2-4,7". Add the indices into the
2349 * \a tgts array and remove duplicates.
2351 * \param[out] tgts array to store indices in
2352 * \param[in] size size of \a tgts array
2353 * \param[in] offset starting index in \a tgts
2354 * \param[in] arg string containing OST index list
2355 * \param[in/out] overstriping index list may contain duplicates
2357 * \retval positive number of indices in \a tgts
2358 * \retval -EINVAL unable to parse \a arg
2360 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2361 unsigned long long *pattern)
2365 int slots = size - offset;
2367 bool overstriped = false;
2373 end_of_loop = false;
2374 while (!end_of_loop) {
2375 int start_index = 0;
2378 char *endptr = NULL;
2382 ptr = strchrnul(arg, ',');
2384 end_of_loop = *ptr == '\0';
2387 start_index = strtol(arg, &endptr, 0);
2388 if (endptr == arg) /* no data at all */
2390 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2393 end_index = start_index;
2394 if (*endptr == '-') {
2395 end_index = strtol(endptr + 1, &endptr, 0);
2396 if (*endptr != '\0')
2398 if (end_index < start_index)
2402 for (i = start_index; i <= end_index && slots > 0; i++) {
2405 /* remove duplicate */
2406 for (j = 0; j < offset; j++) {
2407 if (tgts[j] == i && pattern &&
2408 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2410 else if (tgts[j] == i)
2416 if (j == offset) { /* check complete */
2422 if (slots == 0 && i < end_index)
2430 if (!end_of_loop && ptr)
2433 if (!overstriped && pattern)
2434 *pattern = LLAPI_LAYOUT_DEFAULT;
2436 return rc < 0 ? rc : nr;
2439 struct lfs_setstripe_args {
2440 unsigned long long lsa_comp_end;
2441 unsigned long long lsa_stripe_size;
2442 unsigned long long lsa_extension_size;
2443 long long lsa_stripe_count;
2444 long long lsa_stripe_off;
2445 __u32 lsa_comp_flags;
2446 __u32 lsa_comp_neg_flags;
2447 unsigned long long lsa_pattern;
2448 unsigned int lsa_mirror_count;
2450 bool lsa_first_comp;
2451 bool lsa_extension_comp;
2453 char *lsa_pool_name;
2456 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2458 unsigned int mirror_count = lsa->lsa_mirror_count;
2459 bool first_comp = lsa->lsa_first_comp;
2461 memset(lsa, 0, sizeof(*lsa));
2463 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2464 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2465 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2466 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2467 lsa->lsa_pool_name = NULL;
2469 lsa->lsa_mirror_count = mirror_count;
2470 lsa->lsa_first_comp = first_comp;
2474 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2475 * @lsa: Stripe options to be initialized and inherited.
2477 * This function initializes stripe options in @lsa and inherit
2478 * stripe_size, stripe_count and OST pool_name options.
2482 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2484 unsigned long long stripe_size;
2485 long long stripe_count;
2486 char *pool_name = NULL;
2488 stripe_size = lsa->lsa_stripe_size;
2489 stripe_count = lsa->lsa_stripe_count;
2490 pool_name = lsa->lsa_pool_name;
2492 setstripe_args_init(lsa);
2494 lsa->lsa_stripe_size = stripe_size;
2495 lsa->lsa_stripe_count = stripe_count;
2496 lsa->lsa_pool_name = pool_name;
2499 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2501 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2502 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2503 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2504 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2505 lsa->lsa_comp_end != 0);
2509 * comp_args_to_layout() - Create or extend a composite layout.
2510 * @composite: Pointer to the composite layout.
2511 * @lsa: Stripe options for the new component.
2513 * This function creates or extends a composite layout by adding a new
2514 * component with stripe options from @lsa.
2516 * Return: 0 on success or an error code on failure.
2518 static int comp_args_to_layout(struct llapi_layout **composite,
2519 struct lfs_setstripe_args *lsa,
2522 struct llapi_layout *layout = *composite;
2523 uint64_t prev_end = 0;
2529 layout = llapi_layout_alloc();
2531 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2536 *composite = layout;
2537 lsa->lsa_first_comp = true;
2542 * Get current component extent, current component
2543 * must be the tail component.
2545 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2547 fprintf(stderr, "Get comp extent failed. %s\n",
2552 if (lsa->lsa_first_comp) {
2554 rc = llapi_layout_add_first_comp(layout);
2556 rc = llapi_layout_comp_add(layout);
2559 fprintf(stderr, "Add component failed. %s\n",
2565 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2567 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2568 lsa->lsa_comp_flags, strerror(errno));
2573 uint64_t comp_end = lsa->lsa_comp_end;
2576 * The extendable component is 0-length, so it can be removed
2577 * if there is insufficient space to extend it.
2579 if (lsa->lsa_extension_comp)
2580 comp_end = prev_end;
2582 rc = llapi_layout_comp_extent_set(layout, prev_end,
2585 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2586 prev_end, comp_end, strerror(errno));
2590 /* reset lsa_first_comp */
2591 lsa->lsa_first_comp = false;
2593 /* Data-on-MDT component setting */
2594 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2596 * In case of Data-on-MDT patterns the only extra option
2597 * applicable is stripe size option.
2599 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2601 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2602 lsa->lsa_stripe_count);
2606 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2608 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2609 lsa->lsa_stripe_size);
2613 if (lsa->lsa_nr_tgts != 0) {
2615 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2620 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2622 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2623 lsa->lsa_stripe_off);
2627 if (lsa->lsa_pool_name != 0) {
2629 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2630 lsa->lsa_pool_name);
2635 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2637 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2642 /* Data-on-MDT component has always single stripe up to end */
2643 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2644 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2645 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2647 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2654 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2655 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2657 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2658 rc = llapi_layout_extension_size_set(layout, size);
2660 rc = llapi_layout_stripe_size_set(layout, size);
2663 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2664 size, strerror(errno));
2668 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2670 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2671 lsa->lsa_stripe_count, strerror(errno));
2675 if (lsa->lsa_pool_name) {
2676 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2678 fprintf(stderr, "Set pool name: %s failed. %s\n",
2679 lsa->lsa_pool_name, strerror(errno));
2683 rc = llapi_layout_pool_name_set(layout, "");
2685 fprintf(stderr, "Clear pool name failed: %s\n",
2691 if (lsa->lsa_nr_tgts > 0) {
2692 if (lsa->lsa_stripe_count > 0 &&
2693 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2694 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2695 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2696 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2697 lsa->lsa_stripe_count,
2702 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2703 rc = llapi_layout_ost_index_set(layout, i,
2708 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2709 lsa->lsa_stripe_off != -1) {
2710 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2713 fprintf(stderr, "Set ost index %d failed. %s\n",
2714 i, strerror(errno));
2718 /* Create the second, virtual component of extension space */
2719 if (lsa->lsa_extension_comp) {
2720 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2721 lsa->lsa_extension_comp = false;
2728 static int build_component(struct llapi_layout **layout,
2729 struct lfs_setstripe_args *lsa, bool set_extent)
2733 rc = comp_args_to_layout(layout, lsa, set_extent);
2737 if (lsa->lsa_mirror_count > 0) {
2738 rc = llapi_layout_mirror_count_set(*layout,
2739 lsa->lsa_mirror_count);
2743 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2746 lsa->lsa_mirror_count = 0;
2752 static int build_prev_component(struct llapi_layout **layout,
2753 struct lfs_setstripe_args *prev,
2754 struct lfs_setstripe_args *lsa,
2757 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2760 if (prev->lsa_stripe_size) {
2762 prev->lsa_comp_end = lsa->lsa_comp_end;
2763 prev->lsa_extension_size = lsa->lsa_extension_size;
2764 prev->lsa_extension_comp = true;
2767 rc = build_component(layout, prev, true);
2773 * Copy lsa to previous lsa;
2774 * if this is an extension component, make the previous invalid;
2777 prev->lsa_stripe_size = 0;
2784 static int build_layout_from_yaml_node(struct cYAML *node,
2785 struct llapi_layout **layout,
2786 struct lfs_setstripe_args *lsa,
2787 struct lfs_setstripe_args *prevp)
2789 struct lfs_setstripe_args prev = { 0 };
2790 __u32 *osts = lsa->lsa_tgts;
2798 string = node->cy_string;
2800 if (node->cy_type == CYAML_TYPE_OBJECT) {
2801 /* go deep to sub blocks */
2802 if (string && !strncmp(string, "component", 9) &&
2803 strncmp(string, "component0", 10) &&
2804 strncmp(string, "components", 10)) {
2805 rc = build_prev_component(layout, prevp, lsa,
2810 /* initialize lsa. */
2811 setstripe_args_init(lsa);
2812 lsa->lsa_first_comp = false;
2813 lsa->lsa_tgts = osts;
2816 rc = build_layout_from_yaml_node(node->cy_child, layout,
2821 if (!node->cy_string)
2824 /* skip leading lmm_ if present, to simplify parsing */
2825 if (strncmp(string, "lmm_", 4) == 0)
2828 if (node->cy_type == CYAML_TYPE_STRING) {
2829 if (!strcmp(string, "lcme_extent.e_end")) {
2830 if (!strcmp(node->cy_valuestring, "EOF") ||
2831 !strcmp(node->cy_valuestring, "eof"))
2832 lsa->lsa_comp_end = LUSTRE_EOF;
2833 } else if (!strcmp(string, "pool")) {
2834 lsa->lsa_pool_name = node->cy_valuestring;
2835 } else if (!strcmp(string, "pattern")) {
2836 if (!strcmp(node->cy_valuestring, "mdt"))
2837 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2838 if (!strcmp(node->cy_valuestring,
2839 "raid0,overstriped"))
2841 LLAPI_LAYOUT_OVERSTRIPING;
2842 } else if (!strcmp(string, "lcme_flags")) {
2843 rc = comp_str2flags(node->cy_valuestring,
2844 &lsa->lsa_comp_flags,
2845 &lsa->lsa_comp_neg_flags);
2849 * Only template flags have meaning in
2850 * the layout for a new file
2852 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2854 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2855 if (!strcmp(string, "lcm_mirror_count")) {
2856 lsa->lsa_mirror_count = node->cy_valueint;
2857 } else if (!strcmp(string, "lcme_extent.e_start")) {
2858 if (node->cy_valueint == 0)
2859 lsa->lsa_first_comp = true;
2860 } else if (!strcmp(string, "lcme_extent.e_end")) {
2861 if (node->cy_valueint == -1)
2862 lsa->lsa_comp_end = LUSTRE_EOF;
2864 lsa->lsa_comp_end = node->cy_valueint;
2865 } else if (!strcmp(string, "stripe_count")) {
2866 lsa->lsa_stripe_count = node->cy_valueint;
2867 } else if (!strcmp(string, "stripe_size")) {
2868 lsa->lsa_stripe_size = node->cy_valueint;
2869 } else if (!strcmp(string, "extension_size")) {
2870 lsa->lsa_extension_size = node->cy_valueint;
2871 lsa->lsa_extension_comp = true;
2872 } else if (!strcmp(string, "stripe_offset")) {
2873 lsa->lsa_stripe_off = node->cy_valueint;
2874 } else if (!strcmp(string, "l_ost_idx")) {
2875 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2880 node = node->cy_next;
2883 if (prevp == &prev) {
2884 rc = build_prev_component(layout, prevp, lsa, true);
2888 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2889 rc = build_component(layout, lsa, *layout != NULL);
2895 static int lfs_comp_create_from_yaml(char *template,
2896 struct llapi_layout **layout,
2897 struct lfs_setstripe_args *lsa,
2900 struct cYAML *tree = NULL, *err_rc = NULL;
2903 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2905 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2906 progname, template);
2907 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2908 "can't parse", &err_rc);
2909 cYAML_print_tree2file(stderr, err_rc);
2910 cYAML_free_tree(err_rc);
2915 /* initialize lsa for plain file */
2916 setstripe_args_init(lsa);
2917 lsa->lsa_tgts = osts;
2919 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2921 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2922 progname, template);
2925 /* clean clean lsa */
2926 setstripe_args_init(lsa);
2930 cYAML_free_tree(tree);
2935 * Get the extension size from the next (SEL) component and extend the
2936 * current component on it. The start of the next component is to be
2939 * \param[in] layout the current layout
2940 * \param[in] start the start of the current component
2941 * \param[in,out] end the end of the current component
2942 * \param[in] offset the offset to adjust the end position to instead of
2945 * \retval 0 - extended successfully
2946 * \retval < 0 - error
2948 static int layout_extend_comp(struct llapi_layout *layout,
2949 uint64_t start, uint64_t *end,
2952 uint64_t size, next_start, next_end;
2955 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2958 "%s setstripe: cannot move component cursor: %s\n",
2959 progname, strerror(errno));
2964 * Even if the @size will not be used below, this will fail if
2965 * this is not a SEL component - a good confirmation we are
2966 * working on right components.
2968 rc = llapi_layout_extension_size_get(layout, &size);
2971 "%s setstripe: cannot get component ext size: %s\n",
2972 progname, strerror(errno));
2976 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2978 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2979 progname, strerror(errno));
2983 next_start += offset ?: size;
2984 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2986 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2987 progname, strerror(errno));
2991 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2994 "%s setstripe: cannot move component cursor: %s\n",
2995 progname, strerror(errno));
2999 *end += offset ?: size;
3000 rc = llapi_layout_comp_extent_set(layout, start, *end);
3002 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3003 progname, strerror(errno));
3011 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3012 * end of the last component in the existing file, and adjust the
3013 * first extent start of the components to be added accordingly.
3015 * In the create mode, we need to check if the first component is an extendable
3016 * SEL component and extend its length to the extension size (first component
3017 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3019 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3022 struct llapi_layout *head;
3023 uint64_t start = 0, prev_end = 0;
3027 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3032 head = llapi_layout_get_by_path(fname, 0);
3035 "%s setstripe: cannot read layout from '%s': %s\n",
3036 progname, fname, strerror(errno));
3038 } else if (errno == ENODATA) {
3040 * file without LOVEA, this component-add will be turned
3041 * into a component-create.
3043 llapi_layout_free(head);
3047 * the new layout will be added to an empty one, it
3048 * still needs to be adjusted below
3052 } else if (!llapi_layout_is_composite(head)) {
3054 "%s setstripe: '%s' not a composite file\n",
3056 llapi_layout_free(head);
3060 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3063 "%s setstripe: cannot get prev extent: %s\n",
3064 progname, strerror(errno));
3065 llapi_layout_free(head);
3069 llapi_layout_free(head);
3073 /* Make sure we use the first component of the layout to be added. */
3074 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3077 "%s setstripe: cannot move component cursor: %s\n",
3078 progname, strerror(errno));
3082 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3084 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3085 progname, strerror(errno));
3089 if (start == 0 && end == 0) {
3090 rc = layout_extend_comp(layout, start, &end,
3091 comp_add ? prev_end : 0);
3096 if (start > prev_end || end < prev_end) {
3098 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3099 progname, start, end, prev_end);
3103 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3106 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3107 progname, prev_end, end, strerror(errno));
3114 static int mirror_adjust_first_extents(struct mirror_args *list)
3122 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3125 list = list->m_next;
3131 static inline bool arg_is_eof(char *arg)
3133 return !strncmp(arg, "-1", strlen("-1")) ||
3134 !strncmp(arg, "EOF", strlen("EOF")) ||
3135 !strncmp(arg, "eof", strlen("eof"));
3139 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3141 * Return: Valid mirror_args pointer on success and
3142 * NULL if memory allocation fails.
3144 static struct mirror_args *lfs_mirror_alloc(void)
3146 struct mirror_args *mirror = NULL;
3149 mirror = calloc(1, sizeof(*mirror));
3151 mirror->m_inherit = false;
3162 * lfs_mirror_free() - Free memory allocated for a mirror argument
3164 * @mirror: Previously allocated mirror argument structure by
3165 * lfs_mirror_alloc().
3167 * Free memory allocated for @mirror.
3171 static void lfs_mirror_free(struct mirror_args *mirror)
3173 if (mirror->m_layout)
3174 llapi_layout_free(mirror->m_layout);
3179 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3180 * @mirror_list: Previously allocated mirror list.
3182 * Free memory allocated for @mirror_list.
3186 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3188 struct mirror_args *next_mirror = NULL;
3190 while (mirror_list) {
3191 next_mirror = mirror_list->m_next;
3192 lfs_mirror_free(mirror_list);
3193 mirror_list = next_mirror;
3205 LFS_COMP_NO_VERIFY_OPT,
3207 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3209 LFS_MIRROR_STATE_OPT,
3211 LFS_MIRROR_INDEX_OPT,
3212 LFS_LAYOUT_FOREIGN_OPT,
3218 static int lfs_setstripe_internal(int argc, char **argv,
3219 enum setstripe_origin opc)
3221 struct lfs_setstripe_args lsa = { 0 };
3222 struct llapi_stripe_param *param = NULL;
3223 struct find_param migrate_mdt_param = {
3233 unsigned long long size_units = 1;
3234 bool migrate_mode = false;
3235 bool migrate_mdt_mode = false;
3236 bool setstripe_mode = false;
3237 bool migration_block = false;
3238 __u64 migration_flags = 0;
3239 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3240 int comp_del = 0, comp_set = 0;
3243 struct llapi_layout *layout = NULL;
3244 struct llapi_layout **lpp = &layout;
3245 bool mirror_mode = false;
3246 bool has_m_file = false;
3247 __u32 mirror_count = 0;
3248 enum mirror_flags mirror_flags = 0;
3249 struct mirror_args *mirror_list = NULL;
3250 struct mirror_args *new_mirror = NULL;
3251 struct mirror_args *last_mirror = NULL;
3252 __u16 mirror_id = 0;
3254 bool from_yaml = false;
3255 bool from_copy = false;
3256 char *template = NULL;
3257 bool foreign_mode = false;
3259 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3260 char *mode_opt = NULL;
3261 mode_t previous_umask = 0;
3262 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3264 struct option long_opts[] = {
3265 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3266 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3267 /* --block is only valid in migrate mode */
3268 { .val = 'b', .name = "block", .has_arg = no_argument },
3269 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3270 { .val = LFS_COMP_ADD_OPT,
3271 .name = "comp-add", .has_arg = no_argument },
3272 { .val = LFS_COMP_ADD_OPT,
3273 .name = "component-add", .has_arg = no_argument },
3274 { .val = LFS_COMP_DEL_OPT,
3275 .name = "comp-del", .has_arg = no_argument },
3276 { .val = LFS_COMP_DEL_OPT,
3277 .name = "component-del", .has_arg = no_argument },
3278 { .val = LFS_COMP_FLAGS_OPT,
3279 .name = "comp-flags", .has_arg = required_argument },
3280 { .val = LFS_COMP_FLAGS_OPT,
3281 .name = "component-flags",
3282 .has_arg = required_argument },
3283 { .val = LFS_COMP_SET_OPT,
3284 .name = "comp-set", .has_arg = no_argument },
3285 { .val = LFS_COMP_SET_OPT,
3286 .name = "component-set",
3287 .has_arg = no_argument},
3288 { .val = LFS_COMP_NO_VERIFY_OPT,
3289 .name = "no-verify", .has_arg = no_argument},
3290 { .val = LFS_LAYOUT_FLAGS_OPT,
3291 .name = "flags", .has_arg = required_argument},
3292 { .val = LFS_LAYOUT_FOREIGN_OPT,
3293 .name = "foreign", .has_arg = optional_argument},
3294 { .val = LFS_MIRROR_ID_OPT,
3295 .name = "mirror-id", .has_arg = required_argument},
3296 { .val = LFS_MODE_OPT,
3297 .name = "mode", .has_arg = required_argument},
3298 { .val = LFS_LAYOUT_COPY,
3299 .name = "copy", .has_arg = required_argument},
3300 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3301 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3302 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3303 { .val = 'C', .name = "overstripe-count",
3304 .has_arg = required_argument},
3305 { .val = 'd', .name = "delete", .has_arg = no_argument},
3306 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3307 /* --non-direct is only valid in migrate mode */
3308 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3309 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3310 { .val = 'E', .name = "component-end",
3311 .has_arg = required_argument},
3312 { .val = 'f', .name = "file", .has_arg = required_argument },
3313 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3314 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3315 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3316 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3317 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3318 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3319 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3320 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3321 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3322 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3323 { .val = 'L', .name = "layout", .has_arg = required_argument },
3324 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3325 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3326 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3327 /* --non-block is only valid in migrate mode */
3328 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3329 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3330 { .val = 'o', .name = "ost", .has_arg = required_argument },
3331 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3332 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3333 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3335 { .val = 'p', .name = "pool", .has_arg = required_argument },
3336 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3337 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3338 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3339 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3340 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3341 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3342 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3343 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3344 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3345 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3346 /* --verbose is only valid in migrate mode */
3347 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3348 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3349 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3350 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3351 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3354 setstripe_args_init(&lsa);
3356 migrate_mode = (opc == SO_MIGRATE);
3357 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3358 setstripe_mode = (opc == SO_SETSTRIPE);
3359 if (opc == SO_MIRROR_DELETE) {
3361 mirror_flags = MF_DESTROY;
3364 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3366 while ((c = getopt_long(argc, argv,
3367 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3368 long_opts, NULL)) >= 0) {
3374 case LFS_COMP_ADD_OPT:
3377 case LFS_COMP_DEL_OPT:
3380 case LFS_COMP_FLAGS_OPT:
3381 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3382 &lsa.lsa_comp_neg_flags);
3385 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3387 "%s: inverted flags are not supported\n",
3392 case LFS_COMP_SET_OPT:
3395 case LFS_COMP_NO_VERIFY_OPT:
3396 mirror_flags |= MF_NO_VERIFY;
3398 case LFS_MIRROR_ID_OPT:
3399 mirror_id = strtoul(optarg, &end, 0);
3400 if (*end != '\0' || mirror_id == 0) {
3402 "%s %s: invalid mirror ID '%s'\n",
3403 progname, argv[0], optarg);
3407 case LFS_LAYOUT_FLAGS_OPT: {
3410 /* check for numeric flags (foreign and mirror cases) */
3411 if (setstripe_mode && !mirror_mode && !last_mirror) {
3412 flags = strtoul(optarg, &end, 16);
3415 "%s %s: bad flags '%s'\n",
3416 progname, argv[0], optarg);
3422 if (!mirror_mode || !last_mirror) {
3424 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3429 result = comp_str2flags(optarg, &last_mirror->m_flags,
3436 "%s: inverted flags are not supported\n",
3441 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3443 "%s: unsupported mirror flags: %s\n",
3450 case LFS_LAYOUT_FOREIGN_OPT:
3452 /* check pure numeric */
3453 type = strtoul(optarg, &end, 0);
3456 type = check_foreign_type_name(optarg);
3457 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3459 "%s %s: unrecognized foreign type '%s'\n",
3466 foreign_mode = true;
3471 mode = strtoul(mode_opt, &end, 8);
3474 "%s %s: bad mode '%s'\n",
3475 progname, argv[0], mode_opt);
3478 previous_umask = umask(0);
3481 case LFS_LAYOUT_COPY:
3486 if (!migrate_mode) {
3488 "%s %s: -b|--block valid only for migrate command\n",
3492 migration_block = true;
3495 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3497 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3501 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3504 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3507 "%s %s: invalid stripe count '%s'\n",
3508 progname, argv[0], optarg);
3512 if (lsa.lsa_stripe_count == -1)
3513 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3516 /* delete the default striping pattern */
3518 if (opc == SO_MIRROR_SPLIT) {
3521 "%s %s: -d cannot used with -f\n",
3525 mirror_flags |= MF_DESTROY;
3529 if (!migrate_mode) {
3531 "%s %s: -D|--non-direct is valid only for migrate command\n",
3535 migration_flags |= MIGRATION_NONDIRECT;
3538 if (lsa.lsa_comp_end != 0) {
3539 result = comp_args_to_layout(lpp, &lsa, true);
3541 fprintf(stderr, "%s: invalid layout\n",
3546 setstripe_args_init_inherit(&lsa);
3549 if (arg_is_eof(optarg)) {
3550 lsa.lsa_comp_end = LUSTRE_EOF;
3552 result = llapi_parse_size(optarg,
3555 /* assume units of KB if too small */
3556 if (lsa.lsa_comp_end < 4096)
3557 lsa.lsa_comp_end *= 1024;
3559 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3561 "%s %s: invalid component end '%s'\n",
3562 progname, argv[0], optarg);
3568 if (!migrate_mode) {
3570 "--mdt-hash is valid only for migrate command\n");
3574 lsa.lsa_pattern = check_hashtype(optarg);
3575 if (lsa.lsa_pattern == 0) {
3577 "%s %s: bad stripe hash type '%s'\n",
3578 progname, argv[0], optarg);
3583 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3586 "%s %s: invalid stripe offset '%s'\n",
3587 progname, argv[0], optarg);
3590 if (lsa.lsa_stripe_off == -1)
3591 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3594 comp_id = strtoul(optarg, &end, 0);
3595 if (*end != '\0' || comp_id == 0 ||
3596 comp_id > LCME_ID_MAX) {
3598 "%s %s: invalid component ID '%s'\n",
3599 progname, argv[0], optarg);
3604 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3606 "error: %s: invalid option: %s\n",
3607 progname, argv[optopt + 1]);
3610 if (opc == SO_MIRROR_EXTEND) {
3613 "error: %s: '-N' must exist in front of '%s'\n",
3614 progname, argv[optopt + 1]);
3617 last_mirror->m_file = optarg;
3618 last_mirror->m_count = 1;
3622 mirror_list = lfs_mirror_alloc();
3623 mirror_list->m_file = optarg;
3628 if (strcmp(argv[optind - 1], "mdt") == 0) {
3629 /* Can be only the first component */
3633 "error: 'mdt' layout can be only the first one\n");
3636 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3639 "error: 'mdt' layout size is too big\n");
3642 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3643 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3646 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3652 if (!migrate_mode) {
3654 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3658 migrate_mdt_mode = true;
3659 lsa.lsa_nr_tgts = parse_targets(tgts,
3660 sizeof(tgts) / sizeof(__u32),
3661 lsa.lsa_nr_tgts, optarg, NULL);
3662 if (lsa.lsa_nr_tgts < 0) {
3664 "%s %s: invalid MDT target(s) '%s'\n",
3665 progname, argv[0], optarg);
3669 lsa.lsa_tgts = tgts;
3670 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3671 lsa.lsa_stripe_off = tgts[0];
3674 if (!migrate_mode) {
3676 "%s %s: -n|--non-block valid only for migrate command\n",
3680 migration_flags |= MIGRATION_NONBLOCK;
3683 if (opc == SO_SETSTRIPE) {
3684 opc = SO_MIRROR_CREATE;
3689 mirror_count = strtoul(optarg, &end, 0);
3690 if (*end != '\0' || mirror_count == 0) {
3692 "error: %s: bad mirror count: %s\n",
3699 new_mirror = lfs_mirror_alloc();
3700 new_mirror->m_count = mirror_count;
3703 mirror_list = new_mirror;
3706 /* wrap up last mirror */
3707 if (!setstripe_args_specified(&lsa))
3708 last_mirror->m_inherit = true;
3709 if (lsa.lsa_comp_end == 0)
3710 lsa.lsa_comp_end = LUSTRE_EOF;
3712 result = comp_args_to_layout(lpp, &lsa, true);
3714 lfs_mirror_free(new_mirror);
3718 setstripe_args_init_inherit(&lsa);
3720 last_mirror->m_next = new_mirror;
3723 last_mirror = new_mirror;
3724 lpp = &last_mirror->m_layout;
3727 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3728 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3730 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3732 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3734 "%s %s: -o|--ost incompatible with DoM layout\n",
3739 * -o allows overstriping, and must note it because
3740 * parse_targets is shared with MDT striping, which
3741 * does not allow duplicates
3743 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3744 lsa.lsa_nr_tgts = parse_targets(tgts,
3745 sizeof(tgts) / sizeof(__u32),
3746 lsa.lsa_nr_tgts, optarg,
3748 if (lsa.lsa_nr_tgts < 0) {
3750 "%s %s: invalid OST target(s) '%s'\n",
3751 progname, argv[0], optarg);
3755 lsa.lsa_tgts = tgts;
3756 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3757 lsa.lsa_stripe_off = tgts[0];
3762 lsa.lsa_pool_name = optarg;
3764 if (strlen(lsa.lsa_pool_name) == 0 ||
3765 strncmp(lsa.lsa_pool_name, "none",
3766 LOV_MAXPOOLNAME) == 0)
3767 lsa.lsa_pool_name = NULL;
3770 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3772 /* assume units of KB if too small to be valid */
3773 if (lsa.lsa_stripe_size < 4096)
3774 lsa.lsa_stripe_size *= 1024;
3776 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3778 "%s %s: invalid stripe size '%s'\n",
3779 progname, argv[0], optarg);
3784 if (!migrate_mode) {
3786 "%s %s: -v|--verbose valid only for migrate command\n",
3790 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3791 migration_flags = MIGRATION_VERBOSE;
3801 result = llapi_parse_size(optarg,
3802 &lsa.lsa_extension_size,
3806 "%s %s: invalid extension size '%s'\n",
3807 progname, argv[0], optarg);
3811 lsa.lsa_extension_comp = true;
3814 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3815 progname, argv[0], argv[optind - 1]);
3820 fname = argv[optind];
3822 if (optind == argc) {
3823 fprintf(stderr, "%s %s: FILE must be specified\n",
3828 /* lfs migrate $filename should keep the file's layout by default */
3829 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3833 if (xattr && !foreign_mode) {
3835 * only print a warning as this is harmless and will be ignored
3838 "%s %s: xattr has been specified for non-foreign layout\n",
3840 } else if (foreign_mode && !xattr) {
3842 "%s %s: xattr must be provided in foreign mode\n",
3847 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3848 comp_set || comp_id || delete || from_copy ||
3849 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3852 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3857 if (mirror_mode && mirror_count == 0) {
3859 "error: %s: --mirror-count|-N option is required\n",
3866 if (!setstripe_args_specified(&lsa))
3867 last_mirror->m_inherit = true;
3868 if (lsa.lsa_comp_end == 0)
3869 lsa.lsa_comp_end = LUSTRE_EOF;
3872 if (lsa.lsa_comp_end != 0) {
3873 result = comp_args_to_layout(lpp, &lsa, true);
3875 fprintf(stderr, "error: %s: invalid layout\n",
3882 if (mirror_flags & MF_NO_VERIFY) {
3883 if (opc != SO_MIRROR_EXTEND) {
3885 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3889 } else if (!has_m_file) {
3891 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3898 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
3900 "%s %s: --component-set doesn't have component-id set\n",
3905 if ((delete + comp_set + comp_del + comp_add) > 1) {
3907 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3912 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3913 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3915 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3920 if ((comp_set || comp_del) &&
3921 (setstripe_args_specified(&lsa) || layout != NULL)) {
3923 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3928 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3930 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3935 if (comp_add || comp_del) {
3938 result = lstat(fname, &st);
3939 if (result == 0 && S_ISDIR(st.st_mode)) {
3941 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3948 "error: %s: can't use --component-add or --component-del for mirror operation\n",
3957 "%s %s: option -E must be specified with --component-add\n",
3963 if (from_yaml && from_copy) {
3965 "%s: can't specify --yaml and --copy together\n",
3970 if ((from_yaml || from_copy) &&
3971 (setstripe_args_specified(&lsa) || layout != NULL)) {
3973 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
3978 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3980 "%s %s: options --non-block and --block are mutually exclusive\n",
3985 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3986 opc != SO_MIRROR_DELETE && comp_id != 0) {
3988 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3993 if (migrate_mdt_mode) {
3994 struct lmv_user_md *lmu;
3996 /* initialize migrate mdt parameters */
3997 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3998 LMV_USER_MAGIC_SPECIFIC));
4001 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4002 progname, argv[0], strerror(ENOMEM));
4006 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4007 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4008 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4010 "%s %s: migrate should specify MDT index\n",
4015 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4016 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4017 lmu->lum_hash_type = lsa.lsa_pattern;
4019 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4020 if (lsa.lsa_pool_name) {
4021 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4022 sizeof(lmu->lum_pool_name) - 1);
4023 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4025 if (lsa.lsa_nr_tgts > 1) {
4028 if (lsa.lsa_stripe_count > 0 &&
4029 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4030 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4032 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4033 progname, lsa.lsa_stripe_count,
4039 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4040 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4041 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4042 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4044 lmu->lum_magic = LMV_USER_MAGIC;
4047 migrate_mdt_param.fp_lmv_md = lmu;
4048 migrate_mdt_param.fp_migrate = 1;
4049 } else if (!layout) {
4050 /* initialize stripe parameters */
4051 param = calloc(1, offsetof(typeof(*param),
4052 lsp_osts[lsa.lsa_nr_tgts]));
4055 "%s %s: cannot allocate memory for parameters: %s\n",
4056 progname, argv[0], strerror(ENOMEM));
4061 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4062 param->lsp_stripe_size = lsa.lsa_stripe_size;
4063 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4064 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4065 param->lsp_stripe_count = -1;
4067 param->lsp_stripe_count = lsa.lsa_stripe_count;
4069 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4070 param->lsp_stripe_offset = -1;
4072 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4073 param->lsp_stripe_pattern =
4074 llapi_pattern_to_lov(lsa.lsa_pattern);
4075 if (param->lsp_stripe_pattern == EINVAL) {
4076 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4081 param->lsp_pool = lsa.lsa_pool_name;
4082 param->lsp_is_specific = false;
4083 if (lsa.lsa_nr_tgts > 0) {
4084 if (lsa.lsa_stripe_count > 0 &&
4085 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4086 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4087 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4089 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4090 argv[0], lsa.lsa_stripe_count,
4096 param->lsp_is_specific = true;
4097 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4098 memcpy(param->lsp_osts, tgts,
4099 sizeof(*tgts) * lsa.lsa_nr_tgts);
4104 /* generate a layout from a YAML template */
4105 result = lfs_comp_create_from_yaml(template, &layout,
4109 "error: %s: can't create composite layout from template file %s\n",
4115 if (layout != NULL || mirror_list != NULL) {
4117 result = mirror_adjust_first_extents(mirror_list);
4119 result = layout_adjust_first_extent(fname, layout,
4121 if (result == -ENODATA)
4123 else if (result != 0) {
4124 fprintf(stderr, "error: %s: invalid layout\n",
4130 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4132 layout = llapi_layout_get_by_path(template ?: fname, 0);
4135 "%s: can't create composite layout from file %s: %s\n",
4136 progname, template ?: fname,
4143 if (migrate_mdt_mode) {
4144 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4145 } else if (migrate_mode) {
4146 result = lfs_migrate(fname, migration_flags, param,
4148 } else if (comp_set != 0) {
4149 result = lfs_component_set(fname, comp_id,
4152 lsa.lsa_comp_neg_flags);
4153 } else if (comp_del != 0) {
4154 result = lfs_component_del(fname, comp_id,
4156 lsa.lsa_comp_neg_flags);
4157 } else if (comp_add != 0) {
4158 result = lfs_component_add(fname, layout);
4159 } else if (opc == SO_MIRROR_CREATE) {
4160 result = mirror_create(fname, mirror_list);
4161 } else if (opc == SO_MIRROR_EXTEND) {
4162 result = mirror_extend(fname, mirror_list,
4164 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4165 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4167 "%s: no mirror specified to delete from '%s'\n",
4171 if (lsa.lsa_pool_name)
4172 mirror_flags |= MF_COMP_POOL;
4173 else if (mirror_id != 0)
4174 comp_id = mirror_id;
4176 mirror_flags |= MF_COMP_ID;
4177 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4179 has_m_file ? mirror_list->m_file :
4181 } else if (layout) {
4182 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4188 } else if (foreign_mode) {
4189 result = llapi_file_create_foreign(fname, mode, type,
4196 result = llapi_file_open_param(fname,
4205 /* Save the first error encountered. */
4213 umask(previous_umask);
4216 free(migrate_mdt_param.fp_lmv_md);
4217 llapi_layout_free(layout);
4218 lfs_mirror_list_free(mirror_list);
4223 llapi_layout_free(layout);
4224 lfs_mirror_list_free(mirror_list);
4228 static int lfs_poollist(int argc, char **argv)
4233 return llapi_poollist(argv[1]);
4236 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4237 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4242 char *endptr = "AD";
4247 else if (str[0] == '-')
4253 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4254 long long val = strtoll(timebuf, &endptr, 0);
4259 unit *= 52; /* 52 weeks + 1 day below */
4260 case 'w': /* fallthrough */
4262 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4263 param->fp_time_margin *= (1 + unit / 52);
4264 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4265 case '\0': /* days are default unit if none used */
4266 case 'd': /* fallthrough */
4268 case 'h': /* fallthrough */
4270 case 'm': /* fallthrough */
4272 case 's': /* fallthrough */
4274 /* don't need to multiply by 1 for seconds */
4277 "%s find: bad time string '%s': %s\n",
4278 progname, timebuf, strerror(EINVAL));
4282 if (param->fp_time_margin == 0 ||
4283 (*endptr && unit < param->fp_time_margin))
4284 param->fp_time_margin = unit;
4291 fprintf(stderr, "%s find: bad time '%s': too large\n",
4301 static int str2quotaid(__u32 *id, const char *arg)
4303 unsigned long int projid_tmp = 0;
4304 char *endptr = NULL;
4306 projid_tmp = strtoul(arg, &endptr, 10);
4307 if (*endptr != '\0')
4309 if (projid_tmp >= UINT32_MAX)
4316 static int name2uid(unsigned int *id, const char *name)
4318 struct passwd *passwd;
4320 passwd = getpwnam(name);
4323 *id = passwd->pw_uid;
4328 static int name2gid(unsigned int *id, const char *name)
4330 struct group *group;
4332 group = getgrnam(name);
4335 *id = group->gr_gid;
4340 static inline int name2projid(unsigned int *id, const char *name)
4345 static int uid2name(char **name, unsigned int id)
4347 struct passwd *passwd;
4349 passwd = getpwuid(id);
4352 *name = passwd->pw_name;
4357 static inline int gid2name(char **name, unsigned int id)
4359 struct group *group;
4361 group = getgrgid(id);
4364 *name = group->gr_name;
4369 static int name2layout(__u32 *layout, char *name)
4371 char *ptr, *layout_name;
4374 for (ptr = name; ; ptr = NULL) {
4375 layout_name = strtok(ptr, ",");
4378 if (strcmp(layout_name, "released") == 0)
4379 *layout |= LOV_PATTERN_F_RELEASED;
4380 else if (strcmp(layout_name, "raid0") == 0)
4381 *layout |= LOV_PATTERN_RAID0;
4382 else if (strcmp(layout_name, "mdt") == 0)
4383 *layout |= LOV_PATTERN_MDT;
4384 else if (strcmp(layout_name, "overstriping") == 0)
4385 *layout |= LOV_PATTERN_OVERSTRIPING;
4392 static int lfs_find(int argc, char **argv)
4397 struct find_param param = {
4400 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4402 struct option long_opts[] = {
4403 { .val = 'A', .name = "atime", .has_arg = required_argument },
4404 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4405 { .val = 'B', .name = "btime", .has_arg = required_argument },
4406 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4407 { .val = LFS_COMP_COUNT_OPT,
4408 .name = "comp-count", .has_arg = required_argument },
4409 { .val = LFS_COMP_COUNT_OPT,
4410 .name = "component-count",
4411 .has_arg = required_argument },
4412 { .val = LFS_COMP_FLAGS_OPT,
4413 .name = "comp-flags", .has_arg = required_argument },
4414 { .val = LFS_COMP_FLAGS_OPT,
4415 .name = "component-flags",
4416 .has_arg = required_argument },
4417 { .val = LFS_COMP_START_OPT,
4418 .name = "comp-start", .has_arg = required_argument },
4419 { .val = LFS_COMP_START_OPT,
4420 .name = "component-start",
4421 .has_arg = required_argument },
4422 { .val = LFS_MIRROR_STATE_OPT,
4423 .name = "mirror-state", .has_arg = required_argument },
4424 { .val = LFS_LAYOUT_FOREIGN_OPT,
4425 .name = "foreign", .has_arg = optional_argument},
4426 { .val = LFS_NEWERXY_OPT,
4427 .name = "newer", .has_arg = required_argument},
4428 { .val = LFS_NEWERXY_OPT,
4429 .name = "neweraa", .has_arg = required_argument},
4430 { .val = LFS_NEWERXY_OPT,
4431 .name = "neweram", .has_arg = required_argument},
4432 { .val = LFS_NEWERXY_OPT,
4433 .name = "newerac", .has_arg = required_argument},
4434 { .val = LFS_NEWERXY_OPT,
4435 .name = "newerab", .has_arg = required_argument},
4436 { .val = LFS_NEWERXY_OPT,
4437 .name = "newerma", .has_arg = required_argument},
4438 { .val = LFS_NEWERXY_OPT,
4439 .name = "newermm", .has_arg = required_argument},
4440 { .val = LFS_NEWERXY_OPT,
4441 .name = "newermc", .has_arg = required_argument},
4442 { .val = LFS_NEWERXY_OPT,
4443 .name = "newermb", .has_arg = required_argument},
4444 { .val = LFS_NEWERXY_OPT,
4445 .name = "newerca", .has_arg = required_argument},
4446 { .val = LFS_NEWERXY_OPT,
4447 .name = "newercm", .has_arg = required_argument},
4448 { .val = LFS_NEWERXY_OPT,
4449 .name = "newercc", .has_arg = required_argument},
4450 { .val = LFS_NEWERXY_OPT,
4451 .name = "newercb", .has_arg = required_argument},
4452 { .val = LFS_NEWERXY_OPT,
4453 .name = "newerba", .has_arg = required_argument},
4454 { .val = LFS_NEWERXY_OPT,
4455 .name = "newerbm", .has_arg = required_argument},
4456 { .val = LFS_NEWERXY_OPT,
4457 .name = "newerbc", .has_arg = required_argument},
4458 { .val = LFS_NEWERXY_OPT,
4459 .name = "newerbb", .has_arg = required_argument},
4460 { .val = LFS_NEWERXY_OPT,
4461 .name = "newerBa", .has_arg = required_argument},
4462 { .val = LFS_NEWERXY_OPT,
4463 .name = "newerBm", .has_arg = required_argument},
4464 { .val = LFS_NEWERXY_OPT,
4465 .name = "newerBc", .has_arg = required_argument},
4466 { .val = LFS_NEWERXY_OPT,
4467 .name = "newerBB", .has_arg = required_argument},
4468 { .val = LFS_NEWERXY_OPT,
4469 .name = "newerat", .has_arg = required_argument},
4470 { .val = LFS_NEWERXY_OPT,
4471 .name = "newermt", .has_arg = required_argument},
4472 { .val = LFS_NEWERXY_OPT,
4473 .name = "newerct", .has_arg = required_argument},
4474 { .val = LFS_NEWERXY_OPT,
4475 .name = "newerbt", .has_arg = required_argument},
4476 { .val = LFS_NEWERXY_OPT,
4477 .name = "newerBt", .has_arg = required_argument},
4478 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4479 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4480 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4481 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4482 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4483 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4484 { .val = 'E', .name = "component-end",
4485 .has_arg = required_argument },
4486 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4487 { .val = LFS_LAYOUT_FOREIGN_OPT,
4488 .name = "foreign", .has_arg = optional_argument},
4489 { .val = 'g', .name = "gid", .has_arg = required_argument },
4490 { .val = 'G', .name = "group", .has_arg = required_argument },
4491 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4492 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4493 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4494 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4495 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4496 { .val = 'L', .name = "layout", .has_arg = required_argument },
4497 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4498 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4499 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4500 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4501 { .val = 'n', .name = "name", .has_arg = required_argument },
4502 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4503 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4504 { .val = 'O', .name = "obd", .has_arg = required_argument },
4505 { .val = 'O', .name = "ost", .has_arg = required_argument },
4506 /* no short option for pool yet, can be 'p' after 2.18 */
4507 { .val = LFS_POOL_OPT,
4508 .name = "pool", .has_arg = required_argument },
4509 { .val = '0', .name = "print0", .has_arg = no_argument },
4510 { .val = 'P', .name = "print", .has_arg = no_argument },
4511 { .val = LFS_PROJID_OPT,
4512 .name = "projid", .has_arg = required_argument },
4513 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4514 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4515 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4516 { .val = 's', .name = "size", .has_arg = required_argument },
4517 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4518 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4519 { .val = 't', .name = "type", .has_arg = required_argument },
4520 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4521 { .val = 'u', .name = "uid", .has_arg = required_argument },
4522 { .val = 'U', .name = "user", .has_arg = required_argument },
4523 { .val = 'z', .name = "extension-size",
4524 .has_arg = required_argument },
4525 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4526 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4527 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4541 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4542 while ((c = getopt_long_only(argc, argv,
4543 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4544 long_opts, &optidx)) >= 0) {
4549 /* '!' is part of option */
4551 * when getopt_long_only() finds a string which is not
4552 * an option nor a known option argument it returns 1
4553 * in that case if we already have found pathstart and pathend
4554 * (i.e. we have the list of pathnames),
4555 * the only supported value is "!"
4557 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4558 if (!isoption && pathend != -1) {
4560 "err: %s: filename|dirname must either precede options or follow options\n",
4565 if (!isoption && pathstart == -1)
4566 pathstart = optind - 1;
4567 if (isoption && pathstart != -1 && pathend == -1)
4568 pathend = optind - 2;
4575 * unknown; opt is "!" or path component,
4576 * checking done above.
4578 if (strcmp(optarg, "!") == 0)
4582 xtime = ¶m.fp_atime;
4583 xsign = ¶m.fp_asign;
4584 param.fp_exclude_atime = !!neg_opt;
4585 /* no break, this falls through to 'B' for btime */
4588 xtime = ¶m.fp_btime;
4589 xsign = ¶m.fp_bsign;
4590 param.fp_exclude_btime = !!neg_opt;
4592 /* no break, this falls through to 'C' for ctime */
4595 xtime = ¶m.fp_ctime;
4596 xsign = ¶m.fp_csign;
4597 param.fp_exclude_ctime = !!neg_opt;
4599 /* no break, this falls through to 'M' for mtime */
4602 xtime = ¶m.fp_mtime;
4603 xsign = ¶m.fp_msign;
4604 param.fp_exclude_mtime = !!neg_opt;
4606 rc = set_time(¶m, &t, xtime, optarg);
4607 if (rc == LONG_MAX) {
4615 if (optarg[0] == '+') {
4616 param.fp_blocks_sign = -1;
4618 } else if (optarg[0] == '-') {
4619 param.fp_blocks_sign = 1;
4623 param.fp_blocks_units = 1024;
4624 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4625 ¶m.fp_blocks_units, 0);
4627 fprintf(stderr, "error: bad blocks '%s'\n",
4631 param.fp_check_blocks = 1;
4632 param.fp_exclude_blocks = !!neg_opt;
4634 case LFS_COMP_COUNT_OPT:
4635 if (optarg[0] == '+') {
4636 param.fp_comp_count_sign = -1;
4638 } else if (optarg[0] == '-') {
4639 param.fp_comp_count_sign = 1;
4643 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4644 if (*endptr != '\0') {
4646 "error: bad component count '%s'\n",
4650 param.fp_check_comp_count = 1;
4651 param.fp_exclude_comp_count = !!neg_opt;
4653 case LFS_COMP_FLAGS_OPT:
4654 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4655 ¶m.fp_comp_neg_flags);
4658 "error: bad component flags '%s'\n",
4662 param.fp_check_comp_flags = 1;
4664 __u32 flags = param.fp_comp_neg_flags;
4666 param.fp_comp_neg_flags = param.fp_comp_flags;
4667 param.fp_comp_flags = flags;
4670 case LFS_COMP_START_OPT:
4671 if (optarg[0] == '+') {
4672 param.fp_comp_start_sign = -1;
4674 } else if (optarg[0] == '-') {
4675 param.fp_comp_start_sign = 1;
4679 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4680 ¶m.fp_comp_start_units, 0);
4683 "error: bad component start '%s'\n",
4687 param.fp_check_comp_start = 1;
4688 param.fp_exclude_comp_start = !!neg_opt;
4690 case LFS_MIRROR_STATE_OPT:
4691 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4692 ¶m.fp_mirror_neg_state);
4695 "error: bad mirrored file state '%s'\n",
4699 param.fp_check_mirror_state = 1;
4701 __u16 state = param.fp_mirror_neg_state;
4703 param.fp_mirror_neg_state =
4704 param.fp_mirror_state;
4705 param.fp_mirror_state = state;
4709 if (optarg[0] == '+') {
4710 param.fp_stripe_count_sign = -1;
4712 } else if (optarg[0] == '-') {
4713 param.fp_stripe_count_sign = 1;
4717 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4718 if (*endptr != '\0') {
4720 "error: bad stripe_count '%s'\n",
4725 param.fp_check_stripe_count = 1;
4726 param.fp_exclude_stripe_count = !!neg_opt;
4729 param.fp_max_depth = strtol(optarg, 0, 0);
4732 if (optarg[0] == '+') {
4733 param.fp_comp_end_sign = -1;
4735 } else if (optarg[0] == '-') {
4736 param.fp_comp_end_sign = 1;
4740 if (arg_is_eof(optarg)) {
4741 param.fp_comp_end = LUSTRE_EOF;
4742 param.fp_comp_end_units = 1;
4745 rc = llapi_parse_size(optarg,
4747 ¶m.fp_comp_end_units, 0);
4748 /* assume units of KB if too small */
4749 if (param.fp_comp_end < 4096)
4750 param.fp_comp_end *= 1024;
4754 "error: bad component end '%s'\n",
4758 param.fp_check_comp_end = 1;
4759 param.fp_exclude_comp_end = !!neg_opt;
4761 case LFS_LAYOUT_FOREIGN_OPT: {
4762 /* all types by default */
4763 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4766 /* check pure numeric */
4767 type = strtoul(optarg, &endptr, 0);
4770 type = check_foreign_type_name(optarg);
4771 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4773 "%s %s: unknown foreign type '%s'\n",
4780 param.fp_foreign_type = type;
4781 param.fp_check_foreign = 1;
4782 param.fp_exclude_foreign = !!neg_opt;
4785 case LFS_NEWERXY_OPT: {
4791 time_t ref = time(NULL);
4793 /* no need to check bad options, they won't get here */
4794 if (strlen(long_opts[optidx].name) == 7) {
4795 x = long_opts[optidx].name[5];
4796 y = long_opts[optidx].name[6];
4800 static const char *const fmts[] = {
4801 "%Y-%m-%d %H:%M:%S",
4804 "%H:%M:%S", /* sometime today */
4813 for (i = 0; fmts[i] != NULL; i++) {
4816 /* Init for times relative to today */
4817 if (strncmp(fmts[i], "%H", 2) == 0)
4818 localtime_r(&ref, &tm);
4820 memset(&tm, 0, sizeof(tm));
4821 ptr = strptime(optarg, fmts[i], &tm);
4823 while (ptr && isspace(*ptr))
4825 if (ptr == optarg + strlen(optarg)) {
4833 "%s: invalid time '%s'\n",
4836 "supported formats are:\n ");
4837 for (i = 0; fmts[i] != NULL; i++)
4838 fprintf(stderr, "'%s', ",
4840 fprintf(stderr, "\n");
4846 } else if (y == 'b' || y == 'B') {
4849 rc = llapi_get_lum_file(optarg, NULL, &stx,
4851 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4852 if (!(stx.stx_mask & STATX_BTIME))
4857 "%s: get btime failed '%s': %s\n",
4863 ref = stx.stx_btime.tv_sec;
4865 struct stat statbuf;
4867 if (stat(optarg, &statbuf) < 0) {
4869 "%s: cannot stat file '%s': %s\n",
4878 ref = statbuf.st_atime;
4881 ref = statbuf.st_mtime;
4884 ref = statbuf.st_ctime;
4888 "%s: invalid Y argument: '%c'\n",
4897 xidx = NEWERXY_ATIME;
4900 xidx = NEWERXY_MTIME;
4903 xidx = NEWERXY_CTIME;
4907 xidx = NEWERXY_BTIME;
4911 "%s: invalid X argument: '%c'\n",
4918 newery = ¶m.fp_newery[xidx][negidx];
4924 *newery = *newery > ref ? ref : *newery;
4926 *newery = *newery > ref ? *newery : ref;
4928 param.fp_newerxy = 1;
4933 rc = name2gid(¶m.fp_gid, optarg);
4935 if (str2quotaid(¶m.fp_gid, optarg)) {
4937 "Group/GID: %s cannot be found.\n",
4943 param.fp_exclude_gid = !!neg_opt;
4944 param.fp_check_gid = 1;
4947 param.fp_hash_type = check_hashtype(optarg);
4948 if (param.fp_hash_type == 0) {
4949 fprintf(stderr, "error: bad hash_type '%s'\n",
4954 param.fp_check_hash_type = 1;
4955 param.fp_exclude_hash_type = !!neg_opt;
4961 ret = name2layout(¶m.fp_layout, optarg);
4964 param.fp_exclude_layout = !!neg_opt;
4965 param.fp_check_layout = 1;
4969 rc = name2uid(¶m.fp_uid, optarg);
4971 if (str2quotaid(¶m.fp_uid, optarg)) {
4973 "User/UID: %s cannot be found.\n",
4979 param.fp_exclude_uid = !!neg_opt;
4980 param.fp_check_uid = 1;
4983 param.fp_pattern = (char *)optarg;
4984 param.fp_exclude_pattern = !!neg_opt;
4987 if (optarg[0] == '+') {
4988 param.fp_mirror_count_sign = -1;
4990 } else if (optarg[0] == '-') {
4991 param.fp_mirror_count_sign = 1;
4995 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4996 if (*endptr != '\0') {
4998 "error: bad mirror count '%s'\n",
5002 param.fp_check_mirror_count = 1;
5003 param.fp_exclude_mirror_count = !!neg_opt;
5008 char *buf, *token, *next, *p;
5012 buf = strdup(optarg);
5018 param.fp_exclude_obd = !!neg_opt;
5021 while (token && *token) {
5022 token = strchr(token, ',');
5029 param.fp_exclude_mdt = !!neg_opt;
5030 param.fp_num_alloc_mdts += len;
5031 tmp = realloc(param.fp_mdt_uuid,
5032 param.fp_num_alloc_mdts *
5033 sizeof(*param.fp_mdt_uuid));
5039 param.fp_mdt_uuid = tmp;
5041 param.fp_exclude_obd = !!neg_opt;
5042 param.fp_num_alloc_obds += len;
5043 tmp = realloc(param.fp_obd_uuid,
5044 param.fp_num_alloc_obds *
5045 sizeof(*param.fp_obd_uuid));
5051 param.fp_obd_uuid = tmp;
5053 for (token = buf; token && *token; token = next) {
5054 struct obd_uuid *puuid;
5058 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5061 ¶m.fp_obd_uuid[param.fp_num_obds++];
5063 p = strchr(token, ',');
5070 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5075 strncpy(puuid->uuid, token,
5076 sizeof(puuid->uuid));
5083 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5087 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5089 "Pool name %s is too long (max %d)\n",
5090 optarg, LOV_MAXPOOLNAME);
5095 * We do check for empty pool because empty pool
5096 * is used to find V1 LOV attributes
5098 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5099 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5100 param.fp_exclude_pool = !!neg_opt;
5101 param.fp_check_pool = 1;
5103 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5104 case 'p': /* want this for --pool, to match getstripe/find */
5106 "warning: -p deprecated, use --print0 or -0\n");
5109 param.fp_zero_end = 1;
5111 case 'P': /* we always print, this option is a no-op */
5113 case LFS_PROJID_OPT:
5114 rc = name2projid(¶m.fp_projid, optarg);
5116 if (str2quotaid(¶m.fp_projid, optarg)) {
5118 "Invalid project ID: %s\n",
5124 param.fp_exclude_projid = !!neg_opt;
5125 param.fp_check_projid = 1;
5128 if (optarg[0] == '+') {
5129 param.fp_size_sign = -1;
5131 } else if (optarg[0] == '-') {
5132 param.fp_size_sign = 1;
5136 ret = llapi_parse_size(optarg, ¶m.fp_size,
5137 ¶m.fp_size_units, 0);
5139 fprintf(stderr, "error: bad file size '%s'\n",
5143 param.fp_check_size = 1;
5144 param.fp_exclude_size = !!neg_opt;
5147 if (optarg[0] == '+') {
5148 param.fp_stripe_size_sign = -1;
5150 } else if (optarg[0] == '-') {
5151 param.fp_stripe_size_sign = 1;
5155 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5156 ¶m.fp_stripe_size_units, 0);
5157 /* assume units of KB if too small to be valid */
5158 if (param.fp_stripe_size < 4096)
5159 param.fp_stripe_size *= 1024;
5161 fprintf(stderr, "error: bad stripe_size '%s'\n",
5165 param.fp_check_stripe_size = 1;
5166 param.fp_exclude_stripe_size = !!neg_opt;
5169 param.fp_exclude_type = !!neg_opt;
5170 switch (optarg[0]) {
5172 param.fp_type = S_IFBLK;
5175 param.fp_type = S_IFCHR;
5178 param.fp_type = S_IFDIR;
5181 param.fp_type = S_IFREG;
5184 param.fp_type = S_IFLNK;
5187 param.fp_type = S_IFIFO;
5190 param.fp_type = S_IFSOCK;
5193 fprintf(stderr, "error: %s: bad type '%s'\n",
5200 if (optarg[0] == '+') {
5201 param.fp_mdt_count_sign = -1;
5203 } else if (optarg[0] == '-') {
5204 param.fp_mdt_count_sign = 1;
5208 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5209 if (*endptr != '\0') {
5210 fprintf(stderr, "error: bad mdt_count '%s'\n",
5215 param.fp_check_mdt_count = 1;
5216 param.fp_exclude_mdt_count = !!neg_opt;
5219 if (optarg[0] == '+') {
5220 param.fp_ext_size_sign = -1;
5222 } else if (optarg[0] == '-') {
5223 param.fp_ext_size_sign = 1;
5227 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5228 ¶m.fp_ext_size_units, 0);
5230 fprintf(stderr, "error: bad ext-size '%s'\n",
5234 param.fp_ext_size /= SEL_UNIT_SIZE;
5235 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5236 param.fp_check_ext_size = 1;
5237 param.fp_exclude_ext_size = !!neg_opt;
5245 if (pathstart == -1) {
5246 fprintf(stderr, "error: %s: no filename|pathname\n",
5250 } else if (pathend == -1) {
5256 rc = llapi_find(argv[pathstart], ¶m);
5259 pathbad = pathstart;
5261 } while (++pathstart < pathend);
5264 fprintf(stderr, "%s: failed for '%s': %s\n",
5265 progname, argv[pathbad], strerror(-rc));
5268 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5269 free(param.fp_obd_uuid);
5271 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5272 free(param.fp_mdt_uuid);
5277 static int lfs_getstripe_internal(int argc, char **argv,
5278 struct find_param *param)
5280 struct option long_opts[] = {
5281 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5282 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5283 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5284 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5285 { .val = LFS_COMP_COUNT_OPT,
5286 .name = "comp-count", .has_arg = no_argument },
5287 { .val = LFS_COMP_COUNT_OPT,
5288 .name = "component-count", .has_arg = no_argument },
5289 { .val = LFS_COMP_FLAGS_OPT,
5290 .name = "comp-flags", .has_arg = optional_argument },
5291 { .val = LFS_COMP_FLAGS_OPT,
5292 .name = "component-flags", .has_arg = optional_argument },
5293 { .val = LFS_COMP_START_OPT,
5294 .name = "comp-start", .has_arg = optional_argument },
5295 { .val = LFS_COMP_START_OPT,
5296 .name = "component-start", .has_arg = optional_argument },
5297 { .val = LFS_MIRROR_INDEX_OPT,
5298 .name = "mirror-index", .has_arg = required_argument },
5299 { .val = LFS_MIRROR_ID_OPT,
5300 .name = "mirror-id", .has_arg = required_argument },
5301 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5302 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5303 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5304 { .val = 'd', .name = "directory", .has_arg = no_argument },
5305 { .val = 'D', .name = "default", .has_arg = no_argument },
5306 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5307 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5308 { .val = 'F', .name = "fid", .has_arg = no_argument },
5309 { .val = 'g', .name = "generation", .has_arg = no_argument },
5310 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5311 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5312 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5313 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5314 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5315 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5316 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5317 { .val = 'L', .name = "layout", .has_arg = no_argument },
5318 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5319 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5320 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5321 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5322 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5323 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5324 { .val = 'O', .name = "obd", .has_arg = required_argument },
5325 { .val = 'O', .name = "ost", .has_arg = required_argument },
5326 { .val = 'p', .name = "pool", .has_arg = no_argument },
5327 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5328 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5329 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5330 { .val = 'R', .name = "raw", .has_arg = no_argument },
5331 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5332 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5333 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5334 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5335 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5336 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5337 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5338 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5339 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5340 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5344 int pathstart = -1, pathend = -1;
5348 while ((c = getopt_long(argc, argv,
5349 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5350 long_opts, NULL)) != -1) {
5354 /* '!' is part of option */
5355 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5356 if (!isoption && pathend != -1) {
5358 "error: %s: filename|dirname must either precede options or follow options\n",
5362 if (!isoption && pathstart == -1)
5363 pathstart = optind - 1;
5364 if (isoption && pathstart != -1 && pathend == -1)
5365 pathend = optind - 2;
5369 /* unknown: opt is "!" */
5370 if (strcmp(optarg, "!") == 0)
5374 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5375 param->fp_verbose |= VERBOSE_COUNT;
5376 param->fp_max_depth = 0;
5379 case LFS_COMP_COUNT_OPT:
5380 param->fp_verbose |= VERBOSE_COMP_COUNT;
5381 param->fp_max_depth = 0;
5383 case LFS_COMP_FLAGS_OPT:
5385 rc = comp_str2flags(optarg,
5386 ¶m->fp_comp_flags,
5387 ¶m->fp_comp_neg_flags);
5390 "error: %s bad component flags '%s'.\n",
5394 param->fp_check_comp_flags = 1;
5396 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5397 param->fp_max_depth = 0;
5400 case LFS_COMP_START_OPT:
5403 if (tmp[0] == '+') {
5404 param->fp_comp_start_sign = -1;
5406 } else if (tmp[0] == '-') {
5407 param->fp_comp_start_sign = 1;
5410 rc = llapi_parse_size(tmp,
5411 ¶m->fp_comp_start,
5412 ¶m->fp_comp_start_units, 0);
5415 "error: %s bad component start '%s'.\n",
5419 param->fp_check_comp_start = 1;
5421 param->fp_verbose |= VERBOSE_COMP_START;
5422 param->fp_max_depth = 0;
5425 case LFS_MIRROR_INDEX_OPT:
5426 if (optarg[0] == '+') {
5427 param->fp_mirror_index_sign = -1;
5429 } else if (optarg[0] == '-') {
5430 param->fp_mirror_index_sign = 1;
5434 param->fp_mirror_index = strtoul(optarg, &end, 0);
5435 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5436 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5438 "%s %s: invalid mirror index '%s'\n",
5439 progname, argv[0], optarg);
5442 if (param->fp_mirror_id != 0) {
5444 "%s %s: can't specify both mirror index and mirror ID\n",
5448 param->fp_check_mirror_index = 1;
5449 param->fp_exclude_mirror_index = !!neg_opt;
5451 case LFS_MIRROR_ID_OPT:
5452 if (optarg[0] == '+') {
5453 param->fp_mirror_id_sign = -1;
5455 } else if (optarg[0] == '-') {
5456 param->fp_mirror_id_sign = 1;
5460 param->fp_mirror_id = strtoul(optarg, &end, 0);
5461 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5462 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5464 "%s %s: invalid mirror ID '%s'\n",
5465 progname, argv[0], optarg);
5468 if (param->fp_mirror_index != 0) {
5470 "%s %s: can't specify both mirror index and mirror ID\n",
5474 param->fp_check_mirror_id = 1;
5475 param->fp_exclude_mirror_id = !!neg_opt;
5478 param->fp_max_depth = 0;
5481 param->fp_get_default_lmv = 1;
5486 if (tmp[0] == '+') {
5487 param->fp_comp_end_sign = -1;
5489 } else if (tmp[0] == '-') {
5490 param->fp_comp_end_sign = 1;
5494 if (arg_is_eof(tmp)) {
5495 param->fp_comp_end = LUSTRE_EOF;
5496 param->fp_comp_end_units = 1;
5499 rc = llapi_parse_size(tmp,
5500 ¶m->fp_comp_end,
5501 ¶m->fp_comp_end_units, 0);
5502 /* assume units of KB if too small */
5503 if (param->fp_comp_end < 4096)
5504 param->fp_comp_end *= 1024;
5508 "error: %s bad component end '%s'.\n",
5512 param->fp_check_comp_end = 1;
5514 param->fp_verbose |= VERBOSE_COMP_END;
5515 param->fp_max_depth = 0;
5519 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5520 param->fp_verbose |= VERBOSE_DFID;
5521 param->fp_max_depth = 0;
5525 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5526 param->fp_verbose |= VERBOSE_GENERATION;
5527 param->fp_max_depth = 0;
5531 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5532 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5533 param->fp_max_depth = 0;
5538 param->fp_comp_id = strtoul(optarg, &end, 0);
5539 if (*end != '\0' || param->fp_comp_id == 0 ||
5540 param->fp_comp_id > LCME_ID_MAX) {
5542 "error: %s bad component id '%s'\n",
5546 param->fp_check_comp_id = 1;
5548 param->fp_max_depth = 0;
5549 param->fp_verbose |= VERBOSE_COMP_ID;
5553 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5554 param->fp_verbose |= VERBOSE_PATTERN;
5555 param->fp_max_depth = 0;
5558 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5561 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5564 if (!(param->fp_verbose & VERBOSE_DETAIL))
5565 param->fp_max_depth = 0;
5566 param->fp_verbose |= VERBOSE_MDTINDEX;
5569 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5570 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5571 param->fp_max_depth = 0;
5575 if (param->fp_obd_uuid) {
5577 "error: %s: only one obduuid allowed",
5581 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5584 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5585 param->fp_verbose |= VERBOSE_POOL;
5586 param->fp_max_depth = 0;
5593 param->fp_recursive = 1;
5599 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5600 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5601 param->fp_max_depth = 0;
5605 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5611 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5612 param->fp_verbose |= VERBOSE_EXT_SIZE;
5613 param->fp_max_depth = 0;
5621 if (pathstart == -1) {
5622 fprintf(stderr, "error: %s: no filename|pathname\n",
5625 } else if (pathend == -1) {
5633 if (param->fp_recursive)
5634 param->fp_max_depth = -1;
5635 else if (param->fp_verbose & VERBOSE_DETAIL)
5636 param->fp_max_depth = 1;
5638 if (!param->fp_verbose)
5639 param->fp_verbose = VERBOSE_DEFAULT;
5640 if (param->fp_quiet)
5641 param->fp_verbose = VERBOSE_OBJID;
5644 rc = llapi_getstripe(argv[pathstart], param);
5645 } while (++pathstart < pathend && !rc);
5648 fprintf(stderr, "error: %s failed for %s.\n",
5649 argv[0], argv[optind - 1]);
5653 static int lfs_tgts(int argc, char **argv)
5655 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5656 struct find_param param;
5657 int index = 0, rc = 0;
5662 if (argc == 2 && !realpath(argv[1], path)) {
5664 fprintf(stderr, "error: invalid path '%s': %s\n",
5665 argv[1], strerror(-rc));
5669 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5670 /* Check if we have a mount point */
5671 if (mntdir[0] == '\0')
5674 memset(¶m, 0, sizeof(param));
5675 if (!strcmp(argv[0], "mdts"))
5676 param.fp_get_lmv = 1;
5678 rc = llapi_ostlist(mntdir, ¶m);
5680 fprintf(stderr, "error: %s: failed on %s\n",
5683 if (path[0] != '\0')
5685 memset(mntdir, 0, PATH_MAX);
5691 static int lfs_getstripe(int argc, char **argv)
5693 struct find_param param = { 0 };
5695 param.fp_max_depth = 1;
5696 return lfs_getstripe_internal(argc, argv, ¶m);
5700 static int lfs_getdirstripe(int argc, char **argv)
5702 struct find_param param = { 0 };
5703 struct option long_opts[] = {
5704 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5705 { .val = 'D', .name = "default", .has_arg = no_argument },
5706 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5707 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5708 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5709 { .val = 'O', .name = "obd", .has_arg = required_argument },
5710 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5711 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5712 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5713 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5717 param.fp_get_lmv = 1;
5719 while ((c = getopt_long(argc, argv,
5720 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5724 param.fp_verbose |= VERBOSE_COUNT;
5727 param.fp_get_default_lmv = 1;
5731 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5733 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5736 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5739 param.fp_verbose |= VERBOSE_HASH_TYPE;
5742 if (param.fp_obd_uuid) {
5744 "error: %s: only one obduuid allowed",
5748 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5751 param.fp_recursive = 1;
5754 param.fp_verbose |= VERBOSE_DETAIL;
5760 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5761 progname, argv[0], argv[optind - 1]);
5769 if (param.fp_recursive)
5770 param.fp_max_depth = -1;
5772 if (!param.fp_verbose)
5773 param.fp_verbose = VERBOSE_DEFAULT;
5776 rc = llapi_getstripe(argv[optind], ¶m);
5777 } while (++optind < argc && !rc);
5780 fprintf(stderr, "error: %s failed for %s.\n",
5781 argv[0], argv[optind - 1]);
5786 MNTDF_INODES = 0x0001,
5787 MNTDF_COOKED = 0x0002,
5788 MNTDF_LAZY = 0x0004,
5789 MNTDF_VERBOSE = 0x0008,
5790 MNTDF_SHOW = 0x0010,
5793 #define COOK(value) \
5796 while (value > 1024) { \
5804 #define CDF "%11llu"
5805 #define HDF "%8.1f%c"
5809 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5811 double avail, used, ratio = 0;
5814 avail = st->os_ffree;
5815 used = st->os_files - st->os_ffree;
5817 avail = st->os_bavail;
5818 used = st->os_blocks - st->os_bfree;
5820 if (avail + used > 0)
5821 ratio = used / (used + avail) * 100;
5823 /* Round up to match df(1) usage percentage */
5824 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5828 * This is to identify various problem states for "lfs df" if .osn_err = true,
5829 * so only show flags reflecting those states by default. Informational states
5830 * are only shown with "-v" and use lower-case names to distinguish them.
5831 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5833 static struct obd_statfs_state_names {
5834 enum obd_statfs_state osn_state;
5835 const char osn_name;
5838 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5839 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5840 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5841 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5842 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5843 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5844 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5845 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5846 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5849 static int showdf(char *mntdir, struct obd_statfs *stat,
5850 char *uuid, enum mntdf_flags flags,
5851 char *type, int index, int rc)
5853 long long avail, used, total;
5855 char *suffix = "KMGTPEZY";
5856 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5857 char tbuf[3 * sizeof(__u64)];
5858 char ubuf[3 * sizeof(__u64)];
5859 char abuf[3 * sizeof(__u64)];
5860 char rbuf[3 * sizeof(__u64)];
5867 if (flags & MNTDF_INODES) {
5868 avail = stat->os_ffree;
5869 used = stat->os_files - stat->os_ffree;
5870 total = stat->os_files;
5872 int shift = flags & MNTDF_COOKED ? 0 : 10;
5874 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5875 used = ((stat->os_blocks - stat->os_bfree) *
5876 stat->os_bsize) >> shift;
5877 total = (stat->os_blocks * stat->os_bsize) >> shift;
5880 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5882 if (flags & MNTDF_COOKED) {
5886 cook_val = (double)total;
5889 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5892 snprintf(tbuf, sizeof(tbuf), CDF, total);
5894 cook_val = (double)used;
5897 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5900 snprintf(ubuf, sizeof(ubuf), CDF, used);
5902 cook_val = (double)avail;
5905 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5908 snprintf(abuf, sizeof(abuf), CDF, avail);
5910 snprintf(tbuf, sizeof(tbuf), CDF, total);
5911 snprintf(ubuf, sizeof(tbuf), CDF, used);
5912 snprintf(abuf, sizeof(tbuf), CDF, avail);
5915 sprintf(rbuf, RDF, ratio);
5916 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5917 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5919 printf("[%s:%d]", type, index);
5921 if (stat->os_state) {
5925 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5926 if (oss_names[i].osn_state & stat->os_state &&
5927 (oss_names[i].osn_err ||
5928 flags & MNTDF_VERBOSE))
5929 printf("%c", oss_names[i].osn_name);
5936 printf(UUF": inactive device\n", uuid);
5939 printf(UUF": %s\n", uuid, strerror(-rc));
5946 struct ll_stat_type {
5951 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5953 struct ll_statfs_data {
5955 struct obd_statfs sd_st;
5958 struct ll_statfs_buf {
5960 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5963 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5964 int ops, struct ll_statfs_buf *lsb)
5966 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5967 struct obd_uuid uuid_buf;
5968 char *poolname = NULL;
5969 struct ll_stat_type types[] = {
5970 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5971 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5972 { .st_name = NULL } };
5973 struct ll_stat_type *tp;
5974 __u64 ost_files = 0;
5975 __u64 ost_ffree = 0;
5983 poolname = strchr(pool, '.');
5985 if (strncmp(fsname, pool, strlen(fsname))) {
5986 fprintf(stderr, "filesystem name incorrect\n");
5994 fd = open(mntdir, O_RDONLY);
5997 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6002 if (flags & MNTDF_SHOW) {
6003 if (flags & MNTDF_INODES)
6004 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6005 "UUID", "Inodes", "IUsed", "IFree",
6006 "IUse%", "Mounted on");
6008 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6010 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6011 "Used", "Available", "Use%", "Mounted on");
6014 for (tp = types; tp->st_name != NULL; tp++) {
6015 bool have_ost = false;
6017 if (!(tp->st_op & ops))
6020 for (index = 0; ; index++) {
6021 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6022 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6023 type = flags & MNTDF_LAZY ?
6024 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6025 rc2 = llapi_obd_fstatfs(fd, type, index,
6026 &stat_buf, &uuid_buf);
6031 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6032 if (!(flags & MNTDF_VERBOSE))
6034 } else if (rc2 < 0 && rc == 0) {
6039 * If we have OSTs then don't report MDT block counts.
6040 * For MDT-only filesystems the expectation is that all
6041 * layouts have a DoM component. For filesystems with
6042 * OSTs, files are not necessarily going to store data
6043 * on MDTs, and MDT space is limited to a fraction of
6044 * OST space, so don't include it in the summary.
6046 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6053 if (poolname && tp->st_op == LL_STATFS_LOV &&
6054 llapi_search_ost(fsname, poolname,
6055 obd_uuid2str(&uuid_buf)) != 1)
6059 * the llapi_obd_fstatfs() call may have returned with
6060 * an error, but if it filled in uuid_buf we will at
6061 * lease use that to print out a message for that OBD.
6062 * If we didn't get anything in the uuid_buf, then fill
6063 * it in so that we can print an error message.
6065 if (uuid_buf.uuid[0] == '\0')
6066 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6067 "%s%04x", tp->st_name, index);
6069 lsb->sb_buf[lsb->sb_count].sd_index = index;
6070 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6073 if (flags & MNTDF_SHOW)
6074 showdf(mntdir, &stat_buf,
6075 obd_uuid2str(&uuid_buf), flags,
6076 tp->st_name, index, rc2);
6081 if (tp->st_op == LL_STATFS_LMV) {
6082 sum.os_ffree += stat_buf.os_ffree;
6083 sum.os_files += stat_buf.os_files;
6084 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6085 ost_files += stat_buf.os_files;
6086 ost_ffree += stat_buf.os_ffree;
6088 sum.os_blocks += stat_buf.os_blocks *
6090 sum.os_bfree += stat_buf.os_bfree *
6092 sum.os_bavail += stat_buf.os_bavail *
6100 * If we have _some_ OSTs, but don't have as many free objects on the
6101 * OST as inodes on the MDTs, reduce the reported number of inodes
6102 * to compensate, so that the "inodes in use" number is correct.
6103 * This should be kept in sync with ll_statfs_internal().
6105 if (ost_files && ost_ffree < sum.os_ffree) {
6106 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6107 sum.os_ffree = ost_ffree;
6109 if (flags & MNTDF_SHOW) {
6111 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6119 static int lfs_setdirstripe(int argc, char **argv)
6122 struct lfs_setstripe_args lsa = { 0 };
6123 struct llapi_stripe_param *param = NULL;
6124 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6127 char *mode_opt = NULL;
6128 bool default_stripe = false;
6129 bool delete = false;
6130 bool foreign_mode = false;
6131 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6132 mode_t previous_mode = 0;
6134 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6135 struct option long_opts[] = {
6136 { .val = 'c', .name = "count", .has_arg = required_argument },
6137 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6138 { .val = 'd', .name = "delete", .has_arg = no_argument },
6139 { .val = 'D', .name = "default", .has_arg = no_argument },
6140 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6141 { .val = LFS_LAYOUT_FLAGS_OPT,
6142 .name = "flags", .has_arg = required_argument },
6143 { .val = LFS_LAYOUT_FOREIGN_OPT,
6144 .name = "foreign", .has_arg = optional_argument},
6145 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6146 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6147 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6148 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6150 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6151 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6152 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6154 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6155 { .val = 'i', .name = "index", .has_arg = required_argument },
6157 { .val = 'o', .name = "mode", .has_arg = required_argument },
6158 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6159 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6161 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6162 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6163 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6167 setstripe_args_init(&lsa);
6169 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6170 long_opts, NULL)) >= 0) {
6177 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6180 "%s %s: invalid stripe count '%s'\n",
6181 progname, argv[0], optarg);
6187 default_stripe = true;
6190 default_stripe = true;
6192 case LFS_LAYOUT_FOREIGN_OPT:
6194 /* check pure numeric */
6195 type = strtoul(optarg, &end, 0);
6198 type = check_foreign_type_name(optarg);
6199 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6201 "%s %s: unknown foreign type '%s'\n",
6208 foreign_mode = true;
6210 case LFS_LAYOUT_FLAGS_OPT:
6211 flags = strtoul(optarg, &end, 16);
6214 "%s %s: bad flags '%s'\n",
6215 progname, argv[0], optarg);
6219 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6222 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6225 lsa.lsa_pattern = check_hashtype(optarg);
6226 if (lsa.lsa_pattern == 0) {
6228 "%s %s: bad stripe hash type '%s'\n",
6229 progname, argv[0], optarg);
6234 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6237 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6238 if (strcmp(argv[optind - 1], "--index") == 0)
6240 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6243 lsa.lsa_nr_tgts = parse_targets(mdts,
6244 sizeof(mdts) / sizeof(__u32),
6245 lsa.lsa_nr_tgts, optarg, NULL);
6246 if (lsa.lsa_nr_tgts < 0) {
6248 "%s %s: invalid MDT target(s) '%s'\n",
6249 progname, argv[0], optarg);
6253 lsa.lsa_tgts = mdts;
6254 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6255 lsa.lsa_stripe_off = mdts[0];
6257 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6260 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6269 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6270 progname, argv[0], argv[optind - 1]);
6275 if (optind == argc) {
6276 fprintf(stderr, "%s %s: DIR must be specified\n",
6281 if (xattr && !foreign_mode) {
6283 * only print a warning as this is armless and will be
6287 "%s %s: xattr has been specified for non-foreign layout\n",
6289 } else if (foreign_mode && !xattr) {
6291 "%s %s: xattr must be provided in foreign mode\n",
6296 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6297 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6299 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6304 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6305 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6306 /* if no parameters set, create directory on least-used MDTs */
6307 lsa.lsa_stripe_off = -1;
6308 lsa.lsa_stripe_count = 1;
6312 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6313 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6315 "%s %s: cannot specify -d with -c or -i options\n",
6321 mode = strtoul(mode_opt, &end, 8);
6324 "%s %s: bad MODE '%s'\n",
6325 progname, argv[0], mode_opt);
6328 previous_mode = umask(0);
6331 /* foreign LMV/dir case */
6333 if (argc > optind + 1) {
6335 "%s %s: cannot specify multiple foreign dirs\n",
6340 dname = argv[optind];
6341 result = llapi_dir_create_foreign(dname, mode, type, flags,
6345 "%s mkdir: can't create foreign dir '%s': %s\n",
6346 progname, dname, strerror(-result));
6351 * initialize stripe parameters, in case param is converted to specific,
6352 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6354 param = calloc(1, offsetof(typeof(*param),
6355 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6356 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6359 "%s %s: cannot allocate memory for parameters: %s\n",
6360 progname, argv[0], strerror(ENOMEM));
6364 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6365 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6366 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6367 lsa.lsa_stripe_count == 0))
6368 lsa.lsa_stripe_count = 1;
6369 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6370 param->lsp_stripe_count = lsa.lsa_stripe_count;
6371 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6372 param->lsp_stripe_offset = -1;
6374 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6375 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6376 param->lsp_stripe_pattern = lsa.lsa_pattern;
6378 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6379 param->lsp_pool = lsa.lsa_pool_name;
6380 param->lsp_is_specific = false;
6381 if (lsa.lsa_nr_tgts > 1) {
6382 if (lsa.lsa_stripe_count > 0 &&
6383 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6384 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6386 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6387 argv[0], lsa.lsa_stripe_count,
6393 param->lsp_is_specific = true;
6394 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6395 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6398 dname = argv[optind];
6400 if (default_stripe) {
6401 result = llapi_dir_set_default_lmv(dname, param);
6404 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6405 progname, dname, strerror(-result));
6409 result = llapi_dir_create(dname, mode, param);
6412 "%s setdirstripe: cannot create dir '%s': %s\n",
6413 progname, dname, strerror(-result));
6414 } while (!result && (dname = argv[++optind]));
6417 umask(previous_mode);
6424 static int lfs_rmentry(int argc, char **argv)
6431 fprintf(stderr, "error: %s: missing dirname\n",
6437 dname = argv[index];
6439 result = llapi_direntry_remove(dname);
6442 "error: %s: remove dir entry '%s' failed\n",
6446 dname = argv[++index];
6451 static int lfs_unlink_foreign(int argc, char **argv)
6458 fprintf(stderr, "error: %s: missing pathname\n",
6465 while (name != NULL) {
6466 result = llapi_unlink_foreign(name);
6469 "error: %s: unlink foreign entry '%s' failed\n",
6473 name = argv[++index];
6478 static int lfs_mv(int argc, char **argv)
6480 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6481 struct find_param param = {
6488 struct option long_opts[] = {
6489 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6490 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6491 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6494 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6496 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6499 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6502 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6504 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6510 param.fp_verbose = VERBOSE_DETAIL;
6513 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6514 progname, argv[optind - 1]);
6519 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6520 fprintf(stderr, "%s mv: MDT index must be specified\n",
6525 if (optind >= argc) {
6526 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6530 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6532 /* initialize migrate mdt parameters */
6533 param.fp_lmv_md = &lmu;
6534 param.fp_migrate = 1;
6535 rc = llapi_migrate_mdt(argv[optind], ¶m);
6537 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6538 progname, argv[optind], lmu.lum_stripe_offset,
6543 static int lfs_osts(int argc, char **argv)
6545 return lfs_tgts(argc, argv);
6548 static int lfs_mdts(int argc, char **argv)
6550 return lfs_tgts(argc, argv);
6553 static int lfs_df(int argc, char **argv)
6555 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6556 enum mntdf_flags flags = MNTDF_SHOW;
6557 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6558 int c, rc = 0, index = 0;
6559 char fsname[PATH_MAX] = "", *pool_name = NULL;
6560 struct option long_opts[] = {
6561 { .val = 'h', .name = "human-readable",
6562 .has_arg = no_argument },
6563 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6564 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6565 { .val = 'p', .name = "pool", .has_arg = required_argument },
6566 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6569 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6572 flags |= MNTDF_COOKED;
6575 flags |= MNTDF_INODES;
6578 flags |= MNTDF_LAZY;
6584 flags |= MNTDF_VERBOSE;
6590 if (optind < argc && !realpath(argv[optind], path)) {
6592 fprintf(stderr, "error: invalid path '%s': %s\n",
6593 argv[optind], strerror(-rc));
6597 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6598 /* Check if we have a mount point */
6599 if (mntdir[0] == '\0')
6602 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6603 if (rc || path[0] != '\0')
6605 fsname[0] = '\0'; /* avoid matching in next loop */
6606 mntdir[0] = '\0'; /* avoid matching in next loop */
6612 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6613 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6617 if (opt_fsname == opt_instance) { /* both true or both false */
6618 rc = llapi_getname(mntdir, buf, buflen);
6619 } else if (opt_fsname) {
6621 * llapi_search_mounts() fills @buf with fsname, but that is not
6622 * called if explicit paths are specified on the command-line
6625 rc = llapi_get_fsname(mntdir, buf, buflen);
6626 } else /* if (opt_instance) */ {
6627 rc = llapi_get_instance(mntdir, buf, buflen);
6631 fprintf(stderr, "cannot get instance for '%s': %s\n",
6632 mntdir, strerror(-rc));
6637 printf("%s %s\n", buf, mntdir);
6639 printf("%s\n", buf);
6644 static int lfs_getname(int argc, char **argv)
6646 struct option long_opts[] = {
6647 { .val = 'h', .name = "help", .has_arg = no_argument },
6648 { .val = 'i', .name = "instance", .has_arg = no_argument },
6649 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6651 bool opt_instance = false, opt_fsname = false;
6652 char fsname[PATH_MAX] = "";
6655 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6658 opt_instance = true;
6669 if (optind == argc) { /* no paths specified, get all paths. */
6670 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6673 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6674 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6675 opt_instance, opt_fsname, true);
6678 path[0] = fsname[0] = mntdir[0] = '\0';
6680 } else { /* paths specified, only attempt to search these. */
6683 /* if only one path is given, print only requested info */
6684 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6686 for (; optind < argc; optind++) {
6687 rc2 = print_instance(argv[optind], fsname,
6688 sizeof(fsname), opt_instance,
6689 opt_fsname, opt_mntdir);
6699 static int lfs_check(int argc, char **argv)
6701 char mntdir[PATH_MAX] = {'\0'};
6710 fprintf(stderr, "%s check: server type must be specified\n",
6715 obd_types[0] = obd_type1;
6716 obd_types[1] = obd_type2;
6717 obd_types[2] = obd_type3;
6719 if (strcmp(argv[1], "osts") == 0) {
6720 strcpy(obd_types[0], "osc");
6721 } else if (strcmp(argv[1], "mdts") == 0 ||
6722 strcmp(argv[1], "mds") == 0) {
6723 strcpy(obd_types[0], "mdc");
6724 } else if (strcmp(argv[1], "mgts") == 0) {
6725 strcpy(obd_types[0], "mgc");
6726 } else if (strcmp(argv[1], "all") == 0 ||
6727 strcmp(argv[1], "servers") == 0) {
6729 strcpy(obd_types[0], "osc");
6730 strcpy(obd_types[1], "mdc");
6731 strcpy(obd_types[2], "mgc");
6733 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6738 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6739 if (rc < 0 || mntdir[0] == '\0') {
6741 "%s check: cannot find mounted Lustre filesystem: %s\n",
6742 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6746 rc = llapi_target_check(num_types, obd_types, mntdir);
6748 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6749 progname, argv[1], strerror(-rc));
6754 #ifdef HAVE_SYS_QUOTA_H
6755 #define ADD_OVERFLOW(a, b) \
6756 ((((a) + (b)) < (a)) ? \
6757 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6759 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6760 * returns the value or ULONG_MAX on integer overflow or incorrect format
6762 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6763 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6764 * 3. empty integer value is interpreted as 0
6766 static unsigned long str2sec(const char *timestr)
6768 const char spec[] = "smhdw";
6769 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6770 unsigned long val = 0;
6773 if (strpbrk(timestr, spec) == NULL) {
6775 * no specifiers inside the time string,
6776 * should treat it as an integer value
6778 val = strtoul(timestr, &tail, 10);
6779 return *tail ? ULONG_MAX : val;
6782 /* format string is XXwXXdXXhXXmXXs */
6788 v = strtoul(timestr, &tail, 10);
6789 if (v == ULONG_MAX || *tail == '\0')
6791 * value too large (ULONG_MAX or more)
6792 * or missing specifier
6796 ptr = strchr(spec, *tail);
6798 /* unknown specifier */
6803 /* check if product will overflow the type */
6804 if (!(v < ULONG_MAX / mult[ind]))
6807 ADD_OVERFLOW(val, mult[ind] * v);
6808 if (val == ULONG_MAX)
6820 #define ARG2ULL(nr, str, def_units) \
6822 unsigned long long limit, units = def_units; \
6825 rc = llapi_parse_size(str, &limit, &units, 1); \
6827 fprintf(stderr, "%s: invalid limit '%s'\n", \
6834 static inline int has_times_option(int argc, char **argv)
6838 for (i = 1; i < argc; i++)
6839 if (!strcmp(argv[i], "-t"))
6845 static inline int lfs_verify_poolarg(char *pool)
6847 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6849 "Pool name '%.*s' is longer than %d\n",
6850 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6856 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6859 char *mnt, *obd_type = (char *)qctl->obd_type;
6860 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6861 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6862 struct option long_opts[] = {
6863 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6864 { .val = 'g', .name = "group", .has_arg = no_argument },
6865 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6866 { .val = 'p', .name = "projid", .has_arg = no_argument },
6867 { .val = 't', .name = "times", .has_arg = no_argument },
6868 { .val = 'u', .name = "user", .has_arg = no_argument },
6869 { .val = LFS_POOL_OPT,
6870 .name = "pool", .has_arg = required_argument },
6874 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6875 qctl->qc_type = ALLQUOTA;
6877 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6878 long_opts, NULL)) != -1) {
6889 if (qctl->qc_type != ALLQUOTA) {
6891 "error: -u/g/p can't be used more than once\n");
6894 qctl->qc_type = qtype;
6897 if (strncmp(optarg, NOTIFY_GRACE,
6898 strlen(NOTIFY_GRACE)) == 0) {
6899 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6901 dqi->dqi_bgrace = str2sec(optarg);
6902 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6904 "error: bad block-grace: %s\n",
6909 dqb->dqb_valid |= QIF_BTIME;
6912 if (strncmp(optarg, NOTIFY_GRACE,
6913 strlen(NOTIFY_GRACE)) == 0) {
6914 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6916 dqi->dqi_igrace = str2sec(optarg);
6917 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6919 "error: bad inode-grace: %s\n",
6924 dqb->dqb_valid |= QIF_ITIME;
6926 case 't': /* Yes, of course! */
6929 if (lfs_verify_poolarg(optarg))
6932 "Trying to set grace for pool %s\n", optarg);
6933 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
6934 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
6936 /* getopt prints error message for us when opterr != 0 */
6942 if (qctl->qc_type == ALLQUOTA) {
6943 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6947 if (optind != argc - 1) {
6948 fprintf(stderr, "error: unexpected parameters encountered\n");
6953 rc = llapi_quotactl(mnt, qctl);
6956 fprintf(stderr, "%s %s ", obd_type,
6957 obd_uuid2str(&qctl->obd_uuid));
6958 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6965 #define BSLIMIT (1 << 0)
6966 #define BHLIMIT (1 << 1)
6967 #define ISLIMIT (1 << 2)
6968 #define IHLIMIT (1 << 3)
6970 int lfs_setquota(int argc, char **argv)
6973 struct if_quotactl *qctl;
6974 char *mnt, *obd_type;
6975 struct obd_dqblk *dqb;
6976 struct option long_opts[] = {
6977 { .val = 'b', .name = "block-softlimit",
6978 .has_arg = required_argument },
6979 { .val = 'B', .name = "block-hardlimit",
6980 .has_arg = required_argument },
6981 { .val = 'd', .name = "default", .has_arg = no_argument },
6982 { .val = 'g', .name = "group", .has_arg = required_argument },
6983 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6984 { .val = 'i', .name = "inode-softlimit",
6985 .has_arg = required_argument },
6986 { .val = 'I', .name = "inode-hardlimit",
6987 .has_arg = required_argument },
6988 { .val = 'p', .name = "projid", .has_arg = required_argument },
6989 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6990 { .val = 'u', .name = "user", .has_arg = required_argument },
6991 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6992 { .val = LFS_POOL_OPT,
6993 .name = "pool", .has_arg = required_argument },
6995 unsigned int limit_mask = 0;
6996 bool use_default = false;
6997 int qtype, qctl_len;
6999 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7000 qctl = malloc(qctl_len);
7004 memset(qctl, 0, qctl_len);
7005 obd_type = (char *)qctl->obd_type;
7006 dqb = &qctl->qc_dqblk;
7008 if (has_times_option(argc, argv)) {
7009 rc = lfs_setquota_times(argc, argv, qctl);
7013 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7014 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7015 * so it can be used as a marker that qc_type
7016 * isn't reinitialized from command line
7019 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7020 long_opts, NULL)) != -1) {
7023 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7026 goto quota_type_def;
7029 rc = name2uid(&qctl->qc_id, optarg);
7032 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7035 goto quota_type_def;
7038 rc = name2gid(&qctl->qc_id, optarg);
7041 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7044 goto quota_type_def;
7047 rc = name2projid(&qctl->qc_id, optarg);
7050 if (str2quotaid(&qctl->qc_id, optarg)) {
7052 "%s setquota: invalid id '%s'\n",
7059 if (qctl->qc_id == 0) {
7061 "%s setquota: can't set quota for root usr/group/project.\n",
7068 if (qctl->qc_type != ALLQUOTA) {
7070 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7075 qctl->qc_type = qtype;
7078 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7082 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7083 dqb->dqb_bsoftlimit >>= 10;
7084 limit_mask |= BSLIMIT;
7085 if (dqb->dqb_bsoftlimit &&
7086 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7088 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7090 (unsigned long long)dqb->dqb_bsoftlimit,
7094 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7095 dqb->dqb_bhardlimit >>= 10;
7096 limit_mask |= BHLIMIT;
7097 if (dqb->dqb_bhardlimit &&
7098 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7100 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7101 "See '%s help setquota' or Lustre manual for details\n",
7103 (unsigned long long)dqb->dqb_bhardlimit,
7107 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7108 limit_mask |= ISLIMIT;
7109 if (dqb->dqb_isoftlimit &&
7110 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7112 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7114 (unsigned long long)dqb->dqb_isoftlimit,
7118 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7119 limit_mask |= IHLIMIT;
7120 if (dqb->dqb_ihardlimit &&
7121 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7123 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7125 (unsigned long long)dqb->dqb_ihardlimit,
7129 if (lfs_verify_poolarg(optarg)) {
7134 "Trying to set quota for pool %s\n", optarg);
7135 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7136 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7140 "%s setquota: unrecognized option '%s'\n",
7141 progname, argv[optind - 1]);
7147 if (qctl->qc_type == ALLQUOTA) {
7149 "%s setquota: either -u or -g must be specified\n",
7155 if (!use_default && limit_mask == 0) {
7157 "%s setquota: at least one limit must be specified\n",
7163 if (use_default && limit_mask != 0) {
7165 "%s setquota: limits should not be specified when using default quota\n",
7171 if (use_default && qctl->qc_id == 0) {
7173 "%s setquota: can not set default quota for root user/group/project\n",
7179 if (optind != argc - 1) {
7181 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7182 progname, argv[optind]);
7190 dqb->dqb_bhardlimit = 0;
7191 dqb->dqb_bsoftlimit = 0;
7192 dqb->dqb_ihardlimit = 0;
7193 dqb->dqb_isoftlimit = 0;
7196 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7197 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7198 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7199 /* sigh, we can't just set blimits/ilimits */
7200 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7201 .qc_type = qctl->qc_type,
7202 .qc_id = qctl->qc_id};
7204 rc = llapi_quotactl(mnt, &tmp_qctl);
7208 if (!(limit_mask & BHLIMIT))
7209 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7210 if (!(limit_mask & BSLIMIT))
7211 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7212 if (!(limit_mask & IHLIMIT))
7213 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7214 if (!(limit_mask & ISLIMIT))
7215 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7217 /* Keep grace times if we have got no softlimit arguments */
7218 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7219 dqb->dqb_valid |= QIF_BTIME;
7220 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7223 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7224 dqb->dqb_valid |= QIF_ITIME;
7225 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7229 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7230 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7232 rc = llapi_quotactl(mnt, qctl);
7236 "%s setquota: cannot quotactl '%s' '%s': %s",
7238 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7245 /* Converts seconds value into format string
7246 * result is returned in buf
7248 * 1. result is in descenting order: 1w2d3h4m5s
7249 * 2. zero fields are not filled (except for p. 3): 5d1s
7250 * 3. zero seconds value is presented as "0s"
7252 static char *__sec2str(time_t seconds, char *buf)
7254 const char spec[] = "smhdw";
7255 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7260 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7261 c = seconds / mult[i];
7263 if (c > 0 || (i == 0 && buf == tail))
7264 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7273 static void sec2str(time_t seconds, char *buf, int rc)
7280 tail = __sec2str(seconds, tail);
7282 if (rc && tail - buf < 39) {
7288 static void diff2str(time_t seconds, char *buf, time_t now)
7293 if (seconds <= now) {
7294 strcpy(buf, "none");
7297 __sec2str(seconds - now, buf);
7300 static void print_quota_title(char *name, struct if_quotactl *qctl,
7301 bool human_readable, bool show_default)
7304 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7305 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7306 "Filesystem", "bquota", "blimit", "bgrace",
7307 "iquota", "ilimit", "igrace");
7309 printf("Disk quotas for %s %s (%cid %u):\n",
7310 qtype_name(qctl->qc_type), name,
7311 *qtype_name(qctl->qc_type), qctl->qc_id);
7312 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7313 "Filesystem", human_readable ? "used" : "kbytes",
7314 "quota", "limit", "grace",
7315 "files", "quota", "limit", "grace");
7319 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7322 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7325 snprintf(buf, buflen, "%5.4gP",
7326 (double)num / ((__u64)1 << 40));
7328 snprintf(buf, buflen, "%5.4gT",
7329 (double)num / (1 << 30));
7331 snprintf(buf, buflen, "%5.4gG",
7332 (double)num / (1 << 20));
7334 snprintf(buf, buflen, "%5.4gM",
7335 (double)num / (1 << 10));
7337 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7341 #define STRBUF_LEN 32
7342 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7343 int rc, bool h, bool show_default)
7349 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7350 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7351 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7352 int bover = 0, iover = 0;
7353 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7354 char numbuf[3][STRBUF_LEN];
7356 char strbuf[STRBUF_LEN];
7358 if (dqb->dqb_bhardlimit &&
7359 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7361 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7362 if (dqb->dqb_btime > now)
7368 if (dqb->dqb_ihardlimit &&
7369 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7371 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7372 if (dqb->dqb_itime > now)
7378 if (strlen(mnt) > 15)
7379 printf("%s\n%15s", mnt, "");
7381 printf("%15s", mnt);
7384 diff2str(dqb->dqb_btime, timebuf, now);
7385 else if (show_default)
7386 snprintf(timebuf, sizeof(timebuf), "%llu",
7387 (unsigned long long)dqb->dqb_btime);
7389 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7390 strbuf, sizeof(strbuf), h);
7391 if (rc == -EREMOTEIO)
7392 sprintf(numbuf[0], "%s*", strbuf);
7394 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7395 "%s" : "[%s]", strbuf);
7397 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7398 if (type == QC_GENERAL)
7399 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7400 "%s" : "[%s]", strbuf);
7402 sprintf(numbuf[1], "%s", "-");
7404 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7405 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7406 "%s" : "[%s]", strbuf);
7409 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7411 printf(" %7s%c %6s %7s %7s",
7412 numbuf[0], bover ? '*' : ' ', numbuf[1],
7413 numbuf[2], bover > 1 ? timebuf : "-");
7416 diff2str(dqb->dqb_itime, timebuf, now);
7417 else if (show_default)
7418 snprintf(timebuf, sizeof(timebuf), "%llu",
7419 (unsigned long long)dqb->dqb_itime);
7421 snprintf(numbuf[0], sizeof(numbuf),
7422 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7423 (uintmax_t)dqb->dqb_curinodes);
7425 if (type == QC_GENERAL)
7426 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7428 (uintmax_t)dqb->dqb_isoftlimit);
7430 sprintf(numbuf[1], "%s", "-");
7432 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7433 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7436 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7437 else if (type != QC_OSTIDX)
7438 printf(" %7s%c %6s %7s %7s",
7439 numbuf[0], iover ? '*' : ' ', numbuf[1],
7440 numbuf[2], iover > 1 ? timebuf : "-");
7442 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7444 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7445 qctl->qc_cmd == Q_GETOINFO) {
7449 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7450 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7452 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7453 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7454 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7456 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7458 printf("Block grace time: %s; Inode grace time: %s\n",
7459 bgtimebuf, igtimebuf);
7463 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7467 /* format is "lustre-OST0001" */
7468 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7470 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7475 *idx = strtoul(dash, &endp, 16);
7476 if (*idx > 0xffff) {
7477 fprintf(stderr, "wrong index %s\n", tgtname);
7484 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7485 bool h, __u64 *total)
7487 int rc = 0, rc1 = 0, count = 0, i = 0;
7488 char **list = NULL, *buffer = NULL;
7489 __u32 valid = qctl->qc_valid;
7491 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7494 /* Is it correct for the case OST0000, OST0002, OST0003 -
7495 * we will ask OST0001 that is absent and won't ask OST0003? */
7496 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7498 fprintf(stderr, "can not get %s count: %s\n",
7499 is_mdt ? "mdt" : "ost", strerror(-rc));
7503 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7504 char fname[PATH_MAX];
7505 char fsname[LUSTRE_MAXFSNAME + 1];
7506 int bufsize = sizeof(struct obd_uuid) * count;
7508 rc = llapi_search_fsname(mnt, fsname);
7510 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7514 buffer = malloc(bufsize + sizeof(*list) * count);
7517 list = (char **)(buffer + bufsize);
7518 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7519 count = llapi_get_poolmembers(fname, list, count,
7525 for (i = 0; i < count; i++) {
7526 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7529 if (tgt_name2index(list[i], &index))
7531 qctl->qc_idx = index;
7536 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7537 rc = llapi_quotactl(mnt, qctl);
7539 /* It is remote client case. */
7540 if (rc == -EOPNOTSUPP) {
7547 fprintf(stderr, "quotactl %s%d failed.\n",
7548 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7552 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7553 qctl->qc_valid, 0, h, false);
7554 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7555 qctl->qc_dqblk.dqb_bhardlimit;
7560 qctl->qc_valid = valid;
7564 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7565 int verbose, int quiet, bool human_readable,
7568 int rc1 = 0, rc2 = 0, rc3 = 0;
7569 char *obd_type = (char *)qctl->obd_type;
7570 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7571 __u64 total_ialloc = 0, total_balloc = 0;
7572 bool use_default_for_blk = false;
7573 bool use_default_for_file = false;
7576 rc1 = llapi_quotactl(mnt, qctl);
7580 fprintf(stderr, "%s quotas are not enabled.\n",
7581 qtype_name(qctl->qc_type));
7584 fprintf(stderr, "Permission denied.\n");
7587 /* We already got error message. */
7590 fprintf(stderr, "Unexpected quotactl error: %s\n",
7595 if (!show_default && qctl->qc_id == 0) {
7596 qctl->qc_dqblk.dqb_bhardlimit = 0;
7597 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7598 qctl->qc_dqblk.dqb_ihardlimit = 0;
7599 qctl->qc_dqblk.dqb_isoftlimit = 0;
7600 qctl->qc_dqblk.dqb_btime = 0;
7601 qctl->qc_dqblk.dqb_itime = 0;
7602 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7605 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7606 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7607 use_default_for_blk = true;
7608 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7611 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7612 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7613 use_default_for_file = true;
7614 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7617 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7618 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7619 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7620 print_quota_title(name, qctl, human_readable, show_default);
7622 if (rc1 && *obd_type)
7623 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7625 if (qctl->qc_valid != QC_GENERAL)
7628 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7629 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7630 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7631 (QIF_LIMITS|QIF_USAGE));
7633 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7635 if (!show_default && verbose &&
7636 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7637 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7638 char strbuf[STRBUF_LEN];
7640 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7642 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7644 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7646 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7647 (uintmax_t)total_ialloc, strbuf);
7650 if (use_default_for_blk)
7651 printf("%cid %u is using default block quota setting\n",
7652 *qtype_name(qctl->qc_type), qctl->qc_id);
7654 if (use_default_for_file)
7655 printf("%cid %u is using default file quota setting\n",
7656 *qtype_name(qctl->qc_type), qctl->qc_id);
7658 if (rc1 || rc2 || rc3 || inacc)
7659 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7673 static int lfs_project(int argc, char **argv)
7675 int ret = 0, err = 0, c, i;
7676 struct project_handle_control phc = { 0 };
7677 enum lfs_project_ops_t op;
7680 phc.assign_projid = false;
7681 /* default action */
7682 op = LFS_PROJECT_LIST;
7684 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7687 if (op != LFS_PROJECT_LIST) {
7689 "%s: cannot specify '-c' '-C' '-s' together\n",
7694 op = LFS_PROJECT_CHECK;
7697 if (op != LFS_PROJECT_LIST) {
7699 "%s: cannot specify '-c' '-C' '-s' together\n",
7704 op = LFS_PROJECT_CLEAR;
7707 if (op != LFS_PROJECT_LIST) {
7709 "%s: cannot specify '-c' '-C' '-s' together\n",
7714 phc.set_inherit = true;
7715 op = LFS_PROJECT_SET;
7721 phc.keep_projid = true;
7724 phc.recursive = true;
7727 if (str2quotaid(&phc.projid, optarg)) {
7729 "Invalid project ID: %s\n",
7734 phc.assign_projid = true;
7738 phc.newline = false;
7741 fprintf(stderr, "%s: invalid option '%c'\n",
7747 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7748 op = LFS_PROJECT_SET;
7749 phc.set_projid = true;
7750 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7751 phc.set_projid = true;
7755 case LFS_PROJECT_CHECK:
7756 if (phc.keep_projid) {
7758 "%s: '-k' is useless together with '-c'\n",
7763 case LFS_PROJECT_CLEAR:
7766 "%s: '-0' is useless together with '-C'\n",
7770 if (phc.assign_projid) {
7772 "%s: '-p' is useless together with '-C'\n",
7777 case LFS_PROJECT_SET:
7780 "%s: '-0' is useless together with '-s'\n",
7784 if (phc.keep_projid) {
7786 "%s: '-k' is useless together with '-s'\n",
7794 "%s: '-0' is useless for list operations\n",
7804 fprintf(stderr, "%s: missing file or directory target(s)\n",
7809 for (i = 0; i < argc; i++) {
7811 case LFS_PROJECT_CHECK:
7812 err = lfs_project_check(argv[i], &phc);
7814 case LFS_PROJECT_LIST:
7815 err = lfs_project_list(argv[i], &phc);
7817 case LFS_PROJECT_CLEAR:
7818 err = lfs_project_clear(argv[i], &phc);
7820 case LFS_PROJECT_SET:
7821 err = lfs_project_set(argv[i], &phc);
7833 static int lfs_quota(int argc, char **argv)
7836 char *mnt, *name = NULL;
7837 struct if_quotactl *qctl;
7839 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7840 __u32 valid = QC_GENERAL, idx = 0;
7841 bool human_readable = false;
7842 bool show_default = false;
7844 struct option long_opts[] = {
7845 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
7848 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7852 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7853 qctl->qc_type = ALLQUOTA;
7854 obd_uuid = (char *)qctl->obd_uuid.uuid;
7856 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7857 long_opts, NULL)) != -1) {
7860 show_default = true;
7865 show_default = true;
7870 show_default = true;
7874 if (qctl->qc_type != ALLQUOTA) {
7876 "%s quota: only one of -u, -g, or -p may be specified\n",
7881 qctl->qc_type = qtype;
7884 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7887 valid = qctl->qc_valid = QC_UUID;
7888 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
7891 valid = qctl->qc_valid = QC_MDTIDX;
7892 idx = qctl->qc_idx = atoi(optarg);
7893 if (idx == 0 && *optarg != '0') {
7895 "%s quota: invalid MDT index '%s'\n",
7902 valid = qctl->qc_valid = QC_OSTIDX;
7903 idx = qctl->qc_idx = atoi(optarg);
7904 if (idx == 0 && *optarg != '0') {
7906 "%s quota: invalid OST index '%s'\n",
7919 human_readable = true;
7922 if (lfs_verify_poolarg(optarg)) {
7926 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7927 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
7928 LUSTRE_Q_GETINFOPOOL :
7929 LUSTRE_Q_GETQUOTAPOOL;
7932 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7933 progname, argv[optind - 1]);
7939 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7940 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7941 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7942 qctl->qc_type == ALLQUOTA &&
7943 optind == argc - 1 && !show_default) {
7946 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7947 qctl->qc_type = qtype;
7948 qctl->qc_valid = valid;
7949 if (qtype == USRQUOTA) {
7950 qctl->qc_id = geteuid();
7951 rc = uid2name(&name, qctl->qc_id);
7953 qctl->qc_id = getegid();
7954 rc = gid2name(&name, qctl->qc_id);
7955 memset(&qctl->qc_dqblk, 0,
7956 sizeof(qctl->qc_dqblk));
7961 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
7962 human_readable, show_default);
7967 /* lfs quota -u username /path/to/lustre/mount */
7968 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7969 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7970 /* options should be followed by u/g-name and mntpoint */
7971 if ((!show_default && optind + 2 != argc) ||
7972 (show_default && optind + 1 != argc) ||
7973 qctl->qc_type == ALLQUOTA) {
7975 "%s quota: name and mount point must be specified\n",
7981 if (!show_default) {
7982 name = argv[optind++];
7983 switch (qctl->qc_type) {
7985 rc = name2uid(&qctl->qc_id, name);
7988 rc = name2gid(&qctl->qc_id, name);
7991 rc = name2projid(&qctl->qc_id, name);
7998 qctl->qc_valid = QC_GENERAL;
7999 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
8004 if (str2quotaid(&qctl->qc_id, name)) {
8005 fprintf(stderr, "%s quota: invalid id '%s'\n",
8011 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8012 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8019 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8020 human_readable, show_default);
8025 #endif /* HAVE_SYS_QUOTA_H! */
8027 static int flushctx_ioctl(char *mp)
8031 fd = open(mp, O_RDONLY);
8033 fprintf(stderr, "flushctx: error open %s: %s\n",
8034 mp, strerror(errno));
8038 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8040 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8041 mp, strerror(errno));
8047 static int lfs_flushctx(int argc, char **argv)
8049 int kdestroy = 0, reap = 0, c;
8050 char mntdir[PATH_MAX] = {'\0'};
8054 while ((c = getopt(argc, argv, "kr")) != -1) {
8064 "error: %s: option '-%c' unrecognized\n",
8071 rc = system("kdestroy > /dev/null");
8073 rc = WEXITSTATUS(rc);
8075 "error destroying tickets: %d, continuing\n",
8080 if (optind >= argc) {
8081 /* flush for all mounted lustre fs. */
8082 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8083 /* Check if we have a mount point */
8084 if (mntdir[0] == '\0')
8087 if (flushctx_ioctl(mntdir))
8090 mntdir[0] = '\0'; /* avoid matching in next loop */
8093 /* flush fs as specified */
8094 while (optind < argc) {
8095 if (flushctx_ioctl(argv[optind++]))
8101 rc = system("keyctl reap > /dev/null");
8103 rc = WEXITSTATUS(rc);
8104 fprintf(stderr, "error reaping keyring: %d\n", rc);
8111 static int lfs_changelog(int argc, char **argv)
8113 void *changelog_priv;
8114 struct changelog_rec *rec;
8115 long long startrec = 0, endrec = 0;
8117 struct option long_opts[] = {
8118 { .val = 'f', .name = "follow", .has_arg = no_argument },
8120 char short_opts[] = "f";
8123 while ((rc = getopt_long(argc, argv, short_opts,
8124 long_opts, NULL)) != -1) {
8131 "%s changelog: unrecognized option '%s'\n",
8132 progname, argv[optind - 1]);
8136 if (optind >= argc) {
8137 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8142 mdd = argv[optind++];
8144 startrec = strtoll(argv[optind++], NULL, 10);
8146 endrec = strtoll(argv[optind++], NULL, 10);
8148 rc = llapi_changelog_start(&changelog_priv,
8149 CHANGELOG_FLAG_BLOCK |
8150 CHANGELOG_FLAG_JOBID |
8151 CHANGELOG_FLAG_EXTRA_FLAGS |
8152 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8155 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8156 progname, strerror(errno = -rc));
8160 rc = llapi_changelog_set_xflags(changelog_priv,
8161 CHANGELOG_EXTRA_FLAG_UIDGID |
8162 CHANGELOG_EXTRA_FLAG_NID |
8163 CHANGELOG_EXTRA_FLAG_OMODE |
8164 CHANGELOG_EXTRA_FLAG_XATTR);
8167 "%s changelog: cannot set xflags for changelog: %s\n",
8168 progname, strerror(errno = -rc));
8172 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8176 if (endrec && rec->cr_index > endrec) {
8177 llapi_changelog_free(&rec);
8180 if (rec->cr_index < startrec) {
8181 llapi_changelog_free(&rec);
8185 secs = rec->cr_time >> 30;
8186 gmtime_r(&secs, &ts);
8187 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8188 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8189 changelog_type2str(rec->cr_type),
8190 ts.tm_hour, ts.tm_min, ts.tm_sec,
8191 (int)(rec->cr_time & ((1 << 30) - 1)),
8192 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8193 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8195 if (rec->cr_flags & CLF_JOBID) {
8196 struct changelog_ext_jobid *jid =
8197 changelog_rec_jobid(rec);
8199 if (jid->cr_jobid[0] != '\0')
8200 printf(" j=%s", jid->cr_jobid);
8203 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8204 struct changelog_ext_extra_flags *ef =
8205 changelog_rec_extra_flags(rec);
8207 printf(" ef=0x%llx",
8208 (unsigned long long)ef->cr_extra_flags);
8210 if (ef->cr_extra_flags & CLFE_UIDGID) {
8211 struct changelog_ext_uidgid *uidgid =
8212 changelog_rec_uidgid(rec);
8214 printf(" u=%llu:%llu",
8215 (unsigned long long)uidgid->cr_uid,
8216 (unsigned long long)uidgid->cr_gid);
8218 if (ef->cr_extra_flags & CLFE_NID) {
8219 struct changelog_ext_nid *nid =
8220 changelog_rec_nid(rec);
8223 libcfs_nid2str(nid->cr_nid));
8226 if (ef->cr_extra_flags & CLFE_OPEN) {
8227 struct changelog_ext_openmode *omd =
8228 changelog_rec_openmode(rec);
8229 char mode[] = "---";
8231 /* exec mode must be exclusive */
8232 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8235 if (omd->cr_openflags & MDS_FMODE_READ)
8237 if (omd->cr_openflags &
8244 if (strcmp(mode, "---") != 0)
8245 printf(" m=%s", mode);
8248 if (ef->cr_extra_flags & CLFE_XATTR) {
8249 struct changelog_ext_xattr *xattr =
8250 changelog_rec_xattr(rec);
8252 if (xattr->cr_xattr[0] != '\0')
8253 printf(" x=%s", xattr->cr_xattr);
8257 if (!fid_is_zero(&rec->cr_pfid))
8258 printf(" p="DFID, PFID(&rec->cr_pfid));
8259 if (rec->cr_namelen)
8260 printf(" %.*s", rec->cr_namelen,
8261 changelog_rec_name(rec));
8263 if (rec->cr_flags & CLF_RENAME) {
8264 struct changelog_ext_rename *rnm =
8265 changelog_rec_rename(rec);
8267 if (!fid_is_zero(&rnm->cr_sfid))
8268 printf(" s="DFID" sp="DFID" %.*s",
8269 PFID(&rnm->cr_sfid),
8270 PFID(&rnm->cr_spfid),
8271 (int)changelog_rec_snamelen(rec),
8272 changelog_rec_sname(rec));
8276 llapi_changelog_free(&rec);
8279 llapi_changelog_fini(&changelog_priv);
8282 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8283 progname, strerror(errno = -rc));
8285 return (rc == 1 ? 0 : rc);
8288 static int lfs_changelog_clear(int argc, char **argv)
8296 endrec = strtoll(argv[3], NULL, 10);
8298 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8301 fprintf(stderr, "%s: record out of range: %llu\n",
8303 else if (rc == -ENOENT)
8304 fprintf(stderr, "%s: no changelog user: %s\n",
8307 fprintf(stderr, "%s error: %s\n", argv[0],
8316 static void rstripc(char *str, int c)
8318 char *end = str + strlen(str);
8320 for (; str < end && end[-1] == c; --end)
8324 static int lfs_fid2path(int argc, char **argv)
8326 struct option long_opts[] = {
8327 { .val = 'c', .name = "cur", .has_arg = no_argument },
8328 { .val = 'c', .name = "current", .has_arg = no_argument },
8329 { .val = 'c', .name = "print-link", .has_arg = no_argument },
8330 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
8331 { .val = 'l', .name = "link", .has_arg = required_argument },
8333 char short_opts[] = "cfl:pr:";
8334 bool print_link = false;
8335 bool print_fid = false;
8337 char mnt_dir[PATH_MAX] = "";
8339 char *path_or_fsname;
8340 long long recno = -1;
8342 char *endptr = NULL;
8347 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
8356 linkno = strtol(optarg, &endptr, 10);
8357 if (*endptr != '\0') {
8359 "%s fid2path: invalid linkno '%s'\n",
8365 /* recno is something to do with changelogs
8366 * that was never implemented. We just pass it
8367 * through for the MDT to ignore.
8369 recno = strtoll(optarg, &endptr, 10);
8370 if (*endptr != '\0') {
8372 "%s fid2path: invalid recno '%s'\n",
8379 "%s fid2path: unrecognized option '%s'\n",
8380 progname, argv[optind - 1]);
8385 if (argc - optind < 2) {
8387 "Usage: %s fid2path FSNAME|ROOT FID...\n",
8392 path_or_fsname = argv[optind];
8394 if (*path_or_fsname == '/') {
8395 print_mnt_dir = true;
8396 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
8398 print_mnt_dir = false;
8399 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
8404 "%s fid2path: cannot resolve mount point for '%s': %s\n",
8405 progname, path_or_fsname, strerror(-rc));
8409 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
8412 "%s fid2path: cannot open mount point for '%s': %s\n",
8413 progname, path_or_fsname, strerror(-rc));
8417 /* Strip trailing slashes from mnt_dir. */
8418 rstripc(mnt_dir + 1, '/');
8420 for (i = optind + 1; i < argc; i++) {
8421 const char *fid_str = argv[i];
8425 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
8428 "%s fid2path: invalid FID '%s'\n",
8436 int linktmp = (linkno >= 0) ? linkno : 0;
8438 int oldtmp = linktmp;
8439 long long rectmp = recno;
8440 char path_buf[PATH_MAX];
8442 rc2 = llapi_fid2path_at(mnt_fd, &fid,
8443 path_buf, sizeof(path_buf), &rectmp, &linktmp);
8446 "%s fid2path: cannot find %s %s: %s\n",
8447 progname, path_or_fsname, fid_str,
8455 printf("%s ", fid_str);
8458 printf("%d ", linktmp);
8460 /* You may think this looks wrong or weird (and it is!)
8461 * but we are actually trying to preserve the old quirky
8462 * behaviors (enforced by our old quirky tests!) that
8463 * make lfs so much fun to work on:
8465 * lustre 0x200000007:0x1:0x0 => "/"
8466 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
8468 * Note that llapi_fid2path() returns "" for the root
8472 print_mnt_dir ? mnt_dir : "",
8473 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
8477 /* specified linkno */
8480 if (oldtmp == linktmp)
8492 static int lfs_path2fid(int argc, char **argv)
8494 struct option long_opts[] = {
8495 { .val = 'p', .name = "parents", .has_arg = no_argument },
8498 const char short_opts[] = "p";
8499 const char *sep = "";
8502 bool show_parents = false;
8504 while ((rc = getopt_long(argc, argv, short_opts,
8505 long_opts, NULL)) != -1) {
8508 show_parents = true;
8512 "%s path2fid: unrecognized option '%s'\n",
8513 progname, argv[optind - 1]);
8518 if (optind > argc - 1) {
8519 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8522 } else if (optind < argc - 1) {
8527 for (path = argv + optind; *path != NULL; path++) {
8530 if (!show_parents) {
8531 err = llapi_path2fid(*path, &fid);
8533 printf("%s%s"DFID"\n",
8534 *sep != '\0' ? *path : "", sep,
8537 char name[NAME_MAX + 1];
8538 unsigned int linkno = 0;
8540 while ((err = llapi_path2parent(*path, linkno, &fid,
8541 name, sizeof(name))) == 0) {
8542 if (*sep != '\0' && linkno == 0)
8543 printf("%s%s", *path, sep);
8545 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8550 /* err == -ENODATA is end-of-loop */
8551 if (linkno > 0 && err == -ENODATA) {
8559 "%s path2fid: cannot get %sfid for '%s': %s\n",
8560 progname, show_parents ? "parent " : "", *path,
8572 #define MAX_ERRNO 4095
8573 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8575 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8579 rc = llapi_rmfid(device, fa);
8581 fprintf(stderr, "rmfid(): rc = %d\n", rc);
8585 for (k = 0; k < fa->fa_nr; k++) {
8586 rc = (__s32)fa->fa_fids[k].f_ver;
8587 if (!IS_ERR_VALUE(rc))
8593 fa->fa_fids[k].f_ver = 0;
8594 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
8595 PFID(&fa->fa_fids[k]), rc);
8601 static int lfs_rmfid(int argc, char **argv)
8603 char *fidstr, *device;
8604 int rc = 0, rc2, nr;
8605 struct fid_array *fa;
8607 if (optind > argc - 1) {
8608 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8612 device = argv[optind++];
8615 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8621 while (optind < argc) {
8624 fidstr = argv[optind++];
8625 while (*fidstr == '[')
8627 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8629 fprintf(stderr, "unrecognized FID: %s\n",
8634 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8635 /* start another batch */
8636 rc2 = lfs_rmfid_and_show_errors(device, fa);
8643 rc2 = lfs_rmfid_and_show_errors(device, fa);
8651 static int lfs_data_version(int argc, char **argv)
8658 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8661 fprintf(stderr, "%s data_version: FILE must be specified\n",
8666 while ((c = getopt(argc, argv, "nrw")) != -1) {
8669 data_version_flags = 0;
8672 data_version_flags |= LL_DV_RD_FLUSH;
8675 data_version_flags |= LL_DV_WR_FLUSH;
8679 "%s data_version: unrecognized option '%s'\n",
8680 progname, argv[optind - 1]);
8684 if (optind == argc) {
8685 fprintf(stderr, "%s data_version: FILE must be specified\n",
8690 path = argv[optind];
8691 fd = open(path, O_RDONLY);
8694 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8695 progname, path, strerror(-rc));
8699 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8702 "%s data_version: cannot get version for '%s': %s\n",
8703 progname, path, strerror(-rc));
8705 printf("%ju" "\n", (uintmax_t)data_version);
8711 static int lfs_hsm_state(int argc, char **argv)
8716 struct hsm_user_state hus;
8724 rc = llapi_hsm_state_get(path, &hus);
8726 fprintf(stderr, "can't get hsm state for %s: %s\n",
8727 path, strerror(errno = -rc));
8731 /* Display path name and status flags */
8732 printf("%s: (0x%08x)", path, hus.hus_states);
8734 if (hus.hus_states & HS_RELEASED)
8735 printf(" released");
8736 if (hus.hus_states & HS_EXISTS)
8738 if (hus.hus_states & HS_DIRTY)
8740 if (hus.hus_states & HS_ARCHIVED)
8741 printf(" archived");
8742 /* Display user-settable flags */
8743 if (hus.hus_states & HS_NORELEASE)
8744 printf(" never_release");
8745 if (hus.hus_states & HS_NOARCHIVE)
8746 printf(" never_archive");
8747 if (hus.hus_states & HS_LOST)
8748 printf(" lost_from_hsm");
8750 if (hus.hus_archive_id != 0)
8751 printf(", archive_id:%d", hus.hus_archive_id);
8754 } while (++i < argc);
8759 #define LFS_HSM_SET 0
8760 #define LFS_HSM_CLEAR 1
8763 * Generic function to set or clear HSM flags.
8764 * Used by hsm_set and hsm_clear.
8766 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8768 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8770 struct option long_opts[] = {
8771 { .val = 'A', .name = "archived", .has_arg = no_argument },
8772 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8773 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8774 { .val = 'e', .name = "exists", .has_arg = no_argument },
8775 { .val = 'l', .name = "lost", .has_arg = no_argument },
8776 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8777 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8779 char short_opts[] = "lraAdei:";
8783 __u32 archive_id = 0;
8789 while ((c = getopt_long(argc, argv, short_opts,
8790 long_opts, NULL)) != -1) {
8796 mask |= HS_NOARCHIVE;
8799 mask |= HS_ARCHIVED;
8802 mask |= HS_NORELEASE;
8811 archive_id = strtol(optarg, &end, 10);
8813 fprintf(stderr, "invalid archive_id: '%s'\n",
8821 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8822 argv[0], argv[optind - 1]);
8827 /* User should have specified a flag */
8831 while (optind < argc) {
8832 path = argv[optind];
8834 /* If mode == 0, this means we apply the mask. */
8835 if (mode == LFS_HSM_SET)
8836 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8838 rc = llapi_hsm_state_set(path, 0, mask, 0);
8841 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8842 path, strerror(errno = -rc));
8851 static int lfs_hsm_action(int argc, char **argv)
8856 struct hsm_current_action hca;
8857 struct hsm_extent he;
8858 enum hsm_user_action hua;
8859 enum hsm_progress_states hps;
8867 rc = llapi_hsm_current_action(path, &hca);
8869 fprintf(stderr, "can't get hsm action for %s: %s\n",
8870 path, strerror(errno = -rc));
8873 he = hca.hca_location;
8874 hua = hca.hca_action;
8875 hps = hca.hca_state;
8877 printf("%s: %s", path, hsm_user_action2name(hua));
8879 /* Skip file without action */
8880 if (hca.hca_action == HUA_NONE) {
8885 printf(" %s ", hsm_progress_state2name(hps));
8887 if ((hps == HPS_RUNNING) &&
8888 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8889 printf("(%llu bytes moved)\n",
8890 (unsigned long long)he.length);
8891 else if ((he.offset + he.length) == LUSTRE_EOF)
8892 printf("(from %llu to EOF)\n",
8893 (unsigned long long)he.offset);
8895 printf("(from %llu to %llu)\n",
8896 (unsigned long long)he.offset,
8897 (unsigned long long)(he.offset + he.length));
8899 } while (++i < argc);
8904 static int lfs_hsm_set(int argc, char **argv)
8906 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8909 static int lfs_hsm_clear(int argc, char **argv)
8911 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8915 * Check file state and return its fid, to be used by lfs_hsm_request().
8917 * \param[in] file Path to file to check
8918 * \param[in,out] fid Pointer to allocated lu_fid struct.
8919 * \param[in,out] last_dev Pointer to last device id used.
8921 * \return 0 on success.
8923 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8929 rc = lstat(file, &st);
8931 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8935 * Checking for regular file as archiving as posix copytool
8936 * rejects archiving files other than regular files
8938 if (!S_ISREG(st.st_mode)) {
8939 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8942 /* A request should be ... */
8943 if (*last_dev != st.st_dev && *last_dev != 0) {
8945 "All files should be on the same filesystem: %s\n",
8949 *last_dev = st.st_dev;
8951 rc = llapi_path2fid(file, fid);
8953 fprintf(stderr, "Cannot read FID of %s: %s\n",
8954 file, strerror(-rc));
8960 /* Fill an HSM HUR item with a given file name.
8962 * If mntpath is set, then the filename is actually a FID, and no
8963 * lookup on the filesystem will be performed.
8965 * \param[in] hur the user request to fill
8966 * \param[in] idx index of the item inside the HUR to fill
8967 * \param[in] mntpath mountpoint of Lustre
8968 * \param[in] fname filename (if mtnpath is NULL)
8969 * or FID (if mntpath is set)
8970 * \param[in] last_dev pointer to last device id used
8972 * \retval 0 on success
8973 * \retval CMD_HELP or a negative errno on error
8975 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8976 const char *mntpath, const char *fname,
8979 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8982 hui->hui_extent.length = -1;
8985 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8987 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8990 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8994 hur->hur_request.hr_itemcount++;
8999 static int lfs_hsm_request(int argc, char **argv, int action)
9001 struct option long_opts[] = {
9002 { .val = 'a', .name = "archive", .has_arg = required_argument },
9003 { .val = 'D', .name = "data", .has_arg = required_argument },
9004 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9005 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9008 char short_opts[] = "l:D:a:m:";
9009 struct hsm_user_request *hur, *oldhur;
9014 char *filelist = NULL;
9015 char fullpath[PATH_MAX];
9016 char *opaque = NULL;
9020 int nbfile_alloc = 0;
9021 char *some_file = NULL;
9022 char *mntpath = NULL;
9028 while ((c = getopt_long(argc, argv, short_opts,
9029 long_opts, NULL)) != -1) {
9038 if (action != HUA_ARCHIVE &&
9039 action != HUA_REMOVE) {
9041 "error: -a is supported only when archiving or removing\n");
9044 archive_id = atoi(optarg);
9049 some_file = strdup(optarg);
9055 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9056 argv[0], argv[optind - 1]);
9061 /* All remaining args are files, so we have at least nbfile */
9062 nbfile = argc - optind;
9064 if ((nbfile == 0) && (!filelist))
9068 opaque_len = strlen(opaque);
9071 * Alloc the request structure with enough place to store all files
9072 * from command line.
9074 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9076 fprintf(stderr, "Cannot create the request: %s\n",
9080 nbfile_alloc = nbfile;
9082 hur->hur_request.hr_action = action;
9083 hur->hur_request.hr_archive_id = archive_id;
9084 hur->hur_request.hr_flags = 0;
9086 /* All remaining args are files, add them */
9087 if (nbfile != 0 && some_file == NULL)
9088 some_file = strdup(argv[optind]);
9090 for (i = 0; i < nbfile; i++) {
9091 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9097 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9099 /* If a filelist was specified, read the filelist from it. */
9101 fp = fopen(filelist, "r");
9103 fprintf(stderr, "Cannot read the file list %s: %s\n",
9104 filelist, strerror(errno));
9109 while ((rc = getline(&line, &len, fp)) != -1) {
9111 * If allocated buffer was too small, get something
9114 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9117 nbfile_alloc = nbfile_alloc * 2 + 1;
9119 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9123 "hsm: cannot allocate the request: %s\n",
9130 size = hur_len(oldhur);
9133 "hsm: cannot allocate %u files + %u bytes data\n",
9134 oldhur->hur_request.hr_itemcount,
9135 oldhur->hur_request.hr_data_len);
9142 memcpy(hur, oldhur, size);
9147 if (line[strlen(line) - 1] == '\n')
9148 line[strlen(line) - 1] = '\0';
9150 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9151 mntpath, line, &last_dev);
9167 /* If a --data was used, add it to the request */
9168 hur->hur_request.hr_data_len = opaque_len;
9170 memcpy(hur_data(hur), opaque, opaque_len);
9172 /* Send the HSM request */
9173 if (realpath(some_file, fullpath) == NULL) {
9174 fprintf(stderr, "Could not find path '%s': %s\n",
9175 some_file, strerror(errno));
9177 rc = llapi_hsm_request(fullpath, hur);
9179 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9180 some_file, strerror(-rc));
9190 static int lfs_hsm_archive(int argc, char **argv)
9192 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9195 static int lfs_hsm_restore(int argc, char **argv)
9197 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9200 static int lfs_hsm_release(int argc, char **argv)
9202 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9205 static int lfs_hsm_remove(int argc, char **argv)
9207 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9210 static int lfs_hsm_cancel(int argc, char **argv)
9212 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9215 static int lfs_swap_layouts(int argc, char **argv)
9220 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9221 SWAP_LAYOUTS_KEEP_MTIME |
9222 SWAP_LAYOUTS_KEEP_ATIME);
9225 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9227 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9229 int lfs_get_mode(const char *string)
9231 enum lock_mode_user mode;
9233 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9234 if (lock_mode_names[mode] == NULL)
9236 if (strcmp(string, lock_mode_names[mode]) == 0)
9243 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9245 enum lu_ladvise_type advice;
9248 advice < ARRAY_SIZE(ladvise_names); advice++) {
9249 if (ladvise_names[advice] == NULL)
9251 if (strcmp(string, ladvise_names[advice]) == 0)
9255 return LU_LADVISE_INVALID;
9258 static int lfs_ladvise(int argc, char **argv)
9260 struct option long_opts[] = {
9261 { .val = 'a', .name = "advice", .has_arg = required_argument },
9262 { .val = 'b', .name = "background", .has_arg = no_argument },
9263 { .val = 'e', .name = "end", .has_arg = required_argument },
9264 { .val = 'l', .name = "length", .has_arg = required_argument },
9265 { .val = 'm', .name = "mode", .has_arg = required_argument },
9266 { .val = 's', .name = "start", .has_arg = required_argument },
9267 { .val = 'u', .name = "unset", .has_arg = no_argument },
9269 char short_opts[] = "a:be:l:m:s:u";
9274 struct llapi_lu_ladvise advice;
9275 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9276 unsigned long long start = 0;
9277 unsigned long long end = LUSTRE_EOF;
9278 unsigned long long length = 0;
9279 unsigned long long size_units;
9280 unsigned long long flags = 0;
9284 while ((c = getopt_long(argc, argv, short_opts,
9285 long_opts, NULL)) != -1) {
9288 advice_type = lfs_get_ladvice(optarg);
9289 if (advice_type == LU_LADVISE_INVALID) {
9291 "%s: invalid advice type '%s'\n",
9293 fprintf(stderr, "Valid types:");
9295 for (advice_type = 0;
9296 advice_type < ARRAY_SIZE(ladvise_names);
9298 if (ladvise_names[advice_type] == NULL)
9300 fprintf(stderr, " %s",
9301 ladvise_names[advice_type]);
9303 fprintf(stderr, "\n");
9316 rc = llapi_parse_size(optarg, &end,
9319 fprintf(stderr, "%s: bad end offset '%s'\n",
9326 rc = llapi_parse_size(optarg, &start,
9330 "%s: bad start offset '%s'\n",
9337 rc = llapi_parse_size(optarg, &length,
9340 fprintf(stderr, "%s: bad length '%s'\n",
9346 mode = lfs_get_mode(optarg);
9349 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9357 fprintf(stderr, "%s: option '%s' unrecognized\n",
9358 argv[0], argv[optind - 1]);
9363 if (advice_type == LU_LADVISE_INVALID) {
9364 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9365 fprintf(stderr, "Valid types:");
9366 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9368 if (ladvise_names[advice_type] == NULL)
9370 fprintf(stderr, " %s", ladvise_names[advice_type]);
9372 fprintf(stderr, "\n");
9376 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9378 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9383 if (argc <= optind) {
9384 fprintf(stderr, "%s: please give one or more file names\n",
9389 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9390 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9395 if (end == LUSTRE_EOF && length != 0)
9396 end = start + length;
9399 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9400 argv[0], start, end);
9404 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9405 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9410 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9411 fprintf(stderr, "%s: mode is required with lockahead\n",
9416 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 advice.lla_start = start;
9430 advice.lla_end = end;
9431 advice.lla_advice = advice_type;
9432 advice.lla_value1 = 0;
9433 advice.lla_value2 = 0;
9434 advice.lla_value3 = 0;
9435 advice.lla_value4 = 0;
9436 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9437 advice.lla_lockahead_mode = mode;
9438 advice.lla_peradvice_flags = flags;
9441 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9445 "%s: cannot give advice '%s' to file '%s': %s\n",
9446 argv[0], ladvise_names[advice_type],
9447 path, strerror(errno));
9453 if (rc == 0 && rc2 < 0)
9459 static const char *const heat_names[] = LU_HEAT_NAMES;
9461 static int lfs_heat_get(int argc, char **argv)
9463 struct lu_heat *heat;
9472 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9474 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9479 while (optind < argc) {
9480 path = argv[optind++];
9482 fd = open(path, O_RDONLY);
9484 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9485 argv[0], path, strerror(errno));
9490 heat->lh_count = OBD_HEAT_COUNT;
9491 rc2 = llapi_heat_get(fd, heat);
9495 "%s: cannot get heat of file '%s': %s\n",
9496 argv[0], path, strerror(errno));
9500 printf("flags: %x\n", heat->lh_flags);
9501 for (i = 0; i < heat->lh_count; i++)
9502 printf("%s: %llu\n", heat_names[i],
9503 (unsigned long long)heat->lh_heat[i]);
9505 if (rc == 0 && rc2 < 0)
9513 static int lfs_heat_set(int argc, char **argv)
9515 struct option long_opts[] = {
9516 { .val = 'c', .name = "clear", .has_arg = no_argument },
9517 { .val = 'o', .name = "off", .has_arg = no_argument },
9518 { .val = 'O', .name = "on", .has_arg = no_argument },
9520 enum lu_heat_flag flags = 0;
9530 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9533 flags |= LU_HEAT_FLAG_CLEAR;
9536 flags |= LU_HEAT_FLAG_CLEAR;
9537 flags |= LU_HEAT_FLAG_OFF;
9540 flags &= ~LU_HEAT_FLAG_OFF;
9545 fprintf(stderr, "%s: option '%s' unrecognized\n",
9546 argv[0], argv[optind - 1]);
9551 if (argc <= optind) {
9552 fprintf(stderr, "%s: please give one or more file names\n",
9557 while (optind < argc) {
9558 path = argv[optind++];
9560 fd = open(path, O_RDONLY);
9562 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9563 argv[0], path, strerror(errno));
9568 rc2 = llapi_heat_set(fd, flags);
9572 "%s: cannot setflags heat of file '%s': %s\n",
9573 argv[0], path, strerror(errno));
9577 if (rc == 0 && rc2 < 0)
9584 * The input string contains a comma delimited list of component ids and
9585 * ranges, for example "1,2-4,7".
9587 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9589 bool end_of_loop = false;
9597 while (!end_of_loop) {
9601 char *endptr = NULL;
9604 ptr = strchrnul(arg, ',');
9605 end_of_loop = *ptr == '\0';
9608 start_index = strtol(arg, &endptr, 0);
9609 if (endptr == arg) /* no data at all */
9611 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9613 if (start_index < 0)
9616 end_index = start_index;
9617 if (*endptr == '-') {
9618 end_index = strtol(endptr + 1, &endptr, 0);
9619 if (*endptr != '\0')
9621 if (end_index < start_index)
9625 for (i = start_index; i <= end_index && size > 0; i++) {
9628 /* remove duplicate */
9629 for (j = 0; j < nr; j++) {
9633 if (j == nr) { /* no duplicate */
9639 if (size == 0 && i < end_index)
9646 if (!end_of_loop && ptr)
9649 return rc < 0 ? rc : nr;
9653 * struct verify_mirror_id - Mirror id to be verified.
9654 * @mirror_id: A specified mirror id.
9655 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9657 struct verify_mirror_id {
9663 * compare_mirror_ids() - Compare mirror ids.
9664 * @layout: Mirror component list.
9665 * @cbdata: Callback data in verify_mirror_id structure.
9667 * This is a callback function called by llapi_layout_comp_iterate()
9668 * to compare the specified mirror id with the one in the current
9669 * component of @layout. If they are the same, then the specified
9670 * mirror id is valid.
9672 * Return: a negative error code on failure or
9673 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9674 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9677 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9679 struct verify_mirror_id *mirror_id_cbdata =
9680 (struct verify_mirror_id *)cbdata;
9684 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9688 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9689 progname, strerror(errno));
9693 if (mirror_id_cbdata->mirror_id == mirror_id) {
9694 mirror_id_cbdata->is_valid_id = true;
9695 return LLAPI_LAYOUT_ITER_STOP;
9698 return LLAPI_LAYOUT_ITER_CONT;
9702 * verify_mirror_ids() - Verify specified mirror ids.
9703 * @fname: Mirrored file name.
9704 * @mirror_ids: Specified mirror ids to be verified.
9705 * @ids_nr: Number of specified mirror ids.
9707 * This function verifies that specified @mirror_ids are valid
9708 * in the mirrored file @fname.
9710 * Return: 0 on success or a negative error code on failure.
9713 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9715 struct llapi_layout *layout = NULL;
9716 struct verify_mirror_id mirror_id_cbdata = { 0 };
9727 if (stat(fname, &stbuf) < 0) {
9728 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9729 progname, fname, strerror(errno));
9734 if (!S_ISREG(stbuf.st_mode)) {
9735 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9741 fd = open(fname, O_DIRECT | O_RDONLY);
9743 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9744 progname, fname, strerror(errno));
9749 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9751 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9752 progname, fname, strerror(errno));
9756 layout = llapi_layout_get_by_fd(fd, 0);
9758 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9759 progname, fname, strerror(errno));
9761 llapi_lease_release(fd);
9765 rc = llapi_layout_flags_get(layout, &flr_state);
9767 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9768 progname, fname, strerror(errno));
9773 flr_state &= LCM_FL_FLR_MASK;
9774 switch (flr_state) {
9777 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9778 progname, fname, llapi_layout_flags_string(flr_state));
9785 for (i = 0; i < ids_nr; i++) {
9786 mirror_id_cbdata.mirror_id = mirror_ids[i];
9787 mirror_id_cbdata.is_valid_id = false;
9789 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9794 "%s: '%s' failed to verify mirror id: %u.\n",
9795 progname, fname, mirror_ids[i]);
9799 if (!mirror_id_cbdata.is_valid_id) {
9802 "%s: '%s' invalid specified mirror id: %u.\n",
9803 progname, fname, mirror_ids[i]);
9809 llapi_layout_free(layout);
9810 llapi_lease_release(fd);
9818 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9819 __u16 *mirror_ids, int ids_nr)
9821 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9822 struct llapi_layout *layout;
9833 if (stat(fname, &stbuf) < 0) {
9834 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9835 progname, fname, strerror(errno));
9839 if (!S_ISREG(stbuf.st_mode)) {
9840 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9846 fd = open(fname, O_DIRECT | O_RDWR);
9848 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9849 progname, fname, strerror(errno));
9854 layout = llapi_layout_get_by_fd(fd, 0);
9856 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9857 progname, fname, strerror(errno));
9862 rc = llapi_layout_flags_get(layout, &flr_state);
9864 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9865 progname, fname, strerror(errno));
9870 flr_state &= LCM_FL_FLR_MASK;
9871 if (flr_state == LCM_FL_NONE) {
9873 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9878 /* get stale component info */
9879 comp_size = llapi_mirror_find_stale(layout, comp_array,
9880 ARRAY_SIZE(comp_array),
9881 mirror_ids, ids_nr);
9882 if (comp_size <= 0) {
9887 ioc->lil_mode = LL_LEASE_WRLCK;
9888 ioc->lil_flags = LL_LEASE_RESYNC;
9889 rc = llapi_lease_set(fd, ioc);
9891 if (rc == -EALREADY)
9895 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9896 progname, fname, strerror(-rc));
9900 /* get the read range [start, end) */
9901 start = comp_array[0].lrc_start;
9902 end = comp_array[0].lrc_end;
9903 for (idx = 1; idx < comp_size; idx++) {
9904 if (comp_array[idx].lrc_start < start)
9905 start = comp_array[idx].lrc_start;
9906 if (end < comp_array[idx].lrc_end)
9907 end = comp_array[idx].lrc_end;
9910 rc = llapi_lease_check(fd);
9911 if (rc != LL_LEASE_WRLCK) {
9912 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9917 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9920 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9921 progname, fname, strerror(-rc));
9923 /* need to do the lease unlock even resync fails */
9924 ioc->lil_mode = LL_LEASE_UNLCK;
9925 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9927 for (idx = 0; idx < comp_size; idx++) {
9928 if (comp_array[idx].lrc_synced) {
9929 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9934 rc2 = llapi_lease_set(fd, ioc);
9936 * llapi_lease_set returns lease mode when it request to unlock
9940 /* rc2 == 0 means lost lease lock */
9941 if (rc2 == 0 && rc == 0)
9943 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9945 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9949 llapi_layout_free(layout);
9956 static inline int lfs_mirror_resync(int argc, char **argv)
9958 struct ll_ioc_lease *ioc = NULL;
9959 __u16 mirror_ids[128] = { 0 };
9964 struct option long_opts[] = {
9965 { .val = 'o', .name = "only", .has_arg = required_argument },
9968 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9971 rc = parse_mirror_ids(mirror_ids,
9972 sizeof(mirror_ids) / sizeof(__u16),
9976 "%s: bad mirror ids '%s'.\n",
9983 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9984 argv[0], argv[optind - 1]);
9990 if (argc == optind) {
9991 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9996 if (ids_nr > 0 && argc > optind + 1) {
9998 "%s: option '--only' cannot be used upon multiple files.\n",
10005 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10010 /* set the lease on the file */
10011 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10013 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10014 argv[0], strerror(errno));
10019 for (; optind < argc; optind++) {
10020 rc = lfs_mirror_resync_file(argv[optind], ioc,
10021 mirror_ids, ids_nr);
10022 /* ignore previous file's error, continue with next file */
10025 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10033 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10035 struct llapi_layout *layout;
10038 layout = llapi_layout_get_by_fd(fd, 0);
10040 fprintf(stderr, "could not get layout.\n");
10044 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10046 fprintf(stderr, "failed to iterate layout\n");
10047 llapi_layout_free(layout);
10050 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10051 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10052 llapi_layout_free(layout);
10056 llapi_layout_free(layout);
10062 * Check whether two files are the same file
10063 * \retval 0 same file
10064 * \retval 1 not the same file
10065 * \retval <0 error code
10067 static inline int check_same_file(int fd, const char *f2)
10069 struct stat stbuf1;
10070 struct stat stbuf2;
10072 if (fstat(fd, &stbuf1) < 0)
10075 if (stat(f2, &stbuf2) < 0)
10078 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10079 stbuf1.st_ino == stbuf2.st_ino)
10085 static inline int lfs_mirror_read(int argc, char **argv)
10088 __u16 mirror_id = 0;
10089 const char *outfile = NULL;
10095 const size_t buflen = 4 << 20;
10097 struct option long_opts[] = {
10098 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10099 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10100 { .name = NULL } };
10102 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10107 mirror_id = strtoul(optarg, &end, 0);
10108 if (*end != '\0' || mirror_id == 0) {
10110 "%s %s: invalid mirror ID '%s'\n",
10111 progname, argv[0], optarg);
10119 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10120 progname, argv[optind - 1]);
10125 if (argc == optind) {
10126 fprintf(stderr, "%s %s: no mirrored file provided\n",
10127 progname, argv[0]);
10129 } else if (argc > optind + 1) {
10130 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10134 if (mirror_id == 0) {
10135 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10136 progname, argv[0]);
10140 /* open mirror file */
10141 fname = argv[optind];
10142 fd = open(fname, O_DIRECT | O_RDONLY);
10144 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10145 progname, argv[0], fname, strerror(errno));
10149 /* verify mirror id */
10150 rc = verify_mirror_id_by_fd(fd, mirror_id);
10153 "%s %s: cannot find mirror with ID %u in '%s'\n",
10154 progname, argv[0], mirror_id, fname);
10158 /* open output file - O_EXCL ensures output is not the same as input */
10160 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10162 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10163 progname, argv[0], outfile, strerror(errno));
10168 outfd = STDOUT_FILENO;
10171 /* allocate buffer */
10172 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10174 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10175 progname, argv[0], rc);
10181 ssize_t bytes_read;
10182 ssize_t written = 0;
10184 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10185 if (bytes_read < 0) {
10188 "%s %s: fail to read data from mirror %u: %s\n",
10189 progname, argv[0], mirror_id, strerror(-rc));
10194 if (bytes_read == 0)
10197 while (written < bytes_read) {
10200 written2 = write(outfd, buf + written,
10201 bytes_read - written);
10202 if (written2 < 0) {
10204 "%s %s: fail to write %s: %s\n",
10205 progname, argv[0], outfile ? : "STDOUT",
10210 written += written2;
10213 if (written != bytes_read) {
10215 "%s %s: written %ld bytes does not match with %ld read.\n",
10216 progname, argv[0], written, bytes_read);
10238 static inline int lfs_mirror_write(int argc, char **argv)
10241 __u16 mirror_id = 0;
10242 const char *inputfile = NULL;
10248 const size_t buflen = 4 << 20;
10250 size_t page_size = sysconf(_SC_PAGESIZE);
10251 struct ll_ioc_lease_id ioc;
10253 struct option long_opts[] = {
10254 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10255 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10256 { .name = NULL } };
10258 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10263 mirror_id = strtoul(optarg, &end, 0);
10264 if (*end != '\0' || mirror_id == 0) {
10266 "%s %s: invalid mirror ID '%s'\n",
10267 progname, argv[0], optarg);
10272 inputfile = optarg;
10275 fprintf(stderr, "%s: option '%s' unrecognized\n",
10276 progname, argv[optind - 1]);
10281 if (argc == optind) {
10282 fprintf(stderr, "%s %s: no mirrored file provided\n",
10283 progname, argv[0]);
10285 } else if (argc > optind + 1) {
10286 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10290 if (mirror_id == 0) {
10291 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10292 progname, argv[0]);
10296 /* open mirror file */
10297 fname = argv[optind];
10298 fd = open(fname, O_DIRECT | O_WRONLY);
10300 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10301 progname, argv[0], fname, strerror(errno));
10305 /* verify mirror id */
10306 rc = verify_mirror_id_by_fd(fd, mirror_id);
10309 "%s %s: cannot find mirror with ID %u in '%s'\n",
10310 progname, argv[0], mirror_id, fname);
10314 /* open input file */
10316 rc = check_same_file(fd, inputfile);
10319 "%s %s: input file cannot be the mirrored file\n",
10320 progname, argv[0]);
10326 inputfd = open(inputfile, O_RDONLY, 0644);
10328 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10329 progname, argv[0], inputfile, strerror(errno));
10334 inputfd = STDIN_FILENO;
10337 /* allocate buffer */
10338 rc = posix_memalign(&buf, page_size, buflen);
10340 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10341 progname, argv[0], rc);
10342 goto close_inputfd;
10345 /* prepare target mirror components instantiation */
10346 ioc.lil_mode = LL_LEASE_WRLCK;
10347 ioc.lil_flags = LL_LEASE_RESYNC;
10348 ioc.lil_mirror_id = mirror_id;
10349 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10352 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10353 progname, argv[0], fname, strerror(errno));
10359 ssize_t bytes_read;
10363 rc = llapi_lease_check(fd);
10364 if (rc != LL_LEASE_WRLCK) {
10365 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10366 progname, argv[0], fname);
10370 bytes_read = read(inputfd, buf, buflen);
10371 if (bytes_read < 0) {
10374 "%s %s: fail to read data from '%s': %s\n",
10375 progname, argv[0], inputfile ? : "STDIN",
10382 if (bytes_read == 0)
10385 /* round up to page align to make direct IO happy. */
10386 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10388 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10393 "%s %s: fail to write to mirror %u: %s\n",
10394 progname, argv[0], mirror_id,
10402 if (pos & (page_size - 1)) {
10403 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10408 ioc.lil_mode = LL_LEASE_UNLCK;
10409 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10411 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10416 "%s %s: release lease lock of '%s' failed: %s\n",
10417 progname, argv[0], fname, strerror(errno));
10434 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10436 struct llapi_layout *layout;
10437 struct collect_ids_data cid = { .cid_ids = ids,
10439 .cid_exclude = exclude_id, };
10442 layout = llapi_layout_get_by_fd(fd, 0);
10444 fprintf(stderr, "could not get layout\n");
10448 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10450 fprintf(stderr, "failed to iterate layout\n");
10451 llapi_layout_free(layout);
10455 llapi_layout_free(layout);
10457 return cid.cid_count;
10460 static inline int lfs_mirror_copy(int argc, char **argv)
10463 __u16 read_mirror_id = 0;
10464 __u16 ids[128] = { 0 };
10466 struct llapi_layout *layout = NULL;
10467 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10474 struct ll_ioc_lease *ioc = NULL;
10475 struct ll_ioc_lease_id *resync_ioc;
10477 struct option long_opts[] = {
10478 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10479 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10480 { .name = NULL } };
10482 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10487 read_mirror_id = strtoul(optarg, &end, 0);
10488 if (*end != '\0' || read_mirror_id == 0) {
10490 "%s %s: invalid read mirror ID '%s'\n",
10491 progname, argv[0], optarg);
10496 if (!strcmp(optarg, "-1")) {
10497 /* specify all other mirrors */
10498 ids[0] = (__u16)-1;
10501 count = parse_mirror_ids((__u16 *)ids,
10509 fprintf(stderr, "%s: option '%s' unrecognized\n",
10510 progname, argv[optind - 1]);
10515 if (argc == optind) {
10516 fprintf(stderr, "%s %s: no mirrored file provided\n",
10517 progname, argv[0]);
10519 } else if (argc > optind + 1) {
10520 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10524 if (read_mirror_id == 0) {
10526 "%s %s: no valid read mirror ID %d is provided\n",
10527 progname, argv[0], read_mirror_id);
10533 "%s %s: no write mirror ID is provided\n",
10534 progname, argv[0]);
10538 for (i = 0; i < count; i++) {
10539 if (read_mirror_id == ids[i]) {
10541 "%s %s: read and write mirror ID cannot be the same\n",
10542 progname, argv[0]);
10547 /* open mirror file */
10548 fname = argv[optind];
10550 fd = open(fname, O_DIRECT | O_RDWR);
10552 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10553 progname, argv[0], fname, strerror(errno));
10557 /* write to all other mirrors */
10558 if (ids[0] == (__u16)-1) {
10559 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10563 "%s %s: failed to get other mirror ids in '%s': %d\n",
10564 progname, argv[0], fname, rc);
10569 /* verify mirror id */
10570 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10573 "%s %s: cannot find mirror with ID %u in '%s'\n",
10574 progname, argv[0], read_mirror_id, fname);
10578 for (i = 0; i < count; i++) {
10579 rc = verify_mirror_id_by_fd(fd, ids[i]);
10582 "%s %s: cannot find mirror with ID %u in '%s'\n",
10583 progname, argv[0], ids[i], fname);
10588 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10591 "%s %s: cannot alloc comp id array for ioc: %s\n",
10592 progname, argv[0], strerror(errno));
10597 /* get stale component info */
10598 layout = llapi_layout_get_by_fd(fd, 0);
10600 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10601 progname, argv[0], fname, strerror(errno));
10605 comp_size = llapi_mirror_find_stale(layout, comp_array,
10606 ARRAY_SIZE(comp_array),
10608 llapi_layout_free(layout);
10609 if (comp_size < 0) {
10614 /* prepare target mirror components instantiation */
10615 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10616 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10617 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10619 resync_ioc->lil_mirror_id = ids[0];
10621 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10622 rc = llapi_lease_set(fd, ioc);
10625 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10626 progname, argv[0], fname, strerror(errno));
10630 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10633 fprintf(stderr, "%s %s: copy error: %d\n",
10634 progname, argv[0], rc);
10638 fprintf(stdout, "mirror copied successfully: ");
10639 for (i = 0; i < copied; i++)
10640 fprintf(stdout, "%d ", ids[i]);
10641 fprintf(stdout, "\n");
10643 ioc->lil_mode = LL_LEASE_UNLCK;
10644 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10645 ioc->lil_count = 0;
10646 for (i = 0; i < comp_size; i++) {
10649 for (j = 0; j < copied; j++) {
10650 if (comp_array[i].lrc_mirror_id != ids[j])
10653 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10657 rc = llapi_lease_set(fd, ioc);
10662 "%s %s: release lease lock of '%s' failed: %s\n",
10663 progname, argv[0], fname, strerror(errno));
10678 * struct verify_chunk - Mirror chunk to be verified.
10679 * @chunk: [start, end) of the chunk.
10680 * @mirror_count: Number of mirror ids in @mirror_id array.
10681 * @mirror_id: Array of valid mirror ids that cover the chunk.
10683 struct verify_chunk {
10684 struct lu_extent chunk;
10685 unsigned int mirror_count;
10686 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10690 * print_chunks() - Print chunk information.
10691 * @fname: Mirrored file name.
10692 * @chunks: Array of chunks.
10693 * @chunk_count: Number of chunks in @chunks array.
10695 * This function prints [start, end) of each chunk in @chunks
10696 * for mirrored file @fname, and also prints the valid mirror ids
10697 * that cover the chunk.
10702 void print_chunks(const char *fname, struct verify_chunk *chunks,
10708 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10709 for (i = 0; i < chunk_count; i++) {
10710 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10712 if (chunks[i].mirror_count == 0)
10713 fprintf(stdout, "\t[");
10715 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10716 for (j = 1; j < chunks[i].mirror_count; j++)
10717 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10719 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10721 fprintf(stdout, "\n");
10725 * print_checksums() - Print CRC-32 checksum values.
10726 * @chunk: A chunk and its corresponding valid mirror ids.
10727 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10729 * This function prints CRC-32 checksum values on @chunk for
10730 * each valid mirror that covers it.
10735 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10740 "CRC-32 checksum value for chunk "DEXT":\n",
10741 PEXT(&chunk->chunk));
10742 for (i = 0; i < chunk->mirror_count; i++)
10743 fprintf(stdout, "Mirror %u:\t%#lx\n",
10744 chunk->mirror_id[i], crc[i]);
10745 fprintf(stdout, "\n");
10749 * filter_mirror_id() - Filter specified mirror ids.
10750 * @chunks: Array of chunks.
10751 * @chunk_count: Number of chunks in @chunks array.
10752 * @mirror_ids: Specified mirror ids to be verified.
10753 * @ids_nr: Number of specified mirror ids.
10755 * This function scans valid mirror ids that cover each chunk in @chunks
10756 * and filters specified mirror ids.
10761 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10762 __u16 *mirror_ids, int ids_nr)
10767 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10768 unsigned int valid_count = 0;
10770 for (i = 0; i < chunk_count; i++) {
10771 if (chunks[i].mirror_count == 0)
10775 for (j = 0; j < ids_nr; j++) {
10776 for (k = 0; k < chunks[i].mirror_count; k++) {
10777 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10778 valid_id[valid_count] = mirror_ids[j];
10785 memcpy(chunks[i].mirror_id, valid_id,
10786 sizeof(__u16) * valid_count);
10787 chunks[i].mirror_count = valid_count;
10792 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10793 * @layout: Mirror component list.
10794 * @chunks: Array of chunks.
10795 * @chunks_size: Array size of @chunks.
10797 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10798 * to find out chunk segments and store them in @chunks array.
10800 * The @mirror_id array in each element of @chunks will store the valid
10801 * mirror ids that cover the chunk. If a mirror component covering the
10802 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10803 * will not be stored into the @mirror_id array, and the chunk for that
10804 * mirror will not be verified.
10806 * The @mirror_count in each element of @chunks will store the number of
10807 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10808 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10809 * indicates the chunk is valid in only one mirror. In both cases, the
10810 * chunk will not be verified.
10812 * Here is an example:
10814 * 0 1M 2M 3M 4M EOF
10815 * +------+-------------+--------------------+
10816 * | | | S | mirror1
10817 * +------+------+------+------+-------------+
10818 * | | S | S | | mirror2
10819 * +-------------+------+------+-------------+
10821 * prepared @chunks array will contain 5 elements:
10822 * (([0, 1M), [1, 2], 2),
10823 * ([1M, 2M), [1, 2], 2),
10824 * ([2M, 3M), [1], 1),
10825 * ([3M, 4M], [], 0),
10826 * ([4M, EOF), [2], 1))
10828 * Return: the actual array size of @chunks on success
10829 * or a negative error code on failure.
10832 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10833 struct verify_chunk *chunks,
10834 size_t chunks_size)
10838 uint32_t mirror_id;
10844 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10847 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10850 "%s: move to the first layout component: %s.\n",
10851 progname, strerror(errno));
10857 chunks[idx].chunk.e_end = LUSTRE_EOF;
10859 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10862 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10863 progname, strerror(errno));
10867 if (start > chunks[idx].chunk.e_start ||
10868 end <= chunks[idx].chunk.e_start)
10871 if (end < chunks[idx].chunk.e_end)
10872 chunks[idx].chunk.e_end = end;
10874 rc = llapi_layout_comp_flags_get(layout, &flags);
10877 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10878 progname, strerror(errno));
10882 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10885 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10888 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10889 progname, strerror(errno));
10893 chunks[idx].mirror_id[i] = mirror_id;
10895 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10897 "%s: mirror_id array is too small.\n",
10904 rc = llapi_layout_comp_use(layout,
10905 LLAPI_LAYOUT_COMP_USE_NEXT);
10908 "%s: move to the next layout component: %s.\n",
10909 progname, strerror(errno));
10912 } /* loop through all components */
10914 chunks[idx].mirror_count = i;
10916 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10920 if (idx >= chunks_size) {
10921 fprintf(stderr, "%s: chunks array is too small.\n",
10927 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10931 return rc < 0 ? rc : idx + 1;
10935 * lfs_mirror_verify_chunk() - Verify a chunk.
10936 * @fd: File descriptor of the mirrored file.
10937 * @file_size: Size of the mirrored file.
10938 * @chunk: A chunk and its corresponding valid mirror ids.
10939 * @verbose: Verbose mode.
10941 * This function verifies a @chunk contains exactly the same data
10942 * ammong the mirrors that cover it.
10944 * If @verbose is specified, then the function will print where the
10945 * differences are if the data do not match. Otherwise, it will
10946 * just return an error in that case.
10948 * Return: 0 on success or a negative error code on failure.
10951 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10952 struct verify_chunk *chunk, int verbose)
10954 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10956 size_t page_size = sysconf(_SC_PAGESIZE);
10957 ssize_t bytes_read;
10958 ssize_t bytes_done;
10962 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10966 if (file_size == 0)
10969 rc = posix_memalign(&buf, page_size, buflen);
10970 if (rc) /* error code is returned directly */
10974 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10975 PEXT(&chunk->chunk));
10976 for (i = 0; i < chunk->mirror_count; i++)
10977 fprintf(stdout, " %u", chunk->mirror_id[i]);
10978 fprintf(stdout, "\n");
10982 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10983 pos = chunk->chunk.e_start;
10984 while (bytes_done < count) {
10985 /* compute initial CRC-32 checksum */
10986 crc = crc32(0L, Z_NULL, 0);
10987 memset(crc_array, 0, sizeof(crc_array));
10990 for (i = 0; i < chunk->mirror_count; i++) {
10991 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10993 if (bytes_read < 0) {
10996 "%s: failed to read data from mirror %u: %s.\n",
10997 progname, chunk->mirror_id[i],
11002 /* compute new CRC-32 checksum */
11003 crc_array[i] = crc32(crc, buf, bytes_read);
11007 print_checksums(chunk, crc_array);
11009 /* compare CRC-32 checksum values */
11010 for (i = 1; i < chunk->mirror_count; i++) {
11011 if (crc_array[i] != crc_array[0]) {
11015 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11016 progname, PEXT(&chunk->chunk),
11017 chunk->mirror_id[0],
11018 chunk->mirror_id[i]);
11023 bytes_done += bytes_read;
11026 if (verbose > 1 && rc == 0) {
11027 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11028 PEXT(&chunk->chunk));
11029 for (i = 0; i < chunk->mirror_count; i++)
11030 fprintf(stdout, " %u", chunk->mirror_id[i]);
11031 fprintf(stdout, " PASS\n\n");
11040 * lfs_mirror_verify_file() - Verify a mirrored file.
11041 * @fname: Mirrored file name.
11042 * @mirror_ids: Specified mirror ids to be verified.
11043 * @ids_nr: Number of specified mirror ids.
11044 * @verbose: Verbose mode.
11046 * This function verifies that each SYNC mirror of a mirrored file
11047 * specified by @fname contains exactly the same data.
11049 * If @mirror_ids is specified, then the function will verify the
11050 * mirrors specified by @mirror_ids contain exactly the same data.
11052 * If @verbose is specified, then the function will print where the
11053 * differences are if the data do not match. Otherwise, it will
11054 * just return an error in that case.
11056 * Return: 0 on success or a negative error code on failure.
11059 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11062 struct verify_chunk chunks_array[1024] = { };
11063 struct llapi_layout *layout = NULL;
11065 uint32_t flr_state;
11067 int chunk_count = 0;
11073 if (stat(fname, &stbuf) < 0) {
11074 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11075 progname, fname, strerror(errno));
11080 if (!S_ISREG(stbuf.st_mode)) {
11081 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11087 if (stbuf.st_size == 0) {
11089 fprintf(stdout, "%s: '%s' file size is 0.\n",
11095 fd = open(fname, O_DIRECT | O_RDONLY);
11097 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11098 progname, fname, strerror(errno));
11103 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11105 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11106 progname, fname, strerror(errno));
11110 layout = llapi_layout_get_by_fd(fd, 0);
11112 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11113 progname, fname, strerror(errno));
11115 llapi_lease_release(fd);
11119 rc = llapi_layout_flags_get(layout, &flr_state);
11121 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11122 progname, fname, strerror(errno));
11127 flr_state &= LCM_FL_FLR_MASK;
11128 switch (flr_state) {
11131 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11132 progname, fname, llapi_layout_flags_string(flr_state));
11138 /* find out mirror chunks to be verified */
11139 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11140 ARRAY_SIZE(chunks_array));
11141 if (chunk_count < 0) {
11147 /* filter specified mirror ids */
11148 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11151 print_chunks(fname, chunks_array, chunk_count);
11153 for (idx = 0; idx < chunk_count; idx++) {
11154 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11157 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11159 PEXT(&chunks_array[idx].chunk),
11160 (unsigned long long)stbuf.st_size);
11164 if (chunks_array[idx].mirror_count == 0) {
11166 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11168 PEXT(&chunks_array[idx].chunk));
11170 fprintf(stderr, "skipped\n");
11174 fprintf(stderr, "failed\n");
11178 if (chunks_array[idx].mirror_count == 1) {
11181 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11183 PEXT(&chunks_array[idx].chunk),
11184 chunks_array[idx].mirror_id[0]);
11188 rc = llapi_lease_check(fd);
11189 if (rc != LL_LEASE_RDLCK) {
11190 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11195 /* verify one chunk */
11196 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11197 &chunks_array[idx], verbose);
11211 llapi_layout_free(layout);
11212 llapi_lease_release(fd);
11220 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11221 * @argc: The count of lfs mirror verify command line arguments.
11222 * @argv: Array of strings for lfs mirror verify command line arguments.
11224 * This function parses lfs mirror verify command and verifies the
11225 * specified mirrored file(s).
11227 * Return: 0 on success or a negative error code on failure.
11229 static inline int lfs_mirror_verify(int argc, char **argv)
11231 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11237 char cmd[PATH_MAX];
11239 struct option long_opts[] = {
11240 { .val = 'o', .name = "only", .has_arg = required_argument },
11241 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11242 { .name = NULL } };
11244 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11246 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11249 rc = parse_mirror_ids(mirror_ids,
11250 ARRAY_SIZE(mirror_ids),
11254 "%s: bad mirror ids '%s'.\n",
11261 "%s: at least 2 mirror ids needed with '--only' option.\n",
11271 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11272 progname, argv[optind - 1]);
11278 if (argc == optind) {
11279 fprintf(stderr, "%s: no file name given.\n", progname);
11284 if (ids_nr > 0 && argc > optind + 1) {
11286 "%s: '--only' cannot be used upon multiple files.\n",
11293 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11299 for (; optind < argc; optind++) {
11300 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11310 * lfs_mirror() - Parse and execute lfs mirror commands.
11311 * @argc: The count of lfs mirror command line arguments.
11312 * @argv: Array of strings for lfs mirror command line arguments.
11314 * This function parses lfs mirror commands and performs the
11315 * corresponding functions specified in mirror_cmdlist[].
11317 * Return: 0 on success or an error code on failure.
11319 static int lfs_mirror(int argc, char **argv)
11321 char cmd[PATH_MAX];
11324 setlinebuf(stdout);
11326 Parser_init("lfs-mirror > ", mirror_cmdlist);
11328 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11330 program_invocation_short_name = cmd;
11332 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11334 rc = Parser_commands();
11336 return rc < 0 ? -rc : rc;
11339 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11341 #if __BYTE_ORDER == __BIG_ENDIAN
11342 __swab16s(&attrs->lsa_valid);
11343 __swab64s(&attrs->lsa_size);
11344 __swab64s(&attrs->lsa_blocks);
11348 enum lfs_som_type {
11349 LFS_SOM_SIZE = 0x1,
11350 LFS_SOM_BLOCKS = 0x2,
11351 LFS_SOM_FLAGS = 0x4,
11352 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11356 static int lfs_getsom(int argc, char **argv)
11359 struct lustre_som_attrs *attrs;
11360 char buf[sizeof(*attrs) + 64];
11361 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11364 while ((c = getopt(argc, argv, "sbf")) != -1) {
11367 type = LFS_SOM_SIZE;
11370 type = LFS_SOM_BLOCKS;
11373 type = LFS_SOM_FLAGS;
11376 fprintf(stderr, "%s: invalid option '%c'\n",
11386 fprintf(stderr, "%s: %s\n",
11387 progname, argc == 0 ? "miss file target" :
11388 "input more than 2 files");
11393 attrs = (void *)buf;
11394 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11397 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11398 argv[0], strerror(errno), errno);
11402 lustre_som_swab(attrs);
11405 case LFS_SOM_ATTR_ALL:
11406 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11407 path, (unsigned long long)attrs->lsa_size,
11408 (unsigned long long)attrs->lsa_blocks,
11412 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11414 case LFS_SOM_BLOCKS:
11415 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11417 case LFS_SOM_FLAGS:
11418 printf("%x\n", attrs->lsa_valid);
11421 fprintf(stderr, "%s: unknown option\n", progname);
11429 * lfs_mirror_list_commands() - List lfs mirror commands.
11430 * @argc: The count of command line arguments.
11431 * @argv: Array of strings for command line arguments.
11433 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11435 * Return: 0 on success.
11437 static int lfs_mirror_list_commands(int argc, char **argv)
11439 char buffer[81] = "";
11441 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11447 static int lfs_pcc_attach(int argc, char **argv)
11449 struct option long_opts[] = {
11450 { .val = 'i', .name = "id", .has_arg = required_argument },
11451 { .name = NULL } };
11454 __u32 archive_id = 0;
11457 char fullpath[PATH_MAX];
11458 enum lu_pcc_type type = LU_PCC_READWRITE;
11461 while ((c = getopt_long(argc, argv, "i:",
11462 long_opts, NULL)) != -1) {
11465 archive_id = strtoul(optarg, &end, 0);
11466 if (*end != '\0' || archive_id == 0) {
11468 "error: %s: bad archive ID '%s'\n",
11476 fprintf(stderr, "%s: option '%s' unrecognized\n",
11477 argv[0], argv[optind - 1]);
11482 if (archive_id == 0) {
11483 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11487 if (argc <= optind) {
11488 fprintf(stderr, "%s: must specify one or more file names\n",
11493 while (optind < argc) {
11496 path = argv[optind++];
11497 if (!realpath(path, fullpath)) {
11498 fprintf(stderr, "%s: could not find path '%s': %s\n",
11499 argv[0], path, strerror(errno));
11505 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11508 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11509 argv[0], path, archive_id, strerror(-rc2));
11517 static int lfs_pcc_attach_fid(int argc, char **argv)
11519 struct option long_opts[] = {
11520 { .val = 'i', .name = "id", .has_arg = required_argument },
11521 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11522 { .name = NULL } };
11523 char short_opts[] = "i:m:";
11526 __u32 archive_id = 0;
11528 const char *mntpath = NULL;
11529 const char *fidstr;
11530 enum lu_pcc_type type = LU_PCC_READWRITE;
11533 while ((c = getopt_long(argc, argv, short_opts,
11534 long_opts, NULL)) != -1) {
11537 archive_id = strtoul(optarg, &end, 0);
11538 if (*end != '\0') {
11540 "error: %s: bad archive ID '%s'\n",
11551 fprintf(stderr, "%s: option '%s' unrecognized\n",
11552 argv[0], argv[optind - 1]);
11557 if (archive_id == 0) {
11558 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11563 fprintf(stderr, "%s: must specify Lustre mount point\n",
11568 if (argc <= optind) {
11569 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11573 while (optind < argc) {
11576 fidstr = argv[optind++];
11578 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11582 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11583 argv[0], fidstr, mntpath, archive_id,
11586 if (rc == 0 && rc2 < 0)
11592 static int lfs_pcc_detach(int argc, char **argv)
11594 struct option long_opts[] = {
11595 { .val = 'k', .name = "keep", .has_arg = no_argument },
11596 { .name = NULL } };
11597 char short_opts[] = "k";
11601 char fullpath[PATH_MAX];
11602 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11605 while ((c = getopt_long(argc, argv, short_opts,
11606 long_opts, NULL)) != -1) {
11609 detach_opt = PCC_DETACH_OPT_NONE;
11614 fprintf(stderr, "%s: option '%s' unrecognized\n",
11615 argv[0], argv[optind - 1]);
11620 while (optind < argc) {
11623 path = argv[optind++];
11624 if (!realpath(path, fullpath)) {
11625 fprintf(stderr, "%s: could not find path '%s': %s\n",
11626 argv[0], path, strerror(errno));
11632 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11636 "%s: cannot detach '%s' from PCC: %s\n",
11637 argv[0], path, strerror(errno));
11645 static int lfs_pcc_detach_fid(int argc, char **argv)
11647 struct option long_opts[] = {
11648 { .val = 'k', .name = "keep", .has_arg = no_argument },
11649 { .name = NULL } };
11650 char short_opts[] = "k";
11654 const char *mntpath;
11655 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11658 while ((c = getopt_long(argc, argv, short_opts,
11659 long_opts, NULL)) != -1) {
11662 detach_opt = PCC_DETACH_OPT_NONE;
11667 fprintf(stderr, "%s: option '%s' unrecognized\n",
11668 argv[0], argv[optind - 1]);
11673 mntpath = argv[optind++];
11675 while (optind < argc) {
11678 fid = argv[optind++];
11680 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11683 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11684 argv[0], fid, mntpath, strerror(-rc2));
11692 static int lfs_pcc_state(int argc, char **argv)
11696 char fullpath[PATH_MAX];
11697 struct lu_pcc_state state;
11702 fprintf(stderr, "%s: must specify one or more file names\n",
11707 while (optind < argc) {
11710 path = argv[optind++];
11711 if (!realpath(path, fullpath)) {
11712 fprintf(stderr, "%s: could not find path '%s': %s\n",
11713 argv[0], path, strerror(errno));
11719 rc2 = llapi_pcc_state_get(fullpath, &state);
11724 "%s: cannot get PCC state of '%s': %s\n",
11725 argv[0], path, strerror(-rc2));
11729 printf("file: %s", path);
11730 printf(", type: %s", pcc_type2string(state.pccs_type));
11731 if (state.pccs_type == LU_PCC_NONE &&
11732 state.pccs_open_count == 0) {
11737 printf(", PCC file: %s", state.pccs_path);
11738 printf(", user number: %u", state.pccs_open_count);
11739 printf(", flags: %x", state.pccs_flags);
11746 * lfs_pcc_list_commands() - List lfs pcc commands.
11747 * @argc: The count of command line arguments.
11748 * @argv: Array of strings for command line arguments.
11750 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11752 * Return: 0 on success.
11754 static int lfs_pcc_list_commands(int argc, char **argv)
11756 char buffer[81] = "";
11758 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11765 * lfs_pcc() - Parse and execute lfs pcc commands.
11766 * @argc: The count of lfs pcc command line arguments.
11767 * @argv: Array of strings for lfs pcc command line arguments.
11769 * This function parses lfs pcc commands and performs the
11770 * corresponding functions specified in pcc_cmdlist[].
11772 * Return: 0 on success or an error code on failure.
11774 static int lfs_pcc(int argc, char **argv)
11776 char cmd[PATH_MAX];
11779 setlinebuf(stdout);
11781 Parser_init("lfs-pcc > ", pcc_cmdlist);
11783 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11785 program_invocation_short_name = cmd;
11787 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11789 rc = Parser_commands();
11791 return rc < 0 ? -rc : rc;
11794 static int lfs_list_commands(int argc, char **argv)
11796 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11798 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11803 int main(int argc, char **argv)
11807 /* Ensure that liblustreapi constructor has run */
11808 if (!llapi_liblustreapi_initialized())
11809 fprintf(stderr, "liblustreapi was not properly initialized\n");
11811 setlinebuf(stdout);
11814 Parser_init("lfs > ", cmdlist);
11816 progname = program_invocation_short_name; /* Used in error messages */
11818 llapi_set_command_name(argv[1]);
11819 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11820 llapi_clear_command_name();
11822 rc = Parser_commands();
11825 return rc < 0 ? -rc : rc;
11828 #ifdef _LUSTRE_IDL_H_
11829 /* Everything we need here should be included by lustreapi.h. */
11830 # error "lfs should not depend on lustre_idl.h"
11831 #endif /* _LUSTRE_IDL_H_ */