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 {
150 static int lfs_setstripe_internal(int argc, char **argv,
151 enum setstripe_origin opc);
153 static inline int lfs_setstripe(int argc, char **argv)
155 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
157 static inline int lfs_setstripe_migrate(int argc, char **argv)
159 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
161 static inline int lfs_mirror_create(int argc, char **argv)
163 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
165 static inline int lfs_mirror_extend(int argc, char **argv)
167 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
169 static inline int lfs_mirror_split(int argc, char **argv)
171 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
173 static inline int lfs_mirror_delete(int argc, char **argv)
175 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
178 /* Setstripe and migrate share mostly the same parameters */
179 #define SSM_CMD_COMMON(cmd) \
180 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
181 " [--stripe-count|-c <stripe_count>]\n" \
182 " [--overstripe-count|-C <stripe_count>]\n" \
183 " [--stripe-index|-i <start_ost_idx>]\n" \
184 " [--stripe-size|-S <stripe_size>]\n" \
185 " [--extension-size|--ext-size|-z]\n" \
186 " [--layout|-L <pattern>]\n" \
187 " [--mirror_count|-N[mirror_count]]\n" \
188 " [--ost|-o <ost_indices>]\n" \
189 " [--pool|-p <pool_name>]\n" \
190 " [--yaml|-y <yaml_template_file>]\n" \
191 " [--copy=<lustre_src>]\n"
193 #define SSM_HELP_COMMON \
194 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
195 "\t Using -C instead of -c allows overstriping, which\n" \
196 "\t will place more than one stripe per OST if\n" \
197 "\t stripe_count is greater than the number of OSTs\n" \
198 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
199 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
200 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
201 "\t respectively)\n" \
202 "\textension_size:\n" \
203 "\t Number of bytes the previous component is extended\n" \
204 "\t each time. Can be specified with K, M, G (for KB,\n" \
205 "\t MB, GB respectively)\n" \
206 "\tpool_name: Name of OST pool to use (default none)\n" \
207 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
208 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
209 "\t Indices be specified in a format of:\n" \
210 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
212 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
213 "\t If --pool is set with --ost then the OSTs\n" \
214 "\t must be the members of the pool.\n" \
215 "\tcomp_end: Extent end of component, start after previous end.\n"\
216 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
217 "\t respectively, -1 for EOF). Must be a multiple of\n"\
218 "\t stripe_size.\n" \
219 "\tyaml_template_file:\n" \
220 "\t YAML layout template file, can't be used with -c,\n" \
221 "\t -i, -S, -p, -o, or -E arguments.\n" \
222 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
223 "\t another lustre file or directory, can't used with\n" \
224 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
226 #define MIRROR_CREATE_HELP \
227 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
228 "\t setstripe layout options\n" \
229 "\t It defaults to 1 if not specified; if specified,\n" \
230 "\t it must follow the option without a space.\n" \
231 "\t The option can also be repeated multiple times to\n" \
232 "\t separate mirrors that have different layouts.\n" \
233 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
234 "\t It can be a plain layout or a composite layout.\n" \
235 "\t If not specified, the stripe options inherited\n" \
236 "\t from the previous component will be used.\n" \
237 "\tflags: set flags to the component of the current mirror.\n" \
238 "\t Only \"prefer\" flag is supported so far.\n"
240 #define MIRROR_EXTEND_HELP \
242 "\tvictim_file: The layout of victim_file will be split and used\n" \
243 "\t as a mirror added to the mirrored file.\n" \
244 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
245 "\t from victim file(s) in case the victim file(s)\n" \
246 "\t contains the same data as the original mirrored\n" \
249 #define MIRROR_EXTEND_USAGE \
250 " {--mirror-count|-N[mirror_count]}\n" \
251 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
254 #define SETSTRIPE_USAGE \
255 SSM_CMD_COMMON("setstripe") \
256 MIRROR_EXTEND_USAGE \
257 " <directory|filename>\n" \
261 #define MIGRATE_USAGE \
262 SSM_CMD_COMMON("migrate ") \
263 " [--block|-b] [--non-block|-n]\n" \
264 " [--non-direct|-D] [--verbose|-v]\n" \
268 "\tblock: Block file access during data migration (default)\n" \
269 "\tnon-block: Abort migrations if concurrent access is detected\n" \
270 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
271 "\tverbose: Print each filename as it is migrated\n" \
273 #define SETDIRSTRIPE_USAGE \
274 " [--mdt-count|-c stripe_count>\n" \
275 " [--mdt-hash|-H mdt_hash]\n" \
276 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
277 " [--default|-D] [--mode|-o mode] <dir>\n" \
278 "\tstripe_count: stripe count of the striped directory\n" \
279 "\tmdt_index: MDT index of first stripe\n" \
280 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
281 " crush CRUSH hash algorithm (default)\n" \
282 " fnv_1a_64 FNV-1a hash algorithm\n" \
283 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
284 "\tdefault_stripe: set default dirstripe of the directory\n" \
285 "\tmode: the file access permission of the directory (octal)\n" \
286 "To create dir with a foreign (free format) layout :\n" \
287 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
288 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
289 "\tmode: the mode of the directory\n" \
290 "\tforeign_type: none or daos\n"
293 * command_t mirror_cmdlist - lfs mirror commands.
295 command_t mirror_cmdlist[] = {
296 { .pc_name = "create", .pc_func = lfs_mirror_create,
297 .pc_help = "Create a mirrored file.\n"
298 "usage: lfs mirror create "
299 "<--mirror-count|-N[mirror_count]> "
300 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
301 MIRROR_CREATE_HELP },
302 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
303 .pc_help = "Delete a mirror from a file.\n"
304 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
305 "\t --component-id|--comp-id|-I <comp_id> |\n"
306 "\t -p <pool>} <mirrored_file> ...\n"
308 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
309 .pc_help = "Extend a mirrored file.\n"
310 "usage: lfs mirror extend "
311 "<--mirror-count|-N[mirror_count]> [--no-verify] "
312 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
313 MIRROR_EXTEND_HELP },
314 { .pc_name = "split", .pc_func = lfs_mirror_split,
315 .pc_help = "Split a mirrored file.\n"
316 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
317 "\t --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
318 "\t [-f <new_file>] <mirrored_file> ...\n"
319 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
320 "\t can be fetched by lfs getstripe command.\n"
321 "\tcomp_id: Unique component ID within a mirror.\n"
322 "\tpool: Components on specified pool.\n"
323 "\tnew_file: This option indicates the layout of the split\n"
324 "\t mirror will be stored into. If not specified,\n"
325 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
326 "\t will be used.\n" },
327 { .pc_name = "read", .pc_func = lfs_mirror_read,
328 .pc_help = "Read the content of a specified mirror of a file.\n"
329 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
330 "[--outfile|-o <output_file>] <mirrored_file>\n" },
331 { .pc_name = "write", .pc_func = lfs_mirror_write,
332 .pc_help = "Write to a specified mirror of a file.\n"
333 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
334 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
335 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
336 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
337 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
338 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
339 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
340 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
341 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
342 "<mirrored file> [<mirrored file2>...]\n"},
343 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
344 .pc_help = "Verify mirrored file(s).\n"
345 "usage: lfs mirror verify "
346 "[--only <mirror_id,mirror_id2[,...]>] "
347 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
348 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
349 .pc_help = "list commands supported by lfs mirror"},
350 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
351 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
352 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
357 * command_t pcc_cmdlist - lfs pcc commands.
359 command_t pcc_cmdlist[] = {
360 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
361 .pc_help = "Attach given files to the Persistent Client Cache.\n"
362 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
363 "\t-i: archive id for RW-PCC\n" },
364 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
365 .pc_help = "Attach given files into PCC by FID(s).\n"
366 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
368 "\t-i: archive id for RW-PCC\n"
369 "\t-m: Lustre mount point\n" },
370 { .pc_name = "state", .pc_func = lfs_pcc_state,
371 .pc_help = "Display the PCC state for given files.\n"
372 "usage: lfs pcc state <file> ...\n" },
373 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
374 .pc_help = "Detach given files from the Persistent Client Cache.\n"
375 "usage: lfs pcc detach <file> ...\n" },
376 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
377 .pc_help = "Detach given files from PCC by FID(s).\n"
378 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
379 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
380 .pc_help = "list commands supported by lfs pcc"},
381 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
382 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
383 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
387 /* all available commands */
388 command_t cmdlist[] = {
389 {"setstripe", lfs_setstripe, 0,
390 "To create a file with specified striping/composite layout, or\n"
391 "create/replace the default layout on an existing directory:\n"
392 SSM_CMD_COMMON("setstripe")
394 " <directory|filename>\n"
396 "To add component(s) to an existing composite file:\n"
397 SSM_CMD_COMMON("setstripe --component-add")
399 "To totally delete the default striping from an existing directory:\n"
400 "usage: setstripe [--delete|-d] <directory>\n"
402 "To create a mirrored file or set s default mirror layout on a directory:\n"
403 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
405 "To delete the last component(s) from an existing composite file\n"
406 "(note that this will also delete any data in those components):\n"
407 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
408 " [--component-flags|-F <comp_flags>]\n"
410 "\tcomp_id: Unique component ID to delete\n"
411 "\tcomp_flags: 'init' indicating all instantiated components\n"
412 "\t '^init' indicating all uninstantiated components\n"
413 "\t-I and -F cannot be specified at the same time\n"
414 "To create a file with a foreign (free format) layout:\n"
415 "usage: setstripe --foreign[=<foreign_type>]\n"
416 " --xattr|-x <layout_string> [--flags <hex>]\n"
417 " [--mode <mode>] <filename>\n"},
418 {"getstripe", lfs_getstripe, 0,
419 "To list the layout pattern for a given file or files in a\n"
420 "directory or recursively for all files in a directory tree.\n"
421 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
422 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
423 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
424 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
425 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
426 " [--component-id[=comp_id]|-I[comp_id]]\n"
427 " [--component-flags[=comp_flags]]\n"
428 " [--component-count]\n"
429 " [--extension-size|--ext-size|-z]\n"
430 " [--component-start[=[+-]comp_start]]\n"
431 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
432 " [[!] --mirror-index=[+-]<index> |\n"
433 " [!] --mirror-id=[+-]<id>]\n"
434 " <directory|filename> ..."},
435 {"setdirstripe", lfs_setdirstripe, 0,
436 "Create striped directory on specified MDT, same as mkdir.\n"
437 "May be restricted to root or group users, depending on settings.\n"
438 "usage: setdirstripe [OPTION] <directory>\n"
440 {"getdirstripe", lfs_getdirstripe, 0,
441 "To list the layout pattern info for a given directory\n"
442 "or recursively for all directories in a directory tree.\n"
443 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
444 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
445 " [--recursive|-r] [--yaml|-y]\n"
446 " [--verbose|-v] [--default|-D] <dir> ..."},
447 {"mkdir", lfs_setdirstripe, 0,
448 "Create striped directory on specified MDT, same as setdirstripe.\n"
449 "usage: mkdir [OPTION] <directory>\n"
451 {"rm_entry", lfs_rmentry, 0,
452 "To remove the name entry of the remote directory. Note: This\n"
453 "command will only delete the name entry, i.e. the remote directory\n"
454 "will become inaccessable after this command. This can only be done\n"
455 "by the administrator\n"
456 "usage: rm_entry <dir>\n"},
457 {"pool_list", lfs_poollist, 0,
458 "List pools or pool OSTs\n"
459 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
460 {"find", lfs_find, 0,
461 "find files matching given attributes recursively in directory tree.\n"
462 "usage: find <directory|filename> ...\n"
463 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
464 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
465 " [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
466 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
467 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
468 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
469 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
470 " [[!] --stripe-count|-c [+-]<stripes>]\n"
471 " [[!] --stripe-index|-i <index,...>]\n"
472 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
473 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
474 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
475 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
476 " [[!] --projid <projid>]\n"
477 " [[!] --foreign[=<foreign_type>]]\n"
478 " [[!] --layout|-L released,raid0,mdt]\n"
479 " [[!] --foreign[=<foreign_type>]]\n"
480 " [[!] --component-count [+-]<comp_cnt>]\n"
481 " [[!] --component-start [+-]N[kMGTPE]]\n"
482 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
483 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
484 " [[!] --mirror-count|-N [+-]<n>]\n"
485 " [[!] --mirror-state <[^]state>]\n"
486 " [[!] --mdt-count|-T [+-]<stripes>]\n"
487 " [[!] --mdt-hash|-H <hashtype>\n"
488 " [[!] --mdt-index|-m <uuid|index,...>]\n"
489 "\t !: used before an option indicates 'NOT' requested attribute\n"
490 "\t -: used before a value indicates less than requested value\n"
491 "\t +: used before a value indicates more than requested value\n"
492 "\thashtype: hash type of the striped directory.\n"
493 "\t fnv_1a_64 FNV-1a hash algorithm\n"
494 "\t all_char sum of characters % MDT_COUNT\n"},
495 {"check", lfs_check, 0,
496 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
497 "or all the servers (MGTs, MDTs and OSTs).\n"
498 "usage: check <mgts|osts|mdts|all>"},
499 {"osts", lfs_osts, 0, "list OSTs connected to client "
500 "[for specified path only]\n" "usage: osts [path]"},
501 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
502 "[for specified path only]\n" "usage: mdts [path]"},
504 "report filesystem disk space usage or inodes usage "
505 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
506 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
507 " [--pool|-p <fsname>[.<pool>]] [path]"},
508 {"getname", lfs_getname, 0,
509 "list instances and specified mount points [for specified path only]\n"
510 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
511 #ifdef HAVE_SYS_QUOTA_H
512 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
513 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
514 " -b <block-softlimit> -B <block-hardlimit>\n"
515 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
516 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
517 " [--block-softlimit <block-softlimit>]\n"
518 " [--block-hardlimit <block-hardlimit>]\n"
519 " [--inode-softlimit <inode-softlimit>]\n"
520 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
521 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
522 " [--block-grace 'notify'|<block-grace>]\n"
523 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
524 " setquota <-U|-G|-P>\n"
525 " -b <block-softlimit> -B <block-hardlimit>\n"
526 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
527 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
528 " [--block-softlimit <block-softlimit>]\n"
529 " [--block-hardlimit <block-hardlimit>]\n"
530 " [--inode-softlimit <inode-softlimit>]\n"
531 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
532 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
534 " -b can be used instead of --block-softlimit/--block-grace\n"
535 " -B can be used instead of --block-hardlimit\n"
536 " -i can be used instead of --inode-softlimit/--inode-grace\n"
537 " -I can be used instead of --inode-hardlimit\n"
538 " -d can be used instead of --default\n\n"
539 "Note: The total quota space will be split into many qunits and\n"
540 " balanced over all server targets, the minimal qunit size is\n"
541 " 1M bytes for block space and 1K inodes for inode space.\n\n"
542 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
543 " Quota space rebalancing process will stop when this mininum\n"
544 " value is reached. As a result, quota exceeded can be returned\n"
545 " while many targets still have 1MB or 1K inodes of spare\n"
547 " When setting the grace time, 'notify' can be used as grace to\n"
548 " be notified after the quota is over soft limit but prevents\n"
549 " the soft limit from becoming the hard limit."},
550 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
551 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
553 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
554 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
555 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
556 {"project", lfs_project, 0,
557 "Change or list project attribute for specified file or directory.\n"
558 "usage: project [-d|-r] <file|directory...>\n"
559 " list project ID and flags on file(s) or directories\n"
560 " project [-p id] [-s] [-r] <file|directory...>\n"
561 " set project ID and/or inherit flag for specified file(s) or directories\n"
562 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
563 " check project ID and flags on file(s) or directories, print outliers\n"
564 " project -C [-r] [-k] <file|directory...>\n"
565 " clear the project inherit flag and ID on the file or directory\n"
568 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
569 "usage: flushctx [-k] [mountpoint...]"},
570 {"changelog", lfs_changelog, 0,
571 "Show the metadata changes on an MDT."
572 "\nusage: changelog <mdtname> [startrec [endrec]]"},
573 {"changelog_clear", lfs_changelog_clear, 0,
574 "Indicate that old changelog records up to <endrec> are no longer of "
575 "interest to consumer <id>, allowing the system to free up space.\n"
576 "An <endrec> of 0 means all records.\n"
577 "usage: changelog_clear <mdtname> <id> <endrec>"},
578 {"fid2path", lfs_fid2path, 0,
579 "Resolve the full path(s) for given FID(s). For a specific hardlink "
580 "specify link number <linkno>.\n"
581 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
582 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
583 "usage: path2fid [--parents] <path> ..."},
584 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
585 "usage: rmfid <fsname|rootpath> <fid> ..."},
586 {"data_version", lfs_data_version, 0, "Display file data version for "
587 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
588 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
589 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
590 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
591 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
592 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
593 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
595 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
596 "[--archived] [--lost] <file> ..."},
597 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
598 "given files.\n" "usage: hsm_action <file> ..."},
599 {"hsm_archive", lfs_hsm_archive, 0,
600 "Archive file to external storage.\n"
601 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
603 {"hsm_restore", lfs_hsm_restore, 0,
604 "Restore file from external storage.\n"
605 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
606 {"hsm_release", lfs_hsm_release, 0,
607 "Release files from Lustre.\n"
608 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
609 {"hsm_remove", lfs_hsm_remove, 0,
610 "Remove file copy from external storage.\n"
611 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
613 " (FILE [FILE ...] | "
614 "--mntpath MOUNTPATH FID [FID ...])\n"
616 "Note: To remove an archived copy of a file already deleted from a "
618 "--mntpath option and a list of FIDs must be specified"
620 {"hsm_cancel", lfs_hsm_cancel, 0,
621 "Cancel requests related to specified files.\n"
622 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
623 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
624 "usage: swap_layouts <path1> <path2>"},
625 {"migrate", lfs_setstripe_migrate, 0,
626 "migrate a directory between MDTs.\n"
627 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
628 " [--mdt-hash|-H] <hash_type>\n"
629 " [--mdt-index|-m] <start_mdt_index>\n"
632 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
633 " it's the MDT index of first stripe\n"
634 "\tmdt_count: number of MDTs to stripe a directory over\n"
635 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
636 " all_char (type 1)sum of characters % MDT_COUNT\n"
637 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
638 " crush (type 3)CRUSH hash algorithm\n"
640 "migrate file objects from one OST "
641 "layout\nto another (may be not safe with concurent writes).\n"
643 "[--stripe-count|-c] <stripe_count>\n"
644 "[--overstripe-count|-C] <stripe_count>\n"
645 " [--stripe-index|-i] <start_ost_index>\n"
646 " [--stripe-size|-S] <stripe_size>\n"
647 " [--pool|-p] <pool_name>\n"
648 " [--ost|-o] <ost_indices>\n"
650 " [--non-block|-n]\n"
651 " [--non-direct|-D]\n"
652 " <file|directory>\n"
653 "\tstripe_count: number of OSTs to stripe a file over\n"
654 "\t Using -C instead of -c allows overstriping, which\n"
655 "\t will place more than one stripe per OST if\n"
656 "\t stripe_count is greater than the number of OSTs\n"
657 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
658 "\tstripe_size: number of bytes to store before moving to the next OST\n"
659 "\tpool_name: name of the predefined pool of OSTs\n"
660 "\tost_indices: OSTs to stripe over, in order\n"
661 "\tblock: Block file access during data migration (default)\n"
662 "\tnon-block: Abort migrations if concurrent access is detected\n"
663 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
665 "To move directories between MDTs. This command is deprecated, "
666 "use \"migrate\" instead.\n"
667 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
669 {"ladvise", lfs_ladvise, 0,
670 "Provide servers with advice about access patterns for a file.\n"
671 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
672 " [--background|-b] [--unset|-u]\n\n"
673 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
674 " {[--mode|-m [READ,WRITE]}\n"
676 {"mirror", lfs_mirror, mirror_cmdlist,
677 "lfs commands used to manage files with mirrored components:\n"
678 "lfs mirror create - create a mirrored file or directory\n"
679 "lfs mirror extend - add mirror(s) to an existing file\n"
680 "lfs mirror split - split a mirror from an existing mirrored file\n"
681 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
682 "lfs mirror read - read a mirror content of a mirrored file\n"
683 "lfs mirror write - write to a mirror of a mirrored file\n"
684 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
685 "lfs mirror verify - verify mirrored file(s)\n"},
686 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
687 "usage: getsom [-s] [-b] [-f] <path>\n"
688 "\t-s: Only show the size value of the SOM data for a given file\n"
689 "\t-b: Only show the blocks value of the SOM data for a given file\n"
690 "\t-f: Only show the flags value of the SOM data for a given file\n"},
691 {"heat_get", lfs_heat_get, 0,
692 "To get heat of files.\n"
693 "usage: heat_get <file> ...\n"},
694 {"heat_set", lfs_heat_set, 0,
695 "To set heat flags of files.\n"
696 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
697 "\t--clear|-c: Clear file heat for given files\n"
698 "\t--off|-o: Turn off file heat for given files\n"
699 "\t--on|-O: Turn on file heat for given files\n"},
700 {"pcc", lfs_pcc, pcc_cmdlist,
701 "lfs commands used to interact with PCC features:\n"
702 "lfs pcc attach - attach given files to Persistent Client Cache\n"
703 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
704 "lfs pcc state - display the PCC state for given files\n"
705 "lfs pcc detach - detach given files from Persistent Client Cache\n"
706 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
707 {"help", Parser_help, 0, "help"},
708 {"exit", Parser_quit, 0, "quit"},
709 {"quit", Parser_quit, 0, "quit"},
710 {"--version", Parser_version, 0,
711 "output build version of the utility and exit"},
712 {"--list-commands", lfs_list_commands, 0,
713 "list commands supported by the utility and exit"},
718 static int check_hashtype(const char *hashtype)
720 int type_num = atoi(hashtype);
723 /* numeric hash type */
724 if (hashtype && strlen(hashtype) == 1 &&
725 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
727 /* string hash type */
728 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
729 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
735 static uint32_t check_foreign_type_name(const char *foreign_type_name)
739 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
740 if (lu_foreign_types[i].lft_name == NULL)
742 if (strcmp(foreign_type_name,
743 lu_foreign_types[i].lft_name) == 0)
744 return lu_foreign_types[i].lft_type;
747 return LU_FOREIGN_TYPE_UNKNOWN;
750 static const char *error_loc = "syserror";
753 MIGRATION_NONBLOCK = 0x0001,
754 MIGRATION_MIRROR = 0x0002,
755 MIGRATION_NONDIRECT = 0x0004,
756 MIGRATION_VERBOSE = 0x0008,
759 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
760 struct llapi_layout *layout);
763 migrate_open_files(const char *name, __u64 migration_flags,
764 const struct llapi_stripe_param *param,
765 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
772 char parent[PATH_MAX];
773 char volatile_file[PATH_MAX];
779 if (param == NULL && layout == NULL) {
780 error_loc = "layout information";
784 /* search for file directory pathname */
785 if (strlen(name) > sizeof(parent) - 1) {
786 error_loc = "source file name";
790 strncpy(parent, name, sizeof(parent));
791 ptr = strrchr(parent, '/');
793 if (getcwd(parent, sizeof(parent)) == NULL) {
794 error_loc = "getcwd";
798 if (ptr == parent) /* leading '/' */
803 /* open file, direct io */
804 /* even if the file is only read, WR mode is nedeed to allow
805 * layout swap on fd */
807 if (!(migration_flags & MIGRATION_NONDIRECT))
809 fd = open(name, rflags);
812 error_loc = "cannot open source file";
816 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
818 error_loc = "cannot get MDT index";
823 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
824 mode_t open_mode = S_IRUSR | S_IWUSR;
826 random_value = random();
827 rc = snprintf(volatile_file, sizeof(volatile_file),
828 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
829 mdt_index, random_value);
830 if (rc >= sizeof(volatile_file)) {
835 /* create, open a volatile file, use caching (ie no directio) */
837 fdv = lfs_component_create(volatile_file, open_flags,
840 fdv = llapi_file_open_param(volatile_file, open_flags,
842 } while (fdv < 0 && (rc = fdv) == -EEXIST);
845 error_loc = "cannot create volatile file";
849 /* In case the MDT does not support creation of volatile files
850 * we should try to unlink it. */
851 (void)unlink(volatile_file);
853 /* Not-owner (root?) special case.
854 * Need to set owner/group of volatile file like original.
855 * This will allow to pass related check during layout_swap.
860 error_loc = "cannot stat source file";
864 rc = fstat(fdv, &stv);
867 error_loc = "cannot stat volatile";
871 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
872 rc = fchown(fdv, st.st_uid, st.st_gid);
875 error_loc = "cannot change ownwership of volatile";
894 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
896 struct llapi_layout *layout;
897 size_t buf_size = 4 * 1024 * 1024;
906 layout = llapi_layout_get_by_fd(fd_src, 0);
907 if (layout != NULL) {
908 uint64_t stripe_size;
910 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
912 buf_size = stripe_size;
914 llapi_layout_free(layout);
917 /* Use a page-aligned buffer for direct I/O */
918 rc = posix_memalign(&buf, getpagesize(), buf_size);
923 /* read new data only if we have written all
924 * previously read data */
927 rc = check_file(fd_src);
932 rsize = read(fd_src, buf, buf_size);
944 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
963 static int migrate_copy_timestamps(int fd, int fdv)
967 if (fstat(fd, &st) == 0) {
968 struct timeval tv[2] = {
969 {.tv_sec = st.st_atime},
970 {.tv_sec = st.st_mtime}
973 return futimes(fdv, tv);
979 static int migrate_block(int fd, int fdv)
986 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
988 error_loc = "cannot get dataversion";
996 /* The grouplock blocks all concurrent accesses to the file.
997 * It has to be taken after llapi_get_data_version as it would
999 rc = llapi_group_lock(fd, gid);
1001 error_loc = "cannot get group lock";
1005 rc = migrate_copy_data(fd, fdv, NULL);
1007 error_loc = "data copy failed";
1011 /* Make sure we keep original atime/mtime values */
1012 rc = migrate_copy_timestamps(fd, fdv);
1014 error_loc = "timestamp copy failed";
1019 * for a migration we need to check data version on file did
1022 * Pass in gid=0 since we already own grouplock. */
1023 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1024 SWAP_LAYOUTS_CHECK_DV1);
1025 if (rc == -EAGAIN) {
1026 error_loc = "file changed";
1028 } else if (rc < 0) {
1029 error_loc = "cannot swap layout";
1034 rc2 = llapi_group_unlock(fd, gid);
1035 if (rc2 < 0 && rc == 0) {
1036 error_loc = "unlock group lock";
1044 * Internal helper for migrate_copy_data(). Check lease and report error if
1047 * \param[in] fd File descriptor on which to check the lease.
1049 * \retval 0 Migration can keep on going.
1050 * \retval -errno Error occurred, abort migration.
1052 static int check_lease(int fd)
1056 rc = llapi_lease_check(fd);
1058 return 0; /* llapi_check_lease returns > 0 on success. */
1063 static int migrate_nonblock(int fd, int fdv)
1069 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1071 error_loc = "cannot get data version";
1075 rc = migrate_copy_data(fd, fdv, check_lease);
1077 error_loc = "data copy failed";
1081 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1083 error_loc = "cannot get data version";
1089 error_loc = "source file changed";
1093 /* Make sure we keep original atime/mtime values */
1094 rc = migrate_copy_timestamps(fd, fdv);
1096 error_loc = "timestamp copy failed";
1103 static int lfs_component_set(char *fname, int comp_id,
1104 __u32 flags, __u32 neg_flags)
1107 __u32 flags_array[2];
1112 ids[count] = comp_id;
1113 flags_array[count] = flags;
1118 if (neg_flags & LCME_FL_STALE) {
1119 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1120 "component. Please use lfs-mirror-resync(1) "
1121 "instead\n", progname);
1125 ids[count] = comp_id;
1126 flags_array[count] = neg_flags | LCME_FL_NEG;
1130 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1132 if (errno == EUCLEAN) {
1135 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1136 progname, comp_id, fname);
1139 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1140 progname, comp_id, fname, flags, neg_flags);
1147 static int lfs_component_del(char *fname, __u32 comp_id,
1148 __u32 flags, __u32 neg_flags)
1152 if (flags && neg_flags)
1155 if (!flags && neg_flags)
1156 flags = neg_flags | LCME_FL_NEG;
1158 if ((flags && comp_id) || (!flags && !comp_id))
1162 if (flags & ~LCME_KNOWN_FLAGS) {
1164 "%s setstripe: unknown flags %#x\n",
1168 } else if (comp_id > LCME_ID_MAX) {
1169 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1174 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1177 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1178 progname, comp_id, fname, strerror(errno));
1182 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1189 rc = llapi_layout_file_comp_add(fname, layout);
1191 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1192 fname, strerror(errno));
1196 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1197 struct llapi_layout *layout)
1205 fd = lstat(fname, &st);
1206 if (fd == 0 && S_ISDIR(st.st_mode))
1207 open_flags = O_DIRECTORY | O_RDONLY;
1209 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1211 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1212 S_ISDIR(st.st_mode) ?
1213 "set default composite layout for" :
1214 "create composite file",
1215 fname, strerror(errno));
1219 static int lfs_migrate(char *name, __u64 migration_flags,
1220 struct llapi_stripe_param *param,
1221 struct llapi_layout *layout)
1223 struct llapi_layout *existing;
1224 uint64_t dom_new, dom_cur;
1229 rc = migrate_open_files(name, migration_flags, param, layout,
1234 rc = llapi_layout_dom_size(layout, &dom_new);
1236 error_loc = "cannot get new layout DoM size";
1239 /* special case for migration to DOM layout*/
1240 existing = llapi_layout_get_by_fd(fd, 0);
1242 error_loc = "cannot get existing layout";
1246 rc = llapi_layout_dom_size(existing, &dom_cur);
1248 error_loc = "cannot get current layout DoM size";
1252 /* if file has DoM layout already then migration is possible to
1253 * the new layout with the same DoM component via swap layout,
1254 * if new layout used bigger DOM size, then mirroring is used
1256 if (dom_new > dom_cur) {
1257 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1260 error_loc = "cannot migrate to DOM layout";
1264 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1265 /* Blocking mode (forced if servers do not support file lease).
1266 * It is also the default mode, since we cannot distinguish
1267 * between a broken lease and a server that does not support
1268 * atomic swap/close (LU-6785) */
1269 rc = migrate_block(fd, fdv);
1273 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1275 error_loc = "cannot get lease";
1279 rc = migrate_nonblock(fd, fdv);
1281 llapi_lease_release(fd);
1285 /* Atomically put lease, swap layouts and close.
1286 * for a migration we need to check data version on file did
1288 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1290 error_loc = "cannot swap layout";
1302 fprintf(stderr, "error: %s: %s: %s: %s\n",
1303 progname, name, error_loc, strerror(-rc));
1304 else if (migration_flags & MIGRATION_VERBOSE)
1305 printf("%s\n", name);
1310 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1319 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1323 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1324 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1325 const char *comp_name = comp_flags_table[i].cfn_name;
1327 if (strcmp(name, comp_name) == 0) {
1328 *flags |= comp_flag;
1330 } else if (strncmp(name, "^", 1) == 0 &&
1331 strcmp(name + 1, comp_name) == 0) {
1332 *neg_flags |= comp_flag;
1337 llapi_printf(LLAPI_MSG_ERROR,
1338 "%s: component flag '%s' not supported\n",
1344 if (!*flags && !*neg_flags)
1347 /* don't allow to set and exclude the same flag */
1348 if (*flags & *neg_flags)
1354 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1362 if (strncmp(string, "^", 1) == 0) {
1363 *neg_state = llapi_layout_string_flags(string + 1);
1364 if (*neg_state != 0)
1367 *state = llapi_layout_string_flags(string);
1372 llapi_printf(LLAPI_MSG_ERROR,
1373 "%s: mirrored file state '%s' not supported\n",
1379 * struct mirror_args - Command-line arguments for mirror(s).
1380 * @m_count: Number of mirrors to be created with this layout.
1381 * @m_flags: Mirror level flags, only 'prefer' is supported.
1382 * @m_layout: Mirror layout.
1383 * @m_file: A victim file. Its layout will be split and used as a mirror.
1384 * @m_next: Point to the next node of the list.
1386 * Command-line arguments for mirror(s) will be parsed and stored in
1387 * a linked list that consists of this structure.
1389 struct mirror_args {
1392 struct llapi_layout *m_layout;
1394 struct mirror_args *m_next;
1399 * enum mirror_flags - Flags for extending a mirrored file.
1400 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1401 * in case the victim file(s) contains the same data as the
1402 * original mirrored file.
1403 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1404 * @MF_COMP_ID: specified component id instead of mirror id
1406 * Flags for extending a mirrored file.
1416 * mirror_create_sanity_check() - Check mirror list.
1417 * @list: A linked list that stores the mirror arguments.
1419 * This function does a sanity check on @list for creating
1422 * Return: 0 on success or a negative error code on failure.
1424 static int mirror_create_sanity_check(const char *fname,
1425 struct mirror_args *list)
1428 bool has_m_file = false;
1429 bool has_m_layout = false;
1435 struct llapi_layout *layout;
1437 layout = llapi_layout_get_by_path(fname, 0);
1440 "error: %s: file '%s' couldn't get layout\n",
1445 rc = llapi_layout_sanity(layout, false, true);
1447 llapi_layout_free(layout);
1450 llapi_layout_sanity_perror(rc);
1455 while (list != NULL) {
1456 if (list->m_file != NULL) {
1458 llapi_layout_free(list->m_layout);
1461 llapi_layout_get_by_path(list->m_file, 0);
1462 if (list->m_layout == NULL) {
1464 "error: %s: file '%s' has no layout\n",
1465 progname, list->m_file);
1469 has_m_layout = true;
1470 if (list->m_layout == NULL) {
1471 fprintf(stderr, "error: %s: no mirror layout\n",
1477 rc = llapi_layout_sanity(list->m_layout, false, true);
1479 llapi_layout_sanity_perror(rc);
1483 list = list->m_next;
1486 if (has_m_file && has_m_layout) {
1488 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1496 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1498 __u32 mirror_flags = *(__u32 *)cbdata;
1502 rc = llapi_layout_comp_flags_get(layout, &flags);
1507 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1512 return LLAPI_LAYOUT_ITER_CONT;
1516 * mirror_create() - Create a mirrored file.
1517 * @fname: The file to be created.
1518 * @mirror_list: A linked list that stores the mirror arguments.
1520 * This function creates a mirrored file @fname with the mirror(s)
1521 * from @mirror_list.
1523 * Return: 0 on success or a negative error code on failure.
1525 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1527 struct llapi_layout *layout = NULL;
1528 struct mirror_args *cur_mirror = NULL;
1529 uint16_t mirror_count = 0;
1533 rc = mirror_create_sanity_check(NULL, mirror_list);
1537 cur_mirror = mirror_list;
1538 while (cur_mirror != NULL) {
1539 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1541 &cur_mirror->m_flags);
1544 fprintf(stderr, "%s: failed to set mirror flags\n",
1549 for (i = 0; i < cur_mirror->m_count; i++) {
1550 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1553 fprintf(stderr, "error: %s: "
1554 "merge layout failed: %s\n",
1555 progname, strerror(errno));
1559 mirror_count += cur_mirror->m_count;
1560 cur_mirror = cur_mirror->m_next;
1563 if (layout == NULL) {
1564 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1568 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1571 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1572 progname, strerror(errno));
1576 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1584 llapi_layout_free(layout);
1589 * Compare files and check lease on @fd.
1591 * \retval bytes number of bytes are the same
1593 static ssize_t mirror_file_compare(int fd, int fdv)
1595 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1597 ssize_t bytes_done = 0;
1598 ssize_t bytes_read = 0;
1600 buf = malloc(buflen * 2);
1605 if (!llapi_lease_check(fd)) {
1606 bytes_done = -EBUSY;
1610 bytes_read = read(fd, buf, buflen);
1611 if (bytes_read <= 0)
1614 if (bytes_read != read(fdv, buf + buflen, buflen))
1617 /* XXX: should compute the checksum on each buffer and then
1618 * compare checksum to avoid cache collision */
1619 if (memcmp(buf, buf + buflen, bytes_read))
1622 bytes_done += bytes_read;
1630 static int mirror_extend_file(const char *fname, const char *victim_file,
1631 enum mirror_flags mirror_flags)
1636 struct stat stbuf_v;
1637 struct ll_ioc_lease *data = NULL;
1640 fd = open(fname, O_RDWR);
1642 error_loc = "open source file";
1647 fdv = open(victim_file, O_RDWR);
1649 error_loc = "open target file";
1654 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1655 error_loc = "stat source or target file";
1660 if (stbuf.st_dev != stbuf_v.st_dev) {
1661 error_loc = "stat source and target file";
1666 /* mirrors should be of the same size */
1667 if (stbuf.st_size != stbuf_v.st_size) {
1668 error_loc = "file sizes don't match";
1673 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1675 error_loc = "cannot get lease";
1679 if (!(mirror_flags & MF_NO_VERIFY)) {
1681 /* mirrors should have the same contents */
1682 ret = mirror_file_compare(fd, fdv);
1683 if (ret != stbuf.st_size) {
1684 error_loc = "file busy or contents don't match";
1685 rc = ret < 0 ? ret : -EINVAL;
1690 /* Get rid of caching pages from clients */
1691 rc = llapi_file_flush(fd);
1693 error_loc = "cannot get data version";
1697 rc = llapi_file_flush(fdv);
1699 error_loc = "cannot get data version";
1704 /* Make sure we keep original atime/mtime values */
1705 rc = migrate_copy_timestamps(fd, fdv);
1707 error_loc = "cannot copy timestamp";
1711 /* Atomically put lease, merge layouts and close. */
1712 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1714 error_loc = "memory allocation";
1717 data->lil_mode = LL_LEASE_UNLCK;
1718 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1719 data->lil_count = 1;
1720 data->lil_ids[0] = fdv;
1721 rc = llapi_lease_set(fd, data);
1723 error_loc = "cannot merge layout";
1725 } else if (rc == 0) {
1727 error_loc = "lost lease lock";
1740 (void) unlink(victim_file);
1742 fprintf(stderr, "error: %s: %s: %s: %s\n",
1743 progname, fname, error_loc, strerror(-rc));
1747 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1750 struct llapi_layout *f_layout = NULL;
1751 struct ll_ioc_lease *data = NULL;
1757 f_layout = llapi_layout_get_by_path(name, 0);
1758 if (f_layout == NULL) {
1759 fprintf(stderr, "%s: cannot get layout\n", progname);
1762 rc = llapi_layout_get_last_init_comp(f_layout);
1764 fprintf(stderr, "%s: cannot get the last init comp\n",
1768 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1771 "%s: cannot inherit from the last init comp\n",
1776 rc = migrate_open_files(name, 0, NULL, m_layout, &fd, &fdv);
1780 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1782 error_loc = "cannot get lease";
1786 rc = migrate_nonblock(fd, fdv);
1788 llapi_lease_release(fd);
1792 /* Atomically put lease, merge layouts and close. */
1793 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1795 error_loc = "memory allocation";
1798 data->lil_mode = LL_LEASE_UNLCK;
1799 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1800 data->lil_count = 1;
1801 data->lil_ids[0] = fdv;
1802 rc = llapi_lease_set(fd, data);
1804 error_loc = "cannot merge layout";
1806 } else if (rc == 0) {
1808 error_loc = "lost lease lock";
1821 fprintf(stderr, "error: %s: %s: %s: %s\n",
1822 progname, name, error_loc, strerror(-rc));
1826 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1827 enum mirror_flags mirror_flags)
1831 rc = mirror_create_sanity_check(fname, mirror_list);
1835 while (mirror_list) {
1836 if (mirror_list->m_file != NULL) {
1837 rc = mirror_extend_file(fname, mirror_list->m_file,
1840 __u32 mirror_count = mirror_list->m_count;
1842 while (mirror_count > 0) {
1843 rc = mirror_extend_layout(fname,
1844 mirror_list->m_layout,
1845 mirror_list->m_inherit);
1855 mirror_list = mirror_list->m_next;
1861 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1866 rc = llapi_layout_mirror_id_get(layout, &id);
1870 if ((__u16)id == *(__u16 *)cbdata)
1871 return LLAPI_LAYOUT_ITER_STOP;
1873 return LLAPI_LAYOUT_ITER_CONT;
1876 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1881 rc = llapi_layout_comp_id_get(layout, &id);
1885 if (id == *(__u32 *)cbdata)
1886 return LLAPI_LAYOUT_ITER_STOP;
1888 return LLAPI_LAYOUT_ITER_CONT;
1891 struct pool_to_id_cbdata {
1895 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1897 char buf[LOV_MAXPOOLNAME + 1];
1898 struct pool_to_id_cbdata *d = (void *)cbdata;
1902 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1905 if (strcmp(d->pool, buf))
1906 return LLAPI_LAYOUT_ITER_CONT;
1908 rc = llapi_layout_mirror_id_get(layout, &id);
1913 return LLAPI_LAYOUT_ITER_STOP;
1916 struct collect_ids_data {
1922 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1924 struct collect_ids_data *cid = cbdata;
1928 rc = llapi_layout_mirror_id_get(layout, &id);
1932 if ((__u16)id != cid->cid_exclude) {
1935 for (i = 0; i < cid->cid_count; i++) {
1936 /* already collected the mirror id */
1937 if (id == cid->cid_ids[i])
1938 return LLAPI_LAYOUT_ITER_CONT;
1940 cid->cid_ids[cid->cid_count] = id;
1944 return LLAPI_LAYOUT_ITER_CONT;
1948 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1949 * @mirror_id: Mirror id to be checked.
1950 * @layout: Mirror component list.
1952 * This function checks if a mirror with specified @mirror_id is the last
1953 * non-stale mirror of a layout @layout.
1955 * Return: true or false.
1958 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1960 __u16 mirror_ids[128] = { 0 };
1961 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1963 .cid_exclude = mirror_id, };
1966 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1968 for (i = 0; i < cid.cid_count; i++) {
1969 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1972 comp_size = llapi_mirror_find_stale(layout, comp_array,
1973 ARRAY_SIZE(comp_array),
1982 static int mirror_split(const char *fname, __u32 id, const char *pool,
1983 enum mirror_flags mflags, const char *victim_file)
1985 struct llapi_layout *layout;
1986 char parent[PATH_MAX];
1987 char victim[PATH_MAX];
1988 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1990 struct ll_ioc_lease *data;
1991 uint16_t mirror_count;
1997 /* check fname contains mirror with mirror_id/comp_id */
1998 layout = llapi_layout_get_by_path(fname, 0);
2001 "error %s: file '%s' couldn't get layout\n",
2006 rc = llapi_layout_sanity(layout, false, true);
2008 llapi_layout_sanity_perror(rc);
2012 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2015 "error %s: file '%s' couldn't get mirror count\n",
2019 if (mirror_count < 2) {
2021 "error %s: file '%s' has %d component, cannot split\n",
2022 progname, fname, mirror_count);
2026 if (mflags & MF_COMP_POOL) {
2027 struct pool_to_id_cbdata data = { .pool = pool };
2029 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
2031 mirror_id = data.id;
2032 } else if (mflags & MF_COMP_ID) {
2033 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2034 mirror_id = mirror_id_of(id);
2036 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2040 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2043 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2044 if (mflags & MF_COMP_POOL) {
2046 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2047 progname, fname, pool);
2049 } else if (mflags & MF_COMP_ID) {
2051 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2052 progname, fname, id);
2056 "error %s: file '%s' does not contain mirror with id %u\n",
2057 progname, fname, id);
2062 fd = open(fname, O_RDWR);
2065 "error %s: open file '%s' failed: %s\n",
2066 progname, fname, strerror(errno));
2070 /* get victim file directory pathname */
2071 if (strlen(fname) > sizeof(parent) - 1) {
2072 fprintf(stderr, "error %s: file name of '%s' too long\n",
2077 strncpy(parent, fname, sizeof(parent));
2078 ptr = strrchr(parent, '/');
2080 if (getcwd(parent, sizeof(parent)) == NULL) {
2081 fprintf(stderr, "error %s: getcwd failed: %s\n",
2082 progname, strerror(errno));
2092 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2094 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2099 if (victim_file == NULL) {
2100 /* use a temp file to store the splitted layout */
2101 if (mflags & MF_DESTROY) {
2102 if (last_non_stale_mirror(mirror_id, layout)) {
2105 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2110 fdv = llapi_create_volatile_idx(parent, mdt_index,
2111 O_LOV_DELAY_CREATE);
2113 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2115 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2118 /* user specified victim file */
2119 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2124 "error %s: create victim file failed: %s\n",
2125 progname, strerror(errno));
2129 /* get lease lock of fname */
2130 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2133 "error %s: cannot get lease of file '%s': %d\n",
2134 progname, fname, rc);
2138 /* Atomatically put lease, split layouts and close. */
2139 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2145 data->lil_mode = LL_LEASE_UNLCK;
2146 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2147 data->lil_count = 2;
2148 data->lil_ids[0] = fdv;
2149 data->lil_ids[1] = mirror_id;
2150 rc = llapi_lease_set(fd, data);
2152 if (rc == 0) /* lost lease lock */
2155 "error %s: cannot split '%s': %s\n",
2156 progname, fname, strerror(-rc));
2167 llapi_layout_free(layout);
2172 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2173 __u16 *mirror_ids, int ids_nr);
2175 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2176 __u64 migration_flags,
2177 struct llapi_stripe_param *param,
2178 struct llapi_layout *layout)
2180 struct ll_ioc_lease *data = NULL;
2183 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2185 error_loc = "cannot get lease";
2189 /* Atomically put lease, merge layouts, resync and close. */
2190 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2192 error_loc = "memory allocation";
2195 data->lil_mode = LL_LEASE_UNLCK;
2196 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2197 data->lil_count = 1;
2198 data->lil_ids[0] = fdv;
2199 rc = llapi_lease_set(fd, data);
2201 error_loc = "cannot merge layout";
2203 } else if (rc == 0) {
2205 error_loc = "lost lease lock";
2211 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2213 error_loc = "cannot resync file";
2217 /* delete first mirror now */
2218 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2220 error_loc = "cannot delete old layout";
2228 fprintf(stderr, "error: %s: %s: %s: %s\n",
2229 progname, name, error_loc, strerror(-rc));
2230 else if (migration_flags & MIGRATION_VERBOSE)
2231 printf("%s\n", name);
2238 * Parse a string containing an target index list into an array of integers.
2240 * The input string contains a comma delimited list of individual
2241 * indices and ranges, for example "1,2-4,7". Add the indices into the
2242 * \a tgts array and remove duplicates.
2244 * \param[out] tgts array to store indices in
2245 * \param[in] size size of \a tgts array
2246 * \param[in] offset starting index in \a tgts
2247 * \param[in] arg string containing OST index list
2248 * \param[in/out] overstriping index list may contain duplicates
2250 * \retval positive number of indices in \a tgts
2251 * \retval -EINVAL unable to parse \a arg
2253 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2254 unsigned long long *pattern)
2258 int slots = size - offset;
2260 bool overstriped = false;
2266 end_of_loop = false;
2267 while (!end_of_loop) {
2268 int start_index = 0;
2271 char *endptr = NULL;
2275 ptr = strchrnul(arg, ',');
2277 end_of_loop = *ptr == '\0';
2280 start_index = strtol(arg, &endptr, 0);
2281 if (endptr == arg) /* no data at all */
2283 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2286 end_index = start_index;
2287 if (*endptr == '-') {
2288 end_index = strtol(endptr + 1, &endptr, 0);
2289 if (*endptr != '\0')
2291 if (end_index < start_index)
2295 for (i = start_index; i <= end_index && slots > 0; i++) {
2298 /* remove duplicate */
2299 for (j = 0; j < offset; j++) {
2300 if (tgts[j] == i && pattern &&
2301 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2303 else if (tgts[j] == i)
2309 if (j == offset) { /* check complete */
2315 if (slots == 0 && i < end_index)
2323 if (!end_of_loop && ptr != NULL)
2326 if (!overstriped && pattern)
2327 *pattern = LLAPI_LAYOUT_DEFAULT;
2329 return rc < 0 ? rc : nr;
2332 struct lfs_setstripe_args {
2333 unsigned long long lsa_comp_end;
2334 unsigned long long lsa_stripe_size;
2335 unsigned long long lsa_extension_size;
2336 long long lsa_stripe_count;
2337 long long lsa_stripe_off;
2338 __u32 lsa_comp_flags;
2339 __u32 lsa_comp_neg_flags;
2340 unsigned long long lsa_pattern;
2341 unsigned int lsa_mirror_count;
2343 bool lsa_first_comp;
2344 bool lsa_extension_comp;
2346 char *lsa_pool_name;
2349 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2351 unsigned int mirror_count = lsa->lsa_mirror_count;
2352 bool first_comp = lsa->lsa_first_comp;
2354 memset(lsa, 0, sizeof(*lsa));
2356 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2357 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2358 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2359 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2360 lsa->lsa_pool_name = NULL;
2362 lsa->lsa_mirror_count = mirror_count;
2363 lsa->lsa_first_comp = first_comp;
2367 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2368 * @lsa: Stripe options to be initialized and inherited.
2370 * This function initializes stripe options in @lsa and inherit
2371 * stripe_size, stripe_count and OST pool_name options.
2375 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2377 unsigned long long stripe_size;
2378 long long stripe_count;
2379 char *pool_name = NULL;
2381 stripe_size = lsa->lsa_stripe_size;
2382 stripe_count = lsa->lsa_stripe_count;
2383 pool_name = lsa->lsa_pool_name;
2385 setstripe_args_init(lsa);
2387 lsa->lsa_stripe_size = stripe_size;
2388 lsa->lsa_stripe_count = stripe_count;
2389 lsa->lsa_pool_name = pool_name;
2392 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2394 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2395 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2396 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2397 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2398 lsa->lsa_comp_end != 0);
2402 * comp_args_to_layout() - Create or extend a composite layout.
2403 * @composite: Pointer to the composite layout.
2404 * @lsa: Stripe options for the new component.
2406 * This function creates or extends a composite layout by adding a new
2407 * component with stripe options from @lsa.
2409 * Return: 0 on success or an error code on failure.
2411 static int comp_args_to_layout(struct llapi_layout **composite,
2412 struct lfs_setstripe_args *lsa,
2415 struct llapi_layout *layout = *composite;
2416 uint64_t prev_end = 0;
2421 if (layout == NULL) {
2422 layout = llapi_layout_alloc();
2423 if (layout == NULL) {
2424 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2429 *composite = layout;
2430 lsa->lsa_first_comp = true;
2434 /* Get current component extent, current component
2435 * must be the tail component. */
2436 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2438 fprintf(stderr, "Get comp extent failed. %s\n",
2443 if (lsa->lsa_first_comp) {
2445 rc = llapi_layout_add_first_comp(layout);
2447 rc = llapi_layout_comp_add(layout);
2450 fprintf(stderr, "Add component failed. %s\n",
2456 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2458 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2459 lsa->lsa_comp_flags, strerror(errno));
2464 uint64_t comp_end = lsa->lsa_comp_end;
2466 /* The extendable component is 0-length, so it can be removed
2467 * if there is insufficient space to extend it. */
2468 if (lsa->lsa_extension_comp)
2469 comp_end = prev_end;
2471 rc = llapi_layout_comp_extent_set(layout, prev_end,
2474 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2475 prev_end, comp_end, strerror(errno));
2479 /* reset lsa_first_comp */
2480 lsa->lsa_first_comp = false;
2482 /* Data-on-MDT component setting */
2483 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2484 /* In case of Data-on-MDT patterns the only extra option
2485 * applicable is stripe size option. */
2486 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2487 fprintf(stderr, "Option 'stripe-count' can't be "
2488 "specified with Data-on-MDT component: %lld\n",
2489 lsa->lsa_stripe_count);
2493 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2494 fprintf(stderr, "Option 'stripe-size' can't be "
2495 "specified with Data-on-MDT component: %llu\n",
2496 lsa->lsa_stripe_size);
2500 if (lsa->lsa_nr_tgts != 0) {
2501 fprintf(stderr, "Option 'ost-list' can't be specified "
2502 "with Data-on-MDT component: '%i'\n",
2507 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2508 fprintf(stderr, "Option 'stripe-offset' can't be "
2509 "specified with Data-on-MDT component: %lld\n",
2510 lsa->lsa_stripe_off);
2514 if (lsa->lsa_pool_name != 0) {
2515 fprintf(stderr, "Option 'pool' can't be specified "
2516 "with Data-on-MDT component: '%s'\n",
2517 lsa->lsa_pool_name);
2522 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2524 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2529 /* Data-on-MDT component has always single stripe up to end */
2530 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2531 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2532 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2534 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2541 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2542 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2544 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2545 rc = llapi_layout_extension_size_set(layout, size);
2547 rc = llapi_layout_stripe_size_set(layout, size);
2550 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2551 size, strerror(errno));
2555 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2557 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2558 lsa->lsa_stripe_count, strerror(errno));
2562 if (lsa->lsa_pool_name != NULL) {
2563 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2565 fprintf(stderr, "Set pool name: %s failed. %s\n",
2566 lsa->lsa_pool_name, strerror(errno));
2570 rc = llapi_layout_pool_name_set(layout, "");
2572 fprintf(stderr, "Clear pool name failed: %s\n",
2578 if (lsa->lsa_nr_tgts > 0) {
2579 if (lsa->lsa_stripe_count > 0 &&
2580 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2581 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2582 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2583 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2584 lsa->lsa_stripe_count,
2589 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2590 rc = llapi_layout_ost_index_set(layout, i,
2595 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2596 lsa->lsa_stripe_off != -1) {
2597 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2600 fprintf(stderr, "Set ost index %d failed. %s\n",
2601 i, strerror(errno));
2605 /* Create the second, virtual component of extension space */
2606 if (lsa->lsa_extension_comp) {
2607 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2608 lsa->lsa_extension_comp = false;
2615 static int build_component(struct llapi_layout **layout,
2616 struct lfs_setstripe_args *lsa, bool set_extent)
2620 rc = comp_args_to_layout(layout, lsa, set_extent);
2624 if (lsa->lsa_mirror_count > 0) {
2625 rc = llapi_layout_mirror_count_set(*layout,
2626 lsa->lsa_mirror_count);
2630 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2633 lsa->lsa_mirror_count = 0;
2639 static int build_prev_component(struct llapi_layout **layout,
2640 struct lfs_setstripe_args *prev,
2641 struct lfs_setstripe_args *lsa,
2644 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2647 if (prev->lsa_stripe_size) {
2649 prev->lsa_comp_end = lsa->lsa_comp_end;
2650 prev->lsa_extension_size = lsa->lsa_extension_size;
2651 prev->lsa_extension_comp = true;
2654 rc = build_component(layout, prev, true);
2659 /* Copy lsa to previous lsa;
2660 * if this is an extension component, make the previous invalid; */
2662 prev->lsa_stripe_size = 0;
2669 static int build_layout_from_yaml_node(struct cYAML *node,
2670 struct llapi_layout **layout,
2671 struct lfs_setstripe_args *lsa,
2672 struct lfs_setstripe_args *prevp)
2674 struct lfs_setstripe_args prev = { 0 };
2675 __u32 *osts = lsa->lsa_tgts;
2683 string = node->cy_string;
2685 if (node->cy_type == CYAML_TYPE_OBJECT) {
2686 /* go deep to sub blocks */
2687 if (string && !strncmp(string, "component", 9) &&
2688 strncmp(string, "component0", 10) &&
2689 strncmp(string, "components", 10)) {
2690 rc = build_prev_component(layout, prevp, lsa, true);
2694 /* initialize lsa. */
2695 setstripe_args_init(lsa);
2696 lsa->lsa_first_comp = false;
2697 lsa->lsa_tgts = osts;
2700 rc = build_layout_from_yaml_node(node->cy_child, layout,
2705 if (node->cy_string == NULL)
2708 /* skip leading lmm_ if present, to simplify parsing */
2709 if (strncmp(string, "lmm_", 4) == 0)
2712 if (node->cy_type == CYAML_TYPE_STRING) {
2713 if (!strcmp(string, "lcme_extent.e_end")) {
2714 if (!strcmp(node->cy_valuestring, "EOF") ||
2715 !strcmp(node->cy_valuestring, "eof"))
2716 lsa->lsa_comp_end = LUSTRE_EOF;
2717 } else if (!strcmp(string, "pool")) {
2718 lsa->lsa_pool_name = node->cy_valuestring;
2719 } else if (!strcmp(string, "pattern")) {
2720 if (!strcmp(node->cy_valuestring, "mdt"))
2721 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2722 if (!strcmp(node->cy_valuestring,
2723 "raid0,overstriped"))
2725 LLAPI_LAYOUT_OVERSTRIPING;
2726 } else if (!strcmp(string, "lcme_flags")) {
2727 rc = comp_str2flags(node->cy_valuestring,
2728 &lsa->lsa_comp_flags,
2729 &lsa->lsa_comp_neg_flags);
2732 /* Only template flags have meaning in
2733 * the layout for a new file
2735 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2737 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2738 if (!strcmp(string, "lcm_mirror_count")) {
2739 lsa->lsa_mirror_count = node->cy_valueint;
2740 } else if (!strcmp(string, "lcme_extent.e_start")) {
2741 if (node->cy_valueint == 0)
2742 lsa->lsa_first_comp = true;
2743 } else if (!strcmp(string, "lcme_extent.e_end")) {
2744 if (node->cy_valueint == -1)
2745 lsa->lsa_comp_end = LUSTRE_EOF;
2747 lsa->lsa_comp_end = node->cy_valueint;
2748 } else if (!strcmp(string, "stripe_count")) {
2749 lsa->lsa_stripe_count = node->cy_valueint;
2750 } else if (!strcmp(string, "stripe_size")) {
2751 lsa->lsa_stripe_size = node->cy_valueint;
2752 } else if (!strcmp(string, "extension_size")) {
2753 lsa->lsa_extension_size = node->cy_valueint;
2754 lsa->lsa_extension_comp = true;
2755 } else if (!strcmp(string, "stripe_offset")) {
2756 lsa->lsa_stripe_off = node->cy_valueint;
2757 } else if (!strcmp(string, "l_ost_idx")) {
2758 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2763 node = node->cy_next;
2766 if (prevp == &prev) {
2767 rc = build_prev_component(layout, prevp, lsa, true);
2771 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2772 rc = build_component(layout, lsa, *layout != NULL);
2778 static int lfs_comp_create_from_yaml(char *template,
2779 struct llapi_layout **layout,
2780 struct lfs_setstripe_args *lsa,
2783 struct cYAML *tree = NULL, *err_rc = NULL;
2786 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2788 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2789 progname, template);
2790 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2791 "can't parse", &err_rc);
2792 cYAML_print_tree2file(stderr, err_rc);
2793 cYAML_free_tree(err_rc);
2798 /* initialize lsa for plain file */
2799 setstripe_args_init(lsa);
2800 lsa->lsa_tgts = osts;
2802 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
2804 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2805 progname, template);
2808 /* clean clean lsa */
2809 setstripe_args_init(lsa);
2813 cYAML_free_tree(tree);
2818 * Get the extension size from the next (SEL) component and extend the
2819 * current component on it. The start of the next component is to be
2822 * \param[in] layout the current layout
2823 * \param[in] start the start of the current component
2824 * \param[in,out] end the end of the current component
2825 * \param[in] offset the offset to adjust the end position to instead of
2828 * \retval 0 - extended successfully
2829 * \retval < 0 - error
2831 static int layout_extend_comp(struct llapi_layout *layout,
2832 uint64_t start, uint64_t *end,
2835 uint64_t size, next_start, next_end;
2838 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2840 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2841 "%s\n", progname, strerror(errno));
2845 /* Even if the @size will not be used below, this will fail if
2846 * this is not a SEL component - a good confirmation we are
2847 * working on right components. */
2848 rc = llapi_layout_extension_size_get(layout, &size);
2850 fprintf(stderr, "%s setstripe: cannot get component ext size: "
2851 "%s\n", progname, strerror(errno));
2855 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2857 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2858 progname, strerror(errno));
2862 next_start += offset ?: size;
2863 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2865 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2866 progname, strerror(errno));
2870 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2872 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2873 "%s\n", progname, strerror(errno));
2877 *end += offset ?: size;
2878 rc = llapi_layout_comp_extent_set(layout, start, *end);
2880 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2881 progname, strerror(errno));
2889 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2890 * end of the last component in the existing file, and adjust the
2891 * first extent start of the components to be added accordingly.
2893 * In the create mode, we need to check if the first component is an extendable
2894 * SEL component and extend its length to the extension size (first component
2895 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2897 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2900 struct llapi_layout *head;
2901 uint64_t start = 0, prev_end = 0;
2905 if (layout == NULL || !(comp_add || llapi_layout_is_composite(layout)))
2910 head = llapi_layout_get_by_path(fname, 0);
2913 "%s setstripe: cannot read layout from '%s': "
2914 "%s\n", progname, fname, strerror(errno));
2916 } else if (errno == ENODATA) {
2917 /* file without LOVEA, this component-add will be turned
2918 * into a component-create. */
2919 llapi_layout_free(head);
2922 /* the new layout will be added to an empty one, it
2923 * still needs to be adjusted below */
2926 } else if (!llapi_layout_is_composite(head)) {
2927 fprintf(stderr, "%s setstripe: '%s' not a composite "
2928 "file\n", progname, fname);
2929 llapi_layout_free(head);
2933 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2935 fprintf(stderr, "%s setstripe: cannot get prev "
2936 "extent: %s\n", progname, strerror(errno));
2937 llapi_layout_free(head);
2941 llapi_layout_free(head);
2945 /* Make sure we use the first component of the layout to be added. */
2946 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2949 "%s setstripe: cannot move component cursor: %s\n",
2950 progname, strerror(errno));
2954 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2956 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2957 progname, strerror(errno));
2961 if (start == 0 && end == 0) {
2962 rc = layout_extend_comp(layout, start, &end,
2963 comp_add ? prev_end : 0);
2968 if (start > prev_end || end < prev_end) {
2969 fprintf(stderr, "%s setstripe: first extent [%lu, %lu) not "
2970 "adjacent with extent end %lu\n",
2971 progname, start, end, prev_end);
2975 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2977 fprintf(stderr, "%s setstripe: cannot set component extent "
2979 progname, prev_end, end, strerror(errno));
2986 static int mirror_adjust_first_extents(struct mirror_args *list)
2993 while (list != NULL) {
2994 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
2997 list = list->m_next;
3003 static inline bool arg_is_eof(char *arg)
3005 return !strncmp(arg, "-1", strlen("-1")) ||
3006 !strncmp(arg, "EOF", strlen("EOF")) ||
3007 !strncmp(arg, "eof", strlen("eof"));
3011 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3013 * Return: Valid mirror_args pointer on success and
3014 * NULL if memory allocation fails.
3016 static struct mirror_args *lfs_mirror_alloc(void)
3018 struct mirror_args *mirror = NULL;
3021 mirror = calloc(1, sizeof(*mirror));
3022 if (mirror != NULL) {
3023 mirror->m_inherit = false;
3034 * lfs_mirror_free() - Free memory allocated for a mirror argument
3036 * @mirror: Previously allocated mirror argument structure by
3037 * lfs_mirror_alloc().
3039 * Free memory allocated for @mirror.
3043 static void lfs_mirror_free(struct mirror_args *mirror)
3045 if (mirror->m_layout != NULL)
3046 llapi_layout_free(mirror->m_layout);
3051 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3052 * @mirror_list: Previously allocated mirror list.
3054 * Free memory allocated for @mirror_list.
3058 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3060 struct mirror_args *next_mirror = NULL;
3062 while (mirror_list != NULL) {
3063 next_mirror = mirror_list->m_next;
3064 lfs_mirror_free(mirror_list);
3065 mirror_list = next_mirror;
3077 LFS_COMP_NO_VERIFY_OPT,
3079 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3081 LFS_MIRROR_STATE_OPT,
3083 LFS_MIRROR_INDEX_OPT,
3084 LFS_LAYOUT_FOREIGN_OPT,
3090 static int lfs_setstripe_internal(int argc, char **argv,
3091 enum setstripe_origin opc)
3093 struct lfs_setstripe_args lsa = { 0 };
3094 struct llapi_stripe_param *param = NULL;
3095 struct find_param migrate_mdt_param = {
3105 unsigned long long size_units = 1;
3106 bool migrate_mode = false;
3107 bool migrate_mdt_mode = false;
3108 bool setstripe_mode = false;
3109 bool migration_block = false;
3110 __u64 migration_flags = 0;
3111 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3112 int comp_del = 0, comp_set = 0;
3115 struct llapi_layout *layout = NULL;
3116 struct llapi_layout **lpp = &layout;
3117 bool mirror_mode = false;
3118 bool has_m_file = false;
3119 __u32 mirror_count = 0;
3120 enum mirror_flags mirror_flags = 0;
3121 struct mirror_args *mirror_list = NULL;
3122 struct mirror_args *new_mirror = NULL;
3123 struct mirror_args *last_mirror = NULL;
3124 __u16 mirror_id = 0;
3126 bool from_yaml = false;
3127 bool from_copy = false;
3128 char *template = NULL;
3129 bool foreign_mode = false;
3131 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3132 char *mode_opt = NULL;
3133 mode_t previous_umask = 0;
3134 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3136 struct option long_opts[] = {
3137 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3138 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3139 /* --block is only valid in migrate mode */
3140 { .val = 'b', .name = "block", .has_arg = no_argument },
3141 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3142 { .val = LFS_COMP_ADD_OPT,
3143 .name = "comp-add", .has_arg = no_argument },
3144 { .val = LFS_COMP_ADD_OPT,
3145 .name = "component-add", .has_arg = no_argument },
3146 { .val = LFS_COMP_DEL_OPT,
3147 .name = "comp-del", .has_arg = no_argument },
3148 { .val = LFS_COMP_DEL_OPT,
3149 .name = "component-del", .has_arg = no_argument },
3150 { .val = LFS_COMP_FLAGS_OPT,
3151 .name = "comp-flags", .has_arg = required_argument },
3152 { .val = LFS_COMP_FLAGS_OPT,
3153 .name = "component-flags",
3154 .has_arg = required_argument },
3155 { .val = LFS_COMP_SET_OPT,
3156 .name = "comp-set", .has_arg = no_argument },
3157 { .val = LFS_COMP_SET_OPT,
3158 .name = "component-set",
3159 .has_arg = no_argument},
3160 { .val = LFS_COMP_NO_VERIFY_OPT,
3161 .name = "no-verify", .has_arg = no_argument},
3162 { .val = LFS_LAYOUT_FLAGS_OPT,
3163 .name = "flags", .has_arg = required_argument},
3164 { .val = LFS_LAYOUT_FOREIGN_OPT,
3165 .name = "foreign", .has_arg = optional_argument},
3166 { .val = LFS_MIRROR_ID_OPT,
3167 .name = "mirror-id", .has_arg = required_argument},
3168 { .val = LFS_MODE_OPT,
3169 .name = "mode", .has_arg = required_argument},
3170 { .val = LFS_LAYOUT_COPY,
3171 .name = "copy", .has_arg = required_argument},
3172 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3173 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3174 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3175 { .val = 'C', .name = "overstripe-count",
3176 .has_arg = required_argument},
3177 { .val = 'd', .name = "delete", .has_arg = no_argument},
3178 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3179 /* --non-direct is only valid in migrate mode */
3180 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3181 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3182 { .val = 'E', .name = "component-end",
3183 .has_arg = required_argument},
3184 { .val = 'f', .name = "file", .has_arg = required_argument },
3185 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3186 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3187 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3188 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3189 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3190 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3191 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3192 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3193 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3194 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3195 { .val = 'L', .name = "layout", .has_arg = required_argument },
3196 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3197 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3198 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3199 /* --non-block is only valid in migrate mode */
3200 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3201 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3202 { .val = 'o', .name = "ost", .has_arg = required_argument },
3203 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3204 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3205 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3207 { .val = 'p', .name = "pool", .has_arg = required_argument },
3208 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3209 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3210 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3211 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3212 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3213 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3214 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3215 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3216 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3217 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3218 /* --verbose is only valid in migrate mode */
3219 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3220 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3221 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3222 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3223 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3226 setstripe_args_init(&lsa);
3228 migrate_mode = (opc == SO_MIGRATE);
3229 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3230 setstripe_mode = (opc == SO_SETSTRIPE);
3231 if (opc == SO_MIRROR_DELETE) {
3233 mirror_flags = MF_DESTROY;
3236 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3238 while ((c = getopt_long(argc, argv,
3239 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3240 long_opts, NULL)) >= 0) {
3246 case LFS_COMP_ADD_OPT:
3249 case LFS_COMP_DEL_OPT:
3252 case LFS_COMP_FLAGS_OPT:
3253 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3254 &lsa.lsa_comp_neg_flags);
3257 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3258 fprintf(stderr, "%s: inverted flags are not supported\n",
3263 case LFS_COMP_SET_OPT:
3266 case LFS_COMP_NO_VERIFY_OPT:
3267 mirror_flags |= MF_NO_VERIFY;
3269 case LFS_MIRROR_ID_OPT:
3270 mirror_id = strtoul(optarg, &end, 0);
3271 if (*end != '\0' || mirror_id == 0) {
3273 "%s %s: invalid mirror ID '%s'\n",
3274 progname, argv[0], optarg);
3278 case LFS_LAYOUT_FLAGS_OPT: {
3281 /* check for numeric flags (foreign and mirror cases) */
3282 if (setstripe_mode && !mirror_mode && !last_mirror) {
3283 flags = strtoul(optarg, &end, 16);
3286 "%s %s: bad flags '%s'\n",
3287 progname, argv[0], optarg);
3293 if (!mirror_mode || !last_mirror) {
3294 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
3299 result = comp_str2flags(optarg, &last_mirror->m_flags,
3305 fprintf(stderr, "%s: inverted flags are not supported\n",
3310 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3312 "%s: unsupported mirror flags: %s\n",
3319 case LFS_LAYOUT_FOREIGN_OPT:
3320 if (optarg != NULL) {
3321 /* check pure numeric */
3322 type = strtoul(optarg, &end, 0);
3325 type = check_foreign_type_name(optarg);
3326 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3328 "%s %s: unrecognized foreign type '%s'\n",
3335 foreign_mode = true;
3339 if (mode_opt != NULL) {
3340 mode = strtoul(mode_opt, &end, 8);
3343 "%s %s: bad mode '%s'\n",
3344 progname, argv[0], mode_opt);
3347 previous_umask = umask(0);
3350 case LFS_LAYOUT_COPY:
3355 if (!migrate_mode) {
3357 "%s %s: -b|--block valid only for migrate command\n",
3361 migration_block = true;
3364 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3366 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3370 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3373 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3376 "%s %s: invalid stripe count '%s'\n",
3377 progname, argv[0], optarg);
3381 if (lsa.lsa_stripe_count == -1)
3382 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3385 /* delete the default striping pattern */
3387 if (opc == SO_MIRROR_SPLIT) {
3390 "%s %s: -d cannot used with -f\n",
3394 mirror_flags |= MF_DESTROY;
3398 if (!migrate_mode) {
3400 "%s %s: -D|--non-direct is valid "
3401 "only for migrate command\n",
3405 migration_flags |= MIGRATION_NONDIRECT;
3408 if (lsa.lsa_comp_end != 0) {
3409 result = comp_args_to_layout(lpp, &lsa, true);
3411 fprintf(stderr, "%s: invalid layout\n",
3416 setstripe_args_init_inherit(&lsa);
3419 if (arg_is_eof(optarg)) {
3420 lsa.lsa_comp_end = LUSTRE_EOF;
3422 result = llapi_parse_size(optarg,
3427 "%s %s: invalid component end '%s'\n",
3428 progname, argv[0], optarg);
3434 if (!migrate_mode) {
3435 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3439 lsa.lsa_pattern = check_hashtype(optarg);
3440 if (lsa.lsa_pattern == 0) {
3442 "%s %s: bad stripe hash type '%s'\n",
3443 progname, argv[0], optarg);
3448 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3451 "%s %s: invalid stripe offset '%s'\n",
3452 progname, argv[0], optarg);
3455 if (lsa.lsa_stripe_off == -1)
3456 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3459 comp_id = strtoul(optarg, &end, 0);
3460 if (*end != '\0' || comp_id == 0 ||
3461 comp_id > LCME_ID_MAX) {
3463 "%s %s: invalid component ID '%s'\n",
3464 progname, argv[0], optarg);
3469 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3471 "error: %s: invalid option: %s\n",
3472 progname, argv[optopt + 1]);
3475 if (opc == SO_MIRROR_EXTEND) {
3476 if (last_mirror == NULL) {
3478 "error: %s: '-N' must exist in front of '%s'\n",
3479 progname, argv[optopt + 1]);
3482 last_mirror->m_file = optarg;
3483 last_mirror->m_count = 1;
3486 if (mirror_list == NULL)
3487 mirror_list = lfs_mirror_alloc();
3488 mirror_list->m_file = optarg;
3493 if (strcmp(argv[optind - 1], "mdt") == 0) {
3494 /* Can be only the first component */
3495 if (layout != NULL) {
3497 fprintf(stderr, "error: 'mdt' layout "
3498 "can be only the first one\n");
3501 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3503 fprintf(stderr, "error: 'mdt' layout "
3504 "size is too big\n");
3507 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3508 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3510 fprintf(stderr, "error: layout '%s' is "
3511 "unknown, supported layouts are: "
3512 "'mdt', 'raid0'\n", argv[optind]);
3517 if (!migrate_mode) {
3519 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3523 migrate_mdt_mode = true;
3524 lsa.lsa_nr_tgts = parse_targets(tgts,
3525 sizeof(tgts) / sizeof(__u32),
3526 lsa.lsa_nr_tgts, optarg, NULL);
3527 if (lsa.lsa_nr_tgts < 0) {
3529 "%s %s: invalid MDT target(s) '%s'\n",
3530 progname, argv[0], optarg);
3534 lsa.lsa_tgts = tgts;
3535 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3536 lsa.lsa_stripe_off = tgts[0];
3539 if (!migrate_mode) {
3541 "%s %s: -n|--non-block valid only for migrate command\n",
3545 migration_flags |= MIGRATION_NONBLOCK;
3548 if (opc == SO_SETSTRIPE) {
3549 opc = SO_MIRROR_CREATE;
3553 if (optarg != NULL) {
3554 mirror_count = strtoul(optarg, &end, 0);
3555 if (*end != '\0' || mirror_count == 0) {
3557 "error: %s: bad mirror count: %s\n",
3564 new_mirror = lfs_mirror_alloc();
3565 new_mirror->m_count = mirror_count;
3567 if (mirror_list == NULL)
3568 mirror_list = new_mirror;
3570 if (last_mirror != NULL) {
3571 /* wrap up last mirror */
3572 if (!setstripe_args_specified(&lsa))
3573 last_mirror->m_inherit = true;
3574 if (lsa.lsa_comp_end == 0)
3575 lsa.lsa_comp_end = LUSTRE_EOF;
3577 result = comp_args_to_layout(lpp, &lsa, true);
3579 lfs_mirror_free(new_mirror);
3583 setstripe_args_init_inherit(&lsa);
3585 last_mirror->m_next = new_mirror;
3588 last_mirror = new_mirror;
3589 lpp = &last_mirror->m_layout;
3592 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3593 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3594 fprintf(stderr, "warning: '--ost-list' is "
3595 "deprecated, use '--ost' instead\n");
3597 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3599 "%s %s: -o|--ost incompatible with DoM layout\n",
3603 /* -o allows overstriping, and must note it because
3604 * parse_targets is shared with MDT striping, which
3605 * does not allow duplicates
3607 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3608 lsa.lsa_nr_tgts = parse_targets(tgts,
3609 sizeof(tgts) / sizeof(__u32),
3610 lsa.lsa_nr_tgts, optarg,
3612 if (lsa.lsa_nr_tgts < 0) {
3614 "%s %s: invalid OST target(s) '%s'\n",
3615 progname, argv[0], optarg);
3619 lsa.lsa_tgts = tgts;
3620 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3621 lsa.lsa_stripe_off = tgts[0];
3626 lsa.lsa_pool_name = optarg;
3628 if (strlen(lsa.lsa_pool_name) == 0 ||
3629 strncmp(lsa.lsa_pool_name, "none",
3630 LOV_MAXPOOLNAME) == 0)
3631 lsa.lsa_pool_name = NULL;
3634 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3638 "%s %s: invalid stripe size '%s'\n",
3639 progname, argv[0], optarg);
3644 if (!migrate_mode) {
3646 "%s %s: -v|--verbose valid only for migrate command\n",
3650 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3651 migration_flags = MIGRATION_VERBOSE;
3661 result = llapi_parse_size(optarg,
3662 &lsa.lsa_extension_size,
3666 "%s %s: invalid extension size '%s'\n",
3667 progname, argv[0], optarg);
3671 lsa.lsa_extension_comp = true;
3674 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3675 progname, argv[0], argv[optind - 1]);
3680 fname = argv[optind];
3682 if (optind == argc) {
3683 fprintf(stderr, "%s %s: FILE must be specified\n",
3688 /* lfs migrate $filename should keep the file's layout by default */
3689 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3693 if (xattr && !foreign_mode) {
3694 /* only print a warning as this is harmless and will be ignored
3697 "%s %s: xattr has been specified for non-foreign layout\n",
3699 } else if (foreign_mode && !xattr) {
3701 "%s %s: xattr must be provided in foreign mode\n",
3706 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3707 comp_set || comp_id || delete || from_copy ||
3708 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3711 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3716 if (mirror_mode && mirror_count == 0) {
3718 "error: %s: --mirror-count|-N option is required\n",
3725 if (!setstripe_args_specified(&lsa))
3726 last_mirror->m_inherit = true;
3727 if (lsa.lsa_comp_end == 0)
3728 lsa.lsa_comp_end = LUSTRE_EOF;
3731 if (lsa.lsa_comp_end != 0) {
3732 result = comp_args_to_layout(lpp, &lsa, true);
3734 fprintf(stderr, "error: %s: invalid layout\n",
3741 if (mirror_flags & MF_NO_VERIFY) {
3742 if (opc != SO_MIRROR_EXTEND) {
3744 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3748 } else if (!has_m_file) {
3750 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3757 if (comp_set && !comp_id) {
3758 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3763 if ((delete + comp_set + comp_del + comp_add) > 1) {
3765 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3770 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3771 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3773 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3778 if ((comp_set || comp_del) &&
3779 (setstripe_args_specified(&lsa) || layout != NULL)) {
3781 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3786 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3788 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3793 if (comp_add || comp_del) {
3796 result = lstat(fname, &st);
3797 if (result == 0 && S_ISDIR(st.st_mode)) {
3799 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3805 fprintf(stderr, "error: %s: can't use --component-add "
3806 "or --component-del for mirror operation\n",
3813 if (layout == NULL) {
3815 "%s %s: option -E must be specified with --component-add\n",
3821 if (from_yaml && from_copy) {
3823 "%s: can't specify --yaml and --copy together\n",
3828 if ((from_yaml || from_copy) &&
3829 (setstripe_args_specified(&lsa) || layout != NULL)) {
3830 fprintf(stderr, "error: %s: can't specify --yaml or --copy with"
3831 " -c, -S, -i, -o, -p or -E options.\n",
3836 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3838 "%s %s: options --non-block and --block are mutually exclusive\n",
3843 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3844 opc != SO_MIRROR_DELETE && comp_id != 0) {
3846 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3851 if (migrate_mdt_mode) {
3852 struct lmv_user_md *lmu;
3854 /* initialize migrate mdt parameters */
3855 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3856 LMV_USER_MAGIC_SPECIFIC));
3859 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3860 progname, argv[0], strerror(ENOMEM));
3864 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3865 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3866 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3868 "%s %s: migrate should specify MDT index\n",
3873 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3874 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3875 lmu->lum_hash_type = lsa.lsa_pattern;
3877 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
3878 if (lsa.lsa_pool_name) {
3879 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3880 sizeof(lmu->lum_pool_name) - 1);
3881 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3883 if (lsa.lsa_nr_tgts > 1) {
3886 if (lsa.lsa_stripe_count > 0 &&
3887 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3888 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3890 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3891 progname, lsa.lsa_stripe_count,
3897 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3898 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3899 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3900 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3902 lmu->lum_magic = LMV_USER_MAGIC;
3905 migrate_mdt_param.fp_lmv_md = lmu;
3906 migrate_mdt_param.fp_migrate = 1;
3907 } else if (layout == NULL) {
3908 /* initialize stripe parameters */
3909 param = calloc(1, offsetof(typeof(*param),
3910 lsp_osts[lsa.lsa_nr_tgts]));
3911 if (param == NULL) {
3913 "%s %s: cannot allocate memory for parameters: %s\n",
3914 progname, argv[0], strerror(ENOMEM));
3919 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3920 param->lsp_stripe_size = lsa.lsa_stripe_size;
3921 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3922 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3923 param->lsp_stripe_count = -1;
3925 param->lsp_stripe_count = lsa.lsa_stripe_count;
3927 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3928 param->lsp_stripe_offset = -1;
3930 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3931 param->lsp_stripe_pattern =
3932 llapi_pattern_to_lov(lsa.lsa_pattern);
3933 if (param->lsp_stripe_pattern == EINVAL) {
3934 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3939 param->lsp_pool = lsa.lsa_pool_name;
3940 param->lsp_is_specific = false;
3941 if (lsa.lsa_nr_tgts > 0) {
3942 if (lsa.lsa_stripe_count > 0 &&
3943 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3944 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3945 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3947 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3948 argv[0], lsa.lsa_stripe_count,
3954 param->lsp_is_specific = true;
3955 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3956 memcpy(param->lsp_osts, tgts,
3957 sizeof(*tgts) * lsa.lsa_nr_tgts);
3962 /* generate a layout from a YAML template */
3963 result = lfs_comp_create_from_yaml(template, &layout,
3966 fprintf(stderr, "error: %s: can't create composite "
3967 "layout from template file %s\n",
3973 if (layout != NULL || mirror_list != NULL) {
3975 result = mirror_adjust_first_extents(mirror_list);
3977 result = layout_adjust_first_extent(fname, layout,
3979 if (result == -ENODATA)
3981 else if (result != 0) {
3982 fprintf(stderr, "error: %s: invalid layout\n",
3988 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3990 layout = llapi_layout_get_by_path(template ?: fname, 0);
3991 if (layout == NULL) {
3992 fprintf(stderr, "%s: can't create composite "
3993 "layout from file %s.\n",
3994 progname, template ?: fname);
3999 if (migrate_mdt_mode) {
4000 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4001 } else if (migrate_mode) {
4002 result = lfs_migrate(fname, migration_flags, param,
4004 } else if (comp_set != 0) {
4005 result = lfs_component_set(fname, comp_id,
4007 lsa.lsa_comp_neg_flags);
4008 } else if (comp_del != 0) {
4009 result = lfs_component_del(fname, comp_id,
4011 lsa.lsa_comp_neg_flags);
4012 } else if (comp_add != 0) {
4013 result = lfs_component_add(fname, layout);
4014 } else if (opc == SO_MIRROR_CREATE) {
4015 result = mirror_create(fname, mirror_list);
4016 } else if (opc == SO_MIRROR_EXTEND) {
4017 result = mirror_extend(fname, mirror_list,
4019 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4020 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4022 "%s: no mirror specified to delete from '%s'\n",
4026 if (lsa.lsa_pool_name)
4027 mirror_flags |= MF_COMP_POOL;
4028 else if (mirror_id != 0)
4029 comp_id = mirror_id;
4031 mirror_flags |= MF_COMP_ID;
4032 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4034 has_m_file ? mirror_list->m_file :
4036 } else if (layout != NULL) {
4037 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4043 } else if (foreign_mode) {
4044 result = llapi_file_create_foreign(fname, mode, type,
4051 result = llapi_file_open_param(fname,
4060 /* Save the first error encountered. */
4067 if (mode_opt != NULL)
4068 umask(previous_umask);
4071 free(migrate_mdt_param.fp_lmv_md);
4072 llapi_layout_free(layout);
4073 lfs_mirror_list_free(mirror_list);
4078 llapi_layout_free(layout);
4079 lfs_mirror_list_free(mirror_list);
4083 static int lfs_poollist(int argc, char **argv)
4088 return llapi_poollist(argv[1]);
4091 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4096 char *endptr = "AD";
4101 else if (str[0] == '-')
4107 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4108 long long val = strtoll(timebuf, &endptr, 0);
4113 unit *= 52; /* 52 weeks + 1 day below */
4114 case 'w': /* fallthrough */
4116 case '\0': /* days are default unit if none used */
4117 case 'd': /* fallthrough */
4118 unit = (unit + (*endptr == 'y')) * 24;
4119 case 'h': /* fallthrough */
4121 case 'm': /* fallthrough */
4123 case 's': /* fallthrough */
4125 /* don't need to multiply by 1 for seconds */
4128 "%s find: bad time string '%s': %s\n",
4129 progname, timebuf, strerror(EINVAL));
4133 if (param->fp_time_margin == 0 ||
4134 (*endptr && unit < param->fp_time_margin))
4135 param->fp_time_margin = unit;
4142 fprintf(stderr, "%s find: bad time '%s': too large\n",
4152 static int name2uid(unsigned int *id, const char *name)
4154 struct passwd *passwd;
4156 passwd = getpwnam(name);
4159 *id = passwd->pw_uid;
4164 static int name2gid(unsigned int *id, const char *name)
4166 struct group *group;
4168 group = getgrnam(name);
4171 *id = group->gr_gid;
4176 static inline int name2projid(unsigned int *id, const char *name)
4181 static int uid2name(char **name, unsigned int id)
4183 struct passwd *passwd;
4185 passwd = getpwuid(id);
4188 *name = passwd->pw_name;
4193 static inline int gid2name(char **name, unsigned int id)
4195 struct group *group;
4197 group = getgrgid(id);
4200 *name = group->gr_name;
4205 static int name2layout(__u32 *layout, char *name)
4207 char *ptr, *layout_name;
4210 for (ptr = name; ; ptr = NULL) {
4211 layout_name = strtok(ptr, ",");
4212 if (layout_name == NULL)
4214 if (strcmp(layout_name, "released") == 0)
4215 *layout |= LOV_PATTERN_F_RELEASED;
4216 else if (strcmp(layout_name, "raid0") == 0)
4217 *layout |= LOV_PATTERN_RAID0;
4218 else if (strcmp(layout_name, "mdt") == 0)
4219 *layout |= LOV_PATTERN_MDT;
4220 else if (strcmp(layout_name, "overstriping") == 0)
4221 *layout |= LOV_PATTERN_OVERSTRIPING;
4228 static int lfs_find(int argc, char **argv)
4233 struct find_param param = {
4236 .fp_time_margin = 24 * 60 * 60,
4238 struct option long_opts[] = {
4239 { .val = 'A', .name = "atime", .has_arg = required_argument },
4240 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4241 { .val = 'B', .name = "btime", .has_arg = required_argument },
4242 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4243 { .val = LFS_COMP_COUNT_OPT,
4244 .name = "comp-count", .has_arg = required_argument },
4245 { .val = LFS_COMP_COUNT_OPT,
4246 .name = "component-count",
4247 .has_arg = required_argument },
4248 { .val = LFS_COMP_FLAGS_OPT,
4249 .name = "comp-flags", .has_arg = required_argument },
4250 { .val = LFS_COMP_FLAGS_OPT,
4251 .name = "component-flags",
4252 .has_arg = required_argument },
4253 { .val = LFS_COMP_START_OPT,
4254 .name = "comp-start", .has_arg = required_argument },
4255 { .val = LFS_COMP_START_OPT,
4256 .name = "component-start",
4257 .has_arg = required_argument },
4258 { .val = LFS_MIRROR_STATE_OPT,
4259 .name = "mirror-state", .has_arg = required_argument },
4260 { .val = LFS_LAYOUT_FOREIGN_OPT,
4261 .name = "foreign", .has_arg = optional_argument},
4262 { .val = LFS_NEWERXY_OPT,
4263 .name = "newer", .has_arg = required_argument},
4264 { .val = LFS_NEWERXY_OPT,
4265 .name = "neweraa", .has_arg = required_argument},
4266 { .val = LFS_NEWERXY_OPT,
4267 .name = "neweram", .has_arg = required_argument},
4268 { .val = LFS_NEWERXY_OPT,
4269 .name = "newerac", .has_arg = required_argument},
4270 { .val = LFS_NEWERXY_OPT,
4271 .name = "newerab", .has_arg = required_argument},
4272 { .val = LFS_NEWERXY_OPT,
4273 .name = "newerma", .has_arg = required_argument},
4274 { .val = LFS_NEWERXY_OPT,
4275 .name = "newermm", .has_arg = required_argument},
4276 { .val = LFS_NEWERXY_OPT,
4277 .name = "newermc", .has_arg = required_argument},
4278 { .val = LFS_NEWERXY_OPT,
4279 .name = "newermb", .has_arg = required_argument},
4280 { .val = LFS_NEWERXY_OPT,
4281 .name = "newerca", .has_arg = required_argument},
4282 { .val = LFS_NEWERXY_OPT,
4283 .name = "newercm", .has_arg = required_argument},
4284 { .val = LFS_NEWERXY_OPT,
4285 .name = "newercc", .has_arg = required_argument},
4286 { .val = LFS_NEWERXY_OPT,
4287 .name = "newercb", .has_arg = required_argument},
4288 { .val = LFS_NEWERXY_OPT,
4289 .name = "newerba", .has_arg = required_argument},
4290 { .val = LFS_NEWERXY_OPT,
4291 .name = "newerbm", .has_arg = required_argument},
4292 { .val = LFS_NEWERXY_OPT,
4293 .name = "newerbc", .has_arg = required_argument},
4294 { .val = LFS_NEWERXY_OPT,
4295 .name = "newerbb", .has_arg = required_argument},
4296 { .val = LFS_NEWERXY_OPT,
4297 .name = "newerBa", .has_arg = required_argument},
4298 { .val = LFS_NEWERXY_OPT,
4299 .name = "newerBm", .has_arg = required_argument},
4300 { .val = LFS_NEWERXY_OPT,
4301 .name = "newerBc", .has_arg = required_argument},
4302 { .val = LFS_NEWERXY_OPT,
4303 .name = "newerBB", .has_arg = required_argument},
4304 { .val = LFS_NEWERXY_OPT,
4305 .name = "newerat", .has_arg = required_argument},
4306 { .val = LFS_NEWERXY_OPT,
4307 .name = "newermt", .has_arg = required_argument},
4308 { .val = LFS_NEWERXY_OPT,
4309 .name = "newerct", .has_arg = required_argument},
4310 { .val = LFS_NEWERXY_OPT,
4311 .name = "newerbt", .has_arg = required_argument},
4312 { .val = LFS_NEWERXY_OPT,
4313 .name = "newerBt", .has_arg = required_argument},
4314 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4315 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4316 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4317 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4318 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4319 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4320 { .val = 'E', .name = "component-end",
4321 .has_arg = required_argument },
4322 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4323 { .val = LFS_LAYOUT_FOREIGN_OPT,
4324 .name = "foreign", .has_arg = optional_argument},
4325 { .val = 'g', .name = "gid", .has_arg = required_argument },
4326 { .val = 'G', .name = "group", .has_arg = required_argument },
4327 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4328 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4329 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4330 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4331 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4332 { .val = 'L', .name = "layout", .has_arg = required_argument },
4333 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4334 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4335 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4336 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4337 { .val = 'n', .name = "name", .has_arg = required_argument },
4338 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4339 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4340 { .val = 'O', .name = "obd", .has_arg = required_argument },
4341 { .val = 'O', .name = "ost", .has_arg = required_argument },
4342 /* no short option for pool yet, can be 'p' after 2.18 */
4343 { .val = LFS_POOL_OPT,
4344 .name = "pool", .has_arg = required_argument },
4345 { .val = '0', .name = "print0", .has_arg = no_argument },
4346 { .val = 'P', .name = "print", .has_arg = no_argument },
4347 { .val = LFS_PROJID_OPT,
4348 .name = "projid", .has_arg = required_argument },
4349 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4350 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4351 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4352 { .val = 's', .name = "size", .has_arg = required_argument },
4353 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4354 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4355 { .val = 't', .name = "type", .has_arg = required_argument },
4356 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4357 { .val = 'u', .name = "uid", .has_arg = required_argument },
4358 { .val = 'U', .name = "user", .has_arg = required_argument },
4359 { .val = 'z', .name = "extension-size",
4360 .has_arg = required_argument },
4361 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4362 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4363 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4377 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4378 while ((c = getopt_long_only(argc, argv,
4379 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4380 long_opts, &optidx)) >= 0) {
4385 /* '!' is part of option */
4386 /* when getopt_long_only() finds a string which is not
4387 * an option nor a known option argument it returns 1
4388 * in that case if we already have found pathstart and pathend
4389 * (i.e. we have the list of pathnames),
4390 * the only supported value is "!"
4392 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4393 if (!isoption && pathend != -1) {
4394 fprintf(stderr, "err: %s: filename|dirname must either "
4395 "precede options or follow options\n",
4400 if (!isoption && pathstart == -1)
4401 pathstart = optind - 1;
4402 if (isoption && pathstart != -1 && pathend == -1)
4403 pathend = optind - 2;
4409 /* unknown; opt is "!" or path component,
4410 * checking done above.
4412 if (strcmp(optarg, "!") == 0)
4416 xtime = ¶m.fp_atime;
4417 xsign = ¶m.fp_asign;
4418 param.fp_exclude_atime = !!neg_opt;
4419 /* no break, this falls through to 'B' for btime */
4422 xtime = ¶m.fp_btime;
4423 xsign = ¶m.fp_bsign;
4424 param.fp_exclude_btime = !!neg_opt;
4426 /* no break, this falls through to 'C' for ctime */
4429 xtime = ¶m.fp_ctime;
4430 xsign = ¶m.fp_csign;
4431 param.fp_exclude_ctime = !!neg_opt;
4433 /* no break, this falls through to 'M' for mtime */
4436 xtime = ¶m.fp_mtime;
4437 xsign = ¶m.fp_msign;
4438 param.fp_exclude_mtime = !!neg_opt;
4440 rc = set_time(¶m, &t, xtime, optarg);
4441 if (rc == LONG_MAX) {
4449 if (optarg[0] == '+') {
4450 param.fp_blocks_sign = -1;
4452 } else if (optarg[0] == '-') {
4453 param.fp_blocks_sign = 1;
4457 param.fp_blocks_units = 1024;
4458 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4459 ¶m.fp_blocks_units, 0);
4461 fprintf(stderr, "error: bad blocks '%s'\n",
4465 param.fp_check_blocks = 1;
4466 param.fp_exclude_blocks = !!neg_opt;
4468 case LFS_COMP_COUNT_OPT:
4469 if (optarg[0] == '+') {
4470 param.fp_comp_count_sign = -1;
4472 } else if (optarg[0] == '-') {
4473 param.fp_comp_count_sign = 1;
4477 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4478 if (*endptr != '\0') {
4479 fprintf(stderr, "error: bad component count "
4483 param.fp_check_comp_count = 1;
4484 param.fp_exclude_comp_count = !!neg_opt;
4486 case LFS_COMP_FLAGS_OPT:
4487 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4488 ¶m.fp_comp_neg_flags);
4490 fprintf(stderr, "error: bad component flags "
4494 param.fp_check_comp_flags = 1;
4496 __u32 flags = param.fp_comp_neg_flags;
4497 param.fp_comp_neg_flags = param.fp_comp_flags;
4498 param.fp_comp_flags = flags;
4501 case LFS_COMP_START_OPT:
4502 if (optarg[0] == '+') {
4503 param.fp_comp_start_sign = -1;
4505 } else if (optarg[0] == '-') {
4506 param.fp_comp_start_sign = 1;
4510 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4511 ¶m.fp_comp_start_units, 0);
4513 fprintf(stderr, "error: bad component start "
4517 param.fp_check_comp_start = 1;
4518 param.fp_exclude_comp_start = !!neg_opt;
4520 case LFS_MIRROR_STATE_OPT:
4521 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4522 ¶m.fp_mirror_neg_state);
4525 "error: bad mirrored file state '%s'\n",
4529 param.fp_check_mirror_state = 1;
4531 __u16 state = param.fp_mirror_neg_state;
4532 param.fp_mirror_neg_state =
4533 param.fp_mirror_state;
4534 param.fp_mirror_state = state;
4538 if (optarg[0] == '+') {
4539 param.fp_stripe_count_sign = -1;
4541 } else if (optarg[0] == '-') {
4542 param.fp_stripe_count_sign = 1;
4546 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4547 if (*endptr != '\0') {
4548 fprintf(stderr,"error: bad stripe_count '%s'\n",
4553 param.fp_check_stripe_count = 1;
4554 param.fp_exclude_stripe_count = !!neg_opt;
4557 param.fp_max_depth = strtol(optarg, 0, 0);
4560 if (optarg[0] == '+') {
4561 param.fp_comp_end_sign = -1;
4563 } else if (optarg[0] == '-') {
4564 param.fp_comp_end_sign = 1;
4568 if (arg_is_eof(optarg)) {
4569 param.fp_comp_end = LUSTRE_EOF;
4570 param.fp_comp_end_units = 1;
4573 rc = llapi_parse_size(optarg,
4575 ¶m.fp_comp_end_units, 0);
4578 fprintf(stderr, "error: bad component end "
4582 param.fp_check_comp_end = 1;
4583 param.fp_exclude_comp_end = !!neg_opt;
4585 case LFS_LAYOUT_FOREIGN_OPT: {
4586 /* all types by default */
4587 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4589 if (optarg != NULL) {
4590 /* check pure numeric */
4591 type = strtoul(optarg, &endptr, 0);
4594 type = check_foreign_type_name(optarg);
4595 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4597 "%s %s: unknown foreign type '%s'\n",
4604 param.fp_foreign_type = type;
4605 param.fp_check_foreign = 1;
4606 param.fp_exclude_foreign = !!neg_opt;
4609 case LFS_NEWERXY_OPT: {
4615 time_t ref = time(NULL);
4617 /* no need to check bad options, they won't get here */
4618 if (strlen(long_opts[optidx].name) == 7) {
4619 x = long_opts[optidx].name[5];
4620 y = long_opts[optidx].name[6];
4624 static const char *const fmts[] = {
4625 "%Y-%m-%d %H:%M:%S",
4628 "%H:%M:%S", /* sometime today */
4637 for (i = 0; fmts[i] != NULL; i++) {
4640 /* Init for times relative to today */
4641 if (strncmp(fmts[i], "%H", 2) == 0)
4642 localtime_r(&ref, &tm);
4644 memset(&tm, 0, sizeof(tm));
4645 ptr = strptime(optarg, fmts[i], &tm);
4647 while (ptr && isspace(*ptr))
4649 if (ptr == optarg + strlen(optarg)) {
4657 "%s: invalid time '%s'\n",
4660 "supported formats are:\n ");
4661 for (i = 0; fmts[i] != NULL; i++)
4662 fprintf(stderr, "'%s', ",
4664 fprintf(stderr, "\n");
4670 } else if (y == 'b' || y == 'B') {
4673 rc = llapi_get_lum_file(optarg, NULL, &stx,
4675 if (rc || !(stx.stx_mask & STATX_BTIME)) {
4676 if (!(stx.stx_mask & STATX_BTIME))
4681 "%s: get btime failed '%s': %s\n",
4687 ref = stx.stx_btime.tv_sec;
4689 struct stat statbuf;
4691 if (stat(optarg, &statbuf) < 0) {
4693 "%s: cannot stat file '%s': %s\n",
4702 ref = statbuf.st_atime;
4705 ref = statbuf.st_mtime;
4708 ref = statbuf.st_ctime;
4712 "%s: invalid Y argument: '%c'\n",
4721 xidx = NEWERXY_ATIME;
4724 xidx = NEWERXY_MTIME;
4727 xidx = NEWERXY_CTIME;
4731 xidx = NEWERXY_BTIME;
4735 "%s: invalid X argument: '%c'\n",
4742 newery = ¶m.fp_newery[xidx][negidx];
4748 *newery = *newery > ref ? ref : *newery;
4750 *newery = *newery > ref ? *newery : ref;
4752 param.fp_newerxy = 1;
4757 rc = name2gid(¶m.fp_gid, optarg);
4759 param.fp_gid = strtoul(optarg, &endptr, 10);
4760 if (*endptr != '\0') {
4761 fprintf(stderr, "Group/GID: %s cannot "
4762 "be found.\n", optarg);
4767 param.fp_exclude_gid = !!neg_opt;
4768 param.fp_check_gid = 1;
4771 param.fp_hash_type = check_hashtype(optarg);
4772 if (param.fp_hash_type == 0) {
4773 fprintf(stderr, "error: bad hash_type '%s'\n",
4778 param.fp_check_hash_type = 1;
4779 param.fp_exclude_hash_type = !!neg_opt;
4785 ret = name2layout(¶m.fp_layout, optarg);
4788 param.fp_exclude_layout = !!neg_opt;
4789 param.fp_check_layout = 1;
4793 rc = name2uid(¶m.fp_uid, optarg);
4795 param.fp_uid = strtoul(optarg, &endptr, 10);
4796 if (*endptr != '\0') {
4797 fprintf(stderr, "User/UID: %s cannot "
4798 "be found.\n", optarg);
4803 param.fp_exclude_uid = !!neg_opt;
4804 param.fp_check_uid = 1;
4807 param.fp_pattern = (char *)optarg;
4808 param.fp_exclude_pattern = !!neg_opt;
4811 if (optarg[0] == '+') {
4812 param.fp_mirror_count_sign = -1;
4814 } else if (optarg[0] == '-') {
4815 param.fp_mirror_count_sign = 1;
4819 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4820 if (*endptr != '\0') {
4822 "error: bad mirror count '%s'\n",
4826 param.fp_check_mirror_count = 1;
4827 param.fp_exclude_mirror_count = !!neg_opt;
4832 char *buf, *token, *next, *p;
4836 buf = strdup(optarg);
4842 param.fp_exclude_obd = !!neg_opt;
4845 while (token && *token) {
4846 token = strchr(token, ',');
4853 param.fp_exclude_mdt = !!neg_opt;
4854 param.fp_num_alloc_mdts += len;
4855 tmp = realloc(param.fp_mdt_uuid,
4856 param.fp_num_alloc_mdts *
4857 sizeof(*param.fp_mdt_uuid));
4863 param.fp_mdt_uuid = tmp;
4865 param.fp_exclude_obd = !!neg_opt;
4866 param.fp_num_alloc_obds += len;
4867 tmp = realloc(param.fp_obd_uuid,
4868 param.fp_num_alloc_obds *
4869 sizeof(*param.fp_obd_uuid));
4875 param.fp_obd_uuid = tmp;
4877 for (token = buf; token && *token; token = next) {
4878 struct obd_uuid *puuid;
4881 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4884 ¶m.fp_obd_uuid[param.fp_num_obds++];
4886 p = strchr(token, ',');
4893 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4898 strncpy(puuid->uuid, token,
4899 sizeof(puuid->uuid));
4906 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4910 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4912 "Pool name %s is too long (max %d)\n",
4913 optarg, LOV_MAXPOOLNAME);
4918 * We do check for empty pool because empty pool
4919 * is used to find V1 LOV attributes
4921 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4922 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4923 param.fp_exclude_pool = !!neg_opt;
4924 param.fp_check_pool = 1;
4926 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4927 case 'p': /* want this for --pool, to match getstripe/find */
4929 "warning: -p deprecated, use --print0 or -0\n");
4932 param.fp_zero_end = 1;
4934 case 'P': /* we always print, this option is a no-op */
4936 case LFS_PROJID_OPT:
4937 rc = name2projid(¶m.fp_projid, optarg);
4939 param.fp_projid = strtoul(optarg, &endptr, 10);
4940 if (*endptr != '\0') {
4942 "Invalid project ID: %s",
4948 param.fp_exclude_projid = !!neg_opt;
4949 param.fp_check_projid = 1;
4952 if (optarg[0] == '+') {
4953 param.fp_size_sign = -1;
4955 } else if (optarg[0] == '-') {
4956 param.fp_size_sign = 1;
4960 ret = llapi_parse_size(optarg, ¶m.fp_size,
4961 ¶m.fp_size_units, 0);
4963 fprintf(stderr, "error: bad file size '%s'\n",
4967 param.fp_check_size = 1;
4968 param.fp_exclude_size = !!neg_opt;
4971 if (optarg[0] == '+') {
4972 param.fp_stripe_size_sign = -1;
4974 } else if (optarg[0] == '-') {
4975 param.fp_stripe_size_sign = 1;
4979 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4980 ¶m.fp_stripe_size_units, 0);
4982 fprintf(stderr, "error: bad stripe_size '%s'\n",
4986 param.fp_check_stripe_size = 1;
4987 param.fp_exclude_stripe_size = !!neg_opt;
4990 param.fp_exclude_type = !!neg_opt;
4991 switch (optarg[0]) {
4993 param.fp_type = S_IFBLK;
4996 param.fp_type = S_IFCHR;
4999 param.fp_type = S_IFDIR;
5002 param.fp_type = S_IFREG;
5005 param.fp_type = S_IFLNK;
5008 param.fp_type = S_IFIFO;
5011 param.fp_type = S_IFSOCK;
5014 fprintf(stderr, "error: %s: bad type '%s'\n",
5021 if (optarg[0] == '+') {
5022 param.fp_mdt_count_sign = -1;
5024 } else if (optarg[0] == '-') {
5025 param.fp_mdt_count_sign = 1;
5029 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5030 if (*endptr != '\0') {
5031 fprintf(stderr, "error: bad mdt_count '%s'\n",
5036 param.fp_check_mdt_count = 1;
5037 param.fp_exclude_mdt_count = !!neg_opt;
5040 if (optarg[0] == '+') {
5041 param.fp_ext_size_sign = -1;
5043 } else if (optarg[0] == '-') {
5044 param.fp_ext_size_sign = 1;
5048 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5049 ¶m.fp_ext_size_units, 0);
5051 fprintf(stderr, "error: bad ext-size '%s'\n",
5055 param.fp_ext_size /= SEL_UNIT_SIZE;
5056 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5057 param.fp_check_ext_size = 1;
5058 param.fp_exclude_ext_size = !!neg_opt;
5066 if (pathstart == -1) {
5067 fprintf(stderr, "error: %s: no filename|pathname\n",
5071 } else if (pathend == -1) {
5077 rc = llapi_find(argv[pathstart], ¶m);
5080 pathbad = pathstart;
5082 } while (++pathstart < pathend);
5085 fprintf(stderr, "%s: failed for '%s': %s\n",
5086 progname, argv[pathbad], strerror(-rc));
5089 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5090 free(param.fp_obd_uuid);
5092 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5093 free(param.fp_mdt_uuid);
5098 static int lfs_getstripe_internal(int argc, char **argv,
5099 struct find_param *param)
5101 struct option long_opts[] = {
5102 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5103 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5104 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5105 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5106 { .val = LFS_COMP_COUNT_OPT,
5107 .name = "comp-count", .has_arg = no_argument },
5108 { .val = LFS_COMP_COUNT_OPT,
5109 .name = "component-count", .has_arg = no_argument },
5110 { .val = LFS_COMP_FLAGS_OPT,
5111 .name = "comp-flags", .has_arg = optional_argument },
5112 { .val = LFS_COMP_FLAGS_OPT,
5113 .name = "component-flags", .has_arg = optional_argument },
5114 { .val = LFS_COMP_START_OPT,
5115 .name = "comp-start", .has_arg = optional_argument },
5116 { .val = LFS_COMP_START_OPT,
5117 .name = "component-start", .has_arg = optional_argument },
5118 { .val = LFS_MIRROR_INDEX_OPT,
5119 .name = "mirror-index", .has_arg = required_argument },
5120 { .val = LFS_MIRROR_ID_OPT,
5121 .name = "mirror-id", .has_arg = required_argument },
5122 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5123 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5124 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5125 { .val = 'd', .name = "directory", .has_arg = no_argument },
5126 { .val = 'D', .name = "default", .has_arg = no_argument },
5127 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5128 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5129 { .val = 'F', .name = "fid", .has_arg = no_argument },
5130 { .val = 'g', .name = "generation", .has_arg = no_argument },
5131 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5132 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5133 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5134 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5135 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5136 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5137 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5138 { .val = 'L', .name = "layout", .has_arg = no_argument },
5139 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5140 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5141 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5142 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5143 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5144 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5145 { .val = 'O', .name = "obd", .has_arg = required_argument },
5146 { .val = 'O', .name = "ost", .has_arg = required_argument },
5147 { .val = 'p', .name = "pool", .has_arg = no_argument },
5148 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5149 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5150 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5151 { .val = 'R', .name = "raw", .has_arg = no_argument },
5152 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5153 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5154 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5155 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5156 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5157 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5158 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5159 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5160 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5161 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5165 int pathstart = -1, pathend = -1;
5169 while ((c = getopt_long(argc, argv,
5170 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5171 long_opts, NULL)) != -1) {
5175 /* '!' is part of option */
5176 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5177 if (!isoption && pathend != -1) {
5179 "error: %s: filename|dirname must either precede options or follow options\n",
5183 if (!isoption && pathstart == -1)
5184 pathstart = optind - 1;
5185 if (isoption && pathstart != -1 && pathend == -1)
5186 pathend = optind - 2;
5190 /* unknown: opt is "!" */
5191 if (strcmp(optarg, "!") == 0)
5195 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5196 param->fp_verbose |= VERBOSE_COUNT;
5197 param->fp_max_depth = 0;
5200 case LFS_COMP_COUNT_OPT:
5201 param->fp_verbose |= VERBOSE_COMP_COUNT;
5202 param->fp_max_depth = 0;
5204 case LFS_COMP_FLAGS_OPT:
5205 if (optarg != NULL) {
5206 rc = comp_str2flags(optarg,
5207 ¶m->fp_comp_flags,
5208 ¶m->fp_comp_neg_flags);
5210 fprintf(stderr, "error: %s bad "
5211 "component flags '%s'.\n",
5215 param->fp_check_comp_flags = 1;
5217 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5218 param->fp_max_depth = 0;
5221 case LFS_COMP_START_OPT:
5222 if (optarg != NULL) {
5224 if (tmp[0] == '+') {
5225 param->fp_comp_start_sign = -1;
5227 } else if (tmp[0] == '-') {
5228 param->fp_comp_start_sign = 1;
5231 rc = llapi_parse_size(tmp,
5232 ¶m->fp_comp_start,
5233 ¶m->fp_comp_start_units, 0);
5235 fprintf(stderr, "error: %s bad "
5236 "component start '%s'.\n",
5240 param->fp_check_comp_start = 1;
5243 param->fp_verbose |= VERBOSE_COMP_START;
5244 param->fp_max_depth = 0;
5247 case LFS_MIRROR_INDEX_OPT:
5248 if (optarg[0] == '+') {
5249 param->fp_mirror_index_sign = -1;
5251 } else if (optarg[0] == '-') {
5252 param->fp_mirror_index_sign = 1;
5256 param->fp_mirror_index = strtoul(optarg, &end, 0);
5257 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5258 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5260 "%s %s: invalid mirror index '%s'\n",
5261 progname, argv[0], optarg);
5264 if (param->fp_mirror_id != 0) {
5266 "%s %s: can't specify both mirror index and mirror ID\n",
5270 param->fp_check_mirror_index = 1;
5271 param->fp_exclude_mirror_index = !!neg_opt;
5273 case LFS_MIRROR_ID_OPT:
5274 if (optarg[0] == '+') {
5275 param->fp_mirror_id_sign = -1;
5277 } else if (optarg[0] == '-') {
5278 param->fp_mirror_id_sign = 1;
5282 param->fp_mirror_id = strtoul(optarg, &end, 0);
5283 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5284 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5286 "%s %s: invalid mirror ID '%s'\n",
5287 progname, argv[0], optarg);
5290 if (param->fp_mirror_index != 0) {
5292 "%s %s: can't specify both mirror index and mirror ID\n",
5296 param->fp_check_mirror_id = 1;
5297 param->fp_exclude_mirror_id = !!neg_opt;
5300 param->fp_max_depth = 0;
5303 param->fp_get_default_lmv = 1;
5306 if (optarg != NULL) {
5308 if (tmp[0] == '+') {
5309 param->fp_comp_end_sign = -1;
5311 } else if (tmp[0] == '-') {
5312 param->fp_comp_end_sign = 1;
5316 if (arg_is_eof(tmp)) {
5317 param->fp_comp_end = LUSTRE_EOF;
5318 param->fp_comp_end_units = 1;
5321 rc = llapi_parse_size(tmp,
5322 ¶m->fp_comp_end,
5323 ¶m->fp_comp_end_units, 0);
5326 fprintf(stderr, "error: %s bad "
5327 "component end '%s'.\n",
5331 param->fp_check_comp_end = 1;
5333 param->fp_verbose |= VERBOSE_COMP_END;
5334 param->fp_max_depth = 0;
5338 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5339 param->fp_verbose |= VERBOSE_DFID;
5340 param->fp_max_depth = 0;
5344 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5345 param->fp_verbose |= VERBOSE_GENERATION;
5346 param->fp_max_depth = 0;
5350 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5351 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5352 param->fp_max_depth = 0;
5356 if (optarg != NULL) {
5357 param->fp_comp_id = strtoul(optarg, &end, 0);
5358 if (*end != '\0' || param->fp_comp_id == 0 ||
5359 param->fp_comp_id > LCME_ID_MAX) {
5360 fprintf(stderr, "error: %s bad "
5361 "component id '%s'\n",
5365 param->fp_check_comp_id = 1;
5368 param->fp_max_depth = 0;
5369 param->fp_verbose |= VERBOSE_COMP_ID;
5373 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5374 param->fp_verbose |= VERBOSE_PATTERN;
5375 param->fp_max_depth = 0;
5378 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5380 fprintf(stderr, "warning: '-M' deprecated"
5381 ", use '--mdt-index' or '-m' instead\n");
5384 if (!(param->fp_verbose & VERBOSE_DETAIL))
5385 param->fp_max_depth = 0;
5386 param->fp_verbose |= VERBOSE_MDTINDEX;
5389 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5390 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5391 param->fp_max_depth = 0;
5395 if (param->fp_obd_uuid) {
5397 "error: %s: only one obduuid allowed",
5401 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5404 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5405 param->fp_verbose |= VERBOSE_POOL;
5406 param->fp_max_depth = 0;
5413 param->fp_recursive = 1;
5419 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5420 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5421 param->fp_max_depth = 0;
5425 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5431 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5432 param->fp_verbose |= VERBOSE_EXT_SIZE;
5433 param->fp_max_depth = 0;
5441 if (pathstart == -1) {
5442 fprintf(stderr, "error: %s: no filename|pathname\n",
5445 } else if (pathend == -1) {
5453 if (param->fp_recursive)
5454 param->fp_max_depth = -1;
5455 else if (param->fp_verbose & VERBOSE_DETAIL)
5456 param->fp_max_depth = 1;
5458 if (!param->fp_verbose)
5459 param->fp_verbose = VERBOSE_DEFAULT;
5460 if (param->fp_quiet)
5461 param->fp_verbose = VERBOSE_OBJID;
5464 rc = llapi_getstripe(argv[pathstart], param);
5465 } while (++pathstart < pathend && !rc);
5468 fprintf(stderr, "error: %s failed for %s.\n",
5469 argv[0], argv[optind - 1]);
5473 static int lfs_tgts(int argc, char **argv)
5475 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5476 struct find_param param;
5477 int index = 0, rc=0;
5482 if (argc == 2 && !realpath(argv[1], path)) {
5484 fprintf(stderr, "error: invalid path '%s': %s\n",
5485 argv[1], strerror(-rc));
5489 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5490 /* Check if we have a mount point */
5491 if (mntdir[0] == '\0')
5494 memset(¶m, 0, sizeof(param));
5495 if (!strcmp(argv[0], "mdts"))
5496 param.fp_get_lmv = 1;
5498 rc = llapi_ostlist(mntdir, ¶m);
5500 fprintf(stderr, "error: %s: failed on %s\n",
5503 if (path[0] != '\0')
5505 memset(mntdir, 0, PATH_MAX);
5511 static int lfs_getstripe(int argc, char **argv)
5513 struct find_param param = { 0 };
5515 param.fp_max_depth = 1;
5516 return lfs_getstripe_internal(argc, argv, ¶m);
5520 static int lfs_getdirstripe(int argc, char **argv)
5522 struct find_param param = { 0 };
5523 struct option long_opts[] = {
5524 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5525 { .val = 'D', .name = "default", .has_arg = no_argument },
5526 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5527 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5528 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5529 { .val = 'O', .name = "obd", .has_arg = required_argument },
5530 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5531 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5532 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5533 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5537 param.fp_get_lmv = 1;
5539 while ((c = getopt_long(argc, argv,
5540 "cDHimO:rtTvy", long_opts, NULL)) != -1)
5545 param.fp_verbose |= VERBOSE_COUNT;
5548 param.fp_get_default_lmv = 1;
5552 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5554 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5556 fprintf(stderr, "warning: '-t' deprecated, "
5557 "use '--mdt-hash' or '-H' instead\n");
5560 param.fp_verbose |= VERBOSE_HASH_TYPE;
5563 if (param.fp_obd_uuid) {
5565 "error: %s: only one obduuid allowed",
5569 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5572 param.fp_recursive = 1;
5575 param.fp_verbose |= VERBOSE_DETAIL;
5581 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5582 progname, argv[0], argv[optind - 1]);
5590 if (param.fp_recursive)
5591 param.fp_max_depth = -1;
5593 if (!param.fp_verbose)
5594 param.fp_verbose = VERBOSE_DEFAULT;
5597 rc = llapi_getstripe(argv[optind], ¶m);
5598 } while (++optind < argc && !rc);
5601 fprintf(stderr, "error: %s failed for %s.\n",
5602 argv[0], argv[optind - 1]);
5607 MNTDF_INODES = 0x0001,
5608 MNTDF_COOKED = 0x0002,
5609 MNTDF_LAZY = 0x0004,
5610 MNTDF_VERBOSE = 0x0008,
5611 MNTDF_SHOW = 0x0010,
5614 #define COOK(value) \
5617 while (value > 1024) { \
5625 #define CDF "%11llu"
5626 #define HDF "%8.1f%c"
5630 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5632 double avail, used, ratio = 0;
5635 avail = st->os_ffree;
5636 used = st->os_files - st->os_ffree;
5638 avail = st->os_bavail;
5639 used = st->os_blocks - st->os_bfree;
5641 if (avail + used > 0)
5642 ratio = used / (used + avail) * 100;
5644 /* Round up to match df(1) usage percentage */
5645 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5648 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5649 * so only show flags reflecting those states by default. Informational states
5650 * are only shown with "-v" and use lower-case names to distinguish them.
5651 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5653 static struct obd_statfs_state_names {
5654 enum obd_statfs_state osn_state;
5655 const char osn_name;
5658 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
5659 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
5660 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
5661 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
5662 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
5663 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
5664 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
5665 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
5666 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
5669 static int showdf(char *mntdir, struct obd_statfs *stat,
5670 char *uuid, enum mntdf_flags flags,
5671 char *type, int index, int rc)
5673 long long avail, used, total;
5675 char *suffix = "KMGTPEZY";
5676 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5677 char tbuf[3 * sizeof(__u64)];
5678 char ubuf[3 * sizeof(__u64)];
5679 char abuf[3 * sizeof(__u64)];
5680 char rbuf[3 * sizeof(__u64)];
5687 if (flags & MNTDF_INODES) {
5688 avail = stat->os_ffree;
5689 used = stat->os_files - stat->os_ffree;
5690 total = stat->os_files;
5692 int shift = flags & MNTDF_COOKED ? 0 : 10;
5694 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5695 used = ((stat->os_blocks - stat->os_bfree) *
5696 stat->os_bsize) >> shift;
5697 total = (stat->os_blocks * stat->os_bsize) >> shift;
5700 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5702 if (flags & MNTDF_COOKED) {
5706 cook_val = (double)total;
5709 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5712 snprintf(tbuf, sizeof(tbuf), CDF, total);
5714 cook_val = (double)used;
5717 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5720 snprintf(ubuf, sizeof(ubuf), CDF, used);
5722 cook_val = (double)avail;
5725 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5728 snprintf(abuf, sizeof(abuf), CDF, avail);
5730 snprintf(tbuf, sizeof(tbuf), CDF, total);
5731 snprintf(ubuf, sizeof(tbuf), CDF, used);
5732 snprintf(abuf, sizeof(tbuf), CDF, avail);
5735 sprintf(rbuf, RDF, ratio);
5736 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5737 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5739 printf("[%s:%d]", type, index);
5741 if (stat->os_state) {
5745 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5746 if (oss_names[i].osn_state & stat->os_state &&
5747 (oss_names[i].osn_err ||
5748 flags & MNTDF_VERBOSE))
5749 printf("%c", oss_names[i].osn_name);
5756 printf(UUF": inactive device\n", uuid);
5759 printf(UUF": %s\n", uuid, strerror(-rc));
5766 struct ll_stat_type {
5771 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5773 struct ll_statfs_data {
5775 struct obd_statfs sd_st;
5778 struct ll_statfs_buf {
5780 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5783 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5784 int ops, struct ll_statfs_buf *lsb)
5786 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5787 struct obd_uuid uuid_buf;
5788 char *poolname = NULL;
5789 struct ll_stat_type types[] = {
5790 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5791 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5792 { .st_name = NULL } };
5793 struct ll_stat_type *tp;
5794 __u64 ost_files = 0;
5795 __u64 ost_ffree = 0;
5803 poolname = strchr(pool, '.');
5804 if (poolname != NULL) {
5805 if (strncmp(fsname, pool, strlen(fsname))) {
5806 fprintf(stderr, "filesystem name incorrect\n");
5814 fd = open(mntdir, O_RDONLY);
5817 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5822 if (flags & MNTDF_SHOW) {
5823 if (flags & MNTDF_INODES)
5824 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5825 "UUID", "Inodes", "IUsed", "IFree",
5826 "IUse%", "Mounted on");
5828 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5830 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5831 "Used", "Available", "Use%", "Mounted on");
5834 for (tp = types; tp->st_name != NULL; tp++) {
5835 bool have_ost = false;
5837 if (!(tp->st_op & ops))
5840 for (index = 0; ; index++) {
5841 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5842 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5843 type = flags & MNTDF_LAZY ?
5844 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5845 rc2 = llapi_obd_fstatfs(fd, type, index,
5846 &stat_buf, &uuid_buf);
5851 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5852 if (!(flags & MNTDF_VERBOSE))
5854 } else if (rc2 < 0 && rc == 0) {
5858 /* If we have OSTs then don't report MDT block counts.
5859 * For MDT-only filesystems the expectation is that all
5860 * layouts have a DoM component. For filesystems with
5861 * OSTs, files are not necessarily going to store data
5862 * on MDTs, and MDT space is limited to a fraction of
5863 * OST space, so don't include it in the summary.
5865 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5872 if (poolname && tp->st_op == LL_STATFS_LOV &&
5873 llapi_search_ost(fsname, poolname,
5874 obd_uuid2str(&uuid_buf)) != 1)
5877 /* the llapi_obd_statfs() call may have returned with
5878 * an error, but if it filled in uuid_buf we will at
5879 * lease use that to print out a message for that OBD.
5880 * If we didn't get anything in the uuid_buf, then fill
5881 * it in so that we can print an error message. */
5882 if (uuid_buf.uuid[0] == '\0')
5883 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5884 "%s%04x", tp->st_name, index);
5886 lsb->sb_buf[lsb->sb_count].sd_index = index;
5887 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5890 if (flags & MNTDF_SHOW)
5891 showdf(mntdir, &stat_buf,
5892 obd_uuid2str(&uuid_buf), flags,
5893 tp->st_name, index, rc2);
5898 if (tp->st_op == LL_STATFS_LMV) {
5899 sum.os_ffree += stat_buf.os_ffree;
5900 sum.os_files += stat_buf.os_files;
5901 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5902 ost_files += stat_buf.os_files;
5903 ost_ffree += stat_buf.os_ffree;
5905 sum.os_blocks += stat_buf.os_blocks *
5907 sum.os_bfree += stat_buf.os_bfree *
5909 sum.os_bavail += stat_buf.os_bavail *
5916 /* If we have _some_ OSTs, but don't have as many free objects on the
5917 * OST as inodes on the MDTs, reduce the reported number of inodes
5918 * to compensate, so that the "inodes in use" number is correct.
5919 * This should be kept in sync with ll_statfs_internal().
5921 if (ost_files && ost_ffree < sum.os_ffree) {
5922 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5923 sum.os_ffree = ost_ffree;
5925 if (flags & MNTDF_SHOW) {
5927 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5935 static int lfs_setdirstripe(int argc, char **argv)
5938 struct lfs_setstripe_args lsa = { 0 };
5939 struct llapi_stripe_param *param = NULL;
5940 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5943 char *mode_opt = NULL;
5944 bool default_stripe = false;
5945 bool delete = false;
5946 bool foreign_mode = false;
5947 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5948 mode_t previous_mode = 0;
5950 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5951 struct option long_opts[] = {
5952 { .val = 'c', .name = "count", .has_arg = required_argument },
5953 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5954 { .val = 'd', .name = "delete", .has_arg = no_argument },
5955 { .val = 'D', .name = "default", .has_arg = no_argument },
5956 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5957 { .val = LFS_LAYOUT_FLAGS_OPT,
5958 .name = "flags", .has_arg = required_argument },
5959 { .val = LFS_LAYOUT_FOREIGN_OPT,
5960 .name = "foreign", .has_arg = optional_argument},
5961 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5962 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5963 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5964 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5966 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5967 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5968 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5970 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5971 { .val = 'i', .name = "index", .has_arg = required_argument },
5973 { .val = 'o', .name = "mode", .has_arg = required_argument },
5974 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5975 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5977 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5978 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5979 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5983 setstripe_args_init(&lsa);
5985 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5986 long_opts, NULL)) >= 0) {
5993 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5996 "%s %s: invalid stripe count '%s'\n",
5997 progname, argv[0], optarg);
6003 default_stripe = true;
6006 default_stripe = true;
6008 case LFS_LAYOUT_FOREIGN_OPT:
6009 if (optarg != NULL) {
6010 /* check pure numeric */
6011 type = strtoul(optarg, &end, 0);
6014 type = check_foreign_type_name(optarg);
6015 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6017 "%s %s: unknown foreign type '%s'\n",
6024 foreign_mode = true;
6026 case LFS_LAYOUT_FLAGS_OPT:
6027 flags = strtoul(optarg, &end, 16);
6030 "%s %s: bad flags '%s'\n",
6031 progname, argv[0], optarg);
6035 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6037 fprintf(stderr, "warning: '--hash-type' and '-t' "
6038 "deprecated, use '--mdt-hash' or '-H' instead\n");
6041 lsa.lsa_pattern = check_hashtype(optarg);
6042 if (lsa.lsa_pattern == 0) {
6044 "%s %s: bad stripe hash type '%s'\n",
6045 progname, argv[0], optarg);
6050 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6053 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6054 if (strcmp(argv[optind - 1], "--index") == 0)
6056 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6059 lsa.lsa_nr_tgts = parse_targets(mdts,
6060 sizeof(mdts) / sizeof(__u32),
6061 lsa.lsa_nr_tgts, optarg, NULL);
6062 if (lsa.lsa_nr_tgts < 0) {
6064 "%s %s: invalid MDT target(s) '%s'\n",
6065 progname, argv[0], optarg);
6069 lsa.lsa_tgts = mdts;
6070 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6071 lsa.lsa_stripe_off = mdts[0];
6073 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6075 fprintf(stderr, "warning: '-m' is deprecated, "
6076 "use '--mode' or '-o' instead\n");
6085 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6086 progname, argv[0], argv[optind - 1]);
6091 if (optind == argc) {
6092 fprintf(stderr, "%s %s: DIR must be specified\n",
6097 if (xattr && !foreign_mode) {
6098 /* only print a warning as this is armless and will be
6102 "%s %s: xattr has been specified for non-foreign layout\n",
6104 } else if (foreign_mode && !xattr) {
6106 "%s %s: xattr must be provided in foreign mode\n",
6111 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6112 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6114 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6119 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6120 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6122 "%s %s: stripe offset and count must be specified\n",
6128 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6129 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6131 "%s %s: cannot specify -d with -c or -i options\n",
6136 if (mode_opt != NULL) {
6137 mode = strtoul(mode_opt, &end, 8);
6140 "%s %s: bad MODE '%s'\n",
6141 progname, argv[0], mode_opt);
6144 previous_mode = umask(0);
6147 /* foreign LMV/dir case */
6149 if (argc > optind + 1) {
6151 "%s %s: cannot specify multiple foreign dirs\n",
6156 dname = argv[optind];
6157 result = llapi_dir_create_foreign(dname, mode, type, flags,
6161 "%s mkdir: can't create foreign dir '%s': %s\n",
6162 progname, dname, strerror(-result));
6167 * initialize stripe parameters, in case param is converted to specific,
6168 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6170 param = calloc(1, offsetof(typeof(*param),
6171 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6172 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6173 if (param == NULL) {
6175 "%s %s: cannot allocate memory for parameters: %s\n",
6176 progname, argv[0], strerror(ENOMEM));
6180 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6181 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6182 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6183 lsa.lsa_stripe_count == 0))
6184 lsa.lsa_stripe_count = 1;
6185 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6186 param->lsp_stripe_count = lsa.lsa_stripe_count;
6187 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6188 param->lsp_stripe_offset = -1;
6190 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6191 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6192 param->lsp_stripe_pattern = lsa.lsa_pattern;
6194 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6195 param->lsp_pool = lsa.lsa_pool_name;
6196 param->lsp_is_specific = false;
6197 if (lsa.lsa_nr_tgts > 1) {
6198 if (lsa.lsa_stripe_count > 0 &&
6199 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6200 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6202 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6203 argv[0], lsa.lsa_stripe_count,
6209 param->lsp_is_specific = true;
6210 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6211 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6214 dname = argv[optind];
6216 if (default_stripe) {
6217 result = llapi_dir_set_default_lmv(dname, param);
6220 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6221 progname, dname, strerror(-result));
6225 result = llapi_dir_create(dname, mode, param);
6228 "%s setdirstripe: cannot create dir '%s': %s\n",
6229 progname, dname, strerror(-result));
6230 } while (!result && (dname = argv[++optind]));
6232 if (mode_opt != NULL)
6233 umask(previous_mode);
6240 static int lfs_rmentry(int argc, char **argv)
6247 fprintf(stderr, "error: %s: missing dirname\n",
6253 dname = argv[index];
6254 while (dname != NULL) {
6255 result = llapi_direntry_remove(dname);
6257 fprintf(stderr, "error: %s: remove dir entry '%s' "
6258 "failed\n", argv[0], dname);
6261 dname = argv[++index];
6266 static int lfs_mv(int argc, char **argv)
6268 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6269 struct find_param param = {
6276 struct option long_opts[] = {
6277 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6278 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6279 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6282 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6284 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6286 fprintf(stderr, "warning: '-M' deprecated"
6287 ", use '--mdt-index' or '-m' instead\n");
6290 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6292 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6298 param.fp_verbose = VERBOSE_DETAIL;
6301 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6302 progname, argv[optind - 1]);
6307 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6308 fprintf(stderr, "%s mv: MDT index must be specified\n",
6313 if (optind >= argc) {
6314 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6318 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6320 /* initialize migrate mdt parameters */
6321 param.fp_lmv_md = &lmu;
6322 param.fp_migrate = 1;
6323 rc = llapi_migrate_mdt(argv[optind], ¶m);
6325 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6326 progname, argv[optind], lmu.lum_stripe_offset,
6331 static int lfs_osts(int argc, char **argv)
6333 return lfs_tgts(argc, argv);
6336 static int lfs_mdts(int argc, char **argv)
6338 return lfs_tgts(argc, argv);
6341 static int lfs_df(int argc, char **argv)
6343 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6344 enum mntdf_flags flags = MNTDF_SHOW;
6345 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6346 int c, rc = 0, index = 0;
6347 char fsname[PATH_MAX] = "", *pool_name = NULL;
6348 struct option long_opts[] = {
6349 { .val = 'h', .name = "human-readable",
6350 .has_arg = no_argument },
6351 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6352 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6353 { .val = 'p', .name = "pool", .has_arg = required_argument },
6354 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6357 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6360 flags |= MNTDF_COOKED;
6363 flags |= MNTDF_INODES;
6366 flags |= MNTDF_LAZY;
6372 flags |= MNTDF_VERBOSE;
6378 if (optind < argc && !realpath(argv[optind], path)) {
6380 fprintf(stderr, "error: invalid path '%s': %s\n",
6381 argv[optind], strerror(-rc));
6385 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6386 /* Check if we have a mount point */
6387 if (mntdir[0] == '\0')
6390 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6391 if (rc || path[0] != '\0')
6393 fsname[0] = '\0'; /* avoid matching in next loop */
6394 mntdir[0] = '\0'; /* avoid matching in next loop */
6400 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6401 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6405 if (opt_fsname == opt_instance) { /* both true or both false */
6406 rc = llapi_getname(mntdir, buf, buflen);
6407 } else if (opt_fsname) {
6408 /* llapi_search_mounts() fills @buf with fsname, but that is not
6409 * called if explicit paths are specified on the command-line
6412 rc = llapi_get_fsname(mntdir, buf, buflen);
6413 } else /* if (opt_instance) */ {
6414 rc = llapi_get_instance(mntdir, buf, buflen);
6418 fprintf(stderr, "cannot get instance for '%s': %s\n",
6419 mntdir, strerror(-rc));
6424 printf("%s %s\n", buf, mntdir);
6426 printf("%s\n", buf);
6431 static int lfs_getname(int argc, char **argv)
6433 struct option long_opts[] = {
6434 { .val = 'h', .name = "help", .has_arg = no_argument },
6435 { .val = 'i', .name = "instance", .has_arg = no_argument },
6436 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6438 bool opt_instance = false, opt_fsname = false;
6439 char fsname[PATH_MAX] = "";
6442 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6445 opt_instance = true;
6456 if (optind == argc) { /* no paths specified, get all paths. */
6457 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6460 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6461 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6462 opt_instance, opt_fsname, true);
6465 path[0] = fsname[0] = mntdir[0] = '\0';
6467 } else { /* paths specified, only attempt to search these. */
6470 /* if only one path is given, print only requested info */
6471 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6473 for (; optind < argc; optind++) {
6474 rc2 = print_instance(argv[optind], fsname,
6475 sizeof(fsname), opt_instance,
6476 opt_fsname, opt_mntdir);
6486 static int lfs_check(int argc, char **argv)
6488 char mntdir[PATH_MAX] = {'\0'};
6497 fprintf(stderr, "%s check: server type must be specified\n",
6502 obd_types[0] = obd_type1;
6503 obd_types[1] = obd_type2;
6504 obd_types[2] = obd_type3;
6506 if (strcmp(argv[1], "osts") == 0) {
6507 strcpy(obd_types[0], "osc");
6508 } else if (strcmp(argv[1], "mdts") == 0 ||
6509 strcmp(argv[1], "mds") == 0) {
6510 strcpy(obd_types[0], "mdc");
6511 } else if (strcmp(argv[1], "mgts") == 0) {
6512 strcpy(obd_types[0], "mgc");
6513 } else if (strcmp(argv[1], "all") == 0 ||
6514 strcmp(argv[1], "servers") == 0) {
6516 strcpy(obd_types[0], "osc");
6517 strcpy(obd_types[1], "mdc");
6518 strcpy(obd_types[2], "mgc");
6520 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6525 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6526 if (rc < 0 || mntdir[0] == '\0') {
6528 "%s check: cannot find mounted Lustre filesystem: %s\n",
6529 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6533 rc = llapi_target_check(num_types, obd_types, mntdir);
6535 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6536 progname, argv[1], strerror(-rc));
6542 #ifdef HAVE_SYS_QUOTA_H
6543 #define ARG2INT(nr, str, msg) \
6546 nr = strtol(str, &endp, 0); \
6547 if (*endp != '\0') { \
6548 fprintf(stderr, "%s: bad %s '%s'\n", \
6549 progname, msg, str); \
6554 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6556 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6557 * returns the value or ULONG_MAX on integer overflow or incorrect format
6559 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6560 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6561 * 3. empty integer value is interpreted as 0
6563 static unsigned long str2sec(const char* timestr)
6565 const char spec[] = "smhdw";
6566 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6567 unsigned long val = 0;
6570 if (strpbrk(timestr, spec) == NULL) {
6571 /* no specifiers inside the time string,
6572 should treat it as an integer value */
6573 val = strtoul(timestr, &tail, 10);
6574 return *tail ? ULONG_MAX : val;
6577 /* format string is XXwXXdXXhXXmXXs */
6583 v = strtoul(timestr, &tail, 10);
6584 if (v == ULONG_MAX || *tail == '\0')
6585 /* value too large (ULONG_MAX or more)
6586 or missing specifier */
6589 ptr = strchr(spec, *tail);
6591 /* unknown specifier */
6596 /* check if product will overflow the type */
6597 if (!(v < ULONG_MAX / mult[ind]))
6600 ADD_OVERFLOW(val, mult[ind] * v);
6601 if (val == ULONG_MAX)
6613 #define ARG2ULL(nr, str, def_units) \
6615 unsigned long long limit, units = def_units; \
6618 rc = llapi_parse_size(str, &limit, &units, 1); \
6620 fprintf(stderr, "%s: invalid limit '%s'\n", \
6627 static inline int has_times_option(int argc, char **argv)
6631 for (i = 1; i < argc; i++)
6632 if (!strcmp(argv[i], "-t"))
6638 static inline int lfs_verify_poolarg(char *pool)
6640 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
6642 "Pool name '%.*s' is longer than %d\n",
6643 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
6649 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
6652 char *mnt, *obd_type = (char *)qctl->obd_type;
6653 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6654 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
6655 struct option long_opts[] = {
6656 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6657 { .val = 'g', .name = "group", .has_arg = no_argument },
6658 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6659 { .val = 'p', .name = "projid", .has_arg = no_argument },
6660 { .val = 't', .name = "times", .has_arg = no_argument },
6661 { .val = 'u', .name = "user", .has_arg = no_argument },
6662 { .val = 'o', .name = "pool", .has_arg = required_argument },
6666 qctl->qc_cmd = LUSTRE_Q_SETINFO;
6667 qctl->qc_type = ALLQUOTA;
6669 while ((c = getopt_long(argc, argv, "b:gi:ptuo:",
6670 long_opts, NULL)) != -1) {
6681 if (qctl->qc_type != ALLQUOTA) {
6682 fprintf(stderr, "error: -u/g/p can't be used "
6683 "more than once\n");
6686 qctl->qc_type = qtype;
6689 if (strncmp(optarg, NOTIFY_GRACE,
6690 strlen(NOTIFY_GRACE)) == 0) {
6691 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6693 dqi->dqi_bgrace = str2sec(optarg);
6694 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6695 fprintf(stderr, "error: bad "
6696 "block-grace: %s\n", optarg);
6700 dqb->dqb_valid |= QIF_BTIME;
6703 if (strncmp(optarg, NOTIFY_GRACE,
6704 strlen(NOTIFY_GRACE)) == 0) {
6705 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6707 dqi->dqi_igrace = str2sec(optarg);
6708 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6709 fprintf(stderr, "error: bad "
6710 "inode-grace: %s\n", optarg);
6714 dqb->dqb_valid |= QIF_ITIME;
6716 case 't': /* Yes, of course! */
6719 if (lfs_verify_poolarg(optarg))
6722 "Trying to set grace for pool %s\n", optarg);
6723 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
6724 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
6726 /* getopt prints error message for us when opterr != 0 */
6732 if (qctl->qc_type == ALLQUOTA) {
6733 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6737 if (optind != argc - 1) {
6738 fprintf(stderr, "error: unexpected parameters encountered\n");
6743 rc = llapi_quotactl(mnt, qctl);
6746 fprintf(stderr, "%s %s ", obd_type,
6747 obd_uuid2str(&qctl->obd_uuid));
6748 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6755 #define BSLIMIT (1 << 0)
6756 #define BHLIMIT (1 << 1)
6757 #define ISLIMIT (1 << 2)
6758 #define IHLIMIT (1 << 3)
6760 int lfs_setquota(int argc, char **argv)
6763 struct if_quotactl *qctl;
6764 char *mnt, *obd_type;
6765 struct obd_dqblk *dqb;
6766 struct option long_opts[] = {
6767 { .val = 'b', .name = "block-softlimit",
6768 .has_arg = required_argument },
6769 { .val = 'B', .name = "block-hardlimit",
6770 .has_arg = required_argument },
6771 { .val = 'd', .name = "default", .has_arg = no_argument },
6772 { .val = 'g', .name = "group", .has_arg = required_argument },
6773 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6774 { .val = 'i', .name = "inode-softlimit",
6775 .has_arg = required_argument },
6776 { .val = 'I', .name = "inode-hardlimit",
6777 .has_arg = required_argument },
6778 { .val = 'p', .name = "projid", .has_arg = required_argument },
6779 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6780 { .val = 'u', .name = "user", .has_arg = required_argument },
6781 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6782 { .val = 'o', .name = "pool", .has_arg = required_argument },
6784 unsigned limit_mask = 0;
6786 bool use_default = false;
6787 int qtype, qctl_len;
6789 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
6790 qctl = malloc(qctl_len);
6794 memset(qctl, 0, qctl_len);
6795 obd_type = (char *)qctl->obd_type;
6796 dqb = &qctl->qc_dqblk;
6798 if (has_times_option(argc, argv)) {
6799 rc = lfs_setquota_times(argc, argv, qctl);
6803 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
6804 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6805 * so it can be used as a marker that qc_type
6806 * isn't reinitialized from command line */
6808 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:Uo:",
6809 long_opts, NULL)) != -1) {
6812 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6815 goto quota_type_def;
6818 rc = name2uid(&qctl->qc_id, optarg);
6821 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6824 goto quota_type_def;
6827 rc = name2gid(&qctl->qc_id, optarg);
6830 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6833 goto quota_type_def;
6836 rc = name2projid(&qctl->qc_id, optarg);
6839 qctl->qc_id = strtoul(optarg, &endptr, 10);
6840 if (*endptr != '\0') {
6841 fprintf(stderr, "%s setquota: invalid"
6842 " id '%s'\n", progname, optarg);
6848 if (qctl->qc_id == 0) {
6849 fprintf(stderr, "%s setquota: can't set quota"
6850 " for root usr/group/project.\n",
6857 if (qctl->qc_type != ALLQUOTA) {
6859 "%s setquota: only one of -u, -U, -g,"
6860 " -G, -p or -P may be specified\n",
6865 qctl->qc_type = qtype;
6868 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
6872 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6873 dqb->dqb_bsoftlimit >>= 10;
6874 limit_mask |= BSLIMIT;
6875 if (dqb->dqb_bsoftlimit &&
6876 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6878 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6879 "See '%s help setquota' or Lustre manual for details\n",
6881 (unsigned long long)dqb->dqb_bsoftlimit,
6885 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6886 dqb->dqb_bhardlimit >>= 10;
6887 limit_mask |= BHLIMIT;
6888 if (dqb->dqb_bhardlimit &&
6889 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6891 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6892 "See '%s help setquota' or Lustre manual for details\n",
6894 (unsigned long long)dqb->dqb_bhardlimit,
6898 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6899 limit_mask |= ISLIMIT;
6900 if (dqb->dqb_isoftlimit &&
6901 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6903 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6904 "See '%s help setquota' or Lustre manual for details\n",
6906 (unsigned long long)dqb->dqb_isoftlimit,
6910 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6911 limit_mask |= IHLIMIT;
6912 if (dqb->dqb_ihardlimit &&
6913 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6915 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6916 "See '%s help setquota' or Lustre manual for details\n",
6918 (unsigned long long)dqb->dqb_ihardlimit,
6922 if (lfs_verify_poolarg(optarg)) {
6927 "Trying to set quota for pool %s\n", optarg);
6928 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
6929 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
6933 "%s setquota: unrecognized option '%s'\n",
6934 progname, argv[optind - 1]);
6940 if (qctl->qc_type == ALLQUOTA) {
6942 "%s setquota: either -u or -g must be specified\n",
6948 if (!use_default && limit_mask == 0) {
6950 "%s setquota: at least one limit must be specified\n",
6956 if (use_default && limit_mask != 0) {
6958 "%s setquota: limits should not be specified when"
6959 " using default quota\n",
6965 if (use_default && qctl->qc_id == 0) {
6967 "%s setquota: can not set default quota for root"
6968 " user/group/project\n",
6974 if (optind != argc - 1) {
6976 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6977 progname, argv[optind]);
6985 dqb->dqb_bhardlimit = 0;
6986 dqb->dqb_bsoftlimit = 0;
6987 dqb->dqb_ihardlimit = 0;
6988 dqb->dqb_isoftlimit = 0;
6991 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6992 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6993 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6994 /* sigh, we can't just set blimits/ilimits */
6995 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6996 .qc_type = qctl->qc_type,
6997 .qc_id = qctl->qc_id};
6999 rc = llapi_quotactl(mnt, &tmp_qctl);
7003 if (!(limit_mask & BHLIMIT))
7004 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7005 if (!(limit_mask & BSLIMIT))
7006 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7007 if (!(limit_mask & IHLIMIT))
7008 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7009 if (!(limit_mask & ISLIMIT))
7010 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7012 /* Keep grace times if we have got no softlimit arguments */
7013 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7014 dqb->dqb_valid |= QIF_BTIME;
7015 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7018 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7019 dqb->dqb_valid |= QIF_ITIME;
7020 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7024 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7025 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7027 rc = llapi_quotactl(mnt, qctl);
7031 "%s setquota: cannot quotactl '%s' '%s': %s",
7033 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7040 /* Converts seconds value into format string
7041 * result is returned in buf
7043 * 1. result is in descenting order: 1w2d3h4m5s
7044 * 2. zero fields are not filled (except for p. 3): 5d1s
7045 * 3. zero seconds value is presented as "0s"
7047 static char * __sec2str(time_t seconds, char *buf)
7049 const char spec[] = "smhdw";
7050 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7055 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7056 c = seconds / mult[i];
7058 if (c > 0 || (i == 0 && buf == tail))
7059 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7068 static void sec2str(time_t seconds, char *buf, int rc)
7075 tail = __sec2str(seconds, tail);
7077 if (rc && tail - buf < 39) {
7083 static void diff2str(time_t seconds, char *buf, time_t now)
7089 if (seconds <= now) {
7090 strcpy(buf, "none");
7093 __sec2str(seconds - now, buf);
7096 static void print_quota_title(char *name, struct if_quotactl *qctl,
7097 bool human_readable, bool show_default)
7100 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7101 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7102 "Filesystem", "bquota", "blimit", "bgrace",
7103 "iquota", "ilimit", "igrace");
7105 printf("Disk quotas for %s %s (%cid %u):\n",
7106 qtype_name(qctl->qc_type), name,
7107 *qtype_name(qctl->qc_type), qctl->qc_id);
7108 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7109 "Filesystem", human_readable ? "used" : "kbytes",
7110 "quota", "limit", "grace",
7111 "files", "quota", "limit", "grace");
7115 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7118 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7121 snprintf(buf, buflen, "%5.4gP",
7122 (double)num / ((__u64)1 << 40));
7124 snprintf(buf, buflen, "%5.4gT",
7125 (double)num / (1 << 30));
7127 snprintf(buf, buflen, "%5.4gG",
7128 (double)num / (1 << 20));
7130 snprintf(buf, buflen, "%5.4gM",
7131 (double)num / (1 << 10));
7133 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7137 #define STRBUF_LEN 32
7138 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7139 int rc, bool h, bool show_default)
7145 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7146 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7147 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7148 int bover = 0, iover = 0;
7149 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7150 char numbuf[3][STRBUF_LEN];
7152 char strbuf[STRBUF_LEN];
7154 if (dqb->dqb_bhardlimit &&
7155 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7157 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7158 if (dqb->dqb_btime > now) {
7165 if (dqb->dqb_ihardlimit &&
7166 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7168 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7169 if (dqb->dqb_itime > now) {
7177 if (strlen(mnt) > 15)
7178 printf("%s\n%15s", mnt, "");
7180 printf("%15s", mnt);
7183 diff2str(dqb->dqb_btime, timebuf, now);
7184 else if (show_default)
7185 snprintf(timebuf, sizeof(timebuf), "%llu",
7186 (unsigned long long)dqb->dqb_btime);
7188 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7189 strbuf, sizeof(strbuf), h);
7190 if (rc == -EREMOTEIO)
7191 sprintf(numbuf[0], "%s*", strbuf);
7193 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7194 "%s" : "[%s]", strbuf);
7196 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7197 if (type == QC_GENERAL)
7198 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7199 "%s" : "[%s]", strbuf);
7201 sprintf(numbuf[1], "%s", "-");
7203 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7204 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7205 "%s" : "[%s]", strbuf);
7208 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7210 printf(" %7s%c %6s %7s %7s",
7211 numbuf[0], bover ? '*' : ' ', numbuf[1],
7212 numbuf[2], bover > 1 ? timebuf : "-");
7216 diff2str(dqb->dqb_itime, timebuf, now);
7217 else if (show_default)
7218 snprintf(timebuf, sizeof(timebuf), "%llu",
7219 (unsigned long long)dqb->dqb_itime);
7221 snprintf(numbuf[0], sizeof(numbuf),
7222 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7223 (uintmax_t)dqb->dqb_curinodes);
7225 if (type == QC_GENERAL)
7226 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7228 (uintmax_t)dqb->dqb_isoftlimit);
7230 sprintf(numbuf[1], "%s", "-");
7232 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7233 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7236 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7237 else if (type != QC_OSTIDX)
7238 printf(" %7s%c %6s %7s %7s",
7239 numbuf[0], iover ? '*' : ' ', numbuf[1],
7240 numbuf[2], iover > 1 ? timebuf : "-");
7242 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7244 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7245 qctl->qc_cmd == Q_GETOINFO) {
7249 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7250 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7252 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7253 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7254 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7256 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7258 printf("Block grace time: %s; Inode grace time: %s\n",
7259 bgtimebuf, igtimebuf);
7263 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7264 bool h, __u64 *total)
7266 int rc = 0, rc1 = 0, count = 0;
7267 __u32 valid = qctl->qc_valid;
7269 /* TODO: for commands LUSTRE_Q_"S\|G"ETQUOTAPOOL we need
7270 * to go only through OSTs that belong to requested pool. */
7271 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7273 fprintf(stderr, "can not get %s count: %s\n",
7274 is_mdt ? "mdt": "ost", strerror(-rc));
7278 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
7279 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7280 rc = llapi_quotactl(mnt, qctl);
7282 /* It is remote client case. */
7283 if (rc == -EOPNOTSUPP) {
7290 fprintf(stderr, "quotactl %s%d failed.\n",
7291 is_mdt ? "mdt": "ost", qctl->qc_idx);
7295 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7296 qctl->qc_valid, 0, h, false);
7297 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7298 qctl->qc_dqblk.dqb_bhardlimit;
7301 qctl->qc_valid = valid;
7305 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7306 int verbose, int quiet, bool human_readable,
7309 int rc1 = 0, rc2 = 0, rc3 = 0;
7310 char *obd_type = (char *)qctl->obd_type;
7311 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7312 __u64 total_ialloc = 0, total_balloc = 0;
7313 bool use_default_for_blk = false;
7314 bool use_default_for_file = false;
7317 rc1 = llapi_quotactl(mnt, qctl);
7321 fprintf(stderr, "%s quotas are not enabled.\n",
7322 qtype_name(qctl->qc_type));
7325 fprintf(stderr, "Permission denied.\n");
7328 /* We already got error message. */
7331 fprintf(stderr, "Unexpected quotactl error: %s\n",
7336 if (!show_default && qctl->qc_id == 0) {
7337 qctl->qc_dqblk.dqb_bhardlimit = 0;
7338 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7339 qctl->qc_dqblk.dqb_ihardlimit = 0;
7340 qctl->qc_dqblk.dqb_isoftlimit = 0;
7341 qctl->qc_dqblk.dqb_btime = 0;
7342 qctl->qc_dqblk.dqb_itime = 0;
7343 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7346 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7347 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7348 use_default_for_blk = true;
7349 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7352 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7353 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7354 use_default_for_file = true;
7355 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7358 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7359 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7360 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7361 print_quota_title(name, qctl, human_readable, show_default);
7363 if (rc1 && *obd_type)
7364 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7366 if (qctl->qc_valid != QC_GENERAL)
7369 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7370 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7371 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7372 (QIF_LIMITS|QIF_USAGE));
7374 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7376 if (!show_default && verbose &&
7377 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7378 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7379 char strbuf[STRBUF_LEN];
7381 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7383 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7385 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7387 printf("Total allocated inode limit: %ju, total "
7388 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
7392 if (use_default_for_blk)
7393 printf("%cid %u is using default block quota setting\n",
7394 *qtype_name(qctl->qc_type), qctl->qc_id);
7396 if (use_default_for_file)
7397 printf("%cid %u is using default file quota setting\n",
7398 *qtype_name(qctl->qc_type), qctl->qc_id);
7400 if (rc1 || rc2 || rc3 || inacc)
7401 printf("Some errors happened when getting quota info. "
7402 "Some devices may be not working or deactivated. "
7403 "The data in \"[]\" is inaccurate.\n");
7417 static int lfs_project(int argc, char **argv)
7419 int ret = 0, err = 0, c, i;
7420 struct project_handle_control phc = { 0 };
7421 enum lfs_project_ops_t op;
7424 phc.assign_projid = false;
7425 /* default action */
7426 op = LFS_PROJECT_LIST;
7428 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7431 if (op != LFS_PROJECT_LIST) {
7433 "%s: cannot specify '-c' '-C' '-s' together\n",
7438 op = LFS_PROJECT_CHECK;
7441 if (op != LFS_PROJECT_LIST) {
7443 "%s: cannot specify '-c' '-C' '-s' together\n",
7448 op = LFS_PROJECT_CLEAR;
7451 if (op != LFS_PROJECT_LIST) {
7453 "%s: cannot specify '-c' '-C' '-s' together\n",
7458 phc.set_inherit = true;
7459 op = LFS_PROJECT_SET;
7465 phc.keep_projid = true;
7468 phc.recursive = true;
7471 phc.projid = strtoul(optarg, NULL, 0);
7472 phc.assign_projid = true;
7476 phc.newline = false;
7479 fprintf(stderr, "%s: invalid option '%c'\n",
7485 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7486 op = LFS_PROJECT_SET;
7487 phc.set_projid = true;
7488 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7489 phc.set_projid = true;
7493 case LFS_PROJECT_CHECK:
7494 if (phc.keep_projid) {
7496 "%s: '-k' is useless together with '-c'\n",
7501 case LFS_PROJECT_CLEAR:
7504 "%s: '-0' is useless together with '-C'\n",
7508 if (phc.assign_projid) {
7510 "%s: '-p' is useless together with '-C'\n",
7515 case LFS_PROJECT_SET:
7518 "%s: '-0' is useless together with '-s'\n",
7522 if (phc.keep_projid) {
7524 "%s: '-k' is useless together with '-s'\n",
7532 "%s: '-0' is useless for list operations\n",
7542 fprintf(stderr, "%s: missing file or directory target(s)\n",
7547 for (i = 0; i < argc; i++) {
7549 case LFS_PROJECT_CHECK:
7550 err = lfs_project_check(argv[i], &phc);
7552 case LFS_PROJECT_LIST:
7553 err = lfs_project_list(argv[i], &phc);
7555 case LFS_PROJECT_CLEAR:
7556 err = lfs_project_clear(argv[i], &phc);
7558 case LFS_PROJECT_SET:
7559 err = lfs_project_set(argv[i], &phc);
7571 static int lfs_quota(int argc, char **argv)
7574 char *mnt, *name = NULL;
7575 struct if_quotactl *qctl;
7577 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7579 __u32 valid = QC_GENERAL, idx = 0;
7580 bool human_readable = false;
7581 bool show_default = false;
7583 struct option long_opts[] = {
7584 { .val = 1, .name = "pool", .has_arg = required_argument },
7587 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7591 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7592 qctl->qc_type = ALLQUOTA;
7593 obd_uuid = (char *)qctl->obd_uuid.uuid;
7595 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
7596 long_opts, NULL)) != -1) {
7599 show_default = true;
7604 show_default = true;
7609 show_default = true;
7613 if (qctl->qc_type != ALLQUOTA) {
7615 "%s quota: only one of -u, -g, or -p may be specified\n",
7620 qctl->qc_type = qtype;
7623 qctl->qc_cmd = LUSTRE_Q_GETINFO;
7626 valid = qctl->qc_valid = QC_UUID;
7627 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
7630 valid = qctl->qc_valid = QC_MDTIDX;
7631 idx = qctl->qc_idx = atoi(optarg);
7632 if (idx == 0 && *optarg != '0') {
7634 "%s quota: invalid MDT index '%s'\n",
7641 valid = qctl->qc_valid = QC_OSTIDX;
7642 idx = qctl->qc_idx = atoi(optarg);
7643 if (idx == 0 && *optarg != '0') {
7645 "%s quota: invalid OST index '%s'\n",
7658 human_readable = true;
7661 if (lfs_verify_poolarg(optarg)) {
7665 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7666 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
7667 LUSTRE_Q_GETINFOPOOL :
7668 LUSTRE_Q_GETQUOTAPOOL;
7671 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7672 progname, argv[optind - 1]);
7678 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7679 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7680 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7681 qctl->qc_type == ALLQUOTA &&
7682 optind == argc - 1 && !show_default) {
7686 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7687 qctl->qc_type = qtype;
7688 qctl->qc_valid = valid;
7689 if (qtype == USRQUOTA) {
7690 qctl->qc_id = geteuid();
7691 rc = uid2name(&name, qctl->qc_id);
7693 qctl->qc_id = getegid();
7694 rc = gid2name(&name, qctl->qc_id);
7695 memset(&qctl->qc_dqblk, 0,
7696 sizeof(qctl->qc_dqblk));
7701 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
7702 human_readable, show_default);
7707 /* lfs quota -u username /path/to/lustre/mount */
7708 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7709 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7710 /* options should be followed by u/g-name and mntpoint */
7711 if ((!show_default && optind + 2 != argc) ||
7712 (show_default && optind + 1 != argc) ||
7713 qctl->qc_type == ALLQUOTA) {
7715 "%s quota: name and mount point must be specified\n",
7721 if (!show_default) {
7722 name = argv[optind++];
7723 switch (qctl->qc_type) {
7725 rc = name2uid(&qctl->qc_id, name);
7728 rc = name2gid(&qctl->qc_id, name);
7731 rc = name2projid(&qctl->qc_id, name);
7738 qctl->qc_valid = QC_GENERAL;
7739 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
7744 qctl->qc_id = strtoul(name, &endptr, 10);
7745 if (*endptr != '\0') {
7746 fprintf(stderr, "%s quota: invalid id '%s'\n",
7752 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
7753 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7760 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
7761 human_readable, show_default);
7766 #endif /* HAVE_SYS_QUOTA_H! */
7768 static int flushctx_ioctl(char *mp)
7772 fd = open(mp, O_RDONLY);
7774 fprintf(stderr, "flushctx: error open %s: %s\n",
7775 mp, strerror(errno));
7779 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7781 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7782 mp, strerror(errno));
7788 static int lfs_flushctx(int argc, char **argv)
7790 int kdestroy = 0, c;
7791 char mntdir[PATH_MAX] = {'\0'};
7795 while ((c = getopt(argc, argv, "k")) != -1) {
7801 fprintf(stderr, "error: %s: option '-%c' "
7802 "unrecognized\n", argv[0], c);
7808 if ((rc = system("kdestroy > /dev/null")) != 0) {
7809 rc = WEXITSTATUS(rc);
7810 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7814 if (optind >= argc) {
7815 /* flush for all mounted lustre fs. */
7816 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7817 /* Check if we have a mount point */
7818 if (mntdir[0] == '\0')
7821 if (flushctx_ioctl(mntdir))
7824 mntdir[0] = '\0'; /* avoid matching in next loop */
7827 /* flush fs as specified */
7828 while (optind < argc) {
7829 if (flushctx_ioctl(argv[optind++]))
7836 static int lfs_changelog(int argc, char **argv)
7838 void *changelog_priv;
7839 struct changelog_rec *rec;
7840 long long startrec = 0, endrec = 0;
7842 struct option long_opts[] = {
7843 { .val = 'f', .name = "follow", .has_arg = no_argument },
7845 char short_opts[] = "f";
7848 while ((rc = getopt_long(argc, argv, short_opts,
7849 long_opts, NULL)) != -1) {
7856 "%s changelog: unrecognized option '%s'\n",
7857 progname, argv[optind - 1]);
7861 if (optind >= argc) {
7862 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7867 mdd = argv[optind++];
7869 startrec = strtoll(argv[optind++], NULL, 10);
7871 endrec = strtoll(argv[optind++], NULL, 10);
7873 rc = llapi_changelog_start(&changelog_priv,
7874 CHANGELOG_FLAG_BLOCK |
7875 CHANGELOG_FLAG_JOBID |
7876 CHANGELOG_FLAG_EXTRA_FLAGS |
7877 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7880 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7881 progname, strerror(errno = -rc));
7885 rc = llapi_changelog_set_xflags(changelog_priv,
7886 CHANGELOG_EXTRA_FLAG_UIDGID |
7887 CHANGELOG_EXTRA_FLAG_NID |
7888 CHANGELOG_EXTRA_FLAG_OMODE |
7889 CHANGELOG_EXTRA_FLAG_XATTR);
7892 "%s changelog: cannot set xflags for changelog: %s\n",
7893 progname, strerror(errno = -rc));
7897 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7901 if (endrec && rec->cr_index > endrec) {
7902 llapi_changelog_free(&rec);
7905 if (rec->cr_index < startrec) {
7906 llapi_changelog_free(&rec);
7910 secs = rec->cr_time >> 30;
7911 gmtime_r(&secs, &ts);
7912 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7913 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7914 changelog_type2str(rec->cr_type),
7915 ts.tm_hour, ts.tm_min, ts.tm_sec,
7916 (int)(rec->cr_time & ((1 << 30) - 1)),
7917 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7918 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7920 if (rec->cr_flags & CLF_JOBID) {
7921 struct changelog_ext_jobid *jid =
7922 changelog_rec_jobid(rec);
7924 if (jid->cr_jobid[0] != '\0')
7925 printf(" j=%s", jid->cr_jobid);
7928 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7929 struct changelog_ext_extra_flags *ef =
7930 changelog_rec_extra_flags(rec);
7932 printf(" ef=0x%llx",
7933 (unsigned long long)ef->cr_extra_flags);
7935 if (ef->cr_extra_flags & CLFE_UIDGID) {
7936 struct changelog_ext_uidgid *uidgid =
7937 changelog_rec_uidgid(rec);
7939 printf(" u=%llu:%llu",
7940 (unsigned long long)uidgid->cr_uid,
7941 (unsigned long long)uidgid->cr_gid);
7943 if (ef->cr_extra_flags & CLFE_NID) {
7944 struct changelog_ext_nid *nid =
7945 changelog_rec_nid(rec);
7948 libcfs_nid2str(nid->cr_nid));
7951 if (ef->cr_extra_flags & CLFE_OPEN) {
7952 struct changelog_ext_openmode *omd =
7953 changelog_rec_openmode(rec);
7954 char mode[] = "---";
7956 /* exec mode must be exclusive */
7957 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7960 if (omd->cr_openflags & MDS_FMODE_READ)
7962 if (omd->cr_openflags &
7969 if (strcmp(mode, "---") != 0)
7970 printf(" m=%s", mode);
7974 if (ef->cr_extra_flags & CLFE_XATTR) {
7975 struct changelog_ext_xattr *xattr =
7976 changelog_rec_xattr(rec);
7978 if (xattr->cr_xattr[0] != '\0')
7979 printf(" x=%s", xattr->cr_xattr);
7983 if (rec->cr_namelen)
7984 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7985 rec->cr_namelen, changelog_rec_name(rec));
7987 if (rec->cr_flags & CLF_RENAME) {
7988 struct changelog_ext_rename *rnm =
7989 changelog_rec_rename(rec);
7991 if (!fid_is_zero(&rnm->cr_sfid))
7992 printf(" s="DFID" sp="DFID" %.*s",
7993 PFID(&rnm->cr_sfid),
7994 PFID(&rnm->cr_spfid),
7995 (int)changelog_rec_snamelen(rec),
7996 changelog_rec_sname(rec));
8000 llapi_changelog_free(&rec);
8003 llapi_changelog_fini(&changelog_priv);
8006 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8007 progname, strerror(errno = -rc));
8009 return (rc == 1 ? 0 : rc);
8012 static int lfs_changelog_clear(int argc, char **argv)
8020 endrec = strtoll(argv[3], NULL, 10);
8022 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8025 fprintf(stderr, "%s: record out of range: %llu\n",
8027 else if (rc == -ENOENT)
8028 fprintf(stderr, "%s: no changelog user: %s\n",
8031 fprintf(stderr, "%s error: %s\n", argv[0],
8040 static int lfs_fid2path(int argc, char **argv)
8042 struct option long_opts[] = {
8043 { .val = 'c', .name = "cur", .has_arg = no_argument },
8044 { .val = 'l', .name = "link", .has_arg = required_argument },
8045 { .val = 'r', .name = "rec", .has_arg = required_argument },
8047 char short_opts[] = "cl:r:";
8048 char *device, *fid, *path;
8049 long long recno = -1;
8054 char *endptr = NULL;
8056 while ((rc = getopt_long(argc, argv, short_opts,
8057 long_opts, NULL)) != -1) {
8063 linkno = strtol(optarg, &endptr, 10);
8064 if (*endptr != '\0') {
8066 "%s fid2path: invalid linkno '%s'\n",
8072 recno = strtoll(optarg, &endptr, 10);
8073 if (*endptr != '\0') {
8075 "%s fid2path: invalid recno '%s'\n",
8082 "%s fid2path: unrecognized option '%s'\n",
8083 progname, argv[optind - 1]);
8090 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
8095 device = argv[optind++];
8096 path = calloc(1, PATH_MAX);
8100 "%s fid2path: cannot allocate memory for path: %s\n",
8101 progname, strerror(-rc));
8106 while (optind < argc) {
8107 fid = argv[optind++];
8109 lnktmp = (linkno >= 0) ? linkno : 0;
8111 int oldtmp = lnktmp;
8112 long long rectmp = recno;
8114 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
8118 "%s fid2path: cannot find '%s': %s\n",
8119 progname, fid, strerror(errno = -rc2));
8126 fprintf(stdout, "%lld ", rectmp);
8127 if (device[0] == '/') {
8128 fprintf(stdout, "%s", device);
8129 if (device[strlen(device) - 1] != '/')
8130 fprintf(stdout, "/");
8131 } else if (path[0] == '\0') {
8132 fprintf(stdout, "/");
8134 fprintf(stdout, "%s\n", path);
8137 /* specified linkno */
8139 if (oldtmp == lnktmp)
8149 static int lfs_path2fid(int argc, char **argv)
8151 struct option long_opts[] = {
8152 { .val = 'p', .name = "parents", .has_arg = no_argument },
8155 const char short_opts[] = "p";
8156 const char *sep = "";
8159 bool show_parents = false;
8161 while ((rc = getopt_long(argc, argv, short_opts,
8162 long_opts, NULL)) != -1) {
8165 show_parents = true;
8169 "%s path2fid: unrecognized option '%s'\n",
8170 progname, argv[optind - 1]);
8175 if (optind > argc - 1) {
8176 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8180 else if (optind < argc - 1)
8184 for (path = argv + optind; *path != NULL; path++) {
8186 if (!show_parents) {
8187 err = llapi_path2fid(*path, &fid);
8189 printf("%s%s"DFID"\n",
8190 *sep != '\0' ? *path : "", sep,
8193 char name[NAME_MAX + 1];
8194 unsigned int linkno = 0;
8196 while ((err = llapi_path2parent(*path, linkno, &fid,
8197 name, sizeof(name))) == 0) {
8198 if (*sep != '\0' && linkno == 0)
8199 printf("%s%s", *path, sep);
8201 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8206 /* err == -ENODATA is end-of-loop */
8207 if (linkno > 0 && err == -ENODATA) {
8215 "%s path2fid: cannot get %sfid for '%s': %s\n",
8216 progname, show_parents ? "parent " : "", *path,
8228 #define MAX_ERRNO 4095
8229 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8231 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8235 rc = llapi_rmfid(device, fa);
8237 fprintf(stderr, "rmfid(): rc = %d\n", rc);
8241 for (k = 0; k < fa->fa_nr; k++) {
8242 rc = (__s32)fa->fa_fids[k].f_ver;
8243 if (!IS_ERR_VALUE(rc))
8249 fa->fa_fids[k].f_ver = 0;
8250 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
8251 PFID(&fa->fa_fids[k]), rc);
8257 static int lfs_rmfid(int argc, char **argv)
8259 char *fidstr, *device;
8260 int rc = 0, rc2, nr;
8261 struct fid_array *fa;
8263 if (optind > argc - 1) {
8264 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8268 device = argv[optind++];
8271 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8277 while (optind < argc) {
8280 fidstr = argv[optind++];
8281 while (*fidstr == '[')
8283 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8285 fprintf(stderr, "unrecognized FID: %s\n",
8290 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8291 /* start another batch */
8292 rc2 = lfs_rmfid_and_show_errors(device, fa);
8299 rc2 = lfs_rmfid_and_show_errors(device, fa);
8307 static int lfs_data_version(int argc, char **argv)
8314 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8317 fprintf(stderr, "%s data_version: FILE must be specified\n",
8322 while ((c = getopt(argc, argv, "nrw")) != -1) {
8325 data_version_flags = 0;
8328 data_version_flags |= LL_DV_RD_FLUSH;
8331 data_version_flags |= LL_DV_WR_FLUSH;
8335 "%s data_version: unrecognized option '%s'\n",
8336 progname, argv[optind - 1]);
8340 if (optind == argc) {
8341 fprintf(stderr, "%s data_version: FILE must be specified\n",
8346 path = argv[optind];
8347 fd = open(path, O_RDONLY);
8350 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8351 progname, path, strerror(-rc));
8355 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8358 "%s data_version: cannot get version for '%s': %s\n",
8359 progname, path, strerror(-rc));
8361 printf("%ju" "\n", (uintmax_t)data_version);
8367 static int lfs_hsm_state(int argc, char **argv)
8372 struct hsm_user_state hus;
8380 rc = llapi_hsm_state_get(path, &hus);
8382 fprintf(stderr, "can't get hsm state for %s: %s\n",
8383 path, strerror(errno = -rc));
8387 /* Display path name and status flags */
8388 printf("%s: (0x%08x)", path, hus.hus_states);
8390 if (hus.hus_states & HS_RELEASED)
8391 printf(" released");
8392 if (hus.hus_states & HS_EXISTS)
8394 if (hus.hus_states & HS_DIRTY)
8396 if (hus.hus_states & HS_ARCHIVED)
8397 printf(" archived");
8398 /* Display user-settable flags */
8399 if (hus.hus_states & HS_NORELEASE)
8400 printf(" never_release");
8401 if (hus.hus_states & HS_NOARCHIVE)
8402 printf(" never_archive");
8403 if (hus.hus_states & HS_LOST)
8404 printf(" lost_from_hsm");
8406 if (hus.hus_archive_id != 0)
8407 printf(", archive_id:%d", hus.hus_archive_id);
8410 } while (++i < argc);
8415 #define LFS_HSM_SET 0
8416 #define LFS_HSM_CLEAR 1
8419 * Generic function to set or clear HSM flags.
8420 * Used by hsm_set and hsm_clear.
8422 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8424 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8426 struct option long_opts[] = {
8427 { .val = 'A', .name = "archived", .has_arg = no_argument },
8428 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8429 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8430 { .val = 'e', .name = "exists", .has_arg = no_argument },
8431 { .val = 'l', .name = "lost", .has_arg = no_argument },
8432 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8433 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8435 char short_opts[] = "lraAdei:";
8439 __u32 archive_id = 0;
8445 while ((c = getopt_long(argc, argv, short_opts,
8446 long_opts, NULL)) != -1) {
8452 mask |= HS_NOARCHIVE;
8455 mask |= HS_ARCHIVED;
8458 mask |= HS_NORELEASE;
8467 archive_id = strtol(optarg, &end, 10);
8469 fprintf(stderr, "invalid archive_id: '%s'\n",
8477 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8478 argv[0], argv[optind - 1]);
8483 /* User should have specified a flag */
8487 while (optind < argc) {
8489 path = argv[optind];
8491 /* If mode == 0, this means we apply the mask. */
8492 if (mode == LFS_HSM_SET)
8493 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8495 rc = llapi_hsm_state_set(path, 0, mask, 0);
8498 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8499 path, strerror(errno = -rc));
8508 static int lfs_hsm_action(int argc, char **argv)
8513 struct hsm_current_action hca;
8514 struct hsm_extent he;
8515 enum hsm_user_action hua;
8516 enum hsm_progress_states hps;
8524 rc = llapi_hsm_current_action(path, &hca);
8526 fprintf(stderr, "can't get hsm action for %s: %s\n",
8527 path, strerror(errno = -rc));
8530 he = hca.hca_location;
8531 hua = hca.hca_action;
8532 hps = hca.hca_state;
8534 printf("%s: %s", path, hsm_user_action2name(hua));
8536 /* Skip file without action */
8537 if (hca.hca_action == HUA_NONE) {
8542 printf(" %s ", hsm_progress_state2name(hps));
8544 if ((hps == HPS_RUNNING) &&
8545 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8546 printf("(%llu bytes moved)\n",
8547 (unsigned long long)he.length);
8548 else if ((he.offset + he.length) == LUSTRE_EOF)
8549 printf("(from %llu to EOF)\n",
8550 (unsigned long long)he.offset);
8552 printf("(from %llu to %llu)\n",
8553 (unsigned long long)he.offset,
8554 (unsigned long long)(he.offset + he.length));
8556 } while (++i < argc);
8561 static int lfs_hsm_set(int argc, char **argv)
8563 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8566 static int lfs_hsm_clear(int argc, char **argv)
8568 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8572 * Check file state and return its fid, to be used by lfs_hsm_request().
8574 * \param[in] file Path to file to check
8575 * \param[in,out] fid Pointer to allocated lu_fid struct.
8576 * \param[in,out] last_dev Pointer to last device id used.
8578 * \return 0 on success.
8580 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8586 rc = lstat(file, &st);
8588 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8591 /* Checking for regular file as archiving as posix copytool
8592 * rejects archiving files other than regular files
8594 if (!S_ISREG(st.st_mode)) {
8595 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8598 /* A request should be ... */
8599 if (*last_dev != st.st_dev && *last_dev != 0) {
8600 fprintf(stderr, "All files should be "
8601 "on the same filesystem: %s\n", file);
8604 *last_dev = st.st_dev;
8606 rc = llapi_path2fid(file, fid);
8608 fprintf(stderr, "Cannot read FID of %s: %s\n",
8609 file, strerror(-rc));
8615 /* Fill an HSM HUR item with a given file name.
8617 * If mntpath is set, then the filename is actually a FID, and no
8618 * lookup on the filesystem will be performed.
8620 * \param[in] hur the user request to fill
8621 * \param[in] idx index of the item inside the HUR to fill
8622 * \param[in] mntpath mountpoint of Lustre
8623 * \param[in] fname filename (if mtnpath is NULL)
8624 * or FID (if mntpath is set)
8625 * \param[in] last_dev pointer to last device id used
8627 * \retval 0 on success
8628 * \retval CMD_HELP or a negative errno on error
8630 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8631 const char *mntpath, const char *fname,
8634 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8637 hui->hui_extent.length = -1;
8639 if (mntpath != NULL) {
8640 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8642 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8645 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8649 hur->hur_request.hr_itemcount++;
8654 static int lfs_hsm_request(int argc, char **argv, int action)
8656 struct option long_opts[] = {
8657 { .val = 'a', .name = "archive", .has_arg = required_argument },
8658 { .val = 'D', .name = "data", .has_arg = required_argument },
8659 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8660 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8663 char short_opts[] = "l:D:a:m:";
8664 struct hsm_user_request *hur, *oldhur;
8669 char *filelist = NULL;
8670 char fullpath[PATH_MAX];
8671 char *opaque = NULL;
8675 int nbfile_alloc = 0;
8676 char *some_file = NULL;
8677 char *mntpath = NULL;
8683 while ((c = getopt_long(argc, argv, short_opts,
8684 long_opts, NULL)) != -1) {
8693 if (action != HUA_ARCHIVE &&
8694 action != HUA_REMOVE) {
8696 "error: -a is supported only "
8697 "when archiving or removing\n");
8700 archive_id = atoi(optarg);
8703 if (some_file == NULL) {
8705 some_file = strdup(optarg);
8711 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8712 argv[0], argv[optind - 1]);
8717 /* All remaining args are files, so we have at least nbfile */
8718 nbfile = argc - optind;
8720 if ((nbfile == 0) && (filelist == NULL))
8724 opaque_len = strlen(opaque);
8726 /* Alloc the request structure with enough place to store all files
8727 * from command line. */
8728 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8730 fprintf(stderr, "Cannot create the request: %s\n",
8734 nbfile_alloc = nbfile;
8736 hur->hur_request.hr_action = action;
8737 hur->hur_request.hr_archive_id = archive_id;
8738 hur->hur_request.hr_flags = 0;
8740 /* All remaining args are files, add them */
8741 if (nbfile != 0 && some_file == NULL)
8742 some_file = strdup(argv[optind]);
8744 for (i = 0; i < nbfile; i++) {
8745 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8751 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8753 /* If a filelist was specified, read the filelist from it. */
8754 if (filelist != NULL) {
8755 fp = fopen(filelist, "r");
8757 fprintf(stderr, "Cannot read the file list %s: %s\n",
8758 filelist, strerror(errno));
8763 while ((rc = getline(&line, &len, fp)) != -1) {
8764 /* If allocated buffer was too small, get something
8766 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8769 nbfile_alloc = nbfile_alloc * 2 + 1;
8771 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8774 fprintf(stderr, "hsm: cannot allocate "
8775 "the request: %s\n",
8782 size = hur_len(oldhur);
8784 fprintf(stderr, "hsm: cannot allocate "
8785 "%u files + %u bytes data\n",
8786 oldhur->hur_request.hr_itemcount,
8787 oldhur->hur_request.hr_data_len);
8794 memcpy(hur, oldhur, size);
8799 if (line[strlen(line) - 1] == '\n')
8800 line[strlen(line) - 1] = '\0';
8802 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8803 mntpath, line, &last_dev);
8809 if (some_file == NULL) {
8819 /* If a --data was used, add it to the request */
8820 hur->hur_request.hr_data_len = opaque_len;
8822 memcpy(hur_data(hur), opaque, opaque_len);
8824 /* Send the HSM request */
8825 if (realpath(some_file, fullpath) == NULL) {
8826 fprintf(stderr, "Could not find path '%s': %s\n",
8827 some_file, strerror(errno));
8829 rc = llapi_hsm_request(fullpath, hur);
8831 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8832 some_file, strerror(-rc));
8842 static int lfs_hsm_archive(int argc, char **argv)
8844 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8847 static int lfs_hsm_restore(int argc, char **argv)
8849 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8852 static int lfs_hsm_release(int argc, char **argv)
8854 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8857 static int lfs_hsm_remove(int argc, char **argv)
8859 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8862 static int lfs_hsm_cancel(int argc, char **argv)
8864 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8867 static int lfs_swap_layouts(int argc, char **argv)
8872 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8873 SWAP_LAYOUTS_KEEP_MTIME |
8874 SWAP_LAYOUTS_KEEP_ATIME);
8877 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8879 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8881 int lfs_get_mode(const char *string)
8883 enum lock_mode_user mode;
8885 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8886 if (lock_mode_names[mode] == NULL)
8888 if (strcmp(string, lock_mode_names[mode]) == 0)
8895 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8897 enum lu_ladvise_type advice;
8900 advice < ARRAY_SIZE(ladvise_names); advice++) {
8901 if (ladvise_names[advice] == NULL)
8903 if (strcmp(string, ladvise_names[advice]) == 0)
8907 return LU_LADVISE_INVALID;
8910 static int lfs_ladvise(int argc, char **argv)
8912 struct option long_opts[] = {
8913 { .val = 'a', .name = "advice", .has_arg = required_argument },
8914 { .val = 'b', .name = "background", .has_arg = no_argument },
8915 { .val = 'e', .name = "end", .has_arg = required_argument },
8916 { .val = 'l', .name = "length", .has_arg = required_argument },
8917 { .val = 'm', .name = "mode", .has_arg = required_argument },
8918 { .val = 's', .name = "start", .has_arg = required_argument },
8919 { .val = 'u', .name = "unset", .has_arg = no_argument },
8921 char short_opts[] = "a:be:l:m:s:u";
8926 struct llapi_lu_ladvise advice;
8927 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8928 unsigned long long start = 0;
8929 unsigned long long end = LUSTRE_EOF;
8930 unsigned long long length = 0;
8931 unsigned long long size_units;
8932 unsigned long long flags = 0;
8936 while ((c = getopt_long(argc, argv, short_opts,
8937 long_opts, NULL)) != -1) {
8940 advice_type = lfs_get_ladvice(optarg);
8941 if (advice_type == LU_LADVISE_INVALID) {
8942 fprintf(stderr, "%s: invalid advice type "
8943 "'%s'\n", argv[0], optarg);
8944 fprintf(stderr, "Valid types:");
8946 for (advice_type = 0;
8947 advice_type < ARRAY_SIZE(ladvise_names);
8949 if (ladvise_names[advice_type] == NULL)
8951 fprintf(stderr, " %s",
8952 ladvise_names[advice_type]);
8954 fprintf(stderr, "\n");
8967 rc = llapi_parse_size(optarg, &end,
8970 fprintf(stderr, "%s: bad end offset '%s'\n",
8977 rc = llapi_parse_size(optarg, &start,
8980 fprintf(stderr, "%s: bad start offset "
8981 "'%s'\n", argv[0], optarg);
8987 rc = llapi_parse_size(optarg, &length,
8990 fprintf(stderr, "%s: bad length '%s'\n",
8996 mode = lfs_get_mode(optarg);
8998 fprintf(stderr, "%s: bad mode '%s', valid "
8999 "modes are READ or WRITE\n",
9007 fprintf(stderr, "%s: option '%s' unrecognized\n",
9008 argv[0], argv[optind - 1]);
9013 if (advice_type == LU_LADVISE_INVALID) {
9014 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9015 fprintf(stderr, "Valid types:");
9016 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9018 if (ladvise_names[advice_type] == NULL)
9020 fprintf(stderr, " %s", ladvise_names[advice_type]);
9022 fprintf(stderr, "\n");
9026 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9027 fprintf(stderr, "%s: Lock no expand advice is a per file "
9028 "descriptor advice, so when called from lfs, "
9029 "it does nothing.\n", argv[0]);
9033 if (argc <= optind) {
9034 fprintf(stderr, "%s: please give one or more file names\n",
9039 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9040 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9045 if (end == LUSTRE_EOF && length != 0)
9046 end = start + length;
9049 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9050 argv[0], start, end);
9054 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9055 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9060 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9061 fprintf(stderr, "%s: mode is required with lockahead\n",
9066 while (optind < argc) {
9069 path = argv[optind++];
9071 fd = open(path, O_RDONLY);
9073 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9074 argv[0], path, strerror(errno));
9079 advice.lla_start = start;
9080 advice.lla_end = end;
9081 advice.lla_advice = advice_type;
9082 advice.lla_value1 = 0;
9083 advice.lla_value2 = 0;
9084 advice.lla_value3 = 0;
9085 advice.lla_value4 = 0;
9086 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9087 advice.lla_lockahead_mode = mode;
9088 advice.lla_peradvice_flags = flags;
9091 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9094 fprintf(stderr, "%s: cannot give advice '%s' to file "
9095 "'%s': %s\n", argv[0],
9096 ladvise_names[advice_type],
9097 path, strerror(errno));
9103 if (rc == 0 && rc2 < 0)
9109 static const char *const heat_names[] = LU_HEAT_NAMES;
9111 static int lfs_heat_get(int argc, char **argv)
9113 struct lu_heat *heat;
9122 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9124 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9129 while (optind < argc) {
9130 path = argv[optind++];
9132 fd = open(path, O_RDONLY);
9134 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9135 argv[0], path, strerror(errno));
9140 heat->lh_count = OBD_HEAT_COUNT;
9141 rc2 = llapi_heat_get(fd, heat);
9144 fprintf(stderr, "%s: cannot get heat of file '%s'"
9145 ": %s\n", argv[0], path, strerror(errno));
9149 printf("flags: %x\n", heat->lh_flags);
9150 for (i = 0; i < heat->lh_count; i++)
9151 printf("%s: %llu\n", heat_names[i],
9152 (unsigned long long)heat->lh_heat[i]);
9154 if (rc == 0 && rc2 < 0)
9162 static int lfs_heat_set(int argc, char **argv)
9164 struct option long_opts[] = {
9165 { .val = 'c', .name = "clear", .has_arg = no_argument },
9166 { .val = 'o', .name = "off", .has_arg = no_argument },
9167 { .val = 'O', .name = "on", .has_arg = no_argument },
9169 enum lu_heat_flag flags = 0;
9179 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9182 flags |= LU_HEAT_FLAG_CLEAR;
9185 flags |= LU_HEAT_FLAG_CLEAR;
9186 flags |= LU_HEAT_FLAG_OFF;
9189 flags &= ~LU_HEAT_FLAG_OFF;
9194 fprintf(stderr, "%s: option '%s' unrecognized\n",
9195 argv[0], argv[optind - 1]);
9200 if (argc <= optind) {
9201 fprintf(stderr, "%s: please give one or more file names\n",
9206 while (optind < argc) {
9207 path = argv[optind++];
9209 fd = open(path, O_RDONLY);
9211 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9212 argv[0], path, strerror(errno));
9217 rc2 = llapi_heat_set(fd, flags);
9220 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
9221 ": %s\n", argv[0], path, strerror(errno));
9225 if (rc == 0 && rc2 < 0)
9231 /** The input string contains a comma delimited list of component ids and
9232 * ranges, for example "1,2-4,7".
9234 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9236 bool end_of_loop = false;
9244 while (!end_of_loop) {
9248 char *endptr = NULL;
9251 ptr = strchrnul(arg, ',');
9252 end_of_loop = *ptr == '\0';
9255 start_index = strtol(arg, &endptr, 0);
9256 if (endptr == arg) /* no data at all */
9258 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9260 if (start_index < 0)
9263 end_index = start_index;
9264 if (*endptr == '-') {
9265 end_index = strtol(endptr + 1, &endptr, 0);
9266 if (*endptr != '\0')
9268 if (end_index < start_index)
9272 for (i = start_index; i <= end_index && size > 0; i++) {
9275 /* remove duplicate */
9276 for (j = 0; j < nr; j++) {
9280 if (j == nr) { /* no duplicate */
9286 if (size == 0 && i < end_index)
9293 if (!end_of_loop && ptr != NULL)
9296 return rc < 0 ? rc : nr;
9300 * struct verify_mirror_id - Mirror id to be verified.
9301 * @mirror_id: A specified mirror id.
9302 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9304 struct verify_mirror_id {
9310 * compare_mirror_ids() - Compare mirror ids.
9311 * @layout: Mirror component list.
9312 * @cbdata: Callback data in verify_mirror_id structure.
9314 * This is a callback function called by llapi_layout_comp_iterate()
9315 * to compare the specified mirror id with the one in the current
9316 * component of @layout. If they are the same, then the specified
9317 * mirror id is valid.
9319 * Return: a negative error code on failure or
9320 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9321 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9324 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9326 struct verify_mirror_id *mirror_id_cbdata =
9327 (struct verify_mirror_id *)cbdata;
9331 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9335 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9336 progname, strerror(errno));
9340 if (mirror_id_cbdata->mirror_id == mirror_id) {
9341 mirror_id_cbdata->is_valid_id = true;
9342 return LLAPI_LAYOUT_ITER_STOP;
9345 return LLAPI_LAYOUT_ITER_CONT;
9349 * verify_mirror_ids() - Verify specified mirror ids.
9350 * @fname: Mirrored file name.
9351 * @mirror_ids: Specified mirror ids to be verified.
9352 * @ids_nr: Number of specified mirror ids.
9354 * This function verifies that specified @mirror_ids are valid
9355 * in the mirrored file @fname.
9357 * Return: 0 on success or a negative error code on failure.
9360 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9362 struct llapi_layout *layout = NULL;
9363 struct verify_mirror_id mirror_id_cbdata = { 0 };
9374 if (stat(fname, &stbuf) < 0) {
9375 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9376 progname, fname, strerror(errno));
9381 if (!S_ISREG(stbuf.st_mode)) {
9382 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9388 fd = open(fname, O_DIRECT | O_RDONLY);
9390 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9391 progname, fname, strerror(errno));
9396 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9398 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9399 progname, fname, strerror(errno));
9403 layout = llapi_layout_get_by_fd(fd, 0);
9404 if (layout == NULL) {
9405 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9406 progname, fname, strerror(errno));
9408 llapi_lease_release(fd);
9412 rc = llapi_layout_flags_get(layout, &flr_state);
9414 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9415 progname, fname, strerror(errno));
9420 flr_state &= LCM_FL_FLR_MASK;
9421 switch (flr_state) {
9424 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9425 progname, fname, llapi_layout_flags_string(flr_state));
9432 for (i = 0; i < ids_nr; i++) {
9433 mirror_id_cbdata.mirror_id = mirror_ids[i];
9434 mirror_id_cbdata.is_valid_id = false;
9436 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9441 "%s: '%s' failed to verify mirror id: %u.\n",
9442 progname, fname, mirror_ids[i]);
9446 if (!mirror_id_cbdata.is_valid_id) {
9449 "%s: '%s' invalid specified mirror id: %u.\n",
9450 progname, fname, mirror_ids[i]);
9456 llapi_layout_free(layout);
9457 llapi_lease_release(fd);
9465 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9466 __u16 *mirror_ids, int ids_nr)
9468 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9469 struct llapi_layout *layout;
9480 if (stat(fname, &stbuf) < 0) {
9481 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9482 progname, fname, strerror(errno));
9486 if (!S_ISREG(stbuf.st_mode)) {
9487 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9493 fd = open(fname, O_DIRECT | O_RDWR);
9495 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9496 progname, fname, strerror(errno));
9501 layout = llapi_layout_get_by_fd(fd, 0);
9502 if (layout == NULL) {
9503 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9504 progname, fname, strerror(errno));
9509 rc = llapi_layout_flags_get(layout, &flr_state);
9511 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9512 progname, fname, strerror(errno));
9517 flr_state &= LCM_FL_FLR_MASK;
9518 if (flr_state == LCM_FL_NONE) {
9520 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9525 /* get stale component info */
9526 comp_size = llapi_mirror_find_stale(layout, comp_array,
9527 ARRAY_SIZE(comp_array),
9528 mirror_ids, ids_nr);
9529 if (comp_size <= 0) {
9534 ioc->lil_mode = LL_LEASE_WRLCK;
9535 ioc->lil_flags = LL_LEASE_RESYNC;
9536 rc = llapi_lease_set(fd, ioc);
9538 if (rc == -EALREADY)
9542 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9543 progname, fname, strerror(-rc));
9547 /* get the read range [start, end) */
9548 start = comp_array[0].lrc_start;
9549 end = comp_array[0].lrc_end;
9550 for (idx = 1; idx < comp_size; idx++) {
9551 if (comp_array[idx].lrc_start < start)
9552 start = comp_array[idx].lrc_start;
9553 if (end < comp_array[idx].lrc_end)
9554 end = comp_array[idx].lrc_end;
9557 rc = llapi_lease_check(fd);
9558 if (rc != LL_LEASE_WRLCK) {
9559 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9564 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9567 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9568 progname, fname, strerror(-rc));
9570 /* need to do the lease unlock even resync fails */
9571 ioc->lil_mode = LL_LEASE_UNLCK;
9572 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9574 for (idx = 0; idx < comp_size; idx++) {
9575 if (comp_array[idx].lrc_synced) {
9576 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9581 rc2 = llapi_lease_set(fd, ioc);
9583 * llapi_lease_set returns lease mode when it request to unlock
9587 /* rc2 == 0 means lost lease lock */
9588 if (rc2 == 0 && rc == 0)
9590 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9592 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9596 llapi_layout_free(layout);
9603 static inline int lfs_mirror_resync(int argc, char **argv)
9605 struct ll_ioc_lease *ioc = NULL;
9606 __u16 mirror_ids[128] = { 0 };
9611 struct option long_opts[] = {
9612 { .val = 'o', .name = "only", .has_arg = required_argument },
9615 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9618 rc = parse_mirror_ids(mirror_ids,
9619 sizeof(mirror_ids) / sizeof(__u16),
9623 "%s: bad mirror ids '%s'.\n",
9630 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9631 argv[0], argv[optind - 1]);
9637 if (argc == optind) {
9638 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9643 if (ids_nr > 0 && argc > optind + 1) {
9645 "%s: option '--only' cannot be used upon multiple files.\n",
9653 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9658 /* set the lease on the file */
9659 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9661 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9662 argv[0], strerror(errno));
9667 for (; optind < argc; optind++) {
9668 rc = lfs_mirror_resync_file(argv[optind], ioc,
9669 mirror_ids, ids_nr);
9670 /* ignore previous file's error, continue with next file */
9673 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9681 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9683 struct llapi_layout *layout;
9686 layout = llapi_layout_get_by_fd(fd, 0);
9687 if (layout == NULL) {
9688 fprintf(stderr, "could not get layout.\n");
9692 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9694 fprintf(stderr, "failed to iterate layout\n");
9695 llapi_layout_free(layout);
9698 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9699 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9700 llapi_layout_free(layout);
9704 llapi_layout_free(layout);
9710 * Check whether two files are the same file
9711 * \retval 0 same file
9712 * \retval 1 not the same file
9713 * \retval <0 error code
9715 static inline int check_same_file(int fd, const char *f2)
9720 if (fstat(fd, &stbuf1) < 0)
9723 if (stat(f2, &stbuf2) < 0)
9726 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9727 stbuf1.st_ino == stbuf2.st_ino)
9733 static inline int lfs_mirror_read(int argc, char **argv)
9736 __u16 mirror_id = 0;
9737 const char *outfile = NULL;
9743 const size_t buflen = 4 << 20;
9745 struct option long_opts[] = {
9746 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9747 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9750 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9755 mirror_id = strtoul(optarg, &end, 0);
9756 if (*end != '\0' || mirror_id == 0) {
9758 "%s %s: invalid mirror ID '%s'\n",
9759 progname, argv[0], optarg);
9767 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9768 progname, argv[optind - 1]);
9773 if (argc == optind) {
9774 fprintf(stderr, "%s %s: no mirrored file provided\n",
9777 } else if (argc > optind + 1) {
9778 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9782 if (mirror_id == 0) {
9783 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9788 /* open mirror file */
9789 fname = argv[optind];
9790 fd = open(fname, O_DIRECT | O_RDONLY);
9792 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9793 progname, argv[0], fname, strerror(errno));
9797 /* verify mirror id */
9798 rc = verify_mirror_id_by_fd(fd, mirror_id);
9801 "%s %s: cannot find mirror with ID %u in '%s'\n",
9802 progname, argv[0], mirror_id, fname);
9806 /* open output file - O_EXCL ensures output is not the same as input */
9808 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9810 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9811 progname, argv[0], outfile, strerror(errno));
9816 outfd = STDOUT_FILENO;
9819 /* allocate buffer */
9820 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9822 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9823 progname, argv[0], rc);
9830 ssize_t written = 0;
9832 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9833 if (bytes_read < 0) {
9836 "%s %s: fail to read data from mirror %u: %s\n",
9837 progname, argv[0], mirror_id, strerror(-rc));
9842 if (bytes_read == 0)
9845 while (written < bytes_read) {
9848 written2 = write(outfd, buf + written,
9849 bytes_read - written);
9852 "%s %s: fail to write %s: %s\n",
9853 progname, argv[0], outfile ? : "STDOUT",
9858 written += written2;
9861 if (written != bytes_read) {
9863 "%s %s: written %ld bytes does not match with %ld read.\n",
9864 progname, argv[0], written, bytes_read);
9886 static inline int lfs_mirror_write(int argc, char **argv)
9889 __u16 mirror_id = 0;
9890 const char *inputfile = NULL;
9896 const size_t buflen = 4 << 20;
9898 size_t page_size = sysconf(_SC_PAGESIZE);
9899 struct ll_ioc_lease_id ioc;
9901 struct option long_opts[] = {
9902 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9903 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9906 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9911 mirror_id = strtoul(optarg, &end, 0);
9912 if (*end != '\0' || mirror_id == 0) {
9914 "%s %s: invalid mirror ID '%s'\n",
9915 progname, argv[0], optarg);
9923 fprintf(stderr, "%s: option '%s' unrecognized\n",
9924 progname, argv[optind - 1]);
9929 if (argc == optind) {
9930 fprintf(stderr, "%s %s: no mirrored file provided\n",
9933 } else if (argc > optind + 1) {
9934 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9938 if (mirror_id == 0) {
9939 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9944 /* open mirror file */
9945 fname = argv[optind];
9946 fd = open(fname, O_DIRECT | O_WRONLY);
9948 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9949 progname, argv[0], fname, strerror(errno));
9953 /* verify mirror id */
9954 rc = verify_mirror_id_by_fd(fd, mirror_id);
9957 "%s %s: cannot find mirror with ID %u in '%s'\n",
9958 progname, argv[0], mirror_id, fname);
9962 /* open input file */
9964 rc = check_same_file(fd, inputfile);
9967 "%s %s: input file cannot be the mirrored file\n",
9974 inputfd = open(inputfile, O_RDONLY, 0644);
9976 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9977 progname, argv[0], inputfile, strerror(errno));
9982 inputfd = STDIN_FILENO;
9985 /* allocate buffer */
9986 rc = posix_memalign(&buf, page_size, buflen);
9988 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9989 progname, argv[0], rc);
9993 /* prepare target mirror components instantiation */
9994 ioc.lil_mode = LL_LEASE_WRLCK;
9995 ioc.lil_flags = LL_LEASE_RESYNC;
9996 ioc.lil_mirror_id = mirror_id;
9997 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10000 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10001 progname, argv[0], fname, strerror(errno));
10007 ssize_t bytes_read;
10011 rc = llapi_lease_check(fd);
10012 if (rc != LL_LEASE_WRLCK) {
10013 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10014 progname, argv[0], fname);
10018 bytes_read = read(inputfd, buf, buflen);
10019 if (bytes_read < 0) {
10022 "%s %s: fail to read data from '%s': %s\n",
10023 progname, argv[0], inputfile ? : "STDIN",
10030 if (bytes_read == 0)
10033 /* round up to page align to make direct IO happy. */
10034 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10036 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10041 "%s %s: fail to write to mirror %u: %s\n",
10042 progname, argv[0], mirror_id,
10050 if (pos & (page_size - 1)) {
10051 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10056 ioc.lil_mode = LL_LEASE_UNLCK;
10057 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10059 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10064 "%s %s: release lease lock of '%s' failed: %s\n",
10065 progname, argv[0], fname, strerror(errno));
10082 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10084 struct llapi_layout *layout;
10085 struct collect_ids_data cid = { .cid_ids = ids,
10087 .cid_exclude = exclude_id, };
10090 layout = llapi_layout_get_by_fd(fd, 0);
10091 if (layout == NULL) {
10092 fprintf(stderr, "could not get layout\n");
10096 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10098 fprintf(stderr, "failed to iterate layout\n");
10099 llapi_layout_free(layout);
10103 llapi_layout_free(layout);
10105 return cid.cid_count;
10108 static inline int lfs_mirror_copy(int argc, char **argv)
10111 __u16 read_mirror_id = 0;
10112 __u16 ids[128] = { 0 };
10114 struct llapi_layout *layout = NULL;
10115 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10122 struct ll_ioc_lease *ioc = NULL;
10123 struct ll_ioc_lease_id *resync_ioc;
10125 struct option long_opts[] = {
10126 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10127 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10128 { .name = NULL } };
10130 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10135 read_mirror_id = strtoul(optarg, &end, 0);
10136 if (*end != '\0' || read_mirror_id == 0) {
10138 "%s %s: invalid read mirror ID '%s'\n",
10139 progname, argv[0], optarg);
10144 if (!strcmp(optarg, "-1")) {
10145 /* specify all other mirrors */
10146 ids[0] = (__u16)-1;
10149 count = parse_mirror_ids((__u16 *)ids,
10157 fprintf(stderr, "%s: option '%s' unrecognized\n",
10158 progname, argv[optind - 1]);
10163 if (argc == optind) {
10164 fprintf(stderr, "%s %s: no mirrored file provided\n",
10165 progname, argv[0]);
10167 } else if (argc > optind + 1) {
10168 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10172 if (read_mirror_id == 0) {
10174 "%s %s: no valid read mirror ID %d is provided\n",
10175 progname, argv[0], read_mirror_id);
10181 "%s %s: no write mirror ID is provided\n",
10182 progname, argv[0]);
10186 for (i = 0; i < count; i++) {
10187 if (read_mirror_id == ids[i]) {
10189 "%s %s: read and write mirror ID cannot be the same\n",
10190 progname, argv[0]);
10195 /* open mirror file */
10196 fname = argv[optind];
10198 fd = open(fname, O_DIRECT | O_RDWR);
10200 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10201 progname, argv[0], fname, strerror(errno));
10205 /* write to all other mirrors */
10206 if (ids[0] == (__u16)-1) {
10207 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10211 "%s %s: failed to get other mirror ids in '%s': %d\n",
10212 progname, argv[0], fname, rc);
10217 /* verify mirror id */
10218 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10221 "%s %s: cannot find mirror with ID %u in '%s'\n",
10222 progname, argv[0], read_mirror_id, fname);
10226 for (i = 0; i < count; i++) {
10227 rc = verify_mirror_id_by_fd(fd, ids[i]);
10230 "%s %s: cannot find mirror with ID %u in '%s'\n",
10231 progname, argv[0], ids[i], fname);
10236 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10239 "%s %s: cannot alloc comp id array for ioc: %s\n",
10240 progname, argv[0], strerror(errno));
10245 /* get stale component info */
10246 layout = llapi_layout_get_by_fd(fd, 0);
10247 if (layout == NULL) {
10248 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10249 progname, argv[0], fname, strerror(errno));
10253 comp_size = llapi_mirror_find_stale(layout, comp_array,
10254 ARRAY_SIZE(comp_array),
10256 llapi_layout_free(layout);
10257 if (comp_size < 0) {
10262 /* prepare target mirror components instantiation */
10263 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10264 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10265 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10267 resync_ioc->lil_mirror_id = ids[0];
10269 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10270 rc = llapi_lease_set(fd, ioc);
10273 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10274 progname, argv[0], fname, strerror(errno));
10278 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10281 fprintf(stderr, "%s %s: copy error: %d\n",
10282 progname, argv[0], rc);
10286 fprintf(stdout, "mirror copied successfully: ");
10287 for (i = 0; i < copied; i++)
10288 fprintf(stdout, "%d ", ids[i]);
10289 fprintf(stdout, "\n");
10291 ioc->lil_mode = LL_LEASE_UNLCK;
10292 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10293 ioc->lil_count = 0;
10294 for (i = 0; i < comp_size; i++) {
10297 for (j = 0; j < copied; j++) {
10298 if (comp_array[i].lrc_mirror_id != ids[j])
10301 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10305 rc = llapi_lease_set(fd, ioc);
10310 "%s %s: release lease lock of '%s' failed: %s\n",
10311 progname, argv[0], fname, strerror(errno));
10325 * struct verify_chunk - Mirror chunk to be verified.
10326 * @chunk: [start, end) of the chunk.
10327 * @mirror_count: Number of mirror ids in @mirror_id array.
10328 * @mirror_id: Array of valid mirror ids that cover the chunk.
10330 struct verify_chunk {
10331 struct lu_extent chunk;
10332 unsigned int mirror_count;
10333 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10337 * print_chunks() - Print chunk information.
10338 * @fname: Mirrored file name.
10339 * @chunks: Array of chunks.
10340 * @chunk_count: Number of chunks in @chunks array.
10342 * This function prints [start, end) of each chunk in @chunks
10343 * for mirrored file @fname, and also prints the valid mirror ids
10344 * that cover the chunk.
10349 void print_chunks(const char *fname, struct verify_chunk *chunks,
10355 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10356 for (i = 0; i < chunk_count; i++) {
10357 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10359 if (chunks[i].mirror_count == 0)
10360 fprintf(stdout, "\t[");
10362 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10363 for (j = 1; j < chunks[i].mirror_count; j++)
10364 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10366 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10368 fprintf(stdout, "\n");
10372 * print_checksums() - Print CRC-32 checksum values.
10373 * @chunk: A chunk and its corresponding valid mirror ids.
10374 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10376 * This function prints CRC-32 checksum values on @chunk for
10377 * each valid mirror that covers it.
10382 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10387 "CRC-32 checksum value for chunk "DEXT":\n",
10388 PEXT(&chunk->chunk));
10389 for (i = 0; i < chunk->mirror_count; i++)
10390 fprintf(stdout, "Mirror %u:\t%#lx\n",
10391 chunk->mirror_id[i], crc[i]);
10392 fprintf(stdout, "\n");
10396 * filter_mirror_id() - Filter specified mirror ids.
10397 * @chunks: Array of chunks.
10398 * @chunk_count: Number of chunks in @chunks array.
10399 * @mirror_ids: Specified mirror ids to be verified.
10400 * @ids_nr: Number of specified mirror ids.
10402 * This function scans valid mirror ids that cover each chunk in @chunks
10403 * and filters specified mirror ids.
10408 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10409 __u16 *mirror_ids, int ids_nr)
10414 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10415 unsigned int valid_count = 0;
10417 for (i = 0; i < chunk_count; i++) {
10418 if (chunks[i].mirror_count == 0)
10422 for (j = 0; j < ids_nr; j++) {
10423 for (k = 0; k < chunks[i].mirror_count; k++) {
10424 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10425 valid_id[valid_count] = mirror_ids[j];
10432 memcpy(chunks[i].mirror_id, valid_id,
10433 sizeof(__u16) * valid_count);
10434 chunks[i].mirror_count = valid_count;
10439 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10440 * @layout: Mirror component list.
10441 * @chunks: Array of chunks.
10442 * @chunks_size: Array size of @chunks.
10444 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10445 * to find out chunk segments and store them in @chunks array.
10447 * The @mirror_id array in each element of @chunks will store the valid
10448 * mirror ids that cover the chunk. If a mirror component covering the
10449 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10450 * will not be stored into the @mirror_id array, and the chunk for that
10451 * mirror will not be verified.
10453 * The @mirror_count in each element of @chunks will store the number of
10454 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10455 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10456 * indicates the chunk is valid in only one mirror. In both cases, the
10457 * chunk will not be verified.
10459 * Here is an example:
10461 * 0 1M 2M 3M 4M EOF
10462 * +------+-------------+--------------------+
10463 * | | | S | mirror1
10464 * +------+------+------+------+-------------+
10465 * | | S | S | | mirror2
10466 * +-------------+------+------+-------------+
10468 * prepared @chunks array will contain 5 elements:
10469 * (([0, 1M), [1, 2], 2),
10470 * ([1M, 2M), [1, 2], 2),
10471 * ([2M, 3M), [1], 1),
10472 * ([3M, 4M], [], 0),
10473 * ([4M, EOF), [2], 1))
10475 * Return: the actual array size of @chunks on success
10476 * or a negative error code on failure.
10479 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10480 struct verify_chunk *chunks,
10481 size_t chunks_size)
10485 uint32_t mirror_id;
10491 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10494 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10497 "%s: move to the first layout component: %s.\n",
10498 progname, strerror(errno));
10504 chunks[idx].chunk.e_end = LUSTRE_EOF;
10506 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10509 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10510 progname, strerror(errno));
10514 if (start > chunks[idx].chunk.e_start ||
10515 end <= chunks[idx].chunk.e_start)
10518 if (end < chunks[idx].chunk.e_end)
10519 chunks[idx].chunk.e_end = end;
10521 rc = llapi_layout_comp_flags_get(layout, &flags);
10524 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10525 progname, strerror(errno));
10529 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10532 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10535 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10536 progname, strerror(errno));
10540 chunks[idx].mirror_id[i] = mirror_id;
10542 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10544 "%s: mirror_id array is too small.\n",
10551 rc = llapi_layout_comp_use(layout,
10552 LLAPI_LAYOUT_COMP_USE_NEXT);
10555 "%s: move to the next layout component: %s.\n",
10556 progname, strerror(errno));
10559 } /* loop through all components */
10561 chunks[idx].mirror_count = i;
10563 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10567 if (idx >= chunks_size) {
10568 fprintf(stderr, "%s: chunks array is too small.\n",
10574 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10578 return rc < 0 ? rc : idx + 1;
10582 * lfs_mirror_verify_chunk() - Verify a chunk.
10583 * @fd: File descriptor of the mirrored file.
10584 * @file_size: Size of the mirrored file.
10585 * @chunk: A chunk and its corresponding valid mirror ids.
10586 * @verbose: Verbose mode.
10588 * This function verifies a @chunk contains exactly the same data
10589 * ammong the mirrors that cover it.
10591 * If @verbose is specified, then the function will print where the
10592 * differences are if the data do not match. Otherwise, it will
10593 * just return an error in that case.
10595 * Return: 0 on success or a negative error code on failure.
10598 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10599 struct verify_chunk *chunk, int verbose)
10601 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10603 size_t page_size = sysconf(_SC_PAGESIZE);
10604 ssize_t bytes_read;
10605 ssize_t bytes_done;
10609 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10613 if (file_size == 0)
10616 rc = posix_memalign(&buf, page_size, buflen);
10617 if (rc) /* error code is returned directly */
10621 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10622 PEXT(&chunk->chunk));
10623 for (i = 0; i < chunk->mirror_count; i++)
10624 fprintf(stdout, " %u", chunk->mirror_id[i]);
10625 fprintf(stdout, "\n");
10629 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10630 pos = chunk->chunk.e_start;
10631 while (bytes_done < count) {
10632 /* compute initial CRC-32 checksum */
10633 crc = crc32(0L, Z_NULL, 0);
10634 memset(crc_array, 0, sizeof(crc_array));
10637 for (i = 0; i < chunk->mirror_count; i++) {
10638 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10640 if (bytes_read < 0) {
10643 "%s: failed to read data from mirror %u: %s.\n",
10644 progname, chunk->mirror_id[i],
10649 /* compute new CRC-32 checksum */
10650 crc_array[i] = crc32(crc, buf, bytes_read);
10654 print_checksums(chunk, crc_array);
10656 /* compare CRC-32 checksum values */
10657 for (i = 1; i < chunk->mirror_count; i++) {
10658 if (crc_array[i] != crc_array[0]) {
10662 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10663 progname, PEXT(&chunk->chunk),
10664 chunk->mirror_id[0],
10665 chunk->mirror_id[i]);
10670 bytes_done += bytes_read;
10673 if (verbose > 1 && rc == 0) {
10674 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10675 PEXT(&chunk->chunk));
10676 for (i = 0; i < chunk->mirror_count; i++)
10677 fprintf(stdout, " %u", chunk->mirror_id[i]);
10678 fprintf(stdout, " PASS\n\n");
10687 * lfs_mirror_verify_file() - Verify a mirrored file.
10688 * @fname: Mirrored file name.
10689 * @mirror_ids: Specified mirror ids to be verified.
10690 * @ids_nr: Number of specified mirror ids.
10691 * @verbose: Verbose mode.
10693 * This function verifies that each SYNC mirror of a mirrored file
10694 * specified by @fname contains exactly the same data.
10696 * If @mirror_ids is specified, then the function will verify the
10697 * mirrors specified by @mirror_ids contain exactly the same data.
10699 * If @verbose is specified, then the function will print where the
10700 * differences are if the data do not match. Otherwise, it will
10701 * just return an error in that case.
10703 * Return: 0 on success or a negative error code on failure.
10706 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10709 struct verify_chunk chunks_array[1024] = { };
10710 struct llapi_layout *layout = NULL;
10712 uint32_t flr_state;
10714 int chunk_count = 0;
10720 if (stat(fname, &stbuf) < 0) {
10721 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10722 progname, fname, strerror(errno));
10727 if (!S_ISREG(stbuf.st_mode)) {
10728 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10734 if (stbuf.st_size == 0) {
10736 fprintf(stdout, "%s: '%s' file size is 0.\n",
10742 fd = open(fname, O_DIRECT | O_RDONLY);
10744 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10745 progname, fname, strerror(errno));
10750 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10752 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10753 progname, fname, strerror(errno));
10757 layout = llapi_layout_get_by_fd(fd, 0);
10758 if (layout == NULL) {
10759 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10760 progname, fname, strerror(errno));
10762 llapi_lease_release(fd);
10766 rc = llapi_layout_flags_get(layout, &flr_state);
10768 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10769 progname, fname, strerror(errno));
10774 flr_state &= LCM_FL_FLR_MASK;
10775 switch (flr_state) {
10778 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10779 progname, fname, llapi_layout_flags_string(flr_state));
10785 /* find out mirror chunks to be verified */
10786 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10787 ARRAY_SIZE(chunks_array));
10788 if (chunk_count < 0) {
10794 /* filter specified mirror ids */
10795 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10798 print_chunks(fname, chunks_array, chunk_count);
10800 for (idx = 0; idx < chunk_count; idx++) {
10801 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10804 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10806 PEXT(&chunks_array[idx].chunk),
10807 (unsigned long long)stbuf.st_size);
10811 if (chunks_array[idx].mirror_count == 0) {
10813 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10815 PEXT(&chunks_array[idx].chunk));
10817 fprintf(stderr, "skipped\n");
10821 fprintf(stderr, "failed\n");
10825 if (chunks_array[idx].mirror_count == 1) {
10828 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10830 PEXT(&chunks_array[idx].chunk),
10831 chunks_array[idx].mirror_id[0]);
10835 rc = llapi_lease_check(fd);
10836 if (rc != LL_LEASE_RDLCK) {
10837 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10842 /* verify one chunk */
10843 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10844 &chunks_array[idx], verbose);
10858 llapi_layout_free(layout);
10859 llapi_lease_release(fd);
10867 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10868 * @argc: The count of lfs mirror verify command line arguments.
10869 * @argv: Array of strings for lfs mirror verify command line arguments.
10871 * This function parses lfs mirror verify command and verifies the
10872 * specified mirrored file(s).
10874 * Return: 0 on success or a negative error code on failure.
10876 static inline int lfs_mirror_verify(int argc, char **argv)
10878 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10884 char cmd[PATH_MAX];
10886 struct option long_opts[] = {
10887 { .val = 'o', .name = "only", .has_arg = required_argument },
10888 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10889 { .name = NULL } };
10891 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10893 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10896 rc = parse_mirror_ids(mirror_ids,
10897 ARRAY_SIZE(mirror_ids),
10901 "%s: bad mirror ids '%s'.\n",
10908 "%s: at least 2 mirror ids needed with '--only' option.\n",
10918 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10919 progname, argv[optind - 1]);
10925 if (argc == optind) {
10926 fprintf(stderr, "%s: no file name given.\n", progname);
10931 if (ids_nr > 0 && argc > optind + 1) {
10933 "%s: '--only' cannot be used upon multiple files.\n",
10941 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10947 for (; optind < argc; optind++) {
10948 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10958 * lfs_mirror() - Parse and execute lfs mirror commands.
10959 * @argc: The count of lfs mirror command line arguments.
10960 * @argv: Array of strings for lfs mirror command line arguments.
10962 * This function parses lfs mirror commands and performs the
10963 * corresponding functions specified in mirror_cmdlist[].
10965 * Return: 0 on success or an error code on failure.
10967 static int lfs_mirror(int argc, char **argv)
10969 char cmd[PATH_MAX];
10972 setlinebuf(stdout);
10974 Parser_init("lfs-mirror > ", mirror_cmdlist);
10976 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10978 program_invocation_short_name = cmd;
10980 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10982 rc = Parser_commands();
10984 return rc < 0 ? -rc : rc;
10987 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10989 #if __BYTE_ORDER == __BIG_ENDIAN
10990 __swab16s(&attrs->lsa_valid);
10991 __swab64s(&attrs->lsa_size);
10992 __swab64s(&attrs->lsa_blocks);
10996 enum lfs_som_type {
10997 LFS_SOM_SIZE = 0x1,
10998 LFS_SOM_BLOCKS = 0x2,
10999 LFS_SOM_FLAGS = 0x4,
11000 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11004 static int lfs_getsom(int argc, char **argv)
11007 struct lustre_som_attrs *attrs;
11008 char buf[sizeof(*attrs) + 64];
11009 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11012 while ((c = getopt(argc, argv, "sbf")) != -1) {
11015 type = LFS_SOM_SIZE;
11018 type = LFS_SOM_BLOCKS;
11021 type = LFS_SOM_FLAGS;
11024 fprintf(stderr, "%s: invalid option '%c'\n",
11034 fprintf(stderr, "%s: %s\n",
11035 progname, argc == 0 ? "miss file target" :
11036 "input more than 2 files");
11041 attrs = (void *)buf;
11042 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11045 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11046 argv[0], strerror(errno), errno);
11050 lustre_som_swab(attrs);
11053 case LFS_SOM_ATTR_ALL:
11054 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11055 path, (unsigned long long)attrs->lsa_size,
11056 (unsigned long long)attrs->lsa_blocks,
11060 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11062 case LFS_SOM_BLOCKS:
11063 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11065 case LFS_SOM_FLAGS:
11066 printf("%x\n", attrs->lsa_valid);
11069 fprintf(stderr, "%s: unknown option\n", progname);
11077 * lfs_mirror_list_commands() - List lfs mirror commands.
11078 * @argc: The count of command line arguments.
11079 * @argv: Array of strings for command line arguments.
11081 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11083 * Return: 0 on success.
11085 static int lfs_mirror_list_commands(int argc, char **argv)
11087 char buffer[81] = "";
11089 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11095 static int lfs_pcc_attach(int argc, char **argv)
11097 struct option long_opts[] = {
11098 { .val = 'i', .name = "id", .has_arg = required_argument },
11099 { .name = NULL } };
11102 __u32 archive_id = 0;
11105 char fullpath[PATH_MAX];
11106 enum lu_pcc_type type = LU_PCC_READWRITE;
11109 while ((c = getopt_long(argc, argv, "i:",
11110 long_opts, NULL)) != -1) {
11113 archive_id = strtoul(optarg, &end, 0);
11114 if (*end != '\0' || archive_id == 0) {
11115 fprintf(stderr, "error: %s: bad archive ID "
11116 "'%s'\n", argv[0], optarg);
11123 fprintf(stderr, "%s: option '%s' unrecognized\n",
11124 argv[0], argv[optind - 1]);
11129 if (archive_id == 0) {
11130 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11134 if (argc <= optind) {
11135 fprintf(stderr, "%s: must specify one or more file names\n",
11140 while (optind < argc) {
11143 path = argv[optind++];
11144 if (realpath(path, fullpath) == NULL) {
11145 fprintf(stderr, "%s: could not find path '%s': %s\n",
11146 argv[0], path, strerror(errno));
11152 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11154 fprintf(stderr, "%s: cannot attach '%s' to PCC "
11155 "with archive ID '%u': %s\n", argv[0],
11156 path, archive_id, strerror(-rc2));
11164 static int lfs_pcc_attach_fid(int argc, char **argv)
11166 struct option long_opts[] = {
11167 { .val = 'i', .name = "id", .has_arg = required_argument },
11168 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11169 { .name = NULL } };
11170 char short_opts[] = "i:m:";
11173 __u32 archive_id = 0;
11175 const char *mntpath = NULL;
11176 const char *fidstr;
11177 enum lu_pcc_type type = LU_PCC_READWRITE;
11180 while ((c = getopt_long(argc, argv, short_opts,
11181 long_opts, NULL)) != -1) {
11184 archive_id = strtoul(optarg, &end, 0);
11185 if (*end != '\0') {
11186 fprintf(stderr, "error: %s: bad archive ID "
11187 "'%s'\n", argv[0], optarg);
11197 fprintf(stderr, "%s: option '%s' unrecognized\n",
11198 argv[0], argv[optind - 1]);
11203 if (archive_id == 0) {
11204 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11208 if (mntpath == NULL) {
11209 fprintf(stderr, "%s: must specify Lustre mount point\n",
11214 if (argc <= optind) {
11215 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11219 while (optind < argc) {
11222 fidstr = argv[optind++];
11224 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11227 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
11228 "with archive ID '%u': %s\n", argv[0],
11229 fidstr, mntpath, archive_id, strerror(rc2));
11231 if (rc == 0 && rc2 < 0)
11237 static int lfs_pcc_detach(int argc, char **argv)
11239 struct option long_opts[] = {
11240 { .val = 'k', .name = "keep", .has_arg = no_argument },
11241 { .name = NULL } };
11242 char short_opts[] = "k";
11246 char fullpath[PATH_MAX];
11247 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11250 while ((c = getopt_long(argc, argv, short_opts,
11251 long_opts, NULL)) != -1) {
11254 detach_opt = PCC_DETACH_OPT_NONE;
11259 fprintf(stderr, "%s: option '%s' unrecognized\n",
11260 argv[0], argv[optind - 1]);
11265 while (optind < argc) {
11268 path = argv[optind++];
11269 if (realpath(path, fullpath) == NULL) {
11270 fprintf(stderr, "%s: could not find path '%s': %s\n",
11271 argv[0], path, strerror(errno));
11277 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11280 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
11281 "%s\n", argv[0], path, strerror(errno));
11289 static int lfs_pcc_detach_fid(int argc, char **argv)
11291 struct option long_opts[] = {
11292 { .val = 'k', .name = "keep", .has_arg = no_argument },
11293 { .name = NULL } };
11294 char short_opts[] = "k";
11298 const char *mntpath;
11299 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11302 while ((c = getopt_long(argc, argv, short_opts,
11303 long_opts, NULL)) != -1) {
11306 detach_opt = PCC_DETACH_OPT_NONE;
11311 fprintf(stderr, "%s: option '%s' unrecognized\n",
11312 argv[0], argv[optind - 1]);
11317 mntpath = argv[optind++];
11319 while (optind < argc) {
11322 fid = argv[optind++];
11324 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11326 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
11327 "from PCC: %s\n", argv[0], fid, mntpath,
11336 static int lfs_pcc_state(int argc, char **argv)
11340 char fullpath[PATH_MAX];
11341 struct lu_pcc_state state;
11346 fprintf(stderr, "%s: must specify one or more file names\n",
11351 while (optind < argc) {
11354 path = argv[optind++];
11355 if (realpath(path, fullpath) == NULL) {
11356 fprintf(stderr, "%s: could not find path '%s': %s\n",
11357 argv[0], path, strerror(errno));
11363 rc2 = llapi_pcc_state_get(fullpath, &state);
11367 fprintf(stderr, "%s: cannot get PCC state of '%s': "
11368 "%s\n", argv[0], path, strerror(-rc2));
11372 printf("file: %s", path);
11373 printf(", type: %s", pcc_type2string(state.pccs_type));
11374 if (state.pccs_type == LU_PCC_NONE &&
11375 state.pccs_open_count == 0) {
11380 printf(", PCC file: %s", state.pccs_path);
11381 printf(", user number: %u", state.pccs_open_count);
11382 printf(", flags: %x", state.pccs_flags);
11389 * lfs_pcc_list_commands() - List lfs pcc commands.
11390 * @argc: The count of command line arguments.
11391 * @argv: Array of strings for command line arguments.
11393 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11395 * Return: 0 on success.
11397 static int lfs_pcc_list_commands(int argc, char **argv)
11399 char buffer[81] = "";
11401 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11408 * lfs_pcc() - Parse and execute lfs pcc commands.
11409 * @argc: The count of lfs pcc command line arguments.
11410 * @argv: Array of strings for lfs pcc command line arguments.
11412 * This function parses lfs pcc commands and performs the
11413 * corresponding functions specified in pcc_cmdlist[].
11415 * Return: 0 on success or an error code on failure.
11417 static int lfs_pcc(int argc, char **argv)
11419 char cmd[PATH_MAX];
11422 setlinebuf(stdout);
11424 Parser_init("lfs-pcc > ", pcc_cmdlist);
11426 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11428 program_invocation_short_name = cmd;
11430 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11432 rc = Parser_commands();
11434 return rc < 0 ? -rc : rc;
11437 static int lfs_list_commands(int argc, char **argv)
11439 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11441 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11446 int main(int argc, char **argv)
11450 /* Ensure that liblustreapi constructor has run */
11451 if (!llapi_liblustreapi_initialized())
11452 fprintf(stderr, "liblustreapi was not properly initialized\n");
11454 setlinebuf(stdout);
11457 Parser_init("lfs > ", cmdlist);
11459 progname = program_invocation_short_name; /* Used in error messages */
11461 llapi_set_command_name(argv[1]);
11462 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11463 llapi_clear_command_name();
11465 rc = Parser_commands();
11468 return rc < 0 ? -rc : rc;
11471 #ifdef _LUSTRE_IDL_H_
11472 /* Everything we need here should be included by lustreapi.h. */
11473 # error "lfs should not depend on lustre_idl.h"
11474 #endif /* _LUSTRE_IDL_H_ */