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>]\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,
764 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
765 struct llapi_layout *layout);
768 migrate_open_files(const char *name, __u64 migration_flags,
769 const struct llapi_stripe_param *param,
770 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
777 char parent[PATH_MAX];
778 char volatile_file[PATH_MAX];
784 if (!param && !layout) {
785 error_loc = "layout information";
789 /* search for file directory pathname */
790 if (strlen(name) > sizeof(parent) - 1) {
791 error_loc = "source file name";
795 strncpy(parent, name, sizeof(parent));
796 ptr = strrchr(parent, '/');
798 if (!getcwd(parent, sizeof(parent))) {
799 error_loc = "getcwd";
803 if (ptr == parent) /* leading '/' */
808 /* open file, direct io */
809 /* even if the file is only read, WR mode is nedeed to allow
813 if (!(migration_flags & MIGRATION_NONDIRECT))
815 fd = open(name, rflags);
818 error_loc = "cannot open source file";
822 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
824 error_loc = "cannot get MDT index";
829 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
830 mode_t open_mode = S_IRUSR | S_IWUSR;
832 random_value = random();
833 rc = snprintf(volatile_file, sizeof(volatile_file),
834 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
835 mdt_index, random_value);
836 if (rc >= sizeof(volatile_file)) {
841 /* create, open a volatile file, use caching (ie no directio) */
843 fdv = lfs_component_create(volatile_file, open_flags,
846 fdv = llapi_file_open_param(volatile_file, open_flags,
848 } while (fdv < 0 && (rc = fdv) == -EEXIST);
851 error_loc = "cannot create volatile file";
856 * In case the MDT does not support creation of volatile files
857 * we should try to unlink it.
859 (void)unlink(volatile_file);
862 * Not-owner (root?) special case.
863 * Need to set owner/group of volatile file like original.
864 * This will allow to pass related check during layout_swap.
869 error_loc = "cannot stat source file";
873 rc = fstat(fdv, &stv);
876 error_loc = "cannot stat volatile";
880 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
881 rc = fchown(fdv, st.st_uid, st.st_gid);
884 error_loc = "cannot change ownwership of volatile";
903 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
905 struct llapi_layout *layout;
906 size_t buf_size = 4 * 1024 * 1024;
915 layout = llapi_layout_get_by_fd(fd_src, 0);
917 uint64_t stripe_size;
919 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
921 buf_size = stripe_size;
923 llapi_layout_free(layout);
926 /* Use a page-aligned buffer for direct I/O */
927 rc = posix_memalign(&buf, getpagesize(), buf_size);
933 * read new data only if we have written all
934 * previously read data
938 rc = check_file(fd_src);
943 rsize = read(fd_src, buf, buf_size);
955 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
974 static int migrate_copy_timestamps(int fd, int fdv)
978 if (fstat(fd, &st) == 0) {
979 struct timeval tv[2] = {
980 {.tv_sec = st.st_atime},
981 {.tv_sec = st.st_mtime}
984 return futimes(fdv, tv);
990 static int migrate_block(int fd, int fdv)
997 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
999 error_loc = "cannot get dataversion";
1008 * The grouplock blocks all concurrent accesses to the file.
1009 * It has to be taken after llapi_get_data_version as it would
1012 rc = llapi_group_lock(fd, gid);
1014 error_loc = "cannot get group lock";
1018 rc = migrate_copy_data(fd, fdv, NULL);
1020 error_loc = "data copy failed";
1024 /* Make sure we keep original atime/mtime values */
1025 rc = migrate_copy_timestamps(fd, fdv);
1027 error_loc = "timestamp copy failed";
1033 * for a migration we need to check data version on file did
1036 * Pass in gid=0 since we already own grouplock.
1038 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1039 SWAP_LAYOUTS_CHECK_DV1);
1040 if (rc == -EAGAIN) {
1041 error_loc = "file changed";
1043 } else if (rc < 0) {
1044 error_loc = "cannot swap layout";
1049 rc2 = llapi_group_unlock(fd, gid);
1050 if (rc2 < 0 && rc == 0) {
1051 error_loc = "unlock group lock";
1059 * Internal helper for migrate_copy_data(). Check lease and report error if
1062 * \param[in] fd File descriptor on which to check the lease.
1064 * \retval 0 Migration can keep on going.
1065 * \retval -errno Error occurred, abort migration.
1067 static int check_lease(int fd)
1071 rc = llapi_lease_check(fd);
1073 return 0; /* llapi_check_lease returns > 0 on success. */
1078 static int migrate_nonblock(int fd, int fdv)
1084 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1086 error_loc = "cannot get data version";
1090 rc = migrate_copy_data(fd, fdv, check_lease);
1092 error_loc = "data copy failed";
1096 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1098 error_loc = "cannot get data version";
1104 error_loc = "source file changed";
1108 /* Make sure we keep original atime/mtime values */
1109 rc = migrate_copy_timestamps(fd, fdv);
1111 error_loc = "timestamp copy failed";
1118 static int lfs_component_set(char *fname, int comp_id,
1119 __u32 flags, __u32 neg_flags)
1122 __u32 flags_array[2];
1127 ids[count] = comp_id;
1128 flags_array[count] = flags;
1133 if (neg_flags & LCME_FL_STALE) {
1135 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1140 ids[count] = comp_id;
1141 flags_array[count] = neg_flags | LCME_FL_NEG;
1145 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1147 if (errno == EUCLEAN) {
1150 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1151 progname, comp_id, fname);
1154 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1155 progname, comp_id, fname, flags, neg_flags);
1162 static int lfs_component_del(char *fname, __u32 comp_id,
1163 __u32 flags, __u32 neg_flags)
1167 if (flags && neg_flags)
1170 if (!flags && neg_flags)
1171 flags = neg_flags | LCME_FL_NEG;
1173 if ((flags && comp_id) || (!flags && !comp_id))
1177 if (flags & ~LCME_KNOWN_FLAGS) {
1179 "%s setstripe: unknown flags %#x\n",
1183 } else if (comp_id > LCME_ID_MAX) {
1184 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1189 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1192 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1193 progname, comp_id, fname, strerror(errno));
1197 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1204 rc = llapi_layout_file_comp_add(fname, layout);
1206 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1207 fname, strerror(errno));
1211 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1212 struct llapi_layout *layout)
1220 fd = lstat(fname, &st);
1221 if (fd == 0 && S_ISDIR(st.st_mode))
1222 open_flags = O_DIRECTORY | O_RDONLY;
1224 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1226 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1227 S_ISDIR(st.st_mode) ?
1228 "set default composite layout for" :
1229 "create composite file",
1230 fname, strerror(errno));
1234 static int lfs_migrate(char *name, __u64 migration_flags,
1235 struct llapi_stripe_param *param,
1236 struct llapi_layout *layout)
1238 struct llapi_layout *existing;
1239 uint64_t dom_new, dom_cur;
1244 rc = migrate_open_files(name, migration_flags, param, layout,
1249 rc = llapi_layout_dom_size(layout, &dom_new);
1251 error_loc = "cannot get new layout DoM size";
1254 /* special case for migration to DOM layout*/
1255 existing = llapi_layout_get_by_fd(fd, 0);
1257 error_loc = "cannot get existing layout";
1261 rc = llapi_layout_dom_size(existing, &dom_cur);
1263 error_loc = "cannot get current layout DoM size";
1268 * if file has DoM layout already then migration is possible to
1269 * the new layout with the same DoM component via swap layout,
1270 * if new layout used bigger DOM size, then mirroring is used
1272 if (dom_new > dom_cur) {
1273 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1276 error_loc = "cannot migrate to DOM layout";
1280 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1282 * Blocking mode (forced if servers do not support file lease).
1283 * It is also the default mode, since we cannot distinguish
1284 * between a broken lease and a server that does not support
1285 * atomic swap/close (LU-6785)
1287 rc = migrate_block(fd, fdv);
1291 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1293 error_loc = "cannot get lease";
1297 rc = migrate_nonblock(fd, fdv);
1299 llapi_lease_release(fd);
1304 * Atomically put lease, swap layouts and close.
1305 * for a migration we need to check data version on file did
1308 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1310 error_loc = "cannot swap layout";
1322 fprintf(stderr, "error: %s: %s: %s: %s\n",
1323 progname, name, error_loc, strerror(-rc));
1324 else if (migration_flags & MIGRATION_VERBOSE)
1325 printf("%s\n", name);
1330 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1339 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1343 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1344 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1345 const char *comp_name = comp_flags_table[i].cfn_name;
1347 if (strcmp(name, comp_name) == 0) {
1348 *flags |= comp_flag;
1350 } else if (strncmp(name, "^", 1) == 0 &&
1351 strcmp(name + 1, comp_name) == 0) {
1352 *neg_flags |= comp_flag;
1357 llapi_printf(LLAPI_MSG_ERROR,
1358 "%s: component flag '%s' not supported\n",
1364 if (!*flags && !*neg_flags)
1367 /* don't allow to set and exclude the same flag */
1368 if (*flags & *neg_flags)
1374 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1382 if (strncmp(string, "^", 1) == 0) {
1383 *neg_state = llapi_layout_string_flags(string + 1);
1384 if (*neg_state != 0)
1387 *state = llapi_layout_string_flags(string);
1392 llapi_printf(LLAPI_MSG_ERROR,
1393 "%s: mirrored file state '%s' not supported\n",
1399 * struct mirror_args - Command-line arguments for mirror(s).
1400 * @m_count: Number of mirrors to be created with this layout.
1401 * @m_flags: Mirror level flags, only 'prefer' is supported.
1402 * @m_layout: Mirror layout.
1403 * @m_file: A victim file. Its layout will be split and used as a mirror.
1404 * @m_next: Point to the next node of the list.
1406 * Command-line arguments for mirror(s) will be parsed and stored in
1407 * a linked list that consists of this structure.
1409 struct mirror_args {
1412 struct llapi_layout *m_layout;
1414 struct mirror_args *m_next;
1419 * enum mirror_flags - Flags for extending a mirrored file.
1420 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1421 * in case the victim file(s) contains the same data as the
1422 * original mirrored file.
1423 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1424 * @MF_COMP_ID: specified component id instead of mirror id
1426 * Flags for extending a mirrored file.
1436 * mirror_create_sanity_check() - Check mirror list.
1437 * @list: A linked list that stores the mirror arguments.
1439 * This function does a sanity check on @list for creating
1442 * Return: 0 on success or a negative error code on failure.
1444 static int mirror_create_sanity_check(const char *fname,
1445 struct mirror_args *list)
1448 bool has_m_file = false;
1449 bool has_m_layout = false;
1455 struct llapi_layout *layout;
1457 layout = llapi_layout_get_by_path(fname, 0);
1460 "error: %s: file '%s' couldn't get layout\n",
1465 rc = llapi_layout_sanity(layout, false, true);
1467 llapi_layout_free(layout);
1470 llapi_layout_sanity_perror(rc);
1478 llapi_layout_free(list->m_layout);
1481 llapi_layout_get_by_path(list->m_file, 0);
1482 if (!list->m_layout) {
1484 "error: %s: file '%s' has no layout\n",
1485 progname, list->m_file);
1489 has_m_layout = true;
1490 if (!list->m_layout) {
1491 fprintf(stderr, "error: %s: no mirror layout\n",
1497 rc = llapi_layout_sanity(list->m_layout, false, true);
1499 llapi_layout_sanity_perror(rc);
1503 list = list->m_next;
1506 if (has_m_file && has_m_layout) {
1508 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1516 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1518 __u32 mirror_flags = *(__u32 *)cbdata;
1522 rc = llapi_layout_comp_flags_get(layout, &flags);
1527 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1532 return LLAPI_LAYOUT_ITER_CONT;
1536 * mirror_create() - Create a mirrored file.
1537 * @fname: The file to be created.
1538 * @mirror_list: A linked list that stores the mirror arguments.
1540 * This function creates a mirrored file @fname with the mirror(s)
1541 * from @mirror_list.
1543 * Return: 0 on success or a negative error code on failure.
1545 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1547 struct llapi_layout *layout = NULL;
1548 struct mirror_args *cur_mirror = NULL;
1549 uint16_t mirror_count = 0;
1553 rc = mirror_create_sanity_check(NULL, mirror_list);
1557 cur_mirror = mirror_list;
1558 while (cur_mirror) {
1559 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1561 &cur_mirror->m_flags);
1564 fprintf(stderr, "%s: failed to set mirror flags\n",
1569 for (i = 0; i < cur_mirror->m_count; i++) {
1570 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1574 "error: %s: merge layout failed: %s\n",
1575 progname, strerror(errno));
1579 mirror_count += cur_mirror->m_count;
1580 cur_mirror = cur_mirror->m_next;
1584 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1588 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1591 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1592 progname, strerror(errno));
1596 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1604 llapi_layout_free(layout);
1609 * Compare files and check lease on @fd.
1611 * \retval bytes number of bytes are the same
1613 static ssize_t mirror_file_compare(int fd, int fdv)
1615 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1617 ssize_t bytes_done = 0;
1618 ssize_t bytes_read = 0;
1620 buf = malloc(buflen * 2);
1625 if (!llapi_lease_check(fd)) {
1626 bytes_done = -EBUSY;
1630 bytes_read = read(fd, buf, buflen);
1631 if (bytes_read <= 0)
1634 if (bytes_read != read(fdv, buf + buflen, buflen))
1638 * XXX: should compute the checksum on each buffer and then
1639 * compare checksum to avoid cache collision
1641 if (memcmp(buf, buf + buflen, bytes_read))
1644 bytes_done += bytes_read;
1652 static int mirror_extend_file(const char *fname, const char *victim_file,
1653 enum mirror_flags mirror_flags)
1658 struct stat stbuf_v;
1659 struct ll_ioc_lease *data = NULL;
1662 fd = open(fname, O_RDWR);
1664 error_loc = "open source file";
1669 fdv = open(victim_file, O_RDWR);
1671 error_loc = "open target file";
1676 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1677 error_loc = "stat source or target file";
1682 if (stbuf.st_dev != stbuf_v.st_dev) {
1683 error_loc = "stat source and target file";
1688 /* mirrors should be of the same size */
1689 if (stbuf.st_size != stbuf_v.st_size) {
1690 error_loc = "file sizes don't match";
1695 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1697 error_loc = "cannot get lease";
1701 if (!(mirror_flags & MF_NO_VERIFY)) {
1703 /* mirrors should have the same contents */
1704 ret = mirror_file_compare(fd, fdv);
1705 if (ret != stbuf.st_size) {
1706 error_loc = "file busy or contents don't match";
1707 rc = ret < 0 ? ret : -EINVAL;
1712 /* Get rid of caching pages from clients */
1713 rc = llapi_file_flush(fd);
1715 error_loc = "cannot get data version";
1719 rc = llapi_file_flush(fdv);
1721 error_loc = "cannot get data version";
1725 /* Make sure we keep original atime/mtime values */
1726 rc = migrate_copy_timestamps(fd, fdv);
1728 error_loc = "cannot copy timestamp";
1732 /* Atomically put lease, merge layouts and close. */
1733 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1735 error_loc = "memory allocation";
1738 data->lil_mode = LL_LEASE_UNLCK;
1739 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1740 data->lil_count = 1;
1741 data->lil_ids[0] = fdv;
1742 rc = llapi_lease_set(fd, data);
1744 error_loc = "cannot merge layout";
1746 } else if (rc == 0) {
1748 error_loc = "lost lease lock";
1761 (void) unlink(victim_file);
1763 fprintf(stderr, "error: %s: %s: %s: %s\n",
1764 progname, fname, error_loc, strerror(-rc));
1768 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1771 struct llapi_layout *f_layout = NULL;
1772 struct ll_ioc_lease *data = NULL;
1778 f_layout = llapi_layout_get_by_path(name, 0);
1780 fprintf(stderr, "%s: cannot get layout\n", progname);
1783 rc = llapi_layout_get_last_init_comp(f_layout);
1785 fprintf(stderr, "%s: cannot get the last init comp\n",
1789 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1792 "%s: cannot inherit from the last init comp\n",
1797 rc = migrate_open_files(name, 0, NULL, m_layout, &fd, &fdv);
1801 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1803 error_loc = "cannot get lease";
1807 rc = migrate_nonblock(fd, fdv);
1809 llapi_lease_release(fd);
1813 /* Atomically put lease, merge layouts and close. */
1814 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1816 error_loc = "memory allocation";
1819 data->lil_mode = LL_LEASE_UNLCK;
1820 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1821 data->lil_count = 1;
1822 data->lil_ids[0] = fdv;
1823 rc = llapi_lease_set(fd, data);
1825 error_loc = "cannot merge layout";
1827 } else if (rc == 0) {
1829 error_loc = "lost lease lock";
1842 fprintf(stderr, "error: %s: %s: %s: %s\n",
1843 progname, name, error_loc, strerror(-rc));
1847 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1848 enum mirror_flags mirror_flags)
1852 rc = mirror_create_sanity_check(fname, mirror_list);
1856 while (mirror_list) {
1857 if (mirror_list->m_file) {
1858 rc = mirror_extend_file(fname, mirror_list->m_file,
1861 __u32 mirror_count = mirror_list->m_count;
1863 while (mirror_count > 0) {
1864 rc = mirror_extend_layout(fname,
1865 mirror_list->m_layout,
1866 mirror_list->m_inherit);
1876 mirror_list = mirror_list->m_next;
1882 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1887 rc = llapi_layout_mirror_id_get(layout, &id);
1891 if ((__u16)id == *(__u16 *)cbdata)
1892 return LLAPI_LAYOUT_ITER_STOP;
1894 return LLAPI_LAYOUT_ITER_CONT;
1897 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1902 rc = llapi_layout_comp_id_get(layout, &id);
1906 if (id == *(__u32 *)cbdata)
1907 return LLAPI_LAYOUT_ITER_STOP;
1909 return LLAPI_LAYOUT_ITER_CONT;
1912 struct pool_to_id_cbdata {
1917 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1919 char buf[LOV_MAXPOOLNAME + 1];
1920 struct pool_to_id_cbdata *d = (void *)cbdata;
1924 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1927 if (strcmp(d->pool, buf))
1928 return LLAPI_LAYOUT_ITER_CONT;
1930 rc = llapi_layout_mirror_id_get(layout, &id);
1935 return LLAPI_LAYOUT_ITER_STOP;
1938 struct collect_ids_data {
1944 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1946 struct collect_ids_data *cid = cbdata;
1950 rc = llapi_layout_mirror_id_get(layout, &id);
1954 if ((__u16)id != cid->cid_exclude) {
1957 for (i = 0; i < cid->cid_count; i++) {
1958 /* already collected the mirror id */
1959 if (id == cid->cid_ids[i])
1960 return LLAPI_LAYOUT_ITER_CONT;
1962 cid->cid_ids[cid->cid_count] = id;
1966 return LLAPI_LAYOUT_ITER_CONT;
1970 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1971 * @mirror_id: Mirror id to be checked.
1972 * @layout: Mirror component list.
1974 * This function checks if a mirror with specified @mirror_id is the last
1975 * non-stale mirror of a layout @layout.
1977 * Return: true or false.
1980 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1982 __u16 mirror_ids[128] = { 0 };
1983 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1985 .cid_exclude = mirror_id, };
1988 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1990 for (i = 0; i < cid.cid_count; i++) {
1991 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1994 comp_size = llapi_mirror_find_stale(layout, comp_array,
1995 ARRAY_SIZE(comp_array),
2004 static int mirror_split(const char *fname, __u32 id, const char *pool,
2005 enum mirror_flags mflags, const char *victim_file)
2007 struct llapi_layout *layout;
2008 char parent[PATH_MAX];
2009 char victim[PATH_MAX];
2010 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2012 struct ll_ioc_lease *data;
2013 uint16_t mirror_count;
2019 /* check fname contains mirror with mirror_id/comp_id */
2020 layout = llapi_layout_get_by_path(fname, 0);
2023 "error %s: file '%s' couldn't get layout\n",
2028 rc = llapi_layout_sanity(layout, false, true);
2030 llapi_layout_sanity_perror(rc);
2034 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2037 "error %s: file '%s' couldn't get mirror count\n",
2041 if (mirror_count < 2) {
2043 "error %s: file '%s' has %d component, cannot split\n",
2044 progname, fname, mirror_count);
2048 if (mflags & MF_COMP_POOL) {
2049 struct pool_to_id_cbdata data = { .pool = pool };
2051 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
2053 mirror_id = data.id;
2054 } else if (mflags & MF_COMP_ID) {
2055 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2056 mirror_id = mirror_id_of(id);
2058 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2062 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2065 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2066 if (mflags & MF_COMP_POOL) {
2068 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2069 progname, fname, pool);
2071 } else if (mflags & MF_COMP_ID) {
2073 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2074 progname, fname, id);
2078 "error %s: file '%s' does not contain mirror with id %u\n",
2079 progname, fname, id);
2084 fd = open(fname, O_RDWR);
2087 "error %s: open file '%s' failed: %s\n",
2088 progname, fname, strerror(errno));
2092 /* get victim file directory pathname */
2093 if (strlen(fname) > sizeof(parent) - 1) {
2094 fprintf(stderr, "error %s: file name of '%s' too long\n",
2099 strncpy(parent, fname, sizeof(parent));
2100 ptr = strrchr(parent, '/');
2102 if (!getcwd(parent, sizeof(parent))) {
2103 fprintf(stderr, "error %s: getcwd failed: %s\n",
2104 progname, strerror(errno));
2114 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2116 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2122 /* use a temp file to store the splitted layout */
2123 if (mflags & MF_DESTROY) {
2124 if (last_non_stale_mirror(mirror_id, layout)) {
2127 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2132 fdv = llapi_create_volatile_idx(parent, mdt_index,
2133 O_LOV_DELAY_CREATE);
2135 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2137 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2140 /* user specified victim file */
2141 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2146 "error %s: create victim file failed: %s\n",
2147 progname, strerror(errno));
2151 /* get lease lock of fname */
2152 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2155 "error %s: cannot get lease of file '%s': %d\n",
2156 progname, fname, rc);
2160 /* Atomatically put lease, split layouts and close. */
2161 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2167 data->lil_mode = LL_LEASE_UNLCK;
2168 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2169 data->lil_count = 2;
2170 data->lil_ids[0] = fdv;
2171 data->lil_ids[1] = mirror_id;
2172 rc = llapi_lease_set(fd, data);
2174 if (rc == 0) /* lost lease lock */
2177 "error %s: cannot split '%s': %s\n",
2178 progname, fname, strerror(-rc));
2189 llapi_layout_free(layout);
2194 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2195 __u16 *mirror_ids, int ids_nr);
2197 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2198 __u64 migration_flags,
2199 struct llapi_stripe_param *param,
2200 struct llapi_layout *layout)
2202 struct ll_ioc_lease *data = NULL;
2205 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2207 error_loc = "cannot get lease";
2211 /* Atomically put lease, merge layouts, resync and close. */
2212 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2214 error_loc = "memory allocation";
2217 data->lil_mode = LL_LEASE_UNLCK;
2218 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2219 data->lil_count = 1;
2220 data->lil_ids[0] = fdv;
2221 rc = llapi_lease_set(fd, data);
2223 error_loc = "cannot merge layout";
2225 } else if (rc == 0) {
2227 error_loc = "lost lease lock";
2233 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2235 error_loc = "cannot resync file";
2239 /* delete first mirror now */
2240 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2242 error_loc = "cannot delete old layout";
2250 fprintf(stderr, "error: %s: %s: %s: %s\n",
2251 progname, name, error_loc, strerror(-rc));
2252 else if (migration_flags & MIGRATION_VERBOSE)
2253 printf("%s\n", name);
2260 * Parse a string containing an target index list into an array of integers.
2262 * The input string contains a comma delimited list of individual
2263 * indices and ranges, for example "1,2-4,7". Add the indices into the
2264 * \a tgts array and remove duplicates.
2266 * \param[out] tgts array to store indices in
2267 * \param[in] size size of \a tgts array
2268 * \param[in] offset starting index in \a tgts
2269 * \param[in] arg string containing OST index list
2270 * \param[in/out] overstriping index list may contain duplicates
2272 * \retval positive number of indices in \a tgts
2273 * \retval -EINVAL unable to parse \a arg
2275 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2276 unsigned long long *pattern)
2280 int slots = size - offset;
2282 bool overstriped = false;
2288 end_of_loop = false;
2289 while (!end_of_loop) {
2290 int start_index = 0;
2293 char *endptr = NULL;
2297 ptr = strchrnul(arg, ',');
2299 end_of_loop = *ptr == '\0';
2302 start_index = strtol(arg, &endptr, 0);
2303 if (endptr == arg) /* no data at all */
2305 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2308 end_index = start_index;
2309 if (*endptr == '-') {
2310 end_index = strtol(endptr + 1, &endptr, 0);
2311 if (*endptr != '\0')
2313 if (end_index < start_index)
2317 for (i = start_index; i <= end_index && slots > 0; i++) {
2320 /* remove duplicate */
2321 for (j = 0; j < offset; j++) {
2322 if (tgts[j] == i && pattern &&
2323 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2325 else if (tgts[j] == i)
2331 if (j == offset) { /* check complete */
2337 if (slots == 0 && i < end_index)
2345 if (!end_of_loop && ptr)
2348 if (!overstriped && pattern)
2349 *pattern = LLAPI_LAYOUT_DEFAULT;
2351 return rc < 0 ? rc : nr;
2354 struct lfs_setstripe_args {
2355 unsigned long long lsa_comp_end;
2356 unsigned long long lsa_stripe_size;
2357 unsigned long long lsa_extension_size;
2358 long long lsa_stripe_count;
2359 long long lsa_stripe_off;
2360 __u32 lsa_comp_flags;
2361 __u32 lsa_comp_neg_flags;
2362 unsigned long long lsa_pattern;
2363 unsigned int lsa_mirror_count;
2365 bool lsa_first_comp;
2366 bool lsa_extension_comp;
2368 char *lsa_pool_name;
2371 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2373 unsigned int mirror_count = lsa->lsa_mirror_count;
2374 bool first_comp = lsa->lsa_first_comp;
2376 memset(lsa, 0, sizeof(*lsa));
2378 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2379 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2380 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2381 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2382 lsa->lsa_pool_name = NULL;
2384 lsa->lsa_mirror_count = mirror_count;
2385 lsa->lsa_first_comp = first_comp;
2389 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2390 * @lsa: Stripe options to be initialized and inherited.
2392 * This function initializes stripe options in @lsa and inherit
2393 * stripe_size, stripe_count and OST pool_name options.
2397 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2399 unsigned long long stripe_size;
2400 long long stripe_count;
2401 char *pool_name = NULL;
2403 stripe_size = lsa->lsa_stripe_size;
2404 stripe_count = lsa->lsa_stripe_count;
2405 pool_name = lsa->lsa_pool_name;
2407 setstripe_args_init(lsa);
2409 lsa->lsa_stripe_size = stripe_size;
2410 lsa->lsa_stripe_count = stripe_count;
2411 lsa->lsa_pool_name = pool_name;
2414 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2416 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2417 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2418 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2419 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2420 lsa->lsa_comp_end != 0);
2424 * comp_args_to_layout() - Create or extend a composite layout.
2425 * @composite: Pointer to the composite layout.
2426 * @lsa: Stripe options for the new component.
2428 * This function creates or extends a composite layout by adding a new
2429 * component with stripe options from @lsa.
2431 * Return: 0 on success or an error code on failure.
2433 static int comp_args_to_layout(struct llapi_layout **composite,
2434 struct lfs_setstripe_args *lsa,
2437 struct llapi_layout *layout = *composite;
2438 uint64_t prev_end = 0;
2444 layout = llapi_layout_alloc();
2446 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2451 *composite = layout;
2452 lsa->lsa_first_comp = true;
2457 * Get current component extent, current component
2458 * must be the tail component.
2460 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2462 fprintf(stderr, "Get comp extent failed. %s\n",
2467 if (lsa->lsa_first_comp) {
2469 rc = llapi_layout_add_first_comp(layout);
2471 rc = llapi_layout_comp_add(layout);
2474 fprintf(stderr, "Add component failed. %s\n",
2480 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2482 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2483 lsa->lsa_comp_flags, strerror(errno));
2488 uint64_t comp_end = lsa->lsa_comp_end;
2491 * The extendable component is 0-length, so it can be removed
2492 * if there is insufficient space to extend it.
2494 if (lsa->lsa_extension_comp)
2495 comp_end = prev_end;
2497 rc = llapi_layout_comp_extent_set(layout, prev_end,
2500 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2501 prev_end, comp_end, strerror(errno));
2505 /* reset lsa_first_comp */
2506 lsa->lsa_first_comp = false;
2508 /* Data-on-MDT component setting */
2509 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2511 * In case of Data-on-MDT patterns the only extra option
2512 * applicable is stripe size option.
2514 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2516 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2517 lsa->lsa_stripe_count);
2521 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2523 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2524 lsa->lsa_stripe_size);
2528 if (lsa->lsa_nr_tgts != 0) {
2530 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2535 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2537 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2538 lsa->lsa_stripe_off);
2542 if (lsa->lsa_pool_name != 0) {
2544 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2545 lsa->lsa_pool_name);
2550 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2552 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2557 /* Data-on-MDT component has always single stripe up to end */
2558 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2559 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2560 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2562 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2569 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2570 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2572 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2573 rc = llapi_layout_extension_size_set(layout, size);
2575 rc = llapi_layout_stripe_size_set(layout, size);
2578 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2579 size, strerror(errno));
2583 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2585 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2586 lsa->lsa_stripe_count, strerror(errno));
2590 if (lsa->lsa_pool_name) {
2591 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2593 fprintf(stderr, "Set pool name: %s failed. %s\n",
2594 lsa->lsa_pool_name, strerror(errno));
2598 rc = llapi_layout_pool_name_set(layout, "");
2600 fprintf(stderr, "Clear pool name failed: %s\n",
2606 if (lsa->lsa_nr_tgts > 0) {
2607 if (lsa->lsa_stripe_count > 0 &&
2608 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2609 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2610 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2611 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2612 lsa->lsa_stripe_count,
2617 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2618 rc = llapi_layout_ost_index_set(layout, i,
2623 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2624 lsa->lsa_stripe_off != -1) {
2625 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2628 fprintf(stderr, "Set ost index %d failed. %s\n",
2629 i, strerror(errno));
2633 /* Create the second, virtual component of extension space */
2634 if (lsa->lsa_extension_comp) {
2635 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2636 lsa->lsa_extension_comp = false;
2643 static int build_component(struct llapi_layout **layout,
2644 struct lfs_setstripe_args *lsa, bool set_extent)
2648 rc = comp_args_to_layout(layout, lsa, set_extent);
2652 if (lsa->lsa_mirror_count > 0) {
2653 rc = llapi_layout_mirror_count_set(*layout,
2654 lsa->lsa_mirror_count);
2658 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2661 lsa->lsa_mirror_count = 0;
2667 static int build_prev_component(struct llapi_layout **layout,
2668 struct lfs_setstripe_args *prev,
2669 struct lfs_setstripe_args *lsa,
2672 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2675 if (prev->lsa_stripe_size) {
2677 prev->lsa_comp_end = lsa->lsa_comp_end;
2678 prev->lsa_extension_size = lsa->lsa_extension_size;
2679 prev->lsa_extension_comp = true;
2682 rc = build_component(layout, prev, true);
2688 * Copy lsa to previous lsa;
2689 * if this is an extension component, make the previous invalid;
2692 prev->lsa_stripe_size = 0;
2699 static int build_layout_from_yaml_node(struct cYAML *node,
2700 struct llapi_layout **layout,
2701 struct lfs_setstripe_args *lsa,
2702 struct lfs_setstripe_args *prevp)
2704 struct lfs_setstripe_args prev = { 0 };
2705 __u32 *osts = lsa->lsa_tgts;
2713 string = node->cy_string;
2715 if (node->cy_type == CYAML_TYPE_OBJECT) {
2716 /* go deep to sub blocks */
2717 if (string && !strncmp(string, "component", 9) &&
2718 strncmp(string, "component0", 10) &&
2719 strncmp(string, "components", 10)) {
2720 rc = build_prev_component(layout, prevp, lsa,
2725 /* initialize lsa. */
2726 setstripe_args_init(lsa);
2727 lsa->lsa_first_comp = false;
2728 lsa->lsa_tgts = osts;
2731 rc = build_layout_from_yaml_node(node->cy_child, layout,
2736 if (!node->cy_string)
2739 /* skip leading lmm_ if present, to simplify parsing */
2740 if (strncmp(string, "lmm_", 4) == 0)
2743 if (node->cy_type == CYAML_TYPE_STRING) {
2744 if (!strcmp(string, "lcme_extent.e_end")) {
2745 if (!strcmp(node->cy_valuestring, "EOF") ||
2746 !strcmp(node->cy_valuestring, "eof"))
2747 lsa->lsa_comp_end = LUSTRE_EOF;
2748 } else if (!strcmp(string, "pool")) {
2749 lsa->lsa_pool_name = node->cy_valuestring;
2750 } else if (!strcmp(string, "pattern")) {
2751 if (!strcmp(node->cy_valuestring, "mdt"))
2752 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2753 if (!strcmp(node->cy_valuestring,
2754 "raid0,overstriped"))
2756 LLAPI_LAYOUT_OVERSTRIPING;
2757 } else if (!strcmp(string, "lcme_flags")) {
2758 rc = comp_str2flags(node->cy_valuestring,
2759 &lsa->lsa_comp_flags,
2760 &lsa->lsa_comp_neg_flags);
2764 * Only template flags have meaning in
2765 * the layout for a new file
2767 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2769 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2770 if (!strcmp(string, "lcm_mirror_count")) {
2771 lsa->lsa_mirror_count = node->cy_valueint;
2772 } else if (!strcmp(string, "lcme_extent.e_start")) {
2773 if (node->cy_valueint == 0)
2774 lsa->lsa_first_comp = true;
2775 } else if (!strcmp(string, "lcme_extent.e_end")) {
2776 if (node->cy_valueint == -1)
2777 lsa->lsa_comp_end = LUSTRE_EOF;
2779 lsa->lsa_comp_end = node->cy_valueint;
2780 } else if (!strcmp(string, "stripe_count")) {
2781 lsa->lsa_stripe_count = node->cy_valueint;
2782 } else if (!strcmp(string, "stripe_size")) {
2783 lsa->lsa_stripe_size = node->cy_valueint;
2784 } else if (!strcmp(string, "extension_size")) {
2785 lsa->lsa_extension_size = node->cy_valueint;
2786 lsa->lsa_extension_comp = true;
2787 } else if (!strcmp(string, "stripe_offset")) {
2788 lsa->lsa_stripe_off = node->cy_valueint;
2789 } else if (!strcmp(string, "l_ost_idx")) {
2790 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2795 node = node->cy_next;
2798 if (prevp == &prev) {
2799 rc = build_prev_component(layout, prevp, lsa, true);
2803 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2804 rc = build_component(layout, lsa, *layout != NULL);
2810 static int lfs_comp_create_from_yaml(char *template,
2811 struct llapi_layout **layout,
2812 struct lfs_setstripe_args *lsa,
2815 struct cYAML *tree = NULL, *err_rc = NULL;
2818 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2820 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2821 progname, template);
2822 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2823 "can't parse", &err_rc);
2824 cYAML_print_tree2file(stderr, err_rc);
2825 cYAML_free_tree(err_rc);
2830 /* initialize lsa for plain file */
2831 setstripe_args_init(lsa);
2832 lsa->lsa_tgts = osts;
2834 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2836 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2837 progname, template);
2840 /* clean clean lsa */
2841 setstripe_args_init(lsa);
2845 cYAML_free_tree(tree);
2850 * Get the extension size from the next (SEL) component and extend the
2851 * current component on it. The start of the next component is to be
2854 * \param[in] layout the current layout
2855 * \param[in] start the start of the current component
2856 * \param[in,out] end the end of the current component
2857 * \param[in] offset the offset to adjust the end position to instead of
2860 * \retval 0 - extended successfully
2861 * \retval < 0 - error
2863 static int layout_extend_comp(struct llapi_layout *layout,
2864 uint64_t start, uint64_t *end,
2867 uint64_t size, next_start, next_end;
2870 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2873 "%s setstripe: cannot move component cursor: %s\n",
2874 progname, strerror(errno));
2879 * Even if the @size will not be used below, this will fail if
2880 * this is not a SEL component - a good confirmation we are
2881 * working on right components.
2883 rc = llapi_layout_extension_size_get(layout, &size);
2886 "%s setstripe: cannot get component ext size: %s\n",
2887 progname, strerror(errno));
2891 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2893 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2894 progname, strerror(errno));
2898 next_start += offset ?: size;
2899 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2901 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2902 progname, strerror(errno));
2906 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2909 "%s setstripe: cannot move component cursor: %s\n",
2910 progname, strerror(errno));
2914 *end += offset ?: size;
2915 rc = llapi_layout_comp_extent_set(layout, start, *end);
2917 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2918 progname, strerror(errno));
2926 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2927 * end of the last component in the existing file, and adjust the
2928 * first extent start of the components to be added accordingly.
2930 * In the create mode, we need to check if the first component is an extendable
2931 * SEL component and extend its length to the extension size (first component
2932 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2934 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2937 struct llapi_layout *head;
2938 uint64_t start = 0, prev_end = 0;
2942 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
2947 head = llapi_layout_get_by_path(fname, 0);
2950 "%s setstripe: cannot read layout from '%s': %s\n",
2951 progname, fname, strerror(errno));
2953 } else if (errno == ENODATA) {
2955 * file without LOVEA, this component-add will be turned
2956 * into a component-create.
2958 llapi_layout_free(head);
2962 * the new layout will be added to an empty one, it
2963 * still needs to be adjusted below
2967 } else if (!llapi_layout_is_composite(head)) {
2969 "%s setstripe: '%s' not a composite file\n",
2971 llapi_layout_free(head);
2975 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2978 "%s setstripe: cannot get prev extent: %s\n",
2979 progname, strerror(errno));
2980 llapi_layout_free(head);
2984 llapi_layout_free(head);
2988 /* Make sure we use the first component of the layout to be added. */
2989 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2992 "%s setstripe: cannot move component cursor: %s\n",
2993 progname, strerror(errno));
2997 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2999 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3000 progname, strerror(errno));
3004 if (start == 0 && end == 0) {
3005 rc = layout_extend_comp(layout, start, &end,
3006 comp_add ? prev_end : 0);
3011 if (start > prev_end || end < prev_end) {
3013 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3014 progname, start, end, prev_end);
3018 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3021 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3022 progname, prev_end, end, strerror(errno));
3029 static int mirror_adjust_first_extents(struct mirror_args *list)
3037 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3040 list = list->m_next;
3046 static inline bool arg_is_eof(char *arg)
3048 return !strncmp(arg, "-1", strlen("-1")) ||
3049 !strncmp(arg, "EOF", strlen("EOF")) ||
3050 !strncmp(arg, "eof", strlen("eof"));
3054 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3056 * Return: Valid mirror_args pointer on success and
3057 * NULL if memory allocation fails.
3059 static struct mirror_args *lfs_mirror_alloc(void)
3061 struct mirror_args *mirror = NULL;
3064 mirror = calloc(1, sizeof(*mirror));
3066 mirror->m_inherit = false;
3077 * lfs_mirror_free() - Free memory allocated for a mirror argument
3079 * @mirror: Previously allocated mirror argument structure by
3080 * lfs_mirror_alloc().
3082 * Free memory allocated for @mirror.
3086 static void lfs_mirror_free(struct mirror_args *mirror)
3088 if (mirror->m_layout)
3089 llapi_layout_free(mirror->m_layout);
3094 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3095 * @mirror_list: Previously allocated mirror list.
3097 * Free memory allocated for @mirror_list.
3101 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3103 struct mirror_args *next_mirror = NULL;
3105 while (mirror_list) {
3106 next_mirror = mirror_list->m_next;
3107 lfs_mirror_free(mirror_list);
3108 mirror_list = next_mirror;
3120 LFS_COMP_NO_VERIFY_OPT,
3122 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3124 LFS_MIRROR_STATE_OPT,
3126 LFS_MIRROR_INDEX_OPT,
3127 LFS_LAYOUT_FOREIGN_OPT,
3133 static int lfs_setstripe_internal(int argc, char **argv,
3134 enum setstripe_origin opc)
3136 struct lfs_setstripe_args lsa = { 0 };
3137 struct llapi_stripe_param *param = NULL;
3138 struct find_param migrate_mdt_param = {
3148 unsigned long long size_units = 1;
3149 bool migrate_mode = false;
3150 bool migrate_mdt_mode = false;
3151 bool setstripe_mode = false;
3152 bool migration_block = false;
3153 __u64 migration_flags = 0;
3154 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3155 int comp_del = 0, comp_set = 0;
3158 struct llapi_layout *layout = NULL;
3159 struct llapi_layout **lpp = &layout;
3160 bool mirror_mode = false;
3161 bool has_m_file = false;
3162 __u32 mirror_count = 0;
3163 enum mirror_flags mirror_flags = 0;
3164 struct mirror_args *mirror_list = NULL;
3165 struct mirror_args *new_mirror = NULL;
3166 struct mirror_args *last_mirror = NULL;
3167 __u16 mirror_id = 0;
3169 bool from_yaml = false;
3170 bool from_copy = false;
3171 char *template = NULL;
3172 bool foreign_mode = false;
3174 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3175 char *mode_opt = NULL;
3176 mode_t previous_umask = 0;
3177 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3179 struct option long_opts[] = {
3180 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3181 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3182 /* --block is only valid in migrate mode */
3183 { .val = 'b', .name = "block", .has_arg = no_argument },
3184 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3185 { .val = LFS_COMP_ADD_OPT,
3186 .name = "comp-add", .has_arg = no_argument },
3187 { .val = LFS_COMP_ADD_OPT,
3188 .name = "component-add", .has_arg = no_argument },
3189 { .val = LFS_COMP_DEL_OPT,
3190 .name = "comp-del", .has_arg = no_argument },
3191 { .val = LFS_COMP_DEL_OPT,
3192 .name = "component-del", .has_arg = no_argument },
3193 { .val = LFS_COMP_FLAGS_OPT,
3194 .name = "comp-flags", .has_arg = required_argument },
3195 { .val = LFS_COMP_FLAGS_OPT,
3196 .name = "component-flags",
3197 .has_arg = required_argument },
3198 { .val = LFS_COMP_SET_OPT,
3199 .name = "comp-set", .has_arg = no_argument },
3200 { .val = LFS_COMP_SET_OPT,
3201 .name = "component-set",
3202 .has_arg = no_argument},
3203 { .val = LFS_COMP_NO_VERIFY_OPT,
3204 .name = "no-verify", .has_arg = no_argument},
3205 { .val = LFS_LAYOUT_FLAGS_OPT,
3206 .name = "flags", .has_arg = required_argument},
3207 { .val = LFS_LAYOUT_FOREIGN_OPT,
3208 .name = "foreign", .has_arg = optional_argument},
3209 { .val = LFS_MIRROR_ID_OPT,
3210 .name = "mirror-id", .has_arg = required_argument},
3211 { .val = LFS_MODE_OPT,
3212 .name = "mode", .has_arg = required_argument},
3213 { .val = LFS_LAYOUT_COPY,
3214 .name = "copy", .has_arg = required_argument},
3215 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3216 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3217 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3218 { .val = 'C', .name = "overstripe-count",
3219 .has_arg = required_argument},
3220 { .val = 'd', .name = "delete", .has_arg = no_argument},
3221 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3222 /* --non-direct is only valid in migrate mode */
3223 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3224 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3225 { .val = 'E', .name = "component-end",
3226 .has_arg = required_argument},
3227 { .val = 'f', .name = "file", .has_arg = required_argument },
3228 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3229 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3230 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3231 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3232 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3233 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3234 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3235 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3236 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3237 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3238 { .val = 'L', .name = "layout", .has_arg = required_argument },
3239 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3240 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3241 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3242 /* --non-block is only valid in migrate mode */
3243 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3244 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3245 { .val = 'o', .name = "ost", .has_arg = required_argument },
3246 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3247 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3248 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3250 { .val = 'p', .name = "pool", .has_arg = required_argument },
3251 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3252 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3253 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3254 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3255 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3256 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3257 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3258 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3259 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3260 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3261 /* --verbose is only valid in migrate mode */
3262 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3263 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3264 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3265 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3266 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3269 setstripe_args_init(&lsa);
3271 migrate_mode = (opc == SO_MIGRATE);
3272 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3273 setstripe_mode = (opc == SO_SETSTRIPE);
3274 if (opc == SO_MIRROR_DELETE) {
3276 mirror_flags = MF_DESTROY;
3279 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3281 while ((c = getopt_long(argc, argv,
3282 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3283 long_opts, NULL)) >= 0) {
3289 case LFS_COMP_ADD_OPT:
3292 case LFS_COMP_DEL_OPT:
3295 case LFS_COMP_FLAGS_OPT:
3296 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3297 &lsa.lsa_comp_neg_flags);
3300 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3302 "%s: inverted flags are not supported\n",
3307 case LFS_COMP_SET_OPT:
3310 case LFS_COMP_NO_VERIFY_OPT:
3311 mirror_flags |= MF_NO_VERIFY;
3313 case LFS_MIRROR_ID_OPT:
3314 mirror_id = strtoul(optarg, &end, 0);
3315 if (*end != '\0' || mirror_id == 0) {
3317 "%s %s: invalid mirror ID '%s'\n",
3318 progname, argv[0], optarg);
3322 case LFS_LAYOUT_FLAGS_OPT: {
3325 /* check for numeric flags (foreign and mirror cases) */
3326 if (setstripe_mode && !mirror_mode && !last_mirror) {
3327 flags = strtoul(optarg, &end, 16);
3330 "%s %s: bad flags '%s'\n",
3331 progname, argv[0], optarg);
3337 if (!mirror_mode || !last_mirror) {
3339 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3344 result = comp_str2flags(optarg, &last_mirror->m_flags,
3351 "%s: inverted flags are not supported\n",
3356 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3358 "%s: unsupported mirror flags: %s\n",
3365 case LFS_LAYOUT_FOREIGN_OPT:
3367 /* check pure numeric */
3368 type = strtoul(optarg, &end, 0);
3371 type = check_foreign_type_name(optarg);
3372 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3374 "%s %s: unrecognized foreign type '%s'\n",
3381 foreign_mode = true;
3386 mode = strtoul(mode_opt, &end, 8);
3389 "%s %s: bad mode '%s'\n",
3390 progname, argv[0], mode_opt);
3393 previous_umask = umask(0);
3396 case LFS_LAYOUT_COPY:
3401 if (!migrate_mode) {
3403 "%s %s: -b|--block valid only for migrate command\n",
3407 migration_block = true;
3410 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3412 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3416 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3419 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3422 "%s %s: invalid stripe count '%s'\n",
3423 progname, argv[0], optarg);
3427 if (lsa.lsa_stripe_count == -1)
3428 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3431 /* delete the default striping pattern */
3433 if (opc == SO_MIRROR_SPLIT) {
3436 "%s %s: -d cannot used with -f\n",
3440 mirror_flags |= MF_DESTROY;
3444 if (!migrate_mode) {
3446 "%s %s: -D|--non-direct is valid only for migrate command\n",
3450 migration_flags |= MIGRATION_NONDIRECT;
3453 if (lsa.lsa_comp_end != 0) {
3454 result = comp_args_to_layout(lpp, &lsa, true);
3456 fprintf(stderr, "%s: invalid layout\n",
3461 setstripe_args_init_inherit(&lsa);
3464 if (arg_is_eof(optarg)) {
3465 lsa.lsa_comp_end = LUSTRE_EOF;
3467 result = llapi_parse_size(optarg,
3472 "%s %s: invalid component end '%s'\n",
3473 progname, argv[0], optarg);
3479 if (!migrate_mode) {
3481 "--mdt-hash is valid only for migrate command\n");
3485 lsa.lsa_pattern = check_hashtype(optarg);
3486 if (lsa.lsa_pattern == 0) {
3488 "%s %s: bad stripe hash type '%s'\n",
3489 progname, argv[0], optarg);
3494 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3497 "%s %s: invalid stripe offset '%s'\n",
3498 progname, argv[0], optarg);
3501 if (lsa.lsa_stripe_off == -1)
3502 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3505 comp_id = strtoul(optarg, &end, 0);
3506 if (*end != '\0' || comp_id == 0 ||
3507 comp_id > LCME_ID_MAX) {
3509 "%s %s: invalid component ID '%s'\n",
3510 progname, argv[0], optarg);
3515 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3517 "error: %s: invalid option: %s\n",
3518 progname, argv[optopt + 1]);
3521 if (opc == SO_MIRROR_EXTEND) {
3524 "error: %s: '-N' must exist in front of '%s'\n",
3525 progname, argv[optopt + 1]);
3528 last_mirror->m_file = optarg;
3529 last_mirror->m_count = 1;
3533 mirror_list = lfs_mirror_alloc();
3534 mirror_list->m_file = optarg;
3539 if (strcmp(argv[optind - 1], "mdt") == 0) {
3540 /* Can be only the first component */
3544 "error: 'mdt' layout can be only the first one\n");
3547 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3550 "error: 'mdt' layout size is too big\n");
3553 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3554 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3557 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3563 if (!migrate_mode) {
3565 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3569 migrate_mdt_mode = true;
3570 lsa.lsa_nr_tgts = parse_targets(tgts,
3571 sizeof(tgts) / sizeof(__u32),
3572 lsa.lsa_nr_tgts, optarg, NULL);
3573 if (lsa.lsa_nr_tgts < 0) {
3575 "%s %s: invalid MDT target(s) '%s'\n",
3576 progname, argv[0], optarg);
3580 lsa.lsa_tgts = tgts;
3581 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3582 lsa.lsa_stripe_off = tgts[0];
3585 if (!migrate_mode) {
3587 "%s %s: -n|--non-block valid only for migrate command\n",
3591 migration_flags |= MIGRATION_NONBLOCK;
3594 if (opc == SO_SETSTRIPE) {
3595 opc = SO_MIRROR_CREATE;
3600 mirror_count = strtoul(optarg, &end, 0);
3601 if (*end != '\0' || mirror_count == 0) {
3603 "error: %s: bad mirror count: %s\n",
3610 new_mirror = lfs_mirror_alloc();
3611 new_mirror->m_count = mirror_count;
3614 mirror_list = new_mirror;
3617 /* wrap up last mirror */
3618 if (!setstripe_args_specified(&lsa))
3619 last_mirror->m_inherit = true;
3620 if (lsa.lsa_comp_end == 0)
3621 lsa.lsa_comp_end = LUSTRE_EOF;
3623 result = comp_args_to_layout(lpp, &lsa, true);
3625 lfs_mirror_free(new_mirror);
3629 setstripe_args_init_inherit(&lsa);
3631 last_mirror->m_next = new_mirror;
3634 last_mirror = new_mirror;
3635 lpp = &last_mirror->m_layout;
3638 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3639 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3641 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3643 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3645 "%s %s: -o|--ost incompatible with DoM layout\n",
3650 * -o allows overstriping, and must note it because
3651 * parse_targets is shared with MDT striping, which
3652 * does not allow duplicates
3654 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3655 lsa.lsa_nr_tgts = parse_targets(tgts,
3656 sizeof(tgts) / sizeof(__u32),
3657 lsa.lsa_nr_tgts, optarg,
3659 if (lsa.lsa_nr_tgts < 0) {
3661 "%s %s: invalid OST target(s) '%s'\n",
3662 progname, argv[0], optarg);
3666 lsa.lsa_tgts = tgts;
3667 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3668 lsa.lsa_stripe_off = tgts[0];
3673 lsa.lsa_pool_name = optarg;
3675 if (strlen(lsa.lsa_pool_name) == 0 ||
3676 strncmp(lsa.lsa_pool_name, "none",
3677 LOV_MAXPOOLNAME) == 0)
3678 lsa.lsa_pool_name = NULL;
3681 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3685 "%s %s: invalid stripe size '%s'\n",
3686 progname, argv[0], optarg);
3691 if (!migrate_mode) {
3693 "%s %s: -v|--verbose valid only for migrate command\n",
3697 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3698 migration_flags = MIGRATION_VERBOSE;
3708 result = llapi_parse_size(optarg,
3709 &lsa.lsa_extension_size,
3713 "%s %s: invalid extension size '%s'\n",
3714 progname, argv[0], optarg);
3718 lsa.lsa_extension_comp = true;
3721 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3722 progname, argv[0], argv[optind - 1]);
3727 fname = argv[optind];
3729 if (optind == argc) {
3730 fprintf(stderr, "%s %s: FILE must be specified\n",
3735 /* lfs migrate $filename should keep the file's layout by default */
3736 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3740 if (xattr && !foreign_mode) {
3742 * only print a warning as this is harmless and will be ignored
3745 "%s %s: xattr has been specified for non-foreign layout\n",
3747 } else if (foreign_mode && !xattr) {
3749 "%s %s: xattr must be provided in foreign mode\n",
3754 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3755 comp_set || comp_id || delete || from_copy ||
3756 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3759 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3764 if (mirror_mode && mirror_count == 0) {
3766 "error: %s: --mirror-count|-N option is required\n",
3773 if (!setstripe_args_specified(&lsa))
3774 last_mirror->m_inherit = true;
3775 if (lsa.lsa_comp_end == 0)
3776 lsa.lsa_comp_end = LUSTRE_EOF;
3779 if (lsa.lsa_comp_end != 0) {
3780 result = comp_args_to_layout(lpp, &lsa, true);
3782 fprintf(stderr, "error: %s: invalid layout\n",
3789 if (mirror_flags & MF_NO_VERIFY) {
3790 if (opc != SO_MIRROR_EXTEND) {
3792 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3796 } else if (!has_m_file) {
3798 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3805 if (comp_set && !comp_id) {
3807 "%s %s: --component-set doesn't have component-id set\n",
3812 if ((delete + comp_set + comp_del + comp_add) > 1) {
3814 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3819 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3820 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3822 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3827 if ((comp_set || comp_del) &&
3828 (setstripe_args_specified(&lsa) || layout != NULL)) {
3830 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3835 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3837 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3842 if (comp_add || comp_del) {
3845 result = lstat(fname, &st);
3846 if (result == 0 && S_ISDIR(st.st_mode)) {
3848 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3855 "error: %s: can't use --component-add or --component-del for mirror operation\n",
3864 "%s %s: option -E must be specified with --component-add\n",
3870 if (from_yaml && from_copy) {
3872 "%s: can't specify --yaml and --copy together\n",
3877 if ((from_yaml || from_copy) &&
3878 (setstripe_args_specified(&lsa) || layout != NULL)) {
3880 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
3885 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3887 "%s %s: options --non-block and --block are mutually exclusive\n",
3892 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3893 opc != SO_MIRROR_DELETE && comp_id != 0) {
3895 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3900 if (migrate_mdt_mode) {
3901 struct lmv_user_md *lmu;
3903 /* initialize migrate mdt parameters */
3904 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3905 LMV_USER_MAGIC_SPECIFIC));
3908 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3909 progname, argv[0], strerror(ENOMEM));
3913 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3914 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3915 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3917 "%s %s: migrate should specify MDT index\n",
3922 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3923 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3924 lmu->lum_hash_type = lsa.lsa_pattern;
3926 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
3927 if (lsa.lsa_pool_name) {
3928 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3929 sizeof(lmu->lum_pool_name) - 1);
3930 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3932 if (lsa.lsa_nr_tgts > 1) {
3935 if (lsa.lsa_stripe_count > 0 &&
3936 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3937 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3939 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3940 progname, lsa.lsa_stripe_count,
3946 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3947 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3948 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3949 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3951 lmu->lum_magic = LMV_USER_MAGIC;
3954 migrate_mdt_param.fp_lmv_md = lmu;
3955 migrate_mdt_param.fp_migrate = 1;
3956 } else if (!layout) {
3957 /* initialize stripe parameters */
3958 param = calloc(1, offsetof(typeof(*param),
3959 lsp_osts[lsa.lsa_nr_tgts]));
3962 "%s %s: cannot allocate memory for parameters: %s\n",
3963 progname, argv[0], strerror(ENOMEM));
3968 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3969 param->lsp_stripe_size = lsa.lsa_stripe_size;
3970 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3971 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3972 param->lsp_stripe_count = -1;
3974 param->lsp_stripe_count = lsa.lsa_stripe_count;
3976 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3977 param->lsp_stripe_offset = -1;
3979 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3980 param->lsp_stripe_pattern =
3981 llapi_pattern_to_lov(lsa.lsa_pattern);
3982 if (param->lsp_stripe_pattern == EINVAL) {
3983 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3988 param->lsp_pool = lsa.lsa_pool_name;
3989 param->lsp_is_specific = false;
3990 if (lsa.lsa_nr_tgts > 0) {
3991 if (lsa.lsa_stripe_count > 0 &&
3992 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3993 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3994 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3996 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3997 argv[0], lsa.lsa_stripe_count,
4003 param->lsp_is_specific = true;
4004 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4005 memcpy(param->lsp_osts, tgts,
4006 sizeof(*tgts) * lsa.lsa_nr_tgts);
4011 /* generate a layout from a YAML template */
4012 result = lfs_comp_create_from_yaml(template, &layout,
4016 "error: %s: can't create composite layout from template file %s\n",
4022 if (layout != NULL || mirror_list != NULL) {
4024 result = mirror_adjust_first_extents(mirror_list);
4026 result = layout_adjust_first_extent(fname, layout,
4028 if (result == -ENODATA)
4030 else if (result != 0) {
4031 fprintf(stderr, "error: %s: invalid layout\n",
4037 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4039 layout = llapi_layout_get_by_path(template ?: fname, 0);
4042 "%s: can't create composite layout from file %s.\n",
4043 progname, template ?: fname);
4048 if (migrate_mdt_mode) {
4049 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4050 } else if (migrate_mode) {
4051 result = lfs_migrate(fname, migration_flags, param,
4053 } else if (comp_set != 0) {
4054 result = lfs_component_set(fname, comp_id,
4056 lsa.lsa_comp_neg_flags);
4057 } else if (comp_del != 0) {
4058 result = lfs_component_del(fname, comp_id,
4060 lsa.lsa_comp_neg_flags);
4061 } else if (comp_add != 0) {
4062 result = lfs_component_add(fname, layout);
4063 } else if (opc == SO_MIRROR_CREATE) {
4064 result = mirror_create(fname, mirror_list);
4065 } else if (opc == SO_MIRROR_EXTEND) {
4066 result = mirror_extend(fname, mirror_list,
4068 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4069 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4071 "%s: no mirror specified to delete from '%s'\n",
4075 if (lsa.lsa_pool_name)
4076 mirror_flags |= MF_COMP_POOL;
4077 else if (mirror_id != 0)
4078 comp_id = mirror_id;
4080 mirror_flags |= MF_COMP_ID;
4081 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4083 has_m_file ? mirror_list->m_file :
4085 } else if (layout) {
4086 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4092 } else if (foreign_mode) {
4093 result = llapi_file_create_foreign(fname, mode, type,
4100 result = llapi_file_open_param(fname,
4109 /* Save the first error encountered. */
4117 umask(previous_umask);
4120 free(migrate_mdt_param.fp_lmv_md);
4121 llapi_layout_free(layout);
4122 lfs_mirror_list_free(mirror_list);
4127 llapi_layout_free(layout);
4128 lfs_mirror_list_free(mirror_list);
4132 static int lfs_poollist(int argc, char **argv)
4137 return llapi_poollist(argv[1]);
4140 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4145 char *endptr = "AD";
4150 else if (str[0] == '-')
4156 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4157 long long val = strtoll(timebuf, &endptr, 0);
4162 unit *= 52; /* 52 weeks + 1 day below */
4163 case 'w': /* fallthrough */
4165 case '\0': /* days are default unit if none used */
4166 case 'd': /* fallthrough */
4167 unit = (unit + (*endptr == 'y')) * 24;
4168 case 'h': /* fallthrough */
4170 case 'm': /* fallthrough */
4172 case 's': /* fallthrough */
4174 /* don't need to multiply by 1 for seconds */
4177 "%s find: bad time string '%s': %s\n",
4178 progname, timebuf, strerror(EINVAL));
4182 if (param->fp_time_margin == 0 ||
4183 (*endptr && unit < param->fp_time_margin))
4184 param->fp_time_margin = unit;
4191 fprintf(stderr, "%s find: bad time '%s': too large\n",
4201 static int str2quotaid(__u32 *id, const char *arg)
4203 unsigned long int projid_tmp = 0;
4204 char *endptr = NULL;
4206 projid_tmp = strtoul(arg, &endptr, 10);
4207 if (*endptr != '\0')
4209 if (projid_tmp > UINT32_MAX ||
4210 (projid_tmp == ULONG_MAX && (errno == ERANGE)))
4217 static int name2uid(unsigned int *id, const char *name)
4219 struct passwd *passwd;
4221 passwd = getpwnam(name);
4224 *id = passwd->pw_uid;
4229 static int name2gid(unsigned int *id, const char *name)
4231 struct group *group;
4233 group = getgrnam(name);
4236 *id = group->gr_gid;
4241 static inline int name2projid(unsigned int *id, const char *name)
4246 static int uid2name(char **name, unsigned int id)
4248 struct passwd *passwd;
4250 passwd = getpwuid(id);
4253 *name = passwd->pw_name;
4258 static inline int gid2name(char **name, unsigned int id)
4260 struct group *group;
4262 group = getgrgid(id);
4265 *name = group->gr_name;
4270 static int name2layout(__u32 *layout, char *name)
4272 char *ptr, *layout_name;
4275 for (ptr = name; ; ptr = NULL) {
4276 layout_name = strtok(ptr, ",");
4279 if (strcmp(layout_name, "released") == 0)
4280 *layout |= LOV_PATTERN_F_RELEASED;
4281 else if (strcmp(layout_name, "raid0") == 0)
4282 *layout |= LOV_PATTERN_RAID0;
4283 else if (strcmp(layout_name, "mdt") == 0)
4284 *layout |= LOV_PATTERN_MDT;
4285 else if (strcmp(layout_name, "overstriping") == 0)
4286 *layout |= LOV_PATTERN_OVERSTRIPING;
4293 static int lfs_find(int argc, char **argv)
4298 struct find_param param = {
4301 .fp_time_margin = 24 * 60 * 60,
4303 struct option long_opts[] = {
4304 { .val = 'A', .name = "atime", .has_arg = required_argument },
4305 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4306 { .val = 'B', .name = "btime", .has_arg = required_argument },
4307 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4308 { .val = LFS_COMP_COUNT_OPT,
4309 .name = "comp-count", .has_arg = required_argument },
4310 { .val = LFS_COMP_COUNT_OPT,
4311 .name = "component-count",
4312 .has_arg = required_argument },
4313 { .val = LFS_COMP_FLAGS_OPT,
4314 .name = "comp-flags", .has_arg = required_argument },
4315 { .val = LFS_COMP_FLAGS_OPT,
4316 .name = "component-flags",
4317 .has_arg = required_argument },
4318 { .val = LFS_COMP_START_OPT,
4319 .name = "comp-start", .has_arg = required_argument },
4320 { .val = LFS_COMP_START_OPT,
4321 .name = "component-start",
4322 .has_arg = required_argument },
4323 { .val = LFS_MIRROR_STATE_OPT,
4324 .name = "mirror-state", .has_arg = required_argument },
4325 { .val = LFS_LAYOUT_FOREIGN_OPT,
4326 .name = "foreign", .has_arg = optional_argument},
4327 { .val = LFS_NEWERXY_OPT,
4328 .name = "newer", .has_arg = required_argument},
4329 { .val = LFS_NEWERXY_OPT,
4330 .name = "neweraa", .has_arg = required_argument},
4331 { .val = LFS_NEWERXY_OPT,
4332 .name = "neweram", .has_arg = required_argument},
4333 { .val = LFS_NEWERXY_OPT,
4334 .name = "newerac", .has_arg = required_argument},
4335 { .val = LFS_NEWERXY_OPT,
4336 .name = "newerab", .has_arg = required_argument},
4337 { .val = LFS_NEWERXY_OPT,
4338 .name = "newerma", .has_arg = required_argument},
4339 { .val = LFS_NEWERXY_OPT,
4340 .name = "newermm", .has_arg = required_argument},
4341 { .val = LFS_NEWERXY_OPT,
4342 .name = "newermc", .has_arg = required_argument},
4343 { .val = LFS_NEWERXY_OPT,
4344 .name = "newermb", .has_arg = required_argument},
4345 { .val = LFS_NEWERXY_OPT,
4346 .name = "newerca", .has_arg = required_argument},
4347 { .val = LFS_NEWERXY_OPT,
4348 .name = "newercm", .has_arg = required_argument},
4349 { .val = LFS_NEWERXY_OPT,
4350 .name = "newercc", .has_arg = required_argument},
4351 { .val = LFS_NEWERXY_OPT,
4352 .name = "newercb", .has_arg = required_argument},
4353 { .val = LFS_NEWERXY_OPT,
4354 .name = "newerba", .has_arg = required_argument},
4355 { .val = LFS_NEWERXY_OPT,
4356 .name = "newerbm", .has_arg = required_argument},
4357 { .val = LFS_NEWERXY_OPT,
4358 .name = "newerbc", .has_arg = required_argument},
4359 { .val = LFS_NEWERXY_OPT,
4360 .name = "newerbb", .has_arg = required_argument},
4361 { .val = LFS_NEWERXY_OPT,
4362 .name = "newerBa", .has_arg = required_argument},
4363 { .val = LFS_NEWERXY_OPT,
4364 .name = "newerBm", .has_arg = required_argument},
4365 { .val = LFS_NEWERXY_OPT,
4366 .name = "newerBc", .has_arg = required_argument},
4367 { .val = LFS_NEWERXY_OPT,
4368 .name = "newerBB", .has_arg = required_argument},
4369 { .val = LFS_NEWERXY_OPT,
4370 .name = "newerat", .has_arg = required_argument},
4371 { .val = LFS_NEWERXY_OPT,
4372 .name = "newermt", .has_arg = required_argument},
4373 { .val = LFS_NEWERXY_OPT,
4374 .name = "newerct", .has_arg = required_argument},
4375 { .val = LFS_NEWERXY_OPT,
4376 .name = "newerbt", .has_arg = required_argument},
4377 { .val = LFS_NEWERXY_OPT,
4378 .name = "newerBt", .has_arg = required_argument},
4379 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4380 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4381 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4382 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4383 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4384 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4385 { .val = 'E', .name = "component-end",
4386 .has_arg = required_argument },
4387 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4388 { .val = LFS_LAYOUT_FOREIGN_OPT,
4389 .name = "foreign", .has_arg = optional_argument},
4390 { .val = 'g', .name = "gid", .has_arg = required_argument },
4391 { .val = 'G', .name = "group", .has_arg = required_argument },
4392 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4393 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4394 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4395 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4396 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4397 { .val = 'L', .name = "layout", .has_arg = required_argument },
4398 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4399 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4400 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4401 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4402 { .val = 'n', .name = "name", .has_arg = required_argument },
4403 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4404 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4405 { .val = 'O', .name = "obd", .has_arg = required_argument },
4406 { .val = 'O', .name = "ost", .has_arg = required_argument },
4407 /* no short option for pool yet, can be 'p' after 2.18 */
4408 { .val = LFS_POOL_OPT,
4409 .name = "pool", .has_arg = required_argument },
4410 { .val = '0', .name = "print0", .has_arg = no_argument },
4411 { .val = 'P', .name = "print", .has_arg = no_argument },
4412 { .val = LFS_PROJID_OPT,
4413 .name = "projid", .has_arg = required_argument },
4414 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4415 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4416 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4417 { .val = 's', .name = "size", .has_arg = required_argument },
4418 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4419 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4420 { .val = 't', .name = "type", .has_arg = required_argument },
4421 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4422 { .val = 'u', .name = "uid", .has_arg = required_argument },
4423 { .val = 'U', .name = "user", .has_arg = required_argument },
4424 { .val = 'z', .name = "extension-size",
4425 .has_arg = required_argument },
4426 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4427 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4428 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4442 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4443 while ((c = getopt_long_only(argc, argv,
4444 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4445 long_opts, &optidx)) >= 0) {
4450 /* '!' is part of option */
4452 * when getopt_long_only() finds a string which is not
4453 * an option nor a known option argument it returns 1
4454 * in that case if we already have found pathstart and pathend
4455 * (i.e. we have the list of pathnames),
4456 * the only supported value is "!"
4458 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4459 if (!isoption && pathend != -1) {
4461 "err: %s: filename|dirname must either precede options or follow options\n",
4466 if (!isoption && pathstart == -1)
4467 pathstart = optind - 1;
4468 if (isoption && pathstart != -1 && pathend == -1)
4469 pathend = optind - 2;
4476 * unknown; opt is "!" or path component,
4477 * checking done above.
4479 if (strcmp(optarg, "!") == 0)
4483 xtime = ¶m.fp_atime;
4484 xsign = ¶m.fp_asign;
4485 param.fp_exclude_atime = !!neg_opt;
4486 /* no break, this falls through to 'B' for btime */
4489 xtime = ¶m.fp_btime;
4490 xsign = ¶m.fp_bsign;
4491 param.fp_exclude_btime = !!neg_opt;
4493 /* no break, this falls through to 'C' for ctime */
4496 xtime = ¶m.fp_ctime;
4497 xsign = ¶m.fp_csign;
4498 param.fp_exclude_ctime = !!neg_opt;
4500 /* no break, this falls through to 'M' for mtime */
4503 xtime = ¶m.fp_mtime;
4504 xsign = ¶m.fp_msign;
4505 param.fp_exclude_mtime = !!neg_opt;
4507 rc = set_time(¶m, &t, xtime, optarg);
4508 if (rc == LONG_MAX) {
4516 if (optarg[0] == '+') {
4517 param.fp_blocks_sign = -1;
4519 } else if (optarg[0] == '-') {
4520 param.fp_blocks_sign = 1;
4524 param.fp_blocks_units = 1024;
4525 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4526 ¶m.fp_blocks_units, 0);
4528 fprintf(stderr, "error: bad blocks '%s'\n",
4532 param.fp_check_blocks = 1;
4533 param.fp_exclude_blocks = !!neg_opt;
4535 case LFS_COMP_COUNT_OPT:
4536 if (optarg[0] == '+') {
4537 param.fp_comp_count_sign = -1;
4539 } else if (optarg[0] == '-') {
4540 param.fp_comp_count_sign = 1;
4544 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4545 if (*endptr != '\0') {
4547 "error: bad component count '%s'\n",
4551 param.fp_check_comp_count = 1;
4552 param.fp_exclude_comp_count = !!neg_opt;
4554 case LFS_COMP_FLAGS_OPT:
4555 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4556 ¶m.fp_comp_neg_flags);
4559 "error: bad component flags '%s'\n",
4563 param.fp_check_comp_flags = 1;
4565 __u32 flags = param.fp_comp_neg_flags;
4567 param.fp_comp_neg_flags = param.fp_comp_flags;
4568 param.fp_comp_flags = flags;
4571 case LFS_COMP_START_OPT:
4572 if (optarg[0] == '+') {
4573 param.fp_comp_start_sign = -1;
4575 } else if (optarg[0] == '-') {
4576 param.fp_comp_start_sign = 1;
4580 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4581 ¶m.fp_comp_start_units, 0);
4584 "error: bad component start '%s'\n",
4588 param.fp_check_comp_start = 1;
4589 param.fp_exclude_comp_start = !!neg_opt;
4591 case LFS_MIRROR_STATE_OPT:
4592 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4593 ¶m.fp_mirror_neg_state);
4596 "error: bad mirrored file state '%s'\n",
4600 param.fp_check_mirror_state = 1;
4602 __u16 state = param.fp_mirror_neg_state;
4604 param.fp_mirror_neg_state =
4605 param.fp_mirror_state;
4606 param.fp_mirror_state = state;
4610 if (optarg[0] == '+') {
4611 param.fp_stripe_count_sign = -1;
4613 } else if (optarg[0] == '-') {
4614 param.fp_stripe_count_sign = 1;
4618 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4619 if (*endptr != '\0') {
4621 "error: bad stripe_count '%s'\n",
4626 param.fp_check_stripe_count = 1;
4627 param.fp_exclude_stripe_count = !!neg_opt;
4630 param.fp_max_depth = strtol(optarg, 0, 0);
4633 if (optarg[0] == '+') {
4634 param.fp_comp_end_sign = -1;
4636 } else if (optarg[0] == '-') {
4637 param.fp_comp_end_sign = 1;
4641 if (arg_is_eof(optarg)) {
4642 param.fp_comp_end = LUSTRE_EOF;
4643 param.fp_comp_end_units = 1;
4646 rc = llapi_parse_size(optarg,
4648 ¶m.fp_comp_end_units, 0);
4652 "error: bad component end '%s'\n",
4656 param.fp_check_comp_end = 1;
4657 param.fp_exclude_comp_end = !!neg_opt;
4659 case LFS_LAYOUT_FOREIGN_OPT: {
4660 /* all types by default */
4661 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4664 /* check pure numeric */
4665 type = strtoul(optarg, &endptr, 0);
4668 type = check_foreign_type_name(optarg);
4669 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4671 "%s %s: unknown foreign type '%s'\n",
4678 param.fp_foreign_type = type;
4679 param.fp_check_foreign = 1;
4680 param.fp_exclude_foreign = !!neg_opt;
4683 case LFS_NEWERXY_OPT: {
4689 time_t ref = time(NULL);
4691 /* no need to check bad options, they won't get here */
4692 if (strlen(long_opts[optidx].name) == 7) {
4693 x = long_opts[optidx].name[5];
4694 y = long_opts[optidx].name[6];
4698 static const char *const fmts[] = {
4699 "%Y-%m-%d %H:%M:%S",
4702 "%H:%M:%S", /* sometime today */
4711 for (i = 0; fmts[i] != NULL; i++) {
4714 /* Init for times relative to today */
4715 if (strncmp(fmts[i], "%H", 2) == 0)
4716 localtime_r(&ref, &tm);
4718 memset(&tm, 0, sizeof(tm));
4719 ptr = strptime(optarg, fmts[i], &tm);
4721 while (ptr && isspace(*ptr))
4723 if (ptr == optarg + strlen(optarg)) {
4731 "%s: invalid time '%s'\n",
4734 "supported formats are:\n ");
4735 for (i = 0; fmts[i] != NULL; i++)
4736 fprintf(stderr, "'%s', ",
4738 fprintf(stderr, "\n");
4744 } else if (y == 'b' || y == 'B') {
4747 rc = llapi_get_lum_file(optarg, NULL, &stx,
4749 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4750 if (!(stx.stx_mask & STATX_BTIME))
4755 "%s: get btime failed '%s': %s\n",
4761 ref = stx.stx_btime.tv_sec;
4763 struct stat statbuf;
4765 if (stat(optarg, &statbuf) < 0) {
4767 "%s: cannot stat file '%s': %s\n",
4776 ref = statbuf.st_atime;
4779 ref = statbuf.st_mtime;
4782 ref = statbuf.st_ctime;
4786 "%s: invalid Y argument: '%c'\n",
4795 xidx = NEWERXY_ATIME;
4798 xidx = NEWERXY_MTIME;
4801 xidx = NEWERXY_CTIME;
4805 xidx = NEWERXY_BTIME;
4809 "%s: invalid X argument: '%c'\n",
4816 newery = ¶m.fp_newery[xidx][negidx];
4822 *newery = *newery > ref ? ref : *newery;
4824 *newery = *newery > ref ? *newery : ref;
4826 param.fp_newerxy = 1;
4831 rc = name2gid(¶m.fp_gid, optarg);
4833 if (str2quotaid(¶m.fp_gid, optarg)) {
4835 "Group/GID: %s cannot be found.\n",
4841 param.fp_exclude_gid = !!neg_opt;
4842 param.fp_check_gid = 1;
4845 param.fp_hash_type = check_hashtype(optarg);
4846 if (param.fp_hash_type == 0) {
4847 fprintf(stderr, "error: bad hash_type '%s'\n",
4852 param.fp_check_hash_type = 1;
4853 param.fp_exclude_hash_type = !!neg_opt;
4859 ret = name2layout(¶m.fp_layout, optarg);
4862 param.fp_exclude_layout = !!neg_opt;
4863 param.fp_check_layout = 1;
4867 rc = name2uid(¶m.fp_uid, optarg);
4869 if (str2quotaid(¶m.fp_uid, optarg)) {
4871 "User/UID: %s cannot be found.\n",
4877 param.fp_exclude_uid = !!neg_opt;
4878 param.fp_check_uid = 1;
4881 param.fp_pattern = (char *)optarg;
4882 param.fp_exclude_pattern = !!neg_opt;
4885 if (optarg[0] == '+') {
4886 param.fp_mirror_count_sign = -1;
4888 } else if (optarg[0] == '-') {
4889 param.fp_mirror_count_sign = 1;
4893 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4894 if (*endptr != '\0') {
4896 "error: bad mirror count '%s'\n",
4900 param.fp_check_mirror_count = 1;
4901 param.fp_exclude_mirror_count = !!neg_opt;
4906 char *buf, *token, *next, *p;
4910 buf = strdup(optarg);
4916 param.fp_exclude_obd = !!neg_opt;
4919 while (token && *token) {
4920 token = strchr(token, ',');
4927 param.fp_exclude_mdt = !!neg_opt;
4928 param.fp_num_alloc_mdts += len;
4929 tmp = realloc(param.fp_mdt_uuid,
4930 param.fp_num_alloc_mdts *
4931 sizeof(*param.fp_mdt_uuid));
4937 param.fp_mdt_uuid = tmp;
4939 param.fp_exclude_obd = !!neg_opt;
4940 param.fp_num_alloc_obds += len;
4941 tmp = realloc(param.fp_obd_uuid,
4942 param.fp_num_alloc_obds *
4943 sizeof(*param.fp_obd_uuid));
4949 param.fp_obd_uuid = tmp;
4951 for (token = buf; token && *token; token = next) {
4952 struct obd_uuid *puuid;
4956 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4959 ¶m.fp_obd_uuid[param.fp_num_obds++];
4961 p = strchr(token, ',');
4968 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4973 strncpy(puuid->uuid, token,
4974 sizeof(puuid->uuid));
4981 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4985 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4987 "Pool name %s is too long (max %d)\n",
4988 optarg, LOV_MAXPOOLNAME);
4993 * We do check for empty pool because empty pool
4994 * is used to find V1 LOV attributes
4996 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4997 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4998 param.fp_exclude_pool = !!neg_opt;
4999 param.fp_check_pool = 1;
5001 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5002 case 'p': /* want this for --pool, to match getstripe/find */
5004 "warning: -p deprecated, use --print0 or -0\n");
5007 param.fp_zero_end = 1;
5009 case 'P': /* we always print, this option is a no-op */
5011 case LFS_PROJID_OPT:
5012 rc = name2projid(¶m.fp_projid, optarg);
5014 if (str2quotaid(¶m.fp_projid, optarg)) {
5016 "Invalid project ID: %s\n",
5022 param.fp_exclude_projid = !!neg_opt;
5023 param.fp_check_projid = 1;
5026 if (optarg[0] == '+') {
5027 param.fp_size_sign = -1;
5029 } else if (optarg[0] == '-') {
5030 param.fp_size_sign = 1;
5034 ret = llapi_parse_size(optarg, ¶m.fp_size,
5035 ¶m.fp_size_units, 0);
5037 fprintf(stderr, "error: bad file size '%s'\n",
5041 param.fp_check_size = 1;
5042 param.fp_exclude_size = !!neg_opt;
5045 if (optarg[0] == '+') {
5046 param.fp_stripe_size_sign = -1;
5048 } else if (optarg[0] == '-') {
5049 param.fp_stripe_size_sign = 1;
5053 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5054 ¶m.fp_stripe_size_units, 0);
5056 fprintf(stderr, "error: bad stripe_size '%s'\n",
5060 param.fp_check_stripe_size = 1;
5061 param.fp_exclude_stripe_size = !!neg_opt;
5064 param.fp_exclude_type = !!neg_opt;
5065 switch (optarg[0]) {
5067 param.fp_type = S_IFBLK;
5070 param.fp_type = S_IFCHR;
5073 param.fp_type = S_IFDIR;
5076 param.fp_type = S_IFREG;
5079 param.fp_type = S_IFLNK;
5082 param.fp_type = S_IFIFO;
5085 param.fp_type = S_IFSOCK;
5088 fprintf(stderr, "error: %s: bad type '%s'\n",
5095 if (optarg[0] == '+') {
5096 param.fp_mdt_count_sign = -1;
5098 } else if (optarg[0] == '-') {
5099 param.fp_mdt_count_sign = 1;
5103 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5104 if (*endptr != '\0') {
5105 fprintf(stderr, "error: bad mdt_count '%s'\n",
5110 param.fp_check_mdt_count = 1;
5111 param.fp_exclude_mdt_count = !!neg_opt;
5114 if (optarg[0] == '+') {
5115 param.fp_ext_size_sign = -1;
5117 } else if (optarg[0] == '-') {
5118 param.fp_ext_size_sign = 1;
5122 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5123 ¶m.fp_ext_size_units, 0);
5125 fprintf(stderr, "error: bad ext-size '%s'\n",
5129 param.fp_ext_size /= SEL_UNIT_SIZE;
5130 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5131 param.fp_check_ext_size = 1;
5132 param.fp_exclude_ext_size = !!neg_opt;
5140 if (pathstart == -1) {
5141 fprintf(stderr, "error: %s: no filename|pathname\n",
5145 } else if (pathend == -1) {
5151 rc = llapi_find(argv[pathstart], ¶m);
5154 pathbad = pathstart;
5156 } while (++pathstart < pathend);
5159 fprintf(stderr, "%s: failed for '%s': %s\n",
5160 progname, argv[pathbad], strerror(-rc));
5163 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5164 free(param.fp_obd_uuid);
5166 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5167 free(param.fp_mdt_uuid);
5172 static int lfs_getstripe_internal(int argc, char **argv,
5173 struct find_param *param)
5175 struct option long_opts[] = {
5176 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5177 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5178 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5179 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5180 { .val = LFS_COMP_COUNT_OPT,
5181 .name = "comp-count", .has_arg = no_argument },
5182 { .val = LFS_COMP_COUNT_OPT,
5183 .name = "component-count", .has_arg = no_argument },
5184 { .val = LFS_COMP_FLAGS_OPT,
5185 .name = "comp-flags", .has_arg = optional_argument },
5186 { .val = LFS_COMP_FLAGS_OPT,
5187 .name = "component-flags", .has_arg = optional_argument },
5188 { .val = LFS_COMP_START_OPT,
5189 .name = "comp-start", .has_arg = optional_argument },
5190 { .val = LFS_COMP_START_OPT,
5191 .name = "component-start", .has_arg = optional_argument },
5192 { .val = LFS_MIRROR_INDEX_OPT,
5193 .name = "mirror-index", .has_arg = required_argument },
5194 { .val = LFS_MIRROR_ID_OPT,
5195 .name = "mirror-id", .has_arg = required_argument },
5196 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5197 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5198 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5199 { .val = 'd', .name = "directory", .has_arg = no_argument },
5200 { .val = 'D', .name = "default", .has_arg = no_argument },
5201 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5202 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5203 { .val = 'F', .name = "fid", .has_arg = no_argument },
5204 { .val = 'g', .name = "generation", .has_arg = no_argument },
5205 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5206 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5207 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5208 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5209 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5210 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5211 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5212 { .val = 'L', .name = "layout", .has_arg = no_argument },
5213 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5214 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5215 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5216 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5217 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5218 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5219 { .val = 'O', .name = "obd", .has_arg = required_argument },
5220 { .val = 'O', .name = "ost", .has_arg = required_argument },
5221 { .val = 'p', .name = "pool", .has_arg = no_argument },
5222 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5223 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5224 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5225 { .val = 'R', .name = "raw", .has_arg = no_argument },
5226 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5227 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5228 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5229 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5230 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5231 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5232 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5233 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5234 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5235 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5239 int pathstart = -1, pathend = -1;
5243 while ((c = getopt_long(argc, argv,
5244 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5245 long_opts, NULL)) != -1) {
5249 /* '!' is part of option */
5250 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5251 if (!isoption && pathend != -1) {
5253 "error: %s: filename|dirname must either precede options or follow options\n",
5257 if (!isoption && pathstart == -1)
5258 pathstart = optind - 1;
5259 if (isoption && pathstart != -1 && pathend == -1)
5260 pathend = optind - 2;
5264 /* unknown: opt is "!" */
5265 if (strcmp(optarg, "!") == 0)
5269 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5270 param->fp_verbose |= VERBOSE_COUNT;
5271 param->fp_max_depth = 0;
5274 case LFS_COMP_COUNT_OPT:
5275 param->fp_verbose |= VERBOSE_COMP_COUNT;
5276 param->fp_max_depth = 0;
5278 case LFS_COMP_FLAGS_OPT:
5280 rc = comp_str2flags(optarg,
5281 ¶m->fp_comp_flags,
5282 ¶m->fp_comp_neg_flags);
5285 "error: %s bad component flags '%s'.\n",
5289 param->fp_check_comp_flags = 1;
5291 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5292 param->fp_max_depth = 0;
5295 case LFS_COMP_START_OPT:
5298 if (tmp[0] == '+') {
5299 param->fp_comp_start_sign = -1;
5301 } else if (tmp[0] == '-') {
5302 param->fp_comp_start_sign = 1;
5305 rc = llapi_parse_size(tmp,
5306 ¶m->fp_comp_start,
5307 ¶m->fp_comp_start_units, 0);
5310 "error: %s bad component start '%s'.\n",
5314 param->fp_check_comp_start = 1;
5316 param->fp_verbose |= VERBOSE_COMP_START;
5317 param->fp_max_depth = 0;
5320 case LFS_MIRROR_INDEX_OPT:
5321 if (optarg[0] == '+') {
5322 param->fp_mirror_index_sign = -1;
5324 } else if (optarg[0] == '-') {
5325 param->fp_mirror_index_sign = 1;
5329 param->fp_mirror_index = strtoul(optarg, &end, 0);
5330 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5331 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5333 "%s %s: invalid mirror index '%s'\n",
5334 progname, argv[0], optarg);
5337 if (param->fp_mirror_id != 0) {
5339 "%s %s: can't specify both mirror index and mirror ID\n",
5343 param->fp_check_mirror_index = 1;
5344 param->fp_exclude_mirror_index = !!neg_opt;
5346 case LFS_MIRROR_ID_OPT:
5347 if (optarg[0] == '+') {
5348 param->fp_mirror_id_sign = -1;
5350 } else if (optarg[0] == '-') {
5351 param->fp_mirror_id_sign = 1;
5355 param->fp_mirror_id = strtoul(optarg, &end, 0);
5356 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5357 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5359 "%s %s: invalid mirror ID '%s'\n",
5360 progname, argv[0], optarg);
5363 if (param->fp_mirror_index != 0) {
5365 "%s %s: can't specify both mirror index and mirror ID\n",
5369 param->fp_check_mirror_id = 1;
5370 param->fp_exclude_mirror_id = !!neg_opt;
5373 param->fp_max_depth = 0;
5376 param->fp_get_default_lmv = 1;
5381 if (tmp[0] == '+') {
5382 param->fp_comp_end_sign = -1;
5384 } else if (tmp[0] == '-') {
5385 param->fp_comp_end_sign = 1;
5389 if (arg_is_eof(tmp)) {
5390 param->fp_comp_end = LUSTRE_EOF;
5391 param->fp_comp_end_units = 1;
5394 rc = llapi_parse_size(tmp,
5395 ¶m->fp_comp_end,
5396 ¶m->fp_comp_end_units, 0);
5400 "error: %s bad component end '%s'.\n",
5404 param->fp_check_comp_end = 1;
5406 param->fp_verbose |= VERBOSE_COMP_END;
5407 param->fp_max_depth = 0;
5411 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5412 param->fp_verbose |= VERBOSE_DFID;
5413 param->fp_max_depth = 0;
5417 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5418 param->fp_verbose |= VERBOSE_GENERATION;
5419 param->fp_max_depth = 0;
5423 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5424 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5425 param->fp_max_depth = 0;
5430 param->fp_comp_id = strtoul(optarg, &end, 0);
5431 if (*end != '\0' || param->fp_comp_id == 0 ||
5432 param->fp_comp_id > LCME_ID_MAX) {
5434 "error: %s bad component id '%s'\n",
5438 param->fp_check_comp_id = 1;
5440 param->fp_max_depth = 0;
5441 param->fp_verbose |= VERBOSE_COMP_ID;
5445 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5446 param->fp_verbose |= VERBOSE_PATTERN;
5447 param->fp_max_depth = 0;
5450 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5453 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5456 if (!(param->fp_verbose & VERBOSE_DETAIL))
5457 param->fp_max_depth = 0;
5458 param->fp_verbose |= VERBOSE_MDTINDEX;
5461 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5462 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5463 param->fp_max_depth = 0;
5467 if (param->fp_obd_uuid) {
5469 "error: %s: only one obduuid allowed",
5473 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5476 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5477 param->fp_verbose |= VERBOSE_POOL;
5478 param->fp_max_depth = 0;
5485 param->fp_recursive = 1;
5491 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5492 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5493 param->fp_max_depth = 0;
5497 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5503 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5504 param->fp_verbose |= VERBOSE_EXT_SIZE;
5505 param->fp_max_depth = 0;
5513 if (pathstart == -1) {
5514 fprintf(stderr, "error: %s: no filename|pathname\n",
5517 } else if (pathend == -1) {
5525 if (param->fp_recursive)
5526 param->fp_max_depth = -1;
5527 else if (param->fp_verbose & VERBOSE_DETAIL)
5528 param->fp_max_depth = 1;
5530 if (!param->fp_verbose)
5531 param->fp_verbose = VERBOSE_DEFAULT;
5532 if (param->fp_quiet)
5533 param->fp_verbose = VERBOSE_OBJID;
5536 rc = llapi_getstripe(argv[pathstart], param);
5537 } while (++pathstart < pathend && !rc);
5540 fprintf(stderr, "error: %s failed for %s.\n",
5541 argv[0], argv[optind - 1]);
5545 static int lfs_tgts(int argc, char **argv)
5547 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5548 struct find_param param;
5549 int index = 0, rc = 0;
5554 if (argc == 2 && !realpath(argv[1], path)) {
5556 fprintf(stderr, "error: invalid path '%s': %s\n",
5557 argv[1], strerror(-rc));
5561 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5562 /* Check if we have a mount point */
5563 if (mntdir[0] == '\0')
5566 memset(¶m, 0, sizeof(param));
5567 if (!strcmp(argv[0], "mdts"))
5568 param.fp_get_lmv = 1;
5570 rc = llapi_ostlist(mntdir, ¶m);
5572 fprintf(stderr, "error: %s: failed on %s\n",
5575 if (path[0] != '\0')
5577 memset(mntdir, 0, PATH_MAX);
5583 static int lfs_getstripe(int argc, char **argv)
5585 struct find_param param = { 0 };
5587 param.fp_max_depth = 1;
5588 return lfs_getstripe_internal(argc, argv, ¶m);
5592 static int lfs_getdirstripe(int argc, char **argv)
5594 struct find_param param = { 0 };
5595 struct option long_opts[] = {
5596 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5597 { .val = 'D', .name = "default", .has_arg = no_argument },
5598 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5599 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5600 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5601 { .val = 'O', .name = "obd", .has_arg = required_argument },
5602 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5603 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5604 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5605 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5609 param.fp_get_lmv = 1;
5611 while ((c = getopt_long(argc, argv,
5612 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5616 param.fp_verbose |= VERBOSE_COUNT;
5619 param.fp_get_default_lmv = 1;
5623 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5625 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5628 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5631 param.fp_verbose |= VERBOSE_HASH_TYPE;
5634 if (param.fp_obd_uuid) {
5636 "error: %s: only one obduuid allowed",
5640 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5643 param.fp_recursive = 1;
5646 param.fp_verbose |= VERBOSE_DETAIL;
5652 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5653 progname, argv[0], argv[optind - 1]);
5661 if (param.fp_recursive)
5662 param.fp_max_depth = -1;
5664 if (!param.fp_verbose)
5665 param.fp_verbose = VERBOSE_DEFAULT;
5668 rc = llapi_getstripe(argv[optind], ¶m);
5669 } while (++optind < argc && !rc);
5672 fprintf(stderr, "error: %s failed for %s.\n",
5673 argv[0], argv[optind - 1]);
5678 MNTDF_INODES = 0x0001,
5679 MNTDF_COOKED = 0x0002,
5680 MNTDF_LAZY = 0x0004,
5681 MNTDF_VERBOSE = 0x0008,
5682 MNTDF_SHOW = 0x0010,
5685 #define COOK(value) \
5688 while (value > 1024) { \
5696 #define CDF "%11llu"
5697 #define HDF "%8.1f%c"
5701 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5703 double avail, used, ratio = 0;
5706 avail = st->os_ffree;
5707 used = st->os_files - st->os_ffree;
5709 avail = st->os_bavail;
5710 used = st->os_blocks - st->os_bfree;
5712 if (avail + used > 0)
5713 ratio = used / (used + avail) * 100;
5715 /* Round up to match df(1) usage percentage */
5716 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5720 * This is to identify various problem states for "lfs df" if .osn_err = true,
5721 * so only show flags reflecting those states by default. Informational states
5722 * are only shown with "-v" and use lower-case names to distinguish them.
5723 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5725 static struct obd_statfs_state_names {
5726 enum obd_statfs_state osn_state;
5727 const char osn_name;
5730 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5731 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5732 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5733 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5734 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5735 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5736 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5737 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5738 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5741 static int showdf(char *mntdir, struct obd_statfs *stat,
5742 char *uuid, enum mntdf_flags flags,
5743 char *type, int index, int rc)
5745 long long avail, used, total;
5747 char *suffix = "KMGTPEZY";
5748 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5749 char tbuf[3 * sizeof(__u64)];
5750 char ubuf[3 * sizeof(__u64)];
5751 char abuf[3 * sizeof(__u64)];
5752 char rbuf[3 * sizeof(__u64)];
5759 if (flags & MNTDF_INODES) {
5760 avail = stat->os_ffree;
5761 used = stat->os_files - stat->os_ffree;
5762 total = stat->os_files;
5764 int shift = flags & MNTDF_COOKED ? 0 : 10;
5766 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5767 used = ((stat->os_blocks - stat->os_bfree) *
5768 stat->os_bsize) >> shift;
5769 total = (stat->os_blocks * stat->os_bsize) >> shift;
5772 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5774 if (flags & MNTDF_COOKED) {
5778 cook_val = (double)total;
5781 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5784 snprintf(tbuf, sizeof(tbuf), CDF, total);
5786 cook_val = (double)used;
5789 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5792 snprintf(ubuf, sizeof(ubuf), CDF, used);
5794 cook_val = (double)avail;
5797 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5800 snprintf(abuf, sizeof(abuf), CDF, avail);
5802 snprintf(tbuf, sizeof(tbuf), CDF, total);
5803 snprintf(ubuf, sizeof(tbuf), CDF, used);
5804 snprintf(abuf, sizeof(tbuf), CDF, avail);
5807 sprintf(rbuf, RDF, ratio);
5808 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5809 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5811 printf("[%s:%d]", type, index);
5813 if (stat->os_state) {
5817 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5818 if (oss_names[i].osn_state & stat->os_state &&
5819 (oss_names[i].osn_err ||
5820 flags & MNTDF_VERBOSE))
5821 printf("%c", oss_names[i].osn_name);
5828 printf(UUF": inactive device\n", uuid);
5831 printf(UUF": %s\n", uuid, strerror(-rc));
5838 struct ll_stat_type {
5843 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5845 struct ll_statfs_data {
5847 struct obd_statfs sd_st;
5850 struct ll_statfs_buf {
5852 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5855 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5856 int ops, struct ll_statfs_buf *lsb)
5858 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5859 struct obd_uuid uuid_buf;
5860 char *poolname = NULL;
5861 struct ll_stat_type types[] = {
5862 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5863 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5864 { .st_name = NULL } };
5865 struct ll_stat_type *tp;
5866 __u64 ost_files = 0;
5867 __u64 ost_ffree = 0;
5875 poolname = strchr(pool, '.');
5877 if (strncmp(fsname, pool, strlen(fsname))) {
5878 fprintf(stderr, "filesystem name incorrect\n");
5886 fd = open(mntdir, O_RDONLY);
5889 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5894 if (flags & MNTDF_SHOW) {
5895 if (flags & MNTDF_INODES)
5896 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5897 "UUID", "Inodes", "IUsed", "IFree",
5898 "IUse%", "Mounted on");
5900 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5902 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5903 "Used", "Available", "Use%", "Mounted on");
5906 for (tp = types; tp->st_name != NULL; tp++) {
5907 bool have_ost = false;
5909 if (!(tp->st_op & ops))
5912 for (index = 0; ; index++) {
5913 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5914 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5915 type = flags & MNTDF_LAZY ?
5916 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5917 rc2 = llapi_obd_fstatfs(fd, type, index,
5918 &stat_buf, &uuid_buf);
5923 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5924 if (!(flags & MNTDF_VERBOSE))
5926 } else if (rc2 < 0 && rc == 0) {
5931 * If we have OSTs then don't report MDT block counts.
5932 * For MDT-only filesystems the expectation is that all
5933 * layouts have a DoM component. For filesystems with
5934 * OSTs, files are not necessarily going to store data
5935 * on MDTs, and MDT space is limited to a fraction of
5936 * OST space, so don't include it in the summary.
5938 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5945 if (poolname && tp->st_op == LL_STATFS_LOV &&
5946 llapi_search_ost(fsname, poolname,
5947 obd_uuid2str(&uuid_buf)) != 1)
5951 * the llapi_obd_fstatfs() call may have returned with
5952 * an error, but if it filled in uuid_buf we will at
5953 * lease use that to print out a message for that OBD.
5954 * If we didn't get anything in the uuid_buf, then fill
5955 * it in so that we can print an error message.
5957 if (uuid_buf.uuid[0] == '\0')
5958 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5959 "%s%04x", tp->st_name, index);
5961 lsb->sb_buf[lsb->sb_count].sd_index = index;
5962 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5965 if (flags & MNTDF_SHOW)
5966 showdf(mntdir, &stat_buf,
5967 obd_uuid2str(&uuid_buf), flags,
5968 tp->st_name, index, rc2);
5973 if (tp->st_op == LL_STATFS_LMV) {
5974 sum.os_ffree += stat_buf.os_ffree;
5975 sum.os_files += stat_buf.os_files;
5976 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5977 ost_files += stat_buf.os_files;
5978 ost_ffree += stat_buf.os_ffree;
5980 sum.os_blocks += stat_buf.os_blocks *
5982 sum.os_bfree += stat_buf.os_bfree *
5984 sum.os_bavail += stat_buf.os_bavail *
5992 * If we have _some_ OSTs, but don't have as many free objects on the
5993 * OST as inodes on the MDTs, reduce the reported number of inodes
5994 * to compensate, so that the "inodes in use" number is correct.
5995 * This should be kept in sync with ll_statfs_internal().
5997 if (ost_files && ost_ffree < sum.os_ffree) {
5998 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5999 sum.os_ffree = ost_ffree;
6001 if (flags & MNTDF_SHOW) {
6003 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6011 static int lfs_setdirstripe(int argc, char **argv)
6014 struct lfs_setstripe_args lsa = { 0 };
6015 struct llapi_stripe_param *param = NULL;
6016 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6019 char *mode_opt = NULL;
6020 bool default_stripe = false;
6021 bool delete = false;
6022 bool foreign_mode = false;
6023 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6024 mode_t previous_mode = 0;
6026 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
6027 struct option long_opts[] = {
6028 { .val = 'c', .name = "count", .has_arg = required_argument },
6029 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6030 { .val = 'd', .name = "delete", .has_arg = no_argument },
6031 { .val = 'D', .name = "default", .has_arg = no_argument },
6032 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6033 { .val = LFS_LAYOUT_FLAGS_OPT,
6034 .name = "flags", .has_arg = required_argument },
6035 { .val = LFS_LAYOUT_FOREIGN_OPT,
6036 .name = "foreign", .has_arg = optional_argument},
6037 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6038 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6039 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6040 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6042 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6043 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6044 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6046 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6047 { .val = 'i', .name = "index", .has_arg = required_argument },
6049 { .val = 'o', .name = "mode", .has_arg = required_argument },
6050 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6051 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6053 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6054 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6055 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6059 setstripe_args_init(&lsa);
6061 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6062 long_opts, NULL)) >= 0) {
6069 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6072 "%s %s: invalid stripe count '%s'\n",
6073 progname, argv[0], optarg);
6079 default_stripe = true;
6082 default_stripe = true;
6084 case LFS_LAYOUT_FOREIGN_OPT:
6086 /* check pure numeric */
6087 type = strtoul(optarg, &end, 0);
6090 type = check_foreign_type_name(optarg);
6091 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6093 "%s %s: unknown foreign type '%s'\n",
6100 foreign_mode = true;
6102 case LFS_LAYOUT_FLAGS_OPT:
6103 flags = strtoul(optarg, &end, 16);
6106 "%s %s: bad flags '%s'\n",
6107 progname, argv[0], optarg);
6111 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6114 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6117 lsa.lsa_pattern = check_hashtype(optarg);
6118 if (lsa.lsa_pattern == 0) {
6120 "%s %s: bad stripe hash type '%s'\n",
6121 progname, argv[0], optarg);
6126 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6129 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6130 if (strcmp(argv[optind - 1], "--index") == 0)
6132 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6135 lsa.lsa_nr_tgts = parse_targets(mdts,
6136 sizeof(mdts) / sizeof(__u32),
6137 lsa.lsa_nr_tgts, optarg, NULL);
6138 if (lsa.lsa_nr_tgts < 0) {
6140 "%s %s: invalid MDT target(s) '%s'\n",
6141 progname, argv[0], optarg);
6145 lsa.lsa_tgts = mdts;
6146 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6147 lsa.lsa_stripe_off = mdts[0];
6149 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6152 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6161 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6162 progname, argv[0], argv[optind - 1]);
6167 if (optind == argc) {
6168 fprintf(stderr, "%s %s: DIR must be specified\n",
6173 if (xattr && !foreign_mode) {
6175 * only print a warning as this is armless and will be
6179 "%s %s: xattr has been specified for non-foreign layout\n",
6181 } else if (foreign_mode && !xattr) {
6183 "%s %s: xattr must be provided in foreign mode\n",
6188 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6189 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6191 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6196 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6197 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6199 "%s %s: stripe offset and count must be specified\n",
6205 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6206 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6208 "%s %s: cannot specify -d with -c or -i options\n",
6214 mode = strtoul(mode_opt, &end, 8);
6217 "%s %s: bad MODE '%s'\n",
6218 progname, argv[0], mode_opt);
6221 previous_mode = umask(0);
6224 /* foreign LMV/dir case */
6226 if (argc > optind + 1) {
6228 "%s %s: cannot specify multiple foreign dirs\n",
6233 dname = argv[optind];
6234 result = llapi_dir_create_foreign(dname, mode, type, flags,
6238 "%s mkdir: can't create foreign dir '%s': %s\n",
6239 progname, dname, strerror(-result));
6244 * initialize stripe parameters, in case param is converted to specific,
6245 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6247 param = calloc(1, offsetof(typeof(*param),
6248 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6249 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6252 "%s %s: cannot allocate memory for parameters: %s\n",
6253 progname, argv[0], strerror(ENOMEM));
6257 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6258 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6259 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6260 lsa.lsa_stripe_count == 0))
6261 lsa.lsa_stripe_count = 1;
6262 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6263 param->lsp_stripe_count = lsa.lsa_stripe_count;
6264 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6265 param->lsp_stripe_offset = -1;
6267 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6268 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6269 param->lsp_stripe_pattern = lsa.lsa_pattern;
6271 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6272 param->lsp_pool = lsa.lsa_pool_name;
6273 param->lsp_is_specific = false;
6274 if (lsa.lsa_nr_tgts > 1) {
6275 if (lsa.lsa_stripe_count > 0 &&
6276 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6277 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6279 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6280 argv[0], lsa.lsa_stripe_count,
6286 param->lsp_is_specific = true;
6287 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6288 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6291 dname = argv[optind];
6293 if (default_stripe) {
6294 result = llapi_dir_set_default_lmv(dname, param);
6297 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6298 progname, dname, strerror(-result));
6302 result = llapi_dir_create(dname, mode, param);
6305 "%s setdirstripe: cannot create dir '%s': %s\n",
6306 progname, dname, strerror(-result));
6307 } while (!result && (dname = argv[++optind]));
6310 umask(previous_mode);
6317 static int lfs_rmentry(int argc, char **argv)
6324 fprintf(stderr, "error: %s: missing dirname\n",
6330 dname = argv[index];
6332 result = llapi_direntry_remove(dname);
6335 "error: %s: remove dir entry '%s' failed\n",
6339 dname = argv[++index];
6344 static int lfs_mv(int argc, char **argv)
6346 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6347 struct find_param param = {
6354 struct option long_opts[] = {
6355 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6356 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6357 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6360 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6362 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6365 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6368 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6370 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6376 param.fp_verbose = VERBOSE_DETAIL;
6379 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6380 progname, argv[optind - 1]);
6385 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6386 fprintf(stderr, "%s mv: MDT index must be specified\n",
6391 if (optind >= argc) {
6392 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6396 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6398 /* initialize migrate mdt parameters */
6399 param.fp_lmv_md = &lmu;
6400 param.fp_migrate = 1;
6401 rc = llapi_migrate_mdt(argv[optind], ¶m);
6403 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6404 progname, argv[optind], lmu.lum_stripe_offset,
6409 static int lfs_osts(int argc, char **argv)
6411 return lfs_tgts(argc, argv);
6414 static int lfs_mdts(int argc, char **argv)
6416 return lfs_tgts(argc, argv);
6419 static int lfs_df(int argc, char **argv)
6421 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6422 enum mntdf_flags flags = MNTDF_SHOW;
6423 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6424 int c, rc = 0, index = 0;
6425 char fsname[PATH_MAX] = "", *pool_name = NULL;
6426 struct option long_opts[] = {
6427 { .val = 'h', .name = "human-readable",
6428 .has_arg = no_argument },
6429 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6430 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6431 { .val = 'p', .name = "pool", .has_arg = required_argument },
6432 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6435 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6438 flags |= MNTDF_COOKED;
6441 flags |= MNTDF_INODES;
6444 flags |= MNTDF_LAZY;
6450 flags |= MNTDF_VERBOSE;
6456 if (optind < argc && !realpath(argv[optind], path)) {
6458 fprintf(stderr, "error: invalid path '%s': %s\n",
6459 argv[optind], strerror(-rc));
6463 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6464 /* Check if we have a mount point */
6465 if (mntdir[0] == '\0')
6468 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6469 if (rc || path[0] != '\0')
6471 fsname[0] = '\0'; /* avoid matching in next loop */
6472 mntdir[0] = '\0'; /* avoid matching in next loop */
6478 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6479 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6483 if (opt_fsname == opt_instance) { /* both true or both false */
6484 rc = llapi_getname(mntdir, buf, buflen);
6485 } else if (opt_fsname) {
6487 * llapi_search_mounts() fills @buf with fsname, but that is not
6488 * called if explicit paths are specified on the command-line
6491 rc = llapi_get_fsname(mntdir, buf, buflen);
6492 } else /* if (opt_instance) */ {
6493 rc = llapi_get_instance(mntdir, buf, buflen);
6497 fprintf(stderr, "cannot get instance for '%s': %s\n",
6498 mntdir, strerror(-rc));
6503 printf("%s %s\n", buf, mntdir);
6505 printf("%s\n", buf);
6510 static int lfs_getname(int argc, char **argv)
6512 struct option long_opts[] = {
6513 { .val = 'h', .name = "help", .has_arg = no_argument },
6514 { .val = 'i', .name = "instance", .has_arg = no_argument },
6515 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6517 bool opt_instance = false, opt_fsname = false;
6518 char fsname[PATH_MAX] = "";
6521 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6524 opt_instance = true;
6535 if (optind == argc) { /* no paths specified, get all paths. */
6536 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6539 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6540 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6541 opt_instance, opt_fsname, true);
6544 path[0] = fsname[0] = mntdir[0] = '\0';
6546 } else { /* paths specified, only attempt to search these. */
6549 /* if only one path is given, print only requested info */
6550 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6552 for (; optind < argc; optind++) {
6553 rc2 = print_instance(argv[optind], fsname,
6554 sizeof(fsname), opt_instance,
6555 opt_fsname, opt_mntdir);
6565 static int lfs_check(int argc, char **argv)
6567 char mntdir[PATH_MAX] = {'\0'};
6576 fprintf(stderr, "%s check: server type must be specified\n",
6581 obd_types[0] = obd_type1;
6582 obd_types[1] = obd_type2;
6583 obd_types[2] = obd_type3;
6585 if (strcmp(argv[1], "osts") == 0) {
6586 strcpy(obd_types[0], "osc");
6587 } else if (strcmp(argv[1], "mdts") == 0 ||
6588 strcmp(argv[1], "mds") == 0) {
6589 strcpy(obd_types[0], "mdc");
6590 } else if (strcmp(argv[1], "mgts") == 0) {
6591 strcpy(obd_types[0], "mgc");
6592 } else if (strcmp(argv[1], "all") == 0 ||
6593 strcmp(argv[1], "servers") == 0) {
6595 strcpy(obd_types[0], "osc");
6596 strcpy(obd_types[1], "mdc");
6597 strcpy(obd_types[2], "mgc");
6599 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6604 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6605 if (rc < 0 || mntdir[0] == '\0') {
6607 "%s check: cannot find mounted Lustre filesystem: %s\n",
6608 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6612 rc = llapi_target_check(num_types, obd_types, mntdir);
6614 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6615 progname, argv[1], strerror(-rc));
6620 #ifdef HAVE_SYS_QUOTA_H
6621 #define ADD_OVERFLOW(a, b) \
6622 ((((a) + (b)) < (a)) ? \
6623 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6625 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6626 * returns the value or ULONG_MAX on integer overflow or incorrect format
6628 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6629 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6630 * 3. empty integer value is interpreted as 0
6632 static unsigned long str2sec(const char *timestr)
6634 const char spec[] = "smhdw";
6635 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6636 unsigned long val = 0;
6639 if (strpbrk(timestr, spec) == NULL) {
6641 * no specifiers inside the time string,
6642 * should treat it as an integer value
6644 val = strtoul(timestr, &tail, 10);
6645 return *tail ? ULONG_MAX : val;
6648 /* format string is XXwXXdXXhXXmXXs */
6654 v = strtoul(timestr, &tail, 10);
6655 if (v == ULONG_MAX || *tail == '\0')
6657 * value too large (ULONG_MAX or more)
6658 * or missing specifier
6662 ptr = strchr(spec, *tail);
6664 /* unknown specifier */
6669 /* check if product will overflow the type */
6670 if (!(v < ULONG_MAX / mult[ind]))
6673 ADD_OVERFLOW(val, mult[ind] * v);
6674 if (val == ULONG_MAX)
6686 #define ARG2ULL(nr, str, def_units) \
6688 unsigned long long limit, units = def_units; \
6691 rc = llapi_parse_size(str, &limit, &units, 1); \
6693 fprintf(stderr, "%s: invalid limit '%s'\n", \
6700 static inline int has_times_option(int argc, char **argv)
6704 for (i = 1; i < argc; i++)
6705 if (!strcmp(argv[i], "-t"))
6711 static inline int lfs_verify_poolarg(char *pool)
6713 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6715 "Pool name '%.*s' is longer than %d\n",
6716 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6722 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6725 char *mnt, *obd_type = (char *)qctl->obd_type;
6726 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6727 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6728 struct option long_opts[] = {
6729 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6730 { .val = 'g', .name = "group", .has_arg = no_argument },
6731 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6732 { .val = 'p', .name = "projid", .has_arg = no_argument },
6733 { .val = 't', .name = "times", .has_arg = no_argument },
6734 { .val = 'u', .name = "user", .has_arg = no_argument },
6735 { .val = 'o', .name = "pool", .has_arg = required_argument },
6739 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6740 qctl->qc_type = ALLQUOTA;
6742 while ((c = getopt_long(argc, argv, "b:gi:ptuo:",
6743 long_opts, NULL)) != -1) {
6754 if (qctl->qc_type != ALLQUOTA) {
6756 "error: -u/g/p can't be used more than once\n");
6759 qctl->qc_type = qtype;
6762 if (strncmp(optarg, NOTIFY_GRACE,
6763 strlen(NOTIFY_GRACE)) == 0) {
6764 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6766 dqi->dqi_bgrace = str2sec(optarg);
6767 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6769 "error: bad block-grace: %s\n",
6774 dqb->dqb_valid |= QIF_BTIME;
6777 if (strncmp(optarg, NOTIFY_GRACE,
6778 strlen(NOTIFY_GRACE)) == 0) {
6779 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6781 dqi->dqi_igrace = str2sec(optarg);
6782 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6784 "error: bad inode-grace: %s\n",
6789 dqb->dqb_valid |= QIF_ITIME;
6791 case 't': /* Yes, of course! */
6794 if (lfs_verify_poolarg(optarg))
6797 "Trying to set grace for pool %s\n", optarg);
6798 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
6799 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
6801 /* getopt prints error message for us when opterr != 0 */
6807 if (qctl->qc_type == ALLQUOTA) {
6808 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6812 if (optind != argc - 1) {
6813 fprintf(stderr, "error: unexpected parameters encountered\n");
6818 rc = llapi_quotactl(mnt, qctl);
6821 fprintf(stderr, "%s %s ", obd_type,
6822 obd_uuid2str(&qctl->obd_uuid));
6823 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6830 #define BSLIMIT (1 << 0)
6831 #define BHLIMIT (1 << 1)
6832 #define ISLIMIT (1 << 2)
6833 #define IHLIMIT (1 << 3)
6835 int lfs_setquota(int argc, char **argv)
6838 struct if_quotactl *qctl;
6839 char *mnt, *obd_type;
6840 struct obd_dqblk *dqb;
6841 struct option long_opts[] = {
6842 { .val = 'b', .name = "block-softlimit",
6843 .has_arg = required_argument },
6844 { .val = 'B', .name = "block-hardlimit",
6845 .has_arg = required_argument },
6846 { .val = 'd', .name = "default", .has_arg = no_argument },
6847 { .val = 'g', .name = "group", .has_arg = required_argument },
6848 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6849 { .val = 'i', .name = "inode-softlimit",
6850 .has_arg = required_argument },
6851 { .val = 'I', .name = "inode-hardlimit",
6852 .has_arg = required_argument },
6853 { .val = 'p', .name = "projid", .has_arg = required_argument },
6854 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6855 { .val = 'u', .name = "user", .has_arg = required_argument },
6856 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6857 { .val = 'o', .name = "pool", .has_arg = required_argument },
6859 unsigned int limit_mask = 0;
6860 bool use_default = false;
6861 int qtype, qctl_len;
6863 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
6864 qctl = malloc(qctl_len);
6868 memset(qctl, 0, qctl_len);
6869 obd_type = (char *)qctl->obd_type;
6870 dqb = &qctl->qc_dqblk;
6872 if (has_times_option(argc, argv)) {
6873 rc = lfs_setquota_times(argc, argv, qctl);
6877 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
6878 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6879 * so it can be used as a marker that qc_type
6880 * isn't reinitialized from command line
6883 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:Uo:",
6884 long_opts, NULL)) != -1) {
6887 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6890 goto quota_type_def;
6893 rc = name2uid(&qctl->qc_id, optarg);
6896 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6899 goto quota_type_def;
6902 rc = name2gid(&qctl->qc_id, optarg);
6905 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6908 goto quota_type_def;
6911 rc = name2projid(&qctl->qc_id, optarg);
6914 if (str2quotaid(&qctl->qc_id, optarg)) {
6916 "%s setquota: invalid id '%s'\n",
6923 if (qctl->qc_id == 0) {
6925 "%s setquota: can't set quota for root usr/group/project.\n",
6932 if (qctl->qc_type != ALLQUOTA) {
6934 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
6939 qctl->qc_type = qtype;
6942 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6946 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6947 dqb->dqb_bsoftlimit >>= 10;
6948 limit_mask |= BSLIMIT;
6949 if (dqb->dqb_bsoftlimit &&
6950 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6952 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6954 (unsigned long long)dqb->dqb_bsoftlimit,
6958 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6959 dqb->dqb_bhardlimit >>= 10;
6960 limit_mask |= BHLIMIT;
6961 if (dqb->dqb_bhardlimit &&
6962 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6964 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6965 "See '%s help setquota' or Lustre manual for details\n",
6967 (unsigned long long)dqb->dqb_bhardlimit,
6971 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6972 limit_mask |= ISLIMIT;
6973 if (dqb->dqb_isoftlimit &&
6974 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6976 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6978 (unsigned long long)dqb->dqb_isoftlimit,
6982 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6983 limit_mask |= IHLIMIT;
6984 if (dqb->dqb_ihardlimit &&
6985 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6987 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6989 (unsigned long long)dqb->dqb_ihardlimit,
6993 if (lfs_verify_poolarg(optarg)) {
6998 "Trying to set quota for pool %s\n", optarg);
6999 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7000 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7004 "%s setquota: unrecognized option '%s'\n",
7005 progname, argv[optind - 1]);
7011 if (qctl->qc_type == ALLQUOTA) {
7013 "%s setquota: either -u or -g must be specified\n",
7019 if (!use_default && limit_mask == 0) {
7021 "%s setquota: at least one limit must be specified\n",
7027 if (use_default && limit_mask != 0) {
7029 "%s setquota: limits should not be specified when using default quota\n",
7035 if (use_default && qctl->qc_id == 0) {
7037 "%s setquota: can not set default quota for root user/group/project\n",
7043 if (optind != argc - 1) {
7045 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7046 progname, argv[optind]);
7054 dqb->dqb_bhardlimit = 0;
7055 dqb->dqb_bsoftlimit = 0;
7056 dqb->dqb_ihardlimit = 0;
7057 dqb->dqb_isoftlimit = 0;
7060 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7061 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7062 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7063 /* sigh, we can't just set blimits/ilimits */
7064 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7065 .qc_type = qctl->qc_type,
7066 .qc_id = qctl->qc_id};
7068 rc = llapi_quotactl(mnt, &tmp_qctl);
7072 if (!(limit_mask & BHLIMIT))
7073 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7074 if (!(limit_mask & BSLIMIT))
7075 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7076 if (!(limit_mask & IHLIMIT))
7077 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7078 if (!(limit_mask & ISLIMIT))
7079 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7081 /* Keep grace times if we have got no softlimit arguments */
7082 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7083 dqb->dqb_valid |= QIF_BTIME;
7084 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7087 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7088 dqb->dqb_valid |= QIF_ITIME;
7089 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7093 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7094 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7096 rc = llapi_quotactl(mnt, qctl);
7100 "%s setquota: cannot quotactl '%s' '%s': %s",
7102 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7109 /* Converts seconds value into format string
7110 * result is returned in buf
7112 * 1. result is in descenting order: 1w2d3h4m5s
7113 * 2. zero fields are not filled (except for p. 3): 5d1s
7114 * 3. zero seconds value is presented as "0s"
7116 static char *__sec2str(time_t seconds, char *buf)
7118 const char spec[] = "smhdw";
7119 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7124 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7125 c = seconds / mult[i];
7127 if (c > 0 || (i == 0 && buf == tail))
7128 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7137 static void sec2str(time_t seconds, char *buf, int rc)
7144 tail = __sec2str(seconds, tail);
7146 if (rc && tail - buf < 39) {
7152 static void diff2str(time_t seconds, char *buf, time_t now)
7157 if (seconds <= now) {
7158 strcpy(buf, "none");
7161 __sec2str(seconds - now, buf);
7164 static void print_quota_title(char *name, struct if_quotactl *qctl,
7165 bool human_readable, bool show_default)
7168 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7169 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7170 "Filesystem", "bquota", "blimit", "bgrace",
7171 "iquota", "ilimit", "igrace");
7173 printf("Disk quotas for %s %s (%cid %u):\n",
7174 qtype_name(qctl->qc_type), name,
7175 *qtype_name(qctl->qc_type), qctl->qc_id);
7176 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7177 "Filesystem", human_readable ? "used" : "kbytes",
7178 "quota", "limit", "grace",
7179 "files", "quota", "limit", "grace");
7183 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7186 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7189 snprintf(buf, buflen, "%5.4gP",
7190 (double)num / ((__u64)1 << 40));
7192 snprintf(buf, buflen, "%5.4gT",
7193 (double)num / (1 << 30));
7195 snprintf(buf, buflen, "%5.4gG",
7196 (double)num / (1 << 20));
7198 snprintf(buf, buflen, "%5.4gM",
7199 (double)num / (1 << 10));
7201 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7205 #define STRBUF_LEN 32
7206 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7207 int rc, bool h, bool show_default)
7213 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7214 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7215 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7216 int bover = 0, iover = 0;
7217 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7218 char numbuf[3][STRBUF_LEN];
7220 char strbuf[STRBUF_LEN];
7222 if (dqb->dqb_bhardlimit &&
7223 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7225 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7226 if (dqb->dqb_btime > now)
7232 if (dqb->dqb_ihardlimit &&
7233 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7235 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7236 if (dqb->dqb_itime > now)
7242 if (strlen(mnt) > 15)
7243 printf("%s\n%15s", mnt, "");
7245 printf("%15s", mnt);
7248 diff2str(dqb->dqb_btime, timebuf, now);
7249 else if (show_default)
7250 snprintf(timebuf, sizeof(timebuf), "%llu",
7251 (unsigned long long)dqb->dqb_btime);
7253 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7254 strbuf, sizeof(strbuf), h);
7255 if (rc == -EREMOTEIO)
7256 sprintf(numbuf[0], "%s*", strbuf);
7258 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7259 "%s" : "[%s]", strbuf);
7261 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7262 if (type == QC_GENERAL)
7263 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7264 "%s" : "[%s]", strbuf);
7266 sprintf(numbuf[1], "%s", "-");
7268 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7269 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7270 "%s" : "[%s]", strbuf);
7273 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7275 printf(" %7s%c %6s %7s %7s",
7276 numbuf[0], bover ? '*' : ' ', numbuf[1],
7277 numbuf[2], bover > 1 ? timebuf : "-");
7280 diff2str(dqb->dqb_itime, timebuf, now);
7281 else if (show_default)
7282 snprintf(timebuf, sizeof(timebuf), "%llu",
7283 (unsigned long long)dqb->dqb_itime);
7285 snprintf(numbuf[0], sizeof(numbuf),
7286 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7287 (uintmax_t)dqb->dqb_curinodes);
7289 if (type == QC_GENERAL)
7290 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7292 (uintmax_t)dqb->dqb_isoftlimit);
7294 sprintf(numbuf[1], "%s", "-");
7296 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7297 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7300 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7301 else if (type != QC_OSTIDX)
7302 printf(" %7s%c %6s %7s %7s",
7303 numbuf[0], iover ? '*' : ' ', numbuf[1],
7304 numbuf[2], iover > 1 ? timebuf : "-");
7306 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7308 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7309 qctl->qc_cmd == Q_GETOINFO) {
7313 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7314 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7316 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7317 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7318 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7320 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7322 printf("Block grace time: %s; Inode grace time: %s\n",
7323 bgtimebuf, igtimebuf);
7327 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7328 bool h, __u64 *total)
7330 int rc = 0, rc1 = 0, count = 0;
7331 __u32 valid = qctl->qc_valid;
7334 * TODO: for commands LUSTRE_Q_"S\|G"ETQUOTAPOOL we need
7335 * to go only through OSTs that belong to requested pool.
7337 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7339 fprintf(stderr, "can not get %s count: %s\n",
7340 is_mdt ? "mdt" : "ost", strerror(-rc));
7344 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
7345 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7346 rc = llapi_quotactl(mnt, qctl);
7348 /* It is remote client case. */
7349 if (rc == -EOPNOTSUPP) {
7356 fprintf(stderr, "quotactl %s%d failed.\n",
7357 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7361 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7362 qctl->qc_valid, 0, h, false);
7363 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7364 qctl->qc_dqblk.dqb_bhardlimit;
7367 qctl->qc_valid = valid;
7371 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7372 int verbose, int quiet, bool human_readable,
7375 int rc1 = 0, rc2 = 0, rc3 = 0;
7376 char *obd_type = (char *)qctl->obd_type;
7377 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7378 __u64 total_ialloc = 0, total_balloc = 0;
7379 bool use_default_for_blk = false;
7380 bool use_default_for_file = false;
7383 rc1 = llapi_quotactl(mnt, qctl);
7387 fprintf(stderr, "%s quotas are not enabled.\n",
7388 qtype_name(qctl->qc_type));
7391 fprintf(stderr, "Permission denied.\n");
7394 /* We already got error message. */
7397 fprintf(stderr, "Unexpected quotactl error: %s\n",
7402 if (!show_default && qctl->qc_id == 0) {
7403 qctl->qc_dqblk.dqb_bhardlimit = 0;
7404 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7405 qctl->qc_dqblk.dqb_ihardlimit = 0;
7406 qctl->qc_dqblk.dqb_isoftlimit = 0;
7407 qctl->qc_dqblk.dqb_btime = 0;
7408 qctl->qc_dqblk.dqb_itime = 0;
7409 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7412 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7413 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7414 use_default_for_blk = true;
7415 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7418 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7419 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7420 use_default_for_file = true;
7421 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7424 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7425 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7426 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7427 print_quota_title(name, qctl, human_readable, show_default);
7429 if (rc1 && *obd_type)
7430 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7432 if (qctl->qc_valid != QC_GENERAL)
7435 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7436 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7437 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7438 (QIF_LIMITS|QIF_USAGE));
7440 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7442 if (!show_default && verbose &&
7443 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7444 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7445 char strbuf[STRBUF_LEN];
7447 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7449 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7451 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7453 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7454 (uintmax_t)total_ialloc, strbuf);
7457 if (use_default_for_blk)
7458 printf("%cid %u is using default block quota setting\n",
7459 *qtype_name(qctl->qc_type), qctl->qc_id);
7461 if (use_default_for_file)
7462 printf("%cid %u is using default file quota setting\n",
7463 *qtype_name(qctl->qc_type), qctl->qc_id);
7465 if (rc1 || rc2 || rc3 || inacc)
7466 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7480 static int lfs_project(int argc, char **argv)
7482 int ret = 0, err = 0, c, i;
7483 struct project_handle_control phc = { 0 };
7484 enum lfs_project_ops_t op;
7487 phc.assign_projid = false;
7488 /* default action */
7489 op = LFS_PROJECT_LIST;
7491 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7494 if (op != LFS_PROJECT_LIST) {
7496 "%s: cannot specify '-c' '-C' '-s' together\n",
7501 op = LFS_PROJECT_CHECK;
7504 if (op != LFS_PROJECT_LIST) {
7506 "%s: cannot specify '-c' '-C' '-s' together\n",
7511 op = LFS_PROJECT_CLEAR;
7514 if (op != LFS_PROJECT_LIST) {
7516 "%s: cannot specify '-c' '-C' '-s' together\n",
7521 phc.set_inherit = true;
7522 op = LFS_PROJECT_SET;
7528 phc.keep_projid = true;
7531 phc.recursive = true;
7534 if (str2quotaid(&phc.projid, optarg)) {
7536 "Invalid project ID: %s\n",
7541 phc.assign_projid = true;
7545 phc.newline = false;
7548 fprintf(stderr, "%s: invalid option '%c'\n",
7554 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7555 op = LFS_PROJECT_SET;
7556 phc.set_projid = true;
7557 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7558 phc.set_projid = true;
7562 case LFS_PROJECT_CHECK:
7563 if (phc.keep_projid) {
7565 "%s: '-k' is useless together with '-c'\n",
7570 case LFS_PROJECT_CLEAR:
7573 "%s: '-0' is useless together with '-C'\n",
7577 if (phc.assign_projid) {
7579 "%s: '-p' is useless together with '-C'\n",
7584 case LFS_PROJECT_SET:
7587 "%s: '-0' is useless together with '-s'\n",
7591 if (phc.keep_projid) {
7593 "%s: '-k' is useless together with '-s'\n",
7601 "%s: '-0' is useless for list operations\n",
7611 fprintf(stderr, "%s: missing file or directory target(s)\n",
7616 for (i = 0; i < argc; i++) {
7618 case LFS_PROJECT_CHECK:
7619 err = lfs_project_check(argv[i], &phc);
7621 case LFS_PROJECT_LIST:
7622 err = lfs_project_list(argv[i], &phc);
7624 case LFS_PROJECT_CLEAR:
7625 err = lfs_project_clear(argv[i], &phc);
7627 case LFS_PROJECT_SET:
7628 err = lfs_project_set(argv[i], &phc);
7640 static int lfs_quota(int argc, char **argv)
7643 char *mnt, *name = NULL;
7644 struct if_quotactl *qctl;
7646 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7647 __u32 valid = QC_GENERAL, idx = 0;
7648 bool human_readable = false;
7649 bool show_default = false;
7651 struct option long_opts[] = {
7652 { .val = 1, .name = "pool", .has_arg = required_argument },
7655 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7659 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7660 qctl->qc_type = ALLQUOTA;
7661 obd_uuid = (char *)qctl->obd_uuid.uuid;
7663 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7664 long_opts, NULL)) != -1) {
7667 show_default = true;
7672 show_default = true;
7677 show_default = true;
7681 if (qctl->qc_type != ALLQUOTA) {
7683 "%s quota: only one of -u, -g, or -p may be specified\n",
7688 qctl->qc_type = qtype;
7691 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7694 valid = qctl->qc_valid = QC_UUID;
7695 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
7698 valid = qctl->qc_valid = QC_MDTIDX;
7699 idx = qctl->qc_idx = atoi(optarg);
7700 if (idx == 0 && *optarg != '0') {
7702 "%s quota: invalid MDT index '%s'\n",
7709 valid = qctl->qc_valid = QC_OSTIDX;
7710 idx = qctl->qc_idx = atoi(optarg);
7711 if (idx == 0 && *optarg != '0') {
7713 "%s quota: invalid OST index '%s'\n",
7726 human_readable = true;
7729 if (lfs_verify_poolarg(optarg)) {
7733 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7734 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
7735 LUSTRE_Q_GETINFOPOOL :
7736 LUSTRE_Q_GETQUOTAPOOL;
7739 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7740 progname, argv[optind - 1]);
7746 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7747 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7748 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7749 qctl->qc_type == ALLQUOTA &&
7750 optind == argc - 1 && !show_default) {
7753 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7754 qctl->qc_type = qtype;
7755 qctl->qc_valid = valid;
7756 if (qtype == USRQUOTA) {
7757 qctl->qc_id = geteuid();
7758 rc = uid2name(&name, qctl->qc_id);
7760 qctl->qc_id = getegid();
7761 rc = gid2name(&name, qctl->qc_id);
7762 memset(&qctl->qc_dqblk, 0,
7763 sizeof(qctl->qc_dqblk));
7768 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
7769 human_readable, show_default);
7774 /* lfs quota -u username /path/to/lustre/mount */
7775 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7776 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7777 /* options should be followed by u/g-name and mntpoint */
7778 if ((!show_default && optind + 2 != argc) ||
7779 (show_default && optind + 1 != argc) ||
7780 qctl->qc_type == ALLQUOTA) {
7782 "%s quota: name and mount point must be specified\n",
7788 if (!show_default) {
7789 name = argv[optind++];
7790 switch (qctl->qc_type) {
7792 rc = name2uid(&qctl->qc_id, name);
7795 rc = name2gid(&qctl->qc_id, name);
7798 rc = name2projid(&qctl->qc_id, name);
7805 qctl->qc_valid = QC_GENERAL;
7806 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
7811 if (str2quotaid(&qctl->qc_id, name)) {
7812 fprintf(stderr, "%s quota: invalid id '%s'\n",
7818 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
7819 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7826 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
7827 human_readable, show_default);
7832 #endif /* HAVE_SYS_QUOTA_H! */
7834 static int flushctx_ioctl(char *mp)
7838 fd = open(mp, O_RDONLY);
7840 fprintf(stderr, "flushctx: error open %s: %s\n",
7841 mp, strerror(errno));
7845 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7847 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7848 mp, strerror(errno));
7854 static int lfs_flushctx(int argc, char **argv)
7856 int kdestroy = 0, c;
7857 char mntdir[PATH_MAX] = {'\0'};
7861 while ((c = getopt(argc, argv, "k")) != -1) {
7868 "error: %s: option '-%c' unrecognized\n",
7875 rc = system("kdestroy > /dev/null");
7877 rc = WEXITSTATUS(rc);
7879 "error destroying tickets: %d, continuing\n",
7884 if (optind >= argc) {
7885 /* flush for all mounted lustre fs. */
7886 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7887 /* Check if we have a mount point */
7888 if (mntdir[0] == '\0')
7891 if (flushctx_ioctl(mntdir))
7894 mntdir[0] = '\0'; /* avoid matching in next loop */
7897 /* flush fs as specified */
7898 while (optind < argc) {
7899 if (flushctx_ioctl(argv[optind++]))
7906 static int lfs_changelog(int argc, char **argv)
7908 void *changelog_priv;
7909 struct changelog_rec *rec;
7910 long long startrec = 0, endrec = 0;
7912 struct option long_opts[] = {
7913 { .val = 'f', .name = "follow", .has_arg = no_argument },
7915 char short_opts[] = "f";
7918 while ((rc = getopt_long(argc, argv, short_opts,
7919 long_opts, NULL)) != -1) {
7926 "%s changelog: unrecognized option '%s'\n",
7927 progname, argv[optind - 1]);
7931 if (optind >= argc) {
7932 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7937 mdd = argv[optind++];
7939 startrec = strtoll(argv[optind++], NULL, 10);
7941 endrec = strtoll(argv[optind++], NULL, 10);
7943 rc = llapi_changelog_start(&changelog_priv,
7944 CHANGELOG_FLAG_BLOCK |
7945 CHANGELOG_FLAG_JOBID |
7946 CHANGELOG_FLAG_EXTRA_FLAGS |
7947 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7950 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7951 progname, strerror(errno = -rc));
7955 rc = llapi_changelog_set_xflags(changelog_priv,
7956 CHANGELOG_EXTRA_FLAG_UIDGID |
7957 CHANGELOG_EXTRA_FLAG_NID |
7958 CHANGELOG_EXTRA_FLAG_OMODE |
7959 CHANGELOG_EXTRA_FLAG_XATTR);
7962 "%s changelog: cannot set xflags for changelog: %s\n",
7963 progname, strerror(errno = -rc));
7967 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7971 if (endrec && rec->cr_index > endrec) {
7972 llapi_changelog_free(&rec);
7975 if (rec->cr_index < startrec) {
7976 llapi_changelog_free(&rec);
7980 secs = rec->cr_time >> 30;
7981 gmtime_r(&secs, &ts);
7982 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7983 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7984 changelog_type2str(rec->cr_type),
7985 ts.tm_hour, ts.tm_min, ts.tm_sec,
7986 (int)(rec->cr_time & ((1 << 30) - 1)),
7987 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7988 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7990 if (rec->cr_flags & CLF_JOBID) {
7991 struct changelog_ext_jobid *jid =
7992 changelog_rec_jobid(rec);
7994 if (jid->cr_jobid[0] != '\0')
7995 printf(" j=%s", jid->cr_jobid);
7998 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7999 struct changelog_ext_extra_flags *ef =
8000 changelog_rec_extra_flags(rec);
8002 printf(" ef=0x%llx",
8003 (unsigned long long)ef->cr_extra_flags);
8005 if (ef->cr_extra_flags & CLFE_UIDGID) {
8006 struct changelog_ext_uidgid *uidgid =
8007 changelog_rec_uidgid(rec);
8009 printf(" u=%llu:%llu",
8010 (unsigned long long)uidgid->cr_uid,
8011 (unsigned long long)uidgid->cr_gid);
8013 if (ef->cr_extra_flags & CLFE_NID) {
8014 struct changelog_ext_nid *nid =
8015 changelog_rec_nid(rec);
8018 libcfs_nid2str(nid->cr_nid));
8021 if (ef->cr_extra_flags & CLFE_OPEN) {
8022 struct changelog_ext_openmode *omd =
8023 changelog_rec_openmode(rec);
8024 char mode[] = "---";
8026 /* exec mode must be exclusive */
8027 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8030 if (omd->cr_openflags & MDS_FMODE_READ)
8032 if (omd->cr_openflags &
8039 if (strcmp(mode, "---") != 0)
8040 printf(" m=%s", mode);
8043 if (ef->cr_extra_flags & CLFE_XATTR) {
8044 struct changelog_ext_xattr *xattr =
8045 changelog_rec_xattr(rec);
8047 if (xattr->cr_xattr[0] != '\0')
8048 printf(" x=%s", xattr->cr_xattr);
8052 if (rec->cr_namelen)
8053 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
8054 rec->cr_namelen, changelog_rec_name(rec));
8056 if (rec->cr_flags & CLF_RENAME) {
8057 struct changelog_ext_rename *rnm =
8058 changelog_rec_rename(rec);
8060 if (!fid_is_zero(&rnm->cr_sfid))
8061 printf(" s="DFID" sp="DFID" %.*s",
8062 PFID(&rnm->cr_sfid),
8063 PFID(&rnm->cr_spfid),
8064 (int)changelog_rec_snamelen(rec),
8065 changelog_rec_sname(rec));
8069 llapi_changelog_free(&rec);
8072 llapi_changelog_fini(&changelog_priv);
8075 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8076 progname, strerror(errno = -rc));
8078 return (rc == 1 ? 0 : rc);
8081 static int lfs_changelog_clear(int argc, char **argv)
8089 endrec = strtoll(argv[3], NULL, 10);
8091 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8094 fprintf(stderr, "%s: record out of range: %llu\n",
8096 else if (rc == -ENOENT)
8097 fprintf(stderr, "%s: no changelog user: %s\n",
8100 fprintf(stderr, "%s error: %s\n", argv[0],
8109 static int lfs_fid2path(int argc, char **argv)
8111 struct option long_opts[] = {
8112 { .val = 'c', .name = "cur", .has_arg = no_argument },
8113 { .val = 'l', .name = "link", .has_arg = required_argument },
8114 { .val = 'r', .name = "rec", .has_arg = required_argument },
8116 char short_opts[] = "cl:r:";
8117 char mntdir[PATH_MAX];
8118 char *device, *fid, *path, *rootpath;
8119 long long recno = -1;
8124 char *endptr = NULL;
8126 while ((rc = getopt_long(argc, argv, short_opts,
8127 long_opts, NULL)) != -1) {
8133 linkno = strtol(optarg, &endptr, 10);
8134 if (*endptr != '\0') {
8136 "%s fid2path: invalid linkno '%s'\n",
8142 recno = strtoll(optarg, &endptr, 10);
8143 if (*endptr != '\0') {
8145 "%s fid2path: invalid recno '%s'\n",
8152 "%s fid2path: unrecognized option '%s'\n",
8153 progname, argv[optind - 1]);
8160 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
8165 device = argv[optind++];
8166 path = calloc(1, PATH_MAX);
8170 "%s fid2path: cannot allocate memory for path: %s\n",
8171 progname, strerror(-rc));
8176 /* in case that device is not the mountpoint */
8177 if (device[0] == '/') {
8178 rc = llapi_search_mounts(device, 0, mntdir, NULL);
8183 "%s fid2path: %s has no mountpoint: %s\n",
8184 progname, device, strerror(-rc));
8188 while (optind < argc) {
8189 fid = argv[optind++];
8191 lnktmp = (linkno >= 0) ? linkno : 0;
8193 int oldtmp = lnktmp;
8194 long long rectmp = recno;
8197 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
8201 "%s fid2path: cannot find %s %s: %s\n",
8202 progname, device, fid,
8203 strerror(errno = -rc2));
8210 fprintf(stdout, "%lld ", rectmp);
8211 if (device[0] == '/') {
8212 fprintf(stdout, "%s", rootpath);
8213 if (rootpath[strlen(rootpath) - 1] != '/')
8214 fprintf(stdout, "/");
8215 } else if (path[0] == '\0') {
8216 fprintf(stdout, "/");
8218 fprintf(stdout, "%s\n", path);
8221 /* specified linkno */
8223 if (oldtmp == lnktmp)
8233 static int lfs_path2fid(int argc, char **argv)
8235 struct option long_opts[] = {
8236 { .val = 'p', .name = "parents", .has_arg = no_argument },
8239 const char short_opts[] = "p";
8240 const char *sep = "";
8243 bool show_parents = false;
8245 while ((rc = getopt_long(argc, argv, short_opts,
8246 long_opts, NULL)) != -1) {
8249 show_parents = true;
8253 "%s path2fid: unrecognized option '%s'\n",
8254 progname, argv[optind - 1]);
8259 if (optind > argc - 1) {
8260 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8263 } else if (optind < argc - 1) {
8268 for (path = argv + optind; *path != NULL; path++) {
8271 if (!show_parents) {
8272 err = llapi_path2fid(*path, &fid);
8274 printf("%s%s"DFID"\n",
8275 *sep != '\0' ? *path : "", sep,
8278 char name[NAME_MAX + 1];
8279 unsigned int linkno = 0;
8281 while ((err = llapi_path2parent(*path, linkno, &fid,
8282 name, sizeof(name))) == 0) {
8283 if (*sep != '\0' && linkno == 0)
8284 printf("%s%s", *path, sep);
8286 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8291 /* err == -ENODATA is end-of-loop */
8292 if (linkno > 0 && err == -ENODATA) {
8300 "%s path2fid: cannot get %sfid for '%s': %s\n",
8301 progname, show_parents ? "parent " : "", *path,
8313 #define MAX_ERRNO 4095
8314 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8316 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8320 rc = llapi_rmfid(device, fa);
8322 fprintf(stderr, "rmfid(): rc = %d\n", rc);
8326 for (k = 0; k < fa->fa_nr; k++) {
8327 rc = (__s32)fa->fa_fids[k].f_ver;
8328 if (!IS_ERR_VALUE(rc))
8334 fa->fa_fids[k].f_ver = 0;
8335 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
8336 PFID(&fa->fa_fids[k]), rc);
8342 static int lfs_rmfid(int argc, char **argv)
8344 char *fidstr, *device;
8345 int rc = 0, rc2, nr;
8346 struct fid_array *fa;
8348 if (optind > argc - 1) {
8349 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8353 device = argv[optind++];
8356 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8362 while (optind < argc) {
8365 fidstr = argv[optind++];
8366 while (*fidstr == '[')
8368 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8370 fprintf(stderr, "unrecognized FID: %s\n",
8375 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8376 /* start another batch */
8377 rc2 = lfs_rmfid_and_show_errors(device, fa);
8384 rc2 = lfs_rmfid_and_show_errors(device, fa);
8392 static int lfs_data_version(int argc, char **argv)
8399 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8402 fprintf(stderr, "%s data_version: FILE must be specified\n",
8407 while ((c = getopt(argc, argv, "nrw")) != -1) {
8410 data_version_flags = 0;
8413 data_version_flags |= LL_DV_RD_FLUSH;
8416 data_version_flags |= LL_DV_WR_FLUSH;
8420 "%s data_version: unrecognized option '%s'\n",
8421 progname, argv[optind - 1]);
8425 if (optind == argc) {
8426 fprintf(stderr, "%s data_version: FILE must be specified\n",
8431 path = argv[optind];
8432 fd = open(path, O_RDONLY);
8435 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8436 progname, path, strerror(-rc));
8440 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8443 "%s data_version: cannot get version for '%s': %s\n",
8444 progname, path, strerror(-rc));
8446 printf("%ju" "\n", (uintmax_t)data_version);
8452 static int lfs_hsm_state(int argc, char **argv)
8457 struct hsm_user_state hus;
8465 rc = llapi_hsm_state_get(path, &hus);
8467 fprintf(stderr, "can't get hsm state for %s: %s\n",
8468 path, strerror(errno = -rc));
8472 /* Display path name and status flags */
8473 printf("%s: (0x%08x)", path, hus.hus_states);
8475 if (hus.hus_states & HS_RELEASED)
8476 printf(" released");
8477 if (hus.hus_states & HS_EXISTS)
8479 if (hus.hus_states & HS_DIRTY)
8481 if (hus.hus_states & HS_ARCHIVED)
8482 printf(" archived");
8483 /* Display user-settable flags */
8484 if (hus.hus_states & HS_NORELEASE)
8485 printf(" never_release");
8486 if (hus.hus_states & HS_NOARCHIVE)
8487 printf(" never_archive");
8488 if (hus.hus_states & HS_LOST)
8489 printf(" lost_from_hsm");
8491 if (hus.hus_archive_id != 0)
8492 printf(", archive_id:%d", hus.hus_archive_id);
8495 } while (++i < argc);
8500 #define LFS_HSM_SET 0
8501 #define LFS_HSM_CLEAR 1
8504 * Generic function to set or clear HSM flags.
8505 * Used by hsm_set and hsm_clear.
8507 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8509 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8511 struct option long_opts[] = {
8512 { .val = 'A', .name = "archived", .has_arg = no_argument },
8513 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8514 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8515 { .val = 'e', .name = "exists", .has_arg = no_argument },
8516 { .val = 'l', .name = "lost", .has_arg = no_argument },
8517 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8518 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8520 char short_opts[] = "lraAdei:";
8524 __u32 archive_id = 0;
8530 while ((c = getopt_long(argc, argv, short_opts,
8531 long_opts, NULL)) != -1) {
8537 mask |= HS_NOARCHIVE;
8540 mask |= HS_ARCHIVED;
8543 mask |= HS_NORELEASE;
8552 archive_id = strtol(optarg, &end, 10);
8554 fprintf(stderr, "invalid archive_id: '%s'\n",
8562 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8563 argv[0], argv[optind - 1]);
8568 /* User should have specified a flag */
8572 while (optind < argc) {
8573 path = argv[optind];
8575 /* If mode == 0, this means we apply the mask. */
8576 if (mode == LFS_HSM_SET)
8577 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8579 rc = llapi_hsm_state_set(path, 0, mask, 0);
8582 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8583 path, strerror(errno = -rc));
8592 static int lfs_hsm_action(int argc, char **argv)
8597 struct hsm_current_action hca;
8598 struct hsm_extent he;
8599 enum hsm_user_action hua;
8600 enum hsm_progress_states hps;
8608 rc = llapi_hsm_current_action(path, &hca);
8610 fprintf(stderr, "can't get hsm action for %s: %s\n",
8611 path, strerror(errno = -rc));
8614 he = hca.hca_location;
8615 hua = hca.hca_action;
8616 hps = hca.hca_state;
8618 printf("%s: %s", path, hsm_user_action2name(hua));
8620 /* Skip file without action */
8621 if (hca.hca_action == HUA_NONE) {
8626 printf(" %s ", hsm_progress_state2name(hps));
8628 if ((hps == HPS_RUNNING) &&
8629 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8630 printf("(%llu bytes moved)\n",
8631 (unsigned long long)he.length);
8632 else if ((he.offset + he.length) == LUSTRE_EOF)
8633 printf("(from %llu to EOF)\n",
8634 (unsigned long long)he.offset);
8636 printf("(from %llu to %llu)\n",
8637 (unsigned long long)he.offset,
8638 (unsigned long long)(he.offset + he.length));
8640 } while (++i < argc);
8645 static int lfs_hsm_set(int argc, char **argv)
8647 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8650 static int lfs_hsm_clear(int argc, char **argv)
8652 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8656 * Check file state and return its fid, to be used by lfs_hsm_request().
8658 * \param[in] file Path to file to check
8659 * \param[in,out] fid Pointer to allocated lu_fid struct.
8660 * \param[in,out] last_dev Pointer to last device id used.
8662 * \return 0 on success.
8664 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8670 rc = lstat(file, &st);
8672 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8676 * Checking for regular file as archiving as posix copytool
8677 * rejects archiving files other than regular files
8679 if (!S_ISREG(st.st_mode)) {
8680 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8683 /* A request should be ... */
8684 if (*last_dev != st.st_dev && *last_dev != 0) {
8686 "All files should be on the same filesystem: %s\n",
8690 *last_dev = st.st_dev;
8692 rc = llapi_path2fid(file, fid);
8694 fprintf(stderr, "Cannot read FID of %s: %s\n",
8695 file, strerror(-rc));
8701 /* Fill an HSM HUR item with a given file name.
8703 * If mntpath is set, then the filename is actually a FID, and no
8704 * lookup on the filesystem will be performed.
8706 * \param[in] hur the user request to fill
8707 * \param[in] idx index of the item inside the HUR to fill
8708 * \param[in] mntpath mountpoint of Lustre
8709 * \param[in] fname filename (if mtnpath is NULL)
8710 * or FID (if mntpath is set)
8711 * \param[in] last_dev pointer to last device id used
8713 * \retval 0 on success
8714 * \retval CMD_HELP or a negative errno on error
8716 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8717 const char *mntpath, const char *fname,
8720 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8723 hui->hui_extent.length = -1;
8726 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8728 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8731 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8735 hur->hur_request.hr_itemcount++;
8740 static int lfs_hsm_request(int argc, char **argv, int action)
8742 struct option long_opts[] = {
8743 { .val = 'a', .name = "archive", .has_arg = required_argument },
8744 { .val = 'D', .name = "data", .has_arg = required_argument },
8745 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8746 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8749 char short_opts[] = "l:D:a:m:";
8750 struct hsm_user_request *hur, *oldhur;
8755 char *filelist = NULL;
8756 char fullpath[PATH_MAX];
8757 char *opaque = NULL;
8761 int nbfile_alloc = 0;
8762 char *some_file = NULL;
8763 char *mntpath = NULL;
8769 while ((c = getopt_long(argc, argv, short_opts,
8770 long_opts, NULL)) != -1) {
8779 if (action != HUA_ARCHIVE &&
8780 action != HUA_REMOVE) {
8782 "error: -a is supported only when archiving or removing\n");
8785 archive_id = atoi(optarg);
8790 some_file = strdup(optarg);
8796 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8797 argv[0], argv[optind - 1]);
8802 /* All remaining args are files, so we have at least nbfile */
8803 nbfile = argc - optind;
8805 if ((nbfile == 0) && (!filelist))
8809 opaque_len = strlen(opaque);
8812 * Alloc the request structure with enough place to store all files
8813 * from command line.
8815 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8817 fprintf(stderr, "Cannot create the request: %s\n",
8821 nbfile_alloc = nbfile;
8823 hur->hur_request.hr_action = action;
8824 hur->hur_request.hr_archive_id = archive_id;
8825 hur->hur_request.hr_flags = 0;
8827 /* All remaining args are files, add them */
8828 if (nbfile != 0 && some_file == NULL)
8829 some_file = strdup(argv[optind]);
8831 for (i = 0; i < nbfile; i++) {
8832 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8838 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8840 /* If a filelist was specified, read the filelist from it. */
8842 fp = fopen(filelist, "r");
8844 fprintf(stderr, "Cannot read the file list %s: %s\n",
8845 filelist, strerror(errno));
8850 while ((rc = getline(&line, &len, fp)) != -1) {
8852 * If allocated buffer was too small, get something
8855 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8858 nbfile_alloc = nbfile_alloc * 2 + 1;
8860 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8864 "hsm: cannot allocate the request: %s\n",
8871 size = hur_len(oldhur);
8874 "hsm: cannot allocate %u files + %u bytes data\n",
8875 oldhur->hur_request.hr_itemcount,
8876 oldhur->hur_request.hr_data_len);
8883 memcpy(hur, oldhur, size);
8888 if (line[strlen(line) - 1] == '\n')
8889 line[strlen(line) - 1] = '\0';
8891 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8892 mntpath, line, &last_dev);
8908 /* If a --data was used, add it to the request */
8909 hur->hur_request.hr_data_len = opaque_len;
8911 memcpy(hur_data(hur), opaque, opaque_len);
8913 /* Send the HSM request */
8914 if (realpath(some_file, fullpath) == NULL) {
8915 fprintf(stderr, "Could not find path '%s': %s\n",
8916 some_file, strerror(errno));
8918 rc = llapi_hsm_request(fullpath, hur);
8920 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8921 some_file, strerror(-rc));
8931 static int lfs_hsm_archive(int argc, char **argv)
8933 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8936 static int lfs_hsm_restore(int argc, char **argv)
8938 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8941 static int lfs_hsm_release(int argc, char **argv)
8943 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8946 static int lfs_hsm_remove(int argc, char **argv)
8948 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8951 static int lfs_hsm_cancel(int argc, char **argv)
8953 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8956 static int lfs_swap_layouts(int argc, char **argv)
8961 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8962 SWAP_LAYOUTS_KEEP_MTIME |
8963 SWAP_LAYOUTS_KEEP_ATIME);
8966 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8968 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8970 int lfs_get_mode(const char *string)
8972 enum lock_mode_user mode;
8974 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8975 if (lock_mode_names[mode] == NULL)
8977 if (strcmp(string, lock_mode_names[mode]) == 0)
8984 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8986 enum lu_ladvise_type advice;
8989 advice < ARRAY_SIZE(ladvise_names); advice++) {
8990 if (ladvise_names[advice] == NULL)
8992 if (strcmp(string, ladvise_names[advice]) == 0)
8996 return LU_LADVISE_INVALID;
8999 static int lfs_ladvise(int argc, char **argv)
9001 struct option long_opts[] = {
9002 { .val = 'a', .name = "advice", .has_arg = required_argument },
9003 { .val = 'b', .name = "background", .has_arg = no_argument },
9004 { .val = 'e', .name = "end", .has_arg = required_argument },
9005 { .val = 'l', .name = "length", .has_arg = required_argument },
9006 { .val = 'm', .name = "mode", .has_arg = required_argument },
9007 { .val = 's', .name = "start", .has_arg = required_argument },
9008 { .val = 'u', .name = "unset", .has_arg = no_argument },
9010 char short_opts[] = "a:be:l:m:s:u";
9015 struct llapi_lu_ladvise advice;
9016 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9017 unsigned long long start = 0;
9018 unsigned long long end = LUSTRE_EOF;
9019 unsigned long long length = 0;
9020 unsigned long long size_units;
9021 unsigned long long flags = 0;
9025 while ((c = getopt_long(argc, argv, short_opts,
9026 long_opts, NULL)) != -1) {
9029 advice_type = lfs_get_ladvice(optarg);
9030 if (advice_type == LU_LADVISE_INVALID) {
9032 "%s: invalid advice type '%s'\n",
9034 fprintf(stderr, "Valid types:");
9036 for (advice_type = 0;
9037 advice_type < ARRAY_SIZE(ladvise_names);
9039 if (ladvise_names[advice_type] == NULL)
9041 fprintf(stderr, " %s",
9042 ladvise_names[advice_type]);
9044 fprintf(stderr, "\n");
9057 rc = llapi_parse_size(optarg, &end,
9060 fprintf(stderr, "%s: bad end offset '%s'\n",
9067 rc = llapi_parse_size(optarg, &start,
9071 "%s: bad start offset '%s'\n",
9078 rc = llapi_parse_size(optarg, &length,
9081 fprintf(stderr, "%s: bad length '%s'\n",
9087 mode = lfs_get_mode(optarg);
9090 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9098 fprintf(stderr, "%s: option '%s' unrecognized\n",
9099 argv[0], argv[optind - 1]);
9104 if (advice_type == LU_LADVISE_INVALID) {
9105 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9106 fprintf(stderr, "Valid types:");
9107 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9109 if (ladvise_names[advice_type] == NULL)
9111 fprintf(stderr, " %s", ladvise_names[advice_type]);
9113 fprintf(stderr, "\n");
9117 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9119 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9124 if (argc <= optind) {
9125 fprintf(stderr, "%s: please give one or more file names\n",
9130 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9131 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9136 if (end == LUSTRE_EOF && length != 0)
9137 end = start + length;
9140 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9141 argv[0], start, end);
9145 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9146 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9151 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9152 fprintf(stderr, "%s: mode is required with lockahead\n",
9157 while (optind < argc) {
9160 path = argv[optind++];
9162 fd = open(path, O_RDONLY);
9164 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9165 argv[0], path, strerror(errno));
9170 advice.lla_start = start;
9171 advice.lla_end = end;
9172 advice.lla_advice = advice_type;
9173 advice.lla_value1 = 0;
9174 advice.lla_value2 = 0;
9175 advice.lla_value3 = 0;
9176 advice.lla_value4 = 0;
9177 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9178 advice.lla_lockahead_mode = mode;
9179 advice.lla_peradvice_flags = flags;
9182 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9186 "%s: cannot give advice '%s' to file '%s': %s\n",
9187 argv[0], ladvise_names[advice_type],
9188 path, strerror(errno));
9194 if (rc == 0 && rc2 < 0)
9200 static const char *const heat_names[] = LU_HEAT_NAMES;
9202 static int lfs_heat_get(int argc, char **argv)
9204 struct lu_heat *heat;
9213 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9215 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9220 while (optind < argc) {
9221 path = argv[optind++];
9223 fd = open(path, O_RDONLY);
9225 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9226 argv[0], path, strerror(errno));
9231 heat->lh_count = OBD_HEAT_COUNT;
9232 rc2 = llapi_heat_get(fd, heat);
9236 "%s: cannot get heat of file '%s': %s\n",
9237 argv[0], path, strerror(errno));
9241 printf("flags: %x\n", heat->lh_flags);
9242 for (i = 0; i < heat->lh_count; i++)
9243 printf("%s: %llu\n", heat_names[i],
9244 (unsigned long long)heat->lh_heat[i]);
9246 if (rc == 0 && rc2 < 0)
9254 static int lfs_heat_set(int argc, char **argv)
9256 struct option long_opts[] = {
9257 { .val = 'c', .name = "clear", .has_arg = no_argument },
9258 { .val = 'o', .name = "off", .has_arg = no_argument },
9259 { .val = 'O', .name = "on", .has_arg = no_argument },
9261 enum lu_heat_flag flags = 0;
9271 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9274 flags |= LU_HEAT_FLAG_CLEAR;
9277 flags |= LU_HEAT_FLAG_CLEAR;
9278 flags |= LU_HEAT_FLAG_OFF;
9281 flags &= ~LU_HEAT_FLAG_OFF;
9286 fprintf(stderr, "%s: option '%s' unrecognized\n",
9287 argv[0], argv[optind - 1]);
9292 if (argc <= optind) {
9293 fprintf(stderr, "%s: please give one or more file names\n",
9298 while (optind < argc) {
9299 path = argv[optind++];
9301 fd = open(path, O_RDONLY);
9303 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9304 argv[0], path, strerror(errno));
9309 rc2 = llapi_heat_set(fd, flags);
9313 "%s: cannot setflags heat of file '%s': %s\n",
9314 argv[0], path, strerror(errno));
9318 if (rc == 0 && rc2 < 0)
9325 * The input string contains a comma delimited list of component ids and
9326 * ranges, for example "1,2-4,7".
9328 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9330 bool end_of_loop = false;
9338 while (!end_of_loop) {
9342 char *endptr = NULL;
9345 ptr = strchrnul(arg, ',');
9346 end_of_loop = *ptr == '\0';
9349 start_index = strtol(arg, &endptr, 0);
9350 if (endptr == arg) /* no data at all */
9352 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9354 if (start_index < 0)
9357 end_index = start_index;
9358 if (*endptr == '-') {
9359 end_index = strtol(endptr + 1, &endptr, 0);
9360 if (*endptr != '\0')
9362 if (end_index < start_index)
9366 for (i = start_index; i <= end_index && size > 0; i++) {
9369 /* remove duplicate */
9370 for (j = 0; j < nr; j++) {
9374 if (j == nr) { /* no duplicate */
9380 if (size == 0 && i < end_index)
9387 if (!end_of_loop && ptr)
9390 return rc < 0 ? rc : nr;
9394 * struct verify_mirror_id - Mirror id to be verified.
9395 * @mirror_id: A specified mirror id.
9396 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9398 struct verify_mirror_id {
9404 * compare_mirror_ids() - Compare mirror ids.
9405 * @layout: Mirror component list.
9406 * @cbdata: Callback data in verify_mirror_id structure.
9408 * This is a callback function called by llapi_layout_comp_iterate()
9409 * to compare the specified mirror id with the one in the current
9410 * component of @layout. If they are the same, then the specified
9411 * mirror id is valid.
9413 * Return: a negative error code on failure or
9414 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9415 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9418 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9420 struct verify_mirror_id *mirror_id_cbdata =
9421 (struct verify_mirror_id *)cbdata;
9425 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9429 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9430 progname, strerror(errno));
9434 if (mirror_id_cbdata->mirror_id == mirror_id) {
9435 mirror_id_cbdata->is_valid_id = true;
9436 return LLAPI_LAYOUT_ITER_STOP;
9439 return LLAPI_LAYOUT_ITER_CONT;
9443 * verify_mirror_ids() - Verify specified mirror ids.
9444 * @fname: Mirrored file name.
9445 * @mirror_ids: Specified mirror ids to be verified.
9446 * @ids_nr: Number of specified mirror ids.
9448 * This function verifies that specified @mirror_ids are valid
9449 * in the mirrored file @fname.
9451 * Return: 0 on success or a negative error code on failure.
9454 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9456 struct llapi_layout *layout = NULL;
9457 struct verify_mirror_id mirror_id_cbdata = { 0 };
9468 if (stat(fname, &stbuf) < 0) {
9469 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9470 progname, fname, strerror(errno));
9475 if (!S_ISREG(stbuf.st_mode)) {
9476 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9482 fd = open(fname, O_DIRECT | O_RDONLY);
9484 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9485 progname, fname, strerror(errno));
9490 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9492 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9493 progname, fname, strerror(errno));
9497 layout = llapi_layout_get_by_fd(fd, 0);
9499 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9500 progname, fname, strerror(errno));
9502 llapi_lease_release(fd);
9506 rc = llapi_layout_flags_get(layout, &flr_state);
9508 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9509 progname, fname, strerror(errno));
9514 flr_state &= LCM_FL_FLR_MASK;
9515 switch (flr_state) {
9518 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9519 progname, fname, llapi_layout_flags_string(flr_state));
9526 for (i = 0; i < ids_nr; i++) {
9527 mirror_id_cbdata.mirror_id = mirror_ids[i];
9528 mirror_id_cbdata.is_valid_id = false;
9530 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9535 "%s: '%s' failed to verify mirror id: %u.\n",
9536 progname, fname, mirror_ids[i]);
9540 if (!mirror_id_cbdata.is_valid_id) {
9543 "%s: '%s' invalid specified mirror id: %u.\n",
9544 progname, fname, mirror_ids[i]);
9550 llapi_layout_free(layout);
9551 llapi_lease_release(fd);
9559 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9560 __u16 *mirror_ids, int ids_nr)
9562 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9563 struct llapi_layout *layout;
9574 if (stat(fname, &stbuf) < 0) {
9575 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9576 progname, fname, strerror(errno));
9580 if (!S_ISREG(stbuf.st_mode)) {
9581 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9587 fd = open(fname, O_DIRECT | O_RDWR);
9589 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9590 progname, fname, strerror(errno));
9595 layout = llapi_layout_get_by_fd(fd, 0);
9597 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9598 progname, fname, strerror(errno));
9603 rc = llapi_layout_flags_get(layout, &flr_state);
9605 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9606 progname, fname, strerror(errno));
9611 flr_state &= LCM_FL_FLR_MASK;
9612 if (flr_state == LCM_FL_NONE) {
9614 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9619 /* get stale component info */
9620 comp_size = llapi_mirror_find_stale(layout, comp_array,
9621 ARRAY_SIZE(comp_array),
9622 mirror_ids, ids_nr);
9623 if (comp_size <= 0) {
9628 ioc->lil_mode = LL_LEASE_WRLCK;
9629 ioc->lil_flags = LL_LEASE_RESYNC;
9630 rc = llapi_lease_set(fd, ioc);
9632 if (rc == -EALREADY)
9636 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9637 progname, fname, strerror(-rc));
9641 /* get the read range [start, end) */
9642 start = comp_array[0].lrc_start;
9643 end = comp_array[0].lrc_end;
9644 for (idx = 1; idx < comp_size; idx++) {
9645 if (comp_array[idx].lrc_start < start)
9646 start = comp_array[idx].lrc_start;
9647 if (end < comp_array[idx].lrc_end)
9648 end = comp_array[idx].lrc_end;
9651 rc = llapi_lease_check(fd);
9652 if (rc != LL_LEASE_WRLCK) {
9653 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9658 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9661 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9662 progname, fname, strerror(-rc));
9664 /* need to do the lease unlock even resync fails */
9665 ioc->lil_mode = LL_LEASE_UNLCK;
9666 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9668 for (idx = 0; idx < comp_size; idx++) {
9669 if (comp_array[idx].lrc_synced) {
9670 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9675 rc2 = llapi_lease_set(fd, ioc);
9677 * llapi_lease_set returns lease mode when it request to unlock
9681 /* rc2 == 0 means lost lease lock */
9682 if (rc2 == 0 && rc == 0)
9684 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9686 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9690 llapi_layout_free(layout);
9697 static inline int lfs_mirror_resync(int argc, char **argv)
9699 struct ll_ioc_lease *ioc = NULL;
9700 __u16 mirror_ids[128] = { 0 };
9705 struct option long_opts[] = {
9706 { .val = 'o', .name = "only", .has_arg = required_argument },
9709 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9712 rc = parse_mirror_ids(mirror_ids,
9713 sizeof(mirror_ids) / sizeof(__u16),
9717 "%s: bad mirror ids '%s'.\n",
9724 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9725 argv[0], argv[optind - 1]);
9731 if (argc == optind) {
9732 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9737 if (ids_nr > 0 && argc > optind + 1) {
9739 "%s: option '--only' cannot be used upon multiple files.\n",
9746 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9751 /* set the lease on the file */
9752 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9754 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9755 argv[0], strerror(errno));
9760 for (; optind < argc; optind++) {
9761 rc = lfs_mirror_resync_file(argv[optind], ioc,
9762 mirror_ids, ids_nr);
9763 /* ignore previous file's error, continue with next file */
9766 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9774 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9776 struct llapi_layout *layout;
9779 layout = llapi_layout_get_by_fd(fd, 0);
9781 fprintf(stderr, "could not get layout.\n");
9785 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9787 fprintf(stderr, "failed to iterate layout\n");
9788 llapi_layout_free(layout);
9791 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9792 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9793 llapi_layout_free(layout);
9797 llapi_layout_free(layout);
9803 * Check whether two files are the same file
9804 * \retval 0 same file
9805 * \retval 1 not the same file
9806 * \retval <0 error code
9808 static inline int check_same_file(int fd, const char *f2)
9813 if (fstat(fd, &stbuf1) < 0)
9816 if (stat(f2, &stbuf2) < 0)
9819 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9820 stbuf1.st_ino == stbuf2.st_ino)
9826 static inline int lfs_mirror_read(int argc, char **argv)
9829 __u16 mirror_id = 0;
9830 const char *outfile = NULL;
9836 const size_t buflen = 4 << 20;
9838 struct option long_opts[] = {
9839 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9840 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9843 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9848 mirror_id = strtoul(optarg, &end, 0);
9849 if (*end != '\0' || mirror_id == 0) {
9851 "%s %s: invalid mirror ID '%s'\n",
9852 progname, argv[0], optarg);
9860 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9861 progname, argv[optind - 1]);
9866 if (argc == optind) {
9867 fprintf(stderr, "%s %s: no mirrored file provided\n",
9870 } else if (argc > optind + 1) {
9871 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9875 if (mirror_id == 0) {
9876 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9881 /* open mirror file */
9882 fname = argv[optind];
9883 fd = open(fname, O_DIRECT | O_RDONLY);
9885 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9886 progname, argv[0], fname, strerror(errno));
9890 /* verify mirror id */
9891 rc = verify_mirror_id_by_fd(fd, mirror_id);
9894 "%s %s: cannot find mirror with ID %u in '%s'\n",
9895 progname, argv[0], mirror_id, fname);
9899 /* open output file - O_EXCL ensures output is not the same as input */
9901 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9903 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9904 progname, argv[0], outfile, strerror(errno));
9909 outfd = STDOUT_FILENO;
9912 /* allocate buffer */
9913 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9915 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9916 progname, argv[0], rc);
9923 ssize_t written = 0;
9925 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9926 if (bytes_read < 0) {
9929 "%s %s: fail to read data from mirror %u: %s\n",
9930 progname, argv[0], mirror_id, strerror(-rc));
9935 if (bytes_read == 0)
9938 while (written < bytes_read) {
9941 written2 = write(outfd, buf + written,
9942 bytes_read - written);
9945 "%s %s: fail to write %s: %s\n",
9946 progname, argv[0], outfile ? : "STDOUT",
9951 written += written2;
9954 if (written != bytes_read) {
9956 "%s %s: written %ld bytes does not match with %ld read.\n",
9957 progname, argv[0], written, bytes_read);
9979 static inline int lfs_mirror_write(int argc, char **argv)
9982 __u16 mirror_id = 0;
9983 const char *inputfile = NULL;
9989 const size_t buflen = 4 << 20;
9991 size_t page_size = sysconf(_SC_PAGESIZE);
9992 struct ll_ioc_lease_id ioc;
9994 struct option long_opts[] = {
9995 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9996 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9999 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10004 mirror_id = strtoul(optarg, &end, 0);
10005 if (*end != '\0' || mirror_id == 0) {
10007 "%s %s: invalid mirror ID '%s'\n",
10008 progname, argv[0], optarg);
10013 inputfile = optarg;
10016 fprintf(stderr, "%s: option '%s' unrecognized\n",
10017 progname, argv[optind - 1]);
10022 if (argc == optind) {
10023 fprintf(stderr, "%s %s: no mirrored file provided\n",
10024 progname, argv[0]);
10026 } else if (argc > optind + 1) {
10027 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10031 if (mirror_id == 0) {
10032 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10033 progname, argv[0]);
10037 /* open mirror file */
10038 fname = argv[optind];
10039 fd = open(fname, O_DIRECT | O_WRONLY);
10041 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10042 progname, argv[0], fname, strerror(errno));
10046 /* verify mirror id */
10047 rc = verify_mirror_id_by_fd(fd, mirror_id);
10050 "%s %s: cannot find mirror with ID %u in '%s'\n",
10051 progname, argv[0], mirror_id, fname);
10055 /* open input file */
10057 rc = check_same_file(fd, inputfile);
10060 "%s %s: input file cannot be the mirrored file\n",
10061 progname, argv[0]);
10067 inputfd = open(inputfile, O_RDONLY, 0644);
10069 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10070 progname, argv[0], inputfile, strerror(errno));
10075 inputfd = STDIN_FILENO;
10078 /* allocate buffer */
10079 rc = posix_memalign(&buf, page_size, buflen);
10081 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10082 progname, argv[0], rc);
10083 goto close_inputfd;
10086 /* prepare target mirror components instantiation */
10087 ioc.lil_mode = LL_LEASE_WRLCK;
10088 ioc.lil_flags = LL_LEASE_RESYNC;
10089 ioc.lil_mirror_id = mirror_id;
10090 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10093 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10094 progname, argv[0], fname, strerror(errno));
10100 ssize_t bytes_read;
10104 rc = llapi_lease_check(fd);
10105 if (rc != LL_LEASE_WRLCK) {
10106 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10107 progname, argv[0], fname);
10111 bytes_read = read(inputfd, buf, buflen);
10112 if (bytes_read < 0) {
10115 "%s %s: fail to read data from '%s': %s\n",
10116 progname, argv[0], inputfile ? : "STDIN",
10123 if (bytes_read == 0)
10126 /* round up to page align to make direct IO happy. */
10127 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10129 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10134 "%s %s: fail to write to mirror %u: %s\n",
10135 progname, argv[0], mirror_id,
10143 if (pos & (page_size - 1)) {
10144 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10149 ioc.lil_mode = LL_LEASE_UNLCK;
10150 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10152 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10157 "%s %s: release lease lock of '%s' failed: %s\n",
10158 progname, argv[0], fname, strerror(errno));
10175 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10177 struct llapi_layout *layout;
10178 struct collect_ids_data cid = { .cid_ids = ids,
10180 .cid_exclude = exclude_id, };
10183 layout = llapi_layout_get_by_fd(fd, 0);
10185 fprintf(stderr, "could not get layout\n");
10189 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10191 fprintf(stderr, "failed to iterate layout\n");
10192 llapi_layout_free(layout);
10196 llapi_layout_free(layout);
10198 return cid.cid_count;
10201 static inline int lfs_mirror_copy(int argc, char **argv)
10204 __u16 read_mirror_id = 0;
10205 __u16 ids[128] = { 0 };
10207 struct llapi_layout *layout = NULL;
10208 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10215 struct ll_ioc_lease *ioc = NULL;
10216 struct ll_ioc_lease_id *resync_ioc;
10218 struct option long_opts[] = {
10219 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10220 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10221 { .name = NULL } };
10223 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10228 read_mirror_id = strtoul(optarg, &end, 0);
10229 if (*end != '\0' || read_mirror_id == 0) {
10231 "%s %s: invalid read mirror ID '%s'\n",
10232 progname, argv[0], optarg);
10237 if (!strcmp(optarg, "-1")) {
10238 /* specify all other mirrors */
10239 ids[0] = (__u16)-1;
10242 count = parse_mirror_ids((__u16 *)ids,
10250 fprintf(stderr, "%s: option '%s' unrecognized\n",
10251 progname, argv[optind - 1]);
10256 if (argc == optind) {
10257 fprintf(stderr, "%s %s: no mirrored file provided\n",
10258 progname, argv[0]);
10260 } else if (argc > optind + 1) {
10261 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10265 if (read_mirror_id == 0) {
10267 "%s %s: no valid read mirror ID %d is provided\n",
10268 progname, argv[0], read_mirror_id);
10274 "%s %s: no write mirror ID is provided\n",
10275 progname, argv[0]);
10279 for (i = 0; i < count; i++) {
10280 if (read_mirror_id == ids[i]) {
10282 "%s %s: read and write mirror ID cannot be the same\n",
10283 progname, argv[0]);
10288 /* open mirror file */
10289 fname = argv[optind];
10291 fd = open(fname, O_DIRECT | O_RDWR);
10293 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10294 progname, argv[0], fname, strerror(errno));
10298 /* write to all other mirrors */
10299 if (ids[0] == (__u16)-1) {
10300 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10304 "%s %s: failed to get other mirror ids in '%s': %d\n",
10305 progname, argv[0], fname, rc);
10310 /* verify mirror id */
10311 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10314 "%s %s: cannot find mirror with ID %u in '%s'\n",
10315 progname, argv[0], read_mirror_id, fname);
10319 for (i = 0; i < count; i++) {
10320 rc = verify_mirror_id_by_fd(fd, ids[i]);
10323 "%s %s: cannot find mirror with ID %u in '%s'\n",
10324 progname, argv[0], ids[i], fname);
10329 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10332 "%s %s: cannot alloc comp id array for ioc: %s\n",
10333 progname, argv[0], strerror(errno));
10338 /* get stale component info */
10339 layout = llapi_layout_get_by_fd(fd, 0);
10341 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10342 progname, argv[0], fname, strerror(errno));
10346 comp_size = llapi_mirror_find_stale(layout, comp_array,
10347 ARRAY_SIZE(comp_array),
10349 llapi_layout_free(layout);
10350 if (comp_size < 0) {
10355 /* prepare target mirror components instantiation */
10356 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10357 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10358 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10360 resync_ioc->lil_mirror_id = ids[0];
10362 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10363 rc = llapi_lease_set(fd, ioc);
10366 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10367 progname, argv[0], fname, strerror(errno));
10371 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10374 fprintf(stderr, "%s %s: copy error: %d\n",
10375 progname, argv[0], rc);
10379 fprintf(stdout, "mirror copied successfully: ");
10380 for (i = 0; i < copied; i++)
10381 fprintf(stdout, "%d ", ids[i]);
10382 fprintf(stdout, "\n");
10384 ioc->lil_mode = LL_LEASE_UNLCK;
10385 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10386 ioc->lil_count = 0;
10387 for (i = 0; i < comp_size; i++) {
10390 for (j = 0; j < copied; j++) {
10391 if (comp_array[i].lrc_mirror_id != ids[j])
10394 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10398 rc = llapi_lease_set(fd, ioc);
10403 "%s %s: release lease lock of '%s' failed: %s\n",
10404 progname, argv[0], fname, strerror(errno));
10419 * struct verify_chunk - Mirror chunk to be verified.
10420 * @chunk: [start, end) of the chunk.
10421 * @mirror_count: Number of mirror ids in @mirror_id array.
10422 * @mirror_id: Array of valid mirror ids that cover the chunk.
10424 struct verify_chunk {
10425 struct lu_extent chunk;
10426 unsigned int mirror_count;
10427 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10431 * print_chunks() - Print chunk information.
10432 * @fname: Mirrored file name.
10433 * @chunks: Array of chunks.
10434 * @chunk_count: Number of chunks in @chunks array.
10436 * This function prints [start, end) of each chunk in @chunks
10437 * for mirrored file @fname, and also prints the valid mirror ids
10438 * that cover the chunk.
10443 void print_chunks(const char *fname, struct verify_chunk *chunks,
10449 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10450 for (i = 0; i < chunk_count; i++) {
10451 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10453 if (chunks[i].mirror_count == 0)
10454 fprintf(stdout, "\t[");
10456 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10457 for (j = 1; j < chunks[i].mirror_count; j++)
10458 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10460 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10462 fprintf(stdout, "\n");
10466 * print_checksums() - Print CRC-32 checksum values.
10467 * @chunk: A chunk and its corresponding valid mirror ids.
10468 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10470 * This function prints CRC-32 checksum values on @chunk for
10471 * each valid mirror that covers it.
10476 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10481 "CRC-32 checksum value for chunk "DEXT":\n",
10482 PEXT(&chunk->chunk));
10483 for (i = 0; i < chunk->mirror_count; i++)
10484 fprintf(stdout, "Mirror %u:\t%#lx\n",
10485 chunk->mirror_id[i], crc[i]);
10486 fprintf(stdout, "\n");
10490 * filter_mirror_id() - Filter specified mirror ids.
10491 * @chunks: Array of chunks.
10492 * @chunk_count: Number of chunks in @chunks array.
10493 * @mirror_ids: Specified mirror ids to be verified.
10494 * @ids_nr: Number of specified mirror ids.
10496 * This function scans valid mirror ids that cover each chunk in @chunks
10497 * and filters specified mirror ids.
10502 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10503 __u16 *mirror_ids, int ids_nr)
10508 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10509 unsigned int valid_count = 0;
10511 for (i = 0; i < chunk_count; i++) {
10512 if (chunks[i].mirror_count == 0)
10516 for (j = 0; j < ids_nr; j++) {
10517 for (k = 0; k < chunks[i].mirror_count; k++) {
10518 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10519 valid_id[valid_count] = mirror_ids[j];
10526 memcpy(chunks[i].mirror_id, valid_id,
10527 sizeof(__u16) * valid_count);
10528 chunks[i].mirror_count = valid_count;
10533 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10534 * @layout: Mirror component list.
10535 * @chunks: Array of chunks.
10536 * @chunks_size: Array size of @chunks.
10538 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10539 * to find out chunk segments and store them in @chunks array.
10541 * The @mirror_id array in each element of @chunks will store the valid
10542 * mirror ids that cover the chunk. If a mirror component covering the
10543 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10544 * will not be stored into the @mirror_id array, and the chunk for that
10545 * mirror will not be verified.
10547 * The @mirror_count in each element of @chunks will store the number of
10548 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10549 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10550 * indicates the chunk is valid in only one mirror. In both cases, the
10551 * chunk will not be verified.
10553 * Here is an example:
10555 * 0 1M 2M 3M 4M EOF
10556 * +------+-------------+--------------------+
10557 * | | | S | mirror1
10558 * +------+------+------+------+-------------+
10559 * | | S | S | | mirror2
10560 * +-------------+------+------+-------------+
10562 * prepared @chunks array will contain 5 elements:
10563 * (([0, 1M), [1, 2], 2),
10564 * ([1M, 2M), [1, 2], 2),
10565 * ([2M, 3M), [1], 1),
10566 * ([3M, 4M], [], 0),
10567 * ([4M, EOF), [2], 1))
10569 * Return: the actual array size of @chunks on success
10570 * or a negative error code on failure.
10573 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10574 struct verify_chunk *chunks,
10575 size_t chunks_size)
10579 uint32_t mirror_id;
10585 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10588 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10591 "%s: move to the first layout component: %s.\n",
10592 progname, strerror(errno));
10598 chunks[idx].chunk.e_end = LUSTRE_EOF;
10600 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10603 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10604 progname, strerror(errno));
10608 if (start > chunks[idx].chunk.e_start ||
10609 end <= chunks[idx].chunk.e_start)
10612 if (end < chunks[idx].chunk.e_end)
10613 chunks[idx].chunk.e_end = end;
10615 rc = llapi_layout_comp_flags_get(layout, &flags);
10618 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10619 progname, strerror(errno));
10623 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10626 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10629 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10630 progname, strerror(errno));
10634 chunks[idx].mirror_id[i] = mirror_id;
10636 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10638 "%s: mirror_id array is too small.\n",
10645 rc = llapi_layout_comp_use(layout,
10646 LLAPI_LAYOUT_COMP_USE_NEXT);
10649 "%s: move to the next layout component: %s.\n",
10650 progname, strerror(errno));
10653 } /* loop through all components */
10655 chunks[idx].mirror_count = i;
10657 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10661 if (idx >= chunks_size) {
10662 fprintf(stderr, "%s: chunks array is too small.\n",
10668 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10672 return rc < 0 ? rc : idx + 1;
10676 * lfs_mirror_verify_chunk() - Verify a chunk.
10677 * @fd: File descriptor of the mirrored file.
10678 * @file_size: Size of the mirrored file.
10679 * @chunk: A chunk and its corresponding valid mirror ids.
10680 * @verbose: Verbose mode.
10682 * This function verifies a @chunk contains exactly the same data
10683 * ammong the mirrors that cover it.
10685 * If @verbose is specified, then the function will print where the
10686 * differences are if the data do not match. Otherwise, it will
10687 * just return an error in that case.
10689 * Return: 0 on success or a negative error code on failure.
10692 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10693 struct verify_chunk *chunk, int verbose)
10695 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10697 size_t page_size = sysconf(_SC_PAGESIZE);
10698 ssize_t bytes_read;
10699 ssize_t bytes_done;
10703 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10707 if (file_size == 0)
10710 rc = posix_memalign(&buf, page_size, buflen);
10711 if (rc) /* error code is returned directly */
10715 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10716 PEXT(&chunk->chunk));
10717 for (i = 0; i < chunk->mirror_count; i++)
10718 fprintf(stdout, " %u", chunk->mirror_id[i]);
10719 fprintf(stdout, "\n");
10723 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10724 pos = chunk->chunk.e_start;
10725 while (bytes_done < count) {
10726 /* compute initial CRC-32 checksum */
10727 crc = crc32(0L, Z_NULL, 0);
10728 memset(crc_array, 0, sizeof(crc_array));
10731 for (i = 0; i < chunk->mirror_count; i++) {
10732 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10734 if (bytes_read < 0) {
10737 "%s: failed to read data from mirror %u: %s.\n",
10738 progname, chunk->mirror_id[i],
10743 /* compute new CRC-32 checksum */
10744 crc_array[i] = crc32(crc, buf, bytes_read);
10748 print_checksums(chunk, crc_array);
10750 /* compare CRC-32 checksum values */
10751 for (i = 1; i < chunk->mirror_count; i++) {
10752 if (crc_array[i] != crc_array[0]) {
10756 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10757 progname, PEXT(&chunk->chunk),
10758 chunk->mirror_id[0],
10759 chunk->mirror_id[i]);
10764 bytes_done += bytes_read;
10767 if (verbose > 1 && rc == 0) {
10768 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10769 PEXT(&chunk->chunk));
10770 for (i = 0; i < chunk->mirror_count; i++)
10771 fprintf(stdout, " %u", chunk->mirror_id[i]);
10772 fprintf(stdout, " PASS\n\n");
10781 * lfs_mirror_verify_file() - Verify a mirrored file.
10782 * @fname: Mirrored file name.
10783 * @mirror_ids: Specified mirror ids to be verified.
10784 * @ids_nr: Number of specified mirror ids.
10785 * @verbose: Verbose mode.
10787 * This function verifies that each SYNC mirror of a mirrored file
10788 * specified by @fname contains exactly the same data.
10790 * If @mirror_ids is specified, then the function will verify the
10791 * mirrors specified by @mirror_ids contain exactly the same data.
10793 * If @verbose is specified, then the function will print where the
10794 * differences are if the data do not match. Otherwise, it will
10795 * just return an error in that case.
10797 * Return: 0 on success or a negative error code on failure.
10800 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10803 struct verify_chunk chunks_array[1024] = { };
10804 struct llapi_layout *layout = NULL;
10806 uint32_t flr_state;
10808 int chunk_count = 0;
10814 if (stat(fname, &stbuf) < 0) {
10815 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10816 progname, fname, strerror(errno));
10821 if (!S_ISREG(stbuf.st_mode)) {
10822 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10828 if (stbuf.st_size == 0) {
10830 fprintf(stdout, "%s: '%s' file size is 0.\n",
10836 fd = open(fname, O_DIRECT | O_RDONLY);
10838 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10839 progname, fname, strerror(errno));
10844 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10846 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10847 progname, fname, strerror(errno));
10851 layout = llapi_layout_get_by_fd(fd, 0);
10853 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10854 progname, fname, strerror(errno));
10856 llapi_lease_release(fd);
10860 rc = llapi_layout_flags_get(layout, &flr_state);
10862 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10863 progname, fname, strerror(errno));
10868 flr_state &= LCM_FL_FLR_MASK;
10869 switch (flr_state) {
10872 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10873 progname, fname, llapi_layout_flags_string(flr_state));
10879 /* find out mirror chunks to be verified */
10880 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10881 ARRAY_SIZE(chunks_array));
10882 if (chunk_count < 0) {
10888 /* filter specified mirror ids */
10889 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10892 print_chunks(fname, chunks_array, chunk_count);
10894 for (idx = 0; idx < chunk_count; idx++) {
10895 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10898 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10900 PEXT(&chunks_array[idx].chunk),
10901 (unsigned long long)stbuf.st_size);
10905 if (chunks_array[idx].mirror_count == 0) {
10907 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10909 PEXT(&chunks_array[idx].chunk));
10911 fprintf(stderr, "skipped\n");
10915 fprintf(stderr, "failed\n");
10919 if (chunks_array[idx].mirror_count == 1) {
10922 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10924 PEXT(&chunks_array[idx].chunk),
10925 chunks_array[idx].mirror_id[0]);
10929 rc = llapi_lease_check(fd);
10930 if (rc != LL_LEASE_RDLCK) {
10931 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10936 /* verify one chunk */
10937 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10938 &chunks_array[idx], verbose);
10952 llapi_layout_free(layout);
10953 llapi_lease_release(fd);
10961 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10962 * @argc: The count of lfs mirror verify command line arguments.
10963 * @argv: Array of strings for lfs mirror verify command line arguments.
10965 * This function parses lfs mirror verify command and verifies the
10966 * specified mirrored file(s).
10968 * Return: 0 on success or a negative error code on failure.
10970 static inline int lfs_mirror_verify(int argc, char **argv)
10972 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10978 char cmd[PATH_MAX];
10980 struct option long_opts[] = {
10981 { .val = 'o', .name = "only", .has_arg = required_argument },
10982 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10983 { .name = NULL } };
10985 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10987 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10990 rc = parse_mirror_ids(mirror_ids,
10991 ARRAY_SIZE(mirror_ids),
10995 "%s: bad mirror ids '%s'.\n",
11002 "%s: at least 2 mirror ids needed with '--only' option.\n",
11012 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11013 progname, argv[optind - 1]);
11019 if (argc == optind) {
11020 fprintf(stderr, "%s: no file name given.\n", progname);
11025 if (ids_nr > 0 && argc > optind + 1) {
11027 "%s: '--only' cannot be used upon multiple files.\n",
11034 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11040 for (; optind < argc; optind++) {
11041 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11051 * lfs_mirror() - Parse and execute lfs mirror commands.
11052 * @argc: The count of lfs mirror command line arguments.
11053 * @argv: Array of strings for lfs mirror command line arguments.
11055 * This function parses lfs mirror commands and performs the
11056 * corresponding functions specified in mirror_cmdlist[].
11058 * Return: 0 on success or an error code on failure.
11060 static int lfs_mirror(int argc, char **argv)
11062 char cmd[PATH_MAX];
11065 setlinebuf(stdout);
11067 Parser_init("lfs-mirror > ", mirror_cmdlist);
11069 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11071 program_invocation_short_name = cmd;
11073 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11075 rc = Parser_commands();
11077 return rc < 0 ? -rc : rc;
11080 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11082 #if __BYTE_ORDER == __BIG_ENDIAN
11083 __swab16s(&attrs->lsa_valid);
11084 __swab64s(&attrs->lsa_size);
11085 __swab64s(&attrs->lsa_blocks);
11089 enum lfs_som_type {
11090 LFS_SOM_SIZE = 0x1,
11091 LFS_SOM_BLOCKS = 0x2,
11092 LFS_SOM_FLAGS = 0x4,
11093 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11097 static int lfs_getsom(int argc, char **argv)
11100 struct lustre_som_attrs *attrs;
11101 char buf[sizeof(*attrs) + 64];
11102 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11105 while ((c = getopt(argc, argv, "sbf")) != -1) {
11108 type = LFS_SOM_SIZE;
11111 type = LFS_SOM_BLOCKS;
11114 type = LFS_SOM_FLAGS;
11117 fprintf(stderr, "%s: invalid option '%c'\n",
11127 fprintf(stderr, "%s: %s\n",
11128 progname, argc == 0 ? "miss file target" :
11129 "input more than 2 files");
11134 attrs = (void *)buf;
11135 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11138 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11139 argv[0], strerror(errno), errno);
11143 lustre_som_swab(attrs);
11146 case LFS_SOM_ATTR_ALL:
11147 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11148 path, (unsigned long long)attrs->lsa_size,
11149 (unsigned long long)attrs->lsa_blocks,
11153 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11155 case LFS_SOM_BLOCKS:
11156 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11158 case LFS_SOM_FLAGS:
11159 printf("%x\n", attrs->lsa_valid);
11162 fprintf(stderr, "%s: unknown option\n", progname);
11170 * lfs_mirror_list_commands() - List lfs mirror commands.
11171 * @argc: The count of command line arguments.
11172 * @argv: Array of strings for command line arguments.
11174 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11176 * Return: 0 on success.
11178 static int lfs_mirror_list_commands(int argc, char **argv)
11180 char buffer[81] = "";
11182 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11188 static int lfs_pcc_attach(int argc, char **argv)
11190 struct option long_opts[] = {
11191 { .val = 'i', .name = "id", .has_arg = required_argument },
11192 { .name = NULL } };
11195 __u32 archive_id = 0;
11198 char fullpath[PATH_MAX];
11199 enum lu_pcc_type type = LU_PCC_READWRITE;
11202 while ((c = getopt_long(argc, argv, "i:",
11203 long_opts, NULL)) != -1) {
11206 archive_id = strtoul(optarg, &end, 0);
11207 if (*end != '\0' || archive_id == 0) {
11209 "error: %s: bad archive ID '%s'\n",
11217 fprintf(stderr, "%s: option '%s' unrecognized\n",
11218 argv[0], argv[optind - 1]);
11223 if (archive_id == 0) {
11224 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11228 if (argc <= optind) {
11229 fprintf(stderr, "%s: must specify one or more file names\n",
11234 while (optind < argc) {
11237 path = argv[optind++];
11238 if (!realpath(path, fullpath)) {
11239 fprintf(stderr, "%s: could not find path '%s': %s\n",
11240 argv[0], path, strerror(errno));
11246 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11249 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11250 argv[0], path, archive_id, strerror(-rc2));
11258 static int lfs_pcc_attach_fid(int argc, char **argv)
11260 struct option long_opts[] = {
11261 { .val = 'i', .name = "id", .has_arg = required_argument },
11262 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11263 { .name = NULL } };
11264 char short_opts[] = "i:m:";
11267 __u32 archive_id = 0;
11269 const char *mntpath = NULL;
11270 const char *fidstr;
11271 enum lu_pcc_type type = LU_PCC_READWRITE;
11274 while ((c = getopt_long(argc, argv, short_opts,
11275 long_opts, NULL)) != -1) {
11278 archive_id = strtoul(optarg, &end, 0);
11279 if (*end != '\0') {
11281 "error: %s: bad archive ID '%s'\n",
11292 fprintf(stderr, "%s: option '%s' unrecognized\n",
11293 argv[0], argv[optind - 1]);
11298 if (archive_id == 0) {
11299 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11304 fprintf(stderr, "%s: must specify Lustre mount point\n",
11309 if (argc <= optind) {
11310 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11314 while (optind < argc) {
11317 fidstr = argv[optind++];
11319 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11323 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11324 argv[0], fidstr, mntpath, archive_id,
11327 if (rc == 0 && rc2 < 0)
11333 static int lfs_pcc_detach(int argc, char **argv)
11335 struct option long_opts[] = {
11336 { .val = 'k', .name = "keep", .has_arg = no_argument },
11337 { .name = NULL } };
11338 char short_opts[] = "k";
11342 char fullpath[PATH_MAX];
11343 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11346 while ((c = getopt_long(argc, argv, short_opts,
11347 long_opts, NULL)) != -1) {
11350 detach_opt = PCC_DETACH_OPT_NONE;
11355 fprintf(stderr, "%s: option '%s' unrecognized\n",
11356 argv[0], argv[optind - 1]);
11361 while (optind < argc) {
11364 path = argv[optind++];
11365 if (!realpath(path, fullpath)) {
11366 fprintf(stderr, "%s: could not find path '%s': %s\n",
11367 argv[0], path, strerror(errno));
11373 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11377 "%s: cannot detach '%s' from PCC: %s\n",
11378 argv[0], path, strerror(errno));
11386 static int lfs_pcc_detach_fid(int argc, char **argv)
11388 struct option long_opts[] = {
11389 { .val = 'k', .name = "keep", .has_arg = no_argument },
11390 { .name = NULL } };
11391 char short_opts[] = "k";
11395 const char *mntpath;
11396 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11399 while ((c = getopt_long(argc, argv, short_opts,
11400 long_opts, NULL)) != -1) {
11403 detach_opt = PCC_DETACH_OPT_NONE;
11408 fprintf(stderr, "%s: option '%s' unrecognized\n",
11409 argv[0], argv[optind - 1]);
11414 mntpath = argv[optind++];
11416 while (optind < argc) {
11419 fid = argv[optind++];
11421 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11424 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11425 argv[0], fid, mntpath, strerror(-rc2));
11433 static int lfs_pcc_state(int argc, char **argv)
11437 char fullpath[PATH_MAX];
11438 struct lu_pcc_state state;
11443 fprintf(stderr, "%s: must specify one or more file names\n",
11448 while (optind < argc) {
11451 path = argv[optind++];
11452 if (!realpath(path, fullpath)) {
11453 fprintf(stderr, "%s: could not find path '%s': %s\n",
11454 argv[0], path, strerror(errno));
11460 rc2 = llapi_pcc_state_get(fullpath, &state);
11465 "%s: cannot get PCC state of '%s': %s\n",
11466 argv[0], path, strerror(-rc2));
11470 printf("file: %s", path);
11471 printf(", type: %s", pcc_type2string(state.pccs_type));
11472 if (state.pccs_type == LU_PCC_NONE &&
11473 state.pccs_open_count == 0) {
11478 printf(", PCC file: %s", state.pccs_path);
11479 printf(", user number: %u", state.pccs_open_count);
11480 printf(", flags: %x", state.pccs_flags);
11487 * lfs_pcc_list_commands() - List lfs pcc commands.
11488 * @argc: The count of command line arguments.
11489 * @argv: Array of strings for command line arguments.
11491 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11493 * Return: 0 on success.
11495 static int lfs_pcc_list_commands(int argc, char **argv)
11497 char buffer[81] = "";
11499 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11506 * lfs_pcc() - Parse and execute lfs pcc commands.
11507 * @argc: The count of lfs pcc command line arguments.
11508 * @argv: Array of strings for lfs pcc command line arguments.
11510 * This function parses lfs pcc commands and performs the
11511 * corresponding functions specified in pcc_cmdlist[].
11513 * Return: 0 on success or an error code on failure.
11515 static int lfs_pcc(int argc, char **argv)
11517 char cmd[PATH_MAX];
11520 setlinebuf(stdout);
11522 Parser_init("lfs-pcc > ", pcc_cmdlist);
11524 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11526 program_invocation_short_name = cmd;
11528 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11530 rc = Parser_commands();
11532 return rc < 0 ? -rc : rc;
11535 static int lfs_list_commands(int argc, char **argv)
11537 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11539 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11544 int main(int argc, char **argv)
11548 /* Ensure that liblustreapi constructor has run */
11549 if (!llapi_liblustreapi_initialized())
11550 fprintf(stderr, "liblustreapi was not properly initialized\n");
11552 setlinebuf(stdout);
11555 Parser_init("lfs > ", cmdlist);
11557 progname = program_invocation_short_name; /* Used in error messages */
11559 llapi_set_command_name(argv[1]);
11560 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11561 llapi_clear_command_name();
11563 rc = Parser_commands();
11566 return rc < 0 ? -rc : rc;
11569 #ifdef _LUSTRE_IDL_H_
11570 /* Everything we need here should be included by lustreapi.h. */
11571 # error "lfs should not depend on lustre_idl.h"
11572 #endif /* _LUSTRE_IDL_H_ */