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);
957 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
976 static int migrate_copy_timestamps(int fd, int fdv)
980 if (fstat(fd, &st) == 0) {
981 struct timeval tv[2] = {
982 {.tv_sec = st.st_atime},
983 {.tv_sec = st.st_mtime}
986 return futimes(fdv, tv);
992 static int migrate_block(int fd, int fdv)
999 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1001 error_loc = "cannot get dataversion";
1010 * The grouplock blocks all concurrent accesses to the file.
1011 * It has to be taken after llapi_get_data_version as it would
1014 rc = llapi_group_lock(fd, gid);
1016 error_loc = "cannot get group lock";
1020 rc = migrate_copy_data(fd, fdv, NULL);
1022 error_loc = "data copy failed";
1026 /* Make sure we keep original atime/mtime values */
1027 rc = migrate_copy_timestamps(fd, fdv);
1029 error_loc = "timestamp copy failed";
1035 * for a migration we need to check data version on file did
1038 * Pass in gid=0 since we already own grouplock.
1040 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1041 SWAP_LAYOUTS_CHECK_DV1);
1042 if (rc == -EAGAIN) {
1043 error_loc = "file changed";
1045 } else if (rc < 0) {
1046 error_loc = "cannot swap layout";
1051 rc2 = llapi_group_unlock(fd, gid);
1052 if (rc2 < 0 && rc == 0) {
1053 error_loc = "unlock group lock";
1061 * Internal helper for migrate_copy_data(). Check lease and report error if
1064 * \param[in] fd File descriptor on which to check the lease.
1066 * \retval 0 Migration can keep on going.
1067 * \retval -errno Error occurred, abort migration.
1069 static int check_lease(int fd)
1073 rc = llapi_lease_check(fd);
1075 return 0; /* llapi_check_lease returns > 0 on success. */
1080 static int migrate_nonblock(int fd, int fdv)
1086 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1088 error_loc = "cannot get data version";
1092 rc = migrate_copy_data(fd, fdv, check_lease);
1094 error_loc = "data copy failed";
1098 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1100 error_loc = "cannot get data version";
1106 error_loc = "source file changed";
1110 /* Make sure we keep original atime/mtime values */
1111 rc = migrate_copy_timestamps(fd, fdv);
1113 error_loc = "timestamp copy failed";
1120 static int lfs_component_set(char *fname, int comp_id,
1121 __u32 flags, __u32 neg_flags)
1124 __u32 flags_array[2];
1129 ids[count] = comp_id;
1130 flags_array[count] = flags;
1135 if (neg_flags & LCME_FL_STALE) {
1137 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1142 ids[count] = comp_id;
1143 flags_array[count] = neg_flags | LCME_FL_NEG;
1147 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1149 if (errno == EUCLEAN) {
1152 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1153 progname, comp_id, fname);
1156 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1157 progname, comp_id, fname, flags, neg_flags);
1164 static int lfs_component_del(char *fname, __u32 comp_id,
1165 __u32 flags, __u32 neg_flags)
1169 if (flags && neg_flags)
1172 if (!flags && neg_flags)
1173 flags = neg_flags | LCME_FL_NEG;
1175 if ((flags && comp_id) || (!flags && !comp_id))
1179 if (flags & ~LCME_KNOWN_FLAGS) {
1181 "%s setstripe: unknown flags %#x\n",
1185 } else if (comp_id > LCME_ID_MAX) {
1186 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1191 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1194 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1195 progname, comp_id, fname, strerror(errno));
1199 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1206 rc = llapi_layout_file_comp_add(fname, layout);
1208 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1209 fname, strerror(errno));
1213 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1214 struct llapi_layout *layout)
1222 fd = lstat(fname, &st);
1223 if (fd == 0 && S_ISDIR(st.st_mode))
1224 open_flags = O_DIRECTORY | O_RDONLY;
1226 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1228 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1229 S_ISDIR(st.st_mode) ?
1230 "set default composite layout for" :
1231 "create composite file",
1232 fname, strerror(errno));
1236 static int lfs_migrate(char *name, __u64 migration_flags,
1237 struct llapi_stripe_param *param,
1238 struct llapi_layout *layout)
1240 struct llapi_layout *existing;
1241 uint64_t dom_new, dom_cur;
1246 rc = migrate_open_files(name, migration_flags, param, layout,
1251 rc = llapi_layout_dom_size(layout, &dom_new);
1253 error_loc = "cannot get new layout DoM size";
1256 /* special case for migration to DOM layout*/
1257 existing = llapi_layout_get_by_fd(fd, 0);
1259 error_loc = "cannot get existing layout";
1263 rc = llapi_layout_dom_size(existing, &dom_cur);
1265 error_loc = "cannot get current layout DoM size";
1270 * if file has DoM layout already then migration is possible to
1271 * the new layout with the same DoM component via swap layout,
1272 * if new layout used bigger DOM size, then mirroring is used
1274 if (dom_new > dom_cur) {
1275 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1278 error_loc = "cannot migrate to DOM layout";
1282 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1284 * Blocking mode (forced if servers do not support file lease).
1285 * It is also the default mode, since we cannot distinguish
1286 * between a broken lease and a server that does not support
1287 * atomic swap/close (LU-6785)
1289 rc = migrate_block(fd, fdv);
1293 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1295 error_loc = "cannot get lease";
1299 rc = migrate_nonblock(fd, fdv);
1301 llapi_lease_release(fd);
1306 * Atomically put lease, swap layouts and close.
1307 * for a migration we need to check data version on file did
1310 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1312 error_loc = "cannot swap layout";
1324 fprintf(stderr, "error: %s: %s: %s: %s\n",
1325 progname, name, error_loc, strerror(-rc));
1326 else if (migration_flags & MIGRATION_VERBOSE)
1327 printf("%s\n", name);
1332 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1341 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1345 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1346 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1347 const char *comp_name = comp_flags_table[i].cfn_name;
1349 if (strcmp(name, comp_name) == 0) {
1350 *flags |= comp_flag;
1352 } else if (strncmp(name, "^", 1) == 0 &&
1353 strcmp(name + 1, comp_name) == 0) {
1354 *neg_flags |= comp_flag;
1359 llapi_printf(LLAPI_MSG_ERROR,
1360 "%s: component flag '%s' not supported\n",
1366 if (!*flags && !*neg_flags)
1369 /* don't allow to set and exclude the same flag */
1370 if (*flags & *neg_flags)
1376 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1384 if (strncmp(string, "^", 1) == 0) {
1385 *neg_state = llapi_layout_string_flags(string + 1);
1386 if (*neg_state != 0)
1389 *state = llapi_layout_string_flags(string);
1394 llapi_printf(LLAPI_MSG_ERROR,
1395 "%s: mirrored file state '%s' not supported\n",
1401 * struct mirror_args - Command-line arguments for mirror(s).
1402 * @m_count: Number of mirrors to be created with this layout.
1403 * @m_flags: Mirror level flags, only 'prefer' is supported.
1404 * @m_layout: Mirror layout.
1405 * @m_file: A victim file. Its layout will be split and used as a mirror.
1406 * @m_next: Point to the next node of the list.
1408 * Command-line arguments for mirror(s) will be parsed and stored in
1409 * a linked list that consists of this structure.
1411 struct mirror_args {
1414 struct llapi_layout *m_layout;
1416 struct mirror_args *m_next;
1421 * enum mirror_flags - Flags for extending a mirrored file.
1422 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1423 * in case the victim file(s) contains the same data as the
1424 * original mirrored file.
1425 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1426 * @MF_COMP_ID: specified component id instead of mirror id
1428 * Flags for extending a mirrored file.
1438 * mirror_create_sanity_check() - Check mirror list.
1439 * @list: A linked list that stores the mirror arguments.
1441 * This function does a sanity check on @list for creating
1444 * Return: 0 on success or a negative error code on failure.
1446 static int mirror_create_sanity_check(const char *fname,
1447 struct mirror_args *list)
1450 bool has_m_file = false;
1451 bool has_m_layout = false;
1457 struct llapi_layout *layout;
1459 layout = llapi_layout_get_by_path(fname, 0);
1462 "error: %s: file '%s' couldn't get layout\n",
1467 rc = llapi_layout_sanity(layout, false, true);
1469 llapi_layout_free(layout);
1472 llapi_layout_sanity_perror(rc);
1480 llapi_layout_free(list->m_layout);
1483 llapi_layout_get_by_path(list->m_file, 0);
1484 if (!list->m_layout) {
1486 "error: %s: file '%s' has no layout\n",
1487 progname, list->m_file);
1491 has_m_layout = true;
1492 if (!list->m_layout) {
1493 fprintf(stderr, "error: %s: no mirror layout\n",
1499 rc = llapi_layout_sanity(list->m_layout, false, true);
1501 llapi_layout_sanity_perror(rc);
1505 list = list->m_next;
1508 if (has_m_file && has_m_layout) {
1510 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1518 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1520 __u32 mirror_flags = *(__u32 *)cbdata;
1524 rc = llapi_layout_comp_flags_get(layout, &flags);
1529 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1534 return LLAPI_LAYOUT_ITER_CONT;
1538 * mirror_create() - Create a mirrored file.
1539 * @fname: The file to be created.
1540 * @mirror_list: A linked list that stores the mirror arguments.
1542 * This function creates a mirrored file @fname with the mirror(s)
1543 * from @mirror_list.
1545 * Return: 0 on success or a negative error code on failure.
1547 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1549 struct llapi_layout *layout = NULL;
1550 struct mirror_args *cur_mirror = NULL;
1551 uint16_t mirror_count = 0;
1555 rc = mirror_create_sanity_check(NULL, mirror_list);
1559 cur_mirror = mirror_list;
1560 while (cur_mirror) {
1561 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1563 &cur_mirror->m_flags);
1566 fprintf(stderr, "%s: failed to set mirror flags\n",
1571 for (i = 0; i < cur_mirror->m_count; i++) {
1572 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1576 "error: %s: merge layout failed: %s\n",
1577 progname, strerror(errno));
1581 mirror_count += cur_mirror->m_count;
1582 cur_mirror = cur_mirror->m_next;
1586 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1590 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1593 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1594 progname, strerror(errno));
1598 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1606 llapi_layout_free(layout);
1611 * Compare files and check lease on @fd.
1613 * \retval bytes number of bytes are the same
1615 static ssize_t mirror_file_compare(int fd, int fdv)
1617 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1619 ssize_t bytes_done = 0;
1620 ssize_t bytes_read = 0;
1622 buf = malloc(buflen * 2);
1627 if (!llapi_lease_check(fd)) {
1628 bytes_done = -EBUSY;
1632 bytes_read = read(fd, buf, buflen);
1633 if (bytes_read <= 0)
1636 if (bytes_read != read(fdv, buf + buflen, buflen))
1640 * XXX: should compute the checksum on each buffer and then
1641 * compare checksum to avoid cache collision
1643 if (memcmp(buf, buf + buflen, bytes_read))
1646 bytes_done += bytes_read;
1654 static int mirror_extend_file(const char *fname, const char *victim_file,
1655 enum mirror_flags mirror_flags)
1660 struct stat stbuf_v;
1661 struct ll_ioc_lease *data = NULL;
1664 fd = open(fname, O_RDWR);
1666 error_loc = "open source file";
1671 fdv = open(victim_file, O_RDWR);
1673 error_loc = "open target file";
1678 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1679 error_loc = "stat source or target file";
1684 if (stbuf.st_dev != stbuf_v.st_dev) {
1685 error_loc = "stat source and target file";
1690 /* mirrors should be of the same size */
1691 if (stbuf.st_size != stbuf_v.st_size) {
1692 error_loc = "file sizes don't match";
1697 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1699 error_loc = "cannot get lease";
1703 if (!(mirror_flags & MF_NO_VERIFY)) {
1705 /* mirrors should have the same contents */
1706 ret = mirror_file_compare(fd, fdv);
1707 if (ret != stbuf.st_size) {
1708 error_loc = "file busy or contents don't match";
1709 rc = ret < 0 ? ret : -EINVAL;
1714 /* Get rid of caching pages from clients */
1715 rc = llapi_file_flush(fd);
1717 error_loc = "cannot get data version";
1721 rc = llapi_file_flush(fdv);
1723 error_loc = "cannot get data version";
1727 /* Make sure we keep original atime/mtime values */
1728 rc = migrate_copy_timestamps(fd, fdv);
1730 error_loc = "cannot copy timestamp";
1734 /* Atomically put lease, merge layouts and close. */
1735 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1737 error_loc = "memory allocation";
1740 data->lil_mode = LL_LEASE_UNLCK;
1741 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1742 data->lil_count = 1;
1743 data->lil_ids[0] = fdv;
1744 rc = llapi_lease_set(fd, data);
1746 error_loc = "cannot merge layout";
1748 } else if (rc == 0) {
1750 error_loc = "lost lease lock";
1763 (void) unlink(victim_file);
1765 fprintf(stderr, "error: %s: %s: %s: %s\n",
1766 progname, fname, error_loc, strerror(-rc));
1770 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1771 bool inherit, uint32_t flags)
1773 struct llapi_layout *f_layout = NULL;
1774 struct ll_ioc_lease *data = NULL;
1780 f_layout = llapi_layout_get_by_path(name, 0);
1782 fprintf(stderr, "%s: cannot get layout\n", progname);
1785 rc = llapi_layout_get_last_init_comp(f_layout);
1787 fprintf(stderr, "%s: cannot get the last init comp\n",
1791 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1794 "%s: cannot inherit from the last init comp\n",
1799 llapi_layout_comp_flags_set(m_layout, flags);
1800 rc = migrate_open_files(name, 0, NULL, m_layout, &fd, &fdv);
1804 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1806 error_loc = "cannot get lease";
1810 rc = migrate_nonblock(fd, fdv);
1812 llapi_lease_release(fd);
1816 /* Atomically put lease, merge layouts and close. */
1817 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1819 error_loc = "memory allocation";
1822 data->lil_mode = LL_LEASE_UNLCK;
1823 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1824 data->lil_count = 1;
1825 data->lil_ids[0] = fdv;
1826 rc = llapi_lease_set(fd, data);
1828 error_loc = "cannot merge layout";
1830 } else if (rc == 0) {
1832 error_loc = "lost lease lock";
1845 fprintf(stderr, "error: %s: %s: %s: %s\n",
1846 progname, name, error_loc, strerror(-rc));
1850 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1851 enum mirror_flags mirror_flags)
1855 rc = mirror_create_sanity_check(fname, mirror_list);
1859 while (mirror_list) {
1860 if (mirror_list->m_file) {
1861 rc = mirror_extend_file(fname, mirror_list->m_file,
1864 __u32 mirror_count = mirror_list->m_count;
1866 while (mirror_count > 0) {
1867 rc = mirror_extend_layout(fname,
1868 mirror_list->m_layout,
1869 mirror_list->m_inherit,
1870 mirror_list->m_flags);
1880 mirror_list = mirror_list->m_next;
1886 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1891 rc = llapi_layout_mirror_id_get(layout, &id);
1895 if ((__u16)id == *(__u16 *)cbdata)
1896 return LLAPI_LAYOUT_ITER_STOP;
1898 return LLAPI_LAYOUT_ITER_CONT;
1901 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1906 rc = llapi_layout_comp_id_get(layout, &id);
1910 if (id == *(__u32 *)cbdata)
1911 return LLAPI_LAYOUT_ITER_STOP;
1913 return LLAPI_LAYOUT_ITER_CONT;
1916 struct pool_to_id_cbdata {
1921 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1923 char buf[LOV_MAXPOOLNAME + 1];
1924 struct pool_to_id_cbdata *d = (void *)cbdata;
1928 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1931 if (strcmp(d->pool, buf))
1932 return LLAPI_LAYOUT_ITER_CONT;
1934 rc = llapi_layout_mirror_id_get(layout, &id);
1939 return LLAPI_LAYOUT_ITER_STOP;
1942 struct collect_ids_data {
1948 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1950 struct collect_ids_data *cid = cbdata;
1954 rc = llapi_layout_mirror_id_get(layout, &id);
1958 if ((__u16)id != cid->cid_exclude) {
1961 for (i = 0; i < cid->cid_count; i++) {
1962 /* already collected the mirror id */
1963 if (id == cid->cid_ids[i])
1964 return LLAPI_LAYOUT_ITER_CONT;
1966 cid->cid_ids[cid->cid_count] = id;
1970 return LLAPI_LAYOUT_ITER_CONT;
1974 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1975 * @mirror_id: Mirror id to be checked.
1976 * @layout: Mirror component list.
1978 * This function checks if a mirror with specified @mirror_id is the last
1979 * non-stale mirror of a layout @layout.
1981 * Return: true or false.
1984 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1986 __u16 mirror_ids[128] = { 0 };
1987 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1989 .cid_exclude = mirror_id, };
1992 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1994 for (i = 0; i < cid.cid_count; i++) {
1995 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1998 comp_size = llapi_mirror_find_stale(layout, comp_array,
1999 ARRAY_SIZE(comp_array),
2008 static int mirror_split(const char *fname, __u32 id, const char *pool,
2009 enum mirror_flags mflags, const char *victim_file)
2011 struct llapi_layout *layout;
2012 char parent[PATH_MAX];
2013 char victim[PATH_MAX];
2014 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2016 struct ll_ioc_lease *data;
2017 uint16_t mirror_count;
2023 /* check fname contains mirror with mirror_id/comp_id */
2024 layout = llapi_layout_get_by_path(fname, 0);
2027 "error %s: file '%s' couldn't get layout\n",
2032 rc = llapi_layout_sanity(layout, false, true);
2034 llapi_layout_sanity_perror(rc);
2038 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2041 "error %s: file '%s' couldn't get mirror count\n",
2045 if (mirror_count < 2) {
2047 "error %s: file '%s' has %d component, cannot split\n",
2048 progname, fname, mirror_count);
2052 if (mflags & MF_COMP_POOL) {
2053 struct pool_to_id_cbdata data = { .pool = pool };
2055 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
2057 mirror_id = data.id;
2058 } else if (mflags & MF_COMP_ID) {
2059 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2060 mirror_id = mirror_id_of(id);
2062 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2066 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2069 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2070 if (mflags & MF_COMP_POOL) {
2072 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2073 progname, fname, pool);
2075 } else if (mflags & MF_COMP_ID) {
2077 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2078 progname, fname, id);
2082 "error %s: file '%s' does not contain mirror with id %u\n",
2083 progname, fname, id);
2088 fd = open(fname, O_RDWR);
2091 "error %s: open file '%s' failed: %s\n",
2092 progname, fname, strerror(errno));
2096 /* get victim file directory pathname */
2097 if (strlen(fname) > sizeof(parent) - 1) {
2098 fprintf(stderr, "error %s: file name of '%s' too long\n",
2103 strncpy(parent, fname, sizeof(parent));
2104 ptr = strrchr(parent, '/');
2106 if (!getcwd(parent, sizeof(parent))) {
2107 fprintf(stderr, "error %s: getcwd failed: %s\n",
2108 progname, strerror(errno));
2118 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2120 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2126 /* use a temp file to store the splitted layout */
2127 if (mflags & MF_DESTROY) {
2128 if (last_non_stale_mirror(mirror_id, layout)) {
2131 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2136 fdv = llapi_create_volatile_idx(parent, mdt_index,
2137 O_LOV_DELAY_CREATE);
2139 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2141 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2144 /* user specified victim file */
2145 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2150 "error %s: create victim file failed: %s\n",
2151 progname, strerror(errno));
2155 /* get lease lock of fname */
2156 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2159 "error %s: cannot get lease of file '%s': %d\n",
2160 progname, fname, rc);
2164 /* Atomatically put lease, split layouts and close. */
2165 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2171 data->lil_mode = LL_LEASE_UNLCK;
2172 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2173 data->lil_count = 2;
2174 data->lil_ids[0] = fdv;
2175 data->lil_ids[1] = mirror_id;
2176 rc = llapi_lease_set(fd, data);
2178 if (rc == 0) /* lost lease lock */
2181 "error %s: cannot split '%s': %s\n",
2182 progname, fname, strerror(-rc));
2193 llapi_layout_free(layout);
2198 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2199 __u16 *mirror_ids, int ids_nr);
2201 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2202 __u64 migration_flags,
2203 struct llapi_stripe_param *param,
2204 struct llapi_layout *layout)
2206 struct ll_ioc_lease *data = NULL;
2209 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2211 error_loc = "cannot get lease";
2215 /* Atomically put lease, merge layouts, resync and close. */
2216 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2218 error_loc = "memory allocation";
2221 data->lil_mode = LL_LEASE_UNLCK;
2222 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2223 data->lil_count = 1;
2224 data->lil_ids[0] = fdv;
2225 rc = llapi_lease_set(fd, data);
2227 error_loc = "cannot merge layout";
2229 } else if (rc == 0) {
2231 error_loc = "lost lease lock";
2237 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2239 error_loc = "cannot resync file";
2243 /* delete first mirror now */
2244 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2246 error_loc = "cannot delete old layout";
2254 fprintf(stderr, "error: %s: %s: %s: %s\n",
2255 progname, name, error_loc, strerror(-rc));
2256 else if (migration_flags & MIGRATION_VERBOSE)
2257 printf("%s\n", name);
2264 * Parse a string containing an target index list into an array of integers.
2266 * The input string contains a comma delimited list of individual
2267 * indices and ranges, for example "1,2-4,7". Add the indices into the
2268 * \a tgts array and remove duplicates.
2270 * \param[out] tgts array to store indices in
2271 * \param[in] size size of \a tgts array
2272 * \param[in] offset starting index in \a tgts
2273 * \param[in] arg string containing OST index list
2274 * \param[in/out] overstriping index list may contain duplicates
2276 * \retval positive number of indices in \a tgts
2277 * \retval -EINVAL unable to parse \a arg
2279 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2280 unsigned long long *pattern)
2284 int slots = size - offset;
2286 bool overstriped = false;
2292 end_of_loop = false;
2293 while (!end_of_loop) {
2294 int start_index = 0;
2297 char *endptr = NULL;
2301 ptr = strchrnul(arg, ',');
2303 end_of_loop = *ptr == '\0';
2306 start_index = strtol(arg, &endptr, 0);
2307 if (endptr == arg) /* no data at all */
2309 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2312 end_index = start_index;
2313 if (*endptr == '-') {
2314 end_index = strtol(endptr + 1, &endptr, 0);
2315 if (*endptr != '\0')
2317 if (end_index < start_index)
2321 for (i = start_index; i <= end_index && slots > 0; i++) {
2324 /* remove duplicate */
2325 for (j = 0; j < offset; j++) {
2326 if (tgts[j] == i && pattern &&
2327 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2329 else if (tgts[j] == i)
2335 if (j == offset) { /* check complete */
2341 if (slots == 0 && i < end_index)
2349 if (!end_of_loop && ptr)
2352 if (!overstriped && pattern)
2353 *pattern = LLAPI_LAYOUT_DEFAULT;
2355 return rc < 0 ? rc : nr;
2358 struct lfs_setstripe_args {
2359 unsigned long long lsa_comp_end;
2360 unsigned long long lsa_stripe_size;
2361 unsigned long long lsa_extension_size;
2362 long long lsa_stripe_count;
2363 long long lsa_stripe_off;
2364 __u32 lsa_comp_flags;
2365 __u32 lsa_comp_neg_flags;
2366 unsigned long long lsa_pattern;
2367 unsigned int lsa_mirror_count;
2369 bool lsa_first_comp;
2370 bool lsa_extension_comp;
2372 char *lsa_pool_name;
2375 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2377 unsigned int mirror_count = lsa->lsa_mirror_count;
2378 bool first_comp = lsa->lsa_first_comp;
2380 memset(lsa, 0, sizeof(*lsa));
2382 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2383 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2384 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2385 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2386 lsa->lsa_pool_name = NULL;
2388 lsa->lsa_mirror_count = mirror_count;
2389 lsa->lsa_first_comp = first_comp;
2393 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2394 * @lsa: Stripe options to be initialized and inherited.
2396 * This function initializes stripe options in @lsa and inherit
2397 * stripe_size, stripe_count and OST pool_name options.
2401 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2403 unsigned long long stripe_size;
2404 long long stripe_count;
2405 char *pool_name = NULL;
2407 stripe_size = lsa->lsa_stripe_size;
2408 stripe_count = lsa->lsa_stripe_count;
2409 pool_name = lsa->lsa_pool_name;
2411 setstripe_args_init(lsa);
2413 lsa->lsa_stripe_size = stripe_size;
2414 lsa->lsa_stripe_count = stripe_count;
2415 lsa->lsa_pool_name = pool_name;
2418 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2420 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2421 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2422 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2423 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2424 lsa->lsa_comp_end != 0);
2428 * comp_args_to_layout() - Create or extend a composite layout.
2429 * @composite: Pointer to the composite layout.
2430 * @lsa: Stripe options for the new component.
2432 * This function creates or extends a composite layout by adding a new
2433 * component with stripe options from @lsa.
2435 * Return: 0 on success or an error code on failure.
2437 static int comp_args_to_layout(struct llapi_layout **composite,
2438 struct lfs_setstripe_args *lsa,
2441 struct llapi_layout *layout = *composite;
2442 uint64_t prev_end = 0;
2448 layout = llapi_layout_alloc();
2450 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2455 *composite = layout;
2456 lsa->lsa_first_comp = true;
2461 * Get current component extent, current component
2462 * must be the tail component.
2464 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2466 fprintf(stderr, "Get comp extent failed. %s\n",
2471 if (lsa->lsa_first_comp) {
2473 rc = llapi_layout_add_first_comp(layout);
2475 rc = llapi_layout_comp_add(layout);
2478 fprintf(stderr, "Add component failed. %s\n",
2484 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2486 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2487 lsa->lsa_comp_flags, strerror(errno));
2492 uint64_t comp_end = lsa->lsa_comp_end;
2495 * The extendable component is 0-length, so it can be removed
2496 * if there is insufficient space to extend it.
2498 if (lsa->lsa_extension_comp)
2499 comp_end = prev_end;
2501 rc = llapi_layout_comp_extent_set(layout, prev_end,
2504 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2505 prev_end, comp_end, strerror(errno));
2509 /* reset lsa_first_comp */
2510 lsa->lsa_first_comp = false;
2512 /* Data-on-MDT component setting */
2513 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2515 * In case of Data-on-MDT patterns the only extra option
2516 * applicable is stripe size option.
2518 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2520 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2521 lsa->lsa_stripe_count);
2525 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2527 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2528 lsa->lsa_stripe_size);
2532 if (lsa->lsa_nr_tgts != 0) {
2534 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2539 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2541 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2542 lsa->lsa_stripe_off);
2546 if (lsa->lsa_pool_name != 0) {
2548 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2549 lsa->lsa_pool_name);
2554 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2556 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2561 /* Data-on-MDT component has always single stripe up to end */
2562 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2563 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2564 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2566 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2573 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2574 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2576 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2577 rc = llapi_layout_extension_size_set(layout, size);
2579 rc = llapi_layout_stripe_size_set(layout, size);
2582 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2583 size, strerror(errno));
2587 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2589 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2590 lsa->lsa_stripe_count, strerror(errno));
2594 if (lsa->lsa_pool_name) {
2595 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2597 fprintf(stderr, "Set pool name: %s failed. %s\n",
2598 lsa->lsa_pool_name, strerror(errno));
2602 rc = llapi_layout_pool_name_set(layout, "");
2604 fprintf(stderr, "Clear pool name failed: %s\n",
2610 if (lsa->lsa_nr_tgts > 0) {
2611 if (lsa->lsa_stripe_count > 0 &&
2612 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2613 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2614 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2615 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2616 lsa->lsa_stripe_count,
2621 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2622 rc = llapi_layout_ost_index_set(layout, i,
2627 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2628 lsa->lsa_stripe_off != -1) {
2629 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2632 fprintf(stderr, "Set ost index %d failed. %s\n",
2633 i, strerror(errno));
2637 /* Create the second, virtual component of extension space */
2638 if (lsa->lsa_extension_comp) {
2639 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2640 lsa->lsa_extension_comp = false;
2647 static int build_component(struct llapi_layout **layout,
2648 struct lfs_setstripe_args *lsa, bool set_extent)
2652 rc = comp_args_to_layout(layout, lsa, set_extent);
2656 if (lsa->lsa_mirror_count > 0) {
2657 rc = llapi_layout_mirror_count_set(*layout,
2658 lsa->lsa_mirror_count);
2662 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2665 lsa->lsa_mirror_count = 0;
2671 static int build_prev_component(struct llapi_layout **layout,
2672 struct lfs_setstripe_args *prev,
2673 struct lfs_setstripe_args *lsa,
2676 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2679 if (prev->lsa_stripe_size) {
2681 prev->lsa_comp_end = lsa->lsa_comp_end;
2682 prev->lsa_extension_size = lsa->lsa_extension_size;
2683 prev->lsa_extension_comp = true;
2686 rc = build_component(layout, prev, true);
2692 * Copy lsa to previous lsa;
2693 * if this is an extension component, make the previous invalid;
2696 prev->lsa_stripe_size = 0;
2703 static int build_layout_from_yaml_node(struct cYAML *node,
2704 struct llapi_layout **layout,
2705 struct lfs_setstripe_args *lsa,
2706 struct lfs_setstripe_args *prevp)
2708 struct lfs_setstripe_args prev = { 0 };
2709 __u32 *osts = lsa->lsa_tgts;
2717 string = node->cy_string;
2719 if (node->cy_type == CYAML_TYPE_OBJECT) {
2720 /* go deep to sub blocks */
2721 if (string && !strncmp(string, "component", 9) &&
2722 strncmp(string, "component0", 10) &&
2723 strncmp(string, "components", 10)) {
2724 rc = build_prev_component(layout, prevp, lsa,
2729 /* initialize lsa. */
2730 setstripe_args_init(lsa);
2731 lsa->lsa_first_comp = false;
2732 lsa->lsa_tgts = osts;
2735 rc = build_layout_from_yaml_node(node->cy_child, layout,
2740 if (!node->cy_string)
2743 /* skip leading lmm_ if present, to simplify parsing */
2744 if (strncmp(string, "lmm_", 4) == 0)
2747 if (node->cy_type == CYAML_TYPE_STRING) {
2748 if (!strcmp(string, "lcme_extent.e_end")) {
2749 if (!strcmp(node->cy_valuestring, "EOF") ||
2750 !strcmp(node->cy_valuestring, "eof"))
2751 lsa->lsa_comp_end = LUSTRE_EOF;
2752 } else if (!strcmp(string, "pool")) {
2753 lsa->lsa_pool_name = node->cy_valuestring;
2754 } else if (!strcmp(string, "pattern")) {
2755 if (!strcmp(node->cy_valuestring, "mdt"))
2756 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2757 if (!strcmp(node->cy_valuestring,
2758 "raid0,overstriped"))
2760 LLAPI_LAYOUT_OVERSTRIPING;
2761 } else if (!strcmp(string, "lcme_flags")) {
2762 rc = comp_str2flags(node->cy_valuestring,
2763 &lsa->lsa_comp_flags,
2764 &lsa->lsa_comp_neg_flags);
2768 * Only template flags have meaning in
2769 * the layout for a new file
2771 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2773 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2774 if (!strcmp(string, "lcm_mirror_count")) {
2775 lsa->lsa_mirror_count = node->cy_valueint;
2776 } else if (!strcmp(string, "lcme_extent.e_start")) {
2777 if (node->cy_valueint == 0)
2778 lsa->lsa_first_comp = true;
2779 } else if (!strcmp(string, "lcme_extent.e_end")) {
2780 if (node->cy_valueint == -1)
2781 lsa->lsa_comp_end = LUSTRE_EOF;
2783 lsa->lsa_comp_end = node->cy_valueint;
2784 } else if (!strcmp(string, "stripe_count")) {
2785 lsa->lsa_stripe_count = node->cy_valueint;
2786 } else if (!strcmp(string, "stripe_size")) {
2787 lsa->lsa_stripe_size = node->cy_valueint;
2788 } else if (!strcmp(string, "extension_size")) {
2789 lsa->lsa_extension_size = node->cy_valueint;
2790 lsa->lsa_extension_comp = true;
2791 } else if (!strcmp(string, "stripe_offset")) {
2792 lsa->lsa_stripe_off = node->cy_valueint;
2793 } else if (!strcmp(string, "l_ost_idx")) {
2794 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2799 node = node->cy_next;
2802 if (prevp == &prev) {
2803 rc = build_prev_component(layout, prevp, lsa, true);
2807 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2808 rc = build_component(layout, lsa, *layout != NULL);
2814 static int lfs_comp_create_from_yaml(char *template,
2815 struct llapi_layout **layout,
2816 struct lfs_setstripe_args *lsa,
2819 struct cYAML *tree = NULL, *err_rc = NULL;
2822 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2824 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2825 progname, template);
2826 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2827 "can't parse", &err_rc);
2828 cYAML_print_tree2file(stderr, err_rc);
2829 cYAML_free_tree(err_rc);
2834 /* initialize lsa for plain file */
2835 setstripe_args_init(lsa);
2836 lsa->lsa_tgts = osts;
2838 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2840 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2841 progname, template);
2844 /* clean clean lsa */
2845 setstripe_args_init(lsa);
2849 cYAML_free_tree(tree);
2854 * Get the extension size from the next (SEL) component and extend the
2855 * current component on it. The start of the next component is to be
2858 * \param[in] layout the current layout
2859 * \param[in] start the start of the current component
2860 * \param[in,out] end the end of the current component
2861 * \param[in] offset the offset to adjust the end position to instead of
2864 * \retval 0 - extended successfully
2865 * \retval < 0 - error
2867 static int layout_extend_comp(struct llapi_layout *layout,
2868 uint64_t start, uint64_t *end,
2871 uint64_t size, next_start, next_end;
2874 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2877 "%s setstripe: cannot move component cursor: %s\n",
2878 progname, strerror(errno));
2883 * Even if the @size will not be used below, this will fail if
2884 * this is not a SEL component - a good confirmation we are
2885 * working on right components.
2887 rc = llapi_layout_extension_size_get(layout, &size);
2890 "%s setstripe: cannot get component ext size: %s\n",
2891 progname, strerror(errno));
2895 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2897 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2898 progname, strerror(errno));
2902 next_start += offset ?: size;
2903 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2905 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2906 progname, strerror(errno));
2910 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2913 "%s setstripe: cannot move component cursor: %s\n",
2914 progname, strerror(errno));
2918 *end += offset ?: size;
2919 rc = llapi_layout_comp_extent_set(layout, start, *end);
2921 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2922 progname, strerror(errno));
2930 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2931 * end of the last component in the existing file, and adjust the
2932 * first extent start of the components to be added accordingly.
2934 * In the create mode, we need to check if the first component is an extendable
2935 * SEL component and extend its length to the extension size (first component
2936 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2938 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2941 struct llapi_layout *head;
2942 uint64_t start = 0, prev_end = 0;
2946 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
2951 head = llapi_layout_get_by_path(fname, 0);
2954 "%s setstripe: cannot read layout from '%s': %s\n",
2955 progname, fname, strerror(errno));
2957 } else if (errno == ENODATA) {
2959 * file without LOVEA, this component-add will be turned
2960 * into a component-create.
2962 llapi_layout_free(head);
2966 * the new layout will be added to an empty one, it
2967 * still needs to be adjusted below
2971 } else if (!llapi_layout_is_composite(head)) {
2973 "%s setstripe: '%s' not a composite file\n",
2975 llapi_layout_free(head);
2979 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2982 "%s setstripe: cannot get prev extent: %s\n",
2983 progname, strerror(errno));
2984 llapi_layout_free(head);
2988 llapi_layout_free(head);
2992 /* Make sure we use the first component of the layout to be added. */
2993 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2996 "%s setstripe: cannot move component cursor: %s\n",
2997 progname, strerror(errno));
3001 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3003 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3004 progname, strerror(errno));
3008 if (start == 0 && end == 0) {
3009 rc = layout_extend_comp(layout, start, &end,
3010 comp_add ? prev_end : 0);
3015 if (start > prev_end || end < prev_end) {
3017 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3018 progname, start, end, prev_end);
3022 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3025 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3026 progname, prev_end, end, strerror(errno));
3033 static int mirror_adjust_first_extents(struct mirror_args *list)
3041 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3044 list = list->m_next;
3050 static inline bool arg_is_eof(char *arg)
3052 return !strncmp(arg, "-1", strlen("-1")) ||
3053 !strncmp(arg, "EOF", strlen("EOF")) ||
3054 !strncmp(arg, "eof", strlen("eof"));
3058 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3060 * Return: Valid mirror_args pointer on success and
3061 * NULL if memory allocation fails.
3063 static struct mirror_args *lfs_mirror_alloc(void)
3065 struct mirror_args *mirror = NULL;
3068 mirror = calloc(1, sizeof(*mirror));
3070 mirror->m_inherit = false;
3081 * lfs_mirror_free() - Free memory allocated for a mirror argument
3083 * @mirror: Previously allocated mirror argument structure by
3084 * lfs_mirror_alloc().
3086 * Free memory allocated for @mirror.
3090 static void lfs_mirror_free(struct mirror_args *mirror)
3092 if (mirror->m_layout)
3093 llapi_layout_free(mirror->m_layout);
3098 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3099 * @mirror_list: Previously allocated mirror list.
3101 * Free memory allocated for @mirror_list.
3105 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3107 struct mirror_args *next_mirror = NULL;
3109 while (mirror_list) {
3110 next_mirror = mirror_list->m_next;
3111 lfs_mirror_free(mirror_list);
3112 mirror_list = next_mirror;
3124 LFS_COMP_NO_VERIFY_OPT,
3126 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3128 LFS_MIRROR_STATE_OPT,
3130 LFS_MIRROR_INDEX_OPT,
3131 LFS_LAYOUT_FOREIGN_OPT,
3137 static int lfs_setstripe_internal(int argc, char **argv,
3138 enum setstripe_origin opc)
3140 struct lfs_setstripe_args lsa = { 0 };
3141 struct llapi_stripe_param *param = NULL;
3142 struct find_param migrate_mdt_param = {
3152 unsigned long long size_units = 1;
3153 bool migrate_mode = false;
3154 bool migrate_mdt_mode = false;
3155 bool setstripe_mode = false;
3156 bool migration_block = false;
3157 __u64 migration_flags = 0;
3158 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3159 int comp_del = 0, comp_set = 0;
3162 struct llapi_layout *layout = NULL;
3163 struct llapi_layout **lpp = &layout;
3164 bool mirror_mode = false;
3165 bool has_m_file = false;
3166 __u32 mirror_count = 0;
3167 enum mirror_flags mirror_flags = 0;
3168 struct mirror_args *mirror_list = NULL;
3169 struct mirror_args *new_mirror = NULL;
3170 struct mirror_args *last_mirror = NULL;
3171 __u16 mirror_id = 0;
3173 bool from_yaml = false;
3174 bool from_copy = false;
3175 char *template = NULL;
3176 bool foreign_mode = false;
3178 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3179 char *mode_opt = NULL;
3180 mode_t previous_umask = 0;
3181 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3183 struct option long_opts[] = {
3184 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3185 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3186 /* --block is only valid in migrate mode */
3187 { .val = 'b', .name = "block", .has_arg = no_argument },
3188 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3189 { .val = LFS_COMP_ADD_OPT,
3190 .name = "comp-add", .has_arg = no_argument },
3191 { .val = LFS_COMP_ADD_OPT,
3192 .name = "component-add", .has_arg = no_argument },
3193 { .val = LFS_COMP_DEL_OPT,
3194 .name = "comp-del", .has_arg = no_argument },
3195 { .val = LFS_COMP_DEL_OPT,
3196 .name = "component-del", .has_arg = no_argument },
3197 { .val = LFS_COMP_FLAGS_OPT,
3198 .name = "comp-flags", .has_arg = required_argument },
3199 { .val = LFS_COMP_FLAGS_OPT,
3200 .name = "component-flags",
3201 .has_arg = required_argument },
3202 { .val = LFS_COMP_SET_OPT,
3203 .name = "comp-set", .has_arg = no_argument },
3204 { .val = LFS_COMP_SET_OPT,
3205 .name = "component-set",
3206 .has_arg = no_argument},
3207 { .val = LFS_COMP_NO_VERIFY_OPT,
3208 .name = "no-verify", .has_arg = no_argument},
3209 { .val = LFS_LAYOUT_FLAGS_OPT,
3210 .name = "flags", .has_arg = required_argument},
3211 { .val = LFS_LAYOUT_FOREIGN_OPT,
3212 .name = "foreign", .has_arg = optional_argument},
3213 { .val = LFS_MIRROR_ID_OPT,
3214 .name = "mirror-id", .has_arg = required_argument},
3215 { .val = LFS_MODE_OPT,
3216 .name = "mode", .has_arg = required_argument},
3217 { .val = LFS_LAYOUT_COPY,
3218 .name = "copy", .has_arg = required_argument},
3219 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3220 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3221 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3222 { .val = 'C', .name = "overstripe-count",
3223 .has_arg = required_argument},
3224 { .val = 'd', .name = "delete", .has_arg = no_argument},
3225 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3226 /* --non-direct is only valid in migrate mode */
3227 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3228 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3229 { .val = 'E', .name = "component-end",
3230 .has_arg = required_argument},
3231 { .val = 'f', .name = "file", .has_arg = required_argument },
3232 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3233 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3234 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3235 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3236 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3237 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3238 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3239 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3240 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3241 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3242 { .val = 'L', .name = "layout", .has_arg = required_argument },
3243 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3244 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3245 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3246 /* --non-block is only valid in migrate mode */
3247 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3248 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3249 { .val = 'o', .name = "ost", .has_arg = required_argument },
3250 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3251 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3252 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3254 { .val = 'p', .name = "pool", .has_arg = required_argument },
3255 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3256 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3257 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3258 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3259 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3260 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3261 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3262 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3263 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3264 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3265 /* --verbose is only valid in migrate mode */
3266 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3267 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3268 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3269 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3270 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3273 setstripe_args_init(&lsa);
3275 migrate_mode = (opc == SO_MIGRATE);
3276 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3277 setstripe_mode = (opc == SO_SETSTRIPE);
3278 if (opc == SO_MIRROR_DELETE) {
3280 mirror_flags = MF_DESTROY;
3283 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3285 while ((c = getopt_long(argc, argv,
3286 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3287 long_opts, NULL)) >= 0) {
3293 case LFS_COMP_ADD_OPT:
3296 case LFS_COMP_DEL_OPT:
3299 case LFS_COMP_FLAGS_OPT:
3300 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3301 &lsa.lsa_comp_neg_flags);
3304 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3306 "%s: inverted flags are not supported\n",
3311 case LFS_COMP_SET_OPT:
3314 case LFS_COMP_NO_VERIFY_OPT:
3315 mirror_flags |= MF_NO_VERIFY;
3317 case LFS_MIRROR_ID_OPT:
3318 mirror_id = strtoul(optarg, &end, 0);
3319 if (*end != '\0' || mirror_id == 0) {
3321 "%s %s: invalid mirror ID '%s'\n",
3322 progname, argv[0], optarg);
3326 case LFS_LAYOUT_FLAGS_OPT: {
3329 /* check for numeric flags (foreign and mirror cases) */
3330 if (setstripe_mode && !mirror_mode && !last_mirror) {
3331 flags = strtoul(optarg, &end, 16);
3334 "%s %s: bad flags '%s'\n",
3335 progname, argv[0], optarg);
3341 if (!mirror_mode || !last_mirror) {
3343 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3348 result = comp_str2flags(optarg, &last_mirror->m_flags,
3355 "%s: inverted flags are not supported\n",
3360 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3362 "%s: unsupported mirror flags: %s\n",
3369 case LFS_LAYOUT_FOREIGN_OPT:
3371 /* check pure numeric */
3372 type = strtoul(optarg, &end, 0);
3375 type = check_foreign_type_name(optarg);
3376 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3378 "%s %s: unrecognized foreign type '%s'\n",
3385 foreign_mode = true;
3390 mode = strtoul(mode_opt, &end, 8);
3393 "%s %s: bad mode '%s'\n",
3394 progname, argv[0], mode_opt);
3397 previous_umask = umask(0);
3400 case LFS_LAYOUT_COPY:
3405 if (!migrate_mode) {
3407 "%s %s: -b|--block valid only for migrate command\n",
3411 migration_block = true;
3414 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3416 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3420 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3423 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3426 "%s %s: invalid stripe count '%s'\n",
3427 progname, argv[0], optarg);
3431 if (lsa.lsa_stripe_count == -1)
3432 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3435 /* delete the default striping pattern */
3437 if (opc == SO_MIRROR_SPLIT) {
3440 "%s %s: -d cannot used with -f\n",
3444 mirror_flags |= MF_DESTROY;
3448 if (!migrate_mode) {
3450 "%s %s: -D|--non-direct is valid only for migrate command\n",
3454 migration_flags |= MIGRATION_NONDIRECT;
3457 if (lsa.lsa_comp_end != 0) {
3458 result = comp_args_to_layout(lpp, &lsa, true);
3460 fprintf(stderr, "%s: invalid layout\n",
3465 setstripe_args_init_inherit(&lsa);
3468 if (arg_is_eof(optarg)) {
3469 lsa.lsa_comp_end = LUSTRE_EOF;
3471 result = llapi_parse_size(optarg,
3476 "%s %s: invalid component end '%s'\n",
3477 progname, argv[0], optarg);
3483 if (!migrate_mode) {
3485 "--mdt-hash is valid only for migrate command\n");
3489 lsa.lsa_pattern = check_hashtype(optarg);
3490 if (lsa.lsa_pattern == 0) {
3492 "%s %s: bad stripe hash type '%s'\n",
3493 progname, argv[0], optarg);
3498 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3501 "%s %s: invalid stripe offset '%s'\n",
3502 progname, argv[0], optarg);
3505 if (lsa.lsa_stripe_off == -1)
3506 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3509 comp_id = strtoul(optarg, &end, 0);
3510 if (*end != '\0' || comp_id == 0 ||
3511 comp_id > LCME_ID_MAX) {
3513 "%s %s: invalid component ID '%s'\n",
3514 progname, argv[0], optarg);
3519 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3521 "error: %s: invalid option: %s\n",
3522 progname, argv[optopt + 1]);
3525 if (opc == SO_MIRROR_EXTEND) {
3528 "error: %s: '-N' must exist in front of '%s'\n",
3529 progname, argv[optopt + 1]);
3532 last_mirror->m_file = optarg;
3533 last_mirror->m_count = 1;
3537 mirror_list = lfs_mirror_alloc();
3538 mirror_list->m_file = optarg;
3543 if (strcmp(argv[optind - 1], "mdt") == 0) {
3544 /* Can be only the first component */
3548 "error: 'mdt' layout can be only the first one\n");
3551 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3554 "error: 'mdt' layout size is too big\n");
3557 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3558 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3561 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3567 if (!migrate_mode) {
3569 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3573 migrate_mdt_mode = true;
3574 lsa.lsa_nr_tgts = parse_targets(tgts,
3575 sizeof(tgts) / sizeof(__u32),
3576 lsa.lsa_nr_tgts, optarg, NULL);
3577 if (lsa.lsa_nr_tgts < 0) {
3579 "%s %s: invalid MDT target(s) '%s'\n",
3580 progname, argv[0], optarg);
3584 lsa.lsa_tgts = tgts;
3585 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3586 lsa.lsa_stripe_off = tgts[0];
3589 if (!migrate_mode) {
3591 "%s %s: -n|--non-block valid only for migrate command\n",
3595 migration_flags |= MIGRATION_NONBLOCK;
3598 if (opc == SO_SETSTRIPE) {
3599 opc = SO_MIRROR_CREATE;
3604 mirror_count = strtoul(optarg, &end, 0);
3605 if (*end != '\0' || mirror_count == 0) {
3607 "error: %s: bad mirror count: %s\n",
3614 new_mirror = lfs_mirror_alloc();
3615 new_mirror->m_count = mirror_count;
3618 mirror_list = new_mirror;
3621 /* wrap up last mirror */
3622 if (!setstripe_args_specified(&lsa))
3623 last_mirror->m_inherit = true;
3624 if (lsa.lsa_comp_end == 0)
3625 lsa.lsa_comp_end = LUSTRE_EOF;
3627 result = comp_args_to_layout(lpp, &lsa, true);
3629 lfs_mirror_free(new_mirror);
3633 setstripe_args_init_inherit(&lsa);
3635 last_mirror->m_next = new_mirror;
3638 last_mirror = new_mirror;
3639 lpp = &last_mirror->m_layout;
3642 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3643 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3645 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3647 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3649 "%s %s: -o|--ost incompatible with DoM layout\n",
3654 * -o allows overstriping, and must note it because
3655 * parse_targets is shared with MDT striping, which
3656 * does not allow duplicates
3658 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3659 lsa.lsa_nr_tgts = parse_targets(tgts,
3660 sizeof(tgts) / sizeof(__u32),
3661 lsa.lsa_nr_tgts, optarg,
3663 if (lsa.lsa_nr_tgts < 0) {
3665 "%s %s: invalid OST target(s) '%s'\n",
3666 progname, argv[0], optarg);
3670 lsa.lsa_tgts = tgts;
3671 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3672 lsa.lsa_stripe_off = tgts[0];
3677 lsa.lsa_pool_name = optarg;
3679 if (strlen(lsa.lsa_pool_name) == 0 ||
3680 strncmp(lsa.lsa_pool_name, "none",
3681 LOV_MAXPOOLNAME) == 0)
3682 lsa.lsa_pool_name = NULL;
3685 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3689 "%s %s: invalid stripe size '%s'\n",
3690 progname, argv[0], optarg);
3695 if (!migrate_mode) {
3697 "%s %s: -v|--verbose valid only for migrate command\n",
3701 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3702 migration_flags = MIGRATION_VERBOSE;
3712 result = llapi_parse_size(optarg,
3713 &lsa.lsa_extension_size,
3717 "%s %s: invalid extension size '%s'\n",
3718 progname, argv[0], optarg);
3722 lsa.lsa_extension_comp = true;
3725 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3726 progname, argv[0], argv[optind - 1]);
3731 fname = argv[optind];
3733 if (optind == argc) {
3734 fprintf(stderr, "%s %s: FILE must be specified\n",
3739 /* lfs migrate $filename should keep the file's layout by default */
3740 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3744 if (xattr && !foreign_mode) {
3746 * only print a warning as this is harmless and will be ignored
3749 "%s %s: xattr has been specified for non-foreign layout\n",
3751 } else if (foreign_mode && !xattr) {
3753 "%s %s: xattr must be provided in foreign mode\n",
3758 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3759 comp_set || comp_id || delete || from_copy ||
3760 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3763 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3768 if (mirror_mode && mirror_count == 0) {
3770 "error: %s: --mirror-count|-N option is required\n",
3777 if (!setstripe_args_specified(&lsa))
3778 last_mirror->m_inherit = true;
3779 if (lsa.lsa_comp_end == 0)
3780 lsa.lsa_comp_end = LUSTRE_EOF;
3783 if (lsa.lsa_comp_end != 0) {
3784 result = comp_args_to_layout(lpp, &lsa, true);
3786 fprintf(stderr, "error: %s: invalid layout\n",
3793 if (mirror_flags & MF_NO_VERIFY) {
3794 if (opc != SO_MIRROR_EXTEND) {
3796 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3800 } else if (!has_m_file) {
3802 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3809 if (comp_set && !comp_id) {
3811 "%s %s: --component-set doesn't have component-id set\n",
3816 if ((delete + comp_set + comp_del + comp_add) > 1) {
3818 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3823 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3824 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3826 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3831 if ((comp_set || comp_del) &&
3832 (setstripe_args_specified(&lsa) || layout != NULL)) {
3834 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3839 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3841 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3846 if (comp_add || comp_del) {
3849 result = lstat(fname, &st);
3850 if (result == 0 && S_ISDIR(st.st_mode)) {
3852 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3859 "error: %s: can't use --component-add or --component-del for mirror operation\n",
3868 "%s %s: option -E must be specified with --component-add\n",
3874 if (from_yaml && from_copy) {
3876 "%s: can't specify --yaml and --copy together\n",
3881 if ((from_yaml || from_copy) &&
3882 (setstripe_args_specified(&lsa) || layout != NULL)) {
3884 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
3889 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3891 "%s %s: options --non-block and --block are mutually exclusive\n",
3896 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3897 opc != SO_MIRROR_DELETE && comp_id != 0) {
3899 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3904 if (migrate_mdt_mode) {
3905 struct lmv_user_md *lmu;
3907 /* initialize migrate mdt parameters */
3908 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3909 LMV_USER_MAGIC_SPECIFIC));
3912 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3913 progname, argv[0], strerror(ENOMEM));
3917 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3918 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3919 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3921 "%s %s: migrate should specify MDT index\n",
3926 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3927 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3928 lmu->lum_hash_type = lsa.lsa_pattern;
3930 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
3931 if (lsa.lsa_pool_name) {
3932 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3933 sizeof(lmu->lum_pool_name) - 1);
3934 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3936 if (lsa.lsa_nr_tgts > 1) {
3939 if (lsa.lsa_stripe_count > 0 &&
3940 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3941 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3943 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3944 progname, lsa.lsa_stripe_count,
3950 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3951 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3952 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3953 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3955 lmu->lum_magic = LMV_USER_MAGIC;
3958 migrate_mdt_param.fp_lmv_md = lmu;
3959 migrate_mdt_param.fp_migrate = 1;
3960 } else if (!layout) {
3961 /* initialize stripe parameters */
3962 param = calloc(1, offsetof(typeof(*param),
3963 lsp_osts[lsa.lsa_nr_tgts]));
3966 "%s %s: cannot allocate memory for parameters: %s\n",
3967 progname, argv[0], strerror(ENOMEM));
3972 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3973 param->lsp_stripe_size = lsa.lsa_stripe_size;
3974 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3975 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3976 param->lsp_stripe_count = -1;
3978 param->lsp_stripe_count = lsa.lsa_stripe_count;
3980 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3981 param->lsp_stripe_offset = -1;
3983 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3984 param->lsp_stripe_pattern =
3985 llapi_pattern_to_lov(lsa.lsa_pattern);
3986 if (param->lsp_stripe_pattern == EINVAL) {
3987 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3992 param->lsp_pool = lsa.lsa_pool_name;
3993 param->lsp_is_specific = false;
3994 if (lsa.lsa_nr_tgts > 0) {
3995 if (lsa.lsa_stripe_count > 0 &&
3996 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3997 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3998 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4000 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4001 argv[0], lsa.lsa_stripe_count,
4007 param->lsp_is_specific = true;
4008 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4009 memcpy(param->lsp_osts, tgts,
4010 sizeof(*tgts) * lsa.lsa_nr_tgts);
4015 /* generate a layout from a YAML template */
4016 result = lfs_comp_create_from_yaml(template, &layout,
4020 "error: %s: can't create composite layout from template file %s\n",
4026 if (layout != NULL || mirror_list != NULL) {
4028 result = mirror_adjust_first_extents(mirror_list);
4030 result = layout_adjust_first_extent(fname, layout,
4032 if (result == -ENODATA)
4034 else if (result != 0) {
4035 fprintf(stderr, "error: %s: invalid layout\n",
4041 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4043 layout = llapi_layout_get_by_path(template ?: fname, 0);
4046 "%s: can't create composite layout from file %s: %s\n",
4047 progname, template ?: fname,
4054 if (migrate_mdt_mode) {
4055 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4056 } else if (migrate_mode) {
4057 result = lfs_migrate(fname, migration_flags, param,
4059 } else if (comp_set != 0) {
4060 result = lfs_component_set(fname, comp_id,
4062 lsa.lsa_comp_neg_flags);
4063 } else if (comp_del != 0) {
4064 result = lfs_component_del(fname, comp_id,
4066 lsa.lsa_comp_neg_flags);
4067 } else if (comp_add != 0) {
4068 result = lfs_component_add(fname, layout);
4069 } else if (opc == SO_MIRROR_CREATE) {
4070 result = mirror_create(fname, mirror_list);
4071 } else if (opc == SO_MIRROR_EXTEND) {
4072 result = mirror_extend(fname, mirror_list,
4074 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4075 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4077 "%s: no mirror specified to delete from '%s'\n",
4081 if (lsa.lsa_pool_name)
4082 mirror_flags |= MF_COMP_POOL;
4083 else if (mirror_id != 0)
4084 comp_id = mirror_id;
4086 mirror_flags |= MF_COMP_ID;
4087 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4089 has_m_file ? mirror_list->m_file :
4091 } else if (layout) {
4092 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4098 } else if (foreign_mode) {
4099 result = llapi_file_create_foreign(fname, mode, type,
4106 result = llapi_file_open_param(fname,
4115 /* Save the first error encountered. */
4123 umask(previous_umask);
4126 free(migrate_mdt_param.fp_lmv_md);
4127 llapi_layout_free(layout);
4128 lfs_mirror_list_free(mirror_list);
4133 llapi_layout_free(layout);
4134 lfs_mirror_list_free(mirror_list);
4138 static int lfs_poollist(int argc, char **argv)
4143 return llapi_poollist(argv[1]);
4146 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4147 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4152 char *endptr = "AD";
4157 else if (str[0] == '-')
4163 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4164 long long val = strtoll(timebuf, &endptr, 0);
4169 unit *= 52; /* 52 weeks + 1 day below */
4170 case 'w': /* fallthrough */
4172 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4173 param->fp_time_margin *= (1 + unit / 52);
4174 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4175 case '\0': /* days are default unit if none used */
4176 case 'd': /* fallthrough */
4178 case 'h': /* fallthrough */
4180 case 'm': /* fallthrough */
4182 case 's': /* fallthrough */
4184 /* don't need to multiply by 1 for seconds */
4187 "%s find: bad time string '%s': %s\n",
4188 progname, timebuf, strerror(EINVAL));
4192 if (param->fp_time_margin == 0 ||
4193 (*endptr && unit < param->fp_time_margin))
4194 param->fp_time_margin = unit;
4201 fprintf(stderr, "%s find: bad time '%s': too large\n",
4211 static int str2quotaid(__u32 *id, const char *arg)
4213 unsigned long int projid_tmp = 0;
4214 char *endptr = NULL;
4216 projid_tmp = strtoul(arg, &endptr, 10);
4217 if (*endptr != '\0')
4219 if (projid_tmp >= UINT32_MAX)
4226 static int name2uid(unsigned int *id, const char *name)
4228 struct passwd *passwd;
4230 passwd = getpwnam(name);
4233 *id = passwd->pw_uid;
4238 static int name2gid(unsigned int *id, const char *name)
4240 struct group *group;
4242 group = getgrnam(name);
4245 *id = group->gr_gid;
4250 static inline int name2projid(unsigned int *id, const char *name)
4255 static int uid2name(char **name, unsigned int id)
4257 struct passwd *passwd;
4259 passwd = getpwuid(id);
4262 *name = passwd->pw_name;
4267 static inline int gid2name(char **name, unsigned int id)
4269 struct group *group;
4271 group = getgrgid(id);
4274 *name = group->gr_name;
4279 static int name2layout(__u32 *layout, char *name)
4281 char *ptr, *layout_name;
4284 for (ptr = name; ; ptr = NULL) {
4285 layout_name = strtok(ptr, ",");
4288 if (strcmp(layout_name, "released") == 0)
4289 *layout |= LOV_PATTERN_F_RELEASED;
4290 else if (strcmp(layout_name, "raid0") == 0)
4291 *layout |= LOV_PATTERN_RAID0;
4292 else if (strcmp(layout_name, "mdt") == 0)
4293 *layout |= LOV_PATTERN_MDT;
4294 else if (strcmp(layout_name, "overstriping") == 0)
4295 *layout |= LOV_PATTERN_OVERSTRIPING;
4302 static int lfs_find(int argc, char **argv)
4307 struct find_param param = {
4310 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4312 struct option long_opts[] = {
4313 { .val = 'A', .name = "atime", .has_arg = required_argument },
4314 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4315 { .val = 'B', .name = "btime", .has_arg = required_argument },
4316 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4317 { .val = LFS_COMP_COUNT_OPT,
4318 .name = "comp-count", .has_arg = required_argument },
4319 { .val = LFS_COMP_COUNT_OPT,
4320 .name = "component-count",
4321 .has_arg = required_argument },
4322 { .val = LFS_COMP_FLAGS_OPT,
4323 .name = "comp-flags", .has_arg = required_argument },
4324 { .val = LFS_COMP_FLAGS_OPT,
4325 .name = "component-flags",
4326 .has_arg = required_argument },
4327 { .val = LFS_COMP_START_OPT,
4328 .name = "comp-start", .has_arg = required_argument },
4329 { .val = LFS_COMP_START_OPT,
4330 .name = "component-start",
4331 .has_arg = required_argument },
4332 { .val = LFS_MIRROR_STATE_OPT,
4333 .name = "mirror-state", .has_arg = required_argument },
4334 { .val = LFS_LAYOUT_FOREIGN_OPT,
4335 .name = "foreign", .has_arg = optional_argument},
4336 { .val = LFS_NEWERXY_OPT,
4337 .name = "newer", .has_arg = required_argument},
4338 { .val = LFS_NEWERXY_OPT,
4339 .name = "neweraa", .has_arg = required_argument},
4340 { .val = LFS_NEWERXY_OPT,
4341 .name = "neweram", .has_arg = required_argument},
4342 { .val = LFS_NEWERXY_OPT,
4343 .name = "newerac", .has_arg = required_argument},
4344 { .val = LFS_NEWERXY_OPT,
4345 .name = "newerab", .has_arg = required_argument},
4346 { .val = LFS_NEWERXY_OPT,
4347 .name = "newerma", .has_arg = required_argument},
4348 { .val = LFS_NEWERXY_OPT,
4349 .name = "newermm", .has_arg = required_argument},
4350 { .val = LFS_NEWERXY_OPT,
4351 .name = "newermc", .has_arg = required_argument},
4352 { .val = LFS_NEWERXY_OPT,
4353 .name = "newermb", .has_arg = required_argument},
4354 { .val = LFS_NEWERXY_OPT,
4355 .name = "newerca", .has_arg = required_argument},
4356 { .val = LFS_NEWERXY_OPT,
4357 .name = "newercm", .has_arg = required_argument},
4358 { .val = LFS_NEWERXY_OPT,
4359 .name = "newercc", .has_arg = required_argument},
4360 { .val = LFS_NEWERXY_OPT,
4361 .name = "newercb", .has_arg = required_argument},
4362 { .val = LFS_NEWERXY_OPT,
4363 .name = "newerba", .has_arg = required_argument},
4364 { .val = LFS_NEWERXY_OPT,
4365 .name = "newerbm", .has_arg = required_argument},
4366 { .val = LFS_NEWERXY_OPT,
4367 .name = "newerbc", .has_arg = required_argument},
4368 { .val = LFS_NEWERXY_OPT,
4369 .name = "newerbb", .has_arg = required_argument},
4370 { .val = LFS_NEWERXY_OPT,
4371 .name = "newerBa", .has_arg = required_argument},
4372 { .val = LFS_NEWERXY_OPT,
4373 .name = "newerBm", .has_arg = required_argument},
4374 { .val = LFS_NEWERXY_OPT,
4375 .name = "newerBc", .has_arg = required_argument},
4376 { .val = LFS_NEWERXY_OPT,
4377 .name = "newerBB", .has_arg = required_argument},
4378 { .val = LFS_NEWERXY_OPT,
4379 .name = "newerat", .has_arg = required_argument},
4380 { .val = LFS_NEWERXY_OPT,
4381 .name = "newermt", .has_arg = required_argument},
4382 { .val = LFS_NEWERXY_OPT,
4383 .name = "newerct", .has_arg = required_argument},
4384 { .val = LFS_NEWERXY_OPT,
4385 .name = "newerbt", .has_arg = required_argument},
4386 { .val = LFS_NEWERXY_OPT,
4387 .name = "newerBt", .has_arg = required_argument},
4388 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4389 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4390 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4391 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4392 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4393 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4394 { .val = 'E', .name = "component-end",
4395 .has_arg = required_argument },
4396 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4397 { .val = LFS_LAYOUT_FOREIGN_OPT,
4398 .name = "foreign", .has_arg = optional_argument},
4399 { .val = 'g', .name = "gid", .has_arg = required_argument },
4400 { .val = 'G', .name = "group", .has_arg = required_argument },
4401 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4402 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4403 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4404 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4405 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4406 { .val = 'L', .name = "layout", .has_arg = required_argument },
4407 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4408 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4409 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4410 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4411 { .val = 'n', .name = "name", .has_arg = required_argument },
4412 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4413 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4414 { .val = 'O', .name = "obd", .has_arg = required_argument },
4415 { .val = 'O', .name = "ost", .has_arg = required_argument },
4416 /* no short option for pool yet, can be 'p' after 2.18 */
4417 { .val = LFS_POOL_OPT,
4418 .name = "pool", .has_arg = required_argument },
4419 { .val = '0', .name = "print0", .has_arg = no_argument },
4420 { .val = 'P', .name = "print", .has_arg = no_argument },
4421 { .val = LFS_PROJID_OPT,
4422 .name = "projid", .has_arg = required_argument },
4423 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4424 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4425 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4426 { .val = 's', .name = "size", .has_arg = required_argument },
4427 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4428 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4429 { .val = 't', .name = "type", .has_arg = required_argument },
4430 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4431 { .val = 'u', .name = "uid", .has_arg = required_argument },
4432 { .val = 'U', .name = "user", .has_arg = required_argument },
4433 { .val = 'z', .name = "extension-size",
4434 .has_arg = required_argument },
4435 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4436 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4437 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4451 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4452 while ((c = getopt_long_only(argc, argv,
4453 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4454 long_opts, &optidx)) >= 0) {
4459 /* '!' is part of option */
4461 * when getopt_long_only() finds a string which is not
4462 * an option nor a known option argument it returns 1
4463 * in that case if we already have found pathstart and pathend
4464 * (i.e. we have the list of pathnames),
4465 * the only supported value is "!"
4467 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4468 if (!isoption && pathend != -1) {
4470 "err: %s: filename|dirname must either precede options or follow options\n",
4475 if (!isoption && pathstart == -1)
4476 pathstart = optind - 1;
4477 if (isoption && pathstart != -1 && pathend == -1)
4478 pathend = optind - 2;
4485 * unknown; opt is "!" or path component,
4486 * checking done above.
4488 if (strcmp(optarg, "!") == 0)
4492 xtime = ¶m.fp_atime;
4493 xsign = ¶m.fp_asign;
4494 param.fp_exclude_atime = !!neg_opt;
4495 /* no break, this falls through to 'B' for btime */
4498 xtime = ¶m.fp_btime;
4499 xsign = ¶m.fp_bsign;
4500 param.fp_exclude_btime = !!neg_opt;
4502 /* no break, this falls through to 'C' for ctime */
4505 xtime = ¶m.fp_ctime;
4506 xsign = ¶m.fp_csign;
4507 param.fp_exclude_ctime = !!neg_opt;
4509 /* no break, this falls through to 'M' for mtime */
4512 xtime = ¶m.fp_mtime;
4513 xsign = ¶m.fp_msign;
4514 param.fp_exclude_mtime = !!neg_opt;
4516 rc = set_time(¶m, &t, xtime, optarg);
4517 if (rc == LONG_MAX) {
4525 if (optarg[0] == '+') {
4526 param.fp_blocks_sign = -1;
4528 } else if (optarg[0] == '-') {
4529 param.fp_blocks_sign = 1;
4533 param.fp_blocks_units = 1024;
4534 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4535 ¶m.fp_blocks_units, 0);
4537 fprintf(stderr, "error: bad blocks '%s'\n",
4541 param.fp_check_blocks = 1;
4542 param.fp_exclude_blocks = !!neg_opt;
4544 case LFS_COMP_COUNT_OPT:
4545 if (optarg[0] == '+') {
4546 param.fp_comp_count_sign = -1;
4548 } else if (optarg[0] == '-') {
4549 param.fp_comp_count_sign = 1;
4553 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4554 if (*endptr != '\0') {
4556 "error: bad component count '%s'\n",
4560 param.fp_check_comp_count = 1;
4561 param.fp_exclude_comp_count = !!neg_opt;
4563 case LFS_COMP_FLAGS_OPT:
4564 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4565 ¶m.fp_comp_neg_flags);
4568 "error: bad component flags '%s'\n",
4572 param.fp_check_comp_flags = 1;
4574 __u32 flags = param.fp_comp_neg_flags;
4576 param.fp_comp_neg_flags = param.fp_comp_flags;
4577 param.fp_comp_flags = flags;
4580 case LFS_COMP_START_OPT:
4581 if (optarg[0] == '+') {
4582 param.fp_comp_start_sign = -1;
4584 } else if (optarg[0] == '-') {
4585 param.fp_comp_start_sign = 1;
4589 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4590 ¶m.fp_comp_start_units, 0);
4593 "error: bad component start '%s'\n",
4597 param.fp_check_comp_start = 1;
4598 param.fp_exclude_comp_start = !!neg_opt;
4600 case LFS_MIRROR_STATE_OPT:
4601 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4602 ¶m.fp_mirror_neg_state);
4605 "error: bad mirrored file state '%s'\n",
4609 param.fp_check_mirror_state = 1;
4611 __u16 state = param.fp_mirror_neg_state;
4613 param.fp_mirror_neg_state =
4614 param.fp_mirror_state;
4615 param.fp_mirror_state = state;
4619 if (optarg[0] == '+') {
4620 param.fp_stripe_count_sign = -1;
4622 } else if (optarg[0] == '-') {
4623 param.fp_stripe_count_sign = 1;
4627 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4628 if (*endptr != '\0') {
4630 "error: bad stripe_count '%s'\n",
4635 param.fp_check_stripe_count = 1;
4636 param.fp_exclude_stripe_count = !!neg_opt;
4639 param.fp_max_depth = strtol(optarg, 0, 0);
4642 if (optarg[0] == '+') {
4643 param.fp_comp_end_sign = -1;
4645 } else if (optarg[0] == '-') {
4646 param.fp_comp_end_sign = 1;
4650 if (arg_is_eof(optarg)) {
4651 param.fp_comp_end = LUSTRE_EOF;
4652 param.fp_comp_end_units = 1;
4655 rc = llapi_parse_size(optarg,
4657 ¶m.fp_comp_end_units, 0);
4661 "error: bad component end '%s'\n",
4665 param.fp_check_comp_end = 1;
4666 param.fp_exclude_comp_end = !!neg_opt;
4668 case LFS_LAYOUT_FOREIGN_OPT: {
4669 /* all types by default */
4670 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4673 /* check pure numeric */
4674 type = strtoul(optarg, &endptr, 0);
4677 type = check_foreign_type_name(optarg);
4678 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4680 "%s %s: unknown foreign type '%s'\n",
4687 param.fp_foreign_type = type;
4688 param.fp_check_foreign = 1;
4689 param.fp_exclude_foreign = !!neg_opt;
4692 case LFS_NEWERXY_OPT: {
4698 time_t ref = time(NULL);
4700 /* no need to check bad options, they won't get here */
4701 if (strlen(long_opts[optidx].name) == 7) {
4702 x = long_opts[optidx].name[5];
4703 y = long_opts[optidx].name[6];
4707 static const char *const fmts[] = {
4708 "%Y-%m-%d %H:%M:%S",
4711 "%H:%M:%S", /* sometime today */
4720 for (i = 0; fmts[i] != NULL; i++) {
4723 /* Init for times relative to today */
4724 if (strncmp(fmts[i], "%H", 2) == 0)
4725 localtime_r(&ref, &tm);
4727 memset(&tm, 0, sizeof(tm));
4728 ptr = strptime(optarg, fmts[i], &tm);
4730 while (ptr && isspace(*ptr))
4732 if (ptr == optarg + strlen(optarg)) {
4740 "%s: invalid time '%s'\n",
4743 "supported formats are:\n ");
4744 for (i = 0; fmts[i] != NULL; i++)
4745 fprintf(stderr, "'%s', ",
4747 fprintf(stderr, "\n");
4753 } else if (y == 'b' || y == 'B') {
4756 rc = llapi_get_lum_file(optarg, NULL, &stx,
4758 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4759 if (!(stx.stx_mask & STATX_BTIME))
4764 "%s: get btime failed '%s': %s\n",
4770 ref = stx.stx_btime.tv_sec;
4772 struct stat statbuf;
4774 if (stat(optarg, &statbuf) < 0) {
4776 "%s: cannot stat file '%s': %s\n",
4785 ref = statbuf.st_atime;
4788 ref = statbuf.st_mtime;
4791 ref = statbuf.st_ctime;
4795 "%s: invalid Y argument: '%c'\n",
4804 xidx = NEWERXY_ATIME;
4807 xidx = NEWERXY_MTIME;
4810 xidx = NEWERXY_CTIME;
4814 xidx = NEWERXY_BTIME;
4818 "%s: invalid X argument: '%c'\n",
4825 newery = ¶m.fp_newery[xidx][negidx];
4831 *newery = *newery > ref ? ref : *newery;
4833 *newery = *newery > ref ? *newery : ref;
4835 param.fp_newerxy = 1;
4840 rc = name2gid(¶m.fp_gid, optarg);
4842 if (str2quotaid(¶m.fp_gid, optarg)) {
4844 "Group/GID: %s cannot be found.\n",
4850 param.fp_exclude_gid = !!neg_opt;
4851 param.fp_check_gid = 1;
4854 param.fp_hash_type = check_hashtype(optarg);
4855 if (param.fp_hash_type == 0) {
4856 fprintf(stderr, "error: bad hash_type '%s'\n",
4861 param.fp_check_hash_type = 1;
4862 param.fp_exclude_hash_type = !!neg_opt;
4868 ret = name2layout(¶m.fp_layout, optarg);
4871 param.fp_exclude_layout = !!neg_opt;
4872 param.fp_check_layout = 1;
4876 rc = name2uid(¶m.fp_uid, optarg);
4878 if (str2quotaid(¶m.fp_uid, optarg)) {
4880 "User/UID: %s cannot be found.\n",
4886 param.fp_exclude_uid = !!neg_opt;
4887 param.fp_check_uid = 1;
4890 param.fp_pattern = (char *)optarg;
4891 param.fp_exclude_pattern = !!neg_opt;
4894 if (optarg[0] == '+') {
4895 param.fp_mirror_count_sign = -1;
4897 } else if (optarg[0] == '-') {
4898 param.fp_mirror_count_sign = 1;
4902 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4903 if (*endptr != '\0') {
4905 "error: bad mirror count '%s'\n",
4909 param.fp_check_mirror_count = 1;
4910 param.fp_exclude_mirror_count = !!neg_opt;
4915 char *buf, *token, *next, *p;
4919 buf = strdup(optarg);
4925 param.fp_exclude_obd = !!neg_opt;
4928 while (token && *token) {
4929 token = strchr(token, ',');
4936 param.fp_exclude_mdt = !!neg_opt;
4937 param.fp_num_alloc_mdts += len;
4938 tmp = realloc(param.fp_mdt_uuid,
4939 param.fp_num_alloc_mdts *
4940 sizeof(*param.fp_mdt_uuid));
4946 param.fp_mdt_uuid = tmp;
4948 param.fp_exclude_obd = !!neg_opt;
4949 param.fp_num_alloc_obds += len;
4950 tmp = realloc(param.fp_obd_uuid,
4951 param.fp_num_alloc_obds *
4952 sizeof(*param.fp_obd_uuid));
4958 param.fp_obd_uuid = tmp;
4960 for (token = buf; token && *token; token = next) {
4961 struct obd_uuid *puuid;
4965 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4968 ¶m.fp_obd_uuid[param.fp_num_obds++];
4970 p = strchr(token, ',');
4977 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4982 strncpy(puuid->uuid, token,
4983 sizeof(puuid->uuid));
4990 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4994 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4996 "Pool name %s is too long (max %d)\n",
4997 optarg, LOV_MAXPOOLNAME);
5002 * We do check for empty pool because empty pool
5003 * is used to find V1 LOV attributes
5005 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5006 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5007 param.fp_exclude_pool = !!neg_opt;
5008 param.fp_check_pool = 1;
5010 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5011 case 'p': /* want this for --pool, to match getstripe/find */
5013 "warning: -p deprecated, use --print0 or -0\n");
5016 param.fp_zero_end = 1;
5018 case 'P': /* we always print, this option is a no-op */
5020 case LFS_PROJID_OPT:
5021 rc = name2projid(¶m.fp_projid, optarg);
5023 if (str2quotaid(¶m.fp_projid, optarg)) {
5025 "Invalid project ID: %s\n",
5031 param.fp_exclude_projid = !!neg_opt;
5032 param.fp_check_projid = 1;
5035 if (optarg[0] == '+') {
5036 param.fp_size_sign = -1;
5038 } else if (optarg[0] == '-') {
5039 param.fp_size_sign = 1;
5043 ret = llapi_parse_size(optarg, ¶m.fp_size,
5044 ¶m.fp_size_units, 0);
5046 fprintf(stderr, "error: bad file size '%s'\n",
5050 param.fp_check_size = 1;
5051 param.fp_exclude_size = !!neg_opt;
5054 if (optarg[0] == '+') {
5055 param.fp_stripe_size_sign = -1;
5057 } else if (optarg[0] == '-') {
5058 param.fp_stripe_size_sign = 1;
5062 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5063 ¶m.fp_stripe_size_units, 0);
5065 fprintf(stderr, "error: bad stripe_size '%s'\n",
5069 param.fp_check_stripe_size = 1;
5070 param.fp_exclude_stripe_size = !!neg_opt;
5073 param.fp_exclude_type = !!neg_opt;
5074 switch (optarg[0]) {
5076 param.fp_type = S_IFBLK;
5079 param.fp_type = S_IFCHR;
5082 param.fp_type = S_IFDIR;
5085 param.fp_type = S_IFREG;
5088 param.fp_type = S_IFLNK;
5091 param.fp_type = S_IFIFO;
5094 param.fp_type = S_IFSOCK;
5097 fprintf(stderr, "error: %s: bad type '%s'\n",
5104 if (optarg[0] == '+') {
5105 param.fp_mdt_count_sign = -1;
5107 } else if (optarg[0] == '-') {
5108 param.fp_mdt_count_sign = 1;
5112 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5113 if (*endptr != '\0') {
5114 fprintf(stderr, "error: bad mdt_count '%s'\n",
5119 param.fp_check_mdt_count = 1;
5120 param.fp_exclude_mdt_count = !!neg_opt;
5123 if (optarg[0] == '+') {
5124 param.fp_ext_size_sign = -1;
5126 } else if (optarg[0] == '-') {
5127 param.fp_ext_size_sign = 1;
5131 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5132 ¶m.fp_ext_size_units, 0);
5134 fprintf(stderr, "error: bad ext-size '%s'\n",
5138 param.fp_ext_size /= SEL_UNIT_SIZE;
5139 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5140 param.fp_check_ext_size = 1;
5141 param.fp_exclude_ext_size = !!neg_opt;
5149 if (pathstart == -1) {
5150 fprintf(stderr, "error: %s: no filename|pathname\n",
5154 } else if (pathend == -1) {
5160 rc = llapi_find(argv[pathstart], ¶m);
5163 pathbad = pathstart;
5165 } while (++pathstart < pathend);
5168 fprintf(stderr, "%s: failed for '%s': %s\n",
5169 progname, argv[pathbad], strerror(-rc));
5172 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5173 free(param.fp_obd_uuid);
5175 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5176 free(param.fp_mdt_uuid);
5181 static int lfs_getstripe_internal(int argc, char **argv,
5182 struct find_param *param)
5184 struct option long_opts[] = {
5185 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5186 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5187 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5188 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5189 { .val = LFS_COMP_COUNT_OPT,
5190 .name = "comp-count", .has_arg = no_argument },
5191 { .val = LFS_COMP_COUNT_OPT,
5192 .name = "component-count", .has_arg = no_argument },
5193 { .val = LFS_COMP_FLAGS_OPT,
5194 .name = "comp-flags", .has_arg = optional_argument },
5195 { .val = LFS_COMP_FLAGS_OPT,
5196 .name = "component-flags", .has_arg = optional_argument },
5197 { .val = LFS_COMP_START_OPT,
5198 .name = "comp-start", .has_arg = optional_argument },
5199 { .val = LFS_COMP_START_OPT,
5200 .name = "component-start", .has_arg = optional_argument },
5201 { .val = LFS_MIRROR_INDEX_OPT,
5202 .name = "mirror-index", .has_arg = required_argument },
5203 { .val = LFS_MIRROR_ID_OPT,
5204 .name = "mirror-id", .has_arg = required_argument },
5205 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5206 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5207 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5208 { .val = 'd', .name = "directory", .has_arg = no_argument },
5209 { .val = 'D', .name = "default", .has_arg = no_argument },
5210 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5211 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5212 { .val = 'F', .name = "fid", .has_arg = no_argument },
5213 { .val = 'g', .name = "generation", .has_arg = no_argument },
5214 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5215 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5216 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5217 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5218 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5219 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5220 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5221 { .val = 'L', .name = "layout", .has_arg = no_argument },
5222 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5223 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5224 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5225 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5226 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5227 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5228 { .val = 'O', .name = "obd", .has_arg = required_argument },
5229 { .val = 'O', .name = "ost", .has_arg = required_argument },
5230 { .val = 'p', .name = "pool", .has_arg = no_argument },
5231 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5232 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5233 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5234 { .val = 'R', .name = "raw", .has_arg = no_argument },
5235 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5236 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5237 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5238 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5239 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5240 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5241 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5242 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5243 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5244 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5248 int pathstart = -1, pathend = -1;
5252 while ((c = getopt_long(argc, argv,
5253 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5254 long_opts, NULL)) != -1) {
5258 /* '!' is part of option */
5259 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5260 if (!isoption && pathend != -1) {
5262 "error: %s: filename|dirname must either precede options or follow options\n",
5266 if (!isoption && pathstart == -1)
5267 pathstart = optind - 1;
5268 if (isoption && pathstart != -1 && pathend == -1)
5269 pathend = optind - 2;
5273 /* unknown: opt is "!" */
5274 if (strcmp(optarg, "!") == 0)
5278 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5279 param->fp_verbose |= VERBOSE_COUNT;
5280 param->fp_max_depth = 0;
5283 case LFS_COMP_COUNT_OPT:
5284 param->fp_verbose |= VERBOSE_COMP_COUNT;
5285 param->fp_max_depth = 0;
5287 case LFS_COMP_FLAGS_OPT:
5289 rc = comp_str2flags(optarg,
5290 ¶m->fp_comp_flags,
5291 ¶m->fp_comp_neg_flags);
5294 "error: %s bad component flags '%s'.\n",
5298 param->fp_check_comp_flags = 1;
5300 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5301 param->fp_max_depth = 0;
5304 case LFS_COMP_START_OPT:
5307 if (tmp[0] == '+') {
5308 param->fp_comp_start_sign = -1;
5310 } else if (tmp[0] == '-') {
5311 param->fp_comp_start_sign = 1;
5314 rc = llapi_parse_size(tmp,
5315 ¶m->fp_comp_start,
5316 ¶m->fp_comp_start_units, 0);
5319 "error: %s bad component start '%s'.\n",
5323 param->fp_check_comp_start = 1;
5325 param->fp_verbose |= VERBOSE_COMP_START;
5326 param->fp_max_depth = 0;
5329 case LFS_MIRROR_INDEX_OPT:
5330 if (optarg[0] == '+') {
5331 param->fp_mirror_index_sign = -1;
5333 } else if (optarg[0] == '-') {
5334 param->fp_mirror_index_sign = 1;
5338 param->fp_mirror_index = strtoul(optarg, &end, 0);
5339 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5340 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5342 "%s %s: invalid mirror index '%s'\n",
5343 progname, argv[0], optarg);
5346 if (param->fp_mirror_id != 0) {
5348 "%s %s: can't specify both mirror index and mirror ID\n",
5352 param->fp_check_mirror_index = 1;
5353 param->fp_exclude_mirror_index = !!neg_opt;
5355 case LFS_MIRROR_ID_OPT:
5356 if (optarg[0] == '+') {
5357 param->fp_mirror_id_sign = -1;
5359 } else if (optarg[0] == '-') {
5360 param->fp_mirror_id_sign = 1;
5364 param->fp_mirror_id = strtoul(optarg, &end, 0);
5365 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5366 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5368 "%s %s: invalid mirror ID '%s'\n",
5369 progname, argv[0], optarg);
5372 if (param->fp_mirror_index != 0) {
5374 "%s %s: can't specify both mirror index and mirror ID\n",
5378 param->fp_check_mirror_id = 1;
5379 param->fp_exclude_mirror_id = !!neg_opt;
5382 param->fp_max_depth = 0;
5385 param->fp_get_default_lmv = 1;
5390 if (tmp[0] == '+') {
5391 param->fp_comp_end_sign = -1;
5393 } else if (tmp[0] == '-') {
5394 param->fp_comp_end_sign = 1;
5398 if (arg_is_eof(tmp)) {
5399 param->fp_comp_end = LUSTRE_EOF;
5400 param->fp_comp_end_units = 1;
5403 rc = llapi_parse_size(tmp,
5404 ¶m->fp_comp_end,
5405 ¶m->fp_comp_end_units, 0);
5409 "error: %s bad component end '%s'.\n",
5413 param->fp_check_comp_end = 1;
5415 param->fp_verbose |= VERBOSE_COMP_END;
5416 param->fp_max_depth = 0;
5420 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5421 param->fp_verbose |= VERBOSE_DFID;
5422 param->fp_max_depth = 0;
5426 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5427 param->fp_verbose |= VERBOSE_GENERATION;
5428 param->fp_max_depth = 0;
5432 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5433 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5434 param->fp_max_depth = 0;
5439 param->fp_comp_id = strtoul(optarg, &end, 0);
5440 if (*end != '\0' || param->fp_comp_id == 0 ||
5441 param->fp_comp_id > LCME_ID_MAX) {
5443 "error: %s bad component id '%s'\n",
5447 param->fp_check_comp_id = 1;
5449 param->fp_max_depth = 0;
5450 param->fp_verbose |= VERBOSE_COMP_ID;
5454 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5455 param->fp_verbose |= VERBOSE_PATTERN;
5456 param->fp_max_depth = 0;
5459 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5462 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5465 if (!(param->fp_verbose & VERBOSE_DETAIL))
5466 param->fp_max_depth = 0;
5467 param->fp_verbose |= VERBOSE_MDTINDEX;
5470 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5471 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5472 param->fp_max_depth = 0;
5476 if (param->fp_obd_uuid) {
5478 "error: %s: only one obduuid allowed",
5482 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5485 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5486 param->fp_verbose |= VERBOSE_POOL;
5487 param->fp_max_depth = 0;
5494 param->fp_recursive = 1;
5500 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5501 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5502 param->fp_max_depth = 0;
5506 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5512 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5513 param->fp_verbose |= VERBOSE_EXT_SIZE;
5514 param->fp_max_depth = 0;
5522 if (pathstart == -1) {
5523 fprintf(stderr, "error: %s: no filename|pathname\n",
5526 } else if (pathend == -1) {
5534 if (param->fp_recursive)
5535 param->fp_max_depth = -1;
5536 else if (param->fp_verbose & VERBOSE_DETAIL)
5537 param->fp_max_depth = 1;
5539 if (!param->fp_verbose)
5540 param->fp_verbose = VERBOSE_DEFAULT;
5541 if (param->fp_quiet)
5542 param->fp_verbose = VERBOSE_OBJID;
5545 rc = llapi_getstripe(argv[pathstart], param);
5546 } while (++pathstart < pathend && !rc);
5549 fprintf(stderr, "error: %s failed for %s.\n",
5550 argv[0], argv[optind - 1]);
5554 static int lfs_tgts(int argc, char **argv)
5556 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5557 struct find_param param;
5558 int index = 0, rc = 0;
5563 if (argc == 2 && !realpath(argv[1], path)) {
5565 fprintf(stderr, "error: invalid path '%s': %s\n",
5566 argv[1], strerror(-rc));
5570 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5571 /* Check if we have a mount point */
5572 if (mntdir[0] == '\0')
5575 memset(¶m, 0, sizeof(param));
5576 if (!strcmp(argv[0], "mdts"))
5577 param.fp_get_lmv = 1;
5579 rc = llapi_ostlist(mntdir, ¶m);
5581 fprintf(stderr, "error: %s: failed on %s\n",
5584 if (path[0] != '\0')
5586 memset(mntdir, 0, PATH_MAX);
5592 static int lfs_getstripe(int argc, char **argv)
5594 struct find_param param = { 0 };
5596 param.fp_max_depth = 1;
5597 return lfs_getstripe_internal(argc, argv, ¶m);
5601 static int lfs_getdirstripe(int argc, char **argv)
5603 struct find_param param = { 0 };
5604 struct option long_opts[] = {
5605 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5606 { .val = 'D', .name = "default", .has_arg = no_argument },
5607 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5608 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5609 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5610 { .val = 'O', .name = "obd", .has_arg = required_argument },
5611 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5612 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5613 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5614 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5618 param.fp_get_lmv = 1;
5620 while ((c = getopt_long(argc, argv,
5621 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5625 param.fp_verbose |= VERBOSE_COUNT;
5628 param.fp_get_default_lmv = 1;
5632 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5634 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5637 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5640 param.fp_verbose |= VERBOSE_HASH_TYPE;
5643 if (param.fp_obd_uuid) {
5645 "error: %s: only one obduuid allowed",
5649 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5652 param.fp_recursive = 1;
5655 param.fp_verbose |= VERBOSE_DETAIL;
5661 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5662 progname, argv[0], argv[optind - 1]);
5670 if (param.fp_recursive)
5671 param.fp_max_depth = -1;
5673 if (!param.fp_verbose)
5674 param.fp_verbose = VERBOSE_DEFAULT;
5677 rc = llapi_getstripe(argv[optind], ¶m);
5678 } while (++optind < argc && !rc);
5681 fprintf(stderr, "error: %s failed for %s.\n",
5682 argv[0], argv[optind - 1]);
5687 MNTDF_INODES = 0x0001,
5688 MNTDF_COOKED = 0x0002,
5689 MNTDF_LAZY = 0x0004,
5690 MNTDF_VERBOSE = 0x0008,
5691 MNTDF_SHOW = 0x0010,
5694 #define COOK(value) \
5697 while (value > 1024) { \
5705 #define CDF "%11llu"
5706 #define HDF "%8.1f%c"
5710 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5712 double avail, used, ratio = 0;
5715 avail = st->os_ffree;
5716 used = st->os_files - st->os_ffree;
5718 avail = st->os_bavail;
5719 used = st->os_blocks - st->os_bfree;
5721 if (avail + used > 0)
5722 ratio = used / (used + avail) * 100;
5724 /* Round up to match df(1) usage percentage */
5725 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5729 * This is to identify various problem states for "lfs df" if .osn_err = true,
5730 * so only show flags reflecting those states by default. Informational states
5731 * are only shown with "-v" and use lower-case names to distinguish them.
5732 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5734 static struct obd_statfs_state_names {
5735 enum obd_statfs_state osn_state;
5736 const char osn_name;
5739 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5740 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5741 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5742 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5743 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5744 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5745 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5746 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5747 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5750 static int showdf(char *mntdir, struct obd_statfs *stat,
5751 char *uuid, enum mntdf_flags flags,
5752 char *type, int index, int rc)
5754 long long avail, used, total;
5756 char *suffix = "KMGTPEZY";
5757 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5758 char tbuf[3 * sizeof(__u64)];
5759 char ubuf[3 * sizeof(__u64)];
5760 char abuf[3 * sizeof(__u64)];
5761 char rbuf[3 * sizeof(__u64)];
5768 if (flags & MNTDF_INODES) {
5769 avail = stat->os_ffree;
5770 used = stat->os_files - stat->os_ffree;
5771 total = stat->os_files;
5773 int shift = flags & MNTDF_COOKED ? 0 : 10;
5775 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5776 used = ((stat->os_blocks - stat->os_bfree) *
5777 stat->os_bsize) >> shift;
5778 total = (stat->os_blocks * stat->os_bsize) >> shift;
5781 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5783 if (flags & MNTDF_COOKED) {
5787 cook_val = (double)total;
5790 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5793 snprintf(tbuf, sizeof(tbuf), CDF, total);
5795 cook_val = (double)used;
5798 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5801 snprintf(ubuf, sizeof(ubuf), CDF, used);
5803 cook_val = (double)avail;
5806 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5809 snprintf(abuf, sizeof(abuf), CDF, avail);
5811 snprintf(tbuf, sizeof(tbuf), CDF, total);
5812 snprintf(ubuf, sizeof(tbuf), CDF, used);
5813 snprintf(abuf, sizeof(tbuf), CDF, avail);
5816 sprintf(rbuf, RDF, ratio);
5817 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5818 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5820 printf("[%s:%d]", type, index);
5822 if (stat->os_state) {
5826 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5827 if (oss_names[i].osn_state & stat->os_state &&
5828 (oss_names[i].osn_err ||
5829 flags & MNTDF_VERBOSE))
5830 printf("%c", oss_names[i].osn_name);
5837 printf(UUF": inactive device\n", uuid);
5840 printf(UUF": %s\n", uuid, strerror(-rc));
5847 struct ll_stat_type {
5852 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5854 struct ll_statfs_data {
5856 struct obd_statfs sd_st;
5859 struct ll_statfs_buf {
5861 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5864 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5865 int ops, struct ll_statfs_buf *lsb)
5867 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5868 struct obd_uuid uuid_buf;
5869 char *poolname = NULL;
5870 struct ll_stat_type types[] = {
5871 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5872 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5873 { .st_name = NULL } };
5874 struct ll_stat_type *tp;
5875 __u64 ost_files = 0;
5876 __u64 ost_ffree = 0;
5884 poolname = strchr(pool, '.');
5886 if (strncmp(fsname, pool, strlen(fsname))) {
5887 fprintf(stderr, "filesystem name incorrect\n");
5895 fd = open(mntdir, O_RDONLY);
5898 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5903 if (flags & MNTDF_SHOW) {
5904 if (flags & MNTDF_INODES)
5905 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5906 "UUID", "Inodes", "IUsed", "IFree",
5907 "IUse%", "Mounted on");
5909 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5911 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5912 "Used", "Available", "Use%", "Mounted on");
5915 for (tp = types; tp->st_name != NULL; tp++) {
5916 bool have_ost = false;
5918 if (!(tp->st_op & ops))
5921 for (index = 0; ; index++) {
5922 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5923 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5924 type = flags & MNTDF_LAZY ?
5925 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5926 rc2 = llapi_obd_fstatfs(fd, type, index,
5927 &stat_buf, &uuid_buf);
5932 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5933 if (!(flags & MNTDF_VERBOSE))
5935 } else if (rc2 < 0 && rc == 0) {
5940 * If we have OSTs then don't report MDT block counts.
5941 * For MDT-only filesystems the expectation is that all
5942 * layouts have a DoM component. For filesystems with
5943 * OSTs, files are not necessarily going to store data
5944 * on MDTs, and MDT space is limited to a fraction of
5945 * OST space, so don't include it in the summary.
5947 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5954 if (poolname && tp->st_op == LL_STATFS_LOV &&
5955 llapi_search_ost(fsname, poolname,
5956 obd_uuid2str(&uuid_buf)) != 1)
5960 * the llapi_obd_fstatfs() call may have returned with
5961 * an error, but if it filled in uuid_buf we will at
5962 * lease use that to print out a message for that OBD.
5963 * If we didn't get anything in the uuid_buf, then fill
5964 * it in so that we can print an error message.
5966 if (uuid_buf.uuid[0] == '\0')
5967 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5968 "%s%04x", tp->st_name, index);
5970 lsb->sb_buf[lsb->sb_count].sd_index = index;
5971 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5974 if (flags & MNTDF_SHOW)
5975 showdf(mntdir, &stat_buf,
5976 obd_uuid2str(&uuid_buf), flags,
5977 tp->st_name, index, rc2);
5982 if (tp->st_op == LL_STATFS_LMV) {
5983 sum.os_ffree += stat_buf.os_ffree;
5984 sum.os_files += stat_buf.os_files;
5985 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5986 ost_files += stat_buf.os_files;
5987 ost_ffree += stat_buf.os_ffree;
5989 sum.os_blocks += stat_buf.os_blocks *
5991 sum.os_bfree += stat_buf.os_bfree *
5993 sum.os_bavail += stat_buf.os_bavail *
6001 * If we have _some_ OSTs, but don't have as many free objects on the
6002 * OST as inodes on the MDTs, reduce the reported number of inodes
6003 * to compensate, so that the "inodes in use" number is correct.
6004 * This should be kept in sync with ll_statfs_internal().
6006 if (ost_files && ost_ffree < sum.os_ffree) {
6007 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6008 sum.os_ffree = ost_ffree;
6010 if (flags & MNTDF_SHOW) {
6012 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6020 static int lfs_setdirstripe(int argc, char **argv)
6023 struct lfs_setstripe_args lsa = { 0 };
6024 struct llapi_stripe_param *param = NULL;
6025 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6028 char *mode_opt = NULL;
6029 bool default_stripe = false;
6030 bool delete = false;
6031 bool foreign_mode = false;
6032 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6033 mode_t previous_mode = 0;
6035 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
6036 struct option long_opts[] = {
6037 { .val = 'c', .name = "count", .has_arg = required_argument },
6038 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6039 { .val = 'd', .name = "delete", .has_arg = no_argument },
6040 { .val = 'D', .name = "default", .has_arg = no_argument },
6041 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6042 { .val = LFS_LAYOUT_FLAGS_OPT,
6043 .name = "flags", .has_arg = required_argument },
6044 { .val = LFS_LAYOUT_FOREIGN_OPT,
6045 .name = "foreign", .has_arg = optional_argument},
6046 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6047 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6048 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6049 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6051 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6052 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6053 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6055 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6056 { .val = 'i', .name = "index", .has_arg = required_argument },
6058 { .val = 'o', .name = "mode", .has_arg = required_argument },
6059 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6060 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6062 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6063 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6064 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6068 setstripe_args_init(&lsa);
6070 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6071 long_opts, NULL)) >= 0) {
6078 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6081 "%s %s: invalid stripe count '%s'\n",
6082 progname, argv[0], optarg);
6088 default_stripe = true;
6091 default_stripe = true;
6093 case LFS_LAYOUT_FOREIGN_OPT:
6095 /* check pure numeric */
6096 type = strtoul(optarg, &end, 0);
6099 type = check_foreign_type_name(optarg);
6100 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6102 "%s %s: unknown foreign type '%s'\n",
6109 foreign_mode = true;
6111 case LFS_LAYOUT_FLAGS_OPT:
6112 flags = strtoul(optarg, &end, 16);
6115 "%s %s: bad flags '%s'\n",
6116 progname, argv[0], optarg);
6120 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6123 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6126 lsa.lsa_pattern = check_hashtype(optarg);
6127 if (lsa.lsa_pattern == 0) {
6129 "%s %s: bad stripe hash type '%s'\n",
6130 progname, argv[0], optarg);
6135 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6138 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6139 if (strcmp(argv[optind - 1], "--index") == 0)
6141 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6144 lsa.lsa_nr_tgts = parse_targets(mdts,
6145 sizeof(mdts) / sizeof(__u32),
6146 lsa.lsa_nr_tgts, optarg, NULL);
6147 if (lsa.lsa_nr_tgts < 0) {
6149 "%s %s: invalid MDT target(s) '%s'\n",
6150 progname, argv[0], optarg);
6154 lsa.lsa_tgts = mdts;
6155 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6156 lsa.lsa_stripe_off = mdts[0];
6158 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6161 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6170 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6171 progname, argv[0], argv[optind - 1]);
6176 if (optind == argc) {
6177 fprintf(stderr, "%s %s: DIR must be specified\n",
6182 if (xattr && !foreign_mode) {
6184 * only print a warning as this is armless and will be
6188 "%s %s: xattr has been specified for non-foreign layout\n",
6190 } else if (foreign_mode && !xattr) {
6192 "%s %s: xattr must be provided in foreign mode\n",
6197 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6198 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6200 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6205 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6206 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6208 "%s %s: stripe offset and count must be specified\n",
6214 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6215 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6217 "%s %s: cannot specify -d with -c or -i options\n",
6223 mode = strtoul(mode_opt, &end, 8);
6226 "%s %s: bad MODE '%s'\n",
6227 progname, argv[0], mode_opt);
6230 previous_mode = umask(0);
6233 /* foreign LMV/dir case */
6235 if (argc > optind + 1) {
6237 "%s %s: cannot specify multiple foreign dirs\n",
6242 dname = argv[optind];
6243 result = llapi_dir_create_foreign(dname, mode, type, flags,
6247 "%s mkdir: can't create foreign dir '%s': %s\n",
6248 progname, dname, strerror(-result));
6253 * initialize stripe parameters, in case param is converted to specific,
6254 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6256 param = calloc(1, offsetof(typeof(*param),
6257 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6258 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6261 "%s %s: cannot allocate memory for parameters: %s\n",
6262 progname, argv[0], strerror(ENOMEM));
6266 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6267 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6268 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6269 lsa.lsa_stripe_count == 0))
6270 lsa.lsa_stripe_count = 1;
6271 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6272 param->lsp_stripe_count = lsa.lsa_stripe_count;
6273 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6274 param->lsp_stripe_offset = -1;
6276 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6277 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6278 param->lsp_stripe_pattern = lsa.lsa_pattern;
6280 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6281 param->lsp_pool = lsa.lsa_pool_name;
6282 param->lsp_is_specific = false;
6283 if (lsa.lsa_nr_tgts > 1) {
6284 if (lsa.lsa_stripe_count > 0 &&
6285 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6286 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6288 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6289 argv[0], lsa.lsa_stripe_count,
6295 param->lsp_is_specific = true;
6296 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6297 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6300 dname = argv[optind];
6302 if (default_stripe) {
6303 result = llapi_dir_set_default_lmv(dname, param);
6306 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6307 progname, dname, strerror(-result));
6311 result = llapi_dir_create(dname, mode, param);
6314 "%s setdirstripe: cannot create dir '%s': %s\n",
6315 progname, dname, strerror(-result));
6316 } while (!result && (dname = argv[++optind]));
6319 umask(previous_mode);
6326 static int lfs_rmentry(int argc, char **argv)
6333 fprintf(stderr, "error: %s: missing dirname\n",
6339 dname = argv[index];
6341 result = llapi_direntry_remove(dname);
6344 "error: %s: remove dir entry '%s' failed\n",
6348 dname = argv[++index];
6353 static int lfs_mv(int argc, char **argv)
6355 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6356 struct find_param param = {
6363 struct option long_opts[] = {
6364 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6365 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6366 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6369 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6371 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6374 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6377 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6379 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6385 param.fp_verbose = VERBOSE_DETAIL;
6388 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6389 progname, argv[optind - 1]);
6394 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6395 fprintf(stderr, "%s mv: MDT index must be specified\n",
6400 if (optind >= argc) {
6401 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6405 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6407 /* initialize migrate mdt parameters */
6408 param.fp_lmv_md = &lmu;
6409 param.fp_migrate = 1;
6410 rc = llapi_migrate_mdt(argv[optind], ¶m);
6412 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6413 progname, argv[optind], lmu.lum_stripe_offset,
6418 static int lfs_osts(int argc, char **argv)
6420 return lfs_tgts(argc, argv);
6423 static int lfs_mdts(int argc, char **argv)
6425 return lfs_tgts(argc, argv);
6428 static int lfs_df(int argc, char **argv)
6430 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6431 enum mntdf_flags flags = MNTDF_SHOW;
6432 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6433 int c, rc = 0, index = 0;
6434 char fsname[PATH_MAX] = "", *pool_name = NULL;
6435 struct option long_opts[] = {
6436 { .val = 'h', .name = "human-readable",
6437 .has_arg = no_argument },
6438 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6439 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6440 { .val = 'p', .name = "pool", .has_arg = required_argument },
6441 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6444 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6447 flags |= MNTDF_COOKED;
6450 flags |= MNTDF_INODES;
6453 flags |= MNTDF_LAZY;
6459 flags |= MNTDF_VERBOSE;
6465 if (optind < argc && !realpath(argv[optind], path)) {
6467 fprintf(stderr, "error: invalid path '%s': %s\n",
6468 argv[optind], strerror(-rc));
6472 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6473 /* Check if we have a mount point */
6474 if (mntdir[0] == '\0')
6477 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6478 if (rc || path[0] != '\0')
6480 fsname[0] = '\0'; /* avoid matching in next loop */
6481 mntdir[0] = '\0'; /* avoid matching in next loop */
6487 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6488 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6492 if (opt_fsname == opt_instance) { /* both true or both false */
6493 rc = llapi_getname(mntdir, buf, buflen);
6494 } else if (opt_fsname) {
6496 * llapi_search_mounts() fills @buf with fsname, but that is not
6497 * called if explicit paths are specified on the command-line
6500 rc = llapi_get_fsname(mntdir, buf, buflen);
6501 } else /* if (opt_instance) */ {
6502 rc = llapi_get_instance(mntdir, buf, buflen);
6506 fprintf(stderr, "cannot get instance for '%s': %s\n",
6507 mntdir, strerror(-rc));
6512 printf("%s %s\n", buf, mntdir);
6514 printf("%s\n", buf);
6519 static int lfs_getname(int argc, char **argv)
6521 struct option long_opts[] = {
6522 { .val = 'h', .name = "help", .has_arg = no_argument },
6523 { .val = 'i', .name = "instance", .has_arg = no_argument },
6524 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6526 bool opt_instance = false, opt_fsname = false;
6527 char fsname[PATH_MAX] = "";
6530 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6533 opt_instance = true;
6544 if (optind == argc) { /* no paths specified, get all paths. */
6545 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6548 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6549 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6550 opt_instance, opt_fsname, true);
6553 path[0] = fsname[0] = mntdir[0] = '\0';
6555 } else { /* paths specified, only attempt to search these. */
6558 /* if only one path is given, print only requested info */
6559 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6561 for (; optind < argc; optind++) {
6562 rc2 = print_instance(argv[optind], fsname,
6563 sizeof(fsname), opt_instance,
6564 opt_fsname, opt_mntdir);
6574 static int lfs_check(int argc, char **argv)
6576 char mntdir[PATH_MAX] = {'\0'};
6585 fprintf(stderr, "%s check: server type must be specified\n",
6590 obd_types[0] = obd_type1;
6591 obd_types[1] = obd_type2;
6592 obd_types[2] = obd_type3;
6594 if (strcmp(argv[1], "osts") == 0) {
6595 strcpy(obd_types[0], "osc");
6596 } else if (strcmp(argv[1], "mdts") == 0 ||
6597 strcmp(argv[1], "mds") == 0) {
6598 strcpy(obd_types[0], "mdc");
6599 } else if (strcmp(argv[1], "mgts") == 0) {
6600 strcpy(obd_types[0], "mgc");
6601 } else if (strcmp(argv[1], "all") == 0 ||
6602 strcmp(argv[1], "servers") == 0) {
6604 strcpy(obd_types[0], "osc");
6605 strcpy(obd_types[1], "mdc");
6606 strcpy(obd_types[2], "mgc");
6608 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6613 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6614 if (rc < 0 || mntdir[0] == '\0') {
6616 "%s check: cannot find mounted Lustre filesystem: %s\n",
6617 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6621 rc = llapi_target_check(num_types, obd_types, mntdir);
6623 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6624 progname, argv[1], strerror(-rc));
6629 #ifdef HAVE_SYS_QUOTA_H
6630 #define ADD_OVERFLOW(a, b) \
6631 ((((a) + (b)) < (a)) ? \
6632 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6634 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6635 * returns the value or ULONG_MAX on integer overflow or incorrect format
6637 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6638 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6639 * 3. empty integer value is interpreted as 0
6641 static unsigned long str2sec(const char *timestr)
6643 const char spec[] = "smhdw";
6644 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6645 unsigned long val = 0;
6648 if (strpbrk(timestr, spec) == NULL) {
6650 * no specifiers inside the time string,
6651 * should treat it as an integer value
6653 val = strtoul(timestr, &tail, 10);
6654 return *tail ? ULONG_MAX : val;
6657 /* format string is XXwXXdXXhXXmXXs */
6663 v = strtoul(timestr, &tail, 10);
6664 if (v == ULONG_MAX || *tail == '\0')
6666 * value too large (ULONG_MAX or more)
6667 * or missing specifier
6671 ptr = strchr(spec, *tail);
6673 /* unknown specifier */
6678 /* check if product will overflow the type */
6679 if (!(v < ULONG_MAX / mult[ind]))
6682 ADD_OVERFLOW(val, mult[ind] * v);
6683 if (val == ULONG_MAX)
6695 #define ARG2ULL(nr, str, def_units) \
6697 unsigned long long limit, units = def_units; \
6700 rc = llapi_parse_size(str, &limit, &units, 1); \
6702 fprintf(stderr, "%s: invalid limit '%s'\n", \
6709 static inline int has_times_option(int argc, char **argv)
6713 for (i = 1; i < argc; i++)
6714 if (!strcmp(argv[i], "-t"))
6720 static inline int lfs_verify_poolarg(char *pool)
6722 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6724 "Pool name '%.*s' is longer than %d\n",
6725 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6731 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6734 char *mnt, *obd_type = (char *)qctl->obd_type;
6735 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6736 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6737 struct option long_opts[] = {
6738 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6739 { .val = 'g', .name = "group", .has_arg = no_argument },
6740 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6741 { .val = 'p', .name = "projid", .has_arg = no_argument },
6742 { .val = 't', .name = "times", .has_arg = no_argument },
6743 { .val = 'u', .name = "user", .has_arg = no_argument },
6744 { .val = LFS_POOL_OPT,
6745 .name = "pool", .has_arg = required_argument },
6749 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6750 qctl->qc_type = ALLQUOTA;
6752 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6753 long_opts, NULL)) != -1) {
6764 if (qctl->qc_type != ALLQUOTA) {
6766 "error: -u/g/p can't be used more than once\n");
6769 qctl->qc_type = qtype;
6772 if (strncmp(optarg, NOTIFY_GRACE,
6773 strlen(NOTIFY_GRACE)) == 0) {
6774 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6776 dqi->dqi_bgrace = str2sec(optarg);
6777 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6779 "error: bad block-grace: %s\n",
6784 dqb->dqb_valid |= QIF_BTIME;
6787 if (strncmp(optarg, NOTIFY_GRACE,
6788 strlen(NOTIFY_GRACE)) == 0) {
6789 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6791 dqi->dqi_igrace = str2sec(optarg);
6792 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6794 "error: bad inode-grace: %s\n",
6799 dqb->dqb_valid |= QIF_ITIME;
6801 case 't': /* Yes, of course! */
6804 if (lfs_verify_poolarg(optarg))
6807 "Trying to set grace for pool %s\n", optarg);
6808 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
6809 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
6811 /* getopt prints error message for us when opterr != 0 */
6817 if (qctl->qc_type == ALLQUOTA) {
6818 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6822 if (optind != argc - 1) {
6823 fprintf(stderr, "error: unexpected parameters encountered\n");
6828 rc = llapi_quotactl(mnt, qctl);
6831 fprintf(stderr, "%s %s ", obd_type,
6832 obd_uuid2str(&qctl->obd_uuid));
6833 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6840 #define BSLIMIT (1 << 0)
6841 #define BHLIMIT (1 << 1)
6842 #define ISLIMIT (1 << 2)
6843 #define IHLIMIT (1 << 3)
6845 int lfs_setquota(int argc, char **argv)
6848 struct if_quotactl *qctl;
6849 char *mnt, *obd_type;
6850 struct obd_dqblk *dqb;
6851 struct option long_opts[] = {
6852 { .val = 'b', .name = "block-softlimit",
6853 .has_arg = required_argument },
6854 { .val = 'B', .name = "block-hardlimit",
6855 .has_arg = required_argument },
6856 { .val = 'd', .name = "default", .has_arg = no_argument },
6857 { .val = 'g', .name = "group", .has_arg = required_argument },
6858 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6859 { .val = 'i', .name = "inode-softlimit",
6860 .has_arg = required_argument },
6861 { .val = 'I', .name = "inode-hardlimit",
6862 .has_arg = required_argument },
6863 { .val = 'p', .name = "projid", .has_arg = required_argument },
6864 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6865 { .val = 'u', .name = "user", .has_arg = required_argument },
6866 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6867 { .val = LFS_POOL_OPT,
6868 .name = "pool", .has_arg = required_argument },
6870 unsigned int limit_mask = 0;
6871 bool use_default = false;
6872 int qtype, qctl_len;
6874 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
6875 qctl = malloc(qctl_len);
6879 memset(qctl, 0, qctl_len);
6880 obd_type = (char *)qctl->obd_type;
6881 dqb = &qctl->qc_dqblk;
6883 if (has_times_option(argc, argv)) {
6884 rc = lfs_setquota_times(argc, argv, qctl);
6888 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
6889 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6890 * so it can be used as a marker that qc_type
6891 * isn't reinitialized from command line
6894 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6895 long_opts, NULL)) != -1) {
6898 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6901 goto quota_type_def;
6904 rc = name2uid(&qctl->qc_id, optarg);
6907 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6910 goto quota_type_def;
6913 rc = name2gid(&qctl->qc_id, optarg);
6916 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6919 goto quota_type_def;
6922 rc = name2projid(&qctl->qc_id, optarg);
6925 if (str2quotaid(&qctl->qc_id, optarg)) {
6927 "%s setquota: invalid id '%s'\n",
6934 if (qctl->qc_id == 0) {
6936 "%s setquota: can't set quota for root usr/group/project.\n",
6943 if (qctl->qc_type != ALLQUOTA) {
6945 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
6950 qctl->qc_type = qtype;
6953 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6957 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6958 dqb->dqb_bsoftlimit >>= 10;
6959 limit_mask |= BSLIMIT;
6960 if (dqb->dqb_bsoftlimit &&
6961 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6963 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6965 (unsigned long long)dqb->dqb_bsoftlimit,
6969 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6970 dqb->dqb_bhardlimit >>= 10;
6971 limit_mask |= BHLIMIT;
6972 if (dqb->dqb_bhardlimit &&
6973 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6975 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6976 "See '%s help setquota' or Lustre manual for details\n",
6978 (unsigned long long)dqb->dqb_bhardlimit,
6982 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6983 limit_mask |= ISLIMIT;
6984 if (dqb->dqb_isoftlimit &&
6985 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6987 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6989 (unsigned long long)dqb->dqb_isoftlimit,
6993 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6994 limit_mask |= IHLIMIT;
6995 if (dqb->dqb_ihardlimit &&
6996 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6998 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7000 (unsigned long long)dqb->dqb_ihardlimit,
7004 if (lfs_verify_poolarg(optarg)) {
7009 "Trying to set quota for pool %s\n", optarg);
7010 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7011 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7015 "%s setquota: unrecognized option '%s'\n",
7016 progname, argv[optind - 1]);
7022 if (qctl->qc_type == ALLQUOTA) {
7024 "%s setquota: either -u or -g must be specified\n",
7030 if (!use_default && limit_mask == 0) {
7032 "%s setquota: at least one limit must be specified\n",
7038 if (use_default && limit_mask != 0) {
7040 "%s setquota: limits should not be specified when using default quota\n",
7046 if (use_default && qctl->qc_id == 0) {
7048 "%s setquota: can not set default quota for root user/group/project\n",
7054 if (optind != argc - 1) {
7056 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7057 progname, argv[optind]);
7065 dqb->dqb_bhardlimit = 0;
7066 dqb->dqb_bsoftlimit = 0;
7067 dqb->dqb_ihardlimit = 0;
7068 dqb->dqb_isoftlimit = 0;
7071 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7072 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7073 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7074 /* sigh, we can't just set blimits/ilimits */
7075 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7076 .qc_type = qctl->qc_type,
7077 .qc_id = qctl->qc_id};
7079 rc = llapi_quotactl(mnt, &tmp_qctl);
7083 if (!(limit_mask & BHLIMIT))
7084 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7085 if (!(limit_mask & BSLIMIT))
7086 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7087 if (!(limit_mask & IHLIMIT))
7088 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7089 if (!(limit_mask & ISLIMIT))
7090 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7092 /* Keep grace times if we have got no softlimit arguments */
7093 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7094 dqb->dqb_valid |= QIF_BTIME;
7095 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7098 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7099 dqb->dqb_valid |= QIF_ITIME;
7100 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7104 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7105 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7107 rc = llapi_quotactl(mnt, qctl);
7111 "%s setquota: cannot quotactl '%s' '%s': %s",
7113 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7120 /* Converts seconds value into format string
7121 * result is returned in buf
7123 * 1. result is in descenting order: 1w2d3h4m5s
7124 * 2. zero fields are not filled (except for p. 3): 5d1s
7125 * 3. zero seconds value is presented as "0s"
7127 static char *__sec2str(time_t seconds, char *buf)
7129 const char spec[] = "smhdw";
7130 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7135 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7136 c = seconds / mult[i];
7138 if (c > 0 || (i == 0 && buf == tail))
7139 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7148 static void sec2str(time_t seconds, char *buf, int rc)
7155 tail = __sec2str(seconds, tail);
7157 if (rc && tail - buf < 39) {
7163 static void diff2str(time_t seconds, char *buf, time_t now)
7168 if (seconds <= now) {
7169 strcpy(buf, "none");
7172 __sec2str(seconds - now, buf);
7175 static void print_quota_title(char *name, struct if_quotactl *qctl,
7176 bool human_readable, bool show_default)
7179 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7180 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7181 "Filesystem", "bquota", "blimit", "bgrace",
7182 "iquota", "ilimit", "igrace");
7184 printf("Disk quotas for %s %s (%cid %u):\n",
7185 qtype_name(qctl->qc_type), name,
7186 *qtype_name(qctl->qc_type), qctl->qc_id);
7187 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7188 "Filesystem", human_readable ? "used" : "kbytes",
7189 "quota", "limit", "grace",
7190 "files", "quota", "limit", "grace");
7194 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7197 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7200 snprintf(buf, buflen, "%5.4gP",
7201 (double)num / ((__u64)1 << 40));
7203 snprintf(buf, buflen, "%5.4gT",
7204 (double)num / (1 << 30));
7206 snprintf(buf, buflen, "%5.4gG",
7207 (double)num / (1 << 20));
7209 snprintf(buf, buflen, "%5.4gM",
7210 (double)num / (1 << 10));
7212 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7216 #define STRBUF_LEN 32
7217 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7218 int rc, bool h, bool show_default)
7224 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7225 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7226 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7227 int bover = 0, iover = 0;
7228 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7229 char numbuf[3][STRBUF_LEN];
7231 char strbuf[STRBUF_LEN];
7233 if (dqb->dqb_bhardlimit &&
7234 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7236 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7237 if (dqb->dqb_btime > now)
7243 if (dqb->dqb_ihardlimit &&
7244 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7246 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7247 if (dqb->dqb_itime > now)
7253 if (strlen(mnt) > 15)
7254 printf("%s\n%15s", mnt, "");
7256 printf("%15s", mnt);
7259 diff2str(dqb->dqb_btime, timebuf, now);
7260 else if (show_default)
7261 snprintf(timebuf, sizeof(timebuf), "%llu",
7262 (unsigned long long)dqb->dqb_btime);
7264 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7265 strbuf, sizeof(strbuf), h);
7266 if (rc == -EREMOTEIO)
7267 sprintf(numbuf[0], "%s*", strbuf);
7269 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7270 "%s" : "[%s]", strbuf);
7272 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7273 if (type == QC_GENERAL)
7274 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7275 "%s" : "[%s]", strbuf);
7277 sprintf(numbuf[1], "%s", "-");
7279 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7280 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7281 "%s" : "[%s]", strbuf);
7284 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7286 printf(" %7s%c %6s %7s %7s",
7287 numbuf[0], bover ? '*' : ' ', numbuf[1],
7288 numbuf[2], bover > 1 ? timebuf : "-");
7291 diff2str(dqb->dqb_itime, timebuf, now);
7292 else if (show_default)
7293 snprintf(timebuf, sizeof(timebuf), "%llu",
7294 (unsigned long long)dqb->dqb_itime);
7296 snprintf(numbuf[0], sizeof(numbuf),
7297 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7298 (uintmax_t)dqb->dqb_curinodes);
7300 if (type == QC_GENERAL)
7301 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7303 (uintmax_t)dqb->dqb_isoftlimit);
7305 sprintf(numbuf[1], "%s", "-");
7307 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7308 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7311 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7312 else if (type != QC_OSTIDX)
7313 printf(" %7s%c %6s %7s %7s",
7314 numbuf[0], iover ? '*' : ' ', numbuf[1],
7315 numbuf[2], iover > 1 ? timebuf : "-");
7317 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7319 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7320 qctl->qc_cmd == Q_GETOINFO) {
7324 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7325 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7327 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7328 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7329 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7331 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7333 printf("Block grace time: %s; Inode grace time: %s\n",
7334 bgtimebuf, igtimebuf);
7338 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7342 /* format is "lustre-OST0001" */
7343 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7345 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7350 *idx = strtoul(dash, &endp, 16);
7351 if (*idx > 0xffff) {
7352 fprintf(stderr, "wrong index %s\n", tgtname);
7359 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7360 bool h, __u64 *total)
7362 int rc = 0, rc1 = 0, count = 0, i = 0;
7363 char **list = NULL, *buffer = NULL;
7364 __u32 valid = qctl->qc_valid;
7366 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7369 /* Is it correct for the case OST0000, OST0002, OST0003 -
7370 * we will ask OST0001 that is absent and won't ask OST0003? */
7371 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7373 fprintf(stderr, "can not get %s count: %s\n",
7374 is_mdt ? "mdt" : "ost", strerror(-rc));
7378 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7379 char fname[PATH_MAX];
7380 char fsname[LUSTRE_MAXFSNAME + 1];
7381 int bufsize = sizeof(struct obd_uuid) * count;
7383 rc = llapi_search_fsname(mnt, fsname);
7385 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7389 buffer = malloc(bufsize + sizeof(*list) * count);
7392 list = (char **)(buffer + bufsize);
7393 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7394 count = llapi_get_poolmembers(fname, list, count,
7400 for (i = 0; i < count; i++) {
7401 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7404 if (tgt_name2index(list[i], &index))
7406 qctl->qc_idx = index;
7411 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7412 rc = llapi_quotactl(mnt, qctl);
7414 /* It is remote client case. */
7415 if (rc == -EOPNOTSUPP) {
7422 fprintf(stderr, "quotactl %s%d failed.\n",
7423 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7427 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7428 qctl->qc_valid, 0, h, false);
7429 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7430 qctl->qc_dqblk.dqb_bhardlimit;
7435 qctl->qc_valid = valid;
7439 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7440 int verbose, int quiet, bool human_readable,
7443 int rc1 = 0, rc2 = 0, rc3 = 0;
7444 char *obd_type = (char *)qctl->obd_type;
7445 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7446 __u64 total_ialloc = 0, total_balloc = 0;
7447 bool use_default_for_blk = false;
7448 bool use_default_for_file = false;
7451 rc1 = llapi_quotactl(mnt, qctl);
7455 fprintf(stderr, "%s quotas are not enabled.\n",
7456 qtype_name(qctl->qc_type));
7459 fprintf(stderr, "Permission denied.\n");
7462 /* We already got error message. */
7465 fprintf(stderr, "Unexpected quotactl error: %s\n",
7470 if (!show_default && qctl->qc_id == 0) {
7471 qctl->qc_dqblk.dqb_bhardlimit = 0;
7472 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7473 qctl->qc_dqblk.dqb_ihardlimit = 0;
7474 qctl->qc_dqblk.dqb_isoftlimit = 0;
7475 qctl->qc_dqblk.dqb_btime = 0;
7476 qctl->qc_dqblk.dqb_itime = 0;
7477 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7480 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7481 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7482 use_default_for_blk = true;
7483 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7486 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7487 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7488 use_default_for_file = true;
7489 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7492 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7493 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7494 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7495 print_quota_title(name, qctl, human_readable, show_default);
7497 if (rc1 && *obd_type)
7498 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7500 if (qctl->qc_valid != QC_GENERAL)
7503 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7504 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7505 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7506 (QIF_LIMITS|QIF_USAGE));
7508 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7510 if (!show_default && verbose &&
7511 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7512 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7513 char strbuf[STRBUF_LEN];
7515 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7517 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7519 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7521 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7522 (uintmax_t)total_ialloc, strbuf);
7525 if (use_default_for_blk)
7526 printf("%cid %u is using default block quota setting\n",
7527 *qtype_name(qctl->qc_type), qctl->qc_id);
7529 if (use_default_for_file)
7530 printf("%cid %u is using default file quota setting\n",
7531 *qtype_name(qctl->qc_type), qctl->qc_id);
7533 if (rc1 || rc2 || rc3 || inacc)
7534 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7548 static int lfs_project(int argc, char **argv)
7550 int ret = 0, err = 0, c, i;
7551 struct project_handle_control phc = { 0 };
7552 enum lfs_project_ops_t op;
7555 phc.assign_projid = false;
7556 /* default action */
7557 op = LFS_PROJECT_LIST;
7559 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7562 if (op != LFS_PROJECT_LIST) {
7564 "%s: cannot specify '-c' '-C' '-s' together\n",
7569 op = LFS_PROJECT_CHECK;
7572 if (op != LFS_PROJECT_LIST) {
7574 "%s: cannot specify '-c' '-C' '-s' together\n",
7579 op = LFS_PROJECT_CLEAR;
7582 if (op != LFS_PROJECT_LIST) {
7584 "%s: cannot specify '-c' '-C' '-s' together\n",
7589 phc.set_inherit = true;
7590 op = LFS_PROJECT_SET;
7596 phc.keep_projid = true;
7599 phc.recursive = true;
7602 if (str2quotaid(&phc.projid, optarg)) {
7604 "Invalid project ID: %s\n",
7609 phc.assign_projid = true;
7613 phc.newline = false;
7616 fprintf(stderr, "%s: invalid option '%c'\n",
7622 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7623 op = LFS_PROJECT_SET;
7624 phc.set_projid = true;
7625 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7626 phc.set_projid = true;
7630 case LFS_PROJECT_CHECK:
7631 if (phc.keep_projid) {
7633 "%s: '-k' is useless together with '-c'\n",
7638 case LFS_PROJECT_CLEAR:
7641 "%s: '-0' is useless together with '-C'\n",
7645 if (phc.assign_projid) {
7647 "%s: '-p' is useless together with '-C'\n",
7652 case LFS_PROJECT_SET:
7655 "%s: '-0' is useless together with '-s'\n",
7659 if (phc.keep_projid) {
7661 "%s: '-k' is useless together with '-s'\n",
7669 "%s: '-0' is useless for list operations\n",
7679 fprintf(stderr, "%s: missing file or directory target(s)\n",
7684 for (i = 0; i < argc; i++) {
7686 case LFS_PROJECT_CHECK:
7687 err = lfs_project_check(argv[i], &phc);
7689 case LFS_PROJECT_LIST:
7690 err = lfs_project_list(argv[i], &phc);
7692 case LFS_PROJECT_CLEAR:
7693 err = lfs_project_clear(argv[i], &phc);
7695 case LFS_PROJECT_SET:
7696 err = lfs_project_set(argv[i], &phc);
7708 static int lfs_quota(int argc, char **argv)
7711 char *mnt, *name = NULL;
7712 struct if_quotactl *qctl;
7714 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7715 __u32 valid = QC_GENERAL, idx = 0;
7716 bool human_readable = false;
7717 bool show_default = false;
7719 struct option long_opts[] = {
7720 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
7723 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7727 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7728 qctl->qc_type = ALLQUOTA;
7729 obd_uuid = (char *)qctl->obd_uuid.uuid;
7731 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7732 long_opts, NULL)) != -1) {
7735 show_default = true;
7740 show_default = true;
7745 show_default = true;
7749 if (qctl->qc_type != ALLQUOTA) {
7751 "%s quota: only one of -u, -g, or -p may be specified\n",
7756 qctl->qc_type = qtype;
7759 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7762 valid = qctl->qc_valid = QC_UUID;
7763 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
7766 valid = qctl->qc_valid = QC_MDTIDX;
7767 idx = qctl->qc_idx = atoi(optarg);
7768 if (idx == 0 && *optarg != '0') {
7770 "%s quota: invalid MDT index '%s'\n",
7777 valid = qctl->qc_valid = QC_OSTIDX;
7778 idx = qctl->qc_idx = atoi(optarg);
7779 if (idx == 0 && *optarg != '0') {
7781 "%s quota: invalid OST index '%s'\n",
7794 human_readable = true;
7797 if (lfs_verify_poolarg(optarg)) {
7801 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7802 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
7803 LUSTRE_Q_GETINFOPOOL :
7804 LUSTRE_Q_GETQUOTAPOOL;
7807 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7808 progname, argv[optind - 1]);
7814 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7815 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7816 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7817 qctl->qc_type == ALLQUOTA &&
7818 optind == argc - 1 && !show_default) {
7821 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7822 qctl->qc_type = qtype;
7823 qctl->qc_valid = valid;
7824 if (qtype == USRQUOTA) {
7825 qctl->qc_id = geteuid();
7826 rc = uid2name(&name, qctl->qc_id);
7828 qctl->qc_id = getegid();
7829 rc = gid2name(&name, qctl->qc_id);
7830 memset(&qctl->qc_dqblk, 0,
7831 sizeof(qctl->qc_dqblk));
7836 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
7837 human_readable, show_default);
7842 /* lfs quota -u username /path/to/lustre/mount */
7843 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7844 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7845 /* options should be followed by u/g-name and mntpoint */
7846 if ((!show_default && optind + 2 != argc) ||
7847 (show_default && optind + 1 != argc) ||
7848 qctl->qc_type == ALLQUOTA) {
7850 "%s quota: name and mount point must be specified\n",
7856 if (!show_default) {
7857 name = argv[optind++];
7858 switch (qctl->qc_type) {
7860 rc = name2uid(&qctl->qc_id, name);
7863 rc = name2gid(&qctl->qc_id, name);
7866 rc = name2projid(&qctl->qc_id, name);
7873 qctl->qc_valid = QC_GENERAL;
7874 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
7879 if (str2quotaid(&qctl->qc_id, name)) {
7880 fprintf(stderr, "%s quota: invalid id '%s'\n",
7886 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
7887 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7894 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
7895 human_readable, show_default);
7900 #endif /* HAVE_SYS_QUOTA_H! */
7902 static int flushctx_ioctl(char *mp)
7906 fd = open(mp, O_RDONLY);
7908 fprintf(stderr, "flushctx: error open %s: %s\n",
7909 mp, strerror(errno));
7913 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7915 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7916 mp, strerror(errno));
7922 static int lfs_flushctx(int argc, char **argv)
7924 int kdestroy = 0, c;
7925 char mntdir[PATH_MAX] = {'\0'};
7929 while ((c = getopt(argc, argv, "k")) != -1) {
7936 "error: %s: option '-%c' unrecognized\n",
7943 rc = system("kdestroy > /dev/null");
7945 rc = WEXITSTATUS(rc);
7947 "error destroying tickets: %d, continuing\n",
7952 if (optind >= argc) {
7953 /* flush for all mounted lustre fs. */
7954 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7955 /* Check if we have a mount point */
7956 if (mntdir[0] == '\0')
7959 if (flushctx_ioctl(mntdir))
7962 mntdir[0] = '\0'; /* avoid matching in next loop */
7965 /* flush fs as specified */
7966 while (optind < argc) {
7967 if (flushctx_ioctl(argv[optind++]))
7974 static int lfs_changelog(int argc, char **argv)
7976 void *changelog_priv;
7977 struct changelog_rec *rec;
7978 long long startrec = 0, endrec = 0;
7980 struct option long_opts[] = {
7981 { .val = 'f', .name = "follow", .has_arg = no_argument },
7983 char short_opts[] = "f";
7986 while ((rc = getopt_long(argc, argv, short_opts,
7987 long_opts, NULL)) != -1) {
7994 "%s changelog: unrecognized option '%s'\n",
7995 progname, argv[optind - 1]);
7999 if (optind >= argc) {
8000 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8005 mdd = argv[optind++];
8007 startrec = strtoll(argv[optind++], NULL, 10);
8009 endrec = strtoll(argv[optind++], NULL, 10);
8011 rc = llapi_changelog_start(&changelog_priv,
8012 CHANGELOG_FLAG_BLOCK |
8013 CHANGELOG_FLAG_JOBID |
8014 CHANGELOG_FLAG_EXTRA_FLAGS |
8015 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8018 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8019 progname, strerror(errno = -rc));
8023 rc = llapi_changelog_set_xflags(changelog_priv,
8024 CHANGELOG_EXTRA_FLAG_UIDGID |
8025 CHANGELOG_EXTRA_FLAG_NID |
8026 CHANGELOG_EXTRA_FLAG_OMODE |
8027 CHANGELOG_EXTRA_FLAG_XATTR);
8030 "%s changelog: cannot set xflags for changelog: %s\n",
8031 progname, strerror(errno = -rc));
8035 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8039 if (endrec && rec->cr_index > endrec) {
8040 llapi_changelog_free(&rec);
8043 if (rec->cr_index < startrec) {
8044 llapi_changelog_free(&rec);
8048 secs = rec->cr_time >> 30;
8049 gmtime_r(&secs, &ts);
8050 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8051 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8052 changelog_type2str(rec->cr_type),
8053 ts.tm_hour, ts.tm_min, ts.tm_sec,
8054 (int)(rec->cr_time & ((1 << 30) - 1)),
8055 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8056 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8058 if (rec->cr_flags & CLF_JOBID) {
8059 struct changelog_ext_jobid *jid =
8060 changelog_rec_jobid(rec);
8062 if (jid->cr_jobid[0] != '\0')
8063 printf(" j=%s", jid->cr_jobid);
8066 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8067 struct changelog_ext_extra_flags *ef =
8068 changelog_rec_extra_flags(rec);
8070 printf(" ef=0x%llx",
8071 (unsigned long long)ef->cr_extra_flags);
8073 if (ef->cr_extra_flags & CLFE_UIDGID) {
8074 struct changelog_ext_uidgid *uidgid =
8075 changelog_rec_uidgid(rec);
8077 printf(" u=%llu:%llu",
8078 (unsigned long long)uidgid->cr_uid,
8079 (unsigned long long)uidgid->cr_gid);
8081 if (ef->cr_extra_flags & CLFE_NID) {
8082 struct changelog_ext_nid *nid =
8083 changelog_rec_nid(rec);
8086 libcfs_nid2str(nid->cr_nid));
8089 if (ef->cr_extra_flags & CLFE_OPEN) {
8090 struct changelog_ext_openmode *omd =
8091 changelog_rec_openmode(rec);
8092 char mode[] = "---";
8094 /* exec mode must be exclusive */
8095 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8098 if (omd->cr_openflags & MDS_FMODE_READ)
8100 if (omd->cr_openflags &
8107 if (strcmp(mode, "---") != 0)
8108 printf(" m=%s", mode);
8111 if (ef->cr_extra_flags & CLFE_XATTR) {
8112 struct changelog_ext_xattr *xattr =
8113 changelog_rec_xattr(rec);
8115 if (xattr->cr_xattr[0] != '\0')
8116 printf(" x=%s", xattr->cr_xattr);
8120 if (!fid_is_zero(&rec->cr_pfid))
8121 printf(" p="DFID, PFID(&rec->cr_pfid));
8122 if (rec->cr_namelen)
8123 printf(" %.*s", rec->cr_namelen,
8124 changelog_rec_name(rec));
8126 if (rec->cr_flags & CLF_RENAME) {
8127 struct changelog_ext_rename *rnm =
8128 changelog_rec_rename(rec);
8130 if (!fid_is_zero(&rnm->cr_sfid))
8131 printf(" s="DFID" sp="DFID" %.*s",
8132 PFID(&rnm->cr_sfid),
8133 PFID(&rnm->cr_spfid),
8134 (int)changelog_rec_snamelen(rec),
8135 changelog_rec_sname(rec));
8139 llapi_changelog_free(&rec);
8142 llapi_changelog_fini(&changelog_priv);
8145 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8146 progname, strerror(errno = -rc));
8148 return (rc == 1 ? 0 : rc);
8151 static int lfs_changelog_clear(int argc, char **argv)
8159 endrec = strtoll(argv[3], NULL, 10);
8161 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8164 fprintf(stderr, "%s: record out of range: %llu\n",
8166 else if (rc == -ENOENT)
8167 fprintf(stderr, "%s: no changelog user: %s\n",
8170 fprintf(stderr, "%s error: %s\n", argv[0],
8179 static int lfs_fid2path(int argc, char **argv)
8181 struct option long_opts[] = {
8182 { .val = 'c', .name = "cur", .has_arg = no_argument },
8183 { .val = 'l', .name = "link", .has_arg = required_argument },
8184 { .val = 'r', .name = "rec", .has_arg = required_argument },
8186 char short_opts[] = "cl:r:";
8187 char mntdir[PATH_MAX];
8188 char *device, *fid, *path, *rootpath;
8189 long long recno = -1;
8194 char *endptr = NULL;
8196 while ((rc = getopt_long(argc, argv, short_opts,
8197 long_opts, NULL)) != -1) {
8203 linkno = strtol(optarg, &endptr, 10);
8204 if (*endptr != '\0') {
8206 "%s fid2path: invalid linkno '%s'\n",
8212 recno = strtoll(optarg, &endptr, 10);
8213 if (*endptr != '\0') {
8215 "%s fid2path: invalid recno '%s'\n",
8222 "%s fid2path: unrecognized option '%s'\n",
8223 progname, argv[optind - 1]);
8230 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
8235 device = argv[optind++];
8236 path = calloc(1, PATH_MAX);
8240 "%s fid2path: cannot allocate memory for path: %s\n",
8241 progname, strerror(-rc));
8246 /* in case that device is not the mountpoint */
8247 if (device[0] == '/') {
8248 rc = llapi_search_mounts(device, 0, mntdir, NULL);
8253 "%s fid2path: %s has no mountpoint: %s\n",
8254 progname, device, strerror(-rc));
8258 while (optind < argc) {
8259 fid = argv[optind++];
8261 lnktmp = (linkno >= 0) ? linkno : 0;
8263 int oldtmp = lnktmp;
8264 long long rectmp = recno;
8267 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
8271 "%s fid2path: cannot find %s %s: %s\n",
8272 progname, device, fid,
8273 strerror(errno = -rc2));
8280 fprintf(stdout, "%lld ", rectmp);
8281 if (device[0] == '/') {
8282 fprintf(stdout, "%s", rootpath);
8283 if (rootpath[strlen(rootpath) - 1] != '/')
8284 fprintf(stdout, "/");
8285 } else if (path[0] == '\0') {
8286 fprintf(stdout, "/");
8288 fprintf(stdout, "%s\n", path);
8291 /* specified linkno */
8293 if (oldtmp == lnktmp)
8303 static int lfs_path2fid(int argc, char **argv)
8305 struct option long_opts[] = {
8306 { .val = 'p', .name = "parents", .has_arg = no_argument },
8309 const char short_opts[] = "p";
8310 const char *sep = "";
8313 bool show_parents = false;
8315 while ((rc = getopt_long(argc, argv, short_opts,
8316 long_opts, NULL)) != -1) {
8319 show_parents = true;
8323 "%s path2fid: unrecognized option '%s'\n",
8324 progname, argv[optind - 1]);
8329 if (optind > argc - 1) {
8330 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8333 } else if (optind < argc - 1) {
8338 for (path = argv + optind; *path != NULL; path++) {
8341 if (!show_parents) {
8342 err = llapi_path2fid(*path, &fid);
8344 printf("%s%s"DFID"\n",
8345 *sep != '\0' ? *path : "", sep,
8348 char name[NAME_MAX + 1];
8349 unsigned int linkno = 0;
8351 while ((err = llapi_path2parent(*path, linkno, &fid,
8352 name, sizeof(name))) == 0) {
8353 if (*sep != '\0' && linkno == 0)
8354 printf("%s%s", *path, sep);
8356 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8361 /* err == -ENODATA is end-of-loop */
8362 if (linkno > 0 && err == -ENODATA) {
8370 "%s path2fid: cannot get %sfid for '%s': %s\n",
8371 progname, show_parents ? "parent " : "", *path,
8383 #define MAX_ERRNO 4095
8384 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8386 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8390 rc = llapi_rmfid(device, fa);
8392 fprintf(stderr, "rmfid(): rc = %d\n", rc);
8396 for (k = 0; k < fa->fa_nr; k++) {
8397 rc = (__s32)fa->fa_fids[k].f_ver;
8398 if (!IS_ERR_VALUE(rc))
8404 fa->fa_fids[k].f_ver = 0;
8405 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
8406 PFID(&fa->fa_fids[k]), rc);
8412 static int lfs_rmfid(int argc, char **argv)
8414 char *fidstr, *device;
8415 int rc = 0, rc2, nr;
8416 struct fid_array *fa;
8418 if (optind > argc - 1) {
8419 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8423 device = argv[optind++];
8426 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8432 while (optind < argc) {
8435 fidstr = argv[optind++];
8436 while (*fidstr == '[')
8438 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8440 fprintf(stderr, "unrecognized FID: %s\n",
8445 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8446 /* start another batch */
8447 rc2 = lfs_rmfid_and_show_errors(device, fa);
8454 rc2 = lfs_rmfid_and_show_errors(device, fa);
8462 static int lfs_data_version(int argc, char **argv)
8469 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8472 fprintf(stderr, "%s data_version: FILE must be specified\n",
8477 while ((c = getopt(argc, argv, "nrw")) != -1) {
8480 data_version_flags = 0;
8483 data_version_flags |= LL_DV_RD_FLUSH;
8486 data_version_flags |= LL_DV_WR_FLUSH;
8490 "%s data_version: unrecognized option '%s'\n",
8491 progname, argv[optind - 1]);
8495 if (optind == argc) {
8496 fprintf(stderr, "%s data_version: FILE must be specified\n",
8501 path = argv[optind];
8502 fd = open(path, O_RDONLY);
8505 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8506 progname, path, strerror(-rc));
8510 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8513 "%s data_version: cannot get version for '%s': %s\n",
8514 progname, path, strerror(-rc));
8516 printf("%ju" "\n", (uintmax_t)data_version);
8522 static int lfs_hsm_state(int argc, char **argv)
8527 struct hsm_user_state hus;
8535 rc = llapi_hsm_state_get(path, &hus);
8537 fprintf(stderr, "can't get hsm state for %s: %s\n",
8538 path, strerror(errno = -rc));
8542 /* Display path name and status flags */
8543 printf("%s: (0x%08x)", path, hus.hus_states);
8545 if (hus.hus_states & HS_RELEASED)
8546 printf(" released");
8547 if (hus.hus_states & HS_EXISTS)
8549 if (hus.hus_states & HS_DIRTY)
8551 if (hus.hus_states & HS_ARCHIVED)
8552 printf(" archived");
8553 /* Display user-settable flags */
8554 if (hus.hus_states & HS_NORELEASE)
8555 printf(" never_release");
8556 if (hus.hus_states & HS_NOARCHIVE)
8557 printf(" never_archive");
8558 if (hus.hus_states & HS_LOST)
8559 printf(" lost_from_hsm");
8561 if (hus.hus_archive_id != 0)
8562 printf(", archive_id:%d", hus.hus_archive_id);
8565 } while (++i < argc);
8570 #define LFS_HSM_SET 0
8571 #define LFS_HSM_CLEAR 1
8574 * Generic function to set or clear HSM flags.
8575 * Used by hsm_set and hsm_clear.
8577 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8579 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8581 struct option long_opts[] = {
8582 { .val = 'A', .name = "archived", .has_arg = no_argument },
8583 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8584 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8585 { .val = 'e', .name = "exists", .has_arg = no_argument },
8586 { .val = 'l', .name = "lost", .has_arg = no_argument },
8587 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8588 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8590 char short_opts[] = "lraAdei:";
8594 __u32 archive_id = 0;
8600 while ((c = getopt_long(argc, argv, short_opts,
8601 long_opts, NULL)) != -1) {
8607 mask |= HS_NOARCHIVE;
8610 mask |= HS_ARCHIVED;
8613 mask |= HS_NORELEASE;
8622 archive_id = strtol(optarg, &end, 10);
8624 fprintf(stderr, "invalid archive_id: '%s'\n",
8632 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8633 argv[0], argv[optind - 1]);
8638 /* User should have specified a flag */
8642 while (optind < argc) {
8643 path = argv[optind];
8645 /* If mode == 0, this means we apply the mask. */
8646 if (mode == LFS_HSM_SET)
8647 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8649 rc = llapi_hsm_state_set(path, 0, mask, 0);
8652 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8653 path, strerror(errno = -rc));
8662 static int lfs_hsm_action(int argc, char **argv)
8667 struct hsm_current_action hca;
8668 struct hsm_extent he;
8669 enum hsm_user_action hua;
8670 enum hsm_progress_states hps;
8678 rc = llapi_hsm_current_action(path, &hca);
8680 fprintf(stderr, "can't get hsm action for %s: %s\n",
8681 path, strerror(errno = -rc));
8684 he = hca.hca_location;
8685 hua = hca.hca_action;
8686 hps = hca.hca_state;
8688 printf("%s: %s", path, hsm_user_action2name(hua));
8690 /* Skip file without action */
8691 if (hca.hca_action == HUA_NONE) {
8696 printf(" %s ", hsm_progress_state2name(hps));
8698 if ((hps == HPS_RUNNING) &&
8699 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8700 printf("(%llu bytes moved)\n",
8701 (unsigned long long)he.length);
8702 else if ((he.offset + he.length) == LUSTRE_EOF)
8703 printf("(from %llu to EOF)\n",
8704 (unsigned long long)he.offset);
8706 printf("(from %llu to %llu)\n",
8707 (unsigned long long)he.offset,
8708 (unsigned long long)(he.offset + he.length));
8710 } while (++i < argc);
8715 static int lfs_hsm_set(int argc, char **argv)
8717 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8720 static int lfs_hsm_clear(int argc, char **argv)
8722 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8726 * Check file state and return its fid, to be used by lfs_hsm_request().
8728 * \param[in] file Path to file to check
8729 * \param[in,out] fid Pointer to allocated lu_fid struct.
8730 * \param[in,out] last_dev Pointer to last device id used.
8732 * \return 0 on success.
8734 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8740 rc = lstat(file, &st);
8742 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8746 * Checking for regular file as archiving as posix copytool
8747 * rejects archiving files other than regular files
8749 if (!S_ISREG(st.st_mode)) {
8750 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8753 /* A request should be ... */
8754 if (*last_dev != st.st_dev && *last_dev != 0) {
8756 "All files should be on the same filesystem: %s\n",
8760 *last_dev = st.st_dev;
8762 rc = llapi_path2fid(file, fid);
8764 fprintf(stderr, "Cannot read FID of %s: %s\n",
8765 file, strerror(-rc));
8771 /* Fill an HSM HUR item with a given file name.
8773 * If mntpath is set, then the filename is actually a FID, and no
8774 * lookup on the filesystem will be performed.
8776 * \param[in] hur the user request to fill
8777 * \param[in] idx index of the item inside the HUR to fill
8778 * \param[in] mntpath mountpoint of Lustre
8779 * \param[in] fname filename (if mtnpath is NULL)
8780 * or FID (if mntpath is set)
8781 * \param[in] last_dev pointer to last device id used
8783 * \retval 0 on success
8784 * \retval CMD_HELP or a negative errno on error
8786 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8787 const char *mntpath, const char *fname,
8790 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8793 hui->hui_extent.length = -1;
8796 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8798 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8801 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8805 hur->hur_request.hr_itemcount++;
8810 static int lfs_hsm_request(int argc, char **argv, int action)
8812 struct option long_opts[] = {
8813 { .val = 'a', .name = "archive", .has_arg = required_argument },
8814 { .val = 'D', .name = "data", .has_arg = required_argument },
8815 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8816 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8819 char short_opts[] = "l:D:a:m:";
8820 struct hsm_user_request *hur, *oldhur;
8825 char *filelist = NULL;
8826 char fullpath[PATH_MAX];
8827 char *opaque = NULL;
8831 int nbfile_alloc = 0;
8832 char *some_file = NULL;
8833 char *mntpath = NULL;
8839 while ((c = getopt_long(argc, argv, short_opts,
8840 long_opts, NULL)) != -1) {
8849 if (action != HUA_ARCHIVE &&
8850 action != HUA_REMOVE) {
8852 "error: -a is supported only when archiving or removing\n");
8855 archive_id = atoi(optarg);
8860 some_file = strdup(optarg);
8866 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8867 argv[0], argv[optind - 1]);
8872 /* All remaining args are files, so we have at least nbfile */
8873 nbfile = argc - optind;
8875 if ((nbfile == 0) && (!filelist))
8879 opaque_len = strlen(opaque);
8882 * Alloc the request structure with enough place to store all files
8883 * from command line.
8885 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8887 fprintf(stderr, "Cannot create the request: %s\n",
8891 nbfile_alloc = nbfile;
8893 hur->hur_request.hr_action = action;
8894 hur->hur_request.hr_archive_id = archive_id;
8895 hur->hur_request.hr_flags = 0;
8897 /* All remaining args are files, add them */
8898 if (nbfile != 0 && some_file == NULL)
8899 some_file = strdup(argv[optind]);
8901 for (i = 0; i < nbfile; i++) {
8902 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8908 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8910 /* If a filelist was specified, read the filelist from it. */
8912 fp = fopen(filelist, "r");
8914 fprintf(stderr, "Cannot read the file list %s: %s\n",
8915 filelist, strerror(errno));
8920 while ((rc = getline(&line, &len, fp)) != -1) {
8922 * If allocated buffer was too small, get something
8925 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8928 nbfile_alloc = nbfile_alloc * 2 + 1;
8930 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8934 "hsm: cannot allocate the request: %s\n",
8941 size = hur_len(oldhur);
8944 "hsm: cannot allocate %u files + %u bytes data\n",
8945 oldhur->hur_request.hr_itemcount,
8946 oldhur->hur_request.hr_data_len);
8953 memcpy(hur, oldhur, size);
8958 if (line[strlen(line) - 1] == '\n')
8959 line[strlen(line) - 1] = '\0';
8961 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8962 mntpath, line, &last_dev);
8978 /* If a --data was used, add it to the request */
8979 hur->hur_request.hr_data_len = opaque_len;
8981 memcpy(hur_data(hur), opaque, opaque_len);
8983 /* Send the HSM request */
8984 if (realpath(some_file, fullpath) == NULL) {
8985 fprintf(stderr, "Could not find path '%s': %s\n",
8986 some_file, strerror(errno));
8988 rc = llapi_hsm_request(fullpath, hur);
8990 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8991 some_file, strerror(-rc));
9001 static int lfs_hsm_archive(int argc, char **argv)
9003 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9006 static int lfs_hsm_restore(int argc, char **argv)
9008 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9011 static int lfs_hsm_release(int argc, char **argv)
9013 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9016 static int lfs_hsm_remove(int argc, char **argv)
9018 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9021 static int lfs_hsm_cancel(int argc, char **argv)
9023 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9026 static int lfs_swap_layouts(int argc, char **argv)
9031 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9032 SWAP_LAYOUTS_KEEP_MTIME |
9033 SWAP_LAYOUTS_KEEP_ATIME);
9036 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9038 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9040 int lfs_get_mode(const char *string)
9042 enum lock_mode_user mode;
9044 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9045 if (lock_mode_names[mode] == NULL)
9047 if (strcmp(string, lock_mode_names[mode]) == 0)
9054 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9056 enum lu_ladvise_type advice;
9059 advice < ARRAY_SIZE(ladvise_names); advice++) {
9060 if (ladvise_names[advice] == NULL)
9062 if (strcmp(string, ladvise_names[advice]) == 0)
9066 return LU_LADVISE_INVALID;
9069 static int lfs_ladvise(int argc, char **argv)
9071 struct option long_opts[] = {
9072 { .val = 'a', .name = "advice", .has_arg = required_argument },
9073 { .val = 'b', .name = "background", .has_arg = no_argument },
9074 { .val = 'e', .name = "end", .has_arg = required_argument },
9075 { .val = 'l', .name = "length", .has_arg = required_argument },
9076 { .val = 'm', .name = "mode", .has_arg = required_argument },
9077 { .val = 's', .name = "start", .has_arg = required_argument },
9078 { .val = 'u', .name = "unset", .has_arg = no_argument },
9080 char short_opts[] = "a:be:l:m:s:u";
9085 struct llapi_lu_ladvise advice;
9086 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9087 unsigned long long start = 0;
9088 unsigned long long end = LUSTRE_EOF;
9089 unsigned long long length = 0;
9090 unsigned long long size_units;
9091 unsigned long long flags = 0;
9095 while ((c = getopt_long(argc, argv, short_opts,
9096 long_opts, NULL)) != -1) {
9099 advice_type = lfs_get_ladvice(optarg);
9100 if (advice_type == LU_LADVISE_INVALID) {
9102 "%s: invalid advice type '%s'\n",
9104 fprintf(stderr, "Valid types:");
9106 for (advice_type = 0;
9107 advice_type < ARRAY_SIZE(ladvise_names);
9109 if (ladvise_names[advice_type] == NULL)
9111 fprintf(stderr, " %s",
9112 ladvise_names[advice_type]);
9114 fprintf(stderr, "\n");
9127 rc = llapi_parse_size(optarg, &end,
9130 fprintf(stderr, "%s: bad end offset '%s'\n",
9137 rc = llapi_parse_size(optarg, &start,
9141 "%s: bad start offset '%s'\n",
9148 rc = llapi_parse_size(optarg, &length,
9151 fprintf(stderr, "%s: bad length '%s'\n",
9157 mode = lfs_get_mode(optarg);
9160 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9168 fprintf(stderr, "%s: option '%s' unrecognized\n",
9169 argv[0], argv[optind - 1]);
9174 if (advice_type == LU_LADVISE_INVALID) {
9175 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9176 fprintf(stderr, "Valid types:");
9177 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9179 if (ladvise_names[advice_type] == NULL)
9181 fprintf(stderr, " %s", ladvise_names[advice_type]);
9183 fprintf(stderr, "\n");
9187 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9189 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9194 if (argc <= optind) {
9195 fprintf(stderr, "%s: please give one or more file names\n",
9200 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9201 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9206 if (end == LUSTRE_EOF && length != 0)
9207 end = start + length;
9210 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9211 argv[0], start, end);
9215 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9216 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9221 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9222 fprintf(stderr, "%s: mode is required with lockahead\n",
9227 while (optind < argc) {
9230 path = argv[optind++];
9232 fd = open(path, O_RDONLY);
9234 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9235 argv[0], path, strerror(errno));
9240 advice.lla_start = start;
9241 advice.lla_end = end;
9242 advice.lla_advice = advice_type;
9243 advice.lla_value1 = 0;
9244 advice.lla_value2 = 0;
9245 advice.lla_value3 = 0;
9246 advice.lla_value4 = 0;
9247 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9248 advice.lla_lockahead_mode = mode;
9249 advice.lla_peradvice_flags = flags;
9252 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9256 "%s: cannot give advice '%s' to file '%s': %s\n",
9257 argv[0], ladvise_names[advice_type],
9258 path, strerror(errno));
9264 if (rc == 0 && rc2 < 0)
9270 static const char *const heat_names[] = LU_HEAT_NAMES;
9272 static int lfs_heat_get(int argc, char **argv)
9274 struct lu_heat *heat;
9283 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9285 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9290 while (optind < argc) {
9291 path = argv[optind++];
9293 fd = open(path, O_RDONLY);
9295 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9296 argv[0], path, strerror(errno));
9301 heat->lh_count = OBD_HEAT_COUNT;
9302 rc2 = llapi_heat_get(fd, heat);
9306 "%s: cannot get heat of file '%s': %s\n",
9307 argv[0], path, strerror(errno));
9311 printf("flags: %x\n", heat->lh_flags);
9312 for (i = 0; i < heat->lh_count; i++)
9313 printf("%s: %llu\n", heat_names[i],
9314 (unsigned long long)heat->lh_heat[i]);
9316 if (rc == 0 && rc2 < 0)
9324 static int lfs_heat_set(int argc, char **argv)
9326 struct option long_opts[] = {
9327 { .val = 'c', .name = "clear", .has_arg = no_argument },
9328 { .val = 'o', .name = "off", .has_arg = no_argument },
9329 { .val = 'O', .name = "on", .has_arg = no_argument },
9331 enum lu_heat_flag flags = 0;
9341 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9344 flags |= LU_HEAT_FLAG_CLEAR;
9347 flags |= LU_HEAT_FLAG_CLEAR;
9348 flags |= LU_HEAT_FLAG_OFF;
9351 flags &= ~LU_HEAT_FLAG_OFF;
9356 fprintf(stderr, "%s: option '%s' unrecognized\n",
9357 argv[0], argv[optind - 1]);
9362 if (argc <= optind) {
9363 fprintf(stderr, "%s: please give one or more file names\n",
9368 while (optind < argc) {
9369 path = argv[optind++];
9371 fd = open(path, O_RDONLY);
9373 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9374 argv[0], path, strerror(errno));
9379 rc2 = llapi_heat_set(fd, flags);
9383 "%s: cannot setflags heat of file '%s': %s\n",
9384 argv[0], path, strerror(errno));
9388 if (rc == 0 && rc2 < 0)
9395 * The input string contains a comma delimited list of component ids and
9396 * ranges, for example "1,2-4,7".
9398 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9400 bool end_of_loop = false;
9408 while (!end_of_loop) {
9412 char *endptr = NULL;
9415 ptr = strchrnul(arg, ',');
9416 end_of_loop = *ptr == '\0';
9419 start_index = strtol(arg, &endptr, 0);
9420 if (endptr == arg) /* no data at all */
9422 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9424 if (start_index < 0)
9427 end_index = start_index;
9428 if (*endptr == '-') {
9429 end_index = strtol(endptr + 1, &endptr, 0);
9430 if (*endptr != '\0')
9432 if (end_index < start_index)
9436 for (i = start_index; i <= end_index && size > 0; i++) {
9439 /* remove duplicate */
9440 for (j = 0; j < nr; j++) {
9444 if (j == nr) { /* no duplicate */
9450 if (size == 0 && i < end_index)
9457 if (!end_of_loop && ptr)
9460 return rc < 0 ? rc : nr;
9464 * struct verify_mirror_id - Mirror id to be verified.
9465 * @mirror_id: A specified mirror id.
9466 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9468 struct verify_mirror_id {
9474 * compare_mirror_ids() - Compare mirror ids.
9475 * @layout: Mirror component list.
9476 * @cbdata: Callback data in verify_mirror_id structure.
9478 * This is a callback function called by llapi_layout_comp_iterate()
9479 * to compare the specified mirror id with the one in the current
9480 * component of @layout. If they are the same, then the specified
9481 * mirror id is valid.
9483 * Return: a negative error code on failure or
9484 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9485 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9488 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9490 struct verify_mirror_id *mirror_id_cbdata =
9491 (struct verify_mirror_id *)cbdata;
9495 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9499 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9500 progname, strerror(errno));
9504 if (mirror_id_cbdata->mirror_id == mirror_id) {
9505 mirror_id_cbdata->is_valid_id = true;
9506 return LLAPI_LAYOUT_ITER_STOP;
9509 return LLAPI_LAYOUT_ITER_CONT;
9513 * verify_mirror_ids() - Verify specified mirror ids.
9514 * @fname: Mirrored file name.
9515 * @mirror_ids: Specified mirror ids to be verified.
9516 * @ids_nr: Number of specified mirror ids.
9518 * This function verifies that specified @mirror_ids are valid
9519 * in the mirrored file @fname.
9521 * Return: 0 on success or a negative error code on failure.
9524 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9526 struct llapi_layout *layout = NULL;
9527 struct verify_mirror_id mirror_id_cbdata = { 0 };
9538 if (stat(fname, &stbuf) < 0) {
9539 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9540 progname, fname, strerror(errno));
9545 if (!S_ISREG(stbuf.st_mode)) {
9546 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9552 fd = open(fname, O_DIRECT | O_RDONLY);
9554 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9555 progname, fname, strerror(errno));
9560 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9562 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9563 progname, fname, strerror(errno));
9567 layout = llapi_layout_get_by_fd(fd, 0);
9569 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9570 progname, fname, strerror(errno));
9572 llapi_lease_release(fd);
9576 rc = llapi_layout_flags_get(layout, &flr_state);
9578 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9579 progname, fname, strerror(errno));
9584 flr_state &= LCM_FL_FLR_MASK;
9585 switch (flr_state) {
9588 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9589 progname, fname, llapi_layout_flags_string(flr_state));
9596 for (i = 0; i < ids_nr; i++) {
9597 mirror_id_cbdata.mirror_id = mirror_ids[i];
9598 mirror_id_cbdata.is_valid_id = false;
9600 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9605 "%s: '%s' failed to verify mirror id: %u.\n",
9606 progname, fname, mirror_ids[i]);
9610 if (!mirror_id_cbdata.is_valid_id) {
9613 "%s: '%s' invalid specified mirror id: %u.\n",
9614 progname, fname, mirror_ids[i]);
9620 llapi_layout_free(layout);
9621 llapi_lease_release(fd);
9629 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9630 __u16 *mirror_ids, int ids_nr)
9632 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9633 struct llapi_layout *layout;
9644 if (stat(fname, &stbuf) < 0) {
9645 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9646 progname, fname, strerror(errno));
9650 if (!S_ISREG(stbuf.st_mode)) {
9651 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9657 fd = open(fname, O_DIRECT | O_RDWR);
9659 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9660 progname, fname, strerror(errno));
9665 layout = llapi_layout_get_by_fd(fd, 0);
9667 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9668 progname, fname, strerror(errno));
9673 rc = llapi_layout_flags_get(layout, &flr_state);
9675 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9676 progname, fname, strerror(errno));
9681 flr_state &= LCM_FL_FLR_MASK;
9682 if (flr_state == LCM_FL_NONE) {
9684 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9689 /* get stale component info */
9690 comp_size = llapi_mirror_find_stale(layout, comp_array,
9691 ARRAY_SIZE(comp_array),
9692 mirror_ids, ids_nr);
9693 if (comp_size <= 0) {
9698 ioc->lil_mode = LL_LEASE_WRLCK;
9699 ioc->lil_flags = LL_LEASE_RESYNC;
9700 rc = llapi_lease_set(fd, ioc);
9702 if (rc == -EALREADY)
9706 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9707 progname, fname, strerror(-rc));
9711 /* get the read range [start, end) */
9712 start = comp_array[0].lrc_start;
9713 end = comp_array[0].lrc_end;
9714 for (idx = 1; idx < comp_size; idx++) {
9715 if (comp_array[idx].lrc_start < start)
9716 start = comp_array[idx].lrc_start;
9717 if (end < comp_array[idx].lrc_end)
9718 end = comp_array[idx].lrc_end;
9721 rc = llapi_lease_check(fd);
9722 if (rc != LL_LEASE_WRLCK) {
9723 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9728 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9731 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9732 progname, fname, strerror(-rc));
9734 /* need to do the lease unlock even resync fails */
9735 ioc->lil_mode = LL_LEASE_UNLCK;
9736 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9738 for (idx = 0; idx < comp_size; idx++) {
9739 if (comp_array[idx].lrc_synced) {
9740 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9745 rc2 = llapi_lease_set(fd, ioc);
9747 * llapi_lease_set returns lease mode when it request to unlock
9751 /* rc2 == 0 means lost lease lock */
9752 if (rc2 == 0 && rc == 0)
9754 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9756 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9760 llapi_layout_free(layout);
9767 static inline int lfs_mirror_resync(int argc, char **argv)
9769 struct ll_ioc_lease *ioc = NULL;
9770 __u16 mirror_ids[128] = { 0 };
9775 struct option long_opts[] = {
9776 { .val = 'o', .name = "only", .has_arg = required_argument },
9779 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9782 rc = parse_mirror_ids(mirror_ids,
9783 sizeof(mirror_ids) / sizeof(__u16),
9787 "%s: bad mirror ids '%s'.\n",
9794 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9795 argv[0], argv[optind - 1]);
9801 if (argc == optind) {
9802 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9807 if (ids_nr > 0 && argc > optind + 1) {
9809 "%s: option '--only' cannot be used upon multiple files.\n",
9816 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9821 /* set the lease on the file */
9822 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9824 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9825 argv[0], strerror(errno));
9830 for (; optind < argc; optind++) {
9831 rc = lfs_mirror_resync_file(argv[optind], ioc,
9832 mirror_ids, ids_nr);
9833 /* ignore previous file's error, continue with next file */
9836 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9844 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9846 struct llapi_layout *layout;
9849 layout = llapi_layout_get_by_fd(fd, 0);
9851 fprintf(stderr, "could not get layout.\n");
9855 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9857 fprintf(stderr, "failed to iterate layout\n");
9858 llapi_layout_free(layout);
9861 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9862 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9863 llapi_layout_free(layout);
9867 llapi_layout_free(layout);
9873 * Check whether two files are the same file
9874 * \retval 0 same file
9875 * \retval 1 not the same file
9876 * \retval <0 error code
9878 static inline int check_same_file(int fd, const char *f2)
9883 if (fstat(fd, &stbuf1) < 0)
9886 if (stat(f2, &stbuf2) < 0)
9889 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9890 stbuf1.st_ino == stbuf2.st_ino)
9896 static inline int lfs_mirror_read(int argc, char **argv)
9899 __u16 mirror_id = 0;
9900 const char *outfile = NULL;
9906 const size_t buflen = 4 << 20;
9908 struct option long_opts[] = {
9909 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9910 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9913 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9918 mirror_id = strtoul(optarg, &end, 0);
9919 if (*end != '\0' || mirror_id == 0) {
9921 "%s %s: invalid mirror ID '%s'\n",
9922 progname, argv[0], optarg);
9930 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9931 progname, argv[optind - 1]);
9936 if (argc == optind) {
9937 fprintf(stderr, "%s %s: no mirrored file provided\n",
9940 } else if (argc > optind + 1) {
9941 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9945 if (mirror_id == 0) {
9946 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9951 /* open mirror file */
9952 fname = argv[optind];
9953 fd = open(fname, O_DIRECT | O_RDONLY);
9955 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9956 progname, argv[0], fname, strerror(errno));
9960 /* verify mirror id */
9961 rc = verify_mirror_id_by_fd(fd, mirror_id);
9964 "%s %s: cannot find mirror with ID %u in '%s'\n",
9965 progname, argv[0], mirror_id, fname);
9969 /* open output file - O_EXCL ensures output is not the same as input */
9971 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9973 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9974 progname, argv[0], outfile, strerror(errno));
9979 outfd = STDOUT_FILENO;
9982 /* allocate buffer */
9983 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9985 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9986 progname, argv[0], rc);
9993 ssize_t written = 0;
9995 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9996 if (bytes_read < 0) {
9999 "%s %s: fail to read data from mirror %u: %s\n",
10000 progname, argv[0], mirror_id, strerror(-rc));
10005 if (bytes_read == 0)
10008 while (written < bytes_read) {
10011 written2 = write(outfd, buf + written,
10012 bytes_read - written);
10013 if (written2 < 0) {
10015 "%s %s: fail to write %s: %s\n",
10016 progname, argv[0], outfile ? : "STDOUT",
10021 written += written2;
10024 if (written != bytes_read) {
10026 "%s %s: written %ld bytes does not match with %ld read.\n",
10027 progname, argv[0], written, bytes_read);
10049 static inline int lfs_mirror_write(int argc, char **argv)
10052 __u16 mirror_id = 0;
10053 const char *inputfile = NULL;
10059 const size_t buflen = 4 << 20;
10061 size_t page_size = sysconf(_SC_PAGESIZE);
10062 struct ll_ioc_lease_id ioc;
10064 struct option long_opts[] = {
10065 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10066 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10067 { .name = NULL } };
10069 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10074 mirror_id = strtoul(optarg, &end, 0);
10075 if (*end != '\0' || mirror_id == 0) {
10077 "%s %s: invalid mirror ID '%s'\n",
10078 progname, argv[0], optarg);
10083 inputfile = optarg;
10086 fprintf(stderr, "%s: option '%s' unrecognized\n",
10087 progname, argv[optind - 1]);
10092 if (argc == optind) {
10093 fprintf(stderr, "%s %s: no mirrored file provided\n",
10094 progname, argv[0]);
10096 } else if (argc > optind + 1) {
10097 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10101 if (mirror_id == 0) {
10102 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10103 progname, argv[0]);
10107 /* open mirror file */
10108 fname = argv[optind];
10109 fd = open(fname, O_DIRECT | O_WRONLY);
10111 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10112 progname, argv[0], fname, strerror(errno));
10116 /* verify mirror id */
10117 rc = verify_mirror_id_by_fd(fd, mirror_id);
10120 "%s %s: cannot find mirror with ID %u in '%s'\n",
10121 progname, argv[0], mirror_id, fname);
10125 /* open input file */
10127 rc = check_same_file(fd, inputfile);
10130 "%s %s: input file cannot be the mirrored file\n",
10131 progname, argv[0]);
10137 inputfd = open(inputfile, O_RDONLY, 0644);
10139 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10140 progname, argv[0], inputfile, strerror(errno));
10145 inputfd = STDIN_FILENO;
10148 /* allocate buffer */
10149 rc = posix_memalign(&buf, page_size, buflen);
10151 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10152 progname, argv[0], rc);
10153 goto close_inputfd;
10156 /* prepare target mirror components instantiation */
10157 ioc.lil_mode = LL_LEASE_WRLCK;
10158 ioc.lil_flags = LL_LEASE_RESYNC;
10159 ioc.lil_mirror_id = mirror_id;
10160 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10163 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10164 progname, argv[0], fname, strerror(errno));
10170 ssize_t bytes_read;
10174 rc = llapi_lease_check(fd);
10175 if (rc != LL_LEASE_WRLCK) {
10176 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10177 progname, argv[0], fname);
10181 bytes_read = read(inputfd, buf, buflen);
10182 if (bytes_read < 0) {
10185 "%s %s: fail to read data from '%s': %s\n",
10186 progname, argv[0], inputfile ? : "STDIN",
10193 if (bytes_read == 0)
10196 /* round up to page align to make direct IO happy. */
10197 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10199 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10204 "%s %s: fail to write to mirror %u: %s\n",
10205 progname, argv[0], mirror_id,
10213 if (pos & (page_size - 1)) {
10214 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10219 ioc.lil_mode = LL_LEASE_UNLCK;
10220 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10222 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10227 "%s %s: release lease lock of '%s' failed: %s\n",
10228 progname, argv[0], fname, strerror(errno));
10245 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10247 struct llapi_layout *layout;
10248 struct collect_ids_data cid = { .cid_ids = ids,
10250 .cid_exclude = exclude_id, };
10253 layout = llapi_layout_get_by_fd(fd, 0);
10255 fprintf(stderr, "could not get layout\n");
10259 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10261 fprintf(stderr, "failed to iterate layout\n");
10262 llapi_layout_free(layout);
10266 llapi_layout_free(layout);
10268 return cid.cid_count;
10271 static inline int lfs_mirror_copy(int argc, char **argv)
10274 __u16 read_mirror_id = 0;
10275 __u16 ids[128] = { 0 };
10277 struct llapi_layout *layout = NULL;
10278 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10285 struct ll_ioc_lease *ioc = NULL;
10286 struct ll_ioc_lease_id *resync_ioc;
10288 struct option long_opts[] = {
10289 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10290 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10291 { .name = NULL } };
10293 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10298 read_mirror_id = strtoul(optarg, &end, 0);
10299 if (*end != '\0' || read_mirror_id == 0) {
10301 "%s %s: invalid read mirror ID '%s'\n",
10302 progname, argv[0], optarg);
10307 if (!strcmp(optarg, "-1")) {
10308 /* specify all other mirrors */
10309 ids[0] = (__u16)-1;
10312 count = parse_mirror_ids((__u16 *)ids,
10320 fprintf(stderr, "%s: option '%s' unrecognized\n",
10321 progname, argv[optind - 1]);
10326 if (argc == optind) {
10327 fprintf(stderr, "%s %s: no mirrored file provided\n",
10328 progname, argv[0]);
10330 } else if (argc > optind + 1) {
10331 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10335 if (read_mirror_id == 0) {
10337 "%s %s: no valid read mirror ID %d is provided\n",
10338 progname, argv[0], read_mirror_id);
10344 "%s %s: no write mirror ID is provided\n",
10345 progname, argv[0]);
10349 for (i = 0; i < count; i++) {
10350 if (read_mirror_id == ids[i]) {
10352 "%s %s: read and write mirror ID cannot be the same\n",
10353 progname, argv[0]);
10358 /* open mirror file */
10359 fname = argv[optind];
10361 fd = open(fname, O_DIRECT | O_RDWR);
10363 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10364 progname, argv[0], fname, strerror(errno));
10368 /* write to all other mirrors */
10369 if (ids[0] == (__u16)-1) {
10370 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10374 "%s %s: failed to get other mirror ids in '%s': %d\n",
10375 progname, argv[0], fname, rc);
10380 /* verify mirror id */
10381 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10384 "%s %s: cannot find mirror with ID %u in '%s'\n",
10385 progname, argv[0], read_mirror_id, fname);
10389 for (i = 0; i < count; i++) {
10390 rc = verify_mirror_id_by_fd(fd, ids[i]);
10393 "%s %s: cannot find mirror with ID %u in '%s'\n",
10394 progname, argv[0], ids[i], fname);
10399 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10402 "%s %s: cannot alloc comp id array for ioc: %s\n",
10403 progname, argv[0], strerror(errno));
10408 /* get stale component info */
10409 layout = llapi_layout_get_by_fd(fd, 0);
10411 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10412 progname, argv[0], fname, strerror(errno));
10416 comp_size = llapi_mirror_find_stale(layout, comp_array,
10417 ARRAY_SIZE(comp_array),
10419 llapi_layout_free(layout);
10420 if (comp_size < 0) {
10425 /* prepare target mirror components instantiation */
10426 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10427 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10428 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10430 resync_ioc->lil_mirror_id = ids[0];
10432 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10433 rc = llapi_lease_set(fd, ioc);
10436 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10437 progname, argv[0], fname, strerror(errno));
10441 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10444 fprintf(stderr, "%s %s: copy error: %d\n",
10445 progname, argv[0], rc);
10449 fprintf(stdout, "mirror copied successfully: ");
10450 for (i = 0; i < copied; i++)
10451 fprintf(stdout, "%d ", ids[i]);
10452 fprintf(stdout, "\n");
10454 ioc->lil_mode = LL_LEASE_UNLCK;
10455 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10456 ioc->lil_count = 0;
10457 for (i = 0; i < comp_size; i++) {
10460 for (j = 0; j < copied; j++) {
10461 if (comp_array[i].lrc_mirror_id != ids[j])
10464 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10468 rc = llapi_lease_set(fd, ioc);
10473 "%s %s: release lease lock of '%s' failed: %s\n",
10474 progname, argv[0], fname, strerror(errno));
10489 * struct verify_chunk - Mirror chunk to be verified.
10490 * @chunk: [start, end) of the chunk.
10491 * @mirror_count: Number of mirror ids in @mirror_id array.
10492 * @mirror_id: Array of valid mirror ids that cover the chunk.
10494 struct verify_chunk {
10495 struct lu_extent chunk;
10496 unsigned int mirror_count;
10497 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10501 * print_chunks() - Print chunk information.
10502 * @fname: Mirrored file name.
10503 * @chunks: Array of chunks.
10504 * @chunk_count: Number of chunks in @chunks array.
10506 * This function prints [start, end) of each chunk in @chunks
10507 * for mirrored file @fname, and also prints the valid mirror ids
10508 * that cover the chunk.
10513 void print_chunks(const char *fname, struct verify_chunk *chunks,
10519 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10520 for (i = 0; i < chunk_count; i++) {
10521 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10523 if (chunks[i].mirror_count == 0)
10524 fprintf(stdout, "\t[");
10526 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10527 for (j = 1; j < chunks[i].mirror_count; j++)
10528 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10530 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10532 fprintf(stdout, "\n");
10536 * print_checksums() - Print CRC-32 checksum values.
10537 * @chunk: A chunk and its corresponding valid mirror ids.
10538 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10540 * This function prints CRC-32 checksum values on @chunk for
10541 * each valid mirror that covers it.
10546 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10551 "CRC-32 checksum value for chunk "DEXT":\n",
10552 PEXT(&chunk->chunk));
10553 for (i = 0; i < chunk->mirror_count; i++)
10554 fprintf(stdout, "Mirror %u:\t%#lx\n",
10555 chunk->mirror_id[i], crc[i]);
10556 fprintf(stdout, "\n");
10560 * filter_mirror_id() - Filter specified mirror ids.
10561 * @chunks: Array of chunks.
10562 * @chunk_count: Number of chunks in @chunks array.
10563 * @mirror_ids: Specified mirror ids to be verified.
10564 * @ids_nr: Number of specified mirror ids.
10566 * This function scans valid mirror ids that cover each chunk in @chunks
10567 * and filters specified mirror ids.
10572 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10573 __u16 *mirror_ids, int ids_nr)
10578 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10579 unsigned int valid_count = 0;
10581 for (i = 0; i < chunk_count; i++) {
10582 if (chunks[i].mirror_count == 0)
10586 for (j = 0; j < ids_nr; j++) {
10587 for (k = 0; k < chunks[i].mirror_count; k++) {
10588 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10589 valid_id[valid_count] = mirror_ids[j];
10596 memcpy(chunks[i].mirror_id, valid_id,
10597 sizeof(__u16) * valid_count);
10598 chunks[i].mirror_count = valid_count;
10603 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10604 * @layout: Mirror component list.
10605 * @chunks: Array of chunks.
10606 * @chunks_size: Array size of @chunks.
10608 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10609 * to find out chunk segments and store them in @chunks array.
10611 * The @mirror_id array in each element of @chunks will store the valid
10612 * mirror ids that cover the chunk. If a mirror component covering the
10613 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10614 * will not be stored into the @mirror_id array, and the chunk for that
10615 * mirror will not be verified.
10617 * The @mirror_count in each element of @chunks will store the number of
10618 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10619 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10620 * indicates the chunk is valid in only one mirror. In both cases, the
10621 * chunk will not be verified.
10623 * Here is an example:
10625 * 0 1M 2M 3M 4M EOF
10626 * +------+-------------+--------------------+
10627 * | | | S | mirror1
10628 * +------+------+------+------+-------------+
10629 * | | S | S | | mirror2
10630 * +-------------+------+------+-------------+
10632 * prepared @chunks array will contain 5 elements:
10633 * (([0, 1M), [1, 2], 2),
10634 * ([1M, 2M), [1, 2], 2),
10635 * ([2M, 3M), [1], 1),
10636 * ([3M, 4M], [], 0),
10637 * ([4M, EOF), [2], 1))
10639 * Return: the actual array size of @chunks on success
10640 * or a negative error code on failure.
10643 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10644 struct verify_chunk *chunks,
10645 size_t chunks_size)
10649 uint32_t mirror_id;
10655 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10658 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10661 "%s: move to the first layout component: %s.\n",
10662 progname, strerror(errno));
10668 chunks[idx].chunk.e_end = LUSTRE_EOF;
10670 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10673 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10674 progname, strerror(errno));
10678 if (start > chunks[idx].chunk.e_start ||
10679 end <= chunks[idx].chunk.e_start)
10682 if (end < chunks[idx].chunk.e_end)
10683 chunks[idx].chunk.e_end = end;
10685 rc = llapi_layout_comp_flags_get(layout, &flags);
10688 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10689 progname, strerror(errno));
10693 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10696 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10699 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10700 progname, strerror(errno));
10704 chunks[idx].mirror_id[i] = mirror_id;
10706 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10708 "%s: mirror_id array is too small.\n",
10715 rc = llapi_layout_comp_use(layout,
10716 LLAPI_LAYOUT_COMP_USE_NEXT);
10719 "%s: move to the next layout component: %s.\n",
10720 progname, strerror(errno));
10723 } /* loop through all components */
10725 chunks[idx].mirror_count = i;
10727 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10731 if (idx >= chunks_size) {
10732 fprintf(stderr, "%s: chunks array is too small.\n",
10738 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10742 return rc < 0 ? rc : idx + 1;
10746 * lfs_mirror_verify_chunk() - Verify a chunk.
10747 * @fd: File descriptor of the mirrored file.
10748 * @file_size: Size of the mirrored file.
10749 * @chunk: A chunk and its corresponding valid mirror ids.
10750 * @verbose: Verbose mode.
10752 * This function verifies a @chunk contains exactly the same data
10753 * ammong the mirrors that cover it.
10755 * If @verbose is specified, then the function will print where the
10756 * differences are if the data do not match. Otherwise, it will
10757 * just return an error in that case.
10759 * Return: 0 on success or a negative error code on failure.
10762 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10763 struct verify_chunk *chunk, int verbose)
10765 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10767 size_t page_size = sysconf(_SC_PAGESIZE);
10768 ssize_t bytes_read;
10769 ssize_t bytes_done;
10773 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10777 if (file_size == 0)
10780 rc = posix_memalign(&buf, page_size, buflen);
10781 if (rc) /* error code is returned directly */
10785 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10786 PEXT(&chunk->chunk));
10787 for (i = 0; i < chunk->mirror_count; i++)
10788 fprintf(stdout, " %u", chunk->mirror_id[i]);
10789 fprintf(stdout, "\n");
10793 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10794 pos = chunk->chunk.e_start;
10795 while (bytes_done < count) {
10796 /* compute initial CRC-32 checksum */
10797 crc = crc32(0L, Z_NULL, 0);
10798 memset(crc_array, 0, sizeof(crc_array));
10801 for (i = 0; i < chunk->mirror_count; i++) {
10802 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10804 if (bytes_read < 0) {
10807 "%s: failed to read data from mirror %u: %s.\n",
10808 progname, chunk->mirror_id[i],
10813 /* compute new CRC-32 checksum */
10814 crc_array[i] = crc32(crc, buf, bytes_read);
10818 print_checksums(chunk, crc_array);
10820 /* compare CRC-32 checksum values */
10821 for (i = 1; i < chunk->mirror_count; i++) {
10822 if (crc_array[i] != crc_array[0]) {
10826 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10827 progname, PEXT(&chunk->chunk),
10828 chunk->mirror_id[0],
10829 chunk->mirror_id[i]);
10834 bytes_done += bytes_read;
10837 if (verbose > 1 && rc == 0) {
10838 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10839 PEXT(&chunk->chunk));
10840 for (i = 0; i < chunk->mirror_count; i++)
10841 fprintf(stdout, " %u", chunk->mirror_id[i]);
10842 fprintf(stdout, " PASS\n\n");
10851 * lfs_mirror_verify_file() - Verify a mirrored file.
10852 * @fname: Mirrored file name.
10853 * @mirror_ids: Specified mirror ids to be verified.
10854 * @ids_nr: Number of specified mirror ids.
10855 * @verbose: Verbose mode.
10857 * This function verifies that each SYNC mirror of a mirrored file
10858 * specified by @fname contains exactly the same data.
10860 * If @mirror_ids is specified, then the function will verify the
10861 * mirrors specified by @mirror_ids contain exactly the same data.
10863 * If @verbose is specified, then the function will print where the
10864 * differences are if the data do not match. Otherwise, it will
10865 * just return an error in that case.
10867 * Return: 0 on success or a negative error code on failure.
10870 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10873 struct verify_chunk chunks_array[1024] = { };
10874 struct llapi_layout *layout = NULL;
10876 uint32_t flr_state;
10878 int chunk_count = 0;
10884 if (stat(fname, &stbuf) < 0) {
10885 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10886 progname, fname, strerror(errno));
10891 if (!S_ISREG(stbuf.st_mode)) {
10892 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10898 if (stbuf.st_size == 0) {
10900 fprintf(stdout, "%s: '%s' file size is 0.\n",
10906 fd = open(fname, O_DIRECT | O_RDONLY);
10908 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10909 progname, fname, strerror(errno));
10914 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10916 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10917 progname, fname, strerror(errno));
10921 layout = llapi_layout_get_by_fd(fd, 0);
10923 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10924 progname, fname, strerror(errno));
10926 llapi_lease_release(fd);
10930 rc = llapi_layout_flags_get(layout, &flr_state);
10932 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10933 progname, fname, strerror(errno));
10938 flr_state &= LCM_FL_FLR_MASK;
10939 switch (flr_state) {
10942 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10943 progname, fname, llapi_layout_flags_string(flr_state));
10949 /* find out mirror chunks to be verified */
10950 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10951 ARRAY_SIZE(chunks_array));
10952 if (chunk_count < 0) {
10958 /* filter specified mirror ids */
10959 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10962 print_chunks(fname, chunks_array, chunk_count);
10964 for (idx = 0; idx < chunk_count; idx++) {
10965 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10968 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10970 PEXT(&chunks_array[idx].chunk),
10971 (unsigned long long)stbuf.st_size);
10975 if (chunks_array[idx].mirror_count == 0) {
10977 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10979 PEXT(&chunks_array[idx].chunk));
10981 fprintf(stderr, "skipped\n");
10985 fprintf(stderr, "failed\n");
10989 if (chunks_array[idx].mirror_count == 1) {
10992 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10994 PEXT(&chunks_array[idx].chunk),
10995 chunks_array[idx].mirror_id[0]);
10999 rc = llapi_lease_check(fd);
11000 if (rc != LL_LEASE_RDLCK) {
11001 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11006 /* verify one chunk */
11007 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11008 &chunks_array[idx], verbose);
11022 llapi_layout_free(layout);
11023 llapi_lease_release(fd);
11031 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11032 * @argc: The count of lfs mirror verify command line arguments.
11033 * @argv: Array of strings for lfs mirror verify command line arguments.
11035 * This function parses lfs mirror verify command and verifies the
11036 * specified mirrored file(s).
11038 * Return: 0 on success or a negative error code on failure.
11040 static inline int lfs_mirror_verify(int argc, char **argv)
11042 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11048 char cmd[PATH_MAX];
11050 struct option long_opts[] = {
11051 { .val = 'o', .name = "only", .has_arg = required_argument },
11052 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11053 { .name = NULL } };
11055 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11057 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11060 rc = parse_mirror_ids(mirror_ids,
11061 ARRAY_SIZE(mirror_ids),
11065 "%s: bad mirror ids '%s'.\n",
11072 "%s: at least 2 mirror ids needed with '--only' option.\n",
11082 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11083 progname, argv[optind - 1]);
11089 if (argc == optind) {
11090 fprintf(stderr, "%s: no file name given.\n", progname);
11095 if (ids_nr > 0 && argc > optind + 1) {
11097 "%s: '--only' cannot be used upon multiple files.\n",
11104 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11110 for (; optind < argc; optind++) {
11111 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11121 * lfs_mirror() - Parse and execute lfs mirror commands.
11122 * @argc: The count of lfs mirror command line arguments.
11123 * @argv: Array of strings for lfs mirror command line arguments.
11125 * This function parses lfs mirror commands and performs the
11126 * corresponding functions specified in mirror_cmdlist[].
11128 * Return: 0 on success or an error code on failure.
11130 static int lfs_mirror(int argc, char **argv)
11132 char cmd[PATH_MAX];
11135 setlinebuf(stdout);
11137 Parser_init("lfs-mirror > ", mirror_cmdlist);
11139 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11141 program_invocation_short_name = cmd;
11143 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11145 rc = Parser_commands();
11147 return rc < 0 ? -rc : rc;
11150 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11152 #if __BYTE_ORDER == __BIG_ENDIAN
11153 __swab16s(&attrs->lsa_valid);
11154 __swab64s(&attrs->lsa_size);
11155 __swab64s(&attrs->lsa_blocks);
11159 enum lfs_som_type {
11160 LFS_SOM_SIZE = 0x1,
11161 LFS_SOM_BLOCKS = 0x2,
11162 LFS_SOM_FLAGS = 0x4,
11163 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11167 static int lfs_getsom(int argc, char **argv)
11170 struct lustre_som_attrs *attrs;
11171 char buf[sizeof(*attrs) + 64];
11172 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11175 while ((c = getopt(argc, argv, "sbf")) != -1) {
11178 type = LFS_SOM_SIZE;
11181 type = LFS_SOM_BLOCKS;
11184 type = LFS_SOM_FLAGS;
11187 fprintf(stderr, "%s: invalid option '%c'\n",
11197 fprintf(stderr, "%s: %s\n",
11198 progname, argc == 0 ? "miss file target" :
11199 "input more than 2 files");
11204 attrs = (void *)buf;
11205 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11208 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11209 argv[0], strerror(errno), errno);
11213 lustre_som_swab(attrs);
11216 case LFS_SOM_ATTR_ALL:
11217 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11218 path, (unsigned long long)attrs->lsa_size,
11219 (unsigned long long)attrs->lsa_blocks,
11223 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11225 case LFS_SOM_BLOCKS:
11226 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11228 case LFS_SOM_FLAGS:
11229 printf("%x\n", attrs->lsa_valid);
11232 fprintf(stderr, "%s: unknown option\n", progname);
11240 * lfs_mirror_list_commands() - List lfs mirror commands.
11241 * @argc: The count of command line arguments.
11242 * @argv: Array of strings for command line arguments.
11244 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11246 * Return: 0 on success.
11248 static int lfs_mirror_list_commands(int argc, char **argv)
11250 char buffer[81] = "";
11252 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11258 static int lfs_pcc_attach(int argc, char **argv)
11260 struct option long_opts[] = {
11261 { .val = 'i', .name = "id", .has_arg = required_argument },
11262 { .name = NULL } };
11265 __u32 archive_id = 0;
11268 char fullpath[PATH_MAX];
11269 enum lu_pcc_type type = LU_PCC_READWRITE;
11272 while ((c = getopt_long(argc, argv, "i:",
11273 long_opts, NULL)) != -1) {
11276 archive_id = strtoul(optarg, &end, 0);
11277 if (*end != '\0' || archive_id == 0) {
11279 "error: %s: bad archive ID '%s'\n",
11287 fprintf(stderr, "%s: option '%s' unrecognized\n",
11288 argv[0], argv[optind - 1]);
11293 if (archive_id == 0) {
11294 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11298 if (argc <= optind) {
11299 fprintf(stderr, "%s: must specify one or more file names\n",
11304 while (optind < argc) {
11307 path = argv[optind++];
11308 if (!realpath(path, fullpath)) {
11309 fprintf(stderr, "%s: could not find path '%s': %s\n",
11310 argv[0], path, strerror(errno));
11316 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11319 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11320 argv[0], path, archive_id, strerror(-rc2));
11328 static int lfs_pcc_attach_fid(int argc, char **argv)
11330 struct option long_opts[] = {
11331 { .val = 'i', .name = "id", .has_arg = required_argument },
11332 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11333 { .name = NULL } };
11334 char short_opts[] = "i:m:";
11337 __u32 archive_id = 0;
11339 const char *mntpath = NULL;
11340 const char *fidstr;
11341 enum lu_pcc_type type = LU_PCC_READWRITE;
11344 while ((c = getopt_long(argc, argv, short_opts,
11345 long_opts, NULL)) != -1) {
11348 archive_id = strtoul(optarg, &end, 0);
11349 if (*end != '\0') {
11351 "error: %s: bad archive ID '%s'\n",
11362 fprintf(stderr, "%s: option '%s' unrecognized\n",
11363 argv[0], argv[optind - 1]);
11368 if (archive_id == 0) {
11369 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11374 fprintf(stderr, "%s: must specify Lustre mount point\n",
11379 if (argc <= optind) {
11380 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11384 while (optind < argc) {
11387 fidstr = argv[optind++];
11389 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11393 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11394 argv[0], fidstr, mntpath, archive_id,
11397 if (rc == 0 && rc2 < 0)
11403 static int lfs_pcc_detach(int argc, char **argv)
11405 struct option long_opts[] = {
11406 { .val = 'k', .name = "keep", .has_arg = no_argument },
11407 { .name = NULL } };
11408 char short_opts[] = "k";
11412 char fullpath[PATH_MAX];
11413 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11416 while ((c = getopt_long(argc, argv, short_opts,
11417 long_opts, NULL)) != -1) {
11420 detach_opt = PCC_DETACH_OPT_NONE;
11425 fprintf(stderr, "%s: option '%s' unrecognized\n",
11426 argv[0], argv[optind - 1]);
11431 while (optind < argc) {
11434 path = argv[optind++];
11435 if (!realpath(path, fullpath)) {
11436 fprintf(stderr, "%s: could not find path '%s': %s\n",
11437 argv[0], path, strerror(errno));
11443 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11447 "%s: cannot detach '%s' from PCC: %s\n",
11448 argv[0], path, strerror(errno));
11456 static int lfs_pcc_detach_fid(int argc, char **argv)
11458 struct option long_opts[] = {
11459 { .val = 'k', .name = "keep", .has_arg = no_argument },
11460 { .name = NULL } };
11461 char short_opts[] = "k";
11465 const char *mntpath;
11466 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11469 while ((c = getopt_long(argc, argv, short_opts,
11470 long_opts, NULL)) != -1) {
11473 detach_opt = PCC_DETACH_OPT_NONE;
11478 fprintf(stderr, "%s: option '%s' unrecognized\n",
11479 argv[0], argv[optind - 1]);
11484 mntpath = argv[optind++];
11486 while (optind < argc) {
11489 fid = argv[optind++];
11491 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11494 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11495 argv[0], fid, mntpath, strerror(-rc2));
11503 static int lfs_pcc_state(int argc, char **argv)
11507 char fullpath[PATH_MAX];
11508 struct lu_pcc_state state;
11513 fprintf(stderr, "%s: must specify one or more file names\n",
11518 while (optind < argc) {
11521 path = argv[optind++];
11522 if (!realpath(path, fullpath)) {
11523 fprintf(stderr, "%s: could not find path '%s': %s\n",
11524 argv[0], path, strerror(errno));
11530 rc2 = llapi_pcc_state_get(fullpath, &state);
11535 "%s: cannot get PCC state of '%s': %s\n",
11536 argv[0], path, strerror(-rc2));
11540 printf("file: %s", path);
11541 printf(", type: %s", pcc_type2string(state.pccs_type));
11542 if (state.pccs_type == LU_PCC_NONE &&
11543 state.pccs_open_count == 0) {
11548 printf(", PCC file: %s", state.pccs_path);
11549 printf(", user number: %u", state.pccs_open_count);
11550 printf(", flags: %x", state.pccs_flags);
11557 * lfs_pcc_list_commands() - List lfs pcc commands.
11558 * @argc: The count of command line arguments.
11559 * @argv: Array of strings for command line arguments.
11561 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11563 * Return: 0 on success.
11565 static int lfs_pcc_list_commands(int argc, char **argv)
11567 char buffer[81] = "";
11569 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11576 * lfs_pcc() - Parse and execute lfs pcc commands.
11577 * @argc: The count of lfs pcc command line arguments.
11578 * @argv: Array of strings for lfs pcc command line arguments.
11580 * This function parses lfs pcc commands and performs the
11581 * corresponding functions specified in pcc_cmdlist[].
11583 * Return: 0 on success or an error code on failure.
11585 static int lfs_pcc(int argc, char **argv)
11587 char cmd[PATH_MAX];
11590 setlinebuf(stdout);
11592 Parser_init("lfs-pcc > ", pcc_cmdlist);
11594 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11596 program_invocation_short_name = cmd;
11598 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11600 rc = Parser_commands();
11602 return rc < 0 ? -rc : rc;
11605 static int lfs_list_commands(int argc, char **argv)
11607 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11609 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11614 int main(int argc, char **argv)
11618 /* Ensure that liblustreapi constructor has run */
11619 if (!llapi_liblustreapi_initialized())
11620 fprintf(stderr, "liblustreapi was not properly initialized\n");
11622 setlinebuf(stdout);
11625 Parser_init("lfs > ", cmdlist);
11627 progname = program_invocation_short_name; /* Used in error messages */
11629 llapi_set_command_name(argv[1]);
11630 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11631 llapi_clear_command_name();
11633 rc = Parser_commands();
11636 return rc < 0 ? -rc : rc;
11639 #ifdef _LUSTRE_IDL_H_
11640 /* Everything we need here should be included by lustreapi.h. */
11641 # error "lfs should not depend on lustre_idl.h"
11642 #endif /* _LUSTRE_IDL_H_ */