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_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_rmfid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_getsom(int argc, char **argv);
119 static int lfs_heat_get(int argc, char **argv);
120 static int lfs_heat_set(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
129 static int lfs_pcc_attach(int argc, char **argv);
130 static int lfs_pcc_attach_fid(int argc, char **argv);
131 static int lfs_pcc_detach(int argc, char **argv);
132 static int lfs_pcc_detach_fid(int argc, char **argv);
133 static int lfs_pcc_state(int argc, char **argv);
134 static int lfs_pcc(int argc, char **argv);
135 static int lfs_pcc_list_commands(int argc, char **argv);
136 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
137 __u64 migration_flags,
138 struct llapi_stripe_param *param,
139 struct llapi_layout *layout);
141 enum setstripe_origin {
151 static int lfs_setstripe_internal(int argc, char **argv,
152 enum setstripe_origin opc);
154 static inline int lfs_setstripe(int argc, char **argv)
156 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
159 static inline int lfs_setstripe_migrate(int argc, char **argv)
161 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
164 static inline int lfs_mirror_create(int argc, char **argv)
166 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
169 static inline int lfs_mirror_extend(int argc, char **argv)
171 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
174 static inline int lfs_mirror_split(int argc, char **argv)
176 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
179 static inline int lfs_mirror_delete(int argc, char **argv)
181 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
184 /* Setstripe and migrate share mostly the same parameters */
185 #define SSM_CMD_COMMON(cmd) \
186 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
187 " [--stripe-count|-c <stripe_count>]\n" \
188 " [--overstripe-count|-C <stripe_count>]\n" \
189 " [--stripe-index|-i <start_ost_idx>]\n" \
190 " [--stripe-size|-S <stripe_size>]\n" \
191 " [--extension-size|--ext-size|-z]\n" \
192 " [--layout|-L <pattern>]\n" \
193 " [--mirror-count|-N[mirror_count]]\n" \
194 " [--ost|-o <ost_indices>]\n" \
195 " [--pool|-p <pool_name>]\n" \
196 " [--yaml|-y <yaml_template_file>]\n" \
197 " [--copy=<lustre_src>]\n"
199 #define SSM_HELP_COMMON \
200 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
201 "\t Using -C instead of -c allows overstriping, which\n" \
202 "\t will place more than one stripe per OST if\n" \
203 "\t stripe_count is greater than the number of OSTs\n" \
204 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
205 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
206 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
207 "\t respectively)\n" \
208 "\textension_size:\n" \
209 "\t Number of bytes the previous component is extended\n" \
210 "\t each time. Can be specified with K, M, G (for KB,\n" \
211 "\t MB, GB respectively)\n" \
212 "\tpool_name: Name of OST pool to use (default none)\n" \
213 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
214 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
215 "\t Indices be specified in a format of:\n" \
216 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
218 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
219 "\t If --pool is set with --ost then the OSTs\n" \
220 "\t must be the members of the pool.\n" \
221 "\tcomp_end: Extent end of component, start after previous end.\n"\
222 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
223 "\t respectively, -1 for EOF). Must be a multiple of\n"\
224 "\t stripe_size.\n" \
225 "\tyaml_template_file:\n" \
226 "\t YAML layout template file, can't be used with -c,\n" \
227 "\t -i, -S, -p, -o, or -E arguments.\n" \
228 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
229 "\t another lustre file or directory, can't used with\n" \
230 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
232 #define MIRROR_CREATE_HELP \
233 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
234 "\t setstripe layout options\n" \
235 "\t It defaults to 1 if not specified; if specified,\n" \
236 "\t it must follow the option without a space.\n" \
237 "\t The option can also be repeated multiple times to\n" \
238 "\t separate mirrors that have different layouts.\n" \
239 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
240 "\t It can be a plain layout or a composite layout.\n" \
241 "\t If not specified, the stripe options inherited\n" \
242 "\t from the previous component will be used.\n" \
243 "\tflags: set flags to the component of the current mirror.\n" \
244 "\t Only \"prefer\" flag is supported so far.\n"
246 #define MIRROR_EXTEND_HELP \
248 "\tvictim_file: The layout of victim_file will be split and used\n" \
249 "\t as a mirror added to the mirrored file.\n" \
250 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
251 "\t from victim file(s) in case the victim file(s)\n" \
252 "\t contains the same data as the original mirrored\n" \
255 #define MIRROR_EXTEND_USAGE \
256 " {--mirror-count|-N[mirror_count]}\n" \
257 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
260 #define SETSTRIPE_USAGE \
261 SSM_CMD_COMMON("setstripe") \
262 MIRROR_EXTEND_USAGE \
263 " <directory|filename>\n" \
267 #define MIGRATE_USAGE \
268 SSM_CMD_COMMON("migrate ") \
269 " [--block|-b] [--non-block|-n]\n" \
270 " [--non-direct|-D] [--verbose|-v]\n" \
274 "\tblock: Block file access during data migration (default)\n" \
275 "\tnon-block: Abort migrations if concurrent access is detected\n" \
276 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
277 "\tverbose: Print each filename as it is migrated\n" \
279 #define SETDIRSTRIPE_USAGE \
280 " [--mdt-count|-c stripe_count>\n" \
281 " [--mdt-hash|-H mdt_hash]\n" \
282 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
283 " [--default|-D] [--mode|-o mode] <dir>\n" \
284 "\tstripe_count: stripe count of the striped directory\n" \
285 "\tmdt_index: MDT index of first stripe\n" \
286 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
287 " crush CRUSH hash algorithm (default)\n" \
288 " fnv_1a_64 FNV-1a hash algorithm\n" \
289 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
290 "\tdefault_stripe: set default dirstripe of the directory\n" \
291 "\tmode: the file access permission of the directory (octal)\n" \
292 "To create dir with a foreign (free format) layout :\n" \
293 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
294 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
295 "\tmode: the mode of the directory\n" \
296 "\tforeign_type: none or daos\n"
299 * command_t mirror_cmdlist - lfs mirror commands.
301 command_t mirror_cmdlist[] = {
302 { .pc_name = "create", .pc_func = lfs_mirror_create,
303 .pc_help = "Create a mirrored file.\n"
304 "usage: lfs mirror create "
305 "<--mirror-count|-N[mirror_count]> "
306 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
307 MIRROR_CREATE_HELP },
308 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
309 .pc_help = "Delete a mirror from a file.\n"
310 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
311 "\t --component-id|--comp-id|-I <comp_id> |\n"
312 "\t -p <pool>} <mirrored_file> ...\n"
314 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
315 .pc_help = "Extend a mirrored file.\n"
316 "usage: lfs mirror extend "
317 "<--mirror-count|-N[mirror_count]> [--no-verify] "
318 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
319 MIRROR_EXTEND_HELP },
320 { .pc_name = "split", .pc_func = lfs_mirror_split,
321 .pc_help = "Split a mirrored file.\n"
322 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
323 "\t --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
324 "\t [-f <new_file>] <mirrored_file> ...\n"
325 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
326 "\t can be fetched by lfs getstripe command.\n"
327 "\tcomp_id: Unique component ID within a mirror.\n"
328 "\tpool: Components on specified pool.\n"
329 "\tnew_file: This option indicates the layout of the split\n"
330 "\t mirror will be stored into. If not specified,\n"
331 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
332 "\t will be used.\n" },
333 { .pc_name = "read", .pc_func = lfs_mirror_read,
334 .pc_help = "Read the content of a specified mirror of a file.\n"
335 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
336 "[--outfile|-o <output_file>] <mirrored_file>\n" },
337 { .pc_name = "write", .pc_func = lfs_mirror_write,
338 .pc_help = "Write to a specified mirror of a file.\n"
339 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
340 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
341 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
342 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
343 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
344 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
345 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
346 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
347 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
348 "<mirrored file> [<mirrored file2>...]\n"},
349 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
350 .pc_help = "Verify mirrored file(s).\n"
351 "usage: lfs mirror verify "
352 "[--only <mirror_id,mirror_id2[,...]>] "
353 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
354 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
355 .pc_help = "list commands supported by lfs mirror"},
356 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
357 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
358 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
363 * command_t pcc_cmdlist - lfs pcc commands.
365 command_t pcc_cmdlist[] = {
366 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
367 .pc_help = "Attach given files to the Persistent Client Cache.\n"
368 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
369 "\t-i: archive id for RW-PCC\n" },
370 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
371 .pc_help = "Attach given files into PCC by FID(s).\n"
372 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
374 "\t-i: archive id for RW-PCC\n"
375 "\t-m: Lustre mount point\n" },
376 { .pc_name = "state", .pc_func = lfs_pcc_state,
377 .pc_help = "Display the PCC state for given files.\n"
378 "usage: lfs pcc state <file> ...\n" },
379 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
380 .pc_help = "Detach given files from the Persistent Client Cache.\n"
381 "usage: lfs pcc detach <file> ...\n" },
382 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
383 .pc_help = "Detach given files from PCC by FID(s).\n"
384 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
385 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
386 .pc_help = "list commands supported by lfs pcc"},
387 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
388 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
389 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
393 /* all available commands */
394 command_t cmdlist[] = {
395 {"setstripe", lfs_setstripe, 0,
396 "To create a file with specified striping/composite layout, or\n"
397 "create/replace the default layout on an existing directory:\n"
398 SSM_CMD_COMMON("setstripe")
400 " <directory|filename>\n"
402 "To add component(s) to an existing composite file:\n"
403 SSM_CMD_COMMON("setstripe --component-add")
405 "To totally delete the default striping from an existing directory:\n"
406 "usage: setstripe [--delete|-d] <directory>\n"
408 "To create a mirrored file or set s default mirror layout on a directory:\n"
409 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
411 "To delete the last component(s) from an existing composite file\n"
412 "(note that this will also delete any data in those components):\n"
413 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
414 " [--component-flags|-F <comp_flags>]\n"
416 "\tcomp_id: Unique component ID to delete\n"
417 "\tcomp_flags: 'init' indicating all instantiated components\n"
418 "\t '^init' indicating all uninstantiated components\n"
419 "\t-I and -F cannot be specified at the same time\n"
420 "To create a file with a foreign (free format) layout:\n"
421 "usage: setstripe --foreign[=<foreign_type>]\n"
422 " --xattr|-x <layout_string> [--flags <hex>]\n"
423 " [--mode <mode>] <filename>\n"},
424 {"getstripe", lfs_getstripe, 0,
425 "To list the layout pattern for a given file or files in a\n"
426 "directory or recursively for all files in a directory tree.\n"
427 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
428 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
429 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
430 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
431 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
432 " [--component-id[=comp_id]|-I[comp_id]]\n"
433 " [--component-flags[=comp_flags]]\n"
434 " [--component-count]\n"
435 " [--extension-size|--ext-size|-z]\n"
436 " [--component-start[=[+-]comp_start]]\n"
437 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
438 " [[!] --mirror-index=[+-]<index> |\n"
439 " [!] --mirror-id=[+-]<id>] [--mirror-count|-N]\n"
440 " <directory|filename> ..."},
441 {"setdirstripe", lfs_setdirstripe, 0,
442 "Create striped directory on specified MDT, same as mkdir.\n"
443 "May be restricted to root or group users, depending on settings.\n"
444 "usage: setdirstripe [OPTION] <directory>\n"
446 {"getdirstripe", lfs_getdirstripe, 0,
447 "To list the layout pattern info for a given directory\n"
448 "or recursively for all directories in a directory tree.\n"
449 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
450 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
451 " [--recursive|-r] [--yaml|-y]\n"
452 " [--verbose|-v] [--default|-D] <dir> ..."},
453 {"mkdir", lfs_setdirstripe, 0,
454 "Create striped directory on specified MDT, same as setdirstripe.\n"
455 "usage: mkdir [OPTION] <directory>\n"
457 {"rm_entry", lfs_rmentry, 0,
458 "To remove the name entry of the remote directory. Note: This\n"
459 "command will only delete the name entry, i.e. the remote directory\n"
460 "will become inaccessable after this command. This can only be done\n"
461 "by the administrator\n"
462 "usage: rm_entry <dir>\n"},
463 {"pool_list", lfs_poollist, 0,
464 "List pools or pool OSTs\n"
465 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
466 {"find", lfs_find, 0,
467 "find files matching given attributes recursively in directory tree.\n"
468 "usage: find <directory|filename> ...\n"
469 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
470 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
471 " [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
472 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
473 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
474 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
475 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
476 " [[!] --stripe-count|-c [+-]<stripes>]\n"
477 " [[!] --stripe-index|-i <index,...>]\n"
478 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
479 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
480 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
481 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
482 " [[!] --projid <projid>]\n"
483 " [[!] --foreign[=<foreign_type>]]\n"
484 " [[!] --layout|-L released,raid0,mdt]\n"
485 " [[!] --foreign[=<foreign_type>]]\n"
486 " [[!] --component-count [+-]<comp_cnt>]\n"
487 " [[!] --component-start [+-]N[kMGTPE]]\n"
488 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
489 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
490 " [[!] --mirror-count|-N [+-]<n>]\n"
491 " [[!] --mirror-state <[^]state>]\n"
492 " [[!] --mdt-count|-T [+-]<stripes>]\n"
493 " [[!] --mdt-hash|-H <hashtype>\n"
494 " [[!] --mdt-index|-m <uuid|index,...>]\n"
495 "\t !: used before an option indicates 'NOT' requested attribute\n"
496 "\t -: used before a value indicates less than requested value\n"
497 "\t +: used before a value indicates more than requested value\n"
498 "\thashtype: hash type of the striped directory.\n"
499 "\t fnv_1a_64 FNV-1a hash algorithm\n"
500 "\t all_char sum of characters % MDT_COUNT\n"},
501 {"check", lfs_check, 0,
502 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
503 "or all the servers (MGTs, MDTs and OSTs).\n"
504 "usage: check <mgts|osts|mdts|all>"},
505 {"osts", lfs_osts, 0, "list OSTs connected to client "
506 "[for specified path only]\n" "usage: osts [path]"},
507 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
508 "[for specified path only]\n" "usage: mdts [path]"},
510 "report filesystem disk space usage or inodes usage "
511 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
512 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
513 " [--pool|-p <fsname>[.<pool>]] [path]"},
514 {"getname", lfs_getname, 0,
515 "list instances and specified mount points [for specified path only]\n"
516 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
517 #ifdef HAVE_SYS_QUOTA_H
518 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
519 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
520 " -b <block-softlimit> -B <block-hardlimit>\n"
521 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
522 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
523 " [--block-softlimit <block-softlimit>]\n"
524 " [--block-hardlimit <block-hardlimit>]\n"
525 " [--inode-softlimit <inode-softlimit>]\n"
526 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
527 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
528 " [--block-grace 'notify'|<block-grace>]\n"
529 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
530 " setquota <-U|-G|-P>\n"
531 " -b <block-softlimit> -B <block-hardlimit>\n"
532 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
533 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
534 " [--block-softlimit <block-softlimit>]\n"
535 " [--block-hardlimit <block-hardlimit>]\n"
536 " [--inode-softlimit <inode-softlimit>]\n"
537 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
538 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
540 " -b can be used instead of --block-softlimit/--block-grace\n"
541 " -B can be used instead of --block-hardlimit\n"
542 " -i can be used instead of --inode-softlimit/--inode-grace\n"
543 " -I can be used instead of --inode-hardlimit\n"
544 " -d can be used instead of --default\n\n"
545 "Note: The total quota space will be split into many qunits and\n"
546 " balanced over all server targets, the minimal qunit size is\n"
547 " 1M bytes for block space and 1K inodes for inode space.\n\n"
548 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
549 " Quota space rebalancing process will stop when this mininum\n"
550 " value is reached. As a result, quota exceeded can be returned\n"
551 " while many targets still have 1MB or 1K inodes of spare\n"
553 " When setting the grace time, 'notify' can be used as grace to\n"
554 " be notified after the quota is over soft limit but prevents\n"
555 " the soft limit from becoming the hard limit."},
556 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
557 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
559 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
560 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
561 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
562 {"project", lfs_project, 0,
563 "Change or list project attribute for specified file or directory.\n"
564 "usage: project [-d|-r] <file|directory...>\n"
565 " list project ID and flags on file(s) or directories\n"
566 " project [-p id] [-s] [-r] <file|directory...>\n"
567 " set project ID and/or inherit flag for specified file(s) or directories\n"
568 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
569 " check project ID and flags on file(s) or directories, print outliers\n"
570 " project -C [-r] [-k] <file|directory...>\n"
571 " clear the project inherit flag and ID on the file or directory\n"
574 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
575 "usage: flushctx [-k] [mountpoint...]"},
576 {"changelog", lfs_changelog, 0,
577 "Show the metadata changes on an MDT."
578 "\nusage: changelog <mdtname> [startrec [endrec]]"},
579 {"changelog_clear", lfs_changelog_clear, 0,
580 "Indicate that old changelog records up to <endrec> are no longer of "
581 "interest to consumer <id>, allowing the system to free up space.\n"
582 "An <endrec> of 0 means all records.\n"
583 "usage: changelog_clear <mdtname> <id> <endrec>"},
584 {"fid2path", lfs_fid2path, 0,
585 "Resolve the full path(s) for given FID(s). For a specific hardlink "
586 "specify link number <linkno>.\n"
587 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
588 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
589 "usage: path2fid [--parents] <path> ..."},
590 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
591 "usage: rmfid <fsname|rootpath> <fid> ..."},
592 {"data_version", lfs_data_version, 0, "Display file data version for "
593 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
594 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
595 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
596 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
597 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
598 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
599 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
601 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
602 "[--archived] [--lost] <file> ..."},
603 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
604 "given files.\n" "usage: hsm_action <file> ..."},
605 {"hsm_archive", lfs_hsm_archive, 0,
606 "Archive file to external storage.\n"
607 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
609 {"hsm_restore", lfs_hsm_restore, 0,
610 "Restore file from external storage.\n"
611 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
612 {"hsm_release", lfs_hsm_release, 0,
613 "Release files from Lustre.\n"
614 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
615 {"hsm_remove", lfs_hsm_remove, 0,
616 "Remove file copy from external storage.\n"
617 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
619 " (FILE [FILE ...] | "
620 "--mntpath MOUNTPATH FID [FID ...])\n"
622 "Note: To remove an archived copy of a file already deleted from a "
624 "--mntpath option and a list of FIDs must be specified"
626 {"hsm_cancel", lfs_hsm_cancel, 0,
627 "Cancel requests related to specified files.\n"
628 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
629 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
630 "usage: swap_layouts <path1> <path2>"},
631 {"migrate", lfs_setstripe_migrate, 0,
632 "migrate a directory between MDTs.\n"
633 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
634 " [--mdt-hash|-H] <hash_type>\n"
635 " [--mdt-index|-m] <start_mdt_index>\n"
638 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
639 " it's the MDT index of first stripe\n"
640 "\tmdt_count: number of MDTs to stripe a directory over\n"
641 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
642 " all_char (type 1)sum of characters % MDT_COUNT\n"
643 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
644 " crush (type 3)CRUSH hash algorithm\n"
646 "migrate file objects from one OST "
647 "layout\nto another (may be not safe with concurent writes).\n"
649 "[--stripe-count|-c] <stripe_count>\n"
650 "[--overstripe-count|-C] <stripe_count>\n"
651 " [--stripe-index|-i] <start_ost_index>\n"
652 " [--stripe-size|-S] <stripe_size>\n"
653 " [--pool|-p] <pool_name>\n"
654 " [--ost|-o] <ost_indices>\n"
656 " [--non-block|-n]\n"
657 " [--non-direct|-D]\n"
658 " <file|directory>\n"
659 "\tstripe_count: number of OSTs to stripe a file over\n"
660 "\t Using -C instead of -c allows overstriping, which\n"
661 "\t will place more than one stripe per OST if\n"
662 "\t stripe_count is greater than the number of OSTs\n"
663 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
664 "\tstripe_size: number of bytes to store before moving to the next OST\n"
665 "\tpool_name: name of the predefined pool of OSTs\n"
666 "\tost_indices: OSTs to stripe over, in order\n"
667 "\tblock: Block file access during data migration (default)\n"
668 "\tnon-block: Abort migrations if concurrent access is detected\n"
669 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
671 "To move directories between MDTs. This command is deprecated, "
672 "use \"migrate\" instead.\n"
673 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
675 {"ladvise", lfs_ladvise, 0,
676 "Provide servers with advice about access patterns for a file.\n"
677 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
678 " [--background|-b] [--unset|-u]\n\n"
679 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
680 " {[--mode|-m [READ,WRITE]}\n"
682 {"mirror", lfs_mirror, mirror_cmdlist,
683 "lfs commands used to manage files with mirrored components:\n"
684 "lfs mirror create - create a mirrored file or directory\n"
685 "lfs mirror extend - add mirror(s) to an existing file\n"
686 "lfs mirror split - split a mirror from an existing mirrored file\n"
687 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
688 "lfs mirror read - read a mirror content of a mirrored file\n"
689 "lfs mirror write - write to a mirror of a mirrored file\n"
690 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
691 "lfs mirror verify - verify mirrored file(s)\n"},
692 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
693 "usage: getsom [-s] [-b] [-f] <path>\n"
694 "\t-s: Only show the size value of the SOM data for a given file\n"
695 "\t-b: Only show the blocks value of the SOM data for a given file\n"
696 "\t-f: Only show the flags value of the SOM data for a given file\n"},
697 {"heat_get", lfs_heat_get, 0,
698 "To get heat of files.\n"
699 "usage: heat_get <file> ...\n"},
700 {"heat_set", lfs_heat_set, 0,
701 "To set heat flags of files.\n"
702 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
703 "\t--clear|-c: Clear file heat for given files\n"
704 "\t--off|-o: Turn off file heat for given files\n"
705 "\t--on|-O: Turn on file heat for given files\n"},
706 {"pcc", lfs_pcc, pcc_cmdlist,
707 "lfs commands used to interact with PCC features:\n"
708 "lfs pcc attach - attach given files to Persistent Client Cache\n"
709 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
710 "lfs pcc state - display the PCC state for given files\n"
711 "lfs pcc detach - detach given files from Persistent Client Cache\n"
712 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
713 {"help", Parser_help, 0, "help"},
714 {"exit", Parser_quit, 0, "quit"},
715 {"quit", Parser_quit, 0, "quit"},
716 {"--version", Parser_version, 0,
717 "output build version of the utility and exit"},
718 {"--list-commands", lfs_list_commands, 0,
719 "list commands supported by the utility and exit"},
723 static int check_hashtype(const char *hashtype)
725 int type_num = atoi(hashtype);
728 /* numeric hash type */
729 if (hashtype && strlen(hashtype) == 1 &&
730 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
732 /* string hash type */
733 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
734 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
740 static uint32_t check_foreign_type_name(const char *foreign_type_name)
744 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
745 if (!lu_foreign_types[i].lft_name)
747 if (strcmp(foreign_type_name,
748 lu_foreign_types[i].lft_name) == 0)
749 return lu_foreign_types[i].lft_type;
752 return LU_FOREIGN_TYPE_UNKNOWN;
755 static const char *error_loc = "syserror";
758 MIGRATION_NONBLOCK = 0x0001,
759 MIGRATION_MIRROR = 0x0002,
760 MIGRATION_NONDIRECT = 0x0004,
761 MIGRATION_VERBOSE = 0x0008,
765 migrate_open_files(const char *name, __u64 migration_flags,
766 const struct llapi_stripe_param *param,
767 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
774 char parent[PATH_MAX];
775 char volatile_file[PATH_MAX];
781 if (!param && !layout) {
782 error_loc = "layout information";
786 /* search for file directory pathname */
787 if (strlen(name) > sizeof(parent) - 1) {
788 error_loc = "source file name";
792 strncpy(parent, name, sizeof(parent));
793 ptr = strrchr(parent, '/');
795 if (!getcwd(parent, sizeof(parent))) {
796 error_loc = "getcwd";
800 if (ptr == parent) /* leading '/' */
805 /* open file, direct io */
806 /* even if the file is only read, WR mode is nedeed to allow
810 if (!(migration_flags & MIGRATION_NONDIRECT))
812 fd = open(name, rflags);
815 error_loc = "cannot open source file";
819 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
821 error_loc = "cannot get MDT index";
826 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
827 mode_t open_mode = S_IRUSR | S_IWUSR;
829 random_value = random();
830 rc = snprintf(volatile_file, sizeof(volatile_file),
831 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
832 mdt_index, random_value);
833 if (rc >= sizeof(volatile_file)) {
838 /* create, open a volatile file, use caching (ie no directio) */
840 /* Returns -1 and sets errno on error: */
841 fdv = llapi_layout_file_open(volatile_file, open_flags,
846 /* Does the right thing on error: */
847 fdv = llapi_file_open_param(volatile_file, open_flags,
850 } while (fdv < 0 && (rc = fdv) == -EEXIST);
853 error_loc = "cannot create volatile file";
858 * In case the MDT does not support creation of volatile files
859 * we should try to unlink it.
861 (void)unlink(volatile_file);
864 * Not-owner (root?) special case.
865 * Need to set owner/group of volatile file like original.
866 * This will allow to pass related check during layout_swap.
871 error_loc = "cannot stat source file";
875 rc = fstat(fdv, &stv);
878 error_loc = "cannot stat volatile";
882 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
883 rc = fchown(fdv, st.st_uid, st.st_gid);
886 error_loc = "cannot change ownwership of volatile";
905 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
907 struct llapi_layout *layout;
908 size_t buf_size = 4 * 1024 * 1024;
917 layout = llapi_layout_get_by_fd(fd_src, 0);
919 uint64_t stripe_size;
921 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
923 buf_size = stripe_size;
925 llapi_layout_free(layout);
928 /* Use a page-aligned buffer for direct I/O */
929 rc = posix_memalign(&buf, getpagesize(), buf_size);
935 * read new data only if we have written all
936 * previously read data
940 rc = check_file(fd_src);
945 rsize = read(fd_src, buf, buf_size);
959 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
972 /* Try to avoid page cache pollution after migration. */
973 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
974 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
980 static int migrate_copy_timestamps(int fd, int fdv)
984 if (fstat(fd, &st) == 0) {
985 struct timeval tv[2] = {
986 {.tv_sec = st.st_atime},
987 {.tv_sec = st.st_mtime}
990 return futimes(fdv, tv);
996 static int migrate_block(int fd, int fdv)
1003 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1005 error_loc = "cannot get dataversion";
1014 * The grouplock blocks all concurrent accesses to the file.
1015 * It has to be taken after llapi_get_data_version as it would
1018 rc = llapi_group_lock(fd, gid);
1020 error_loc = "cannot get group lock";
1024 rc = migrate_copy_data(fd, fdv, NULL);
1026 error_loc = "data copy failed";
1030 /* Make sure we keep original atime/mtime values */
1031 rc = migrate_copy_timestamps(fd, fdv);
1033 error_loc = "timestamp copy failed";
1039 * for a migration we need to check data version on file did
1042 * Pass in gid=0 since we already own grouplock.
1044 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1045 SWAP_LAYOUTS_CHECK_DV1);
1046 if (rc == -EAGAIN) {
1047 error_loc = "file changed";
1049 } else if (rc < 0) {
1050 error_loc = "cannot swap layout";
1055 rc2 = llapi_group_unlock(fd, gid);
1056 if (rc2 < 0 && rc == 0) {
1057 error_loc = "unlock group lock";
1065 * Internal helper for migrate_copy_data(). Check lease and report error if
1068 * \param[in] fd File descriptor on which to check the lease.
1070 * \retval 0 Migration can keep on going.
1071 * \retval -errno Error occurred, abort migration.
1073 static int check_lease(int fd)
1077 rc = llapi_lease_check(fd);
1079 return 0; /* llapi_check_lease returns > 0 on success. */
1084 static int migrate_nonblock(int fd, int fdv)
1090 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1092 error_loc = "cannot get data version";
1096 rc = migrate_copy_data(fd, fdv, check_lease);
1098 error_loc = "data copy failed";
1102 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1104 error_loc = "cannot get data version";
1110 error_loc = "source file changed";
1114 /* Make sure we keep original atime/mtime values */
1115 rc = migrate_copy_timestamps(fd, fdv);
1117 error_loc = "timestamp copy failed";
1124 static int lfs_component_set(char *fname, int comp_id,
1125 __u32 flags, __u32 neg_flags)
1128 __u32 flags_array[2];
1133 ids[count] = comp_id;
1134 flags_array[count] = flags;
1139 if (neg_flags & LCME_FL_STALE) {
1141 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1146 ids[count] = comp_id;
1147 flags_array[count] = neg_flags | LCME_FL_NEG;
1151 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1153 if (errno == EUCLEAN) {
1156 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1157 progname, comp_id, fname);
1160 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1161 progname, comp_id, fname, flags, neg_flags);
1168 static int lfs_component_del(char *fname, __u32 comp_id,
1169 __u32 flags, __u32 neg_flags)
1173 if (flags && neg_flags)
1176 if (!flags && neg_flags)
1177 flags = neg_flags | LCME_FL_NEG;
1179 if ((flags && comp_id) || (!flags && !comp_id))
1183 if (flags & ~LCME_KNOWN_FLAGS) {
1185 "%s setstripe: unknown flags %#x\n",
1189 } else if (comp_id > LCME_ID_MAX) {
1190 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1195 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1198 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1199 progname, comp_id, fname, strerror(errno));
1203 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1210 rc = llapi_layout_file_comp_add(fname, layout);
1212 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1213 fname, strerror(errno));
1217 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1218 struct llapi_layout *layout)
1226 fd = lstat(fname, &st);
1227 if (fd == 0 && S_ISDIR(st.st_mode))
1228 open_flags = O_DIRECTORY | O_RDONLY;
1230 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1232 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1233 S_ISDIR(st.st_mode) ?
1234 "set default composite layout for" :
1235 "create composite file",
1236 fname, strerror(errno));
1240 static int lfs_migrate(char *name, __u64 migration_flags,
1241 struct llapi_stripe_param *param,
1242 struct llapi_layout *layout)
1244 struct llapi_layout *existing;
1245 uint64_t dom_new, dom_cur;
1250 rc = migrate_open_files(name, migration_flags, param, layout,
1255 rc = llapi_layout_dom_size(layout, &dom_new);
1257 error_loc = "cannot get new layout DoM size";
1260 /* special case for migration to DOM layout*/
1261 existing = llapi_layout_get_by_fd(fd, 0);
1263 error_loc = "cannot get existing layout";
1267 rc = llapi_layout_dom_size(existing, &dom_cur);
1269 error_loc = "cannot get current layout DoM size";
1274 * if file has DoM layout already then migration is possible to
1275 * the new layout with the same DoM component via swap layout,
1276 * if new layout used bigger DOM size, then mirroring is used
1278 if (dom_new > dom_cur) {
1279 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1282 error_loc = "cannot migrate to DOM layout";
1286 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1288 * Blocking mode (forced if servers do not support file lease).
1289 * It is also the default mode, since we cannot distinguish
1290 * between a broken lease and a server that does not support
1291 * atomic swap/close (LU-6785)
1293 rc = migrate_block(fd, fdv);
1297 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1299 error_loc = "cannot get lease";
1303 rc = migrate_nonblock(fd, fdv);
1305 llapi_lease_release(fd);
1310 * Atomically put lease, swap layouts and close.
1311 * for a migration we need to check data version on file did
1314 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1316 error_loc = "cannot swap layout";
1328 fprintf(stderr, "error: %s: %s: %s: %s\n",
1329 progname, name, error_loc, strerror(-rc));
1330 else if (migration_flags & MIGRATION_VERBOSE)
1331 printf("%s\n", name);
1336 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1345 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1349 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1350 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1351 const char *comp_name = comp_flags_table[i].cfn_name;
1353 if (strcmp(name, comp_name) == 0) {
1354 *flags |= comp_flag;
1356 } else if (strncmp(name, "^", 1) == 0 &&
1357 strcmp(name + 1, comp_name) == 0) {
1358 *neg_flags |= comp_flag;
1363 llapi_printf(LLAPI_MSG_ERROR,
1364 "%s: component flag '%s' not supported\n",
1370 if (!*flags && !*neg_flags)
1373 /* don't allow to set and exclude the same flag */
1374 if (*flags & *neg_flags)
1380 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1388 if (strncmp(string, "^", 1) == 0) {
1389 *neg_state = llapi_layout_string_flags(string + 1);
1390 if (*neg_state != 0)
1393 *state = llapi_layout_string_flags(string);
1398 llapi_printf(LLAPI_MSG_ERROR,
1399 "%s: mirrored file state '%s' not supported\n",
1405 * struct mirror_args - Command-line arguments for mirror(s).
1406 * @m_count: Number of mirrors to be created with this layout.
1407 * @m_flags: Mirror level flags, only 'prefer' is supported.
1408 * @m_layout: Mirror layout.
1409 * @m_file: A victim file. Its layout will be split and used as a mirror.
1410 * @m_next: Point to the next node of the list.
1412 * Command-line arguments for mirror(s) will be parsed and stored in
1413 * a linked list that consists of this structure.
1415 struct mirror_args {
1418 struct llapi_layout *m_layout;
1420 struct mirror_args *m_next;
1425 * enum mirror_flags - Flags for extending a mirrored file.
1426 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1427 * in case the victim file(s) contains the same data as the
1428 * original mirrored file.
1429 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1430 * @MF_COMP_ID: specified component id instead of mirror id
1432 * Flags for extending a mirrored file.
1442 * mirror_create_sanity_check() - Check mirror list.
1443 * @list: A linked list that stores the mirror arguments.
1445 * This function does a sanity check on @list for creating
1448 * Return: 0 on success or a negative error code on failure.
1450 static int mirror_create_sanity_check(const char *fname,
1451 struct mirror_args *list)
1454 bool has_m_file = false;
1455 bool has_m_layout = false;
1461 struct llapi_layout *layout;
1463 layout = llapi_layout_get_by_path(fname, 0);
1466 "error: %s: file '%s' couldn't get layout\n",
1471 rc = llapi_layout_sanity(layout, false, true);
1473 llapi_layout_free(layout);
1476 llapi_layout_sanity_perror(rc);
1484 llapi_layout_free(list->m_layout);
1487 llapi_layout_get_by_path(list->m_file, 0);
1488 if (!list->m_layout) {
1490 "error: %s: file '%s' has no layout\n",
1491 progname, list->m_file);
1495 has_m_layout = true;
1496 if (!list->m_layout) {
1497 fprintf(stderr, "error: %s: no mirror layout\n",
1503 rc = llapi_layout_sanity(list->m_layout, false, true);
1505 llapi_layout_sanity_perror(rc);
1509 list = list->m_next;
1512 if (has_m_file && has_m_layout) {
1514 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1522 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1524 __u32 mirror_flags = *(__u32 *)cbdata;
1528 rc = llapi_layout_comp_flags_get(layout, &flags);
1533 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1538 return LLAPI_LAYOUT_ITER_CONT;
1542 * mirror_create() - Create a mirrored file.
1543 * @fname: The file to be created.
1544 * @mirror_list: A linked list that stores the mirror arguments.
1546 * This function creates a mirrored file @fname with the mirror(s)
1547 * from @mirror_list.
1549 * Return: 0 on success or a negative error code on failure.
1551 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1553 struct llapi_layout *layout = NULL;
1554 struct mirror_args *cur_mirror = NULL;
1555 uint16_t mirror_count = 0;
1559 rc = mirror_create_sanity_check(NULL, mirror_list);
1563 cur_mirror = mirror_list;
1564 while (cur_mirror) {
1565 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1567 &cur_mirror->m_flags);
1570 fprintf(stderr, "%s: failed to set mirror flags\n",
1575 for (i = 0; i < cur_mirror->m_count; i++) {
1576 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1580 "error: %s: merge layout failed: %s\n",
1581 progname, strerror(errno));
1585 mirror_count += cur_mirror->m_count;
1586 cur_mirror = cur_mirror->m_next;
1590 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1594 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1597 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1598 progname, strerror(errno));
1602 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1610 llapi_layout_free(layout);
1615 * Compare files and check lease on @fd.
1617 * \retval bytes number of bytes are the same
1619 static ssize_t mirror_file_compare(int fd, int fdv)
1621 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1623 ssize_t bytes_done = 0;
1624 ssize_t bytes_read = 0;
1626 buf = malloc(buflen * 2);
1631 if (!llapi_lease_check(fd)) {
1632 bytes_done = -EBUSY;
1636 bytes_read = read(fd, buf, buflen);
1637 if (bytes_read <= 0)
1640 if (bytes_read != read(fdv, buf + buflen, buflen))
1644 * XXX: should compute the checksum on each buffer and then
1645 * compare checksum to avoid cache collision
1647 if (memcmp(buf, buf + buflen, bytes_read))
1650 bytes_done += bytes_read;
1658 static int mirror_extend_file(const char *fname, const char *victim_file,
1659 enum mirror_flags mirror_flags)
1664 struct stat stbuf_v;
1665 struct ll_ioc_lease *data = NULL;
1668 fd = open(fname, O_RDWR);
1670 error_loc = "open source file";
1675 fdv = open(victim_file, O_RDWR);
1677 error_loc = "open target file";
1682 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1683 error_loc = "stat source or target file";
1688 if (stbuf.st_dev != stbuf_v.st_dev) {
1689 error_loc = "stat source and target file";
1694 /* mirrors should be of the same size */
1695 if (stbuf.st_size != stbuf_v.st_size) {
1696 error_loc = "file sizes don't match";
1701 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1703 error_loc = "cannot get lease";
1707 if (!(mirror_flags & MF_NO_VERIFY)) {
1709 /* mirrors should have the same contents */
1710 ret = mirror_file_compare(fd, fdv);
1711 if (ret != stbuf.st_size) {
1712 error_loc = "file busy or contents don't match";
1713 rc = ret < 0 ? ret : -EINVAL;
1718 /* Get rid of caching pages from clients */
1719 rc = llapi_file_flush(fd);
1721 error_loc = "cannot get data version";
1725 rc = llapi_file_flush(fdv);
1727 error_loc = "cannot get data version";
1731 /* Make sure we keep original atime/mtime values */
1732 rc = migrate_copy_timestamps(fd, fdv);
1734 error_loc = "cannot copy timestamp";
1738 /* Atomically put lease, merge layouts and close. */
1739 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1741 error_loc = "memory allocation";
1744 data->lil_mode = LL_LEASE_UNLCK;
1745 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1746 data->lil_count = 1;
1747 data->lil_ids[0] = fdv;
1748 rc = llapi_lease_set(fd, data);
1750 error_loc = "cannot merge layout";
1752 } else if (rc == 0) {
1754 error_loc = "lost lease lock";
1767 (void) unlink(victim_file);
1769 fprintf(stderr, "error: %s: %s: %s: %s\n",
1770 progname, fname, error_loc, strerror(-rc));
1774 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1775 bool inherit, uint32_t flags)
1777 struct llapi_layout *f_layout = NULL;
1778 struct ll_ioc_lease *data = NULL;
1784 f_layout = llapi_layout_get_by_path(name, 0);
1786 fprintf(stderr, "%s: cannot get layout\n", progname);
1789 rc = llapi_layout_get_last_init_comp(f_layout);
1791 fprintf(stderr, "%s: cannot get the last init comp\n",
1795 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1798 "%s: cannot inherit from the last init comp\n",
1803 llapi_layout_comp_flags_set(m_layout, flags);
1804 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1809 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1811 error_loc = "cannot get lease";
1815 rc = migrate_nonblock(fd, fdv);
1817 llapi_lease_release(fd);
1821 /* Atomically put lease, merge layouts and close. */
1822 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1824 error_loc = "memory allocation";
1827 data->lil_mode = LL_LEASE_UNLCK;
1828 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1829 data->lil_count = 1;
1830 data->lil_ids[0] = fdv;
1831 rc = llapi_lease_set(fd, data);
1833 error_loc = "cannot merge layout";
1835 } else if (rc == 0) {
1837 error_loc = "lost lease lock";
1850 fprintf(stderr, "error: %s: %s: %s: %s\n",
1851 progname, name, error_loc, strerror(-rc));
1855 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1856 enum mirror_flags mirror_flags)
1860 rc = mirror_create_sanity_check(fname, mirror_list);
1864 while (mirror_list) {
1865 if (mirror_list->m_file) {
1866 rc = mirror_extend_file(fname, mirror_list->m_file,
1869 __u32 mirror_count = mirror_list->m_count;
1871 while (mirror_count > 0) {
1872 rc = mirror_extend_layout(fname,
1873 mirror_list->m_layout,
1874 mirror_list->m_inherit,
1875 mirror_list->m_flags);
1885 mirror_list = mirror_list->m_next;
1891 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1896 rc = llapi_layout_mirror_id_get(layout, &id);
1900 if ((__u16)id == *(__u16 *)cbdata)
1901 return LLAPI_LAYOUT_ITER_STOP;
1903 return LLAPI_LAYOUT_ITER_CONT;
1906 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1911 rc = llapi_layout_comp_id_get(layout, &id);
1915 if (id == *(__u32 *)cbdata)
1916 return LLAPI_LAYOUT_ITER_STOP;
1918 return LLAPI_LAYOUT_ITER_CONT;
1921 struct pool_to_id_cbdata {
1926 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1928 char buf[LOV_MAXPOOLNAME + 1];
1929 struct pool_to_id_cbdata *d = (void *)cbdata;
1933 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1936 if (strcmp(d->pool, buf))
1937 return LLAPI_LAYOUT_ITER_CONT;
1939 rc = llapi_layout_mirror_id_get(layout, &id);
1944 return LLAPI_LAYOUT_ITER_STOP;
1947 struct collect_ids_data {
1953 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1955 struct collect_ids_data *cid = cbdata;
1959 rc = llapi_layout_mirror_id_get(layout, &id);
1963 if ((__u16)id != cid->cid_exclude) {
1966 for (i = 0; i < cid->cid_count; i++) {
1967 /* already collected the mirror id */
1968 if (id == cid->cid_ids[i])
1969 return LLAPI_LAYOUT_ITER_CONT;
1971 cid->cid_ids[cid->cid_count] = id;
1975 return LLAPI_LAYOUT_ITER_CONT;
1979 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1980 * @mirror_id: Mirror id to be checked.
1981 * @layout: Mirror component list.
1983 * This function checks if a mirror with specified @mirror_id is the last
1984 * non-stale mirror of a layout @layout.
1986 * Return: true or false.
1989 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1991 __u16 mirror_ids[128] = { 0 };
1992 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1994 .cid_exclude = mirror_id, };
1997 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1999 for (i = 0; i < cid.cid_count; i++) {
2000 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2003 comp_size = llapi_mirror_find_stale(layout, comp_array,
2004 ARRAY_SIZE(comp_array),
2013 static int mirror_split(const char *fname, __u32 id, const char *pool,
2014 enum mirror_flags mflags, const char *victim_file)
2016 struct llapi_layout *layout;
2017 char parent[PATH_MAX];
2018 char victim[PATH_MAX];
2019 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2021 struct ll_ioc_lease *data;
2022 uint16_t mirror_count;
2028 /* check fname contains mirror with mirror_id/comp_id */
2029 layout = llapi_layout_get_by_path(fname, 0);
2032 "error %s: file '%s' couldn't get layout\n",
2037 rc = llapi_layout_sanity(layout, false, true);
2039 llapi_layout_sanity_perror(rc);
2043 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2046 "error %s: file '%s' couldn't get mirror count\n",
2050 if (mirror_count < 2) {
2052 "error %s: file '%s' has %d component, cannot split\n",
2053 progname, fname, mirror_count);
2057 if (mflags & MF_COMP_POOL) {
2058 struct pool_to_id_cbdata data = { .pool = pool };
2060 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
2062 mirror_id = data.id;
2063 } else if (mflags & MF_COMP_ID) {
2064 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2065 mirror_id = mirror_id_of(id);
2067 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2071 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2074 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2075 if (mflags & MF_COMP_POOL) {
2077 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2078 progname, fname, pool);
2080 } else if (mflags & MF_COMP_ID) {
2082 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2083 progname, fname, id);
2087 "error %s: file '%s' does not contain mirror with id %u\n",
2088 progname, fname, id);
2093 fd = open(fname, O_RDWR);
2096 "error %s: open file '%s' failed: %s\n",
2097 progname, fname, strerror(errno));
2101 /* get victim file directory pathname */
2102 if (strlen(fname) > sizeof(parent) - 1) {
2103 fprintf(stderr, "error %s: file name of '%s' too long\n",
2108 strncpy(parent, fname, sizeof(parent));
2109 ptr = strrchr(parent, '/');
2111 if (!getcwd(parent, sizeof(parent))) {
2112 fprintf(stderr, "error %s: getcwd failed: %s\n",
2113 progname, strerror(errno));
2123 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2125 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2131 /* use a temp file to store the splitted layout */
2132 if (mflags & MF_DESTROY) {
2133 if (last_non_stale_mirror(mirror_id, layout)) {
2136 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2141 fdv = llapi_create_volatile_idx(parent, mdt_index,
2142 O_LOV_DELAY_CREATE);
2144 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2146 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2149 /* user specified victim file */
2150 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2155 "error %s: create victim file failed: %s\n",
2156 progname, strerror(errno));
2160 /* get lease lock of fname */
2161 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2164 "error %s: cannot get lease of file '%s': %d\n",
2165 progname, fname, rc);
2169 /* Atomatically put lease, split layouts and close. */
2170 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2176 data->lil_mode = LL_LEASE_UNLCK;
2177 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2178 data->lil_count = 2;
2179 data->lil_ids[0] = fdv;
2180 data->lil_ids[1] = mirror_id;
2181 rc = llapi_lease_set(fd, data);
2183 if (rc == 0) /* lost lease lock */
2186 "error %s: cannot split '%s': %s\n",
2187 progname, fname, strerror(-rc));
2198 llapi_layout_free(layout);
2203 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2204 __u16 *mirror_ids, int ids_nr);
2206 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2207 __u64 migration_flags,
2208 struct llapi_stripe_param *param,
2209 struct llapi_layout *layout)
2211 struct ll_ioc_lease *data = NULL;
2214 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2216 error_loc = "cannot get lease";
2220 /* Atomically put lease, merge layouts, resync and close. */
2221 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2223 error_loc = "memory allocation";
2226 data->lil_mode = LL_LEASE_UNLCK;
2227 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2228 data->lil_count = 1;
2229 data->lil_ids[0] = fdv;
2230 rc = llapi_lease_set(fd, data);
2232 error_loc = "cannot merge layout";
2234 } else if (rc == 0) {
2236 error_loc = "lost lease lock";
2242 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2244 error_loc = "cannot resync file";
2248 /* delete first mirror now */
2249 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2251 error_loc = "cannot delete old layout";
2259 fprintf(stderr, "error: %s: %s: %s: %s\n",
2260 progname, name, error_loc, strerror(-rc));
2261 else if (migration_flags & MIGRATION_VERBOSE)
2262 printf("%s\n", name);
2269 * Parse a string containing an target index list into an array of integers.
2271 * The input string contains a comma delimited list of individual
2272 * indices and ranges, for example "1,2-4,7". Add the indices into the
2273 * \a tgts array and remove duplicates.
2275 * \param[out] tgts array to store indices in
2276 * \param[in] size size of \a tgts array
2277 * \param[in] offset starting index in \a tgts
2278 * \param[in] arg string containing OST index list
2279 * \param[in/out] overstriping index list may contain duplicates
2281 * \retval positive number of indices in \a tgts
2282 * \retval -EINVAL unable to parse \a arg
2284 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2285 unsigned long long *pattern)
2289 int slots = size - offset;
2291 bool overstriped = false;
2297 end_of_loop = false;
2298 while (!end_of_loop) {
2299 int start_index = 0;
2302 char *endptr = NULL;
2306 ptr = strchrnul(arg, ',');
2308 end_of_loop = *ptr == '\0';
2311 start_index = strtol(arg, &endptr, 0);
2312 if (endptr == arg) /* no data at all */
2314 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2317 end_index = start_index;
2318 if (*endptr == '-') {
2319 end_index = strtol(endptr + 1, &endptr, 0);
2320 if (*endptr != '\0')
2322 if (end_index < start_index)
2326 for (i = start_index; i <= end_index && slots > 0; i++) {
2329 /* remove duplicate */
2330 for (j = 0; j < offset; j++) {
2331 if (tgts[j] == i && pattern &&
2332 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2334 else if (tgts[j] == i)
2340 if (j == offset) { /* check complete */
2346 if (slots == 0 && i < end_index)
2354 if (!end_of_loop && ptr)
2357 if (!overstriped && pattern)
2358 *pattern = LLAPI_LAYOUT_DEFAULT;
2360 return rc < 0 ? rc : nr;
2363 struct lfs_setstripe_args {
2364 unsigned long long lsa_comp_end;
2365 unsigned long long lsa_stripe_size;
2366 unsigned long long lsa_extension_size;
2367 long long lsa_stripe_count;
2368 long long lsa_stripe_off;
2369 __u32 lsa_comp_flags;
2370 __u32 lsa_comp_neg_flags;
2371 unsigned long long lsa_pattern;
2372 unsigned int lsa_mirror_count;
2374 bool lsa_first_comp;
2375 bool lsa_extension_comp;
2377 char *lsa_pool_name;
2380 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2382 unsigned int mirror_count = lsa->lsa_mirror_count;
2383 bool first_comp = lsa->lsa_first_comp;
2385 memset(lsa, 0, sizeof(*lsa));
2387 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2388 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2389 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2390 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2391 lsa->lsa_pool_name = NULL;
2393 lsa->lsa_mirror_count = mirror_count;
2394 lsa->lsa_first_comp = first_comp;
2398 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2399 * @lsa: Stripe options to be initialized and inherited.
2401 * This function initializes stripe options in @lsa and inherit
2402 * stripe_size, stripe_count and OST pool_name options.
2406 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2408 unsigned long long stripe_size;
2409 long long stripe_count;
2410 char *pool_name = NULL;
2412 stripe_size = lsa->lsa_stripe_size;
2413 stripe_count = lsa->lsa_stripe_count;
2414 pool_name = lsa->lsa_pool_name;
2416 setstripe_args_init(lsa);
2418 lsa->lsa_stripe_size = stripe_size;
2419 lsa->lsa_stripe_count = stripe_count;
2420 lsa->lsa_pool_name = pool_name;
2423 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2425 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2426 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2427 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2428 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2429 lsa->lsa_comp_end != 0);
2433 * comp_args_to_layout() - Create or extend a composite layout.
2434 * @composite: Pointer to the composite layout.
2435 * @lsa: Stripe options for the new component.
2437 * This function creates or extends a composite layout by adding a new
2438 * component with stripe options from @lsa.
2440 * Return: 0 on success or an error code on failure.
2442 static int comp_args_to_layout(struct llapi_layout **composite,
2443 struct lfs_setstripe_args *lsa,
2446 struct llapi_layout *layout = *composite;
2447 uint64_t prev_end = 0;
2453 layout = llapi_layout_alloc();
2455 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2460 *composite = layout;
2461 lsa->lsa_first_comp = true;
2466 * Get current component extent, current component
2467 * must be the tail component.
2469 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2471 fprintf(stderr, "Get comp extent failed. %s\n",
2476 if (lsa->lsa_first_comp) {
2478 rc = llapi_layout_add_first_comp(layout);
2480 rc = llapi_layout_comp_add(layout);
2483 fprintf(stderr, "Add component failed. %s\n",
2489 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2491 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2492 lsa->lsa_comp_flags, strerror(errno));
2497 uint64_t comp_end = lsa->lsa_comp_end;
2500 * The extendable component is 0-length, so it can be removed
2501 * if there is insufficient space to extend it.
2503 if (lsa->lsa_extension_comp)
2504 comp_end = prev_end;
2506 rc = llapi_layout_comp_extent_set(layout, prev_end,
2509 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2510 prev_end, comp_end, strerror(errno));
2514 /* reset lsa_first_comp */
2515 lsa->lsa_first_comp = false;
2517 /* Data-on-MDT component setting */
2518 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2520 * In case of Data-on-MDT patterns the only extra option
2521 * applicable is stripe size option.
2523 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2525 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2526 lsa->lsa_stripe_count);
2530 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2532 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2533 lsa->lsa_stripe_size);
2537 if (lsa->lsa_nr_tgts != 0) {
2539 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2544 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2546 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2547 lsa->lsa_stripe_off);
2551 if (lsa->lsa_pool_name != 0) {
2553 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2554 lsa->lsa_pool_name);
2559 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2561 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2566 /* Data-on-MDT component has always single stripe up to end */
2567 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2568 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2569 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2571 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2578 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2579 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2581 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2582 rc = llapi_layout_extension_size_set(layout, size);
2584 rc = llapi_layout_stripe_size_set(layout, size);
2587 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2588 size, strerror(errno));
2592 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2594 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2595 lsa->lsa_stripe_count, strerror(errno));
2599 if (lsa->lsa_pool_name) {
2600 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2602 fprintf(stderr, "Set pool name: %s failed. %s\n",
2603 lsa->lsa_pool_name, strerror(errno));
2607 rc = llapi_layout_pool_name_set(layout, "");
2609 fprintf(stderr, "Clear pool name failed: %s\n",
2615 if (lsa->lsa_nr_tgts > 0) {
2616 if (lsa->lsa_stripe_count > 0 &&
2617 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2618 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2619 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2620 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2621 lsa->lsa_stripe_count,
2626 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2627 rc = llapi_layout_ost_index_set(layout, i,
2632 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2633 lsa->lsa_stripe_off != -1) {
2634 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2637 fprintf(stderr, "Set ost index %d failed. %s\n",
2638 i, strerror(errno));
2642 /* Create the second, virtual component of extension space */
2643 if (lsa->lsa_extension_comp) {
2644 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2645 lsa->lsa_extension_comp = false;
2652 static int build_component(struct llapi_layout **layout,
2653 struct lfs_setstripe_args *lsa, bool set_extent)
2657 rc = comp_args_to_layout(layout, lsa, set_extent);
2661 if (lsa->lsa_mirror_count > 0) {
2662 rc = llapi_layout_mirror_count_set(*layout,
2663 lsa->lsa_mirror_count);
2667 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2670 lsa->lsa_mirror_count = 0;
2676 static int build_prev_component(struct llapi_layout **layout,
2677 struct lfs_setstripe_args *prev,
2678 struct lfs_setstripe_args *lsa,
2681 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2684 if (prev->lsa_stripe_size) {
2686 prev->lsa_comp_end = lsa->lsa_comp_end;
2687 prev->lsa_extension_size = lsa->lsa_extension_size;
2688 prev->lsa_extension_comp = true;
2691 rc = build_component(layout, prev, true);
2697 * Copy lsa to previous lsa;
2698 * if this is an extension component, make the previous invalid;
2701 prev->lsa_stripe_size = 0;
2708 static int build_layout_from_yaml_node(struct cYAML *node,
2709 struct llapi_layout **layout,
2710 struct lfs_setstripe_args *lsa,
2711 struct lfs_setstripe_args *prevp)
2713 struct lfs_setstripe_args prev = { 0 };
2714 __u32 *osts = lsa->lsa_tgts;
2722 string = node->cy_string;
2724 if (node->cy_type == CYAML_TYPE_OBJECT) {
2725 /* go deep to sub blocks */
2726 if (string && !strncmp(string, "component", 9) &&
2727 strncmp(string, "component0", 10) &&
2728 strncmp(string, "components", 10)) {
2729 rc = build_prev_component(layout, prevp, lsa,
2734 /* initialize lsa. */
2735 setstripe_args_init(lsa);
2736 lsa->lsa_first_comp = false;
2737 lsa->lsa_tgts = osts;
2740 rc = build_layout_from_yaml_node(node->cy_child, layout,
2745 if (!node->cy_string)
2748 /* skip leading lmm_ if present, to simplify parsing */
2749 if (strncmp(string, "lmm_", 4) == 0)
2752 if (node->cy_type == CYAML_TYPE_STRING) {
2753 if (!strcmp(string, "lcme_extent.e_end")) {
2754 if (!strcmp(node->cy_valuestring, "EOF") ||
2755 !strcmp(node->cy_valuestring, "eof"))
2756 lsa->lsa_comp_end = LUSTRE_EOF;
2757 } else if (!strcmp(string, "pool")) {
2758 lsa->lsa_pool_name = node->cy_valuestring;
2759 } else if (!strcmp(string, "pattern")) {
2760 if (!strcmp(node->cy_valuestring, "mdt"))
2761 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2762 if (!strcmp(node->cy_valuestring,
2763 "raid0,overstriped"))
2765 LLAPI_LAYOUT_OVERSTRIPING;
2766 } else if (!strcmp(string, "lcme_flags")) {
2767 rc = comp_str2flags(node->cy_valuestring,
2768 &lsa->lsa_comp_flags,
2769 &lsa->lsa_comp_neg_flags);
2773 * Only template flags have meaning in
2774 * the layout for a new file
2776 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2778 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2779 if (!strcmp(string, "lcm_mirror_count")) {
2780 lsa->lsa_mirror_count = node->cy_valueint;
2781 } else if (!strcmp(string, "lcme_extent.e_start")) {
2782 if (node->cy_valueint == 0)
2783 lsa->lsa_first_comp = true;
2784 } else if (!strcmp(string, "lcme_extent.e_end")) {
2785 if (node->cy_valueint == -1)
2786 lsa->lsa_comp_end = LUSTRE_EOF;
2788 lsa->lsa_comp_end = node->cy_valueint;
2789 } else if (!strcmp(string, "stripe_count")) {
2790 lsa->lsa_stripe_count = node->cy_valueint;
2791 } else if (!strcmp(string, "stripe_size")) {
2792 lsa->lsa_stripe_size = node->cy_valueint;
2793 } else if (!strcmp(string, "extension_size")) {
2794 lsa->lsa_extension_size = node->cy_valueint;
2795 lsa->lsa_extension_comp = true;
2796 } else if (!strcmp(string, "stripe_offset")) {
2797 lsa->lsa_stripe_off = node->cy_valueint;
2798 } else if (!strcmp(string, "l_ost_idx")) {
2799 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2804 node = node->cy_next;
2807 if (prevp == &prev) {
2808 rc = build_prev_component(layout, prevp, lsa, true);
2812 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2813 rc = build_component(layout, lsa, *layout != NULL);
2819 static int lfs_comp_create_from_yaml(char *template,
2820 struct llapi_layout **layout,
2821 struct lfs_setstripe_args *lsa,
2824 struct cYAML *tree = NULL, *err_rc = NULL;
2827 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2829 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2830 progname, template);
2831 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2832 "can't parse", &err_rc);
2833 cYAML_print_tree2file(stderr, err_rc);
2834 cYAML_free_tree(err_rc);
2839 /* initialize lsa for plain file */
2840 setstripe_args_init(lsa);
2841 lsa->lsa_tgts = osts;
2843 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2845 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2846 progname, template);
2849 /* clean clean lsa */
2850 setstripe_args_init(lsa);
2854 cYAML_free_tree(tree);
2859 * Get the extension size from the next (SEL) component and extend the
2860 * current component on it. The start of the next component is to be
2863 * \param[in] layout the current layout
2864 * \param[in] start the start of the current component
2865 * \param[in,out] end the end of the current component
2866 * \param[in] offset the offset to adjust the end position to instead of
2869 * \retval 0 - extended successfully
2870 * \retval < 0 - error
2872 static int layout_extend_comp(struct llapi_layout *layout,
2873 uint64_t start, uint64_t *end,
2876 uint64_t size, next_start, next_end;
2879 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2882 "%s setstripe: cannot move component cursor: %s\n",
2883 progname, strerror(errno));
2888 * Even if the @size will not be used below, this will fail if
2889 * this is not a SEL component - a good confirmation we are
2890 * working on right components.
2892 rc = llapi_layout_extension_size_get(layout, &size);
2895 "%s setstripe: cannot get component ext size: %s\n",
2896 progname, strerror(errno));
2900 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2902 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2903 progname, strerror(errno));
2907 next_start += offset ?: size;
2908 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2910 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2911 progname, strerror(errno));
2915 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2918 "%s setstripe: cannot move component cursor: %s\n",
2919 progname, strerror(errno));
2923 *end += offset ?: size;
2924 rc = llapi_layout_comp_extent_set(layout, start, *end);
2926 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2927 progname, strerror(errno));
2935 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2936 * end of the last component in the existing file, and adjust the
2937 * first extent start of the components to be added accordingly.
2939 * In the create mode, we need to check if the first component is an extendable
2940 * SEL component and extend its length to the extension size (first component
2941 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2943 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2946 struct llapi_layout *head;
2947 uint64_t start = 0, prev_end = 0;
2951 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
2956 head = llapi_layout_get_by_path(fname, 0);
2959 "%s setstripe: cannot read layout from '%s': %s\n",
2960 progname, fname, strerror(errno));
2962 } else if (errno == ENODATA) {
2964 * file without LOVEA, this component-add will be turned
2965 * into a component-create.
2967 llapi_layout_free(head);
2971 * the new layout will be added to an empty one, it
2972 * still needs to be adjusted below
2976 } else if (!llapi_layout_is_composite(head)) {
2978 "%s setstripe: '%s' not a composite file\n",
2980 llapi_layout_free(head);
2984 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2987 "%s setstripe: cannot get prev extent: %s\n",
2988 progname, strerror(errno));
2989 llapi_layout_free(head);
2993 llapi_layout_free(head);
2997 /* Make sure we use the first component of the layout to be added. */
2998 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3001 "%s setstripe: cannot move component cursor: %s\n",
3002 progname, strerror(errno));
3006 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3008 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3009 progname, strerror(errno));
3013 if (start == 0 && end == 0) {
3014 rc = layout_extend_comp(layout, start, &end,
3015 comp_add ? prev_end : 0);
3020 if (start > prev_end || end < prev_end) {
3022 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3023 progname, start, end, prev_end);
3027 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3030 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3031 progname, prev_end, end, strerror(errno));
3038 static int mirror_adjust_first_extents(struct mirror_args *list)
3046 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3049 list = list->m_next;
3055 static inline bool arg_is_eof(char *arg)
3057 return !strncmp(arg, "-1", strlen("-1")) ||
3058 !strncmp(arg, "EOF", strlen("EOF")) ||
3059 !strncmp(arg, "eof", strlen("eof"));
3063 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3065 * Return: Valid mirror_args pointer on success and
3066 * NULL if memory allocation fails.
3068 static struct mirror_args *lfs_mirror_alloc(void)
3070 struct mirror_args *mirror = NULL;
3073 mirror = calloc(1, sizeof(*mirror));
3075 mirror->m_inherit = false;
3086 * lfs_mirror_free() - Free memory allocated for a mirror argument
3088 * @mirror: Previously allocated mirror argument structure by
3089 * lfs_mirror_alloc().
3091 * Free memory allocated for @mirror.
3095 static void lfs_mirror_free(struct mirror_args *mirror)
3097 if (mirror->m_layout)
3098 llapi_layout_free(mirror->m_layout);
3103 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3104 * @mirror_list: Previously allocated mirror list.
3106 * Free memory allocated for @mirror_list.
3110 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3112 struct mirror_args *next_mirror = NULL;
3114 while (mirror_list) {
3115 next_mirror = mirror_list->m_next;
3116 lfs_mirror_free(mirror_list);
3117 mirror_list = next_mirror;
3129 LFS_COMP_NO_VERIFY_OPT,
3131 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3133 LFS_MIRROR_STATE_OPT,
3135 LFS_MIRROR_INDEX_OPT,
3136 LFS_LAYOUT_FOREIGN_OPT,
3142 static int lfs_setstripe_internal(int argc, char **argv,
3143 enum setstripe_origin opc)
3145 struct lfs_setstripe_args lsa = { 0 };
3146 struct llapi_stripe_param *param = NULL;
3147 struct find_param migrate_mdt_param = {
3157 unsigned long long size_units = 1;
3158 bool migrate_mode = false;
3159 bool migrate_mdt_mode = false;
3160 bool setstripe_mode = false;
3161 bool migration_block = false;
3162 __u64 migration_flags = 0;
3163 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3164 int comp_del = 0, comp_set = 0;
3167 struct llapi_layout *layout = NULL;
3168 struct llapi_layout **lpp = &layout;
3169 bool mirror_mode = false;
3170 bool has_m_file = false;
3171 __u32 mirror_count = 0;
3172 enum mirror_flags mirror_flags = 0;
3173 struct mirror_args *mirror_list = NULL;
3174 struct mirror_args *new_mirror = NULL;
3175 struct mirror_args *last_mirror = NULL;
3176 __u16 mirror_id = 0;
3178 bool from_yaml = false;
3179 bool from_copy = false;
3180 char *template = NULL;
3181 bool foreign_mode = false;
3183 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3184 char *mode_opt = NULL;
3185 mode_t previous_umask = 0;
3186 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3188 struct option long_opts[] = {
3189 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3190 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3191 /* --block is only valid in migrate mode */
3192 { .val = 'b', .name = "block", .has_arg = no_argument },
3193 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3194 { .val = LFS_COMP_ADD_OPT,
3195 .name = "comp-add", .has_arg = no_argument },
3196 { .val = LFS_COMP_ADD_OPT,
3197 .name = "component-add", .has_arg = no_argument },
3198 { .val = LFS_COMP_DEL_OPT,
3199 .name = "comp-del", .has_arg = no_argument },
3200 { .val = LFS_COMP_DEL_OPT,
3201 .name = "component-del", .has_arg = no_argument },
3202 { .val = LFS_COMP_FLAGS_OPT,
3203 .name = "comp-flags", .has_arg = required_argument },
3204 { .val = LFS_COMP_FLAGS_OPT,
3205 .name = "component-flags",
3206 .has_arg = required_argument },
3207 { .val = LFS_COMP_SET_OPT,
3208 .name = "comp-set", .has_arg = no_argument },
3209 { .val = LFS_COMP_SET_OPT,
3210 .name = "component-set",
3211 .has_arg = no_argument},
3212 { .val = LFS_COMP_NO_VERIFY_OPT,
3213 .name = "no-verify", .has_arg = no_argument},
3214 { .val = LFS_LAYOUT_FLAGS_OPT,
3215 .name = "flags", .has_arg = required_argument},
3216 { .val = LFS_LAYOUT_FOREIGN_OPT,
3217 .name = "foreign", .has_arg = optional_argument},
3218 { .val = LFS_MIRROR_ID_OPT,
3219 .name = "mirror-id", .has_arg = required_argument},
3220 { .val = LFS_MODE_OPT,
3221 .name = "mode", .has_arg = required_argument},
3222 { .val = LFS_LAYOUT_COPY,
3223 .name = "copy", .has_arg = required_argument},
3224 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3225 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3226 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3227 { .val = 'C', .name = "overstripe-count",
3228 .has_arg = required_argument},
3229 { .val = 'd', .name = "delete", .has_arg = no_argument},
3230 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3231 /* --non-direct is only valid in migrate mode */
3232 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3233 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3234 { .val = 'E', .name = "component-end",
3235 .has_arg = required_argument},
3236 { .val = 'f', .name = "file", .has_arg = required_argument },
3237 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3238 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3239 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3240 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3241 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3242 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3243 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3244 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3245 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3246 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3247 { .val = 'L', .name = "layout", .has_arg = required_argument },
3248 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3249 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3250 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3251 /* --non-block is only valid in migrate mode */
3252 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3253 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3254 { .val = 'o', .name = "ost", .has_arg = required_argument },
3255 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3256 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3257 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3259 { .val = 'p', .name = "pool", .has_arg = required_argument },
3260 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3261 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3262 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3263 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3264 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3265 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3266 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3267 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3268 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3269 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3270 /* --verbose is only valid in migrate mode */
3271 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3272 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3273 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3274 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3275 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3278 setstripe_args_init(&lsa);
3280 migrate_mode = (opc == SO_MIGRATE);
3281 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3282 setstripe_mode = (opc == SO_SETSTRIPE);
3283 if (opc == SO_MIRROR_DELETE) {
3285 mirror_flags = MF_DESTROY;
3288 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3290 while ((c = getopt_long(argc, argv,
3291 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3292 long_opts, NULL)) >= 0) {
3298 case LFS_COMP_ADD_OPT:
3301 case LFS_COMP_DEL_OPT:
3304 case LFS_COMP_FLAGS_OPT:
3305 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3306 &lsa.lsa_comp_neg_flags);
3309 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3311 "%s: inverted flags are not supported\n",
3316 case LFS_COMP_SET_OPT:
3319 case LFS_COMP_NO_VERIFY_OPT:
3320 mirror_flags |= MF_NO_VERIFY;
3322 case LFS_MIRROR_ID_OPT:
3323 mirror_id = strtoul(optarg, &end, 0);
3324 if (*end != '\0' || mirror_id == 0) {
3326 "%s %s: invalid mirror ID '%s'\n",
3327 progname, argv[0], optarg);
3331 case LFS_LAYOUT_FLAGS_OPT: {
3334 /* check for numeric flags (foreign and mirror cases) */
3335 if (setstripe_mode && !mirror_mode && !last_mirror) {
3336 flags = strtoul(optarg, &end, 16);
3339 "%s %s: bad flags '%s'\n",
3340 progname, argv[0], optarg);
3346 if (!mirror_mode || !last_mirror) {
3348 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3353 result = comp_str2flags(optarg, &last_mirror->m_flags,
3360 "%s: inverted flags are not supported\n",
3365 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3367 "%s: unsupported mirror flags: %s\n",
3374 case LFS_LAYOUT_FOREIGN_OPT:
3376 /* check pure numeric */
3377 type = strtoul(optarg, &end, 0);
3380 type = check_foreign_type_name(optarg);
3381 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3383 "%s %s: unrecognized foreign type '%s'\n",
3390 foreign_mode = true;
3395 mode = strtoul(mode_opt, &end, 8);
3398 "%s %s: bad mode '%s'\n",
3399 progname, argv[0], mode_opt);
3402 previous_umask = umask(0);
3405 case LFS_LAYOUT_COPY:
3410 if (!migrate_mode) {
3412 "%s %s: -b|--block valid only for migrate command\n",
3416 migration_block = true;
3419 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3421 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3425 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3428 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3431 "%s %s: invalid stripe count '%s'\n",
3432 progname, argv[0], optarg);
3436 if (lsa.lsa_stripe_count == -1)
3437 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3440 /* delete the default striping pattern */
3442 if (opc == SO_MIRROR_SPLIT) {
3445 "%s %s: -d cannot used with -f\n",
3449 mirror_flags |= MF_DESTROY;
3453 if (!migrate_mode) {
3455 "%s %s: -D|--non-direct is valid only for migrate command\n",
3459 migration_flags |= MIGRATION_NONDIRECT;
3462 if (lsa.lsa_comp_end != 0) {
3463 result = comp_args_to_layout(lpp, &lsa, true);
3465 fprintf(stderr, "%s: invalid layout\n",
3470 setstripe_args_init_inherit(&lsa);
3473 if (arg_is_eof(optarg)) {
3474 lsa.lsa_comp_end = LUSTRE_EOF;
3476 result = llapi_parse_size(optarg,
3481 "%s %s: invalid component end '%s'\n",
3482 progname, argv[0], optarg);
3488 if (!migrate_mode) {
3490 "--mdt-hash is valid only for migrate command\n");
3494 lsa.lsa_pattern = check_hashtype(optarg);
3495 if (lsa.lsa_pattern == 0) {
3497 "%s %s: bad stripe hash type '%s'\n",
3498 progname, argv[0], optarg);
3503 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3506 "%s %s: invalid stripe offset '%s'\n",
3507 progname, argv[0], optarg);
3510 if (lsa.lsa_stripe_off == -1)
3511 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3514 comp_id = strtoul(optarg, &end, 0);
3515 if (*end != '\0' || comp_id == 0 ||
3516 comp_id > LCME_ID_MAX) {
3518 "%s %s: invalid component ID '%s'\n",
3519 progname, argv[0], optarg);
3524 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3526 "error: %s: invalid option: %s\n",
3527 progname, argv[optopt + 1]);
3530 if (opc == SO_MIRROR_EXTEND) {
3533 "error: %s: '-N' must exist in front of '%s'\n",
3534 progname, argv[optopt + 1]);
3537 last_mirror->m_file = optarg;
3538 last_mirror->m_count = 1;
3542 mirror_list = lfs_mirror_alloc();
3543 mirror_list->m_file = optarg;
3548 if (strcmp(argv[optind - 1], "mdt") == 0) {
3549 /* Can be only the first component */
3553 "error: 'mdt' layout can be only the first one\n");
3556 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3559 "error: 'mdt' layout size is too big\n");
3562 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3563 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3566 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3572 if (!migrate_mode) {
3574 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3578 migrate_mdt_mode = true;
3579 lsa.lsa_nr_tgts = parse_targets(tgts,
3580 sizeof(tgts) / sizeof(__u32),
3581 lsa.lsa_nr_tgts, optarg, NULL);
3582 if (lsa.lsa_nr_tgts < 0) {
3584 "%s %s: invalid MDT target(s) '%s'\n",
3585 progname, argv[0], optarg);
3589 lsa.lsa_tgts = tgts;
3590 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3591 lsa.lsa_stripe_off = tgts[0];
3594 if (!migrate_mode) {
3596 "%s %s: -n|--non-block valid only for migrate command\n",
3600 migration_flags |= MIGRATION_NONBLOCK;
3603 if (opc == SO_SETSTRIPE) {
3604 opc = SO_MIRROR_CREATE;
3609 mirror_count = strtoul(optarg, &end, 0);
3610 if (*end != '\0' || mirror_count == 0) {
3612 "error: %s: bad mirror count: %s\n",
3619 new_mirror = lfs_mirror_alloc();
3620 new_mirror->m_count = mirror_count;
3623 mirror_list = new_mirror;
3626 /* wrap up last mirror */
3627 if (!setstripe_args_specified(&lsa))
3628 last_mirror->m_inherit = true;
3629 if (lsa.lsa_comp_end == 0)
3630 lsa.lsa_comp_end = LUSTRE_EOF;
3632 result = comp_args_to_layout(lpp, &lsa, true);
3634 lfs_mirror_free(new_mirror);
3638 setstripe_args_init_inherit(&lsa);
3640 last_mirror->m_next = new_mirror;
3643 last_mirror = new_mirror;
3644 lpp = &last_mirror->m_layout;
3647 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3648 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3650 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3652 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3654 "%s %s: -o|--ost incompatible with DoM layout\n",
3659 * -o allows overstriping, and must note it because
3660 * parse_targets is shared with MDT striping, which
3661 * does not allow duplicates
3663 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3664 lsa.lsa_nr_tgts = parse_targets(tgts,
3665 sizeof(tgts) / sizeof(__u32),
3666 lsa.lsa_nr_tgts, optarg,
3668 if (lsa.lsa_nr_tgts < 0) {
3670 "%s %s: invalid OST target(s) '%s'\n",
3671 progname, argv[0], optarg);
3675 lsa.lsa_tgts = tgts;
3676 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3677 lsa.lsa_stripe_off = tgts[0];
3682 lsa.lsa_pool_name = optarg;
3684 if (strlen(lsa.lsa_pool_name) == 0 ||
3685 strncmp(lsa.lsa_pool_name, "none",
3686 LOV_MAXPOOLNAME) == 0)
3687 lsa.lsa_pool_name = NULL;
3690 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3694 "%s %s: invalid stripe size '%s'\n",
3695 progname, argv[0], optarg);
3700 if (!migrate_mode) {
3702 "%s %s: -v|--verbose valid only for migrate command\n",
3706 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3707 migration_flags = MIGRATION_VERBOSE;
3717 result = llapi_parse_size(optarg,
3718 &lsa.lsa_extension_size,
3722 "%s %s: invalid extension size '%s'\n",
3723 progname, argv[0], optarg);
3727 lsa.lsa_extension_comp = true;
3730 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3731 progname, argv[0], argv[optind - 1]);
3736 fname = argv[optind];
3738 if (optind == argc) {
3739 fprintf(stderr, "%s %s: FILE must be specified\n",
3744 /* lfs migrate $filename should keep the file's layout by default */
3745 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3749 if (xattr && !foreign_mode) {
3751 * only print a warning as this is harmless and will be ignored
3754 "%s %s: xattr has been specified for non-foreign layout\n",
3756 } else if (foreign_mode && !xattr) {
3758 "%s %s: xattr must be provided in foreign mode\n",
3763 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3764 comp_set || comp_id || delete || from_copy ||
3765 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3768 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3773 if (mirror_mode && mirror_count == 0) {
3775 "error: %s: --mirror-count|-N option is required\n",
3782 if (!setstripe_args_specified(&lsa))
3783 last_mirror->m_inherit = true;
3784 if (lsa.lsa_comp_end == 0)
3785 lsa.lsa_comp_end = LUSTRE_EOF;
3788 if (lsa.lsa_comp_end != 0) {
3789 result = comp_args_to_layout(lpp, &lsa, true);
3791 fprintf(stderr, "error: %s: invalid layout\n",
3798 if (mirror_flags & MF_NO_VERIFY) {
3799 if (opc != SO_MIRROR_EXTEND) {
3801 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3805 } else if (!has_m_file) {
3807 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3814 if (comp_set && !comp_id) {
3816 "%s %s: --component-set doesn't have component-id set\n",
3821 if ((delete + comp_set + comp_del + comp_add) > 1) {
3823 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3828 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3829 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3831 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3836 if ((comp_set || comp_del) &&
3837 (setstripe_args_specified(&lsa) || layout != NULL)) {
3839 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3844 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3846 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3851 if (comp_add || comp_del) {
3854 result = lstat(fname, &st);
3855 if (result == 0 && S_ISDIR(st.st_mode)) {
3857 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3864 "error: %s: can't use --component-add or --component-del for mirror operation\n",
3873 "%s %s: option -E must be specified with --component-add\n",
3879 if (from_yaml && from_copy) {
3881 "%s: can't specify --yaml and --copy together\n",
3886 if ((from_yaml || from_copy) &&
3887 (setstripe_args_specified(&lsa) || layout != NULL)) {
3889 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
3894 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3896 "%s %s: options --non-block and --block are mutually exclusive\n",
3901 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3902 opc != SO_MIRROR_DELETE && comp_id != 0) {
3904 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3909 if (migrate_mdt_mode) {
3910 struct lmv_user_md *lmu;
3912 /* initialize migrate mdt parameters */
3913 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3914 LMV_USER_MAGIC_SPECIFIC));
3917 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3918 progname, argv[0], strerror(ENOMEM));
3922 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3923 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3924 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3926 "%s %s: migrate should specify MDT index\n",
3931 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3932 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3933 lmu->lum_hash_type = lsa.lsa_pattern;
3935 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
3936 if (lsa.lsa_pool_name) {
3937 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3938 sizeof(lmu->lum_pool_name) - 1);
3939 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3941 if (lsa.lsa_nr_tgts > 1) {
3944 if (lsa.lsa_stripe_count > 0 &&
3945 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3946 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3948 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3949 progname, lsa.lsa_stripe_count,
3955 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3956 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3957 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3958 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3960 lmu->lum_magic = LMV_USER_MAGIC;
3963 migrate_mdt_param.fp_lmv_md = lmu;
3964 migrate_mdt_param.fp_migrate = 1;
3965 } else if (!layout) {
3966 /* initialize stripe parameters */
3967 param = calloc(1, offsetof(typeof(*param),
3968 lsp_osts[lsa.lsa_nr_tgts]));
3971 "%s %s: cannot allocate memory for parameters: %s\n",
3972 progname, argv[0], strerror(ENOMEM));
3977 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3978 param->lsp_stripe_size = lsa.lsa_stripe_size;
3979 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3980 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3981 param->lsp_stripe_count = -1;
3983 param->lsp_stripe_count = lsa.lsa_stripe_count;
3985 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3986 param->lsp_stripe_offset = -1;
3988 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3989 param->lsp_stripe_pattern =
3990 llapi_pattern_to_lov(lsa.lsa_pattern);
3991 if (param->lsp_stripe_pattern == EINVAL) {
3992 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3997 param->lsp_pool = lsa.lsa_pool_name;
3998 param->lsp_is_specific = false;
3999 if (lsa.lsa_nr_tgts > 0) {
4000 if (lsa.lsa_stripe_count > 0 &&
4001 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4002 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4003 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4005 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4006 argv[0], lsa.lsa_stripe_count,
4012 param->lsp_is_specific = true;
4013 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4014 memcpy(param->lsp_osts, tgts,
4015 sizeof(*tgts) * lsa.lsa_nr_tgts);
4020 /* generate a layout from a YAML template */
4021 result = lfs_comp_create_from_yaml(template, &layout,
4025 "error: %s: can't create composite layout from template file %s\n",
4031 if (layout != NULL || mirror_list != NULL) {
4033 result = mirror_adjust_first_extents(mirror_list);
4035 result = layout_adjust_first_extent(fname, layout,
4037 if (result == -ENODATA)
4039 else if (result != 0) {
4040 fprintf(stderr, "error: %s: invalid layout\n",
4046 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4048 layout = llapi_layout_get_by_path(template ?: fname, 0);
4051 "%s: can't create composite layout from file %s: %s\n",
4052 progname, template ?: fname,
4059 if (migrate_mdt_mode) {
4060 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4061 } else if (migrate_mode) {
4062 result = lfs_migrate(fname, migration_flags, param,
4064 } else if (comp_set != 0) {
4065 result = lfs_component_set(fname, comp_id,
4067 lsa.lsa_comp_neg_flags);
4068 } else if (comp_del != 0) {
4069 result = lfs_component_del(fname, comp_id,
4071 lsa.lsa_comp_neg_flags);
4072 } else if (comp_add != 0) {
4073 result = lfs_component_add(fname, layout);
4074 } else if (opc == SO_MIRROR_CREATE) {
4075 result = mirror_create(fname, mirror_list);
4076 } else if (opc == SO_MIRROR_EXTEND) {
4077 result = mirror_extend(fname, mirror_list,
4079 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4080 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4082 "%s: no mirror specified to delete from '%s'\n",
4086 if (lsa.lsa_pool_name)
4087 mirror_flags |= MF_COMP_POOL;
4088 else if (mirror_id != 0)
4089 comp_id = mirror_id;
4091 mirror_flags |= MF_COMP_ID;
4092 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4094 has_m_file ? mirror_list->m_file :
4096 } else if (layout) {
4097 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4103 } else if (foreign_mode) {
4104 result = llapi_file_create_foreign(fname, mode, type,
4111 result = llapi_file_open_param(fname,
4120 /* Save the first error encountered. */
4128 umask(previous_umask);
4131 free(migrate_mdt_param.fp_lmv_md);
4132 llapi_layout_free(layout);
4133 lfs_mirror_list_free(mirror_list);
4138 llapi_layout_free(layout);
4139 lfs_mirror_list_free(mirror_list);
4143 static int lfs_poollist(int argc, char **argv)
4148 return llapi_poollist(argv[1]);
4151 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4152 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4157 char *endptr = "AD";
4162 else if (str[0] == '-')
4168 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4169 long long val = strtoll(timebuf, &endptr, 0);
4174 unit *= 52; /* 52 weeks + 1 day below */
4175 case 'w': /* fallthrough */
4177 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4178 param->fp_time_margin *= (1 + unit / 52);
4179 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4180 case '\0': /* days are default unit if none used */
4181 case 'd': /* fallthrough */
4183 case 'h': /* fallthrough */
4185 case 'm': /* fallthrough */
4187 case 's': /* fallthrough */
4189 /* don't need to multiply by 1 for seconds */
4192 "%s find: bad time string '%s': %s\n",
4193 progname, timebuf, strerror(EINVAL));
4197 if (param->fp_time_margin == 0 ||
4198 (*endptr && unit < param->fp_time_margin))
4199 param->fp_time_margin = unit;
4206 fprintf(stderr, "%s find: bad time '%s': too large\n",
4216 static int str2quotaid(__u32 *id, const char *arg)
4218 unsigned long int projid_tmp = 0;
4219 char *endptr = NULL;
4221 projid_tmp = strtoul(arg, &endptr, 10);
4222 if (*endptr != '\0')
4224 if (projid_tmp >= UINT32_MAX)
4231 static int name2uid(unsigned int *id, const char *name)
4233 struct passwd *passwd;
4235 passwd = getpwnam(name);
4238 *id = passwd->pw_uid;
4243 static int name2gid(unsigned int *id, const char *name)
4245 struct group *group;
4247 group = getgrnam(name);
4250 *id = group->gr_gid;
4255 static inline int name2projid(unsigned int *id, const char *name)
4260 static int uid2name(char **name, unsigned int id)
4262 struct passwd *passwd;
4264 passwd = getpwuid(id);
4267 *name = passwd->pw_name;
4272 static inline int gid2name(char **name, unsigned int id)
4274 struct group *group;
4276 group = getgrgid(id);
4279 *name = group->gr_name;
4284 static int name2layout(__u32 *layout, char *name)
4286 char *ptr, *layout_name;
4289 for (ptr = name; ; ptr = NULL) {
4290 layout_name = strtok(ptr, ",");
4293 if (strcmp(layout_name, "released") == 0)
4294 *layout |= LOV_PATTERN_F_RELEASED;
4295 else if (strcmp(layout_name, "raid0") == 0)
4296 *layout |= LOV_PATTERN_RAID0;
4297 else if (strcmp(layout_name, "mdt") == 0)
4298 *layout |= LOV_PATTERN_MDT;
4299 else if (strcmp(layout_name, "overstriping") == 0)
4300 *layout |= LOV_PATTERN_OVERSTRIPING;
4307 static int lfs_find(int argc, char **argv)
4312 struct find_param param = {
4315 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4317 struct option long_opts[] = {
4318 { .val = 'A', .name = "atime", .has_arg = required_argument },
4319 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4320 { .val = 'B', .name = "btime", .has_arg = required_argument },
4321 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4322 { .val = LFS_COMP_COUNT_OPT,
4323 .name = "comp-count", .has_arg = required_argument },
4324 { .val = LFS_COMP_COUNT_OPT,
4325 .name = "component-count",
4326 .has_arg = required_argument },
4327 { .val = LFS_COMP_FLAGS_OPT,
4328 .name = "comp-flags", .has_arg = required_argument },
4329 { .val = LFS_COMP_FLAGS_OPT,
4330 .name = "component-flags",
4331 .has_arg = required_argument },
4332 { .val = LFS_COMP_START_OPT,
4333 .name = "comp-start", .has_arg = required_argument },
4334 { .val = LFS_COMP_START_OPT,
4335 .name = "component-start",
4336 .has_arg = required_argument },
4337 { .val = LFS_MIRROR_STATE_OPT,
4338 .name = "mirror-state", .has_arg = required_argument },
4339 { .val = LFS_LAYOUT_FOREIGN_OPT,
4340 .name = "foreign", .has_arg = optional_argument},
4341 { .val = LFS_NEWERXY_OPT,
4342 .name = "newer", .has_arg = required_argument},
4343 { .val = LFS_NEWERXY_OPT,
4344 .name = "neweraa", .has_arg = required_argument},
4345 { .val = LFS_NEWERXY_OPT,
4346 .name = "neweram", .has_arg = required_argument},
4347 { .val = LFS_NEWERXY_OPT,
4348 .name = "newerac", .has_arg = required_argument},
4349 { .val = LFS_NEWERXY_OPT,
4350 .name = "newerab", .has_arg = required_argument},
4351 { .val = LFS_NEWERXY_OPT,
4352 .name = "newerma", .has_arg = required_argument},
4353 { .val = LFS_NEWERXY_OPT,
4354 .name = "newermm", .has_arg = required_argument},
4355 { .val = LFS_NEWERXY_OPT,
4356 .name = "newermc", .has_arg = required_argument},
4357 { .val = LFS_NEWERXY_OPT,
4358 .name = "newermb", .has_arg = required_argument},
4359 { .val = LFS_NEWERXY_OPT,
4360 .name = "newerca", .has_arg = required_argument},
4361 { .val = LFS_NEWERXY_OPT,
4362 .name = "newercm", .has_arg = required_argument},
4363 { .val = LFS_NEWERXY_OPT,
4364 .name = "newercc", .has_arg = required_argument},
4365 { .val = LFS_NEWERXY_OPT,
4366 .name = "newercb", .has_arg = required_argument},
4367 { .val = LFS_NEWERXY_OPT,
4368 .name = "newerba", .has_arg = required_argument},
4369 { .val = LFS_NEWERXY_OPT,
4370 .name = "newerbm", .has_arg = required_argument},
4371 { .val = LFS_NEWERXY_OPT,
4372 .name = "newerbc", .has_arg = required_argument},
4373 { .val = LFS_NEWERXY_OPT,
4374 .name = "newerbb", .has_arg = required_argument},
4375 { .val = LFS_NEWERXY_OPT,
4376 .name = "newerBa", .has_arg = required_argument},
4377 { .val = LFS_NEWERXY_OPT,
4378 .name = "newerBm", .has_arg = required_argument},
4379 { .val = LFS_NEWERXY_OPT,
4380 .name = "newerBc", .has_arg = required_argument},
4381 { .val = LFS_NEWERXY_OPT,
4382 .name = "newerBB", .has_arg = required_argument},
4383 { .val = LFS_NEWERXY_OPT,
4384 .name = "newerat", .has_arg = required_argument},
4385 { .val = LFS_NEWERXY_OPT,
4386 .name = "newermt", .has_arg = required_argument},
4387 { .val = LFS_NEWERXY_OPT,
4388 .name = "newerct", .has_arg = required_argument},
4389 { .val = LFS_NEWERXY_OPT,
4390 .name = "newerbt", .has_arg = required_argument},
4391 { .val = LFS_NEWERXY_OPT,
4392 .name = "newerBt", .has_arg = required_argument},
4393 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4394 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4395 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4396 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4397 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4398 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4399 { .val = 'E', .name = "component-end",
4400 .has_arg = required_argument },
4401 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4402 { .val = LFS_LAYOUT_FOREIGN_OPT,
4403 .name = "foreign", .has_arg = optional_argument},
4404 { .val = 'g', .name = "gid", .has_arg = required_argument },
4405 { .val = 'G', .name = "group", .has_arg = required_argument },
4406 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4407 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4408 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4409 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4410 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4411 { .val = 'L', .name = "layout", .has_arg = required_argument },
4412 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4413 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4414 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4415 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4416 { .val = 'n', .name = "name", .has_arg = required_argument },
4417 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4418 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4419 { .val = 'O', .name = "obd", .has_arg = required_argument },
4420 { .val = 'O', .name = "ost", .has_arg = required_argument },
4421 /* no short option for pool yet, can be 'p' after 2.18 */
4422 { .val = LFS_POOL_OPT,
4423 .name = "pool", .has_arg = required_argument },
4424 { .val = '0', .name = "print0", .has_arg = no_argument },
4425 { .val = 'P', .name = "print", .has_arg = no_argument },
4426 { .val = LFS_PROJID_OPT,
4427 .name = "projid", .has_arg = required_argument },
4428 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4429 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4430 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4431 { .val = 's', .name = "size", .has_arg = required_argument },
4432 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4433 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4434 { .val = 't', .name = "type", .has_arg = required_argument },
4435 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4436 { .val = 'u', .name = "uid", .has_arg = required_argument },
4437 { .val = 'U', .name = "user", .has_arg = required_argument },
4438 { .val = 'z', .name = "extension-size",
4439 .has_arg = required_argument },
4440 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4441 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4442 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4456 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4457 while ((c = getopt_long_only(argc, argv,
4458 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4459 long_opts, &optidx)) >= 0) {
4464 /* '!' is part of option */
4466 * when getopt_long_only() finds a string which is not
4467 * an option nor a known option argument it returns 1
4468 * in that case if we already have found pathstart and pathend
4469 * (i.e. we have the list of pathnames),
4470 * the only supported value is "!"
4472 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4473 if (!isoption && pathend != -1) {
4475 "err: %s: filename|dirname must either precede options or follow options\n",
4480 if (!isoption && pathstart == -1)
4481 pathstart = optind - 1;
4482 if (isoption && pathstart != -1 && pathend == -1)
4483 pathend = optind - 2;
4490 * unknown; opt is "!" or path component,
4491 * checking done above.
4493 if (strcmp(optarg, "!") == 0)
4497 xtime = ¶m.fp_atime;
4498 xsign = ¶m.fp_asign;
4499 param.fp_exclude_atime = !!neg_opt;
4500 /* no break, this falls through to 'B' for btime */
4503 xtime = ¶m.fp_btime;
4504 xsign = ¶m.fp_bsign;
4505 param.fp_exclude_btime = !!neg_opt;
4507 /* no break, this falls through to 'C' for ctime */
4510 xtime = ¶m.fp_ctime;
4511 xsign = ¶m.fp_csign;
4512 param.fp_exclude_ctime = !!neg_opt;
4514 /* no break, this falls through to 'M' for mtime */
4517 xtime = ¶m.fp_mtime;
4518 xsign = ¶m.fp_msign;
4519 param.fp_exclude_mtime = !!neg_opt;
4521 rc = set_time(¶m, &t, xtime, optarg);
4522 if (rc == LONG_MAX) {
4530 if (optarg[0] == '+') {
4531 param.fp_blocks_sign = -1;
4533 } else if (optarg[0] == '-') {
4534 param.fp_blocks_sign = 1;
4538 param.fp_blocks_units = 1024;
4539 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4540 ¶m.fp_blocks_units, 0);
4542 fprintf(stderr, "error: bad blocks '%s'\n",
4546 param.fp_check_blocks = 1;
4547 param.fp_exclude_blocks = !!neg_opt;
4549 case LFS_COMP_COUNT_OPT:
4550 if (optarg[0] == '+') {
4551 param.fp_comp_count_sign = -1;
4553 } else if (optarg[0] == '-') {
4554 param.fp_comp_count_sign = 1;
4558 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4559 if (*endptr != '\0') {
4561 "error: bad component count '%s'\n",
4565 param.fp_check_comp_count = 1;
4566 param.fp_exclude_comp_count = !!neg_opt;
4568 case LFS_COMP_FLAGS_OPT:
4569 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4570 ¶m.fp_comp_neg_flags);
4573 "error: bad component flags '%s'\n",
4577 param.fp_check_comp_flags = 1;
4579 __u32 flags = param.fp_comp_neg_flags;
4581 param.fp_comp_neg_flags = param.fp_comp_flags;
4582 param.fp_comp_flags = flags;
4585 case LFS_COMP_START_OPT:
4586 if (optarg[0] == '+') {
4587 param.fp_comp_start_sign = -1;
4589 } else if (optarg[0] == '-') {
4590 param.fp_comp_start_sign = 1;
4594 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4595 ¶m.fp_comp_start_units, 0);
4598 "error: bad component start '%s'\n",
4602 param.fp_check_comp_start = 1;
4603 param.fp_exclude_comp_start = !!neg_opt;
4605 case LFS_MIRROR_STATE_OPT:
4606 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4607 ¶m.fp_mirror_neg_state);
4610 "error: bad mirrored file state '%s'\n",
4614 param.fp_check_mirror_state = 1;
4616 __u16 state = param.fp_mirror_neg_state;
4618 param.fp_mirror_neg_state =
4619 param.fp_mirror_state;
4620 param.fp_mirror_state = state;
4624 if (optarg[0] == '+') {
4625 param.fp_stripe_count_sign = -1;
4627 } else if (optarg[0] == '-') {
4628 param.fp_stripe_count_sign = 1;
4632 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4633 if (*endptr != '\0') {
4635 "error: bad stripe_count '%s'\n",
4640 param.fp_check_stripe_count = 1;
4641 param.fp_exclude_stripe_count = !!neg_opt;
4644 param.fp_max_depth = strtol(optarg, 0, 0);
4647 if (optarg[0] == '+') {
4648 param.fp_comp_end_sign = -1;
4650 } else if (optarg[0] == '-') {
4651 param.fp_comp_end_sign = 1;
4655 if (arg_is_eof(optarg)) {
4656 param.fp_comp_end = LUSTRE_EOF;
4657 param.fp_comp_end_units = 1;
4660 rc = llapi_parse_size(optarg,
4662 ¶m.fp_comp_end_units, 0);
4666 "error: bad component end '%s'\n",
4670 param.fp_check_comp_end = 1;
4671 param.fp_exclude_comp_end = !!neg_opt;
4673 case LFS_LAYOUT_FOREIGN_OPT: {
4674 /* all types by default */
4675 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4678 /* check pure numeric */
4679 type = strtoul(optarg, &endptr, 0);
4682 type = check_foreign_type_name(optarg);
4683 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4685 "%s %s: unknown foreign type '%s'\n",
4692 param.fp_foreign_type = type;
4693 param.fp_check_foreign = 1;
4694 param.fp_exclude_foreign = !!neg_opt;
4697 case LFS_NEWERXY_OPT: {
4703 time_t ref = time(NULL);
4705 /* no need to check bad options, they won't get here */
4706 if (strlen(long_opts[optidx].name) == 7) {
4707 x = long_opts[optidx].name[5];
4708 y = long_opts[optidx].name[6];
4712 static const char *const fmts[] = {
4713 "%Y-%m-%d %H:%M:%S",
4716 "%H:%M:%S", /* sometime today */
4725 for (i = 0; fmts[i] != NULL; i++) {
4728 /* Init for times relative to today */
4729 if (strncmp(fmts[i], "%H", 2) == 0)
4730 localtime_r(&ref, &tm);
4732 memset(&tm, 0, sizeof(tm));
4733 ptr = strptime(optarg, fmts[i], &tm);
4735 while (ptr && isspace(*ptr))
4737 if (ptr == optarg + strlen(optarg)) {
4745 "%s: invalid time '%s'\n",
4748 "supported formats are:\n ");
4749 for (i = 0; fmts[i] != NULL; i++)
4750 fprintf(stderr, "'%s', ",
4752 fprintf(stderr, "\n");
4758 } else if (y == 'b' || y == 'B') {
4761 rc = llapi_get_lum_file(optarg, NULL, &stx,
4763 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4764 if (!(stx.stx_mask & STATX_BTIME))
4769 "%s: get btime failed '%s': %s\n",
4775 ref = stx.stx_btime.tv_sec;
4777 struct stat statbuf;
4779 if (stat(optarg, &statbuf) < 0) {
4781 "%s: cannot stat file '%s': %s\n",
4790 ref = statbuf.st_atime;
4793 ref = statbuf.st_mtime;
4796 ref = statbuf.st_ctime;
4800 "%s: invalid Y argument: '%c'\n",
4809 xidx = NEWERXY_ATIME;
4812 xidx = NEWERXY_MTIME;
4815 xidx = NEWERXY_CTIME;
4819 xidx = NEWERXY_BTIME;
4823 "%s: invalid X argument: '%c'\n",
4830 newery = ¶m.fp_newery[xidx][negidx];
4836 *newery = *newery > ref ? ref : *newery;
4838 *newery = *newery > ref ? *newery : ref;
4840 param.fp_newerxy = 1;
4845 rc = name2gid(¶m.fp_gid, optarg);
4847 if (str2quotaid(¶m.fp_gid, optarg)) {
4849 "Group/GID: %s cannot be found.\n",
4855 param.fp_exclude_gid = !!neg_opt;
4856 param.fp_check_gid = 1;
4859 param.fp_hash_type = check_hashtype(optarg);
4860 if (param.fp_hash_type == 0) {
4861 fprintf(stderr, "error: bad hash_type '%s'\n",
4866 param.fp_check_hash_type = 1;
4867 param.fp_exclude_hash_type = !!neg_opt;
4873 ret = name2layout(¶m.fp_layout, optarg);
4876 param.fp_exclude_layout = !!neg_opt;
4877 param.fp_check_layout = 1;
4881 rc = name2uid(¶m.fp_uid, optarg);
4883 if (str2quotaid(¶m.fp_uid, optarg)) {
4885 "User/UID: %s cannot be found.\n",
4891 param.fp_exclude_uid = !!neg_opt;
4892 param.fp_check_uid = 1;
4895 param.fp_pattern = (char *)optarg;
4896 param.fp_exclude_pattern = !!neg_opt;
4899 if (optarg[0] == '+') {
4900 param.fp_mirror_count_sign = -1;
4902 } else if (optarg[0] == '-') {
4903 param.fp_mirror_count_sign = 1;
4907 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4908 if (*endptr != '\0') {
4910 "error: bad mirror count '%s'\n",
4914 param.fp_check_mirror_count = 1;
4915 param.fp_exclude_mirror_count = !!neg_opt;
4920 char *buf, *token, *next, *p;
4924 buf = strdup(optarg);
4930 param.fp_exclude_obd = !!neg_opt;
4933 while (token && *token) {
4934 token = strchr(token, ',');
4941 param.fp_exclude_mdt = !!neg_opt;
4942 param.fp_num_alloc_mdts += len;
4943 tmp = realloc(param.fp_mdt_uuid,
4944 param.fp_num_alloc_mdts *
4945 sizeof(*param.fp_mdt_uuid));
4951 param.fp_mdt_uuid = tmp;
4953 param.fp_exclude_obd = !!neg_opt;
4954 param.fp_num_alloc_obds += len;
4955 tmp = realloc(param.fp_obd_uuid,
4956 param.fp_num_alloc_obds *
4957 sizeof(*param.fp_obd_uuid));
4963 param.fp_obd_uuid = tmp;
4965 for (token = buf; token && *token; token = next) {
4966 struct obd_uuid *puuid;
4970 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4973 ¶m.fp_obd_uuid[param.fp_num_obds++];
4975 p = strchr(token, ',');
4982 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4987 strncpy(puuid->uuid, token,
4988 sizeof(puuid->uuid));
4995 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4999 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5001 "Pool name %s is too long (max %d)\n",
5002 optarg, LOV_MAXPOOLNAME);
5007 * We do check for empty pool because empty pool
5008 * is used to find V1 LOV attributes
5010 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5011 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5012 param.fp_exclude_pool = !!neg_opt;
5013 param.fp_check_pool = 1;
5015 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5016 case 'p': /* want this for --pool, to match getstripe/find */
5018 "warning: -p deprecated, use --print0 or -0\n");
5021 param.fp_zero_end = 1;
5023 case 'P': /* we always print, this option is a no-op */
5025 case LFS_PROJID_OPT:
5026 rc = name2projid(¶m.fp_projid, optarg);
5028 if (str2quotaid(¶m.fp_projid, optarg)) {
5030 "Invalid project ID: %s\n",
5036 param.fp_exclude_projid = !!neg_opt;
5037 param.fp_check_projid = 1;
5040 if (optarg[0] == '+') {
5041 param.fp_size_sign = -1;
5043 } else if (optarg[0] == '-') {
5044 param.fp_size_sign = 1;
5048 ret = llapi_parse_size(optarg, ¶m.fp_size,
5049 ¶m.fp_size_units, 0);
5051 fprintf(stderr, "error: bad file size '%s'\n",
5055 param.fp_check_size = 1;
5056 param.fp_exclude_size = !!neg_opt;
5059 if (optarg[0] == '+') {
5060 param.fp_stripe_size_sign = -1;
5062 } else if (optarg[0] == '-') {
5063 param.fp_stripe_size_sign = 1;
5067 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5068 ¶m.fp_stripe_size_units, 0);
5070 fprintf(stderr, "error: bad stripe_size '%s'\n",
5074 param.fp_check_stripe_size = 1;
5075 param.fp_exclude_stripe_size = !!neg_opt;
5078 param.fp_exclude_type = !!neg_opt;
5079 switch (optarg[0]) {
5081 param.fp_type = S_IFBLK;
5084 param.fp_type = S_IFCHR;
5087 param.fp_type = S_IFDIR;
5090 param.fp_type = S_IFREG;
5093 param.fp_type = S_IFLNK;
5096 param.fp_type = S_IFIFO;
5099 param.fp_type = S_IFSOCK;
5102 fprintf(stderr, "error: %s: bad type '%s'\n",
5109 if (optarg[0] == '+') {
5110 param.fp_mdt_count_sign = -1;
5112 } else if (optarg[0] == '-') {
5113 param.fp_mdt_count_sign = 1;
5117 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5118 if (*endptr != '\0') {
5119 fprintf(stderr, "error: bad mdt_count '%s'\n",
5124 param.fp_check_mdt_count = 1;
5125 param.fp_exclude_mdt_count = !!neg_opt;
5128 if (optarg[0] == '+') {
5129 param.fp_ext_size_sign = -1;
5131 } else if (optarg[0] == '-') {
5132 param.fp_ext_size_sign = 1;
5136 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5137 ¶m.fp_ext_size_units, 0);
5139 fprintf(stderr, "error: bad ext-size '%s'\n",
5143 param.fp_ext_size /= SEL_UNIT_SIZE;
5144 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5145 param.fp_check_ext_size = 1;
5146 param.fp_exclude_ext_size = !!neg_opt;
5154 if (pathstart == -1) {
5155 fprintf(stderr, "error: %s: no filename|pathname\n",
5159 } else if (pathend == -1) {
5165 rc = llapi_find(argv[pathstart], ¶m);
5168 pathbad = pathstart;
5170 } while (++pathstart < pathend);
5173 fprintf(stderr, "%s: failed for '%s': %s\n",
5174 progname, argv[pathbad], strerror(-rc));
5177 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5178 free(param.fp_obd_uuid);
5180 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5181 free(param.fp_mdt_uuid);
5186 static int lfs_getstripe_internal(int argc, char **argv,
5187 struct find_param *param)
5189 struct option long_opts[] = {
5190 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5191 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5192 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5193 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5194 { .val = LFS_COMP_COUNT_OPT,
5195 .name = "comp-count", .has_arg = no_argument },
5196 { .val = LFS_COMP_COUNT_OPT,
5197 .name = "component-count", .has_arg = no_argument },
5198 { .val = LFS_COMP_FLAGS_OPT,
5199 .name = "comp-flags", .has_arg = optional_argument },
5200 { .val = LFS_COMP_FLAGS_OPT,
5201 .name = "component-flags", .has_arg = optional_argument },
5202 { .val = LFS_COMP_START_OPT,
5203 .name = "comp-start", .has_arg = optional_argument },
5204 { .val = LFS_COMP_START_OPT,
5205 .name = "component-start", .has_arg = optional_argument },
5206 { .val = LFS_MIRROR_INDEX_OPT,
5207 .name = "mirror-index", .has_arg = required_argument },
5208 { .val = LFS_MIRROR_ID_OPT,
5209 .name = "mirror-id", .has_arg = required_argument },
5210 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5211 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5212 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5213 { .val = 'd', .name = "directory", .has_arg = no_argument },
5214 { .val = 'D', .name = "default", .has_arg = no_argument },
5215 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5216 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5217 { .val = 'F', .name = "fid", .has_arg = no_argument },
5218 { .val = 'g', .name = "generation", .has_arg = no_argument },
5219 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5220 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5221 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5222 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5223 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5224 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5225 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5226 { .val = 'L', .name = "layout", .has_arg = no_argument },
5227 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5228 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5229 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5230 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5231 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5232 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5233 { .val = 'O', .name = "obd", .has_arg = required_argument },
5234 { .val = 'O', .name = "ost", .has_arg = required_argument },
5235 { .val = 'p', .name = "pool", .has_arg = no_argument },
5236 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5237 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5238 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5239 { .val = 'R', .name = "raw", .has_arg = no_argument },
5240 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5241 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5242 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5243 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5244 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5245 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5246 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5247 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5248 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5249 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5253 int pathstart = -1, pathend = -1;
5257 while ((c = getopt_long(argc, argv,
5258 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5259 long_opts, NULL)) != -1) {
5263 /* '!' is part of option */
5264 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5265 if (!isoption && pathend != -1) {
5267 "error: %s: filename|dirname must either precede options or follow options\n",
5271 if (!isoption && pathstart == -1)
5272 pathstart = optind - 1;
5273 if (isoption && pathstart != -1 && pathend == -1)
5274 pathend = optind - 2;
5278 /* unknown: opt is "!" */
5279 if (strcmp(optarg, "!") == 0)
5283 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5284 param->fp_verbose |= VERBOSE_COUNT;
5285 param->fp_max_depth = 0;
5288 case LFS_COMP_COUNT_OPT:
5289 param->fp_verbose |= VERBOSE_COMP_COUNT;
5290 param->fp_max_depth = 0;
5292 case LFS_COMP_FLAGS_OPT:
5294 rc = comp_str2flags(optarg,
5295 ¶m->fp_comp_flags,
5296 ¶m->fp_comp_neg_flags);
5299 "error: %s bad component flags '%s'.\n",
5303 param->fp_check_comp_flags = 1;
5305 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5306 param->fp_max_depth = 0;
5309 case LFS_COMP_START_OPT:
5312 if (tmp[0] == '+') {
5313 param->fp_comp_start_sign = -1;
5315 } else if (tmp[0] == '-') {
5316 param->fp_comp_start_sign = 1;
5319 rc = llapi_parse_size(tmp,
5320 ¶m->fp_comp_start,
5321 ¶m->fp_comp_start_units, 0);
5324 "error: %s bad component start '%s'.\n",
5328 param->fp_check_comp_start = 1;
5330 param->fp_verbose |= VERBOSE_COMP_START;
5331 param->fp_max_depth = 0;
5334 case LFS_MIRROR_INDEX_OPT:
5335 if (optarg[0] == '+') {
5336 param->fp_mirror_index_sign = -1;
5338 } else if (optarg[0] == '-') {
5339 param->fp_mirror_index_sign = 1;
5343 param->fp_mirror_index = strtoul(optarg, &end, 0);
5344 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5345 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5347 "%s %s: invalid mirror index '%s'\n",
5348 progname, argv[0], optarg);
5351 if (param->fp_mirror_id != 0) {
5353 "%s %s: can't specify both mirror index and mirror ID\n",
5357 param->fp_check_mirror_index = 1;
5358 param->fp_exclude_mirror_index = !!neg_opt;
5360 case LFS_MIRROR_ID_OPT:
5361 if (optarg[0] == '+') {
5362 param->fp_mirror_id_sign = -1;
5364 } else if (optarg[0] == '-') {
5365 param->fp_mirror_id_sign = 1;
5369 param->fp_mirror_id = strtoul(optarg, &end, 0);
5370 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5371 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5373 "%s %s: invalid mirror ID '%s'\n",
5374 progname, argv[0], optarg);
5377 if (param->fp_mirror_index != 0) {
5379 "%s %s: can't specify both mirror index and mirror ID\n",
5383 param->fp_check_mirror_id = 1;
5384 param->fp_exclude_mirror_id = !!neg_opt;
5387 param->fp_max_depth = 0;
5390 param->fp_get_default_lmv = 1;
5395 if (tmp[0] == '+') {
5396 param->fp_comp_end_sign = -1;
5398 } else if (tmp[0] == '-') {
5399 param->fp_comp_end_sign = 1;
5403 if (arg_is_eof(tmp)) {
5404 param->fp_comp_end = LUSTRE_EOF;
5405 param->fp_comp_end_units = 1;
5408 rc = llapi_parse_size(tmp,
5409 ¶m->fp_comp_end,
5410 ¶m->fp_comp_end_units, 0);
5414 "error: %s bad component end '%s'.\n",
5418 param->fp_check_comp_end = 1;
5420 param->fp_verbose |= VERBOSE_COMP_END;
5421 param->fp_max_depth = 0;
5425 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5426 param->fp_verbose |= VERBOSE_DFID;
5427 param->fp_max_depth = 0;
5431 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5432 param->fp_verbose |= VERBOSE_GENERATION;
5433 param->fp_max_depth = 0;
5437 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5438 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5439 param->fp_max_depth = 0;
5444 param->fp_comp_id = strtoul(optarg, &end, 0);
5445 if (*end != '\0' || param->fp_comp_id == 0 ||
5446 param->fp_comp_id > LCME_ID_MAX) {
5448 "error: %s bad component id '%s'\n",
5452 param->fp_check_comp_id = 1;
5454 param->fp_max_depth = 0;
5455 param->fp_verbose |= VERBOSE_COMP_ID;
5459 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5460 param->fp_verbose |= VERBOSE_PATTERN;
5461 param->fp_max_depth = 0;
5464 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5467 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5470 if (!(param->fp_verbose & VERBOSE_DETAIL))
5471 param->fp_max_depth = 0;
5472 param->fp_verbose |= VERBOSE_MDTINDEX;
5475 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5476 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5477 param->fp_max_depth = 0;
5481 if (param->fp_obd_uuid) {
5483 "error: %s: only one obduuid allowed",
5487 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5490 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5491 param->fp_verbose |= VERBOSE_POOL;
5492 param->fp_max_depth = 0;
5499 param->fp_recursive = 1;
5505 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5506 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5507 param->fp_max_depth = 0;
5511 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5517 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5518 param->fp_verbose |= VERBOSE_EXT_SIZE;
5519 param->fp_max_depth = 0;
5527 if (pathstart == -1) {
5528 fprintf(stderr, "error: %s: no filename|pathname\n",
5531 } else if (pathend == -1) {
5539 if (param->fp_recursive)
5540 param->fp_max_depth = -1;
5541 else if (param->fp_verbose & VERBOSE_DETAIL)
5542 param->fp_max_depth = 1;
5544 if (!param->fp_verbose)
5545 param->fp_verbose = VERBOSE_DEFAULT;
5546 if (param->fp_quiet)
5547 param->fp_verbose = VERBOSE_OBJID;
5550 rc = llapi_getstripe(argv[pathstart], param);
5551 } while (++pathstart < pathend && !rc);
5554 fprintf(stderr, "error: %s failed for %s.\n",
5555 argv[0], argv[optind - 1]);
5559 static int lfs_tgts(int argc, char **argv)
5561 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5562 struct find_param param;
5563 int index = 0, rc = 0;
5568 if (argc == 2 && !realpath(argv[1], path)) {
5570 fprintf(stderr, "error: invalid path '%s': %s\n",
5571 argv[1], strerror(-rc));
5575 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5576 /* Check if we have a mount point */
5577 if (mntdir[0] == '\0')
5580 memset(¶m, 0, sizeof(param));
5581 if (!strcmp(argv[0], "mdts"))
5582 param.fp_get_lmv = 1;
5584 rc = llapi_ostlist(mntdir, ¶m);
5586 fprintf(stderr, "error: %s: failed on %s\n",
5589 if (path[0] != '\0')
5591 memset(mntdir, 0, PATH_MAX);
5597 static int lfs_getstripe(int argc, char **argv)
5599 struct find_param param = { 0 };
5601 param.fp_max_depth = 1;
5602 return lfs_getstripe_internal(argc, argv, ¶m);
5606 static int lfs_getdirstripe(int argc, char **argv)
5608 struct find_param param = { 0 };
5609 struct option long_opts[] = {
5610 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5611 { .val = 'D', .name = "default", .has_arg = no_argument },
5612 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5613 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5614 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5615 { .val = 'O', .name = "obd", .has_arg = required_argument },
5616 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5617 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5618 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5619 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5623 param.fp_get_lmv = 1;
5625 while ((c = getopt_long(argc, argv,
5626 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5630 param.fp_verbose |= VERBOSE_COUNT;
5633 param.fp_get_default_lmv = 1;
5637 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5639 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5642 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5645 param.fp_verbose |= VERBOSE_HASH_TYPE;
5648 if (param.fp_obd_uuid) {
5650 "error: %s: only one obduuid allowed",
5654 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5657 param.fp_recursive = 1;
5660 param.fp_verbose |= VERBOSE_DETAIL;
5666 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5667 progname, argv[0], argv[optind - 1]);
5675 if (param.fp_recursive)
5676 param.fp_max_depth = -1;
5678 if (!param.fp_verbose)
5679 param.fp_verbose = VERBOSE_DEFAULT;
5682 rc = llapi_getstripe(argv[optind], ¶m);
5683 } while (++optind < argc && !rc);
5686 fprintf(stderr, "error: %s failed for %s.\n",
5687 argv[0], argv[optind - 1]);
5692 MNTDF_INODES = 0x0001,
5693 MNTDF_COOKED = 0x0002,
5694 MNTDF_LAZY = 0x0004,
5695 MNTDF_VERBOSE = 0x0008,
5696 MNTDF_SHOW = 0x0010,
5699 #define COOK(value) \
5702 while (value > 1024) { \
5710 #define CDF "%11llu"
5711 #define HDF "%8.1f%c"
5715 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5717 double avail, used, ratio = 0;
5720 avail = st->os_ffree;
5721 used = st->os_files - st->os_ffree;
5723 avail = st->os_bavail;
5724 used = st->os_blocks - st->os_bfree;
5726 if (avail + used > 0)
5727 ratio = used / (used + avail) * 100;
5729 /* Round up to match df(1) usage percentage */
5730 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5734 * This is to identify various problem states for "lfs df" if .osn_err = true,
5735 * so only show flags reflecting those states by default. Informational states
5736 * are only shown with "-v" and use lower-case names to distinguish them.
5737 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5739 static struct obd_statfs_state_names {
5740 enum obd_statfs_state osn_state;
5741 const char osn_name;
5744 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5745 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5746 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5747 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5748 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5749 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5750 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5751 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5752 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5755 static int showdf(char *mntdir, struct obd_statfs *stat,
5756 char *uuid, enum mntdf_flags flags,
5757 char *type, int index, int rc)
5759 long long avail, used, total;
5761 char *suffix = "KMGTPEZY";
5762 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5763 char tbuf[3 * sizeof(__u64)];
5764 char ubuf[3 * sizeof(__u64)];
5765 char abuf[3 * sizeof(__u64)];
5766 char rbuf[3 * sizeof(__u64)];
5773 if (flags & MNTDF_INODES) {
5774 avail = stat->os_ffree;
5775 used = stat->os_files - stat->os_ffree;
5776 total = stat->os_files;
5778 int shift = flags & MNTDF_COOKED ? 0 : 10;
5780 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5781 used = ((stat->os_blocks - stat->os_bfree) *
5782 stat->os_bsize) >> shift;
5783 total = (stat->os_blocks * stat->os_bsize) >> shift;
5786 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5788 if (flags & MNTDF_COOKED) {
5792 cook_val = (double)total;
5795 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5798 snprintf(tbuf, sizeof(tbuf), CDF, total);
5800 cook_val = (double)used;
5803 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5806 snprintf(ubuf, sizeof(ubuf), CDF, used);
5808 cook_val = (double)avail;
5811 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5814 snprintf(abuf, sizeof(abuf), CDF, avail);
5816 snprintf(tbuf, sizeof(tbuf), CDF, total);
5817 snprintf(ubuf, sizeof(tbuf), CDF, used);
5818 snprintf(abuf, sizeof(tbuf), CDF, avail);
5821 sprintf(rbuf, RDF, ratio);
5822 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5823 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5825 printf("[%s:%d]", type, index);
5827 if (stat->os_state) {
5831 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5832 if (oss_names[i].osn_state & stat->os_state &&
5833 (oss_names[i].osn_err ||
5834 flags & MNTDF_VERBOSE))
5835 printf("%c", oss_names[i].osn_name);
5842 printf(UUF": inactive device\n", uuid);
5845 printf(UUF": %s\n", uuid, strerror(-rc));
5852 struct ll_stat_type {
5857 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5859 struct ll_statfs_data {
5861 struct obd_statfs sd_st;
5864 struct ll_statfs_buf {
5866 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5869 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5870 int ops, struct ll_statfs_buf *lsb)
5872 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5873 struct obd_uuid uuid_buf;
5874 char *poolname = NULL;
5875 struct ll_stat_type types[] = {
5876 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5877 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5878 { .st_name = NULL } };
5879 struct ll_stat_type *tp;
5880 __u64 ost_files = 0;
5881 __u64 ost_ffree = 0;
5889 poolname = strchr(pool, '.');
5891 if (strncmp(fsname, pool, strlen(fsname))) {
5892 fprintf(stderr, "filesystem name incorrect\n");
5900 fd = open(mntdir, O_RDONLY);
5903 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5908 if (flags & MNTDF_SHOW) {
5909 if (flags & MNTDF_INODES)
5910 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5911 "UUID", "Inodes", "IUsed", "IFree",
5912 "IUse%", "Mounted on");
5914 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5916 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5917 "Used", "Available", "Use%", "Mounted on");
5920 for (tp = types; tp->st_name != NULL; tp++) {
5921 bool have_ost = false;
5923 if (!(tp->st_op & ops))
5926 for (index = 0; ; index++) {
5927 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5928 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5929 type = flags & MNTDF_LAZY ?
5930 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5931 rc2 = llapi_obd_fstatfs(fd, type, index,
5932 &stat_buf, &uuid_buf);
5937 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5938 if (!(flags & MNTDF_VERBOSE))
5940 } else if (rc2 < 0 && rc == 0) {
5945 * If we have OSTs then don't report MDT block counts.
5946 * For MDT-only filesystems the expectation is that all
5947 * layouts have a DoM component. For filesystems with
5948 * OSTs, files are not necessarily going to store data
5949 * on MDTs, and MDT space is limited to a fraction of
5950 * OST space, so don't include it in the summary.
5952 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5959 if (poolname && tp->st_op == LL_STATFS_LOV &&
5960 llapi_search_ost(fsname, poolname,
5961 obd_uuid2str(&uuid_buf)) != 1)
5965 * the llapi_obd_fstatfs() call may have returned with
5966 * an error, but if it filled in uuid_buf we will at
5967 * lease use that to print out a message for that OBD.
5968 * If we didn't get anything in the uuid_buf, then fill
5969 * it in so that we can print an error message.
5971 if (uuid_buf.uuid[0] == '\0')
5972 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5973 "%s%04x", tp->st_name, index);
5975 lsb->sb_buf[lsb->sb_count].sd_index = index;
5976 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5979 if (flags & MNTDF_SHOW)
5980 showdf(mntdir, &stat_buf,
5981 obd_uuid2str(&uuid_buf), flags,
5982 tp->st_name, index, rc2);
5987 if (tp->st_op == LL_STATFS_LMV) {
5988 sum.os_ffree += stat_buf.os_ffree;
5989 sum.os_files += stat_buf.os_files;
5990 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5991 ost_files += stat_buf.os_files;
5992 ost_ffree += stat_buf.os_ffree;
5994 sum.os_blocks += stat_buf.os_blocks *
5996 sum.os_bfree += stat_buf.os_bfree *
5998 sum.os_bavail += stat_buf.os_bavail *
6006 * If we have _some_ OSTs, but don't have as many free objects on the
6007 * OST as inodes on the MDTs, reduce the reported number of inodes
6008 * to compensate, so that the "inodes in use" number is correct.
6009 * This should be kept in sync with ll_statfs_internal().
6011 if (ost_files && ost_ffree < sum.os_ffree) {
6012 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6013 sum.os_ffree = ost_ffree;
6015 if (flags & MNTDF_SHOW) {
6017 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6025 static int lfs_setdirstripe(int argc, char **argv)
6028 struct lfs_setstripe_args lsa = { 0 };
6029 struct llapi_stripe_param *param = NULL;
6030 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6033 char *mode_opt = NULL;
6034 bool default_stripe = false;
6035 bool delete = false;
6036 bool foreign_mode = false;
6037 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6038 mode_t previous_mode = 0;
6040 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
6041 struct option long_opts[] = {
6042 { .val = 'c', .name = "count", .has_arg = required_argument },
6043 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6044 { .val = 'd', .name = "delete", .has_arg = no_argument },
6045 { .val = 'D', .name = "default", .has_arg = no_argument },
6046 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6047 { .val = LFS_LAYOUT_FLAGS_OPT,
6048 .name = "flags", .has_arg = required_argument },
6049 { .val = LFS_LAYOUT_FOREIGN_OPT,
6050 .name = "foreign", .has_arg = optional_argument},
6051 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6052 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6053 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6054 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6056 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6057 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6058 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6060 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6061 { .val = 'i', .name = "index", .has_arg = required_argument },
6063 { .val = 'o', .name = "mode", .has_arg = required_argument },
6064 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6065 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6067 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6068 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6069 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6073 setstripe_args_init(&lsa);
6075 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6076 long_opts, NULL)) >= 0) {
6083 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6086 "%s %s: invalid stripe count '%s'\n",
6087 progname, argv[0], optarg);
6093 default_stripe = true;
6096 default_stripe = true;
6098 case LFS_LAYOUT_FOREIGN_OPT:
6100 /* check pure numeric */
6101 type = strtoul(optarg, &end, 0);
6104 type = check_foreign_type_name(optarg);
6105 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6107 "%s %s: unknown foreign type '%s'\n",
6114 foreign_mode = true;
6116 case LFS_LAYOUT_FLAGS_OPT:
6117 flags = strtoul(optarg, &end, 16);
6120 "%s %s: bad flags '%s'\n",
6121 progname, argv[0], optarg);
6125 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6128 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6131 lsa.lsa_pattern = check_hashtype(optarg);
6132 if (lsa.lsa_pattern == 0) {
6134 "%s %s: bad stripe hash type '%s'\n",
6135 progname, argv[0], optarg);
6140 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6143 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6144 if (strcmp(argv[optind - 1], "--index") == 0)
6146 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6149 lsa.lsa_nr_tgts = parse_targets(mdts,
6150 sizeof(mdts) / sizeof(__u32),
6151 lsa.lsa_nr_tgts, optarg, NULL);
6152 if (lsa.lsa_nr_tgts < 0) {
6154 "%s %s: invalid MDT target(s) '%s'\n",
6155 progname, argv[0], optarg);
6159 lsa.lsa_tgts = mdts;
6160 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6161 lsa.lsa_stripe_off = mdts[0];
6163 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6166 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6175 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6176 progname, argv[0], argv[optind - 1]);
6181 if (optind == argc) {
6182 fprintf(stderr, "%s %s: DIR must be specified\n",
6187 if (xattr && !foreign_mode) {
6189 * only print a warning as this is armless and will be
6193 "%s %s: xattr has been specified for non-foreign layout\n",
6195 } else if (foreign_mode && !xattr) {
6197 "%s %s: xattr must be provided in foreign mode\n",
6202 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6203 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6205 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6210 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6211 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6212 /* if no parameters set, create directory on least-used MDTs */
6213 lsa.lsa_stripe_off = -1;
6214 lsa.lsa_stripe_count = 1;
6218 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6219 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6221 "%s %s: cannot specify -d with -c or -i options\n",
6227 mode = strtoul(mode_opt, &end, 8);
6230 "%s %s: bad MODE '%s'\n",
6231 progname, argv[0], mode_opt);
6234 previous_mode = umask(0);
6237 /* foreign LMV/dir case */
6239 if (argc > optind + 1) {
6241 "%s %s: cannot specify multiple foreign dirs\n",
6246 dname = argv[optind];
6247 result = llapi_dir_create_foreign(dname, mode, type, flags,
6251 "%s mkdir: can't create foreign dir '%s': %s\n",
6252 progname, dname, strerror(-result));
6257 * initialize stripe parameters, in case param is converted to specific,
6258 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6260 param = calloc(1, offsetof(typeof(*param),
6261 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6262 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6265 "%s %s: cannot allocate memory for parameters: %s\n",
6266 progname, argv[0], strerror(ENOMEM));
6270 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6271 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6272 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6273 lsa.lsa_stripe_count == 0))
6274 lsa.lsa_stripe_count = 1;
6275 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6276 param->lsp_stripe_count = lsa.lsa_stripe_count;
6277 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6278 param->lsp_stripe_offset = -1;
6280 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6281 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6282 param->lsp_stripe_pattern = lsa.lsa_pattern;
6284 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6285 param->lsp_pool = lsa.lsa_pool_name;
6286 param->lsp_is_specific = false;
6287 if (lsa.lsa_nr_tgts > 1) {
6288 if (lsa.lsa_stripe_count > 0 &&
6289 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6290 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6292 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6293 argv[0], lsa.lsa_stripe_count,
6299 param->lsp_is_specific = true;
6300 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6301 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6304 dname = argv[optind];
6306 if (default_stripe) {
6307 result = llapi_dir_set_default_lmv(dname, param);
6310 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6311 progname, dname, strerror(-result));
6315 result = llapi_dir_create(dname, mode, param);
6318 "%s setdirstripe: cannot create dir '%s': %s\n",
6319 progname, dname, strerror(-result));
6320 } while (!result && (dname = argv[++optind]));
6323 umask(previous_mode);
6330 static int lfs_rmentry(int argc, char **argv)
6337 fprintf(stderr, "error: %s: missing dirname\n",
6343 dname = argv[index];
6345 result = llapi_direntry_remove(dname);
6348 "error: %s: remove dir entry '%s' failed\n",
6352 dname = argv[++index];
6357 static int lfs_mv(int argc, char **argv)
6359 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6360 struct find_param param = {
6367 struct option long_opts[] = {
6368 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6369 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6370 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6373 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6375 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6378 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6381 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6383 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6389 param.fp_verbose = VERBOSE_DETAIL;
6392 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6393 progname, argv[optind - 1]);
6398 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6399 fprintf(stderr, "%s mv: MDT index must be specified\n",
6404 if (optind >= argc) {
6405 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6409 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6411 /* initialize migrate mdt parameters */
6412 param.fp_lmv_md = &lmu;
6413 param.fp_migrate = 1;
6414 rc = llapi_migrate_mdt(argv[optind], ¶m);
6416 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6417 progname, argv[optind], lmu.lum_stripe_offset,
6422 static int lfs_osts(int argc, char **argv)
6424 return lfs_tgts(argc, argv);
6427 static int lfs_mdts(int argc, char **argv)
6429 return lfs_tgts(argc, argv);
6432 static int lfs_df(int argc, char **argv)
6434 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6435 enum mntdf_flags flags = MNTDF_SHOW;
6436 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6437 int c, rc = 0, index = 0;
6438 char fsname[PATH_MAX] = "", *pool_name = NULL;
6439 struct option long_opts[] = {
6440 { .val = 'h', .name = "human-readable",
6441 .has_arg = no_argument },
6442 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6443 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6444 { .val = 'p', .name = "pool", .has_arg = required_argument },
6445 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6448 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6451 flags |= MNTDF_COOKED;
6454 flags |= MNTDF_INODES;
6457 flags |= MNTDF_LAZY;
6463 flags |= MNTDF_VERBOSE;
6469 if (optind < argc && !realpath(argv[optind], path)) {
6471 fprintf(stderr, "error: invalid path '%s': %s\n",
6472 argv[optind], strerror(-rc));
6476 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6477 /* Check if we have a mount point */
6478 if (mntdir[0] == '\0')
6481 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6482 if (rc || path[0] != '\0')
6484 fsname[0] = '\0'; /* avoid matching in next loop */
6485 mntdir[0] = '\0'; /* avoid matching in next loop */
6491 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6492 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6496 if (opt_fsname == opt_instance) { /* both true or both false */
6497 rc = llapi_getname(mntdir, buf, buflen);
6498 } else if (opt_fsname) {
6500 * llapi_search_mounts() fills @buf with fsname, but that is not
6501 * called if explicit paths are specified on the command-line
6504 rc = llapi_get_fsname(mntdir, buf, buflen);
6505 } else /* if (opt_instance) */ {
6506 rc = llapi_get_instance(mntdir, buf, buflen);
6510 fprintf(stderr, "cannot get instance for '%s': %s\n",
6511 mntdir, strerror(-rc));
6516 printf("%s %s\n", buf, mntdir);
6518 printf("%s\n", buf);
6523 static int lfs_getname(int argc, char **argv)
6525 struct option long_opts[] = {
6526 { .val = 'h', .name = "help", .has_arg = no_argument },
6527 { .val = 'i', .name = "instance", .has_arg = no_argument },
6528 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6530 bool opt_instance = false, opt_fsname = false;
6531 char fsname[PATH_MAX] = "";
6534 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6537 opt_instance = true;
6548 if (optind == argc) { /* no paths specified, get all paths. */
6549 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6552 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6553 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6554 opt_instance, opt_fsname, true);
6557 path[0] = fsname[0] = mntdir[0] = '\0';
6559 } else { /* paths specified, only attempt to search these. */
6562 /* if only one path is given, print only requested info */
6563 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6565 for (; optind < argc; optind++) {
6566 rc2 = print_instance(argv[optind], fsname,
6567 sizeof(fsname), opt_instance,
6568 opt_fsname, opt_mntdir);
6578 static int lfs_check(int argc, char **argv)
6580 char mntdir[PATH_MAX] = {'\0'};
6589 fprintf(stderr, "%s check: server type must be specified\n",
6594 obd_types[0] = obd_type1;
6595 obd_types[1] = obd_type2;
6596 obd_types[2] = obd_type3;
6598 if (strcmp(argv[1], "osts") == 0) {
6599 strcpy(obd_types[0], "osc");
6600 } else if (strcmp(argv[1], "mdts") == 0 ||
6601 strcmp(argv[1], "mds") == 0) {
6602 strcpy(obd_types[0], "mdc");
6603 } else if (strcmp(argv[1], "mgts") == 0) {
6604 strcpy(obd_types[0], "mgc");
6605 } else if (strcmp(argv[1], "all") == 0 ||
6606 strcmp(argv[1], "servers") == 0) {
6608 strcpy(obd_types[0], "osc");
6609 strcpy(obd_types[1], "mdc");
6610 strcpy(obd_types[2], "mgc");
6612 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6617 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6618 if (rc < 0 || mntdir[0] == '\0') {
6620 "%s check: cannot find mounted Lustre filesystem: %s\n",
6621 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6625 rc = llapi_target_check(num_types, obd_types, mntdir);
6627 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6628 progname, argv[1], strerror(-rc));
6633 #ifdef HAVE_SYS_QUOTA_H
6634 #define ADD_OVERFLOW(a, b) \
6635 ((((a) + (b)) < (a)) ? \
6636 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6638 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6639 * returns the value or ULONG_MAX on integer overflow or incorrect format
6641 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6642 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6643 * 3. empty integer value is interpreted as 0
6645 static unsigned long str2sec(const char *timestr)
6647 const char spec[] = "smhdw";
6648 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6649 unsigned long val = 0;
6652 if (strpbrk(timestr, spec) == NULL) {
6654 * no specifiers inside the time string,
6655 * should treat it as an integer value
6657 val = strtoul(timestr, &tail, 10);
6658 return *tail ? ULONG_MAX : val;
6661 /* format string is XXwXXdXXhXXmXXs */
6667 v = strtoul(timestr, &tail, 10);
6668 if (v == ULONG_MAX || *tail == '\0')
6670 * value too large (ULONG_MAX or more)
6671 * or missing specifier
6675 ptr = strchr(spec, *tail);
6677 /* unknown specifier */
6682 /* check if product will overflow the type */
6683 if (!(v < ULONG_MAX / mult[ind]))
6686 ADD_OVERFLOW(val, mult[ind] * v);
6687 if (val == ULONG_MAX)
6699 #define ARG2ULL(nr, str, def_units) \
6701 unsigned long long limit, units = def_units; \
6704 rc = llapi_parse_size(str, &limit, &units, 1); \
6706 fprintf(stderr, "%s: invalid limit '%s'\n", \
6713 static inline int has_times_option(int argc, char **argv)
6717 for (i = 1; i < argc; i++)
6718 if (!strcmp(argv[i], "-t"))
6724 static inline int lfs_verify_poolarg(char *pool)
6726 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6728 "Pool name '%.*s' is longer than %d\n",
6729 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6735 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6738 char *mnt, *obd_type = (char *)qctl->obd_type;
6739 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6740 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6741 struct option long_opts[] = {
6742 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6743 { .val = 'g', .name = "group", .has_arg = no_argument },
6744 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6745 { .val = 'p', .name = "projid", .has_arg = no_argument },
6746 { .val = 't', .name = "times", .has_arg = no_argument },
6747 { .val = 'u', .name = "user", .has_arg = no_argument },
6748 { .val = LFS_POOL_OPT,
6749 .name = "pool", .has_arg = required_argument },
6753 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6754 qctl->qc_type = ALLQUOTA;
6756 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6757 long_opts, NULL)) != -1) {
6768 if (qctl->qc_type != ALLQUOTA) {
6770 "error: -u/g/p can't be used more than once\n");
6773 qctl->qc_type = qtype;
6776 if (strncmp(optarg, NOTIFY_GRACE,
6777 strlen(NOTIFY_GRACE)) == 0) {
6778 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6780 dqi->dqi_bgrace = str2sec(optarg);
6781 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6783 "error: bad block-grace: %s\n",
6788 dqb->dqb_valid |= QIF_BTIME;
6791 if (strncmp(optarg, NOTIFY_GRACE,
6792 strlen(NOTIFY_GRACE)) == 0) {
6793 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6795 dqi->dqi_igrace = str2sec(optarg);
6796 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6798 "error: bad inode-grace: %s\n",
6803 dqb->dqb_valid |= QIF_ITIME;
6805 case 't': /* Yes, of course! */
6808 if (lfs_verify_poolarg(optarg))
6811 "Trying to set grace for pool %s\n", optarg);
6812 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
6813 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
6815 /* getopt prints error message for us when opterr != 0 */
6821 if (qctl->qc_type == ALLQUOTA) {
6822 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6826 if (optind != argc - 1) {
6827 fprintf(stderr, "error: unexpected parameters encountered\n");
6832 rc = llapi_quotactl(mnt, qctl);
6835 fprintf(stderr, "%s %s ", obd_type,
6836 obd_uuid2str(&qctl->obd_uuid));
6837 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6844 #define BSLIMIT (1 << 0)
6845 #define BHLIMIT (1 << 1)
6846 #define ISLIMIT (1 << 2)
6847 #define IHLIMIT (1 << 3)
6849 int lfs_setquota(int argc, char **argv)
6852 struct if_quotactl *qctl;
6853 char *mnt, *obd_type;
6854 struct obd_dqblk *dqb;
6855 struct option long_opts[] = {
6856 { .val = 'b', .name = "block-softlimit",
6857 .has_arg = required_argument },
6858 { .val = 'B', .name = "block-hardlimit",
6859 .has_arg = required_argument },
6860 { .val = 'd', .name = "default", .has_arg = no_argument },
6861 { .val = 'g', .name = "group", .has_arg = required_argument },
6862 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6863 { .val = 'i', .name = "inode-softlimit",
6864 .has_arg = required_argument },
6865 { .val = 'I', .name = "inode-hardlimit",
6866 .has_arg = required_argument },
6867 { .val = 'p', .name = "projid", .has_arg = required_argument },
6868 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6869 { .val = 'u', .name = "user", .has_arg = required_argument },
6870 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6871 { .val = LFS_POOL_OPT,
6872 .name = "pool", .has_arg = required_argument },
6874 unsigned int limit_mask = 0;
6875 bool use_default = false;
6876 int qtype, qctl_len;
6878 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
6879 qctl = malloc(qctl_len);
6883 memset(qctl, 0, qctl_len);
6884 obd_type = (char *)qctl->obd_type;
6885 dqb = &qctl->qc_dqblk;
6887 if (has_times_option(argc, argv)) {
6888 rc = lfs_setquota_times(argc, argv, qctl);
6892 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
6893 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6894 * so it can be used as a marker that qc_type
6895 * isn't reinitialized from command line
6898 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6899 long_opts, NULL)) != -1) {
6902 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6905 goto quota_type_def;
6908 rc = name2uid(&qctl->qc_id, optarg);
6911 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6914 goto quota_type_def;
6917 rc = name2gid(&qctl->qc_id, optarg);
6920 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6923 goto quota_type_def;
6926 rc = name2projid(&qctl->qc_id, optarg);
6929 if (str2quotaid(&qctl->qc_id, optarg)) {
6931 "%s setquota: invalid id '%s'\n",
6938 if (qctl->qc_id == 0) {
6940 "%s setquota: can't set quota for root usr/group/project.\n",
6947 if (qctl->qc_type != ALLQUOTA) {
6949 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
6954 qctl->qc_type = qtype;
6957 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6961 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6962 dqb->dqb_bsoftlimit >>= 10;
6963 limit_mask |= BSLIMIT;
6964 if (dqb->dqb_bsoftlimit &&
6965 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6967 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6969 (unsigned long long)dqb->dqb_bsoftlimit,
6973 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6974 dqb->dqb_bhardlimit >>= 10;
6975 limit_mask |= BHLIMIT;
6976 if (dqb->dqb_bhardlimit &&
6977 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6979 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6980 "See '%s help setquota' or Lustre manual for details\n",
6982 (unsigned long long)dqb->dqb_bhardlimit,
6986 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6987 limit_mask |= ISLIMIT;
6988 if (dqb->dqb_isoftlimit &&
6989 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6991 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6993 (unsigned long long)dqb->dqb_isoftlimit,
6997 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6998 limit_mask |= IHLIMIT;
6999 if (dqb->dqb_ihardlimit &&
7000 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7002 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7004 (unsigned long long)dqb->dqb_ihardlimit,
7008 if (lfs_verify_poolarg(optarg)) {
7013 "Trying to set quota for pool %s\n", optarg);
7014 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7015 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7019 "%s setquota: unrecognized option '%s'\n",
7020 progname, argv[optind - 1]);
7026 if (qctl->qc_type == ALLQUOTA) {
7028 "%s setquota: either -u or -g must be specified\n",
7034 if (!use_default && limit_mask == 0) {
7036 "%s setquota: at least one limit must be specified\n",
7042 if (use_default && limit_mask != 0) {
7044 "%s setquota: limits should not be specified when using default quota\n",
7050 if (use_default && qctl->qc_id == 0) {
7052 "%s setquota: can not set default quota for root user/group/project\n",
7058 if (optind != argc - 1) {
7060 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7061 progname, argv[optind]);
7069 dqb->dqb_bhardlimit = 0;
7070 dqb->dqb_bsoftlimit = 0;
7071 dqb->dqb_ihardlimit = 0;
7072 dqb->dqb_isoftlimit = 0;
7075 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7076 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7077 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7078 /* sigh, we can't just set blimits/ilimits */
7079 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7080 .qc_type = qctl->qc_type,
7081 .qc_id = qctl->qc_id};
7083 rc = llapi_quotactl(mnt, &tmp_qctl);
7087 if (!(limit_mask & BHLIMIT))
7088 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7089 if (!(limit_mask & BSLIMIT))
7090 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7091 if (!(limit_mask & IHLIMIT))
7092 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7093 if (!(limit_mask & ISLIMIT))
7094 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7096 /* Keep grace times if we have got no softlimit arguments */
7097 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7098 dqb->dqb_valid |= QIF_BTIME;
7099 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7102 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7103 dqb->dqb_valid |= QIF_ITIME;
7104 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7108 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7109 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7111 rc = llapi_quotactl(mnt, qctl);
7115 "%s setquota: cannot quotactl '%s' '%s': %s",
7117 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7124 /* Converts seconds value into format string
7125 * result is returned in buf
7127 * 1. result is in descenting order: 1w2d3h4m5s
7128 * 2. zero fields are not filled (except for p. 3): 5d1s
7129 * 3. zero seconds value is presented as "0s"
7131 static char *__sec2str(time_t seconds, char *buf)
7133 const char spec[] = "smhdw";
7134 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7139 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7140 c = seconds / mult[i];
7142 if (c > 0 || (i == 0 && buf == tail))
7143 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7152 static void sec2str(time_t seconds, char *buf, int rc)
7159 tail = __sec2str(seconds, tail);
7161 if (rc && tail - buf < 39) {
7167 static void diff2str(time_t seconds, char *buf, time_t now)
7172 if (seconds <= now) {
7173 strcpy(buf, "none");
7176 __sec2str(seconds - now, buf);
7179 static void print_quota_title(char *name, struct if_quotactl *qctl,
7180 bool human_readable, bool show_default)
7183 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7184 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7185 "Filesystem", "bquota", "blimit", "bgrace",
7186 "iquota", "ilimit", "igrace");
7188 printf("Disk quotas for %s %s (%cid %u):\n",
7189 qtype_name(qctl->qc_type), name,
7190 *qtype_name(qctl->qc_type), qctl->qc_id);
7191 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7192 "Filesystem", human_readable ? "used" : "kbytes",
7193 "quota", "limit", "grace",
7194 "files", "quota", "limit", "grace");
7198 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7201 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7204 snprintf(buf, buflen, "%5.4gP",
7205 (double)num / ((__u64)1 << 40));
7207 snprintf(buf, buflen, "%5.4gT",
7208 (double)num / (1 << 30));
7210 snprintf(buf, buflen, "%5.4gG",
7211 (double)num / (1 << 20));
7213 snprintf(buf, buflen, "%5.4gM",
7214 (double)num / (1 << 10));
7216 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7220 #define STRBUF_LEN 32
7221 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7222 int rc, bool h, bool show_default)
7228 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7229 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7230 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7231 int bover = 0, iover = 0;
7232 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7233 char numbuf[3][STRBUF_LEN];
7235 char strbuf[STRBUF_LEN];
7237 if (dqb->dqb_bhardlimit &&
7238 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7240 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7241 if (dqb->dqb_btime > now)
7247 if (dqb->dqb_ihardlimit &&
7248 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7250 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7251 if (dqb->dqb_itime > now)
7257 if (strlen(mnt) > 15)
7258 printf("%s\n%15s", mnt, "");
7260 printf("%15s", mnt);
7263 diff2str(dqb->dqb_btime, timebuf, now);
7264 else if (show_default)
7265 snprintf(timebuf, sizeof(timebuf), "%llu",
7266 (unsigned long long)dqb->dqb_btime);
7268 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7269 strbuf, sizeof(strbuf), h);
7270 if (rc == -EREMOTEIO)
7271 sprintf(numbuf[0], "%s*", strbuf);
7273 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7274 "%s" : "[%s]", strbuf);
7276 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7277 if (type == QC_GENERAL)
7278 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7279 "%s" : "[%s]", strbuf);
7281 sprintf(numbuf[1], "%s", "-");
7283 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7284 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7285 "%s" : "[%s]", strbuf);
7288 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7290 printf(" %7s%c %6s %7s %7s",
7291 numbuf[0], bover ? '*' : ' ', numbuf[1],
7292 numbuf[2], bover > 1 ? timebuf : "-");
7295 diff2str(dqb->dqb_itime, timebuf, now);
7296 else if (show_default)
7297 snprintf(timebuf, sizeof(timebuf), "%llu",
7298 (unsigned long long)dqb->dqb_itime);
7300 snprintf(numbuf[0], sizeof(numbuf),
7301 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7302 (uintmax_t)dqb->dqb_curinodes);
7304 if (type == QC_GENERAL)
7305 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7307 (uintmax_t)dqb->dqb_isoftlimit);
7309 sprintf(numbuf[1], "%s", "-");
7311 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7312 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7315 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7316 else if (type != QC_OSTIDX)
7317 printf(" %7s%c %6s %7s %7s",
7318 numbuf[0], iover ? '*' : ' ', numbuf[1],
7319 numbuf[2], iover > 1 ? timebuf : "-");
7321 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7323 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7324 qctl->qc_cmd == Q_GETOINFO) {
7328 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7329 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7331 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7332 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7333 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7335 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7337 printf("Block grace time: %s; Inode grace time: %s\n",
7338 bgtimebuf, igtimebuf);
7342 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7346 /* format is "lustre-OST0001" */
7347 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7349 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7354 *idx = strtoul(dash, &endp, 16);
7355 if (*idx > 0xffff) {
7356 fprintf(stderr, "wrong index %s\n", tgtname);
7363 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7364 bool h, __u64 *total)
7366 int rc = 0, rc1 = 0, count = 0, i = 0;
7367 char **list = NULL, *buffer = NULL;
7368 __u32 valid = qctl->qc_valid;
7370 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7373 /* Is it correct for the case OST0000, OST0002, OST0003 -
7374 * we will ask OST0001 that is absent and won't ask OST0003? */
7375 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7377 fprintf(stderr, "can not get %s count: %s\n",
7378 is_mdt ? "mdt" : "ost", strerror(-rc));
7382 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7383 char fname[PATH_MAX];
7384 char fsname[LUSTRE_MAXFSNAME + 1];
7385 int bufsize = sizeof(struct obd_uuid) * count;
7387 rc = llapi_search_fsname(mnt, fsname);
7389 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7393 buffer = malloc(bufsize + sizeof(*list) * count);
7396 list = (char **)(buffer + bufsize);
7397 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7398 count = llapi_get_poolmembers(fname, list, count,
7404 for (i = 0; i < count; i++) {
7405 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7408 if (tgt_name2index(list[i], &index))
7410 qctl->qc_idx = index;
7415 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7416 rc = llapi_quotactl(mnt, qctl);
7418 /* It is remote client case. */
7419 if (rc == -EOPNOTSUPP) {
7426 fprintf(stderr, "quotactl %s%d failed.\n",
7427 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7431 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7432 qctl->qc_valid, 0, h, false);
7433 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7434 qctl->qc_dqblk.dqb_bhardlimit;
7439 qctl->qc_valid = valid;
7443 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7444 int verbose, int quiet, bool human_readable,
7447 int rc1 = 0, rc2 = 0, rc3 = 0;
7448 char *obd_type = (char *)qctl->obd_type;
7449 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7450 __u64 total_ialloc = 0, total_balloc = 0;
7451 bool use_default_for_blk = false;
7452 bool use_default_for_file = false;
7455 rc1 = llapi_quotactl(mnt, qctl);
7459 fprintf(stderr, "%s quotas are not enabled.\n",
7460 qtype_name(qctl->qc_type));
7463 fprintf(stderr, "Permission denied.\n");
7466 /* We already got error message. */
7469 fprintf(stderr, "Unexpected quotactl error: %s\n",
7474 if (!show_default && qctl->qc_id == 0) {
7475 qctl->qc_dqblk.dqb_bhardlimit = 0;
7476 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7477 qctl->qc_dqblk.dqb_ihardlimit = 0;
7478 qctl->qc_dqblk.dqb_isoftlimit = 0;
7479 qctl->qc_dqblk.dqb_btime = 0;
7480 qctl->qc_dqblk.dqb_itime = 0;
7481 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7484 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7485 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7486 use_default_for_blk = true;
7487 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7490 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7491 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7492 use_default_for_file = true;
7493 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7496 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7497 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7498 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7499 print_quota_title(name, qctl, human_readable, show_default);
7501 if (rc1 && *obd_type)
7502 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7504 if (qctl->qc_valid != QC_GENERAL)
7507 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7508 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7509 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7510 (QIF_LIMITS|QIF_USAGE));
7512 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7514 if (!show_default && verbose &&
7515 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7516 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7517 char strbuf[STRBUF_LEN];
7519 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7521 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7523 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7525 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7526 (uintmax_t)total_ialloc, strbuf);
7529 if (use_default_for_blk)
7530 printf("%cid %u is using default block quota setting\n",
7531 *qtype_name(qctl->qc_type), qctl->qc_id);
7533 if (use_default_for_file)
7534 printf("%cid %u is using default file quota setting\n",
7535 *qtype_name(qctl->qc_type), qctl->qc_id);
7537 if (rc1 || rc2 || rc3 || inacc)
7538 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7552 static int lfs_project(int argc, char **argv)
7554 int ret = 0, err = 0, c, i;
7555 struct project_handle_control phc = { 0 };
7556 enum lfs_project_ops_t op;
7559 phc.assign_projid = false;
7560 /* default action */
7561 op = LFS_PROJECT_LIST;
7563 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7566 if (op != LFS_PROJECT_LIST) {
7568 "%s: cannot specify '-c' '-C' '-s' together\n",
7573 op = LFS_PROJECT_CHECK;
7576 if (op != LFS_PROJECT_LIST) {
7578 "%s: cannot specify '-c' '-C' '-s' together\n",
7583 op = LFS_PROJECT_CLEAR;
7586 if (op != LFS_PROJECT_LIST) {
7588 "%s: cannot specify '-c' '-C' '-s' together\n",
7593 phc.set_inherit = true;
7594 op = LFS_PROJECT_SET;
7600 phc.keep_projid = true;
7603 phc.recursive = true;
7606 if (str2quotaid(&phc.projid, optarg)) {
7608 "Invalid project ID: %s\n",
7613 phc.assign_projid = true;
7617 phc.newline = false;
7620 fprintf(stderr, "%s: invalid option '%c'\n",
7626 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7627 op = LFS_PROJECT_SET;
7628 phc.set_projid = true;
7629 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7630 phc.set_projid = true;
7634 case LFS_PROJECT_CHECK:
7635 if (phc.keep_projid) {
7637 "%s: '-k' is useless together with '-c'\n",
7642 case LFS_PROJECT_CLEAR:
7645 "%s: '-0' is useless together with '-C'\n",
7649 if (phc.assign_projid) {
7651 "%s: '-p' is useless together with '-C'\n",
7656 case LFS_PROJECT_SET:
7659 "%s: '-0' is useless together with '-s'\n",
7663 if (phc.keep_projid) {
7665 "%s: '-k' is useless together with '-s'\n",
7673 "%s: '-0' is useless for list operations\n",
7683 fprintf(stderr, "%s: missing file or directory target(s)\n",
7688 for (i = 0; i < argc; i++) {
7690 case LFS_PROJECT_CHECK:
7691 err = lfs_project_check(argv[i], &phc);
7693 case LFS_PROJECT_LIST:
7694 err = lfs_project_list(argv[i], &phc);
7696 case LFS_PROJECT_CLEAR:
7697 err = lfs_project_clear(argv[i], &phc);
7699 case LFS_PROJECT_SET:
7700 err = lfs_project_set(argv[i], &phc);
7712 static int lfs_quota(int argc, char **argv)
7715 char *mnt, *name = NULL;
7716 struct if_quotactl *qctl;
7718 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7719 __u32 valid = QC_GENERAL, idx = 0;
7720 bool human_readable = false;
7721 bool show_default = false;
7723 struct option long_opts[] = {
7724 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
7727 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7731 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7732 qctl->qc_type = ALLQUOTA;
7733 obd_uuid = (char *)qctl->obd_uuid.uuid;
7735 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7736 long_opts, NULL)) != -1) {
7739 show_default = true;
7744 show_default = true;
7749 show_default = true;
7753 if (qctl->qc_type != ALLQUOTA) {
7755 "%s quota: only one of -u, -g, or -p may be specified\n",
7760 qctl->qc_type = qtype;
7763 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7766 valid = qctl->qc_valid = QC_UUID;
7767 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
7770 valid = qctl->qc_valid = QC_MDTIDX;
7771 idx = qctl->qc_idx = atoi(optarg);
7772 if (idx == 0 && *optarg != '0') {
7774 "%s quota: invalid MDT index '%s'\n",
7781 valid = qctl->qc_valid = QC_OSTIDX;
7782 idx = qctl->qc_idx = atoi(optarg);
7783 if (idx == 0 && *optarg != '0') {
7785 "%s quota: invalid OST index '%s'\n",
7798 human_readable = true;
7801 if (lfs_verify_poolarg(optarg)) {
7805 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7806 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
7807 LUSTRE_Q_GETINFOPOOL :
7808 LUSTRE_Q_GETQUOTAPOOL;
7811 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7812 progname, argv[optind - 1]);
7818 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7819 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7820 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7821 qctl->qc_type == ALLQUOTA &&
7822 optind == argc - 1 && !show_default) {
7825 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7826 qctl->qc_type = qtype;
7827 qctl->qc_valid = valid;
7828 if (qtype == USRQUOTA) {
7829 qctl->qc_id = geteuid();
7830 rc = uid2name(&name, qctl->qc_id);
7832 qctl->qc_id = getegid();
7833 rc = gid2name(&name, qctl->qc_id);
7834 memset(&qctl->qc_dqblk, 0,
7835 sizeof(qctl->qc_dqblk));
7840 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
7841 human_readable, show_default);
7846 /* lfs quota -u username /path/to/lustre/mount */
7847 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7848 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7849 /* options should be followed by u/g-name and mntpoint */
7850 if ((!show_default && optind + 2 != argc) ||
7851 (show_default && optind + 1 != argc) ||
7852 qctl->qc_type == ALLQUOTA) {
7854 "%s quota: name and mount point must be specified\n",
7860 if (!show_default) {
7861 name = argv[optind++];
7862 switch (qctl->qc_type) {
7864 rc = name2uid(&qctl->qc_id, name);
7867 rc = name2gid(&qctl->qc_id, name);
7870 rc = name2projid(&qctl->qc_id, name);
7877 qctl->qc_valid = QC_GENERAL;
7878 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
7883 if (str2quotaid(&qctl->qc_id, name)) {
7884 fprintf(stderr, "%s quota: invalid id '%s'\n",
7890 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
7891 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7898 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
7899 human_readable, show_default);
7904 #endif /* HAVE_SYS_QUOTA_H! */
7906 static int flushctx_ioctl(char *mp)
7910 fd = open(mp, O_RDONLY);
7912 fprintf(stderr, "flushctx: error open %s: %s\n",
7913 mp, strerror(errno));
7917 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7919 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7920 mp, strerror(errno));
7926 static int lfs_flushctx(int argc, char **argv)
7928 int kdestroy = 0, c;
7929 char mntdir[PATH_MAX] = {'\0'};
7933 while ((c = getopt(argc, argv, "k")) != -1) {
7940 "error: %s: option '-%c' unrecognized\n",
7947 rc = system("kdestroy > /dev/null");
7949 rc = WEXITSTATUS(rc);
7951 "error destroying tickets: %d, continuing\n",
7956 if (optind >= argc) {
7957 /* flush for all mounted lustre fs. */
7958 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7959 /* Check if we have a mount point */
7960 if (mntdir[0] == '\0')
7963 if (flushctx_ioctl(mntdir))
7966 mntdir[0] = '\0'; /* avoid matching in next loop */
7969 /* flush fs as specified */
7970 while (optind < argc) {
7971 if (flushctx_ioctl(argv[optind++]))
7978 static int lfs_changelog(int argc, char **argv)
7980 void *changelog_priv;
7981 struct changelog_rec *rec;
7982 long long startrec = 0, endrec = 0;
7984 struct option long_opts[] = {
7985 { .val = 'f', .name = "follow", .has_arg = no_argument },
7987 char short_opts[] = "f";
7990 while ((rc = getopt_long(argc, argv, short_opts,
7991 long_opts, NULL)) != -1) {
7998 "%s changelog: unrecognized option '%s'\n",
7999 progname, argv[optind - 1]);
8003 if (optind >= argc) {
8004 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8009 mdd = argv[optind++];
8011 startrec = strtoll(argv[optind++], NULL, 10);
8013 endrec = strtoll(argv[optind++], NULL, 10);
8015 rc = llapi_changelog_start(&changelog_priv,
8016 CHANGELOG_FLAG_BLOCK |
8017 CHANGELOG_FLAG_JOBID |
8018 CHANGELOG_FLAG_EXTRA_FLAGS |
8019 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8022 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8023 progname, strerror(errno = -rc));
8027 rc = llapi_changelog_set_xflags(changelog_priv,
8028 CHANGELOG_EXTRA_FLAG_UIDGID |
8029 CHANGELOG_EXTRA_FLAG_NID |
8030 CHANGELOG_EXTRA_FLAG_OMODE |
8031 CHANGELOG_EXTRA_FLAG_XATTR);
8034 "%s changelog: cannot set xflags for changelog: %s\n",
8035 progname, strerror(errno = -rc));
8039 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8043 if (endrec && rec->cr_index > endrec) {
8044 llapi_changelog_free(&rec);
8047 if (rec->cr_index < startrec) {
8048 llapi_changelog_free(&rec);
8052 secs = rec->cr_time >> 30;
8053 gmtime_r(&secs, &ts);
8054 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8055 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8056 changelog_type2str(rec->cr_type),
8057 ts.tm_hour, ts.tm_min, ts.tm_sec,
8058 (int)(rec->cr_time & ((1 << 30) - 1)),
8059 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8060 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8062 if (rec->cr_flags & CLF_JOBID) {
8063 struct changelog_ext_jobid *jid =
8064 changelog_rec_jobid(rec);
8066 if (jid->cr_jobid[0] != '\0')
8067 printf(" j=%s", jid->cr_jobid);
8070 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8071 struct changelog_ext_extra_flags *ef =
8072 changelog_rec_extra_flags(rec);
8074 printf(" ef=0x%llx",
8075 (unsigned long long)ef->cr_extra_flags);
8077 if (ef->cr_extra_flags & CLFE_UIDGID) {
8078 struct changelog_ext_uidgid *uidgid =
8079 changelog_rec_uidgid(rec);
8081 printf(" u=%llu:%llu",
8082 (unsigned long long)uidgid->cr_uid,
8083 (unsigned long long)uidgid->cr_gid);
8085 if (ef->cr_extra_flags & CLFE_NID) {
8086 struct changelog_ext_nid *nid =
8087 changelog_rec_nid(rec);
8090 libcfs_nid2str(nid->cr_nid));
8093 if (ef->cr_extra_flags & CLFE_OPEN) {
8094 struct changelog_ext_openmode *omd =
8095 changelog_rec_openmode(rec);
8096 char mode[] = "---";
8098 /* exec mode must be exclusive */
8099 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8102 if (omd->cr_openflags & MDS_FMODE_READ)
8104 if (omd->cr_openflags &
8111 if (strcmp(mode, "---") != 0)
8112 printf(" m=%s", mode);
8115 if (ef->cr_extra_flags & CLFE_XATTR) {
8116 struct changelog_ext_xattr *xattr =
8117 changelog_rec_xattr(rec);
8119 if (xattr->cr_xattr[0] != '\0')
8120 printf(" x=%s", xattr->cr_xattr);
8124 if (!fid_is_zero(&rec->cr_pfid))
8125 printf(" p="DFID, PFID(&rec->cr_pfid));
8126 if (rec->cr_namelen)
8127 printf(" %.*s", rec->cr_namelen,
8128 changelog_rec_name(rec));
8130 if (rec->cr_flags & CLF_RENAME) {
8131 struct changelog_ext_rename *rnm =
8132 changelog_rec_rename(rec);
8134 if (!fid_is_zero(&rnm->cr_sfid))
8135 printf(" s="DFID" sp="DFID" %.*s",
8136 PFID(&rnm->cr_sfid),
8137 PFID(&rnm->cr_spfid),
8138 (int)changelog_rec_snamelen(rec),
8139 changelog_rec_sname(rec));
8143 llapi_changelog_free(&rec);
8146 llapi_changelog_fini(&changelog_priv);
8149 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8150 progname, strerror(errno = -rc));
8152 return (rc == 1 ? 0 : rc);
8155 static int lfs_changelog_clear(int argc, char **argv)
8163 endrec = strtoll(argv[3], NULL, 10);
8165 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8168 fprintf(stderr, "%s: record out of range: %llu\n",
8170 else if (rc == -ENOENT)
8171 fprintf(stderr, "%s: no changelog user: %s\n",
8174 fprintf(stderr, "%s error: %s\n", argv[0],
8183 static int lfs_fid2path(int argc, char **argv)
8185 struct option long_opts[] = {
8186 { .val = 'c', .name = "cur", .has_arg = no_argument },
8187 { .val = 'l', .name = "link", .has_arg = required_argument },
8188 { .val = 'r', .name = "rec", .has_arg = required_argument },
8190 char short_opts[] = "cl:r:";
8191 char mntdir[PATH_MAX];
8192 char *device, *fid, *path, *rootpath;
8193 long long recno = -1;
8198 char *endptr = NULL;
8200 while ((rc = getopt_long(argc, argv, short_opts,
8201 long_opts, NULL)) != -1) {
8207 linkno = strtol(optarg, &endptr, 10);
8208 if (*endptr != '\0') {
8210 "%s fid2path: invalid linkno '%s'\n",
8216 recno = strtoll(optarg, &endptr, 10);
8217 if (*endptr != '\0') {
8219 "%s fid2path: invalid recno '%s'\n",
8226 "%s fid2path: unrecognized option '%s'\n",
8227 progname, argv[optind - 1]);
8234 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
8239 device = argv[optind++];
8240 path = calloc(1, PATH_MAX);
8244 "%s fid2path: cannot allocate memory for path: %s\n",
8245 progname, strerror(-rc));
8250 /* in case that device is not the mountpoint */
8251 if (device[0] == '/') {
8252 rc = llapi_search_mounts(device, 0, mntdir, NULL);
8257 "%s fid2path: %s has no mountpoint: %s\n",
8258 progname, device, strerror(-rc));
8262 while (optind < argc) {
8263 fid = argv[optind++];
8265 lnktmp = (linkno >= 0) ? linkno : 0;
8267 int oldtmp = lnktmp;
8268 long long rectmp = recno;
8271 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
8275 "%s fid2path: cannot find %s %s: %s\n",
8276 progname, device, fid,
8277 strerror(errno = -rc2));
8284 fprintf(stdout, "%lld ", rectmp);
8285 if (device[0] == '/') {
8286 fprintf(stdout, "%s", rootpath);
8287 if (rootpath[strlen(rootpath) - 1] != '/')
8288 fprintf(stdout, "/");
8289 } else if (path[0] == '\0') {
8290 fprintf(stdout, "/");
8292 fprintf(stdout, "%s\n", path);
8295 /* specified linkno */
8297 if (oldtmp == lnktmp)
8307 static int lfs_path2fid(int argc, char **argv)
8309 struct option long_opts[] = {
8310 { .val = 'p', .name = "parents", .has_arg = no_argument },
8313 const char short_opts[] = "p";
8314 const char *sep = "";
8317 bool show_parents = false;
8319 while ((rc = getopt_long(argc, argv, short_opts,
8320 long_opts, NULL)) != -1) {
8323 show_parents = true;
8327 "%s path2fid: unrecognized option '%s'\n",
8328 progname, argv[optind - 1]);
8333 if (optind > argc - 1) {
8334 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8337 } else if (optind < argc - 1) {
8342 for (path = argv + optind; *path != NULL; path++) {
8345 if (!show_parents) {
8346 err = llapi_path2fid(*path, &fid);
8348 printf("%s%s"DFID"\n",
8349 *sep != '\0' ? *path : "", sep,
8352 char name[NAME_MAX + 1];
8353 unsigned int linkno = 0;
8355 while ((err = llapi_path2parent(*path, linkno, &fid,
8356 name, sizeof(name))) == 0) {
8357 if (*sep != '\0' && linkno == 0)
8358 printf("%s%s", *path, sep);
8360 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8365 /* err == -ENODATA is end-of-loop */
8366 if (linkno > 0 && err == -ENODATA) {
8374 "%s path2fid: cannot get %sfid for '%s': %s\n",
8375 progname, show_parents ? "parent " : "", *path,
8387 #define MAX_ERRNO 4095
8388 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8390 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8394 rc = llapi_rmfid(device, fa);
8396 fprintf(stderr, "rmfid(): rc = %d\n", rc);
8400 for (k = 0; k < fa->fa_nr; k++) {
8401 rc = (__s32)fa->fa_fids[k].f_ver;
8402 if (!IS_ERR_VALUE(rc))
8408 fa->fa_fids[k].f_ver = 0;
8409 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
8410 PFID(&fa->fa_fids[k]), rc);
8416 static int lfs_rmfid(int argc, char **argv)
8418 char *fidstr, *device;
8419 int rc = 0, rc2, nr;
8420 struct fid_array *fa;
8422 if (optind > argc - 1) {
8423 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8427 device = argv[optind++];
8430 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8436 while (optind < argc) {
8439 fidstr = argv[optind++];
8440 while (*fidstr == '[')
8442 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8444 fprintf(stderr, "unrecognized FID: %s\n",
8449 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8450 /* start another batch */
8451 rc2 = lfs_rmfid_and_show_errors(device, fa);
8458 rc2 = lfs_rmfid_and_show_errors(device, fa);
8466 static int lfs_data_version(int argc, char **argv)
8473 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8476 fprintf(stderr, "%s data_version: FILE must be specified\n",
8481 while ((c = getopt(argc, argv, "nrw")) != -1) {
8484 data_version_flags = 0;
8487 data_version_flags |= LL_DV_RD_FLUSH;
8490 data_version_flags |= LL_DV_WR_FLUSH;
8494 "%s data_version: unrecognized option '%s'\n",
8495 progname, argv[optind - 1]);
8499 if (optind == argc) {
8500 fprintf(stderr, "%s data_version: FILE must be specified\n",
8505 path = argv[optind];
8506 fd = open(path, O_RDONLY);
8509 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8510 progname, path, strerror(-rc));
8514 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8517 "%s data_version: cannot get version for '%s': %s\n",
8518 progname, path, strerror(-rc));
8520 printf("%ju" "\n", (uintmax_t)data_version);
8526 static int lfs_hsm_state(int argc, char **argv)
8531 struct hsm_user_state hus;
8539 rc = llapi_hsm_state_get(path, &hus);
8541 fprintf(stderr, "can't get hsm state for %s: %s\n",
8542 path, strerror(errno = -rc));
8546 /* Display path name and status flags */
8547 printf("%s: (0x%08x)", path, hus.hus_states);
8549 if (hus.hus_states & HS_RELEASED)
8550 printf(" released");
8551 if (hus.hus_states & HS_EXISTS)
8553 if (hus.hus_states & HS_DIRTY)
8555 if (hus.hus_states & HS_ARCHIVED)
8556 printf(" archived");
8557 /* Display user-settable flags */
8558 if (hus.hus_states & HS_NORELEASE)
8559 printf(" never_release");
8560 if (hus.hus_states & HS_NOARCHIVE)
8561 printf(" never_archive");
8562 if (hus.hus_states & HS_LOST)
8563 printf(" lost_from_hsm");
8565 if (hus.hus_archive_id != 0)
8566 printf(", archive_id:%d", hus.hus_archive_id);
8569 } while (++i < argc);
8574 #define LFS_HSM_SET 0
8575 #define LFS_HSM_CLEAR 1
8578 * Generic function to set or clear HSM flags.
8579 * Used by hsm_set and hsm_clear.
8581 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8583 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8585 struct option long_opts[] = {
8586 { .val = 'A', .name = "archived", .has_arg = no_argument },
8587 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8588 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8589 { .val = 'e', .name = "exists", .has_arg = no_argument },
8590 { .val = 'l', .name = "lost", .has_arg = no_argument },
8591 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8592 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8594 char short_opts[] = "lraAdei:";
8598 __u32 archive_id = 0;
8604 while ((c = getopt_long(argc, argv, short_opts,
8605 long_opts, NULL)) != -1) {
8611 mask |= HS_NOARCHIVE;
8614 mask |= HS_ARCHIVED;
8617 mask |= HS_NORELEASE;
8626 archive_id = strtol(optarg, &end, 10);
8628 fprintf(stderr, "invalid archive_id: '%s'\n",
8636 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8637 argv[0], argv[optind - 1]);
8642 /* User should have specified a flag */
8646 while (optind < argc) {
8647 path = argv[optind];
8649 /* If mode == 0, this means we apply the mask. */
8650 if (mode == LFS_HSM_SET)
8651 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8653 rc = llapi_hsm_state_set(path, 0, mask, 0);
8656 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8657 path, strerror(errno = -rc));
8666 static int lfs_hsm_action(int argc, char **argv)
8671 struct hsm_current_action hca;
8672 struct hsm_extent he;
8673 enum hsm_user_action hua;
8674 enum hsm_progress_states hps;
8682 rc = llapi_hsm_current_action(path, &hca);
8684 fprintf(stderr, "can't get hsm action for %s: %s\n",
8685 path, strerror(errno = -rc));
8688 he = hca.hca_location;
8689 hua = hca.hca_action;
8690 hps = hca.hca_state;
8692 printf("%s: %s", path, hsm_user_action2name(hua));
8694 /* Skip file without action */
8695 if (hca.hca_action == HUA_NONE) {
8700 printf(" %s ", hsm_progress_state2name(hps));
8702 if ((hps == HPS_RUNNING) &&
8703 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8704 printf("(%llu bytes moved)\n",
8705 (unsigned long long)he.length);
8706 else if ((he.offset + he.length) == LUSTRE_EOF)
8707 printf("(from %llu to EOF)\n",
8708 (unsigned long long)he.offset);
8710 printf("(from %llu to %llu)\n",
8711 (unsigned long long)he.offset,
8712 (unsigned long long)(he.offset + he.length));
8714 } while (++i < argc);
8719 static int lfs_hsm_set(int argc, char **argv)
8721 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8724 static int lfs_hsm_clear(int argc, char **argv)
8726 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8730 * Check file state and return its fid, to be used by lfs_hsm_request().
8732 * \param[in] file Path to file to check
8733 * \param[in,out] fid Pointer to allocated lu_fid struct.
8734 * \param[in,out] last_dev Pointer to last device id used.
8736 * \return 0 on success.
8738 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8744 rc = lstat(file, &st);
8746 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8750 * Checking for regular file as archiving as posix copytool
8751 * rejects archiving files other than regular files
8753 if (!S_ISREG(st.st_mode)) {
8754 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8757 /* A request should be ... */
8758 if (*last_dev != st.st_dev && *last_dev != 0) {
8760 "All files should be on the same filesystem: %s\n",
8764 *last_dev = st.st_dev;
8766 rc = llapi_path2fid(file, fid);
8768 fprintf(stderr, "Cannot read FID of %s: %s\n",
8769 file, strerror(-rc));
8775 /* Fill an HSM HUR item with a given file name.
8777 * If mntpath is set, then the filename is actually a FID, and no
8778 * lookup on the filesystem will be performed.
8780 * \param[in] hur the user request to fill
8781 * \param[in] idx index of the item inside the HUR to fill
8782 * \param[in] mntpath mountpoint of Lustre
8783 * \param[in] fname filename (if mtnpath is NULL)
8784 * or FID (if mntpath is set)
8785 * \param[in] last_dev pointer to last device id used
8787 * \retval 0 on success
8788 * \retval CMD_HELP or a negative errno on error
8790 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8791 const char *mntpath, const char *fname,
8794 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8797 hui->hui_extent.length = -1;
8800 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8802 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8805 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8809 hur->hur_request.hr_itemcount++;
8814 static int lfs_hsm_request(int argc, char **argv, int action)
8816 struct option long_opts[] = {
8817 { .val = 'a', .name = "archive", .has_arg = required_argument },
8818 { .val = 'D', .name = "data", .has_arg = required_argument },
8819 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8820 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8823 char short_opts[] = "l:D:a:m:";
8824 struct hsm_user_request *hur, *oldhur;
8829 char *filelist = NULL;
8830 char fullpath[PATH_MAX];
8831 char *opaque = NULL;
8835 int nbfile_alloc = 0;
8836 char *some_file = NULL;
8837 char *mntpath = NULL;
8843 while ((c = getopt_long(argc, argv, short_opts,
8844 long_opts, NULL)) != -1) {
8853 if (action != HUA_ARCHIVE &&
8854 action != HUA_REMOVE) {
8856 "error: -a is supported only when archiving or removing\n");
8859 archive_id = atoi(optarg);
8864 some_file = strdup(optarg);
8870 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8871 argv[0], argv[optind - 1]);
8876 /* All remaining args are files, so we have at least nbfile */
8877 nbfile = argc - optind;
8879 if ((nbfile == 0) && (!filelist))
8883 opaque_len = strlen(opaque);
8886 * Alloc the request structure with enough place to store all files
8887 * from command line.
8889 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8891 fprintf(stderr, "Cannot create the request: %s\n",
8895 nbfile_alloc = nbfile;
8897 hur->hur_request.hr_action = action;
8898 hur->hur_request.hr_archive_id = archive_id;
8899 hur->hur_request.hr_flags = 0;
8901 /* All remaining args are files, add them */
8902 if (nbfile != 0 && some_file == NULL)
8903 some_file = strdup(argv[optind]);
8905 for (i = 0; i < nbfile; i++) {
8906 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8912 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8914 /* If a filelist was specified, read the filelist from it. */
8916 fp = fopen(filelist, "r");
8918 fprintf(stderr, "Cannot read the file list %s: %s\n",
8919 filelist, strerror(errno));
8924 while ((rc = getline(&line, &len, fp)) != -1) {
8926 * If allocated buffer was too small, get something
8929 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8932 nbfile_alloc = nbfile_alloc * 2 + 1;
8934 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8938 "hsm: cannot allocate the request: %s\n",
8945 size = hur_len(oldhur);
8948 "hsm: cannot allocate %u files + %u bytes data\n",
8949 oldhur->hur_request.hr_itemcount,
8950 oldhur->hur_request.hr_data_len);
8957 memcpy(hur, oldhur, size);
8962 if (line[strlen(line) - 1] == '\n')
8963 line[strlen(line) - 1] = '\0';
8965 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8966 mntpath, line, &last_dev);
8982 /* If a --data was used, add it to the request */
8983 hur->hur_request.hr_data_len = opaque_len;
8985 memcpy(hur_data(hur), opaque, opaque_len);
8987 /* Send the HSM request */
8988 if (realpath(some_file, fullpath) == NULL) {
8989 fprintf(stderr, "Could not find path '%s': %s\n",
8990 some_file, strerror(errno));
8992 rc = llapi_hsm_request(fullpath, hur);
8994 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8995 some_file, strerror(-rc));
9005 static int lfs_hsm_archive(int argc, char **argv)
9007 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9010 static int lfs_hsm_restore(int argc, char **argv)
9012 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9015 static int lfs_hsm_release(int argc, char **argv)
9017 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9020 static int lfs_hsm_remove(int argc, char **argv)
9022 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9025 static int lfs_hsm_cancel(int argc, char **argv)
9027 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9030 static int lfs_swap_layouts(int argc, char **argv)
9035 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9036 SWAP_LAYOUTS_KEEP_MTIME |
9037 SWAP_LAYOUTS_KEEP_ATIME);
9040 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9042 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9044 int lfs_get_mode(const char *string)
9046 enum lock_mode_user mode;
9048 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9049 if (lock_mode_names[mode] == NULL)
9051 if (strcmp(string, lock_mode_names[mode]) == 0)
9058 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9060 enum lu_ladvise_type advice;
9063 advice < ARRAY_SIZE(ladvise_names); advice++) {
9064 if (ladvise_names[advice] == NULL)
9066 if (strcmp(string, ladvise_names[advice]) == 0)
9070 return LU_LADVISE_INVALID;
9073 static int lfs_ladvise(int argc, char **argv)
9075 struct option long_opts[] = {
9076 { .val = 'a', .name = "advice", .has_arg = required_argument },
9077 { .val = 'b', .name = "background", .has_arg = no_argument },
9078 { .val = 'e', .name = "end", .has_arg = required_argument },
9079 { .val = 'l', .name = "length", .has_arg = required_argument },
9080 { .val = 'm', .name = "mode", .has_arg = required_argument },
9081 { .val = 's', .name = "start", .has_arg = required_argument },
9082 { .val = 'u', .name = "unset", .has_arg = no_argument },
9084 char short_opts[] = "a:be:l:m:s:u";
9089 struct llapi_lu_ladvise advice;
9090 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9091 unsigned long long start = 0;
9092 unsigned long long end = LUSTRE_EOF;
9093 unsigned long long length = 0;
9094 unsigned long long size_units;
9095 unsigned long long flags = 0;
9099 while ((c = getopt_long(argc, argv, short_opts,
9100 long_opts, NULL)) != -1) {
9103 advice_type = lfs_get_ladvice(optarg);
9104 if (advice_type == LU_LADVISE_INVALID) {
9106 "%s: invalid advice type '%s'\n",
9108 fprintf(stderr, "Valid types:");
9110 for (advice_type = 0;
9111 advice_type < ARRAY_SIZE(ladvise_names);
9113 if (ladvise_names[advice_type] == NULL)
9115 fprintf(stderr, " %s",
9116 ladvise_names[advice_type]);
9118 fprintf(stderr, "\n");
9131 rc = llapi_parse_size(optarg, &end,
9134 fprintf(stderr, "%s: bad end offset '%s'\n",
9141 rc = llapi_parse_size(optarg, &start,
9145 "%s: bad start offset '%s'\n",
9152 rc = llapi_parse_size(optarg, &length,
9155 fprintf(stderr, "%s: bad length '%s'\n",
9161 mode = lfs_get_mode(optarg);
9164 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9172 fprintf(stderr, "%s: option '%s' unrecognized\n",
9173 argv[0], argv[optind - 1]);
9178 if (advice_type == LU_LADVISE_INVALID) {
9179 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9180 fprintf(stderr, "Valid types:");
9181 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9183 if (ladvise_names[advice_type] == NULL)
9185 fprintf(stderr, " %s", ladvise_names[advice_type]);
9187 fprintf(stderr, "\n");
9191 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9193 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9198 if (argc <= optind) {
9199 fprintf(stderr, "%s: please give one or more file names\n",
9204 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9205 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9210 if (end == LUSTRE_EOF && length != 0)
9211 end = start + length;
9214 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9215 argv[0], start, end);
9219 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9220 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9225 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9226 fprintf(stderr, "%s: mode is required with lockahead\n",
9231 while (optind < argc) {
9234 path = argv[optind++];
9236 fd = open(path, O_RDONLY);
9238 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9239 argv[0], path, strerror(errno));
9244 advice.lla_start = start;
9245 advice.lla_end = end;
9246 advice.lla_advice = advice_type;
9247 advice.lla_value1 = 0;
9248 advice.lla_value2 = 0;
9249 advice.lla_value3 = 0;
9250 advice.lla_value4 = 0;
9251 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9252 advice.lla_lockahead_mode = mode;
9253 advice.lla_peradvice_flags = flags;
9256 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9260 "%s: cannot give advice '%s' to file '%s': %s\n",
9261 argv[0], ladvise_names[advice_type],
9262 path, strerror(errno));
9268 if (rc == 0 && rc2 < 0)
9274 static const char *const heat_names[] = LU_HEAT_NAMES;
9276 static int lfs_heat_get(int argc, char **argv)
9278 struct lu_heat *heat;
9287 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9289 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9294 while (optind < argc) {
9295 path = argv[optind++];
9297 fd = open(path, O_RDONLY);
9299 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9300 argv[0], path, strerror(errno));
9305 heat->lh_count = OBD_HEAT_COUNT;
9306 rc2 = llapi_heat_get(fd, heat);
9310 "%s: cannot get heat of file '%s': %s\n",
9311 argv[0], path, strerror(errno));
9315 printf("flags: %x\n", heat->lh_flags);
9316 for (i = 0; i < heat->lh_count; i++)
9317 printf("%s: %llu\n", heat_names[i],
9318 (unsigned long long)heat->lh_heat[i]);
9320 if (rc == 0 && rc2 < 0)
9328 static int lfs_heat_set(int argc, char **argv)
9330 struct option long_opts[] = {
9331 { .val = 'c', .name = "clear", .has_arg = no_argument },
9332 { .val = 'o', .name = "off", .has_arg = no_argument },
9333 { .val = 'O', .name = "on", .has_arg = no_argument },
9335 enum lu_heat_flag flags = 0;
9345 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9348 flags |= LU_HEAT_FLAG_CLEAR;
9351 flags |= LU_HEAT_FLAG_CLEAR;
9352 flags |= LU_HEAT_FLAG_OFF;
9355 flags &= ~LU_HEAT_FLAG_OFF;
9360 fprintf(stderr, "%s: option '%s' unrecognized\n",
9361 argv[0], argv[optind - 1]);
9366 if (argc <= optind) {
9367 fprintf(stderr, "%s: please give one or more file names\n",
9372 while (optind < argc) {
9373 path = argv[optind++];
9375 fd = open(path, O_RDONLY);
9377 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9378 argv[0], path, strerror(errno));
9383 rc2 = llapi_heat_set(fd, flags);
9387 "%s: cannot setflags heat of file '%s': %s\n",
9388 argv[0], path, strerror(errno));
9392 if (rc == 0 && rc2 < 0)
9399 * The input string contains a comma delimited list of component ids and
9400 * ranges, for example "1,2-4,7".
9402 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9404 bool end_of_loop = false;
9412 while (!end_of_loop) {
9416 char *endptr = NULL;
9419 ptr = strchrnul(arg, ',');
9420 end_of_loop = *ptr == '\0';
9423 start_index = strtol(arg, &endptr, 0);
9424 if (endptr == arg) /* no data at all */
9426 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9428 if (start_index < 0)
9431 end_index = start_index;
9432 if (*endptr == '-') {
9433 end_index = strtol(endptr + 1, &endptr, 0);
9434 if (*endptr != '\0')
9436 if (end_index < start_index)
9440 for (i = start_index; i <= end_index && size > 0; i++) {
9443 /* remove duplicate */
9444 for (j = 0; j < nr; j++) {
9448 if (j == nr) { /* no duplicate */
9454 if (size == 0 && i < end_index)
9461 if (!end_of_loop && ptr)
9464 return rc < 0 ? rc : nr;
9468 * struct verify_mirror_id - Mirror id to be verified.
9469 * @mirror_id: A specified mirror id.
9470 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9472 struct verify_mirror_id {
9478 * compare_mirror_ids() - Compare mirror ids.
9479 * @layout: Mirror component list.
9480 * @cbdata: Callback data in verify_mirror_id structure.
9482 * This is a callback function called by llapi_layout_comp_iterate()
9483 * to compare the specified mirror id with the one in the current
9484 * component of @layout. If they are the same, then the specified
9485 * mirror id is valid.
9487 * Return: a negative error code on failure or
9488 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9489 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9492 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9494 struct verify_mirror_id *mirror_id_cbdata =
9495 (struct verify_mirror_id *)cbdata;
9499 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9503 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9504 progname, strerror(errno));
9508 if (mirror_id_cbdata->mirror_id == mirror_id) {
9509 mirror_id_cbdata->is_valid_id = true;
9510 return LLAPI_LAYOUT_ITER_STOP;
9513 return LLAPI_LAYOUT_ITER_CONT;
9517 * verify_mirror_ids() - Verify specified mirror ids.
9518 * @fname: Mirrored file name.
9519 * @mirror_ids: Specified mirror ids to be verified.
9520 * @ids_nr: Number of specified mirror ids.
9522 * This function verifies that specified @mirror_ids are valid
9523 * in the mirrored file @fname.
9525 * Return: 0 on success or a negative error code on failure.
9528 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9530 struct llapi_layout *layout = NULL;
9531 struct verify_mirror_id mirror_id_cbdata = { 0 };
9542 if (stat(fname, &stbuf) < 0) {
9543 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9544 progname, fname, strerror(errno));
9549 if (!S_ISREG(stbuf.st_mode)) {
9550 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9556 fd = open(fname, O_DIRECT | O_RDONLY);
9558 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9559 progname, fname, strerror(errno));
9564 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9566 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9567 progname, fname, strerror(errno));
9571 layout = llapi_layout_get_by_fd(fd, 0);
9573 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9574 progname, fname, strerror(errno));
9576 llapi_lease_release(fd);
9580 rc = llapi_layout_flags_get(layout, &flr_state);
9582 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9583 progname, fname, strerror(errno));
9588 flr_state &= LCM_FL_FLR_MASK;
9589 switch (flr_state) {
9592 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9593 progname, fname, llapi_layout_flags_string(flr_state));
9600 for (i = 0; i < ids_nr; i++) {
9601 mirror_id_cbdata.mirror_id = mirror_ids[i];
9602 mirror_id_cbdata.is_valid_id = false;
9604 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9609 "%s: '%s' failed to verify mirror id: %u.\n",
9610 progname, fname, mirror_ids[i]);
9614 if (!mirror_id_cbdata.is_valid_id) {
9617 "%s: '%s' invalid specified mirror id: %u.\n",
9618 progname, fname, mirror_ids[i]);
9624 llapi_layout_free(layout);
9625 llapi_lease_release(fd);
9633 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9634 __u16 *mirror_ids, int ids_nr)
9636 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9637 struct llapi_layout *layout;
9648 if (stat(fname, &stbuf) < 0) {
9649 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9650 progname, fname, strerror(errno));
9654 if (!S_ISREG(stbuf.st_mode)) {
9655 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9661 fd = open(fname, O_DIRECT | O_RDWR);
9663 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9664 progname, fname, strerror(errno));
9669 layout = llapi_layout_get_by_fd(fd, 0);
9671 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9672 progname, fname, strerror(errno));
9677 rc = llapi_layout_flags_get(layout, &flr_state);
9679 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9680 progname, fname, strerror(errno));
9685 flr_state &= LCM_FL_FLR_MASK;
9686 if (flr_state == LCM_FL_NONE) {
9688 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9693 /* get stale component info */
9694 comp_size = llapi_mirror_find_stale(layout, comp_array,
9695 ARRAY_SIZE(comp_array),
9696 mirror_ids, ids_nr);
9697 if (comp_size <= 0) {
9702 ioc->lil_mode = LL_LEASE_WRLCK;
9703 ioc->lil_flags = LL_LEASE_RESYNC;
9704 rc = llapi_lease_set(fd, ioc);
9706 if (rc == -EALREADY)
9710 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9711 progname, fname, strerror(-rc));
9715 /* get the read range [start, end) */
9716 start = comp_array[0].lrc_start;
9717 end = comp_array[0].lrc_end;
9718 for (idx = 1; idx < comp_size; idx++) {
9719 if (comp_array[idx].lrc_start < start)
9720 start = comp_array[idx].lrc_start;
9721 if (end < comp_array[idx].lrc_end)
9722 end = comp_array[idx].lrc_end;
9725 rc = llapi_lease_check(fd);
9726 if (rc != LL_LEASE_WRLCK) {
9727 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9732 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9735 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9736 progname, fname, strerror(-rc));
9738 /* need to do the lease unlock even resync fails */
9739 ioc->lil_mode = LL_LEASE_UNLCK;
9740 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9742 for (idx = 0; idx < comp_size; idx++) {
9743 if (comp_array[idx].lrc_synced) {
9744 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9749 rc2 = llapi_lease_set(fd, ioc);
9751 * llapi_lease_set returns lease mode when it request to unlock
9755 /* rc2 == 0 means lost lease lock */
9756 if (rc2 == 0 && rc == 0)
9758 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9760 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9764 llapi_layout_free(layout);
9771 static inline int lfs_mirror_resync(int argc, char **argv)
9773 struct ll_ioc_lease *ioc = NULL;
9774 __u16 mirror_ids[128] = { 0 };
9779 struct option long_opts[] = {
9780 { .val = 'o', .name = "only", .has_arg = required_argument },
9783 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9786 rc = parse_mirror_ids(mirror_ids,
9787 sizeof(mirror_ids) / sizeof(__u16),
9791 "%s: bad mirror ids '%s'.\n",
9798 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9799 argv[0], argv[optind - 1]);
9805 if (argc == optind) {
9806 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9811 if (ids_nr > 0 && argc > optind + 1) {
9813 "%s: option '--only' cannot be used upon multiple files.\n",
9820 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9825 /* set the lease on the file */
9826 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9828 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9829 argv[0], strerror(errno));
9834 for (; optind < argc; optind++) {
9835 rc = lfs_mirror_resync_file(argv[optind], ioc,
9836 mirror_ids, ids_nr);
9837 /* ignore previous file's error, continue with next file */
9840 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9848 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9850 struct llapi_layout *layout;
9853 layout = llapi_layout_get_by_fd(fd, 0);
9855 fprintf(stderr, "could not get layout.\n");
9859 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9861 fprintf(stderr, "failed to iterate layout\n");
9862 llapi_layout_free(layout);
9865 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9866 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9867 llapi_layout_free(layout);
9871 llapi_layout_free(layout);
9877 * Check whether two files are the same file
9878 * \retval 0 same file
9879 * \retval 1 not the same file
9880 * \retval <0 error code
9882 static inline int check_same_file(int fd, const char *f2)
9887 if (fstat(fd, &stbuf1) < 0)
9890 if (stat(f2, &stbuf2) < 0)
9893 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9894 stbuf1.st_ino == stbuf2.st_ino)
9900 static inline int lfs_mirror_read(int argc, char **argv)
9903 __u16 mirror_id = 0;
9904 const char *outfile = NULL;
9910 const size_t buflen = 4 << 20;
9912 struct option long_opts[] = {
9913 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9914 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9917 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9922 mirror_id = strtoul(optarg, &end, 0);
9923 if (*end != '\0' || mirror_id == 0) {
9925 "%s %s: invalid mirror ID '%s'\n",
9926 progname, argv[0], optarg);
9934 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9935 progname, argv[optind - 1]);
9940 if (argc == optind) {
9941 fprintf(stderr, "%s %s: no mirrored file provided\n",
9944 } else if (argc > optind + 1) {
9945 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9949 if (mirror_id == 0) {
9950 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9955 /* open mirror file */
9956 fname = argv[optind];
9957 fd = open(fname, O_DIRECT | O_RDONLY);
9959 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9960 progname, argv[0], fname, strerror(errno));
9964 /* verify mirror id */
9965 rc = verify_mirror_id_by_fd(fd, mirror_id);
9968 "%s %s: cannot find mirror with ID %u in '%s'\n",
9969 progname, argv[0], mirror_id, fname);
9973 /* open output file - O_EXCL ensures output is not the same as input */
9975 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9977 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9978 progname, argv[0], outfile, strerror(errno));
9983 outfd = STDOUT_FILENO;
9986 /* allocate buffer */
9987 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9989 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9990 progname, argv[0], rc);
9997 ssize_t written = 0;
9999 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10000 if (bytes_read < 0) {
10003 "%s %s: fail to read data from mirror %u: %s\n",
10004 progname, argv[0], mirror_id, strerror(-rc));
10009 if (bytes_read == 0)
10012 while (written < bytes_read) {
10015 written2 = write(outfd, buf + written,
10016 bytes_read - written);
10017 if (written2 < 0) {
10019 "%s %s: fail to write %s: %s\n",
10020 progname, argv[0], outfile ? : "STDOUT",
10025 written += written2;
10028 if (written != bytes_read) {
10030 "%s %s: written %ld bytes does not match with %ld read.\n",
10031 progname, argv[0], written, bytes_read);
10053 static inline int lfs_mirror_write(int argc, char **argv)
10056 __u16 mirror_id = 0;
10057 const char *inputfile = NULL;
10063 const size_t buflen = 4 << 20;
10065 size_t page_size = sysconf(_SC_PAGESIZE);
10066 struct ll_ioc_lease_id ioc;
10068 struct option long_opts[] = {
10069 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10070 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10071 { .name = NULL } };
10073 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10078 mirror_id = strtoul(optarg, &end, 0);
10079 if (*end != '\0' || mirror_id == 0) {
10081 "%s %s: invalid mirror ID '%s'\n",
10082 progname, argv[0], optarg);
10087 inputfile = optarg;
10090 fprintf(stderr, "%s: option '%s' unrecognized\n",
10091 progname, argv[optind - 1]);
10096 if (argc == optind) {
10097 fprintf(stderr, "%s %s: no mirrored file provided\n",
10098 progname, argv[0]);
10100 } else if (argc > optind + 1) {
10101 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10105 if (mirror_id == 0) {
10106 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10107 progname, argv[0]);
10111 /* open mirror file */
10112 fname = argv[optind];
10113 fd = open(fname, O_DIRECT | O_WRONLY);
10115 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10116 progname, argv[0], fname, strerror(errno));
10120 /* verify mirror id */
10121 rc = verify_mirror_id_by_fd(fd, mirror_id);
10124 "%s %s: cannot find mirror with ID %u in '%s'\n",
10125 progname, argv[0], mirror_id, fname);
10129 /* open input file */
10131 rc = check_same_file(fd, inputfile);
10134 "%s %s: input file cannot be the mirrored file\n",
10135 progname, argv[0]);
10141 inputfd = open(inputfile, O_RDONLY, 0644);
10143 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10144 progname, argv[0], inputfile, strerror(errno));
10149 inputfd = STDIN_FILENO;
10152 /* allocate buffer */
10153 rc = posix_memalign(&buf, page_size, buflen);
10155 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10156 progname, argv[0], rc);
10157 goto close_inputfd;
10160 /* prepare target mirror components instantiation */
10161 ioc.lil_mode = LL_LEASE_WRLCK;
10162 ioc.lil_flags = LL_LEASE_RESYNC;
10163 ioc.lil_mirror_id = mirror_id;
10164 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10167 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10168 progname, argv[0], fname, strerror(errno));
10174 ssize_t bytes_read;
10178 rc = llapi_lease_check(fd);
10179 if (rc != LL_LEASE_WRLCK) {
10180 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10181 progname, argv[0], fname);
10185 bytes_read = read(inputfd, buf, buflen);
10186 if (bytes_read < 0) {
10189 "%s %s: fail to read data from '%s': %s\n",
10190 progname, argv[0], inputfile ? : "STDIN",
10197 if (bytes_read == 0)
10200 /* round up to page align to make direct IO happy. */
10201 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10203 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10208 "%s %s: fail to write to mirror %u: %s\n",
10209 progname, argv[0], mirror_id,
10217 if (pos & (page_size - 1)) {
10218 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10223 ioc.lil_mode = LL_LEASE_UNLCK;
10224 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10226 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10231 "%s %s: release lease lock of '%s' failed: %s\n",
10232 progname, argv[0], fname, strerror(errno));
10249 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10251 struct llapi_layout *layout;
10252 struct collect_ids_data cid = { .cid_ids = ids,
10254 .cid_exclude = exclude_id, };
10257 layout = llapi_layout_get_by_fd(fd, 0);
10259 fprintf(stderr, "could not get layout\n");
10263 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10265 fprintf(stderr, "failed to iterate layout\n");
10266 llapi_layout_free(layout);
10270 llapi_layout_free(layout);
10272 return cid.cid_count;
10275 static inline int lfs_mirror_copy(int argc, char **argv)
10278 __u16 read_mirror_id = 0;
10279 __u16 ids[128] = { 0 };
10281 struct llapi_layout *layout = NULL;
10282 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10289 struct ll_ioc_lease *ioc = NULL;
10290 struct ll_ioc_lease_id *resync_ioc;
10292 struct option long_opts[] = {
10293 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10294 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10295 { .name = NULL } };
10297 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10302 read_mirror_id = strtoul(optarg, &end, 0);
10303 if (*end != '\0' || read_mirror_id == 0) {
10305 "%s %s: invalid read mirror ID '%s'\n",
10306 progname, argv[0], optarg);
10311 if (!strcmp(optarg, "-1")) {
10312 /* specify all other mirrors */
10313 ids[0] = (__u16)-1;
10316 count = parse_mirror_ids((__u16 *)ids,
10324 fprintf(stderr, "%s: option '%s' unrecognized\n",
10325 progname, argv[optind - 1]);
10330 if (argc == optind) {
10331 fprintf(stderr, "%s %s: no mirrored file provided\n",
10332 progname, argv[0]);
10334 } else if (argc > optind + 1) {
10335 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10339 if (read_mirror_id == 0) {
10341 "%s %s: no valid read mirror ID %d is provided\n",
10342 progname, argv[0], read_mirror_id);
10348 "%s %s: no write mirror ID is provided\n",
10349 progname, argv[0]);
10353 for (i = 0; i < count; i++) {
10354 if (read_mirror_id == ids[i]) {
10356 "%s %s: read and write mirror ID cannot be the same\n",
10357 progname, argv[0]);
10362 /* open mirror file */
10363 fname = argv[optind];
10365 fd = open(fname, O_DIRECT | O_RDWR);
10367 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10368 progname, argv[0], fname, strerror(errno));
10372 /* write to all other mirrors */
10373 if (ids[0] == (__u16)-1) {
10374 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10378 "%s %s: failed to get other mirror ids in '%s': %d\n",
10379 progname, argv[0], fname, rc);
10384 /* verify mirror id */
10385 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10388 "%s %s: cannot find mirror with ID %u in '%s'\n",
10389 progname, argv[0], read_mirror_id, fname);
10393 for (i = 0; i < count; i++) {
10394 rc = verify_mirror_id_by_fd(fd, ids[i]);
10397 "%s %s: cannot find mirror with ID %u in '%s'\n",
10398 progname, argv[0], ids[i], fname);
10403 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10406 "%s %s: cannot alloc comp id array for ioc: %s\n",
10407 progname, argv[0], strerror(errno));
10412 /* get stale component info */
10413 layout = llapi_layout_get_by_fd(fd, 0);
10415 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10416 progname, argv[0], fname, strerror(errno));
10420 comp_size = llapi_mirror_find_stale(layout, comp_array,
10421 ARRAY_SIZE(comp_array),
10423 llapi_layout_free(layout);
10424 if (comp_size < 0) {
10429 /* prepare target mirror components instantiation */
10430 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10431 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10432 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10434 resync_ioc->lil_mirror_id = ids[0];
10436 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10437 rc = llapi_lease_set(fd, ioc);
10440 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10441 progname, argv[0], fname, strerror(errno));
10445 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10448 fprintf(stderr, "%s %s: copy error: %d\n",
10449 progname, argv[0], rc);
10453 fprintf(stdout, "mirror copied successfully: ");
10454 for (i = 0; i < copied; i++)
10455 fprintf(stdout, "%d ", ids[i]);
10456 fprintf(stdout, "\n");
10458 ioc->lil_mode = LL_LEASE_UNLCK;
10459 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10460 ioc->lil_count = 0;
10461 for (i = 0; i < comp_size; i++) {
10464 for (j = 0; j < copied; j++) {
10465 if (comp_array[i].lrc_mirror_id != ids[j])
10468 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10472 rc = llapi_lease_set(fd, ioc);
10477 "%s %s: release lease lock of '%s' failed: %s\n",
10478 progname, argv[0], fname, strerror(errno));
10493 * struct verify_chunk - Mirror chunk to be verified.
10494 * @chunk: [start, end) of the chunk.
10495 * @mirror_count: Number of mirror ids in @mirror_id array.
10496 * @mirror_id: Array of valid mirror ids that cover the chunk.
10498 struct verify_chunk {
10499 struct lu_extent chunk;
10500 unsigned int mirror_count;
10501 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10505 * print_chunks() - Print chunk information.
10506 * @fname: Mirrored file name.
10507 * @chunks: Array of chunks.
10508 * @chunk_count: Number of chunks in @chunks array.
10510 * This function prints [start, end) of each chunk in @chunks
10511 * for mirrored file @fname, and also prints the valid mirror ids
10512 * that cover the chunk.
10517 void print_chunks(const char *fname, struct verify_chunk *chunks,
10523 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10524 for (i = 0; i < chunk_count; i++) {
10525 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10527 if (chunks[i].mirror_count == 0)
10528 fprintf(stdout, "\t[");
10530 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10531 for (j = 1; j < chunks[i].mirror_count; j++)
10532 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10534 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10536 fprintf(stdout, "\n");
10540 * print_checksums() - Print CRC-32 checksum values.
10541 * @chunk: A chunk and its corresponding valid mirror ids.
10542 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10544 * This function prints CRC-32 checksum values on @chunk for
10545 * each valid mirror that covers it.
10550 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10555 "CRC-32 checksum value for chunk "DEXT":\n",
10556 PEXT(&chunk->chunk));
10557 for (i = 0; i < chunk->mirror_count; i++)
10558 fprintf(stdout, "Mirror %u:\t%#lx\n",
10559 chunk->mirror_id[i], crc[i]);
10560 fprintf(stdout, "\n");
10564 * filter_mirror_id() - Filter specified mirror ids.
10565 * @chunks: Array of chunks.
10566 * @chunk_count: Number of chunks in @chunks array.
10567 * @mirror_ids: Specified mirror ids to be verified.
10568 * @ids_nr: Number of specified mirror ids.
10570 * This function scans valid mirror ids that cover each chunk in @chunks
10571 * and filters specified mirror ids.
10576 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10577 __u16 *mirror_ids, int ids_nr)
10582 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10583 unsigned int valid_count = 0;
10585 for (i = 0; i < chunk_count; i++) {
10586 if (chunks[i].mirror_count == 0)
10590 for (j = 0; j < ids_nr; j++) {
10591 for (k = 0; k < chunks[i].mirror_count; k++) {
10592 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10593 valid_id[valid_count] = mirror_ids[j];
10600 memcpy(chunks[i].mirror_id, valid_id,
10601 sizeof(__u16) * valid_count);
10602 chunks[i].mirror_count = valid_count;
10607 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10608 * @layout: Mirror component list.
10609 * @chunks: Array of chunks.
10610 * @chunks_size: Array size of @chunks.
10612 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10613 * to find out chunk segments and store them in @chunks array.
10615 * The @mirror_id array in each element of @chunks will store the valid
10616 * mirror ids that cover the chunk. If a mirror component covering the
10617 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10618 * will not be stored into the @mirror_id array, and the chunk for that
10619 * mirror will not be verified.
10621 * The @mirror_count in each element of @chunks will store the number of
10622 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10623 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10624 * indicates the chunk is valid in only one mirror. In both cases, the
10625 * chunk will not be verified.
10627 * Here is an example:
10629 * 0 1M 2M 3M 4M EOF
10630 * +------+-------------+--------------------+
10631 * | | | S | mirror1
10632 * +------+------+------+------+-------------+
10633 * | | S | S | | mirror2
10634 * +-------------+------+------+-------------+
10636 * prepared @chunks array will contain 5 elements:
10637 * (([0, 1M), [1, 2], 2),
10638 * ([1M, 2M), [1, 2], 2),
10639 * ([2M, 3M), [1], 1),
10640 * ([3M, 4M], [], 0),
10641 * ([4M, EOF), [2], 1))
10643 * Return: the actual array size of @chunks on success
10644 * or a negative error code on failure.
10647 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10648 struct verify_chunk *chunks,
10649 size_t chunks_size)
10653 uint32_t mirror_id;
10659 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10662 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10665 "%s: move to the first layout component: %s.\n",
10666 progname, strerror(errno));
10672 chunks[idx].chunk.e_end = LUSTRE_EOF;
10674 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10677 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10678 progname, strerror(errno));
10682 if (start > chunks[idx].chunk.e_start ||
10683 end <= chunks[idx].chunk.e_start)
10686 if (end < chunks[idx].chunk.e_end)
10687 chunks[idx].chunk.e_end = end;
10689 rc = llapi_layout_comp_flags_get(layout, &flags);
10692 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10693 progname, strerror(errno));
10697 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10700 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10703 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10704 progname, strerror(errno));
10708 chunks[idx].mirror_id[i] = mirror_id;
10710 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10712 "%s: mirror_id array is too small.\n",
10719 rc = llapi_layout_comp_use(layout,
10720 LLAPI_LAYOUT_COMP_USE_NEXT);
10723 "%s: move to the next layout component: %s.\n",
10724 progname, strerror(errno));
10727 } /* loop through all components */
10729 chunks[idx].mirror_count = i;
10731 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10735 if (idx >= chunks_size) {
10736 fprintf(stderr, "%s: chunks array is too small.\n",
10742 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10746 return rc < 0 ? rc : idx + 1;
10750 * lfs_mirror_verify_chunk() - Verify a chunk.
10751 * @fd: File descriptor of the mirrored file.
10752 * @file_size: Size of the mirrored file.
10753 * @chunk: A chunk and its corresponding valid mirror ids.
10754 * @verbose: Verbose mode.
10756 * This function verifies a @chunk contains exactly the same data
10757 * ammong the mirrors that cover it.
10759 * If @verbose is specified, then the function will print where the
10760 * differences are if the data do not match. Otherwise, it will
10761 * just return an error in that case.
10763 * Return: 0 on success or a negative error code on failure.
10766 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10767 struct verify_chunk *chunk, int verbose)
10769 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10771 size_t page_size = sysconf(_SC_PAGESIZE);
10772 ssize_t bytes_read;
10773 ssize_t bytes_done;
10777 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10781 if (file_size == 0)
10784 rc = posix_memalign(&buf, page_size, buflen);
10785 if (rc) /* error code is returned directly */
10789 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10790 PEXT(&chunk->chunk));
10791 for (i = 0; i < chunk->mirror_count; i++)
10792 fprintf(stdout, " %u", chunk->mirror_id[i]);
10793 fprintf(stdout, "\n");
10797 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10798 pos = chunk->chunk.e_start;
10799 while (bytes_done < count) {
10800 /* compute initial CRC-32 checksum */
10801 crc = crc32(0L, Z_NULL, 0);
10802 memset(crc_array, 0, sizeof(crc_array));
10805 for (i = 0; i < chunk->mirror_count; i++) {
10806 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10808 if (bytes_read < 0) {
10811 "%s: failed to read data from mirror %u: %s.\n",
10812 progname, chunk->mirror_id[i],
10817 /* compute new CRC-32 checksum */
10818 crc_array[i] = crc32(crc, buf, bytes_read);
10822 print_checksums(chunk, crc_array);
10824 /* compare CRC-32 checksum values */
10825 for (i = 1; i < chunk->mirror_count; i++) {
10826 if (crc_array[i] != crc_array[0]) {
10830 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10831 progname, PEXT(&chunk->chunk),
10832 chunk->mirror_id[0],
10833 chunk->mirror_id[i]);
10838 bytes_done += bytes_read;
10841 if (verbose > 1 && rc == 0) {
10842 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10843 PEXT(&chunk->chunk));
10844 for (i = 0; i < chunk->mirror_count; i++)
10845 fprintf(stdout, " %u", chunk->mirror_id[i]);
10846 fprintf(stdout, " PASS\n\n");
10855 * lfs_mirror_verify_file() - Verify a mirrored file.
10856 * @fname: Mirrored file name.
10857 * @mirror_ids: Specified mirror ids to be verified.
10858 * @ids_nr: Number of specified mirror ids.
10859 * @verbose: Verbose mode.
10861 * This function verifies that each SYNC mirror of a mirrored file
10862 * specified by @fname contains exactly the same data.
10864 * If @mirror_ids is specified, then the function will verify the
10865 * mirrors specified by @mirror_ids contain exactly the same data.
10867 * If @verbose is specified, then the function will print where the
10868 * differences are if the data do not match. Otherwise, it will
10869 * just return an error in that case.
10871 * Return: 0 on success or a negative error code on failure.
10874 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10877 struct verify_chunk chunks_array[1024] = { };
10878 struct llapi_layout *layout = NULL;
10880 uint32_t flr_state;
10882 int chunk_count = 0;
10888 if (stat(fname, &stbuf) < 0) {
10889 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10890 progname, fname, strerror(errno));
10895 if (!S_ISREG(stbuf.st_mode)) {
10896 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10902 if (stbuf.st_size == 0) {
10904 fprintf(stdout, "%s: '%s' file size is 0.\n",
10910 fd = open(fname, O_DIRECT | O_RDONLY);
10912 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10913 progname, fname, strerror(errno));
10918 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10920 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10921 progname, fname, strerror(errno));
10925 layout = llapi_layout_get_by_fd(fd, 0);
10927 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10928 progname, fname, strerror(errno));
10930 llapi_lease_release(fd);
10934 rc = llapi_layout_flags_get(layout, &flr_state);
10936 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10937 progname, fname, strerror(errno));
10942 flr_state &= LCM_FL_FLR_MASK;
10943 switch (flr_state) {
10946 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10947 progname, fname, llapi_layout_flags_string(flr_state));
10953 /* find out mirror chunks to be verified */
10954 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10955 ARRAY_SIZE(chunks_array));
10956 if (chunk_count < 0) {
10962 /* filter specified mirror ids */
10963 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10966 print_chunks(fname, chunks_array, chunk_count);
10968 for (idx = 0; idx < chunk_count; idx++) {
10969 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10972 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10974 PEXT(&chunks_array[idx].chunk),
10975 (unsigned long long)stbuf.st_size);
10979 if (chunks_array[idx].mirror_count == 0) {
10981 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10983 PEXT(&chunks_array[idx].chunk));
10985 fprintf(stderr, "skipped\n");
10989 fprintf(stderr, "failed\n");
10993 if (chunks_array[idx].mirror_count == 1) {
10996 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10998 PEXT(&chunks_array[idx].chunk),
10999 chunks_array[idx].mirror_id[0]);
11003 rc = llapi_lease_check(fd);
11004 if (rc != LL_LEASE_RDLCK) {
11005 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11010 /* verify one chunk */
11011 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11012 &chunks_array[idx], verbose);
11026 llapi_layout_free(layout);
11027 llapi_lease_release(fd);
11035 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11036 * @argc: The count of lfs mirror verify command line arguments.
11037 * @argv: Array of strings for lfs mirror verify command line arguments.
11039 * This function parses lfs mirror verify command and verifies the
11040 * specified mirrored file(s).
11042 * Return: 0 on success or a negative error code on failure.
11044 static inline int lfs_mirror_verify(int argc, char **argv)
11046 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11052 char cmd[PATH_MAX];
11054 struct option long_opts[] = {
11055 { .val = 'o', .name = "only", .has_arg = required_argument },
11056 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11057 { .name = NULL } };
11059 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11061 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11064 rc = parse_mirror_ids(mirror_ids,
11065 ARRAY_SIZE(mirror_ids),
11069 "%s: bad mirror ids '%s'.\n",
11076 "%s: at least 2 mirror ids needed with '--only' option.\n",
11086 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11087 progname, argv[optind - 1]);
11093 if (argc == optind) {
11094 fprintf(stderr, "%s: no file name given.\n", progname);
11099 if (ids_nr > 0 && argc > optind + 1) {
11101 "%s: '--only' cannot be used upon multiple files.\n",
11108 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11114 for (; optind < argc; optind++) {
11115 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11125 * lfs_mirror() - Parse and execute lfs mirror commands.
11126 * @argc: The count of lfs mirror command line arguments.
11127 * @argv: Array of strings for lfs mirror command line arguments.
11129 * This function parses lfs mirror commands and performs the
11130 * corresponding functions specified in mirror_cmdlist[].
11132 * Return: 0 on success or an error code on failure.
11134 static int lfs_mirror(int argc, char **argv)
11136 char cmd[PATH_MAX];
11139 setlinebuf(stdout);
11141 Parser_init("lfs-mirror > ", mirror_cmdlist);
11143 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11145 program_invocation_short_name = cmd;
11147 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11149 rc = Parser_commands();
11151 return rc < 0 ? -rc : rc;
11154 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11156 #if __BYTE_ORDER == __BIG_ENDIAN
11157 __swab16s(&attrs->lsa_valid);
11158 __swab64s(&attrs->lsa_size);
11159 __swab64s(&attrs->lsa_blocks);
11163 enum lfs_som_type {
11164 LFS_SOM_SIZE = 0x1,
11165 LFS_SOM_BLOCKS = 0x2,
11166 LFS_SOM_FLAGS = 0x4,
11167 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11171 static int lfs_getsom(int argc, char **argv)
11174 struct lustre_som_attrs *attrs;
11175 char buf[sizeof(*attrs) + 64];
11176 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11179 while ((c = getopt(argc, argv, "sbf")) != -1) {
11182 type = LFS_SOM_SIZE;
11185 type = LFS_SOM_BLOCKS;
11188 type = LFS_SOM_FLAGS;
11191 fprintf(stderr, "%s: invalid option '%c'\n",
11201 fprintf(stderr, "%s: %s\n",
11202 progname, argc == 0 ? "miss file target" :
11203 "input more than 2 files");
11208 attrs = (void *)buf;
11209 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11212 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11213 argv[0], strerror(errno), errno);
11217 lustre_som_swab(attrs);
11220 case LFS_SOM_ATTR_ALL:
11221 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11222 path, (unsigned long long)attrs->lsa_size,
11223 (unsigned long long)attrs->lsa_blocks,
11227 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11229 case LFS_SOM_BLOCKS:
11230 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11232 case LFS_SOM_FLAGS:
11233 printf("%x\n", attrs->lsa_valid);
11236 fprintf(stderr, "%s: unknown option\n", progname);
11244 * lfs_mirror_list_commands() - List lfs mirror commands.
11245 * @argc: The count of command line arguments.
11246 * @argv: Array of strings for command line arguments.
11248 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11250 * Return: 0 on success.
11252 static int lfs_mirror_list_commands(int argc, char **argv)
11254 char buffer[81] = "";
11256 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11262 static int lfs_pcc_attach(int argc, char **argv)
11264 struct option long_opts[] = {
11265 { .val = 'i', .name = "id", .has_arg = required_argument },
11266 { .name = NULL } };
11269 __u32 archive_id = 0;
11272 char fullpath[PATH_MAX];
11273 enum lu_pcc_type type = LU_PCC_READWRITE;
11276 while ((c = getopt_long(argc, argv, "i:",
11277 long_opts, NULL)) != -1) {
11280 archive_id = strtoul(optarg, &end, 0);
11281 if (*end != '\0' || archive_id == 0) {
11283 "error: %s: bad archive ID '%s'\n",
11291 fprintf(stderr, "%s: option '%s' unrecognized\n",
11292 argv[0], argv[optind - 1]);
11297 if (archive_id == 0) {
11298 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11302 if (argc <= optind) {
11303 fprintf(stderr, "%s: must specify one or more file names\n",
11308 while (optind < argc) {
11311 path = argv[optind++];
11312 if (!realpath(path, fullpath)) {
11313 fprintf(stderr, "%s: could not find path '%s': %s\n",
11314 argv[0], path, strerror(errno));
11320 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11323 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11324 argv[0], path, archive_id, strerror(-rc2));
11332 static int lfs_pcc_attach_fid(int argc, char **argv)
11334 struct option long_opts[] = {
11335 { .val = 'i', .name = "id", .has_arg = required_argument },
11336 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11337 { .name = NULL } };
11338 char short_opts[] = "i:m:";
11341 __u32 archive_id = 0;
11343 const char *mntpath = NULL;
11344 const char *fidstr;
11345 enum lu_pcc_type type = LU_PCC_READWRITE;
11348 while ((c = getopt_long(argc, argv, short_opts,
11349 long_opts, NULL)) != -1) {
11352 archive_id = strtoul(optarg, &end, 0);
11353 if (*end != '\0') {
11355 "error: %s: bad archive ID '%s'\n",
11366 fprintf(stderr, "%s: option '%s' unrecognized\n",
11367 argv[0], argv[optind - 1]);
11372 if (archive_id == 0) {
11373 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11378 fprintf(stderr, "%s: must specify Lustre mount point\n",
11383 if (argc <= optind) {
11384 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11388 while (optind < argc) {
11391 fidstr = argv[optind++];
11393 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11397 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11398 argv[0], fidstr, mntpath, archive_id,
11401 if (rc == 0 && rc2 < 0)
11407 static int lfs_pcc_detach(int argc, char **argv)
11409 struct option long_opts[] = {
11410 { .val = 'k', .name = "keep", .has_arg = no_argument },
11411 { .name = NULL } };
11412 char short_opts[] = "k";
11416 char fullpath[PATH_MAX];
11417 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11420 while ((c = getopt_long(argc, argv, short_opts,
11421 long_opts, NULL)) != -1) {
11424 detach_opt = PCC_DETACH_OPT_NONE;
11429 fprintf(stderr, "%s: option '%s' unrecognized\n",
11430 argv[0], argv[optind - 1]);
11435 while (optind < argc) {
11438 path = argv[optind++];
11439 if (!realpath(path, fullpath)) {
11440 fprintf(stderr, "%s: could not find path '%s': %s\n",
11441 argv[0], path, strerror(errno));
11447 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11451 "%s: cannot detach '%s' from PCC: %s\n",
11452 argv[0], path, strerror(errno));
11460 static int lfs_pcc_detach_fid(int argc, char **argv)
11462 struct option long_opts[] = {
11463 { .val = 'k', .name = "keep", .has_arg = no_argument },
11464 { .name = NULL } };
11465 char short_opts[] = "k";
11469 const char *mntpath;
11470 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11473 while ((c = getopt_long(argc, argv, short_opts,
11474 long_opts, NULL)) != -1) {
11477 detach_opt = PCC_DETACH_OPT_NONE;
11482 fprintf(stderr, "%s: option '%s' unrecognized\n",
11483 argv[0], argv[optind - 1]);
11488 mntpath = argv[optind++];
11490 while (optind < argc) {
11493 fid = argv[optind++];
11495 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11498 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11499 argv[0], fid, mntpath, strerror(-rc2));
11507 static int lfs_pcc_state(int argc, char **argv)
11511 char fullpath[PATH_MAX];
11512 struct lu_pcc_state state;
11517 fprintf(stderr, "%s: must specify one or more file names\n",
11522 while (optind < argc) {
11525 path = argv[optind++];
11526 if (!realpath(path, fullpath)) {
11527 fprintf(stderr, "%s: could not find path '%s': %s\n",
11528 argv[0], path, strerror(errno));
11534 rc2 = llapi_pcc_state_get(fullpath, &state);
11539 "%s: cannot get PCC state of '%s': %s\n",
11540 argv[0], path, strerror(-rc2));
11544 printf("file: %s", path);
11545 printf(", type: %s", pcc_type2string(state.pccs_type));
11546 if (state.pccs_type == LU_PCC_NONE &&
11547 state.pccs_open_count == 0) {
11552 printf(", PCC file: %s", state.pccs_path);
11553 printf(", user number: %u", state.pccs_open_count);
11554 printf(", flags: %x", state.pccs_flags);
11561 * lfs_pcc_list_commands() - List lfs pcc commands.
11562 * @argc: The count of command line arguments.
11563 * @argv: Array of strings for command line arguments.
11565 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11567 * Return: 0 on success.
11569 static int lfs_pcc_list_commands(int argc, char **argv)
11571 char buffer[81] = "";
11573 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11580 * lfs_pcc() - Parse and execute lfs pcc commands.
11581 * @argc: The count of lfs pcc command line arguments.
11582 * @argv: Array of strings for lfs pcc command line arguments.
11584 * This function parses lfs pcc commands and performs the
11585 * corresponding functions specified in pcc_cmdlist[].
11587 * Return: 0 on success or an error code on failure.
11589 static int lfs_pcc(int argc, char **argv)
11591 char cmd[PATH_MAX];
11594 setlinebuf(stdout);
11596 Parser_init("lfs-pcc > ", pcc_cmdlist);
11598 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11600 program_invocation_short_name = cmd;
11602 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11604 rc = Parser_commands();
11606 return rc < 0 ? -rc : rc;
11609 static int lfs_list_commands(int argc, char **argv)
11611 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11613 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11618 int main(int argc, char **argv)
11622 /* Ensure that liblustreapi constructor has run */
11623 if (!llapi_liblustreapi_initialized())
11624 fprintf(stderr, "liblustreapi was not properly initialized\n");
11626 setlinebuf(stdout);
11629 Parser_init("lfs > ", cmdlist);
11631 progname = program_invocation_short_name; /* Used in error messages */
11633 llapi_set_command_name(argv[1]);
11634 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11635 llapi_clear_command_name();
11637 rc = Parser_commands();
11640 return rc < 0 ? -rc : rc;
11643 #ifdef _LUSTRE_IDL_H_
11644 /* Everything we need here should be included by lustreapi.h. */
11645 # error "lfs should not depend on lustre_idl.h"
11646 #endif /* _LUSTRE_IDL_H_ */