4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter J. Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
58 #include <sys/types.h>
60 #include <sys/param.h>
61 #include <sys/xattr.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <libcfs/util/string.h>
75 #include <lustre/lustreapi.h>
76 #include <linux/lustre/lustre_ver.h>
77 #include <linux/lustre/lustre_param.h>
78 #include <linux/lnet/nidstr.h>
79 #include <lnetconfig/cyaml.h>
83 static int lfs_find(int argc, char **argv);
84 static int lfs_getstripe(int argc, char **argv);
85 static int lfs_getdirstripe(int argc, char **argv);
86 static int lfs_setdirstripe(int argc, char **argv);
87 static int lfs_rmentry(int argc, char **argv);
88 static int lfs_unlink_foreign(int argc, char **argv);
89 static int lfs_osts(int argc, char **argv);
90 static int lfs_mdts(int argc, char **argv);
91 static int lfs_df(int argc, char **argv);
92 static int lfs_getname(int argc, char **argv);
93 static int lfs_check(int argc, char **argv);
94 #ifdef HAVE_SYS_QUOTA_H
95 static int lfs_setquota(int argc, char **argv);
96 static int lfs_quota(int argc, char **argv);
97 static int lfs_project(int argc, char **argv);
99 static int lfs_flushctx(int argc, char **argv);
100 static int lfs_poollist(int argc, char **argv);
101 static int lfs_changelog(int argc, char **argv);
102 static int lfs_changelog_clear(int argc, char **argv);
103 static int lfs_fid2path(int argc, char **argv);
104 static int lfs_path2fid(int argc, char **argv);
105 static int lfs_rmfid(int argc, char **argv);
106 static int lfs_data_version(int argc, char **argv);
107 static int lfs_hsm_state(int argc, char **argv);
108 static int lfs_hsm_set(int argc, char **argv);
109 static int lfs_hsm_clear(int argc, char **argv);
110 static int lfs_hsm_action(int argc, char **argv);
111 static int lfs_hsm_archive(int argc, char **argv);
112 static int lfs_hsm_restore(int argc, char **argv);
113 static int lfs_hsm_release(int argc, char **argv);
114 static int lfs_hsm_remove(int argc, char **argv);
115 static int lfs_hsm_cancel(int argc, char **argv);
116 static int lfs_swap_layouts(int argc, char **argv);
117 static int lfs_mv(int argc, char **argv);
118 static int lfs_ladvise(int argc, char **argv);
119 static int lfs_getsom(int argc, char **argv);
120 static int lfs_heat_get(int argc, char **argv);
121 static int lfs_heat_set(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_read(int argc, char **argv);
128 static inline int lfs_mirror_write(int argc, char **argv);
129 static inline int lfs_mirror_copy(int argc, char **argv);
130 static int lfs_pcc_attach(int argc, char **argv);
131 static int lfs_pcc_attach_fid(int argc, char **argv);
132 static int lfs_pcc_detach(int argc, char **argv);
133 static int lfs_pcc_detach_fid(int argc, char **argv);
134 static int lfs_pcc_state(int argc, char **argv);
135 static int lfs_pcc(int argc, char **argv);
136 static int lfs_pcc_list_commands(int argc, char **argv);
137 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
138 __u64 migration_flags,
139 struct llapi_stripe_param *param,
140 struct llapi_layout *layout);
142 struct pool_to_id_cbdata {
146 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
147 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
149 enum setstripe_origin {
159 static int lfs_setstripe_internal(int argc, char **argv,
160 enum setstripe_origin opc);
162 static inline int lfs_setstripe(int argc, char **argv)
164 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
167 static inline int lfs_setstripe_migrate(int argc, char **argv)
169 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
172 static inline int lfs_mirror_create(int argc, char **argv)
174 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
177 static inline int lfs_mirror_extend(int argc, char **argv)
179 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
182 static inline int lfs_mirror_split(int argc, char **argv)
184 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
187 static inline int lfs_mirror_delete(int argc, char **argv)
189 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
192 /* Setstripe and migrate share mostly the same parameters */
193 #define SSM_CMD_COMMON(cmd) \
194 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
195 " [--stripe-count|-c <stripe_count>]\n" \
196 " [--overstripe-count|-C <stripe_count>]\n" \
197 " [--stripe-index|-i <start_ost_idx>]\n" \
198 " [--stripe-size|-S <stripe_size>]\n" \
199 " [--extension-size|--ext-size|-z]\n" \
200 " [--layout|-L <pattern>]\n" \
201 " [--mirror-count|-N[mirror_count]]\n" \
202 " [--ost|-o <ost_indices>]\n" \
203 " [--pool|-p <pool_name>]\n" \
204 " [--yaml|-y <yaml_template_file>]\n" \
205 " [--copy=<lustre_src>]\n"
207 #define SSM_HELP_COMMON \
208 "\tstripe_count: Number of OSTs to stripe on (0=fs default, -1 all)\n" \
209 "\t Using -C instead of -c allows overstriping, which\n" \
210 "\t will place more than one stripe per OST if\n" \
211 "\t stripe_count is greater than the number of OSTs.\n" \
212 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
213 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
214 "\t Optional K, M, or G suffix (for KB, MB, GB\n" \
215 "\t respectively). Must be a multiple of 64KiB.\n" \
216 "\textension_size:\n" \
217 "\t Number of bytes the previous component is extended\n" \
218 "\t each time. Optional K, M, or G suffix (for KB,\n" \
219 "\t MB, GB respectively)\n" \
220 "\tpool_name: Name of OST pool to use (default none)\n" \
221 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n" \
222 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
223 "\t Indices be specified in a format of:\n" \
224 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
226 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
227 "\t If --pool is set with --ost then the OSTs\n" \
228 "\t must be the members of the pool.\n" \
229 "\tcomp_end: Extent end of component, start after previous end.\n" \
230 "\t Optional K, M, or G suffix (for KiB, MiB, GiB), or\n" \
231 "\t -1 or 'eof' for max file size). Must be a multiple\n" \
232 "\t of stripe_size and a multiple of 64KiB.\n" \
233 "\tyaml_template_file:\n" \
234 "\t YAML layout template file, can't be used with -c,\n" \
235 "\t -i, -S, -p, -o, or -E arguments.\n" \
236 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
237 "\t another lustre file or directory, can't used with\n" \
238 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
240 #define MIRROR_CREATE_HELP \
241 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
242 "\t setstripe layout options\n" \
243 "\t It defaults to 1 if not specified; if specified,\n" \
244 "\t it must follow the option without a space.\n" \
245 "\t The option can also be repeated multiple times to\n" \
246 "\t separate mirrors that have different layouts.\n" \
247 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
248 "\t It can be a plain layout or a composite layout.\n" \
249 "\t If not specified, the stripe options inherited\n" \
250 "\t from the previous component will be used.\n" \
251 "\tflags: set flags to the component of the current mirror.\n" \
252 "\t Only \"prefer\" flag is supported so far.\n"
254 #define MIRROR_EXTEND_HELP \
256 "\tvictim_file: The layout of victim_file will be split and used\n" \
257 "\t as a mirror added to the mirrored file.\n" \
258 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
259 "\t from victim file(s) in case the victim file(s)\n" \
260 "\t contains the same data as the original mirrored\n" \
263 #define MIRROR_EXTEND_USAGE \
264 " {--mirror-count|-N[mirror_count]}\n" \
265 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
268 #define SETSTRIPE_USAGE \
269 SSM_CMD_COMMON("setstripe") \
270 MIRROR_EXTEND_USAGE \
271 " <directory|filename>\n" \
275 #define MIGRATE_USAGE \
276 SSM_CMD_COMMON("migrate ") \
277 " [--block|-b] [--non-block|-n]\n" \
278 " [--non-direct|-D] [--verbose|-v]\n" \
282 "\tblock: Block file access during data migration (default)\n" \
283 "\tnon-block: Abort migrations if concurrent access is detected\n" \
284 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
285 "\tverbose: Print each filename as it is migrated\n" \
287 #define SETDIRSTRIPE_USAGE \
288 " [--mdt-count|-c stripe_count>\n" \
289 " [--mdt-hash|-H mdt_hash]\n" \
290 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
291 " [--default|-D] [--mode|-o mode] <dir>\n" \
292 "\tstripe_count: stripe count of the striped directory\n" \
293 "\tmdt_index: MDT index of first stripe\n" \
294 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
295 " crush CRUSH hash algorithm (default)\n" \
296 " fnv_1a_64 FNV-1a hash algorithm\n" \
297 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
298 "\tdefault_stripe: set default dirstripe of the directory\n" \
299 "\tmode: the file access permission of the directory (octal)\n" \
300 "To create dir with a foreign (free format) layout :\n" \
301 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
302 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
303 "\tmode: the mode of the directory\n" \
304 "\tforeign_type: none or symlink\n"
307 * command_t mirror_cmdlist - lfs mirror commands.
309 command_t mirror_cmdlist[] = {
310 { .pc_name = "create", .pc_func = lfs_mirror_create,
311 .pc_help = "Create a mirrored file.\n"
312 "usage: lfs mirror create "
313 "<--mirror-count|-N[mirror_count]> "
314 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
315 MIRROR_CREATE_HELP },
316 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
317 .pc_help = "Delete a mirror from a file.\n"
318 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
319 "\t --component-id|--comp-id|-I <comp_id> |\n"
320 "\t -p <pool>} <mirrored_file> ...\n"
322 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
323 .pc_help = "Extend a mirrored file.\n"
324 "usage: lfs mirror extend "
325 "<--mirror-count|-N[mirror_count]> [--no-verify] "
326 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
327 MIRROR_EXTEND_HELP },
328 { .pc_name = "split", .pc_func = lfs_mirror_split,
329 .pc_help = "Split a mirrored file.\n"
330 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
331 "\t --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
332 "\t [-f <new_file>] <mirrored_file> ...\n"
333 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
334 "\t can be fetched by lfs getstripe command.\n"
335 "\tcomp_id: Unique component ID within a mirror.\n"
336 "\tpool: Components on specified pool.\n"
337 "\tnew_file: This option indicates the layout of the split\n"
338 "\t mirror will be stored into. If not specified,\n"
339 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
340 "\t will be used.\n" },
341 { .pc_name = "read", .pc_func = lfs_mirror_read,
342 .pc_help = "Read the content of a specified mirror of a file.\n"
343 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
344 "[--outfile|-o <output_file>] <mirrored_file>\n" },
345 { .pc_name = "write", .pc_func = lfs_mirror_write,
346 .pc_help = "Write to a specified mirror of a file.\n"
347 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
348 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
349 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
350 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
351 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
352 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
353 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
354 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
355 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
356 "<mirrored file> [<mirrored file2>...]\n"},
357 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
358 .pc_help = "Verify mirrored file(s).\n"
359 "usage: lfs mirror verify "
360 "[--only <mirror_id,mirror_id2[,...]>] "
361 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
362 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
363 .pc_help = "list commands supported by lfs mirror"},
364 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
365 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
366 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
371 * command_t pcc_cmdlist - lfs pcc commands.
373 command_t pcc_cmdlist[] = {
374 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
375 .pc_help = "Attach given files to the Persistent Client Cache.\n"
376 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
377 "\t-i: archive id for RW-PCC\n" },
378 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
379 .pc_help = "Attach given files into PCC by FID(s).\n"
380 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
382 "\t-i: archive id for RW-PCC\n"
383 "\t-m: Lustre mount point\n" },
384 { .pc_name = "state", .pc_func = lfs_pcc_state,
385 .pc_help = "Display the PCC state for given files.\n"
386 "usage: lfs pcc state <file> ...\n" },
387 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
388 .pc_help = "Detach given files from the Persistent Client Cache.\n"
389 "usage: lfs pcc detach <file> ...\n" },
390 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
391 .pc_help = "Detach given files from PCC by FID(s).\n"
392 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
393 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
394 .pc_help = "list commands supported by lfs pcc"},
395 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
396 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
397 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
401 /* all available commands */
402 command_t cmdlist[] = {
403 {"setstripe", lfs_setstripe, 0,
404 "To create a file with specified striping/composite layout, or\n"
405 "create/replace the default layout on an existing directory:\n"
406 SSM_CMD_COMMON("setstripe")
408 " <directory|filename>\n"
410 "To add component(s) to an existing composite file:\n"
411 SSM_CMD_COMMON("setstripe --component-add")
413 "To totally delete the default striping from an existing directory:\n"
414 "usage: setstripe [--delete|-d] <directory>\n"
416 "To create a mirrored file or set s default mirror layout on a directory:\n"
417 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
419 "To delete the last component(s) from an existing composite file\n"
420 "(note that this will also delete any data in those components):\n"
421 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
422 " [--component-flags|-F <comp_flags>]\n"
424 "\tcomp_id: Unique component ID to delete\n"
425 "\tcomp_flags: 'init' indicating all instantiated components\n"
426 "\t '^init' indicating all uninstantiated components\n"
427 "\t-I and -F cannot be specified at the same time\n"
429 "To set or clear flags on a specific component\n"
430 "(note that this command can only be applied to mirrored files:\n"
431 "usage: setstripe --comp-set {-I comp_id|--comp-flags=comp_flags}\n"
434 "To create a file with a foreign (free format) layout:\n"
435 "usage: setstripe --foreign[=<foreign_type>]\n"
436 " --xattr|-x <layout_string> [--flags <hex>]\n"
437 " [--mode <mode>] <filename>\n"},
438 {"getstripe", lfs_getstripe, 0,
439 "To list the layout pattern for a given file or files in a\n"
440 "directory or recursively for all files in a directory tree.\n"
441 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
442 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
443 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
444 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
445 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
446 " [--component-id[=comp_id]|-I[comp_id]]\n"
447 " [--component-flags[=comp_flags]]\n"
448 " [--component-count]\n"
449 " [--extension-size|--ext-size|-z]\n"
450 " [--component-start[=[+-]comp_start]]\n"
451 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
452 " [[!] --mirror-index=[+-]<index> |\n"
453 " [!] --mirror-id=[+-]<id>] [--mirror-count|-N]\n"
454 " <directory|filename> ..."},
455 {"setdirstripe", lfs_setdirstripe, 0,
456 "Create striped directory on specified MDT, same as mkdir.\n"
457 "May be restricted to root or group users, depending on settings.\n"
458 "usage: setdirstripe [OPTION] <directory>\n"
460 {"getdirstripe", lfs_getdirstripe, 0,
461 "To list the layout pattern info for a given directory\n"
462 "or recursively for all directories in a directory tree.\n"
463 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
464 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
465 " [--recursive|-r] [--yaml|-y]\n"
466 " [--verbose|-v] [--default|-D] <dir> ..."},
467 {"mkdir", lfs_setdirstripe, 0,
468 "Create striped directory on specified MDT, same as setdirstripe.\n"
469 "usage: mkdir [OPTION] <directory>\n"
471 {"rm_entry", lfs_rmentry, 0,
472 "To remove the name entry of the remote directory. Note: This\n"
473 "command will only delete the name entry, i.e. the remote directory\n"
474 "will become inaccessable after this command. This can only be done\n"
475 "by the administrator\n"
476 "usage: rm_entry <dir>\n"},
477 {"unlink_foreign", lfs_unlink_foreign, 0,
478 "To remove the foreign file/dir.\n"
479 "Note: This is for files/dirs prevented to be removed using\n"
480 "unlink/rmdir, but works also for regular ones\n"
481 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
482 {"pool_list", lfs_poollist, 0,
483 "List pools or pool OSTs\n"
484 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
485 {"find", lfs_find, 0,
486 "find files matching given attributes recursively in directory tree.\n"
487 "usage: find <directory|filename> ...\n"
488 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
489 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
490 " [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
491 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
492 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
493 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
494 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
495 " [[!] --stripe-count|-c [+-]<stripes>]\n"
496 " [[!] --stripe-index|-i <index,...>]\n"
497 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
498 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
499 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
500 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
501 " [[!] --projid <projid>]\n"
502 " [[!] --layout|-L released,raid0,mdt]\n"
503 " [[!] --foreign[=<foreign_type>]]\n"
504 " [[!] --component-count [+-]<comp_cnt>]\n"
505 " [[!] --component-start [+-]N[kMGTPE]]\n"
506 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
507 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
508 " [[!] --mirror-count|-N [+-]<n>]\n"
509 " [[!] --mirror-state <[^]state>]\n"
510 " [[!] --mdt-count|-T [+-]<stripes>]\n"
511 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
512 " [[!] --mdt-index|-m <uuid|index,...>]\n"
513 "\t !: used before an option indicates 'NOT' requested attribute\n"
514 "\t -: used before a value indicates less than requested value\n"
515 "\t +: used before a value indicates more than requested value\n"
516 "\t ^: used before a flag indicates to exclude it\n"},
517 {"check", lfs_check, 0,
518 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
519 "or all the servers (MGTs, MDTs and OSTs).\n"
520 "usage: check <mgts|osts|mdts|all>"},
521 {"osts", lfs_osts, 0, "list OSTs connected to client "
522 "[for specified path only]\n" "usage: osts [path]"},
523 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
524 "[for specified path only]\n" "usage: mdts [path]"},
526 "report filesystem disk space usage or inodes usage "
527 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
528 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
529 " [--pool|-p <fsname>[.<pool>]] [path]"},
530 {"getname", lfs_getname, 0,
531 "list instances and specified mount points [for specified path only]\n"
532 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
533 #ifdef HAVE_SYS_QUOTA_H
534 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
535 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
536 " -b <block-softlimit> -B <block-hardlimit>\n"
537 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
538 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
539 " [--block-softlimit <block-softlimit>]\n"
540 " [--block-hardlimit <block-hardlimit>]\n"
541 " [--inode-softlimit <inode-softlimit>]\n"
542 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
543 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
544 " [--block-grace 'notify'|<block-grace>]\n"
545 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
546 " setquota <-U|-G|-P>\n"
547 " -b <block-softlimit> -B <block-hardlimit>\n"
548 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
549 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
550 " [--block-softlimit <block-softlimit>]\n"
551 " [--block-hardlimit <block-hardlimit>]\n"
552 " [--inode-softlimit <inode-softlimit>]\n"
553 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
554 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
556 " -b can be used instead of --block-softlimit/--block-grace\n"
557 " -B can be used instead of --block-hardlimit\n"
558 " -i can be used instead of --inode-softlimit/--inode-grace\n"
559 " -I can be used instead of --inode-hardlimit\n"
560 " -d can be used instead of --default\n\n"
561 "Note: The total quota space will be split into many qunits and\n"
562 " balanced over all server targets, the minimal qunit size is\n"
563 " 1M bytes for block space and 1K inodes for inode space.\n\n"
564 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
565 " Quota space rebalancing process will stop when this mininum\n"
566 " value is reached. As a result, quota exceeded can be returned\n"
567 " while many targets still have 1MB or 1K inodes of spare\n"
569 " When setting the grace time, 'notify' can be used as grace to\n"
570 " be notified after the quota is over soft limit but prevents\n"
571 " the soft limit from becoming the hard limit."},
572 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
573 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
575 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
576 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
577 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
578 {"project", lfs_project, 0,
579 "Change or list project attribute for specified file or directory.\n"
580 "usage: project [-d|-r] <file|directory...>\n"
581 " list project ID and flags on file(s) or directories\n"
582 " project [-p id] [-s] [-r] <file|directory...>\n"
583 " set project ID and/or inherit flag for specified file(s) or directories\n"
584 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
585 " check project ID and flags on file(s) or directories, print outliers\n"
586 " project -C [-r] [-k] <file|directory...>\n"
587 " clear the project inherit flag and ID on the file or directory\n"
590 {"flushctx", lfs_flushctx, 0,
591 "Flush security context for current user.\n"
592 "usage: flushctx [-k] [-r] [mountpoint...]"},
593 {"changelog", lfs_changelog, 0,
594 "Show the metadata changes on an MDT."
595 "\nusage: changelog <mdtname> [startrec [endrec]]"},
596 {"changelog_clear", lfs_changelog_clear, 0,
597 "Indicate that old changelog records up to <endrec> are no longer of "
598 "interest to consumer <id>, allowing the system to free up space.\n"
599 "An <endrec> of 0 means all records.\n"
600 "usage: changelog_clear <mdtname> <id> <endrec>"},
601 {"fid2path", lfs_fid2path, 0,
602 "Resolve the full path(s) for given FID(s). For a specific hardlink "
603 "specify link number <linkno>.\n"
604 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
605 "<fsname|root> <fid>..."},
606 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
607 "usage: path2fid [--parents] <path> ..."},
608 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
609 "usage: rmfid <fsname|rootpath> <fid> ..."},
610 {"data_version", lfs_data_version, 0, "Display file data version for "
611 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
612 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
613 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
614 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
615 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
616 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
617 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
619 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
620 "[--archived] [--lost] <file> ..."},
621 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
622 "given files.\n" "usage: hsm_action <file> ..."},
623 {"hsm_archive", lfs_hsm_archive, 0,
624 "Archive file to external storage.\n"
625 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
627 {"hsm_restore", lfs_hsm_restore, 0,
628 "Restore file from external storage.\n"
629 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
630 {"hsm_release", lfs_hsm_release, 0,
631 "Release files from Lustre.\n"
632 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
633 {"hsm_remove", lfs_hsm_remove, 0,
634 "Remove file copy from external storage.\n"
635 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
637 " (FILE [FILE ...] | "
638 "--mntpath MOUNTPATH FID [FID ...])\n"
640 "Note: To remove an archived copy of a file already deleted from a "
642 "--mntpath option and a list of FIDs must be specified"
644 {"hsm_cancel", lfs_hsm_cancel, 0,
645 "Cancel requests related to specified files.\n"
646 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
647 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
648 "usage: swap_layouts <path1> <path2>"},
649 {"migrate", lfs_setstripe_migrate, 0,
650 "migrate a directory between MDTs.\n"
651 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
652 " [--mdt-hash|-H] <hash_type>\n"
653 " [--mdt-index|-m] <start_mdt_index>\n"
656 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
657 " it's the MDT index of first stripe\n"
658 "\tmdt_count: number of MDTs to stripe a directory over\n"
659 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
660 " all_char (type 1)sum of characters % MDT_COUNT\n"
661 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
662 " crush (type 3)CRUSH hash algorithm\n"
664 "migrate file objects from one OST "
665 "layout\nto another (may be not safe with concurent writes).\n"
667 "[--stripe-count|-c] <stripe_count>\n"
668 "[--overstripe-count|-C] <stripe_count>\n"
669 " [--stripe-index|-i] <start_ost_index>\n"
670 " [--stripe-size|-S] <stripe_size>\n"
671 " [--pool|-p] <pool_name>\n"
672 " [--ost|-o] <ost_indices>\n"
674 " [--non-block|-n]\n"
675 " [--non-direct|-D]\n"
676 " <file|directory>\n"
677 "\tstripe_count: number of OSTs to stripe a file over\n"
678 "\t Using -C instead of -c allows overstriping, which\n"
679 "\t will place more than one stripe per OST if\n"
680 "\t stripe_count is greater than the number of OSTs\n"
681 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
682 "\tstripe_size: number of bytes to store before moving to the next OST\n"
683 "\tpool_name: name of the predefined pool of OSTs\n"
684 "\tost_indices: OSTs to stripe over, in order\n"
685 "\tblock: Block file access during data migration (default)\n"
686 "\tnon-block: Abort migrations if concurrent access is detected\n"
687 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
689 "To move directories between MDTs. This command is deprecated, "
690 "use \"migrate\" instead.\n"
691 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
693 {"ladvise", lfs_ladvise, 0,
694 "Provide servers with advice about access patterns for a file.\n"
695 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
696 " [--background|-b] [--unset|-u]\n\n"
697 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
698 " {[--mode|-m [READ,WRITE]}\n"
700 {"mirror", lfs_mirror, mirror_cmdlist,
701 "lfs commands used to manage files with mirrored components:\n"
702 "lfs mirror create - create a mirrored file or directory\n"
703 "lfs mirror extend - add mirror(s) to an existing file\n"
704 "lfs mirror split - split a mirror from an existing mirrored file\n"
705 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
706 "lfs mirror read - read a mirror content of a mirrored file\n"
707 "lfs mirror write - write to a mirror of a mirrored file\n"
708 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
709 "lfs mirror verify - verify mirrored file(s)\n"},
710 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
711 "usage: getsom [-s] [-b] [-f] <path>\n"
712 "\t-s: Only show the size value of the SOM data for a given file\n"
713 "\t-b: Only show the blocks value of the SOM data for a given file\n"
714 "\t-f: Only show the flags value of the SOM data for a given file\n"},
715 {"heat_get", lfs_heat_get, 0,
716 "To get heat of files.\n"
717 "usage: heat_get <file> ...\n"},
718 {"heat_set", lfs_heat_set, 0,
719 "To set heat flags of files.\n"
720 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
721 "\t--clear|-c: Clear file heat for given files\n"
722 "\t--off|-o: Turn off file heat for given files\n"
723 "\t--on|-O: Turn on file heat for given files\n"},
724 {"pcc", lfs_pcc, pcc_cmdlist,
725 "lfs commands used to interact with PCC features:\n"
726 "lfs pcc attach - attach given files to Persistent Client Cache\n"
727 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
728 "lfs pcc state - display the PCC state for given files\n"
729 "lfs pcc detach - detach given files from Persistent Client Cache\n"
730 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
731 {"help", Parser_help, 0, "help"},
732 {"exit", Parser_quit, 0, "quit"},
733 {"quit", Parser_quit, 0, "quit"},
734 {"--version", Parser_version, 0,
735 "output build version of the utility and exit"},
736 {"--list-commands", lfs_list_commands, 0,
737 "list commands supported by the utility and exit"},
741 static int check_hashtype(const char *hashtype)
743 int type_num = atoi(hashtype);
746 /* numeric hash type */
747 if (hashtype && strlen(hashtype) == 1 &&
748 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
750 /* string hash type */
751 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
752 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
758 static uint32_t check_foreign_type_name(const char *foreign_type_name)
762 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
763 if (!lu_foreign_types[i].lft_name)
765 if (strcmp(foreign_type_name,
766 lu_foreign_types[i].lft_name) == 0)
767 return lu_foreign_types[i].lft_type;
770 return LU_FOREIGN_TYPE_UNKNOWN;
773 static const char *error_loc = "syserror";
776 MIGRATION_NONBLOCK = 0x0001,
777 MIGRATION_MIRROR = 0x0002,
778 MIGRATION_NONDIRECT = 0x0004,
779 MIGRATION_VERBOSE = 0x0008,
783 migrate_open_files(const char *name, __u64 migration_flags,
784 const struct llapi_stripe_param *param,
785 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
792 char parent[PATH_MAX];
793 char volatile_file[PATH_MAX];
799 if (!param && !layout) {
800 error_loc = "layout information";
804 /* search for file directory pathname */
805 if (strlen(name) > sizeof(parent) - 1) {
806 error_loc = "source file name";
810 strncpy(parent, name, sizeof(parent));
811 ptr = strrchr(parent, '/');
813 if (!getcwd(parent, sizeof(parent))) {
814 error_loc = "getcwd";
818 if (ptr == parent) /* leading '/' */
823 /* open file, direct io */
824 /* even if the file is only read, WR mode is nedeed to allow
828 if (!(migration_flags & MIGRATION_NONDIRECT))
830 fd = open(name, rflags);
833 error_loc = "cannot open source file";
837 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
839 error_loc = "cannot get MDT index";
844 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
845 mode_t open_mode = S_IRUSR | S_IWUSR;
847 random_value = random();
848 rc = snprintf(volatile_file, sizeof(volatile_file),
849 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
850 mdt_index, random_value);
851 if (rc >= sizeof(volatile_file)) {
856 /* create, open a volatile file, use caching (ie no directio) */
858 /* Returns -1 and sets errno on error: */
859 fdv = llapi_layout_file_open(volatile_file, open_flags,
864 /* Does the right thing on error: */
865 fdv = llapi_file_open_param(volatile_file, open_flags,
868 } while (fdv < 0 && (rc = fdv) == -EEXIST);
871 error_loc = "cannot create volatile file";
876 * In case the MDT does not support creation of volatile files
877 * we should try to unlink it.
879 (void)unlink(volatile_file);
882 * Not-owner (root?) special case.
883 * Need to set owner/group of volatile file like original.
884 * This will allow to pass related check during layout_swap.
889 error_loc = "cannot stat source file";
893 rc = fstat(fdv, &stv);
896 error_loc = "cannot stat volatile";
900 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
901 rc = fchown(fdv, st.st_uid, st.st_gid);
904 error_loc = "cannot change ownwership of volatile";
923 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
925 struct llapi_layout *layout;
926 size_t buf_size = 4 * 1024 * 1024;
930 size_t page_size = sysconf(_SC_PAGESIZE);
934 layout = llapi_layout_get_by_fd(fd_src, 0);
936 uint64_t stripe_size;
938 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
940 buf_size = stripe_size;
942 llapi_layout_free(layout);
945 /* Use a page-aligned buffer for direct I/O */
946 rc = posix_memalign(&buf, page_size, buf_size);
950 sparse = llapi_file_is_sparse(fd_src);
952 rc = ftruncate(fd_dst, pos);
961 size_t to_read, to_write;
964 if (sparse && pos >= data_end) {
967 data_off = llapi_data_seek(fd_src, pos, &data_size);
969 /* Non-fatal, switch to full copy */
973 /* hole at the end of file, truncate up to it */
975 rc = ftruncate(fd_dst, data_off);
979 pos = data_off & ~(page_size - 1);
980 data_end = data_off + data_size;
981 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
982 to_read = MIN(to_read, buf_size);
988 rc = check_file(fd_src);
993 rsize = pread(fd_src, buf, to_read, pos);
1003 while (to_write > 0) {
1006 written = pwrite(fd_dst, buf, to_write, pos);
1012 to_write -= written;
1014 if (rc || rsize < to_read)
1022 /* Try to avoid page cache pollution after migration. */
1023 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
1024 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1030 static int migrate_copy_timestamps(int fd, int fdv)
1034 if (fstat(fd, &st) == 0) {
1035 struct timeval tv[2] = {
1036 {.tv_sec = st.st_atime},
1037 {.tv_sec = st.st_mtime}
1040 return futimes(fdv, tv);
1046 static int migrate_block(int fd, int fdv)
1053 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1055 error_loc = "cannot get dataversion";
1064 * The grouplock blocks all concurrent accesses to the file.
1065 * It has to be taken after llapi_get_data_version as it would
1068 rc = llapi_group_lock(fd, gid);
1070 error_loc = "cannot get group lock";
1074 rc = migrate_copy_data(fd, fdv, NULL);
1076 error_loc = "data copy failed";
1080 /* Make sure we keep original atime/mtime values */
1081 rc = migrate_copy_timestamps(fd, fdv);
1083 error_loc = "timestamp copy failed";
1089 * for a migration we need to check data version on file did
1092 * Pass in gid=0 since we already own grouplock.
1094 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1095 SWAP_LAYOUTS_CHECK_DV1);
1096 if (rc == -EAGAIN) {
1097 error_loc = "file changed";
1099 } else if (rc < 0) {
1100 error_loc = "cannot swap layout";
1105 rc2 = llapi_group_unlock(fd, gid);
1106 if (rc2 < 0 && rc == 0) {
1107 error_loc = "unlock group lock";
1115 * Internal helper for migrate_copy_data(). Check lease and report error if
1118 * \param[in] fd File descriptor on which to check the lease.
1120 * \retval 0 Migration can keep on going.
1121 * \retval -errno Error occurred, abort migration.
1123 static int check_lease(int fd)
1127 rc = llapi_lease_check(fd);
1129 return 0; /* llapi_check_lease returns > 0 on success. */
1134 static int migrate_nonblock(int fd, int fdv)
1140 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1142 error_loc = "cannot get data version";
1146 rc = migrate_copy_data(fd, fdv, check_lease);
1148 error_loc = "data copy failed";
1152 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1154 error_loc = "cannot get data version";
1160 error_loc = "source file changed";
1164 /* Make sure we keep original atime/mtime values */
1165 rc = migrate_copy_timestamps(fd, fdv);
1167 error_loc = "timestamp copy failed";
1175 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1177 struct pool_to_id_cbdata data = { .pool = pool };
1178 struct llapi_layout *layout = NULL;
1181 layout = llapi_layout_get_by_path(fname, 0);
1184 "error %s: file '%s' couldn't get layout: rc=%d\n",
1185 progname, fname, errno);
1189 rc = llapi_layout_sanity(layout, false, true);
1191 llapi_layout_sanity_perror(errno);
1194 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1203 llapi_layout_free(layout);
1207 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1208 __u32 flags, __u32 neg_flags)
1211 __u32 flags_array[2];
1218 "error %s: neither component id nor pool is specified\n",
1222 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1228 ids[count] = comp_id;
1229 flags_array[count] = flags;
1234 if (neg_flags & LCME_FL_STALE) {
1236 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1241 ids[count] = comp_id;
1242 flags_array[count] = neg_flags | LCME_FL_NEG;
1246 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1248 if (errno == EUCLEAN) {
1251 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1252 progname, comp_id, fname);
1255 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1256 progname, comp_id, fname, flags, neg_flags);
1263 static int lfs_component_del(char *fname, __u32 comp_id,
1264 __u32 flags, __u32 neg_flags)
1268 if (flags && neg_flags)
1271 if (!flags && neg_flags)
1272 flags = neg_flags | LCME_FL_NEG;
1274 if ((flags && comp_id) || (!flags && !comp_id))
1278 if (flags & ~LCME_KNOWN_FLAGS) {
1280 "%s setstripe: unknown flags %#x\n",
1284 } else if (comp_id > LCME_ID_MAX) {
1285 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1290 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1293 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1294 progname, comp_id, fname, strerror(errno));
1298 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1305 rc = llapi_layout_file_comp_add(fname, layout);
1307 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1308 fname, strerror(errno));
1312 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1313 struct llapi_layout *layout)
1321 fd = lstat(fname, &st);
1322 if (fd == 0 && S_ISDIR(st.st_mode))
1323 open_flags = O_DIRECTORY | O_RDONLY;
1325 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1327 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1328 S_ISDIR(st.st_mode) ?
1329 "set default composite layout for" :
1330 "create composite file",
1331 fname, strerror(errno));
1335 static int lfs_migrate(char *name, __u64 migration_flags,
1336 struct llapi_stripe_param *param,
1337 struct llapi_layout *layout)
1339 struct llapi_layout *existing;
1340 uint64_t dom_new, dom_cur;
1345 rc = migrate_open_files(name, migration_flags, param, layout,
1350 rc = llapi_layout_dom_size(layout, &dom_new);
1352 error_loc = "cannot get new layout DoM size";
1355 /* special case for migration to DOM layout*/
1356 existing = llapi_layout_get_by_fd(fd, 0);
1358 error_loc = "cannot get existing layout";
1362 rc = llapi_layout_dom_size(existing, &dom_cur);
1364 error_loc = "cannot get current layout DoM size";
1369 * if file has DoM layout already then migration is possible to
1370 * the new layout with the same DoM component via swap layout,
1371 * if new layout used bigger DOM size, then mirroring is used
1373 if (dom_new > dom_cur) {
1374 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1377 error_loc = "cannot migrate to DOM layout";
1381 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1383 * Blocking mode (forced if servers do not support file lease).
1384 * It is also the default mode, since we cannot distinguish
1385 * between a broken lease and a server that does not support
1386 * atomic swap/close (LU-6785)
1388 rc = migrate_block(fd, fdv);
1392 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1394 error_loc = "cannot get lease";
1398 rc = migrate_nonblock(fd, fdv);
1400 llapi_lease_release(fd);
1405 * Atomically put lease, swap layouts and close.
1406 * for a migration we need to check data version on file did
1409 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1411 error_loc = "cannot swap layout";
1423 fprintf(stderr, "error: %s: %s: %s: %s\n",
1424 progname, name, error_loc, strerror(-rc));
1425 else if (migration_flags & MIGRATION_VERBOSE)
1426 printf("%s\n", name);
1431 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1434 char *dup_string = NULL;
1440 if (!string || !string[0])
1443 dup_string = strdup(string);
1445 llapi_printf(LLAPI_MSG_ERROR,
1446 "%s: insufficient memory\n",
1451 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1455 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1456 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1457 const char *comp_name = comp_flags_table[i].cfn_name;
1459 if (strcmp(name, comp_name) == 0) {
1460 *flags |= comp_flag;
1462 } else if (strncmp(name, "^", 1) == 0 &&
1463 strcmp(name + 1, comp_name) == 0) {
1464 *neg_flags |= comp_flag;
1469 llapi_printf(LLAPI_MSG_ERROR,
1470 "%s: component flag '%s' not supported\n",
1477 if (!*flags && !*neg_flags)
1480 /* don't allow to set and exclude the same flag */
1481 if (*flags & *neg_flags)
1489 static int mdthash_input(char *string, __u32 *inflags,
1490 __u32 *exflags, __u32 *type)
1497 {"migrating", LMV_HASH_FLAG_MIGRATION},
1498 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1499 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1508 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1512 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1513 if (strcmp(name, mhflist[i].name) == 0 ||
1514 name[0] == mhflist[i].name[0]) {
1515 *inflags |= mhflist[i].flag;
1517 } else if (name[0] == '^' &&
1518 (strcmp(name + 1, mhflist[i].name) == 0 ||
1519 name[1] == mhflist[i].name[0])) {
1520 *exflags |= mhflist[i].flag;
1525 i = check_hashtype(name);
1530 llapi_printf(LLAPI_MSG_ERROR,
1531 "%s: invalid mdt_hash value '%s'\n",
1537 /* don't allow to include and exclude the same flag */
1538 if (*inflags & *exflags) {
1539 llapi_printf(LLAPI_MSG_ERROR,
1540 "%s: include and exclude same flag '%s'\n",
1548 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1556 if (strncmp(string, "^", 1) == 0) {
1557 *neg_state = llapi_layout_string_flags(string + 1);
1558 if (*neg_state != 0)
1561 *state = llapi_layout_string_flags(string);
1566 llapi_printf(LLAPI_MSG_ERROR,
1567 "%s: mirrored file state '%s' not supported\n",
1573 * struct mirror_args - Command-line arguments for mirror(s).
1574 * @m_count: Number of mirrors to be created with this layout.
1575 * @m_flags: Mirror level flags, only 'prefer' is supported.
1576 * @m_layout: Mirror layout.
1577 * @m_file: A victim file. Its layout will be split and used as a mirror.
1578 * @m_next: Point to the next node of the list.
1580 * Command-line arguments for mirror(s) will be parsed and stored in
1581 * a linked list that consists of this structure.
1583 struct mirror_args {
1586 struct llapi_layout *m_layout;
1588 struct mirror_args *m_next;
1593 * enum mirror_flags - Flags for extending a mirrored file.
1594 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1595 * in case the victim file(s) contains the same data as the
1596 * original mirrored file.
1597 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1598 * @MF_COMP_ID: specified component id instead of mirror id
1600 * Flags for extending a mirrored file.
1610 * mirror_create_sanity_check() - Check mirror list.
1611 * @list: A linked list that stores the mirror arguments.
1613 * This function does a sanity check on @list for creating
1616 * Return: 0 on success or a negative error code on failure.
1618 static int mirror_create_sanity_check(const char *fname,
1619 struct mirror_args *list)
1622 bool has_m_file = false;
1623 bool has_m_layout = false;
1629 struct llapi_layout *layout;
1631 layout = llapi_layout_get_by_path(fname, 0);
1634 "error: %s: file '%s' couldn't get layout\n",
1639 rc = llapi_layout_sanity(layout, false, true);
1641 llapi_layout_free(layout);
1644 llapi_layout_sanity_perror(rc);
1652 llapi_layout_free(list->m_layout);
1655 llapi_layout_get_by_path(list->m_file, 0);
1656 if (!list->m_layout) {
1658 "error: %s: file '%s' has no layout\n",
1659 progname, list->m_file);
1663 has_m_layout = true;
1664 if (!list->m_layout) {
1665 fprintf(stderr, "error: %s: no mirror layout\n",
1671 rc = llapi_layout_sanity(list->m_layout, false, true);
1673 llapi_layout_sanity_perror(rc);
1677 list = list->m_next;
1680 if (has_m_file && has_m_layout) {
1682 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1690 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1692 __u32 mirror_flags = *(__u32 *)cbdata;
1696 rc = llapi_layout_comp_flags_get(layout, &flags);
1701 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1706 return LLAPI_LAYOUT_ITER_CONT;
1710 * mirror_create() - Create a mirrored file.
1711 * @fname: The file to be created.
1712 * @mirror_list: A linked list that stores the mirror arguments.
1714 * This function creates a mirrored file @fname with the mirror(s)
1715 * from @mirror_list.
1717 * Return: 0 on success or a negative error code on failure.
1719 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1721 struct llapi_layout *layout = NULL;
1722 struct mirror_args *cur_mirror = NULL;
1723 uint16_t mirror_count = 0;
1727 rc = mirror_create_sanity_check(NULL, mirror_list);
1731 cur_mirror = mirror_list;
1732 while (cur_mirror) {
1733 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1735 &cur_mirror->m_flags);
1738 fprintf(stderr, "%s: failed to set mirror flags\n",
1743 for (i = 0; i < cur_mirror->m_count; i++) {
1744 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1748 "error: %s: merge layout failed: %s\n",
1749 progname, strerror(errno));
1753 mirror_count += cur_mirror->m_count;
1754 cur_mirror = cur_mirror->m_next;
1758 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1762 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1765 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1766 progname, strerror(errno));
1770 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1778 llapi_layout_free(layout);
1783 * Compare files and check lease on @fd.
1785 * \retval bytes number of bytes are the same
1787 static ssize_t mirror_file_compare(int fd, int fdv)
1789 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1791 ssize_t bytes_done = 0;
1792 ssize_t bytes_read = 0;
1794 buf = malloc(buflen * 2);
1799 if (!llapi_lease_check(fd)) {
1800 bytes_done = -EBUSY;
1804 bytes_read = read(fd, buf, buflen);
1805 if (bytes_read <= 0)
1808 if (bytes_read != read(fdv, buf + buflen, buflen))
1812 * XXX: should compute the checksum on each buffer and then
1813 * compare checksum to avoid cache collision
1815 if (memcmp(buf, buf + buflen, bytes_read))
1818 bytes_done += bytes_read;
1826 static int mirror_extend_file(const char *fname, const char *victim_file,
1827 enum mirror_flags mirror_flags)
1832 struct stat stbuf_v;
1833 struct ll_ioc_lease *data = NULL;
1836 fd = open(fname, O_RDWR);
1838 error_loc = "open source file";
1843 fdv = open(victim_file, O_RDWR);
1845 error_loc = "open target file";
1850 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1851 error_loc = "stat source or target file";
1856 if (stbuf.st_dev != stbuf_v.st_dev) {
1857 error_loc = "stat source and target file";
1862 /* mirrors should be of the same size */
1863 if (stbuf.st_size != stbuf_v.st_size) {
1864 error_loc = "file sizes don't match";
1869 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1871 error_loc = "cannot get lease";
1875 if (!(mirror_flags & MF_NO_VERIFY)) {
1877 /* mirrors should have the same contents */
1878 ret = mirror_file_compare(fd, fdv);
1879 if (ret != stbuf.st_size) {
1880 error_loc = "file busy or contents don't match";
1881 rc = ret < 0 ? ret : -EINVAL;
1886 /* Get rid of caching pages from clients */
1887 rc = llapi_file_flush(fd);
1889 error_loc = "cannot get data version";
1893 rc = llapi_file_flush(fdv);
1895 error_loc = "cannot get data version";
1899 /* Make sure we keep original atime/mtime values */
1900 rc = migrate_copy_timestamps(fd, fdv);
1902 error_loc = "cannot copy timestamp";
1906 /* Atomically put lease, merge layouts and close. */
1907 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1909 error_loc = "memory allocation";
1912 data->lil_mode = LL_LEASE_UNLCK;
1913 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1914 data->lil_count = 1;
1915 data->lil_ids[0] = fdv;
1916 rc = llapi_lease_set(fd, data);
1918 error_loc = "cannot merge layout";
1920 } else if (rc == 0) {
1922 error_loc = "lost lease lock";
1935 (void) unlink(victim_file);
1937 fprintf(stderr, "error: %s: %s: %s: %s\n",
1938 progname, fname, error_loc, strerror(-rc));
1942 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1943 bool inherit, uint32_t flags)
1945 struct llapi_layout *f_layout = NULL;
1946 struct ll_ioc_lease *data = NULL;
1952 f_layout = llapi_layout_get_by_path(name, 0);
1954 fprintf(stderr, "%s: cannot get layout\n", progname);
1957 rc = llapi_layout_get_last_init_comp(f_layout);
1959 fprintf(stderr, "%s: cannot get the last init comp\n",
1963 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1966 "%s: cannot inherit from the last init comp\n",
1971 llapi_layout_comp_flags_set(m_layout, flags);
1972 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1977 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1979 error_loc = "cannot get lease";
1983 rc = migrate_nonblock(fd, fdv);
1985 llapi_lease_release(fd);
1989 /* Atomically put lease, merge layouts and close. */
1990 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1992 error_loc = "memory allocation";
1995 data->lil_mode = LL_LEASE_UNLCK;
1996 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1997 data->lil_count = 1;
1998 data->lil_ids[0] = fdv;
1999 rc = llapi_lease_set(fd, data);
2001 error_loc = "cannot merge layout";
2003 } else if (rc == 0) {
2005 error_loc = "lost lease lock";
2018 fprintf(stderr, "error: %s: %s: %s: %s\n",
2019 progname, name, error_loc, strerror(-rc));
2023 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2024 enum mirror_flags mirror_flags)
2028 rc = mirror_create_sanity_check(fname, mirror_list);
2032 while (mirror_list) {
2033 if (mirror_list->m_file) {
2034 rc = mirror_extend_file(fname, mirror_list->m_file,
2037 __u32 mirror_count = mirror_list->m_count;
2039 while (mirror_count > 0) {
2040 rc = mirror_extend_layout(fname,
2041 mirror_list->m_layout,
2042 mirror_list->m_inherit,
2043 mirror_list->m_flags);
2053 mirror_list = mirror_list->m_next;
2059 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2064 rc = llapi_layout_mirror_id_get(layout, &id);
2068 if ((__u16)id == *(__u16 *)cbdata)
2069 return LLAPI_LAYOUT_ITER_STOP;
2071 return LLAPI_LAYOUT_ITER_CONT;
2074 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2079 rc = llapi_layout_comp_id_get(layout, &id);
2083 if (id == *(__u32 *)cbdata)
2084 return LLAPI_LAYOUT_ITER_STOP;
2086 return LLAPI_LAYOUT_ITER_CONT;
2089 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2091 char buf[LOV_MAXPOOLNAME + 1];
2092 struct pool_to_id_cbdata *d = (void *)cbdata;
2096 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2099 if (strcmp(d->pool, buf))
2100 return LLAPI_LAYOUT_ITER_CONT;
2102 rc = llapi_layout_mirror_id_get(layout, &id);
2107 return LLAPI_LAYOUT_ITER_STOP;
2110 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2112 char buf[LOV_MAXPOOLNAME + 1];
2113 struct pool_to_id_cbdata *d = (void *)cbdata;
2117 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2120 if (strcmp(d->pool, buf))
2121 return LLAPI_LAYOUT_ITER_CONT;
2123 rc = llapi_layout_comp_id_get(layout, &id);
2128 return LLAPI_LAYOUT_ITER_STOP;
2131 struct collect_ids_data {
2137 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2139 struct collect_ids_data *cid = cbdata;
2143 rc = llapi_layout_mirror_id_get(layout, &id);
2147 if ((__u16)id != cid->cid_exclude) {
2150 for (i = 0; i < cid->cid_count; i++) {
2151 /* already collected the mirror id */
2152 if (id == cid->cid_ids[i])
2153 return LLAPI_LAYOUT_ITER_CONT;
2155 cid->cid_ids[cid->cid_count] = id;
2159 return LLAPI_LAYOUT_ITER_CONT;
2163 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2164 * @mirror_id: Mirror id to be checked.
2165 * @layout: Mirror component list.
2167 * This function checks if a mirror with specified @mirror_id is the last
2168 * non-stale mirror of a layout @layout.
2170 * Return: true or false.
2173 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2175 __u16 mirror_ids[128] = { 0 };
2176 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2178 .cid_exclude = mirror_id, };
2181 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2183 for (i = 0; i < cid.cid_count; i++) {
2184 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2187 comp_size = llapi_mirror_find_stale(layout, comp_array,
2188 ARRAY_SIZE(comp_array),
2197 static int mirror_split(const char *fname, __u32 id, const char *pool,
2198 enum mirror_flags mflags, const char *victim_file)
2200 struct llapi_layout *layout;
2201 char parent[PATH_MAX];
2202 char victim[PATH_MAX];
2203 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2205 struct ll_ioc_lease *data;
2206 uint16_t mirror_count;
2212 /* check fname contains mirror with mirror_id/comp_id */
2213 layout = llapi_layout_get_by_path(fname, 0);
2216 "error %s: file '%s' couldn't get layout\n",
2221 rc = llapi_layout_sanity(layout, false, true);
2223 llapi_layout_sanity_perror(rc);
2227 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2230 "error %s: file '%s' couldn't get mirror count\n",
2234 if (mirror_count < 2) {
2236 "error %s: file '%s' has %d component, cannot split\n",
2237 progname, fname, mirror_count);
2241 if (mflags & MF_COMP_POOL) {
2242 struct pool_to_id_cbdata data = { .pool = pool };
2244 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2246 mirror_id = data.id;
2247 } else if (mflags & MF_COMP_ID) {
2248 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2249 mirror_id = mirror_id_of(id);
2251 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2255 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2258 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2259 if (mflags & MF_COMP_POOL) {
2261 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2262 progname, fname, pool);
2264 } else if (mflags & MF_COMP_ID) {
2266 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2267 progname, fname, id);
2271 "error %s: file '%s' does not contain mirror with id %u\n",
2272 progname, fname, id);
2277 fd = open(fname, O_RDWR);
2280 "error %s: open file '%s' failed: %s\n",
2281 progname, fname, strerror(errno));
2285 /* get victim file directory pathname */
2286 if (strlen(fname) > sizeof(parent) - 1) {
2287 fprintf(stderr, "error %s: file name of '%s' too long\n",
2292 strncpy(parent, fname, sizeof(parent));
2293 ptr = strrchr(parent, '/');
2295 if (!getcwd(parent, sizeof(parent))) {
2296 fprintf(stderr, "error %s: getcwd failed: %s\n",
2297 progname, strerror(errno));
2307 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2309 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2315 /* use a temp file to store the splitted layout */
2316 if (mflags & MF_DESTROY) {
2317 if (last_non_stale_mirror(mirror_id, layout)) {
2320 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2325 fdv = llapi_create_volatile_idx(parent, mdt_index,
2326 O_LOV_DELAY_CREATE);
2328 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2330 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2333 /* user specified victim file */
2334 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2339 "error %s: create victim file failed: %s\n",
2340 progname, strerror(errno));
2344 /* get lease lock of fname */
2345 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2348 "error %s: cannot get lease of file '%s': %d\n",
2349 progname, fname, rc);
2353 /* Atomatically put lease, split layouts and close. */
2354 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2360 data->lil_mode = LL_LEASE_UNLCK;
2361 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2362 data->lil_count = 2;
2363 data->lil_ids[0] = fdv;
2364 data->lil_ids[1] = mirror_id;
2365 rc = llapi_lease_set(fd, data);
2367 if (rc == 0) /* lost lease lock */
2370 "error %s: cannot split '%s': %s\n",
2371 progname, fname, strerror(-rc));
2382 llapi_layout_free(layout);
2387 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2388 __u16 *mirror_ids, int ids_nr);
2390 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2391 __u64 migration_flags,
2392 struct llapi_stripe_param *param,
2393 struct llapi_layout *layout)
2395 struct ll_ioc_lease *data = NULL;
2398 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2400 error_loc = "cannot get lease";
2404 /* Atomically put lease, merge layouts, resync and close. */
2405 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2407 error_loc = "memory allocation";
2410 data->lil_mode = LL_LEASE_UNLCK;
2411 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2412 data->lil_count = 1;
2413 data->lil_ids[0] = fdv;
2414 rc = llapi_lease_set(fd, data);
2416 error_loc = "cannot merge layout";
2418 } else if (rc == 0) {
2420 error_loc = "lost lease lock";
2426 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2428 error_loc = "cannot resync file";
2432 /* delete first mirror now */
2433 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2435 error_loc = "cannot delete old layout";
2443 fprintf(stderr, "error: %s: %s: %s: %s\n",
2444 progname, name, error_loc, strerror(-rc));
2445 else if (migration_flags & MIGRATION_VERBOSE)
2446 printf("%s\n", name);
2453 * Parse a string containing an target index list into an array of integers.
2455 * The input string contains a comma delimited list of individual
2456 * indices and ranges, for example "1,2-4,7". Add the indices into the
2457 * \a tgts array and remove duplicates.
2459 * \param[out] tgts array to store indices in
2460 * \param[in] size size of \a tgts array
2461 * \param[in] offset starting index in \a tgts
2462 * \param[in] arg string containing OST index list
2463 * \param[in/out] overstriping index list may contain duplicates
2465 * \retval positive number of indices in \a tgts
2466 * \retval -EINVAL unable to parse \a arg
2468 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2469 unsigned long long *pattern)
2473 int slots = size - offset;
2475 bool overstriped = false;
2481 end_of_loop = false;
2482 while (!end_of_loop) {
2483 int start_index = 0;
2486 char *endptr = NULL;
2490 ptr = strchrnul(arg, ',');
2492 end_of_loop = *ptr == '\0';
2496 start_index = strtol(arg, &endptr, 0);
2497 if (endptr == arg) /* no data at all */
2499 if (errno != 0 || start_index < -1 ||
2500 (*endptr != '-' && *endptr != '\0'))
2503 end_index = start_index;
2504 if (*endptr == '-') {
2506 end_index = strtol(endptr + 1, &endptr, 0);
2507 if (errno != 0 || *endptr != '\0' || end_index < -1)
2509 if (end_index < start_index)
2513 for (i = start_index; i <= end_index && slots > 0; i++) {
2516 /* remove duplicate */
2517 for (j = 0; j < offset; j++) {
2518 if (tgts[j] == i && pattern &&
2519 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2521 else if (tgts[j] == i)
2527 if (j == offset) { /* check complete */
2533 if (slots == 0 && i < end_index)
2541 if (!end_of_loop && ptr)
2544 if (!overstriped && pattern)
2545 *pattern = LLAPI_LAYOUT_DEFAULT;
2547 return rc < 0 ? rc : nr;
2550 struct lfs_setstripe_args {
2551 unsigned long long lsa_comp_end;
2552 unsigned long long lsa_stripe_size;
2553 unsigned long long lsa_extension_size;
2554 long long lsa_stripe_count;
2555 long long lsa_stripe_off;
2556 __u32 lsa_comp_flags;
2557 __u32 lsa_comp_neg_flags;
2558 unsigned long long lsa_pattern;
2559 unsigned int lsa_mirror_count;
2561 bool lsa_first_comp;
2562 bool lsa_extension_comp;
2564 char *lsa_pool_name;
2567 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2569 unsigned int mirror_count = lsa->lsa_mirror_count;
2570 bool first_comp = lsa->lsa_first_comp;
2572 memset(lsa, 0, sizeof(*lsa));
2574 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2575 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2576 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2577 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2578 lsa->lsa_pool_name = NULL;
2580 lsa->lsa_mirror_count = mirror_count;
2581 lsa->lsa_first_comp = first_comp;
2585 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2586 * @lsa: Stripe options to be initialized and inherited.
2588 * This function initializes stripe options in @lsa and inherit
2589 * stripe_size, stripe_count and OST pool_name options.
2593 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2595 unsigned long long stripe_size;
2596 long long stripe_count;
2597 char *pool_name = NULL;
2599 stripe_size = lsa->lsa_stripe_size;
2600 stripe_count = lsa->lsa_stripe_count;
2601 pool_name = lsa->lsa_pool_name;
2603 setstripe_args_init(lsa);
2605 lsa->lsa_stripe_size = stripe_size;
2606 lsa->lsa_stripe_count = stripe_count;
2607 lsa->lsa_pool_name = pool_name;
2610 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2612 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2613 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2614 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2615 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2616 lsa->lsa_comp_end != 0);
2620 * comp_args_to_layout() - Create or extend a composite layout.
2621 * @composite: Pointer to the composite layout.
2622 * @lsa: Stripe options for the new component.
2624 * This function creates or extends a composite layout by adding a new
2625 * component with stripe options from @lsa.
2627 * Return: 0 on success or an error code on failure.
2629 static int comp_args_to_layout(struct llapi_layout **composite,
2630 struct lfs_setstripe_args *lsa,
2633 struct llapi_layout *layout = *composite;
2634 uint64_t prev_end = 0;
2640 layout = llapi_layout_alloc();
2642 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2647 *composite = layout;
2648 lsa->lsa_first_comp = true;
2653 * Get current component extent, current component
2654 * must be the tail component.
2656 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2658 fprintf(stderr, "Get comp extent failed. %s\n",
2663 if (lsa->lsa_first_comp) {
2665 rc = llapi_layout_add_first_comp(layout);
2667 rc = llapi_layout_comp_add(layout);
2670 fprintf(stderr, "Add component failed. %s\n",
2676 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2678 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2679 lsa->lsa_comp_flags, strerror(errno));
2684 uint64_t comp_end = lsa->lsa_comp_end;
2687 * The extendable component is 0-length, so it can be removed
2688 * if there is insufficient space to extend it.
2690 if (lsa->lsa_extension_comp)
2691 comp_end = prev_end;
2693 rc = llapi_layout_comp_extent_set(layout, prev_end,
2696 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2697 prev_end, comp_end, strerror(errno));
2701 /* reset lsa_first_comp */
2702 lsa->lsa_first_comp = false;
2704 /* Data-on-MDT component setting */
2705 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2707 * In case of Data-on-MDT patterns the only extra option
2708 * applicable is stripe size option.
2710 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2712 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2713 lsa->lsa_stripe_count);
2717 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2719 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2720 lsa->lsa_stripe_size);
2724 if (lsa->lsa_nr_tgts != 0) {
2726 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2731 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2733 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2734 lsa->lsa_stripe_off);
2738 if (lsa->lsa_pool_name != 0) {
2740 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2741 lsa->lsa_pool_name);
2746 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2748 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2753 /* Data-on-MDT component has always single stripe up to end */
2754 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2755 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2756 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2758 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2765 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2766 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2768 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2769 rc = llapi_layout_extension_size_set(layout, size);
2771 rc = llapi_layout_stripe_size_set(layout, size);
2774 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2775 size, strerror(errno));
2779 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2781 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2782 lsa->lsa_stripe_count, strerror(errno));
2786 if (lsa->lsa_pool_name) {
2787 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2789 fprintf(stderr, "Set pool name: %s failed. %s\n",
2790 lsa->lsa_pool_name, strerror(errno));
2794 rc = llapi_layout_pool_name_set(layout, "");
2796 fprintf(stderr, "Clear pool name failed: %s\n",
2802 if (lsa->lsa_nr_tgts > 0) {
2803 if (lsa->lsa_stripe_count > 0 &&
2804 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2805 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2806 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2807 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2808 lsa->lsa_stripe_count,
2813 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2814 rc = llapi_layout_ost_index_set(layout, i,
2819 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2820 lsa->lsa_stripe_off != -1) {
2821 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2824 fprintf(stderr, "Set ost index %d failed. %s\n",
2825 i, strerror(errno));
2829 /* Create the second, virtual component of extension space */
2830 if (lsa->lsa_extension_comp) {
2831 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2832 lsa->lsa_extension_comp = false;
2839 static int build_component(struct llapi_layout **layout,
2840 struct lfs_setstripe_args *lsa, bool set_extent)
2844 rc = comp_args_to_layout(layout, lsa, set_extent);
2848 if (lsa->lsa_mirror_count > 0) {
2849 rc = llapi_layout_mirror_count_set(*layout,
2850 lsa->lsa_mirror_count);
2854 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2857 lsa->lsa_mirror_count = 0;
2863 static int build_prev_component(struct llapi_layout **layout,
2864 struct lfs_setstripe_args *prev,
2865 struct lfs_setstripe_args *lsa,
2868 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2871 if (prev->lsa_stripe_size) {
2873 prev->lsa_comp_end = lsa->lsa_comp_end;
2874 prev->lsa_extension_size = lsa->lsa_extension_size;
2875 prev->lsa_extension_comp = true;
2878 rc = build_component(layout, prev, true);
2884 * Copy lsa to previous lsa;
2885 * if this is an extension component, make the previous invalid;
2888 prev->lsa_stripe_size = 0;
2895 static int build_layout_from_yaml_node(struct cYAML *node,
2896 struct llapi_layout **layout,
2897 struct lfs_setstripe_args *lsa,
2898 struct lfs_setstripe_args *prevp)
2900 struct lfs_setstripe_args prev = { 0 };
2901 __u32 *osts = lsa->lsa_tgts;
2909 string = node->cy_string;
2911 if (node->cy_type == CYAML_TYPE_OBJECT) {
2912 /* go deep to sub blocks */
2913 if (string && !strncmp(string, "component", 9) &&
2914 strncmp(string, "component0", 10) &&
2915 strncmp(string, "components", 10)) {
2916 rc = build_prev_component(layout, prevp, lsa,
2921 /* initialize lsa. */
2922 setstripe_args_init(lsa);
2923 lsa->lsa_first_comp = false;
2924 lsa->lsa_tgts = osts;
2927 rc = build_layout_from_yaml_node(node->cy_child, layout,
2932 if (!node->cy_string)
2935 /* skip leading lmm_ if present, to simplify parsing */
2936 if (strncmp(string, "lmm_", 4) == 0)
2939 if (node->cy_type == CYAML_TYPE_STRING) {
2940 if (!strcmp(string, "lcme_extent.e_end")) {
2941 if (!strcmp(node->cy_valuestring, "EOF") ||
2942 !strcmp(node->cy_valuestring, "eof"))
2943 lsa->lsa_comp_end = LUSTRE_EOF;
2944 } else if (!strcmp(string, "pool")) {
2945 lsa->lsa_pool_name = node->cy_valuestring;
2946 } else if (!strcmp(string, "pattern")) {
2947 if (!strcmp(node->cy_valuestring, "mdt"))
2948 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2949 if (!strcmp(node->cy_valuestring,
2950 "raid0,overstriped"))
2952 LLAPI_LAYOUT_OVERSTRIPING;
2953 } else if (!strcmp(string, "lcme_flags")) {
2954 rc = comp_str2flags(node->cy_valuestring,
2955 &lsa->lsa_comp_flags,
2956 &lsa->lsa_comp_neg_flags);
2960 * Only template flags have meaning in
2961 * the layout for a new file
2963 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2965 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2966 if (!strcmp(string, "lcm_mirror_count")) {
2967 lsa->lsa_mirror_count = node->cy_valueint;
2968 } else if (!strcmp(string, "lcme_extent.e_start")) {
2969 if (node->cy_valueint == 0)
2970 lsa->lsa_first_comp = true;
2971 } else if (!strcmp(string, "lcme_extent.e_end")) {
2972 if (node->cy_valueint == -1)
2973 lsa->lsa_comp_end = LUSTRE_EOF;
2975 lsa->lsa_comp_end = node->cy_valueint;
2976 } else if (!strcmp(string, "stripe_count")) {
2977 lsa->lsa_stripe_count = node->cy_valueint;
2978 } else if (!strcmp(string, "stripe_size")) {
2979 lsa->lsa_stripe_size = node->cy_valueint;
2980 } else if (!strcmp(string, "extension_size")) {
2981 lsa->lsa_extension_size = node->cy_valueint;
2982 lsa->lsa_extension_comp = true;
2983 } else if (!strcmp(string, "stripe_offset")) {
2984 lsa->lsa_stripe_off = node->cy_valueint;
2985 } else if (!strcmp(string, "l_ost_idx")) {
2986 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2991 node = node->cy_next;
2994 if (prevp == &prev) {
2995 rc = build_prev_component(layout, prevp, lsa, true);
2999 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3000 rc = build_component(layout, lsa, *layout != NULL);
3006 static int lfs_comp_create_from_yaml(char *template,
3007 struct llapi_layout **layout,
3008 struct lfs_setstripe_args *lsa,
3011 struct cYAML *tree = NULL, *err_rc = NULL;
3014 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3016 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3017 progname, template);
3018 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3019 "can't parse", &err_rc);
3020 cYAML_print_tree2file(stderr, err_rc);
3021 cYAML_free_tree(err_rc);
3026 /* initialize lsa for plain file */
3027 setstripe_args_init(lsa);
3028 lsa->lsa_tgts = osts;
3030 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3032 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3033 progname, template);
3036 /* clean clean lsa */
3037 setstripe_args_init(lsa);
3041 cYAML_free_tree(tree);
3046 * Get the extension size from the next (SEL) component and extend the
3047 * current component on it. The start of the next component is to be
3050 * \param[in] layout the current layout
3051 * \param[in] start the start of the current component
3052 * \param[in,out] end the end of the current component
3053 * \param[in] offset the offset to adjust the end position to instead of
3056 * \retval 0 - extended successfully
3057 * \retval < 0 - error
3059 static int layout_extend_comp(struct llapi_layout *layout,
3060 uint64_t start, uint64_t *end,
3063 uint64_t size, next_start, next_end;
3066 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3069 "%s setstripe: cannot move component cursor: %s\n",
3070 progname, strerror(errno));
3075 * Even if the @size will not be used below, this will fail if
3076 * this is not a SEL component - a good confirmation we are
3077 * working on right components.
3079 rc = llapi_layout_extension_size_get(layout, &size);
3082 "%s setstripe: cannot get component ext size: %s\n",
3083 progname, strerror(errno));
3087 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3089 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3090 progname, strerror(errno));
3094 next_start += offset ?: size;
3095 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3097 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3098 progname, strerror(errno));
3102 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3105 "%s setstripe: cannot move component cursor: %s\n",
3106 progname, strerror(errno));
3110 *end += offset ?: size;
3111 rc = llapi_layout_comp_extent_set(layout, start, *end);
3113 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3114 progname, strerror(errno));
3122 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3123 * end of the last component in the existing file, and adjust the
3124 * first extent start of the components to be added accordingly.
3126 * In the create mode, we need to check if the first component is an extendable
3127 * SEL component and extend its length to the extension size (first component
3128 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3130 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3133 struct llapi_layout *head;
3134 uint64_t start = 0, prev_end = 0;
3138 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3143 head = llapi_layout_get_by_path(fname, 0);
3146 "%s setstripe: cannot read layout from '%s': %s\n",
3147 progname, fname, strerror(errno));
3149 } else if (errno == ENODATA) {
3151 * file without LOVEA, this component-add will be turned
3152 * into a component-create.
3154 llapi_layout_free(head);
3158 * the new layout will be added to an empty one, it
3159 * still needs to be adjusted below
3163 } else if (!llapi_layout_is_composite(head)) {
3165 "%s setstripe: '%s' not a composite file\n",
3167 llapi_layout_free(head);
3171 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3174 "%s setstripe: cannot get prev extent: %s\n",
3175 progname, strerror(errno));
3176 llapi_layout_free(head);
3180 llapi_layout_free(head);
3184 /* Make sure we use the first component of the layout to be added. */
3185 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3188 "%s setstripe: cannot move component cursor: %s\n",
3189 progname, strerror(errno));
3193 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3195 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3196 progname, strerror(errno));
3200 if (start == 0 && end == 0) {
3201 rc = layout_extend_comp(layout, start, &end,
3202 comp_add ? prev_end : 0);
3207 if (start > prev_end || end < prev_end) {
3209 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3210 progname, start, end, prev_end);
3214 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3217 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3218 progname, prev_end, end, strerror(errno));
3225 static int mirror_adjust_first_extents(struct mirror_args *list)
3233 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3236 list = list->m_next;
3242 static inline bool arg_is_eof(char *arg)
3244 return !strncmp(arg, "-1", strlen("-1")) ||
3245 !strncmp(arg, "EOF", strlen("EOF")) ||
3246 !strncmp(arg, "eof", strlen("eof"));
3250 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3252 * Return: Valid mirror_args pointer on success and
3253 * NULL if memory allocation fails.
3255 static struct mirror_args *lfs_mirror_alloc(void)
3257 struct mirror_args *mirror = NULL;
3260 mirror = calloc(1, sizeof(*mirror));
3262 mirror->m_inherit = false;
3273 * lfs_mirror_free() - Free memory allocated for a mirror argument
3275 * @mirror: Previously allocated mirror argument structure by
3276 * lfs_mirror_alloc().
3278 * Free memory allocated for @mirror.
3282 static void lfs_mirror_free(struct mirror_args *mirror)
3284 if (mirror->m_layout)
3285 llapi_layout_free(mirror->m_layout);
3290 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3291 * @mirror_list: Previously allocated mirror list.
3293 * Free memory allocated for @mirror_list.
3297 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3299 struct mirror_args *next_mirror = NULL;
3301 while (mirror_list) {
3302 next_mirror = mirror_list->m_next;
3303 lfs_mirror_free(mirror_list);
3304 mirror_list = next_mirror;
3316 LFS_COMP_NO_VERIFY_OPT,
3318 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3320 LFS_MIRROR_STATE_OPT,
3322 LFS_MIRROR_INDEX_OPT,
3323 LFS_LAYOUT_FOREIGN_OPT,
3329 static int lfs_setstripe_internal(int argc, char **argv,
3330 enum setstripe_origin opc)
3332 struct lfs_setstripe_args lsa = { 0 };
3333 struct llapi_stripe_param *param = NULL;
3334 struct find_param migrate_mdt_param = {
3344 unsigned long long size_units = 1;
3345 bool migrate_mode = false;
3346 bool migrate_mdt_mode = false;
3347 bool setstripe_mode = false;
3348 bool migration_block = false;
3349 __u64 migration_flags = 0;
3350 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3351 int comp_del = 0, comp_set = 0;
3354 struct llapi_layout *layout = NULL;
3355 struct llapi_layout **lpp = &layout;
3356 bool mirror_mode = false;
3357 bool has_m_file = false;
3358 __u32 mirror_count = 0;
3359 enum mirror_flags mirror_flags = 0;
3360 struct mirror_args *mirror_list = NULL;
3361 struct mirror_args *new_mirror = NULL;
3362 struct mirror_args *last_mirror = NULL;
3363 __u16 mirror_id = 0;
3365 bool from_yaml = false;
3366 bool from_copy = false;
3367 char *template = NULL;
3368 bool foreign_mode = false;
3370 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3371 char *mode_opt = NULL;
3372 mode_t previous_umask = 0;
3373 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3375 struct option long_opts[] = {
3376 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3377 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3378 /* --block is only valid in migrate mode */
3379 { .val = 'b', .name = "block", .has_arg = no_argument },
3380 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3381 { .val = LFS_COMP_ADD_OPT,
3382 .name = "comp-add", .has_arg = no_argument },
3383 { .val = LFS_COMP_ADD_OPT,
3384 .name = "component-add", .has_arg = no_argument },
3385 { .val = LFS_COMP_DEL_OPT,
3386 .name = "comp-del", .has_arg = no_argument },
3387 { .val = LFS_COMP_DEL_OPT,
3388 .name = "component-del", .has_arg = no_argument },
3389 { .val = LFS_COMP_FLAGS_OPT,
3390 .name = "comp-flags", .has_arg = required_argument },
3391 { .val = LFS_COMP_FLAGS_OPT,
3392 .name = "component-flags",
3393 .has_arg = required_argument },
3394 { .val = LFS_COMP_SET_OPT,
3395 .name = "comp-set", .has_arg = no_argument },
3396 { .val = LFS_COMP_SET_OPT,
3397 .name = "component-set",
3398 .has_arg = no_argument},
3399 { .val = LFS_COMP_NO_VERIFY_OPT,
3400 .name = "no-verify", .has_arg = no_argument},
3401 { .val = LFS_LAYOUT_FLAGS_OPT,
3402 .name = "flags", .has_arg = required_argument},
3403 { .val = LFS_LAYOUT_FOREIGN_OPT,
3404 .name = "foreign", .has_arg = optional_argument},
3405 { .val = LFS_MIRROR_ID_OPT,
3406 .name = "mirror-id", .has_arg = required_argument},
3407 { .val = LFS_MODE_OPT,
3408 .name = "mode", .has_arg = required_argument},
3409 { .val = LFS_LAYOUT_COPY,
3410 .name = "copy", .has_arg = required_argument},
3411 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3412 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3413 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3414 { .val = 'C', .name = "overstripe-count",
3415 .has_arg = required_argument},
3416 { .val = 'd', .name = "delete", .has_arg = no_argument},
3417 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3418 /* --non-direct is only valid in migrate mode */
3419 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3420 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3421 { .val = 'E', .name = "component-end",
3422 .has_arg = required_argument},
3423 { .val = 'f', .name = "file", .has_arg = required_argument },
3424 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3425 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3426 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3427 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3428 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3429 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3430 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3431 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3432 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3433 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3434 { .val = 'L', .name = "layout", .has_arg = required_argument },
3435 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3436 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3437 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3438 /* --non-block is only valid in migrate mode */
3439 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3440 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3441 { .val = 'o', .name = "ost", .has_arg = required_argument },
3442 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3443 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3444 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3446 { .val = 'p', .name = "pool", .has_arg = required_argument },
3447 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3448 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3449 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3450 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3451 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3452 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3453 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3454 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3455 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3456 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3457 /* --verbose is only valid in migrate mode */
3458 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3459 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3460 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3461 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3462 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3465 setstripe_args_init(&lsa);
3467 migrate_mode = (opc == SO_MIGRATE);
3468 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3469 setstripe_mode = (opc == SO_SETSTRIPE);
3470 if (opc == SO_MIRROR_DELETE) {
3472 mirror_flags = MF_DESTROY;
3475 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3477 while ((c = getopt_long(argc, argv,
3478 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3479 long_opts, NULL)) >= 0) {
3485 case LFS_COMP_ADD_OPT:
3488 case LFS_COMP_DEL_OPT:
3491 case LFS_COMP_FLAGS_OPT:
3492 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3493 &lsa.lsa_comp_neg_flags);
3496 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3498 "%s: inverted flags are not supported\n",
3503 case LFS_COMP_SET_OPT:
3506 case LFS_COMP_NO_VERIFY_OPT:
3507 mirror_flags |= MF_NO_VERIFY;
3509 case LFS_MIRROR_ID_OPT: {
3510 unsigned long int id;
3513 id = strtoul(optarg, &end, 0);
3514 if (errno != 0 || *end != '\0' || id == 0 ||
3517 "%s %s: invalid mirror ID '%s'\n",
3518 progname, argv[0], optarg);
3522 mirror_id = (__u16)id;
3525 case LFS_LAYOUT_FLAGS_OPT: {
3528 /* check for numeric flags (foreign and mirror cases) */
3529 if (setstripe_mode && !mirror_mode && !last_mirror) {
3531 flags = strtoul(optarg, &end, 16);
3532 if (errno != 0 || *end != '\0' ||
3533 flags >= UINT32_MAX) {
3535 "%s %s: invalid hex flags '%s'\n",
3536 progname, argv[0], optarg);
3539 if (!foreign_mode) {
3541 "%s %s: hex flags must be specified with --foreign option\n",
3548 if (!mirror_mode || !last_mirror) {
3550 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3555 result = comp_str2flags(optarg, &last_mirror->m_flags,
3562 "%s: inverted flags are not supported\n",
3567 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3569 "%s: unsupported mirror flags: %s\n",
3576 case LFS_LAYOUT_FOREIGN_OPT:
3578 /* check pure numeric */
3579 type = strtoul(optarg, &end, 0);
3582 type = check_foreign_type_name(optarg);
3583 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3585 "%s %s: unrecognized foreign type '%s'\n",
3590 } else if (type >= UINT32_MAX) {
3592 "%s %s: invalid foreign type '%s'\n",
3593 progname, argv[0], optarg);
3597 foreign_mode = true;
3602 mode = strtoul(mode_opt, &end, 8);
3605 "%s %s: bad mode '%s'\n",
3606 progname, argv[0], mode_opt);
3609 previous_umask = umask(0);
3612 case LFS_LAYOUT_COPY:
3617 if (!migrate_mode) {
3619 "%s %s: -b|--block valid only for migrate command\n",
3623 migration_block = true;
3626 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3628 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3632 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3636 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3637 if (errno != 0 || *end != '\0'||
3638 lsa.lsa_stripe_count < -1 ||
3639 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3641 "%s %s: invalid stripe count '%s'\n",
3642 progname, argv[0], optarg);
3646 if (lsa.lsa_stripe_count == -1)
3647 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3650 /* delete the default striping pattern */
3652 if (opc == SO_MIRROR_SPLIT) {
3655 "%s %s: -d cannot used with -f\n",
3659 mirror_flags |= MF_DESTROY;
3663 if (!migrate_mode) {
3665 "%s %s: -D|--non-direct is valid only for migrate command\n",
3669 migration_flags |= MIGRATION_NONDIRECT;
3672 if (lsa.lsa_comp_end != 0) {
3673 result = comp_args_to_layout(lpp, &lsa, true);
3675 fprintf(stderr, "%s: invalid layout\n",
3680 setstripe_args_init_inherit(&lsa);
3683 if (arg_is_eof(optarg)) {
3684 lsa.lsa_comp_end = LUSTRE_EOF;
3686 result = llapi_parse_size(optarg,
3689 /* assume units of KB if too small */
3690 if (lsa.lsa_comp_end < 4096)
3691 lsa.lsa_comp_end *= 1024;
3693 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3695 "%s %s: invalid component end '%s'\n",
3696 progname, argv[0], optarg);
3702 if (!migrate_mode) {
3704 "--mdt-hash is valid only for migrate command\n");
3708 lsa.lsa_pattern = check_hashtype(optarg);
3709 if (lsa.lsa_pattern == 0) {
3711 "%s %s: bad stripe hash type '%s'\n",
3712 progname, argv[0], optarg);
3718 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3719 if (errno != 0 || *end != '\0' ||
3720 lsa.lsa_stripe_off < -1 ||
3721 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3723 "%s %s: invalid stripe offset '%s'\n",
3724 progname, argv[0], optarg);
3727 if (lsa.lsa_stripe_off == -1)
3728 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3731 comp_id = strtoul(optarg, &end, 0);
3732 if (*end != '\0' || comp_id == 0 ||
3733 comp_id > LCME_ID_MAX) {
3735 "%s %s: invalid component ID '%s'\n",
3736 progname, argv[0], optarg);
3741 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3743 "error: %s: invalid option: %s\n",
3744 progname, argv[optopt + 1]);
3747 if (opc == SO_MIRROR_EXTEND) {
3750 "error: %s: '-N' must exist in front of '%s'\n",
3751 progname, argv[optopt + 1]);
3754 last_mirror->m_file = optarg;
3755 last_mirror->m_count = 1;
3759 mirror_list = lfs_mirror_alloc();
3760 mirror_list->m_file = optarg;
3765 if (strcmp(argv[optind - 1], "mdt") == 0) {
3766 /* Can be only the first component */
3770 "error: 'mdt' layout can be only the first one\n");
3773 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3776 "error: 'mdt' layout size is too big\n");
3779 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3780 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3783 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3789 if (!migrate_mode) {
3791 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3795 migrate_mdt_mode = true;
3796 lsa.lsa_nr_tgts = parse_targets(tgts,
3797 sizeof(tgts) / sizeof(__u32),
3798 lsa.lsa_nr_tgts, optarg, NULL);
3799 if (lsa.lsa_nr_tgts < 0) {
3801 "%s %s: invalid MDT target(s) '%s'\n",
3802 progname, argv[0], optarg);
3806 lsa.lsa_tgts = tgts;
3807 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3808 lsa.lsa_stripe_off = tgts[0];
3811 if (!migrate_mode) {
3813 "%s %s: -n|--non-block valid only for migrate command\n",
3817 migration_flags |= MIGRATION_NONBLOCK;
3820 if (opc == SO_SETSTRIPE) {
3821 opc = SO_MIRROR_CREATE;
3827 mirror_count = strtoul(optarg, &end, 0);
3828 if (errno != 0 || *end != '\0' ||
3829 mirror_count == 0 ||
3830 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3832 "error: %s: bad mirror count: %s\n",
3839 new_mirror = lfs_mirror_alloc();
3840 new_mirror->m_count = mirror_count;
3843 mirror_list = new_mirror;
3846 /* wrap up last mirror */
3847 if (!setstripe_args_specified(&lsa))
3848 last_mirror->m_inherit = true;
3849 if (lsa.lsa_comp_end == 0)
3850 lsa.lsa_comp_end = LUSTRE_EOF;
3852 result = comp_args_to_layout(lpp, &lsa, true);
3854 lfs_mirror_free(new_mirror);
3858 setstripe_args_init_inherit(&lsa);
3860 last_mirror->m_next = new_mirror;
3863 last_mirror = new_mirror;
3864 lpp = &last_mirror->m_layout;
3867 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3868 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3870 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3872 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3874 "%s %s: -o|--ost incompatible with DoM layout\n",
3879 * -o allows overstriping, and must note it because
3880 * parse_targets is shared with MDT striping, which
3881 * does not allow duplicates
3883 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3884 lsa.lsa_nr_tgts = parse_targets(tgts,
3885 sizeof(tgts) / sizeof(__u32),
3886 lsa.lsa_nr_tgts, optarg,
3888 if (lsa.lsa_nr_tgts < 0) {
3890 "%s %s: invalid OST target(s) '%s'\n",
3891 progname, argv[0], optarg);
3895 lsa.lsa_tgts = tgts;
3896 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3897 lsa.lsa_stripe_off = tgts[0];
3902 lsa.lsa_pool_name = optarg;
3904 if (strlen(lsa.lsa_pool_name) == 0 ||
3905 strncmp(lsa.lsa_pool_name, "none",
3906 LOV_MAXPOOLNAME) == 0)
3907 lsa.lsa_pool_name = NULL;
3910 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3912 /* assume units of KB if too small to be valid */
3913 if (lsa.lsa_stripe_size < 4096)
3914 lsa.lsa_stripe_size *= 1024;
3916 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3918 "%s %s: invalid stripe size '%s'\n",
3919 progname, argv[0], optarg);
3924 if (!migrate_mode) {
3926 "%s %s: -v|--verbose valid only for migrate command\n",
3930 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3931 migration_flags = MIGRATION_VERBOSE;
3941 result = llapi_parse_size(optarg,
3942 &lsa.lsa_extension_size,
3946 "%s %s: invalid extension size '%s'\n",
3947 progname, argv[0], optarg);
3951 lsa.lsa_extension_comp = true;
3954 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3955 progname, argv[0], argv[optind - 1]);
3960 fname = argv[optind];
3962 if (optind == argc) {
3963 fprintf(stderr, "%s %s: FILE must be specified\n",
3968 /* lfs migrate $filename should keep the file's layout by default */
3969 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3973 if (xattr && !foreign_mode) {
3975 * only print a warning as this is harmless and will be ignored
3978 "%s %s: xattr has been specified for non-foreign layout\n",
3980 } else if (foreign_mode && !xattr) {
3982 "%s %s: xattr must be provided in foreign mode\n",
3987 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3988 comp_set || comp_id || delete || from_copy ||
3989 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3992 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3997 if (mirror_mode && mirror_count == 0) {
3999 "error: %s: --mirror-count|-N option is required\n",
4006 if (!setstripe_args_specified(&lsa))
4007 last_mirror->m_inherit = true;
4008 if (lsa.lsa_comp_end == 0)
4009 lsa.lsa_comp_end = LUSTRE_EOF;
4012 if (lsa.lsa_comp_end != 0) {
4013 result = comp_args_to_layout(lpp, &lsa, true);
4015 fprintf(stderr, "error: %s: invalid layout\n",
4022 if (mirror_flags & MF_NO_VERIFY) {
4023 if (opc != SO_MIRROR_EXTEND) {
4025 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4029 } else if (!has_m_file) {
4031 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4038 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4040 "%s %s: --component-set doesn't have component-id set\n",
4045 if ((delete + comp_set + comp_del + comp_add) > 1) {
4047 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4052 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4053 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4055 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4060 if ((comp_set || comp_del) &&
4061 (setstripe_args_specified(&lsa) || layout != NULL)) {
4063 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4068 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4070 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4075 if (comp_add || comp_del) {
4078 result = lstat(fname, &st);
4079 if (result == 0 && S_ISDIR(st.st_mode)) {
4081 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4088 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4097 "%s %s: option -E must be specified with --component-add\n",
4103 if (from_yaml && from_copy) {
4105 "%s: can't specify --yaml and --copy together\n",
4110 if ((from_yaml || from_copy) &&
4111 (setstripe_args_specified(&lsa) || layout != NULL)) {
4113 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4118 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4120 "%s %s: options --non-block and --block are mutually exclusive\n",
4125 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4126 opc != SO_MIRROR_DELETE && comp_id != 0) {
4128 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4133 if (migrate_mdt_mode) {
4134 struct lmv_user_md *lmu;
4136 /* initialize migrate mdt parameters */
4137 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4138 LMV_USER_MAGIC_SPECIFIC));
4141 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4142 progname, argv[0], strerror(ENOMEM));
4146 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4147 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4148 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4150 "%s %s: migrate should specify MDT index\n",
4155 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4156 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4157 lmu->lum_hash_type = lsa.lsa_pattern;
4159 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4160 if (lsa.lsa_pool_name) {
4161 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4162 sizeof(lmu->lum_pool_name) - 1);
4163 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4165 if (lsa.lsa_nr_tgts > 1) {
4168 if (lsa.lsa_stripe_count > 0 &&
4169 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4170 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4172 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4173 progname, lsa.lsa_stripe_count,
4179 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4180 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4181 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4182 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4184 lmu->lum_magic = LMV_USER_MAGIC;
4187 migrate_mdt_param.fp_lmv_md = lmu;
4188 migrate_mdt_param.fp_migrate = 1;
4189 } else if (!layout) {
4190 /* initialize stripe parameters */
4191 param = calloc(1, offsetof(typeof(*param),
4192 lsp_osts[lsa.lsa_nr_tgts]));
4195 "%s %s: cannot allocate memory for parameters: %s\n",
4196 progname, argv[0], strerror(ENOMEM));
4201 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4202 param->lsp_stripe_size = lsa.lsa_stripe_size;
4203 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4204 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4205 param->lsp_stripe_count = -1;
4207 param->lsp_stripe_count = lsa.lsa_stripe_count;
4209 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4210 param->lsp_stripe_offset = -1;
4212 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4213 param->lsp_stripe_pattern =
4214 llapi_pattern_to_lov(lsa.lsa_pattern);
4215 if (param->lsp_stripe_pattern == EINVAL) {
4216 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4221 param->lsp_pool = lsa.lsa_pool_name;
4222 param->lsp_is_specific = false;
4223 if (lsa.lsa_nr_tgts > 0) {
4224 if (lsa.lsa_stripe_count > 0 &&
4225 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4226 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
4227 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
4229 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
4230 argv[0], lsa.lsa_stripe_count,
4236 param->lsp_is_specific = true;
4237 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4238 memcpy(param->lsp_osts, tgts,
4239 sizeof(*tgts) * lsa.lsa_nr_tgts);
4244 /* generate a layout from a YAML template */
4245 result = lfs_comp_create_from_yaml(template, &layout,
4249 "error: %s: can't create composite layout from template file %s\n",
4255 if (layout != NULL || mirror_list != NULL) {
4257 result = mirror_adjust_first_extents(mirror_list);
4259 result = layout_adjust_first_extent(fname, layout,
4261 if (result == -ENODATA)
4263 else if (result != 0) {
4264 fprintf(stderr, "error: %s: invalid layout\n",
4270 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4272 layout = llapi_layout_get_by_path(template ?: fname, 0);
4275 "%s: can't create composite layout from file %s: %s\n",
4276 progname, template ?: fname,
4283 if (migrate_mdt_mode) {
4284 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4285 } else if (migrate_mode) {
4286 result = lfs_migrate(fname, migration_flags, param,
4288 } else if (comp_set != 0) {
4289 result = lfs_component_set(fname, comp_id,
4292 lsa.lsa_comp_neg_flags);
4293 } else if (comp_del != 0) {
4294 result = lfs_component_del(fname, comp_id,
4296 lsa.lsa_comp_neg_flags);
4297 } else if (comp_add != 0) {
4298 result = lfs_component_add(fname, layout);
4299 } else if (opc == SO_MIRROR_CREATE) {
4300 result = mirror_create(fname, mirror_list);
4301 } else if (opc == SO_MIRROR_EXTEND) {
4302 result = mirror_extend(fname, mirror_list,
4304 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4305 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4307 "%s: no mirror specified to delete from '%s'\n",
4311 if (lsa.lsa_pool_name)
4312 mirror_flags |= MF_COMP_POOL;
4313 else if (mirror_id != 0)
4314 comp_id = mirror_id;
4316 mirror_flags |= MF_COMP_ID;
4317 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4319 has_m_file ? mirror_list->m_file :
4321 } else if (layout) {
4322 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4328 } else if (foreign_mode) {
4329 result = llapi_file_create_foreign(fname, mode, type,
4336 result = llapi_file_open_param(fname,
4345 /* Save the first error encountered. */
4353 umask(previous_umask);
4356 free(migrate_mdt_param.fp_lmv_md);
4357 llapi_layout_free(layout);
4358 lfs_mirror_list_free(mirror_list);
4363 llapi_layout_free(layout);
4364 lfs_mirror_list_free(mirror_list);
4368 static int lfs_poollist(int argc, char **argv)
4373 return llapi_poollist(argv[1]);
4376 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4377 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4382 char *endptr = "AD";
4387 else if (str[0] == '-')
4393 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4394 long long val = strtoll(timebuf, &endptr, 0);
4399 unit *= 52; /* 52 weeks + 1 day below */
4400 case 'w': /* fallthrough */
4402 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4403 param->fp_time_margin *= (1 + unit / 52);
4404 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4405 case '\0': /* days are default unit if none used */
4406 case 'd': /* fallthrough */
4408 case 'h': /* fallthrough */
4410 case 'm': /* fallthrough */
4412 case 's': /* fallthrough */
4414 /* don't need to multiply by 1 for seconds */
4417 "%s find: bad time string '%s': %s\n",
4418 progname, timebuf, strerror(EINVAL));
4422 if (param->fp_time_margin == 0 ||
4423 (*endptr && unit < param->fp_time_margin))
4424 param->fp_time_margin = unit;
4431 fprintf(stderr, "%s find: bad time '%s': too large\n",
4441 static int str2quotaid(__u32 *id, const char *arg)
4443 unsigned long int projid_tmp = 0;
4444 char *endptr = NULL;
4446 projid_tmp = strtoul(arg, &endptr, 10);
4447 if (*endptr != '\0')
4449 if (projid_tmp >= UINT32_MAX)
4456 static int name2uid(unsigned int *id, const char *name)
4458 struct passwd *passwd;
4460 passwd = getpwnam(name);
4463 *id = passwd->pw_uid;
4468 static int name2gid(unsigned int *id, const char *name)
4470 struct group *group;
4472 group = getgrnam(name);
4475 *id = group->gr_gid;
4480 static inline int name2projid(unsigned int *id, const char *name)
4485 static int uid2name(char **name, unsigned int id)
4487 struct passwd *passwd;
4489 passwd = getpwuid(id);
4492 *name = passwd->pw_name;
4497 static inline int gid2name(char **name, unsigned int id)
4499 struct group *group;
4501 group = getgrgid(id);
4504 *name = group->gr_name;
4509 static int name2layout(__u32 *layout, char *name)
4511 char *ptr, *layout_name;
4514 for (ptr = name; ; ptr = NULL) {
4515 layout_name = strtok(ptr, ",");
4518 if (strcmp(layout_name, "released") == 0)
4519 *layout |= LOV_PATTERN_F_RELEASED;
4520 else if (strcmp(layout_name, "raid0") == 0)
4521 *layout |= LOV_PATTERN_RAID0;
4522 else if (strcmp(layout_name, "mdt") == 0)
4523 *layout |= LOV_PATTERN_MDT;
4524 else if (strcmp(layout_name, "overstriping") == 0)
4525 *layout |= LOV_PATTERN_OVERSTRIPING;
4532 static int lfs_find(int argc, char **argv)
4537 struct find_param param = {
4540 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4542 struct option long_opts[] = {
4543 { .val = 'A', .name = "atime", .has_arg = required_argument },
4544 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4545 { .val = 'B', .name = "btime", .has_arg = required_argument },
4546 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4547 { .val = LFS_COMP_COUNT_OPT,
4548 .name = "comp-count", .has_arg = required_argument },
4549 { .val = LFS_COMP_COUNT_OPT,
4550 .name = "component-count",
4551 .has_arg = required_argument },
4552 { .val = LFS_COMP_FLAGS_OPT,
4553 .name = "comp-flags", .has_arg = required_argument },
4554 { .val = LFS_COMP_FLAGS_OPT,
4555 .name = "component-flags",
4556 .has_arg = required_argument },
4557 { .val = LFS_COMP_START_OPT,
4558 .name = "comp-start", .has_arg = required_argument },
4559 { .val = LFS_COMP_START_OPT,
4560 .name = "component-start",
4561 .has_arg = required_argument },
4562 { .val = LFS_MIRROR_STATE_OPT,
4563 .name = "mirror-state", .has_arg = required_argument },
4564 { .val = LFS_NEWERXY_OPT,
4565 .name = "newer", .has_arg = required_argument},
4566 { .val = LFS_NEWERXY_OPT,
4567 .name = "neweraa", .has_arg = required_argument},
4568 { .val = LFS_NEWERXY_OPT,
4569 .name = "neweram", .has_arg = required_argument},
4570 { .val = LFS_NEWERXY_OPT,
4571 .name = "newerac", .has_arg = required_argument},
4572 { .val = LFS_NEWERXY_OPT,
4573 .name = "newerab", .has_arg = required_argument},
4574 { .val = LFS_NEWERXY_OPT,
4575 .name = "newerma", .has_arg = required_argument},
4576 { .val = LFS_NEWERXY_OPT,
4577 .name = "newermm", .has_arg = required_argument},
4578 { .val = LFS_NEWERXY_OPT,
4579 .name = "newermc", .has_arg = required_argument},
4580 { .val = LFS_NEWERXY_OPT,
4581 .name = "newermb", .has_arg = required_argument},
4582 { .val = LFS_NEWERXY_OPT,
4583 .name = "newerca", .has_arg = required_argument},
4584 { .val = LFS_NEWERXY_OPT,
4585 .name = "newercm", .has_arg = required_argument},
4586 { .val = LFS_NEWERXY_OPT,
4587 .name = "newercc", .has_arg = required_argument},
4588 { .val = LFS_NEWERXY_OPT,
4589 .name = "newercb", .has_arg = required_argument},
4590 { .val = LFS_NEWERXY_OPT,
4591 .name = "newerba", .has_arg = required_argument},
4592 { .val = LFS_NEWERXY_OPT,
4593 .name = "newerbm", .has_arg = required_argument},
4594 { .val = LFS_NEWERXY_OPT,
4595 .name = "newerbc", .has_arg = required_argument},
4596 { .val = LFS_NEWERXY_OPT,
4597 .name = "newerbb", .has_arg = required_argument},
4598 { .val = LFS_NEWERXY_OPT,
4599 .name = "newerBa", .has_arg = required_argument},
4600 { .val = LFS_NEWERXY_OPT,
4601 .name = "newerBm", .has_arg = required_argument},
4602 { .val = LFS_NEWERXY_OPT,
4603 .name = "newerBc", .has_arg = required_argument},
4604 { .val = LFS_NEWERXY_OPT,
4605 .name = "newerBB", .has_arg = required_argument},
4606 { .val = LFS_NEWERXY_OPT,
4607 .name = "newerat", .has_arg = required_argument},
4608 { .val = LFS_NEWERXY_OPT,
4609 .name = "newermt", .has_arg = required_argument},
4610 { .val = LFS_NEWERXY_OPT,
4611 .name = "newerct", .has_arg = required_argument},
4612 { .val = LFS_NEWERXY_OPT,
4613 .name = "newerbt", .has_arg = required_argument},
4614 { .val = LFS_NEWERXY_OPT,
4615 .name = "newerBt", .has_arg = required_argument},
4616 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4617 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4618 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4619 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4620 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4621 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4622 { .val = 'E', .name = "component-end",
4623 .has_arg = required_argument },
4624 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4625 { .val = LFS_LAYOUT_FOREIGN_OPT,
4626 .name = "foreign", .has_arg = optional_argument},
4627 { .val = 'g', .name = "gid", .has_arg = required_argument },
4628 { .val = 'G', .name = "group", .has_arg = required_argument },
4629 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4630 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4631 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4632 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4633 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4634 { .val = 'L', .name = "layout", .has_arg = required_argument },
4635 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4636 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4637 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4638 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4639 { .val = 'n', .name = "name", .has_arg = required_argument },
4640 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4641 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4642 { .val = 'O', .name = "obd", .has_arg = required_argument },
4643 { .val = 'O', .name = "ost", .has_arg = required_argument },
4644 /* no short option for pool yet, can be 'p' after 2.18 */
4645 { .val = LFS_POOL_OPT,
4646 .name = "pool", .has_arg = required_argument },
4647 { .val = '0', .name = "print0", .has_arg = no_argument },
4648 { .val = 'P', .name = "print", .has_arg = no_argument },
4649 { .val = LFS_PROJID_OPT,
4650 .name = "projid", .has_arg = required_argument },
4651 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4652 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4653 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4654 { .val = 's', .name = "size", .has_arg = required_argument },
4655 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4656 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4657 { .val = 't', .name = "type", .has_arg = required_argument },
4658 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4659 { .val = 'u', .name = "uid", .has_arg = required_argument },
4660 { .val = 'U', .name = "user", .has_arg = required_argument },
4661 { .val = 'z', .name = "extension-size",
4662 .has_arg = required_argument },
4663 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4664 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4665 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4679 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4680 while ((c = getopt_long_only(argc, argv,
4681 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4682 long_opts, &optidx)) >= 0) {
4687 /* '!' is part of option */
4689 * when getopt_long_only() finds a string which is not
4690 * an option nor a known option argument it returns 1
4691 * in that case if we already have found pathstart and pathend
4692 * (i.e. we have the list of pathnames),
4693 * the only supported value is "!"
4695 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4696 if (!isoption && pathend != -1) {
4698 "err: %s: filename|dirname must either precede options or follow options\n",
4703 if (!isoption && pathstart == -1)
4704 pathstart = optind - 1;
4705 if (isoption && pathstart != -1 && pathend == -1)
4706 pathend = optind - 2;
4713 * unknown; opt is "!" or path component,
4714 * checking done above.
4716 if (strcmp(optarg, "!") == 0)
4720 xtime = ¶m.fp_atime;
4721 xsign = ¶m.fp_asign;
4722 param.fp_exclude_atime = !!neg_opt;
4723 /* no break, this falls through to 'B' for btime */
4726 xtime = ¶m.fp_btime;
4727 xsign = ¶m.fp_bsign;
4728 param.fp_exclude_btime = !!neg_opt;
4730 /* no break, this falls through to 'C' for ctime */
4733 xtime = ¶m.fp_ctime;
4734 xsign = ¶m.fp_csign;
4735 param.fp_exclude_ctime = !!neg_opt;
4737 /* no break, this falls through to 'M' for mtime */
4740 xtime = ¶m.fp_mtime;
4741 xsign = ¶m.fp_msign;
4742 param.fp_exclude_mtime = !!neg_opt;
4744 rc = set_time(¶m, &t, xtime, optarg);
4745 if (rc == LONG_MAX) {
4753 if (optarg[0] == '+') {
4754 param.fp_blocks_sign = -1;
4756 } else if (optarg[0] == '-') {
4757 param.fp_blocks_sign = 1;
4761 param.fp_blocks_units = 1024;
4762 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4763 ¶m.fp_blocks_units, 0);
4765 fprintf(stderr, "error: bad blocks '%s'\n",
4769 param.fp_check_blocks = 1;
4770 param.fp_exclude_blocks = !!neg_opt;
4772 case LFS_COMP_COUNT_OPT:
4773 if (optarg[0] == '+') {
4774 param.fp_comp_count_sign = -1;
4776 } else if (optarg[0] == '-') {
4777 param.fp_comp_count_sign = 1;
4782 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4783 if (errno != 0 || *endptr != '\0' ||
4784 param.fp_comp_count > UINT32_MAX) {
4786 "error: bad component count '%s'\n",
4790 param.fp_check_comp_count = 1;
4791 param.fp_exclude_comp_count = !!neg_opt;
4793 case LFS_COMP_FLAGS_OPT:
4794 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4795 ¶m.fp_comp_neg_flags);
4798 "error: bad component flags '%s'\n",
4802 param.fp_check_comp_flags = 1;
4804 __u32 flags = param.fp_comp_neg_flags;
4806 param.fp_comp_neg_flags = param.fp_comp_flags;
4807 param.fp_comp_flags = flags;
4810 case LFS_COMP_START_OPT:
4811 if (optarg[0] == '+') {
4812 param.fp_comp_start_sign = -1;
4814 } else if (optarg[0] == '-') {
4815 param.fp_comp_start_sign = 1;
4819 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4820 ¶m.fp_comp_start_units, 0);
4823 "error: bad component start '%s'\n",
4827 param.fp_check_comp_start = 1;
4828 param.fp_exclude_comp_start = !!neg_opt;
4830 case LFS_MIRROR_STATE_OPT:
4831 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4832 ¶m.fp_mirror_neg_state);
4835 "error: bad mirrored file state '%s'\n",
4839 param.fp_check_mirror_state = 1;
4841 __u16 state = param.fp_mirror_neg_state;
4843 param.fp_mirror_neg_state =
4844 param.fp_mirror_state;
4845 param.fp_mirror_state = state;
4849 if (optarg[0] == '+') {
4850 param.fp_stripe_count_sign = -1;
4852 } else if (optarg[0] == '-') {
4853 param.fp_stripe_count_sign = 1;
4858 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4859 if (errno != 0 || *endptr != '\0' ||
4860 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
4862 "error: bad stripe_count '%s'\n",
4867 param.fp_check_stripe_count = 1;
4868 param.fp_exclude_stripe_count = !!neg_opt;
4872 param.fp_max_depth = strtol(optarg, 0, 0);
4873 if (errno != 0 || param.fp_max_depth < 0) {
4875 "error: bad maxdepth '%s'\n",
4882 if (optarg[0] == '+') {
4883 param.fp_comp_end_sign = -1;
4885 } else if (optarg[0] == '-') {
4886 param.fp_comp_end_sign = 1;
4890 if (arg_is_eof(optarg)) {
4891 param.fp_comp_end = LUSTRE_EOF;
4892 param.fp_comp_end_units = 1;
4895 rc = llapi_parse_size(optarg,
4897 ¶m.fp_comp_end_units, 0);
4898 /* assume units of KB if too small */
4899 if (param.fp_comp_end < 4096)
4900 param.fp_comp_end *= 1024;
4904 "error: bad component end '%s'\n",
4908 param.fp_check_comp_end = 1;
4909 param.fp_exclude_comp_end = !!neg_opt;
4911 case LFS_LAYOUT_FOREIGN_OPT: {
4912 /* all types by default */
4913 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4916 /* check pure numeric */
4917 type = strtoul(optarg, &endptr, 0);
4920 type = check_foreign_type_name(optarg);
4921 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4923 "%s %s: unknown foreign type '%s'\n",
4928 } else if (type >= UINT32_MAX) {
4930 "%s %s: invalid foreign type '%s'\n",
4931 progname, argv[0], optarg);
4935 param.fp_foreign_type = type;
4936 param.fp_check_foreign = 1;
4937 param.fp_exclude_foreign = !!neg_opt;
4940 case LFS_NEWERXY_OPT: {
4946 time_t ref = time(NULL);
4948 /* no need to check bad options, they won't get here */
4949 if (strlen(long_opts[optidx].name) == 7) {
4950 x = long_opts[optidx].name[5];
4951 y = long_opts[optidx].name[6];
4955 static const char *const fmts[] = {
4956 "%Y-%m-%d %H:%M:%S",
4959 "%H:%M:%S", /* sometime today */
4968 for (i = 0; fmts[i] != NULL; i++) {
4971 /* Init for times relative to today */
4972 if (strncmp(fmts[i], "%H", 2) == 0)
4973 localtime_r(&ref, &tm);
4975 memset(&tm, 0, sizeof(tm));
4976 ptr = strptime(optarg, fmts[i], &tm);
4978 while (ptr && isspace(*ptr))
4980 if (ptr == optarg + strlen(optarg)) {
4988 "%s: invalid time '%s'\n",
4991 "supported formats are:\n ");
4992 for (i = 0; fmts[i] != NULL; i++)
4993 fprintf(stderr, "'%s', ",
4995 fprintf(stderr, "\n");
5001 } else if (y == 'b' || y == 'B') {
5004 rc = llapi_get_lum_file(optarg, NULL, &stx,
5006 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5007 if (!(stx.stx_mask & STATX_BTIME))
5012 "%s: get btime failed '%s': %s\n",
5018 ref = stx.stx_btime.tv_sec;
5020 struct stat statbuf;
5022 if (stat(optarg, &statbuf) < 0) {
5024 "%s: cannot stat file '%s': %s\n",
5033 ref = statbuf.st_atime;
5036 ref = statbuf.st_mtime;
5039 ref = statbuf.st_ctime;
5043 "%s: invalid Y argument: '%c'\n",
5052 xidx = NEWERXY_ATIME;
5055 xidx = NEWERXY_MTIME;
5058 xidx = NEWERXY_CTIME;
5062 xidx = NEWERXY_BTIME;
5066 "%s: invalid X argument: '%c'\n",
5073 newery = ¶m.fp_newery[xidx][negidx];
5079 *newery = *newery > ref ? ref : *newery;
5081 *newery = *newery > ref ? *newery : ref;
5083 param.fp_newerxy = 1;
5088 rc = name2gid(¶m.fp_gid, optarg);
5090 if (str2quotaid(¶m.fp_gid, optarg)) {
5092 "Group/GID: %s cannot be found.\n",
5098 param.fp_exclude_gid = !!neg_opt;
5099 param.fp_check_gid = 1;
5102 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5103 ¶m.fp_hash_exflags,
5104 ¶m.fp_hash_type);
5109 if (param.fp_hash_inflags || param.fp_hash_exflags)
5110 param.fp_check_hash_flag = 1;
5111 param.fp_exclude_hash_type = !!neg_opt;
5117 ret = name2layout(¶m.fp_layout, optarg);
5120 param.fp_exclude_layout = !!neg_opt;
5121 param.fp_check_layout = 1;
5125 rc = name2uid(¶m.fp_uid, optarg);
5127 if (str2quotaid(¶m.fp_uid, optarg)) {
5129 "User/UID: %s cannot be found.\n",
5135 param.fp_exclude_uid = !!neg_opt;
5136 param.fp_check_uid = 1;
5139 param.fp_pattern = (char *)optarg;
5140 param.fp_exclude_pattern = !!neg_opt;
5143 if (optarg[0] == '+') {
5144 param.fp_mirror_count_sign = -1;
5146 } else if (optarg[0] == '-') {
5147 param.fp_mirror_count_sign = 1;
5152 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5153 if (errno != 0 || *endptr != '\0' ||
5154 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5156 "error: bad mirror count '%s'\n",
5160 param.fp_check_mirror_count = 1;
5161 param.fp_exclude_mirror_count = !!neg_opt;
5166 char *buf, *token, *next, *p;
5170 buf = strdup(optarg);
5176 param.fp_exclude_obd = !!neg_opt;
5179 while (token && *token) {
5180 token = strchr(token, ',');
5187 param.fp_exclude_mdt = !!neg_opt;
5188 param.fp_num_alloc_mdts += len;
5189 tmp = realloc(param.fp_mdt_uuid,
5190 param.fp_num_alloc_mdts *
5191 sizeof(*param.fp_mdt_uuid));
5197 param.fp_mdt_uuid = tmp;
5199 param.fp_exclude_obd = !!neg_opt;
5200 param.fp_num_alloc_obds += len;
5201 tmp = realloc(param.fp_obd_uuid,
5202 param.fp_num_alloc_obds *
5203 sizeof(*param.fp_obd_uuid));
5209 param.fp_obd_uuid = tmp;
5211 for (token = buf; token && *token; token = next) {
5212 struct obd_uuid *puuid;
5216 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5219 ¶m.fp_obd_uuid[param.fp_num_obds++];
5221 p = strchr(token, ',');
5228 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5233 strncpy(puuid->uuid, token,
5234 sizeof(puuid->uuid));
5241 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5245 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5247 "Pool name %s is too long (max %d)\n",
5248 optarg, LOV_MAXPOOLNAME);
5253 * We do check for empty pool because empty pool
5254 * is used to find V1 LOV attributes
5256 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5257 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5258 param.fp_exclude_pool = !!neg_opt;
5259 param.fp_check_pool = 1;
5261 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5262 case 'p': /* want this for --pool, to match getstripe/find */
5264 "warning: -p deprecated, use --print0 or -0\n");
5267 param.fp_zero_end = 1;
5269 case 'P': /* we always print, this option is a no-op */
5271 case LFS_PROJID_OPT:
5272 rc = name2projid(¶m.fp_projid, optarg);
5274 if (str2quotaid(¶m.fp_projid, optarg)) {
5276 "Invalid project ID: %s\n",
5282 param.fp_exclude_projid = !!neg_opt;
5283 param.fp_check_projid = 1;
5286 if (optarg[0] == '+') {
5287 param.fp_size_sign = -1;
5289 } else if (optarg[0] == '-') {
5290 param.fp_size_sign = 1;
5294 ret = llapi_parse_size(optarg, ¶m.fp_size,
5295 ¶m.fp_size_units, 0);
5297 fprintf(stderr, "error: bad file size '%s'\n",
5301 param.fp_check_size = 1;
5302 param.fp_exclude_size = !!neg_opt;
5305 if (optarg[0] == '+') {
5306 param.fp_stripe_size_sign = -1;
5308 } else if (optarg[0] == '-') {
5309 param.fp_stripe_size_sign = 1;
5313 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5314 ¶m.fp_stripe_size_units, 0);
5315 /* assume units of KB if too small to be valid */
5316 if (param.fp_stripe_size < 4096)
5317 param.fp_stripe_size *= 1024;
5319 fprintf(stderr, "error: bad stripe_size '%s'\n",
5323 param.fp_check_stripe_size = 1;
5324 param.fp_exclude_stripe_size = !!neg_opt;
5327 param.fp_exclude_type = !!neg_opt;
5328 switch (optarg[0]) {
5330 param.fp_type = S_IFBLK;
5333 param.fp_type = S_IFCHR;
5336 param.fp_type = S_IFDIR;
5339 param.fp_type = S_IFREG;
5342 param.fp_type = S_IFLNK;
5345 param.fp_type = S_IFIFO;
5348 param.fp_type = S_IFSOCK;
5351 fprintf(stderr, "error: %s: bad type '%s'\n",
5358 if (optarg[0] == '+') {
5359 param.fp_mdt_count_sign = -1;
5361 } else if (optarg[0] == '-') {
5362 param.fp_mdt_count_sign = 1;
5367 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5368 if (errno != 0 || *endptr != '\0' ||
5369 param.fp_mdt_count >= UINT32_MAX) {
5370 fprintf(stderr, "error: bad mdt_count '%s'\n",
5375 param.fp_check_mdt_count = 1;
5376 param.fp_exclude_mdt_count = !!neg_opt;
5379 if (optarg[0] == '+') {
5380 param.fp_ext_size_sign = -1;
5382 } else if (optarg[0] == '-') {
5383 param.fp_ext_size_sign = 1;
5387 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5388 ¶m.fp_ext_size_units, 0);
5390 fprintf(stderr, "error: bad ext-size '%s'\n",
5394 param.fp_ext_size /= SEL_UNIT_SIZE;
5395 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5396 param.fp_check_ext_size = 1;
5397 param.fp_exclude_ext_size = !!neg_opt;
5405 if (pathstart == -1) {
5406 fprintf(stderr, "error: %s: no filename|pathname\n",
5410 } else if (pathend == -1) {
5416 rc = llapi_find(argv[pathstart], ¶m);
5419 pathbad = pathstart;
5421 } while (++pathstart < pathend);
5424 fprintf(stderr, "%s: failed for '%s': %s\n",
5425 progname, argv[pathbad], strerror(-rc));
5428 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5429 free(param.fp_obd_uuid);
5431 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5432 free(param.fp_mdt_uuid);
5437 static int lfs_getstripe_internal(int argc, char **argv,
5438 struct find_param *param)
5440 struct option long_opts[] = {
5441 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5442 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5443 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5444 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5445 { .val = LFS_COMP_COUNT_OPT,
5446 .name = "comp-count", .has_arg = no_argument },
5447 { .val = LFS_COMP_COUNT_OPT,
5448 .name = "component-count", .has_arg = no_argument },
5449 { .val = LFS_COMP_FLAGS_OPT,
5450 .name = "comp-flags", .has_arg = optional_argument },
5451 { .val = LFS_COMP_FLAGS_OPT,
5452 .name = "component-flags", .has_arg = optional_argument },
5453 { .val = LFS_COMP_START_OPT,
5454 .name = "comp-start", .has_arg = optional_argument },
5455 { .val = LFS_COMP_START_OPT,
5456 .name = "component-start", .has_arg = optional_argument },
5457 { .val = LFS_MIRROR_INDEX_OPT,
5458 .name = "mirror-index", .has_arg = required_argument },
5459 { .val = LFS_MIRROR_ID_OPT,
5460 .name = "mirror-id", .has_arg = required_argument },
5461 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5462 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5463 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5464 { .val = 'd', .name = "directory", .has_arg = no_argument },
5465 { .val = 'D', .name = "default", .has_arg = no_argument },
5466 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5467 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5468 { .val = 'F', .name = "fid", .has_arg = no_argument },
5469 { .val = 'g', .name = "generation", .has_arg = no_argument },
5470 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5471 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5472 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5473 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5474 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5475 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5476 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5477 { .val = 'L', .name = "layout", .has_arg = no_argument },
5478 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5479 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5480 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5481 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5482 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5483 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5484 { .val = 'O', .name = "obd", .has_arg = required_argument },
5485 { .val = 'O', .name = "ost", .has_arg = required_argument },
5486 { .val = 'p', .name = "pool", .has_arg = no_argument },
5487 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5488 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5489 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5490 { .val = 'R', .name = "raw", .has_arg = no_argument },
5491 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5492 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5493 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5494 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5495 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5496 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5497 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5498 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5499 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5500 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5504 int pathstart = -1, pathend = -1;
5508 while ((c = getopt_long(argc, argv,
5509 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5510 long_opts, NULL)) != -1) {
5514 /* '!' is part of option */
5515 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5516 if (!isoption && pathend != -1) {
5518 "error: %s: filename|dirname must either precede options or follow options\n",
5522 if (!isoption && pathstart == -1)
5523 pathstart = optind - 1;
5524 if (isoption && pathstart != -1 && pathend == -1)
5525 pathend = optind - 2;
5529 /* unknown: opt is "!" */
5530 if (strcmp(optarg, "!") == 0)
5534 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5535 param->fp_verbose |= VERBOSE_COUNT;
5536 param->fp_max_depth = 0;
5539 case LFS_COMP_COUNT_OPT:
5540 param->fp_verbose |= VERBOSE_COMP_COUNT;
5541 param->fp_max_depth = 0;
5543 case LFS_COMP_FLAGS_OPT:
5545 rc = comp_str2flags(optarg,
5546 ¶m->fp_comp_flags,
5547 ¶m->fp_comp_neg_flags);
5550 "error: %s bad component flags '%s'.\n",
5554 param->fp_check_comp_flags = 1;
5556 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5557 param->fp_max_depth = 0;
5560 case LFS_COMP_START_OPT:
5563 if (tmp[0] == '+') {
5564 param->fp_comp_start_sign = -1;
5566 } else if (tmp[0] == '-') {
5567 param->fp_comp_start_sign = 1;
5570 rc = llapi_parse_size(tmp,
5571 ¶m->fp_comp_start,
5572 ¶m->fp_comp_start_units, 0);
5575 "error: %s bad component start '%s'.\n",
5579 param->fp_check_comp_start = 1;
5581 param->fp_verbose |= VERBOSE_COMP_START;
5582 param->fp_max_depth = 0;
5585 case LFS_MIRROR_INDEX_OPT: {
5586 unsigned long int mirror_index;
5588 if (optarg[0] == '+') {
5589 param->fp_mirror_index_sign = -1;
5591 } else if (optarg[0] == '-') {
5592 param->fp_mirror_index_sign = 1;
5597 mirror_index = strtoul(optarg, &end, 0);
5598 if (errno != 0 || *end != '\0' ||
5599 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5600 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5602 "%s %s: invalid mirror index '%s'\n",
5603 progname, argv[0], optarg);
5607 param->fp_mirror_index = (__u16)mirror_index;
5609 if (param->fp_mirror_id != 0) {
5611 "%s %s: can't specify both mirror index and mirror ID\n",
5615 param->fp_check_mirror_index = 1;
5616 param->fp_exclude_mirror_index = !!neg_opt;
5619 case LFS_MIRROR_ID_OPT: {
5620 unsigned long int mirror_id;
5622 if (optarg[0] == '+') {
5623 param->fp_mirror_id_sign = -1;
5625 } else if (optarg[0] == '-') {
5626 param->fp_mirror_id_sign = 1;
5631 mirror_id = strtoul(optarg, &end, 0);
5632 if (errno != 0 || *end != '\0' ||
5633 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5634 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5636 "%s %s: invalid mirror ID '%s'\n",
5637 progname, argv[0], optarg);
5641 param->fp_mirror_id = (__u16)mirror_id;
5643 if (param->fp_mirror_index != 0) {
5645 "%s %s: can't specify both mirror index and mirror ID\n",
5649 param->fp_check_mirror_id = 1;
5650 param->fp_exclude_mirror_id = !!neg_opt;
5654 param->fp_max_depth = 0;
5657 param->fp_get_default_lmv = 1;
5662 if (tmp[0] == '+') {
5663 param->fp_comp_end_sign = -1;
5665 } else if (tmp[0] == '-') {
5666 param->fp_comp_end_sign = 1;
5670 if (arg_is_eof(tmp)) {
5671 param->fp_comp_end = LUSTRE_EOF;
5672 param->fp_comp_end_units = 1;
5675 rc = llapi_parse_size(tmp,
5676 ¶m->fp_comp_end,
5677 ¶m->fp_comp_end_units, 0);
5678 /* assume units of KB if too small */
5679 if (param->fp_comp_end < 4096)
5680 param->fp_comp_end *= 1024;
5684 "error: %s bad component end '%s'.\n",
5688 param->fp_check_comp_end = 1;
5690 param->fp_verbose |= VERBOSE_COMP_END;
5691 param->fp_max_depth = 0;
5695 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5696 param->fp_verbose |= VERBOSE_DFID;
5697 param->fp_max_depth = 0;
5701 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5702 param->fp_verbose |= VERBOSE_GENERATION;
5703 param->fp_max_depth = 0;
5707 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5708 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5709 param->fp_max_depth = 0;
5714 param->fp_comp_id = strtoul(optarg, &end, 0);
5715 if (*end != '\0' || param->fp_comp_id == 0 ||
5716 param->fp_comp_id > LCME_ID_MAX) {
5718 "error: %s bad component id '%s'\n",
5722 param->fp_check_comp_id = 1;
5724 param->fp_max_depth = 0;
5725 param->fp_verbose |= VERBOSE_COMP_ID;
5729 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5730 param->fp_verbose |= VERBOSE_PATTERN;
5731 param->fp_max_depth = 0;
5734 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5737 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
5740 if (!(param->fp_verbose & VERBOSE_DETAIL))
5741 param->fp_max_depth = 0;
5742 param->fp_verbose |= VERBOSE_MDTINDEX;
5745 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5746 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5747 param->fp_max_depth = 0;
5751 if (param->fp_obd_uuid) {
5753 "error: %s: only one obduuid allowed",
5757 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5760 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5761 param->fp_verbose |= VERBOSE_POOL;
5762 param->fp_max_depth = 0;
5769 param->fp_recursive = 1;
5775 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5776 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5777 param->fp_max_depth = 0;
5781 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5787 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5788 param->fp_verbose |= VERBOSE_EXT_SIZE;
5789 param->fp_max_depth = 0;
5797 if (pathstart == -1) {
5798 fprintf(stderr, "error: %s: no filename|pathname\n",
5801 } else if (pathend == -1) {
5809 if (param->fp_recursive)
5810 param->fp_max_depth = -1;
5811 else if (param->fp_verbose & VERBOSE_DETAIL)
5812 param->fp_max_depth = 1;
5814 if (!param->fp_verbose)
5815 param->fp_verbose = VERBOSE_DEFAULT;
5816 if (param->fp_quiet)
5817 param->fp_verbose = VERBOSE_OBJID;
5820 rc = llapi_getstripe(argv[pathstart], param);
5821 } while (++pathstart < pathend && !rc);
5824 fprintf(stderr, "error: %s failed for %s.\n",
5825 argv[0], argv[optind - 1]);
5829 static int lfs_tgts(int argc, char **argv)
5831 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5832 struct find_param param;
5833 int index = 0, rc = 0;
5838 if (argc == 2 && !realpath(argv[1], path)) {
5840 fprintf(stderr, "error: invalid path '%s': %s\n",
5841 argv[1], strerror(-rc));
5845 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5846 /* Check if we have a mount point */
5847 if (mntdir[0] == '\0')
5850 memset(¶m, 0, sizeof(param));
5851 if (!strcmp(argv[0], "mdts"))
5852 param.fp_get_lmv = 1;
5854 rc = llapi_ostlist(mntdir, ¶m);
5856 fprintf(stderr, "error: %s: failed on %s\n",
5859 if (path[0] != '\0')
5861 memset(mntdir, 0, PATH_MAX);
5867 static int lfs_getstripe(int argc, char **argv)
5869 struct find_param param = { 0 };
5871 param.fp_max_depth = 1;
5872 return lfs_getstripe_internal(argc, argv, ¶m);
5876 static int lfs_getdirstripe(int argc, char **argv)
5878 struct find_param param = { 0 };
5879 struct option long_opts[] = {
5880 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5881 { .val = 'D', .name = "default", .has_arg = no_argument },
5882 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5883 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5884 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5885 { .val = 'O', .name = "obd", .has_arg = required_argument },
5886 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5887 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5888 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5889 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5893 param.fp_get_lmv = 1;
5895 while ((c = getopt_long(argc, argv,
5896 "cDHimO:rtTvy", long_opts, NULL)) != -1) {
5900 param.fp_verbose |= VERBOSE_COUNT;
5903 param.fp_get_default_lmv = 1;
5907 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5909 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5912 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
5915 param.fp_verbose |= VERBOSE_HASH_TYPE;
5918 if (param.fp_obd_uuid) {
5920 "error: %s: only one obduuid allowed",
5924 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5927 param.fp_recursive = 1;
5930 param.fp_verbose |= VERBOSE_DETAIL;
5936 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5937 progname, argv[0], argv[optind - 1]);
5945 if (param.fp_recursive)
5946 param.fp_max_depth = -1;
5948 if (!param.fp_verbose)
5949 param.fp_verbose = VERBOSE_DEFAULT;
5952 rc = llapi_getstripe(argv[optind], ¶m);
5953 } while (++optind < argc && !rc);
5956 fprintf(stderr, "error: %s failed for %s.\n",
5957 argv[0], argv[optind - 1]);
5962 MNTDF_INODES = 0x0001,
5963 MNTDF_COOKED = 0x0002,
5964 MNTDF_LAZY = 0x0004,
5965 MNTDF_VERBOSE = 0x0008,
5966 MNTDF_SHOW = 0x0010,
5967 MNTDF_DECIMAL = 0x0020,
5970 #define COOK(value, base) \
5973 while (value > base) { \
5981 #define CDF "%11llu"
5982 #define HDF "%8.1f%c"
5986 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5988 double avail, used, ratio = 0;
5991 avail = st->os_ffree;
5992 used = st->os_files - st->os_ffree;
5994 avail = st->os_bavail;
5995 used = st->os_blocks - st->os_bfree;
5997 if (avail + used > 0)
5998 ratio = used / (used + avail) * 100;
6000 /* Round up to match df(1) usage percentage */
6001 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6005 * This is to identify various problem states for "lfs df" if .osn_err = true,
6006 * so only show flags reflecting those states by default. Informational states
6007 * are only shown with "-v" and use lower-case names to distinguish them.
6008 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6010 static struct obd_statfs_state_names {
6011 enum obd_statfs_state osn_state;
6012 const char osn_name;
6015 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6016 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6017 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6018 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6019 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6020 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6021 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6022 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6023 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6026 static int showdf(char *mntdir, struct obd_statfs *stat,
6027 char *uuid, enum mntdf_flags flags,
6028 char *type, int index, int rc)
6030 long long avail, used, total;
6032 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6033 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6034 char tbuf[3 * sizeof(__u64)];
6035 char ubuf[3 * sizeof(__u64)];
6036 char abuf[3 * sizeof(__u64)];
6037 char rbuf[3 * sizeof(__u64)];
6044 if (flags & MNTDF_INODES) {
6045 avail = stat->os_ffree;
6046 used = stat->os_files - stat->os_ffree;
6047 total = stat->os_files;
6049 int shift = flags & MNTDF_COOKED ? 0 : 10;
6051 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6052 used = ((stat->os_blocks - stat->os_bfree) *
6053 stat->os_bsize) >> shift;
6054 total = (stat->os_blocks * stat->os_bsize) >> shift;
6057 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6059 if (flags & MNTDF_COOKED) {
6060 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6064 cook_val = (double)total;
6065 i = COOK(cook_val, base);
6067 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6070 snprintf(tbuf, sizeof(tbuf), CDF, total);
6072 cook_val = (double)used;
6073 i = COOK(cook_val, base);
6075 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6078 snprintf(ubuf, sizeof(ubuf), CDF, used);
6080 cook_val = (double)avail;
6081 i = COOK(cook_val, base);
6083 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6086 snprintf(abuf, sizeof(abuf), CDF, avail);
6088 snprintf(tbuf, sizeof(tbuf), CDF, total);
6089 snprintf(ubuf, sizeof(tbuf), CDF, used);
6090 snprintf(abuf, sizeof(tbuf), CDF, avail);
6093 sprintf(rbuf, RDF, ratio);
6094 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6095 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6097 printf("[%s:%d]", type, index);
6099 if (stat->os_state) {
6103 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6104 if (oss_names[i].osn_state & stat->os_state &&
6105 (oss_names[i].osn_err ||
6106 flags & MNTDF_VERBOSE))
6107 printf("%c", oss_names[i].osn_name);
6114 printf(UUF": inactive device\n", uuid);
6117 printf(UUF": %s\n", uuid, strerror(-rc));
6124 struct ll_stat_type {
6129 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6131 struct ll_statfs_data {
6133 struct obd_statfs sd_st;
6136 struct ll_statfs_buf {
6138 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6141 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6142 int ops, struct ll_statfs_buf *lsb)
6144 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6145 struct obd_uuid uuid_buf;
6146 char *poolname = NULL;
6147 struct ll_stat_type types[] = {
6148 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6149 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6150 { .st_name = NULL } };
6151 struct ll_stat_type *tp;
6152 __u64 ost_files = 0;
6153 __u64 ost_ffree = 0;
6161 poolname = strchr(pool, '.');
6163 if (strncmp(fsname, pool, strlen(fsname))) {
6164 fprintf(stderr, "filesystem name incorrect\n");
6172 fd = open(mntdir, O_RDONLY);
6175 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6180 if (flags & MNTDF_SHOW) {
6181 if (flags & MNTDF_INODES)
6182 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6183 "UUID", "Inodes", "IUsed", "IFree",
6184 "IUse%", "Mounted on");
6186 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6188 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6189 "Used", "Available", "Use%", "Mounted on");
6192 for (tp = types; tp->st_name != NULL; tp++) {
6193 bool have_ost = false;
6195 if (!(tp->st_op & ops))
6198 for (index = 0; ; index++) {
6199 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6200 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6201 type = flags & MNTDF_LAZY ?
6202 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6203 rc2 = llapi_obd_fstatfs(fd, type, index,
6204 &stat_buf, &uuid_buf);
6209 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6210 if (!(flags & MNTDF_VERBOSE))
6212 } else if (rc2 < 0 && rc == 0) {
6217 * If we have OSTs then don't report MDT block counts.
6218 * For MDT-only filesystems the expectation is that all
6219 * layouts have a DoM component. For filesystems with
6220 * OSTs, files are not necessarily going to store data
6221 * on MDTs, and MDT space is limited to a fraction of
6222 * OST space, so don't include it in the summary.
6224 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6231 if (poolname && tp->st_op == LL_STATFS_LOV &&
6232 llapi_search_ost(fsname, poolname,
6233 obd_uuid2str(&uuid_buf)) != 1)
6237 * the llapi_obd_fstatfs() call may have returned with
6238 * an error, but if it filled in uuid_buf we will at
6239 * lease use that to print out a message for that OBD.
6240 * If we didn't get anything in the uuid_buf, then fill
6241 * it in so that we can print an error message.
6243 if (uuid_buf.uuid[0] == '\0')
6244 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6245 "%s%04x", tp->st_name, index);
6247 lsb->sb_buf[lsb->sb_count].sd_index = index;
6248 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6251 if (flags & MNTDF_SHOW)
6252 showdf(mntdir, &stat_buf,
6253 obd_uuid2str(&uuid_buf), flags,
6254 tp->st_name, index, rc2);
6259 if (tp->st_op == LL_STATFS_LMV) {
6260 sum.os_ffree += stat_buf.os_ffree;
6261 sum.os_files += stat_buf.os_files;
6262 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6263 ost_files += stat_buf.os_files;
6264 ost_ffree += stat_buf.os_ffree;
6266 sum.os_blocks += stat_buf.os_blocks *
6268 sum.os_bfree += stat_buf.os_bfree *
6270 sum.os_bavail += stat_buf.os_bavail *
6278 * If we have _some_ OSTs, but don't have as many free objects on the
6279 * OST as inodes on the MDTs, reduce the reported number of inodes
6280 * to compensate, so that the "inodes in use" number is correct.
6281 * This should be kept in sync with ll_statfs_internal().
6283 if (ost_files && ost_ffree < sum.os_ffree) {
6284 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6285 sum.os_ffree = ost_ffree;
6287 if (flags & MNTDF_SHOW) {
6289 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6297 static int lfs_setdirstripe(int argc, char **argv)
6300 struct lfs_setstripe_args lsa = { 0 };
6301 struct llapi_stripe_param *param = NULL;
6302 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6305 char *mode_opt = NULL;
6306 bool default_stripe = false;
6307 bool delete = false;
6308 bool foreign_mode = false;
6309 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6310 mode_t previous_mode = 0;
6312 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6313 struct option long_opts[] = {
6314 { .val = 'c', .name = "count", .has_arg = required_argument },
6315 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6316 { .val = 'd', .name = "delete", .has_arg = no_argument },
6317 { .val = 'D', .name = "default", .has_arg = no_argument },
6318 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6319 { .val = LFS_LAYOUT_FLAGS_OPT,
6320 .name = "flags", .has_arg = required_argument },
6321 { .val = LFS_LAYOUT_FOREIGN_OPT,
6322 .name = "foreign", .has_arg = optional_argument},
6323 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6324 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6325 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6326 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6328 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6329 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6330 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6332 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6333 { .val = 'i', .name = "index", .has_arg = required_argument },
6335 { .val = 'o', .name = "mode", .has_arg = required_argument },
6336 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6337 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6339 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6340 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6341 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6345 setstripe_args_init(&lsa);
6347 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
6348 long_opts, NULL)) >= 0) {
6356 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6357 if (errno != 0 || *end != '\0' ||
6358 lsa.lsa_stripe_count < -1 ||
6359 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6361 "%s %s: invalid stripe count '%s'\n",
6362 progname, argv[0], optarg);
6368 default_stripe = true;
6371 default_stripe = true;
6373 case LFS_LAYOUT_FOREIGN_OPT:
6375 /* check pure numeric */
6376 type = strtoul(optarg, &end, 0);
6379 type = check_foreign_type_name(optarg);
6380 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6382 "%s %s: unknown foreign type '%s'\n",
6387 } else if (type >= UINT32_MAX) {
6389 "%s %s: invalid foreign type '%s'\n",
6390 progname, argv[0], optarg);
6394 foreign_mode = true;
6396 case LFS_LAYOUT_FLAGS_OPT:
6398 flags = strtoul(optarg, &end, 16);
6399 if (errno != 0 || *end != '\0' ||
6400 flags >= UINT32_MAX) {
6402 "%s %s: invalid hex flags '%s'\n",
6403 progname, argv[0], optarg);
6406 if (!foreign_mode) {
6408 "%s %s: hex flags must be specified with --foreign option\n",
6413 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6416 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6419 lsa.lsa_pattern = check_hashtype(optarg);
6420 if (lsa.lsa_pattern == 0) {
6422 "%s %s: bad stripe hash type '%s'\n",
6423 progname, argv[0], optarg);
6428 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6431 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6432 if (strcmp(argv[optind - 1], "--index") == 0)
6434 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6437 lsa.lsa_nr_tgts = parse_targets(mdts,
6438 sizeof(mdts) / sizeof(__u32),
6439 lsa.lsa_nr_tgts, optarg, NULL);
6440 if (lsa.lsa_nr_tgts < 0) {
6442 "%s %s: invalid MDT target(s) '%s'\n",
6443 progname, argv[0], optarg);
6447 lsa.lsa_tgts = mdts;
6448 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6449 lsa.lsa_stripe_off = mdts[0];
6451 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6454 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6463 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6464 progname, argv[0], argv[optind - 1]);
6469 if (optind == argc) {
6470 fprintf(stderr, "%s %s: DIR must be specified\n",
6475 if (xattr && !foreign_mode) {
6477 * only print a warning as this is armless and will be
6481 "%s %s: xattr has been specified for non-foreign layout\n",
6483 } else if (foreign_mode && !xattr) {
6485 "%s %s: xattr must be provided in foreign mode\n",
6490 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6491 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6493 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6498 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6499 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6500 /* if no parameters set, create directory on least-used MDTs */
6501 lsa.lsa_stripe_off = -1;
6502 lsa.lsa_stripe_count = 1;
6506 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6507 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6509 "%s %s: cannot specify -d with -c or -i options\n",
6515 mode = strtoul(mode_opt, &end, 8);
6518 "%s %s: bad MODE '%s'\n",
6519 progname, argv[0], mode_opt);
6522 previous_mode = umask(0);
6525 /* foreign LMV/dir case */
6527 if (argc > optind + 1) {
6529 "%s %s: cannot specify multiple foreign dirs\n",
6534 dname = argv[optind];
6535 result = llapi_dir_create_foreign(dname, mode, type, flags,
6539 "%s mkdir: can't create foreign dir '%s': %s\n",
6540 progname, dname, strerror(-result));
6545 * initialize stripe parameters, in case param is converted to specific,
6546 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6548 param = calloc(1, offsetof(typeof(*param),
6549 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6550 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6553 "%s %s: cannot allocate memory for parameters: %s\n",
6554 progname, argv[0], strerror(ENOMEM));
6558 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6559 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6560 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6561 lsa.lsa_stripe_count == 0))
6562 lsa.lsa_stripe_count = 1;
6563 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6564 param->lsp_stripe_count = lsa.lsa_stripe_count;
6565 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6566 param->lsp_stripe_offset = -1;
6568 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6569 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6570 param->lsp_stripe_pattern = lsa.lsa_pattern;
6572 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6573 param->lsp_pool = lsa.lsa_pool_name;
6574 param->lsp_is_specific = false;
6575 if (lsa.lsa_nr_tgts > 1) {
6576 if (lsa.lsa_stripe_count > 0 &&
6577 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6578 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6580 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6581 argv[0], lsa.lsa_stripe_count,
6587 param->lsp_is_specific = true;
6588 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6589 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6592 dname = argv[optind];
6594 if (default_stripe) {
6595 result = llapi_dir_set_default_lmv(dname, param);
6598 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6599 progname, dname, strerror(-result));
6603 result = llapi_dir_create(dname, mode, param);
6606 "%s setdirstripe: cannot create dir '%s': %s\n",
6607 progname, dname, strerror(-result));
6608 } while (!result && (dname = argv[++optind]));
6611 umask(previous_mode);
6618 static int lfs_rmentry(int argc, char **argv)
6625 fprintf(stderr, "error: %s: missing dirname\n",
6631 dname = argv[index];
6633 result = llapi_direntry_remove(dname);
6636 "error: %s: remove dir entry '%s' failed\n",
6640 dname = argv[++index];
6645 static int lfs_unlink_foreign(int argc, char **argv)
6652 fprintf(stderr, "error: %s: missing pathname\n",
6659 while (name != NULL) {
6660 result = llapi_unlink_foreign(name);
6663 "error: %s: unlink foreign entry '%s' failed\n",
6667 name = argv[++index];
6672 static int lfs_mv(int argc, char **argv)
6674 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6675 struct find_param param = {
6682 struct option long_opts[] = {
6683 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6684 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6685 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6688 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6690 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6693 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6697 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6698 if (errno != 0 || *end != '\0' ||
6699 lmu.lum_stripe_offset >= UINT32_MAX) {
6700 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6706 param.fp_verbose = VERBOSE_DETAIL;
6709 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6710 progname, argv[optind - 1]);
6715 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6716 fprintf(stderr, "%s mv: MDT index must be specified\n",
6721 if (optind >= argc) {
6722 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6726 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6728 /* initialize migrate mdt parameters */
6729 param.fp_lmv_md = &lmu;
6730 param.fp_migrate = 1;
6731 rc = llapi_migrate_mdt(argv[optind], ¶m);
6733 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6734 progname, argv[optind], lmu.lum_stripe_offset,
6739 static int lfs_osts(int argc, char **argv)
6741 return lfs_tgts(argc, argv);
6744 static int lfs_mdts(int argc, char **argv)
6746 return lfs_tgts(argc, argv);
6749 static int lfs_df(int argc, char **argv)
6751 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6752 enum mntdf_flags flags = MNTDF_SHOW;
6753 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6754 int c, rc = 0, index = 0;
6755 char fsname[PATH_MAX] = "", *pool_name = NULL;
6756 struct option long_opts[] = {
6757 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
6758 { .val = 'H', .name = "si", .has_arg = no_argument },
6759 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6760 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6761 { .val = 'p', .name = "pool", .has_arg = required_argument },
6762 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6765 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
6768 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
6771 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
6774 flags |= MNTDF_INODES;
6777 flags |= MNTDF_LAZY;
6783 flags |= MNTDF_VERBOSE;
6789 if (optind < argc && !realpath(argv[optind], path)) {
6791 fprintf(stderr, "error: invalid path '%s': %s\n",
6792 argv[optind], strerror(-rc));
6796 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6797 /* Check if we have a mount point */
6798 if (mntdir[0] == '\0')
6801 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6802 if (rc || path[0] != '\0')
6804 fsname[0] = '\0'; /* avoid matching in next loop */
6805 mntdir[0] = '\0'; /* avoid matching in next loop */
6811 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6812 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6816 if (opt_fsname == opt_instance) { /* both true or both false */
6817 rc = llapi_getname(mntdir, buf, buflen);
6818 } else if (opt_fsname) {
6820 * llapi_search_mounts() fills @buf with fsname, but that is not
6821 * called if explicit paths are specified on the command-line
6824 rc = llapi_get_fsname(mntdir, buf, buflen);
6825 } else /* if (opt_instance) */ {
6826 rc = llapi_get_instance(mntdir, buf, buflen);
6830 fprintf(stderr, "cannot get instance for '%s': %s\n",
6831 mntdir, strerror(-rc));
6836 printf("%s %s\n", buf, mntdir);
6838 printf("%s\n", buf);
6843 static int lfs_getname(int argc, char **argv)
6845 struct option long_opts[] = {
6846 { .val = 'h', .name = "help", .has_arg = no_argument },
6847 { .val = 'i', .name = "instance", .has_arg = no_argument },
6848 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6850 bool opt_instance = false, opt_fsname = false;
6851 char fsname[PATH_MAX] = "";
6854 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6857 opt_instance = true;
6868 if (optind == argc) { /* no paths specified, get all paths. */
6869 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6872 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6873 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6874 opt_instance, opt_fsname, true);
6877 path[0] = fsname[0] = mntdir[0] = '\0';
6879 } else { /* paths specified, only attempt to search these. */
6882 /* if only one path is given, print only requested info */
6883 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6885 for (; optind < argc; optind++) {
6886 rc2 = print_instance(argv[optind], fsname,
6887 sizeof(fsname), opt_instance,
6888 opt_fsname, opt_mntdir);
6898 static int lfs_check(int argc, char **argv)
6900 char mntdir[PATH_MAX] = {'\0'};
6909 fprintf(stderr, "%s check: server type must be specified\n",
6914 obd_types[0] = obd_type1;
6915 obd_types[1] = obd_type2;
6916 obd_types[2] = obd_type3;
6918 if (strcmp(argv[1], "osts") == 0) {
6919 strcpy(obd_types[0], "osc");
6920 } else if (strcmp(argv[1], "mdts") == 0 ||
6921 strcmp(argv[1], "mds") == 0) {
6922 strcpy(obd_types[0], "mdc");
6923 } else if (strcmp(argv[1], "mgts") == 0) {
6924 strcpy(obd_types[0], "mgc");
6925 } else if (strcmp(argv[1], "all") == 0 ||
6926 strcmp(argv[1], "servers") == 0) {
6928 strcpy(obd_types[0], "osc");
6929 strcpy(obd_types[1], "mdc");
6930 strcpy(obd_types[2], "mgc");
6932 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6937 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6938 if (rc < 0 || mntdir[0] == '\0') {
6940 "%s check: cannot find mounted Lustre filesystem: %s\n",
6941 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6945 rc = llapi_target_check(num_types, obd_types, mntdir);
6947 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6948 progname, argv[1], strerror(-rc));
6953 #ifdef HAVE_SYS_QUOTA_H
6954 #define ADD_OVERFLOW(a, b) \
6955 ((((a) + (b)) < (a)) ? \
6956 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
6958 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6959 * returns the value or ULONG_MAX on integer overflow or incorrect format
6961 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6962 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6963 * 3. empty integer value is interpreted as 0
6965 static unsigned long str2sec(const char *timestr)
6967 const char spec[] = "smhdw";
6968 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6969 unsigned long val = 0;
6972 if (strpbrk(timestr, spec) == NULL) {
6974 * no specifiers inside the time string,
6975 * should treat it as an integer value
6977 val = strtoul(timestr, &tail, 10);
6978 return *tail ? ULONG_MAX : val;
6981 /* format string is XXwXXdXXhXXmXXs */
6987 v = strtoul(timestr, &tail, 10);
6988 if (v == ULONG_MAX || *tail == '\0')
6990 * value too large (ULONG_MAX or more)
6991 * or missing specifier
6995 ptr = strchr(spec, *tail);
6997 /* unknown specifier */
7002 /* check if product will overflow the type */
7003 if (!(v < ULONG_MAX / mult[ind]))
7006 ADD_OVERFLOW(val, mult[ind] * v);
7007 if (val == ULONG_MAX)
7019 #define ARG2ULL(nr, str, def_units) \
7021 unsigned long long limit, units = def_units; \
7024 rc = llapi_parse_size(str, &limit, &units, 1); \
7026 fprintf(stderr, "%s: invalid limit '%s'\n", \
7033 static inline int has_times_option(int argc, char **argv)
7037 for (i = 1; i < argc; i++)
7038 if (!strcmp(argv[i], "-t"))
7044 static inline int lfs_verify_poolarg(char *pool)
7046 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7048 "Pool name '%.*s' is longer than %d\n",
7049 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7055 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7058 char *mnt, *obd_type = (char *)qctl->obd_type;
7059 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7060 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7061 struct option long_opts[] = {
7062 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7063 { .val = 'g', .name = "group", .has_arg = no_argument },
7064 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7065 { .val = 'p', .name = "projid", .has_arg = no_argument },
7066 { .val = 't', .name = "times", .has_arg = no_argument },
7067 { .val = 'u', .name = "user", .has_arg = no_argument },
7068 { .val = LFS_POOL_OPT,
7069 .name = "pool", .has_arg = required_argument },
7073 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7074 qctl->qc_type = ALLQUOTA;
7076 while ((c = getopt_long(argc, argv, "b:gi:ptu",
7077 long_opts, NULL)) != -1) {
7088 if (qctl->qc_type != ALLQUOTA) {
7090 "error: -u/g/p can't be used more than once\n");
7093 qctl->qc_type = qtype;
7096 if (strncmp(optarg, NOTIFY_GRACE,
7097 strlen(NOTIFY_GRACE)) == 0) {
7098 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7100 dqi->dqi_bgrace = str2sec(optarg);
7101 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7103 "error: bad block-grace: %s\n",
7108 dqb->dqb_valid |= QIF_BTIME;
7111 if (strncmp(optarg, NOTIFY_GRACE,
7112 strlen(NOTIFY_GRACE)) == 0) {
7113 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7115 dqi->dqi_igrace = str2sec(optarg);
7116 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7118 "error: bad inode-grace: %s\n",
7123 dqb->dqb_valid |= QIF_ITIME;
7125 case 't': /* Yes, of course! */
7128 if (lfs_verify_poolarg(optarg))
7131 "Trying to set grace for pool %s\n", optarg);
7132 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7133 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7135 /* getopt prints error message for us when opterr != 0 */
7141 if (qctl->qc_type == ALLQUOTA) {
7142 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
7146 if (optind != argc - 1) {
7147 fprintf(stderr, "error: unexpected parameters encountered\n");
7152 rc = llapi_quotactl(mnt, qctl);
7155 fprintf(stderr, "%s %s ", obd_type,
7156 obd_uuid2str(&qctl->obd_uuid));
7157 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7164 #define BSLIMIT (1 << 0)
7165 #define BHLIMIT (1 << 1)
7166 #define ISLIMIT (1 << 2)
7167 #define IHLIMIT (1 << 3)
7169 int lfs_setquota(int argc, char **argv)
7172 struct if_quotactl *qctl;
7173 char *mnt, *obd_type;
7174 struct obd_dqblk *dqb;
7175 struct option long_opts[] = {
7176 { .val = 'b', .name = "block-softlimit",
7177 .has_arg = required_argument },
7178 { .val = 'B', .name = "block-hardlimit",
7179 .has_arg = required_argument },
7180 { .val = 'd', .name = "default", .has_arg = no_argument },
7181 { .val = 'g', .name = "group", .has_arg = required_argument },
7182 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7183 { .val = 'i', .name = "inode-softlimit",
7184 .has_arg = required_argument },
7185 { .val = 'I', .name = "inode-hardlimit",
7186 .has_arg = required_argument },
7187 { .val = 'p', .name = "projid", .has_arg = required_argument },
7188 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7189 { .val = 'u', .name = "user", .has_arg = required_argument },
7190 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7191 { .val = LFS_POOL_OPT,
7192 .name = "pool", .has_arg = required_argument },
7194 unsigned int limit_mask = 0;
7195 bool use_default = false;
7196 int qtype, qctl_len;
7198 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7199 qctl = malloc(qctl_len);
7203 memset(qctl, 0, qctl_len);
7204 obd_type = (char *)qctl->obd_type;
7205 dqb = &qctl->qc_dqblk;
7207 if (has_times_option(argc, argv)) {
7208 rc = lfs_setquota_times(argc, argv, qctl);
7212 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7213 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7214 * so it can be used as a marker that qc_type
7215 * isn't reinitialized from command line
7218 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7219 long_opts, NULL)) != -1) {
7222 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7225 goto quota_type_def;
7228 rc = name2uid(&qctl->qc_id, optarg);
7231 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7234 goto quota_type_def;
7237 rc = name2gid(&qctl->qc_id, optarg);
7240 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7243 goto quota_type_def;
7246 rc = name2projid(&qctl->qc_id, optarg);
7249 if (str2quotaid(&qctl->qc_id, optarg)) {
7251 "%s setquota: invalid id '%s'\n",
7258 if (qctl->qc_id == 0) {
7260 "%s setquota: can't set quota for root usr/group/project.\n",
7267 if (qctl->qc_type != ALLQUOTA) {
7269 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7274 qctl->qc_type = qtype;
7277 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7281 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7282 dqb->dqb_bsoftlimit >>= 10;
7283 limit_mask |= BSLIMIT;
7284 if (dqb->dqb_bsoftlimit &&
7285 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7287 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7289 (unsigned long long)dqb->dqb_bsoftlimit,
7293 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7294 dqb->dqb_bhardlimit >>= 10;
7295 limit_mask |= BHLIMIT;
7296 if (dqb->dqb_bhardlimit &&
7297 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7299 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7300 "See '%s help setquota' or Lustre manual for details\n",
7302 (unsigned long long)dqb->dqb_bhardlimit,
7306 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7307 limit_mask |= ISLIMIT;
7308 if (dqb->dqb_isoftlimit &&
7309 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7311 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7313 (unsigned long long)dqb->dqb_isoftlimit,
7317 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7318 limit_mask |= IHLIMIT;
7319 if (dqb->dqb_ihardlimit &&
7320 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7322 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7324 (unsigned long long)dqb->dqb_ihardlimit,
7328 if (lfs_verify_poolarg(optarg)) {
7333 "Trying to set quota for pool %s\n", optarg);
7334 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7335 qctl->qc_cmd = LUSTRE_Q_SETQUOTAPOOL;
7339 "%s setquota: unrecognized option '%s'\n",
7340 progname, argv[optind - 1]);
7346 if (qctl->qc_type == ALLQUOTA) {
7348 "%s setquota: either -u or -g must be specified\n",
7354 if (!use_default && limit_mask == 0) {
7356 "%s setquota: at least one limit must be specified\n",
7362 if (use_default && limit_mask != 0) {
7364 "%s setquota: limits should not be specified when using default quota\n",
7370 if (use_default && qctl->qc_id == 0) {
7372 "%s setquota: can not set default quota for root user/group/project\n",
7378 if (optind != argc - 1) {
7380 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7381 progname, argv[optind]);
7389 dqb->dqb_bhardlimit = 0;
7390 dqb->dqb_bsoftlimit = 0;
7391 dqb->dqb_ihardlimit = 0;
7392 dqb->dqb_isoftlimit = 0;
7395 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7396 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7397 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7398 /* sigh, we can't just set blimits/ilimits */
7399 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7400 .qc_type = qctl->qc_type,
7401 .qc_id = qctl->qc_id};
7403 rc = llapi_quotactl(mnt, &tmp_qctl);
7407 if (!(limit_mask & BHLIMIT))
7408 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7409 if (!(limit_mask & BSLIMIT))
7410 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7411 if (!(limit_mask & IHLIMIT))
7412 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7413 if (!(limit_mask & ISLIMIT))
7414 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7416 /* Keep grace times if we have got no softlimit arguments */
7417 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7418 dqb->dqb_valid |= QIF_BTIME;
7419 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7422 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7423 dqb->dqb_valid |= QIF_ITIME;
7424 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7428 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7429 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7431 rc = llapi_quotactl(mnt, qctl);
7435 "%s setquota: cannot quotactl '%s' '%s': %s",
7437 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7444 /* Converts seconds value into format string
7445 * result is returned in buf
7447 * 1. result is in descenting order: 1w2d3h4m5s
7448 * 2. zero fields are not filled (except for p. 3): 5d1s
7449 * 3. zero seconds value is presented as "0s"
7451 static char *__sec2str(time_t seconds, char *buf)
7453 const char spec[] = "smhdw";
7454 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7459 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7460 c = seconds / mult[i];
7462 if (c > 0 || (i == 0 && buf == tail))
7463 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7472 static void sec2str(time_t seconds, char *buf, int rc)
7479 tail = __sec2str(seconds, tail);
7481 if (rc && tail - buf < 39) {
7487 static void diff2str(time_t seconds, char *buf, time_t now)
7492 if (seconds <= now) {
7493 strcpy(buf, "none");
7496 __sec2str(seconds - now, buf);
7499 static void print_quota_title(char *name, struct if_quotactl *qctl,
7500 bool human_readable, bool show_default)
7503 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7504 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7505 "Filesystem", "bquota", "blimit", "bgrace",
7506 "iquota", "ilimit", "igrace");
7508 printf("Disk quotas for %s %s (%cid %u):\n",
7509 qtype_name(qctl->qc_type), name,
7510 *qtype_name(qctl->qc_type), qctl->qc_id);
7511 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7512 "Filesystem", human_readable ? "used" : "kbytes",
7513 "quota", "limit", "grace",
7514 "files", "quota", "limit", "grace");
7518 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7521 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7524 snprintf(buf, buflen, "%5.4gP",
7525 (double)num / ((__u64)1 << 40));
7527 snprintf(buf, buflen, "%5.4gT",
7528 (double)num / (1 << 30));
7530 snprintf(buf, buflen, "%5.4gG",
7531 (double)num / (1 << 20));
7533 snprintf(buf, buflen, "%5.4gM",
7534 (double)num / (1 << 10));
7536 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7540 #define STRBUF_LEN 32
7541 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7542 int rc, bool h, bool show_default)
7548 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7549 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7550 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
7551 int bover = 0, iover = 0;
7552 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7553 char numbuf[3][STRBUF_LEN];
7555 char strbuf[STRBUF_LEN];
7557 if (dqb->dqb_bhardlimit &&
7558 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7560 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7561 if (dqb->dqb_btime > now)
7567 if (dqb->dqb_ihardlimit &&
7568 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7570 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7571 if (dqb->dqb_itime > now)
7577 if (strlen(mnt) > 15)
7578 printf("%s\n%15s", mnt, "");
7580 printf("%15s", mnt);
7583 diff2str(dqb->dqb_btime, timebuf, now);
7584 else if (show_default)
7585 snprintf(timebuf, sizeof(timebuf), "%llu",
7586 (unsigned long long)dqb->dqb_btime);
7588 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7589 strbuf, sizeof(strbuf), h);
7590 if (rc == -EREMOTEIO)
7591 sprintf(numbuf[0], "%s*", strbuf);
7593 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7594 "%s" : "[%s]", strbuf);
7596 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7597 if (type == QC_GENERAL)
7598 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7599 "%s" : "[%s]", strbuf);
7601 sprintf(numbuf[1], "%s", "-");
7603 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7604 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7605 "%s" : "[%s]", strbuf);
7608 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7610 printf(" %7s%c %6s %7s %7s",
7611 numbuf[0], bover ? '*' : ' ', numbuf[1],
7612 numbuf[2], bover > 1 ? timebuf : "-");
7615 diff2str(dqb->dqb_itime, timebuf, now);
7616 else if (show_default)
7617 snprintf(timebuf, sizeof(timebuf), "%llu",
7618 (unsigned long long)dqb->dqb_itime);
7620 snprintf(numbuf[0], sizeof(numbuf),
7621 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7622 (uintmax_t)dqb->dqb_curinodes);
7624 if (type == QC_GENERAL)
7625 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7627 (uintmax_t)dqb->dqb_isoftlimit);
7629 sprintf(numbuf[1], "%s", "-");
7631 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7632 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7635 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7636 else if (type != QC_OSTIDX)
7637 printf(" %7s%c %6s %7s %7s",
7638 numbuf[0], iover ? '*' : ' ', numbuf[1],
7639 numbuf[2], iover > 1 ? timebuf : "-");
7641 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7643 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
7644 qctl->qc_cmd == Q_GETOINFO) {
7648 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7649 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7651 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7652 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7653 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7655 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7657 printf("Block grace time: %s; Inode grace time: %s\n",
7658 bgtimebuf, igtimebuf);
7662 static int tgt_name2index(const char *tgtname, unsigned int *idx)
7666 /* format is "lustre-OST0001" */
7667 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
7669 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
7674 *idx = strtoul(dash, &endp, 16);
7675 if (*idx > 0xffff) {
7676 fprintf(stderr, "wrong index %s\n", tgtname);
7683 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7684 bool h, __u64 *total)
7686 int rc = 0, rc1 = 0, count = 0, i = 0;
7687 char **list = NULL, *buffer = NULL;
7688 __u32 valid = qctl->qc_valid;
7690 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
7693 /* Is it correct for the case OST0000, OST0002, OST0003 -
7694 * we will ask OST0001 that is absent and won't ask OST0003? */
7695 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7697 fprintf(stderr, "can not get %s count: %s\n",
7698 is_mdt ? "mdt" : "ost", strerror(-rc));
7702 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7703 char fname[PATH_MAX];
7704 char fsname[LUSTRE_MAXFSNAME + 1];
7705 int bufsize = sizeof(struct obd_uuid) * count;
7707 rc = llapi_search_fsname(mnt, fsname);
7709 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
7713 buffer = malloc(bufsize + sizeof(*list) * count);
7716 list = (char **)(buffer + bufsize);
7717 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
7718 count = llapi_get_poolmembers(fname, list, count,
7724 for (i = 0; i < count; i++) {
7725 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
7728 if (tgt_name2index(list[i], &index))
7730 qctl->qc_idx = index;
7735 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7736 rc = llapi_quotactl(mnt, qctl);
7738 /* It is remote client case. */
7739 if (rc == -EOPNOTSUPP) {
7746 fprintf(stderr, "quotactl %s%d failed.\n",
7747 is_mdt ? "mdt" : "ost", qctl->qc_idx);
7751 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7752 qctl->qc_valid, 0, h, false);
7753 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7754 qctl->qc_dqblk.dqb_bhardlimit;
7759 qctl->qc_valid = valid;
7763 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7764 int verbose, int quiet, bool human_readable,
7767 int rc1 = 0, rc2 = 0, rc3 = 0;
7768 char *obd_type = (char *)qctl->obd_type;
7769 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7770 __u64 total_ialloc = 0, total_balloc = 0;
7771 bool use_default_for_blk = false;
7772 bool use_default_for_file = false;
7775 rc1 = llapi_quotactl(mnt, qctl);
7779 fprintf(stderr, "%s quotas are not enabled.\n",
7780 qtype_name(qctl->qc_type));
7783 fprintf(stderr, "Permission denied.\n");
7786 /* We already got error message. */
7789 fprintf(stderr, "Unexpected quotactl error: %s\n",
7794 if (!show_default && qctl->qc_id == 0) {
7795 qctl->qc_dqblk.dqb_bhardlimit = 0;
7796 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7797 qctl->qc_dqblk.dqb_ihardlimit = 0;
7798 qctl->qc_dqblk.dqb_isoftlimit = 0;
7799 qctl->qc_dqblk.dqb_btime = 0;
7800 qctl->qc_dqblk.dqb_itime = 0;
7801 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7804 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7805 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7806 use_default_for_blk = true;
7807 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7810 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7811 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7812 use_default_for_file = true;
7813 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7816 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7817 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7818 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7819 print_quota_title(name, qctl, human_readable, show_default);
7821 if (rc1 && *obd_type)
7822 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7824 if (qctl->qc_valid != QC_GENERAL)
7827 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7828 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
7829 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7830 (QIF_LIMITS|QIF_USAGE));
7832 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7834 if (!show_default && verbose &&
7835 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
7836 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
7837 char strbuf[STRBUF_LEN];
7839 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7841 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7843 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7845 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
7846 (uintmax_t)total_ialloc, strbuf);
7849 if (use_default_for_blk)
7850 printf("%cid %u is using default block quota setting\n",
7851 *qtype_name(qctl->qc_type), qctl->qc_id);
7853 if (use_default_for_file)
7854 printf("%cid %u is using default file quota setting\n",
7855 *qtype_name(qctl->qc_type), qctl->qc_id);
7857 if (rc1 || rc2 || rc3 || inacc)
7858 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
7872 static int lfs_project(int argc, char **argv)
7874 int ret = 0, err = 0, c, i;
7875 struct project_handle_control phc = { 0 };
7876 enum lfs_project_ops_t op;
7879 phc.assign_projid = false;
7880 /* default action */
7881 op = LFS_PROJECT_LIST;
7883 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7886 if (op != LFS_PROJECT_LIST) {
7888 "%s: cannot specify '-c' '-C' '-s' together\n",
7893 op = LFS_PROJECT_CHECK;
7896 if (op != LFS_PROJECT_LIST) {
7898 "%s: cannot specify '-c' '-C' '-s' together\n",
7903 op = LFS_PROJECT_CLEAR;
7906 if (op != LFS_PROJECT_LIST) {
7908 "%s: cannot specify '-c' '-C' '-s' together\n",
7913 phc.set_inherit = true;
7914 op = LFS_PROJECT_SET;
7920 phc.keep_projid = true;
7923 phc.recursive = true;
7926 if (str2quotaid(&phc.projid, optarg)) {
7928 "Invalid project ID: %s\n",
7933 phc.assign_projid = true;
7937 phc.newline = false;
7940 fprintf(stderr, "%s: invalid option '%c'\n",
7946 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7947 op = LFS_PROJECT_SET;
7948 phc.set_projid = true;
7949 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7950 phc.set_projid = true;
7954 case LFS_PROJECT_CHECK:
7955 if (phc.keep_projid) {
7957 "%s: '-k' is useless together with '-c'\n",
7962 case LFS_PROJECT_CLEAR:
7965 "%s: '-0' is useless together with '-C'\n",
7969 if (phc.assign_projid) {
7971 "%s: '-p' is useless together with '-C'\n",
7976 case LFS_PROJECT_SET:
7979 "%s: '-0' is useless together with '-s'\n",
7983 if (phc.keep_projid) {
7985 "%s: '-k' is useless together with '-s'\n",
7993 "%s: '-0' is useless for list operations\n",
8003 fprintf(stderr, "%s: missing file or directory target(s)\n",
8008 for (i = 0; i < argc; i++) {
8010 case LFS_PROJECT_CHECK:
8011 err = lfs_project_check(argv[i], &phc);
8013 case LFS_PROJECT_LIST:
8014 err = lfs_project_list(argv[i], &phc);
8016 case LFS_PROJECT_CLEAR:
8017 err = lfs_project_clear(argv[i], &phc);
8019 case LFS_PROJECT_SET:
8020 err = lfs_project_set(argv[i], &phc);
8032 static int lfs_quota(int argc, char **argv)
8035 char *mnt, *name = NULL;
8036 struct if_quotactl *qctl;
8038 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8039 __u32 valid = QC_GENERAL, idx = 0;
8040 bool human_readable = false;
8041 bool show_default = false;
8043 struct option long_opts[] = {
8044 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
8047 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8051 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8052 qctl->qc_type = ALLQUOTA;
8053 obd_uuid = (char *)qctl->obd_uuid.uuid;
8055 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8056 long_opts, NULL)) != -1) {
8059 show_default = true;
8064 show_default = true;
8069 show_default = true;
8073 if (qctl->qc_type != ALLQUOTA) {
8075 "%s quota: only one of -u, -g, or -p may be specified\n",
8080 qctl->qc_type = qtype;
8083 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8086 valid = qctl->qc_valid = QC_UUID;
8087 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8090 valid = qctl->qc_valid = QC_MDTIDX;
8091 idx = qctl->qc_idx = atoi(optarg);
8092 if (idx == 0 && *optarg != '0') {
8094 "%s quota: invalid MDT index '%s'\n",
8101 valid = qctl->qc_valid = QC_OSTIDX;
8102 idx = qctl->qc_idx = atoi(optarg);
8103 if (idx == 0 && *optarg != '0') {
8105 "%s quota: invalid OST index '%s'\n",
8118 human_readable = true;
8121 if (lfs_verify_poolarg(optarg)) {
8125 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8126 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
8127 LUSTRE_Q_GETINFOPOOL :
8128 LUSTRE_Q_GETQUOTAPOOL;
8131 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8132 progname, argv[optind - 1]);
8138 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8139 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8140 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8141 qctl->qc_type == ALLQUOTA &&
8142 optind == argc - 1 && !show_default) {
8145 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8146 qctl->qc_type = qtype;
8147 qctl->qc_valid = valid;
8148 if (qtype == USRQUOTA) {
8149 qctl->qc_id = geteuid();
8150 rc = uid2name(&name, qctl->qc_id);
8152 qctl->qc_id = getegid();
8153 rc = gid2name(&name, qctl->qc_id);
8154 memset(&qctl->qc_dqblk, 0,
8155 sizeof(qctl->qc_dqblk));
8160 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8161 human_readable, show_default);
8166 /* lfs quota -u username /path/to/lustre/mount */
8167 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8168 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8169 /* options should be followed by u/g-name and mntpoint */
8170 if ((!show_default && optind + 2 != argc) ||
8171 (show_default && optind + 1 != argc) ||
8172 qctl->qc_type == ALLQUOTA) {
8174 "%s quota: name and mount point must be specified\n",
8180 if (!show_default) {
8181 name = argv[optind++];
8182 switch (qctl->qc_type) {
8184 rc = name2uid(&qctl->qc_id, name);
8187 rc = name2gid(&qctl->qc_id, name);
8190 rc = name2projid(&qctl->qc_id, name);
8197 qctl->qc_valid = QC_GENERAL;
8198 qctl->qc_cmd = LUSTRE_Q_GETDEFAULT;
8203 if (str2quotaid(&qctl->qc_id, name)) {
8204 fprintf(stderr, "%s quota: invalid id '%s'\n",
8210 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8211 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8218 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8219 human_readable, show_default);
8224 #endif /* HAVE_SYS_QUOTA_H! */
8226 static int flushctx_ioctl(char *mp)
8230 fd = open(mp, O_RDONLY);
8232 fprintf(stderr, "flushctx: error open %s: %s\n",
8233 mp, strerror(errno));
8237 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8239 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8240 mp, strerror(errno));
8246 static int lfs_flushctx(int argc, char **argv)
8248 int kdestroy = 0, reap = 0, c;
8249 char mntdir[PATH_MAX] = {'\0'};
8253 while ((c = getopt(argc, argv, "kr")) != -1) {
8263 "error: %s: option '-%c' unrecognized\n",
8270 rc = system("kdestroy > /dev/null");
8272 rc = WEXITSTATUS(rc);
8274 "error destroying tickets: %d, continuing\n",
8279 if (optind >= argc) {
8280 /* flush for all mounted lustre fs. */
8281 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8282 /* Check if we have a mount point */
8283 if (mntdir[0] == '\0')
8286 if (flushctx_ioctl(mntdir))
8289 mntdir[0] = '\0'; /* avoid matching in next loop */
8292 /* flush fs as specified */
8293 while (optind < argc) {
8294 if (flushctx_ioctl(argv[optind++]))
8300 rc = system("keyctl reap > /dev/null");
8302 rc = WEXITSTATUS(rc);
8303 fprintf(stderr, "error reaping keyring: %d\n", rc);
8310 static int lfs_changelog(int argc, char **argv)
8312 void *changelog_priv;
8313 struct changelog_rec *rec;
8314 long long startrec = 0, endrec = 0;
8316 struct option long_opts[] = {
8317 { .val = 'f', .name = "follow", .has_arg = no_argument },
8319 char short_opts[] = "f";
8322 while ((rc = getopt_long(argc, argv, short_opts,
8323 long_opts, NULL)) != -1) {
8330 "%s changelog: unrecognized option '%s'\n",
8331 progname, argv[optind - 1]);
8335 if (optind >= argc) {
8336 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8341 mdd = argv[optind++];
8342 if (argc > optind) {
8344 startrec = strtoll(argv[optind++], NULL, 10);
8345 if (errno != 0 || startrec < 0) {
8347 "%s changelog: bad startrec\n",
8353 if (argc > optind) {
8355 endrec = strtoll(argv[optind++], NULL, 10);
8356 if (errno != 0 || endrec < 0) {
8358 "%s changelog: bad endrec\n",
8364 rc = llapi_changelog_start(&changelog_priv,
8365 CHANGELOG_FLAG_BLOCK |
8366 CHANGELOG_FLAG_JOBID |
8367 CHANGELOG_FLAG_EXTRA_FLAGS |
8368 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8371 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8372 progname, strerror(errno = -rc));
8376 rc = llapi_changelog_set_xflags(changelog_priv,
8377 CHANGELOG_EXTRA_FLAG_UIDGID |
8378 CHANGELOG_EXTRA_FLAG_NID |
8379 CHANGELOG_EXTRA_FLAG_OMODE |
8380 CHANGELOG_EXTRA_FLAG_XATTR);
8383 "%s changelog: cannot set xflags for changelog: %s\n",
8384 progname, strerror(errno = -rc));
8388 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8392 if (endrec && rec->cr_index > endrec) {
8393 llapi_changelog_free(&rec);
8396 if (rec->cr_index < startrec) {
8397 llapi_changelog_free(&rec);
8401 secs = rec->cr_time >> 30;
8402 gmtime_r(&secs, &ts);
8403 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8404 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8405 changelog_type2str(rec->cr_type),
8406 ts.tm_hour, ts.tm_min, ts.tm_sec,
8407 (int)(rec->cr_time & ((1 << 30) - 1)),
8408 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8409 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8411 if (rec->cr_flags & CLF_JOBID) {
8412 struct changelog_ext_jobid *jid =
8413 changelog_rec_jobid(rec);
8415 if (jid->cr_jobid[0] != '\0')
8416 printf(" j=%s", jid->cr_jobid);
8419 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8420 struct changelog_ext_extra_flags *ef =
8421 changelog_rec_extra_flags(rec);
8423 printf(" ef=0x%llx",
8424 (unsigned long long)ef->cr_extra_flags);
8426 if (ef->cr_extra_flags & CLFE_UIDGID) {
8427 struct changelog_ext_uidgid *uidgid =
8428 changelog_rec_uidgid(rec);
8430 printf(" u=%llu:%llu",
8431 (unsigned long long)uidgid->cr_uid,
8432 (unsigned long long)uidgid->cr_gid);
8434 if (ef->cr_extra_flags & CLFE_NID) {
8435 struct changelog_ext_nid *nid =
8436 changelog_rec_nid(rec);
8439 libcfs_nid2str(nid->cr_nid));
8442 if (ef->cr_extra_flags & CLFE_OPEN) {
8443 struct changelog_ext_openmode *omd =
8444 changelog_rec_openmode(rec);
8445 char mode[] = "---";
8447 /* exec mode must be exclusive */
8448 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8451 if (omd->cr_openflags & MDS_FMODE_READ)
8453 if (omd->cr_openflags &
8460 if (strcmp(mode, "---") != 0)
8461 printf(" m=%s", mode);
8464 if (ef->cr_extra_flags & CLFE_XATTR) {
8465 struct changelog_ext_xattr *xattr =
8466 changelog_rec_xattr(rec);
8468 if (xattr->cr_xattr[0] != '\0')
8469 printf(" x=%s", xattr->cr_xattr);
8473 if (!fid_is_zero(&rec->cr_pfid))
8474 printf(" p="DFID, PFID(&rec->cr_pfid));
8475 if (rec->cr_namelen)
8476 printf(" %.*s", rec->cr_namelen,
8477 changelog_rec_name(rec));
8479 if (rec->cr_flags & CLF_RENAME) {
8480 struct changelog_ext_rename *rnm =
8481 changelog_rec_rename(rec);
8483 if (!fid_is_zero(&rnm->cr_sfid))
8484 printf(" s="DFID" sp="DFID" %.*s",
8485 PFID(&rnm->cr_sfid),
8486 PFID(&rnm->cr_spfid),
8487 (int)changelog_rec_snamelen(rec),
8488 changelog_rec_sname(rec));
8492 llapi_changelog_free(&rec);
8495 llapi_changelog_fini(&changelog_priv);
8498 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8499 progname, strerror(errno = -rc));
8501 return (rc == 1 ? 0 : rc);
8504 static int lfs_changelog_clear(int argc, char **argv)
8513 endrec = strtoll(argv[3], NULL, 10);
8514 if (errno != 0 || endrec < 0) {
8516 "%s: bad endrec '%s'\n",
8521 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8524 fprintf(stderr, "%s: record out of range: %llu\n",
8526 else if (rc == -ENOENT)
8527 fprintf(stderr, "%s: no changelog user: %s\n",
8530 fprintf(stderr, "%s error: %s\n", argv[0],
8539 static void rstripc(char *str, int c)
8541 char *end = str + strlen(str);
8543 for (; str < end && end[-1] == c; --end)
8547 static int lfs_fid2path(int argc, char **argv)
8549 struct option long_opts[] = {
8550 { .val = 'c', .name = "cur", .has_arg = no_argument },
8551 { .val = 'c', .name = "current", .has_arg = no_argument },
8552 { .val = 'c', .name = "print-link", .has_arg = no_argument },
8553 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
8554 { .val = 'l', .name = "link", .has_arg = required_argument },
8556 char short_opts[] = "cfl:pr:";
8557 bool print_link = false;
8558 bool print_fid = false;
8560 char mnt_dir[PATH_MAX] = "";
8562 char *path_or_fsname;
8563 long long recno = -1;
8565 char *endptr = NULL;
8570 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
8580 linkno = strtol(optarg, &endptr, 10);
8581 if (errno != 0 || *endptr != '\0' || linkno < 0) {
8583 "%s fid2path: invalid linkno '%s'\n",
8589 /* recno is something to do with changelogs
8590 * that was never implemented. We just pass it
8591 * through for the MDT to ignore.
8594 recno = strtoll(optarg, &endptr, 10);
8595 if (errno != 0 || *endptr != '\0' || recno < 0) {
8597 "%s fid2path: invalid recno '%s'\n",
8604 "%s fid2path: unrecognized option '%s'\n",
8605 progname, argv[optind - 1]);
8610 if (argc - optind < 2) {
8612 "Usage: %s fid2path FSNAME|ROOT FID...\n",
8617 path_or_fsname = argv[optind];
8619 if (*path_or_fsname == '/') {
8620 print_mnt_dir = true;
8621 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
8623 print_mnt_dir = false;
8624 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
8629 "%s fid2path: cannot resolve mount point for '%s': %s\n",
8630 progname, path_or_fsname, strerror(-rc));
8634 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
8637 "%s fid2path: cannot open mount point for '%s': %s\n",
8638 progname, path_or_fsname, strerror(-rc));
8642 /* Strip trailing slashes from mnt_dir. */
8643 rstripc(mnt_dir + 1, '/');
8645 for (i = optind + 1; i < argc; i++) {
8646 const char *fid_str = argv[i];
8650 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
8653 "%s fid2path: invalid FID '%s'\n",
8661 int linktmp = (linkno >= 0) ? linkno : 0;
8663 int oldtmp = linktmp;
8664 long long rectmp = recno;
8665 char path_buf[PATH_MAX];
8667 rc2 = llapi_fid2path_at(mnt_fd, &fid,
8668 path_buf, sizeof(path_buf), &rectmp, &linktmp);
8671 "%s fid2path: cannot find %s %s: %s\n",
8672 progname, path_or_fsname, fid_str,
8680 printf("%s ", fid_str);
8683 printf("%d ", linktmp);
8685 /* You may think this looks wrong or weird (and it is!)
8686 * but we are actually trying to preserve the old quirky
8687 * behaviors (enforced by our old quirky tests!) that
8688 * make lfs so much fun to work on:
8690 * lustre 0x200000007:0x1:0x0 => "/"
8691 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
8693 * Note that llapi_fid2path() returns "" for the root
8697 print_mnt_dir ? mnt_dir : "",
8698 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
8702 /* specified linkno */
8705 if (oldtmp == linktmp)
8717 static int lfs_path2fid(int argc, char **argv)
8719 struct option long_opts[] = {
8720 { .val = 'p', .name = "parents", .has_arg = no_argument },
8723 const char short_opts[] = "p";
8724 const char *sep = "";
8727 bool show_parents = false;
8729 while ((rc = getopt_long(argc, argv, short_opts,
8730 long_opts, NULL)) != -1) {
8733 show_parents = true;
8737 "%s path2fid: unrecognized option '%s'\n",
8738 progname, argv[optind - 1]);
8743 if (optind > argc - 1) {
8744 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
8747 } else if (optind < argc - 1) {
8752 for (path = argv + optind; *path != NULL; path++) {
8755 if (!show_parents) {
8756 err = llapi_path2fid(*path, &fid);
8758 printf("%s%s"DFID"\n",
8759 *sep != '\0' ? *path : "", sep,
8762 char name[NAME_MAX + 1];
8763 unsigned int linkno = 0;
8765 while ((err = llapi_path2parent(*path, linkno, &fid,
8766 name, sizeof(name))) == 0) {
8767 if (*sep != '\0' && linkno == 0)
8768 printf("%s%s", *path, sep);
8770 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
8775 /* err == -ENODATA is end-of-loop */
8776 if (linkno > 0 && err == -ENODATA) {
8784 "%s path2fid: cannot get %sfid for '%s': %s\n",
8785 progname, show_parents ? "parent " : "", *path,
8797 #define MAX_ERRNO 4095
8798 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8800 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8804 rc = llapi_rmfid(device, fa);
8806 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
8807 progname, strerror(-rc));
8811 for (k = 0; k < fa->fa_nr; k++) {
8812 rc2 = (__s32)fa->fa_fids[k].f_ver;
8813 if (!IS_ERR_VALUE(rc2))
8819 fa->fa_fids[k].f_ver = 0;
8820 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
8821 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
8827 static int lfs_rmfid(int argc, char **argv)
8829 char *fidstr, *device;
8830 int rc = 0, rc2, nr;
8831 struct fid_array *fa;
8833 if (optind > argc - 1) {
8834 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8838 device = argv[optind++];
8841 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8847 while (optind < argc) {
8850 fidstr = argv[optind++];
8851 while (*fidstr == '[')
8853 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8855 fprintf(stderr, "unrecognized FID: %s\n",
8860 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8861 /* start another batch */
8862 rc2 = lfs_rmfid_and_show_errors(device, fa);
8869 rc2 = lfs_rmfid_and_show_errors(device, fa);
8877 static int lfs_data_version(int argc, char **argv)
8884 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8887 fprintf(stderr, "%s data_version: FILE must be specified\n",
8892 while ((c = getopt(argc, argv, "nrw")) != -1) {
8895 data_version_flags = 0;
8898 data_version_flags |= LL_DV_RD_FLUSH;
8901 data_version_flags |= LL_DV_WR_FLUSH;
8905 "%s data_version: unrecognized option '%s'\n",
8906 progname, argv[optind - 1]);
8910 if (optind == argc) {
8911 fprintf(stderr, "%s data_version: FILE must be specified\n",
8916 path = argv[optind];
8917 fd = open(path, O_RDONLY);
8920 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8921 progname, path, strerror(-rc));
8925 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8928 "%s data_version: cannot get version for '%s': %s\n",
8929 progname, path, strerror(-rc));
8931 printf("%ju" "\n", (uintmax_t)data_version);
8937 static int lfs_hsm_state(int argc, char **argv)
8942 struct hsm_user_state hus;
8950 rc = llapi_hsm_state_get(path, &hus);
8952 fprintf(stderr, "can't get hsm state for %s: %s\n",
8953 path, strerror(errno = -rc));
8957 /* Display path name and status flags */
8958 printf("%s: (0x%08x)", path, hus.hus_states);
8960 if (hus.hus_states & HS_RELEASED)
8961 printf(" released");
8962 if (hus.hus_states & HS_EXISTS)
8964 if (hus.hus_states & HS_DIRTY)
8966 if (hus.hus_states & HS_ARCHIVED)
8967 printf(" archived");
8968 /* Display user-settable flags */
8969 if (hus.hus_states & HS_NORELEASE)
8970 printf(" never_release");
8971 if (hus.hus_states & HS_NOARCHIVE)
8972 printf(" never_archive");
8973 if (hus.hus_states & HS_LOST)
8974 printf(" lost_from_hsm");
8976 if (hus.hus_archive_id != 0)
8977 printf(", archive_id:%d", hus.hus_archive_id);
8980 } while (++i < argc);
8985 #define LFS_HSM_SET 0
8986 #define LFS_HSM_CLEAR 1
8989 * Generic function to set or clear HSM flags.
8990 * Used by hsm_set and hsm_clear.
8992 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8994 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8996 struct option long_opts[] = {
8997 { .val = 'A', .name = "archived", .has_arg = no_argument },
8998 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8999 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9000 { .val = 'e', .name = "exists", .has_arg = no_argument },
9001 { .val = 'l', .name = "lost", .has_arg = no_argument },
9002 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9003 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9005 char short_opts[] = "lraAdei:";
9009 __u32 archive_id = 0;
9015 while ((c = getopt_long(argc, argv, short_opts,
9016 long_opts, NULL)) != -1) {
9022 mask |= HS_NOARCHIVE;
9025 mask |= HS_ARCHIVED;
9028 mask |= HS_NORELEASE;
9038 archive_id = strtol(optarg, &end, 10);
9039 if (errno != 0 || *end != '\0' || archive_id < 0) {
9040 fprintf(stderr, "invalid archive_id: '%s'\n",
9048 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9049 argv[0], argv[optind - 1]);
9054 /* User should have specified a flag */
9058 while (optind < argc) {
9059 path = argv[optind];
9061 /* If mode == 0, this means we apply the mask. */
9062 if (mode == LFS_HSM_SET)
9063 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9065 rc = llapi_hsm_state_set(path, 0, mask, 0);
9068 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9069 path, strerror(errno = -rc));
9078 static int lfs_hsm_action(int argc, char **argv)
9083 struct hsm_current_action hca;
9084 struct hsm_extent he;
9085 enum hsm_user_action hua;
9086 enum hsm_progress_states hps;
9094 rc = llapi_hsm_current_action(path, &hca);
9096 fprintf(stderr, "can't get hsm action for %s: %s\n",
9097 path, strerror(errno = -rc));
9100 he = hca.hca_location;
9101 hua = hca.hca_action;
9102 hps = hca.hca_state;
9104 printf("%s: %s", path, hsm_user_action2name(hua));
9106 /* Skip file without action */
9107 if (hca.hca_action == HUA_NONE) {
9112 printf(" %s ", hsm_progress_state2name(hps));
9114 if ((hps == HPS_RUNNING) &&
9115 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9116 printf("(%llu bytes moved)\n",
9117 (unsigned long long)he.length);
9118 else if ((he.offset + he.length) == LUSTRE_EOF)
9119 printf("(from %llu to EOF)\n",
9120 (unsigned long long)he.offset);
9122 printf("(from %llu to %llu)\n",
9123 (unsigned long long)he.offset,
9124 (unsigned long long)(he.offset + he.length));
9126 } while (++i < argc);
9131 static int lfs_hsm_set(int argc, char **argv)
9133 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9136 static int lfs_hsm_clear(int argc, char **argv)
9138 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9142 * Check file state and return its fid, to be used by lfs_hsm_request().
9144 * \param[in] file Path to file to check
9145 * \param[in,out] fid Pointer to allocated lu_fid struct.
9146 * \param[in,out] last_dev Pointer to last device id used.
9148 * \return 0 on success.
9150 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9156 rc = lstat(file, &st);
9158 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9162 * Checking for regular file as archiving as posix copytool
9163 * rejects archiving files other than regular files
9165 if (!S_ISREG(st.st_mode)) {
9166 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9169 /* A request should be ... */
9170 if (*last_dev != st.st_dev && *last_dev != 0) {
9172 "All files should be on the same filesystem: %s\n",
9176 *last_dev = st.st_dev;
9178 rc = llapi_path2fid(file, fid);
9180 fprintf(stderr, "Cannot read FID of %s: %s\n",
9181 file, strerror(-rc));
9187 /* Fill an HSM HUR item with a given file name.
9189 * If mntpath is set, then the filename is actually a FID, and no
9190 * lookup on the filesystem will be performed.
9192 * \param[in] hur the user request to fill
9193 * \param[in] idx index of the item inside the HUR to fill
9194 * \param[in] mntpath mountpoint of Lustre
9195 * \param[in] fname filename (if mtnpath is NULL)
9196 * or FID (if mntpath is set)
9197 * \param[in] last_dev pointer to last device id used
9199 * \retval 0 on success
9200 * \retval CMD_HELP or a negative errno on error
9202 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9203 const char *mntpath, const char *fname,
9206 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9209 hui->hui_extent.length = -1;
9212 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9214 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9217 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9221 hur->hur_request.hr_itemcount++;
9226 static int lfs_hsm_request(int argc, char **argv, int action)
9228 struct option long_opts[] = {
9229 { .val = 'a', .name = "archive", .has_arg = required_argument },
9230 { .val = 'D', .name = "data", .has_arg = required_argument },
9231 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9232 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9235 char short_opts[] = "l:D:a:m:";
9236 struct hsm_user_request *hur, *oldhur;
9241 char *filelist = NULL;
9242 char fullpath[PATH_MAX];
9243 char *opaque = NULL;
9247 int nbfile_alloc = 0;
9248 char *some_file = NULL;
9249 char *mntpath = NULL;
9255 while ((c = getopt_long(argc, argv, short_opts,
9256 long_opts, NULL)) != -1) {
9265 if (action != HUA_ARCHIVE &&
9266 action != HUA_REMOVE) {
9268 "error: -a is supported only when archiving or removing\n");
9271 archive_id = atoi(optarg);
9276 some_file = strdup(optarg);
9282 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9283 argv[0], argv[optind - 1]);
9288 /* All remaining args are files, so we have at least nbfile */
9289 nbfile = argc - optind;
9291 if ((nbfile == 0) && (!filelist))
9295 opaque_len = strlen(opaque);
9298 * Alloc the request structure with enough place to store all files
9299 * from command line.
9301 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9303 fprintf(stderr, "Cannot create the request: %s\n",
9307 nbfile_alloc = nbfile;
9309 hur->hur_request.hr_action = action;
9310 hur->hur_request.hr_archive_id = archive_id;
9311 hur->hur_request.hr_flags = 0;
9313 /* All remaining args are files, add them */
9314 if (nbfile != 0 && some_file == NULL)
9315 some_file = strdup(argv[optind]);
9317 for (i = 0; i < nbfile; i++) {
9318 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9324 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9326 /* If a filelist was specified, read the filelist from it. */
9328 fp = fopen(filelist, "r");
9330 fprintf(stderr, "Cannot read the file list %s: %s\n",
9331 filelist, strerror(errno));
9336 while ((rc = getline(&line, &len, fp)) != -1) {
9338 * If allocated buffer was too small, get something
9341 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9344 nbfile_alloc = nbfile_alloc * 2 + 1;
9346 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9350 "hsm: cannot allocate the request: %s\n",
9357 size = hur_len(oldhur);
9360 "hsm: cannot allocate %u files + %u bytes data\n",
9361 oldhur->hur_request.hr_itemcount,
9362 oldhur->hur_request.hr_data_len);
9369 memcpy(hur, oldhur, size);
9374 if (line[strlen(line) - 1] == '\n')
9375 line[strlen(line) - 1] = '\0';
9377 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9378 mntpath, line, &last_dev);
9394 /* If a --data was used, add it to the request */
9395 hur->hur_request.hr_data_len = opaque_len;
9397 memcpy(hur_data(hur), opaque, opaque_len);
9399 /* Send the HSM request */
9400 if (realpath(some_file, fullpath) == NULL) {
9401 fprintf(stderr, "Could not find path '%s': %s\n",
9402 some_file, strerror(errno));
9404 rc = llapi_hsm_request(fullpath, hur);
9406 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9407 some_file, strerror(-rc));
9417 static int lfs_hsm_archive(int argc, char **argv)
9419 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9422 static int lfs_hsm_restore(int argc, char **argv)
9424 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9427 static int lfs_hsm_release(int argc, char **argv)
9429 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9432 static int lfs_hsm_remove(int argc, char **argv)
9434 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9437 static int lfs_hsm_cancel(int argc, char **argv)
9439 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9442 static int lfs_swap_layouts(int argc, char **argv)
9447 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9448 SWAP_LAYOUTS_KEEP_MTIME |
9449 SWAP_LAYOUTS_KEEP_ATIME);
9452 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9454 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9456 int lfs_get_mode(const char *string)
9458 enum lock_mode_user mode;
9460 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9461 if (lock_mode_names[mode] == NULL)
9463 if (strcmp(string, lock_mode_names[mode]) == 0)
9470 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9472 enum lu_ladvise_type advice;
9475 advice < ARRAY_SIZE(ladvise_names); advice++) {
9476 if (ladvise_names[advice] == NULL)
9478 if (strcmp(string, ladvise_names[advice]) == 0)
9482 return LU_LADVISE_INVALID;
9485 static int lfs_ladvise(int argc, char **argv)
9487 struct option long_opts[] = {
9488 { .val = 'a', .name = "advice", .has_arg = required_argument },
9489 { .val = 'b', .name = "background", .has_arg = no_argument },
9490 { .val = 'e', .name = "end", .has_arg = required_argument },
9491 { .val = 'l', .name = "length", .has_arg = required_argument },
9492 { .val = 'm', .name = "mode", .has_arg = required_argument },
9493 { .val = 's', .name = "start", .has_arg = required_argument },
9494 { .val = 'u', .name = "unset", .has_arg = no_argument },
9496 char short_opts[] = "a:be:l:m:s:u";
9501 struct llapi_lu_ladvise advice;
9502 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
9503 unsigned long long start = 0;
9504 unsigned long long end = LUSTRE_EOF;
9505 unsigned long long length = 0;
9506 unsigned long long size_units;
9507 unsigned long long flags = 0;
9511 while ((c = getopt_long(argc, argv, short_opts,
9512 long_opts, NULL)) != -1) {
9515 advice_type = lfs_get_ladvice(optarg);
9516 if (advice_type == LU_LADVISE_INVALID) {
9518 "%s: invalid advice type '%s'\n",
9520 fprintf(stderr, "Valid types:");
9522 for (advice_type = 0;
9523 advice_type < ARRAY_SIZE(ladvise_names);
9525 if (ladvise_names[advice_type] == NULL)
9527 fprintf(stderr, " %s",
9528 ladvise_names[advice_type]);
9530 fprintf(stderr, "\n");
9543 rc = llapi_parse_size(optarg, &end,
9546 fprintf(stderr, "%s: bad end offset '%s'\n",
9553 rc = llapi_parse_size(optarg, &start,
9557 "%s: bad start offset '%s'\n",
9564 rc = llapi_parse_size(optarg, &length,
9567 fprintf(stderr, "%s: bad length '%s'\n",
9573 mode = lfs_get_mode(optarg);
9576 "%s: bad mode '%s', valid modes are READ or WRITE\n",
9584 fprintf(stderr, "%s: option '%s' unrecognized\n",
9585 argv[0], argv[optind - 1]);
9590 if (advice_type == LU_LADVISE_INVALID) {
9591 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
9592 fprintf(stderr, "Valid types:");
9593 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
9595 if (ladvise_names[advice_type] == NULL)
9597 fprintf(stderr, " %s", ladvise_names[advice_type]);
9599 fprintf(stderr, "\n");
9603 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
9605 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
9610 if (argc <= optind) {
9611 fprintf(stderr, "%s: please give one or more file names\n",
9616 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
9617 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
9622 if (end == LUSTRE_EOF && length != 0)
9623 end = start + length;
9626 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
9627 argv[0], start, end);
9631 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
9632 fprintf(stderr, "%s: mode is only valid with lockahead\n",
9637 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
9638 fprintf(stderr, "%s: mode is required with lockahead\n",
9643 while (optind < argc) {
9646 path = argv[optind++];
9648 fd = open(path, O_RDONLY);
9650 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9651 argv[0], path, strerror(errno));
9656 advice.lla_start = start;
9657 advice.lla_end = end;
9658 advice.lla_advice = advice_type;
9659 advice.lla_value1 = 0;
9660 advice.lla_value2 = 0;
9661 advice.lla_value3 = 0;
9662 advice.lla_value4 = 0;
9663 if (advice_type == LU_LADVISE_LOCKAHEAD) {
9664 advice.lla_lockahead_mode = mode;
9665 advice.lla_peradvice_flags = flags;
9668 rc2 = llapi_ladvise(fd, flags, 1, &advice);
9672 "%s: cannot give advice '%s' to file '%s': %s\n",
9673 argv[0], ladvise_names[advice_type],
9674 path, strerror(errno));
9680 if (rc == 0 && rc2 < 0)
9686 static const char *const heat_names[] = LU_HEAT_NAMES;
9688 static int lfs_heat_get(int argc, char **argv)
9690 struct lu_heat *heat;
9699 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
9701 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
9706 while (optind < argc) {
9707 path = argv[optind++];
9709 fd = open(path, O_RDONLY);
9711 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9712 argv[0], path, strerror(errno));
9717 heat->lh_count = OBD_HEAT_COUNT;
9718 rc2 = llapi_heat_get(fd, heat);
9722 "%s: cannot get heat of file '%s': %s\n",
9723 argv[0], path, strerror(errno));
9727 printf("flags: %x\n", heat->lh_flags);
9728 for (i = 0; i < heat->lh_count; i++)
9729 printf("%s: %llu\n", heat_names[i],
9730 (unsigned long long)heat->lh_heat[i]);
9732 if (rc == 0 && rc2 < 0)
9740 static int lfs_heat_set(int argc, char **argv)
9742 struct option long_opts[] = {
9743 { .val = 'c', .name = "clear", .has_arg = no_argument },
9744 { .val = 'o', .name = "off", .has_arg = no_argument },
9745 { .val = 'O', .name = "on", .has_arg = no_argument },
9747 enum lu_heat_flag flags = 0;
9757 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
9760 flags |= LU_HEAT_FLAG_CLEAR;
9763 flags |= LU_HEAT_FLAG_CLEAR;
9764 flags |= LU_HEAT_FLAG_OFF;
9767 flags &= ~LU_HEAT_FLAG_OFF;
9772 fprintf(stderr, "%s: option '%s' unrecognized\n",
9773 argv[0], argv[optind - 1]);
9778 if (argc <= optind) {
9779 fprintf(stderr, "%s: please give one or more file names\n",
9784 while (optind < argc) {
9785 path = argv[optind++];
9787 fd = open(path, O_RDONLY);
9789 fprintf(stderr, "%s: cannot open file '%s': %s\n",
9790 argv[0], path, strerror(errno));
9795 rc2 = llapi_heat_set(fd, flags);
9799 "%s: cannot setflags heat of file '%s': %s\n",
9800 argv[0], path, strerror(errno));
9804 if (rc == 0 && rc2 < 0)
9811 * The input string contains a comma delimited list of component ids and
9812 * ranges, for example "1,2-4,7".
9814 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9816 bool end_of_loop = false;
9824 while (!end_of_loop) {
9828 char *endptr = NULL;
9831 ptr = strchrnul(arg, ',');
9832 end_of_loop = *ptr == '\0';
9835 start_index = strtol(arg, &endptr, 0);
9836 if (endptr == arg) /* no data at all */
9838 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9840 if (start_index < 0)
9843 end_index = start_index;
9844 if (*endptr == '-') {
9845 end_index = strtol(endptr + 1, &endptr, 0);
9846 if (*endptr != '\0')
9848 if (end_index < start_index)
9852 for (i = start_index; i <= end_index && size > 0; i++) {
9855 /* remove duplicate */
9856 for (j = 0; j < nr; j++) {
9860 if (j == nr) { /* no duplicate */
9866 if (size == 0 && i < end_index)
9873 if (!end_of_loop && ptr)
9876 return rc < 0 ? rc : nr;
9880 * struct verify_mirror_id - Mirror id to be verified.
9881 * @mirror_id: A specified mirror id.
9882 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9884 struct verify_mirror_id {
9890 * compare_mirror_ids() - Compare mirror ids.
9891 * @layout: Mirror component list.
9892 * @cbdata: Callback data in verify_mirror_id structure.
9894 * This is a callback function called by llapi_layout_comp_iterate()
9895 * to compare the specified mirror id with the one in the current
9896 * component of @layout. If they are the same, then the specified
9897 * mirror id is valid.
9899 * Return: a negative error code on failure or
9900 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9901 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9904 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9906 struct verify_mirror_id *mirror_id_cbdata =
9907 (struct verify_mirror_id *)cbdata;
9911 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9915 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9916 progname, strerror(errno));
9920 if (mirror_id_cbdata->mirror_id == mirror_id) {
9921 mirror_id_cbdata->is_valid_id = true;
9922 return LLAPI_LAYOUT_ITER_STOP;
9925 return LLAPI_LAYOUT_ITER_CONT;
9929 * verify_mirror_ids() - Verify specified mirror ids.
9930 * @fname: Mirrored file name.
9931 * @mirror_ids: Specified mirror ids to be verified.
9932 * @ids_nr: Number of specified mirror ids.
9934 * This function verifies that specified @mirror_ids are valid
9935 * in the mirrored file @fname.
9937 * Return: 0 on success or a negative error code on failure.
9940 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9942 struct llapi_layout *layout = NULL;
9943 struct verify_mirror_id mirror_id_cbdata = { 0 };
9954 if (stat(fname, &stbuf) < 0) {
9955 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9956 progname, fname, strerror(errno));
9961 if (!S_ISREG(stbuf.st_mode)) {
9962 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9968 fd = open(fname, O_DIRECT | O_RDONLY);
9970 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9971 progname, fname, strerror(errno));
9976 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9978 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9979 progname, fname, strerror(errno));
9983 layout = llapi_layout_get_by_fd(fd, 0);
9985 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9986 progname, fname, strerror(errno));
9988 llapi_lease_release(fd);
9992 rc = llapi_layout_flags_get(layout, &flr_state);
9994 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9995 progname, fname, strerror(errno));
10000 flr_state &= LCM_FL_FLR_MASK;
10001 switch (flr_state) {
10004 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10005 progname, fname, llapi_layout_flags_string(flr_state));
10012 for (i = 0; i < ids_nr; i++) {
10013 mirror_id_cbdata.mirror_id = mirror_ids[i];
10014 mirror_id_cbdata.is_valid_id = false;
10016 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10017 &mirror_id_cbdata);
10021 "%s: '%s' failed to verify mirror id: %u.\n",
10022 progname, fname, mirror_ids[i]);
10026 if (!mirror_id_cbdata.is_valid_id) {
10029 "%s: '%s' invalid specified mirror id: %u.\n",
10030 progname, fname, mirror_ids[i]);
10036 llapi_layout_free(layout);
10037 llapi_lease_release(fd);
10045 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10046 __u16 *mirror_ids, int ids_nr)
10048 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10049 struct llapi_layout *layout;
10051 uint32_t flr_state;
10060 if (stat(fname, &stbuf) < 0) {
10061 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10062 progname, fname, strerror(errno));
10066 if (!S_ISREG(stbuf.st_mode)) {
10067 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10073 fd = open(fname, O_DIRECT | O_RDWR);
10075 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10076 progname, fname, strerror(errno));
10081 layout = llapi_layout_get_by_fd(fd, 0);
10083 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10084 progname, fname, strerror(errno));
10089 rc = llapi_layout_flags_get(layout, &flr_state);
10091 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10092 progname, fname, strerror(errno));
10097 flr_state &= LCM_FL_FLR_MASK;
10098 if (flr_state == LCM_FL_NONE) {
10100 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10105 /* get stale component info */
10106 comp_size = llapi_mirror_find_stale(layout, comp_array,
10107 ARRAY_SIZE(comp_array),
10108 mirror_ids, ids_nr);
10109 if (comp_size <= 0) {
10114 ioc->lil_mode = LL_LEASE_WRLCK;
10115 ioc->lil_flags = LL_LEASE_RESYNC;
10116 rc = llapi_lease_set(fd, ioc);
10118 if (rc == -EALREADY)
10122 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10123 progname, fname, strerror(-rc));
10127 /* get the read range [start, end) */
10128 start = comp_array[0].lrc_start;
10129 end = comp_array[0].lrc_end;
10130 for (idx = 1; idx < comp_size; idx++) {
10131 if (comp_array[idx].lrc_start < start)
10132 start = comp_array[idx].lrc_start;
10133 if (end < comp_array[idx].lrc_end)
10134 end = comp_array[idx].lrc_end;
10137 rc = llapi_lease_check(fd);
10138 if (rc != LL_LEASE_WRLCK) {
10139 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10144 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10147 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10148 progname, fname, strerror(-rc));
10150 /* need to do the lease unlock even resync fails */
10151 ioc->lil_mode = LL_LEASE_UNLCK;
10152 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10153 ioc->lil_count = 0;
10154 for (idx = 0; idx < comp_size; idx++) {
10155 if (comp_array[idx].lrc_synced) {
10156 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10161 rc2 = llapi_lease_set(fd, ioc);
10163 * llapi_lease_set returns lease mode when it request to unlock
10167 /* rc2 == 0 means lost lease lock */
10168 if (rc2 == 0 && rc == 0)
10170 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10172 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10176 llapi_layout_free(layout);
10183 static inline int lfs_mirror_resync(int argc, char **argv)
10185 struct ll_ioc_lease *ioc = NULL;
10186 __u16 mirror_ids[128] = { 0 };
10191 struct option long_opts[] = {
10192 { .val = 'o', .name = "only", .has_arg = required_argument },
10193 { .name = NULL } };
10195 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10198 rc = parse_mirror_ids(mirror_ids,
10199 sizeof(mirror_ids) / sizeof(__u16),
10203 "%s: bad mirror ids '%s'.\n",
10210 fprintf(stderr, "%s: options '%s' unrecognized.\n",
10211 argv[0], argv[optind - 1]);
10217 if (argc == optind) {
10218 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10223 if (ids_nr > 0 && argc > optind + 1) {
10225 "%s: option '--only' cannot be used upon multiple files.\n",
10232 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10237 /* set the lease on the file */
10238 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10240 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10241 argv[0], strerror(errno));
10246 for (; optind < argc; optind++) {
10247 rc = lfs_mirror_resync_file(argv[optind], ioc,
10248 mirror_ids, ids_nr);
10249 /* ignore previous file's error, continue with next file */
10252 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10260 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10262 struct llapi_layout *layout;
10265 layout = llapi_layout_get_by_fd(fd, 0);
10267 fprintf(stderr, "could not get layout.\n");
10271 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10273 fprintf(stderr, "failed to iterate layout\n");
10274 llapi_layout_free(layout);
10277 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10278 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10279 llapi_layout_free(layout);
10283 llapi_layout_free(layout);
10289 * Check whether two files are the same file
10290 * \retval 0 same file
10291 * \retval 1 not the same file
10292 * \retval <0 error code
10294 static inline int check_same_file(int fd, const char *f2)
10296 struct stat stbuf1;
10297 struct stat stbuf2;
10299 if (fstat(fd, &stbuf1) < 0)
10302 if (stat(f2, &stbuf2) < 0)
10305 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10306 stbuf1.st_ino == stbuf2.st_ino)
10312 static inline int lfs_mirror_read(int argc, char **argv)
10315 __u16 mirror_id = 0;
10316 const char *outfile = NULL;
10322 const size_t buflen = 4 << 20;
10324 struct option long_opts[] = {
10325 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10326 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10327 { .name = NULL } };
10329 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10334 unsigned long int id;
10337 id = strtoul(optarg, &end, 0);
10338 if (errno != 0 || *end != '\0' || id == 0 ||
10341 "%s %s: invalid mirror ID '%s'\n",
10342 progname, argv[0], optarg);
10346 mirror_id = (__u16)id;
10353 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10354 progname, argv[optind - 1]);
10359 if (argc == optind) {
10360 fprintf(stderr, "%s %s: no mirrored file provided\n",
10361 progname, argv[0]);
10363 } else if (argc > optind + 1) {
10364 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10368 if (mirror_id == 0) {
10369 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10370 progname, argv[0]);
10374 /* open mirror file */
10375 fname = argv[optind];
10376 fd = open(fname, O_DIRECT | O_RDONLY);
10378 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10379 progname, argv[0], fname, strerror(errno));
10383 /* verify mirror id */
10384 rc = verify_mirror_id_by_fd(fd, mirror_id);
10387 "%s %s: cannot find mirror with ID %u in '%s'\n",
10388 progname, argv[0], mirror_id, fname);
10392 /* open output file - O_EXCL ensures output is not the same as input */
10394 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10396 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10397 progname, argv[0], outfile, strerror(errno));
10402 outfd = STDOUT_FILENO;
10405 /* allocate buffer */
10406 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10408 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10409 progname, argv[0], rc);
10415 ssize_t bytes_read;
10416 ssize_t written = 0;
10418 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10419 if (bytes_read < 0) {
10422 "%s %s: fail to read data from mirror %u: %s\n",
10423 progname, argv[0], mirror_id, strerror(-rc));
10428 if (bytes_read == 0)
10431 while (written < bytes_read) {
10434 written2 = write(outfd, buf + written,
10435 bytes_read - written);
10436 if (written2 < 0) {
10438 "%s %s: fail to write %s: %s\n",
10439 progname, argv[0], outfile ? : "STDOUT",
10444 written += written2;
10447 if (written != bytes_read) {
10449 "%s %s: written %ld bytes does not match with %ld read.\n",
10450 progname, argv[0], written, bytes_read);
10472 static inline int lfs_mirror_write(int argc, char **argv)
10475 __u16 mirror_id = 0;
10476 const char *inputfile = NULL;
10482 const size_t buflen = 4 << 20;
10484 size_t page_size = sysconf(_SC_PAGESIZE);
10485 struct ll_ioc_lease_id ioc;
10487 struct option long_opts[] = {
10488 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10489 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
10490 { .name = NULL } };
10492 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10497 unsigned long int id;
10500 id = strtoul(optarg, &end, 0);
10501 if (errno != 0 || *end != '\0' || id == 0 ||
10504 "%s %s: invalid mirror ID '%s'\n",
10505 progname, argv[0], optarg);
10509 mirror_id = (__u16)id;
10513 inputfile = optarg;
10516 fprintf(stderr, "%s: option '%s' unrecognized\n",
10517 progname, argv[optind - 1]);
10522 if (argc == optind) {
10523 fprintf(stderr, "%s %s: no mirrored file provided\n",
10524 progname, argv[0]);
10526 } else if (argc > optind + 1) {
10527 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10531 if (mirror_id == 0) {
10532 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10533 progname, argv[0]);
10537 /* open mirror file */
10538 fname = argv[optind];
10539 fd = open(fname, O_DIRECT | O_WRONLY);
10541 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10542 progname, argv[0], fname, strerror(errno));
10546 /* verify mirror id */
10547 rc = verify_mirror_id_by_fd(fd, mirror_id);
10550 "%s %s: cannot find mirror with ID %u in '%s'\n",
10551 progname, argv[0], mirror_id, fname);
10555 /* open input file */
10557 rc = check_same_file(fd, inputfile);
10560 "%s %s: input file cannot be the mirrored file\n",
10561 progname, argv[0]);
10567 inputfd = open(inputfile, O_RDONLY, 0644);
10569 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10570 progname, argv[0], inputfile, strerror(errno));
10575 inputfd = STDIN_FILENO;
10578 /* allocate buffer */
10579 rc = posix_memalign(&buf, page_size, buflen);
10581 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10582 progname, argv[0], rc);
10583 goto close_inputfd;
10586 /* prepare target mirror components instantiation */
10587 ioc.lil_mode = LL_LEASE_WRLCK;
10588 ioc.lil_flags = LL_LEASE_RESYNC;
10589 ioc.lil_mirror_id = mirror_id;
10590 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10593 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10594 progname, argv[0], fname, strerror(errno));
10600 ssize_t bytes_read;
10604 rc = llapi_lease_check(fd);
10605 if (rc != LL_LEASE_WRLCK) {
10606 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
10607 progname, argv[0], fname);
10611 bytes_read = read(inputfd, buf, buflen);
10612 if (bytes_read < 0) {
10615 "%s %s: fail to read data from '%s': %s\n",
10616 progname, argv[0], inputfile ? : "STDIN",
10623 if (bytes_read == 0)
10626 /* round up to page align to make direct IO happy. */
10627 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
10629 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
10634 "%s %s: fail to write to mirror %u: %s\n",
10635 progname, argv[0], mirror_id,
10643 if (pos & (page_size - 1)) {
10644 rc = llapi_mirror_truncate(fd, mirror_id, pos);
10649 ioc.lil_mode = LL_LEASE_UNLCK;
10650 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
10652 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
10657 "%s %s: release lease lock of '%s' failed: %s\n",
10658 progname, argv[0], fname, strerror(errno));
10675 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
10677 struct llapi_layout *layout;
10678 struct collect_ids_data cid = { .cid_ids = ids,
10680 .cid_exclude = exclude_id, };
10683 layout = llapi_layout_get_by_fd(fd, 0);
10685 fprintf(stderr, "could not get layout\n");
10689 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
10691 fprintf(stderr, "failed to iterate layout\n");
10692 llapi_layout_free(layout);
10696 llapi_layout_free(layout);
10698 return cid.cid_count;
10701 static inline int lfs_mirror_copy(int argc, char **argv)
10704 __u16 read_mirror_id = 0;
10705 __u16 ids[128] = { 0 };
10707 struct llapi_layout *layout = NULL;
10708 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10715 struct ll_ioc_lease *ioc = NULL;
10716 struct ll_ioc_lease_id *resync_ioc;
10718 struct option long_opts[] = {
10719 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
10720 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
10721 { .name = NULL } };
10723 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
10728 unsigned long int id;
10731 id = strtoul(optarg, &end, 0);
10732 if (errno != 0 || *end != '\0' || id == 0 ||
10735 "%s %s: invalid read mirror ID '%s'\n",
10736 progname, argv[0], optarg);
10740 read_mirror_id = (__u16)id;
10744 if (!strcmp(optarg, "-1")) {
10745 /* specify all other mirrors */
10746 ids[0] = (__u16)-1;
10749 count = parse_mirror_ids((__u16 *)ids,
10757 fprintf(stderr, "%s: option '%s' unrecognized\n",
10758 progname, argv[optind - 1]);
10763 if (argc == optind) {
10764 fprintf(stderr, "%s %s: no mirrored file provided\n",
10765 progname, argv[0]);
10767 } else if (argc > optind + 1) {
10768 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10772 if (read_mirror_id == 0) {
10774 "%s %s: no valid read mirror ID %d is provided\n",
10775 progname, argv[0], read_mirror_id);
10781 "%s %s: no write mirror ID is provided\n",
10782 progname, argv[0]);
10786 for (i = 0; i < count; i++) {
10787 if (read_mirror_id == ids[i]) {
10789 "%s %s: read and write mirror ID cannot be the same\n",
10790 progname, argv[0]);
10795 /* open mirror file */
10796 fname = argv[optind];
10798 fd = open(fname, O_DIRECT | O_RDWR);
10800 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10801 progname, argv[0], fname, strerror(errno));
10805 /* write to all other mirrors */
10806 if (ids[0] == (__u16)-1) {
10807 count = get_other_mirror_ids(fd, ids, read_mirror_id);
10811 "%s %s: failed to get other mirror ids in '%s': %d\n",
10812 progname, argv[0], fname, rc);
10817 /* verify mirror id */
10818 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10821 "%s %s: cannot find mirror with ID %u in '%s'\n",
10822 progname, argv[0], read_mirror_id, fname);
10826 for (i = 0; i < count; i++) {
10827 rc = verify_mirror_id_by_fd(fd, ids[i]);
10830 "%s %s: cannot find mirror with ID %u in '%s'\n",
10831 progname, argv[0], ids[i], fname);
10836 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10839 "%s %s: cannot alloc comp id array for ioc: %s\n",
10840 progname, argv[0], strerror(errno));
10845 /* get stale component info */
10846 layout = llapi_layout_get_by_fd(fd, 0);
10848 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10849 progname, argv[0], fname, strerror(errno));
10853 comp_size = llapi_mirror_find_stale(layout, comp_array,
10854 ARRAY_SIZE(comp_array),
10856 llapi_layout_free(layout);
10857 if (comp_size < 0) {
10862 /* prepare target mirror components instantiation */
10863 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10864 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10865 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10867 resync_ioc->lil_mirror_id = ids[0];
10869 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10870 rc = llapi_lease_set(fd, ioc);
10873 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10874 progname, argv[0], fname, strerror(errno));
10878 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10881 fprintf(stderr, "%s %s: copy error: %d\n",
10882 progname, argv[0], rc);
10886 fprintf(stdout, "mirror copied successfully: ");
10887 for (i = 0; i < copied; i++)
10888 fprintf(stdout, "%d ", ids[i]);
10889 fprintf(stdout, "\n");
10891 ioc->lil_mode = LL_LEASE_UNLCK;
10892 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10893 ioc->lil_count = 0;
10894 for (i = 0; i < comp_size; i++) {
10897 for (j = 0; j < copied; j++) {
10898 if (comp_array[i].lrc_mirror_id != ids[j])
10901 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10905 rc = llapi_lease_set(fd, ioc);
10910 "%s %s: release lease lock of '%s' failed: %s\n",
10911 progname, argv[0], fname, strerror(errno));
10926 * struct verify_chunk - Mirror chunk to be verified.
10927 * @chunk: [start, end) of the chunk.
10928 * @mirror_count: Number of mirror ids in @mirror_id array.
10929 * @mirror_id: Array of valid mirror ids that cover the chunk.
10931 struct verify_chunk {
10932 struct lu_extent chunk;
10933 unsigned int mirror_count;
10934 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10938 * print_chunks() - Print chunk information.
10939 * @fname: Mirrored file name.
10940 * @chunks: Array of chunks.
10941 * @chunk_count: Number of chunks in @chunks array.
10943 * This function prints [start, end) of each chunk in @chunks
10944 * for mirrored file @fname, and also prints the valid mirror ids
10945 * that cover the chunk.
10950 void print_chunks(const char *fname, struct verify_chunk *chunks,
10956 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10957 for (i = 0; i < chunk_count; i++) {
10958 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10960 if (chunks[i].mirror_count == 0)
10961 fprintf(stdout, "\t[");
10963 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10964 for (j = 1; j < chunks[i].mirror_count; j++)
10965 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10967 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10969 fprintf(stdout, "\n");
10973 * print_checksums() - Print CRC-32 checksum values.
10974 * @chunk: A chunk and its corresponding valid mirror ids.
10975 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10977 * This function prints CRC-32 checksum values on @chunk for
10978 * each valid mirror that covers it.
10983 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10988 "CRC-32 checksum value for chunk "DEXT":\n",
10989 PEXT(&chunk->chunk));
10990 for (i = 0; i < chunk->mirror_count; i++)
10991 fprintf(stdout, "Mirror %u:\t%#lx\n",
10992 chunk->mirror_id[i], crc[i]);
10993 fprintf(stdout, "\n");
10997 * filter_mirror_id() - Filter specified mirror ids.
10998 * @chunks: Array of chunks.
10999 * @chunk_count: Number of chunks in @chunks array.
11000 * @mirror_ids: Specified mirror ids to be verified.
11001 * @ids_nr: Number of specified mirror ids.
11003 * This function scans valid mirror ids that cover each chunk in @chunks
11004 * and filters specified mirror ids.
11009 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11010 __u16 *mirror_ids, int ids_nr)
11015 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11016 unsigned int valid_count = 0;
11018 for (i = 0; i < chunk_count; i++) {
11019 if (chunks[i].mirror_count == 0)
11023 for (j = 0; j < ids_nr; j++) {
11024 for (k = 0; k < chunks[i].mirror_count; k++) {
11025 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11026 valid_id[valid_count] = mirror_ids[j];
11033 memcpy(chunks[i].mirror_id, valid_id,
11034 sizeof(__u16) * valid_count);
11035 chunks[i].mirror_count = valid_count;
11040 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11041 * @layout: Mirror component list.
11042 * @chunks: Array of chunks.
11043 * @chunks_size: Array size of @chunks.
11045 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11046 * to find out chunk segments and store them in @chunks array.
11048 * The @mirror_id array in each element of @chunks will store the valid
11049 * mirror ids that cover the chunk. If a mirror component covering the
11050 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11051 * will not be stored into the @mirror_id array, and the chunk for that
11052 * mirror will not be verified.
11054 * The @mirror_count in each element of @chunks will store the number of
11055 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11056 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11057 * indicates the chunk is valid in only one mirror. In both cases, the
11058 * chunk will not be verified.
11060 * Here is an example:
11062 * 0 1M 2M 3M 4M EOF
11063 * +------+-------------+--------------------+
11064 * | | | S | mirror1
11065 * +------+------+------+------+-------------+
11066 * | | S | S | | mirror2
11067 * +-------------+------+------+-------------+
11069 * prepared @chunks array will contain 5 elements:
11070 * (([0, 1M), [1, 2], 2),
11071 * ([1M, 2M), [1, 2], 2),
11072 * ([2M, 3M), [1], 1),
11073 * ([3M, 4M], [], 0),
11074 * ([4M, EOF), [2], 1))
11076 * Return: the actual array size of @chunks on success
11077 * or a negative error code on failure.
11080 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11081 struct verify_chunk *chunks,
11082 size_t chunks_size)
11086 uint32_t mirror_id;
11092 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11095 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11098 "%s: move to the first layout component: %s.\n",
11099 progname, strerror(errno));
11105 chunks[idx].chunk.e_end = LUSTRE_EOF;
11107 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11110 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11111 progname, strerror(errno));
11115 if (start > chunks[idx].chunk.e_start ||
11116 end <= chunks[idx].chunk.e_start)
11119 if (end < chunks[idx].chunk.e_end)
11120 chunks[idx].chunk.e_end = end;
11122 rc = llapi_layout_comp_flags_get(layout, &flags);
11125 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11126 progname, strerror(errno));
11130 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11133 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11136 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11137 progname, strerror(errno));
11141 chunks[idx].mirror_id[i] = mirror_id;
11143 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11145 "%s: mirror_id array is too small.\n",
11152 rc = llapi_layout_comp_use(layout,
11153 LLAPI_LAYOUT_COMP_USE_NEXT);
11156 "%s: move to the next layout component: %s.\n",
11157 progname, strerror(errno));
11160 } /* loop through all components */
11162 chunks[idx].mirror_count = i;
11164 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11168 if (idx >= chunks_size) {
11169 fprintf(stderr, "%s: chunks array is too small.\n",
11175 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11179 return rc < 0 ? rc : idx + 1;
11183 * lfs_mirror_verify_chunk() - Verify a chunk.
11184 * @fd: File descriptor of the mirrored file.
11185 * @file_size: Size of the mirrored file.
11186 * @chunk: A chunk and its corresponding valid mirror ids.
11187 * @verbose: Verbose mode.
11189 * This function verifies a @chunk contains exactly the same data
11190 * ammong the mirrors that cover it.
11192 * If @verbose is specified, then the function will print where the
11193 * differences are if the data do not match. Otherwise, it will
11194 * just return an error in that case.
11196 * Return: 0 on success or a negative error code on failure.
11199 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11200 struct verify_chunk *chunk, int verbose)
11202 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11204 size_t page_size = sysconf(_SC_PAGESIZE);
11205 ssize_t bytes_read;
11206 ssize_t bytes_done;
11210 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11214 if (file_size == 0)
11217 rc = posix_memalign(&buf, page_size, buflen);
11218 if (rc) /* error code is returned directly */
11222 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11223 PEXT(&chunk->chunk));
11224 for (i = 0; i < chunk->mirror_count; i++)
11225 fprintf(stdout, " %u", chunk->mirror_id[i]);
11226 fprintf(stdout, "\n");
11230 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11231 pos = chunk->chunk.e_start;
11232 while (bytes_done < count) {
11233 /* compute initial CRC-32 checksum */
11234 crc = crc32(0L, Z_NULL, 0);
11235 memset(crc_array, 0, sizeof(crc_array));
11238 for (i = 0; i < chunk->mirror_count; i++) {
11239 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11241 if (bytes_read < 0) {
11244 "%s: failed to read data from mirror %u: %s.\n",
11245 progname, chunk->mirror_id[i],
11250 /* compute new CRC-32 checksum */
11251 crc_array[i] = crc32(crc, buf, bytes_read);
11255 print_checksums(chunk, crc_array);
11257 /* compare CRC-32 checksum values */
11258 for (i = 1; i < chunk->mirror_count; i++) {
11259 if (crc_array[i] != crc_array[0]) {
11263 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11264 progname, PEXT(&chunk->chunk),
11265 chunk->mirror_id[0],
11266 chunk->mirror_id[i]);
11271 bytes_done += bytes_read;
11274 if (verbose > 1 && rc == 0) {
11275 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11276 PEXT(&chunk->chunk));
11277 for (i = 0; i < chunk->mirror_count; i++)
11278 fprintf(stdout, " %u", chunk->mirror_id[i]);
11279 fprintf(stdout, " PASS\n\n");
11288 * lfs_mirror_verify_file() - Verify a mirrored file.
11289 * @fname: Mirrored file name.
11290 * @mirror_ids: Specified mirror ids to be verified.
11291 * @ids_nr: Number of specified mirror ids.
11292 * @verbose: Verbose mode.
11294 * This function verifies that each SYNC mirror of a mirrored file
11295 * specified by @fname contains exactly the same data.
11297 * If @mirror_ids is specified, then the function will verify the
11298 * mirrors specified by @mirror_ids contain exactly the same data.
11300 * If @verbose is specified, then the function will print where the
11301 * differences are if the data do not match. Otherwise, it will
11302 * just return an error in that case.
11304 * Return: 0 on success or a negative error code on failure.
11307 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11310 struct verify_chunk chunks_array[1024] = { };
11311 struct llapi_layout *layout = NULL;
11313 uint32_t flr_state;
11315 int chunk_count = 0;
11321 if (stat(fname, &stbuf) < 0) {
11322 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11323 progname, fname, strerror(errno));
11328 if (!S_ISREG(stbuf.st_mode)) {
11329 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11335 if (stbuf.st_size == 0) {
11337 fprintf(stdout, "%s: '%s' file size is 0.\n",
11343 fd = open(fname, O_DIRECT | O_RDONLY);
11345 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11346 progname, fname, strerror(errno));
11351 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11353 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11354 progname, fname, strerror(errno));
11358 layout = llapi_layout_get_by_fd(fd, 0);
11360 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11361 progname, fname, strerror(errno));
11363 llapi_lease_release(fd);
11367 rc = llapi_layout_flags_get(layout, &flr_state);
11369 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11370 progname, fname, strerror(errno));
11375 flr_state &= LCM_FL_FLR_MASK;
11376 switch (flr_state) {
11379 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11380 progname, fname, llapi_layout_flags_string(flr_state));
11386 /* find out mirror chunks to be verified */
11387 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11388 ARRAY_SIZE(chunks_array));
11389 if (chunk_count < 0) {
11395 /* filter specified mirror ids */
11396 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11399 print_chunks(fname, chunks_array, chunk_count);
11401 for (idx = 0; idx < chunk_count; idx++) {
11402 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11405 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11407 PEXT(&chunks_array[idx].chunk),
11408 (unsigned long long)stbuf.st_size);
11412 if (chunks_array[idx].mirror_count == 0) {
11414 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11416 PEXT(&chunks_array[idx].chunk));
11418 fprintf(stderr, "skipped\n");
11422 fprintf(stderr, "failed\n");
11426 if (chunks_array[idx].mirror_count == 1) {
11429 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11431 PEXT(&chunks_array[idx].chunk),
11432 chunks_array[idx].mirror_id[0]);
11436 rc = llapi_lease_check(fd);
11437 if (rc != LL_LEASE_RDLCK) {
11438 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11443 /* verify one chunk */
11444 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11445 &chunks_array[idx], verbose);
11459 llapi_layout_free(layout);
11460 llapi_lease_release(fd);
11468 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11469 * @argc: The count of lfs mirror verify command line arguments.
11470 * @argv: Array of strings for lfs mirror verify command line arguments.
11472 * This function parses lfs mirror verify command and verifies the
11473 * specified mirrored file(s).
11475 * Return: 0 on success or a negative error code on failure.
11477 static inline int lfs_mirror_verify(int argc, char **argv)
11479 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11485 char cmd[PATH_MAX];
11487 struct option long_opts[] = {
11488 { .val = 'o', .name = "only", .has_arg = required_argument },
11489 { .val = 'v', .name = "verbose", .has_arg = no_argument },
11490 { .name = NULL } };
11492 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11494 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11497 rc = parse_mirror_ids(mirror_ids,
11498 ARRAY_SIZE(mirror_ids),
11502 "%s: bad mirror ids '%s'.\n",
11509 "%s: at least 2 mirror ids needed with '--only' option.\n",
11519 fprintf(stderr, "%s: option '%s' unrecognized.\n",
11520 progname, argv[optind - 1]);
11526 if (argc == optind) {
11527 fprintf(stderr, "%s: no file name given.\n", progname);
11532 if (ids_nr > 0 && argc > optind + 1) {
11534 "%s: '--only' cannot be used upon multiple files.\n",
11541 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11547 for (; optind < argc; optind++) {
11548 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11558 * lfs_mirror() - Parse and execute lfs mirror commands.
11559 * @argc: The count of lfs mirror command line arguments.
11560 * @argv: Array of strings for lfs mirror command line arguments.
11562 * This function parses lfs mirror commands and performs the
11563 * corresponding functions specified in mirror_cmdlist[].
11565 * Return: 0 on success or an error code on failure.
11567 static int lfs_mirror(int argc, char **argv)
11569 char cmd[PATH_MAX];
11572 setlinebuf(stdout);
11574 Parser_init("lfs-mirror > ", mirror_cmdlist);
11576 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11578 program_invocation_short_name = cmd;
11580 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11582 rc = Parser_commands();
11584 return rc < 0 ? -rc : rc;
11587 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11589 #if __BYTE_ORDER == __BIG_ENDIAN
11590 __swab16s(&attrs->lsa_valid);
11591 __swab64s(&attrs->lsa_size);
11592 __swab64s(&attrs->lsa_blocks);
11596 enum lfs_som_type {
11597 LFS_SOM_SIZE = 0x1,
11598 LFS_SOM_BLOCKS = 0x2,
11599 LFS_SOM_FLAGS = 0x4,
11600 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
11604 static int lfs_getsom(int argc, char **argv)
11607 struct lustre_som_attrs *attrs;
11608 char buf[sizeof(*attrs) + 64];
11609 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
11612 while ((c = getopt(argc, argv, "sbf")) != -1) {
11615 type = LFS_SOM_SIZE;
11618 type = LFS_SOM_BLOCKS;
11621 type = LFS_SOM_FLAGS;
11624 fprintf(stderr, "%s: invalid option '%c'\n",
11634 fprintf(stderr, "%s: %s\n",
11635 progname, argc == 0 ? "miss file target" :
11636 "input more than 2 files");
11641 attrs = (void *)buf;
11642 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
11645 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
11646 argv[0], strerror(errno), errno);
11650 lustre_som_swab(attrs);
11653 case LFS_SOM_ATTR_ALL:
11654 printf("file: %s size: %llu blocks: %llu flags: %x\n",
11655 path, (unsigned long long)attrs->lsa_size,
11656 (unsigned long long)attrs->lsa_blocks,
11660 printf("%llu\n", (unsigned long long)attrs->lsa_size);
11662 case LFS_SOM_BLOCKS:
11663 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
11665 case LFS_SOM_FLAGS:
11666 printf("%x\n", attrs->lsa_valid);
11669 fprintf(stderr, "%s: unknown option\n", progname);
11677 * lfs_mirror_list_commands() - List lfs mirror commands.
11678 * @argc: The count of command line arguments.
11679 * @argv: Array of strings for command line arguments.
11681 * This function lists lfs mirror commands defined in mirror_cmdlist[].
11683 * Return: 0 on success.
11685 static int lfs_mirror_list_commands(int argc, char **argv)
11687 char buffer[81] = "";
11689 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
11695 static int lfs_pcc_attach(int argc, char **argv)
11697 struct option long_opts[] = {
11698 { .val = 'i', .name = "id", .has_arg = required_argument },
11699 { .name = NULL } };
11702 __u32 archive_id = 0;
11705 char fullpath[PATH_MAX];
11706 enum lu_pcc_type type = LU_PCC_READWRITE;
11709 while ((c = getopt_long(argc, argv, "i:",
11710 long_opts, NULL)) != -1) {
11714 archive_id = strtoul(optarg, &end, 0);
11715 if (errno != 0 || *end != '\0' ||
11716 archive_id == 0 || archive_id > UINT32_MAX) {
11718 "error: %s: bad archive ID '%s'\n",
11726 fprintf(stderr, "%s: option '%s' unrecognized\n",
11727 argv[0], argv[optind - 1]);
11732 if (archive_id == 0) {
11733 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
11737 if (argc <= optind) {
11738 fprintf(stderr, "%s: must specify one or more file names\n",
11743 while (optind < argc) {
11746 path = argv[optind++];
11747 if (!realpath(path, fullpath)) {
11748 fprintf(stderr, "%s: could not find path '%s': %s\n",
11749 argv[0], path, strerror(errno));
11755 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
11758 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
11759 argv[0], path, archive_id, strerror(-rc2));
11767 static int lfs_pcc_attach_fid(int argc, char **argv)
11769 struct option long_opts[] = {
11770 { .val = 'i', .name = "id", .has_arg = required_argument },
11771 { .val = 'm', .name = "mnt", .has_arg = required_argument },
11772 { .name = NULL } };
11773 char short_opts[] = "i:m:";
11776 __u32 archive_id = 0;
11778 const char *mntpath = NULL;
11779 const char *fidstr;
11780 enum lu_pcc_type type = LU_PCC_READWRITE;
11783 while ((c = getopt_long(argc, argv, short_opts,
11784 long_opts, NULL)) != -1) {
11788 archive_id = strtoul(optarg, &end, 0);
11789 if (errno != 0 || *end != '\0' ||
11790 archive_id > UINT32_MAX) {
11792 "error: %s: bad archive ID '%s'\n",
11803 fprintf(stderr, "%s: option '%s' unrecognized\n",
11804 argv[0], argv[optind - 1]);
11809 if (archive_id == 0) {
11810 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
11815 fprintf(stderr, "%s: must specify Lustre mount point\n",
11820 if (argc <= optind) {
11821 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11825 while (optind < argc) {
11828 fidstr = argv[optind++];
11830 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11834 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
11835 argv[0], fidstr, mntpath, archive_id,
11838 if (rc == 0 && rc2 < 0)
11844 static int lfs_pcc_detach(int argc, char **argv)
11846 struct option long_opts[] = {
11847 { .val = 'k', .name = "keep", .has_arg = no_argument },
11848 { .name = NULL } };
11849 char short_opts[] = "k";
11853 char fullpath[PATH_MAX];
11854 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11857 while ((c = getopt_long(argc, argv, short_opts,
11858 long_opts, NULL)) != -1) {
11861 detach_opt = PCC_DETACH_OPT_NONE;
11866 fprintf(stderr, "%s: option '%s' unrecognized\n",
11867 argv[0], argv[optind - 1]);
11872 while (optind < argc) {
11875 path = argv[optind++];
11876 if (!realpath(path, fullpath)) {
11877 fprintf(stderr, "%s: could not find path '%s': %s\n",
11878 argv[0], path, strerror(errno));
11884 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11888 "%s: cannot detach '%s' from PCC: %s\n",
11889 argv[0], path, strerror(errno));
11897 static int lfs_pcc_detach_fid(int argc, char **argv)
11899 struct option long_opts[] = {
11900 { .val = 'k', .name = "keep", .has_arg = no_argument },
11901 { .name = NULL } };
11902 char short_opts[] = "k";
11906 const char *mntpath;
11907 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11910 while ((c = getopt_long(argc, argv, short_opts,
11911 long_opts, NULL)) != -1) {
11914 detach_opt = PCC_DETACH_OPT_NONE;
11919 fprintf(stderr, "%s: option '%s' unrecognized\n",
11920 argv[0], argv[optind - 1]);
11925 mntpath = argv[optind++];
11927 while (optind < argc) {
11930 fid = argv[optind++];
11932 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11935 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
11936 argv[0], fid, mntpath, strerror(-rc2));
11944 static int lfs_pcc_state(int argc, char **argv)
11948 char fullpath[PATH_MAX];
11949 struct lu_pcc_state state;
11954 fprintf(stderr, "%s: must specify one or more file names\n",
11959 while (optind < argc) {
11962 path = argv[optind++];
11963 if (!realpath(path, fullpath)) {
11964 fprintf(stderr, "%s: could not find path '%s': %s\n",
11965 argv[0], path, strerror(errno));
11971 rc2 = llapi_pcc_state_get(fullpath, &state);
11976 "%s: cannot get PCC state of '%s': %s\n",
11977 argv[0], path, strerror(-rc2));
11981 printf("file: %s", path);
11982 printf(", type: %s", pcc_type2string(state.pccs_type));
11983 if (state.pccs_type == LU_PCC_NONE &&
11984 state.pccs_open_count == 0) {
11989 printf(", PCC file: %s", state.pccs_path);
11990 printf(", user number: %u", state.pccs_open_count);
11991 printf(", flags: %x", state.pccs_flags);
11998 * lfs_pcc_list_commands() - List lfs pcc commands.
11999 * @argc: The count of command line arguments.
12000 * @argv: Array of strings for command line arguments.
12002 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12004 * Return: 0 on success.
12006 static int lfs_pcc_list_commands(int argc, char **argv)
12008 char buffer[81] = "";
12010 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12017 * lfs_pcc() - Parse and execute lfs pcc commands.
12018 * @argc: The count of lfs pcc command line arguments.
12019 * @argv: Array of strings for lfs pcc command line arguments.
12021 * This function parses lfs pcc commands and performs the
12022 * corresponding functions specified in pcc_cmdlist[].
12024 * Return: 0 on success or an error code on failure.
12026 static int lfs_pcc(int argc, char **argv)
12028 char cmd[PATH_MAX];
12031 setlinebuf(stdout);
12033 Parser_init("lfs-pcc > ", pcc_cmdlist);
12035 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12037 program_invocation_short_name = cmd;
12039 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12041 rc = Parser_commands();
12043 return rc < 0 ? -rc : rc;
12046 static int lfs_list_commands(int argc, char **argv)
12048 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12050 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12055 int main(int argc, char **argv)
12059 /* Ensure that liblustreapi constructor has run */
12060 if (!llapi_liblustreapi_initialized())
12061 fprintf(stderr, "liblustreapi was not properly initialized\n");
12063 setlinebuf(stdout);
12066 Parser_init("lfs > ", cmdlist);
12068 progname = program_invocation_short_name; /* Used in error messages */
12070 llapi_set_command_name(argv[1]);
12071 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12072 llapi_clear_command_name();
12074 rc = Parser_commands();
12077 return rc < 0 ? -rc : rc;
12080 #ifdef _LUSTRE_IDL_H_
12081 /* Everything we need here should be included by lustreapi.h. */
12082 # error "lfs should not depend on lustre_idl.h"
12083 #endif /* _LUSTRE_IDL_H_ */