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,
1769 bool inherit, uint32_t flags)
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 llapi_layout_comp_flags_set(m_layout, flags);
1798 rc = migrate_open_files(name, 0, NULL, m_layout, &fd, &fdv);
1802 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1804 error_loc = "cannot get lease";
1808 rc = migrate_nonblock(fd, fdv);
1810 llapi_lease_release(fd);
1814 /* Atomically put lease, merge layouts and close. */
1815 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1817 error_loc = "memory allocation";
1820 data->lil_mode = LL_LEASE_UNLCK;
1821 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1822 data->lil_count = 1;
1823 data->lil_ids[0] = fdv;
1824 rc = llapi_lease_set(fd, data);
1826 error_loc = "cannot merge layout";
1828 } else if (rc == 0) {
1830 error_loc = "lost lease lock";
1843 fprintf(stderr, "error: %s: %s: %s: %s\n",
1844 progname, name, error_loc, strerror(-rc));
1848 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1849 enum mirror_flags mirror_flags)
1853 rc = mirror_create_sanity_check(fname, mirror_list);
1857 while (mirror_list) {
1858 if (mirror_list->m_file) {
1859 rc = mirror_extend_file(fname, mirror_list->m_file,
1862 __u32 mirror_count = mirror_list->m_count;
1864 while (mirror_count > 0) {
1865 rc = mirror_extend_layout(fname,
1866 mirror_list->m_layout,
1867 mirror_list->m_inherit,
1868 mirror_list->m_flags);
1878 mirror_list = mirror_list->m_next;
1884 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1889 rc = llapi_layout_mirror_id_get(layout, &id);
1893 if ((__u16)id == *(__u16 *)cbdata)
1894 return LLAPI_LAYOUT_ITER_STOP;
1896 return LLAPI_LAYOUT_ITER_CONT;
1899 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1904 rc = llapi_layout_comp_id_get(layout, &id);
1908 if (id == *(__u32 *)cbdata)
1909 return LLAPI_LAYOUT_ITER_STOP;
1911 return LLAPI_LAYOUT_ITER_CONT;
1914 struct pool_to_id_cbdata {
1919 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1921 char buf[LOV_MAXPOOLNAME + 1];
1922 struct pool_to_id_cbdata *d = (void *)cbdata;
1926 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1929 if (strcmp(d->pool, buf))
1930 return LLAPI_LAYOUT_ITER_CONT;
1932 rc = llapi_layout_mirror_id_get(layout, &id);
1937 return LLAPI_LAYOUT_ITER_STOP;
1940 struct collect_ids_data {
1946 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1948 struct collect_ids_data *cid = cbdata;
1952 rc = llapi_layout_mirror_id_get(layout, &id);
1956 if ((__u16)id != cid->cid_exclude) {
1959 for (i = 0; i < cid->cid_count; i++) {
1960 /* already collected the mirror id */
1961 if (id == cid->cid_ids[i])
1962 return LLAPI_LAYOUT_ITER_CONT;
1964 cid->cid_ids[cid->cid_count] = id;
1968 return LLAPI_LAYOUT_ITER_CONT;
1972 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1973 * @mirror_id: Mirror id to be checked.
1974 * @layout: Mirror component list.
1976 * This function checks if a mirror with specified @mirror_id is the last
1977 * non-stale mirror of a layout @layout.
1979 * Return: true or false.
1982 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1984 __u16 mirror_ids[128] = { 0 };
1985 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1987 .cid_exclude = mirror_id, };
1990 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1992 for (i = 0; i < cid.cid_count; i++) {
1993 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1996 comp_size = llapi_mirror_find_stale(layout, comp_array,
1997 ARRAY_SIZE(comp_array),
2006 static int mirror_split(const char *fname, __u32 id, const char *pool,
2007 enum mirror_flags mflags, const char *victim_file)
2009 struct llapi_layout *layout;
2010 char parent[PATH_MAX];
2011 char victim[PATH_MAX];
2012 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2014 struct ll_ioc_lease *data;
2015 uint16_t mirror_count;
2021 /* check fname contains mirror with mirror_id/comp_id */
2022 layout = llapi_layout_get_by_path(fname, 0);
2025 "error %s: file '%s' couldn't get layout\n",
2030 rc = llapi_layout_sanity(layout, false, true);
2032 llapi_layout_sanity_perror(rc);
2036 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2039 "error %s: file '%s' couldn't get mirror count\n",
2043 if (mirror_count < 2) {
2045 "error %s: file '%s' has %d component, cannot split\n",
2046 progname, fname, mirror_count);
2050 if (mflags & MF_COMP_POOL) {
2051 struct pool_to_id_cbdata data = { .pool = pool };
2053 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
2055 mirror_id = data.id;
2056 } else if (mflags & MF_COMP_ID) {
2057 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2058 mirror_id = mirror_id_of(id);
2060 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2064 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2067 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2068 if (mflags & MF_COMP_POOL) {
2070 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2071 progname, fname, pool);
2073 } else if (mflags & MF_COMP_ID) {
2075 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2076 progname, fname, id);
2080 "error %s: file '%s' does not contain mirror with id %u\n",
2081 progname, fname, id);
2086 fd = open(fname, O_RDWR);
2089 "error %s: open file '%s' failed: %s\n",
2090 progname, fname, strerror(errno));
2094 /* get victim file directory pathname */
2095 if (strlen(fname) > sizeof(parent) - 1) {
2096 fprintf(stderr, "error %s: file name of '%s' too long\n",
2101 strncpy(parent, fname, sizeof(parent));
2102 ptr = strrchr(parent, '/');
2104 if (!getcwd(parent, sizeof(parent))) {
2105 fprintf(stderr, "error %s: getcwd failed: %s\n",
2106 progname, strerror(errno));
2116 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2118 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2124 /* use a temp file to store the splitted layout */
2125 if (mflags & MF_DESTROY) {
2126 if (last_non_stale_mirror(mirror_id, layout)) {
2129 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2134 fdv = llapi_create_volatile_idx(parent, mdt_index,
2135 O_LOV_DELAY_CREATE);
2137 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2139 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2142 /* user specified victim file */
2143 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2148 "error %s: create victim file failed: %s\n",
2149 progname, strerror(errno));
2153 /* get lease lock of fname */
2154 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2157 "error %s: cannot get lease of file '%s': %d\n",
2158 progname, fname, rc);
2162 /* Atomatically put lease, split layouts and close. */
2163 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2169 data->lil_mode = LL_LEASE_UNLCK;
2170 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2171 data->lil_count = 2;
2172 data->lil_ids[0] = fdv;
2173 data->lil_ids[1] = mirror_id;
2174 rc = llapi_lease_set(fd, data);
2176 if (rc == 0) /* lost lease lock */
2179 "error %s: cannot split '%s': %s\n",
2180 progname, fname, strerror(-rc));
2191 llapi_layout_free(layout);
2196 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2197 __u16 *mirror_ids, int ids_nr);
2199 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2200 __u64 migration_flags,
2201 struct llapi_stripe_param *param,
2202 struct llapi_layout *layout)
2204 struct ll_ioc_lease *data = NULL;
2207 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2209 error_loc = "cannot get lease";
2213 /* Atomically put lease, merge layouts, resync and close. */
2214 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2216 error_loc = "memory allocation";
2219 data->lil_mode = LL_LEASE_UNLCK;
2220 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2221 data->lil_count = 1;
2222 data->lil_ids[0] = fdv;
2223 rc = llapi_lease_set(fd, data);
2225 error_loc = "cannot merge layout";
2227 } else if (rc == 0) {
2229 error_loc = "lost lease lock";
2235 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2237 error_loc = "cannot resync file";
2241 /* delete first mirror now */
2242 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2244 error_loc = "cannot delete old layout";
2252 fprintf(stderr, "error: %s: %s: %s: %s\n",
2253 progname, name, error_loc, strerror(-rc));
2254 else if (migration_flags & MIGRATION_VERBOSE)
2255 printf("%s\n", name);
2262 * Parse a string containing an target index list into an array of integers.
2264 * The input string contains a comma delimited list of individual
2265 * indices and ranges, for example "1,2-4,7". Add the indices into the
2266 * \a tgts array and remove duplicates.
2268 * \param[out] tgts array to store indices in
2269 * \param[in] size size of \a tgts array
2270 * \param[in] offset starting index in \a tgts
2271 * \param[in] arg string containing OST index list
2272 * \param[in/out] overstriping index list may contain duplicates
2274 * \retval positive number of indices in \a tgts
2275 * \retval -EINVAL unable to parse \a arg
2277 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2278 unsigned long long *pattern)
2282 int slots = size - offset;
2284 bool overstriped = false;
2290 end_of_loop = false;
2291 while (!end_of_loop) {
2292 int start_index = 0;
2295 char *endptr = NULL;
2299 ptr = strchrnul(arg, ',');
2301 end_of_loop = *ptr == '\0';
2304 start_index = strtol(arg, &endptr, 0);
2305 if (endptr == arg) /* no data at all */
2307 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2310 end_index = start_index;
2311 if (*endptr == '-') {
2312 end_index = strtol(endptr + 1, &endptr, 0);
2313 if (*endptr != '\0')
2315 if (end_index < start_index)
2319 for (i = start_index; i <= end_index && slots > 0; i++) {
2322 /* remove duplicate */
2323 for (j = 0; j < offset; j++) {
2324 if (tgts[j] == i && pattern &&
2325 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2327 else if (tgts[j] == i)
2333 if (j == offset) { /* check complete */
2339 if (slots == 0 && i < end_index)
2347 if (!end_of_loop && ptr)
2350 if (!overstriped && pattern)
2351 *pattern = LLAPI_LAYOUT_DEFAULT;
2353 return rc < 0 ? rc : nr;
2356 struct lfs_setstripe_args {
2357 unsigned long long lsa_comp_end;
2358 unsigned long long lsa_stripe_size;
2359 unsigned long long lsa_extension_size;
2360 long long lsa_stripe_count;
2361 long long lsa_stripe_off;
2362 __u32 lsa_comp_flags;
2363 __u32 lsa_comp_neg_flags;
2364 unsigned long long lsa_pattern;
2365 unsigned int lsa_mirror_count;
2367 bool lsa_first_comp;
2368 bool lsa_extension_comp;
2370 char *lsa_pool_name;
2373 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2375 unsigned int mirror_count = lsa->lsa_mirror_count;
2376 bool first_comp = lsa->lsa_first_comp;
2378 memset(lsa, 0, sizeof(*lsa));
2380 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2381 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2382 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2383 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2384 lsa->lsa_pool_name = NULL;
2386 lsa->lsa_mirror_count = mirror_count;
2387 lsa->lsa_first_comp = first_comp;
2391 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2392 * @lsa: Stripe options to be initialized and inherited.
2394 * This function initializes stripe options in @lsa and inherit
2395 * stripe_size, stripe_count and OST pool_name options.
2399 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2401 unsigned long long stripe_size;
2402 long long stripe_count;
2403 char *pool_name = NULL;
2405 stripe_size = lsa->lsa_stripe_size;
2406 stripe_count = lsa->lsa_stripe_count;
2407 pool_name = lsa->lsa_pool_name;
2409 setstripe_args_init(lsa);
2411 lsa->lsa_stripe_size = stripe_size;
2412 lsa->lsa_stripe_count = stripe_count;
2413 lsa->lsa_pool_name = pool_name;
2416 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2418 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2419 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2420 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2421 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2422 lsa->lsa_comp_end != 0);
2426 * comp_args_to_layout() - Create or extend a composite layout.
2427 * @composite: Pointer to the composite layout.
2428 * @lsa: Stripe options for the new component.
2430 * This function creates or extends a composite layout by adding a new
2431 * component with stripe options from @lsa.
2433 * Return: 0 on success or an error code on failure.
2435 static int comp_args_to_layout(struct llapi_layout **composite,
2436 struct lfs_setstripe_args *lsa,
2439 struct llapi_layout *layout = *composite;
2440 uint64_t prev_end = 0;
2446 layout = llapi_layout_alloc();
2448 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2453 *composite = layout;
2454 lsa->lsa_first_comp = true;
2459 * Get current component extent, current component
2460 * must be the tail component.
2462 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2464 fprintf(stderr, "Get comp extent failed. %s\n",
2469 if (lsa->lsa_first_comp) {
2471 rc = llapi_layout_add_first_comp(layout);
2473 rc = llapi_layout_comp_add(layout);
2476 fprintf(stderr, "Add component failed. %s\n",
2482 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2484 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2485 lsa->lsa_comp_flags, strerror(errno));
2490 uint64_t comp_end = lsa->lsa_comp_end;
2493 * The extendable component is 0-length, so it can be removed
2494 * if there is insufficient space to extend it.
2496 if (lsa->lsa_extension_comp)
2497 comp_end = prev_end;
2499 rc = llapi_layout_comp_extent_set(layout, prev_end,
2502 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2503 prev_end, comp_end, strerror(errno));
2507 /* reset lsa_first_comp */
2508 lsa->lsa_first_comp = false;
2510 /* Data-on-MDT component setting */
2511 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2513 * In case of Data-on-MDT patterns the only extra option
2514 * applicable is stripe size option.
2516 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2518 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2519 lsa->lsa_stripe_count);
2523 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2525 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2526 lsa->lsa_stripe_size);
2530 if (lsa->lsa_nr_tgts != 0) {
2532 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2537 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2539 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2540 lsa->lsa_stripe_off);
2544 if (lsa->lsa_pool_name != 0) {
2546 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2547 lsa->lsa_pool_name);
2552 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2554 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2559 /* Data-on-MDT component has always single stripe up to end */
2560 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2561 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2562 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2564 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2571 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2572 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2574 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2575 rc = llapi_layout_extension_size_set(layout, size);
2577 rc = llapi_layout_stripe_size_set(layout, size);
2580 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2581 size, strerror(errno));
2585 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2587 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2588 lsa->lsa_stripe_count, strerror(errno));
2592 if (lsa->lsa_pool_name) {
2593 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2595 fprintf(stderr, "Set pool name: %s failed. %s\n",
2596 lsa->lsa_pool_name, strerror(errno));
2600 rc = llapi_layout_pool_name_set(layout, "");
2602 fprintf(stderr, "Clear pool name failed: %s\n",
2608 if (lsa->lsa_nr_tgts > 0) {
2609 if (lsa->lsa_stripe_count > 0 &&
2610 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2611 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2612 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2613 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2614 lsa->lsa_stripe_count,
2619 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2620 rc = llapi_layout_ost_index_set(layout, i,
2625 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2626 lsa->lsa_stripe_off != -1) {
2627 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2630 fprintf(stderr, "Set ost index %d failed. %s\n",
2631 i, strerror(errno));
2635 /* Create the second, virtual component of extension space */
2636 if (lsa->lsa_extension_comp) {
2637 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2638 lsa->lsa_extension_comp = false;
2645 static int build_component(struct llapi_layout **layout,
2646 struct lfs_setstripe_args *lsa, bool set_extent)
2650 rc = comp_args_to_layout(layout, lsa, set_extent);
2654 if (lsa->lsa_mirror_count > 0) {
2655 rc = llapi_layout_mirror_count_set(*layout,
2656 lsa->lsa_mirror_count);
2660 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2663 lsa->lsa_mirror_count = 0;
2669 static int build_prev_component(struct llapi_layout **layout,
2670 struct lfs_setstripe_args *prev,
2671 struct lfs_setstripe_args *lsa,
2674 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2677 if (prev->lsa_stripe_size) {
2679 prev->lsa_comp_end = lsa->lsa_comp_end;
2680 prev->lsa_extension_size = lsa->lsa_extension_size;
2681 prev->lsa_extension_comp = true;
2684 rc = build_component(layout, prev, true);
2690 * Copy lsa to previous lsa;
2691 * if this is an extension component, make the previous invalid;
2694 prev->lsa_stripe_size = 0;
2701 static int build_layout_from_yaml_node(struct cYAML *node,
2702 struct llapi_layout **layout,
2703 struct lfs_setstripe_args *lsa,
2704 struct lfs_setstripe_args *prevp)
2706 struct lfs_setstripe_args prev = { 0 };
2707 __u32 *osts = lsa->lsa_tgts;
2715 string = node->cy_string;
2717 if (node->cy_type == CYAML_TYPE_OBJECT) {
2718 /* go deep to sub blocks */
2719 if (string && !strncmp(string, "component", 9) &&
2720 strncmp(string, "component0", 10) &&
2721 strncmp(string, "components", 10)) {
2722 rc = build_prev_component(layout, prevp, lsa,
2727 /* initialize lsa. */
2728 setstripe_args_init(lsa);
2729 lsa->lsa_first_comp = false;
2730 lsa->lsa_tgts = osts;
2733 rc = build_layout_from_yaml_node(node->cy_child, layout,
2738 if (!node->cy_string)
2741 /* skip leading lmm_ if present, to simplify parsing */
2742 if (strncmp(string, "lmm_", 4) == 0)
2745 if (node->cy_type == CYAML_TYPE_STRING) {
2746 if (!strcmp(string, "lcme_extent.e_end")) {
2747 if (!strcmp(node->cy_valuestring, "EOF") ||
2748 !strcmp(node->cy_valuestring, "eof"))
2749 lsa->lsa_comp_end = LUSTRE_EOF;
2750 } else if (!strcmp(string, "pool")) {
2751 lsa->lsa_pool_name = node->cy_valuestring;
2752 } else if (!strcmp(string, "pattern")) {
2753 if (!strcmp(node->cy_valuestring, "mdt"))
2754 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2755 if (!strcmp(node->cy_valuestring,
2756 "raid0,overstriped"))
2758 LLAPI_LAYOUT_OVERSTRIPING;
2759 } else if (!strcmp(string, "lcme_flags")) {
2760 rc = comp_str2flags(node->cy_valuestring,
2761 &lsa->lsa_comp_flags,
2762 &lsa->lsa_comp_neg_flags);
2766 * Only template flags have meaning in
2767 * the layout for a new file
2769 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2771 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2772 if (!strcmp(string, "lcm_mirror_count")) {
2773 lsa->lsa_mirror_count = node->cy_valueint;
2774 } else if (!strcmp(string, "lcme_extent.e_start")) {
2775 if (node->cy_valueint == 0)
2776 lsa->lsa_first_comp = true;
2777 } else if (!strcmp(string, "lcme_extent.e_end")) {
2778 if (node->cy_valueint == -1)
2779 lsa->lsa_comp_end = LUSTRE_EOF;
2781 lsa->lsa_comp_end = node->cy_valueint;
2782 } else if (!strcmp(string, "stripe_count")) {
2783 lsa->lsa_stripe_count = node->cy_valueint;
2784 } else if (!strcmp(string, "stripe_size")) {
2785 lsa->lsa_stripe_size = node->cy_valueint;
2786 } else if (!strcmp(string, "extension_size")) {
2787 lsa->lsa_extension_size = node->cy_valueint;
2788 lsa->lsa_extension_comp = true;
2789 } else if (!strcmp(string, "stripe_offset")) {
2790 lsa->lsa_stripe_off = node->cy_valueint;
2791 } else if (!strcmp(string, "l_ost_idx")) {
2792 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2797 node = node->cy_next;
2800 if (prevp == &prev) {
2801 rc = build_prev_component(layout, prevp, lsa, true);
2805 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2806 rc = build_component(layout, lsa, *layout != NULL);
2812 static int lfs_comp_create_from_yaml(char *template,
2813 struct llapi_layout **layout,
2814 struct lfs_setstripe_args *lsa,
2817 struct cYAML *tree = NULL, *err_rc = NULL;
2820 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2822 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2823 progname, template);
2824 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2825 "can't parse", &err_rc);
2826 cYAML_print_tree2file(stderr, err_rc);
2827 cYAML_free_tree(err_rc);
2832 /* initialize lsa for plain file */
2833 setstripe_args_init(lsa);
2834 lsa->lsa_tgts = osts;
2836 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2838 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2839 progname, template);
2842 /* clean clean lsa */
2843 setstripe_args_init(lsa);
2847 cYAML_free_tree(tree);
2852 * Get the extension size from the next (SEL) component and extend the
2853 * current component on it. The start of the next component is to be
2856 * \param[in] layout the current layout
2857 * \param[in] start the start of the current component
2858 * \param[in,out] end the end of the current component
2859 * \param[in] offset the offset to adjust the end position to instead of
2862 * \retval 0 - extended successfully
2863 * \retval < 0 - error
2865 static int layout_extend_comp(struct llapi_layout *layout,
2866 uint64_t start, uint64_t *end,
2869 uint64_t size, next_start, next_end;
2872 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2875 "%s setstripe: cannot move component cursor: %s\n",
2876 progname, strerror(errno));
2881 * Even if the @size will not be used below, this will fail if
2882 * this is not a SEL component - a good confirmation we are
2883 * working on right components.
2885 rc = llapi_layout_extension_size_get(layout, &size);
2888 "%s setstripe: cannot get component ext size: %s\n",
2889 progname, strerror(errno));
2893 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2895 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2896 progname, strerror(errno));
2900 next_start += offset ?: size;
2901 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2903 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2904 progname, strerror(errno));
2908 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2911 "%s setstripe: cannot move component cursor: %s\n",
2912 progname, strerror(errno));
2916 *end += offset ?: size;
2917 rc = llapi_layout_comp_extent_set(layout, start, *end);
2919 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2920 progname, strerror(errno));
2928 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2929 * end of the last component in the existing file, and adjust the
2930 * first extent start of the components to be added accordingly.
2932 * In the create mode, we need to check if the first component is an extendable
2933 * SEL component and extend its length to the extension size (first component
2934 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2936 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2939 struct llapi_layout *head;
2940 uint64_t start = 0, prev_end = 0;
2944 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
2949 head = llapi_layout_get_by_path(fname, 0);
2952 "%s setstripe: cannot read layout from '%s': %s\n",
2953 progname, fname, strerror(errno));
2955 } else if (errno == ENODATA) {
2957 * file without LOVEA, this component-add will be turned
2958 * into a component-create.
2960 llapi_layout_free(head);
2964 * the new layout will be added to an empty one, it
2965 * still needs to be adjusted below
2969 } else if (!llapi_layout_is_composite(head)) {
2971 "%s setstripe: '%s' not a composite file\n",
2973 llapi_layout_free(head);
2977 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2980 "%s setstripe: cannot get prev extent: %s\n",
2981 progname, strerror(errno));
2982 llapi_layout_free(head);
2986 llapi_layout_free(head);
2990 /* Make sure we use the first component of the layout to be added. */
2991 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2994 "%s setstripe: cannot move component cursor: %s\n",
2995 progname, strerror(errno));
2999 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3001 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3002 progname, strerror(errno));
3006 if (start == 0 && end == 0) {
3007 rc = layout_extend_comp(layout, start, &end,
3008 comp_add ? prev_end : 0);
3013 if (start > prev_end || end < prev_end) {
3015 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3016 progname, start, end, prev_end);
3020 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3023 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3024 progname, prev_end, end, strerror(errno));
3031 static int mirror_adjust_first_extents(struct mirror_args *list)
3039 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3042 list = list->m_next;
3048 static inline bool arg_is_eof(char *arg)
3050 return !strncmp(arg, "-1", strlen("-1")) ||
3051 !strncmp(arg, "EOF", strlen("EOF")) ||
3052 !strncmp(arg, "eof", strlen("eof"));
3056 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3058 * Return: Valid mirror_args pointer on success and
3059 * NULL if memory allocation fails.
3061 static struct mirror_args *lfs_mirror_alloc(void)
3063 struct mirror_args *mirror = NULL;
3066 mirror = calloc(1, sizeof(*mirror));
3068 mirror->m_inherit = false;
3079 * lfs_mirror_free() - Free memory allocated for a mirror argument
3081 * @mirror: Previously allocated mirror argument structure by
3082 * lfs_mirror_alloc().
3084 * Free memory allocated for @mirror.
3088 static void lfs_mirror_free(struct mirror_args *mirror)
3090 if (mirror->m_layout)
3091 llapi_layout_free(mirror->m_layout);
3096 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3097 * @mirror_list: Previously allocated mirror list.
3099 * Free memory allocated for @mirror_list.
3103 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3105 struct mirror_args *next_mirror = NULL;
3107 while (mirror_list) {
3108 next_mirror = mirror_list->m_next;
3109 lfs_mirror_free(mirror_list);
3110 mirror_list = next_mirror;
3122 LFS_COMP_NO_VERIFY_OPT,
3124 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3126 LFS_MIRROR_STATE_OPT,
3128 LFS_MIRROR_INDEX_OPT,
3129 LFS_LAYOUT_FOREIGN_OPT,
3135 static int lfs_setstripe_internal(int argc, char **argv,
3136 enum setstripe_origin opc)
3138 struct lfs_setstripe_args lsa = { 0 };
3139 struct llapi_stripe_param *param = NULL;
3140 struct find_param migrate_mdt_param = {
3150 unsigned long long size_units = 1;
3151 bool migrate_mode = false;
3152 bool migrate_mdt_mode = false;
3153 bool setstripe_mode = false;
3154 bool migration_block = false;
3155 __u64 migration_flags = 0;
3156 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3157 int comp_del = 0, comp_set = 0;
3160 struct llapi_layout *layout = NULL;
3161 struct llapi_layout **lpp = &layout;
3162 bool mirror_mode = false;
3163 bool has_m_file = false;
3164 __u32 mirror_count = 0;
3165 enum mirror_flags mirror_flags = 0;
3166 struct mirror_args *mirror_list = NULL;
3167 struct mirror_args *new_mirror = NULL;
3168 struct mirror_args *last_mirror = NULL;
3169 __u16 mirror_id = 0;
3171 bool from_yaml = false;
3172 bool from_copy = false;
3173 char *template = NULL;
3174 bool foreign_mode = false;
3176 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3177 char *mode_opt = NULL;
3178 mode_t previous_umask = 0;
3179 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3181 struct option long_opts[] = {
3182 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3183 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3184 /* --block is only valid in migrate mode */
3185 { .val = 'b', .name = "block", .has_arg = no_argument },
3186 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3187 { .val = LFS_COMP_ADD_OPT,
3188 .name = "comp-add", .has_arg = no_argument },
3189 { .val = LFS_COMP_ADD_OPT,
3190 .name = "component-add", .has_arg = no_argument },
3191 { .val = LFS_COMP_DEL_OPT,
3192 .name = "comp-del", .has_arg = no_argument },
3193 { .val = LFS_COMP_DEL_OPT,
3194 .name = "component-del", .has_arg = no_argument },
3195 { .val = LFS_COMP_FLAGS_OPT,
3196 .name = "comp-flags", .has_arg = required_argument },
3197 { .val = LFS_COMP_FLAGS_OPT,
3198 .name = "component-flags",
3199 .has_arg = required_argument },
3200 { .val = LFS_COMP_SET_OPT,
3201 .name = "comp-set", .has_arg = no_argument },
3202 { .val = LFS_COMP_SET_OPT,
3203 .name = "component-set",
3204 .has_arg = no_argument},
3205 { .val = LFS_COMP_NO_VERIFY_OPT,
3206 .name = "no-verify", .has_arg = no_argument},
3207 { .val = LFS_LAYOUT_FLAGS_OPT,
3208 .name = "flags", .has_arg = required_argument},
3209 { .val = LFS_LAYOUT_FOREIGN_OPT,
3210 .name = "foreign", .has_arg = optional_argument},
3211 { .val = LFS_MIRROR_ID_OPT,
3212 .name = "mirror-id", .has_arg = required_argument},
3213 { .val = LFS_MODE_OPT,
3214 .name = "mode", .has_arg = required_argument},
3215 { .val = LFS_LAYOUT_COPY,
3216 .name = "copy", .has_arg = required_argument},
3217 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3218 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3219 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3220 { .val = 'C', .name = "overstripe-count",
3221 .has_arg = required_argument},
3222 { .val = 'd', .name = "delete", .has_arg = no_argument},
3223 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3224 /* --non-direct is only valid in migrate mode */
3225 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3226 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3227 { .val = 'E', .name = "component-end",
3228 .has_arg = required_argument},
3229 { .val = 'f', .name = "file", .has_arg = required_argument },
3230 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3231 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3232 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3233 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3234 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3235 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3236 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3237 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3238 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3239 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3240 { .val = 'L', .name = "layout", .has_arg = required_argument },
3241 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3242 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3243 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3244 /* --non-block is only valid in migrate mode */
3245 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3246 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3247 { .val = 'o', .name = "ost", .has_arg = required_argument },
3248 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3249 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3250 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3252 { .val = 'p', .name = "pool", .has_arg = required_argument },
3253 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3254 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3255 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3256 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3257 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3258 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3259 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3260 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3261 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3262 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3263 /* --verbose is only valid in migrate mode */
3264 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3265 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3266 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3267 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3268 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3271 setstripe_args_init(&lsa);
3273 migrate_mode = (opc == SO_MIGRATE);
3274 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3275 setstripe_mode = (opc == SO_SETSTRIPE);
3276 if (opc == SO_MIRROR_DELETE) {
3278 mirror_flags = MF_DESTROY;
3281 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3283 while ((c = getopt_long(argc, argv,
3284 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3285 long_opts, NULL)) >= 0) {
3291 case LFS_COMP_ADD_OPT:
3294 case LFS_COMP_DEL_OPT:
3297 case LFS_COMP_FLAGS_OPT:
3298 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3299 &lsa.lsa_comp_neg_flags);
3302 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3304 "%s: inverted flags are not supported\n",
3309 case LFS_COMP_SET_OPT:
3312 case LFS_COMP_NO_VERIFY_OPT:
3313 mirror_flags |= MF_NO_VERIFY;
3315 case LFS_MIRROR_ID_OPT:
3316 mirror_id = strtoul(optarg, &end, 0);
3317 if (*end != '\0' || mirror_id == 0) {
3319 "%s %s: invalid mirror ID '%s'\n",
3320 progname, argv[0], optarg);
3324 case LFS_LAYOUT_FLAGS_OPT: {
3327 /* check for numeric flags (foreign and mirror cases) */
3328 if (setstripe_mode && !mirror_mode && !last_mirror) {
3329 flags = strtoul(optarg, &end, 16);
3332 "%s %s: bad flags '%s'\n",
3333 progname, argv[0], optarg);
3339 if (!mirror_mode || !last_mirror) {
3341 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3346 result = comp_str2flags(optarg, &last_mirror->m_flags,
3353 "%s: inverted flags are not supported\n",
3358 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3360 "%s: unsupported mirror flags: %s\n",
3367 case LFS_LAYOUT_FOREIGN_OPT:
3369 /* check pure numeric */
3370 type = strtoul(optarg, &end, 0);
3373 type = check_foreign_type_name(optarg);
3374 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3376 "%s %s: unrecognized foreign type '%s'\n",
3383 foreign_mode = true;
3388 mode = strtoul(mode_opt, &end, 8);
3391 "%s %s: bad mode '%s'\n",
3392 progname, argv[0], mode_opt);
3395 previous_umask = umask(0);
3398 case LFS_LAYOUT_COPY:
3403 if (!migrate_mode) {
3405 "%s %s: -b|--block valid only for migrate command\n",
3409 migration_block = true;
3412 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3414 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3418 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3421 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3424 "%s %s: invalid stripe count '%s'\n",
3425 progname, argv[0], optarg);
3429 if (lsa.lsa_stripe_count == -1)
3430 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3433 /* delete the default striping pattern */
3435 if (opc == SO_MIRROR_SPLIT) {
3438 "%s %s: -d cannot used with -f\n",
3442 mirror_flags |= MF_DESTROY;
3446 if (!migrate_mode) {
3448 "%s %s: -D|--non-direct is valid only for migrate command\n",
3452 migration_flags |= MIGRATION_NONDIRECT;
3455 if (lsa.lsa_comp_end != 0) {
3456 result = comp_args_to_layout(lpp, &lsa, true);
3458 fprintf(stderr, "%s: invalid layout\n",
3463 setstripe_args_init_inherit(&lsa);
3466 if (arg_is_eof(optarg)) {
3467 lsa.lsa_comp_end = LUSTRE_EOF;
3469 result = llapi_parse_size(optarg,
3474 "%s %s: invalid component end '%s'\n",
3475 progname, argv[0], optarg);
3481 if (!migrate_mode) {
3483 "--mdt-hash is valid only for migrate command\n");
3487 lsa.lsa_pattern = check_hashtype(optarg);
3488 if (lsa.lsa_pattern == 0) {
3490 "%s %s: bad stripe hash type '%s'\n",
3491 progname, argv[0], optarg);
3496 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3499 "%s %s: invalid stripe offset '%s'\n",
3500 progname, argv[0], optarg);
3503 if (lsa.lsa_stripe_off == -1)
3504 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3507 comp_id = strtoul(optarg, &end, 0);
3508 if (*end != '\0' || comp_id == 0 ||
3509 comp_id > LCME_ID_MAX) {
3511 "%s %s: invalid component ID '%s'\n",
3512 progname, argv[0], optarg);
3517 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3519 "error: %s: invalid option: %s\n",
3520 progname, argv[optopt + 1]);
3523 if (opc == SO_MIRROR_EXTEND) {
3526 "error: %s: '-N' must exist in front of '%s'\n",
3527 progname, argv[optopt + 1]);
3530 last_mirror->m_file = optarg;
3531 last_mirror->m_count = 1;
3535 mirror_list = lfs_mirror_alloc();
3536 mirror_list->m_file = optarg;
3541 if (strcmp(argv[optind - 1], "mdt") == 0) {
3542 /* Can be only the first component */
3546 "error: 'mdt' layout can be only the first one\n");
3549 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3552 "error: 'mdt' layout size is too big\n");
3555 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3556 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3559 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3565 if (!migrate_mode) {
3567 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3571 migrate_mdt_mode = true;
3572 lsa.lsa_nr_tgts = parse_targets(tgts,
3573 sizeof(tgts) / sizeof(__u32),
3574 lsa.lsa_nr_tgts, optarg, NULL);
3575 if (lsa.lsa_nr_tgts < 0) {
3577 "%s %s: invalid MDT target(s) '%s'\n",
3578 progname, argv[0], optarg);
3582 lsa.lsa_tgts = tgts;
3583 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3584 lsa.lsa_stripe_off = tgts[0];
3587 if (!migrate_mode) {
3589 "%s %s: -n|--non-block valid only for migrate command\n",
3593 migration_flags |= MIGRATION_NONBLOCK;
3596 if (opc == SO_SETSTRIPE) {
3597 opc = SO_MIRROR_CREATE;
3602 mirror_count = strtoul(optarg, &end, 0);
3603 if (*end != '\0' || mirror_count == 0) {
3605 "error: %s: bad mirror count: %s\n",
3612 new_mirror = lfs_mirror_alloc();
3613 new_mirror->m_count = mirror_count;
3616 mirror_list = new_mirror;
3619 /* wrap up last mirror */
3620 if (!setstripe_args_specified(&lsa))
3621 last_mirror->m_inherit = true;
3622 if (lsa.lsa_comp_end == 0)
3623 lsa.lsa_comp_end = LUSTRE_EOF;
3625 result = comp_args_to_layout(lpp, &lsa, true);
3627 lfs_mirror_free(new_mirror);
3631 setstripe_args_init_inherit(&lsa);
3633 last_mirror->m_next = new_mirror;
3636 last_mirror = new_mirror;
3637 lpp = &last_mirror->m_layout;
3640 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3641 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3643 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3645 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3647 "%s %s: -o|--ost incompatible with DoM layout\n",
3652 * -o allows overstriping, and must note it because
3653 * parse_targets is shared with MDT striping, which
3654 * does not allow duplicates
3656 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3657 lsa.lsa_nr_tgts = parse_targets(tgts,
3658 sizeof(tgts) / sizeof(__u32),
3659 lsa.lsa_nr_tgts, optarg,
3661 if (lsa.lsa_nr_tgts < 0) {
3663 "%s %s: invalid OST target(s) '%s'\n",
3664 progname, argv[0], optarg);
3668 lsa.lsa_tgts = tgts;
3669 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3670 lsa.lsa_stripe_off = tgts[0];
3675 lsa.lsa_pool_name = optarg;
3677 if (strlen(lsa.lsa_pool_name) == 0 ||
3678 strncmp(lsa.lsa_pool_name, "none",
3679 LOV_MAXPOOLNAME) == 0)
3680 lsa.lsa_pool_name = NULL;
3683 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3687 "%s %s: invalid stripe size '%s'\n",
3688 progname, argv[0], optarg);
3693 if (!migrate_mode) {
3695 "%s %s: -v|--verbose valid only for migrate command\n",
3699 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3700 migration_flags = MIGRATION_VERBOSE;
3710 result = llapi_parse_size(optarg,
3711 &lsa.lsa_extension_size,
3715 "%s %s: invalid extension size '%s'\n",
3716 progname, argv[0], optarg);
3720 lsa.lsa_extension_comp = true;
3723 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3724 progname, argv[0], argv[optind - 1]);
3729 fname = argv[optind];
3731 if (optind == argc) {
3732 fprintf(stderr, "%s %s: FILE must be specified\n",
3737 /* lfs migrate $filename should keep the file's layout by default */
3738 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3742 if (xattr && !foreign_mode) {
3744 * only print a warning as this is harmless and will be ignored
3747 "%s %s: xattr has been specified for non-foreign layout\n",
3749 } else if (foreign_mode && !xattr) {
3751 "%s %s: xattr must be provided in foreign mode\n",
3756 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3757 comp_set || comp_id || delete || from_copy ||
3758 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3761 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3766 if (mirror_mode && mirror_count == 0) {
3768 "error: %s: --mirror-count|-N option is required\n",
3775 if (!setstripe_args_specified(&lsa))
3776 last_mirror->m_inherit = true;
3777 if (lsa.lsa_comp_end == 0)
3778 lsa.lsa_comp_end = LUSTRE_EOF;
3781 if (lsa.lsa_comp_end != 0) {
3782 result = comp_args_to_layout(lpp, &lsa, true);
3784 fprintf(stderr, "error: %s: invalid layout\n",
3791 if (mirror_flags & MF_NO_VERIFY) {
3792 if (opc != SO_MIRROR_EXTEND) {
3794 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3798 } else if (!has_m_file) {
3800 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3807 if (comp_set && !comp_id) {
3809 "%s %s: --component-set doesn't have component-id set\n",
3814 if ((delete + comp_set + comp_del + comp_add) > 1) {
3816 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3821 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3822 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3824 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3829 if ((comp_set || comp_del) &&
3830 (setstripe_args_specified(&lsa) || layout != NULL)) {
3832 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3837 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3839 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3844 if (comp_add || comp_del) {
3847 result = lstat(fname, &st);
3848 if (result == 0 && S_ISDIR(st.st_mode)) {
3850 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3857 "error: %s: can't use --component-add or --component-del for mirror operation\n",
3866 "%s %s: option -E must be specified with --component-add\n",
3872 if (from_yaml && from_copy) {
3874 "%s: can't specify --yaml and --copy together\n",
3879 if ((from_yaml || from_copy) &&
3880 (setstripe_args_specified(&lsa) || layout != NULL)) {
3882 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
3887 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3889 "%s %s: options --non-block and --block are mutually exclusive\n",
3894 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3895 opc != SO_MIRROR_DELETE && comp_id != 0) {
3897 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3902 if (migrate_mdt_mode) {
3903 struct lmv_user_md *lmu;
3905 /* initialize migrate mdt parameters */
3906 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3907 LMV_USER_MAGIC_SPECIFIC));
3910 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3911 progname, argv[0], strerror(ENOMEM));
3915 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3916 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3917 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3919 "%s %s: migrate should specify MDT index\n",
3924 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3925 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3926 lmu->lum_hash_type = lsa.lsa_pattern;
3928 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
3929 if (lsa.lsa_pool_name) {
3930 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3931 sizeof(lmu->lum_pool_name) - 1);
3932 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3934 if (lsa.lsa_nr_tgts > 1) {
3937 if (lsa.lsa_stripe_count > 0 &&
3938 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3939 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3941 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3942 progname, lsa.lsa_stripe_count,
3948 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3949 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3950 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3951 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3953 lmu->lum_magic = LMV_USER_MAGIC;
3956 migrate_mdt_param.fp_lmv_md = lmu;
3957 migrate_mdt_param.fp_migrate = 1;
3958 } else if (!layout) {
3959 /* initialize stripe parameters */
3960 param = calloc(1, offsetof(typeof(*param),
3961 lsp_osts[lsa.lsa_nr_tgts]));
3964 "%s %s: cannot allocate memory for parameters: %s\n",
3965 progname, argv[0], strerror(ENOMEM));
3970 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3971 param->lsp_stripe_size = lsa.lsa_stripe_size;
3972 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3973 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3974 param->lsp_stripe_count = -1;
3976 param->lsp_stripe_count = lsa.lsa_stripe_count;
3978 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3979 param->lsp_stripe_offset = -1;
3981 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3982 param->lsp_stripe_pattern =
3983 llapi_pattern_to_lov(lsa.lsa_pattern);
3984 if (param->lsp_stripe_pattern == EINVAL) {
3985 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3990 param->lsp_pool = lsa.lsa_pool_name;
3991 param->lsp_is_specific = false;
3992 if (lsa.lsa_nr_tgts > 0) {
3993 if (lsa.lsa_stripe_count > 0 &&
3994 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3995 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3996 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3998 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3999 argv[0], lsa.lsa_stripe_count,
4005 param->lsp_is_specific = true;
4006 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4007 memcpy(param->lsp_osts, tgts,
4008 sizeof(*tgts) * lsa.lsa_nr_tgts);
4013 /* generate a layout from a YAML template */
4014 result = lfs_comp_create_from_yaml(template, &layout,
4018 "error: %s: can't create composite layout from template file %s\n",
4024 if (layout != NULL || mirror_list != NULL) {
4026 result = mirror_adjust_first_extents(mirror_list);
4028 result = layout_adjust_first_extent(fname, layout,
4030 if (result == -ENODATA)
4032 else if (result != 0) {
4033 fprintf(stderr, "error: %s: invalid layout\n",
4039 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4041 layout = llapi_layout_get_by_path(template ?: fname, 0);
4044 "%s: can't create composite layout from file %s.\n",
4045 progname, template ?: fname);
4050 if (migrate_mdt_mode) {
4051 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4052 } else if (migrate_mode) {
4053 result = lfs_migrate(fname, migration_flags, param,
4055 } else if (comp_set != 0) {
4056 result = lfs_component_set(fname, comp_id,
4058 lsa.lsa_comp_neg_flags);
4059 } else if (comp_del != 0) {
4060 result = lfs_component_del(fname, comp_id,
4062 lsa.lsa_comp_neg_flags);
4063 } else if (comp_add != 0) {
4064 result = lfs_component_add(fname, layout);
4065 } else if (opc == SO_MIRROR_CREATE) {
4066 result = mirror_create(fname, mirror_list);
4067 } else if (opc == SO_MIRROR_EXTEND) {
4068 result = mirror_extend(fname, mirror_list,
4070 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4071 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4073 "%s: no mirror specified to delete from '%s'\n",
4077 if (lsa.lsa_pool_name)
4078 mirror_flags |= MF_COMP_POOL;
4079 else if (mirror_id != 0)
4080 comp_id = mirror_id;
4082 mirror_flags |= MF_COMP_ID;
4083 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4085 has_m_file ? mirror_list->m_file :
4087 } else if (layout) {
4088 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4094 } else if (foreign_mode) {
4095 result = llapi_file_create_foreign(fname, mode, type,
4102 result = llapi_file_open_param(fname,
4111 /* Save the first error encountered. */
4119 umask(previous_umask);
4122 free(migrate_mdt_param.fp_lmv_md);
4123 llapi_layout_free(layout);
4124 lfs_mirror_list_free(mirror_list);
4129 llapi_layout_free(layout);
4130 lfs_mirror_list_free(mirror_list);
4134 static int lfs_poollist(int argc, char **argv)
4139 return llapi_poollist(argv[1]);
4142 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4143 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4148 char *endptr = "AD";
4153 else if (str[0] == '-')
4159 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4160 long long val = strtoll(timebuf, &endptr, 0);
4165 unit *= 52; /* 52 weeks + 1 day below */
4166 case 'w': /* fallthrough */
4168 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4169 param->fp_time_margin *= (1 + unit / 52);
4170 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4171 case '\0': /* days are default unit if none used */
4172 case 'd': /* fallthrough */
4174 case 'h': /* fallthrough */
4176 case 'm': /* fallthrough */
4178 case 's': /* fallthrough */
4180 /* don't need to multiply by 1 for seconds */
4183 "%s find: bad time string '%s': %s\n",
4184 progname, timebuf, strerror(EINVAL));
4188 if (param->fp_time_margin == 0 ||
4189 (*endptr && unit < param->fp_time_margin))
4190 param->fp_time_margin = unit;
4197 fprintf(stderr, "%s find: bad time '%s': too large\n",
4207 static int str2quotaid(__u32 *id, const char *arg)
4209 unsigned long int projid_tmp = 0;
4210 char *endptr = NULL;
4212 projid_tmp = strtoul(arg, &endptr, 10);
4213 if (*endptr != '\0')
4215 if (projid_tmp > UINT32_MAX ||
4216 (projid_tmp == ULONG_MAX && (errno == ERANGE)))
4223 static int name2uid(unsigned int *id, const char *name)
4225 struct passwd *passwd;
4227 passwd = getpwnam(name);
4230 *id = passwd->pw_uid;
4235 static int name2gid(unsigned int *id, const char *name)
4237 struct group *group;
4239 group = getgrnam(name);
4242 *id = group->gr_gid;
4247 static inline int name2projid(unsigned int *id, const char *name)
4252 static int uid2name(char **name, unsigned int id)
4254 struct passwd *passwd;
4256 passwd = getpwuid(id);
4259 *name = passwd->pw_name;
4264 static inline int gid2name(char **name, unsigned int id)
4266 struct group *group;
4268 group = getgrgid(id);
4271 *name = group->gr_name;
4276 static int name2layout(__u32 *layout, char *name)
4278 char *ptr, *layout_name;
4281 for (ptr = name; ; ptr = NULL) {
4282 layout_name = strtok(ptr, ",");
4285 if (strcmp(layout_name, "released") == 0)
4286 *layout |= LOV_PATTERN_F_RELEASED;
4287 else if (strcmp(layout_name, "raid0") == 0)
4288 *layout |= LOV_PATTERN_RAID0;
4289 else if (strcmp(layout_name, "mdt") == 0)
4290 *layout |= LOV_PATTERN_MDT;
4291 else if (strcmp(layout_name, "overstriping") == 0)
4292 *layout |= LOV_PATTERN_OVERSTRIPING;
4299 static int lfs_find(int argc, char **argv)
4304 struct find_param param = {
4307 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4309 struct option long_opts[] = {
4310 { .val = 'A', .name = "atime", .has_arg = required_argument },
4311 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4312 { .val = 'B', .name = "btime", .has_arg = required_argument },
4313 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4314 { .val = LFS_COMP_COUNT_OPT,
4315 .name = "comp-count", .has_arg = required_argument },
4316 { .val = LFS_COMP_COUNT_OPT,
4317 .name = "component-count",
4318 .has_arg = required_argument },
4319 { .val = LFS_COMP_FLAGS_OPT,
4320 .name = "comp-flags", .has_arg = required_argument },
4321 { .val = LFS_COMP_FLAGS_OPT,
4322 .name = "component-flags",
4323 .has_arg = required_argument },
4324 { .val = LFS_COMP_START_OPT,
4325 .name = "comp-start", .has_arg = required_argument },
4326 { .val = LFS_COMP_START_OPT,
4327 .name = "component-start",
4328 .has_arg = required_argument },
4329 { .val = LFS_MIRROR_STATE_OPT,
4330 .name = "mirror-state", .has_arg = required_argument },
4331 { .val = LFS_LAYOUT_FOREIGN_OPT,
4332 .name = "foreign", .has_arg = optional_argument},
4333 { .val = LFS_NEWERXY_OPT,
4334 .name = "newer", .has_arg = required_argument},
4335 { .val = LFS_NEWERXY_OPT,
4336 .name = "neweraa", .has_arg = required_argument},
4337 { .val = LFS_NEWERXY_OPT,
4338 .name = "neweram", .has_arg = required_argument},
4339 { .val = LFS_NEWERXY_OPT,
4340 .name = "newerac", .has_arg = required_argument},
4341 { .val = LFS_NEWERXY_OPT,
4342 .name = "newerab", .has_arg = required_argument},
4343 { .val = LFS_NEWERXY_OPT,
4344 .name = "newerma", .has_arg = required_argument},
4345 { .val = LFS_NEWERXY_OPT,
4346 .name = "newermm", .has_arg = required_argument},
4347 { .val = LFS_NEWERXY_OPT,
4348 .name = "newermc", .has_arg = required_argument},
4349 { .val = LFS_NEWERXY_OPT,
4350 .name = "newermb", .has_arg = required_argument},
4351 { .val = LFS_NEWERXY_OPT,
4352 .name = "newerca", .has_arg = required_argument},
4353 { .val = LFS_NEWERXY_OPT,
4354 .name = "newercm", .has_arg = required_argument},
4355 { .val = LFS_NEWERXY_OPT,
4356 .name = "newercc", .has_arg = required_argument},
4357 { .val = LFS_NEWERXY_OPT,
4358 .name = "newercb", .has_arg = required_argument},
4359 { .val = LFS_NEWERXY_OPT,
4360 .name = "newerba", .has_arg = required_argument},
4361 { .val = LFS_NEWERXY_OPT,
4362 .name = "newerbm", .has_arg = required_argument},
4363 { .val = LFS_NEWERXY_OPT,
4364 .name = "newerbc", .has_arg = required_argument},
4365 { .val = LFS_NEWERXY_OPT,
4366 .name = "newerbb", .has_arg = required_argument},
4367 { .val = LFS_NEWERXY_OPT,
4368 .name = "newerBa", .has_arg = required_argument},
4369 { .val = LFS_NEWERXY_OPT,
4370 .name = "newerBm", .has_arg = required_argument},
4371 { .val = LFS_NEWERXY_OPT,
4372 .name = "newerBc", .has_arg = required_argument},
4373 { .val = LFS_NEWERXY_OPT,
4374 .name = "newerBB", .has_arg = required_argument},
4375 { .val = LFS_NEWERXY_OPT,
4376 .name = "newerat", .has_arg = required_argument},
4377 { .val = LFS_NEWERXY_OPT,
4378 .name = "newermt", .has_arg = required_argument},
4379 { .val = LFS_NEWERXY_OPT,
4380 .name = "newerct", .has_arg = required_argument},
4381 { .val = LFS_NEWERXY_OPT,
4382 .name = "newerbt", .has_arg = required_argument},
4383 { .val = LFS_NEWERXY_OPT,
4384 .name = "newerBt", .has_arg = required_argument},
4385 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4386 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4387 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4388 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4389 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4390 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4391 { .val = 'E', .name = "component-end",
4392 .has_arg = required_argument },
4393 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4394 { .val = LFS_LAYOUT_FOREIGN_OPT,
4395 .name = "foreign", .has_arg = optional_argument},
4396 { .val = 'g', .name = "gid", .has_arg = required_argument },
4397 { .val = 'G', .name = "group", .has_arg = required_argument },
4398 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4399 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4400 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4401 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4402 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4403 { .val = 'L', .name = "layout", .has_arg = required_argument },
4404 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4405 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4406 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4407 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4408 { .val = 'n', .name = "name", .has_arg = required_argument },
4409 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4410 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4411 { .val = 'O', .name = "obd", .has_arg = required_argument },
4412 { .val = 'O', .name = "ost", .has_arg = required_argument },
4413 /* no short option for pool yet, can be 'p' after 2.18 */
4414 { .val = LFS_POOL_OPT,
4415 .name = "pool", .has_arg = required_argument },
4416 { .val = '0', .name = "print0", .has_arg = no_argument },
4417 { .val = 'P', .name = "print", .has_arg = no_argument },
4418 { .val = LFS_PROJID_OPT,
4419 .name = "projid", .has_arg = required_argument },
4420 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4421 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4422 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4423 { .val = 's', .name = "size", .has_arg = required_argument },
4424 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4425 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4426 { .val = 't', .name = "type", .has_arg = required_argument },
4427 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4428 { .val = 'u', .name = "uid", .has_arg = required_argument },
4429 { .val = 'U', .name = "user", .has_arg = required_argument },
4430 { .val = 'z', .name = "extension-size",
4431 .has_arg = required_argument },
4432 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4433 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4434 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4448 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4449 while ((c = getopt_long_only(argc, argv,
4450 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4451 long_opts, &optidx)) >= 0) {
4456 /* '!' is part of option */
4458 * when getopt_long_only() finds a string which is not
4459 * an option nor a known option argument it returns 1
4460 * in that case if we already have found pathstart and pathend
4461 * (i.e. we have the list of pathnames),
4462 * the only supported value is "!"
4464 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4465 if (!isoption && pathend != -1) {
4467 "err: %s: filename|dirname must either precede options or follow options\n",
4472 if (!isoption && pathstart == -1)
4473 pathstart = optind - 1;
4474 if (isoption && pathstart != -1 && pathend == -1)
4475 pathend = optind - 2;
4482 * unknown; opt is "!" or path component,
4483 * checking done above.
4485 if (strcmp(optarg, "!") == 0)
4489 xtime = ¶m.fp_atime;
4490 xsign = ¶m.fp_asign;
4491 param.fp_exclude_atime = !!neg_opt;
4492 /* no break, this falls through to 'B' for btime */
4495 xtime = ¶m.fp_btime;
4496 xsign = ¶m.fp_bsign;
4497 param.fp_exclude_btime = !!neg_opt;
4499 /* no break, this falls through to 'C' for ctime */
4502 xtime = ¶m.fp_ctime;
4503 xsign = ¶m.fp_csign;
4504 param.fp_exclude_ctime = !!neg_opt;
4506 /* no break, this falls through to 'M' for mtime */
4509 xtime = ¶m.fp_mtime;
4510 xsign = ¶m.fp_msign;
4511 param.fp_exclude_mtime = !!neg_opt;
4513 rc = set_time(¶m, &t, xtime, optarg);
4514 if (rc == LONG_MAX) {
4522 if (optarg[0] == '+') {
4523 param.fp_blocks_sign = -1;
4525 } else if (optarg[0] == '-') {
4526 param.fp_blocks_sign = 1;
4530 param.fp_blocks_units = 1024;
4531 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4532 ¶m.fp_blocks_units, 0);
4534 fprintf(stderr, "error: bad blocks '%s'\n",
4538 param.fp_check_blocks = 1;
4539 param.fp_exclude_blocks = !!neg_opt;
4541 case LFS_COMP_COUNT_OPT:
4542 if (optarg[0] == '+') {
4543 param.fp_comp_count_sign = -1;
4545 } else if (optarg[0] == '-') {
4546 param.fp_comp_count_sign = 1;
4550 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4551 if (*endptr != '\0') {
4553 "error: bad component count '%s'\n",
4557 param.fp_check_comp_count = 1;
4558 param.fp_exclude_comp_count = !!neg_opt;
4560 case LFS_COMP_FLAGS_OPT:
4561 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4562 ¶m.fp_comp_neg_flags);
4565 "error: bad component flags '%s'\n",
4569 param.fp_check_comp_flags = 1;
4571 __u32 flags = param.fp_comp_neg_flags;
4573 param.fp_comp_neg_flags = param.fp_comp_flags;
4574 param.fp_comp_flags = flags;
4577 case LFS_COMP_START_OPT:
4578 if (optarg[0] == '+') {
4579 param.fp_comp_start_sign = -1;
4581 } else if (optarg[0] == '-') {
4582 param.fp_comp_start_sign = 1;
4586 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4587 ¶m.fp_comp_start_units, 0);
4590 "error: bad component start '%s'\n",
4594 param.fp_check_comp_start = 1;
4595 param.fp_exclude_comp_start = !!neg_opt;
4597 case LFS_MIRROR_STATE_OPT:
4598 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4599 ¶m.fp_mirror_neg_state);
4602 "error: bad mirrored file state '%s'\n",
4606 param.fp_check_mirror_state = 1;
4608 __u16 state = param.fp_mirror_neg_state;
4610 param.fp_mirror_neg_state =
4611 param.fp_mirror_state;
4612 param.fp_mirror_state = state;
4616 if (optarg[0] == '+') {
4617 param.fp_stripe_count_sign = -1;
4619 } else if (optarg[0] == '-') {
4620 param.fp_stripe_count_sign = 1;
4624 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4625 if (*endptr != '\0') {
4627 "error: bad stripe_count '%s'\n",
4632 param.fp_check_stripe_count = 1;
4633 param.fp_exclude_stripe_count = !!neg_opt;
4636 param.fp_max_depth = strtol(optarg, 0, 0);
4639 if (optarg[0] == '+') {
4640 param.fp_comp_end_sign = -1;
4642 } else if (optarg[0] == '-') {
4643 param.fp_comp_end_sign = 1;
4647 if (arg_is_eof(optarg)) {
4648 param.fp_comp_end = LUSTRE_EOF;
4649 param.fp_comp_end_units = 1;
4652 rc = llapi_parse_size(optarg,
4654 ¶m.fp_comp_end_units, 0);
4658 "error: bad component end '%s'\n",
4662 param.fp_check_comp_end = 1;
4663 param.fp_exclude_comp_end = !!neg_opt;
4665 case LFS_LAYOUT_FOREIGN_OPT: {
4666 /* all types by default */
4667 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4670 /* check pure numeric */
4671 type = strtoul(optarg, &endptr, 0);
4674 type = check_foreign_type_name(optarg);
4675 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4677 "%s %s: unknown foreign type '%s'\n",
4684 param.fp_foreign_type = type;
4685 param.fp_check_foreign = 1;
4686 param.fp_exclude_foreign = !!neg_opt;
4689 case LFS_NEWERXY_OPT: {
4695 time_t ref = time(NULL);
4697 /* no need to check bad options, they won't get here */
4698 if (strlen(long_opts[optidx].name) == 7) {
4699 x = long_opts[optidx].name[5];
4700 y = long_opts[optidx].name[6];
4704 static const char *const fmts[] = {
4705 "%Y-%m-%d %H:%M:%S",
4708 "%H:%M:%S", /* sometime today */
4717 for (i = 0; fmts[i] != NULL; i++) {
4720 /* Init for times relative to today */
4721 if (strncmp(fmts[i], "%H", 2) == 0)
4722 localtime_r(&ref, &tm);
4724 memset(&tm, 0, sizeof(tm));
4725 ptr = strptime(optarg, fmts[i], &tm);
4727 while (ptr && isspace(*ptr))
4729 if (ptr == optarg + strlen(optarg)) {
4737 "%s: invalid time '%s'\n",
4740 "supported formats are:\n ");
4741 for (i = 0; fmts[i] != NULL; i++)
4742 fprintf(stderr, "'%s', ",
4744 fprintf(stderr, "\n");
4750 } else if (y == 'b' || y == 'B') {
4753 rc = llapi_get_lum_file(optarg, NULL, &stx,
4755 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4756 if (!(stx.stx_mask & STATX_BTIME))
4761 "%s: get btime failed '%s': %s\n",
4767 ref = stx.stx_btime.tv_sec;
4769 struct stat statbuf;
4771 if (stat(optarg, &statbuf) < 0) {
4773 "%s: cannot stat file '%s': %s\n",
4782 ref = statbuf.st_atime;
4785 ref = statbuf.st_mtime;
4788 ref = statbuf.st_ctime;
4792 "%s: invalid Y argument: '%c'\n",
4801 xidx = NEWERXY_ATIME;
4804 xidx = NEWERXY_MTIME;
4807 xidx = NEWERXY_CTIME;
4811 xidx = NEWERXY_BTIME;
4815 "%s: invalid X argument: '%c'\n",
4822 newery = ¶m.fp_newery[xidx][negidx];
4828 *newery = *newery > ref ? ref : *newery;
4830 *newery = *newery > ref ? *newery : ref;
4832 param.fp_newerxy = 1;
4837 rc = name2gid(¶m.fp_gid, optarg);
4839 if (str2quotaid(¶m.fp_gid, optarg)) {
4841 "Group/GID: %s cannot be found.\n",
4847 param.fp_exclude_gid = !!neg_opt;
4848 param.fp_check_gid = 1;
4851 param.fp_hash_type = check_hashtype(optarg);
4852 if (param.fp_hash_type == 0) {
4853 fprintf(stderr, "error: bad hash_type '%s'\n",
4858 param.fp_check_hash_type = 1;
4859 param.fp_exclude_hash_type = !!neg_opt;
4865 ret = name2layout(¶m.fp_layout, optarg);
4868 param.fp_exclude_layout = !!neg_opt;
4869 param.fp_check_layout = 1;
4873 rc = name2uid(¶m.fp_uid, optarg);
4875 if (str2quotaid(¶m.fp_uid, optarg)) {
4877 "User/UID: %s cannot be found.\n",
4883 param.fp_exclude_uid = !!neg_opt;
4884 param.fp_check_uid = 1;
4887 param.fp_pattern = (char *)optarg;
4888 param.fp_exclude_pattern = !!neg_opt;
4891 if (optarg[0] == '+') {
4892 param.fp_mirror_count_sign = -1;
4894 } else if (optarg[0] == '-') {
4895 param.fp_mirror_count_sign = 1;
4899 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4900 if (*endptr != '\0') {
4902 "error: bad mirror count '%s'\n",
4906 param.fp_check_mirror_count = 1;
4907 param.fp_exclude_mirror_count = !!neg_opt;
4912 char *buf, *token, *next, *p;
4916 buf = strdup(optarg);
4922 param.fp_exclude_obd = !!neg_opt;
4925 while (token && *token) {
4926 token = strchr(token, ',');
4933 param.fp_exclude_mdt = !!neg_opt;
4934 param.fp_num_alloc_mdts += len;
4935 tmp = realloc(param.fp_mdt_uuid,
4936 param.fp_num_alloc_mdts *
4937 sizeof(*param.fp_mdt_uuid));
4943 param.fp_mdt_uuid = tmp;
4945 param.fp_exclude_obd = !!neg_opt;
4946 param.fp_num_alloc_obds += len;
4947 tmp = realloc(param.fp_obd_uuid,
4948 param.fp_num_alloc_obds *
4949 sizeof(*param.fp_obd_uuid));
4955 param.fp_obd_uuid = tmp;
4957 for (token = buf; token && *token; token = next) {
4958 struct obd_uuid *puuid;
4962 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4965 ¶m.fp_obd_uuid[param.fp_num_obds++];
4967 p = strchr(token, ',');
4974 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4979 strncpy(puuid->uuid, token,
4980 sizeof(puuid->uuid));
4987 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4991 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4993 "Pool name %s is too long (max %d)\n",
4994 optarg, LOV_MAXPOOLNAME);
4999 * We do check for empty pool because empty pool
5000 * is used to find V1 LOV attributes
5002 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5003 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5004 param.fp_exclude_pool = !!neg_opt;
5005 param.fp_check_pool = 1;
5007 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5008 case 'p': /* want this for --pool, to match getstripe/find */
5010 "warning: -p deprecated, use --print0 or -0\n");
5013 param.fp_zero_end = 1;
5015 case 'P': /* we always print, this option is a no-op */
5017 case LFS_PROJID_OPT:
5018 rc = name2projid(¶m.fp_projid, optarg);
5020 if (str2quotaid(¶m.fp_projid, optarg)) {
5022 "Invalid project ID: %s\n",
5028 param.fp_exclude_projid = !!neg_opt;
5029 param.fp_check_projid = 1;
5032 if (optarg[0] == '+') {
5033 param.fp_size_sign = -1;
5035 } else if (optarg[0] == '-') {
5036 param.fp_size_sign = 1;
5040 ret = llapi_parse_size(optarg, ¶m.fp_size,
5041 ¶m.fp_size_units, 0);
5043 fprintf(stderr, "error: bad file size '%s'\n",
5047 param.fp_check_size = 1;
5048 param.fp_exclude_size = !!neg_opt;
5051 if (optarg[0] == '+') {
5052 param.fp_stripe_size_sign = -1;
5054 } else if (optarg[0] == '-') {
5055 param.fp_stripe_size_sign = 1;
5059 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5060 ¶m.fp_stripe_size_units, 0);
5062 fprintf(stderr, "error: bad stripe_size '%s'\n",
5066 param.fp_check_stripe_size = 1;
5067 param.fp_exclude_stripe_size = !!neg_opt;
5070 param.fp_exclude_type = !!neg_opt;
5071 switch (optarg[0]) {
5073 param.fp_type = S_IFBLK;
5076 param.fp_type = S_IFCHR;
5079 param.fp_type = S_IFDIR;
5082 param.fp_type = S_IFREG;
5085 param.fp_type = S_IFLNK;
5088 param.fp_type = S_IFIFO;
5091 param.fp_type = S_IFSOCK;
5094 fprintf(stderr, "error: %s: bad type '%s'\n",
5101 if (optarg[0] == '+') {
5102 param.fp_mdt_count_sign = -1;
5104 } else if (optarg[0] == '-') {
5105 param.fp_mdt_count_sign = 1;
5109 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5110 if (*endptr != '\0') {
5111 fprintf(stderr, "error: bad mdt_count '%s'\n",
5116 param.fp_check_mdt_count = 1;
5117 param.fp_exclude_mdt_count = !!neg_opt;
5120 if (optarg[0] == '+') {
5121 param.fp_ext_size_sign = -1;
5123 } else if (optarg[0] == '-') {
5124 param.fp_ext_size_sign = 1;
5128 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5129 ¶m.fp_ext_size_units, 0);
5131 fprintf(stderr, "error: bad ext-size '%s'\n",
5135 param.fp_ext_size /= SEL_UNIT_SIZE;
5136 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5137 param.fp_check_ext_size = 1;
5138 param.fp_exclude_ext_size = !!neg_opt;
5146 if (pathstart == -1) {
5147 fprintf(stderr, "error: %s: no filename|pathname\n",
5151 } else if (pathend == -1) {
5157 rc = llapi_find(argv[pathstart], ¶m);
5160 pathbad = pathstart;
5162 } while (++pathstart < pathend);
5165 fprintf(stderr, "%s: failed for '%s': %s\n",
5166 progname, argv[pathbad], strerror(-rc));
5169 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5170 free(param.fp_obd_uuid);
5172 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5173 free(param.fp_mdt_uuid);
5178 static int lfs_getstripe_internal(int argc, char **argv,
5179 struct find_param *param)
5181 struct option long_opts[] = {
5182 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5183 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5184 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5185 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5186 { .val = LFS_COMP_COUNT_OPT,
5187 .name = "comp-count", .has_arg = no_argument },
5188 { .val = LFS_COMP_COUNT_OPT,
5189 .name = "component-count", .has_arg = no_argument },
5190 { .val = LFS_COMP_FLAGS_OPT,
5191 .name = "comp-flags", .has_arg = optional_argument },
5192 { .val = LFS_COMP_FLAGS_OPT,
5193 .name = "component-flags", .has_arg = optional_argument },
5194 { .val = LFS_COMP_START_OPT,
5195 .name = "comp-start", .has_arg = optional_argument },
5196 { .val = LFS_COMP_START_OPT,
5197 .name = "component-start", .has_arg = optional_argument },
5198 { .val = LFS_MIRROR_INDEX_OPT,
5199 .name = "mirror-index", .has_arg = required_argument },
5200 { .val = LFS_MIRROR_ID_OPT,
5201 .name = "mirror-id", .has_arg = required_argument },
5202 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5203 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5204 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5205 { .val = 'd', .name = "directory", .has_arg = no_argument },
5206 { .val = 'D', .name = "default", .has_arg = no_argument },
5207 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5208 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5209 { .val = 'F', .name = "fid", .has_arg = no_argument },
5210 { .val = 'g', .name = "generation", .has_arg = no_argument },
5211 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5212 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5213 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5214 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5215 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5216 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5217 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5218 { .val = 'L', .name = "layout", .has_arg = no_argument },
5219 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5220 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5221 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5222 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5223 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5224 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5225 { .val = 'O', .name = "obd", .has_arg = required_argument },
5226 { .val = 'O', .name = "ost", .has_arg = required_argument },
5227 { .val = 'p', .name = "pool", .has_arg = no_argument },
5228 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5229 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5230 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5231 { .val = 'R', .name = "raw", .has_arg = no_argument },
5232 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5233 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5234 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5235 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5236 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5237 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5238 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5239 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5240 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5241 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5245 int pathstart = -1, pathend = -1;
5249 while ((c = getopt_long(argc, argv,
5250 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5251 long_opts, NULL)) != -1) {
5255 /* '!' is part of option */
5256 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5257 if (!isoption && pathend != -1) {
5259 "error: %s: filename|dirname must either precede options or follow options\n",
5263 if (!isoption && pathstart == -1)
5264 pathstart = optind - 1;
5265 if (isoption && pathstart != -1 && pathend == -1)
5266 pathend = optind - 2;
5270 /* unknown: opt is "!" */
5271 if (strcmp(optarg, "!") == 0)
5275 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5276 param->fp_verbose |= VERBOSE_COUNT;
5277 param->fp_max_depth = 0;
5280 case LFS_COMP_COUNT_OPT:
5281 param->fp_verbose |= VERBOSE_COMP_COUNT;
5282 param->fp_max_depth = 0;
5284 case LFS_COMP_FLAGS_OPT:
5286 rc = comp_str2flags(optarg,
5287 ¶m->fp_comp_flags,
5288 ¶m->fp_comp_neg_flags);
5291 "error: %s bad component flags '%s'.\n",
5295 param->fp_check_comp_flags = 1;
5297 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5298 param->fp_max_depth = 0;
5301 case LFS_COMP_START_OPT:
5304 if (tmp[0] == '+') {
5305 param->fp_comp_start_sign = -1;
5307 } else if (tmp[0] == '-') {
5308 param->fp_comp_start_sign = 1;
5311 rc = llapi_parse_size(tmp,
5312 ¶m->fp_comp_start,
5313 ¶m->fp_comp_start_units, 0);
5316 "error: %s bad component start '%s'.\n",
5320 param->fp_check_comp_start = 1;
5322 param->fp_verbose |= VERBOSE_COMP_START;
5323 param->fp_max_depth = 0;
5326 case LFS_MIRROR_INDEX_OPT:
5327 if (optarg[0] == '+') {
5328 param->fp_mirror_index_sign = -1;
5330 } else if (optarg[0] == '-') {
5331 param->fp_mirror_index_sign = 1;
5335 param->fp_mirror_index = strtoul(optarg, &end, 0);
5336 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5337 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5339 "%s %s: invalid mirror index '%s'\n",
5340 progname, argv[0], optarg);
5343 if (param->fp_mirror_id != 0) {
5345 "%s %s: can't specify both mirror index and mirror ID\n",
5349 param->fp_check_mirror_index = 1;
5350 param->fp_exclude_mirror_index = !!neg_opt;
5352 case LFS_MIRROR_ID_OPT:
5353 if (optarg[0] == '+') {
5354 param->fp_mirror_id_sign = -1;
5356 } else if (optarg[0] == '-') {
5357 param->fp_mirror_id_sign = 1;
5361 param->fp_mirror_id = strtoul(optarg, &end, 0);
5362 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5363 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5365 "%s %s: invalid mirror ID '%s'\n",
5366 progname, argv[0], optarg);
5369 if (param->fp_mirror_index != 0) {
5371 "%s %s: can't specify both mirror index and mirror ID\n",
5375 param->fp_check_mirror_id = 1;
5376 param->fp_exclude_mirror_id = !!neg_opt;
5379 param->fp_max_depth = 0;
5382 param->fp_get_default_lmv = 1;
5387 if (tmp[0] == '+') {
5388 param->fp_comp_end_sign = -1;
5390 } else if (tmp[0] == '-') {
5391 param->fp_comp_end_sign = 1;
5395 if (arg_is_eof(tmp)) {
5396 param->fp_comp_end = LUSTRE_EOF;
5397 param->fp_comp_end_units = 1;
5400 rc = llapi_parse_size(tmp,
5401 ¶m->fp_comp_end,
5402 ¶m->fp_comp_end_units, 0);
5406 "error: %s bad component end '%s'.\n",
5410 param->fp_check_comp_end = 1;
5412 param->fp_verbose |= VERBOSE_COMP_END;
5413 param->fp_max_depth = 0;
5417 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5418 param->fp_verbose |= VERBOSE_DFID;
5419 param->fp_max_depth = 0;
5423 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5424 param->fp_verbose |= VERBOSE_GENERATION;
5425 param->fp_max_depth = 0;
5429 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5430 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5431 param->fp_max_depth = 0;
5436 param->fp_comp_id = strtoul(optarg, &end, 0);
5437 if (*end != '\0' || param->fp_comp_id == 0 ||
5438 param->fp_comp_id > LCME_ID_MAX) {
5440 "error: %s bad component id '%s'\n",
5444 param->fp_check_comp_id = 1;
5446 param->fp_max_depth = 0;
5447 param->fp_verbose |= VERBOSE_COMP_ID;
5451 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5452 param->fp_verbose |= VERBOSE_PATTERN;
5453 param->fp_max_depth = 0;
5456 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5459 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5462 if (!(param->fp_verbose & VERBOSE_DETAIL))
5463 param->fp_max_depth = 0;
5464 param->fp_verbose |= VERBOSE_MDTINDEX;
5467 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5468 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5469 param->fp_max_depth = 0;
5473 if (param->fp_obd_uuid) {
5475 "error: %s: only one obduuid allowed",
5479 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5482 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5483 param->fp_verbose |= VERBOSE_POOL;
5484 param->fp_max_depth = 0;
5491 param->fp_recursive = 1;
5497 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5498 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5499 param->fp_max_depth = 0;
5503 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5509 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5510 param->fp_verbose |= VERBOSE_EXT_SIZE;
5511 param->fp_max_depth = 0;
5519 if (pathstart == -1) {
5520 fprintf(stderr, "error: %s: no filename|pathname\n",
5523 } else if (pathend == -1) {
5531 if (param->fp_recursive)
5532 param->fp_max_depth = -1;
5533 else if (param->fp_verbose & VERBOSE_DETAIL)
5534 param->fp_max_depth = 1;
5536 if (!param->fp_verbose)
5537 param->fp_verbose = VERBOSE_DEFAULT;
5538 if (param->fp_quiet)
5539 param->fp_verbose = VERBOSE_OBJID;
5542 rc = llapi_getstripe(argv[pathstart], param);
5543 } while (++pathstart < pathend && !rc);
5546 fprintf(stderr, "error: %s failed for %s.\n",
5547 argv[0], argv[optind - 1]);
5551 static int lfs_tgts(int argc, char **argv)
5553 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5554 struct find_param param;
5555 int index = 0, rc = 0;
5560 if (argc == 2 && !realpath(argv[1], path)) {
5562 fprintf(stderr, "error: invalid path '%s': %s\n",
5563 argv[1], strerror(-rc));
5567 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5568 /* Check if we have a mount point */
5569 if (mntdir[0] == '\0')
5572 memset(¶m, 0, sizeof(param));
5573 if (!strcmp(argv[0], "mdts"))
5574 param.fp_get_lmv = 1;
5576 rc = llapi_ostlist(mntdir, ¶m);
5578 fprintf(stderr, "error: %s: failed on %s\n",
5581 if (path[0] != '\0')
5583 memset(mntdir, 0, PATH_MAX);
5589 static int lfs_getstripe(int argc, char **argv)
5591 struct find_param param = { 0 };
5593 param.fp_max_depth = 1;
5594 return lfs_getstripe_internal(argc, argv, ¶m);
5598 static int lfs_getdirstripe(int argc, char **argv)
5600 struct find_param param = { 0 };
5601 struct option long_opts[] = {
5602 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5603 { .val = 'D', .name = "default", .has_arg = no_argument },
5604 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5605 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5606 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5607 { .val = 'O', .name = "obd", .has_arg = required_argument },
5608 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5609 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5610 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5611 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5615 param.fp_get_lmv = 1;
5617 while ((c = getopt_long(argc, argv,
5618 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5622 param.fp_verbose |= VERBOSE_COUNT;
5625 param.fp_get_default_lmv = 1;
5629 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5631 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5634 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5637 param.fp_verbose |= VERBOSE_HASH_TYPE;
5640 if (param.fp_obd_uuid) {
5642 "error: %s: only one obduuid allowed",
5646 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5649 param.fp_recursive = 1;
5652 param.fp_verbose |= VERBOSE_DETAIL;
5658 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5659 progname, argv[0], argv[optind - 1]);
5667 if (param.fp_recursive)
5668 param.fp_max_depth = -1;
5670 if (!param.fp_verbose)
5671 param.fp_verbose = VERBOSE_DEFAULT;
5674 rc = llapi_getstripe(argv[optind], ¶m);
5675 } while (++optind < argc && !rc);
5678 fprintf(stderr, "error: %s failed for %s.\n",
5679 argv[0], argv[optind - 1]);
5684 MNTDF_INODES = 0x0001,
5685 MNTDF_COOKED = 0x0002,
5686 MNTDF_LAZY = 0x0004,
5687 MNTDF_VERBOSE = 0x0008,
5688 MNTDF_SHOW = 0x0010,
5691 #define COOK(value) \
5694 while (value > 1024) { \
5702 #define CDF "%11llu"
5703 #define HDF "%8.1f%c"
5707 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5709 double avail, used, ratio = 0;
5712 avail = st->os_ffree;
5713 used = st->os_files - st->os_ffree;
5715 avail = st->os_bavail;
5716 used = st->os_blocks - st->os_bfree;
5718 if (avail + used > 0)
5719 ratio = used / (used + avail) * 100;
5721 /* Round up to match df(1) usage percentage */
5722 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5726 * This is to identify various problem states for "lfs df" if .osn_err = true,
5727 * so only show flags reflecting those states by default. Informational states
5728 * are only shown with "-v" and use lower-case names to distinguish them.
5729 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5731 static struct obd_statfs_state_names {
5732 enum obd_statfs_state osn_state;
5733 const char osn_name;
5736 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5737 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5738 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5739 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5740 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5741 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5742 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5743 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5744 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5747 static int showdf(char *mntdir, struct obd_statfs *stat,
5748 char *uuid, enum mntdf_flags flags,
5749 char *type, int index, int rc)
5751 long long avail, used, total;
5753 char *suffix = "KMGTPEZY";
5754 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5755 char tbuf[3 * sizeof(__u64)];
5756 char ubuf[3 * sizeof(__u64)];
5757 char abuf[3 * sizeof(__u64)];
5758 char rbuf[3 * sizeof(__u64)];
5765 if (flags & MNTDF_INODES) {
5766 avail = stat->os_ffree;
5767 used = stat->os_files - stat->os_ffree;
5768 total = stat->os_files;
5770 int shift = flags & MNTDF_COOKED ? 0 : 10;
5772 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5773 used = ((stat->os_blocks - stat->os_bfree) *
5774 stat->os_bsize) >> shift;
5775 total = (stat->os_blocks * stat->os_bsize) >> shift;
5778 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5780 if (flags & MNTDF_COOKED) {
5784 cook_val = (double)total;
5787 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5790 snprintf(tbuf, sizeof(tbuf), CDF, total);
5792 cook_val = (double)used;
5795 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5798 snprintf(ubuf, sizeof(ubuf), CDF, used);
5800 cook_val = (double)avail;
5803 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5806 snprintf(abuf, sizeof(abuf), CDF, avail);
5808 snprintf(tbuf, sizeof(tbuf), CDF, total);
5809 snprintf(ubuf, sizeof(tbuf), CDF, used);
5810 snprintf(abuf, sizeof(tbuf), CDF, avail);
5813 sprintf(rbuf, RDF, ratio);
5814 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5815 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5817 printf("[%s:%d]", type, index);
5819 if (stat->os_state) {
5823 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5824 if (oss_names[i].osn_state & stat->os_state &&
5825 (oss_names[i].osn_err ||
5826 flags & MNTDF_VERBOSE))
5827 printf("%c", oss_names[i].osn_name);
5834 printf(UUF": inactive device\n", uuid);
5837 printf(UUF": %s\n", uuid, strerror(-rc));
5844 struct ll_stat_type {
5849 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5851 struct ll_statfs_data {
5853 struct obd_statfs sd_st;
5856 struct ll_statfs_buf {
5858 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5861 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5862 int ops, struct ll_statfs_buf *lsb)
5864 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5865 struct obd_uuid uuid_buf;
5866 char *poolname = NULL;
5867 struct ll_stat_type types[] = {
5868 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5869 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5870 { .st_name = NULL } };
5871 struct ll_stat_type *tp;
5872 __u64 ost_files = 0;
5873 __u64 ost_ffree = 0;
5881 poolname = strchr(pool, '.');
5883 if (strncmp(fsname, pool, strlen(fsname))) {
5884 fprintf(stderr, "filesystem name incorrect\n");
5892 fd = open(mntdir, O_RDONLY);
5895 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5900 if (flags & MNTDF_SHOW) {
5901 if (flags & MNTDF_INODES)
5902 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5903 "UUID", "Inodes", "IUsed", "IFree",
5904 "IUse%", "Mounted on");
5906 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5908 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5909 "Used", "Available", "Use%", "Mounted on");
5912 for (tp = types; tp->st_name != NULL; tp++) {
5913 bool have_ost = false;
5915 if (!(tp->st_op & ops))
5918 for (index = 0; ; index++) {
5919 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5920 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5921 type = flags & MNTDF_LAZY ?
5922 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5923 rc2 = llapi_obd_fstatfs(fd, type, index,
5924 &stat_buf, &uuid_buf);
5929 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5930 if (!(flags & MNTDF_VERBOSE))
5932 } else if (rc2 < 0 && rc == 0) {
5937 * If we have OSTs then don't report MDT block counts.
5938 * For MDT-only filesystems the expectation is that all
5939 * layouts have a DoM component. For filesystems with
5940 * OSTs, files are not necessarily going to store data
5941 * on MDTs, and MDT space is limited to a fraction of
5942 * OST space, so don't include it in the summary.
5944 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5951 if (poolname && tp->st_op == LL_STATFS_LOV &&
5952 llapi_search_ost(fsname, poolname,
5953 obd_uuid2str(&uuid_buf)) != 1)
5957 * the llapi_obd_fstatfs() call may have returned with
5958 * an error, but if it filled in uuid_buf we will at
5959 * lease use that to print out a message for that OBD.
5960 * If we didn't get anything in the uuid_buf, then fill
5961 * it in so that we can print an error message.
5963 if (uuid_buf.uuid[0] == '\0')
5964 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5965 "%s%04x", tp->st_name, index);
5967 lsb->sb_buf[lsb->sb_count].sd_index = index;
5968 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5971 if (flags & MNTDF_SHOW)
5972 showdf(mntdir, &stat_buf,
5973 obd_uuid2str(&uuid_buf), flags,
5974 tp->st_name, index, rc2);
5979 if (tp->st_op == LL_STATFS_LMV) {
5980 sum.os_ffree += stat_buf.os_ffree;
5981 sum.os_files += stat_buf.os_files;
5982 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5983 ost_files += stat_buf.os_files;
5984 ost_ffree += stat_buf.os_ffree;
5986 sum.os_blocks += stat_buf.os_blocks *
5988 sum.os_bfree += stat_buf.os_bfree *
5990 sum.os_bavail += stat_buf.os_bavail *
5998 * If we have _some_ OSTs, but don't have as many free objects on the
5999 * OST as inodes on the MDTs, reduce the reported number of inodes
6000 * to compensate, so that the "inodes in use" number is correct.
6001 * This should be kept in sync with ll_statfs_internal().
6003 if (ost_files && ost_ffree < sum.os_ffree) {
6004 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6005 sum.os_ffree = ost_ffree;
6007 if (flags & MNTDF_SHOW) {
6009 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6017 static int lfs_setdirstripe(int argc, char **argv)
6020 struct lfs_setstripe_args lsa = { 0 };
6021 struct llapi_stripe_param *param = NULL;
6022 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6025 char *mode_opt = NULL;
6026 bool default_stripe = false;
6027 bool delete = false;
6028 bool foreign_mode = false;
6029 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6030 mode_t previous_mode = 0;
6032 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
6033 struct option long_opts[] = {
6034 { .val = 'c', .name = "count", .has_arg = required_argument },
6035 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6036 { .val = 'd', .name = "delete", .has_arg = no_argument },
6037 { .val = 'D', .name = "default", .has_arg = no_argument },
6038 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6039 { .val = LFS_LAYOUT_FLAGS_OPT,
6040 .name = "flags", .has_arg = required_argument },
6041 { .val = LFS_LAYOUT_FOREIGN_OPT,
6042 .name = "foreign", .has_arg = optional_argument},
6043 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6044 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6045 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6046 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6048 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6049 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6050 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6052 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6053 { .val = 'i', .name = "index", .has_arg = required_argument },
6055 { .val = 'o', .name = "mode", .has_arg = required_argument },
6056 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6057 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6059 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6060 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6061 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6065 setstripe_args_init(&lsa);
6067 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6068 long_opts, NULL)) >= 0) {
6075 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6078 "%s %s: invalid stripe count '%s'\n",
6079 progname, argv[0], optarg);
6085 default_stripe = true;
6088 default_stripe = true;
6090 case LFS_LAYOUT_FOREIGN_OPT:
6092 /* check pure numeric */
6093 type = strtoul(optarg, &end, 0);
6096 type = check_foreign_type_name(optarg);
6097 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6099 "%s %s: unknown foreign type '%s'\n",
6106 foreign_mode = true;
6108 case LFS_LAYOUT_FLAGS_OPT:
6109 flags = strtoul(optarg, &end, 16);
6112 "%s %s: bad flags '%s'\n",
6113 progname, argv[0], optarg);
6117 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6120 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6123 lsa.lsa_pattern = check_hashtype(optarg);
6124 if (lsa.lsa_pattern == 0) {
6126 "%s %s: bad stripe hash type '%s'\n",
6127 progname, argv[0], optarg);
6132 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6135 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6136 if (strcmp(argv[optind - 1], "--index") == 0)
6138 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6141 lsa.lsa_nr_tgts = parse_targets(mdts,
6142 sizeof(mdts) / sizeof(__u32),
6143 lsa.lsa_nr_tgts, optarg, NULL);
6144 if (lsa.lsa_nr_tgts < 0) {
6146 "%s %s: invalid MDT target(s) '%s'\n",
6147 progname, argv[0], optarg);
6151 lsa.lsa_tgts = mdts;
6152 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6153 lsa.lsa_stripe_off = mdts[0];
6155 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6158 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6167 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6168 progname, argv[0], argv[optind - 1]);
6173 if (optind == argc) {
6174 fprintf(stderr, "%s %s: DIR must be specified\n",
6179 if (xattr && !foreign_mode) {
6181 * only print a warning as this is armless and will be
6185 "%s %s: xattr has been specified for non-foreign layout\n",
6187 } else if (foreign_mode && !xattr) {
6189 "%s %s: xattr must be provided in foreign mode\n",
6194 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6195 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6197 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6202 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6203 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6205 "%s %s: stripe offset and count must be specified\n",
6211 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6212 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6214 "%s %s: cannot specify -d with -c or -i options\n",
6220 mode = strtoul(mode_opt, &end, 8);
6223 "%s %s: bad MODE '%s'\n",
6224 progname, argv[0], mode_opt);
6227 previous_mode = umask(0);
6230 /* foreign LMV/dir case */
6232 if (argc > optind + 1) {
6234 "%s %s: cannot specify multiple foreign dirs\n",
6239 dname = argv[optind];
6240 result = llapi_dir_create_foreign(dname, mode, type, flags,
6244 "%s mkdir: can't create foreign dir '%s': %s\n",
6245 progname, dname, strerror(-result));
6250 * initialize stripe parameters, in case param is converted to specific,
6251 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6253 param = calloc(1, offsetof(typeof(*param),
6254 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6255 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6258 "%s %s: cannot allocate memory for parameters: %s\n",
6259 progname, argv[0], strerror(ENOMEM));
6263 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6264 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6265 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6266 lsa.lsa_stripe_count == 0))
6267 lsa.lsa_stripe_count = 1;
6268 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6269 param->lsp_stripe_count = lsa.lsa_stripe_count;
6270 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6271 param->lsp_stripe_offset = -1;
6273 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6274 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6275 param->lsp_stripe_pattern = lsa.lsa_pattern;
6277 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6278 param->lsp_pool = lsa.lsa_pool_name;
6279 param->lsp_is_specific = false;
6280 if (lsa.lsa_nr_tgts > 1) {
6281 if (lsa.lsa_stripe_count > 0 &&
6282 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6283 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6285 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6286 argv[0], lsa.lsa_stripe_count,
6292 param->lsp_is_specific = true;
6293 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6294 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6297 dname = argv[optind];
6299 if (default_stripe) {
6300 result = llapi_dir_set_default_lmv(dname, param);
6303 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6304 progname, dname, strerror(-result));
6308 result = llapi_dir_create(dname, mode, param);
6311 "%s setdirstripe: cannot create dir '%s': %s\n",
6312 progname, dname, strerror(-result));
6313 } while (!result && (dname = argv[++optind]));
6316 umask(previous_mode);
6323 static int lfs_rmentry(int argc, char **argv)
6330 fprintf(stderr, "error: %s: missing dirname\n",
6336 dname = argv[index];
6338 result = llapi_direntry_remove(dname);
6341 "error: %s: remove dir entry '%s' failed\n",
6345 dname = argv[++index];
6350 static int lfs_mv(int argc, char **argv)
6352 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6353 struct find_param param = {
6360 struct option long_opts[] = {
6361 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6362 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6363 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6366 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6368 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6371 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6374 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6376 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6382 param.fp_verbose = VERBOSE_DETAIL;
6385 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6386 progname, argv[optind - 1]);
6391 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6392 fprintf(stderr, "%s mv: MDT index must be specified\n",
6397 if (optind >= argc) {
6398 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6402 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6404 /* initialize migrate mdt parameters */
6405 param.fp_lmv_md = &lmu;
6406 param.fp_migrate = 1;
6407 rc = llapi_migrate_mdt(argv[optind], ¶m);
6409 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6410 progname, argv[optind], lmu.lum_stripe_offset,
6415 static int lfs_osts(int argc, char **argv)
6417 return lfs_tgts(argc, argv);
6420 static int lfs_mdts(int argc, char **argv)
6422 return lfs_tgts(argc, argv);
6425 static int lfs_df(int argc, char **argv)
6427 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6428 enum mntdf_flags flags = MNTDF_SHOW;
6429 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6430 int c, rc = 0, index = 0;
6431 char fsname[PATH_MAX] = "", *pool_name = NULL;
6432 struct option long_opts[] = {
6433 { .val = 'h', .name = "human-readable",
6434 .has_arg = no_argument },
6435 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6436 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6437 { .val = 'p', .name = "pool", .has_arg = required_argument },
6438 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6441 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6444 flags |= MNTDF_COOKED;
6447 flags |= MNTDF_INODES;
6450 flags |= MNTDF_LAZY;
6456 flags |= MNTDF_VERBOSE;
6462 if (optind < argc && !realpath(argv[optind], path)) {
6464 fprintf(stderr, "error: invalid path '%s': %s\n",
6465 argv[optind], strerror(-rc));
6469 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6470 /* Check if we have a mount point */
6471 if (mntdir[0] == '\0')
6474 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6475 if (rc || path[0] != '\0')
6477 fsname[0] = '\0'; /* avoid matching in next loop */
6478 mntdir[0] = '\0'; /* avoid matching in next loop */
6484 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6485 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6489 if (opt_fsname == opt_instance) { /* both true or both false */
6490 rc = llapi_getname(mntdir, buf, buflen);
6491 } else if (opt_fsname) {
6493 * llapi_search_mounts() fills @buf with fsname, but that is not
6494 * called if explicit paths are specified on the command-line
6497 rc = llapi_get_fsname(mntdir, buf, buflen);
6498 } else /* if (opt_instance) */ {
6499 rc = llapi_get_instance(mntdir, buf, buflen);
6503 fprintf(stderr, "cannot get instance for '%s': %s\n",
6504 mntdir, strerror(-rc));
6509 printf("%s %s\n", buf, mntdir);
6511 printf("%s\n", buf);
6516 static int lfs_getname(int argc, char **argv)
6518 struct option long_opts[] = {
6519 { .val = 'h', .name = "help", .has_arg = no_argument },
6520 { .val = 'i', .name = "instance", .has_arg = no_argument },
6521 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6523 bool opt_instance = false, opt_fsname = false;
6524 char fsname[PATH_MAX] = "";
6527 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6530 opt_instance = true;
6541 if (optind == argc) { /* no paths specified, get all paths. */
6542 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6545 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6546 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6547 opt_instance, opt_fsname, true);
6550 path[0] = fsname[0] = mntdir[0] = '\0';
6552 } else { /* paths specified, only attempt to search these. */
6555 /* if only one path is given, print only requested info */
6556 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6558 for (; optind < argc; optind++) {
6559 rc2 = print_instance(argv[optind], fsname,
6560 sizeof(fsname), opt_instance,
6561 opt_fsname, opt_mntdir);
6571 static int lfs_check(int argc, char **argv)
6573 char mntdir[PATH_MAX] = {'\0'};
6582 fprintf(stderr, "%s check: server type must be specified\n",
6587 obd_types[0] = obd_type1;
6588 obd_types[1] = obd_type2;
6589 obd_types[2] = obd_type3;
6591 if (strcmp(argv[1], "osts") == 0) {
6592 strcpy(obd_types[0], "osc");
6593 } else if (strcmp(argv[1], "mdts") == 0 ||
6594 strcmp(argv[1], "mds") == 0) {
6595 strcpy(obd_types[0], "mdc");
6596 } else if (strcmp(argv[1], "mgts") == 0) {
6597 strcpy(obd_types[0], "mgc");
6598 } else if (strcmp(argv[1], "all") == 0 ||
6599 strcmp(argv[1], "servers") == 0) {
6601 strcpy(obd_types[0], "osc");
6602 strcpy(obd_types[1], "mdc");
6603 strcpy(obd_types[2], "mgc");
6605 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6610 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6611 if (rc < 0 || mntdir[0] == '\0') {
6613 "%s check: cannot find mounted Lustre filesystem: %s\n",
6614 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6618 rc = llapi_target_check(num_types, obd_types, mntdir);
6620 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6621 progname, argv[1], strerror(-rc));
6626 #ifdef HAVE_SYS_QUOTA_H
6627 #define ADD_OVERFLOW(a, b) \
6628 ((((a) + (b)) < (a)) ? \
6629 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6631 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6632 * returns the value or ULONG_MAX on integer overflow or incorrect format
6634 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6635 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6636 * 3. empty integer value is interpreted as 0
6638 static unsigned long str2sec(const char *timestr)
6640 const char spec[] = "smhdw";
6641 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6642 unsigned long val = 0;
6645 if (strpbrk(timestr, spec) == NULL) {
6647 * no specifiers inside the time string,
6648 * should treat it as an integer value
6650 val = strtoul(timestr, &tail, 10);
6651 return *tail ? ULONG_MAX : val;
6654 /* format string is XXwXXdXXhXXmXXs */
6660 v = strtoul(timestr, &tail, 10);
6661 if (v == ULONG_MAX || *tail == '\0')
6663 * value too large (ULONG_MAX or more)
6664 * or missing specifier
6668 ptr = strchr(spec, *tail);
6670 /* unknown specifier */
6675 /* check if product will overflow the type */
6676 if (!(v < ULONG_MAX / mult[ind]))
6679 ADD_OVERFLOW(val, mult[ind] * v);
6680 if (val == ULONG_MAX)
6692 #define ARG2ULL(nr, str, def_units) \
6694 unsigned long long limit, units = def_units; \
6697 rc = llapi_parse_size(str, &limit, &units, 1); \
6699 fprintf(stderr, "%s: invalid limit '%s'\n", \
6706 static inline int has_times_option(int argc, char **argv)
6710 for (i = 1; i < argc; i++)
6711 if (!strcmp(argv[i], "-t"))
6717 static inline int lfs_verify_poolarg(char *pool)
6719 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6721 "Pool name '%.*s' is longer than %d\n",
6722 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6728 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6731 char *mnt, *obd_type = (char *)qctl->obd_type;
6732 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6733 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6734 struct option long_opts[] = {
6735 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6736 { .val = 'g', .name = "group", .has_arg = no_argument },
6737 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6738 { .val = 'p', .name = "projid", .has_arg = no_argument },
6739 { .val = 't', .name = "times", .has_arg = no_argument },
6740 { .val = 'u', .name = "user", .has_arg = no_argument },
6741 { .val = LFS_POOL_OPT,
6742 .name = "pool", .has_arg = required_argument },
6746 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6747 qctl->qc_type = ALLQUOTA;
6749 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6750 long_opts, NULL)) != -1) {
6761 if (qctl->qc_type != ALLQUOTA) {
6763 "error: -u/g/p can't be used more than once\n");
6766 qctl->qc_type = qtype;
6769 if (strncmp(optarg, NOTIFY_GRACE,
6770 strlen(NOTIFY_GRACE)) == 0) {
6771 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6773 dqi->dqi_bgrace = str2sec(optarg);
6774 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6776 "error: bad block-grace: %s\n",
6781 dqb->dqb_valid |= QIF_BTIME;
6784 if (strncmp(optarg, NOTIFY_GRACE,
6785 strlen(NOTIFY_GRACE)) == 0) {
6786 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6788 dqi->dqi_igrace = str2sec(optarg);
6789 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6791 "error: bad inode-grace: %s\n",
6796 dqb->dqb_valid |= QIF_ITIME;
6798 case 't': /* Yes, of course! */
6801 if (lfs_verify_poolarg(optarg))
6804 "Trying to set grace for pool %s\n", optarg);
6805 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
6806 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
6808 /* getopt prints error message for us when opterr != 0 */
6814 if (qctl->qc_type == ALLQUOTA) {
6815 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6819 if (optind != argc - 1) {
6820 fprintf(stderr, "error: unexpected parameters encountered\n");
6825 rc = llapi_quotactl(mnt, qctl);
6828 fprintf(stderr, "%s %s ", obd_type,
6829 obd_uuid2str(&qctl->obd_uuid));
6830 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6837 #define BSLIMIT (1 << 0)
6838 #define BHLIMIT (1 << 1)
6839 #define ISLIMIT (1 << 2)
6840 #define IHLIMIT (1 << 3)
6842 int lfs_setquota(int argc, char **argv)
6845 struct if_quotactl *qctl;
6846 char *mnt, *obd_type;
6847 struct obd_dqblk *dqb;
6848 struct option long_opts[] = {
6849 { .val = 'b', .name = "block-softlimit",
6850 .has_arg = required_argument },
6851 { .val = 'B', .name = "block-hardlimit",
6852 .has_arg = required_argument },
6853 { .val = 'd', .name = "default", .has_arg = no_argument },
6854 { .val = 'g', .name = "group", .has_arg = required_argument },
6855 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6856 { .val = 'i', .name = "inode-softlimit",
6857 .has_arg = required_argument },
6858 { .val = 'I', .name = "inode-hardlimit",
6859 .has_arg = required_argument },
6860 { .val = 'p', .name = "projid", .has_arg = required_argument },
6861 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6862 { .val = 'u', .name = "user", .has_arg = required_argument },
6863 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6864 { .val = LFS_POOL_OPT,
6865 .name = "pool", .has_arg = required_argument },
6867 unsigned int limit_mask = 0;
6868 bool use_default = false;
6869 int qtype, qctl_len;
6871 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
6872 qctl = malloc(qctl_len);
6876 memset(qctl, 0, qctl_len);
6877 obd_type = (char *)qctl->obd_type;
6878 dqb = &qctl->qc_dqblk;
6880 if (has_times_option(argc, argv)) {
6881 rc = lfs_setquota_times(argc, argv, qctl);
6885 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
6886 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6887 * so it can be used as a marker that qc_type
6888 * isn't reinitialized from command line
6891 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6892 long_opts, NULL)) != -1) {
6895 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6898 goto quota_type_def;
6901 rc = name2uid(&qctl->qc_id, optarg);
6904 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6907 goto quota_type_def;
6910 rc = name2gid(&qctl->qc_id, optarg);
6913 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6916 goto quota_type_def;
6919 rc = name2projid(&qctl->qc_id, optarg);
6922 if (str2quotaid(&qctl->qc_id, optarg)) {
6924 "%s setquota: invalid id '%s'\n",
6931 if (qctl->qc_id == 0) {
6933 "%s setquota: can't set quota for root usr/group/project.\n",
6940 if (qctl->qc_type != ALLQUOTA) {
6942 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
6947 qctl->qc_type = qtype;
6950 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6954 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6955 dqb->dqb_bsoftlimit >>= 10;
6956 limit_mask |= BSLIMIT;
6957 if (dqb->dqb_bsoftlimit &&
6958 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6960 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6962 (unsigned long long)dqb->dqb_bsoftlimit,
6966 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6967 dqb->dqb_bhardlimit >>= 10;
6968 limit_mask |= BHLIMIT;
6969 if (dqb->dqb_bhardlimit &&
6970 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6972 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6973 "See '%s help setquota' or Lustre manual for details\n",
6975 (unsigned long long)dqb->dqb_bhardlimit,
6979 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6980 limit_mask |= ISLIMIT;
6981 if (dqb->dqb_isoftlimit &&
6982 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6984 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6986 (unsigned long long)dqb->dqb_isoftlimit,
6990 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6991 limit_mask |= IHLIMIT;
6992 if (dqb->dqb_ihardlimit &&
6993 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6995 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
6997 (unsigned long long)dqb->dqb_ihardlimit,
7001 if (lfs_verify_poolarg(optarg)) {
7006 "Trying to set quota for pool %s\n", optarg);
7007 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7008 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7012 "%s setquota: unrecognized option '%s'\n",
7013 progname, argv[optind - 1]);
7019 if (qctl->qc_type == ALLQUOTA) {
7021 "%s setquota: either -u or -g must be specified\n",
7027 if (!use_default && limit_mask == 0) {
7029 "%s setquota: at least one limit must be specified\n",
7035 if (use_default && limit_mask != 0) {
7037 "%s setquota: limits should not be specified when using default quota\n",
7043 if (use_default && qctl->qc_id == 0) {
7045 "%s setquota: can not set default quota for root user/group/project\n",
7051 if (optind != argc - 1) {
7053 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7054 progname, argv[optind]);
7062 dqb->dqb_bhardlimit = 0;
7063 dqb->dqb_bsoftlimit = 0;
7064 dqb->dqb_ihardlimit = 0;
7065 dqb->dqb_isoftlimit = 0;
7068 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7069 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7070 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7071 /* sigh, we can't just set blimits/ilimits */
7072 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7073 .qc_type = qctl->qc_type,
7074 .qc_id = qctl->qc_id};
7076 rc = llapi_quotactl(mnt, &tmp_qctl);
7080 if (!(limit_mask & BHLIMIT))
7081 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7082 if (!(limit_mask & BSLIMIT))
7083 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7084 if (!(limit_mask & IHLIMIT))
7085 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7086 if (!(limit_mask & ISLIMIT))
7087 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7089 /* Keep grace times if we have got no softlimit arguments */
7090 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7091 dqb->dqb_valid |= QIF_BTIME;
7092 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7095 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7096 dqb->dqb_valid |= QIF_ITIME;
7097 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7101 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7102 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7104 rc = llapi_quotactl(mnt, qctl);
7108 "%s setquota: cannot quotactl '%s' '%s': %s",
7110 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7117 /* Converts seconds value into format string
7118 * result is returned in buf
7120 * 1. result is in descenting order: 1w2d3h4m5s
7121 * 2. zero fields are not filled (except for p. 3): 5d1s
7122 * 3. zero seconds value is presented as "0s"
7124 static char *__sec2str(time_t seconds, char *buf)
7126 const char spec[] = "smhdw";
7127 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7132 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7133 c = seconds / mult[i];
7135 if (c > 0 || (i == 0 && buf == tail))
7136 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7145 static void sec2str(time_t seconds, char *buf, int rc)
7152 tail = __sec2str(seconds, tail);
7154 if (rc && tail - buf < 39) {
7160 static void diff2str(time_t seconds, char *buf, time_t now)
7165 if (seconds <= now) {
7166 strcpy(buf, "none");
7169 __sec2str(seconds - now, buf);
7172 static void print_quota_title(char *name, struct if_quotactl *qctl,
7173 bool human_readable, bool show_default)
7176 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7177 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7178 "Filesystem", "bquota", "blimit", "bgrace",
7179 "iquota", "ilimit", "igrace");
7181 printf("Disk quotas for %s %s (%cid %u):\n",
7182 qtype_name(qctl->qc_type), name,
7183 *qtype_name(qctl->qc_type), qctl->qc_id);
7184 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7185 "Filesystem", human_readable ? "used" : "kbytes",
7186 "quota", "limit", "grace",
7187 "files", "quota", "limit", "grace");
7191 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7194 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7197 snprintf(buf, buflen, "%5.4gP",
7198 (double)num / ((__u64)1 << 40));
7200 snprintf(buf, buflen, "%5.4gT",
7201 (double)num / (1 << 30));
7203 snprintf(buf, buflen, "%5.4gG",
7204 (double)num / (1 << 20));
7206 snprintf(buf, buflen, "%5.4gM",
7207 (double)num / (1 << 10));
7209 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7213 #define STRBUF_LEN 32
7214 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7215 int rc, bool h, bool show_default)
7221 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7222 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7223 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7224 int bover = 0, iover = 0;
7225 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7226 char numbuf[3][STRBUF_LEN];
7228 char strbuf[STRBUF_LEN];
7230 if (dqb->dqb_bhardlimit &&
7231 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7233 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7234 if (dqb->dqb_btime > now)
7240 if (dqb->dqb_ihardlimit &&
7241 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7243 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7244 if (dqb->dqb_itime > now)
7250 if (strlen(mnt) > 15)
7251 printf("%s\n%15s", mnt, "");
7253 printf("%15s", mnt);
7256 diff2str(dqb->dqb_btime, timebuf, now);
7257 else if (show_default)
7258 snprintf(timebuf, sizeof(timebuf), "%llu",
7259 (unsigned long long)dqb->dqb_btime);
7261 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7262 strbuf, sizeof(strbuf), h);
7263 if (rc == -EREMOTEIO)
7264 sprintf(numbuf[0], "%s*", strbuf);
7266 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7267 "%s" : "[%s]", strbuf);
7269 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7270 if (type == QC_GENERAL)
7271 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7272 "%s" : "[%s]", strbuf);
7274 sprintf(numbuf[1], "%s", "-");
7276 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7277 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7278 "%s" : "[%s]", strbuf);
7281 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7283 printf(" %7s%c %6s %7s %7s",
7284 numbuf[0], bover ? '*' : ' ', numbuf[1],
7285 numbuf[2], bover > 1 ? timebuf : "-");
7288 diff2str(dqb->dqb_itime, timebuf, now);
7289 else if (show_default)
7290 snprintf(timebuf, sizeof(timebuf), "%llu",
7291 (unsigned long long)dqb->dqb_itime);
7293 snprintf(numbuf[0], sizeof(numbuf),
7294 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7295 (uintmax_t)dqb->dqb_curinodes);
7297 if (type == QC_GENERAL)
7298 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7300 (uintmax_t)dqb->dqb_isoftlimit);
7302 sprintf(numbuf[1], "%s", "-");
7304 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7305 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7308 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7309 else if (type != QC_OSTIDX)
7310 printf(" %7s%c %6s %7s %7s",
7311 numbuf[0], iover ? '*' : ' ', numbuf[1],
7312 numbuf[2], iover > 1 ? timebuf : "-");
7314 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7316 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7317 qctl->qc_cmd == Q_GETOINFO) {
7321 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7322 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7324 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7325 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7326 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7328 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7330 printf("Block grace time: %s; Inode grace time: %s\n",
7331 bgtimebuf, igtimebuf);
7335 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7339 /* format is "lustre-OST0001" */
7340 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7342 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7347 *idx = strtoul(dash, &endp, 16);
7348 if (*idx > 0xffff) {
7349 fprintf(stderr, "wrong index %s\n", tgtname);
7356 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7357 bool h, __u64 *total)
7359 int rc = 0, rc1 = 0, count = 0, i = 0;
7360 char **list = NULL, *buffer = NULL;
7361 __u32 valid = qctl->qc_valid;
7363 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7366 /* Is it correct for the case OST0000, OST0002, OST0003 -
7367 * we will ask OST0001 that is absent and won't ask OST0003? */
7368 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7370 fprintf(stderr, "can not get %s count: %s\n",
7371 is_mdt ? "mdt" : "ost", strerror(-rc));
7375 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7376 char fname[PATH_MAX];
7377 char fsname[LUSTRE_MAXFSNAME + 1];
7378 int bufsize = sizeof(struct obd_uuid) * count;
7380 rc = llapi_search_fsname(mnt, fsname);
7382 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7386 buffer = malloc(bufsize + sizeof(*list) * count);
7389 list = (char **)(buffer + bufsize);
7390 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7391 count = llapi_get_poolmembers(fname, list, count,
7397 for (i = 0; i < count; i++) {
7398 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7401 if (tgt_name2index(list[i], &index))
7403 qctl->qc_idx = index;
7408 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7409 rc = llapi_quotactl(mnt, qctl);
7411 /* It is remote client case. */
7412 if (rc == -EOPNOTSUPP) {
7419 fprintf(stderr, "quotactl %s%d failed.\n",
7420 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7424 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7425 qctl->qc_valid, 0, h, false);
7426 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7427 qctl->qc_dqblk.dqb_bhardlimit;
7432 qctl->qc_valid = valid;
7436 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7437 int verbose, int quiet, bool human_readable,
7440 int rc1 = 0, rc2 = 0, rc3 = 0;
7441 char *obd_type = (char *)qctl->obd_type;
7442 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7443 __u64 total_ialloc = 0, total_balloc = 0;
7444 bool use_default_for_blk = false;
7445 bool use_default_for_file = false;
7448 rc1 = llapi_quotactl(mnt, qctl);
7452 fprintf(stderr, "%s quotas are not enabled.\n",
7453 qtype_name(qctl->qc_type));
7456 fprintf(stderr, "Permission denied.\n");
7459 /* We already got error message. */
7462 fprintf(stderr, "Unexpected quotactl error: %s\n",
7467 if (!show_default && qctl->qc_id == 0) {
7468 qctl->qc_dqblk.dqb_bhardlimit = 0;
7469 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7470 qctl->qc_dqblk.dqb_ihardlimit = 0;
7471 qctl->qc_dqblk.dqb_isoftlimit = 0;
7472 qctl->qc_dqblk.dqb_btime = 0;
7473 qctl->qc_dqblk.dqb_itime = 0;
7474 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7477 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7478 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7479 use_default_for_blk = true;
7480 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7483 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7484 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7485 use_default_for_file = true;
7486 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7489 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7490 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7491 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7492 print_quota_title(name, qctl, human_readable, show_default);
7494 if (rc1 && *obd_type)
7495 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7497 if (qctl->qc_valid != QC_GENERAL)
7500 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7501 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7502 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7503 (QIF_LIMITS|QIF_USAGE));
7505 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7507 if (!show_default && verbose &&
7508 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7509 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7510 char strbuf[STRBUF_LEN];
7512 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7514 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7516 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7518 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7519 (uintmax_t)total_ialloc, strbuf);
7522 if (use_default_for_blk)
7523 printf("%cid %u is using default block quota setting\n",
7524 *qtype_name(qctl->qc_type), qctl->qc_id);
7526 if (use_default_for_file)
7527 printf("%cid %u is using default file quota setting\n",
7528 *qtype_name(qctl->qc_type), qctl->qc_id);
7530 if (rc1 || rc2 || rc3 || inacc)
7531 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7545 static int lfs_project(int argc, char **argv)
7547 int ret = 0, err = 0, c, i;
7548 struct project_handle_control phc = { 0 };
7549 enum lfs_project_ops_t op;
7552 phc.assign_projid = false;
7553 /* default action */
7554 op = LFS_PROJECT_LIST;
7556 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7559 if (op != LFS_PROJECT_LIST) {
7561 "%s: cannot specify '-c' '-C' '-s' together\n",
7566 op = LFS_PROJECT_CHECK;
7569 if (op != LFS_PROJECT_LIST) {
7571 "%s: cannot specify '-c' '-C' '-s' together\n",
7576 op = LFS_PROJECT_CLEAR;
7579 if (op != LFS_PROJECT_LIST) {
7581 "%s: cannot specify '-c' '-C' '-s' together\n",
7586 phc.set_inherit = true;
7587 op = LFS_PROJECT_SET;
7593 phc.keep_projid = true;
7596 phc.recursive = true;
7599 if (str2quotaid(&phc.projid, optarg)) {
7601 "Invalid project ID: %s\n",
7606 phc.assign_projid = true;
7610 phc.newline = false;
7613 fprintf(stderr, "%s: invalid option '%c'\n",
7619 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7620 op = LFS_PROJECT_SET;
7621 phc.set_projid = true;
7622 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7623 phc.set_projid = true;
7627 case LFS_PROJECT_CHECK:
7628 if (phc.keep_projid) {
7630 "%s: '-k' is useless together with '-c'\n",
7635 case LFS_PROJECT_CLEAR:
7638 "%s: '-0' is useless together with '-C'\n",
7642 if (phc.assign_projid) {
7644 "%s: '-p' is useless together with '-C'\n",
7649 case LFS_PROJECT_SET:
7652 "%s: '-0' is useless together with '-s'\n",
7656 if (phc.keep_projid) {
7658 "%s: '-k' is useless together with '-s'\n",
7666 "%s: '-0' is useless for list operations\n",
7676 fprintf(stderr, "%s: missing file or directory target(s)\n",
7681 for (i = 0; i < argc; i++) {
7683 case LFS_PROJECT_CHECK:
7684 err = lfs_project_check(argv[i], &phc);
7686 case LFS_PROJECT_LIST:
7687 err = lfs_project_list(argv[i], &phc);
7689 case LFS_PROJECT_CLEAR:
7690 err = lfs_project_clear(argv[i], &phc);
7692 case LFS_PROJECT_SET:
7693 err = lfs_project_set(argv[i], &phc);
7705 static int lfs_quota(int argc, char **argv)
7708 char *mnt, *name = NULL;
7709 struct if_quotactl *qctl;
7711 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7712 __u32 valid = QC_GENERAL, idx = 0;
7713 bool human_readable = false;
7714 bool show_default = false;
7716 struct option long_opts[] = {
7717 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
7720 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7724 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7725 qctl->qc_type = ALLQUOTA;
7726 obd_uuid = (char *)qctl->obd_uuid.uuid;
7728 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7729 long_opts, NULL)) != -1) {
7732 show_default = true;
7737 show_default = true;
7742 show_default = true;
7746 if (qctl->qc_type != ALLQUOTA) {
7748 "%s quota: only one of -u, -g, or -p may be specified\n",
7753 qctl->qc_type = qtype;
7756 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7759 valid = qctl->qc_valid = QC_UUID;
7760 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
7763 valid = qctl->qc_valid = QC_MDTIDX;
7764 idx = qctl->qc_idx = atoi(optarg);
7765 if (idx == 0 && *optarg != '0') {
7767 "%s quota: invalid MDT index '%s'\n",
7774 valid = qctl->qc_valid = QC_OSTIDX;
7775 idx = qctl->qc_idx = atoi(optarg);
7776 if (idx == 0 && *optarg != '0') {
7778 "%s quota: invalid OST index '%s'\n",
7791 human_readable = true;
7794 if (lfs_verify_poolarg(optarg)) {
7798 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7799 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
7800 LUSTRE_Q_GETINFOPOOL :
7801 LUSTRE_Q_GETQUOTAPOOL;
7804 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7805 progname, argv[optind - 1]);
7811 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7812 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7813 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7814 qctl->qc_type == ALLQUOTA &&
7815 optind == argc - 1 && !show_default) {
7818 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7819 qctl->qc_type = qtype;
7820 qctl->qc_valid = valid;
7821 if (qtype == USRQUOTA) {
7822 qctl->qc_id = geteuid();
7823 rc = uid2name(&name, qctl->qc_id);
7825 qctl->qc_id = getegid();
7826 rc = gid2name(&name, qctl->qc_id);
7827 memset(&qctl->qc_dqblk, 0,
7828 sizeof(qctl->qc_dqblk));
7833 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
7834 human_readable, show_default);
7839 /* lfs quota -u username /path/to/lustre/mount */
7840 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7841 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7842 /* options should be followed by u/g-name and mntpoint */
7843 if ((!show_default && optind + 2 != argc) ||
7844 (show_default && optind + 1 != argc) ||
7845 qctl->qc_type == ALLQUOTA) {
7847 "%s quota: name and mount point must be specified\n",
7853 if (!show_default) {
7854 name = argv[optind++];
7855 switch (qctl->qc_type) {
7857 rc = name2uid(&qctl->qc_id, name);
7860 rc = name2gid(&qctl->qc_id, name);
7863 rc = name2projid(&qctl->qc_id, name);
7870 qctl->qc_valid = QC_GENERAL;
7871 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
7876 if (str2quotaid(&qctl->qc_id, name)) {
7877 fprintf(stderr, "%s quota: invalid id '%s'\n",
7883 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
7884 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7891 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
7892 human_readable, show_default);
7897 #endif /* HAVE_SYS_QUOTA_H! */
7899 static int flushctx_ioctl(char *mp)
7903 fd = open(mp, O_RDONLY);
7905 fprintf(stderr, "flushctx: error open %s: %s\n",
7906 mp, strerror(errno));
7910 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7912 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7913 mp, strerror(errno));
7919 static int lfs_flushctx(int argc, char **argv)
7921 int kdestroy = 0, c;
7922 char mntdir[PATH_MAX] = {'\0'};
7926 while ((c = getopt(argc, argv, "k")) != -1) {
7933 "error: %s: option '-%c' unrecognized\n",
7940 rc = system("kdestroy > /dev/null");
7942 rc = WEXITSTATUS(rc);
7944 "error destroying tickets: %d, continuing\n",
7949 if (optind >= argc) {
7950 /* flush for all mounted lustre fs. */
7951 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7952 /* Check if we have a mount point */
7953 if (mntdir[0] == '\0')
7956 if (flushctx_ioctl(mntdir))
7959 mntdir[0] = '\0'; /* avoid matching in next loop */
7962 /* flush fs as specified */
7963 while (optind < argc) {
7964 if (flushctx_ioctl(argv[optind++]))
7971 static int lfs_changelog(int argc, char **argv)
7973 void *changelog_priv;
7974 struct changelog_rec *rec;
7975 long long startrec = 0, endrec = 0;
7977 struct option long_opts[] = {
7978 { .val = 'f', .name = "follow", .has_arg = no_argument },
7980 char short_opts[] = "f";
7983 while ((rc = getopt_long(argc, argv, short_opts,
7984 long_opts, NULL)) != -1) {
7991 "%s changelog: unrecognized option '%s'\n",
7992 progname, argv[optind - 1]);
7996 if (optind >= argc) {
7997 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8002 mdd = argv[optind++];
8004 startrec = strtoll(argv[optind++], NULL, 10);
8006 endrec = strtoll(argv[optind++], NULL, 10);
8008 rc = llapi_changelog_start(&changelog_priv,
8009 CHANGELOG_FLAG_BLOCK |
8010 CHANGELOG_FLAG_JOBID |
8011 CHANGELOG_FLAG_EXTRA_FLAGS |
8012 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8015 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8016 progname, strerror(errno = -rc));
8020 rc = llapi_changelog_set_xflags(changelog_priv,
8021 CHANGELOG_EXTRA_FLAG_UIDGID |
8022 CHANGELOG_EXTRA_FLAG_NID |
8023 CHANGELOG_EXTRA_FLAG_OMODE |
8024 CHANGELOG_EXTRA_FLAG_XATTR);
8027 "%s changelog: cannot set xflags for changelog: %s\n",
8028 progname, strerror(errno = -rc));
8032 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8036 if (endrec && rec->cr_index > endrec) {
8037 llapi_changelog_free(&rec);
8040 if (rec->cr_index < startrec) {
8041 llapi_changelog_free(&rec);
8045 secs = rec->cr_time >> 30;
8046 gmtime_r(&secs, &ts);
8047 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8048 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8049 changelog_type2str(rec->cr_type),
8050 ts.tm_hour, ts.tm_min, ts.tm_sec,
8051 (int)(rec->cr_time & ((1 << 30) - 1)),
8052 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8053 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8055 if (rec->cr_flags & CLF_JOBID) {
8056 struct changelog_ext_jobid *jid =
8057 changelog_rec_jobid(rec);
8059 if (jid->cr_jobid[0] != '\0')
8060 printf(" j=%s", jid->cr_jobid);
8063 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8064 struct changelog_ext_extra_flags *ef =
8065 changelog_rec_extra_flags(rec);
8067 printf(" ef=0x%llx",
8068 (unsigned long long)ef->cr_extra_flags);
8070 if (ef->cr_extra_flags & CLFE_UIDGID) {
8071 struct changelog_ext_uidgid *uidgid =
8072 changelog_rec_uidgid(rec);
8074 printf(" u=%llu:%llu",
8075 (unsigned long long)uidgid->cr_uid,
8076 (unsigned long long)uidgid->cr_gid);
8078 if (ef->cr_extra_flags & CLFE_NID) {
8079 struct changelog_ext_nid *nid =
8080 changelog_rec_nid(rec);
8083 libcfs_nid2str(nid->cr_nid));
8086 if (ef->cr_extra_flags & CLFE_OPEN) {
8087 struct changelog_ext_openmode *omd =
8088 changelog_rec_openmode(rec);
8089 char mode[] = "---";
8091 /* exec mode must be exclusive */
8092 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8095 if (omd->cr_openflags & MDS_FMODE_READ)
8097 if (omd->cr_openflags &
8104 if (strcmp(mode, "---") != 0)
8105 printf(" m=%s", mode);
8108 if (ef->cr_extra_flags & CLFE_XATTR) {
8109 struct changelog_ext_xattr *xattr =
8110 changelog_rec_xattr(rec);
8112 if (xattr->cr_xattr[0] != '\0')
8113 printf(" x=%s", xattr->cr_xattr);
8117 if (!fid_is_zero(&rec->cr_pfid))
8118 printf(" p="DFID, PFID(&rec->cr_pfid));
8119 if (rec->cr_namelen)
8120 printf(" %.*s", rec->cr_namelen,
8121 changelog_rec_name(rec));
8123 if (rec->cr_flags & CLF_RENAME) {
8124 struct changelog_ext_rename *rnm =
8125 changelog_rec_rename(rec);
8127 if (!fid_is_zero(&rnm->cr_sfid))
8128 printf(" s="DFID" sp="DFID" %.*s",
8129 PFID(&rnm->cr_sfid),
8130 PFID(&rnm->cr_spfid),
8131 (int)changelog_rec_snamelen(rec),
8132 changelog_rec_sname(rec));
8136 llapi_changelog_free(&rec);
8139 llapi_changelog_fini(&changelog_priv);
8142 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8143 progname, strerror(errno = -rc));
8145 return (rc == 1 ? 0 : rc);
8148 static int lfs_changelog_clear(int argc, char **argv)
8156 endrec = strtoll(argv[3], NULL, 10);
8158 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8161 fprintf(stderr, "%s: record out of range: %llu\n",
8163 else if (rc == -ENOENT)
8164 fprintf(stderr, "%s: no changelog user: %s\n",
8167 fprintf(stderr, "%s error: %s\n", argv[0],
8176 static int lfs_fid2path(int argc, char **argv)
8178 struct option long_opts[] = {
8179 { .val = 'c', .name = "cur", .has_arg = no_argument },
8180 { .val = 'l', .name = "link", .has_arg = required_argument },
8181 { .val = 'r', .name = "rec", .has_arg = required_argument },
8183 char short_opts[] = "cl:r:";
8184 char mntdir[PATH_MAX];
8185 char *device, *fid, *path, *rootpath;
8186 long long recno = -1;
8191 char *endptr = NULL;
8193 while ((rc = getopt_long(argc, argv, short_opts,
8194 long_opts, NULL)) != -1) {
8200 linkno = strtol(optarg, &endptr, 10);
8201 if (*endptr != '\0') {
8203 "%s fid2path: invalid linkno '%s'\n",
8209 recno = strtoll(optarg, &endptr, 10);
8210 if (*endptr != '\0') {
8212 "%s fid2path: invalid recno '%s'\n",
8219 "%s fid2path: unrecognized option '%s'\n",
8220 progname, argv[optind - 1]);
8227 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
8232 device = argv[optind++];
8233 path = calloc(1, PATH_MAX);
8237 "%s fid2path: cannot allocate memory for path: %s\n",
8238 progname, strerror(-rc));
8243 /* in case that device is not the mountpoint */
8244 if (device[0] == '/') {
8245 rc = llapi_search_mounts(device, 0, mntdir, NULL);
8250 "%s fid2path: %s has no mountpoint: %s\n",
8251 progname, device, strerror(-rc));
8255 while (optind < argc) {
8256 fid = argv[optind++];
8258 lnktmp = (linkno >= 0) ? linkno : 0;
8260 int oldtmp = lnktmp;
8261 long long rectmp = recno;
8264 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
8268 "%s fid2path: cannot find %s %s: %s\n",
8269 progname, device, fid,
8270 strerror(errno = -rc2));
8277 fprintf(stdout, "%lld ", rectmp);
8278 if (device[0] == '/') {
8279 fprintf(stdout, "%s", rootpath);
8280 if (rootpath[strlen(rootpath) - 1] != '/')
8281 fprintf(stdout, "/");
8282 } else if (path[0] == '\0') {
8283 fprintf(stdout, "/");
8285 fprintf(stdout, "%s\n", path);
8288 /* specified linkno */
8290 if (oldtmp == lnktmp)
8300 static int lfs_path2fid(int argc, char **argv)
8302 struct option long_opts[] = {
8303 { .val = 'p', .name = "parents", .has_arg = no_argument },
8306 const char short_opts[] = "p";
8307 const char *sep = "";
8310 bool show_parents = false;
8312 while ((rc = getopt_long(argc, argv, short_opts,
8313 long_opts, NULL)) != -1) {
8316 show_parents = true;
8320 "%s path2fid: unrecognized option '%s'\n",
8321 progname, argv[optind - 1]);
8326 if (optind > argc - 1) {
8327 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8330 } else if (optind < argc - 1) {
8335 for (path = argv + optind; *path != NULL; path++) {
8338 if (!show_parents) {
8339 err = llapi_path2fid(*path, &fid);
8341 printf("%s%s"DFID"\n",
8342 *sep != '\0' ? *path : "", sep,
8345 char name[NAME_MAX + 1];
8346 unsigned int linkno = 0;
8348 while ((err = llapi_path2parent(*path, linkno, &fid,
8349 name, sizeof(name))) == 0) {
8350 if (*sep != '\0' && linkno == 0)
8351 printf("%s%s", *path, sep);
8353 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8358 /* err == -ENODATA is end-of-loop */
8359 if (linkno > 0 && err == -ENODATA) {
8367 "%s path2fid: cannot get %sfid for '%s': %s\n",
8368 progname, show_parents ? "parent " : "", *path,
8380 #define MAX_ERRNO 4095
8381 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8383 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8387 rc = llapi_rmfid(device, fa);
8389 fprintf(stderr, "rmfid(): rc = %d\n", rc);
8393 for (k = 0; k < fa->fa_nr; k++) {
8394 rc = (__s32)fa->fa_fids[k].f_ver;
8395 if (!IS_ERR_VALUE(rc))
8401 fa->fa_fids[k].f_ver = 0;
8402 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
8403 PFID(&fa->fa_fids[k]), rc);
8409 static int lfs_rmfid(int argc, char **argv)
8411 char *fidstr, *device;
8412 int rc = 0, rc2, nr;
8413 struct fid_array *fa;
8415 if (optind > argc - 1) {
8416 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8420 device = argv[optind++];
8423 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8429 while (optind < argc) {
8432 fidstr = argv[optind++];
8433 while (*fidstr == '[')
8435 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8437 fprintf(stderr, "unrecognized FID: %s\n",
8442 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8443 /* start another batch */
8444 rc2 = lfs_rmfid_and_show_errors(device, fa);
8451 rc2 = lfs_rmfid_and_show_errors(device, fa);
8459 static int lfs_data_version(int argc, char **argv)
8466 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8469 fprintf(stderr, "%s data_version: FILE must be specified\n",
8474 while ((c = getopt(argc, argv, "nrw")) != -1) {
8477 data_version_flags = 0;
8480 data_version_flags |= LL_DV_RD_FLUSH;
8483 data_version_flags |= LL_DV_WR_FLUSH;
8487 "%s data_version: unrecognized option '%s'\n",
8488 progname, argv[optind - 1]);
8492 if (optind == argc) {
8493 fprintf(stderr, "%s data_version: FILE must be specified\n",
8498 path = argv[optind];
8499 fd = open(path, O_RDONLY);
8502 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8503 progname, path, strerror(-rc));
8507 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8510 "%s data_version: cannot get version for '%s': %s\n",
8511 progname, path, strerror(-rc));
8513 printf("%ju" "\n", (uintmax_t)data_version);
8519 static int lfs_hsm_state(int argc, char **argv)
8524 struct hsm_user_state hus;
8532 rc = llapi_hsm_state_get(path, &hus);
8534 fprintf(stderr, "can't get hsm state for %s: %s\n",
8535 path, strerror(errno = -rc));
8539 /* Display path name and status flags */
8540 printf("%s: (0x%08x)", path, hus.hus_states);
8542 if (hus.hus_states & HS_RELEASED)
8543 printf(" released");
8544 if (hus.hus_states & HS_EXISTS)
8546 if (hus.hus_states & HS_DIRTY)
8548 if (hus.hus_states & HS_ARCHIVED)
8549 printf(" archived");
8550 /* Display user-settable flags */
8551 if (hus.hus_states & HS_NORELEASE)
8552 printf(" never_release");
8553 if (hus.hus_states & HS_NOARCHIVE)
8554 printf(" never_archive");
8555 if (hus.hus_states & HS_LOST)
8556 printf(" lost_from_hsm");
8558 if (hus.hus_archive_id != 0)
8559 printf(", archive_id:%d", hus.hus_archive_id);
8562 } while (++i < argc);
8567 #define LFS_HSM_SET 0
8568 #define LFS_HSM_CLEAR 1
8571 * Generic function to set or clear HSM flags.
8572 * Used by hsm_set and hsm_clear.
8574 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8576 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8578 struct option long_opts[] = {
8579 { .val = 'A', .name = "archived", .has_arg = no_argument },
8580 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8581 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8582 { .val = 'e', .name = "exists", .has_arg = no_argument },
8583 { .val = 'l', .name = "lost", .has_arg = no_argument },
8584 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8585 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8587 char short_opts[] = "lraAdei:";
8591 __u32 archive_id = 0;
8597 while ((c = getopt_long(argc, argv, short_opts,
8598 long_opts, NULL)) != -1) {
8604 mask |= HS_NOARCHIVE;
8607 mask |= HS_ARCHIVED;
8610 mask |= HS_NORELEASE;
8619 archive_id = strtol(optarg, &end, 10);
8621 fprintf(stderr, "invalid archive_id: '%s'\n",
8629 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8630 argv[0], argv[optind - 1]);
8635 /* User should have specified a flag */
8639 while (optind < argc) {
8640 path = argv[optind];
8642 /* If mode == 0, this means we apply the mask. */
8643 if (mode == LFS_HSM_SET)
8644 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8646 rc = llapi_hsm_state_set(path, 0, mask, 0);
8649 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8650 path, strerror(errno = -rc));
8659 static int lfs_hsm_action(int argc, char **argv)
8664 struct hsm_current_action hca;
8665 struct hsm_extent he;
8666 enum hsm_user_action hua;
8667 enum hsm_progress_states hps;
8675 rc = llapi_hsm_current_action(path, &hca);
8677 fprintf(stderr, "can't get hsm action for %s: %s\n",
8678 path, strerror(errno = -rc));
8681 he = hca.hca_location;
8682 hua = hca.hca_action;
8683 hps = hca.hca_state;
8685 printf("%s: %s", path, hsm_user_action2name(hua));
8687 /* Skip file without action */
8688 if (hca.hca_action == HUA_NONE) {
8693 printf(" %s ", hsm_progress_state2name(hps));
8695 if ((hps == HPS_RUNNING) &&
8696 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8697 printf("(%llu bytes moved)\n",
8698 (unsigned long long)he.length);
8699 else if ((he.offset + he.length) == LUSTRE_EOF)
8700 printf("(from %llu to EOF)\n",
8701 (unsigned long long)he.offset);
8703 printf("(from %llu to %llu)\n",
8704 (unsigned long long)he.offset,
8705 (unsigned long long)(he.offset + he.length));
8707 } while (++i < argc);
8712 static int lfs_hsm_set(int argc, char **argv)
8714 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8717 static int lfs_hsm_clear(int argc, char **argv)
8719 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8723 * Check file state and return its fid, to be used by lfs_hsm_request().
8725 * \param[in] file Path to file to check
8726 * \param[in,out] fid Pointer to allocated lu_fid struct.
8727 * \param[in,out] last_dev Pointer to last device id used.
8729 * \return 0 on success.
8731 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8737 rc = lstat(file, &st);
8739 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8743 * Checking for regular file as archiving as posix copytool
8744 * rejects archiving files other than regular files
8746 if (!S_ISREG(st.st_mode)) {
8747 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8750 /* A request should be ... */
8751 if (*last_dev != st.st_dev && *last_dev != 0) {
8753 "All files should be on the same filesystem: %s\n",
8757 *last_dev = st.st_dev;
8759 rc = llapi_path2fid(file, fid);
8761 fprintf(stderr, "Cannot read FID of %s: %s\n",
8762 file, strerror(-rc));
8768 /* Fill an HSM HUR item with a given file name.
8770 * If mntpath is set, then the filename is actually a FID, and no
8771 * lookup on the filesystem will be performed.
8773 * \param[in] hur the user request to fill
8774 * \param[in] idx index of the item inside the HUR to fill
8775 * \param[in] mntpath mountpoint of Lustre
8776 * \param[in] fname filename (if mtnpath is NULL)
8777 * or FID (if mntpath is set)
8778 * \param[in] last_dev pointer to last device id used
8780 * \retval 0 on success
8781 * \retval CMD_HELP or a negative errno on error
8783 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8784 const char *mntpath, const char *fname,
8787 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8790 hui->hui_extent.length = -1;
8793 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8795 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8798 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8802 hur->hur_request.hr_itemcount++;
8807 static int lfs_hsm_request(int argc, char **argv, int action)
8809 struct option long_opts[] = {
8810 { .val = 'a', .name = "archive", .has_arg = required_argument },
8811 { .val = 'D', .name = "data", .has_arg = required_argument },
8812 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8813 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8816 char short_opts[] = "l:D:a:m:";
8817 struct hsm_user_request *hur, *oldhur;
8822 char *filelist = NULL;
8823 char fullpath[PATH_MAX];
8824 char *opaque = NULL;
8828 int nbfile_alloc = 0;
8829 char *some_file = NULL;
8830 char *mntpath = NULL;
8836 while ((c = getopt_long(argc, argv, short_opts,
8837 long_opts, NULL)) != -1) {
8846 if (action != HUA_ARCHIVE &&
8847 action != HUA_REMOVE) {
8849 "error: -a is supported only when archiving or removing\n");
8852 archive_id = atoi(optarg);
8857 some_file = strdup(optarg);
8863 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8864 argv[0], argv[optind - 1]);
8869 /* All remaining args are files, so we have at least nbfile */
8870 nbfile = argc - optind;
8872 if ((nbfile == 0) && (!filelist))
8876 opaque_len = strlen(opaque);
8879 * Alloc the request structure with enough place to store all files
8880 * from command line.
8882 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8884 fprintf(stderr, "Cannot create the request: %s\n",
8888 nbfile_alloc = nbfile;
8890 hur->hur_request.hr_action = action;
8891 hur->hur_request.hr_archive_id = archive_id;
8892 hur->hur_request.hr_flags = 0;
8894 /* All remaining args are files, add them */
8895 if (nbfile != 0 && some_file == NULL)
8896 some_file = strdup(argv[optind]);
8898 for (i = 0; i < nbfile; i++) {
8899 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8905 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8907 /* If a filelist was specified, read the filelist from it. */
8909 fp = fopen(filelist, "r");
8911 fprintf(stderr, "Cannot read the file list %s: %s\n",
8912 filelist, strerror(errno));
8917 while ((rc = getline(&line, &len, fp)) != -1) {
8919 * If allocated buffer was too small, get something
8922 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8925 nbfile_alloc = nbfile_alloc * 2 + 1;
8927 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8931 "hsm: cannot allocate the request: %s\n",
8938 size = hur_len(oldhur);
8941 "hsm: cannot allocate %u files + %u bytes data\n",
8942 oldhur->hur_request.hr_itemcount,
8943 oldhur->hur_request.hr_data_len);
8950 memcpy(hur, oldhur, size);
8955 if (line[strlen(line) - 1] == '\n')
8956 line[strlen(line) - 1] = '\0';
8958 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8959 mntpath, line, &last_dev);
8975 /* If a --data was used, add it to the request */
8976 hur->hur_request.hr_data_len = opaque_len;
8978 memcpy(hur_data(hur), opaque, opaque_len);
8980 /* Send the HSM request */
8981 if (realpath(some_file, fullpath) == NULL) {
8982 fprintf(stderr, "Could not find path '%s': %s\n",
8983 some_file, strerror(errno));
8985 rc = llapi_hsm_request(fullpath, hur);
8987 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8988 some_file, strerror(-rc));
8998 static int lfs_hsm_archive(int argc, char **argv)
9000 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9003 static int lfs_hsm_restore(int argc, char **argv)
9005 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9008 static int lfs_hsm_release(int argc, char **argv)
9010 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9013 static int lfs_hsm_remove(int argc, char **argv)
9015 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9018 static int lfs_hsm_cancel(int argc, char **argv)
9020 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9023 static int lfs_swap_layouts(int argc, char **argv)
9028 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9029 SWAP_LAYOUTS_KEEP_MTIME |
9030 SWAP_LAYOUTS_KEEP_ATIME);
9033 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9035 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9037 int lfs_get_mode(const char *string)
9039 enum lock_mode_user mode;
9041 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9042 if (lock_mode_names[mode] == NULL)
9044 if (strcmp(string, lock_mode_names[mode]) == 0)
9051 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9053 enum lu_ladvise_type advice;
9056 advice < ARRAY_SIZE(ladvise_names); advice++) {
9057 if (ladvise_names[advice] == NULL)
9059 if (strcmp(string, ladvise_names[advice]) == 0)
9063 return LU_LADVISE_INVALID;
9066 static int lfs_ladvise(int argc, char **argv)
9068 struct option long_opts[] = {
9069 { .val = 'a', .name = "advice", .has_arg = required_argument },
9070 { .val = 'b', .name = "background", .has_arg = no_argument },
9071 { .val = 'e', .name = "end", .has_arg = required_argument },
9072 { .val = 'l', .name = "length", .has_arg = required_argument },
9073 { .val = 'm', .name = "mode", .has_arg = required_argument },
9074 { .val = 's', .name = "start", .has_arg = required_argument },
9075 { .val = 'u', .name = "unset", .has_arg = no_argument },
9077 char short_opts[] = "a:be:l:m:s:u";
9082 struct llapi_lu_ladvise advice;
9083 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9084 unsigned long long start = 0;
9085 unsigned long long end = LUSTRE_EOF;
9086 unsigned long long length = 0;
9087 unsigned long long size_units;
9088 unsigned long long flags = 0;
9092 while ((c = getopt_long(argc, argv, short_opts,
9093 long_opts, NULL)) != -1) {
9096 advice_type = lfs_get_ladvice(optarg);
9097 if (advice_type == LU_LADVISE_INVALID) {
9099 "%s: invalid advice type '%s'\n",
9101 fprintf(stderr, "Valid types:");
9103 for (advice_type = 0;
9104 advice_type < ARRAY_SIZE(ladvise_names);
9106 if (ladvise_names[advice_type] == NULL)
9108 fprintf(stderr, " %s",
9109 ladvise_names[advice_type]);
9111 fprintf(stderr, "\n");
9124 rc = llapi_parse_size(optarg, &end,
9127 fprintf(stderr, "%s: bad end offset '%s'\n",
9134 rc = llapi_parse_size(optarg, &start,
9138 "%s: bad start offset '%s'\n",
9145 rc = llapi_parse_size(optarg, &length,
9148 fprintf(stderr, "%s: bad length '%s'\n",
9154 mode = lfs_get_mode(optarg);
9157 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9165 fprintf(stderr, "%s: option '%s' unrecognized\n",
9166 argv[0], argv[optind - 1]);
9171 if (advice_type == LU_LADVISE_INVALID) {
9172 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9173 fprintf(stderr, "Valid types:");
9174 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9176 if (ladvise_names[advice_type] == NULL)
9178 fprintf(stderr, " %s", ladvise_names[advice_type]);
9180 fprintf(stderr, "\n");
9184 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9186 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9191 if (argc <= optind) {
9192 fprintf(stderr, "%s: please give one or more file names\n",
9197 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9198 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9203 if (end == LUSTRE_EOF && length != 0)
9204 end = start + length;
9207 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9208 argv[0], start, end);
9212 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9213 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9218 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9219 fprintf(stderr, "%s: mode is required with lockahead\n",
9224 while (optind < argc) {
9227 path = argv[optind++];
9229 fd = open(path, O_RDONLY);
9231 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9232 argv[0], path, strerror(errno));
9237 advice.lla_start = start;
9238 advice.lla_end = end;
9239 advice.lla_advice = advice_type;
9240 advice.lla_value1 = 0;
9241 advice.lla_value2 = 0;
9242 advice.lla_value3 = 0;
9243 advice.lla_value4 = 0;
9244 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9245 advice.lla_lockahead_mode = mode;
9246 advice.lla_peradvice_flags = flags;
9249 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9253 "%s: cannot give advice '%s' to file '%s': %s\n",
9254 argv[0], ladvise_names[advice_type],
9255 path, strerror(errno));
9261 if (rc == 0 && rc2 < 0)
9267 static const char *const heat_names[] = LU_HEAT_NAMES;
9269 static int lfs_heat_get(int argc, char **argv)
9271 struct lu_heat *heat;
9280 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9282 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9287 while (optind < argc) {
9288 path = argv[optind++];
9290 fd = open(path, O_RDONLY);
9292 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9293 argv[0], path, strerror(errno));
9298 heat->lh_count = OBD_HEAT_COUNT;
9299 rc2 = llapi_heat_get(fd, heat);
9303 "%s: cannot get heat of file '%s': %s\n",
9304 argv[0], path, strerror(errno));
9308 printf("flags: %x\n", heat->lh_flags);
9309 for (i = 0; i < heat->lh_count; i++)
9310 printf("%s: %llu\n", heat_names[i],
9311 (unsigned long long)heat->lh_heat[i]);
9313 if (rc == 0 && rc2 < 0)
9321 static int lfs_heat_set(int argc, char **argv)
9323 struct option long_opts[] = {
9324 { .val = 'c', .name = "clear", .has_arg = no_argument },
9325 { .val = 'o', .name = "off", .has_arg = no_argument },
9326 { .val = 'O', .name = "on", .has_arg = no_argument },
9328 enum lu_heat_flag flags = 0;
9338 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9341 flags |= LU_HEAT_FLAG_CLEAR;
9344 flags |= LU_HEAT_FLAG_CLEAR;
9345 flags |= LU_HEAT_FLAG_OFF;
9348 flags &= ~LU_HEAT_FLAG_OFF;
9353 fprintf(stderr, "%s: option '%s' unrecognized\n",
9354 argv[0], argv[optind - 1]);
9359 if (argc <= optind) {
9360 fprintf(stderr, "%s: please give one or more file names\n",
9365 while (optind < argc) {
9366 path = argv[optind++];
9368 fd = open(path, O_RDONLY);
9370 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9371 argv[0], path, strerror(errno));
9376 rc2 = llapi_heat_set(fd, flags);
9380 "%s: cannot setflags heat of file '%s': %s\n",
9381 argv[0], path, strerror(errno));
9385 if (rc == 0 && rc2 < 0)
9392 * The input string contains a comma delimited list of component ids and
9393 * ranges, for example "1,2-4,7".
9395 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9397 bool end_of_loop = false;
9405 while (!end_of_loop) {
9409 char *endptr = NULL;
9412 ptr = strchrnul(arg, ',');
9413 end_of_loop = *ptr == '\0';
9416 start_index = strtol(arg, &endptr, 0);
9417 if (endptr == arg) /* no data at all */
9419 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9421 if (start_index < 0)
9424 end_index = start_index;
9425 if (*endptr == '-') {
9426 end_index = strtol(endptr + 1, &endptr, 0);
9427 if (*endptr != '\0')
9429 if (end_index < start_index)
9433 for (i = start_index; i <= end_index && size > 0; i++) {
9436 /* remove duplicate */
9437 for (j = 0; j < nr; j++) {
9441 if (j == nr) { /* no duplicate */
9447 if (size == 0 && i < end_index)
9454 if (!end_of_loop && ptr)
9457 return rc < 0 ? rc : nr;
9461 * struct verify_mirror_id - Mirror id to be verified.
9462 * @mirror_id: A specified mirror id.
9463 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9465 struct verify_mirror_id {
9471 * compare_mirror_ids() - Compare mirror ids.
9472 * @layout: Mirror component list.
9473 * @cbdata: Callback data in verify_mirror_id structure.
9475 * This is a callback function called by llapi_layout_comp_iterate()
9476 * to compare the specified mirror id with the one in the current
9477 * component of @layout. If they are the same, then the specified
9478 * mirror id is valid.
9480 * Return: a negative error code on failure or
9481 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9482 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9485 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9487 struct verify_mirror_id *mirror_id_cbdata =
9488 (struct verify_mirror_id *)cbdata;
9492 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9496 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9497 progname, strerror(errno));
9501 if (mirror_id_cbdata->mirror_id == mirror_id) {
9502 mirror_id_cbdata->is_valid_id = true;
9503 return LLAPI_LAYOUT_ITER_STOP;
9506 return LLAPI_LAYOUT_ITER_CONT;
9510 * verify_mirror_ids() - Verify specified mirror ids.
9511 * @fname: Mirrored file name.
9512 * @mirror_ids: Specified mirror ids to be verified.
9513 * @ids_nr: Number of specified mirror ids.
9515 * This function verifies that specified @mirror_ids are valid
9516 * in the mirrored file @fname.
9518 * Return: 0 on success or a negative error code on failure.
9521 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9523 struct llapi_layout *layout = NULL;
9524 struct verify_mirror_id mirror_id_cbdata = { 0 };
9535 if (stat(fname, &stbuf) < 0) {
9536 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9537 progname, fname, strerror(errno));
9542 if (!S_ISREG(stbuf.st_mode)) {
9543 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9549 fd = open(fname, O_DIRECT | O_RDONLY);
9551 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9552 progname, fname, strerror(errno));
9557 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9559 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9560 progname, fname, strerror(errno));
9564 layout = llapi_layout_get_by_fd(fd, 0);
9566 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9567 progname, fname, strerror(errno));
9569 llapi_lease_release(fd);
9573 rc = llapi_layout_flags_get(layout, &flr_state);
9575 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9576 progname, fname, strerror(errno));
9581 flr_state &= LCM_FL_FLR_MASK;
9582 switch (flr_state) {
9585 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9586 progname, fname, llapi_layout_flags_string(flr_state));
9593 for (i = 0; i < ids_nr; i++) {
9594 mirror_id_cbdata.mirror_id = mirror_ids[i];
9595 mirror_id_cbdata.is_valid_id = false;
9597 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9602 "%s: '%s' failed to verify mirror id: %u.\n",
9603 progname, fname, mirror_ids[i]);
9607 if (!mirror_id_cbdata.is_valid_id) {
9610 "%s: '%s' invalid specified mirror id: %u.\n",
9611 progname, fname, mirror_ids[i]);
9617 llapi_layout_free(layout);
9618 llapi_lease_release(fd);
9626 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9627 __u16 *mirror_ids, int ids_nr)
9629 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9630 struct llapi_layout *layout;
9641 if (stat(fname, &stbuf) < 0) {
9642 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9643 progname, fname, strerror(errno));
9647 if (!S_ISREG(stbuf.st_mode)) {
9648 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9654 fd = open(fname, O_DIRECT | O_RDWR);
9656 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9657 progname, fname, strerror(errno));
9662 layout = llapi_layout_get_by_fd(fd, 0);
9664 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9665 progname, fname, strerror(errno));
9670 rc = llapi_layout_flags_get(layout, &flr_state);
9672 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9673 progname, fname, strerror(errno));
9678 flr_state &= LCM_FL_FLR_MASK;
9679 if (flr_state == LCM_FL_NONE) {
9681 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9686 /* get stale component info */
9687 comp_size = llapi_mirror_find_stale(layout, comp_array,
9688 ARRAY_SIZE(comp_array),
9689 mirror_ids, ids_nr);
9690 if (comp_size <= 0) {
9695 ioc->lil_mode = LL_LEASE_WRLCK;
9696 ioc->lil_flags = LL_LEASE_RESYNC;
9697 rc = llapi_lease_set(fd, ioc);
9699 if (rc == -EALREADY)
9703 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9704 progname, fname, strerror(-rc));
9708 /* get the read range [start, end) */
9709 start = comp_array[0].lrc_start;
9710 end = comp_array[0].lrc_end;
9711 for (idx = 1; idx < comp_size; idx++) {
9712 if (comp_array[idx].lrc_start < start)
9713 start = comp_array[idx].lrc_start;
9714 if (end < comp_array[idx].lrc_end)
9715 end = comp_array[idx].lrc_end;
9718 rc = llapi_lease_check(fd);
9719 if (rc != LL_LEASE_WRLCK) {
9720 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9725 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9728 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9729 progname, fname, strerror(-rc));
9731 /* need to do the lease unlock even resync fails */
9732 ioc->lil_mode = LL_LEASE_UNLCK;
9733 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9735 for (idx = 0; idx < comp_size; idx++) {
9736 if (comp_array[idx].lrc_synced) {
9737 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9742 rc2 = llapi_lease_set(fd, ioc);
9744 * llapi_lease_set returns lease mode when it request to unlock
9748 /* rc2 == 0 means lost lease lock */
9749 if (rc2 == 0 && rc == 0)
9751 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9753 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9757 llapi_layout_free(layout);
9764 static inline int lfs_mirror_resync(int argc, char **argv)
9766 struct ll_ioc_lease *ioc = NULL;
9767 __u16 mirror_ids[128] = { 0 };
9772 struct option long_opts[] = {
9773 { .val = 'o', .name = "only", .has_arg = required_argument },
9776 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9779 rc = parse_mirror_ids(mirror_ids,
9780 sizeof(mirror_ids) / sizeof(__u16),
9784 "%s: bad mirror ids '%s'.\n",
9791 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9792 argv[0], argv[optind - 1]);
9798 if (argc == optind) {
9799 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9804 if (ids_nr > 0 && argc > optind + 1) {
9806 "%s: option '--only' cannot be used upon multiple files.\n",
9813 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9818 /* set the lease on the file */
9819 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9821 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9822 argv[0], strerror(errno));
9827 for (; optind < argc; optind++) {
9828 rc = lfs_mirror_resync_file(argv[optind], ioc,
9829 mirror_ids, ids_nr);
9830 /* ignore previous file's error, continue with next file */
9833 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9841 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9843 struct llapi_layout *layout;
9846 layout = llapi_layout_get_by_fd(fd, 0);
9848 fprintf(stderr, "could not get layout.\n");
9852 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9854 fprintf(stderr, "failed to iterate layout\n");
9855 llapi_layout_free(layout);
9858 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9859 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9860 llapi_layout_free(layout);
9864 llapi_layout_free(layout);
9870 * Check whether two files are the same file
9871 * \retval 0 same file
9872 * \retval 1 not the same file
9873 * \retval <0 error code
9875 static inline int check_same_file(int fd, const char *f2)
9880 if (fstat(fd, &stbuf1) < 0)
9883 if (stat(f2, &stbuf2) < 0)
9886 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9887 stbuf1.st_ino == stbuf2.st_ino)
9893 static inline int lfs_mirror_read(int argc, char **argv)
9896 __u16 mirror_id = 0;
9897 const char *outfile = NULL;
9903 const size_t buflen = 4 << 20;
9905 struct option long_opts[] = {
9906 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9907 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9910 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9915 mirror_id = strtoul(optarg, &end, 0);
9916 if (*end != '\0' || mirror_id == 0) {
9918 "%s %s: invalid mirror ID '%s'\n",
9919 progname, argv[0], optarg);
9927 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9928 progname, argv[optind - 1]);
9933 if (argc == optind) {
9934 fprintf(stderr, "%s %s: no mirrored file provided\n",
9937 } else if (argc > optind + 1) {
9938 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9942 if (mirror_id == 0) {
9943 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9948 /* open mirror file */
9949 fname = argv[optind];
9950 fd = open(fname, O_DIRECT | O_RDONLY);
9952 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9953 progname, argv[0], fname, strerror(errno));
9957 /* verify mirror id */
9958 rc = verify_mirror_id_by_fd(fd, mirror_id);
9961 "%s %s: cannot find mirror with ID %u in '%s'\n",
9962 progname, argv[0], mirror_id, fname);
9966 /* open output file - O_EXCL ensures output is not the same as input */
9968 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9970 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9971 progname, argv[0], outfile, strerror(errno));
9976 outfd = STDOUT_FILENO;
9979 /* allocate buffer */
9980 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9982 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9983 progname, argv[0], rc);
9990 ssize_t written = 0;
9992 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9993 if (bytes_read < 0) {
9996 "%s %s: fail to read data from mirror %u: %s\n",
9997 progname, argv[0], mirror_id, strerror(-rc));
10002 if (bytes_read == 0)
10005 while (written < bytes_read) {
10008 written2 = write(outfd, buf + written,
10009 bytes_read - written);
10010 if (written2 < 0) {
10012 "%s %s: fail to write %s: %s\n",
10013 progname, argv[0], outfile ? : "STDOUT",
10018 written += written2;
10021 if (written != bytes_read) {
10023 "%s %s: written %ld bytes does not match with %ld read.\n",
10024 progname, argv[0], written, bytes_read);
10046 static inline int lfs_mirror_write(int argc, char **argv)
10049 __u16 mirror_id = 0;
10050 const char *inputfile = NULL;
10056 const size_t buflen = 4 << 20;
10058 size_t page_size = sysconf(_SC_PAGESIZE);
10059 struct ll_ioc_lease_id ioc;
10061 struct option long_opts[] = {
10062 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10063 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10064 { .name = NULL } };
10066 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10071 mirror_id = strtoul(optarg, &end, 0);
10072 if (*end != '\0' || mirror_id == 0) {
10074 "%s %s: invalid mirror ID '%s'\n",
10075 progname, argv[0], optarg);
10080 inputfile = optarg;
10083 fprintf(stderr, "%s: option '%s' unrecognized\n",
10084 progname, argv[optind - 1]);
10089 if (argc == optind) {
10090 fprintf(stderr, "%s %s: no mirrored file provided\n",
10091 progname, argv[0]);
10093 } else if (argc > optind + 1) {
10094 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10098 if (mirror_id == 0) {
10099 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10100 progname, argv[0]);
10104 /* open mirror file */
10105 fname = argv[optind];
10106 fd = open(fname, O_DIRECT | O_WRONLY);
10108 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10109 progname, argv[0], fname, strerror(errno));
10113 /* verify mirror id */
10114 rc = verify_mirror_id_by_fd(fd, mirror_id);
10117 "%s %s: cannot find mirror with ID %u in '%s'\n",
10118 progname, argv[0], mirror_id, fname);
10122 /* open input file */
10124 rc = check_same_file(fd, inputfile);
10127 "%s %s: input file cannot be the mirrored file\n",
10128 progname, argv[0]);
10134 inputfd = open(inputfile, O_RDONLY, 0644);
10136 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10137 progname, argv[0], inputfile, strerror(errno));
10142 inputfd = STDIN_FILENO;
10145 /* allocate buffer */
10146 rc = posix_memalign(&buf, page_size, buflen);
10148 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10149 progname, argv[0], rc);
10150 goto close_inputfd;
10153 /* prepare target mirror components instantiation */
10154 ioc.lil_mode = LL_LEASE_WRLCK;
10155 ioc.lil_flags = LL_LEASE_RESYNC;
10156 ioc.lil_mirror_id = mirror_id;
10157 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10160 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10161 progname, argv[0], fname, strerror(errno));
10167 ssize_t bytes_read;
10171 rc = llapi_lease_check(fd);
10172 if (rc != LL_LEASE_WRLCK) {
10173 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10174 progname, argv[0], fname);
10178 bytes_read = read(inputfd, buf, buflen);
10179 if (bytes_read < 0) {
10182 "%s %s: fail to read data from '%s': %s\n",
10183 progname, argv[0], inputfile ? : "STDIN",
10190 if (bytes_read == 0)
10193 /* round up to page align to make direct IO happy. */
10194 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10196 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10201 "%s %s: fail to write to mirror %u: %s\n",
10202 progname, argv[0], mirror_id,
10210 if (pos & (page_size - 1)) {
10211 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10216 ioc.lil_mode = LL_LEASE_UNLCK;
10217 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10219 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10224 "%s %s: release lease lock of '%s' failed: %s\n",
10225 progname, argv[0], fname, strerror(errno));
10242 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10244 struct llapi_layout *layout;
10245 struct collect_ids_data cid = { .cid_ids = ids,
10247 .cid_exclude = exclude_id, };
10250 layout = llapi_layout_get_by_fd(fd, 0);
10252 fprintf(stderr, "could not get layout\n");
10256 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10258 fprintf(stderr, "failed to iterate layout\n");
10259 llapi_layout_free(layout);
10263 llapi_layout_free(layout);
10265 return cid.cid_count;
10268 static inline int lfs_mirror_copy(int argc, char **argv)
10271 __u16 read_mirror_id = 0;
10272 __u16 ids[128] = { 0 };
10274 struct llapi_layout *layout = NULL;
10275 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10282 struct ll_ioc_lease *ioc = NULL;
10283 struct ll_ioc_lease_id *resync_ioc;
10285 struct option long_opts[] = {
10286 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10287 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10288 { .name = NULL } };
10290 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10295 read_mirror_id = strtoul(optarg, &end, 0);
10296 if (*end != '\0' || read_mirror_id == 0) {
10298 "%s %s: invalid read mirror ID '%s'\n",
10299 progname, argv[0], optarg);
10304 if (!strcmp(optarg, "-1")) {
10305 /* specify all other mirrors */
10306 ids[0] = (__u16)-1;
10309 count = parse_mirror_ids((__u16 *)ids,
10317 fprintf(stderr, "%s: option '%s' unrecognized\n",
10318 progname, argv[optind - 1]);
10323 if (argc == optind) {
10324 fprintf(stderr, "%s %s: no mirrored file provided\n",
10325 progname, argv[0]);
10327 } else if (argc > optind + 1) {
10328 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10332 if (read_mirror_id == 0) {
10334 "%s %s: no valid read mirror ID %d is provided\n",
10335 progname, argv[0], read_mirror_id);
10341 "%s %s: no write mirror ID is provided\n",
10342 progname, argv[0]);
10346 for (i = 0; i < count; i++) {
10347 if (read_mirror_id == ids[i]) {
10349 "%s %s: read and write mirror ID cannot be the same\n",
10350 progname, argv[0]);
10355 /* open mirror file */
10356 fname = argv[optind];
10358 fd = open(fname, O_DIRECT | O_RDWR);
10360 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10361 progname, argv[0], fname, strerror(errno));
10365 /* write to all other mirrors */
10366 if (ids[0] == (__u16)-1) {
10367 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10371 "%s %s: failed to get other mirror ids in '%s': %d\n",
10372 progname, argv[0], fname, rc);
10377 /* verify mirror id */
10378 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10381 "%s %s: cannot find mirror with ID %u in '%s'\n",
10382 progname, argv[0], read_mirror_id, fname);
10386 for (i = 0; i < count; i++) {
10387 rc = verify_mirror_id_by_fd(fd, ids[i]);
10390 "%s %s: cannot find mirror with ID %u in '%s'\n",
10391 progname, argv[0], ids[i], fname);
10396 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10399 "%s %s: cannot alloc comp id array for ioc: %s\n",
10400 progname, argv[0], strerror(errno));
10405 /* get stale component info */
10406 layout = llapi_layout_get_by_fd(fd, 0);
10408 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10409 progname, argv[0], fname, strerror(errno));
10413 comp_size = llapi_mirror_find_stale(layout, comp_array,
10414 ARRAY_SIZE(comp_array),
10416 llapi_layout_free(layout);
10417 if (comp_size < 0) {
10422 /* prepare target mirror components instantiation */
10423 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10424 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10425 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10427 resync_ioc->lil_mirror_id = ids[0];
10429 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10430 rc = llapi_lease_set(fd, ioc);
10433 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10434 progname, argv[0], fname, strerror(errno));
10438 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10441 fprintf(stderr, "%s %s: copy error: %d\n",
10442 progname, argv[0], rc);
10446 fprintf(stdout, "mirror copied successfully: ");
10447 for (i = 0; i < copied; i++)
10448 fprintf(stdout, "%d ", ids[i]);
10449 fprintf(stdout, "\n");
10451 ioc->lil_mode = LL_LEASE_UNLCK;
10452 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10453 ioc->lil_count = 0;
10454 for (i = 0; i < comp_size; i++) {
10457 for (j = 0; j < copied; j++) {
10458 if (comp_array[i].lrc_mirror_id != ids[j])
10461 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10465 rc = llapi_lease_set(fd, ioc);
10470 "%s %s: release lease lock of '%s' failed: %s\n",
10471 progname, argv[0], fname, strerror(errno));
10486 * struct verify_chunk - Mirror chunk to be verified.
10487 * @chunk: [start, end) of the chunk.
10488 * @mirror_count: Number of mirror ids in @mirror_id array.
10489 * @mirror_id: Array of valid mirror ids that cover the chunk.
10491 struct verify_chunk {
10492 struct lu_extent chunk;
10493 unsigned int mirror_count;
10494 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10498 * print_chunks() - Print chunk information.
10499 * @fname: Mirrored file name.
10500 * @chunks: Array of chunks.
10501 * @chunk_count: Number of chunks in @chunks array.
10503 * This function prints [start, end) of each chunk in @chunks
10504 * for mirrored file @fname, and also prints the valid mirror ids
10505 * that cover the chunk.
10510 void print_chunks(const char *fname, struct verify_chunk *chunks,
10516 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10517 for (i = 0; i < chunk_count; i++) {
10518 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10520 if (chunks[i].mirror_count == 0)
10521 fprintf(stdout, "\t[");
10523 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10524 for (j = 1; j < chunks[i].mirror_count; j++)
10525 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10527 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10529 fprintf(stdout, "\n");
10533 * print_checksums() - Print CRC-32 checksum values.
10534 * @chunk: A chunk and its corresponding valid mirror ids.
10535 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10537 * This function prints CRC-32 checksum values on @chunk for
10538 * each valid mirror that covers it.
10543 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10548 "CRC-32 checksum value for chunk "DEXT":\n",
10549 PEXT(&chunk->chunk));
10550 for (i = 0; i < chunk->mirror_count; i++)
10551 fprintf(stdout, "Mirror %u:\t%#lx\n",
10552 chunk->mirror_id[i], crc[i]);
10553 fprintf(stdout, "\n");
10557 * filter_mirror_id() - Filter specified mirror ids.
10558 * @chunks: Array of chunks.
10559 * @chunk_count: Number of chunks in @chunks array.
10560 * @mirror_ids: Specified mirror ids to be verified.
10561 * @ids_nr: Number of specified mirror ids.
10563 * This function scans valid mirror ids that cover each chunk in @chunks
10564 * and filters specified mirror ids.
10569 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10570 __u16 *mirror_ids, int ids_nr)
10575 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10576 unsigned int valid_count = 0;
10578 for (i = 0; i < chunk_count; i++) {
10579 if (chunks[i].mirror_count == 0)
10583 for (j = 0; j < ids_nr; j++) {
10584 for (k = 0; k < chunks[i].mirror_count; k++) {
10585 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10586 valid_id[valid_count] = mirror_ids[j];
10593 memcpy(chunks[i].mirror_id, valid_id,
10594 sizeof(__u16) * valid_count);
10595 chunks[i].mirror_count = valid_count;
10600 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10601 * @layout: Mirror component list.
10602 * @chunks: Array of chunks.
10603 * @chunks_size: Array size of @chunks.
10605 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10606 * to find out chunk segments and store them in @chunks array.
10608 * The @mirror_id array in each element of @chunks will store the valid
10609 * mirror ids that cover the chunk. If a mirror component covering the
10610 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10611 * will not be stored into the @mirror_id array, and the chunk for that
10612 * mirror will not be verified.
10614 * The @mirror_count in each element of @chunks will store the number of
10615 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10616 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10617 * indicates the chunk is valid in only one mirror. In both cases, the
10618 * chunk will not be verified.
10620 * Here is an example:
10622 * 0 1M 2M 3M 4M EOF
10623 * +------+-------------+--------------------+
10624 * | | | S | mirror1
10625 * +------+------+------+------+-------------+
10626 * | | S | S | | mirror2
10627 * +-------------+------+------+-------------+
10629 * prepared @chunks array will contain 5 elements:
10630 * (([0, 1M), [1, 2], 2),
10631 * ([1M, 2M), [1, 2], 2),
10632 * ([2M, 3M), [1], 1),
10633 * ([3M, 4M], [], 0),
10634 * ([4M, EOF), [2], 1))
10636 * Return: the actual array size of @chunks on success
10637 * or a negative error code on failure.
10640 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10641 struct verify_chunk *chunks,
10642 size_t chunks_size)
10646 uint32_t mirror_id;
10652 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10655 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10658 "%s: move to the first layout component: %s.\n",
10659 progname, strerror(errno));
10665 chunks[idx].chunk.e_end = LUSTRE_EOF;
10667 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10670 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10671 progname, strerror(errno));
10675 if (start > chunks[idx].chunk.e_start ||
10676 end <= chunks[idx].chunk.e_start)
10679 if (end < chunks[idx].chunk.e_end)
10680 chunks[idx].chunk.e_end = end;
10682 rc = llapi_layout_comp_flags_get(layout, &flags);
10685 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10686 progname, strerror(errno));
10690 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10693 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10696 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10697 progname, strerror(errno));
10701 chunks[idx].mirror_id[i] = mirror_id;
10703 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10705 "%s: mirror_id array is too small.\n",
10712 rc = llapi_layout_comp_use(layout,
10713 LLAPI_LAYOUT_COMP_USE_NEXT);
10716 "%s: move to the next layout component: %s.\n",
10717 progname, strerror(errno));
10720 } /* loop through all components */
10722 chunks[idx].mirror_count = i;
10724 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10728 if (idx >= chunks_size) {
10729 fprintf(stderr, "%s: chunks array is too small.\n",
10735 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10739 return rc < 0 ? rc : idx + 1;
10743 * lfs_mirror_verify_chunk() - Verify a chunk.
10744 * @fd: File descriptor of the mirrored file.
10745 * @file_size: Size of the mirrored file.
10746 * @chunk: A chunk and its corresponding valid mirror ids.
10747 * @verbose: Verbose mode.
10749 * This function verifies a @chunk contains exactly the same data
10750 * ammong the mirrors that cover it.
10752 * If @verbose is specified, then the function will print where the
10753 * differences are if the data do not match. Otherwise, it will
10754 * just return an error in that case.
10756 * Return: 0 on success or a negative error code on failure.
10759 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10760 struct verify_chunk *chunk, int verbose)
10762 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10764 size_t page_size = sysconf(_SC_PAGESIZE);
10765 ssize_t bytes_read;
10766 ssize_t bytes_done;
10770 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10774 if (file_size == 0)
10777 rc = posix_memalign(&buf, page_size, buflen);
10778 if (rc) /* error code is returned directly */
10782 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10783 PEXT(&chunk->chunk));
10784 for (i = 0; i < chunk->mirror_count; i++)
10785 fprintf(stdout, " %u", chunk->mirror_id[i]);
10786 fprintf(stdout, "\n");
10790 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10791 pos = chunk->chunk.e_start;
10792 while (bytes_done < count) {
10793 /* compute initial CRC-32 checksum */
10794 crc = crc32(0L, Z_NULL, 0);
10795 memset(crc_array, 0, sizeof(crc_array));
10798 for (i = 0; i < chunk->mirror_count; i++) {
10799 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10801 if (bytes_read < 0) {
10804 "%s: failed to read data from mirror %u: %s.\n",
10805 progname, chunk->mirror_id[i],
10810 /* compute new CRC-32 checksum */
10811 crc_array[i] = crc32(crc, buf, bytes_read);
10815 print_checksums(chunk, crc_array);
10817 /* compare CRC-32 checksum values */
10818 for (i = 1; i < chunk->mirror_count; i++) {
10819 if (crc_array[i] != crc_array[0]) {
10823 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10824 progname, PEXT(&chunk->chunk),
10825 chunk->mirror_id[0],
10826 chunk->mirror_id[i]);
10831 bytes_done += bytes_read;
10834 if (verbose > 1 && rc == 0) {
10835 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10836 PEXT(&chunk->chunk));
10837 for (i = 0; i < chunk->mirror_count; i++)
10838 fprintf(stdout, " %u", chunk->mirror_id[i]);
10839 fprintf(stdout, " PASS\n\n");
10848 * lfs_mirror_verify_file() - Verify a mirrored file.
10849 * @fname: Mirrored file name.
10850 * @mirror_ids: Specified mirror ids to be verified.
10851 * @ids_nr: Number of specified mirror ids.
10852 * @verbose: Verbose mode.
10854 * This function verifies that each SYNC mirror of a mirrored file
10855 * specified by @fname contains exactly the same data.
10857 * If @mirror_ids is specified, then the function will verify the
10858 * mirrors specified by @mirror_ids contain exactly the same data.
10860 * If @verbose is specified, then the function will print where the
10861 * differences are if the data do not match. Otherwise, it will
10862 * just return an error in that case.
10864 * Return: 0 on success or a negative error code on failure.
10867 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10870 struct verify_chunk chunks_array[1024] = { };
10871 struct llapi_layout *layout = NULL;
10873 uint32_t flr_state;
10875 int chunk_count = 0;
10881 if (stat(fname, &stbuf) < 0) {
10882 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10883 progname, fname, strerror(errno));
10888 if (!S_ISREG(stbuf.st_mode)) {
10889 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10895 if (stbuf.st_size == 0) {
10897 fprintf(stdout, "%s: '%s' file size is 0.\n",
10903 fd = open(fname, O_DIRECT | O_RDONLY);
10905 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10906 progname, fname, strerror(errno));
10911 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10913 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10914 progname, fname, strerror(errno));
10918 layout = llapi_layout_get_by_fd(fd, 0);
10920 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10921 progname, fname, strerror(errno));
10923 llapi_lease_release(fd);
10927 rc = llapi_layout_flags_get(layout, &flr_state);
10929 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10930 progname, fname, strerror(errno));
10935 flr_state &= LCM_FL_FLR_MASK;
10936 switch (flr_state) {
10939 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10940 progname, fname, llapi_layout_flags_string(flr_state));
10946 /* find out mirror chunks to be verified */
10947 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10948 ARRAY_SIZE(chunks_array));
10949 if (chunk_count < 0) {
10955 /* filter specified mirror ids */
10956 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10959 print_chunks(fname, chunks_array, chunk_count);
10961 for (idx = 0; idx < chunk_count; idx++) {
10962 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10965 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10967 PEXT(&chunks_array[idx].chunk),
10968 (unsigned long long)stbuf.st_size);
10972 if (chunks_array[idx].mirror_count == 0) {
10974 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10976 PEXT(&chunks_array[idx].chunk));
10978 fprintf(stderr, "skipped\n");
10982 fprintf(stderr, "failed\n");
10986 if (chunks_array[idx].mirror_count == 1) {
10989 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10991 PEXT(&chunks_array[idx].chunk),
10992 chunks_array[idx].mirror_id[0]);
10996 rc = llapi_lease_check(fd);
10997 if (rc != LL_LEASE_RDLCK) {
10998 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11003 /* verify one chunk */
11004 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11005 &chunks_array[idx], verbose);
11019 llapi_layout_free(layout);
11020 llapi_lease_release(fd);
11028 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11029 * @argc: The count of lfs mirror verify command line arguments.
11030 * @argv: Array of strings for lfs mirror verify command line arguments.
11032 * This function parses lfs mirror verify command and verifies the
11033 * specified mirrored file(s).
11035 * Return: 0 on success or a negative error code on failure.
11037 static inline int lfs_mirror_verify(int argc, char **argv)
11039 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11045 char cmd[PATH_MAX];
11047 struct option long_opts[] = {
11048 { .val = 'o', .name = "only", .has_arg = required_argument },
11049 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11050 { .name = NULL } };
11052 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11054 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11057 rc = parse_mirror_ids(mirror_ids,
11058 ARRAY_SIZE(mirror_ids),
11062 "%s: bad mirror ids '%s'.\n",
11069 "%s: at least 2 mirror ids needed with '--only' option.\n",
11079 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11080 progname, argv[optind - 1]);
11086 if (argc == optind) {
11087 fprintf(stderr, "%s: no file name given.\n", progname);
11092 if (ids_nr > 0 && argc > optind + 1) {
11094 "%s: '--only' cannot be used upon multiple files.\n",
11101 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11107 for (; optind < argc; optind++) {
11108 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11118 * lfs_mirror() - Parse and execute lfs mirror commands.
11119 * @argc: The count of lfs mirror command line arguments.
11120 * @argv: Array of strings for lfs mirror command line arguments.
11122 * This function parses lfs mirror commands and performs the
11123 * corresponding functions specified in mirror_cmdlist[].
11125 * Return: 0 on success or an error code on failure.
11127 static int lfs_mirror(int argc, char **argv)
11129 char cmd[PATH_MAX];
11132 setlinebuf(stdout);
11134 Parser_init("lfs-mirror > ", mirror_cmdlist);
11136 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11138 program_invocation_short_name = cmd;
11140 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11142 rc = Parser_commands();
11144 return rc < 0 ? -rc : rc;
11147 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11149 #if __BYTE_ORDER == __BIG_ENDIAN
11150 __swab16s(&attrs->lsa_valid);
11151 __swab64s(&attrs->lsa_size);
11152 __swab64s(&attrs->lsa_blocks);
11156 enum lfs_som_type {
11157 LFS_SOM_SIZE = 0x1,
11158 LFS_SOM_BLOCKS = 0x2,
11159 LFS_SOM_FLAGS = 0x4,
11160 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11164 static int lfs_getsom(int argc, char **argv)
11167 struct lustre_som_attrs *attrs;
11168 char buf[sizeof(*attrs) + 64];
11169 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11172 while ((c = getopt(argc, argv, "sbf")) != -1) {
11175 type = LFS_SOM_SIZE;
11178 type = LFS_SOM_BLOCKS;
11181 type = LFS_SOM_FLAGS;
11184 fprintf(stderr, "%s: invalid option '%c'\n",
11194 fprintf(stderr, "%s: %s\n",
11195 progname, argc == 0 ? "miss file target" :
11196 "input more than 2 files");
11201 attrs = (void *)buf;
11202 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11205 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11206 argv[0], strerror(errno), errno);
11210 lustre_som_swab(attrs);
11213 case LFS_SOM_ATTR_ALL:
11214 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11215 path, (unsigned long long)attrs->lsa_size,
11216 (unsigned long long)attrs->lsa_blocks,
11220 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11222 case LFS_SOM_BLOCKS:
11223 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11225 case LFS_SOM_FLAGS:
11226 printf("%x\n", attrs->lsa_valid);
11229 fprintf(stderr, "%s: unknown option\n", progname);
11237 * lfs_mirror_list_commands() - List lfs mirror commands.
11238 * @argc: The count of command line arguments.
11239 * @argv: Array of strings for command line arguments.
11241 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11243 * Return: 0 on success.
11245 static int lfs_mirror_list_commands(int argc, char **argv)
11247 char buffer[81] = "";
11249 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11255 static int lfs_pcc_attach(int argc, char **argv)
11257 struct option long_opts[] = {
11258 { .val = 'i', .name = "id", .has_arg = required_argument },
11259 { .name = NULL } };
11262 __u32 archive_id = 0;
11265 char fullpath[PATH_MAX];
11266 enum lu_pcc_type type = LU_PCC_READWRITE;
11269 while ((c = getopt_long(argc, argv, "i:",
11270 long_opts, NULL)) != -1) {
11273 archive_id = strtoul(optarg, &end, 0);
11274 if (*end != '\0' || archive_id == 0) {
11276 "error: %s: bad archive ID '%s'\n",
11284 fprintf(stderr, "%s: option '%s' unrecognized\n",
11285 argv[0], argv[optind - 1]);
11290 if (archive_id == 0) {
11291 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11295 if (argc <= optind) {
11296 fprintf(stderr, "%s: must specify one or more file names\n",
11301 while (optind < argc) {
11304 path = argv[optind++];
11305 if (!realpath(path, fullpath)) {
11306 fprintf(stderr, "%s: could not find path '%s': %s\n",
11307 argv[0], path, strerror(errno));
11313 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11316 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11317 argv[0], path, archive_id, strerror(-rc2));
11325 static int lfs_pcc_attach_fid(int argc, char **argv)
11327 struct option long_opts[] = {
11328 { .val = 'i', .name = "id", .has_arg = required_argument },
11329 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11330 { .name = NULL } };
11331 char short_opts[] = "i:m:";
11334 __u32 archive_id = 0;
11336 const char *mntpath = NULL;
11337 const char *fidstr;
11338 enum lu_pcc_type type = LU_PCC_READWRITE;
11341 while ((c = getopt_long(argc, argv, short_opts,
11342 long_opts, NULL)) != -1) {
11345 archive_id = strtoul(optarg, &end, 0);
11346 if (*end != '\0') {
11348 "error: %s: bad archive ID '%s'\n",
11359 fprintf(stderr, "%s: option '%s' unrecognized\n",
11360 argv[0], argv[optind - 1]);
11365 if (archive_id == 0) {
11366 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11371 fprintf(stderr, "%s: must specify Lustre mount point\n",
11376 if (argc <= optind) {
11377 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11381 while (optind < argc) {
11384 fidstr = argv[optind++];
11386 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11390 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11391 argv[0], fidstr, mntpath, archive_id,
11394 if (rc == 0 && rc2 < 0)
11400 static int lfs_pcc_detach(int argc, char **argv)
11402 struct option long_opts[] = {
11403 { .val = 'k', .name = "keep", .has_arg = no_argument },
11404 { .name = NULL } };
11405 char short_opts[] = "k";
11409 char fullpath[PATH_MAX];
11410 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11413 while ((c = getopt_long(argc, argv, short_opts,
11414 long_opts, NULL)) != -1) {
11417 detach_opt = PCC_DETACH_OPT_NONE;
11422 fprintf(stderr, "%s: option '%s' unrecognized\n",
11423 argv[0], argv[optind - 1]);
11428 while (optind < argc) {
11431 path = argv[optind++];
11432 if (!realpath(path, fullpath)) {
11433 fprintf(stderr, "%s: could not find path '%s': %s\n",
11434 argv[0], path, strerror(errno));
11440 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11444 "%s: cannot detach '%s' from PCC: %s\n",
11445 argv[0], path, strerror(errno));
11453 static int lfs_pcc_detach_fid(int argc, char **argv)
11455 struct option long_opts[] = {
11456 { .val = 'k', .name = "keep", .has_arg = no_argument },
11457 { .name = NULL } };
11458 char short_opts[] = "k";
11462 const char *mntpath;
11463 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11466 while ((c = getopt_long(argc, argv, short_opts,
11467 long_opts, NULL)) != -1) {
11470 detach_opt = PCC_DETACH_OPT_NONE;
11475 fprintf(stderr, "%s: option '%s' unrecognized\n",
11476 argv[0], argv[optind - 1]);
11481 mntpath = argv[optind++];
11483 while (optind < argc) {
11486 fid = argv[optind++];
11488 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11491 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11492 argv[0], fid, mntpath, strerror(-rc2));
11500 static int lfs_pcc_state(int argc, char **argv)
11504 char fullpath[PATH_MAX];
11505 struct lu_pcc_state state;
11510 fprintf(stderr, "%s: must specify one or more file names\n",
11515 while (optind < argc) {
11518 path = argv[optind++];
11519 if (!realpath(path, fullpath)) {
11520 fprintf(stderr, "%s: could not find path '%s': %s\n",
11521 argv[0], path, strerror(errno));
11527 rc2 = llapi_pcc_state_get(fullpath, &state);
11532 "%s: cannot get PCC state of '%s': %s\n",
11533 argv[0], path, strerror(-rc2));
11537 printf("file: %s", path);
11538 printf(", type: %s", pcc_type2string(state.pccs_type));
11539 if (state.pccs_type == LU_PCC_NONE &&
11540 state.pccs_open_count == 0) {
11545 printf(", PCC file: %s", state.pccs_path);
11546 printf(", user number: %u", state.pccs_open_count);
11547 printf(", flags: %x", state.pccs_flags);
11554 * lfs_pcc_list_commands() - List lfs pcc commands.
11555 * @argc: The count of command line arguments.
11556 * @argv: Array of strings for command line arguments.
11558 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11560 * Return: 0 on success.
11562 static int lfs_pcc_list_commands(int argc, char **argv)
11564 char buffer[81] = "";
11566 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11573 * lfs_pcc() - Parse and execute lfs pcc commands.
11574 * @argc: The count of lfs pcc command line arguments.
11575 * @argv: Array of strings for lfs pcc command line arguments.
11577 * This function parses lfs pcc commands and performs the
11578 * corresponding functions specified in pcc_cmdlist[].
11580 * Return: 0 on success or an error code on failure.
11582 static int lfs_pcc(int argc, char **argv)
11584 char cmd[PATH_MAX];
11587 setlinebuf(stdout);
11589 Parser_init("lfs-pcc > ", pcc_cmdlist);
11591 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11593 program_invocation_short_name = cmd;
11595 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11597 rc = Parser_commands();
11599 return rc < 0 ? -rc : rc;
11602 static int lfs_list_commands(int argc, char **argv)
11604 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11606 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11611 int main(int argc, char **argv)
11615 /* Ensure that liblustreapi constructor has run */
11616 if (!llapi_liblustreapi_initialized())
11617 fprintf(stderr, "liblustreapi was not properly initialized\n");
11619 setlinebuf(stdout);
11622 Parser_init("lfs > ", cmdlist);
11624 progname = program_invocation_short_name; /* Used in error messages */
11626 llapi_set_command_name(argv[1]);
11627 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11628 llapi_clear_command_name();
11630 rc = Parser_commands();
11633 return rc < 0 ? -rc : rc;
11636 #ifdef _LUSTRE_IDL_H_
11637 /* Everything we need here should be included by lustreapi.h. */
11638 # error "lfs should not depend on lustre_idl.h"
11639 #endif /* _LUSTRE_IDL_H_ */