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/
33 * Author: Peter J. Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Robert Read <rread@clusterfs.com>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
57 #include <sys/types.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
78 #include <lnetconfig/cyaml.h>
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_unlink_foreign(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 struct pool_to_id_cbdata {
145 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
146 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
148 enum setstripe_origin {
158 static int lfs_setstripe_internal(int argc, char **argv,
159 enum setstripe_origin opc);
161 static inline int lfs_setstripe(int argc, char **argv)
163 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
166 static inline int lfs_setstripe_migrate(int argc, char **argv)
168 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
171 static inline int lfs_mirror_create(int argc, char **argv)
173 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
176 static inline int lfs_mirror_extend(int argc, char **argv)
178 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
181 static inline int lfs_mirror_split(int argc, char **argv)
183 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
186 static inline int lfs_mirror_delete(int argc, char **argv)
188 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
191 /* Setstripe and migrate share mostly the same parameters */
192 #define SSM_CMD_COMMON(cmd) \
193 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
194 " [--stripe-count|-c <stripe_count>]\n" \
195 " [--overstripe-count|-C <stripe_count>]\n" \
196 " [--stripe-index|-i <start_ost_idx>]\n" \
197 " [--stripe-size|-S <stripe_size>]\n" \
198 " [--extension-size|--ext-size|-z]\n" \
199 " [--layout|-L <pattern>]\n" \
200 " [--mirror-count|-N[mirror_count]]\n" \
201 " [--ost|-o <ost_indices>]\n" \
202 " [--pool|-p <pool_name>]\n" \
203 " [--yaml|-y <yaml_template_file>]\n" \
204 " [--copy=<lustre_src>]\n"
206 #define SSM_HELP_COMMON \
207 "\tstripe_count: Number of OSTs to stripe on (0=fs default, -1 all)\n" \
208 "\t Using -C instead of -c allows overstriping, which\n" \
209 "\t will place more than one stripe per OST if\n" \
210 "\t stripe_count is greater than the number of OSTs.\n" \
211 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
212 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
213 "\t Optional K, M, or G suffix (for KB, MB, GB\n" \
214 "\t respectively). Must be a multiple of 64KiB.\n" \
215 "\textension_size:\n" \
216 "\t Number of bytes the previous component is extended\n" \
217 "\t each time. Optional K, M, or G suffix (for KB,\n" \
218 "\t MB, GB respectively)\n" \
219 "\tpool_name: Name of OST pool to use (default none)\n" \
220 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n" \
221 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
222 "\t Indices be specified in a format of:\n" \
223 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
225 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
226 "\t If --pool is set with --ost then the OSTs\n" \
227 "\t must be the members of the pool.\n" \
228 "\tcomp_end: Extent end of component, start after previous end.\n" \
229 "\t Optional K, M, or G suffix (for KiB, MiB, GiB), or\n" \
230 "\t -1 or 'eof' for max file size). Must be a multiple\n" \
231 "\t of stripe_size and a multiple of 64KiB.\n" \
232 "\tyaml_template_file:\n" \
233 "\t YAML layout template file, can't be used with -c,\n" \
234 "\t -i, -S, -p, -o, or -E arguments.\n" \
235 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
236 "\t another lustre file or directory, can't used with\n" \
237 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
239 #define MIRROR_CREATE_HELP \
240 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
241 "\t setstripe layout options\n" \
242 "\t It defaults to 1 if not specified; if specified,\n" \
243 "\t it must follow the option without a space.\n" \
244 "\t The option can also be repeated multiple times to\n" \
245 "\t separate mirrors that have different layouts.\n" \
246 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
247 "\t It can be a plain layout or a composite layout.\n" \
248 "\t If not specified, the stripe options inherited\n" \
249 "\t from the previous component will be used.\n" \
250 "\tflags: set flags to the component of the current mirror.\n" \
251 "\t Only \"prefer\" flag is supported so far.\n"
253 #define MIRROR_EXTEND_HELP \
255 "\tvictim_file: The layout of victim_file will be split and used\n" \
256 "\t as a mirror added to the mirrored file.\n" \
257 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
258 "\t from victim file(s) in case the victim file(s)\n" \
259 "\t contains the same data as the original mirrored\n" \
262 #define MIRROR_EXTEND_USAGE \
263 " {--mirror-count|-N[mirror_count]}\n" \
264 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
267 #define SETSTRIPE_USAGE \
268 SSM_CMD_COMMON("setstripe") \
269 MIRROR_EXTEND_USAGE \
270 " <directory|filename>\n" \
274 #define MIGRATE_USAGE \
275 SSM_CMD_COMMON("migrate ") \
276 " [--block|-b] [--non-block|-n]\n" \
277 " [--non-direct|-D] [--verbose|-v]\n" \
281 "\tblock: Block file access during data migration (default)\n" \
282 "\tnon-block: Abort migrations if concurrent access is detected\n" \
283 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
284 "\tverbose: Print each filename as it is migrated\n" \
286 #define SETDIRSTRIPE_USAGE \
287 " [--mdt-count|-c stripe_count>\n" \
288 " [--mdt-hash|-H mdt_hash]\n" \
289 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
290 " [--default|-D] [--mode|-o mode] <dir>\n" \
291 "\tstripe_count: stripe count of the striped directory\n" \
292 "\tmdt_index: MDT index of first stripe\n" \
293 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
294 " crush CRUSH hash algorithm (default)\n" \
295 " fnv_1a_64 FNV-1a hash algorithm\n" \
296 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
297 "\tdefault_stripe: set default dirstripe of the directory\n" \
298 "\tmode: the file access permission of the directory (octal)\n" \
299 "To create dir with a foreign (free format) layout :\n" \
300 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
301 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
302 "\tmode: the mode of the directory\n" \
303 "\tforeign_type: none or symlink\n"
306 * command_t mirror_cmdlist - lfs mirror commands.
308 command_t mirror_cmdlist[] = {
309 { .pc_name = "create", .pc_func = lfs_mirror_create,
310 .pc_help = "Create a mirrored file.\n"
311 "usage: lfs mirror create "
312 "<--mirror-count|-N[mirror_count]> "
313 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
314 MIRROR_CREATE_HELP },
315 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
316 .pc_help = "Delete a mirror from a file.\n"
317 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
318 "\t --component-id|--comp-id|-I <comp_id> |\n"
319 "\t -p <pool>} <mirrored_file> ...\n"
321 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
322 .pc_help = "Extend a mirrored file.\n"
323 "usage: lfs mirror extend "
324 "<--mirror-count|-N[mirror_count]> [--no-verify] "
325 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
326 MIRROR_EXTEND_HELP },
327 { .pc_name = "split", .pc_func = lfs_mirror_split,
328 .pc_help = "Split a mirrored file.\n"
329 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
330 "\t --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
331 "\t [-f <new_file>] <mirrored_file> ...\n"
332 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
333 "\t can be fetched by lfs getstripe command.\n"
334 "\tcomp_id: Unique component ID within a mirror.\n"
335 "\tpool: Components on specified pool.\n"
336 "\tnew_file: This option indicates the layout of the split\n"
337 "\t mirror will be stored into. If not specified,\n"
338 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
339 "\t will be used.\n" },
340 { .pc_name = "read", .pc_func = lfs_mirror_read,
341 .pc_help = "Read the content of a specified mirror of a file.\n"
342 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
343 "[--outfile|-o <output_file>] <mirrored_file>\n" },
344 { .pc_name = "write", .pc_func = lfs_mirror_write,
345 .pc_help = "Write to a specified mirror of a file.\n"
346 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
347 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
348 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
349 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
350 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
351 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
352 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
353 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
354 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
355 "<mirrored file> [<mirrored file2>...]\n"},
356 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
357 .pc_help = "Verify mirrored file(s).\n"
358 "usage: lfs mirror verify "
359 "[--only <mirror_id,mirror_id2[,...]>] "
360 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
361 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
362 .pc_help = "list commands supported by lfs mirror"},
363 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
364 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
365 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
370 * command_t pcc_cmdlist - lfs pcc commands.
372 command_t pcc_cmdlist[] = {
373 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
374 .pc_help = "Attach given files to the Persistent Client Cache.\n"
375 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
376 "\t-i: archive id for RW-PCC\n" },
377 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
378 .pc_help = "Attach given files into PCC by FID(s).\n"
379 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
381 "\t-i: archive id for RW-PCC\n"
382 "\t-m: Lustre mount point\n" },
383 { .pc_name = "state", .pc_func = lfs_pcc_state,
384 .pc_help = "Display the PCC state for given files.\n"
385 "usage: lfs pcc state <file> ...\n" },
386 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
387 .pc_help = "Detach given files from the Persistent Client Cache.\n"
388 "usage: lfs pcc detach <file> ...\n" },
389 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
390 .pc_help = "Detach given files from PCC by FID(s).\n"
391 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
392 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
393 .pc_help = "list commands supported by lfs pcc"},
394 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
395 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
396 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
400 /* all available commands */
401 command_t cmdlist[] = {
402 {"setstripe", lfs_setstripe, 0,
403 "To create a file with specified striping/composite layout, or\n"
404 "create/replace the default layout on an existing directory:\n"
405 SSM_CMD_COMMON("setstripe")
407 " <directory|filename>\n"
409 "To add component(s) to an existing composite file:\n"
410 SSM_CMD_COMMON("setstripe --component-add")
412 "To totally delete the default striping from an existing directory:\n"
413 "usage: setstripe [--delete|-d] <directory>\n"
415 "To create a mirrored file or set s default mirror layout on a directory:\n"
416 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
418 "To delete the last component(s) from an existing composite file\n"
419 "(note that this will also delete any data in those components):\n"
420 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
421 " [--component-flags|-F <comp_flags>]\n"
423 "\tcomp_id: Unique component ID to delete\n"
424 "\tcomp_flags: 'init' indicating all instantiated components\n"
425 "\t '^init' indicating all uninstantiated components\n"
426 "\t-I and -F cannot be specified at the same time\n"
428 "To set or clear flags on a specific component\n"
429 "(note that this command can only be applied to mirrored files:\n"
430 "usage: setstripe --comp-set {-I comp_id|--comp-flags=comp_flags}\n"
433 "To create a file with a foreign (free format) layout:\n"
434 "usage: setstripe --foreign[=<foreign_type>]\n"
435 " --xattr|-x <layout_string> [--flags <hex>]\n"
436 " [--mode <mode>] <filename>\n"},
437 {"getstripe", lfs_getstripe, 0,
438 "To list the layout pattern for a given file or files in a\n"
439 "directory or recursively for all files in a directory tree.\n"
440 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
441 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
442 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
443 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
444 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
445 " [--component-id[=comp_id]|-I[comp_id]]\n"
446 " [--component-flags[=comp_flags]]\n"
447 " [--component-count]\n"
448 " [--extension-size|--ext-size|-z]\n"
449 " [--component-start[=[+-]comp_start]]\n"
450 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
451 " [[!] --mirror-index=[+-]<index> |\n"
452 " [!] --mirror-id=[+-]<id>] [--mirror-count|-N]\n"
453 " <directory|filename> ..."},
454 {"setdirstripe", lfs_setdirstripe, 0,
455 "Create striped directory on specified MDT, same as mkdir.\n"
456 "May be restricted to root or group users, depending on settings.\n"
457 "usage: setdirstripe [OPTION] <directory>\n"
459 {"getdirstripe", lfs_getdirstripe, 0,
460 "To list the layout pattern info for a given directory\n"
461 "or recursively for all directories in a directory tree.\n"
462 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
463 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
464 " [--recursive|-r] [--yaml|-y]\n"
465 " [--verbose|-v] [--default|-D] <dir> ..."},
466 {"mkdir", lfs_setdirstripe, 0,
467 "Create striped directory on specified MDT, same as setdirstripe.\n"
468 "usage: mkdir [OPTION] <directory>\n"
470 {"rm_entry", lfs_rmentry, 0,
471 "To remove the name entry of the remote directory. Note: This\n"
472 "command will only delete the name entry, i.e. the remote directory\n"
473 "will become inaccessable after this command. This can only be done\n"
474 "by the administrator\n"
475 "usage: rm_entry <dir>\n"},
476 {"unlink_foreign", lfs_unlink_foreign, 0,
477 "To remove the foreign file/dir.\n"
478 "Note: This is for files/dirs prevented to be removed using\n"
479 "unlink/rmdir, but works also for regular ones\n"
480 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
481 {"pool_list", lfs_poollist, 0,
482 "List pools or pool OSTs\n"
483 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
484 {"find", lfs_find, 0,
485 "find files matching given attributes recursively in directory tree.\n"
486 "usage: find <directory|filename> ...\n"
487 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
488 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
489 " [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
490 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
491 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
492 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
493 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
494 " [[!] --stripe-count|-c [+-]<stripes>]\n"
495 " [[!] --stripe-index|-i <index,...>]\n"
496 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
497 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
498 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
499 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
500 " [[!] --projid <projid>]\n"
501 " [[!] --layout|-L released,raid0,mdt]\n"
502 " [[!] --foreign[=<foreign_type>]]\n"
503 " [[!] --component-count [+-]<comp_cnt>]\n"
504 " [[!] --component-start [+-]N[kMGTPE]]\n"
505 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
506 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
507 " [[!] --mirror-count|-N [+-]<n>]\n"
508 " [[!] --mirror-state <[^]state>]\n"
509 " [[!] --mdt-count|-T [+-]<stripes>]\n"
510 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
511 " [[!] --mdt-index|-m <uuid|index,...>]\n"
512 "\t !: used before an option indicates 'NOT' requested attribute\n"
513 "\t -: used before a value indicates less than requested value\n"
514 "\t +: used before a value indicates more than requested value\n"
515 "\t ^: used before a flag indicates to exclude it\n"},
516 {"check", lfs_check, 0,
517 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
518 "or all the servers (MGTs, MDTs and OSTs).\n"
519 "usage: check <mgts|osts|mdts|all>"},
520 {"osts", lfs_osts, 0, "list OSTs connected to client "
521 "[for specified path only]\n" "usage: osts [path]"},
522 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
523 "[for specified path only]\n" "usage: mdts [path]"},
525 "report filesystem disk space usage or inodes usage "
526 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
527 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
528 " [--pool|-p <fsname>[.<pool>]] [path]"},
529 {"getname", lfs_getname, 0,
530 "list instances and specified mount points [for specified path only]\n"
531 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
532 #ifdef HAVE_SYS_QUOTA_H
533 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
534 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
535 " -b <block-softlimit> -B <block-hardlimit>\n"
536 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
537 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
538 " [--block-softlimit <block-softlimit>]\n"
539 " [--block-hardlimit <block-hardlimit>]\n"
540 " [--inode-softlimit <inode-softlimit>]\n"
541 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
542 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
543 " [--block-grace 'notify'|<block-grace>]\n"
544 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
545 " setquota <-U|-G|-P>\n"
546 " -b <block-softlimit> -B <block-hardlimit>\n"
547 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
548 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
549 " [--block-softlimit <block-softlimit>]\n"
550 " [--block-hardlimit <block-hardlimit>]\n"
551 " [--inode-softlimit <inode-softlimit>]\n"
552 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
553 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
555 " -b can be used instead of --block-softlimit/--block-grace\n"
556 " -B can be used instead of --block-hardlimit\n"
557 " -i can be used instead of --inode-softlimit/--inode-grace\n"
558 " -I can be used instead of --inode-hardlimit\n"
559 " -d can be used instead of --default\n\n"
560 "Note: The total quota space will be split into many qunits and\n"
561 " balanced over all server targets, the minimal qunit size is\n"
562 " 1M bytes for block space and 1K inodes for inode space.\n\n"
563 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
564 " Quota space rebalancing process will stop when this mininum\n"
565 " value is reached. As a result, quota exceeded can be returned\n"
566 " while many targets still have 1MB or 1K inodes of spare\n"
568 " When setting the grace time, 'notify' can be used as grace to\n"
569 " be notified after the quota is over soft limit but prevents\n"
570 " the soft limit from becoming the hard limit."},
571 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
572 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
574 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
575 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
576 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
577 {"project", lfs_project, 0,
578 "Change or list project attribute for specified file or directory.\n"
579 "usage: project [-d|-r] <file|directory...>\n"
580 " list project ID and flags on file(s) or directories\n"
581 " project [-p id] [-s] [-r] <file|directory...>\n"
582 " set project ID and/or inherit flag for specified file(s) or directories\n"
583 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
584 " check project ID and flags on file(s) or directories, print outliers\n"
585 " project -C [-r] [-k] <file|directory...>\n"
586 " clear the project inherit flag and ID on the file or directory\n"
589 {"flushctx", lfs_flushctx, 0,
590 "Flush security context for current user.\n"
591 "usage: flushctx [-k] [-r] [mountpoint...]"},
592 {"changelog", lfs_changelog, 0,
593 "Show the metadata changes on an MDT."
594 "\nusage: changelog <mdtname> [startrec [endrec]]"},
595 {"changelog_clear", lfs_changelog_clear, 0,
596 "Indicate that old changelog records up to <endrec> are no longer of "
597 "interest to consumer <id>, allowing the system to free up space.\n"
598 "An <endrec> of 0 means all records.\n"
599 "usage: changelog_clear <mdtname> <id> <endrec>"},
600 {"fid2path", lfs_fid2path, 0,
601 "Resolve the full path(s) for given FID(s). For a specific hardlink "
602 "specify link number <linkno>.\n"
603 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
604 "<fsname|root> <fid>..."},
605 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
606 "usage: path2fid [--parents] <path> ..."},
607 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
608 "usage: rmfid <fsname|rootpath> <fid> ..."},
609 {"data_version", lfs_data_version, 0, "Display file data version for "
610 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
611 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
612 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
613 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
614 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
615 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
616 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
618 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
619 "[--archived] [--lost] <file> ..."},
620 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
621 "given files.\n" "usage: hsm_action <file> ..."},
622 {"hsm_archive", lfs_hsm_archive, 0,
623 "Archive file to external storage.\n"
624 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
626 {"hsm_restore", lfs_hsm_restore, 0,
627 "Restore file from external storage.\n"
628 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
629 {"hsm_release", lfs_hsm_release, 0,
630 "Release files from Lustre.\n"
631 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
632 {"hsm_remove", lfs_hsm_remove, 0,
633 "Remove file copy from external storage.\n"
634 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
636 " (FILE [FILE ...] | "
637 "--mntpath MOUNTPATH FID [FID ...])\n"
639 "Note: To remove an archived copy of a file already deleted from a "
641 "--mntpath option and a list of FIDs must be specified"
643 {"hsm_cancel", lfs_hsm_cancel, 0,
644 "Cancel requests related to specified files.\n"
645 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
646 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
647 "usage: swap_layouts <path1> <path2>"},
648 {"migrate", lfs_setstripe_migrate, 0,
649 "migrate a directory between MDTs.\n"
650 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
651 " [--mdt-hash|-H] <hash_type>\n"
652 " [--mdt-index|-m] <start_mdt_index>\n"
655 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
656 " it's the MDT index of first stripe\n"
657 "\tmdt_count: number of MDTs to stripe a directory over\n"
658 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
659 " all_char (type 1)sum of characters % MDT_COUNT\n"
660 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
661 " crush (type 3)CRUSH hash algorithm\n"
663 "migrate file objects from one OST "
664 "layout\nto another (may be not safe with concurent writes).\n"
666 "[--stripe-count|-c] <stripe_count>\n"
667 "[--overstripe-count|-C] <stripe_count>\n"
668 " [--stripe-index|-i] <start_ost_index>\n"
669 " [--stripe-size|-S] <stripe_size>\n"
670 " [--pool|-p] <pool_name>\n"
671 " [--ost|-o] <ost_indices>\n"
673 " [--non-block|-n]\n"
674 " [--non-direct|-D]\n"
675 " <file|directory>\n"
676 "\tstripe_count: number of OSTs to stripe a file over\n"
677 "\t Using -C instead of -c allows overstriping, which\n"
678 "\t will place more than one stripe per OST if\n"
679 "\t stripe_count is greater than the number of OSTs\n"
680 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
681 "\tstripe_size: number of bytes to store before moving to the next OST\n"
682 "\tpool_name: name of the predefined pool of OSTs\n"
683 "\tost_indices: OSTs to stripe over, in order\n"
684 "\tblock: Block file access during data migration (default)\n"
685 "\tnon-block: Abort migrations if concurrent access is detected\n"
686 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
688 "To move directories between MDTs. This command is deprecated, "
689 "use \"migrate\" instead.\n"
690 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
692 {"ladvise", lfs_ladvise, 0,
693 "Provide servers with advice about access patterns for a file.\n"
694 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
695 " [--background|-b] [--unset|-u]\n\n"
696 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
697 " {[--mode|-m [READ,WRITE]}\n"
699 {"mirror", lfs_mirror, mirror_cmdlist,
700 "lfs commands used to manage files with mirrored components:\n"
701 "lfs mirror create - create a mirrored file or directory\n"
702 "lfs mirror extend - add mirror(s) to an existing file\n"
703 "lfs mirror split - split a mirror from an existing mirrored file\n"
704 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
705 "lfs mirror read - read a mirror content of a mirrored file\n"
706 "lfs mirror write - write to a mirror of a mirrored file\n"
707 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
708 "lfs mirror verify - verify mirrored file(s)\n"},
709 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
710 "usage: getsom [-s] [-b] [-f] <path>\n"
711 "\t-s: Only show the size value of the SOM data for a given file\n"
712 "\t-b: Only show the blocks value of the SOM data for a given file\n"
713 "\t-f: Only show the flags value of the SOM data for a given file\n"},
714 {"heat_get", lfs_heat_get, 0,
715 "To get heat of files.\n"
716 "usage: heat_get <file> ...\n"},
717 {"heat_set", lfs_heat_set, 0,
718 "To set heat flags of files.\n"
719 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
720 "\t--clear|-c: Clear file heat for given files\n"
721 "\t--off|-o: Turn off file heat for given files\n"
722 "\t--on|-O: Turn on file heat for given files\n"},
723 {"pcc", lfs_pcc, pcc_cmdlist,
724 "lfs commands used to interact with PCC features:\n"
725 "lfs pcc attach - attach given files to Persistent Client Cache\n"
726 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
727 "lfs pcc state - display the PCC state for given files\n"
728 "lfs pcc detach - detach given files from Persistent Client Cache\n"
729 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
730 {"help", Parser_help, 0, "help"},
731 {"exit", Parser_quit, 0, "quit"},
732 {"quit", Parser_quit, 0, "quit"},
733 {"--version", Parser_version, 0,
734 "output build version of the utility and exit"},
735 {"--list-commands", lfs_list_commands, 0,
736 "list commands supported by the utility and exit"},
740 static int check_hashtype(const char *hashtype)
742 int type_num = atoi(hashtype);
745 /* numeric hash type */
746 if (hashtype && strlen(hashtype) == 1 &&
747 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
749 /* string hash type */
750 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
751 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
757 static uint32_t check_foreign_type_name(const char *foreign_type_name)
761 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
762 if (!lu_foreign_types[i].lft_name)
764 if (strcmp(foreign_type_name,
765 lu_foreign_types[i].lft_name) == 0)
766 return lu_foreign_types[i].lft_type;
769 return LU_FOREIGN_TYPE_UNKNOWN;
772 static const char *error_loc = "syserror";
775 MIGRATION_NONBLOCK = 0x0001,
776 MIGRATION_MIRROR = 0x0002,
777 MIGRATION_NONDIRECT = 0x0004,
778 MIGRATION_VERBOSE = 0x0008,
782 migrate_open_files(const char *name, __u64 migration_flags,
783 const struct llapi_stripe_param *param,
784 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
791 char parent[PATH_MAX];
792 char volatile_file[PATH_MAX];
798 if (!param && !layout) {
799 error_loc = "layout information";
803 /* search for file directory pathname */
804 if (strlen(name) > sizeof(parent) - 1) {
805 error_loc = "source file name";
809 strncpy(parent, name, sizeof(parent));
810 ptr = strrchr(parent, '/');
812 if (!getcwd(parent, sizeof(parent))) {
813 error_loc = "getcwd";
817 if (ptr == parent) /* leading '/' */
822 /* open file, direct io */
823 /* even if the file is only read, WR mode is nedeed to allow
827 if (!(migration_flags & MIGRATION_NONDIRECT))
829 fd = open(name, rflags);
832 error_loc = "cannot open source file";
836 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
838 error_loc = "cannot get MDT index";
843 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
844 mode_t open_mode = S_IRUSR | S_IWUSR;
846 random_value = random();
847 rc = snprintf(volatile_file, sizeof(volatile_file),
848 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
849 mdt_index, random_value);
850 if (rc >= sizeof(volatile_file)) {
855 /* create, open a volatile file, use caching (ie no directio) */
857 /* Returns -1 and sets errno on error: */
858 fdv = llapi_layout_file_open(volatile_file, open_flags,
863 /* Does the right thing on error: */
864 fdv = llapi_file_open_param(volatile_file, open_flags,
867 } while (fdv < 0 && (rc = fdv) == -EEXIST);
870 error_loc = "cannot create volatile file";
875 * In case the MDT does not support creation of volatile files
876 * we should try to unlink it.
878 (void)unlink(volatile_file);
881 * Not-owner (root?) special case.
882 * Need to set owner/group of volatile file like original.
883 * This will allow to pass related check during layout_swap.
888 error_loc = "cannot stat source file";
892 rc = fstat(fdv, &stv);
895 error_loc = "cannot stat volatile";
899 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
900 rc = fchown(fdv, st.st_uid, st.st_gid);
903 error_loc = "cannot change ownwership of volatile";
922 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
924 struct llapi_layout *layout;
925 size_t buf_size = 4 * 1024 * 1024;
929 size_t page_size = sysconf(_SC_PAGESIZE);
933 layout = llapi_layout_get_by_fd(fd_src, 0);
935 uint64_t stripe_size;
937 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
939 buf_size = stripe_size;
941 llapi_layout_free(layout);
944 /* Use a page-aligned buffer for direct I/O */
945 rc = posix_memalign(&buf, page_size, buf_size);
949 sparse = llapi_file_is_sparse(fd_src);
951 rc = ftruncate(fd_dst, pos);
960 size_t to_read, to_write;
963 if (sparse && pos >= data_end) {
966 data_off = llapi_data_seek(fd_src, pos, &data_size);
968 /* Non-fatal, switch to full copy */
972 /* hole at the end of file, truncate up to it */
974 rc = ftruncate(fd_dst, data_off);
978 pos = data_off & ~(page_size - 1);
979 data_end = data_off + data_size;
980 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
981 to_read = MIN(to_read, buf_size);
987 rc = check_file(fd_src);
992 rsize = pread(fd_src, buf, to_read, pos);
1002 while (to_write > 0) {
1005 written = pwrite(fd_dst, buf, to_write, pos);
1011 to_write -= written;
1013 if (rc || rsize < to_read)
1021 /* Try to avoid page cache pollution after migration. */
1022 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
1023 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1029 static int migrate_copy_timestamps(int fd, int fdv)
1033 if (fstat(fd, &st) == 0) {
1034 struct timeval tv[2] = {
1035 {.tv_sec = st.st_atime},
1036 {.tv_sec = st.st_mtime}
1039 return futimes(fdv, tv);
1045 static int migrate_block(int fd, int fdv)
1052 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1054 error_loc = "cannot get dataversion";
1063 * The grouplock blocks all concurrent accesses to the file.
1064 * It has to be taken after llapi_get_data_version as it would
1067 rc = llapi_group_lock(fd, gid);
1069 error_loc = "cannot get group lock";
1073 rc = migrate_copy_data(fd, fdv, NULL);
1075 error_loc = "data copy failed";
1079 /* Make sure we keep original atime/mtime values */
1080 rc = migrate_copy_timestamps(fd, fdv);
1082 error_loc = "timestamp copy failed";
1088 * for a migration we need to check data version on file did
1091 * Pass in gid=0 since we already own grouplock.
1093 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1094 SWAP_LAYOUTS_CHECK_DV1);
1095 if (rc == -EAGAIN) {
1096 error_loc = "file changed";
1098 } else if (rc < 0) {
1099 error_loc = "cannot swap layout";
1104 rc2 = llapi_group_unlock(fd, gid);
1105 if (rc2 < 0 && rc == 0) {
1106 error_loc = "unlock group lock";
1114 * Internal helper for migrate_copy_data(). Check lease and report error if
1117 * \param[in] fd File descriptor on which to check the lease.
1119 * \retval 0 Migration can keep on going.
1120 * \retval -errno Error occurred, abort migration.
1122 static int check_lease(int fd)
1126 rc = llapi_lease_check(fd);
1128 return 0; /* llapi_check_lease returns > 0 on success. */
1133 static int migrate_nonblock(int fd, int fdv)
1139 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1141 error_loc = "cannot get data version";
1145 rc = migrate_copy_data(fd, fdv, check_lease);
1147 error_loc = "data copy failed";
1151 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1153 error_loc = "cannot get data version";
1159 error_loc = "source file changed";
1163 /* Make sure we keep original atime/mtime values */
1164 rc = migrate_copy_timestamps(fd, fdv);
1166 error_loc = "timestamp copy failed";
1174 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1176 struct pool_to_id_cbdata data = { .pool = pool };
1177 struct llapi_layout *layout = NULL;
1180 layout = llapi_layout_get_by_path(fname, 0);
1183 "error %s: file '%s' couldn't get layout: rc=%d\n",
1184 progname, fname, errno);
1188 rc = llapi_layout_sanity(layout, false, true);
1190 llapi_layout_sanity_perror(errno);
1193 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1202 llapi_layout_free(layout);
1206 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1207 __u32 flags, __u32 neg_flags)
1210 __u32 flags_array[2];
1217 "error %s: neither component id nor pool is specified\n",
1221 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1227 ids[count] = comp_id;
1228 flags_array[count] = flags;
1233 if (neg_flags & LCME_FL_STALE) {
1235 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1240 ids[count] = comp_id;
1241 flags_array[count] = neg_flags | LCME_FL_NEG;
1245 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1247 if (errno == EUCLEAN) {
1250 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1251 progname, comp_id, fname);
1254 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1255 progname, comp_id, fname, flags, neg_flags);
1262 static int lfs_component_del(char *fname, __u32 comp_id,
1263 __u32 flags, __u32 neg_flags)
1267 if (flags && neg_flags)
1270 if (!flags && neg_flags)
1271 flags = neg_flags | LCME_FL_NEG;
1273 if ((flags && comp_id) || (!flags && !comp_id))
1277 if (flags & ~LCME_KNOWN_FLAGS) {
1279 "%s setstripe: unknown flags %#x\n",
1283 } else if (comp_id > LCME_ID_MAX) {
1284 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1289 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1292 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1293 progname, comp_id, fname, strerror(errno));
1297 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1304 rc = llapi_layout_file_comp_add(fname, layout);
1306 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1307 fname, strerror(errno));
1311 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1312 struct llapi_layout *layout)
1320 fd = lstat(fname, &st);
1321 if (fd == 0 && S_ISDIR(st.st_mode))
1322 open_flags = O_DIRECTORY | O_RDONLY;
1324 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1326 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1327 S_ISDIR(st.st_mode) ?
1328 "set default composite layout for" :
1329 "create composite file",
1330 fname, strerror(errno));
1334 static int lfs_migrate(char *name, __u64 migration_flags,
1335 struct llapi_stripe_param *param,
1336 struct llapi_layout *layout)
1338 struct llapi_layout *existing;
1339 uint64_t dom_new, dom_cur;
1344 rc = migrate_open_files(name, migration_flags, param, layout,
1349 rc = llapi_layout_dom_size(layout, &dom_new);
1351 error_loc = "cannot get new layout DoM size";
1354 /* special case for migration to DOM layout*/
1355 existing = llapi_layout_get_by_fd(fd, 0);
1357 error_loc = "cannot get existing layout";
1361 rc = llapi_layout_dom_size(existing, &dom_cur);
1363 error_loc = "cannot get current layout DoM size";
1368 * if file has DoM layout already then migration is possible to
1369 * the new layout with the same DoM component via swap layout,
1370 * if new layout used bigger DOM size, then mirroring is used
1372 if (dom_new > dom_cur) {
1373 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1376 error_loc = "cannot migrate to DOM layout";
1380 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1382 * Blocking mode (forced if servers do not support file lease).
1383 * It is also the default mode, since we cannot distinguish
1384 * between a broken lease and a server that does not support
1385 * atomic swap/close (LU-6785)
1387 rc = migrate_block(fd, fdv);
1391 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1393 error_loc = "cannot get lease";
1397 rc = migrate_nonblock(fd, fdv);
1399 llapi_lease_release(fd);
1404 * Atomically put lease, swap layouts and close.
1405 * for a migration we need to check data version on file did
1408 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1410 error_loc = "cannot swap layout";
1422 fprintf(stderr, "error: %s: %s: %s: %s\n",
1423 progname, name, error_loc, strerror(-rc));
1424 else if (migration_flags & MIGRATION_VERBOSE)
1425 printf("%s\n", name);
1430 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1433 char *dup_string = NULL;
1439 if (!string || !string[0])
1442 dup_string = strdup(string);
1444 llapi_printf(LLAPI_MSG_ERROR,
1445 "%s: insufficient memory\n",
1450 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1454 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1455 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1456 const char *comp_name = comp_flags_table[i].cfn_name;
1458 if (strcmp(name, comp_name) == 0) {
1459 *flags |= comp_flag;
1461 } else if (strncmp(name, "^", 1) == 0 &&
1462 strcmp(name + 1, comp_name) == 0) {
1463 *neg_flags |= comp_flag;
1468 llapi_printf(LLAPI_MSG_ERROR,
1469 "%s: component flag '%s' not supported\n",
1476 if (!*flags && !*neg_flags)
1479 /* don't allow to set and exclude the same flag */
1480 if (*flags & *neg_flags)
1488 static int mdthash_input(char *string, __u32 *inflags,
1489 __u32 *exflags, __u32 *type)
1496 {"migrating", LMV_HASH_FLAG_MIGRATION},
1497 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1498 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1507 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1511 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1512 if (strcmp(name, mhflist[i].name) == 0 ||
1513 name[0] == mhflist[i].name[0]) {
1514 *inflags |= mhflist[i].flag;
1516 } else if (name[0] == '^' &&
1517 (strcmp(name + 1, mhflist[i].name) == 0 ||
1518 name[1] == mhflist[i].name[0])) {
1519 *exflags |= mhflist[i].flag;
1524 i = check_hashtype(name);
1529 llapi_printf(LLAPI_MSG_ERROR,
1530 "%s: invalid mdt_hash value '%s'\n",
1536 /* don't allow to include and exclude the same flag */
1537 if (*inflags & *exflags) {
1538 llapi_printf(LLAPI_MSG_ERROR,
1539 "%s: include and exclude same flag '%s'\n",
1547 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1555 if (strncmp(string, "^", 1) == 0) {
1556 *neg_state = llapi_layout_string_flags(string + 1);
1557 if (*neg_state != 0)
1560 *state = llapi_layout_string_flags(string);
1565 llapi_printf(LLAPI_MSG_ERROR,
1566 "%s: mirrored file state '%s' not supported\n",
1572 * struct mirror_args - Command-line arguments for mirror(s).
1573 * @m_count: Number of mirrors to be created with this layout.
1574 * @m_flags: Mirror level flags, only 'prefer' is supported.
1575 * @m_layout: Mirror layout.
1576 * @m_file: A victim file. Its layout will be split and used as a mirror.
1577 * @m_next: Point to the next node of the list.
1579 * Command-line arguments for mirror(s) will be parsed and stored in
1580 * a linked list that consists of this structure.
1582 struct mirror_args {
1585 struct llapi_layout *m_layout;
1587 struct mirror_args *m_next;
1592 * enum mirror_flags - Flags for extending a mirrored file.
1593 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1594 * in case the victim file(s) contains the same data as the
1595 * original mirrored file.
1596 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1597 * @MF_COMP_ID: specified component id instead of mirror id
1599 * Flags for extending a mirrored file.
1609 * mirror_create_sanity_check() - Check mirror list.
1610 * @list: A linked list that stores the mirror arguments.
1612 * This function does a sanity check on @list for creating
1615 * Return: 0 on success or a negative error code on failure.
1617 static int mirror_create_sanity_check(const char *fname,
1618 struct mirror_args *list)
1621 bool has_m_file = false;
1622 bool has_m_layout = false;
1628 struct llapi_layout *layout;
1630 layout = llapi_layout_get_by_path(fname, 0);
1633 "error: %s: file '%s' couldn't get layout\n",
1638 rc = llapi_layout_sanity(layout, false, true);
1640 llapi_layout_free(layout);
1643 llapi_layout_sanity_perror(rc);
1651 llapi_layout_free(list->m_layout);
1654 llapi_layout_get_by_path(list->m_file, 0);
1655 if (!list->m_layout) {
1657 "error: %s: file '%s' has no layout\n",
1658 progname, list->m_file);
1662 has_m_layout = true;
1663 if (!list->m_layout) {
1664 fprintf(stderr, "error: %s: no mirror layout\n",
1670 rc = llapi_layout_sanity(list->m_layout, false, true);
1672 llapi_layout_sanity_perror(rc);
1676 list = list->m_next;
1679 if (has_m_file && has_m_layout) {
1681 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1689 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1691 __u32 mirror_flags = *(__u32 *)cbdata;
1695 rc = llapi_layout_comp_flags_get(layout, &flags);
1700 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1705 return LLAPI_LAYOUT_ITER_CONT;
1709 * mirror_create() - Create a mirrored file.
1710 * @fname: The file to be created.
1711 * @mirror_list: A linked list that stores the mirror arguments.
1713 * This function creates a mirrored file @fname with the mirror(s)
1714 * from @mirror_list.
1716 * Return: 0 on success or a negative error code on failure.
1718 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1720 struct llapi_layout *layout = NULL;
1721 struct mirror_args *cur_mirror = NULL;
1722 uint16_t mirror_count = 0;
1726 rc = mirror_create_sanity_check(NULL, mirror_list);
1730 cur_mirror = mirror_list;
1731 while (cur_mirror) {
1732 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1734 &cur_mirror->m_flags);
1737 fprintf(stderr, "%s: failed to set mirror flags\n",
1742 for (i = 0; i < cur_mirror->m_count; i++) {
1743 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1747 "error: %s: merge layout failed: %s\n",
1748 progname, strerror(errno));
1752 mirror_count += cur_mirror->m_count;
1753 cur_mirror = cur_mirror->m_next;
1757 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1761 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1764 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1765 progname, strerror(errno));
1769 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1777 llapi_layout_free(layout);
1782 * Compare files and check lease on @fd.
1784 * \retval bytes number of bytes are the same
1786 static ssize_t mirror_file_compare(int fd, int fdv)
1788 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1790 ssize_t bytes_done = 0;
1791 ssize_t bytes_read = 0;
1793 buf = malloc(buflen * 2);
1798 if (!llapi_lease_check(fd)) {
1799 bytes_done = -EBUSY;
1803 bytes_read = read(fd, buf, buflen);
1804 if (bytes_read <= 0)
1807 if (bytes_read != read(fdv, buf + buflen, buflen))
1811 * XXX: should compute the checksum on each buffer and then
1812 * compare checksum to avoid cache collision
1814 if (memcmp(buf, buf + buflen, bytes_read))
1817 bytes_done += bytes_read;
1825 static int mirror_extend_file(const char *fname, const char *victim_file,
1826 enum mirror_flags mirror_flags)
1831 struct stat stbuf_v;
1832 struct ll_ioc_lease *data = NULL;
1835 fd = open(fname, O_RDWR);
1837 error_loc = "open source file";
1842 fdv = open(victim_file, O_RDWR);
1844 error_loc = "open target file";
1849 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1850 error_loc = "stat source or target file";
1855 if (stbuf.st_dev != stbuf_v.st_dev) {
1856 error_loc = "stat source and target file";
1861 /* mirrors should be of the same size */
1862 if (stbuf.st_size != stbuf_v.st_size) {
1863 error_loc = "file sizes don't match";
1868 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1870 error_loc = "cannot get lease";
1874 if (!(mirror_flags & MF_NO_VERIFY)) {
1876 /* mirrors should have the same contents */
1877 ret = mirror_file_compare(fd, fdv);
1878 if (ret != stbuf.st_size) {
1879 error_loc = "file busy or contents don't match";
1880 rc = ret < 0 ? ret : -EINVAL;
1885 /* Get rid of caching pages from clients */
1886 rc = llapi_file_flush(fd);
1888 error_loc = "cannot get data version";
1892 rc = llapi_file_flush(fdv);
1894 error_loc = "cannot get data version";
1898 /* Make sure we keep original atime/mtime values */
1899 rc = migrate_copy_timestamps(fd, fdv);
1901 error_loc = "cannot copy timestamp";
1905 /* Atomically put lease, merge layouts and close. */
1906 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1908 error_loc = "memory allocation";
1911 data->lil_mode = LL_LEASE_UNLCK;
1912 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1913 data->lil_count = 1;
1914 data->lil_ids[0] = fdv;
1915 rc = llapi_lease_set(fd, data);
1917 error_loc = "cannot merge layout";
1919 } else if (rc == 0) {
1921 error_loc = "lost lease lock";
1934 (void) unlink(victim_file);
1936 fprintf(stderr, "error: %s: %s: %s: %s\n",
1937 progname, fname, error_loc, strerror(-rc));
1941 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1942 bool inherit, uint32_t flags)
1944 struct llapi_layout *f_layout = NULL;
1945 struct ll_ioc_lease *data = NULL;
1951 f_layout = llapi_layout_get_by_path(name, 0);
1953 fprintf(stderr, "%s: cannot get layout\n", progname);
1956 rc = llapi_layout_get_last_init_comp(f_layout);
1958 fprintf(stderr, "%s: cannot get the last init comp\n",
1962 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1965 "%s: cannot inherit from the last init comp\n",
1970 llapi_layout_comp_flags_set(m_layout, flags);
1971 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1976 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1978 error_loc = "cannot get lease";
1982 rc = migrate_nonblock(fd, fdv);
1984 llapi_lease_release(fd);
1988 /* Atomically put lease, merge layouts and close. */
1989 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1991 error_loc = "memory allocation";
1994 data->lil_mode = LL_LEASE_UNLCK;
1995 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1996 data->lil_count = 1;
1997 data->lil_ids[0] = fdv;
1998 rc = llapi_lease_set(fd, data);
2000 error_loc = "cannot merge layout";
2002 } else if (rc == 0) {
2004 error_loc = "lost lease lock";
2017 fprintf(stderr, "error: %s: %s: %s: %s\n",
2018 progname, name, error_loc, strerror(-rc));
2022 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2023 enum mirror_flags mirror_flags)
2027 rc = mirror_create_sanity_check(fname, mirror_list);
2031 while (mirror_list) {
2032 if (mirror_list->m_file) {
2033 rc = mirror_extend_file(fname, mirror_list->m_file,
2036 __u32 mirror_count = mirror_list->m_count;
2038 while (mirror_count > 0) {
2039 rc = mirror_extend_layout(fname,
2040 mirror_list->m_layout,
2041 mirror_list->m_inherit,
2042 mirror_list->m_flags);
2052 mirror_list = mirror_list->m_next;
2058 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2063 rc = llapi_layout_mirror_id_get(layout, &id);
2067 if ((__u16)id == *(__u16 *)cbdata)
2068 return LLAPI_LAYOUT_ITER_STOP;
2070 return LLAPI_LAYOUT_ITER_CONT;
2073 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2078 rc = llapi_layout_comp_id_get(layout, &id);
2082 if (id == *(__u32 *)cbdata)
2083 return LLAPI_LAYOUT_ITER_STOP;
2085 return LLAPI_LAYOUT_ITER_CONT;
2088 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2090 char buf[LOV_MAXPOOLNAME + 1];
2091 struct pool_to_id_cbdata *d = (void *)cbdata;
2095 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2098 if (strcmp(d->pool, buf))
2099 return LLAPI_LAYOUT_ITER_CONT;
2101 rc = llapi_layout_mirror_id_get(layout, &id);
2106 return LLAPI_LAYOUT_ITER_STOP;
2109 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2111 char buf[LOV_MAXPOOLNAME + 1];
2112 struct pool_to_id_cbdata *d = (void *)cbdata;
2116 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2119 if (strcmp(d->pool, buf))
2120 return LLAPI_LAYOUT_ITER_CONT;
2122 rc = llapi_layout_comp_id_get(layout, &id);
2127 return LLAPI_LAYOUT_ITER_STOP;
2130 struct collect_ids_data {
2136 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2138 struct collect_ids_data *cid = cbdata;
2142 rc = llapi_layout_mirror_id_get(layout, &id);
2146 if ((__u16)id != cid->cid_exclude) {
2149 for (i = 0; i < cid->cid_count; i++) {
2150 /* already collected the mirror id */
2151 if (id == cid->cid_ids[i])
2152 return LLAPI_LAYOUT_ITER_CONT;
2154 cid->cid_ids[cid->cid_count] = id;
2158 return LLAPI_LAYOUT_ITER_CONT;
2162 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2163 * @mirror_id: Mirror id to be checked.
2164 * @layout: Mirror component list.
2166 * This function checks if a mirror with specified @mirror_id is the last
2167 * non-stale mirror of a layout @layout.
2169 * Return: true or false.
2172 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2174 __u16 mirror_ids[128] = { 0 };
2175 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2177 .cid_exclude = mirror_id, };
2180 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2182 for (i = 0; i < cid.cid_count; i++) {
2183 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2186 comp_size = llapi_mirror_find_stale(layout, comp_array,
2187 ARRAY_SIZE(comp_array),
2196 static int mirror_split(const char *fname, __u32 id, const char *pool,
2197 enum mirror_flags mflags, const char *victim_file)
2199 struct llapi_layout *layout;
2200 char parent[PATH_MAX];
2201 char victim[PATH_MAX];
2202 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2204 struct ll_ioc_lease *data;
2205 uint16_t mirror_count;
2211 /* check fname contains mirror with mirror_id/comp_id */
2212 layout = llapi_layout_get_by_path(fname, 0);
2215 "error %s: file '%s' couldn't get layout\n",
2220 rc = llapi_layout_sanity(layout, false, true);
2222 llapi_layout_sanity_perror(rc);
2226 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2229 "error %s: file '%s' couldn't get mirror count\n",
2233 if (mirror_count < 2) {
2235 "error %s: file '%s' has %d component, cannot split\n",
2236 progname, fname, mirror_count);
2240 if (mflags & MF_COMP_POOL) {
2241 struct pool_to_id_cbdata data = { .pool = pool };
2243 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2245 mirror_id = data.id;
2246 } else if (mflags & MF_COMP_ID) {
2247 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2248 mirror_id = mirror_id_of(id);
2250 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2254 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2257 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2258 if (mflags & MF_COMP_POOL) {
2260 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2261 progname, fname, pool);
2263 } else if (mflags & MF_COMP_ID) {
2265 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2266 progname, fname, id);
2270 "error %s: file '%s' does not contain mirror with id %u\n",
2271 progname, fname, id);
2276 fd = open(fname, O_RDWR);
2279 "error %s: open file '%s' failed: %s\n",
2280 progname, fname, strerror(errno));
2284 /* get victim file directory pathname */
2285 if (strlen(fname) > sizeof(parent) - 1) {
2286 fprintf(stderr, "error %s: file name of '%s' too long\n",
2291 strncpy(parent, fname, sizeof(parent));
2292 ptr = strrchr(parent, '/');
2294 if (!getcwd(parent, sizeof(parent))) {
2295 fprintf(stderr, "error %s: getcwd failed: %s\n",
2296 progname, strerror(errno));
2306 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2308 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2314 /* use a temp file to store the splitted layout */
2315 if (mflags & MF_DESTROY) {
2316 if (last_non_stale_mirror(mirror_id, layout)) {
2319 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2324 fdv = llapi_create_volatile_idx(parent, mdt_index,
2325 O_LOV_DELAY_CREATE);
2327 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2329 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2332 /* user specified victim file */
2333 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2338 "error %s: create victim file failed: %s\n",
2339 progname, strerror(errno));
2343 /* get lease lock of fname */
2344 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2347 "error %s: cannot get lease of file '%s': %d\n",
2348 progname, fname, rc);
2352 /* Atomatically put lease, split layouts and close. */
2353 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2359 data->lil_mode = LL_LEASE_UNLCK;
2360 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2361 data->lil_count = 2;
2362 data->lil_ids[0] = fdv;
2363 data->lil_ids[1] = mirror_id;
2364 rc = llapi_lease_set(fd, data);
2366 if (rc == 0) /* lost lease lock */
2369 "error %s: cannot split '%s': %s\n",
2370 progname, fname, strerror(-rc));
2381 llapi_layout_free(layout);
2386 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2387 __u16 *mirror_ids, int ids_nr);
2389 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2390 __u64 migration_flags,
2391 struct llapi_stripe_param *param,
2392 struct llapi_layout *layout)
2394 struct ll_ioc_lease *data = NULL;
2397 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2399 error_loc = "cannot get lease";
2403 /* Atomically put lease, merge layouts, resync and close. */
2404 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2406 error_loc = "memory allocation";
2409 data->lil_mode = LL_LEASE_UNLCK;
2410 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2411 data->lil_count = 1;
2412 data->lil_ids[0] = fdv;
2413 rc = llapi_lease_set(fd, data);
2415 error_loc = "cannot merge layout";
2417 } else if (rc == 0) {
2419 error_loc = "lost lease lock";
2425 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2427 error_loc = "cannot resync file";
2431 /* delete first mirror now */
2432 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2434 error_loc = "cannot delete old layout";
2442 fprintf(stderr, "error: %s: %s: %s: %s\n",
2443 progname, name, error_loc, strerror(-rc));
2444 else if (migration_flags & MIGRATION_VERBOSE)
2445 printf("%s\n", name);
2452 * Parse a string containing an target index list into an array of integers.
2454 * The input string contains a comma delimited list of individual
2455 * indices and ranges, for example "1,2-4,7". Add the indices into the
2456 * \a tgts array and remove duplicates.
2458 * \param[out] tgts array to store indices in
2459 * \param[in] size size of \a tgts array
2460 * \param[in] offset starting index in \a tgts
2461 * \param[in] arg string containing OST index list
2462 * \param[in/out] overstriping index list may contain duplicates
2464 * \retval positive number of indices in \a tgts
2465 * \retval -EINVAL unable to parse \a arg
2467 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2468 unsigned long long *pattern)
2472 int slots = size - offset;
2474 bool overstriped = false;
2480 end_of_loop = false;
2481 while (!end_of_loop) {
2482 int start_index = 0;
2485 char *endptr = NULL;
2489 ptr = strchrnul(arg, ',');
2491 end_of_loop = *ptr == '\0';
2495 start_index = strtol(arg, &endptr, 0);
2496 if (endptr == arg) /* no data at all */
2498 if (errno != 0 || start_index < -1 ||
2499 (*endptr != '-' && *endptr != '\0'))
2502 end_index = start_index;
2503 if (*endptr == '-') {
2505 end_index = strtol(endptr + 1, &endptr, 0);
2506 if (errno != 0 || *endptr != '\0' || end_index < -1)
2508 if (end_index < start_index)
2512 for (i = start_index; i <= end_index && slots > 0; i++) {
2515 /* remove duplicate */
2516 for (j = 0; j < offset; j++) {
2517 if (tgts[j] == i && pattern &&
2518 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2520 else if (tgts[j] == i)
2526 if (j == offset) { /* check complete */
2532 if (slots == 0 && i < end_index)
2540 if (!end_of_loop && ptr)
2543 if (!overstriped && pattern)
2544 *pattern = LLAPI_LAYOUT_DEFAULT;
2546 return rc < 0 ? rc : nr;
2549 struct lfs_setstripe_args {
2550 unsigned long long lsa_comp_end;
2551 unsigned long long lsa_stripe_size;
2552 unsigned long long lsa_extension_size;
2553 long long lsa_stripe_count;
2554 long long lsa_stripe_off;
2555 __u32 lsa_comp_flags;
2556 __u32 lsa_comp_neg_flags;
2557 unsigned long long lsa_pattern;
2558 unsigned int lsa_mirror_count;
2560 bool lsa_first_comp;
2561 bool lsa_extension_comp;
2563 char *lsa_pool_name;
2566 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2568 unsigned int mirror_count = lsa->lsa_mirror_count;
2569 bool first_comp = lsa->lsa_first_comp;
2571 memset(lsa, 0, sizeof(*lsa));
2573 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2574 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2575 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2576 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2577 lsa->lsa_pool_name = NULL;
2579 lsa->lsa_mirror_count = mirror_count;
2580 lsa->lsa_first_comp = first_comp;
2584 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2585 * @lsa: Stripe options to be initialized and inherited.
2587 * This function initializes stripe options in @lsa and inherit
2588 * stripe_size, stripe_count and OST pool_name options.
2592 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2594 unsigned long long stripe_size;
2595 long long stripe_count;
2596 char *pool_name = NULL;
2598 stripe_size = lsa->lsa_stripe_size;
2599 stripe_count = lsa->lsa_stripe_count;
2600 pool_name = lsa->lsa_pool_name;
2602 setstripe_args_init(lsa);
2604 lsa->lsa_stripe_size = stripe_size;
2605 lsa->lsa_stripe_count = stripe_count;
2606 lsa->lsa_pool_name = pool_name;
2609 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2611 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2612 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2613 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2614 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2615 lsa->lsa_comp_end != 0);
2619 * comp_args_to_layout() - Create or extend a composite layout.
2620 * @composite: Pointer to the composite layout.
2621 * @lsa: Stripe options for the new component.
2623 * This function creates or extends a composite layout by adding a new
2624 * component with stripe options from @lsa.
2626 * Return: 0 on success or an error code on failure.
2628 static int comp_args_to_layout(struct llapi_layout **composite,
2629 struct lfs_setstripe_args *lsa,
2632 struct llapi_layout *layout = *composite;
2633 uint64_t prev_end = 0;
2639 layout = llapi_layout_alloc();
2641 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2646 *composite = layout;
2647 lsa->lsa_first_comp = true;
2652 * Get current component extent, current component
2653 * must be the tail component.
2655 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2657 fprintf(stderr, "Get comp extent failed. %s\n",
2662 if (lsa->lsa_first_comp) {
2664 rc = llapi_layout_add_first_comp(layout);
2666 rc = llapi_layout_comp_add(layout);
2669 fprintf(stderr, "Add component failed. %s\n",
2675 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2677 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2678 lsa->lsa_comp_flags, strerror(errno));
2683 uint64_t comp_end = lsa->lsa_comp_end;
2686 * The extendable component is 0-length, so it can be removed
2687 * if there is insufficient space to extend it.
2689 if (lsa->lsa_extension_comp)
2690 comp_end = prev_end;
2692 rc = llapi_layout_comp_extent_set(layout, prev_end,
2695 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2696 prev_end, comp_end, strerror(errno));
2700 /* reset lsa_first_comp */
2701 lsa->lsa_first_comp = false;
2703 /* Data-on-MDT component setting */
2704 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2706 * In case of Data-on-MDT patterns the only extra option
2707 * applicable is stripe size option.
2709 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2711 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2712 lsa->lsa_stripe_count);
2716 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2718 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2719 lsa->lsa_stripe_size);
2723 if (lsa->lsa_nr_tgts != 0) {
2725 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2730 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2732 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2733 lsa->lsa_stripe_off);
2737 if (lsa->lsa_pool_name != 0) {
2739 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2740 lsa->lsa_pool_name);
2745 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2747 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2752 /* Data-on-MDT component has always single stripe up to end */
2753 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2754 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2755 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2757 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2764 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2765 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2767 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2768 rc = llapi_layout_extension_size_set(layout, size);
2770 rc = llapi_layout_stripe_size_set(layout, size);
2773 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2774 size, strerror(errno));
2778 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2780 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2781 lsa->lsa_stripe_count, strerror(errno));
2785 if (lsa->lsa_pool_name) {
2786 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2788 fprintf(stderr, "Set pool name: %s failed. %s\n",
2789 lsa->lsa_pool_name, strerror(errno));
2793 rc = llapi_layout_pool_name_set(layout, "");
2795 fprintf(stderr, "Clear pool name failed: %s\n",
2801 if (lsa->lsa_nr_tgts > 0) {
2802 if (lsa->lsa_stripe_count > 0 &&
2803 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2804 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2805 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2806 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2807 lsa->lsa_stripe_count,
2812 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2813 rc = llapi_layout_ost_index_set(layout, i,
2818 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2819 lsa->lsa_stripe_off != -1) {
2820 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2823 fprintf(stderr, "Set ost index %d failed. %s\n",
2824 i, strerror(errno));
2828 /* Create the second, virtual component of extension space */
2829 if (lsa->lsa_extension_comp) {
2830 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2831 lsa->lsa_extension_comp = false;
2838 static int build_component(struct llapi_layout **layout,
2839 struct lfs_setstripe_args *lsa, bool set_extent)
2843 rc = comp_args_to_layout(layout, lsa, set_extent);
2847 if (lsa->lsa_mirror_count > 0) {
2848 rc = llapi_layout_mirror_count_set(*layout,
2849 lsa->lsa_mirror_count);
2853 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2856 lsa->lsa_mirror_count = 0;
2862 static int build_prev_component(struct llapi_layout **layout,
2863 struct lfs_setstripe_args *prev,
2864 struct lfs_setstripe_args *lsa,
2867 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2870 if (prev->lsa_stripe_size) {
2872 prev->lsa_comp_end = lsa->lsa_comp_end;
2873 prev->lsa_extension_size = lsa->lsa_extension_size;
2874 prev->lsa_extension_comp = true;
2877 rc = build_component(layout, prev, true);
2883 * Copy lsa to previous lsa;
2884 * if this is an extension component, make the previous invalid;
2887 prev->lsa_stripe_size = 0;
2894 static int build_layout_from_yaml_node(struct cYAML *node,
2895 struct llapi_layout **layout,
2896 struct lfs_setstripe_args *lsa,
2897 struct lfs_setstripe_args *prevp)
2899 struct lfs_setstripe_args prev = { 0 };
2900 __u32 *osts = lsa->lsa_tgts;
2908 string = node->cy_string;
2910 if (node->cy_type == CYAML_TYPE_OBJECT) {
2911 /* go deep to sub blocks */
2912 if (string && !strncmp(string, "component", 9) &&
2913 strncmp(string, "component0", 10) &&
2914 strncmp(string, "components", 10)) {
2915 rc = build_prev_component(layout, prevp, lsa,
2920 /* initialize lsa. */
2921 setstripe_args_init(lsa);
2922 lsa->lsa_first_comp = false;
2923 lsa->lsa_tgts = osts;
2926 rc = build_layout_from_yaml_node(node->cy_child, layout,
2931 if (!node->cy_string)
2934 /* skip leading lmm_ if present, to simplify parsing */
2935 if (strncmp(string, "lmm_", 4) == 0)
2938 if (node->cy_type == CYAML_TYPE_STRING) {
2939 if (!strcmp(string, "lcme_extent.e_end")) {
2940 if (!strcmp(node->cy_valuestring, "EOF") ||
2941 !strcmp(node->cy_valuestring, "eof"))
2942 lsa->lsa_comp_end = LUSTRE_EOF;
2943 } else if (!strcmp(string, "pool")) {
2944 lsa->lsa_pool_name = node->cy_valuestring;
2945 } else if (!strcmp(string, "pattern")) {
2946 if (!strcmp(node->cy_valuestring, "mdt"))
2947 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2948 if (!strcmp(node->cy_valuestring,
2949 "raid0,overstriped"))
2951 LLAPI_LAYOUT_OVERSTRIPING;
2952 } else if (!strcmp(string, "lcme_flags")) {
2953 rc = comp_str2flags(node->cy_valuestring,
2954 &lsa->lsa_comp_flags,
2955 &lsa->lsa_comp_neg_flags);
2959 * Only template flags have meaning in
2960 * the layout for a new file
2962 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2964 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2965 if (!strcmp(string, "lcm_mirror_count")) {
2966 lsa->lsa_mirror_count = node->cy_valueint;
2967 } else if (!strcmp(string, "lcme_extent.e_start")) {
2968 if (node->cy_valueint == 0)
2969 lsa->lsa_first_comp = true;
2970 } else if (!strcmp(string, "lcme_extent.e_end")) {
2971 if (node->cy_valueint == -1)
2972 lsa->lsa_comp_end = LUSTRE_EOF;
2974 lsa->lsa_comp_end = node->cy_valueint;
2975 } else if (!strcmp(string, "stripe_count")) {
2976 lsa->lsa_stripe_count = node->cy_valueint;
2977 } else if (!strcmp(string, "stripe_size")) {
2978 lsa->lsa_stripe_size = node->cy_valueint;
2979 } else if (!strcmp(string, "extension_size")) {
2980 lsa->lsa_extension_size = node->cy_valueint;
2981 lsa->lsa_extension_comp = true;
2982 } else if (!strcmp(string, "stripe_offset")) {
2983 lsa->lsa_stripe_off = node->cy_valueint;
2984 } else if (!strcmp(string, "l_ost_idx")) {
2985 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2990 node = node->cy_next;
2993 if (prevp == &prev) {
2994 rc = build_prev_component(layout, prevp, lsa, true);
2998 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
2999 rc = build_component(layout, lsa, *layout != NULL);
3005 static int lfs_comp_create_from_yaml(char *template,
3006 struct llapi_layout **layout,
3007 struct lfs_setstripe_args *lsa,
3010 struct cYAML *tree = NULL, *err_rc = NULL;
3013 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3015 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3016 progname, template);
3017 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3018 "can't parse", &err_rc);
3019 cYAML_print_tree2file(stderr, err_rc);
3020 cYAML_free_tree(err_rc);
3025 /* initialize lsa for plain file */
3026 setstripe_args_init(lsa);
3027 lsa->lsa_tgts = osts;
3029 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3031 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3032 progname, template);
3035 /* clean clean lsa */
3036 setstripe_args_init(lsa);
3040 cYAML_free_tree(tree);
3045 * Get the extension size from the next (SEL) component and extend the
3046 * current component on it. The start of the next component is to be
3049 * \param[in] layout the current layout
3050 * \param[in] start the start of the current component
3051 * \param[in,out] end the end of the current component
3052 * \param[in] offset the offset to adjust the end position to instead of
3055 * \retval 0 - extended successfully
3056 * \retval < 0 - error
3058 static int layout_extend_comp(struct llapi_layout *layout,
3059 uint64_t start, uint64_t *end,
3062 uint64_t size, next_start, next_end;
3065 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3068 "%s setstripe: cannot move component cursor: %s\n",
3069 progname, strerror(errno));
3074 * Even if the @size will not be used below, this will fail if
3075 * this is not a SEL component - a good confirmation we are
3076 * working on right components.
3078 rc = llapi_layout_extension_size_get(layout, &size);
3081 "%s setstripe: cannot get component ext size: %s\n",
3082 progname, strerror(errno));
3086 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3088 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3089 progname, strerror(errno));
3093 next_start += offset ?: size;
3094 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3096 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3097 progname, strerror(errno));
3101 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3104 "%s setstripe: cannot move component cursor: %s\n",
3105 progname, strerror(errno));
3109 *end += offset ?: size;
3110 rc = llapi_layout_comp_extent_set(layout, start, *end);
3112 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3113 progname, strerror(errno));
3121 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3122 * end of the last component in the existing file, and adjust the
3123 * first extent start of the components to be added accordingly.
3125 * In the create mode, we need to check if the first component is an extendable
3126 * SEL component and extend its length to the extension size (first component
3127 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3129 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3132 struct llapi_layout *head;
3133 uint64_t start = 0, prev_end = 0;
3137 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3142 head = llapi_layout_get_by_path(fname, 0);
3145 "%s setstripe: cannot read layout from '%s': %s\n",
3146 progname, fname, strerror(errno));
3148 } else if (errno == ENODATA) {
3150 * file without LOVEA, this component-add will be turned
3151 * into a component-create.
3153 llapi_layout_free(head);
3157 * the new layout will be added to an empty one, it
3158 * still needs to be adjusted below
3162 } else if (!llapi_layout_is_composite(head)) {
3164 "%s setstripe: '%s' not a composite file\n",
3166 llapi_layout_free(head);
3170 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3173 "%s setstripe: cannot get prev extent: %s\n",
3174 progname, strerror(errno));
3175 llapi_layout_free(head);
3179 llapi_layout_free(head);
3183 /* Make sure we use the first component of the layout to be added. */
3184 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3187 "%s setstripe: cannot move component cursor: %s\n",
3188 progname, strerror(errno));
3192 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3194 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3195 progname, strerror(errno));
3199 if (start == 0 && end == 0) {
3200 rc = layout_extend_comp(layout, start, &end,
3201 comp_add ? prev_end : 0);
3206 if (start > prev_end || end < prev_end) {
3208 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3209 progname, start, end, prev_end);
3213 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3216 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3217 progname, prev_end, end, strerror(errno));
3224 static int mirror_adjust_first_extents(struct mirror_args *list)
3232 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3235 list = list->m_next;
3241 static inline bool arg_is_eof(char *arg)
3243 return !strncmp(arg, "-1", strlen("-1")) ||
3244 !strncmp(arg, "EOF", strlen("EOF")) ||
3245 !strncmp(arg, "eof", strlen("eof"));
3249 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3251 * Return: Valid mirror_args pointer on success and
3252 * NULL if memory allocation fails.
3254 static struct mirror_args *lfs_mirror_alloc(void)
3256 struct mirror_args *mirror = NULL;
3259 mirror = calloc(1, sizeof(*mirror));
3261 mirror->m_inherit = false;
3272 * lfs_mirror_free() - Free memory allocated for a mirror argument
3274 * @mirror: Previously allocated mirror argument structure by
3275 * lfs_mirror_alloc().
3277 * Free memory allocated for @mirror.
3281 static void lfs_mirror_free(struct mirror_args *mirror)
3283 if (mirror->m_layout)
3284 llapi_layout_free(mirror->m_layout);
3289 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3290 * @mirror_list: Previously allocated mirror list.
3292 * Free memory allocated for @mirror_list.
3296 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3298 struct mirror_args *next_mirror = NULL;
3300 while (mirror_list) {
3301 next_mirror = mirror_list->m_next;
3302 lfs_mirror_free(mirror_list);
3303 mirror_list = next_mirror;
3315 LFS_COMP_NO_VERIFY_OPT,
3317 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3319 LFS_MIRROR_STATE_OPT,
3321 LFS_MIRROR_INDEX_OPT,
3322 LFS_LAYOUT_FOREIGN_OPT,
3328 static int lfs_setstripe_internal(int argc, char **argv,
3329 enum setstripe_origin opc)
3331 struct lfs_setstripe_args lsa = { 0 };
3332 struct llapi_stripe_param *param = NULL;
3333 struct find_param migrate_mdt_param = {
3343 unsigned long long size_units = 1;
3344 bool migrate_mode = false;
3345 bool migrate_mdt_mode = false;
3346 bool setstripe_mode = false;
3347 bool migration_block = false;
3348 __u64 migration_flags = 0;
3349 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3350 int comp_del = 0, comp_set = 0;
3353 struct llapi_layout *layout = NULL;
3354 struct llapi_layout **lpp = &layout;
3355 bool mirror_mode = false;
3356 bool has_m_file = false;
3357 __u32 mirror_count = 0;
3358 enum mirror_flags mirror_flags = 0;
3359 struct mirror_args *mirror_list = NULL;
3360 struct mirror_args *new_mirror = NULL;
3361 struct mirror_args *last_mirror = NULL;
3362 __u16 mirror_id = 0;
3364 bool from_yaml = false;
3365 bool from_copy = false;
3366 char *template = NULL;
3367 bool foreign_mode = false;
3369 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3370 char *mode_opt = NULL;
3371 mode_t previous_umask = 0;
3372 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3374 struct option long_opts[] = {
3375 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3376 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3377 /* --block is only valid in migrate mode */
3378 { .val = 'b', .name = "block", .has_arg = no_argument },
3379 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3380 { .val = LFS_COMP_ADD_OPT,
3381 .name = "comp-add", .has_arg = no_argument },
3382 { .val = LFS_COMP_ADD_OPT,
3383 .name = "component-add", .has_arg = no_argument },
3384 { .val = LFS_COMP_DEL_OPT,
3385 .name = "comp-del", .has_arg = no_argument },
3386 { .val = LFS_COMP_DEL_OPT,
3387 .name = "component-del", .has_arg = no_argument },
3388 { .val = LFS_COMP_FLAGS_OPT,
3389 .name = "comp-flags", .has_arg = required_argument },
3390 { .val = LFS_COMP_FLAGS_OPT,
3391 .name = "component-flags",
3392 .has_arg = required_argument },
3393 { .val = LFS_COMP_SET_OPT,
3394 .name = "comp-set", .has_arg = no_argument },
3395 { .val = LFS_COMP_SET_OPT,
3396 .name = "component-set",
3397 .has_arg = no_argument},
3398 { .val = LFS_COMP_NO_VERIFY_OPT,
3399 .name = "no-verify", .has_arg = no_argument},
3400 { .val = LFS_LAYOUT_FLAGS_OPT,
3401 .name = "flags", .has_arg = required_argument},
3402 { .val = LFS_LAYOUT_FOREIGN_OPT,
3403 .name = "foreign", .has_arg = optional_argument},
3404 { .val = LFS_MIRROR_ID_OPT,
3405 .name = "mirror-id", .has_arg = required_argument},
3406 { .val = LFS_MODE_OPT,
3407 .name = "mode", .has_arg = required_argument},
3408 { .val = LFS_LAYOUT_COPY,
3409 .name = "copy", .has_arg = required_argument},
3410 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3411 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3412 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3413 { .val = 'C', .name = "overstripe-count",
3414 .has_arg = required_argument},
3415 { .val = 'd', .name = "delete", .has_arg = no_argument},
3416 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3417 /* --non-direct is only valid in migrate mode */
3418 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3419 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3420 { .val = 'E', .name = "component-end",
3421 .has_arg = required_argument},
3422 { .val = 'f', .name = "file", .has_arg = required_argument },
3423 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3424 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3425 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3426 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3427 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3428 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3429 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3430 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3431 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3432 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3433 { .val = 'L', .name = "layout", .has_arg = required_argument },
3434 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3435 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3436 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3437 /* --non-block is only valid in migrate mode */
3438 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3439 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3440 { .val = 'o', .name = "ost", .has_arg = required_argument },
3441 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3442 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3443 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3445 { .val = 'p', .name = "pool", .has_arg = required_argument },
3446 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3447 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3448 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3449 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3450 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3451 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3452 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3453 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3454 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3455 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3456 /* --verbose is only valid in migrate mode */
3457 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3458 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3459 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3460 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3461 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3464 setstripe_args_init(&lsa);
3466 migrate_mode = (opc == SO_MIGRATE);
3467 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3468 setstripe_mode = (opc == SO_SETSTRIPE);
3469 if (opc == SO_MIRROR_DELETE) {
3471 mirror_flags = MF_DESTROY;
3474 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3476 while ((c = getopt_long(argc, argv,
3477 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3478 long_opts, NULL)) >= 0) {
3484 case LFS_COMP_ADD_OPT:
3487 case LFS_COMP_DEL_OPT:
3490 case LFS_COMP_FLAGS_OPT:
3491 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3492 &lsa.lsa_comp_neg_flags);
3495 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3497 "%s: inverted flags are not supported\n",
3502 case LFS_COMP_SET_OPT:
3505 case LFS_COMP_NO_VERIFY_OPT:
3506 mirror_flags |= MF_NO_VERIFY;
3508 case LFS_MIRROR_ID_OPT: {
3509 unsigned long int id;
3512 id = strtoul(optarg, &end, 0);
3513 if (errno != 0 || *end != '\0' || id == 0 ||
3516 "%s %s: invalid mirror ID '%s'\n",
3517 progname, argv[0], optarg);
3521 mirror_id = (__u16)id;
3524 case LFS_LAYOUT_FLAGS_OPT: {
3527 /* check for numeric flags (foreign and mirror cases) */
3528 if (setstripe_mode && !mirror_mode && !last_mirror) {
3530 flags = strtoul(optarg, &end, 16);
3531 if (errno != 0 || *end != '\0' ||
3532 flags >= UINT32_MAX) {
3534 "%s %s: invalid hex flags '%s'\n",
3535 progname, argv[0], optarg);
3538 if (!foreign_mode) {
3540 "%s %s: hex flags must be specified with --foreign option\n",
3547 if (!mirror_mode || !last_mirror) {
3549 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3554 result = comp_str2flags(optarg, &last_mirror->m_flags,
3561 "%s: inverted flags are not supported\n",
3566 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3568 "%s: unsupported mirror flags: %s\n",
3575 case LFS_LAYOUT_FOREIGN_OPT:
3577 /* check pure numeric */
3578 type = strtoul(optarg, &end, 0);
3581 type = check_foreign_type_name(optarg);
3582 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3584 "%s %s: unrecognized foreign type '%s'\n",
3589 } else if (type >= UINT32_MAX) {
3591 "%s %s: invalid foreign type '%s'\n",
3592 progname, argv[0], optarg);
3596 foreign_mode = true;
3601 mode = strtoul(mode_opt, &end, 8);
3604 "%s %s: bad mode '%s'\n",
3605 progname, argv[0], mode_opt);
3608 previous_umask = umask(0);
3611 case LFS_LAYOUT_COPY:
3616 if (!migrate_mode) {
3618 "%s %s: -b|--block valid only for migrate command\n",
3622 migration_block = true;
3625 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3627 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3631 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3635 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3636 if (errno != 0 || *end != '\0'||
3637 lsa.lsa_stripe_count < -1 ||
3638 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3640 "%s %s: invalid stripe count '%s'\n",
3641 progname, argv[0], optarg);
3645 if (lsa.lsa_stripe_count == -1)
3646 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3649 /* delete the default striping pattern */
3651 if (opc == SO_MIRROR_SPLIT) {
3654 "%s %s: -d cannot used with -f\n",
3658 mirror_flags |= MF_DESTROY;
3662 if (!migrate_mode) {
3664 "%s %s: -D|--non-direct is valid only for migrate command\n",
3668 migration_flags |= MIGRATION_NONDIRECT;
3671 if (lsa.lsa_comp_end != 0) {
3672 result = comp_args_to_layout(lpp, &lsa, true);
3674 fprintf(stderr, "%s: invalid layout\n",
3679 setstripe_args_init_inherit(&lsa);
3682 if (arg_is_eof(optarg)) {
3683 lsa.lsa_comp_end = LUSTRE_EOF;
3685 result = llapi_parse_size(optarg,
3688 /* assume units of KB if too small */
3689 if (lsa.lsa_comp_end < 4096)
3690 lsa.lsa_comp_end *= 1024;
3692 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3694 "%s %s: invalid component end '%s'\n",
3695 progname, argv[0], optarg);
3701 if (!migrate_mode) {
3703 "--mdt-hash is valid only for migrate command\n");
3707 lsa.lsa_pattern = check_hashtype(optarg);
3708 if (lsa.lsa_pattern == 0) {
3710 "%s %s: bad stripe hash type '%s'\n",
3711 progname, argv[0], optarg);
3717 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3718 if (errno != 0 || *end != '\0' ||
3719 lsa.lsa_stripe_off < -1 ||
3720 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3722 "%s %s: invalid stripe offset '%s'\n",
3723 progname, argv[0], optarg);
3726 if (lsa.lsa_stripe_off == -1)
3727 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3730 comp_id = strtoul(optarg, &end, 0);
3731 if (*end != '\0' || comp_id == 0 ||
3732 comp_id > LCME_ID_MAX) {
3734 "%s %s: invalid component ID '%s'\n",
3735 progname, argv[0], optarg);
3740 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3742 "error: %s: invalid option: %s\n",
3743 progname, argv[optopt + 1]);
3746 if (opc == SO_MIRROR_EXTEND) {
3749 "error: %s: '-N' must exist in front of '%s'\n",
3750 progname, argv[optopt + 1]);
3753 last_mirror->m_file = optarg;
3754 last_mirror->m_count = 1;
3758 mirror_list = lfs_mirror_alloc();
3759 mirror_list->m_file = optarg;
3764 if (strcmp(argv[optind - 1], "mdt") == 0) {
3765 /* Can be only the first component */
3769 "error: 'mdt' layout can be only the first one\n");
3772 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3775 "error: 'mdt' layout size is too big\n");
3778 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3779 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3782 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3788 if (!migrate_mode) {
3790 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3794 migrate_mdt_mode = true;
3795 lsa.lsa_nr_tgts = parse_targets(tgts,
3796 sizeof(tgts) / sizeof(__u32),
3797 lsa.lsa_nr_tgts, optarg, NULL);
3798 if (lsa.lsa_nr_tgts < 0) {
3800 "%s %s: invalid MDT target(s) '%s'\n",
3801 progname, argv[0], optarg);
3805 lsa.lsa_tgts = tgts;
3806 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3807 lsa.lsa_stripe_off = tgts[0];
3810 if (!migrate_mode) {
3812 "%s %s: -n|--non-block valid only for migrate command\n",
3816 migration_flags |= MIGRATION_NONBLOCK;
3819 if (opc == SO_SETSTRIPE) {
3820 opc = SO_MIRROR_CREATE;
3826 mirror_count = strtoul(optarg, &end, 0);
3827 if (errno != 0 || *end != '\0' ||
3828 mirror_count == 0 ||
3829 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3831 "error: %s: bad mirror count: %s\n",
3838 new_mirror = lfs_mirror_alloc();
3839 new_mirror->m_count = mirror_count;
3842 mirror_list = new_mirror;
3845 /* wrap up last mirror */
3846 if (!setstripe_args_specified(&lsa))
3847 last_mirror->m_inherit = true;
3848 if (lsa.lsa_comp_end == 0)
3849 lsa.lsa_comp_end = LUSTRE_EOF;
3851 result = comp_args_to_layout(lpp, &lsa, true);
3853 lfs_mirror_free(new_mirror);
3857 setstripe_args_init_inherit(&lsa);
3859 last_mirror->m_next = new_mirror;
3862 last_mirror = new_mirror;
3863 lpp = &last_mirror->m_layout;
3866 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3867 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3869 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3871 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3873 "%s %s: -o|--ost incompatible with DoM layout\n",
3878 * -o allows overstriping, and must note it because
3879 * parse_targets is shared with MDT striping, which
3880 * does not allow duplicates
3882 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3883 lsa.lsa_nr_tgts = parse_targets(tgts,
3884 sizeof(tgts) / sizeof(__u32),
3885 lsa.lsa_nr_tgts, optarg,
3887 if (lsa.lsa_nr_tgts < 0) {
3889 "%s %s: invalid OST target(s) '%s'\n",
3890 progname, argv[0], optarg);
3894 lsa.lsa_tgts = tgts;
3895 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3896 lsa.lsa_stripe_off = tgts[0];
3901 lsa.lsa_pool_name = optarg;
3903 if (strlen(lsa.lsa_pool_name) == 0 ||
3904 strncmp(lsa.lsa_pool_name, "none",
3905 LOV_MAXPOOLNAME) == 0)
3906 lsa.lsa_pool_name = NULL;
3909 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3911 /* assume units of KB if too small to be valid */
3912 if (lsa.lsa_stripe_size < 4096)
3913 lsa.lsa_stripe_size *= 1024;
3915 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3917 "%s %s: invalid stripe size '%s'\n",
3918 progname, argv[0], optarg);
3923 if (!migrate_mode) {
3925 "%s %s: -v|--verbose valid only for migrate command\n",
3929 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3930 migration_flags = MIGRATION_VERBOSE;
3940 result = llapi_parse_size(optarg,
3941 &lsa.lsa_extension_size,
3945 "%s %s: invalid extension size '%s'\n",
3946 progname, argv[0], optarg);
3950 lsa.lsa_extension_comp = true;
3953 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3954 progname, argv[0], argv[optind - 1]);
3959 fname = argv[optind];
3961 if (optind == argc) {
3962 fprintf(stderr, "%s %s: FILE must be specified\n",
3967 /* lfs migrate $filename should keep the file's layout by default */
3968 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3972 if (xattr && !foreign_mode) {
3974 * only print a warning as this is harmless and will be ignored
3977 "%s %s: xattr has been specified for non-foreign layout\n",
3979 } else if (foreign_mode && !xattr) {
3981 "%s %s: xattr must be provided in foreign mode\n",
3986 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3987 comp_set || comp_id || delete || from_copy ||
3988 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3991 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3996 if (mirror_mode && mirror_count == 0) {
3998 "error: %s: --mirror-count|-N option is required\n",
4005 if (!setstripe_args_specified(&lsa))
4006 last_mirror->m_inherit = true;
4007 if (lsa.lsa_comp_end == 0)
4008 lsa.lsa_comp_end = LUSTRE_EOF;
4011 if (lsa.lsa_comp_end != 0) {
4012 result = comp_args_to_layout(lpp, &lsa, true);
4014 fprintf(stderr, "error: %s: invalid layout\n",
4021 if (mirror_flags & MF_NO_VERIFY) {
4022 if (opc != SO_MIRROR_EXTEND) {
4024 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4028 } else if (!has_m_file) {
4030 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4037 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4039 "%s %s: --component-set doesn't have component-id set\n",
4044 if ((delete + comp_set + comp_del + comp_add) > 1) {
4046 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4051 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4052 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4054 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4059 if ((comp_set || comp_del) &&
4060 (setstripe_args_specified(&lsa) || layout != NULL)) {
4062 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4067 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4069 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4074 if (comp_add || comp_del) {
4077 result = lstat(fname, &st);
4078 if (result == 0 && S_ISDIR(st.st_mode)) {
4080 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4087 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4096 "%s %s: option -E must be specified with --component-add\n",
4102 if (from_yaml && from_copy) {
4104 "%s: can't specify --yaml and --copy together\n",
4109 if ((from_yaml || from_copy) &&
4110 (setstripe_args_specified(&lsa) || layout != NULL)) {
4112 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4117 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4119 "%s %s: options --non-block and --block are mutually exclusive\n",
4124 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4125 opc != SO_MIRROR_DELETE && comp_id != 0) {
4127 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4132 if (migrate_mdt_mode) {
4133 struct lmv_user_md *lmu;
4135 /* initialize migrate mdt parameters */
4136 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4137 LMV_USER_MAGIC_SPECIFIC));
4140 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4141 progname, argv[0], strerror(ENOMEM));
4145 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4146 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4147 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4149 "%s %s: migrate should specify MDT index\n",
4154 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4155 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4156 lmu->lum_hash_type = lsa.lsa_pattern;
4158 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4159 if (lsa.lsa_pool_name) {
4160 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4161 sizeof(lmu->lum_pool_name) - 1);
4162 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4164 if (lsa.lsa_nr_tgts > 1) {
4167 if (lsa.lsa_stripe_count > 0 &&
4168 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4169 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4171 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4172 progname, lsa.lsa_stripe_count,
4178 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4179 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4180 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4181 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4183 lmu->lum_magic = LMV_USER_MAGIC;
4186 migrate_mdt_param.fp_lmv_md = lmu;
4187 migrate_mdt_param.fp_migrate = 1;
4188 } else if (!layout) {
4189 /* initialize stripe parameters */
4190 param = calloc(1, offsetof(typeof(*param),
4191 lsp_osts[lsa.lsa_nr_tgts]));
4194 "%s %s: cannot allocate memory for parameters: %s\n",
4195 progname, argv[0], strerror(ENOMEM));
4200 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4201 param->lsp_stripe_size = lsa.lsa_stripe_size;
4202 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4203 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4204 param->lsp_stripe_count = -1;
4206 param->lsp_stripe_count = lsa.lsa_stripe_count;
4208 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4209 param->lsp_stripe_offset = -1;
4211 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4212 param->lsp_stripe_pattern =
4213 llapi_pattern_to_lov(lsa.lsa_pattern);
4214 if (param->lsp_stripe_pattern == EINVAL) {
4215 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4220 param->lsp_pool = lsa.lsa_pool_name;
4221 param->lsp_is_specific = false;
4222 if (lsa.lsa_nr_tgts > 0) {
4223 if (lsa.lsa_stripe_count > 0 &&
4224 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4225 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4226 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4228 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4229 argv[0], lsa.lsa_stripe_count,
4235 param->lsp_is_specific = true;
4236 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4237 memcpy(param->lsp_osts, tgts,
4238 sizeof(*tgts) * lsa.lsa_nr_tgts);
4243 /* generate a layout from a YAML template */
4244 result = lfs_comp_create_from_yaml(template, &layout,
4248 "error: %s: can't create composite layout from template file %s\n",
4254 if (layout != NULL || mirror_list != NULL) {
4256 result = mirror_adjust_first_extents(mirror_list);
4258 result = layout_adjust_first_extent(fname, layout,
4260 if (result == -ENODATA)
4262 else if (result != 0) {
4263 fprintf(stderr, "error: %s: invalid layout\n",
4269 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4271 layout = llapi_layout_get_by_path(template ?: fname, 0);
4274 "%s: can't create composite layout from file %s: %s\n",
4275 progname, template ?: fname,
4282 if (migrate_mdt_mode) {
4283 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4284 } else if (migrate_mode) {
4285 result = lfs_migrate(fname, migration_flags, param,
4287 } else if (comp_set != 0) {
4288 result = lfs_component_set(fname, comp_id,
4291 lsa.lsa_comp_neg_flags);
4292 } else if (comp_del != 0) {
4293 result = lfs_component_del(fname, comp_id,
4295 lsa.lsa_comp_neg_flags);
4296 } else if (comp_add != 0) {
4297 result = lfs_component_add(fname, layout);
4298 } else if (opc == SO_MIRROR_CREATE) {
4299 result = mirror_create(fname, mirror_list);
4300 } else if (opc == SO_MIRROR_EXTEND) {
4301 result = mirror_extend(fname, mirror_list,
4303 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4304 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4306 "%s: no mirror specified to delete from '%s'\n",
4310 if (lsa.lsa_pool_name)
4311 mirror_flags |= MF_COMP_POOL;
4312 else if (mirror_id != 0)
4313 comp_id = mirror_id;
4315 mirror_flags |= MF_COMP_ID;
4316 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4318 has_m_file ? mirror_list->m_file :
4320 } else if (layout) {
4321 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4327 } else if (foreign_mode) {
4328 result = llapi_file_create_foreign(fname, mode, type,
4335 result = llapi_file_open_param(fname,
4344 /* Save the first error encountered. */
4352 umask(previous_umask);
4355 free(migrate_mdt_param.fp_lmv_md);
4356 llapi_layout_free(layout);
4357 lfs_mirror_list_free(mirror_list);
4362 llapi_layout_free(layout);
4363 lfs_mirror_list_free(mirror_list);
4367 static int lfs_poollist(int argc, char **argv)
4372 return llapi_poollist(argv[1]);
4375 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4376 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4381 char *endptr = "AD";
4386 else if (str[0] == '-')
4392 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4393 long long val = strtoll(timebuf, &endptr, 0);
4398 unit *= 52; /* 52 weeks + 1 day below */
4399 case 'w': /* fallthrough */
4401 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4402 param->fp_time_margin *= (1 + unit / 52);
4403 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4404 case '\0': /* days are default unit if none used */
4405 case 'd': /* fallthrough */
4407 case 'h': /* fallthrough */
4409 case 'm': /* fallthrough */
4411 case 's': /* fallthrough */
4413 /* don't need to multiply by 1 for seconds */
4416 "%s find: bad time string '%s': %s\n",
4417 progname, timebuf, strerror(EINVAL));
4421 if (param->fp_time_margin == 0 ||
4422 (*endptr && unit < param->fp_time_margin))
4423 param->fp_time_margin = unit;
4430 fprintf(stderr, "%s find: bad time '%s': too large\n",
4440 static int str2quotaid(__u32 *id, const char *arg)
4442 unsigned long int projid_tmp = 0;
4443 char *endptr = NULL;
4445 projid_tmp = strtoul(arg, &endptr, 10);
4446 if (*endptr != '\0')
4448 if (projid_tmp >= UINT32_MAX)
4455 static int name2uid(unsigned int *id, const char *name)
4457 struct passwd *passwd;
4459 passwd = getpwnam(name);
4462 *id = passwd->pw_uid;
4467 static int name2gid(unsigned int *id, const char *name)
4469 struct group *group;
4471 group = getgrnam(name);
4474 *id = group->gr_gid;
4479 static inline int name2projid(unsigned int *id, const char *name)
4484 static int uid2name(char **name, unsigned int id)
4486 struct passwd *passwd;
4488 passwd = getpwuid(id);
4491 *name = passwd->pw_name;
4496 static inline int gid2name(char **name, unsigned int id)
4498 struct group *group;
4500 group = getgrgid(id);
4503 *name = group->gr_name;
4508 static int name2layout(__u32 *layout, char *name)
4510 char *ptr, *layout_name;
4513 for (ptr = name; ; ptr = NULL) {
4514 layout_name = strtok(ptr, ",");
4517 if (strcmp(layout_name, "released") == 0)
4518 *layout |= LOV_PATTERN_F_RELEASED;
4519 else if (strcmp(layout_name, "raid0") == 0)
4520 *layout |= LOV_PATTERN_RAID0;
4521 else if (strcmp(layout_name, "mdt") == 0)
4522 *layout |= LOV_PATTERN_MDT;
4523 else if (strcmp(layout_name, "overstriping") == 0)
4524 *layout |= LOV_PATTERN_OVERSTRIPING;
4531 static int lfs_find(int argc, char **argv)
4536 struct find_param param = {
4539 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4541 struct option long_opts[] = {
4542 { .val = 'A', .name = "atime", .has_arg = required_argument },
4543 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4544 { .val = 'B', .name = "btime", .has_arg = required_argument },
4545 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4546 { .val = LFS_COMP_COUNT_OPT,
4547 .name = "comp-count", .has_arg = required_argument },
4548 { .val = LFS_COMP_COUNT_OPT,
4549 .name = "component-count",
4550 .has_arg = required_argument },
4551 { .val = LFS_COMP_FLAGS_OPT,
4552 .name = "comp-flags", .has_arg = required_argument },
4553 { .val = LFS_COMP_FLAGS_OPT,
4554 .name = "component-flags",
4555 .has_arg = required_argument },
4556 { .val = LFS_COMP_START_OPT,
4557 .name = "comp-start", .has_arg = required_argument },
4558 { .val = LFS_COMP_START_OPT,
4559 .name = "component-start",
4560 .has_arg = required_argument },
4561 { .val = LFS_MIRROR_STATE_OPT,
4562 .name = "mirror-state", .has_arg = required_argument },
4563 { .val = LFS_NEWERXY_OPT,
4564 .name = "newer", .has_arg = required_argument},
4565 { .val = LFS_NEWERXY_OPT,
4566 .name = "neweraa", .has_arg = required_argument},
4567 { .val = LFS_NEWERXY_OPT,
4568 .name = "neweram", .has_arg = required_argument},
4569 { .val = LFS_NEWERXY_OPT,
4570 .name = "newerac", .has_arg = required_argument},
4571 { .val = LFS_NEWERXY_OPT,
4572 .name = "newerab", .has_arg = required_argument},
4573 { .val = LFS_NEWERXY_OPT,
4574 .name = "newerma", .has_arg = required_argument},
4575 { .val = LFS_NEWERXY_OPT,
4576 .name = "newermm", .has_arg = required_argument},
4577 { .val = LFS_NEWERXY_OPT,
4578 .name = "newermc", .has_arg = required_argument},
4579 { .val = LFS_NEWERXY_OPT,
4580 .name = "newermb", .has_arg = required_argument},
4581 { .val = LFS_NEWERXY_OPT,
4582 .name = "newerca", .has_arg = required_argument},
4583 { .val = LFS_NEWERXY_OPT,
4584 .name = "newercm", .has_arg = required_argument},
4585 { .val = LFS_NEWERXY_OPT,
4586 .name = "newercc", .has_arg = required_argument},
4587 { .val = LFS_NEWERXY_OPT,
4588 .name = "newercb", .has_arg = required_argument},
4589 { .val = LFS_NEWERXY_OPT,
4590 .name = "newerba", .has_arg = required_argument},
4591 { .val = LFS_NEWERXY_OPT,
4592 .name = "newerbm", .has_arg = required_argument},
4593 { .val = LFS_NEWERXY_OPT,
4594 .name = "newerbc", .has_arg = required_argument},
4595 { .val = LFS_NEWERXY_OPT,
4596 .name = "newerbb", .has_arg = required_argument},
4597 { .val = LFS_NEWERXY_OPT,
4598 .name = "newerBa", .has_arg = required_argument},
4599 { .val = LFS_NEWERXY_OPT,
4600 .name = "newerBm", .has_arg = required_argument},
4601 { .val = LFS_NEWERXY_OPT,
4602 .name = "newerBc", .has_arg = required_argument},
4603 { .val = LFS_NEWERXY_OPT,
4604 .name = "newerBB", .has_arg = required_argument},
4605 { .val = LFS_NEWERXY_OPT,
4606 .name = "newerat", .has_arg = required_argument},
4607 { .val = LFS_NEWERXY_OPT,
4608 .name = "newermt", .has_arg = required_argument},
4609 { .val = LFS_NEWERXY_OPT,
4610 .name = "newerct", .has_arg = required_argument},
4611 { .val = LFS_NEWERXY_OPT,
4612 .name = "newerbt", .has_arg = required_argument},
4613 { .val = LFS_NEWERXY_OPT,
4614 .name = "newerBt", .has_arg = required_argument},
4615 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4616 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4617 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4618 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4619 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4620 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4621 { .val = 'E', .name = "component-end",
4622 .has_arg = required_argument },
4623 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4624 { .val = LFS_LAYOUT_FOREIGN_OPT,
4625 .name = "foreign", .has_arg = optional_argument},
4626 { .val = 'g', .name = "gid", .has_arg = required_argument },
4627 { .val = 'G', .name = "group", .has_arg = required_argument },
4628 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4629 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4630 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4631 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4632 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4633 { .val = 'L', .name = "layout", .has_arg = required_argument },
4634 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4635 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4636 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4637 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4638 { .val = 'n', .name = "name", .has_arg = required_argument },
4639 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4640 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4641 { .val = 'O', .name = "obd", .has_arg = required_argument },
4642 { .val = 'O', .name = "ost", .has_arg = required_argument },
4643 /* no short option for pool yet, can be 'p' after 2.18 */
4644 { .val = LFS_POOL_OPT,
4645 .name = "pool", .has_arg = required_argument },
4646 { .val = '0', .name = "print0", .has_arg = no_argument },
4647 { .val = 'P', .name = "print", .has_arg = no_argument },
4648 { .val = LFS_PROJID_OPT,
4649 .name = "projid", .has_arg = required_argument },
4650 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4651 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4652 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4653 { .val = 's', .name = "size", .has_arg = required_argument },
4654 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4655 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4656 { .val = 't', .name = "type", .has_arg = required_argument },
4657 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4658 { .val = 'u', .name = "uid", .has_arg = required_argument },
4659 { .val = 'U', .name = "user", .has_arg = required_argument },
4660 { .val = 'z', .name = "extension-size",
4661 .has_arg = required_argument },
4662 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4663 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4664 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4678 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4679 while ((c = getopt_long_only(argc, argv,
4680 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4681 long_opts, &optidx)) >= 0) {
4686 /* '!' is part of option */
4688 * when getopt_long_only() finds a string which is not
4689 * an option nor a known option argument it returns 1
4690 * in that case if we already have found pathstart and pathend
4691 * (i.e. we have the list of pathnames),
4692 * the only supported value is "!"
4694 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4695 if (!isoption && pathend != -1) {
4697 "err: %s: filename|dirname must either precede options or follow options\n",
4702 if (!isoption && pathstart == -1)
4703 pathstart = optind - 1;
4704 if (isoption && pathstart != -1 && pathend == -1)
4705 pathend = optind - 2;
4712 * unknown; opt is "!" or path component,
4713 * checking done above.
4715 if (strcmp(optarg, "!") == 0)
4719 xtime = ¶m.fp_atime;
4720 xsign = ¶m.fp_asign;
4721 param.fp_exclude_atime = !!neg_opt;
4722 /* no break, this falls through to 'B' for btime */
4725 xtime = ¶m.fp_btime;
4726 xsign = ¶m.fp_bsign;
4727 param.fp_exclude_btime = !!neg_opt;
4729 /* no break, this falls through to 'C' for ctime */
4732 xtime = ¶m.fp_ctime;
4733 xsign = ¶m.fp_csign;
4734 param.fp_exclude_ctime = !!neg_opt;
4736 /* no break, this falls through to 'M' for mtime */
4739 xtime = ¶m.fp_mtime;
4740 xsign = ¶m.fp_msign;
4741 param.fp_exclude_mtime = !!neg_opt;
4743 rc = set_time(¶m, &t, xtime, optarg);
4744 if (rc == LONG_MAX) {
4752 if (optarg[0] == '+') {
4753 param.fp_blocks_sign = -1;
4755 } else if (optarg[0] == '-') {
4756 param.fp_blocks_sign = 1;
4760 param.fp_blocks_units = 1024;
4761 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4762 ¶m.fp_blocks_units, 0);
4764 fprintf(stderr, "error: bad blocks '%s'\n",
4768 param.fp_check_blocks = 1;
4769 param.fp_exclude_blocks = !!neg_opt;
4771 case LFS_COMP_COUNT_OPT:
4772 if (optarg[0] == '+') {
4773 param.fp_comp_count_sign = -1;
4775 } else if (optarg[0] == '-') {
4776 param.fp_comp_count_sign = 1;
4781 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4782 if (errno != 0 || *endptr != '\0' ||
4783 param.fp_comp_count > UINT32_MAX) {
4785 "error: bad component count '%s'\n",
4789 param.fp_check_comp_count = 1;
4790 param.fp_exclude_comp_count = !!neg_opt;
4792 case LFS_COMP_FLAGS_OPT:
4793 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4794 ¶m.fp_comp_neg_flags);
4797 "error: bad component flags '%s'\n",
4801 param.fp_check_comp_flags = 1;
4803 __u32 flags = param.fp_comp_neg_flags;
4805 param.fp_comp_neg_flags = param.fp_comp_flags;
4806 param.fp_comp_flags = flags;
4809 case LFS_COMP_START_OPT:
4810 if (optarg[0] == '+') {
4811 param.fp_comp_start_sign = -1;
4813 } else if (optarg[0] == '-') {
4814 param.fp_comp_start_sign = 1;
4818 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4819 ¶m.fp_comp_start_units, 0);
4822 "error: bad component start '%s'\n",
4826 param.fp_check_comp_start = 1;
4827 param.fp_exclude_comp_start = !!neg_opt;
4829 case LFS_MIRROR_STATE_OPT:
4830 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4831 ¶m.fp_mirror_neg_state);
4834 "error: bad mirrored file state '%s'\n",
4838 param.fp_check_mirror_state = 1;
4840 __u16 state = param.fp_mirror_neg_state;
4842 param.fp_mirror_neg_state =
4843 param.fp_mirror_state;
4844 param.fp_mirror_state = state;
4848 if (optarg[0] == '+') {
4849 param.fp_stripe_count_sign = -1;
4851 } else if (optarg[0] == '-') {
4852 param.fp_stripe_count_sign = 1;
4857 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4858 if (errno != 0 || *endptr != '\0' ||
4859 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
4861 "error: bad stripe_count '%s'\n",
4866 param.fp_check_stripe_count = 1;
4867 param.fp_exclude_stripe_count = !!neg_opt;
4871 param.fp_max_depth = strtol(optarg, 0, 0);
4872 if (errno != 0 || param.fp_max_depth < 0) {
4874 "error: bad maxdepth '%s'\n",
4881 if (optarg[0] == '+') {
4882 param.fp_comp_end_sign = -1;
4884 } else if (optarg[0] == '-') {
4885 param.fp_comp_end_sign = 1;
4889 if (arg_is_eof(optarg)) {
4890 param.fp_comp_end = LUSTRE_EOF;
4891 param.fp_comp_end_units = 1;
4894 rc = llapi_parse_size(optarg,
4896 ¶m.fp_comp_end_units, 0);
4897 /* assume units of KB if too small */
4898 if (param.fp_comp_end < 4096)
4899 param.fp_comp_end *= 1024;
4903 "error: bad component end '%s'\n",
4907 param.fp_check_comp_end = 1;
4908 param.fp_exclude_comp_end = !!neg_opt;
4910 case LFS_LAYOUT_FOREIGN_OPT: {
4911 /* all types by default */
4912 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4915 /* check pure numeric */
4916 type = strtoul(optarg, &endptr, 0);
4919 type = check_foreign_type_name(optarg);
4920 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4922 "%s %s: unknown foreign type '%s'\n",
4927 } else if (type >= UINT32_MAX) {
4929 "%s %s: invalid foreign type '%s'\n",
4930 progname, argv[0], optarg);
4934 param.fp_foreign_type = type;
4935 param.fp_check_foreign = 1;
4936 param.fp_exclude_foreign = !!neg_opt;
4939 case LFS_NEWERXY_OPT: {
4945 time_t ref = time(NULL);
4947 /* no need to check bad options, they won't get here */
4948 if (strlen(long_opts[optidx].name) == 7) {
4949 x = long_opts[optidx].name[5];
4950 y = long_opts[optidx].name[6];
4954 static const char *const fmts[] = {
4955 "%Y-%m-%d %H:%M:%S",
4958 "%H:%M:%S", /* sometime today */
4967 for (i = 0; fmts[i] != NULL; i++) {
4970 /* Init for times relative to today */
4971 if (strncmp(fmts[i], "%H", 2) == 0)
4972 localtime_r(&ref, &tm);
4974 memset(&tm, 0, sizeof(tm));
4975 ptr = strptime(optarg, fmts[i], &tm);
4977 while (ptr && isspace(*ptr))
4979 if (ptr == optarg + strlen(optarg)) {
4987 "%s: invalid time '%s'\n",
4990 "supported formats are:\n ");
4991 for (i = 0; fmts[i] != NULL; i++)
4992 fprintf(stderr, "'%s', ",
4994 fprintf(stderr, "\n");
5000 } else if (y == 'b' || y == 'B') {
5003 rc = llapi_get_lum_file(optarg, NULL, &stx,
5005 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5006 if (!(stx.stx_mask & STATX_BTIME))
5011 "%s: get btime failed '%s': %s\n",
5017 ref = stx.stx_btime.tv_sec;
5019 struct stat statbuf;
5021 if (stat(optarg, &statbuf) < 0) {
5023 "%s: cannot stat file '%s': %s\n",
5032 ref = statbuf.st_atime;
5035 ref = statbuf.st_mtime;
5038 ref = statbuf.st_ctime;
5042 "%s: invalid Y argument: '%c'\n",
5051 xidx = NEWERXY_ATIME;
5054 xidx = NEWERXY_MTIME;
5057 xidx = NEWERXY_CTIME;
5061 xidx = NEWERXY_BTIME;
5065 "%s: invalid X argument: '%c'\n",
5072 newery = ¶m.fp_newery[xidx][negidx];
5078 *newery = *newery > ref ? ref : *newery;
5080 *newery = *newery > ref ? *newery : ref;
5082 param.fp_newerxy = 1;
5087 rc = name2gid(¶m.fp_gid, optarg);
5089 if (str2quotaid(¶m.fp_gid, optarg)) {
5091 "Group/GID: %s cannot be found.\n",
5097 param.fp_exclude_gid = !!neg_opt;
5098 param.fp_check_gid = 1;
5101 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5102 ¶m.fp_hash_exflags,
5103 ¶m.fp_hash_type);
5108 if (param.fp_hash_inflags || param.fp_hash_exflags)
5109 param.fp_check_hash_flag = 1;
5110 param.fp_exclude_hash_type = !!neg_opt;
5116 ret = name2layout(¶m.fp_layout, optarg);
5119 param.fp_exclude_layout = !!neg_opt;
5120 param.fp_check_layout = 1;
5124 rc = name2uid(¶m.fp_uid, optarg);
5126 if (str2quotaid(¶m.fp_uid, optarg)) {
5128 "User/UID: %s cannot be found.\n",
5134 param.fp_exclude_uid = !!neg_opt;
5135 param.fp_check_uid = 1;
5138 param.fp_pattern = (char *)optarg;
5139 param.fp_exclude_pattern = !!neg_opt;
5142 if (optarg[0] == '+') {
5143 param.fp_mirror_count_sign = -1;
5145 } else if (optarg[0] == '-') {
5146 param.fp_mirror_count_sign = 1;
5151 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5152 if (errno != 0 || *endptr != '\0' ||
5153 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5155 "error: bad mirror count '%s'\n",
5159 param.fp_check_mirror_count = 1;
5160 param.fp_exclude_mirror_count = !!neg_opt;
5165 char *buf, *token, *next, *p;
5169 buf = strdup(optarg);
5175 param.fp_exclude_obd = !!neg_opt;
5178 while (token && *token) {
5179 token = strchr(token, ',');
5186 param.fp_exclude_mdt = !!neg_opt;
5187 param.fp_num_alloc_mdts += len;
5188 tmp = realloc(param.fp_mdt_uuid,
5189 param.fp_num_alloc_mdts *
5190 sizeof(*param.fp_mdt_uuid));
5196 param.fp_mdt_uuid = tmp;
5198 param.fp_exclude_obd = !!neg_opt;
5199 param.fp_num_alloc_obds += len;
5200 tmp = realloc(param.fp_obd_uuid,
5201 param.fp_num_alloc_obds *
5202 sizeof(*param.fp_obd_uuid));
5208 param.fp_obd_uuid = tmp;
5210 for (token = buf; token && *token; token = next) {
5211 struct obd_uuid *puuid;
5215 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5218 ¶m.fp_obd_uuid[param.fp_num_obds++];
5220 p = strchr(token, ',');
5227 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5232 strncpy(puuid->uuid, token,
5233 sizeof(puuid->uuid));
5240 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5244 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5246 "Pool name %s is too long (max %d)\n",
5247 optarg, LOV_MAXPOOLNAME);
5252 * We do check for empty pool because empty pool
5253 * is used to find V1 LOV attributes
5255 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5256 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5257 param.fp_exclude_pool = !!neg_opt;
5258 param.fp_check_pool = 1;
5260 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5261 case 'p': /* want this for --pool, to match getstripe/find */
5263 "warning: -p deprecated, use --print0 or -0\n");
5266 param.fp_zero_end = 1;
5268 case 'P': /* we always print, this option is a no-op */
5270 case LFS_PROJID_OPT:
5271 rc = name2projid(¶m.fp_projid, optarg);
5273 if (str2quotaid(¶m.fp_projid, optarg)) {
5275 "Invalid project ID: %s\n",
5281 param.fp_exclude_projid = !!neg_opt;
5282 param.fp_check_projid = 1;
5285 if (optarg[0] == '+') {
5286 param.fp_size_sign = -1;
5288 } else if (optarg[0] == '-') {
5289 param.fp_size_sign = 1;
5293 ret = llapi_parse_size(optarg, ¶m.fp_size,
5294 ¶m.fp_size_units, 0);
5296 fprintf(stderr, "error: bad file size '%s'\n",
5300 param.fp_check_size = 1;
5301 param.fp_exclude_size = !!neg_opt;
5304 if (optarg[0] == '+') {
5305 param.fp_stripe_size_sign = -1;
5307 } else if (optarg[0] == '-') {
5308 param.fp_stripe_size_sign = 1;
5312 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5313 ¶m.fp_stripe_size_units, 0);
5314 /* assume units of KB if too small to be valid */
5315 if (param.fp_stripe_size < 4096)
5316 param.fp_stripe_size *= 1024;
5318 fprintf(stderr, "error: bad stripe_size '%s'\n",
5322 param.fp_check_stripe_size = 1;
5323 param.fp_exclude_stripe_size = !!neg_opt;
5326 param.fp_exclude_type = !!neg_opt;
5327 switch (optarg[0]) {
5329 param.fp_type = S_IFBLK;
5332 param.fp_type = S_IFCHR;
5335 param.fp_type = S_IFDIR;
5338 param.fp_type = S_IFREG;
5341 param.fp_type = S_IFLNK;
5344 param.fp_type = S_IFIFO;
5347 param.fp_type = S_IFSOCK;
5350 fprintf(stderr, "error: %s: bad type '%s'\n",
5357 if (optarg[0] == '+') {
5358 param.fp_mdt_count_sign = -1;
5360 } else if (optarg[0] == '-') {
5361 param.fp_mdt_count_sign = 1;
5366 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5367 if (errno != 0 || *endptr != '\0' ||
5368 param.fp_mdt_count >= UINT32_MAX) {
5369 fprintf(stderr, "error: bad mdt_count '%s'\n",
5374 param.fp_check_mdt_count = 1;
5375 param.fp_exclude_mdt_count = !!neg_opt;
5378 if (optarg[0] == '+') {
5379 param.fp_ext_size_sign = -1;
5381 } else if (optarg[0] == '-') {
5382 param.fp_ext_size_sign = 1;
5386 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5387 ¶m.fp_ext_size_units, 0);
5389 fprintf(stderr, "error: bad ext-size '%s'\n",
5393 param.fp_ext_size /= SEL_UNIT_SIZE;
5394 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5395 param.fp_check_ext_size = 1;
5396 param.fp_exclude_ext_size = !!neg_opt;
5404 if (pathstart == -1) {
5405 fprintf(stderr, "error: %s: no filename|pathname\n",
5409 } else if (pathend == -1) {
5415 rc = llapi_find(argv[pathstart], ¶m);
5418 pathbad = pathstart;
5420 } while (++pathstart < pathend);
5423 fprintf(stderr, "%s: failed for '%s': %s\n",
5424 progname, argv[pathbad], strerror(-rc));
5427 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5428 free(param.fp_obd_uuid);
5430 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5431 free(param.fp_mdt_uuid);
5436 static int lfs_getstripe_internal(int argc, char **argv,
5437 struct find_param *param)
5439 struct option long_opts[] = {
5440 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5441 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5442 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5443 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5444 { .val = LFS_COMP_COUNT_OPT,
5445 .name = "comp-count", .has_arg = no_argument },
5446 { .val = LFS_COMP_COUNT_OPT,
5447 .name = "component-count", .has_arg = no_argument },
5448 { .val = LFS_COMP_FLAGS_OPT,
5449 .name = "comp-flags", .has_arg = optional_argument },
5450 { .val = LFS_COMP_FLAGS_OPT,
5451 .name = "component-flags", .has_arg = optional_argument },
5452 { .val = LFS_COMP_START_OPT,
5453 .name = "comp-start", .has_arg = optional_argument },
5454 { .val = LFS_COMP_START_OPT,
5455 .name = "component-start", .has_arg = optional_argument },
5456 { .val = LFS_MIRROR_INDEX_OPT,
5457 .name = "mirror-index", .has_arg = required_argument },
5458 { .val = LFS_MIRROR_ID_OPT,
5459 .name = "mirror-id", .has_arg = required_argument },
5460 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5461 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5462 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5463 { .val = 'd', .name = "directory", .has_arg = no_argument },
5464 { .val = 'D', .name = "default", .has_arg = no_argument },
5465 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5466 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5467 { .val = 'F', .name = "fid", .has_arg = no_argument },
5468 { .val = 'g', .name = "generation", .has_arg = no_argument },
5469 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5470 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5471 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5472 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5473 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5474 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5475 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5476 { .val = 'L', .name = "layout", .has_arg = no_argument },
5477 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5478 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5479 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5480 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5481 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5482 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5483 { .val = 'O', .name = "obd", .has_arg = required_argument },
5484 { .val = 'O', .name = "ost", .has_arg = required_argument },
5485 { .val = 'p', .name = "pool", .has_arg = no_argument },
5486 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5487 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5488 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5489 { .val = 'R', .name = "raw", .has_arg = no_argument },
5490 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5491 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5492 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5493 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5494 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5495 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5496 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5497 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5498 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5499 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5503 int pathstart = -1, pathend = -1;
5507 while ((c = getopt_long(argc, argv,
5508 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5509 long_opts, NULL)) != -1) {
5513 /* '!' is part of option */
5514 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5515 if (!isoption && pathend != -1) {
5517 "error: %s: filename|dirname must either precede options or follow options\n",
5521 if (!isoption && pathstart == -1)
5522 pathstart = optind - 1;
5523 if (isoption && pathstart != -1 && pathend == -1)
5524 pathend = optind - 2;
5528 /* unknown: opt is "!" */
5529 if (strcmp(optarg, "!") == 0)
5533 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5534 param->fp_verbose |= VERBOSE_COUNT;
5535 param->fp_max_depth = 0;
5538 case LFS_COMP_COUNT_OPT:
5539 param->fp_verbose |= VERBOSE_COMP_COUNT;
5540 param->fp_max_depth = 0;
5542 case LFS_COMP_FLAGS_OPT:
5544 rc = comp_str2flags(optarg,
5545 ¶m->fp_comp_flags,
5546 ¶m->fp_comp_neg_flags);
5549 "error: %s bad component flags '%s'.\n",
5553 param->fp_check_comp_flags = 1;
5555 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5556 param->fp_max_depth = 0;
5559 case LFS_COMP_START_OPT:
5562 if (tmp[0] == '+') {
5563 param->fp_comp_start_sign = -1;
5565 } else if (tmp[0] == '-') {
5566 param->fp_comp_start_sign = 1;
5569 rc = llapi_parse_size(tmp,
5570 ¶m->fp_comp_start,
5571 ¶m->fp_comp_start_units, 0);
5574 "error: %s bad component start '%s'.\n",
5578 param->fp_check_comp_start = 1;
5580 param->fp_verbose |= VERBOSE_COMP_START;
5581 param->fp_max_depth = 0;
5584 case LFS_MIRROR_INDEX_OPT: {
5585 unsigned long int mirror_index;
5587 if (optarg[0] == '+') {
5588 param->fp_mirror_index_sign = -1;
5590 } else if (optarg[0] == '-') {
5591 param->fp_mirror_index_sign = 1;
5596 mirror_index = strtoul(optarg, &end, 0);
5597 if (errno != 0 || *end != '\0' ||
5598 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5599 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5601 "%s %s: invalid mirror index '%s'\n",
5602 progname, argv[0], optarg);
5606 param->fp_mirror_index = (__u16)mirror_index;
5608 if (param->fp_mirror_id != 0) {
5610 "%s %s: can't specify both mirror index and mirror ID\n",
5614 param->fp_check_mirror_index = 1;
5615 param->fp_exclude_mirror_index = !!neg_opt;
5618 case LFS_MIRROR_ID_OPT: {
5619 unsigned long int mirror_id;
5621 if (optarg[0] == '+') {
5622 param->fp_mirror_id_sign = -1;
5624 } else if (optarg[0] == '-') {
5625 param->fp_mirror_id_sign = 1;
5630 mirror_id = strtoul(optarg, &end, 0);
5631 if (errno != 0 || *end != '\0' ||
5632 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5633 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5635 "%s %s: invalid mirror ID '%s'\n",
5636 progname, argv[0], optarg);
5640 param->fp_mirror_id = (__u16)mirror_id;
5642 if (param->fp_mirror_index != 0) {
5644 "%s %s: can't specify both mirror index and mirror ID\n",
5648 param->fp_check_mirror_id = 1;
5649 param->fp_exclude_mirror_id = !!neg_opt;
5653 param->fp_max_depth = 0;
5656 param->fp_get_default_lmv = 1;
5661 if (tmp[0] == '+') {
5662 param->fp_comp_end_sign = -1;
5664 } else if (tmp[0] == '-') {
5665 param->fp_comp_end_sign = 1;
5669 if (arg_is_eof(tmp)) {
5670 param->fp_comp_end = LUSTRE_EOF;
5671 param->fp_comp_end_units = 1;
5674 rc = llapi_parse_size(tmp,
5675 ¶m->fp_comp_end,
5676 ¶m->fp_comp_end_units, 0);
5677 /* assume units of KB if too small */
5678 if (param->fp_comp_end < 4096)
5679 param->fp_comp_end *= 1024;
5683 "error: %s bad component end '%s'.\n",
5687 param->fp_check_comp_end = 1;
5689 param->fp_verbose |= VERBOSE_COMP_END;
5690 param->fp_max_depth = 0;
5694 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5695 param->fp_verbose |= VERBOSE_DFID;
5696 param->fp_max_depth = 0;
5700 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5701 param->fp_verbose |= VERBOSE_GENERATION;
5702 param->fp_max_depth = 0;
5706 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5707 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5708 param->fp_max_depth = 0;
5713 param->fp_comp_id = strtoul(optarg, &end, 0);
5714 if (*end != '\0' || param->fp_comp_id == 0 ||
5715 param->fp_comp_id > LCME_ID_MAX) {
5717 "error: %s bad component id '%s'\n",
5721 param->fp_check_comp_id = 1;
5723 param->fp_max_depth = 0;
5724 param->fp_verbose |= VERBOSE_COMP_ID;
5728 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5729 param->fp_verbose |= VERBOSE_PATTERN;
5730 param->fp_max_depth = 0;
5733 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5736 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5739 if (!(param->fp_verbose & VERBOSE_DETAIL))
5740 param->fp_max_depth = 0;
5741 param->fp_verbose |= VERBOSE_MDTINDEX;
5744 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5745 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5746 param->fp_max_depth = 0;
5750 if (param->fp_obd_uuid) {
5752 "error: %s: only one obduuid allowed",
5756 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5759 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5760 param->fp_verbose |= VERBOSE_POOL;
5761 param->fp_max_depth = 0;
5768 param->fp_recursive = 1;
5774 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5775 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5776 param->fp_max_depth = 0;
5780 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5786 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5787 param->fp_verbose |= VERBOSE_EXT_SIZE;
5788 param->fp_max_depth = 0;
5796 if (pathstart == -1) {
5797 fprintf(stderr, "error: %s: no filename|pathname\n",
5800 } else if (pathend == -1) {
5808 if (param->fp_recursive)
5809 param->fp_max_depth = -1;
5810 else if (param->fp_verbose & VERBOSE_DETAIL)
5811 param->fp_max_depth = 1;
5813 if (!param->fp_verbose)
5814 param->fp_verbose = VERBOSE_DEFAULT;
5815 if (param->fp_quiet)
5816 param->fp_verbose = VERBOSE_OBJID;
5819 rc = llapi_getstripe(argv[pathstart], param);
5820 } while (++pathstart < pathend && !rc);
5823 fprintf(stderr, "error: %s failed for %s.\n",
5824 argv[0], argv[optind - 1]);
5828 static int lfs_tgts(int argc, char **argv)
5830 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5831 struct find_param param;
5832 int index = 0, rc = 0;
5837 if (argc == 2 && !realpath(argv[1], path)) {
5839 fprintf(stderr, "error: invalid path '%s': %s\n",
5840 argv[1], strerror(-rc));
5844 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5845 /* Check if we have a mount point */
5846 if (mntdir[0] == '\0')
5849 memset(¶m, 0, sizeof(param));
5850 if (!strcmp(argv[0], "mdts"))
5851 param.fp_get_lmv = 1;
5853 rc = llapi_ostlist(mntdir, ¶m);
5855 fprintf(stderr, "error: %s: failed on %s\n",
5858 if (path[0] != '\0')
5860 memset(mntdir, 0, PATH_MAX);
5866 static int lfs_getstripe(int argc, char **argv)
5868 struct find_param param = { 0 };
5870 param.fp_max_depth = 1;
5871 return lfs_getstripe_internal(argc, argv, ¶m);
5875 static int lfs_getdirstripe(int argc, char **argv)
5877 struct find_param param = { 0 };
5878 struct option long_opts[] = {
5879 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5880 { .val = 'D', .name = "default", .has_arg = no_argument },
5881 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5882 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5883 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5884 { .val = 'O', .name = "obd", .has_arg = required_argument },
5885 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5886 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5887 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5888 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5892 param.fp_get_lmv = 1;
5894 while ((c = getopt_long(argc, argv,
5895 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5899 param.fp_verbose |= VERBOSE_COUNT;
5902 param.fp_get_default_lmv = 1;
5906 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5908 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5911 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5914 param.fp_verbose |= VERBOSE_HASH_TYPE;
5917 if (param.fp_obd_uuid) {
5919 "error: %s: only one obduuid allowed",
5923 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5926 param.fp_recursive = 1;
5929 param.fp_verbose |= VERBOSE_DETAIL;
5935 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5936 progname, argv[0], argv[optind - 1]);
5944 if (param.fp_recursive)
5945 param.fp_max_depth = -1;
5947 if (!param.fp_verbose)
5948 param.fp_verbose = VERBOSE_DEFAULT;
5951 rc = llapi_getstripe(argv[optind], ¶m);
5952 } while (++optind < argc && !rc);
5955 fprintf(stderr, "error: %s failed for %s.\n",
5956 argv[0], argv[optind - 1]);
5961 MNTDF_INODES = 0x0001,
5962 MNTDF_COOKED = 0x0002,
5963 MNTDF_LAZY = 0x0004,
5964 MNTDF_VERBOSE = 0x0008,
5965 MNTDF_SHOW = 0x0010,
5966 MNTDF_DECIMAL = 0x0020,
5969 #define COOK(value, base) \
5972 while (value > base) { \
5980 #define CDF "%11llu"
5981 #define HDF "%8.1f%c"
5985 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5987 double avail, used, ratio = 0;
5990 avail = st->os_ffree;
5991 used = st->os_files - st->os_ffree;
5993 avail = st->os_bavail;
5994 used = st->os_blocks - st->os_bfree;
5996 if (avail + used > 0)
5997 ratio = used / (used + avail) * 100;
5999 /* Round up to match df(1) usage percentage */
6000 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6004 * This is to identify various problem states for "lfs df" if .osn_err = true,
6005 * so only show flags reflecting those states by default. Informational states
6006 * are only shown with "-v" and use lower-case names to distinguish them.
6007 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6009 static struct obd_statfs_state_names {
6010 enum obd_statfs_state osn_state;
6011 const char osn_name;
6014 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6015 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6016 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6017 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6018 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6019 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6020 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6021 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6022 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6025 static int showdf(char *mntdir, struct obd_statfs *stat,
6026 char *uuid, enum mntdf_flags flags,
6027 char *type, int index, int rc)
6029 long long avail, used, total;
6031 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6032 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6033 char tbuf[3 * sizeof(__u64)];
6034 char ubuf[3 * sizeof(__u64)];
6035 char abuf[3 * sizeof(__u64)];
6036 char rbuf[3 * sizeof(__u64)];
6043 if (flags & MNTDF_INODES) {
6044 avail = stat->os_ffree;
6045 used = stat->os_files - stat->os_ffree;
6046 total = stat->os_files;
6048 int shift = flags & MNTDF_COOKED ? 0 : 10;
6050 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6051 used = ((stat->os_blocks - stat->os_bfree) *
6052 stat->os_bsize) >> shift;
6053 total = (stat->os_blocks * stat->os_bsize) >> shift;
6056 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6058 if (flags & MNTDF_COOKED) {
6059 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6063 cook_val = (double)total;
6064 i = COOK(cook_val, base);
6066 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6069 snprintf(tbuf, sizeof(tbuf), CDF, total);
6071 cook_val = (double)used;
6072 i = COOK(cook_val, base);
6074 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6077 snprintf(ubuf, sizeof(ubuf), CDF, used);
6079 cook_val = (double)avail;
6080 i = COOK(cook_val, base);
6082 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6085 snprintf(abuf, sizeof(abuf), CDF, avail);
6087 snprintf(tbuf, sizeof(tbuf), CDF, total);
6088 snprintf(ubuf, sizeof(tbuf), CDF, used);
6089 snprintf(abuf, sizeof(tbuf), CDF, avail);
6092 sprintf(rbuf, RDF, ratio);
6093 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6094 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6096 printf("[%s:%d]", type, index);
6098 if (stat->os_state) {
6102 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6103 if (oss_names[i].osn_state & stat->os_state &&
6104 (oss_names[i].osn_err ||
6105 flags & MNTDF_VERBOSE))
6106 printf("%c", oss_names[i].osn_name);
6113 printf(UUF": inactive device\n", uuid);
6116 printf(UUF": %s\n", uuid, strerror(-rc));
6123 struct ll_stat_type {
6128 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6130 struct ll_statfs_data {
6132 struct obd_statfs sd_st;
6135 struct ll_statfs_buf {
6137 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6140 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6141 int ops, struct ll_statfs_buf *lsb)
6143 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6144 struct obd_uuid uuid_buf;
6145 char *poolname = NULL;
6146 struct ll_stat_type types[] = {
6147 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6148 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6149 { .st_name = NULL } };
6150 struct ll_stat_type *tp;
6151 __u64 ost_files = 0;
6152 __u64 ost_ffree = 0;
6160 poolname = strchr(pool, '.');
6162 if (strncmp(fsname, pool, strlen(fsname))) {
6163 fprintf(stderr, "filesystem name incorrect\n");
6171 fd = open(mntdir, O_RDONLY);
6174 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6179 if (flags & MNTDF_SHOW) {
6180 if (flags & MNTDF_INODES)
6181 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6182 "UUID", "Inodes", "IUsed", "IFree",
6183 "IUse%", "Mounted on");
6185 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6187 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6188 "Used", "Available", "Use%", "Mounted on");
6191 for (tp = types; tp->st_name != NULL; tp++) {
6192 bool have_ost = false;
6194 if (!(tp->st_op & ops))
6197 for (index = 0; ; index++) {
6198 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6199 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6200 type = flags & MNTDF_LAZY ?
6201 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6202 rc2 = llapi_obd_fstatfs(fd, type, index,
6203 &stat_buf, &uuid_buf);
6208 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6209 if (!(flags & MNTDF_VERBOSE))
6211 } else if (rc2 < 0 && rc == 0) {
6216 * If we have OSTs then don't report MDT block counts.
6217 * For MDT-only filesystems the expectation is that all
6218 * layouts have a DoM component. For filesystems with
6219 * OSTs, files are not necessarily going to store data
6220 * on MDTs, and MDT space is limited to a fraction of
6221 * OST space, so don't include it in the summary.
6223 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6230 if (poolname && tp->st_op == LL_STATFS_LOV &&
6231 llapi_search_ost(fsname, poolname,
6232 obd_uuid2str(&uuid_buf)) != 1)
6236 * the llapi_obd_fstatfs() call may have returned with
6237 * an error, but if it filled in uuid_buf we will at
6238 * lease use that to print out a message for that OBD.
6239 * If we didn't get anything in the uuid_buf, then fill
6240 * it in so that we can print an error message.
6242 if (uuid_buf.uuid[0] == '\0')
6243 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6244 "%s%04x", tp->st_name, index);
6246 lsb->sb_buf[lsb->sb_count].sd_index = index;
6247 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6250 if (flags & MNTDF_SHOW)
6251 showdf(mntdir, &stat_buf,
6252 obd_uuid2str(&uuid_buf), flags,
6253 tp->st_name, index, rc2);
6258 if (tp->st_op == LL_STATFS_LMV) {
6259 sum.os_ffree += stat_buf.os_ffree;
6260 sum.os_files += stat_buf.os_files;
6261 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6262 ost_files += stat_buf.os_files;
6263 ost_ffree += stat_buf.os_ffree;
6265 sum.os_blocks += stat_buf.os_blocks *
6267 sum.os_bfree += stat_buf.os_bfree *
6269 sum.os_bavail += stat_buf.os_bavail *
6277 * If we have _some_ OSTs, but don't have as many free objects on the
6278 * OST as inodes on the MDTs, reduce the reported number of inodes
6279 * to compensate, so that the "inodes in use" number is correct.
6280 * This should be kept in sync with ll_statfs_internal().
6282 if (ost_files && ost_ffree < sum.os_ffree) {
6283 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6284 sum.os_ffree = ost_ffree;
6286 if (flags & MNTDF_SHOW) {
6288 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6296 static int lfs_setdirstripe(int argc, char **argv)
6299 struct lfs_setstripe_args lsa = { 0 };
6300 struct llapi_stripe_param *param = NULL;
6301 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6304 char *mode_opt = NULL;
6305 bool default_stripe = false;
6306 bool delete = false;
6307 bool foreign_mode = false;
6308 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6309 mode_t previous_mode = 0;
6311 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6312 struct option long_opts[] = {
6313 { .val = 'c', .name = "count", .has_arg = required_argument },
6314 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6315 { .val = 'd', .name = "delete", .has_arg = no_argument },
6316 { .val = 'D', .name = "default", .has_arg = no_argument },
6317 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6318 { .val = LFS_LAYOUT_FLAGS_OPT,
6319 .name = "flags", .has_arg = required_argument },
6320 { .val = LFS_LAYOUT_FOREIGN_OPT,
6321 .name = "foreign", .has_arg = optional_argument},
6322 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6323 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6324 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6325 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6327 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6328 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6329 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6331 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6332 { .val = 'i', .name = "index", .has_arg = required_argument },
6334 { .val = 'o', .name = "mode", .has_arg = required_argument },
6335 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6336 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6338 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6339 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6340 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6344 setstripe_args_init(&lsa);
6346 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6347 long_opts, NULL)) >= 0) {
6355 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6356 if (errno != 0 || *end != '\0' ||
6357 lsa.lsa_stripe_count < -1 ||
6358 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6360 "%s %s: invalid stripe count '%s'\n",
6361 progname, argv[0], optarg);
6367 default_stripe = true;
6370 default_stripe = true;
6372 case LFS_LAYOUT_FOREIGN_OPT:
6374 /* check pure numeric */
6375 type = strtoul(optarg, &end, 0);
6378 type = check_foreign_type_name(optarg);
6379 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6381 "%s %s: unknown foreign type '%s'\n",
6386 } else if (type >= UINT32_MAX) {
6388 "%s %s: invalid foreign type '%s'\n",
6389 progname, argv[0], optarg);
6393 foreign_mode = true;
6395 case LFS_LAYOUT_FLAGS_OPT:
6397 flags = strtoul(optarg, &end, 16);
6398 if (errno != 0 || *end != '\0' ||
6399 flags >= UINT32_MAX) {
6401 "%s %s: invalid hex flags '%s'\n",
6402 progname, argv[0], optarg);
6405 if (!foreign_mode) {
6407 "%s %s: hex flags must be specified with --foreign option\n",
6412 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6415 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6418 lsa.lsa_pattern = check_hashtype(optarg);
6419 if (lsa.lsa_pattern == 0) {
6421 "%s %s: bad stripe hash type '%s'\n",
6422 progname, argv[0], optarg);
6427 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6430 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6431 if (strcmp(argv[optind - 1], "--index") == 0)
6433 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6436 lsa.lsa_nr_tgts = parse_targets(mdts,
6437 sizeof(mdts) / sizeof(__u32),
6438 lsa.lsa_nr_tgts, optarg, NULL);
6439 if (lsa.lsa_nr_tgts < 0) {
6441 "%s %s: invalid MDT target(s) '%s'\n",
6442 progname, argv[0], optarg);
6446 lsa.lsa_tgts = mdts;
6447 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6448 lsa.lsa_stripe_off = mdts[0];
6450 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6453 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6462 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6463 progname, argv[0], argv[optind - 1]);
6468 if (optind == argc) {
6469 fprintf(stderr, "%s %s: DIR must be specified\n",
6474 if (xattr && !foreign_mode) {
6476 * only print a warning as this is armless and will be
6480 "%s %s: xattr has been specified for non-foreign layout\n",
6482 } else if (foreign_mode && !xattr) {
6484 "%s %s: xattr must be provided in foreign mode\n",
6489 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6490 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6492 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6497 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6498 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6499 /* if no parameters set, create directory on least-used MDTs */
6500 lsa.lsa_stripe_off = -1;
6501 lsa.lsa_stripe_count = 1;
6505 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6506 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6508 "%s %s: cannot specify -d with -c or -i options\n",
6514 mode = strtoul(mode_opt, &end, 8);
6517 "%s %s: bad MODE '%s'\n",
6518 progname, argv[0], mode_opt);
6521 previous_mode = umask(0);
6524 /* foreign LMV/dir case */
6526 if (argc > optind + 1) {
6528 "%s %s: cannot specify multiple foreign dirs\n",
6533 dname = argv[optind];
6534 result = llapi_dir_create_foreign(dname, mode, type, flags,
6538 "%s mkdir: can't create foreign dir '%s': %s\n",
6539 progname, dname, strerror(-result));
6544 * initialize stripe parameters, in case param is converted to specific,
6545 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6547 param = calloc(1, offsetof(typeof(*param),
6548 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6549 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6552 "%s %s: cannot allocate memory for parameters: %s\n",
6553 progname, argv[0], strerror(ENOMEM));
6557 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6558 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6559 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6560 lsa.lsa_stripe_count == 0))
6561 lsa.lsa_stripe_count = 1;
6562 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6563 param->lsp_stripe_count = lsa.lsa_stripe_count;
6564 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6565 param->lsp_stripe_offset = -1;
6567 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6568 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6569 param->lsp_stripe_pattern = lsa.lsa_pattern;
6571 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6572 param->lsp_pool = lsa.lsa_pool_name;
6573 param->lsp_is_specific = false;
6574 if (lsa.lsa_nr_tgts > 1) {
6575 if (lsa.lsa_stripe_count > 0 &&
6576 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6577 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6579 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6580 argv[0], lsa.lsa_stripe_count,
6586 param->lsp_is_specific = true;
6587 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6588 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6591 dname = argv[optind];
6593 if (default_stripe) {
6594 result = llapi_dir_set_default_lmv(dname, param);
6597 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6598 progname, dname, strerror(-result));
6602 result = llapi_dir_create(dname, mode, param);
6605 "%s setdirstripe: cannot create dir '%s': %s\n",
6606 progname, dname, strerror(-result));
6607 } while (!result && (dname = argv[++optind]));
6610 umask(previous_mode);
6617 static int lfs_rmentry(int argc, char **argv)
6624 fprintf(stderr, "error: %s: missing dirname\n",
6630 dname = argv[index];
6632 result = llapi_direntry_remove(dname);
6635 "error: %s: remove dir entry '%s' failed\n",
6639 dname = argv[++index];
6644 static int lfs_unlink_foreign(int argc, char **argv)
6651 fprintf(stderr, "error: %s: missing pathname\n",
6658 while (name != NULL) {
6659 result = llapi_unlink_foreign(name);
6662 "error: %s: unlink foreign entry '%s' failed\n",
6666 name = argv[++index];
6671 static int lfs_mv(int argc, char **argv)
6673 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6674 struct find_param param = {
6681 struct option long_opts[] = {
6682 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6683 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6684 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6687 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6689 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6692 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6696 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6697 if (errno != 0 || *end != '\0' ||
6698 lmu.lum_stripe_offset >= UINT32_MAX) {
6699 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6705 param.fp_verbose = VERBOSE_DETAIL;
6708 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6709 progname, argv[optind - 1]);
6714 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6715 fprintf(stderr, "%s mv: MDT index must be specified\n",
6720 if (optind >= argc) {
6721 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6725 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6727 /* initialize migrate mdt parameters */
6728 param.fp_lmv_md = &lmu;
6729 param.fp_migrate = 1;
6730 rc = llapi_migrate_mdt(argv[optind], ¶m);
6732 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6733 progname, argv[optind], lmu.lum_stripe_offset,
6738 static int lfs_osts(int argc, char **argv)
6740 return lfs_tgts(argc, argv);
6743 static int lfs_mdts(int argc, char **argv)
6745 return lfs_tgts(argc, argv);
6748 static int lfs_df(int argc, char **argv)
6750 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6751 enum mntdf_flags flags = MNTDF_SHOW;
6752 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6753 int c, rc = 0, index = 0;
6754 char fsname[PATH_MAX] = "", *pool_name = NULL;
6755 struct option long_opts[] = {
6756 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
6757 { .val = 'H', .name = "si", .has_arg = no_argument },
6758 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6759 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6760 { .val = 'p', .name = "pool", .has_arg = required_argument },
6761 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6764 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
6767 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
6770 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
6773 flags |= MNTDF_INODES;
6776 flags |= MNTDF_LAZY;
6782 flags |= MNTDF_VERBOSE;
6788 if (optind < argc && !realpath(argv[optind], path)) {
6790 fprintf(stderr, "error: invalid path '%s': %s\n",
6791 argv[optind], strerror(-rc));
6795 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6796 /* Check if we have a mount point */
6797 if (mntdir[0] == '\0')
6800 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6801 if (rc || path[0] != '\0')
6803 fsname[0] = '\0'; /* avoid matching in next loop */
6804 mntdir[0] = '\0'; /* avoid matching in next loop */
6810 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6811 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6815 if (opt_fsname == opt_instance) { /* both true or both false */
6816 rc = llapi_getname(mntdir, buf, buflen);
6817 } else if (opt_fsname) {
6819 * llapi_search_mounts() fills @buf with fsname, but that is not
6820 * called if explicit paths are specified on the command-line
6823 rc = llapi_get_fsname(mntdir, buf, buflen);
6824 } else /* if (opt_instance) */ {
6825 rc = llapi_get_instance(mntdir, buf, buflen);
6829 fprintf(stderr, "cannot get instance for '%s': %s\n",
6830 mntdir, strerror(-rc));
6835 printf("%s %s\n", buf, mntdir);
6837 printf("%s\n", buf);
6842 static int lfs_getname(int argc, char **argv)
6844 struct option long_opts[] = {
6845 { .val = 'h', .name = "help", .has_arg = no_argument },
6846 { .val = 'i', .name = "instance", .has_arg = no_argument },
6847 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6849 bool opt_instance = false, opt_fsname = false;
6850 char fsname[PATH_MAX] = "";
6853 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6856 opt_instance = true;
6867 if (optind == argc) { /* no paths specified, get all paths. */
6868 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6871 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6872 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6873 opt_instance, opt_fsname, true);
6876 path[0] = fsname[0] = mntdir[0] = '\0';
6878 } else { /* paths specified, only attempt to search these. */
6881 /* if only one path is given, print only requested info */
6882 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6884 for (; optind < argc; optind++) {
6885 rc2 = print_instance(argv[optind], fsname,
6886 sizeof(fsname), opt_instance,
6887 opt_fsname, opt_mntdir);
6897 static int lfs_check(int argc, char **argv)
6899 char mntdir[PATH_MAX] = {'\0'};
6908 fprintf(stderr, "%s check: server type must be specified\n",
6913 obd_types[0] = obd_type1;
6914 obd_types[1] = obd_type2;
6915 obd_types[2] = obd_type3;
6917 if (strcmp(argv[1], "osts") == 0) {
6918 strcpy(obd_types[0], "osc");
6919 } else if (strcmp(argv[1], "mdts") == 0 ||
6920 strcmp(argv[1], "mds") == 0) {
6921 strcpy(obd_types[0], "mdc");
6922 } else if (strcmp(argv[1], "mgts") == 0) {
6923 strcpy(obd_types[0], "mgc");
6924 } else if (strcmp(argv[1], "all") == 0 ||
6925 strcmp(argv[1], "servers") == 0) {
6927 strcpy(obd_types[0], "osc");
6928 strcpy(obd_types[1], "mdc");
6929 strcpy(obd_types[2], "mgc");
6931 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6936 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6937 if (rc < 0 || mntdir[0] == '\0') {
6939 "%s check: cannot find mounted Lustre filesystem: %s\n",
6940 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6944 rc = llapi_target_check(num_types, obd_types, mntdir);
6946 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6947 progname, argv[1], strerror(-rc));
6952 #ifdef HAVE_SYS_QUOTA_H
6953 #define ADD_OVERFLOW(a, b) \
6954 ((((a) + (b)) < (a)) ? \
6955 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6957 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6958 * returns the value or ULONG_MAX on integer overflow or incorrect format
6960 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6961 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6962 * 3. empty integer value is interpreted as 0
6964 static unsigned long str2sec(const char *timestr)
6966 const char spec[] = "smhdw";
6967 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6968 unsigned long val = 0;
6971 if (strpbrk(timestr, spec) == NULL) {
6973 * no specifiers inside the time string,
6974 * should treat it as an integer value
6976 val = strtoul(timestr, &tail, 10);
6977 return *tail ? ULONG_MAX : val;
6980 /* format string is XXwXXdXXhXXmXXs */
6986 v = strtoul(timestr, &tail, 10);
6987 if (v == ULONG_MAX || *tail == '\0')
6989 * value too large (ULONG_MAX or more)
6990 * or missing specifier
6994 ptr = strchr(spec, *tail);
6996 /* unknown specifier */
7001 /* check if product will overflow the type */
7002 if (!(v < ULONG_MAX / mult[ind]))
7005 ADD_OVERFLOW(val, mult[ind] * v);
7006 if (val == ULONG_MAX)
7018 #define ARG2ULL(nr, str, def_units) \
7020 unsigned long long limit, units = def_units; \
7023 rc = llapi_parse_size(str, &limit, &units, 1); \
7025 fprintf(stderr, "%s: invalid limit '%s'\n", \
7032 static inline int has_times_option(int argc, char **argv)
7036 for (i = 1; i < argc; i++)
7037 if (!strcmp(argv[i], "-t"))
7043 static inline int lfs_verify_poolarg(char *pool)
7045 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7047 "Pool name '%.*s' is longer than %d\n",
7048 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7054 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7057 char *mnt, *obd_type = (char *)qctl->obd_type;
7058 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7059 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7060 struct option long_opts[] = {
7061 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7062 { .val = 'g', .name = "group", .has_arg = no_argument },
7063 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7064 { .val = 'p', .name = "projid", .has_arg = no_argument },
7065 { .val = 't', .name = "times", .has_arg = no_argument },
7066 { .val = 'u', .name = "user", .has_arg = no_argument },
7067 { .val = LFS_POOL_OPT,
7068 .name = "pool", .has_arg = required_argument },
7072 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7073 qctl->qc_type = ALLQUOTA;
7075 while ((c = getopt_long(argc, argv, "b:gi:ptu",
7076 long_opts, NULL)) != -1) {
7087 if (qctl->qc_type != ALLQUOTA) {
7089 "error: -u/g/p can't be used more than once\n");
7092 qctl->qc_type = qtype;
7095 if (strncmp(optarg, NOTIFY_GRACE,
7096 strlen(NOTIFY_GRACE)) == 0) {
7097 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7099 dqi->dqi_bgrace = str2sec(optarg);
7100 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7102 "error: bad block-grace: %s\n",
7107 dqb->dqb_valid |= QIF_BTIME;
7110 if (strncmp(optarg, NOTIFY_GRACE,
7111 strlen(NOTIFY_GRACE)) == 0) {
7112 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7114 dqi->dqi_igrace = str2sec(optarg);
7115 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7117 "error: bad inode-grace: %s\n",
7122 dqb->dqb_valid |= QIF_ITIME;
7124 case 't': /* Yes, of course! */
7127 if (lfs_verify_poolarg(optarg))
7130 "Trying to set grace for pool %s\n", optarg);
7131 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7132 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7134 /* getopt prints error message for us when opterr != 0 */
7140 if (qctl->qc_type == ALLQUOTA) {
7141 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
7145 if (optind != argc - 1) {
7146 fprintf(stderr, "error: unexpected parameters encountered\n");
7151 rc = llapi_quotactl(mnt, qctl);
7154 fprintf(stderr, "%s %s ", obd_type,
7155 obd_uuid2str(&qctl->obd_uuid));
7156 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7163 #define BSLIMIT (1 << 0)
7164 #define BHLIMIT (1 << 1)
7165 #define ISLIMIT (1 << 2)
7166 #define IHLIMIT (1 << 3)
7168 int lfs_setquota(int argc, char **argv)
7171 struct if_quotactl *qctl;
7172 char *mnt, *obd_type;
7173 struct obd_dqblk *dqb;
7174 struct option long_opts[] = {
7175 { .val = 'b', .name = "block-softlimit",
7176 .has_arg = required_argument },
7177 { .val = 'B', .name = "block-hardlimit",
7178 .has_arg = required_argument },
7179 { .val = 'd', .name = "default", .has_arg = no_argument },
7180 { .val = 'g', .name = "group", .has_arg = required_argument },
7181 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7182 { .val = 'i', .name = "inode-softlimit",
7183 .has_arg = required_argument },
7184 { .val = 'I', .name = "inode-hardlimit",
7185 .has_arg = required_argument },
7186 { .val = 'p', .name = "projid", .has_arg = required_argument },
7187 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7188 { .val = 'u', .name = "user", .has_arg = required_argument },
7189 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7190 { .val = LFS_POOL_OPT,
7191 .name = "pool", .has_arg = required_argument },
7193 unsigned int limit_mask = 0;
7194 bool use_default = false;
7195 int qtype, qctl_len;
7197 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7198 qctl = malloc(qctl_len);
7202 memset(qctl, 0, qctl_len);
7203 obd_type = (char *)qctl->obd_type;
7204 dqb = &qctl->qc_dqblk;
7206 if (has_times_option(argc, argv)) {
7207 rc = lfs_setquota_times(argc, argv, qctl);
7211 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7212 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7213 * so it can be used as a marker that qc_type
7214 * isn't reinitialized from command line
7217 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7218 long_opts, NULL)) != -1) {
7221 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7224 goto quota_type_def;
7227 rc = name2uid(&qctl->qc_id, optarg);
7230 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7233 goto quota_type_def;
7236 rc = name2gid(&qctl->qc_id, optarg);
7239 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7242 goto quota_type_def;
7245 rc = name2projid(&qctl->qc_id, optarg);
7248 if (str2quotaid(&qctl->qc_id, optarg)) {
7250 "%s setquota: invalid id '%s'\n",
7257 if (qctl->qc_id == 0) {
7259 "%s setquota: can't set quota for root usr/group/project.\n",
7266 if (qctl->qc_type != ALLQUOTA) {
7268 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7273 qctl->qc_type = qtype;
7276 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7280 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7281 dqb->dqb_bsoftlimit >>= 10;
7282 limit_mask |= BSLIMIT;
7283 if (dqb->dqb_bsoftlimit &&
7284 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7286 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7288 (unsigned long long)dqb->dqb_bsoftlimit,
7292 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7293 dqb->dqb_bhardlimit >>= 10;
7294 limit_mask |= BHLIMIT;
7295 if (dqb->dqb_bhardlimit &&
7296 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7298 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7299 "See '%s help setquota' or Lustre manual for details\n",
7301 (unsigned long long)dqb->dqb_bhardlimit,
7305 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7306 limit_mask |= ISLIMIT;
7307 if (dqb->dqb_isoftlimit &&
7308 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7310 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7312 (unsigned long long)dqb->dqb_isoftlimit,
7316 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7317 limit_mask |= IHLIMIT;
7318 if (dqb->dqb_ihardlimit &&
7319 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7321 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7323 (unsigned long long)dqb->dqb_ihardlimit,
7327 if (lfs_verify_poolarg(optarg)) {
7332 "Trying to set quota for pool %s\n", optarg);
7333 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7334 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7338 "%s setquota: unrecognized option '%s'\n",
7339 progname, argv[optind - 1]);
7345 if (qctl->qc_type == ALLQUOTA) {
7347 "%s setquota: either -u or -g must be specified\n",
7353 if (!use_default && limit_mask == 0) {
7355 "%s setquota: at least one limit must be specified\n",
7361 if (use_default && limit_mask != 0) {
7363 "%s setquota: limits should not be specified when using default quota\n",
7369 if (use_default && qctl->qc_id == 0) {
7371 "%s setquota: can not set default quota for root user/group/project\n",
7377 if (optind != argc - 1) {
7379 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7380 progname, argv[optind]);
7388 dqb->dqb_bhardlimit = 0;
7389 dqb->dqb_bsoftlimit = 0;
7390 dqb->dqb_ihardlimit = 0;
7391 dqb->dqb_isoftlimit = 0;
7394 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7395 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7396 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7397 /* sigh, we can't just set blimits/ilimits */
7398 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7399 .qc_type = qctl->qc_type,
7400 .qc_id = qctl->qc_id};
7402 rc = llapi_quotactl(mnt, &tmp_qctl);
7406 if (!(limit_mask & BHLIMIT))
7407 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7408 if (!(limit_mask & BSLIMIT))
7409 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7410 if (!(limit_mask & IHLIMIT))
7411 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7412 if (!(limit_mask & ISLIMIT))
7413 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7415 /* Keep grace times if we have got no softlimit arguments */
7416 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7417 dqb->dqb_valid |= QIF_BTIME;
7418 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7421 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7422 dqb->dqb_valid |= QIF_ITIME;
7423 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7427 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7428 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7430 rc = llapi_quotactl(mnt, qctl);
7434 "%s setquota: cannot quotactl '%s' '%s': %s",
7436 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7443 /* Converts seconds value into format string
7444 * result is returned in buf
7446 * 1. result is in descenting order: 1w2d3h4m5s
7447 * 2. zero fields are not filled (except for p. 3): 5d1s
7448 * 3. zero seconds value is presented as "0s"
7450 static char *__sec2str(time_t seconds, char *buf)
7452 const char spec[] = "smhdw";
7453 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7458 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7459 c = seconds / mult[i];
7461 if (c > 0 || (i == 0 && buf == tail))
7462 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7471 static void sec2str(time_t seconds, char *buf, int rc)
7478 tail = __sec2str(seconds, tail);
7480 if (rc && tail - buf < 39) {
7486 static void diff2str(time_t seconds, char *buf, time_t now)
7491 if (seconds <= now) {
7492 strcpy(buf, "none");
7495 __sec2str(seconds - now, buf);
7498 static void print_quota_title(char *name, struct if_quotactl *qctl,
7499 bool human_readable, bool show_default)
7502 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7503 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7504 "Filesystem", "bquota", "blimit", "bgrace",
7505 "iquota", "ilimit", "igrace");
7507 printf("Disk quotas for %s %s (%cid %u):\n",
7508 qtype_name(qctl->qc_type), name,
7509 *qtype_name(qctl->qc_type), qctl->qc_id);
7510 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7511 "Filesystem", human_readable ? "used" : "kbytes",
7512 "quota", "limit", "grace",
7513 "files", "quota", "limit", "grace");
7517 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7520 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7523 snprintf(buf, buflen, "%5.4gP",
7524 (double)num / ((__u64)1 << 40));
7526 snprintf(buf, buflen, "%5.4gT",
7527 (double)num / (1 << 30));
7529 snprintf(buf, buflen, "%5.4gG",
7530 (double)num / (1 << 20));
7532 snprintf(buf, buflen, "%5.4gM",
7533 (double)num / (1 << 10));
7535 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7539 #define STRBUF_LEN 32
7540 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7541 int rc, bool h, bool show_default)
7547 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7548 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7549 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7550 int bover = 0, iover = 0;
7551 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7552 char numbuf[3][STRBUF_LEN];
7554 char strbuf[STRBUF_LEN];
7556 if (dqb->dqb_bhardlimit &&
7557 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7559 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7560 if (dqb->dqb_btime > now)
7566 if (dqb->dqb_ihardlimit &&
7567 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7569 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7570 if (dqb->dqb_itime > now)
7576 if (strlen(mnt) > 15)
7577 printf("%s\n%15s", mnt, "");
7579 printf("%15s", mnt);
7582 diff2str(dqb->dqb_btime, timebuf, now);
7583 else if (show_default)
7584 snprintf(timebuf, sizeof(timebuf), "%llu",
7585 (unsigned long long)dqb->dqb_btime);
7587 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7588 strbuf, sizeof(strbuf), h);
7589 if (rc == -EREMOTEIO)
7590 sprintf(numbuf[0], "%s*", strbuf);
7592 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7593 "%s" : "[%s]", strbuf);
7595 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7596 if (type == QC_GENERAL)
7597 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7598 "%s" : "[%s]", strbuf);
7600 sprintf(numbuf[1], "%s", "-");
7602 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7603 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7604 "%s" : "[%s]", strbuf);
7607 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7609 printf(" %7s%c %6s %7s %7s",
7610 numbuf[0], bover ? '*' : ' ', numbuf[1],
7611 numbuf[2], bover > 1 ? timebuf : "-");
7614 diff2str(dqb->dqb_itime, timebuf, now);
7615 else if (show_default)
7616 snprintf(timebuf, sizeof(timebuf), "%llu",
7617 (unsigned long long)dqb->dqb_itime);
7619 snprintf(numbuf[0], sizeof(numbuf),
7620 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7621 (uintmax_t)dqb->dqb_curinodes);
7623 if (type == QC_GENERAL)
7624 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7626 (uintmax_t)dqb->dqb_isoftlimit);
7628 sprintf(numbuf[1], "%s", "-");
7630 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7631 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7634 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7635 else if (type != QC_OSTIDX)
7636 printf(" %7s%c %6s %7s %7s",
7637 numbuf[0], iover ? '*' : ' ', numbuf[1],
7638 numbuf[2], iover > 1 ? timebuf : "-");
7640 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7642 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7643 qctl->qc_cmd == Q_GETOINFO) {
7647 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7648 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7650 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7651 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7652 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7654 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7656 printf("Block grace time: %s; Inode grace time: %s\n",
7657 bgtimebuf, igtimebuf);
7661 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7665 /* format is "lustre-OST0001" */
7666 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7668 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7673 *idx = strtoul(dash, &endp, 16);
7674 if (*idx > 0xffff) {
7675 fprintf(stderr, "wrong index %s\n", tgtname);
7682 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7683 bool h, __u64 *total)
7685 int rc = 0, rc1 = 0, count = 0, i = 0;
7686 char **list = NULL, *buffer = NULL;
7687 __u32 valid = qctl->qc_valid;
7689 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7692 /* Is it correct for the case OST0000, OST0002, OST0003 -
7693 * we will ask OST0001 that is absent and won't ask OST0003? */
7694 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7696 fprintf(stderr, "can not get %s count: %s\n",
7697 is_mdt ? "mdt" : "ost", strerror(-rc));
7701 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7702 char fname[PATH_MAX];
7703 char fsname[LUSTRE_MAXFSNAME + 1];
7704 int bufsize = sizeof(struct obd_uuid) * count;
7706 rc = llapi_search_fsname(mnt, fsname);
7708 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7712 buffer = malloc(bufsize + sizeof(*list) * count);
7715 list = (char **)(buffer + bufsize);
7716 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7717 count = llapi_get_poolmembers(fname, list, count,
7723 for (i = 0; i < count; i++) {
7724 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7727 if (tgt_name2index(list[i], &index))
7729 qctl->qc_idx = index;
7734 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7735 rc = llapi_quotactl(mnt, qctl);
7737 /* It is remote client case. */
7738 if (rc == -EOPNOTSUPP) {
7745 fprintf(stderr, "quotactl %s%d failed.\n",
7746 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7750 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7751 qctl->qc_valid, 0, h, false);
7752 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7753 qctl->qc_dqblk.dqb_bhardlimit;
7758 qctl->qc_valid = valid;
7762 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7763 int verbose, int quiet, bool human_readable,
7766 int rc1 = 0, rc2 = 0, rc3 = 0;
7767 char *obd_type = (char *)qctl->obd_type;
7768 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7769 __u64 total_ialloc = 0, total_balloc = 0;
7770 bool use_default_for_blk = false;
7771 bool use_default_for_file = false;
7774 rc1 = llapi_quotactl(mnt, qctl);
7778 fprintf(stderr, "%s quotas are not enabled.\n",
7779 qtype_name(qctl->qc_type));
7782 fprintf(stderr, "Permission denied.\n");
7785 /* We already got error message. */
7788 fprintf(stderr, "Unexpected quotactl error: %s\n",
7793 if (!show_default && qctl->qc_id == 0) {
7794 qctl->qc_dqblk.dqb_bhardlimit = 0;
7795 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7796 qctl->qc_dqblk.dqb_ihardlimit = 0;
7797 qctl->qc_dqblk.dqb_isoftlimit = 0;
7798 qctl->qc_dqblk.dqb_btime = 0;
7799 qctl->qc_dqblk.dqb_itime = 0;
7800 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7803 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7804 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7805 use_default_for_blk = true;
7806 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7809 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7810 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7811 use_default_for_file = true;
7812 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7815 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7816 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7817 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7818 print_quota_title(name, qctl, human_readable, show_default);
7820 if (rc1 && *obd_type)
7821 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7823 if (qctl->qc_valid != QC_GENERAL)
7826 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7827 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7828 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7829 (QIF_LIMITS|QIF_USAGE));
7831 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7833 if (!show_default && verbose &&
7834 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7835 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7836 char strbuf[STRBUF_LEN];
7838 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7840 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7842 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7844 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7845 (uintmax_t)total_ialloc, strbuf);
7848 if (use_default_for_blk)
7849 printf("%cid %u is using default block quota setting\n",
7850 *qtype_name(qctl->qc_type), qctl->qc_id);
7852 if (use_default_for_file)
7853 printf("%cid %u is using default file quota setting\n",
7854 *qtype_name(qctl->qc_type), qctl->qc_id);
7856 if (rc1 || rc2 || rc3 || inacc)
7857 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7871 static int lfs_project(int argc, char **argv)
7873 int ret = 0, err = 0, c, i;
7874 struct project_handle_control phc = { 0 };
7875 enum lfs_project_ops_t op;
7878 phc.assign_projid = false;
7879 /* default action */
7880 op = LFS_PROJECT_LIST;
7882 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7885 if (op != LFS_PROJECT_LIST) {
7887 "%s: cannot specify '-c' '-C' '-s' together\n",
7892 op = LFS_PROJECT_CHECK;
7895 if (op != LFS_PROJECT_LIST) {
7897 "%s: cannot specify '-c' '-C' '-s' together\n",
7902 op = LFS_PROJECT_CLEAR;
7905 if (op != LFS_PROJECT_LIST) {
7907 "%s: cannot specify '-c' '-C' '-s' together\n",
7912 phc.set_inherit = true;
7913 op = LFS_PROJECT_SET;
7919 phc.keep_projid = true;
7922 phc.recursive = true;
7925 if (str2quotaid(&phc.projid, optarg)) {
7927 "Invalid project ID: %s\n",
7932 phc.assign_projid = true;
7936 phc.newline = false;
7939 fprintf(stderr, "%s: invalid option '%c'\n",
7945 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7946 op = LFS_PROJECT_SET;
7947 phc.set_projid = true;
7948 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7949 phc.set_projid = true;
7953 case LFS_PROJECT_CHECK:
7954 if (phc.keep_projid) {
7956 "%s: '-k' is useless together with '-c'\n",
7961 case LFS_PROJECT_CLEAR:
7964 "%s: '-0' is useless together with '-C'\n",
7968 if (phc.assign_projid) {
7970 "%s: '-p' is useless together with '-C'\n",
7975 case LFS_PROJECT_SET:
7978 "%s: '-0' is useless together with '-s'\n",
7982 if (phc.keep_projid) {
7984 "%s: '-k' is useless together with '-s'\n",
7992 "%s: '-0' is useless for list operations\n",
8002 fprintf(stderr, "%s: missing file or directory target(s)\n",
8007 for (i = 0; i < argc; i++) {
8009 case LFS_PROJECT_CHECK:
8010 err = lfs_project_check(argv[i], &phc);
8012 case LFS_PROJECT_LIST:
8013 err = lfs_project_list(argv[i], &phc);
8015 case LFS_PROJECT_CLEAR:
8016 err = lfs_project_clear(argv[i], &phc);
8018 case LFS_PROJECT_SET:
8019 err = lfs_project_set(argv[i], &phc);
8031 static int lfs_quota(int argc, char **argv)
8034 char *mnt, *name = NULL;
8035 struct if_quotactl *qctl;
8037 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8038 __u32 valid = QC_GENERAL, idx = 0;
8039 bool human_readable = false;
8040 bool show_default = false;
8042 struct option long_opts[] = {
8043 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
8046 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8050 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8051 qctl->qc_type = ALLQUOTA;
8052 obd_uuid = (char *)qctl->obd_uuid.uuid;
8054 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8055 long_opts, NULL)) != -1) {
8058 show_default = true;
8063 show_default = true;
8068 show_default = true;
8072 if (qctl->qc_type != ALLQUOTA) {
8074 "%s quota: only one of -u, -g, or -p may be specified\n",
8079 qctl->qc_type = qtype;
8082 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8085 valid = qctl->qc_valid = QC_UUID;
8086 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8089 valid = qctl->qc_valid = QC_MDTIDX;
8090 idx = qctl->qc_idx = atoi(optarg);
8091 if (idx == 0 && *optarg != '0') {
8093 "%s quota: invalid MDT index '%s'\n",
8100 valid = qctl->qc_valid = QC_OSTIDX;
8101 idx = qctl->qc_idx = atoi(optarg);
8102 if (idx == 0 && *optarg != '0') {
8104 "%s quota: invalid OST index '%s'\n",
8117 human_readable = true;
8120 if (lfs_verify_poolarg(optarg)) {
8124 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8125 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
8126 LUSTRE_Q_GETINFOPOOL :
8127 LUSTRE_Q_GETQUOTAPOOL;
8130 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8131 progname, argv[optind - 1]);
8137 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8138 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8139 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8140 qctl->qc_type == ALLQUOTA &&
8141 optind == argc - 1 && !show_default) {
8144 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8145 qctl->qc_type = qtype;
8146 qctl->qc_valid = valid;
8147 if (qtype == USRQUOTA) {
8148 qctl->qc_id = geteuid();
8149 rc = uid2name(&name, qctl->qc_id);
8151 qctl->qc_id = getegid();
8152 rc = gid2name(&name, qctl->qc_id);
8153 memset(&qctl->qc_dqblk, 0,
8154 sizeof(qctl->qc_dqblk));
8159 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8160 human_readable, show_default);
8165 /* lfs quota -u username /path/to/lustre/mount */
8166 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8167 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8168 /* options should be followed by u/g-name and mntpoint */
8169 if ((!show_default && optind + 2 != argc) ||
8170 (show_default && optind + 1 != argc) ||
8171 qctl->qc_type == ALLQUOTA) {
8173 "%s quota: name and mount point must be specified\n",
8179 if (!show_default) {
8180 name = argv[optind++];
8181 switch (qctl->qc_type) {
8183 rc = name2uid(&qctl->qc_id, name);
8186 rc = name2gid(&qctl->qc_id, name);
8189 rc = name2projid(&qctl->qc_id, name);
8196 qctl->qc_valid = QC_GENERAL;
8197 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
8202 if (str2quotaid(&qctl->qc_id, name)) {
8203 fprintf(stderr, "%s quota: invalid id '%s'\n",
8209 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8210 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8217 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8218 human_readable, show_default);
8223 #endif /* HAVE_SYS_QUOTA_H! */
8225 static int flushctx_ioctl(char *mp)
8229 fd = open(mp, O_RDONLY);
8231 fprintf(stderr, "flushctx: error open %s: %s\n",
8232 mp, strerror(errno));
8236 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8238 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8239 mp, strerror(errno));
8245 static int lfs_flushctx(int argc, char **argv)
8247 int kdestroy = 0, reap = 0, c;
8248 char mntdir[PATH_MAX] = {'\0'};
8252 while ((c = getopt(argc, argv, "kr")) != -1) {
8262 "error: %s: option '-%c' unrecognized\n",
8269 rc = system("kdestroy > /dev/null");
8271 rc = WEXITSTATUS(rc);
8273 "error destroying tickets: %d, continuing\n",
8278 if (optind >= argc) {
8279 /* flush for all mounted lustre fs. */
8280 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8281 /* Check if we have a mount point */
8282 if (mntdir[0] == '\0')
8285 if (flushctx_ioctl(mntdir))
8288 mntdir[0] = '\0'; /* avoid matching in next loop */
8291 /* flush fs as specified */
8292 while (optind < argc) {
8293 if (flushctx_ioctl(argv[optind++]))
8299 rc = system("keyctl reap > /dev/null");
8301 rc = WEXITSTATUS(rc);
8302 fprintf(stderr, "error reaping keyring: %d\n", rc);
8309 static int lfs_changelog(int argc, char **argv)
8311 void *changelog_priv;
8312 struct changelog_rec *rec;
8313 long long startrec = 0, endrec = 0;
8315 struct option long_opts[] = {
8316 { .val = 'f', .name = "follow", .has_arg = no_argument },
8318 char short_opts[] = "f";
8321 while ((rc = getopt_long(argc, argv, short_opts,
8322 long_opts, NULL)) != -1) {
8329 "%s changelog: unrecognized option '%s'\n",
8330 progname, argv[optind - 1]);
8334 if (optind >= argc) {
8335 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8340 mdd = argv[optind++];
8341 if (argc > optind) {
8343 startrec = strtoll(argv[optind++], NULL, 10);
8344 if (errno != 0 || startrec < 0) {
8346 "%s changelog: bad startrec\n",
8352 if (argc > optind) {
8354 endrec = strtoll(argv[optind++], NULL, 10);
8355 if (errno != 0 || endrec < 0) {
8357 "%s changelog: bad endrec\n",
8363 rc = llapi_changelog_start(&changelog_priv,
8364 CHANGELOG_FLAG_BLOCK |
8365 CHANGELOG_FLAG_JOBID |
8366 CHANGELOG_FLAG_EXTRA_FLAGS |
8367 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8370 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8371 progname, strerror(errno = -rc));
8375 rc = llapi_changelog_set_xflags(changelog_priv,
8376 CHANGELOG_EXTRA_FLAG_UIDGID |
8377 CHANGELOG_EXTRA_FLAG_NID |
8378 CHANGELOG_EXTRA_FLAG_OMODE |
8379 CHANGELOG_EXTRA_FLAG_XATTR);
8382 "%s changelog: cannot set xflags for changelog: %s\n",
8383 progname, strerror(errno = -rc));
8387 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8391 if (endrec && rec->cr_index > endrec) {
8392 llapi_changelog_free(&rec);
8395 if (rec->cr_index < startrec) {
8396 llapi_changelog_free(&rec);
8400 secs = rec->cr_time >> 30;
8401 gmtime_r(&secs, &ts);
8402 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8403 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8404 changelog_type2str(rec->cr_type),
8405 ts.tm_hour, ts.tm_min, ts.tm_sec,
8406 (int)(rec->cr_time & ((1 << 30) - 1)),
8407 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8408 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8410 if (rec->cr_flags & CLF_JOBID) {
8411 struct changelog_ext_jobid *jid =
8412 changelog_rec_jobid(rec);
8414 if (jid->cr_jobid[0] != '\0')
8415 printf(" j=%s", jid->cr_jobid);
8418 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8419 struct changelog_ext_extra_flags *ef =
8420 changelog_rec_extra_flags(rec);
8422 printf(" ef=0x%llx",
8423 (unsigned long long)ef->cr_extra_flags);
8425 if (ef->cr_extra_flags & CLFE_UIDGID) {
8426 struct changelog_ext_uidgid *uidgid =
8427 changelog_rec_uidgid(rec);
8429 printf(" u=%llu:%llu",
8430 (unsigned long long)uidgid->cr_uid,
8431 (unsigned long long)uidgid->cr_gid);
8433 if (ef->cr_extra_flags & CLFE_NID) {
8434 struct changelog_ext_nid *nid =
8435 changelog_rec_nid(rec);
8438 libcfs_nid2str(nid->cr_nid));
8441 if (ef->cr_extra_flags & CLFE_OPEN) {
8442 struct changelog_ext_openmode *omd =
8443 changelog_rec_openmode(rec);
8444 char mode[] = "---";
8446 /* exec mode must be exclusive */
8447 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8450 if (omd->cr_openflags & MDS_FMODE_READ)
8452 if (omd->cr_openflags &
8459 if (strcmp(mode, "---") != 0)
8460 printf(" m=%s", mode);
8463 if (ef->cr_extra_flags & CLFE_XATTR) {
8464 struct changelog_ext_xattr *xattr =
8465 changelog_rec_xattr(rec);
8467 if (xattr->cr_xattr[0] != '\0')
8468 printf(" x=%s", xattr->cr_xattr);
8472 if (!fid_is_zero(&rec->cr_pfid))
8473 printf(" p="DFID, PFID(&rec->cr_pfid));
8474 if (rec->cr_namelen)
8475 printf(" %.*s", rec->cr_namelen,
8476 changelog_rec_name(rec));
8478 if (rec->cr_flags & CLF_RENAME) {
8479 struct changelog_ext_rename *rnm =
8480 changelog_rec_rename(rec);
8482 if (!fid_is_zero(&rnm->cr_sfid))
8483 printf(" s="DFID" sp="DFID" %.*s",
8484 PFID(&rnm->cr_sfid),
8485 PFID(&rnm->cr_spfid),
8486 (int)changelog_rec_snamelen(rec),
8487 changelog_rec_sname(rec));
8491 llapi_changelog_free(&rec);
8494 llapi_changelog_fini(&changelog_priv);
8497 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8498 progname, strerror(errno = -rc));
8500 return (rc == 1 ? 0 : rc);
8503 static int lfs_changelog_clear(int argc, char **argv)
8512 endrec = strtoll(argv[3], NULL, 10);
8513 if (errno != 0 || endrec < 0) {
8515 "%s: bad endrec '%s'\n",
8520 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8523 fprintf(stderr, "%s: record out of range: %llu\n",
8525 else if (rc == -ENOENT)
8526 fprintf(stderr, "%s: no changelog user: %s\n",
8529 fprintf(stderr, "%s error: %s\n", argv[0],
8538 static void rstripc(char *str, int c)
8540 char *end = str + strlen(str);
8542 for (; str < end && end[-1] == c; --end)
8546 static int lfs_fid2path(int argc, char **argv)
8548 struct option long_opts[] = {
8549 { .val = 'c', .name = "cur", .has_arg = no_argument },
8550 { .val = 'c', .name = "current", .has_arg = no_argument },
8551 { .val = 'c', .name = "print-link", .has_arg = no_argument },
8552 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
8553 { .val = 'l', .name = "link", .has_arg = required_argument },
8555 char short_opts[] = "cfl:pr:";
8556 bool print_link = false;
8557 bool print_fid = false;
8559 char mnt_dir[PATH_MAX] = "";
8561 char *path_or_fsname;
8562 long long recno = -1;
8564 char *endptr = NULL;
8569 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
8579 linkno = strtol(optarg, &endptr, 10);
8580 if (errno != 0 || *endptr != '\0' || linkno < 0) {
8582 "%s fid2path: invalid linkno '%s'\n",
8588 /* recno is something to do with changelogs
8589 * that was never implemented. We just pass it
8590 * through for the MDT to ignore.
8593 recno = strtoll(optarg, &endptr, 10);
8594 if (errno != 0 || *endptr != '\0' || recno < 0) {
8596 "%s fid2path: invalid recno '%s'\n",
8603 "%s fid2path: unrecognized option '%s'\n",
8604 progname, argv[optind - 1]);
8609 if (argc - optind < 2) {
8611 "Usage: %s fid2path FSNAME|ROOT FID...\n",
8616 path_or_fsname = argv[optind];
8618 if (*path_or_fsname == '/') {
8619 print_mnt_dir = true;
8620 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
8622 print_mnt_dir = false;
8623 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
8628 "%s fid2path: cannot resolve mount point for '%s': %s\n",
8629 progname, path_or_fsname, strerror(-rc));
8633 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
8636 "%s fid2path: cannot open mount point for '%s': %s\n",
8637 progname, path_or_fsname, strerror(-rc));
8641 /* Strip trailing slashes from mnt_dir. */
8642 rstripc(mnt_dir + 1, '/');
8644 for (i = optind + 1; i < argc; i++) {
8645 const char *fid_str = argv[i];
8649 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
8652 "%s fid2path: invalid FID '%s'\n",
8660 int linktmp = (linkno >= 0) ? linkno : 0;
8662 int oldtmp = linktmp;
8663 long long rectmp = recno;
8664 char path_buf[PATH_MAX];
8666 rc2 = llapi_fid2path_at(mnt_fd, &fid,
8667 path_buf, sizeof(path_buf), &rectmp, &linktmp);
8670 "%s fid2path: cannot find %s %s: %s\n",
8671 progname, path_or_fsname, fid_str,
8679 printf("%s ", fid_str);
8682 printf("%d ", linktmp);
8684 /* You may think this looks wrong or weird (and it is!)
8685 * but we are actually trying to preserve the old quirky
8686 * behaviors (enforced by our old quirky tests!) that
8687 * make lfs so much fun to work on:
8689 * lustre 0x200000007:0x1:0x0 => "/"
8690 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
8692 * Note that llapi_fid2path() returns "" for the root
8696 print_mnt_dir ? mnt_dir : "",
8697 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
8701 /* specified linkno */
8704 if (oldtmp == linktmp)
8716 static int lfs_path2fid(int argc, char **argv)
8718 struct option long_opts[] = {
8719 { .val = 'p', .name = "parents", .has_arg = no_argument },
8722 const char short_opts[] = "p";
8723 const char *sep = "";
8726 bool show_parents = false;
8728 while ((rc = getopt_long(argc, argv, short_opts,
8729 long_opts, NULL)) != -1) {
8732 show_parents = true;
8736 "%s path2fid: unrecognized option '%s'\n",
8737 progname, argv[optind - 1]);
8742 if (optind > argc - 1) {
8743 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8746 } else if (optind < argc - 1) {
8751 for (path = argv + optind; *path != NULL; path++) {
8754 if (!show_parents) {
8755 err = llapi_path2fid(*path, &fid);
8757 printf("%s%s"DFID"\n",
8758 *sep != '\0' ? *path : "", sep,
8761 char name[NAME_MAX + 1];
8762 unsigned int linkno = 0;
8764 while ((err = llapi_path2parent(*path, linkno, &fid,
8765 name, sizeof(name))) == 0) {
8766 if (*sep != '\0' && linkno == 0)
8767 printf("%s%s", *path, sep);
8769 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8774 /* err == -ENODATA is end-of-loop */
8775 if (linkno > 0 && err == -ENODATA) {
8783 "%s path2fid: cannot get %sfid for '%s': %s\n",
8784 progname, show_parents ? "parent " : "", *path,
8796 #define MAX_ERRNO 4095
8797 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8799 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8803 rc = llapi_rmfid(device, fa);
8805 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
8806 progname, strerror(-rc));
8810 for (k = 0; k < fa->fa_nr; k++) {
8811 rc2 = (__s32)fa->fa_fids[k].f_ver;
8812 if (!IS_ERR_VALUE(rc2))
8818 fa->fa_fids[k].f_ver = 0;
8819 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
8820 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
8826 static int lfs_rmfid(int argc, char **argv)
8828 char *fidstr, *device;
8829 int rc = 0, rc2, nr;
8830 struct fid_array *fa;
8832 if (optind > argc - 1) {
8833 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8837 device = argv[optind++];
8840 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8846 while (optind < argc) {
8849 fidstr = argv[optind++];
8850 while (*fidstr == '[')
8852 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8854 fprintf(stderr, "unrecognized FID: %s\n",
8859 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8860 /* start another batch */
8861 rc2 = lfs_rmfid_and_show_errors(device, fa);
8868 rc2 = lfs_rmfid_and_show_errors(device, fa);
8876 static int lfs_data_version(int argc, char **argv)
8883 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8886 fprintf(stderr, "%s data_version: FILE must be specified\n",
8891 while ((c = getopt(argc, argv, "nrw")) != -1) {
8894 data_version_flags = 0;
8897 data_version_flags |= LL_DV_RD_FLUSH;
8900 data_version_flags |= LL_DV_WR_FLUSH;
8904 "%s data_version: unrecognized option '%s'\n",
8905 progname, argv[optind - 1]);
8909 if (optind == argc) {
8910 fprintf(stderr, "%s data_version: FILE must be specified\n",
8915 path = argv[optind];
8916 fd = open(path, O_RDONLY);
8919 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8920 progname, path, strerror(-rc));
8924 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8927 "%s data_version: cannot get version for '%s': %s\n",
8928 progname, path, strerror(-rc));
8930 printf("%ju" "\n", (uintmax_t)data_version);
8936 static int lfs_hsm_state(int argc, char **argv)
8941 struct hsm_user_state hus;
8949 rc = llapi_hsm_state_get(path, &hus);
8951 fprintf(stderr, "can't get hsm state for %s: %s\n",
8952 path, strerror(errno = -rc));
8956 /* Display path name and status flags */
8957 printf("%s: (0x%08x)", path, hus.hus_states);
8959 if (hus.hus_states & HS_RELEASED)
8960 printf(" released");
8961 if (hus.hus_states & HS_EXISTS)
8963 if (hus.hus_states & HS_DIRTY)
8965 if (hus.hus_states & HS_ARCHIVED)
8966 printf(" archived");
8967 /* Display user-settable flags */
8968 if (hus.hus_states & HS_NORELEASE)
8969 printf(" never_release");
8970 if (hus.hus_states & HS_NOARCHIVE)
8971 printf(" never_archive");
8972 if (hus.hus_states & HS_LOST)
8973 printf(" lost_from_hsm");
8975 if (hus.hus_archive_id != 0)
8976 printf(", archive_id:%d", hus.hus_archive_id);
8979 } while (++i < argc);
8984 #define LFS_HSM_SET 0
8985 #define LFS_HSM_CLEAR 1
8988 * Generic function to set or clear HSM flags.
8989 * Used by hsm_set and hsm_clear.
8991 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8993 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8995 struct option long_opts[] = {
8996 { .val = 'A', .name = "archived", .has_arg = no_argument },
8997 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8998 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8999 { .val = 'e', .name = "exists", .has_arg = no_argument },
9000 { .val = 'l', .name = "lost", .has_arg = no_argument },
9001 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9002 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9004 char short_opts[] = "lraAdei:";
9008 __u32 archive_id = 0;
9014 while ((c = getopt_long(argc, argv, short_opts,
9015 long_opts, NULL)) != -1) {
9021 mask |= HS_NOARCHIVE;
9024 mask |= HS_ARCHIVED;
9027 mask |= HS_NORELEASE;
9037 archive_id = strtol(optarg, &end, 10);
9038 if (errno != 0 || *end != '\0' || archive_id < 0) {
9039 fprintf(stderr, "invalid archive_id: '%s'\n",
9047 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9048 argv[0], argv[optind - 1]);
9053 /* User should have specified a flag */
9057 while (optind < argc) {
9058 path = argv[optind];
9060 /* If mode == 0, this means we apply the mask. */
9061 if (mode == LFS_HSM_SET)
9062 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9064 rc = llapi_hsm_state_set(path, 0, mask, 0);
9067 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9068 path, strerror(errno = -rc));
9077 static int lfs_hsm_action(int argc, char **argv)
9082 struct hsm_current_action hca;
9083 struct hsm_extent he;
9084 enum hsm_user_action hua;
9085 enum hsm_progress_states hps;
9093 rc = llapi_hsm_current_action(path, &hca);
9095 fprintf(stderr, "can't get hsm action for %s: %s\n",
9096 path, strerror(errno = -rc));
9099 he = hca.hca_location;
9100 hua = hca.hca_action;
9101 hps = hca.hca_state;
9103 printf("%s: %s", path, hsm_user_action2name(hua));
9105 /* Skip file without action */
9106 if (hca.hca_action == HUA_NONE) {
9111 printf(" %s ", hsm_progress_state2name(hps));
9113 if ((hps == HPS_RUNNING) &&
9114 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9115 printf("(%llu bytes moved)\n",
9116 (unsigned long long)he.length);
9117 else if ((he.offset + he.length) == LUSTRE_EOF)
9118 printf("(from %llu to EOF)\n",
9119 (unsigned long long)he.offset);
9121 printf("(from %llu to %llu)\n",
9122 (unsigned long long)he.offset,
9123 (unsigned long long)(he.offset + he.length));
9125 } while (++i < argc);
9130 static int lfs_hsm_set(int argc, char **argv)
9132 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9135 static int lfs_hsm_clear(int argc, char **argv)
9137 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9141 * Check file state and return its fid, to be used by lfs_hsm_request().
9143 * \param[in] file Path to file to check
9144 * \param[in,out] fid Pointer to allocated lu_fid struct.
9145 * \param[in,out] last_dev Pointer to last device id used.
9147 * \return 0 on success.
9149 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9155 rc = lstat(file, &st);
9157 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9161 * Checking for regular file as archiving as posix copytool
9162 * rejects archiving files other than regular files
9164 if (!S_ISREG(st.st_mode)) {
9165 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9168 /* A request should be ... */
9169 if (*last_dev != st.st_dev && *last_dev != 0) {
9171 "All files should be on the same filesystem: %s\n",
9175 *last_dev = st.st_dev;
9177 rc = llapi_path2fid(file, fid);
9179 fprintf(stderr, "Cannot read FID of %s: %s\n",
9180 file, strerror(-rc));
9186 /* Fill an HSM HUR item with a given file name.
9188 * If mntpath is set, then the filename is actually a FID, and no
9189 * lookup on the filesystem will be performed.
9191 * \param[in] hur the user request to fill
9192 * \param[in] idx index of the item inside the HUR to fill
9193 * \param[in] mntpath mountpoint of Lustre
9194 * \param[in] fname filename (if mtnpath is NULL)
9195 * or FID (if mntpath is set)
9196 * \param[in] last_dev pointer to last device id used
9198 * \retval 0 on success
9199 * \retval CMD_HELP or a negative errno on error
9201 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9202 const char *mntpath, const char *fname,
9205 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9208 hui->hui_extent.length = -1;
9211 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9213 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9216 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9220 hur->hur_request.hr_itemcount++;
9225 static int lfs_hsm_request(int argc, char **argv, int action)
9227 struct option long_opts[] = {
9228 { .val = 'a', .name = "archive", .has_arg = required_argument },
9229 { .val = 'D', .name = "data", .has_arg = required_argument },
9230 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9231 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9234 char short_opts[] = "l:D:a:m:";
9235 struct hsm_user_request *hur, *oldhur;
9240 char *filelist = NULL;
9241 char fullpath[PATH_MAX];
9242 char *opaque = NULL;
9246 int nbfile_alloc = 0;
9247 char *some_file = NULL;
9248 char *mntpath = NULL;
9254 while ((c = getopt_long(argc, argv, short_opts,
9255 long_opts, NULL)) != -1) {
9264 if (action != HUA_ARCHIVE &&
9265 action != HUA_REMOVE) {
9267 "error: -a is supported only when archiving or removing\n");
9270 archive_id = atoi(optarg);
9275 some_file = strdup(optarg);
9281 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9282 argv[0], argv[optind - 1]);
9287 /* All remaining args are files, so we have at least nbfile */
9288 nbfile = argc - optind;
9290 if ((nbfile == 0) && (!filelist))
9294 opaque_len = strlen(opaque);
9297 * Alloc the request structure with enough place to store all files
9298 * from command line.
9300 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9302 fprintf(stderr, "Cannot create the request: %s\n",
9306 nbfile_alloc = nbfile;
9308 hur->hur_request.hr_action = action;
9309 hur->hur_request.hr_archive_id = archive_id;
9310 hur->hur_request.hr_flags = 0;
9312 /* All remaining args are files, add them */
9313 if (nbfile != 0 && some_file == NULL)
9314 some_file = strdup(argv[optind]);
9316 for (i = 0; i < nbfile; i++) {
9317 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9323 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9325 /* If a filelist was specified, read the filelist from it. */
9327 fp = fopen(filelist, "r");
9329 fprintf(stderr, "Cannot read the file list %s: %s\n",
9330 filelist, strerror(errno));
9335 while ((rc = getline(&line, &len, fp)) != -1) {
9337 * If allocated buffer was too small, get something
9340 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9343 nbfile_alloc = nbfile_alloc * 2 + 1;
9345 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9349 "hsm: cannot allocate the request: %s\n",
9356 size = hur_len(oldhur);
9359 "hsm: cannot allocate %u files + %u bytes data\n",
9360 oldhur->hur_request.hr_itemcount,
9361 oldhur->hur_request.hr_data_len);
9368 memcpy(hur, oldhur, size);
9373 if (line[strlen(line) - 1] == '\n')
9374 line[strlen(line) - 1] = '\0';
9376 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9377 mntpath, line, &last_dev);
9393 /* If a --data was used, add it to the request */
9394 hur->hur_request.hr_data_len = opaque_len;
9396 memcpy(hur_data(hur), opaque, opaque_len);
9398 /* Send the HSM request */
9399 if (realpath(some_file, fullpath) == NULL) {
9400 fprintf(stderr, "Could not find path '%s': %s\n",
9401 some_file, strerror(errno));
9403 rc = llapi_hsm_request(fullpath, hur);
9405 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9406 some_file, strerror(-rc));
9416 static int lfs_hsm_archive(int argc, char **argv)
9418 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9421 static int lfs_hsm_restore(int argc, char **argv)
9423 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9426 static int lfs_hsm_release(int argc, char **argv)
9428 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9431 static int lfs_hsm_remove(int argc, char **argv)
9433 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9436 static int lfs_hsm_cancel(int argc, char **argv)
9438 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9441 static int lfs_swap_layouts(int argc, char **argv)
9446 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9447 SWAP_LAYOUTS_KEEP_MTIME |
9448 SWAP_LAYOUTS_KEEP_ATIME);
9451 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9453 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9455 int lfs_get_mode(const char *string)
9457 enum lock_mode_user mode;
9459 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9460 if (lock_mode_names[mode] == NULL)
9462 if (strcmp(string, lock_mode_names[mode]) == 0)
9469 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9471 enum lu_ladvise_type advice;
9474 advice < ARRAY_SIZE(ladvise_names); advice++) {
9475 if (ladvise_names[advice] == NULL)
9477 if (strcmp(string, ladvise_names[advice]) == 0)
9481 return LU_LADVISE_INVALID;
9484 static int lfs_ladvise(int argc, char **argv)
9486 struct option long_opts[] = {
9487 { .val = 'a', .name = "advice", .has_arg = required_argument },
9488 { .val = 'b', .name = "background", .has_arg = no_argument },
9489 { .val = 'e', .name = "end", .has_arg = required_argument },
9490 { .val = 'l', .name = "length", .has_arg = required_argument },
9491 { .val = 'm', .name = "mode", .has_arg = required_argument },
9492 { .val = 's', .name = "start", .has_arg = required_argument },
9493 { .val = 'u', .name = "unset", .has_arg = no_argument },
9495 char short_opts[] = "a:be:l:m:s:u";
9500 struct llapi_lu_ladvise advice;
9501 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9502 unsigned long long start = 0;
9503 unsigned long long end = LUSTRE_EOF;
9504 unsigned long long length = 0;
9505 unsigned long long size_units;
9506 unsigned long long flags = 0;
9510 while ((c = getopt_long(argc, argv, short_opts,
9511 long_opts, NULL)) != -1) {
9514 advice_type = lfs_get_ladvice(optarg);
9515 if (advice_type == LU_LADVISE_INVALID) {
9517 "%s: invalid advice type '%s'\n",
9519 fprintf(stderr, "Valid types:");
9521 for (advice_type = 0;
9522 advice_type < ARRAY_SIZE(ladvise_names);
9524 if (ladvise_names[advice_type] == NULL)
9526 fprintf(stderr, " %s",
9527 ladvise_names[advice_type]);
9529 fprintf(stderr, "\n");
9542 rc = llapi_parse_size(optarg, &end,
9545 fprintf(stderr, "%s: bad end offset '%s'\n",
9552 rc = llapi_parse_size(optarg, &start,
9556 "%s: bad start offset '%s'\n",
9563 rc = llapi_parse_size(optarg, &length,
9566 fprintf(stderr, "%s: bad length '%s'\n",
9572 mode = lfs_get_mode(optarg);
9575 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9583 fprintf(stderr, "%s: option '%s' unrecognized\n",
9584 argv[0], argv[optind - 1]);
9589 if (advice_type == LU_LADVISE_INVALID) {
9590 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9591 fprintf(stderr, "Valid types:");
9592 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9594 if (ladvise_names[advice_type] == NULL)
9596 fprintf(stderr, " %s", ladvise_names[advice_type]);
9598 fprintf(stderr, "\n");
9602 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9604 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9609 if (argc <= optind) {
9610 fprintf(stderr, "%s: please give one or more file names\n",
9615 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9616 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9621 if (end == LUSTRE_EOF && length != 0)
9622 end = start + length;
9625 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9626 argv[0], start, end);
9630 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9631 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9636 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9637 fprintf(stderr, "%s: mode is required with lockahead\n",
9642 while (optind < argc) {
9645 path = argv[optind++];
9647 fd = open(path, O_RDONLY);
9649 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9650 argv[0], path, strerror(errno));
9655 advice.lla_start = start;
9656 advice.lla_end = end;
9657 advice.lla_advice = advice_type;
9658 advice.lla_value1 = 0;
9659 advice.lla_value2 = 0;
9660 advice.lla_value3 = 0;
9661 advice.lla_value4 = 0;
9662 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9663 advice.lla_lockahead_mode = mode;
9664 advice.lla_peradvice_flags = flags;
9667 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9671 "%s: cannot give advice '%s' to file '%s': %s\n",
9672 argv[0], ladvise_names[advice_type],
9673 path, strerror(errno));
9679 if (rc == 0 && rc2 < 0)
9685 static const char *const heat_names[] = LU_HEAT_NAMES;
9687 static int lfs_heat_get(int argc, char **argv)
9689 struct lu_heat *heat;
9698 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9700 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9705 while (optind < argc) {
9706 path = argv[optind++];
9708 fd = open(path, O_RDONLY);
9710 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9711 argv[0], path, strerror(errno));
9716 heat->lh_count = OBD_HEAT_COUNT;
9717 rc2 = llapi_heat_get(fd, heat);
9721 "%s: cannot get heat of file '%s': %s\n",
9722 argv[0], path, strerror(errno));
9726 printf("flags: %x\n", heat->lh_flags);
9727 for (i = 0; i < heat->lh_count; i++)
9728 printf("%s: %llu\n", heat_names[i],
9729 (unsigned long long)heat->lh_heat[i]);
9731 if (rc == 0 && rc2 < 0)
9739 static int lfs_heat_set(int argc, char **argv)
9741 struct option long_opts[] = {
9742 { .val = 'c', .name = "clear", .has_arg = no_argument },
9743 { .val = 'o', .name = "off", .has_arg = no_argument },
9744 { .val = 'O', .name = "on", .has_arg = no_argument },
9746 enum lu_heat_flag flags = 0;
9756 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9759 flags |= LU_HEAT_FLAG_CLEAR;
9762 flags |= LU_HEAT_FLAG_CLEAR;
9763 flags |= LU_HEAT_FLAG_OFF;
9766 flags &= ~LU_HEAT_FLAG_OFF;
9771 fprintf(stderr, "%s: option '%s' unrecognized\n",
9772 argv[0], argv[optind - 1]);
9777 if (argc <= optind) {
9778 fprintf(stderr, "%s: please give one or more file names\n",
9783 while (optind < argc) {
9784 path = argv[optind++];
9786 fd = open(path, O_RDONLY);
9788 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9789 argv[0], path, strerror(errno));
9794 rc2 = llapi_heat_set(fd, flags);
9798 "%s: cannot setflags heat of file '%s': %s\n",
9799 argv[0], path, strerror(errno));
9803 if (rc == 0 && rc2 < 0)
9810 * The input string contains a comma delimited list of component ids and
9811 * ranges, for example "1,2-4,7".
9813 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9815 bool end_of_loop = false;
9823 while (!end_of_loop) {
9827 char *endptr = NULL;
9830 ptr = strchrnul(arg, ',');
9831 end_of_loop = *ptr == '\0';
9834 start_index = strtol(arg, &endptr, 0);
9835 if (endptr == arg) /* no data at all */
9837 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9839 if (start_index < 0)
9842 end_index = start_index;
9843 if (*endptr == '-') {
9844 end_index = strtol(endptr + 1, &endptr, 0);
9845 if (*endptr != '\0')
9847 if (end_index < start_index)
9851 for (i = start_index; i <= end_index && size > 0; i++) {
9854 /* remove duplicate */
9855 for (j = 0; j < nr; j++) {
9859 if (j == nr) { /* no duplicate */
9865 if (size == 0 && i < end_index)
9872 if (!end_of_loop && ptr)
9875 return rc < 0 ? rc : nr;
9879 * struct verify_mirror_id - Mirror id to be verified.
9880 * @mirror_id: A specified mirror id.
9881 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9883 struct verify_mirror_id {
9889 * compare_mirror_ids() - Compare mirror ids.
9890 * @layout: Mirror component list.
9891 * @cbdata: Callback data in verify_mirror_id structure.
9893 * This is a callback function called by llapi_layout_comp_iterate()
9894 * to compare the specified mirror id with the one in the current
9895 * component of @layout. If they are the same, then the specified
9896 * mirror id is valid.
9898 * Return: a negative error code on failure or
9899 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9900 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9903 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9905 struct verify_mirror_id *mirror_id_cbdata =
9906 (struct verify_mirror_id *)cbdata;
9910 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9914 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9915 progname, strerror(errno));
9919 if (mirror_id_cbdata->mirror_id == mirror_id) {
9920 mirror_id_cbdata->is_valid_id = true;
9921 return LLAPI_LAYOUT_ITER_STOP;
9924 return LLAPI_LAYOUT_ITER_CONT;
9928 * verify_mirror_ids() - Verify specified mirror ids.
9929 * @fname: Mirrored file name.
9930 * @mirror_ids: Specified mirror ids to be verified.
9931 * @ids_nr: Number of specified mirror ids.
9933 * This function verifies that specified @mirror_ids are valid
9934 * in the mirrored file @fname.
9936 * Return: 0 on success or a negative error code on failure.
9939 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9941 struct llapi_layout *layout = NULL;
9942 struct verify_mirror_id mirror_id_cbdata = { 0 };
9953 if (stat(fname, &stbuf) < 0) {
9954 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9955 progname, fname, strerror(errno));
9960 if (!S_ISREG(stbuf.st_mode)) {
9961 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9967 fd = open(fname, O_DIRECT | O_RDONLY);
9969 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9970 progname, fname, strerror(errno));
9975 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9977 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9978 progname, fname, strerror(errno));
9982 layout = llapi_layout_get_by_fd(fd, 0);
9984 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9985 progname, fname, strerror(errno));
9987 llapi_lease_release(fd);
9991 rc = llapi_layout_flags_get(layout, &flr_state);
9993 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9994 progname, fname, strerror(errno));
9999 flr_state &= LCM_FL_FLR_MASK;
10000 switch (flr_state) {
10003 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10004 progname, fname, llapi_layout_flags_string(flr_state));
10011 for (i = 0; i < ids_nr; i++) {
10012 mirror_id_cbdata.mirror_id = mirror_ids[i];
10013 mirror_id_cbdata.is_valid_id = false;
10015 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10016 &mirror_id_cbdata);
10020 "%s: '%s' failed to verify mirror id: %u.\n",
10021 progname, fname, mirror_ids[i]);
10025 if (!mirror_id_cbdata.is_valid_id) {
10028 "%s: '%s' invalid specified mirror id: %u.\n",
10029 progname, fname, mirror_ids[i]);
10035 llapi_layout_free(layout);
10036 llapi_lease_release(fd);
10044 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10045 __u16 *mirror_ids, int ids_nr)
10047 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10048 struct llapi_layout *layout;
10050 uint32_t flr_state;
10059 if (stat(fname, &stbuf) < 0) {
10060 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10061 progname, fname, strerror(errno));
10065 if (!S_ISREG(stbuf.st_mode)) {
10066 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10072 fd = open(fname, O_DIRECT | O_RDWR);
10074 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10075 progname, fname, strerror(errno));
10080 layout = llapi_layout_get_by_fd(fd, 0);
10082 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10083 progname, fname, strerror(errno));
10088 rc = llapi_layout_flags_get(layout, &flr_state);
10090 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10091 progname, fname, strerror(errno));
10096 flr_state &= LCM_FL_FLR_MASK;
10097 if (flr_state == LCM_FL_NONE) {
10099 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10104 /* get stale component info */
10105 comp_size = llapi_mirror_find_stale(layout, comp_array,
10106 ARRAY_SIZE(comp_array),
10107 mirror_ids, ids_nr);
10108 if (comp_size <= 0) {
10113 ioc->lil_mode = LL_LEASE_WRLCK;
10114 ioc->lil_flags = LL_LEASE_RESYNC;
10115 rc = llapi_lease_set(fd, ioc);
10117 if (rc == -EALREADY)
10121 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10122 progname, fname, strerror(-rc));
10126 /* get the read range [start, end) */
10127 start = comp_array[0].lrc_start;
10128 end = comp_array[0].lrc_end;
10129 for (idx = 1; idx < comp_size; idx++) {
10130 if (comp_array[idx].lrc_start < start)
10131 start = comp_array[idx].lrc_start;
10132 if (end < comp_array[idx].lrc_end)
10133 end = comp_array[idx].lrc_end;
10136 rc = llapi_lease_check(fd);
10137 if (rc != LL_LEASE_WRLCK) {
10138 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10143 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10146 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10147 progname, fname, strerror(-rc));
10149 /* need to do the lease unlock even resync fails */
10150 ioc->lil_mode = LL_LEASE_UNLCK;
10151 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10152 ioc->lil_count = 0;
10153 for (idx = 0; idx < comp_size; idx++) {
10154 if (comp_array[idx].lrc_synced) {
10155 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10160 rc2 = llapi_lease_set(fd, ioc);
10162 * llapi_lease_set returns lease mode when it request to unlock
10166 /* rc2 == 0 means lost lease lock */
10167 if (rc2 == 0 && rc == 0)
10169 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10171 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10175 llapi_layout_free(layout);
10182 static inline int lfs_mirror_resync(int argc, char **argv)
10184 struct ll_ioc_lease *ioc = NULL;
10185 __u16 mirror_ids[128] = { 0 };
10190 struct option long_opts[] = {
10191 { .val = 'o', .name = "only", .has_arg = required_argument },
10192 { .name = NULL } };
10194 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10197 rc = parse_mirror_ids(mirror_ids,
10198 sizeof(mirror_ids) / sizeof(__u16),
10202 "%s: bad mirror ids '%s'.\n",
10209 fprintf(stderr, "%s: options '%s' unrecognized.\n",
10210 argv[0], argv[optind - 1]);
10216 if (argc == optind) {
10217 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10222 if (ids_nr > 0 && argc > optind + 1) {
10224 "%s: option '--only' cannot be used upon multiple files.\n",
10231 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10236 /* set the lease on the file */
10237 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10239 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10240 argv[0], strerror(errno));
10245 for (; optind < argc; optind++) {
10246 rc = lfs_mirror_resync_file(argv[optind], ioc,
10247 mirror_ids, ids_nr);
10248 /* ignore previous file's error, continue with next file */
10251 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10259 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10261 struct llapi_layout *layout;
10264 layout = llapi_layout_get_by_fd(fd, 0);
10266 fprintf(stderr, "could not get layout.\n");
10270 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10272 fprintf(stderr, "failed to iterate layout\n");
10273 llapi_layout_free(layout);
10276 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10277 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10278 llapi_layout_free(layout);
10282 llapi_layout_free(layout);
10288 * Check whether two files are the same file
10289 * \retval 0 same file
10290 * \retval 1 not the same file
10291 * \retval <0 error code
10293 static inline int check_same_file(int fd, const char *f2)
10295 struct stat stbuf1;
10296 struct stat stbuf2;
10298 if (fstat(fd, &stbuf1) < 0)
10301 if (stat(f2, &stbuf2) < 0)
10304 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10305 stbuf1.st_ino == stbuf2.st_ino)
10311 static inline int lfs_mirror_read(int argc, char **argv)
10314 __u16 mirror_id = 0;
10315 const char *outfile = NULL;
10321 const size_t buflen = 4 << 20;
10323 struct option long_opts[] = {
10324 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10325 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10326 { .name = NULL } };
10328 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10333 unsigned long int id;
10336 id = strtoul(optarg, &end, 0);
10337 if (errno != 0 || *end != '\0' || id == 0 ||
10340 "%s %s: invalid mirror ID '%s'\n",
10341 progname, argv[0], optarg);
10345 mirror_id = (__u16)id;
10352 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10353 progname, argv[optind - 1]);
10358 if (argc == optind) {
10359 fprintf(stderr, "%s %s: no mirrored file provided\n",
10360 progname, argv[0]);
10362 } else if (argc > optind + 1) {
10363 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10367 if (mirror_id == 0) {
10368 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10369 progname, argv[0]);
10373 /* open mirror file */
10374 fname = argv[optind];
10375 fd = open(fname, O_DIRECT | O_RDONLY);
10377 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10378 progname, argv[0], fname, strerror(errno));
10382 /* verify mirror id */
10383 rc = verify_mirror_id_by_fd(fd, mirror_id);
10386 "%s %s: cannot find mirror with ID %u in '%s'\n",
10387 progname, argv[0], mirror_id, fname);
10391 /* open output file - O_EXCL ensures output is not the same as input */
10393 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10395 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10396 progname, argv[0], outfile, strerror(errno));
10401 outfd = STDOUT_FILENO;
10404 /* allocate buffer */
10405 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10407 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10408 progname, argv[0], rc);
10414 ssize_t bytes_read;
10415 ssize_t written = 0;
10417 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10418 if (bytes_read < 0) {
10421 "%s %s: fail to read data from mirror %u: %s\n",
10422 progname, argv[0], mirror_id, strerror(-rc));
10427 if (bytes_read == 0)
10430 while (written < bytes_read) {
10433 written2 = write(outfd, buf + written,
10434 bytes_read - written);
10435 if (written2 < 0) {
10437 "%s %s: fail to write %s: %s\n",
10438 progname, argv[0], outfile ? : "STDOUT",
10443 written += written2;
10446 if (written != bytes_read) {
10448 "%s %s: written %ld bytes does not match with %ld read.\n",
10449 progname, argv[0], written, bytes_read);
10471 static inline int lfs_mirror_write(int argc, char **argv)
10474 __u16 mirror_id = 0;
10475 const char *inputfile = NULL;
10481 const size_t buflen = 4 << 20;
10483 size_t page_size = sysconf(_SC_PAGESIZE);
10484 struct ll_ioc_lease_id ioc;
10486 struct option long_opts[] = {
10487 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10488 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10489 { .name = NULL } };
10491 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10496 unsigned long int id;
10499 id = strtoul(optarg, &end, 0);
10500 if (errno != 0 || *end != '\0' || id == 0 ||
10503 "%s %s: invalid mirror ID '%s'\n",
10504 progname, argv[0], optarg);
10508 mirror_id = (__u16)id;
10512 inputfile = optarg;
10515 fprintf(stderr, "%s: option '%s' unrecognized\n",
10516 progname, argv[optind - 1]);
10521 if (argc == optind) {
10522 fprintf(stderr, "%s %s: no mirrored file provided\n",
10523 progname, argv[0]);
10525 } else if (argc > optind + 1) {
10526 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10530 if (mirror_id == 0) {
10531 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10532 progname, argv[0]);
10536 /* open mirror file */
10537 fname = argv[optind];
10538 fd = open(fname, O_DIRECT | O_WRONLY);
10540 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10541 progname, argv[0], fname, strerror(errno));
10545 /* verify mirror id */
10546 rc = verify_mirror_id_by_fd(fd, mirror_id);
10549 "%s %s: cannot find mirror with ID %u in '%s'\n",
10550 progname, argv[0], mirror_id, fname);
10554 /* open input file */
10556 rc = check_same_file(fd, inputfile);
10559 "%s %s: input file cannot be the mirrored file\n",
10560 progname, argv[0]);
10566 inputfd = open(inputfile, O_RDONLY, 0644);
10568 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10569 progname, argv[0], inputfile, strerror(errno));
10574 inputfd = STDIN_FILENO;
10577 /* allocate buffer */
10578 rc = posix_memalign(&buf, page_size, buflen);
10580 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10581 progname, argv[0], rc);
10582 goto close_inputfd;
10585 /* prepare target mirror components instantiation */
10586 ioc.lil_mode = LL_LEASE_WRLCK;
10587 ioc.lil_flags = LL_LEASE_RESYNC;
10588 ioc.lil_mirror_id = mirror_id;
10589 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10592 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10593 progname, argv[0], fname, strerror(errno));
10599 ssize_t bytes_read;
10603 rc = llapi_lease_check(fd);
10604 if (rc != LL_LEASE_WRLCK) {
10605 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10606 progname, argv[0], fname);
10610 bytes_read = read(inputfd, buf, buflen);
10611 if (bytes_read < 0) {
10614 "%s %s: fail to read data from '%s': %s\n",
10615 progname, argv[0], inputfile ? : "STDIN",
10622 if (bytes_read == 0)
10625 /* round up to page align to make direct IO happy. */
10626 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10628 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10633 "%s %s: fail to write to mirror %u: %s\n",
10634 progname, argv[0], mirror_id,
10642 if (pos & (page_size - 1)) {
10643 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10648 ioc.lil_mode = LL_LEASE_UNLCK;
10649 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10651 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10656 "%s %s: release lease lock of '%s' failed: %s\n",
10657 progname, argv[0], fname, strerror(errno));
10674 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10676 struct llapi_layout *layout;
10677 struct collect_ids_data cid = { .cid_ids = ids,
10679 .cid_exclude = exclude_id, };
10682 layout = llapi_layout_get_by_fd(fd, 0);
10684 fprintf(stderr, "could not get layout\n");
10688 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10690 fprintf(stderr, "failed to iterate layout\n");
10691 llapi_layout_free(layout);
10695 llapi_layout_free(layout);
10697 return cid.cid_count;
10700 static inline int lfs_mirror_copy(int argc, char **argv)
10703 __u16 read_mirror_id = 0;
10704 __u16 ids[128] = { 0 };
10706 struct llapi_layout *layout = NULL;
10707 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10714 struct ll_ioc_lease *ioc = NULL;
10715 struct ll_ioc_lease_id *resync_ioc;
10717 struct option long_opts[] = {
10718 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10719 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10720 { .name = NULL } };
10722 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10727 unsigned long int id;
10730 id = strtoul(optarg, &end, 0);
10731 if (errno != 0 || *end != '\0' || id == 0 ||
10734 "%s %s: invalid read mirror ID '%s'\n",
10735 progname, argv[0], optarg);
10739 read_mirror_id = (__u16)id;
10743 if (!strcmp(optarg, "-1")) {
10744 /* specify all other mirrors */
10745 ids[0] = (__u16)-1;
10748 count = parse_mirror_ids((__u16 *)ids,
10756 fprintf(stderr, "%s: option '%s' unrecognized\n",
10757 progname, argv[optind - 1]);
10762 if (argc == optind) {
10763 fprintf(stderr, "%s %s: no mirrored file provided\n",
10764 progname, argv[0]);
10766 } else if (argc > optind + 1) {
10767 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10771 if (read_mirror_id == 0) {
10773 "%s %s: no valid read mirror ID %d is provided\n",
10774 progname, argv[0], read_mirror_id);
10780 "%s %s: no write mirror ID is provided\n",
10781 progname, argv[0]);
10785 for (i = 0; i < count; i++) {
10786 if (read_mirror_id == ids[i]) {
10788 "%s %s: read and write mirror ID cannot be the same\n",
10789 progname, argv[0]);
10794 /* open mirror file */
10795 fname = argv[optind];
10797 fd = open(fname, O_DIRECT | O_RDWR);
10799 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10800 progname, argv[0], fname, strerror(errno));
10804 /* write to all other mirrors */
10805 if (ids[0] == (__u16)-1) {
10806 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10810 "%s %s: failed to get other mirror ids in '%s': %d\n",
10811 progname, argv[0], fname, rc);
10816 /* verify mirror id */
10817 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10820 "%s %s: cannot find mirror with ID %u in '%s'\n",
10821 progname, argv[0], read_mirror_id, fname);
10825 for (i = 0; i < count; i++) {
10826 rc = verify_mirror_id_by_fd(fd, ids[i]);
10829 "%s %s: cannot find mirror with ID %u in '%s'\n",
10830 progname, argv[0], ids[i], fname);
10835 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10838 "%s %s: cannot alloc comp id array for ioc: %s\n",
10839 progname, argv[0], strerror(errno));
10844 /* get stale component info */
10845 layout = llapi_layout_get_by_fd(fd, 0);
10847 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10848 progname, argv[0], fname, strerror(errno));
10852 comp_size = llapi_mirror_find_stale(layout, comp_array,
10853 ARRAY_SIZE(comp_array),
10855 llapi_layout_free(layout);
10856 if (comp_size < 0) {
10861 /* prepare target mirror components instantiation */
10862 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10863 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10864 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10866 resync_ioc->lil_mirror_id = ids[0];
10868 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10869 rc = llapi_lease_set(fd, ioc);
10872 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10873 progname, argv[0], fname, strerror(errno));
10877 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10880 fprintf(stderr, "%s %s: copy error: %d\n",
10881 progname, argv[0], rc);
10885 fprintf(stdout, "mirror copied successfully: ");
10886 for (i = 0; i < copied; i++)
10887 fprintf(stdout, "%d ", ids[i]);
10888 fprintf(stdout, "\n");
10890 ioc->lil_mode = LL_LEASE_UNLCK;
10891 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10892 ioc->lil_count = 0;
10893 for (i = 0; i < comp_size; i++) {
10896 for (j = 0; j < copied; j++) {
10897 if (comp_array[i].lrc_mirror_id != ids[j])
10900 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10904 rc = llapi_lease_set(fd, ioc);
10909 "%s %s: release lease lock of '%s' failed: %s\n",
10910 progname, argv[0], fname, strerror(errno));
10925 * struct verify_chunk - Mirror chunk to be verified.
10926 * @chunk: [start, end) of the chunk.
10927 * @mirror_count: Number of mirror ids in @mirror_id array.
10928 * @mirror_id: Array of valid mirror ids that cover the chunk.
10930 struct verify_chunk {
10931 struct lu_extent chunk;
10932 unsigned int mirror_count;
10933 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10937 * print_chunks() - Print chunk information.
10938 * @fname: Mirrored file name.
10939 * @chunks: Array of chunks.
10940 * @chunk_count: Number of chunks in @chunks array.
10942 * This function prints [start, end) of each chunk in @chunks
10943 * for mirrored file @fname, and also prints the valid mirror ids
10944 * that cover the chunk.
10949 void print_chunks(const char *fname, struct verify_chunk *chunks,
10955 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10956 for (i = 0; i < chunk_count; i++) {
10957 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10959 if (chunks[i].mirror_count == 0)
10960 fprintf(stdout, "\t[");
10962 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10963 for (j = 1; j < chunks[i].mirror_count; j++)
10964 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10966 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10968 fprintf(stdout, "\n");
10972 * print_checksums() - Print CRC-32 checksum values.
10973 * @chunk: A chunk and its corresponding valid mirror ids.
10974 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10976 * This function prints CRC-32 checksum values on @chunk for
10977 * each valid mirror that covers it.
10982 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10987 "CRC-32 checksum value for chunk "DEXT":\n",
10988 PEXT(&chunk->chunk));
10989 for (i = 0; i < chunk->mirror_count; i++)
10990 fprintf(stdout, "Mirror %u:\t%#lx\n",
10991 chunk->mirror_id[i], crc[i]);
10992 fprintf(stdout, "\n");
10996 * filter_mirror_id() - Filter specified mirror ids.
10997 * @chunks: Array of chunks.
10998 * @chunk_count: Number of chunks in @chunks array.
10999 * @mirror_ids: Specified mirror ids to be verified.
11000 * @ids_nr: Number of specified mirror ids.
11002 * This function scans valid mirror ids that cover each chunk in @chunks
11003 * and filters specified mirror ids.
11008 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11009 __u16 *mirror_ids, int ids_nr)
11014 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11015 unsigned int valid_count = 0;
11017 for (i = 0; i < chunk_count; i++) {
11018 if (chunks[i].mirror_count == 0)
11022 for (j = 0; j < ids_nr; j++) {
11023 for (k = 0; k < chunks[i].mirror_count; k++) {
11024 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11025 valid_id[valid_count] = mirror_ids[j];
11032 memcpy(chunks[i].mirror_id, valid_id,
11033 sizeof(__u16) * valid_count);
11034 chunks[i].mirror_count = valid_count;
11039 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11040 * @layout: Mirror component list.
11041 * @chunks: Array of chunks.
11042 * @chunks_size: Array size of @chunks.
11044 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11045 * to find out chunk segments and store them in @chunks array.
11047 * The @mirror_id array in each element of @chunks will store the valid
11048 * mirror ids that cover the chunk. If a mirror component covering the
11049 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11050 * will not be stored into the @mirror_id array, and the chunk for that
11051 * mirror will not be verified.
11053 * The @mirror_count in each element of @chunks will store the number of
11054 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11055 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11056 * indicates the chunk is valid in only one mirror. In both cases, the
11057 * chunk will not be verified.
11059 * Here is an example:
11061 * 0 1M 2M 3M 4M EOF
11062 * +------+-------------+--------------------+
11063 * | | | S | mirror1
11064 * +------+------+------+------+-------------+
11065 * | | S | S | | mirror2
11066 * +-------------+------+------+-------------+
11068 * prepared @chunks array will contain 5 elements:
11069 * (([0, 1M), [1, 2], 2),
11070 * ([1M, 2M), [1, 2], 2),
11071 * ([2M, 3M), [1], 1),
11072 * ([3M, 4M], [], 0),
11073 * ([4M, EOF), [2], 1))
11075 * Return: the actual array size of @chunks on success
11076 * or a negative error code on failure.
11079 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11080 struct verify_chunk *chunks,
11081 size_t chunks_size)
11085 uint32_t mirror_id;
11091 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11094 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11097 "%s: move to the first layout component: %s.\n",
11098 progname, strerror(errno));
11104 chunks[idx].chunk.e_end = LUSTRE_EOF;
11106 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11109 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11110 progname, strerror(errno));
11114 if (start > chunks[idx].chunk.e_start ||
11115 end <= chunks[idx].chunk.e_start)
11118 if (end < chunks[idx].chunk.e_end)
11119 chunks[idx].chunk.e_end = end;
11121 rc = llapi_layout_comp_flags_get(layout, &flags);
11124 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11125 progname, strerror(errno));
11129 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11132 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11135 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11136 progname, strerror(errno));
11140 chunks[idx].mirror_id[i] = mirror_id;
11142 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11144 "%s: mirror_id array is too small.\n",
11151 rc = llapi_layout_comp_use(layout,
11152 LLAPI_LAYOUT_COMP_USE_NEXT);
11155 "%s: move to the next layout component: %s.\n",
11156 progname, strerror(errno));
11159 } /* loop through all components */
11161 chunks[idx].mirror_count = i;
11163 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11167 if (idx >= chunks_size) {
11168 fprintf(stderr, "%s: chunks array is too small.\n",
11174 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11178 return rc < 0 ? rc : idx + 1;
11182 * lfs_mirror_verify_chunk() - Verify a chunk.
11183 * @fd: File descriptor of the mirrored file.
11184 * @file_size: Size of the mirrored file.
11185 * @chunk: A chunk and its corresponding valid mirror ids.
11186 * @verbose: Verbose mode.
11188 * This function verifies a @chunk contains exactly the same data
11189 * ammong the mirrors that cover it.
11191 * If @verbose is specified, then the function will print where the
11192 * differences are if the data do not match. Otherwise, it will
11193 * just return an error in that case.
11195 * Return: 0 on success or a negative error code on failure.
11198 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11199 struct verify_chunk *chunk, int verbose)
11201 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11203 size_t page_size = sysconf(_SC_PAGESIZE);
11204 ssize_t bytes_read;
11205 ssize_t bytes_done;
11209 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11213 if (file_size == 0)
11216 rc = posix_memalign(&buf, page_size, buflen);
11217 if (rc) /* error code is returned directly */
11221 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11222 PEXT(&chunk->chunk));
11223 for (i = 0; i < chunk->mirror_count; i++)
11224 fprintf(stdout, " %u", chunk->mirror_id[i]);
11225 fprintf(stdout, "\n");
11229 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11230 pos = chunk->chunk.e_start;
11231 while (bytes_done < count) {
11232 /* compute initial CRC-32 checksum */
11233 crc = crc32(0L, Z_NULL, 0);
11234 memset(crc_array, 0, sizeof(crc_array));
11237 for (i = 0; i < chunk->mirror_count; i++) {
11238 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11240 if (bytes_read < 0) {
11243 "%s: failed to read data from mirror %u: %s.\n",
11244 progname, chunk->mirror_id[i],
11249 /* compute new CRC-32 checksum */
11250 crc_array[i] = crc32(crc, buf, bytes_read);
11254 print_checksums(chunk, crc_array);
11256 /* compare CRC-32 checksum values */
11257 for (i = 1; i < chunk->mirror_count; i++) {
11258 if (crc_array[i] != crc_array[0]) {
11262 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11263 progname, PEXT(&chunk->chunk),
11264 chunk->mirror_id[0],
11265 chunk->mirror_id[i]);
11270 bytes_done += bytes_read;
11273 if (verbose > 1 && rc == 0) {
11274 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11275 PEXT(&chunk->chunk));
11276 for (i = 0; i < chunk->mirror_count; i++)
11277 fprintf(stdout, " %u", chunk->mirror_id[i]);
11278 fprintf(stdout, " PASS\n\n");
11287 * lfs_mirror_verify_file() - Verify a mirrored file.
11288 * @fname: Mirrored file name.
11289 * @mirror_ids: Specified mirror ids to be verified.
11290 * @ids_nr: Number of specified mirror ids.
11291 * @verbose: Verbose mode.
11293 * This function verifies that each SYNC mirror of a mirrored file
11294 * specified by @fname contains exactly the same data.
11296 * If @mirror_ids is specified, then the function will verify the
11297 * mirrors specified by @mirror_ids contain exactly the same data.
11299 * If @verbose is specified, then the function will print where the
11300 * differences are if the data do not match. Otherwise, it will
11301 * just return an error in that case.
11303 * Return: 0 on success or a negative error code on failure.
11306 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11309 struct verify_chunk chunks_array[1024] = { };
11310 struct llapi_layout *layout = NULL;
11312 uint32_t flr_state;
11314 int chunk_count = 0;
11320 if (stat(fname, &stbuf) < 0) {
11321 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11322 progname, fname, strerror(errno));
11327 if (!S_ISREG(stbuf.st_mode)) {
11328 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11334 if (stbuf.st_size == 0) {
11336 fprintf(stdout, "%s: '%s' file size is 0.\n",
11342 fd = open(fname, O_DIRECT | O_RDONLY);
11344 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11345 progname, fname, strerror(errno));
11350 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11352 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11353 progname, fname, strerror(errno));
11357 layout = llapi_layout_get_by_fd(fd, 0);
11359 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11360 progname, fname, strerror(errno));
11362 llapi_lease_release(fd);
11366 rc = llapi_layout_flags_get(layout, &flr_state);
11368 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11369 progname, fname, strerror(errno));
11374 flr_state &= LCM_FL_FLR_MASK;
11375 switch (flr_state) {
11378 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11379 progname, fname, llapi_layout_flags_string(flr_state));
11385 /* find out mirror chunks to be verified */
11386 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11387 ARRAY_SIZE(chunks_array));
11388 if (chunk_count < 0) {
11394 /* filter specified mirror ids */
11395 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11398 print_chunks(fname, chunks_array, chunk_count);
11400 for (idx = 0; idx < chunk_count; idx++) {
11401 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11404 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11406 PEXT(&chunks_array[idx].chunk),
11407 (unsigned long long)stbuf.st_size);
11411 if (chunks_array[idx].mirror_count == 0) {
11413 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11415 PEXT(&chunks_array[idx].chunk));
11417 fprintf(stderr, "skipped\n");
11421 fprintf(stderr, "failed\n");
11425 if (chunks_array[idx].mirror_count == 1) {
11428 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11430 PEXT(&chunks_array[idx].chunk),
11431 chunks_array[idx].mirror_id[0]);
11435 rc = llapi_lease_check(fd);
11436 if (rc != LL_LEASE_RDLCK) {
11437 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11442 /* verify one chunk */
11443 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11444 &chunks_array[idx], verbose);
11458 llapi_layout_free(layout);
11459 llapi_lease_release(fd);
11467 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11468 * @argc: The count of lfs mirror verify command line arguments.
11469 * @argv: Array of strings for lfs mirror verify command line arguments.
11471 * This function parses lfs mirror verify command and verifies the
11472 * specified mirrored file(s).
11474 * Return: 0 on success or a negative error code on failure.
11476 static inline int lfs_mirror_verify(int argc, char **argv)
11478 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11484 char cmd[PATH_MAX];
11486 struct option long_opts[] = {
11487 { .val = 'o', .name = "only", .has_arg = required_argument },
11488 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11489 { .name = NULL } };
11491 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11493 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11496 rc = parse_mirror_ids(mirror_ids,
11497 ARRAY_SIZE(mirror_ids),
11501 "%s: bad mirror ids '%s'.\n",
11508 "%s: at least 2 mirror ids needed with '--only' option.\n",
11518 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11519 progname, argv[optind - 1]);
11525 if (argc == optind) {
11526 fprintf(stderr, "%s: no file name given.\n", progname);
11531 if (ids_nr > 0 && argc > optind + 1) {
11533 "%s: '--only' cannot be used upon multiple files.\n",
11540 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11546 for (; optind < argc; optind++) {
11547 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11557 * lfs_mirror() - Parse and execute lfs mirror commands.
11558 * @argc: The count of lfs mirror command line arguments.
11559 * @argv: Array of strings for lfs mirror command line arguments.
11561 * This function parses lfs mirror commands and performs the
11562 * corresponding functions specified in mirror_cmdlist[].
11564 * Return: 0 on success or an error code on failure.
11566 static int lfs_mirror(int argc, char **argv)
11568 char cmd[PATH_MAX];
11571 setlinebuf(stdout);
11573 Parser_init("lfs-mirror > ", mirror_cmdlist);
11575 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11577 program_invocation_short_name = cmd;
11579 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11581 rc = Parser_commands();
11583 return rc < 0 ? -rc : rc;
11586 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11588 #if __BYTE_ORDER == __BIG_ENDIAN
11589 __swab16s(&attrs->lsa_valid);
11590 __swab64s(&attrs->lsa_size);
11591 __swab64s(&attrs->lsa_blocks);
11595 enum lfs_som_type {
11596 LFS_SOM_SIZE = 0x1,
11597 LFS_SOM_BLOCKS = 0x2,
11598 LFS_SOM_FLAGS = 0x4,
11599 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11603 static int lfs_getsom(int argc, char **argv)
11606 struct lustre_som_attrs *attrs;
11607 char buf[sizeof(*attrs) + 64];
11608 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11611 while ((c = getopt(argc, argv, "sbf")) != -1) {
11614 type = LFS_SOM_SIZE;
11617 type = LFS_SOM_BLOCKS;
11620 type = LFS_SOM_FLAGS;
11623 fprintf(stderr, "%s: invalid option '%c'\n",
11633 fprintf(stderr, "%s: %s\n",
11634 progname, argc == 0 ? "miss file target" :
11635 "input more than 2 files");
11640 attrs = (void *)buf;
11641 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11644 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11645 argv[0], strerror(errno), errno);
11649 lustre_som_swab(attrs);
11652 case LFS_SOM_ATTR_ALL:
11653 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11654 path, (unsigned long long)attrs->lsa_size,
11655 (unsigned long long)attrs->lsa_blocks,
11659 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11661 case LFS_SOM_BLOCKS:
11662 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11664 case LFS_SOM_FLAGS:
11665 printf("%x\n", attrs->lsa_valid);
11668 fprintf(stderr, "%s: unknown option\n", progname);
11676 * lfs_mirror_list_commands() - List lfs mirror commands.
11677 * @argc: The count of command line arguments.
11678 * @argv: Array of strings for command line arguments.
11680 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11682 * Return: 0 on success.
11684 static int lfs_mirror_list_commands(int argc, char **argv)
11686 char buffer[81] = "";
11688 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11694 static int lfs_pcc_attach(int argc, char **argv)
11696 struct option long_opts[] = {
11697 { .val = 'i', .name = "id", .has_arg = required_argument },
11698 { .name = NULL } };
11701 __u32 archive_id = 0;
11704 char fullpath[PATH_MAX];
11705 enum lu_pcc_type type = LU_PCC_READWRITE;
11708 while ((c = getopt_long(argc, argv, "i:",
11709 long_opts, NULL)) != -1) {
11713 archive_id = strtoul(optarg, &end, 0);
11714 if (errno != 0 || *end != '\0' ||
11715 archive_id == 0 || archive_id > UINT32_MAX) {
11717 "error: %s: bad archive ID '%s'\n",
11725 fprintf(stderr, "%s: option '%s' unrecognized\n",
11726 argv[0], argv[optind - 1]);
11731 if (archive_id == 0) {
11732 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11736 if (argc <= optind) {
11737 fprintf(stderr, "%s: must specify one or more file names\n",
11742 while (optind < argc) {
11745 path = argv[optind++];
11746 if (!realpath(path, fullpath)) {
11747 fprintf(stderr, "%s: could not find path '%s': %s\n",
11748 argv[0], path, strerror(errno));
11754 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11757 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11758 argv[0], path, archive_id, strerror(-rc2));
11766 static int lfs_pcc_attach_fid(int argc, char **argv)
11768 struct option long_opts[] = {
11769 { .val = 'i', .name = "id", .has_arg = required_argument },
11770 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11771 { .name = NULL } };
11772 char short_opts[] = "i:m:";
11775 __u32 archive_id = 0;
11777 const char *mntpath = NULL;
11778 const char *fidstr;
11779 enum lu_pcc_type type = LU_PCC_READWRITE;
11782 while ((c = getopt_long(argc, argv, short_opts,
11783 long_opts, NULL)) != -1) {
11787 archive_id = strtoul(optarg, &end, 0);
11788 if (errno != 0 || *end != '\0' ||
11789 archive_id > UINT32_MAX) {
11791 "error: %s: bad archive ID '%s'\n",
11802 fprintf(stderr, "%s: option '%s' unrecognized\n",
11803 argv[0], argv[optind - 1]);
11808 if (archive_id == 0) {
11809 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11814 fprintf(stderr, "%s: must specify Lustre mount point\n",
11819 if (argc <= optind) {
11820 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11824 while (optind < argc) {
11827 fidstr = argv[optind++];
11829 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11833 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11834 argv[0], fidstr, mntpath, archive_id,
11837 if (rc == 0 && rc2 < 0)
11843 static int lfs_pcc_detach(int argc, char **argv)
11845 struct option long_opts[] = {
11846 { .val = 'k', .name = "keep", .has_arg = no_argument },
11847 { .name = NULL } };
11848 char short_opts[] = "k";
11852 char fullpath[PATH_MAX];
11853 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11856 while ((c = getopt_long(argc, argv, short_opts,
11857 long_opts, NULL)) != -1) {
11860 detach_opt = PCC_DETACH_OPT_NONE;
11865 fprintf(stderr, "%s: option '%s' unrecognized\n",
11866 argv[0], argv[optind - 1]);
11871 while (optind < argc) {
11874 path = argv[optind++];
11875 if (!realpath(path, fullpath)) {
11876 fprintf(stderr, "%s: could not find path '%s': %s\n",
11877 argv[0], path, strerror(errno));
11883 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11887 "%s: cannot detach '%s' from PCC: %s\n",
11888 argv[0], path, strerror(errno));
11896 static int lfs_pcc_detach_fid(int argc, char **argv)
11898 struct option long_opts[] = {
11899 { .val = 'k', .name = "keep", .has_arg = no_argument },
11900 { .name = NULL } };
11901 char short_opts[] = "k";
11905 const char *mntpath;
11906 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11909 while ((c = getopt_long(argc, argv, short_opts,
11910 long_opts, NULL)) != -1) {
11913 detach_opt = PCC_DETACH_OPT_NONE;
11918 fprintf(stderr, "%s: option '%s' unrecognized\n",
11919 argv[0], argv[optind - 1]);
11924 mntpath = argv[optind++];
11926 while (optind < argc) {
11929 fid = argv[optind++];
11931 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11934 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11935 argv[0], fid, mntpath, strerror(-rc2));
11943 static int lfs_pcc_state(int argc, char **argv)
11947 char fullpath[PATH_MAX];
11948 struct lu_pcc_state state;
11953 fprintf(stderr, "%s: must specify one or more file names\n",
11958 while (optind < argc) {
11961 path = argv[optind++];
11962 if (!realpath(path, fullpath)) {
11963 fprintf(stderr, "%s: could not find path '%s': %s\n",
11964 argv[0], path, strerror(errno));
11970 rc2 = llapi_pcc_state_get(fullpath, &state);
11975 "%s: cannot get PCC state of '%s': %s\n",
11976 argv[0], path, strerror(-rc2));
11980 printf("file: %s", path);
11981 printf(", type: %s", pcc_type2string(state.pccs_type));
11982 if (state.pccs_type == LU_PCC_NONE &&
11983 state.pccs_open_count == 0) {
11988 printf(", PCC file: %s", state.pccs_path);
11989 printf(", user number: %u", state.pccs_open_count);
11990 printf(", flags: %x", state.pccs_flags);
11997 * lfs_pcc_list_commands() - List lfs pcc commands.
11998 * @argc: The count of command line arguments.
11999 * @argv: Array of strings for command line arguments.
12001 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12003 * Return: 0 on success.
12005 static int lfs_pcc_list_commands(int argc, char **argv)
12007 char buffer[81] = "";
12009 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12016 * lfs_pcc() - Parse and execute lfs pcc commands.
12017 * @argc: The count of lfs pcc command line arguments.
12018 * @argv: Array of strings for lfs pcc command line arguments.
12020 * This function parses lfs pcc commands and performs the
12021 * corresponding functions specified in pcc_cmdlist[].
12023 * Return: 0 on success or an error code on failure.
12025 static int lfs_pcc(int argc, char **argv)
12027 char cmd[PATH_MAX];
12030 setlinebuf(stdout);
12032 Parser_init("lfs-pcc > ", pcc_cmdlist);
12034 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12036 program_invocation_short_name = cmd;
12038 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12040 rc = Parser_commands();
12042 return rc < 0 ? -rc : rc;
12045 static int lfs_list_commands(int argc, char **argv)
12047 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12049 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12054 int main(int argc, char **argv)
12058 /* Ensure that liblustreapi constructor has run */
12059 if (!llapi_liblustreapi_initialized())
12060 fprintf(stderr, "liblustreapi was not properly initialized\n");
12062 setlinebuf(stdout);
12065 Parser_init("lfs > ", cmdlist);
12067 progname = program_invocation_short_name; /* Used in error messages */
12069 llapi_set_command_name(argv[1]);
12070 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12071 llapi_clear_command_name();
12073 rc = Parser_commands();
12076 return rc < 0 ? -rc : rc;
12079 #ifdef _LUSTRE_IDL_H_
12080 /* Everything we need here should be included by lustreapi.h. */
12081 # error "lfs should not depend on lustre_idl.h"
12082 #endif /* _LUSTRE_IDL_H_ */