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>
82 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
83 #endif /* !ARRAY_SIZE */
86 static int lfs_find(int argc, char **argv);
87 static int lfs_getstripe(int argc, char **argv);
88 static int lfs_getdirstripe(int argc, char **argv);
89 static int lfs_setdirstripe(int argc, char **argv);
90 static int lfs_rmentry(int argc, char **argv);
91 static int lfs_osts(int argc, char **argv);
92 static int lfs_mdts(int argc, char **argv);
93 static int lfs_df(int argc, char **argv);
94 static int lfs_getname(int argc, char **argv);
95 static int lfs_check(int argc, char **argv);
96 #ifdef HAVE_SYS_QUOTA_H
97 static int lfs_setquota(int argc, char **argv);
98 static int lfs_quota(int argc, char **argv);
99 static int lfs_project(int argc, char **argv);
101 static int lfs_flushctx(int argc, char **argv);
102 static int lfs_poollist(int argc, char **argv);
103 static int lfs_changelog(int argc, char **argv);
104 static int lfs_changelog_clear(int argc, char **argv);
105 static int lfs_fid2path(int argc, char **argv);
106 static int lfs_path2fid(int argc, char **argv);
107 static int lfs_rmfid(int argc, char **argv);
108 static int lfs_data_version(int argc, char **argv);
109 static int lfs_hsm_state(int argc, char **argv);
110 static int lfs_hsm_set(int argc, char **argv);
111 static int lfs_hsm_clear(int argc, char **argv);
112 static int lfs_hsm_action(int argc, char **argv);
113 static int lfs_hsm_archive(int argc, char **argv);
114 static int lfs_hsm_restore(int argc, char **argv);
115 static int lfs_hsm_release(int argc, char **argv);
116 static int lfs_hsm_remove(int argc, char **argv);
117 static int lfs_hsm_cancel(int argc, char **argv);
118 static int lfs_swap_layouts(int argc, char **argv);
119 static int lfs_mv(int argc, char **argv);
120 static int lfs_ladvise(int argc, char **argv);
121 static int lfs_getsom(int argc, char **argv);
122 static int lfs_heat_get(int argc, char **argv);
123 static int lfs_heat_set(int argc, char **argv);
124 static int lfs_mirror(int argc, char **argv);
125 static int lfs_mirror_list_commands(int argc, char **argv);
126 static int lfs_list_commands(int argc, char **argv);
127 static inline int lfs_mirror_resync(int argc, char **argv);
128 static inline int lfs_mirror_verify(int argc, char **argv);
129 static inline int lfs_mirror_read(int argc, char **argv);
130 static inline int lfs_mirror_write(int argc, char **argv);
131 static inline int lfs_mirror_copy(int argc, char **argv);
132 static int lfs_pcc_attach(int argc, char **argv);
133 static int lfs_pcc_attach_fid(int argc, char **argv);
134 static int lfs_pcc_detach(int argc, char **argv);
135 static int lfs_pcc_detach_fid(int argc, char **argv);
136 static int lfs_pcc_state(int argc, char **argv);
137 static int lfs_pcc(int argc, char **argv);
138 static int lfs_pcc_list_commands(int argc, char **argv);
139 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
140 __u64 migration_flags,
141 struct llapi_stripe_param *param,
142 struct llapi_layout *layout);
144 enum setstripe_origin {
153 static int lfs_setstripe_internal(int argc, char **argv,
154 enum setstripe_origin opc);
156 static inline int lfs_setstripe(int argc, char **argv)
158 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
160 static inline int lfs_setstripe_migrate(int argc, char **argv)
162 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
164 static inline int lfs_mirror_create(int argc, char **argv)
166 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
168 static inline int lfs_mirror_extend(int argc, char **argv)
170 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
172 static inline int lfs_mirror_split(int argc, char **argv)
174 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
176 static inline int lfs_mirror_delete(int argc, char **argv)
178 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
181 /* Setstripe and migrate share mostly the same parameters */
182 #define SSM_CMD_COMMON(cmd) \
183 "usage: "cmd" [--component-end|-E <comp_end>]\n" \
184 " [--stripe-count|-c <stripe_count>]\n" \
185 " [--overstripe-count|-C <stripe_count>]\n" \
186 " [--stripe-index|-i <start_ost_idx>]\n" \
187 " [--stripe-size|-S <stripe_size>]\n" \
188 " [--layout|-L <pattern>]\n" \
189 " [--mirror_count|-N[mirror_count]]\n" \
190 " [--ost|-o <ost_indices>]\n" \
191 " [--pool|-p <pool_name>]\n" \
192 " [--yaml|-y <yaml_template_file>]\n" \
193 " [--copy=<lustre_src>]\n"
195 #define SSM_HELP_COMMON \
196 "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
197 "\t Using -C instead of -c allows overstriping, which\n" \
198 "\t will place more than one stripe per OST if\n" \
199 "\t stripe_count is greater than the number of OSTs\n" \
200 "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
201 "\tstripe_size: Number of bytes on each OST (0=fs default)\n" \
202 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
203 "\t respectively)\n" \
204 "\tpool_name: Name of OST pool to use (default none)\n" \
205 "\tlayout: stripe pattern type: raid0, mdt (default raid0)\n"\
206 "\tost_indices: List of OST indices, can be repeated multiple times\n"\
207 "\t Indices be specified in a format of:\n" \
208 "\t -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n" \
210 "\t -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n" \
211 "\t If --pool is set with --ost then the OSTs\n" \
212 "\t must be the members of the pool.\n" \
213 "\tcomp_end: Extent end of component, start after previous end.\n"\
214 "\t Can be specified with K, M or G (for KB, MB, GB\n" \
215 "\t respectively, -1 for EOF). Must be a multiple of\n"\
216 "\t stripe_size.\n" \
217 "\tyaml_template_file:\n" \
218 "\t YAML layout template file, can't be used with -c,\n" \
219 "\t -i, -S, -p, -o, or -E arguments.\n" \
220 "\tlustre_src: Lustre file/dir whose layout info is used to set\n" \
221 "\t another lustre file or directory, can't used with\n" \
222 "\t -c, -i, -S, -p, -o, or -E arguments.\n"
224 #define MIRROR_CREATE_HELP \
225 "\tmirror_count: Number of mirrors to be created with the upcoming\n" \
226 "\t setstripe layout options\n" \
227 "\t It defaults to 1 if not specified; if specified,\n" \
228 "\t it must follow the option without a space.\n" \
229 "\t The option can also be repeated multiple times to\n" \
230 "\t separate mirrors that have different layouts.\n" \
231 "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n" \
232 "\t It can be a plain layout or a composite layout.\n" \
233 "\t If not specified, the stripe options inherited\n" \
234 "\t from the previous component will be used.\n" \
235 "\tflags: set flags to the component of the current mirror.\n" \
236 "\t Only \"prefer\" flag is supported so far.\n"
238 #define MIRROR_EXTEND_HELP \
240 "\tvictim_file: The layout of victim_file will be split and used\n" \
241 "\t as a mirror added to the mirrored file.\n" \
242 "\tno-verify: This option indicates not to verify the mirror(s)\n" \
243 "\t from victim file(s) in case the victim file(s)\n" \
244 "\t contains the same data as the original mirrored\n" \
247 #define MIRROR_EXTEND_USAGE \
248 " {--mirror-count|-N[mirror_count]}\n" \
249 " [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n" \
252 #define SETSTRIPE_USAGE \
253 SSM_CMD_COMMON("setstripe") \
254 MIRROR_EXTEND_USAGE \
255 " <directory|filename>\n" \
259 #define MIGRATE_USAGE \
260 SSM_CMD_COMMON("migrate ") \
261 " [--block|-b] [--non-block|-n]\n" \
262 " [--non-direct|-D] [--verbose|-v]\n" \
266 "\tblock: Block file access during data migration (default)\n" \
267 "\tnon-block: Abort migrations if concurrent access is detected\n" \
268 "\tnon-direct: Do not use direct I/O to copy file contents\n" \
269 "\tverbose: Print each filename as it is migrated\n" \
271 #define SETDIRSTRIPE_USAGE \
272 " [--mdt-count|-c stripe_count>\n" \
273 " [--mdt-hash|-H mdt_hash]\n" \
274 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
275 " [--default|-D] [--mode|-o mode] <dir>\n" \
276 "\tstripe_count: stripe count of the striped directory\n" \
277 "\tmdt_index: MDT index of first stripe\n" \
278 "\tmdt_hash: hash type of the striped directory. mdt types:\n" \
279 " crush CRUSH hash algorithm (default)\n" \
280 " fnv_1a_64 FNV-1a hash algorithm\n" \
281 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
282 "\tdefault_stripe: set default dirstripe of the directory\n" \
283 "\tmode: the file access permission of the directory (octal)\n" \
284 "To create dir with a foreign (free format) layout :\n" \
285 "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
286 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
287 "\tmode: the mode of the directory\n" \
288 "\tforeign_type: none or daos\n"
291 * command_t mirror_cmdlist - lfs mirror commands.
293 command_t mirror_cmdlist[] = {
294 { .pc_name = "create", .pc_func = lfs_mirror_create,
295 .pc_help = "Create a mirrored file.\n"
296 "usage: lfs mirror create "
297 "<--mirror-count|-N[mirror_count]> "
298 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
299 MIRROR_CREATE_HELP },
300 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
301 .pc_help = "delete a mirror from a file.\n"
302 "usage: lfs mirror delete {--comp-id|-I <comp_id>|-p <pool>} <mirrored_file> ...\n"
304 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
305 .pc_help = "Extend a mirrored file.\n"
306 "usage: lfs mirror extend "
307 "<--mirror-count|-N[mirror_count]> [--no-verify] "
308 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
309 MIRROR_EXTEND_HELP },
310 { .pc_name = "split", .pc_func = lfs_mirror_split,
311 .pc_help = "Split a mirrored file.\n"
312 "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
313 "\t <--component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
314 "\t [-f <new_file>] <mirrored_file> ...\n"
315 "\tmirror_id: The numerical unique identifier for a mirror. It\n"
316 "\t can be fetched by lfs getstripe command.\n"
317 "\tcomp_id: Unique component ID within a mirror.\n"
318 "\tpool: Components on specified pool.\n"
319 "\tnew_file: This option indicates the layout of the split\n"
320 "\t mirror will be stored into. If not specified,\n"
321 "\t a new file named <mirrored_file>.mirror~<mirror_id>\n"
322 "\t will be used.\n" },
323 { .pc_name = "read", .pc_func = lfs_mirror_read,
324 .pc_help = "Read the content of a specified mirror of a file.\n"
325 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
326 "[--outfile|-o <output_file>] <mirrored_file>\n" },
327 { .pc_name = "write", .pc_func = lfs_mirror_write,
328 .pc_help = "Write to a specified mirror of a file.\n"
329 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
330 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
331 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
332 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
333 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
334 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
335 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
336 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
337 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
338 "<mirrored file> [<mirrored file2>...]\n"},
339 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
340 .pc_help = "Verify mirrored file(s).\n"
341 "usage: lfs mirror verify "
342 "[--only <mirror_id,mirror_id2[,...]>] "
343 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
344 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
345 .pc_help = "list commands supported by lfs mirror"},
346 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
347 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
348 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
353 * command_t pcc_cmdlist - lfs pcc commands.
355 command_t pcc_cmdlist[] = {
356 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
357 .pc_help = "Attach given files to the Persistent Client Cache.\n"
358 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
359 "\t-i: archive id for RW-PCC\n" },
360 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
361 .pc_help = "Attach given files into PCC by FID(s).\n"
362 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
364 "\t-i: archive id for RW-PCC\n"
365 "\t-m: Lustre mount point\n" },
366 { .pc_name = "state", .pc_func = lfs_pcc_state,
367 .pc_help = "Display the PCC state for given files.\n"
368 "usage: lfs pcc state <file> ...\n" },
369 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
370 .pc_help = "Detach given files from the Persistent Client Cache.\n"
371 "usage: lfs pcc detach <file> ...\n" },
372 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
373 .pc_help = "Detach given files from PCC by FID(s).\n"
374 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
375 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
376 .pc_help = "list commands supported by lfs pcc"},
377 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
378 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
379 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
383 /* all available commands */
384 command_t cmdlist[] = {
385 {"setstripe", lfs_setstripe, 0,
386 "To create a file with specified striping/composite layout, or\n"
387 "create/replace the default layout on an existing directory:\n"
388 SSM_CMD_COMMON("setstripe")
390 " <directory|filename>\n"
392 "To add component(s) to an existing composite file:\n"
393 SSM_CMD_COMMON("setstripe --component-add")
395 "To totally delete the default striping from an existing directory:\n"
396 "usage: setstripe [--delete|-d] <directory>\n"
398 "To create a mirrored file or set s default mirror layout on a directory:\n"
399 "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
401 "To delete the last component(s) from an existing composite file\n"
402 "(note that this will also delete any data in those components):\n"
403 "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
404 " [--component-flags|-F <comp_flags>]\n"
406 "\tcomp_id: Unique component ID to delete\n"
407 "\tcomp_flags: 'init' indicating all instantiated components\n"
408 "\t '^init' indicating all uninstantiated components\n"
409 "\t-I and -F cannot be specified at the same time\n"
410 "To create a file with a foreign (free format) layout:\n"
411 "usage: setstripe --foreign[=<foreign_type>]\n"
412 " --xattr|-x <layout_string> [--flags <hex>]\n"
413 " [--mode <mode>] <filename>\n"},
414 {"getstripe", lfs_getstripe, 0,
415 "To list the layout pattern for a given file or files in a\n"
416 "directory or recursively for all files in a directory tree.\n"
417 "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
418 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
419 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
420 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
421 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
422 " [--component-id[=comp_id]|-I[comp_id]]\n"
423 " [--component-flags[=comp_flags]]\n"
424 " [--component-count]\n"
425 " [--extension-size|--ext-size|-z]\n"
426 " [--component-start[=[+-]comp_start]]\n"
427 " [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
428 " [[!] --mirror-index=[+-]<index> |\n"
429 " [!] --mirror-id=[+-]<id>]\n"
430 " <directory|filename> ..."},
431 {"setdirstripe", lfs_setdirstripe, 0,
432 "Create striped directory on specified MDT, same as mkdir.\n"
433 "May be restricted to root or group users, depending on settings.\n"
434 "usage: setdirstripe [OPTION] <directory>\n"
436 {"getdirstripe", lfs_getdirstripe, 0,
437 "To list the layout pattern info for a given directory\n"
438 "or recursively for all directories in a directory tree.\n"
439 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
440 " [--mdt-hash|-H] [--obd|-O <uuid>]\n"
441 " [--recursive|-r] [--yaml|-y]\n"
442 " [--verbose|-v] [--default|-D] <dir> ..."},
443 {"mkdir", lfs_setdirstripe, 0,
444 "Create striped directory on specified MDT, same as setdirstripe.\n"
445 "usage: mkdir [OPTION] <directory>\n"
447 {"rm_entry", lfs_rmentry, 0,
448 "To remove the name entry of the remote directory. Note: This\n"
449 "command will only delete the name entry, i.e. the remote directory\n"
450 "will become inaccessable after this command. This can only be done\n"
451 "by the administrator\n"
452 "usage: rm_entry <dir>\n"},
453 {"pool_list", lfs_poollist, 0,
454 "List pools or pool OSTs\n"
455 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
456 {"find", lfs_find, 0,
457 "find files matching given attributes recursively in directory tree.\n"
458 "usage: find <directory|filename> ...\n"
459 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
460 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
461 " [[!] --newer[XY] <reference>]\n"
462 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
463 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
464 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
465 " [[!] --stripe-count|-c [+-]<stripes>]\n"
466 " [[!] --stripe-index|-i <index,...>]\n"
467 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
468 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
469 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
470 " [[!] --projid <projid>]\n"
471 " [[!] --foreign[=<foreign_type>]]\n"
472 " [[!] --layout|-L released,raid0,mdt]\n"
473 " [[!] --foreign[=<foreign_type>]]\n"
474 " [[!] --component-count [+-]<comp_cnt>]\n"
475 " [[!] --component-start [+-]N[kMGTPE]]\n"
476 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
477 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
478 " [[!] --mirror-count|-N [+-]<n>]\n"
479 " [[!] --mirror-state <[^]state>]\n"
480 " [[!] --mdt-count|-T [+-]<stripes>]\n"
481 " [[!] --mdt-hash|-H <hashtype>\n"
482 " [[!] --mdt-index|-m <uuid|index,...>]\n"
483 "\t !: used before an option indicates 'NOT' requested attribute\n"
484 "\t -: used before a value indicates less than requested value\n"
485 "\t +: used before a value indicates more than requested value\n"
486 "\thashtype: hash type of the striped directory.\n"
487 "\t fnv_1a_64 FNV-1a hash algorithm\n"
488 "\t all_char sum of characters % MDT_COUNT\n"},
489 {"check", lfs_check, 0,
490 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
491 "or all the servers (MGTs, MDTs and OSTs).\n"
492 "usage: check <mgts|osts|mdts|all>"},
493 {"osts", lfs_osts, 0, "list OSTs connected to client "
494 "[for specified path only]\n" "usage: osts [path]"},
495 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
496 "[for specified path only]\n" "usage: mdts [path]"},
498 "report filesystem disk space usage or inodes usage "
499 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
500 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
501 " [--pool|-p <fsname>[.<pool>]] [path]"},
502 {"getname", lfs_getname, 0,
503 "list instances and specified mount points [for specified path only]\n"
504 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
505 #ifdef HAVE_SYS_QUOTA_H
506 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
507 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
508 " -b <block-softlimit> -B <block-hardlimit>\n"
509 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
510 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
511 " [--block-softlimit <block-softlimit>]\n"
512 " [--block-hardlimit <block-hardlimit>]\n"
513 " [--inode-softlimit <inode-softlimit>]\n"
514 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
515 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
516 " [--block-grace 'notify'|<block-grace>]\n"
517 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
518 " setquota <-U|-G|-P>\n"
519 " -b <block-softlimit> -B <block-hardlimit>\n"
520 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
521 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
522 " [--block-softlimit <block-softlimit>]\n"
523 " [--block-hardlimit <block-hardlimit>]\n"
524 " [--inode-softlimit <inode-softlimit>]\n"
525 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
526 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
528 " -b can be used instead of --block-softlimit/--block-grace\n"
529 " -B can be used instead of --block-hardlimit\n"
530 " -i can be used instead of --inode-softlimit/--inode-grace\n"
531 " -I can be used instead of --inode-hardlimit\n"
532 " -d can be used instead of --default\n\n"
533 "Note: The total quota space will be split into many qunits and\n"
534 " balanced over all server targets, the minimal qunit size is\n"
535 " 1M bytes for block space and 1K inodes for inode space.\n\n"
536 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
537 " Quota space rebalancing process will stop when this mininum\n"
538 " value is reached. As a result, quota exceeded can be returned\n"
539 " while many targets still have 1MB or 1K inodes of spare\n"
541 " When setting the grace time, 'notify' can be used as grace to\n"
542 " be notified after the quota is over soft limit but prevents\n"
543 " the soft limit from becoming the hard limit."},
544 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
545 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
547 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
548 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
549 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
550 {"project", lfs_project, 0,
551 "Change or list project attribute for specified file or directory.\n"
552 "usage: project [-d|-r] <file|directory...>\n"
553 " list project ID and flags on file(s) or directories\n"
554 " project [-p id] [-s] [-r] <file|directory...>\n"
555 " set project ID and/or inherit flag for specified file(s) or directories\n"
556 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
557 " check project ID and flags on file(s) or directories, print outliers\n"
558 " project -C [-r] [-k] <file|directory...>\n"
559 " clear the project inherit flag and ID on the file or directory\n"
562 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
563 "usage: flushctx [-k] [mountpoint...]"},
564 {"changelog", lfs_changelog, 0,
565 "Show the metadata changes on an MDT."
566 "\nusage: changelog <mdtname> [startrec [endrec]]"},
567 {"changelog_clear", lfs_changelog_clear, 0,
568 "Indicate that old changelog records up to <endrec> are no longer of "
569 "interest to consumer <id>, allowing the system to free up space.\n"
570 "An <endrec> of 0 means all records.\n"
571 "usage: changelog_clear <mdtname> <id> <endrec>"},
572 {"fid2path", lfs_fid2path, 0,
573 "Resolve the full path(s) for given FID(s). For a specific hardlink "
574 "specify link number <linkno>.\n"
575 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
576 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
577 "usage: path2fid [--parents] <path> ..."},
578 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
579 "usage: rmfid <fsname|rootpath> <fid> ..."},
580 {"data_version", lfs_data_version, 0, "Display file data version for "
581 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
582 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
583 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
584 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
585 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
586 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
587 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
589 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
590 "[--archived] [--lost] <file> ..."},
591 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
592 "given files.\n" "usage: hsm_action <file> ..."},
593 {"hsm_archive", lfs_hsm_archive, 0,
594 "Archive file to external storage.\n"
595 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
597 {"hsm_restore", lfs_hsm_restore, 0,
598 "Restore file from external storage.\n"
599 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
600 {"hsm_release", lfs_hsm_release, 0,
601 "Release files from Lustre.\n"
602 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
603 {"hsm_remove", lfs_hsm_remove, 0,
604 "Remove file copy from external storage.\n"
605 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
607 " (FILE [FILE ...] | "
608 "--mntpath MOUNTPATH FID [FID ...])\n"
610 "Note: To remove an archived copy of a file already deleted from a "
612 "--mntpath option and a list of FIDs must be specified"
614 {"hsm_cancel", lfs_hsm_cancel, 0,
615 "Cancel requests related to specified files.\n"
616 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
617 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
618 "usage: swap_layouts <path1> <path2>"},
619 {"migrate", lfs_setstripe_migrate, 0,
620 "migrate a directory between MDTs.\n"
621 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
622 " [--mdt-hash|-H] <hash_type>\n"
623 " [--mdt-index|-m] <start_mdt_index>\n"
626 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
627 " it's the MDT index of first stripe\n"
628 "\tmdt_count: number of MDTs to stripe a directory over\n"
629 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
630 " all_char (type 1)sum of characters % MDT_COUNT\n"
631 " fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
632 " crush (type 3)CRUSH hash algorithm\n"
634 "migrate file objects from one OST "
635 "layout\nto another (may be not safe with concurent writes).\n"
637 "[--stripe-count|-c] <stripe_count>\n"
638 "[--overstripe-count|-C] <stripe_count>\n"
639 " [--stripe-index|-i] <start_ost_index>\n"
640 " [--stripe-size|-S] <stripe_size>\n"
641 " [--pool|-p] <pool_name>\n"
642 " [--ost|-o] <ost_indices>\n"
644 " [--non-block|-n]\n"
645 " [--non-direct|-D]\n"
646 " <file|directory>\n"
647 "\tstripe_count: number of OSTs to stripe a file over\n"
648 "\t Using -C instead of -c allows overstriping, which\n"
649 "\t will place more than one stripe per OST if\n"
650 "\t stripe_count is greater than the number of OSTs\n"
651 "\tstripe_ost_index: index of the first OST to stripe a file over\n"
652 "\tstripe_size: number of bytes to store before moving to the next OST\n"
653 "\tpool_name: name of the predefined pool of OSTs\n"
654 "\tost_indices: OSTs to stripe over, in order\n"
655 "\tblock: Block file access during data migration (default)\n"
656 "\tnon-block: Abort migrations if concurrent access is detected\n"
657 "\tnon-direct: do not use direct I/O to copy file contents.\n"},
659 "To move directories between MDTs. This command is deprecated, "
660 "use \"migrate\" instead.\n"
661 "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
663 {"ladvise", lfs_ladvise, 0,
664 "Provide servers with advice about access patterns for a file.\n"
665 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
666 " [--background|-b] [--unset|-u]\n\n"
667 " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
668 " {[--mode|-m [READ,WRITE]}\n"
670 {"mirror", lfs_mirror, mirror_cmdlist,
671 "lfs commands used to manage files with mirrored components:\n"
672 "lfs mirror create - create a mirrored file or directory\n"
673 "lfs mirror extend - add mirror(s) to an existing file\n"
674 "lfs mirror split - split a mirror from an existing mirrored file\n"
675 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
676 "lfs mirror read - read a mirror content of a mirrored file\n"
677 "lfs mirror write - write to a mirror of a mirrored file\n"
678 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
679 "lfs mirror verify - verify mirrored file(s)\n"},
680 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
681 "usage: getsom [-s] [-b] [-f] <path>\n"
682 "\t-s: Only show the size value of the SOM data for a given file\n"
683 "\t-b: Only show the blocks value of the SOM data for a given file\n"
684 "\t-f: Only show the flags value of the SOM data for a given file\n"},
685 {"heat_get", lfs_heat_get, 0,
686 "To get heat of files.\n"
687 "usage: heat_get <file> ...\n"},
688 {"heat_set", lfs_heat_set, 0,
689 "To set heat flags of files.\n"
690 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
691 "\t--clear|-c: Clear file heat for given files\n"
692 "\t--off|-o: Turn off file heat for given files\n"
693 "\t--on|-O: Turn on file heat for given files\n"},
694 {"pcc", lfs_pcc, pcc_cmdlist,
695 "lfs commands used to interact with PCC features:\n"
696 "lfs pcc attach - attach given files to Persistent Client Cache\n"
697 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
698 "lfs pcc state - display the PCC state for given files\n"
699 "lfs pcc detach - detach given files from Persistent Client Cache\n"
700 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
701 {"help", Parser_help, 0, "help"},
702 {"exit", Parser_quit, 0, "quit"},
703 {"quit", Parser_quit, 0, "quit"},
704 {"--version", Parser_version, 0,
705 "output build version of the utility and exit"},
706 {"--list-commands", lfs_list_commands, 0,
707 "list commands supported by the utility and exit"},
712 static int check_hashtype(const char *hashtype)
714 int type_num = atoi(hashtype);
717 /* numeric hash type */
718 if (hashtype && strlen(hashtype) == 1 &&
719 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
721 /* string hash type */
722 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
723 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
729 static uint32_t check_foreign_type_name(const char *foreign_type_name)
733 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
734 if (lu_foreign_types[i].lft_name == NULL)
736 if (strcmp(foreign_type_name,
737 lu_foreign_types[i].lft_name) == 0)
738 return lu_foreign_types[i].lft_type;
741 return LU_FOREIGN_TYPE_UNKNOWN;
744 static const char *error_loc = "syserror";
747 MIGRATION_NONBLOCK = 0x0001,
748 MIGRATION_MIRROR = 0x0002,
749 MIGRATION_NONDIRECT = 0x0004,
750 MIGRATION_VERBOSE = 0x0008,
753 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
754 struct llapi_layout *layout);
757 migrate_open_files(const char *name, __u64 migration_flags,
758 const struct llapi_stripe_param *param,
759 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
766 char parent[PATH_MAX];
767 char volatile_file[PATH_MAX];
773 if (param == NULL && layout == NULL) {
774 error_loc = "layout information";
778 /* search for file directory pathname */
779 if (strlen(name) > sizeof(parent) - 1) {
780 error_loc = "source file name";
784 strncpy(parent, name, sizeof(parent));
785 ptr = strrchr(parent, '/');
787 if (getcwd(parent, sizeof(parent)) == NULL) {
788 error_loc = "getcwd";
792 if (ptr == parent) /* leading '/' */
797 /* open file, direct io */
798 /* even if the file is only read, WR mode is nedeed to allow
799 * layout swap on fd */
801 if (!(migration_flags & MIGRATION_NONDIRECT))
803 fd = open(name, rflags);
806 error_loc = "cannot open source file";
810 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
812 error_loc = "cannot get MDT index";
817 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
818 mode_t open_mode = S_IRUSR | S_IWUSR;
820 random_value = random();
821 rc = snprintf(volatile_file, sizeof(volatile_file),
822 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
823 mdt_index, random_value);
824 if (rc >= sizeof(volatile_file)) {
829 /* create, open a volatile file, use caching (ie no directio) */
831 fdv = lfs_component_create(volatile_file, open_flags,
834 fdv = llapi_file_open_param(volatile_file, open_flags,
836 } while (fdv < 0 && (rc = fdv) == -EEXIST);
839 error_loc = "cannot create volatile file";
843 /* In case the MDT does not support creation of volatile files
844 * we should try to unlink it. */
845 (void)unlink(volatile_file);
847 /* Not-owner (root?) special case.
848 * Need to set owner/group of volatile file like original.
849 * This will allow to pass related check during layout_swap.
854 error_loc = "cannot stat source file";
858 rc = fstat(fdv, &stv);
861 error_loc = "cannot stat volatile";
865 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
866 rc = fchown(fdv, st.st_uid, st.st_gid);
869 error_loc = "cannot change ownwership of volatile";
888 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
890 struct llapi_layout *layout;
891 size_t buf_size = 4 * 1024 * 1024;
900 layout = llapi_layout_get_by_fd(fd_src, 0);
901 if (layout != NULL) {
902 uint64_t stripe_size;
904 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
906 buf_size = stripe_size;
908 llapi_layout_free(layout);
911 /* Use a page-aligned buffer for direct I/O */
912 rc = posix_memalign(&buf, getpagesize(), buf_size);
917 /* read new data only if we have written all
918 * previously read data */
921 rc = check_file(fd_src);
926 rsize = read(fd_src, buf, buf_size);
938 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
957 static int migrate_copy_timestamps(int fd, int fdv)
961 if (fstat(fd, &st) == 0) {
962 struct timeval tv[2] = {
963 {.tv_sec = st.st_atime},
964 {.tv_sec = st.st_mtime}
967 return futimes(fdv, tv);
973 static int migrate_block(int fd, int fdv)
980 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
982 error_loc = "cannot get dataversion";
990 /* The grouplock blocks all concurrent accesses to the file.
991 * It has to be taken after llapi_get_data_version as it would
993 rc = llapi_group_lock(fd, gid);
995 error_loc = "cannot get group lock";
999 rc = migrate_copy_data(fd, fdv, NULL);
1001 error_loc = "data copy failed";
1005 /* Make sure we keep original atime/mtime values */
1006 rc = migrate_copy_timestamps(fd, fdv);
1008 error_loc = "timestamp copy failed";
1013 * for a migration we need to check data version on file did
1016 * Pass in gid=0 since we already own grouplock. */
1017 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1018 SWAP_LAYOUTS_CHECK_DV1);
1019 if (rc == -EAGAIN) {
1020 error_loc = "file changed";
1022 } else if (rc < 0) {
1023 error_loc = "cannot swap layout";
1028 rc2 = llapi_group_unlock(fd, gid);
1029 if (rc2 < 0 && rc == 0) {
1030 error_loc = "unlock group lock";
1038 * Internal helper for migrate_copy_data(). Check lease and report error if
1041 * \param[in] fd File descriptor on which to check the lease.
1043 * \retval 0 Migration can keep on going.
1044 * \retval -errno Error occurred, abort migration.
1046 static int check_lease(int fd)
1050 rc = llapi_lease_check(fd);
1052 return 0; /* llapi_check_lease returns > 0 on success. */
1057 static int migrate_nonblock(int fd, int fdv)
1063 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1065 error_loc = "cannot get data version";
1069 rc = migrate_copy_data(fd, fdv, check_lease);
1071 error_loc = "data copy failed";
1075 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1077 error_loc = "cannot get data version";
1083 error_loc = "source file changed";
1087 /* Make sure we keep original atime/mtime values */
1088 rc = migrate_copy_timestamps(fd, fdv);
1090 error_loc = "timestamp copy failed";
1097 static int lfs_component_set(char *fname, int comp_id,
1098 __u32 flags, __u32 neg_flags)
1101 __u32 flags_array[2];
1106 ids[count] = comp_id;
1107 flags_array[count] = flags;
1112 if (neg_flags & LCME_FL_STALE) {
1113 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1114 "component. Please use lfs-mirror-resync(1) "
1115 "instead\n", progname);
1119 ids[count] = comp_id;
1120 flags_array[count] = neg_flags | LCME_FL_NEG;
1124 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1126 if (errno == EUCLEAN) {
1129 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1130 progname, comp_id, fname);
1133 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1134 progname, comp_id, fname, flags, neg_flags);
1141 static int lfs_component_del(char *fname, __u32 comp_id,
1142 __u32 flags, __u32 neg_flags)
1146 if (flags && neg_flags)
1149 if (!flags && neg_flags)
1150 flags = neg_flags | LCME_FL_NEG;
1152 if ((flags && comp_id) || (!flags && !comp_id))
1156 if (flags & ~LCME_KNOWN_FLAGS) {
1158 "%s setstripe: unknown flags %#x\n",
1162 } else if (comp_id > LCME_ID_MAX) {
1163 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1168 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1171 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1172 progname, comp_id, fname, strerror(errno));
1176 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1183 rc = llapi_layout_file_comp_add(fname, layout);
1185 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1186 fname, strerror(errno));
1190 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1191 struct llapi_layout *layout)
1199 fd = lstat(fname, &st);
1200 if (fd == 0 && S_ISDIR(st.st_mode))
1201 open_flags = O_DIRECTORY | O_RDONLY;
1203 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1205 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1206 S_ISDIR(st.st_mode) ?
1207 "set default composite layout for" :
1208 "create composite file",
1209 fname, strerror(errno));
1213 static int lfs_migrate(char *name, __u64 migration_flags,
1214 struct llapi_stripe_param *param,
1215 struct llapi_layout *layout)
1217 struct llapi_layout *existing;
1218 uint64_t dom_new, dom_cur;
1223 rc = migrate_open_files(name, migration_flags, param, layout,
1228 rc = llapi_layout_dom_size(layout, &dom_new);
1230 error_loc = "cannot get new layout DoM size";
1233 /* special case for migration to DOM layout*/
1234 existing = llapi_layout_get_by_fd(fd, 0);
1236 error_loc = "cannot get existing layout";
1240 rc = llapi_layout_dom_size(existing, &dom_cur);
1242 error_loc = "cannot get current layout DoM size";
1246 /* if file has DoM layout already then migration is possible to
1247 * the new layout with the same DoM component via swap layout,
1248 * if new layout used bigger DOM size, then mirroring is used
1250 if (dom_new > dom_cur) {
1251 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1254 error_loc = "cannot migrate to DOM layout";
1258 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1259 /* Blocking mode (forced if servers do not support file lease).
1260 * It is also the default mode, since we cannot distinguish
1261 * between a broken lease and a server that does not support
1262 * atomic swap/close (LU-6785) */
1263 rc = migrate_block(fd, fdv);
1267 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1269 error_loc = "cannot get lease";
1273 rc = migrate_nonblock(fd, fdv);
1275 llapi_lease_release(fd);
1279 /* Atomically put lease, swap layouts and close.
1280 * for a migration we need to check data version on file did
1282 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1284 error_loc = "cannot swap layout";
1296 fprintf(stderr, "error: %s: %s: %s: %s\n",
1297 progname, name, error_loc, strerror(-rc));
1298 else if (migration_flags & MIGRATION_VERBOSE)
1299 printf("%s\n", name);
1304 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1313 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1317 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1318 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1319 const char *comp_name = comp_flags_table[i].cfn_name;
1321 if (strcmp(name, comp_name) == 0) {
1322 *flags |= comp_flag;
1324 } else if (strncmp(name, "^", 1) == 0 &&
1325 strcmp(name + 1, comp_name) == 0) {
1326 *neg_flags |= comp_flag;
1331 llapi_printf(LLAPI_MSG_ERROR,
1332 "%s: component flag '%s' not supported\n",
1338 if (!*flags && !*neg_flags)
1341 /* don't allow to set and exclude the same flag */
1342 if (*flags & *neg_flags)
1348 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1356 if (strncmp(string, "^", 1) == 0) {
1357 *neg_state = llapi_layout_string_flags(string + 1);
1358 if (*neg_state != 0)
1361 *state = llapi_layout_string_flags(string);
1366 llapi_printf(LLAPI_MSG_ERROR,
1367 "%s: mirrored file state '%s' not supported\n",
1373 * struct mirror_args - Command-line arguments for mirror(s).
1374 * @m_count: Number of mirrors to be created with this layout.
1375 * @m_flags: Mirror level flags, only 'prefer' is supported.
1376 * @m_layout: Mirror layout.
1377 * @m_file: A victim file. Its layout will be split and used as a mirror.
1378 * @m_next: Point to the next node of the list.
1380 * Command-line arguments for mirror(s) will be parsed and stored in
1381 * a linked list that consists of this structure.
1383 struct mirror_args {
1386 struct llapi_layout *m_layout;
1388 struct mirror_args *m_next;
1393 * enum mirror_flags - Flags for extending a mirrored file.
1394 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1395 * in case the victim file(s) contains the same data as the
1396 * original mirrored file.
1397 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1398 * @MF_COMP_ID: specified component id instead of mirror id
1400 * Flags for extending a mirrored file.
1410 * mirror_create_sanity_check() - Check mirror list.
1411 * @list: A linked list that stores the mirror arguments.
1413 * This function does a sanity check on @list for creating
1416 * Return: 0 on success or a negative error code on failure.
1418 static int mirror_create_sanity_check(const char *fname,
1419 struct mirror_args *list)
1422 bool has_m_file = false;
1423 bool has_m_layout = false;
1429 struct llapi_layout *layout;
1431 layout = llapi_layout_get_by_path(fname, 0);
1434 "error: %s: file '%s' couldn't get layout\n",
1439 rc = llapi_layout_sanity(layout, false, true);
1441 llapi_layout_free(layout);
1444 llapi_layout_sanity_perror(rc);
1449 while (list != NULL) {
1450 if (list->m_file != NULL) {
1452 llapi_layout_free(list->m_layout);
1455 llapi_layout_get_by_path(list->m_file, 0);
1456 if (list->m_layout == NULL) {
1458 "error: %s: file '%s' has no layout\n",
1459 progname, list->m_file);
1463 has_m_layout = true;
1464 if (list->m_layout == NULL) {
1465 fprintf(stderr, "error: %s: no mirror layout\n",
1471 rc = llapi_layout_sanity(list->m_layout, false, true);
1473 llapi_layout_sanity_perror(rc);
1477 list = list->m_next;
1480 if (has_m_file && has_m_layout) {
1482 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1490 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1492 __u32 mirror_flags = *(__u32 *)cbdata;
1496 rc = llapi_layout_comp_flags_get(layout, &flags);
1501 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1506 return LLAPI_LAYOUT_ITER_CONT;
1510 * mirror_create() - Create a mirrored file.
1511 * @fname: The file to be created.
1512 * @mirror_list: A linked list that stores the mirror arguments.
1514 * This function creates a mirrored file @fname with the mirror(s)
1515 * from @mirror_list.
1517 * Return: 0 on success or a negative error code on failure.
1519 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1521 struct llapi_layout *layout = NULL;
1522 struct mirror_args *cur_mirror = NULL;
1523 uint16_t mirror_count = 0;
1527 rc = mirror_create_sanity_check(NULL, mirror_list);
1531 cur_mirror = mirror_list;
1532 while (cur_mirror != NULL) {
1533 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1535 &cur_mirror->m_flags);
1538 fprintf(stderr, "%s: failed to set mirror flags\n",
1543 for (i = 0; i < cur_mirror->m_count; i++) {
1544 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1547 fprintf(stderr, "error: %s: "
1548 "merge layout failed: %s\n",
1549 progname, strerror(errno));
1553 mirror_count += cur_mirror->m_count;
1554 cur_mirror = cur_mirror->m_next;
1557 if (layout == NULL) {
1558 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1562 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1565 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1566 progname, strerror(errno));
1570 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1578 llapi_layout_free(layout);
1583 * Compare files and check lease on @fd.
1585 * \retval bytes number of bytes are the same
1587 static ssize_t mirror_file_compare(int fd, int fdv)
1589 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1591 ssize_t bytes_done = 0;
1592 ssize_t bytes_read = 0;
1594 buf = malloc(buflen * 2);
1599 if (!llapi_lease_check(fd)) {
1600 bytes_done = -EBUSY;
1604 bytes_read = read(fd, buf, buflen);
1605 if (bytes_read <= 0)
1608 if (bytes_read != read(fdv, buf + buflen, buflen))
1611 /* XXX: should compute the checksum on each buffer and then
1612 * compare checksum to avoid cache collision */
1613 if (memcmp(buf, buf + buflen, bytes_read))
1616 bytes_done += bytes_read;
1624 static int mirror_extend_file(const char *fname, const char *victim_file,
1625 enum mirror_flags mirror_flags)
1630 struct stat stbuf_v;
1631 struct ll_ioc_lease *data = NULL;
1634 fd = open(fname, O_RDWR);
1636 error_loc = "open source file";
1641 fdv = open(victim_file, O_RDWR);
1643 error_loc = "open target file";
1648 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1649 error_loc = "stat source or target file";
1654 if (stbuf.st_dev != stbuf_v.st_dev) {
1655 error_loc = "stat source and target file";
1660 /* mirrors should be of the same size */
1661 if (stbuf.st_size != stbuf_v.st_size) {
1662 error_loc = "file sizes don't match";
1667 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1669 error_loc = "cannot get lease";
1673 if (!(mirror_flags & MF_NO_VERIFY)) {
1675 /* mirrors should have the same contents */
1676 ret = mirror_file_compare(fd, fdv);
1677 if (ret != stbuf.st_size) {
1678 error_loc = "file busy or contents don't match";
1679 rc = ret < 0 ? ret : -EINVAL;
1684 /* Get rid of caching pages from clients */
1685 rc = llapi_file_flush(fd);
1687 error_loc = "cannot get data version";
1691 rc = llapi_file_flush(fdv);
1693 error_loc = "cannot get data version";
1698 /* Make sure we keep original atime/mtime values */
1699 rc = migrate_copy_timestamps(fd, fdv);
1701 error_loc = "cannot copy timestamp";
1705 /* Atomically put lease, merge layouts and close. */
1706 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1708 error_loc = "memory allocation";
1711 data->lil_mode = LL_LEASE_UNLCK;
1712 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1713 data->lil_count = 1;
1714 data->lil_ids[0] = fdv;
1715 rc = llapi_lease_set(fd, data);
1717 error_loc = "cannot merge layout";
1719 } else if (rc == 0) {
1721 error_loc = "lost lease lock";
1734 (void) unlink(victim_file);
1736 fprintf(stderr, "error: %s: %s: %s: %s\n",
1737 progname, fname, error_loc, strerror(-rc));
1741 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1744 struct llapi_layout *f_layout = NULL;
1745 struct ll_ioc_lease *data = NULL;
1751 f_layout = llapi_layout_get_by_path(name, 0);
1752 if (f_layout == NULL) {
1753 fprintf(stderr, "%s: cannot get layout\n", progname);
1756 rc = llapi_layout_get_last_init_comp(f_layout);
1758 fprintf(stderr, "%s: cannot get the last init comp\n",
1762 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1765 "%s: cannot inherit from the last init comp\n",
1770 rc = migrate_open_files(name, 0, NULL, m_layout, &fd, &fdv);
1774 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1776 error_loc = "cannot get lease";
1780 rc = migrate_nonblock(fd, fdv);
1782 llapi_lease_release(fd);
1786 /* Atomically put lease, merge layouts and close. */
1787 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1789 error_loc = "memory allocation";
1792 data->lil_mode = LL_LEASE_UNLCK;
1793 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1794 data->lil_count = 1;
1795 data->lil_ids[0] = fdv;
1796 rc = llapi_lease_set(fd, data);
1798 error_loc = "cannot merge layout";
1800 } else if (rc == 0) {
1802 error_loc = "lost lease lock";
1815 fprintf(stderr, "error: %s: %s: %s: %s\n",
1816 progname, name, error_loc, strerror(-rc));
1820 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1821 enum mirror_flags mirror_flags)
1825 rc = mirror_create_sanity_check(fname, mirror_list);
1829 while (mirror_list) {
1830 if (mirror_list->m_file != NULL) {
1831 rc = mirror_extend_file(fname, mirror_list->m_file,
1834 __u32 mirror_count = mirror_list->m_count;
1836 while (mirror_count > 0) {
1837 rc = mirror_extend_layout(fname,
1838 mirror_list->m_layout,
1839 mirror_list->m_inherit);
1849 mirror_list = mirror_list->m_next;
1855 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1860 rc = llapi_layout_mirror_id_get(layout, &id);
1864 if ((__u16)id == *(__u16 *)cbdata)
1865 return LLAPI_LAYOUT_ITER_STOP;
1867 return LLAPI_LAYOUT_ITER_CONT;
1870 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1875 rc = llapi_layout_comp_id_get(layout, &id);
1879 if (id == *(__u32 *)cbdata)
1880 return LLAPI_LAYOUT_ITER_STOP;
1882 return LLAPI_LAYOUT_ITER_CONT;
1885 struct pool_to_id_cbdata {
1889 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1891 char buf[LOV_MAXPOOLNAME + 1];
1892 struct pool_to_id_cbdata *d = (void *)cbdata;
1896 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1899 if (strcmp(d->pool, buf))
1900 return LLAPI_LAYOUT_ITER_CONT;
1902 rc = llapi_layout_mirror_id_get(layout, &id);
1907 return LLAPI_LAYOUT_ITER_STOP;
1910 struct collect_ids_data {
1916 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1918 struct collect_ids_data *cid = cbdata;
1922 rc = llapi_layout_mirror_id_get(layout, &id);
1926 if ((__u16)id != cid->cid_exclude) {
1929 for (i = 0; i < cid->cid_count; i++) {
1930 /* already collected the mirror id */
1931 if (id == cid->cid_ids[i])
1932 return LLAPI_LAYOUT_ITER_CONT;
1934 cid->cid_ids[cid->cid_count] = id;
1938 return LLAPI_LAYOUT_ITER_CONT;
1942 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1943 * @mirror_id: Mirror id to be checked.
1944 * @layout: Mirror component list.
1946 * This function checks if a mirror with specified @mirror_id is the last
1947 * non-stale mirror of a layout @layout.
1949 * Return: true or false.
1952 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1954 __u16 mirror_ids[128] = { 0 };
1955 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1957 .cid_exclude = mirror_id, };
1960 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1962 for (i = 0; i < cid.cid_count; i++) {
1963 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1966 comp_size = llapi_mirror_find_stale(layout, comp_array,
1967 ARRAY_SIZE(comp_array),
1976 static int mirror_split(const char *fname, __u32 id, const char *pool,
1977 enum mirror_flags mflags, const char *victim_file)
1979 struct llapi_layout *layout;
1980 char parent[PATH_MAX];
1981 char victim[PATH_MAX];
1982 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1984 struct ll_ioc_lease *data;
1985 uint16_t mirror_count;
1990 /* check fname contains mirror with mirror_id/comp_id */
1991 layout = llapi_layout_get_by_path(fname, 0);
1994 "error %s: file '%s' couldn't get layout\n",
1999 rc = llapi_layout_sanity(layout, false, true);
2001 llapi_layout_sanity_perror(rc);
2005 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2008 "error %s: file '%s' couldn't get mirror count\n",
2012 if (mirror_count < 2) {
2014 "error %s: file '%s' has %d component, cannot split\n",
2015 progname, fname, mirror_count);
2019 if (mflags & MF_COMP_POOL) {
2020 struct pool_to_id_cbdata data = { .pool = pool };
2022 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
2025 } else if (mflags & MF_COMP_ID) {
2026 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2027 id = mirror_id_of(id);
2029 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2032 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2035 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2037 "error %s: file '%s' does not contain mirror with id %u\n",
2038 progname, fname, id);
2042 fd = open(fname, O_RDWR);
2045 "error %s: open file '%s' failed: %s\n",
2046 progname, fname, strerror(errno));
2050 /* get victim file directory pathname */
2051 if (strlen(fname) > sizeof(parent) - 1) {
2052 fprintf(stderr, "error %s: file name of '%s' too long\n",
2057 strncpy(parent, fname, sizeof(parent));
2058 ptr = strrchr(parent, '/');
2060 if (getcwd(parent, sizeof(parent)) == NULL) {
2061 fprintf(stderr, "error %s: getcwd failed: %s\n",
2062 progname, strerror(errno));
2072 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2074 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2079 if (victim_file == NULL) {
2080 /* use a temp file to store the splitted layout */
2081 if (mflags & MF_DESTROY) {
2082 if (last_non_stale_mirror(id, layout)) {
2085 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2090 fdv = llapi_create_volatile_idx(parent, mdt_index,
2091 O_LOV_DELAY_CREATE);
2093 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2095 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2098 /* user specified victim file */
2099 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2104 "error %s: create victim file failed: %s\n",
2105 progname, strerror(errno));
2109 /* get lease lock of fname */
2110 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2113 "error %s: cannot get lease of file '%s': %d\n",
2114 progname, fname, rc);
2118 /* Atomatically put lease, split layouts and close. */
2119 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2125 data->lil_mode = LL_LEASE_UNLCK;
2126 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2127 data->lil_count = 2;
2128 data->lil_ids[0] = fdv;
2129 data->lil_ids[1] = id;
2130 rc = llapi_lease_set(fd, data);
2132 if (rc == 0) /* lost lease lock */
2135 "error %s: cannot split '%s': %s\n",
2136 progname, fname, strerror(-rc));
2147 llapi_layout_free(layout);
2152 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2153 __u16 *mirror_ids, int ids_nr);
2155 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2156 __u64 migration_flags,
2157 struct llapi_stripe_param *param,
2158 struct llapi_layout *layout)
2160 struct ll_ioc_lease *data = NULL;
2163 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2165 error_loc = "cannot get lease";
2169 /* Atomically put lease, merge layouts, resync and close. */
2170 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2172 error_loc = "memory allocation";
2175 data->lil_mode = LL_LEASE_UNLCK;
2176 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2177 data->lil_count = 1;
2178 data->lil_ids[0] = fdv;
2179 rc = llapi_lease_set(fd, data);
2181 error_loc = "cannot merge layout";
2183 } else if (rc == 0) {
2185 error_loc = "lost lease lock";
2191 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2193 error_loc = "cannot resync file";
2197 /* delete first mirror now */
2198 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2200 error_loc = "cannot delete old layout";
2208 fprintf(stderr, "error: %s: %s: %s: %s\n",
2209 progname, name, error_loc, strerror(-rc));
2210 else if (migration_flags & MIGRATION_VERBOSE)
2211 printf("%s\n", name);
2218 * Parse a string containing an target index list into an array of integers.
2220 * The input string contains a comma delimited list of individual
2221 * indices and ranges, for example "1,2-4,7". Add the indices into the
2222 * \a tgts array and remove duplicates.
2224 * \param[out] tgts array to store indices in
2225 * \param[in] size size of \a tgts array
2226 * \param[in] offset starting index in \a tgts
2227 * \param[in] arg string containing OST index list
2228 * \param[in/out] overstriping index list may contain duplicates
2230 * \retval positive number of indices in \a tgts
2231 * \retval -EINVAL unable to parse \a arg
2233 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2234 unsigned long long *pattern)
2238 int slots = size - offset;
2240 bool overstriped = false;
2246 end_of_loop = false;
2247 while (!end_of_loop) {
2248 int start_index = 0;
2251 char *endptr = NULL;
2255 ptr = strchrnul(arg, ',');
2257 end_of_loop = *ptr == '\0';
2260 start_index = strtol(arg, &endptr, 0);
2261 if (endptr == arg) /* no data at all */
2263 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2266 end_index = start_index;
2267 if (*endptr == '-') {
2268 end_index = strtol(endptr + 1, &endptr, 0);
2269 if (*endptr != '\0')
2271 if (end_index < start_index)
2275 for (i = start_index; i <= end_index && slots > 0; i++) {
2278 /* remove duplicate */
2279 for (j = 0; j < offset; j++) {
2280 if (tgts[j] == i && pattern &&
2281 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2283 else if (tgts[j] == i)
2289 if (j == offset) { /* check complete */
2295 if (slots == 0 && i < end_index)
2303 if (!end_of_loop && ptr != NULL)
2306 if (!overstriped && pattern)
2307 *pattern = LLAPI_LAYOUT_DEFAULT;
2309 return rc < 0 ? rc : nr;
2312 struct lfs_setstripe_args {
2313 unsigned long long lsa_comp_end;
2314 unsigned long long lsa_stripe_size;
2315 unsigned long long lsa_extension_size;
2316 long long lsa_stripe_count;
2317 long long lsa_stripe_off;
2318 __u32 lsa_comp_flags;
2319 __u32 lsa_comp_neg_flags;
2320 unsigned long long lsa_pattern;
2321 unsigned int lsa_mirror_count;
2323 bool lsa_first_comp;
2324 bool lsa_extension_comp;
2326 char *lsa_pool_name;
2329 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2331 unsigned int mirror_count = lsa->lsa_mirror_count;
2332 bool first_comp = lsa->lsa_first_comp;
2334 memset(lsa, 0, sizeof(*lsa));
2336 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2337 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2338 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2339 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2340 lsa->lsa_pool_name = NULL;
2342 lsa->lsa_mirror_count = mirror_count;
2343 lsa->lsa_first_comp = first_comp;
2347 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2348 * @lsa: Stripe options to be initialized and inherited.
2350 * This function initializes stripe options in @lsa and inherit
2351 * stripe_size, stripe_count and OST pool_name options.
2355 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2357 unsigned long long stripe_size;
2358 long long stripe_count;
2359 char *pool_name = NULL;
2361 stripe_size = lsa->lsa_stripe_size;
2362 stripe_count = lsa->lsa_stripe_count;
2363 pool_name = lsa->lsa_pool_name;
2365 setstripe_args_init(lsa);
2367 lsa->lsa_stripe_size = stripe_size;
2368 lsa->lsa_stripe_count = stripe_count;
2369 lsa->lsa_pool_name = pool_name;
2372 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2374 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2375 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2376 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2377 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2378 lsa->lsa_comp_end != 0);
2382 * comp_args_to_layout() - Create or extend a composite layout.
2383 * @composite: Pointer to the composite layout.
2384 * @lsa: Stripe options for the new component.
2386 * This function creates or extends a composite layout by adding a new
2387 * component with stripe options from @lsa.
2389 * Return: 0 on success or an error code on failure.
2391 static int comp_args_to_layout(struct llapi_layout **composite,
2392 struct lfs_setstripe_args *lsa,
2395 struct llapi_layout *layout = *composite;
2396 uint64_t prev_end = 0;
2401 if (layout == NULL) {
2402 layout = llapi_layout_alloc();
2403 if (layout == NULL) {
2404 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2409 *composite = layout;
2410 lsa->lsa_first_comp = true;
2414 /* Get current component extent, current component
2415 * must be the tail component. */
2416 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2418 fprintf(stderr, "Get comp extent failed. %s\n",
2423 if (lsa->lsa_first_comp) {
2425 rc = llapi_layout_add_first_comp(layout);
2427 rc = llapi_layout_comp_add(layout);
2430 fprintf(stderr, "Add component failed. %s\n",
2436 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2438 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2439 lsa->lsa_comp_flags, strerror(errno));
2444 uint64_t comp_end = lsa->lsa_comp_end;
2446 /* The extendable component is 0-length, so it can be removed
2447 * if there is insufficient space to extend it. */
2448 if (lsa->lsa_extension_comp)
2449 comp_end = prev_end;
2451 rc = llapi_layout_comp_extent_set(layout, prev_end,
2454 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2455 prev_end, comp_end, strerror(errno));
2459 /* reset lsa_first_comp */
2460 lsa->lsa_first_comp = false;
2462 /* Data-on-MDT component setting */
2463 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2464 /* In case of Data-on-MDT patterns the only extra option
2465 * applicable is stripe size option. */
2466 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2467 fprintf(stderr, "Option 'stripe-count' can't be "
2468 "specified with Data-on-MDT component: %lld\n",
2469 lsa->lsa_stripe_count);
2473 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2474 fprintf(stderr, "Option 'stripe-size' can't be "
2475 "specified with Data-on-MDT component: %llu\n",
2476 lsa->lsa_stripe_size);
2480 if (lsa->lsa_nr_tgts != 0) {
2481 fprintf(stderr, "Option 'ost-list' can't be specified "
2482 "with Data-on-MDT component: '%i'\n",
2487 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2488 fprintf(stderr, "Option 'stripe-offset' can't be "
2489 "specified with Data-on-MDT component: %lld\n",
2490 lsa->lsa_stripe_off);
2494 if (lsa->lsa_pool_name != 0) {
2495 fprintf(stderr, "Option 'pool' can't be specified "
2496 "with Data-on-MDT component: '%s'\n",
2497 lsa->lsa_pool_name);
2502 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2504 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2509 /* Data-on-MDT component has always single stripe up to end */
2510 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2511 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2512 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2514 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2521 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2522 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2524 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2525 rc = llapi_layout_extension_size_set(layout, size);
2527 rc = llapi_layout_stripe_size_set(layout, size);
2530 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2531 size, strerror(errno));
2535 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2537 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2538 lsa->lsa_stripe_count, strerror(errno));
2542 if (lsa->lsa_pool_name != NULL) {
2543 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2545 fprintf(stderr, "Set pool name: %s failed. %s\n",
2546 lsa->lsa_pool_name, strerror(errno));
2550 rc = llapi_layout_pool_name_set(layout, "");
2552 fprintf(stderr, "Clear pool name failed: %s\n",
2558 if (lsa->lsa_nr_tgts > 0) {
2559 if (lsa->lsa_stripe_count > 0 &&
2560 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2561 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2562 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2563 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2564 lsa->lsa_stripe_count,
2569 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2570 rc = llapi_layout_ost_index_set(layout, i,
2575 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2576 lsa->lsa_stripe_off != -1) {
2577 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2580 fprintf(stderr, "Set ost index %d failed. %s\n",
2581 i, strerror(errno));
2585 /* Create the second, virtual component of extension space */
2586 if (lsa->lsa_extension_comp) {
2587 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2588 lsa->lsa_extension_comp = false;
2595 static int build_component(struct llapi_layout **layout,
2596 struct lfs_setstripe_args *lsa, bool set_extent)
2600 rc = comp_args_to_layout(layout, lsa, set_extent);
2604 if (lsa->lsa_mirror_count > 0) {
2605 rc = llapi_layout_mirror_count_set(*layout,
2606 lsa->lsa_mirror_count);
2610 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2613 lsa->lsa_mirror_count = 0;
2619 static int build_layout_from_yaml_node(struct cYAML *node,
2620 struct llapi_layout **layout,
2621 struct lfs_setstripe_args *lsa,
2628 if (node->cy_type == CYAML_TYPE_OBJECT) {
2629 /* go deep to sub blocks */
2630 rc = build_layout_from_yaml_node(node->cy_child, layout,
2635 if (node->cy_string == NULL)
2638 string = node->cy_string;
2639 /* skip leading lmm_ if present, to simplify parsing */
2640 if (strncmp(string, "lmm_", 4) == 0)
2643 if (node->cy_type == CYAML_TYPE_STRING) {
2644 if (!strcmp(string, "lcme_extent.e_end")) {
2645 if (!strcmp(node->cy_valuestring, "EOF") ||
2646 !strcmp(node->cy_valuestring, "eof"))
2647 lsa->lsa_comp_end = LUSTRE_EOF;
2648 } else if (!strcmp(string, "pool")) {
2649 lsa->lsa_pool_name = node->cy_valuestring;
2650 } else if (!strcmp(string, "pattern")) {
2651 if (!strcmp(node->cy_valuestring, "mdt"))
2652 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2653 if (!strcmp(node->cy_valuestring,
2654 "raid0,overstriped"))
2656 LLAPI_LAYOUT_OVERSTRIPING;
2657 } else if (!strcmp(string, "lcme_flags")) {
2658 rc = comp_str2flags(node->cy_valuestring,
2659 &lsa->lsa_comp_flags,
2660 &lsa->lsa_comp_neg_flags);
2663 /* Only template flags have meaning in
2664 * the layout for a new file
2666 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2668 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2669 if (!strcmp(string, "lcm_mirror_count")) {
2670 lsa->lsa_mirror_count = node->cy_valueint;
2671 } else if (!strcmp(string, "lcme_extent.e_start")) {
2672 if (node->cy_valueint != 0 || *layout != NULL) {
2673 rc = build_component(layout, lsa, true);
2678 if (node->cy_valueint == 0)
2679 lsa->lsa_first_comp = true;
2681 /* initialize lsa */
2682 setstripe_args_init(lsa);
2683 lsa->lsa_tgts = osts;
2684 } else if (!strcmp(string, "lcme_extent.e_end")) {
2685 if (node->cy_valueint == -1)
2686 lsa->lsa_comp_end = LUSTRE_EOF;
2688 lsa->lsa_comp_end = node->cy_valueint;
2689 } else if (!strcmp(string, "stripe_count")) {
2690 lsa->lsa_stripe_count = node->cy_valueint;
2691 } else if (!strcmp(string, "stripe_size")) {
2692 lsa->lsa_stripe_size = node->cy_valueint;
2693 } else if (!strcmp(string, "stripe_offset")) {
2694 lsa->lsa_stripe_off = node->cy_valueint;
2695 } else if (!strcmp(string, "l_ost_idx")) {
2696 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2701 node = node->cy_next;
2707 static int lfs_comp_create_from_yaml(char *template,
2708 struct llapi_layout **layout,
2709 struct lfs_setstripe_args *lsa,
2712 struct cYAML *tree = NULL, *err_rc = NULL;
2715 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2717 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2718 progname, template);
2719 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2720 "can't parse", &err_rc);
2721 cYAML_print_tree2file(stderr, err_rc);
2722 cYAML_free_tree(err_rc);
2727 /* initialize lsa for plain file */
2728 setstripe_args_init(lsa);
2729 lsa->lsa_tgts = osts;
2731 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2733 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2734 progname, template);
2737 rc = build_component(layout, lsa, *layout != NULL);
2739 /* clean clean lsa */
2740 setstripe_args_init(lsa);
2744 cYAML_free_tree(tree);
2749 * Get the extension size from the next (SEL) component and extend the
2750 * current component on it. The start of the next component is to be
2753 * \param[in] layout the current layout
2754 * \param[in] start the start of the current component
2755 * \param[in,out] end the end of the current component
2756 * \param[in] offset the offset to adjust the end position to instead of
2759 * \retval 0 - extended successfully
2760 * \retval < 0 - error
2762 static int layout_extend_comp(struct llapi_layout *layout,
2763 uint64_t start, uint64_t *end,
2766 uint64_t size, next_start, next_end;
2769 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2771 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2772 "%s\n", progname, strerror(errno));
2776 /* Even if the @size will not be used below, this will fail if
2777 * this is not a SEL component - a good confirmation we are
2778 * working on right components. */
2779 rc = llapi_layout_extension_size_get(layout, &size);
2781 fprintf(stderr, "%s setstripe: cannot get component ext size: "
2782 "%s\n", progname, strerror(errno));
2786 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2788 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2789 progname, strerror(errno));
2793 next_start += offset ?: size;
2794 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2796 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2797 progname, strerror(errno));
2801 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2803 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2804 "%s\n", progname, strerror(errno));
2808 *end += offset ?: size;
2809 rc = llapi_layout_comp_extent_set(layout, start, *end);
2811 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2812 progname, strerror(errno));
2820 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2821 * end of the last component in the existing file, and adjust the
2822 * first extent start of the components to be added accordingly.
2824 * In the create mode, we need to check if the first component is an extendable
2825 * SEL component and extend its length to the extension size (first component
2826 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2828 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2831 struct llapi_layout *head;
2832 uint64_t start = 0, prev_end = 0;
2841 head = llapi_layout_get_by_path(fname, 0);
2844 "%s setstripe: cannot read layout from '%s': "
2845 "%s\n", progname, fname, strerror(errno));
2847 } else if (errno == ENODATA) {
2848 /* file without LOVEA, this component-add will be turned
2849 * into a component-create. */
2850 llapi_layout_free(head);
2853 /* the new layout will be added to an empty one, it
2854 * still needs to be adjusted below */
2857 } else if (!llapi_layout_is_composite(head)) {
2858 fprintf(stderr, "%s setstripe: '%s' not a composite "
2859 "file\n", progname, fname);
2860 llapi_layout_free(head);
2864 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2866 fprintf(stderr, "%s setstripe: cannot get prev "
2867 "extent: %s\n", progname, strerror(errno));
2868 llapi_layout_free(head);
2872 llapi_layout_free(head);
2876 /* Make sure we use the first component of the layout to be added. */
2877 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2880 "%s setstripe: cannot move component cursor: %s\n",
2881 progname, strerror(errno));
2885 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2887 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2888 progname, strerror(errno));
2892 if (start == 0 && end == 0) {
2893 rc = layout_extend_comp(layout, start, &end,
2894 comp_add ? prev_end : 0);
2899 if (start > prev_end || end < prev_end) {
2900 fprintf(stderr, "%s setstripe: first extent [%lu, %lu) not "
2901 "adjacent with extent end %lu\n",
2902 progname, start, end, prev_end);
2906 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2908 fprintf(stderr, "%s setstripe: cannot set component extent "
2910 progname, prev_end, end, strerror(errno));
2917 static int mirror_adjust_first_extents(struct mirror_args *list)
2924 while (list != NULL) {
2925 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
2928 list = list->m_next;
2934 static inline bool arg_is_eof(char *arg)
2936 return !strncmp(arg, "-1", strlen("-1")) ||
2937 !strncmp(arg, "EOF", strlen("EOF")) ||
2938 !strncmp(arg, "eof", strlen("eof"));
2942 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2944 * Return: Valid mirror_args pointer on success and
2945 * NULL if memory allocation fails.
2947 static struct mirror_args *lfs_mirror_alloc(void)
2949 struct mirror_args *mirror = NULL;
2952 mirror = calloc(1, sizeof(*mirror));
2953 if (mirror != NULL) {
2954 mirror->m_inherit = false;
2965 * lfs_mirror_free() - Free memory allocated for a mirror argument
2967 * @mirror: Previously allocated mirror argument structure by
2968 * lfs_mirror_alloc().
2970 * Free memory allocated for @mirror.
2974 static void lfs_mirror_free(struct mirror_args *mirror)
2976 if (mirror->m_layout != NULL)
2977 llapi_layout_free(mirror->m_layout);
2982 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2983 * @mirror_list: Previously allocated mirror list.
2985 * Free memory allocated for @mirror_list.
2989 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2991 struct mirror_args *next_mirror = NULL;
2993 while (mirror_list != NULL) {
2994 next_mirror = mirror_list->m_next;
2995 lfs_mirror_free(mirror_list);
2996 mirror_list = next_mirror;
3008 LFS_COMP_NO_VERIFY_OPT,
3010 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3012 LFS_MIRROR_STATE_OPT,
3014 LFS_MIRROR_INDEX_OPT,
3015 LFS_LAYOUT_FOREIGN_OPT,
3021 static int lfs_setstripe_internal(int argc, char **argv,
3022 enum setstripe_origin opc)
3024 struct lfs_setstripe_args lsa = { 0 };
3025 struct llapi_stripe_param *param = NULL;
3026 struct find_param migrate_mdt_param = {
3036 unsigned long long size_units = 1;
3037 bool migrate_mode = false;
3038 bool migrate_mdt_mode = false;
3039 bool setstripe_mode = false;
3040 bool migration_block = false;
3041 __u64 migration_flags = 0;
3042 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3043 int comp_del = 0, comp_set = 0;
3046 struct llapi_layout *layout = NULL;
3047 struct llapi_layout **lpp = &layout;
3048 bool mirror_mode = false;
3049 bool has_m_file = false;
3050 __u32 mirror_count = 0;
3051 enum mirror_flags mirror_flags = 0;
3052 struct mirror_args *mirror_list = NULL;
3053 struct mirror_args *new_mirror = NULL;
3054 struct mirror_args *last_mirror = NULL;
3055 __u16 mirror_id = 0;
3057 bool from_yaml = false;
3058 bool from_copy = false;
3059 char *template = NULL;
3060 bool foreign_mode = false;
3062 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3063 char *mode_opt = NULL;
3064 mode_t previous_umask = 0;
3065 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3067 struct option long_opts[] = {
3068 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3069 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3070 /* --block is only valid in migrate mode */
3071 { .val = 'b', .name = "block", .has_arg = no_argument },
3072 { .val = LFS_COMP_ADD_OPT,
3073 .name = "comp-add", .has_arg = no_argument },
3074 { .val = LFS_COMP_ADD_OPT,
3075 .name = "component-add", .has_arg = no_argument },
3076 { .val = LFS_COMP_DEL_OPT,
3077 .name = "comp-del", .has_arg = no_argument },
3078 { .val = LFS_COMP_DEL_OPT,
3079 .name = "component-del", .has_arg = no_argument },
3080 { .val = LFS_COMP_FLAGS_OPT,
3081 .name = "comp-flags", .has_arg = required_argument },
3082 { .val = LFS_COMP_FLAGS_OPT,
3083 .name = "component-flags",
3084 .has_arg = required_argument },
3085 { .val = LFS_COMP_SET_OPT,
3086 .name = "comp-set", .has_arg = no_argument },
3087 { .val = LFS_COMP_SET_OPT,
3088 .name = "component-set",
3089 .has_arg = no_argument},
3090 { .val = LFS_COMP_NO_VERIFY_OPT,
3091 .name = "no-verify", .has_arg = no_argument},
3092 { .val = LFS_LAYOUT_FLAGS_OPT,
3093 .name = "flags", .has_arg = required_argument},
3094 { .val = LFS_LAYOUT_FOREIGN_OPT,
3095 .name = "foreign", .has_arg = optional_argument},
3096 { .val = LFS_MIRROR_ID_OPT,
3097 .name = "mirror-id", .has_arg = required_argument},
3098 { .val = LFS_MODE_OPT,
3099 .name = "mode", .has_arg = required_argument},
3100 { .val = LFS_LAYOUT_COPY,
3101 .name = "copy", .has_arg = required_argument},
3102 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3103 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3104 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3105 { .val = 'C', .name = "overstripe-count",
3106 .has_arg = required_argument},
3107 { .val = 'd', .name = "delete", .has_arg = no_argument},
3108 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3109 /* --non-direct is only valid in migrate mode */
3110 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3111 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3112 { .val = 'E', .name = "component-end",
3113 .has_arg = required_argument},
3114 { .val = 'f', .name = "file", .has_arg = required_argument },
3115 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3116 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3117 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3118 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3119 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3120 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3121 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3122 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3123 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3124 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3125 { .val = 'L', .name = "layout", .has_arg = required_argument },
3126 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3127 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3128 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3129 /* --non-block is only valid in migrate mode */
3130 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3131 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3132 { .val = 'o', .name = "ost", .has_arg = required_argument },
3133 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3134 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3135 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3137 { .val = 'p', .name = "pool", .has_arg = required_argument },
3138 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3139 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3140 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3141 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3142 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3143 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3144 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3145 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3146 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3147 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3148 /* --verbose is only valid in migrate mode */
3149 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3150 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3151 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3152 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3153 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3156 setstripe_args_init(&lsa);
3158 migrate_mode = (opc == SO_MIGRATE);
3159 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3160 setstripe_mode = (opc == SO_SETSTRIPE);
3161 if (opc == SO_MIRROR_DELETE) {
3163 mirror_flags = MF_DESTROY;
3166 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3168 while ((c = getopt_long(argc, argv,
3169 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3170 long_opts, NULL)) >= 0) {
3176 case LFS_COMP_ADD_OPT:
3179 case LFS_COMP_DEL_OPT:
3182 case LFS_COMP_FLAGS_OPT:
3183 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3184 &lsa.lsa_comp_neg_flags);
3187 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3188 fprintf(stderr, "%s: inverted flags are not supported\n",
3193 case LFS_COMP_SET_OPT:
3196 case LFS_COMP_NO_VERIFY_OPT:
3197 mirror_flags |= MF_NO_VERIFY;
3199 case LFS_MIRROR_ID_OPT:
3200 mirror_id = strtoul(optarg, &end, 0);
3201 if (*end != '\0' || mirror_id == 0) {
3203 "%s %s: invalid mirror ID '%s'\n",
3204 progname, argv[0], optarg);
3208 case LFS_LAYOUT_FLAGS_OPT: {
3211 /* check for numeric flags (foreign and mirror cases) */
3212 if (setstripe_mode && !mirror_mode && !last_mirror) {
3213 flags = strtoul(optarg, &end, 16);
3216 "%s %s: bad flags '%s'\n",
3217 progname, argv[0], optarg);
3223 if (!mirror_mode || !last_mirror) {
3224 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
3229 result = comp_str2flags(optarg, &last_mirror->m_flags,
3235 fprintf(stderr, "%s: inverted flags are not supported\n",
3240 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3242 "%s: unsupported mirror flags: %s\n",
3249 case LFS_LAYOUT_FOREIGN_OPT:
3250 if (optarg != NULL) {
3251 /* check pure numeric */
3252 type = strtoul(optarg, &end, 0);
3255 type = check_foreign_type_name(optarg);
3256 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3258 "%s %s: unrecognized foreign type '%s'\n",
3265 foreign_mode = true;
3269 if (mode_opt != NULL) {
3270 mode = strtoul(mode_opt, &end, 8);
3273 "%s %s: bad mode '%s'\n",
3274 progname, argv[0], mode_opt);
3277 previous_umask = umask(0);
3280 case LFS_LAYOUT_COPY:
3285 if (!migrate_mode) {
3287 "%s %s: -b|--block valid only for migrate command\n",
3291 migration_block = true;
3294 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3296 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3300 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3303 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3306 "%s %s: invalid stripe count '%s'\n",
3307 progname, argv[0], optarg);
3311 if (lsa.lsa_stripe_count == -1)
3312 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3315 /* delete the default striping pattern */
3317 if (opc == SO_MIRROR_SPLIT) {
3320 "%s %s: -d cannot used with -f\n",
3324 mirror_flags |= MF_DESTROY;
3328 if (!migrate_mode) {
3330 "%s %s: -D|--non-direct is valid "
3331 "only for migrate command\n",
3335 migration_flags |= MIGRATION_NONDIRECT;
3338 if (lsa.lsa_comp_end != 0) {
3339 result = comp_args_to_layout(lpp, &lsa, true);
3341 fprintf(stderr, "%s: invalid layout\n",
3346 setstripe_args_init_inherit(&lsa);
3349 if (arg_is_eof(optarg)) {
3350 lsa.lsa_comp_end = LUSTRE_EOF;
3352 result = llapi_parse_size(optarg,
3357 "%s %s: invalid component end '%s'\n",
3358 progname, argv[0], optarg);
3364 if (!migrate_mode) {
3365 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3369 lsa.lsa_pattern = check_hashtype(optarg);
3370 if (lsa.lsa_pattern == 0) {
3372 "%s %s: bad stripe hash type '%s'\n",
3373 progname, argv[0], optarg);
3378 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3381 "%s %s: invalid stripe offset '%s'\n",
3382 progname, argv[0], optarg);
3385 if (lsa.lsa_stripe_off == -1)
3386 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3389 comp_id = strtoul(optarg, &end, 0);
3390 if (*end != '\0' || comp_id == 0 ||
3391 comp_id > LCME_ID_MAX) {
3393 "%s %s: invalid component ID '%s'\n",
3394 progname, argv[0], optarg);
3399 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3401 "error: %s: invalid option: %s\n",
3402 progname, argv[optopt + 1]);
3405 if (opc == SO_MIRROR_EXTEND) {
3406 if (last_mirror == NULL) {
3408 "error: %s: '-N' must exist in front of '%s'\n",
3409 progname, argv[optopt + 1]);
3412 last_mirror->m_file = optarg;
3413 last_mirror->m_count = 1;
3416 if (mirror_list == NULL)
3417 mirror_list = lfs_mirror_alloc();
3418 mirror_list->m_file = optarg;
3423 if (strcmp(argv[optind - 1], "mdt") == 0) {
3424 /* Can be only the first component */
3425 if (layout != NULL) {
3427 fprintf(stderr, "error: 'mdt' layout "
3428 "can be only the first one\n");
3431 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3433 fprintf(stderr, "error: 'mdt' layout "
3434 "size is too big\n");
3437 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3438 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3440 fprintf(stderr, "error: layout '%s' is "
3441 "unknown, supported layouts are: "
3442 "'mdt', 'raid0'\n", argv[optind]);
3447 if (!migrate_mode) {
3449 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3453 migrate_mdt_mode = true;
3454 lsa.lsa_nr_tgts = parse_targets(tgts,
3455 sizeof(tgts) / sizeof(__u32),
3456 lsa.lsa_nr_tgts, optarg, NULL);
3457 if (lsa.lsa_nr_tgts < 0) {
3459 "%s %s: invalid MDT target(s) '%s'\n",
3460 progname, argv[0], optarg);
3464 lsa.lsa_tgts = tgts;
3465 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3466 lsa.lsa_stripe_off = tgts[0];
3469 if (!migrate_mode) {
3471 "%s %s: -n|--non-block valid only for migrate command\n",
3475 migration_flags |= MIGRATION_NONBLOCK;
3478 if (opc == SO_SETSTRIPE) {
3479 opc = SO_MIRROR_CREATE;
3483 if (optarg != NULL) {
3484 mirror_count = strtoul(optarg, &end, 0);
3485 if (*end != '\0' || mirror_count == 0) {
3487 "error: %s: bad mirror count: %s\n",
3494 new_mirror = lfs_mirror_alloc();
3495 new_mirror->m_count = mirror_count;
3497 if (mirror_list == NULL)
3498 mirror_list = new_mirror;
3500 if (last_mirror != NULL) {
3501 /* wrap up last mirror */
3502 if (!setstripe_args_specified(&lsa))
3503 last_mirror->m_inherit = true;
3504 if (lsa.lsa_comp_end == 0)
3505 lsa.lsa_comp_end = LUSTRE_EOF;
3507 result = comp_args_to_layout(lpp, &lsa, true);
3509 lfs_mirror_free(new_mirror);
3513 setstripe_args_init_inherit(&lsa);
3515 last_mirror->m_next = new_mirror;
3518 last_mirror = new_mirror;
3519 lpp = &last_mirror->m_layout;
3522 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3523 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3524 fprintf(stderr, "warning: '--ost-list' is "
3525 "deprecated, use '--ost' instead\n");
3527 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3529 "%s %s: -o|--ost incompatible with DoM layout\n",
3533 /* -o allows overstriping, and must note it because
3534 * parse_targets is shared with MDT striping, which
3535 * does not allow duplicates
3537 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3538 lsa.lsa_nr_tgts = parse_targets(tgts,
3539 sizeof(tgts) / sizeof(__u32),
3540 lsa.lsa_nr_tgts, optarg,
3542 if (lsa.lsa_nr_tgts < 0) {
3544 "%s %s: invalid OST target(s) '%s'\n",
3545 progname, argv[0], optarg);
3549 lsa.lsa_tgts = tgts;
3550 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3551 lsa.lsa_stripe_off = tgts[0];
3556 lsa.lsa_pool_name = optarg;
3558 if (strlen(lsa.lsa_pool_name) == 0 ||
3559 strncmp(lsa.lsa_pool_name, "none",
3560 LOV_MAXPOOLNAME) == 0)
3561 lsa.lsa_pool_name = NULL;
3564 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3568 "%s %s: invalid stripe size '%s'\n",
3569 progname, argv[0], optarg);
3574 if (!migrate_mode) {
3576 "%s %s: -v|--verbose valid only for migrate command\n",
3580 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3581 migration_flags = MIGRATION_VERBOSE;
3591 result = llapi_parse_size(optarg,
3592 &lsa.lsa_extension_size,
3596 "%s %s: invalid extension size '%s'\n",
3597 progname, argv[0], optarg);
3601 lsa.lsa_extension_comp = true;
3604 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3605 progname, argv[0], argv[optind - 1]);
3610 fname = argv[optind];
3612 if (optind == argc) {
3613 fprintf(stderr, "%s %s: FILE must be specified\n",
3618 /* lfs migrate $filename should keep the file's layout by default */
3619 if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
3623 if (xattr && !foreign_mode) {
3624 /* only print a warning as this is harmless and will be ignored
3627 "%s %s: xattr has been specified for non-foreign layout\n",
3629 } else if (foreign_mode && !xattr) {
3631 "%s %s: xattr must be provided in foreign mode\n",
3636 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3637 comp_set || comp_id || delete || from_copy ||
3638 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3641 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3646 if (mirror_mode && mirror_count == 0) {
3648 "error: %s: --mirror-count|-N option is required\n",
3655 if (!setstripe_args_specified(&lsa))
3656 last_mirror->m_inherit = true;
3657 if (lsa.lsa_comp_end == 0)
3658 lsa.lsa_comp_end = LUSTRE_EOF;
3661 if (lsa.lsa_comp_end != 0) {
3662 result = comp_args_to_layout(lpp, &lsa, true);
3664 fprintf(stderr, "error: %s: invalid layout\n",
3671 if (mirror_flags & MF_NO_VERIFY) {
3672 if (opc != SO_MIRROR_EXTEND) {
3674 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3678 } else if (!has_m_file) {
3680 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3687 if (comp_set && !comp_id) {
3688 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3693 if ((delete + comp_set + comp_del + comp_add) > 1) {
3695 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3700 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3701 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3703 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3708 if ((comp_set || comp_del) &&
3709 (setstripe_args_specified(&lsa) || layout != NULL)) {
3711 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3716 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3718 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3723 if (comp_add || comp_del) {
3726 result = lstat(fname, &st);
3727 if (result == 0 && S_ISDIR(st.st_mode)) {
3729 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3735 fprintf(stderr, "error: %s: can't use --component-add "
3736 "or --component-del for mirror operation\n",
3743 if (layout == NULL) {
3745 "%s %s: option -E must be specified with --component-add\n",
3751 if (layout != NULL || mirror_list != NULL) {
3753 result = mirror_adjust_first_extents(mirror_list);
3755 result = layout_adjust_first_extent(fname, layout,
3757 if (result == -ENODATA)
3759 else if (result != 0) {
3760 fprintf(stderr, "error: %s: invalid layout\n",
3766 if (from_yaml && from_copy) {
3768 "%s: can't specify --yaml and --copy together\n",
3773 if ((from_yaml || from_copy) &&
3774 (setstripe_args_specified(&lsa) || layout != NULL)) {
3775 fprintf(stderr, "error: %s: can't specify --yaml or --copy with"
3776 " -c, -S, -i, -o, -p or -E options.\n",
3781 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3783 "%s %s: options --non-block and --block are mutually exclusive\n",
3788 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3789 opc != SO_MIRROR_DELETE && comp_id != 0) {
3791 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3796 if (migrate_mdt_mode) {
3797 struct lmv_user_md *lmu;
3799 /* initialize migrate mdt parameters */
3800 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3801 LMV_USER_MAGIC_SPECIFIC));
3804 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3805 progname, argv[0], strerror(ENOMEM));
3809 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3810 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3811 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3813 "%s %s: migrate should specify MDT index\n",
3818 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3819 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3820 lmu->lum_hash_type = lsa.lsa_pattern;
3822 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
3823 if (lsa.lsa_pool_name) {
3824 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3825 sizeof(lmu->lum_pool_name) - 1);
3826 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3828 if (lsa.lsa_nr_tgts > 1) {
3831 if (lsa.lsa_stripe_count > 0 &&
3832 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3833 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3835 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3836 progname, lsa.lsa_stripe_count,
3842 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3843 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3844 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3845 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3847 lmu->lum_magic = LMV_USER_MAGIC;
3850 migrate_mdt_param.fp_lmv_md = lmu;
3851 migrate_mdt_param.fp_migrate = 1;
3852 } else if (layout == NULL) {
3853 /* initialize stripe parameters */
3854 param = calloc(1, offsetof(typeof(*param),
3855 lsp_osts[lsa.lsa_nr_tgts]));
3856 if (param == NULL) {
3858 "%s %s: cannot allocate memory for parameters: %s\n",
3859 progname, argv[0], strerror(ENOMEM));
3864 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3865 param->lsp_stripe_size = lsa.lsa_stripe_size;
3866 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3867 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3868 param->lsp_stripe_count = -1;
3870 param->lsp_stripe_count = lsa.lsa_stripe_count;
3872 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3873 param->lsp_stripe_offset = -1;
3875 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3876 param->lsp_stripe_pattern =
3877 llapi_pattern_to_lov(lsa.lsa_pattern);
3878 if (param->lsp_stripe_pattern == EINVAL) {
3879 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3884 param->lsp_pool = lsa.lsa_pool_name;
3885 param->lsp_is_specific = false;
3886 if (lsa.lsa_nr_tgts > 0) {
3887 if (lsa.lsa_stripe_count > 0 &&
3888 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3889 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3890 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3892 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3893 argv[0], lsa.lsa_stripe_count,
3899 param->lsp_is_specific = true;
3900 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3901 memcpy(param->lsp_osts, tgts,
3902 sizeof(*tgts) * lsa.lsa_nr_tgts);
3907 /* generate a layout from a YAML template */
3908 result = lfs_comp_create_from_yaml(template, &layout,
3911 fprintf(stderr, "error: %s: can't create composite "
3912 "layout from template file %s\n",
3918 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3920 layout = llapi_layout_get_by_path(template ?: fname, 0);
3921 if (layout == NULL) {
3922 fprintf(stderr, "%s: can't create composite "
3923 "layout from file %s.\n",
3924 progname, template ?: fname);
3929 if (migrate_mdt_mode) {
3930 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3931 } else if (migrate_mode) {
3932 result = lfs_migrate(fname, migration_flags, param,
3934 } else if (comp_set != 0) {
3935 result = lfs_component_set(fname, comp_id,
3937 lsa.lsa_comp_neg_flags);
3938 } else if (comp_del != 0) {
3939 result = lfs_component_del(fname, comp_id,
3941 lsa.lsa_comp_neg_flags);
3942 } else if (comp_add != 0) {
3943 result = lfs_component_add(fname, layout);
3944 } else if (opc == SO_MIRROR_CREATE) {
3945 result = mirror_create(fname, mirror_list);
3946 } else if (opc == SO_MIRROR_EXTEND) {
3947 result = mirror_extend(fname, mirror_list,
3949 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
3950 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3952 "%s: no mirror specified to delete from '%s'\n",
3956 if (lsa.lsa_pool_name)
3957 mirror_flags |= MF_COMP_POOL;
3958 else if (mirror_id != 0)
3959 comp_id = mirror_id;
3961 mirror_flags |= MF_COMP_ID;
3962 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3964 has_m_file ? mirror_list->m_file :
3966 } else if (layout != NULL) {
3967 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3973 } else if (foreign_mode) {
3974 result = llapi_file_create_foreign(fname, mode, type,
3981 result = llapi_file_open_param(fname,
3990 /* Save the first error encountered. */
3997 if (mode_opt != NULL)
3998 umask(previous_umask);
4001 free(migrate_mdt_param.fp_lmv_md);
4002 llapi_layout_free(layout);
4003 lfs_mirror_list_free(mirror_list);
4008 llapi_layout_free(layout);
4009 lfs_mirror_list_free(mirror_list);
4013 static int lfs_poollist(int argc, char **argv)
4018 return llapi_poollist(argv[1]);
4021 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4026 char *endptr = "AD";
4031 else if (str[0] == '-')
4037 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4038 long long val = strtoll(timebuf, &endptr, 0);
4043 unit *= 52; /* 52 weeks + 1 day below */
4044 case 'w': /* fallthrough */
4046 case '\0': /* days are default unit if none used */
4047 case 'd': /* fallthrough */
4048 unit = (unit + (*endptr == 'y')) * 24;
4049 case 'h': /* fallthrough */
4051 case 'm': /* fallthrough */
4053 case 's': /* fallthrough */
4055 /* don't need to multiply by 1 for seconds */
4058 "%s find: bad time string '%s': %s\n",
4059 progname, timebuf, strerror(EINVAL));
4063 if (param->fp_time_margin == 0 ||
4064 (*endptr && unit < param->fp_time_margin))
4065 param->fp_time_margin = unit;
4072 fprintf(stderr, "%s find: bad time '%s': too large\n",
4082 static int name2uid(unsigned int *id, const char *name)
4084 struct passwd *passwd;
4086 passwd = getpwnam(name);
4089 *id = passwd->pw_uid;
4094 static int name2gid(unsigned int *id, const char *name)
4096 struct group *group;
4098 group = getgrnam(name);
4101 *id = group->gr_gid;
4106 static inline int name2projid(unsigned int *id, const char *name)
4111 static int uid2name(char **name, unsigned int id)
4113 struct passwd *passwd;
4115 passwd = getpwuid(id);
4118 *name = passwd->pw_name;
4123 static inline int gid2name(char **name, unsigned int id)
4125 struct group *group;
4127 group = getgrgid(id);
4130 *name = group->gr_name;
4135 static int name2layout(__u32 *layout, char *name)
4137 char *ptr, *layout_name;
4140 for (ptr = name; ; ptr = NULL) {
4141 layout_name = strtok(ptr, ",");
4142 if (layout_name == NULL)
4144 if (strcmp(layout_name, "released") == 0)
4145 *layout |= LOV_PATTERN_F_RELEASED;
4146 else if (strcmp(layout_name, "raid0") == 0)
4147 *layout |= LOV_PATTERN_RAID0;
4148 else if (strcmp(layout_name, "mdt") == 0)
4149 *layout |= LOV_PATTERN_MDT;
4150 else if (strcmp(layout_name, "overstriping") == 0)
4151 *layout |= LOV_PATTERN_OVERSTRIPING;
4158 static int lfs_find(int argc, char **argv)
4163 struct find_param param = {
4166 .fp_time_margin = 24 * 60 * 60,
4168 struct option long_opts[] = {
4169 { .val = 'A', .name = "atime", .has_arg = required_argument },
4170 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4171 { .val = LFS_COMP_COUNT_OPT,
4172 .name = "comp-count", .has_arg = required_argument },
4173 { .val = LFS_COMP_COUNT_OPT,
4174 .name = "component-count",
4175 .has_arg = required_argument },
4176 { .val = LFS_COMP_FLAGS_OPT,
4177 .name = "comp-flags", .has_arg = required_argument },
4178 { .val = LFS_COMP_FLAGS_OPT,
4179 .name = "component-flags",
4180 .has_arg = required_argument },
4181 { .val = LFS_COMP_START_OPT,
4182 .name = "comp-start", .has_arg = required_argument },
4183 { .val = LFS_COMP_START_OPT,
4184 .name = "component-start",
4185 .has_arg = required_argument },
4186 { .val = LFS_MIRROR_STATE_OPT,
4187 .name = "mirror-state", .has_arg = required_argument },
4188 { .val = LFS_LAYOUT_FOREIGN_OPT,
4189 .name = "foreign", .has_arg = optional_argument},
4190 { .val = LFS_NEWERXY_OPT,
4191 .name = "newer", .has_arg = required_argument},
4192 { .val = LFS_NEWERXY_OPT,
4193 .name = "neweraa", .has_arg = required_argument},
4194 { .val = LFS_NEWERXY_OPT,
4195 .name = "neweram", .has_arg = required_argument},
4196 { .val = LFS_NEWERXY_OPT,
4197 .name = "newerac", .has_arg = required_argument},
4198 { .val = LFS_NEWERXY_OPT,
4199 .name = "newerma", .has_arg = required_argument},
4200 { .val = LFS_NEWERXY_OPT,
4201 .name = "newermm", .has_arg = required_argument},
4202 { .val = LFS_NEWERXY_OPT,
4203 .name = "newermc", .has_arg = required_argument},
4204 { .val = LFS_NEWERXY_OPT,
4205 .name = "newerca", .has_arg = required_argument},
4206 { .val = LFS_NEWERXY_OPT,
4207 .name = "newercm", .has_arg = required_argument},
4208 { .val = LFS_NEWERXY_OPT,
4209 .name = "newercc", .has_arg = required_argument},
4210 { .val = LFS_NEWERXY_OPT,
4211 .name = "newerat", .has_arg = required_argument},
4212 { .val = LFS_NEWERXY_OPT,
4213 .name = "newermt", .has_arg = required_argument},
4214 { .val = LFS_NEWERXY_OPT,
4215 .name = "newerct", .has_arg = required_argument},
4216 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4217 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4218 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4219 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4220 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4221 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4222 { .val = 'E', .name = "component-end",
4223 .has_arg = required_argument },
4224 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4225 { .val = LFS_LAYOUT_FOREIGN_OPT,
4226 .name = "foreign", .has_arg = optional_argument},
4227 { .val = 'g', .name = "gid", .has_arg = required_argument },
4228 { .val = 'G', .name = "group", .has_arg = required_argument },
4229 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4230 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4231 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4232 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4233 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4234 { .val = 'L', .name = "layout", .has_arg = required_argument },
4235 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4236 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4237 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4238 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4239 { .val = 'n', .name = "name", .has_arg = required_argument },
4240 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4241 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4242 { .val = 'O', .name = "obd", .has_arg = required_argument },
4243 { .val = 'O', .name = "ost", .has_arg = required_argument },
4244 /* no short option for pool yet, can be 'p' after 2.18 */
4245 { .val = LFS_POOL_OPT,
4246 .name = "pool", .has_arg = required_argument },
4247 { .val = '0', .name = "print0", .has_arg = no_argument },
4248 { .val = 'P', .name = "print", .has_arg = no_argument },
4249 { .val = LFS_PROJID_OPT,
4250 .name = "projid", .has_arg = required_argument },
4251 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4252 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4253 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4254 { .val = 's', .name = "size", .has_arg = required_argument },
4255 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4256 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4257 { .val = 't', .name = "type", .has_arg = required_argument },
4258 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4259 { .val = 'u', .name = "uid", .has_arg = required_argument },
4260 { .val = 'U', .name = "user", .has_arg = required_argument },
4261 { .val = 'z', .name = "extension-size",
4262 .has_arg = required_argument },
4263 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4264 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4265 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4279 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4280 while ((c = getopt_long_only(argc, argv,
4281 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4282 long_opts, &optidx)) >= 0) {
4287 /* '!' is part of option */
4288 /* when getopt_long_only() finds a string which is not
4289 * an option nor a known option argument it returns 1
4290 * in that case if we already have found pathstart and pathend
4291 * (i.e. we have the list of pathnames),
4292 * the only supported value is "!"
4294 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4295 if (!isoption && pathend != -1) {
4296 fprintf(stderr, "err: %s: filename|dirname must either "
4297 "precede options or follow options\n",
4302 if (!isoption && pathstart == -1)
4303 pathstart = optind - 1;
4304 if (isoption && pathstart != -1 && pathend == -1)
4305 pathend = optind - 2;
4311 /* unknown; opt is "!" or path component,
4312 * checking done above.
4314 if (strcmp(optarg, "!") == 0)
4318 xtime = ¶m.fp_atime;
4319 xsign = ¶m.fp_asign;
4320 param.fp_exclude_atime = !!neg_opt;
4321 /* no break, this falls through to 'C' for ctime */
4324 xtime = ¶m.fp_ctime;
4325 xsign = ¶m.fp_csign;
4326 param.fp_exclude_ctime = !!neg_opt;
4328 /* no break, this falls through to 'M' for mtime */
4331 xtime = ¶m.fp_mtime;
4332 xsign = ¶m.fp_msign;
4333 param.fp_exclude_mtime = !!neg_opt;
4335 rc = set_time(¶m, &t, xtime, optarg);
4336 if (rc == LONG_MAX) {
4344 if (optarg[0] == '+') {
4345 param.fp_blocks_sign = -1;
4347 } else if (optarg[0] == '-') {
4348 param.fp_blocks_sign = 1;
4352 param.fp_blocks_units = 1024;
4353 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4354 ¶m.fp_blocks_units, 0);
4356 fprintf(stderr, "error: bad blocks '%s'\n",
4360 param.fp_check_blocks = 1;
4361 param.fp_exclude_blocks = !!neg_opt;
4363 case LFS_COMP_COUNT_OPT:
4364 if (optarg[0] == '+') {
4365 param.fp_comp_count_sign = -1;
4367 } else if (optarg[0] == '-') {
4368 param.fp_comp_count_sign = 1;
4372 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4373 if (*endptr != '\0') {
4374 fprintf(stderr, "error: bad component count "
4378 param.fp_check_comp_count = 1;
4379 param.fp_exclude_comp_count = !!neg_opt;
4381 case LFS_COMP_FLAGS_OPT:
4382 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4383 ¶m.fp_comp_neg_flags);
4385 fprintf(stderr, "error: bad component flags "
4389 param.fp_check_comp_flags = 1;
4391 __u32 flags = param.fp_comp_neg_flags;
4392 param.fp_comp_neg_flags = param.fp_comp_flags;
4393 param.fp_comp_flags = flags;
4396 case LFS_COMP_START_OPT:
4397 if (optarg[0] == '+') {
4398 param.fp_comp_start_sign = -1;
4400 } else if (optarg[0] == '-') {
4401 param.fp_comp_start_sign = 1;
4405 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4406 ¶m.fp_comp_start_units, 0);
4408 fprintf(stderr, "error: bad component start "
4412 param.fp_check_comp_start = 1;
4413 param.fp_exclude_comp_start = !!neg_opt;
4415 case LFS_MIRROR_STATE_OPT:
4416 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4417 ¶m.fp_mirror_neg_state);
4420 "error: bad mirrored file state '%s'\n",
4424 param.fp_check_mirror_state = 1;
4426 __u16 state = param.fp_mirror_neg_state;
4427 param.fp_mirror_neg_state =
4428 param.fp_mirror_state;
4429 param.fp_mirror_state = state;
4433 if (optarg[0] == '+') {
4434 param.fp_stripe_count_sign = -1;
4436 } else if (optarg[0] == '-') {
4437 param.fp_stripe_count_sign = 1;
4441 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4442 if (*endptr != '\0') {
4443 fprintf(stderr,"error: bad stripe_count '%s'\n",
4448 param.fp_check_stripe_count = 1;
4449 param.fp_exclude_stripe_count = !!neg_opt;
4452 param.fp_max_depth = strtol(optarg, 0, 0);
4455 if (optarg[0] == '+') {
4456 param.fp_comp_end_sign = -1;
4458 } else if (optarg[0] == '-') {
4459 param.fp_comp_end_sign = 1;
4463 if (arg_is_eof(optarg)) {
4464 param.fp_comp_end = LUSTRE_EOF;
4465 param.fp_comp_end_units = 1;
4468 rc = llapi_parse_size(optarg,
4470 ¶m.fp_comp_end_units, 0);
4473 fprintf(stderr, "error: bad component end "
4477 param.fp_check_comp_end = 1;
4478 param.fp_exclude_comp_end = !!neg_opt;
4480 case LFS_LAYOUT_FOREIGN_OPT: {
4481 /* all types by default */
4482 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4484 if (optarg != NULL) {
4485 /* check pure numeric */
4486 type = strtoul(optarg, &endptr, 0);
4489 type = check_foreign_type_name(optarg);
4490 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4492 "%s %s: unknown foreign type '%s'\n",
4499 param.fp_foreign_type = type;
4500 param.fp_check_foreign = 1;
4501 param.fp_exclude_foreign = !!neg_opt;
4504 case LFS_NEWERXY_OPT: {
4510 time_t ref = time(NULL);
4512 /* no need to check bad options, they won't get here */
4513 if (strlen(long_opts[optidx].name) == 7) {
4514 x = long_opts[optidx].name[5];
4515 y = long_opts[optidx].name[6];
4519 static const char *const fmts[] = {
4520 "%Y-%m-%d %H:%M:%S",
4523 "%H:%M:%S", /* sometime today */
4532 for (i = 0; fmts[i] != NULL; i++) {
4535 /* Init for times relative to today */
4536 if (strncmp(fmts[i], "%H", 2) == 0)
4537 localtime_r(&ref, &tm);
4539 memset(&tm, 0, sizeof(tm));
4540 ptr = strptime(optarg, fmts[i], &tm);
4542 while (ptr && isspace(*ptr))
4544 if (ptr == optarg + strlen(optarg)) {
4552 "%s: invalid time '%s'\n",
4555 "supported formats are:\n ");
4556 for (i = 0; fmts[i] != NULL; i++)
4557 fprintf(stderr, "'%s', ",
4559 fprintf(stderr, "\n");
4566 struct stat statbuf;
4568 if (stat(optarg, &statbuf) < 0) {
4570 "%s: cannot stat file '%s': %s\n",
4579 ref = statbuf.st_atime;
4582 ref = statbuf.st_mtime;
4585 ref = statbuf.st_ctime;
4589 "%s: invalid Y argument: '%c'\n",
4598 xidx = NEWERXY_ATIME;
4601 xidx = NEWERXY_MTIME;
4604 xidx = NEWERXY_CTIME;
4608 "%s: invalid X argument: '%c'\n",
4615 newery = ¶m.fp_newery[xidx][negidx];
4621 *newery = *newery > ref ? ref : *newery;
4623 *newery = *newery > ref ? *newery : ref;
4625 param.fp_newerxy = 1;
4630 rc = name2gid(¶m.fp_gid, optarg);
4632 param.fp_gid = strtoul(optarg, &endptr, 10);
4633 if (*endptr != '\0') {
4634 fprintf(stderr, "Group/GID: %s cannot "
4635 "be found.\n", optarg);
4640 param.fp_exclude_gid = !!neg_opt;
4641 param.fp_check_gid = 1;
4644 param.fp_hash_type = check_hashtype(optarg);
4645 if (param.fp_hash_type == 0) {
4646 fprintf(stderr, "error: bad hash_type '%s'\n",
4651 param.fp_check_hash_type = 1;
4652 param.fp_exclude_hash_type = !!neg_opt;
4658 ret = name2layout(¶m.fp_layout, optarg);
4661 param.fp_exclude_layout = !!neg_opt;
4662 param.fp_check_layout = 1;
4666 rc = name2uid(¶m.fp_uid, optarg);
4668 param.fp_uid = strtoul(optarg, &endptr, 10);
4669 if (*endptr != '\0') {
4670 fprintf(stderr, "User/UID: %s cannot "
4671 "be found.\n", optarg);
4676 param.fp_exclude_uid = !!neg_opt;
4677 param.fp_check_uid = 1;
4680 param.fp_pattern = (char *)optarg;
4681 param.fp_exclude_pattern = !!neg_opt;
4684 if (optarg[0] == '+') {
4685 param.fp_mirror_count_sign = -1;
4687 } else if (optarg[0] == '-') {
4688 param.fp_mirror_count_sign = 1;
4692 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4693 if (*endptr != '\0') {
4695 "error: bad mirror count '%s'\n",
4699 param.fp_check_mirror_count = 1;
4700 param.fp_exclude_mirror_count = !!neg_opt;
4705 char *buf, *token, *next, *p;
4709 buf = strdup(optarg);
4715 param.fp_exclude_obd = !!neg_opt;
4718 while (token && *token) {
4719 token = strchr(token, ',');
4726 param.fp_exclude_mdt = !!neg_opt;
4727 param.fp_num_alloc_mdts += len;
4728 tmp = realloc(param.fp_mdt_uuid,
4729 param.fp_num_alloc_mdts *
4730 sizeof(*param.fp_mdt_uuid));
4736 param.fp_mdt_uuid = tmp;
4738 param.fp_exclude_obd = !!neg_opt;
4739 param.fp_num_alloc_obds += len;
4740 tmp = realloc(param.fp_obd_uuid,
4741 param.fp_num_alloc_obds *
4742 sizeof(*param.fp_obd_uuid));
4748 param.fp_obd_uuid = tmp;
4750 for (token = buf; token && *token; token = next) {
4751 struct obd_uuid *puuid;
4754 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4757 ¶m.fp_obd_uuid[param.fp_num_obds++];
4759 p = strchr(token, ',');
4766 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4771 strncpy(puuid->uuid, token,
4772 sizeof(puuid->uuid));
4779 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4783 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4785 "Pool name %s is too long (max %d)\n",
4786 optarg, LOV_MAXPOOLNAME);
4791 * We do check for empty pool because empty pool
4792 * is used to find V1 LOV attributes
4794 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4795 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4796 param.fp_exclude_pool = !!neg_opt;
4797 param.fp_check_pool = 1;
4799 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4800 case 'p': /* want this for --pool, to match getstripe/find */
4802 "warning: -p deprecated, use --print0 or -0\n");
4805 param.fp_zero_end = 1;
4807 case 'P': /* we always print, this option is a no-op */
4809 case LFS_PROJID_OPT:
4810 rc = name2projid(¶m.fp_projid, optarg);
4812 param.fp_projid = strtoul(optarg, &endptr, 10);
4813 if (*endptr != '\0') {
4815 "Invalid project ID: %s",
4821 param.fp_exclude_projid = !!neg_opt;
4822 param.fp_check_projid = 1;
4825 if (optarg[0] == '+') {
4826 param.fp_size_sign = -1;
4828 } else if (optarg[0] == '-') {
4829 param.fp_size_sign = 1;
4833 ret = llapi_parse_size(optarg, ¶m.fp_size,
4834 ¶m.fp_size_units, 0);
4836 fprintf(stderr, "error: bad file size '%s'\n",
4840 param.fp_check_size = 1;
4841 param.fp_exclude_size = !!neg_opt;
4844 if (optarg[0] == '+') {
4845 param.fp_stripe_size_sign = -1;
4847 } else if (optarg[0] == '-') {
4848 param.fp_stripe_size_sign = 1;
4852 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4853 ¶m.fp_stripe_size_units, 0);
4855 fprintf(stderr, "error: bad stripe_size '%s'\n",
4859 param.fp_check_stripe_size = 1;
4860 param.fp_exclude_stripe_size = !!neg_opt;
4863 param.fp_exclude_type = !!neg_opt;
4864 switch (optarg[0]) {
4866 param.fp_type = S_IFBLK;
4869 param.fp_type = S_IFCHR;
4872 param.fp_type = S_IFDIR;
4875 param.fp_type = S_IFREG;
4878 param.fp_type = S_IFLNK;
4881 param.fp_type = S_IFIFO;
4884 param.fp_type = S_IFSOCK;
4887 fprintf(stderr, "error: %s: bad type '%s'\n",
4894 if (optarg[0] == '+') {
4895 param.fp_mdt_count_sign = -1;
4897 } else if (optarg[0] == '-') {
4898 param.fp_mdt_count_sign = 1;
4902 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4903 if (*endptr != '\0') {
4904 fprintf(stderr, "error: bad mdt_count '%s'\n",
4909 param.fp_check_mdt_count = 1;
4910 param.fp_exclude_mdt_count = !!neg_opt;
4913 if (optarg[0] == '+') {
4914 param.fp_ext_size_sign = -1;
4916 } else if (optarg[0] == '-') {
4917 param.fp_ext_size_sign = 1;
4921 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4922 ¶m.fp_ext_size_units, 0);
4924 fprintf(stderr, "error: bad ext-size '%s'\n",
4928 param.fp_ext_size /= SEL_UNIT_SIZE;
4929 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4930 param.fp_check_ext_size = 1;
4931 param.fp_exclude_ext_size = !!neg_opt;
4939 if (pathstart == -1) {
4940 fprintf(stderr, "error: %s: no filename|pathname\n",
4944 } else if (pathend == -1) {
4950 rc = llapi_find(argv[pathstart], ¶m);
4953 pathbad = pathstart;
4955 } while (++pathstart < pathend);
4958 fprintf(stderr, "%s: failed for '%s': %s\n",
4959 progname, argv[pathbad], strerror(-rc));
4962 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4963 free(param.fp_obd_uuid);
4965 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4966 free(param.fp_mdt_uuid);
4971 static int lfs_getstripe_internal(int argc, char **argv,
4972 struct find_param *param)
4974 struct option long_opts[] = {
4975 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4976 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4977 { .val = LFS_COMP_COUNT_OPT,
4978 .name = "comp-count", .has_arg = no_argument },
4979 { .val = LFS_COMP_COUNT_OPT,
4980 .name = "component-count", .has_arg = no_argument },
4981 { .val = LFS_COMP_FLAGS_OPT,
4982 .name = "comp-flags", .has_arg = optional_argument },
4983 { .val = LFS_COMP_FLAGS_OPT,
4984 .name = "component-flags", .has_arg = optional_argument },
4985 { .val = LFS_COMP_START_OPT,
4986 .name = "comp-start", .has_arg = optional_argument },
4987 { .val = LFS_COMP_START_OPT,
4988 .name = "component-start", .has_arg = optional_argument },
4989 { .val = LFS_MIRROR_INDEX_OPT,
4990 .name = "mirror-index", .has_arg = required_argument },
4991 { .val = LFS_MIRROR_ID_OPT,
4992 .name = "mirror-id", .has_arg = required_argument },
4993 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4994 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4995 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4996 { .val = 'd', .name = "directory", .has_arg = no_argument },
4997 { .val = 'D', .name = "default", .has_arg = no_argument },
4998 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4999 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5000 { .val = 'F', .name = "fid", .has_arg = no_argument },
5001 { .val = 'g', .name = "generation", .has_arg = no_argument },
5002 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5003 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5004 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5005 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5006 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5007 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5008 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5009 { .val = 'L', .name = "layout", .has_arg = no_argument },
5010 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5011 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5012 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5013 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5014 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5015 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5016 { .val = 'O', .name = "obd", .has_arg = required_argument },
5017 { .val = 'O', .name = "ost", .has_arg = required_argument },
5018 { .val = 'p', .name = "pool", .has_arg = no_argument },
5019 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5020 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5021 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5022 { .val = 'R', .name = "raw", .has_arg = no_argument },
5023 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5024 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5025 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5026 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5027 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5028 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5029 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5030 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5031 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5032 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5036 int pathstart = -1, pathend = -1;
5040 while ((c = getopt_long(argc, argv,
5041 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5042 long_opts, NULL)) != -1) {
5046 /* '!' is part of option */
5047 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5048 if (!isoption && pathend != -1) {
5050 "error: %s: filename|dirname must either precede options or follow options\n",
5054 if (!isoption && pathstart == -1)
5055 pathstart = optind - 1;
5056 if (isoption && pathstart != -1 && pathend == -1)
5057 pathend = optind - 2;
5061 /* unknown: opt is "!" */
5062 if (strcmp(optarg, "!") == 0)
5066 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5067 param->fp_verbose |= VERBOSE_COUNT;
5068 param->fp_max_depth = 0;
5071 case LFS_COMP_COUNT_OPT:
5072 param->fp_verbose |= VERBOSE_COMP_COUNT;
5073 param->fp_max_depth = 0;
5075 case LFS_COMP_FLAGS_OPT:
5076 if (optarg != NULL) {
5077 rc = comp_str2flags(optarg,
5078 ¶m->fp_comp_flags,
5079 ¶m->fp_comp_neg_flags);
5081 fprintf(stderr, "error: %s bad "
5082 "component flags '%s'.\n",
5086 param->fp_check_comp_flags = 1;
5088 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5089 param->fp_max_depth = 0;
5092 case LFS_COMP_START_OPT:
5093 if (optarg != NULL) {
5095 if (tmp[0] == '+') {
5096 param->fp_comp_start_sign = -1;
5098 } else if (tmp[0] == '-') {
5099 param->fp_comp_start_sign = 1;
5102 rc = llapi_parse_size(tmp,
5103 ¶m->fp_comp_start,
5104 ¶m->fp_comp_start_units, 0);
5106 fprintf(stderr, "error: %s bad "
5107 "component start '%s'.\n",
5111 param->fp_check_comp_start = 1;
5114 param->fp_verbose |= VERBOSE_COMP_START;
5115 param->fp_max_depth = 0;
5118 case LFS_MIRROR_INDEX_OPT:
5119 if (optarg[0] == '+') {
5120 param->fp_mirror_index_sign = -1;
5122 } else if (optarg[0] == '-') {
5123 param->fp_mirror_index_sign = 1;
5127 param->fp_mirror_index = strtoul(optarg, &end, 0);
5128 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5129 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5131 "%s %s: invalid mirror index '%s'\n",
5132 progname, argv[0], optarg);
5135 if (param->fp_mirror_id != 0) {
5137 "%s %s: can't specify both mirror index and mirror ID\n",
5141 param->fp_check_mirror_index = 1;
5142 param->fp_exclude_mirror_index = !!neg_opt;
5144 case LFS_MIRROR_ID_OPT:
5145 if (optarg[0] == '+') {
5146 param->fp_mirror_id_sign = -1;
5148 } else if (optarg[0] == '-') {
5149 param->fp_mirror_id_sign = 1;
5153 param->fp_mirror_id = strtoul(optarg, &end, 0);
5154 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5155 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5157 "%s %s: invalid mirror ID '%s'\n",
5158 progname, argv[0], optarg);
5161 if (param->fp_mirror_index != 0) {
5163 "%s %s: can't specify both mirror index and mirror ID\n",
5167 param->fp_check_mirror_id = 1;
5168 param->fp_exclude_mirror_id = !!neg_opt;
5171 param->fp_max_depth = 0;
5174 param->fp_get_default_lmv = 1;
5177 if (optarg != NULL) {
5179 if (tmp[0] == '+') {
5180 param->fp_comp_end_sign = -1;
5182 } else if (tmp[0] == '-') {
5183 param->fp_comp_end_sign = 1;
5187 if (arg_is_eof(tmp)) {
5188 param->fp_comp_end = LUSTRE_EOF;
5189 param->fp_comp_end_units = 1;
5192 rc = llapi_parse_size(tmp,
5193 ¶m->fp_comp_end,
5194 ¶m->fp_comp_end_units, 0);
5197 fprintf(stderr, "error: %s bad "
5198 "component end '%s'.\n",
5202 param->fp_check_comp_end = 1;
5204 param->fp_verbose |= VERBOSE_COMP_END;
5205 param->fp_max_depth = 0;
5209 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5210 param->fp_verbose |= VERBOSE_DFID;
5211 param->fp_max_depth = 0;
5215 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5216 param->fp_verbose |= VERBOSE_GENERATION;
5217 param->fp_max_depth = 0;
5221 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5222 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5223 param->fp_max_depth = 0;
5227 if (optarg != NULL) {
5228 param->fp_comp_id = strtoul(optarg, &end, 0);
5229 if (*end != '\0' || param->fp_comp_id == 0 ||
5230 param->fp_comp_id > LCME_ID_MAX) {
5231 fprintf(stderr, "error: %s bad "
5232 "component id '%s'\n",
5236 param->fp_check_comp_id = 1;
5239 param->fp_max_depth = 0;
5240 param->fp_verbose |= VERBOSE_COMP_ID;
5244 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5245 param->fp_verbose |= VERBOSE_PATTERN;
5246 param->fp_max_depth = 0;
5249 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5251 fprintf(stderr, "warning: '-M' deprecated"
5252 ", use '--mdt-index' or '-m' instead\n");
5255 if (!(param->fp_verbose & VERBOSE_DETAIL))
5256 param->fp_max_depth = 0;
5257 param->fp_verbose |= VERBOSE_MDTINDEX;
5260 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5261 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5262 param->fp_max_depth = 0;
5266 if (param->fp_obd_uuid) {
5268 "error: %s: only one obduuid allowed",
5272 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5275 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5276 param->fp_verbose |= VERBOSE_POOL;
5277 param->fp_max_depth = 0;
5284 param->fp_recursive = 1;
5290 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5291 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5292 param->fp_max_depth = 0;
5296 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5302 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5303 param->fp_verbose |= VERBOSE_EXT_SIZE;
5304 param->fp_max_depth = 0;
5312 if (pathstart == -1) {
5313 fprintf(stderr, "error: %s: no filename|pathname\n",
5316 } else if (pathend == -1) {
5324 if (param->fp_recursive)
5325 param->fp_max_depth = -1;
5326 else if (param->fp_verbose & VERBOSE_DETAIL)
5327 param->fp_max_depth = 1;
5329 if (!param->fp_verbose)
5330 param->fp_verbose = VERBOSE_DEFAULT;
5331 if (param->fp_quiet)
5332 param->fp_verbose = VERBOSE_OBJID;
5335 rc = llapi_getstripe(argv[pathstart], param);
5336 } while (++pathstart < pathend && !rc);
5339 fprintf(stderr, "error: %s failed for %s.\n",
5340 argv[0], argv[optind - 1]);
5344 static int lfs_tgts(int argc, char **argv)
5346 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5347 struct find_param param;
5348 int index = 0, rc=0;
5353 if (argc == 2 && !realpath(argv[1], path)) {
5355 fprintf(stderr, "error: invalid path '%s': %s\n",
5356 argv[1], strerror(-rc));
5360 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5361 /* Check if we have a mount point */
5362 if (mntdir[0] == '\0')
5365 memset(¶m, 0, sizeof(param));
5366 if (!strcmp(argv[0], "mdts"))
5367 param.fp_get_lmv = 1;
5369 rc = llapi_ostlist(mntdir, ¶m);
5371 fprintf(stderr, "error: %s: failed on %s\n",
5374 if (path[0] != '\0')
5376 memset(mntdir, 0, PATH_MAX);
5382 static int lfs_getstripe(int argc, char **argv)
5384 struct find_param param = { 0 };
5386 param.fp_max_depth = 1;
5387 return lfs_getstripe_internal(argc, argv, ¶m);
5391 static int lfs_getdirstripe(int argc, char **argv)
5393 struct find_param param = { 0 };
5394 struct option long_opts[] = {
5395 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5396 { .val = 'D', .name = "default", .has_arg = no_argument },
5397 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5398 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5399 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5400 { .val = 'O', .name = "obd", .has_arg = required_argument },
5401 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5402 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5403 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5404 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5408 param.fp_get_lmv = 1;
5410 while ((c = getopt_long(argc, argv,
5411 "cDHimO:rtTvy", long_opts, NULL)) != -1)
5416 param.fp_verbose |= VERBOSE_COUNT;
5419 param.fp_get_default_lmv = 1;
5423 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5425 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5427 fprintf(stderr, "warning: '-t' deprecated, "
5428 "use '--mdt-hash' or '-H' instead\n");
5431 param.fp_verbose |= VERBOSE_HASH_TYPE;
5434 if (param.fp_obd_uuid) {
5436 "error: %s: only one obduuid allowed",
5440 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5443 param.fp_recursive = 1;
5446 param.fp_verbose |= VERBOSE_DETAIL;
5452 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5453 progname, argv[0], argv[optind - 1]);
5461 if (param.fp_recursive)
5462 param.fp_max_depth = -1;
5464 if (!param.fp_verbose)
5465 param.fp_verbose = VERBOSE_DEFAULT;
5468 rc = llapi_getstripe(argv[optind], ¶m);
5469 } while (++optind < argc && !rc);
5472 fprintf(stderr, "error: %s failed for %s.\n",
5473 argv[0], argv[optind - 1]);
5478 MNTDF_INODES = 0x0001,
5479 MNTDF_COOKED = 0x0002,
5480 MNTDF_LAZY = 0x0004,
5481 MNTDF_VERBOSE = 0x0008,
5482 MNTDF_SHOW = 0x0010,
5485 #define COOK(value) \
5488 while (value > 1024) { \
5496 #define CDF "%11llu"
5497 #define HDF "%8.1f%c"
5501 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5503 double avail, used, ratio = 0;
5506 avail = st->os_ffree;
5507 used = st->os_files - st->os_ffree;
5509 avail = st->os_bavail;
5510 used = st->os_blocks - st->os_bfree;
5512 if (avail + used > 0)
5513 ratio = used / (used + avail) * 100;
5515 /* Round up to match df(1) usage percentage */
5516 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5519 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5520 * so only show flags reflecting those states by default. Informational states
5521 * are only shown with "-v" and use lower-case names to distinguish them.
5522 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5524 static struct obd_statfs_state_names {
5525 enum obd_statfs_state osn_state;
5526 const char osn_name;
5529 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
5530 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
5531 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
5532 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
5533 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
5534 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
5535 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
5536 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
5537 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
5540 static int showdf(char *mntdir, struct obd_statfs *stat,
5541 char *uuid, enum mntdf_flags flags,
5542 char *type, int index, int rc)
5544 long long avail, used, total;
5546 char *suffix = "KMGTPEZY";
5547 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5548 char tbuf[3 * sizeof(__u64)];
5549 char ubuf[3 * sizeof(__u64)];
5550 char abuf[3 * sizeof(__u64)];
5551 char rbuf[3 * sizeof(__u64)];
5558 if (flags & MNTDF_INODES) {
5559 avail = stat->os_ffree;
5560 used = stat->os_files - stat->os_ffree;
5561 total = stat->os_files;
5563 int shift = flags & MNTDF_COOKED ? 0 : 10;
5565 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5566 used = ((stat->os_blocks - stat->os_bfree) *
5567 stat->os_bsize) >> shift;
5568 total = (stat->os_blocks * stat->os_bsize) >> shift;
5571 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5573 if (flags & MNTDF_COOKED) {
5577 cook_val = (double)total;
5580 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5583 snprintf(tbuf, sizeof(tbuf), CDF, total);
5585 cook_val = (double)used;
5588 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5591 snprintf(ubuf, sizeof(ubuf), CDF, used);
5593 cook_val = (double)avail;
5596 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5599 snprintf(abuf, sizeof(abuf), CDF, avail);
5601 snprintf(tbuf, sizeof(tbuf), CDF, total);
5602 snprintf(ubuf, sizeof(tbuf), CDF, used);
5603 snprintf(abuf, sizeof(tbuf), CDF, avail);
5606 sprintf(rbuf, RDF, ratio);
5607 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5608 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5610 printf("[%s:%d]", type, index);
5612 if (stat->os_state) {
5616 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5617 if (oss_names[i].osn_state & stat->os_state &&
5618 (oss_names[i].osn_err ||
5619 flags & MNTDF_VERBOSE))
5620 printf("%c", oss_names[i].osn_name);
5627 printf(UUF": inactive device\n", uuid);
5630 printf(UUF": %s\n", uuid, strerror(-rc));
5637 struct ll_stat_type {
5642 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5644 struct ll_statfs_data {
5646 struct obd_statfs sd_st;
5649 struct ll_statfs_buf {
5651 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5654 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5655 int ops, struct ll_statfs_buf *lsb)
5657 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5658 struct obd_uuid uuid_buf;
5659 char *poolname = NULL;
5660 struct ll_stat_type types[] = {
5661 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5662 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5663 { .st_name = NULL } };
5664 struct ll_stat_type *tp;
5665 __u64 ost_files = 0;
5666 __u64 ost_ffree = 0;
5674 poolname = strchr(pool, '.');
5675 if (poolname != NULL) {
5676 if (strncmp(fsname, pool, strlen(fsname))) {
5677 fprintf(stderr, "filesystem name incorrect\n");
5685 fd = open(mntdir, O_RDONLY);
5688 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5693 if (flags & MNTDF_SHOW) {
5694 if (flags & MNTDF_INODES)
5695 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5696 "UUID", "Inodes", "IUsed", "IFree",
5697 "IUse%", "Mounted on");
5699 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5701 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5702 "Used", "Available", "Use%", "Mounted on");
5705 for (tp = types; tp->st_name != NULL; tp++) {
5706 bool have_ost = false;
5708 if (!(tp->st_op & ops))
5711 for (index = 0; ; index++) {
5712 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5713 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5714 type = flags & MNTDF_LAZY ?
5715 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5716 rc2 = llapi_obd_fstatfs(fd, type, index,
5717 &stat_buf, &uuid_buf);
5722 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5723 if (!(flags & MNTDF_VERBOSE))
5725 } else if (rc2 < 0 && rc == 0) {
5729 /* If we have OSTs then don't report MDT block counts.
5730 * For MDT-only filesystems the expectation is that all
5731 * layouts have a DoM component. For filesystems with
5732 * OSTs, files are not necessarily going to store data
5733 * on MDTs, and MDT space is limited to a fraction of
5734 * OST space, so don't include it in the summary.
5736 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5743 if (poolname && tp->st_op == LL_STATFS_LOV &&
5744 llapi_search_ost(fsname, poolname,
5745 obd_uuid2str(&uuid_buf)) != 1)
5748 /* the llapi_obd_statfs() call may have returned with
5749 * an error, but if it filled in uuid_buf we will at
5750 * lease use that to print out a message for that OBD.
5751 * If we didn't get anything in the uuid_buf, then fill
5752 * it in so that we can print an error message. */
5753 if (uuid_buf.uuid[0] == '\0')
5754 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5755 "%s%04x", tp->st_name, index);
5757 lsb->sb_buf[lsb->sb_count].sd_index = index;
5758 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5761 if (flags & MNTDF_SHOW)
5762 showdf(mntdir, &stat_buf,
5763 obd_uuid2str(&uuid_buf), flags,
5764 tp->st_name, index, rc2);
5769 if (tp->st_op == LL_STATFS_LMV) {
5770 sum.os_ffree += stat_buf.os_ffree;
5771 sum.os_files += stat_buf.os_files;
5772 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5773 ost_files += stat_buf.os_files;
5774 ost_ffree += stat_buf.os_ffree;
5776 sum.os_blocks += stat_buf.os_blocks *
5778 sum.os_bfree += stat_buf.os_bfree *
5780 sum.os_bavail += stat_buf.os_bavail *
5787 /* If we have _some_ OSTs, but don't have as many free objects on the
5788 * OST as inodes on the MDTs, reduce the reported number of inodes
5789 * to compensate, so that the "inodes in use" number is correct.
5790 * This should be kept in sync with ll_statfs_internal().
5792 if (ost_files && ost_ffree < sum.os_ffree) {
5793 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5794 sum.os_ffree = ost_ffree;
5796 if (flags & MNTDF_SHOW) {
5798 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5806 static int lfs_setdirstripe(int argc, char **argv)
5809 struct lfs_setstripe_args lsa = { 0 };
5810 struct llapi_stripe_param *param = NULL;
5811 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5814 char *mode_opt = NULL;
5815 bool default_stripe = false;
5816 bool delete = false;
5817 bool foreign_mode = false;
5818 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5819 mode_t previous_mode = 0;
5821 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5822 struct option long_opts[] = {
5823 { .val = 'c', .name = "count", .has_arg = required_argument },
5824 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5825 { .val = 'd', .name = "delete", .has_arg = no_argument },
5826 { .val = 'D', .name = "default", .has_arg = no_argument },
5827 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5828 { .val = LFS_LAYOUT_FLAGS_OPT,
5829 .name = "flags", .has_arg = required_argument },
5830 { .val = LFS_LAYOUT_FOREIGN_OPT,
5831 .name = "foreign", .has_arg = optional_argument},
5832 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5833 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5834 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5835 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5837 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5838 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5839 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5841 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5842 { .val = 'i', .name = "index", .has_arg = required_argument },
5844 { .val = 'o', .name = "mode", .has_arg = required_argument },
5845 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5846 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5848 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5849 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5850 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5854 setstripe_args_init(&lsa);
5856 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5857 long_opts, NULL)) >= 0) {
5864 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5867 "%s %s: invalid stripe count '%s'\n",
5868 progname, argv[0], optarg);
5874 default_stripe = true;
5877 default_stripe = true;
5879 case LFS_LAYOUT_FOREIGN_OPT:
5880 if (optarg != NULL) {
5881 /* check pure numeric */
5882 type = strtoul(optarg, &end, 0);
5885 type = check_foreign_type_name(optarg);
5886 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5888 "%s %s: unknown foreign type '%s'\n",
5895 foreign_mode = true;
5897 case LFS_LAYOUT_FLAGS_OPT:
5898 flags = strtoul(optarg, &end, 16);
5901 "%s %s: bad flags '%s'\n",
5902 progname, argv[0], optarg);
5906 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5908 fprintf(stderr, "warning: '--hash-type' and '-t' "
5909 "deprecated, use '--mdt-hash' or '-H' instead\n");
5912 lsa.lsa_pattern = check_hashtype(optarg);
5913 if (lsa.lsa_pattern == 0) {
5915 "%s %s: bad stripe hash type '%s'\n",
5916 progname, argv[0], optarg);
5921 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5924 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5925 if (strcmp(argv[optind - 1], "--index") == 0)
5927 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5930 lsa.lsa_nr_tgts = parse_targets(mdts,
5931 sizeof(mdts) / sizeof(__u32),
5932 lsa.lsa_nr_tgts, optarg, NULL);
5933 if (lsa.lsa_nr_tgts < 0) {
5935 "%s %s: invalid MDT target(s) '%s'\n",
5936 progname, argv[0], optarg);
5940 lsa.lsa_tgts = mdts;
5941 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5942 lsa.lsa_stripe_off = mdts[0];
5944 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5946 fprintf(stderr, "warning: '-m' is deprecated, "
5947 "use '--mode' or '-o' instead\n");
5956 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5957 progname, argv[0], argv[optind - 1]);
5962 if (optind == argc) {
5963 fprintf(stderr, "%s %s: DIR must be specified\n",
5968 if (xattr && !foreign_mode) {
5969 /* only print a warning as this is armless and will be
5973 "%s %s: xattr has been specified for non-foreign layout\n",
5975 } else if (foreign_mode && !xattr) {
5977 "%s %s: xattr must be provided in foreign mode\n",
5982 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5983 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5985 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5990 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5991 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5993 "%s %s: stripe offset and count must be specified\n",
5999 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6000 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6002 "%s %s: cannot specify -d with -c or -i options\n",
6007 if (mode_opt != NULL) {
6008 mode = strtoul(mode_opt, &end, 8);
6011 "%s %s: bad MODE '%s'\n",
6012 progname, argv[0], mode_opt);
6015 previous_mode = umask(0);
6018 /* foreign LMV/dir case */
6020 if (argc > optind + 1) {
6022 "%s %s: cannot specify multiple foreign dirs\n",
6027 dname = argv[optind];
6028 result = llapi_dir_create_foreign(dname, mode, type, flags,
6032 "%s mkdir: can't create foreign dir '%s': %s\n",
6033 progname, dname, strerror(-result));
6038 * initialize stripe parameters, in case param is converted to specific,
6039 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6041 param = calloc(1, offsetof(typeof(*param),
6042 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6043 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6044 if (param == NULL) {
6046 "%s %s: cannot allocate memory for parameters: %s\n",
6047 progname, argv[0], strerror(ENOMEM));
6051 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6052 if (default_stripe && lsa.lsa_stripe_off == -1 &&
6053 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6054 lsa.lsa_stripe_count == 0))
6055 lsa.lsa_stripe_count = 1;
6056 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6057 param->lsp_stripe_count = lsa.lsa_stripe_count;
6058 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6059 param->lsp_stripe_offset = -1;
6061 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6062 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6063 param->lsp_stripe_pattern = lsa.lsa_pattern;
6065 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6066 param->lsp_pool = lsa.lsa_pool_name;
6067 param->lsp_is_specific = false;
6068 if (lsa.lsa_nr_tgts > 1) {
6069 if (lsa.lsa_stripe_count > 0 &&
6070 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6071 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6073 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6074 argv[0], lsa.lsa_stripe_count,
6080 param->lsp_is_specific = true;
6081 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6082 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6085 dname = argv[optind];
6087 if (default_stripe) {
6088 result = llapi_dir_set_default_lmv(dname, param);
6091 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6092 progname, dname, strerror(-result));
6096 result = llapi_dir_create(dname, mode, param);
6099 "%s setdirstripe: cannot create dir '%s': %s\n",
6100 progname, dname, strerror(-result));
6101 } while (!result && (dname = argv[++optind]));
6103 if (mode_opt != NULL)
6104 umask(previous_mode);
6111 static int lfs_rmentry(int argc, char **argv)
6118 fprintf(stderr, "error: %s: missing dirname\n",
6124 dname = argv[index];
6125 while (dname != NULL) {
6126 result = llapi_direntry_remove(dname);
6128 fprintf(stderr, "error: %s: remove dir entry '%s' "
6129 "failed\n", argv[0], dname);
6132 dname = argv[++index];
6137 static int lfs_mv(int argc, char **argv)
6139 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6140 struct find_param param = {
6147 struct option long_opts[] = {
6148 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6149 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6150 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6153 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6155 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6157 fprintf(stderr, "warning: '-M' deprecated"
6158 ", use '--mdt-index' or '-m' instead\n");
6161 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6163 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6169 param.fp_verbose = VERBOSE_DETAIL;
6172 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6173 progname, argv[optind - 1]);
6178 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6179 fprintf(stderr, "%s mv: MDT index must be specified\n",
6184 if (optind >= argc) {
6185 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6189 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
6191 /* initialize migrate mdt parameters */
6192 param.fp_lmv_md = &lmu;
6193 param.fp_migrate = 1;
6194 rc = llapi_migrate_mdt(argv[optind], ¶m);
6196 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6197 progname, argv[optind], lmu.lum_stripe_offset,
6202 static int lfs_osts(int argc, char **argv)
6204 return lfs_tgts(argc, argv);
6207 static int lfs_mdts(int argc, char **argv)
6209 return lfs_tgts(argc, argv);
6212 static int lfs_df(int argc, char **argv)
6214 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6215 enum mntdf_flags flags = MNTDF_SHOW;
6216 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6217 int c, rc = 0, index = 0;
6218 char fsname[PATH_MAX] = "", *pool_name = NULL;
6219 struct option long_opts[] = {
6220 { .val = 'h', .name = "human-readable",
6221 .has_arg = no_argument },
6222 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6223 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6224 { .val = 'p', .name = "pool", .has_arg = required_argument },
6225 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6228 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6231 flags |= MNTDF_COOKED;
6234 flags |= MNTDF_INODES;
6237 flags |= MNTDF_LAZY;
6243 flags |= MNTDF_VERBOSE;
6249 if (optind < argc && !realpath(argv[optind], path)) {
6251 fprintf(stderr, "error: invalid path '%s': %s\n",
6252 argv[optind], strerror(-rc));
6256 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6257 /* Check if we have a mount point */
6258 if (mntdir[0] == '\0')
6261 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6262 if (rc || path[0] != '\0')
6264 fsname[0] = '\0'; /* avoid matching in next loop */
6265 mntdir[0] = '\0'; /* avoid matching in next loop */
6271 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6272 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6276 if (opt_fsname == opt_instance) { /* both true or both false */
6277 rc = llapi_getname(mntdir, buf, buflen);
6278 } else if (opt_fsname) {
6279 /* llapi_search_mounts() fills @buf with fsname, but that is not
6280 * called if explicit paths are specified on the command-line
6283 rc = llapi_get_fsname(mntdir, buf, buflen);
6284 } else /* if (opt_instance) */ {
6285 rc = llapi_get_instance(mntdir, buf, buflen);
6289 fprintf(stderr, "cannot get instance for '%s': %s\n",
6290 mntdir, strerror(-rc));
6295 printf("%s %s\n", buf, mntdir);
6297 printf("%s\n", buf);
6302 static int lfs_getname(int argc, char **argv)
6304 struct option long_opts[] = {
6305 { .val = 'h', .name = "help", .has_arg = no_argument },
6306 { .val = 'i', .name = "instance", .has_arg = no_argument },
6307 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6309 bool opt_instance = false, opt_fsname = false;
6310 char fsname[PATH_MAX] = "";
6313 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6316 opt_instance = true;
6327 if (optind == argc) { /* no paths specified, get all paths. */
6328 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6331 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6332 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6333 opt_instance, opt_fsname, true);
6336 path[0] = fsname[0] = mntdir[0] = '\0';
6338 } else { /* paths specified, only attempt to search these. */
6341 /* if only one path is given, print only requested info */
6342 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6344 for (; optind < argc; optind++) {
6345 rc2 = print_instance(argv[optind], fsname,
6346 sizeof(fsname), opt_instance,
6347 opt_fsname, opt_mntdir);
6357 static int lfs_check(int argc, char **argv)
6359 char mntdir[PATH_MAX] = {'\0'};
6368 fprintf(stderr, "%s check: server type must be specified\n",
6373 obd_types[0] = obd_type1;
6374 obd_types[1] = obd_type2;
6375 obd_types[2] = obd_type3;
6377 if (strcmp(argv[1], "osts") == 0) {
6378 strcpy(obd_types[0], "osc");
6379 } else if (strcmp(argv[1], "mdts") == 0 ||
6380 strcmp(argv[1], "mds") == 0) {
6381 strcpy(obd_types[0], "mdc");
6382 } else if (strcmp(argv[1], "mgts") == 0) {
6383 strcpy(obd_types[0], "mgc");
6384 } else if (strcmp(argv[1], "all") == 0 ||
6385 strcmp(argv[1], "servers") == 0) {
6387 strcpy(obd_types[0], "osc");
6388 strcpy(obd_types[1], "mdc");
6389 strcpy(obd_types[2], "mgc");
6391 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6396 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6397 if (rc < 0 || mntdir[0] == '\0') {
6399 "%s check: cannot find mounted Lustre filesystem: %s\n",
6400 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6404 rc = llapi_target_check(num_types, obd_types, mntdir);
6406 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6407 progname, argv[1], strerror(-rc));
6413 #ifdef HAVE_SYS_QUOTA_H
6414 #define ARG2INT(nr, str, msg) \
6417 nr = strtol(str, &endp, 0); \
6418 if (*endp != '\0') { \
6419 fprintf(stderr, "%s: bad %s '%s'\n", \
6420 progname, msg, str); \
6425 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6427 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6428 * returns the value or ULONG_MAX on integer overflow or incorrect format
6430 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6431 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6432 * 3. empty integer value is interpreted as 0
6434 static unsigned long str2sec(const char* timestr)
6436 const char spec[] = "smhdw";
6437 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6438 unsigned long val = 0;
6441 if (strpbrk(timestr, spec) == NULL) {
6442 /* no specifiers inside the time string,
6443 should treat it as an integer value */
6444 val = strtoul(timestr, &tail, 10);
6445 return *tail ? ULONG_MAX : val;
6448 /* format string is XXwXXdXXhXXmXXs */
6454 v = strtoul(timestr, &tail, 10);
6455 if (v == ULONG_MAX || *tail == '\0')
6456 /* value too large (ULONG_MAX or more)
6457 or missing specifier */
6460 ptr = strchr(spec, *tail);
6462 /* unknown specifier */
6467 /* check if product will overflow the type */
6468 if (!(v < ULONG_MAX / mult[ind]))
6471 ADD_OVERFLOW(val, mult[ind] * v);
6472 if (val == ULONG_MAX)
6484 #define ARG2ULL(nr, str, def_units) \
6486 unsigned long long limit, units = def_units; \
6489 rc = llapi_parse_size(str, &limit, &units, 1); \
6491 fprintf(stderr, "%s: invalid limit '%s'\n", \
6498 static inline int has_times_option(int argc, char **argv)
6502 for (i = 1; i < argc; i++)
6503 if (!strcmp(argv[i], "-t"))
6509 int lfs_setquota_times(int argc, char **argv)
6512 struct if_quotactl qctl;
6513 char *mnt, *obd_type = (char *)qctl.obd_type;
6514 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6515 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6516 struct option long_opts[] = {
6517 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6518 { .val = 'g', .name = "group", .has_arg = no_argument },
6519 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6520 { .val = 'p', .name = "projid", .has_arg = no_argument },
6521 { .val = 't', .name = "times", .has_arg = no_argument },
6522 { .val = 'u', .name = "user", .has_arg = no_argument },
6526 memset(&qctl, 0, sizeof(qctl));
6527 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6528 qctl.qc_type = ALLQUOTA;
6530 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6531 long_opts, NULL)) != -1) {
6542 if (qctl.qc_type != ALLQUOTA) {
6543 fprintf(stderr, "error: -u/g/p can't be used "
6544 "more than once\n");
6547 qctl.qc_type = qtype;
6550 if (strncmp(optarg, NOTIFY_GRACE,
6551 strlen(NOTIFY_GRACE)) == 0) {
6552 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6554 dqi->dqi_bgrace = str2sec(optarg);
6555 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6556 fprintf(stderr, "error: bad "
6557 "block-grace: %s\n", optarg);
6561 dqb->dqb_valid |= QIF_BTIME;
6564 if (strncmp(optarg, NOTIFY_GRACE,
6565 strlen(NOTIFY_GRACE)) == 0) {
6566 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6568 dqi->dqi_igrace = str2sec(optarg);
6569 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6570 fprintf(stderr, "error: bad "
6571 "inode-grace: %s\n", optarg);
6575 dqb->dqb_valid |= QIF_ITIME;
6577 case 't': /* Yes, of course! */
6579 /* getopt prints error message for us when opterr != 0 */
6585 if (qctl.qc_type == ALLQUOTA) {
6586 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6590 if (optind != argc - 1) {
6591 fprintf(stderr, "error: unexpected parameters encountered\n");
6596 rc = llapi_quotactl(mnt, &qctl);
6599 fprintf(stderr, "%s %s ", obd_type,
6600 obd_uuid2str(&qctl.obd_uuid));
6601 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6608 #define BSLIMIT (1 << 0)
6609 #define BHLIMIT (1 << 1)
6610 #define ISLIMIT (1 << 2)
6611 #define IHLIMIT (1 << 3)
6613 int lfs_setquota(int argc, char **argv)
6616 struct if_quotactl qctl;
6617 char *mnt, *obd_type = (char *)qctl.obd_type;
6618 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6619 struct option long_opts[] = {
6620 { .val = 'b', .name = "block-softlimit",
6621 .has_arg = required_argument },
6622 { .val = 'B', .name = "block-hardlimit",
6623 .has_arg = required_argument },
6624 { .val = 'd', .name = "default", .has_arg = no_argument },
6625 { .val = 'g', .name = "group", .has_arg = required_argument },
6626 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6627 { .val = 'i', .name = "inode-softlimit",
6628 .has_arg = required_argument },
6629 { .val = 'I', .name = "inode-hardlimit",
6630 .has_arg = required_argument },
6631 { .val = 'p', .name = "projid", .has_arg = required_argument },
6632 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6633 { .val = 'u', .name = "user", .has_arg = required_argument },
6634 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6636 unsigned limit_mask = 0;
6638 bool use_default = false;
6641 if (has_times_option(argc, argv))
6642 return lfs_setquota_times(argc, argv);
6644 memset(&qctl, 0, sizeof(qctl));
6645 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6646 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6647 * so it can be used as a marker that qc_type
6648 * isn't reinitialized from command line */
6650 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6651 long_opts, NULL)) != -1) {
6654 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6657 goto quota_type_def;
6660 rc = name2uid(&qctl.qc_id, optarg);
6663 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6666 goto quota_type_def;
6669 rc = name2gid(&qctl.qc_id, optarg);
6672 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6675 goto quota_type_def;
6678 rc = name2projid(&qctl.qc_id, optarg);
6681 qctl.qc_id = strtoul(optarg, &endptr, 10);
6682 if (*endptr != '\0') {
6683 fprintf(stderr, "%s setquota: invalid"
6684 " id '%s'\n", progname, optarg);
6689 if (qctl.qc_id == 0) {
6690 fprintf(stderr, "%s setquota: can't set quota"
6691 " for root usr/group/project.\n",
6697 if (qctl.qc_type != ALLQUOTA) {
6699 "%s setquota: only one of -u, -U, -g,"
6700 " -G, -p or -P may be specified\n",
6704 qctl.qc_type = qtype;
6707 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6711 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6712 dqb->dqb_bsoftlimit >>= 10;
6713 limit_mask |= BSLIMIT;
6714 if (dqb->dqb_bsoftlimit &&
6715 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6717 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6718 "See '%s help setquota' or Lustre manual for details\n",
6720 (unsigned long long)dqb->dqb_bsoftlimit,
6724 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6725 dqb->dqb_bhardlimit >>= 10;
6726 limit_mask |= BHLIMIT;
6727 if (dqb->dqb_bhardlimit &&
6728 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6730 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6731 "See '%s help setquota' or Lustre manual for details\n",
6733 (unsigned long long)dqb->dqb_bhardlimit,
6737 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6738 limit_mask |= ISLIMIT;
6739 if (dqb->dqb_isoftlimit &&
6740 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6742 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6743 "See '%s help setquota' or Lustre manual for details\n",
6745 (unsigned long long)dqb->dqb_isoftlimit,
6749 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6750 limit_mask |= IHLIMIT;
6751 if (dqb->dqb_ihardlimit &&
6752 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6754 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6755 "See '%s help setquota' or Lustre manual for details\n",
6757 (unsigned long long)dqb->dqb_ihardlimit,
6762 "%s setquota: unrecognized option '%s'\n",
6763 progname, argv[optind - 1]);
6768 if (qctl.qc_type == ALLQUOTA) {
6770 "%s setquota: either -u or -g must be specified\n",
6775 if (!use_default && limit_mask == 0) {
6777 "%s setquota: at least one limit must be specified\n",
6782 if (use_default && limit_mask != 0) {
6784 "%s setquota: limits should not be specified when"
6785 " using default quota\n",
6790 if (use_default && qctl.qc_id == 0) {
6792 "%s setquota: can not set default quota for root"
6793 " user/group/project\n",
6798 if (optind != argc - 1) {
6800 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6801 progname, argv[optind]);
6808 dqb->dqb_bhardlimit = 0;
6809 dqb->dqb_bsoftlimit = 0;
6810 dqb->dqb_ihardlimit = 0;
6811 dqb->dqb_isoftlimit = 0;
6814 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6815 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6816 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6817 /* sigh, we can't just set blimits/ilimits */
6818 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6819 .qc_type = qctl.qc_type,
6820 .qc_id = qctl.qc_id};
6822 rc = llapi_quotactl(mnt, &tmp_qctl);
6826 if (!(limit_mask & BHLIMIT))
6827 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6828 if (!(limit_mask & BSLIMIT))
6829 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6830 if (!(limit_mask & IHLIMIT))
6831 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6832 if (!(limit_mask & ISLIMIT))
6833 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6835 /* Keep grace times if we have got no softlimit arguments */
6836 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6837 dqb->dqb_valid |= QIF_BTIME;
6838 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6841 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6842 dqb->dqb_valid |= QIF_ITIME;
6843 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6847 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6848 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6850 rc = llapi_quotactl(mnt, &qctl);
6854 "%s setquota: cannot quotactl '%s' '%s': %s",
6856 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6863 /* Converts seconds value into format string
6864 * result is returned in buf
6866 * 1. result is in descenting order: 1w2d3h4m5s
6867 * 2. zero fields are not filled (except for p. 3): 5d1s
6868 * 3. zero seconds value is presented as "0s"
6870 static char * __sec2str(time_t seconds, char *buf)
6872 const char spec[] = "smhdw";
6873 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6878 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
6879 c = seconds / mult[i];
6881 if (c > 0 || (i == 0 && buf == tail))
6882 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
6891 static void sec2str(time_t seconds, char *buf, int rc)
6898 tail = __sec2str(seconds, tail);
6900 if (rc && tail - buf < 39) {
6906 static void diff2str(time_t seconds, char *buf, time_t now)
6912 if (seconds <= now) {
6913 strcpy(buf, "none");
6916 __sec2str(seconds - now, buf);
6919 static void print_quota_title(char *name, struct if_quotactl *qctl,
6920 bool human_readable, bool show_default)
6923 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6924 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6925 "Filesystem", "bquota", "blimit", "bgrace",
6926 "iquota", "ilimit", "igrace");
6928 printf("Disk quotas for %s %s (%cid %u):\n",
6929 qtype_name(qctl->qc_type), name,
6930 *qtype_name(qctl->qc_type), qctl->qc_id);
6931 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6932 "Filesystem", human_readable ? "used" : "kbytes",
6933 "quota", "limit", "grace",
6934 "files", "quota", "limit", "grace");
6938 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6941 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6944 snprintf(buf, buflen, "%5.4gP",
6945 (double)num / ((__u64)1 << 40));
6947 snprintf(buf, buflen, "%5.4gT",
6948 (double)num / (1 << 30));
6950 snprintf(buf, buflen, "%5.4gG",
6951 (double)num / (1 << 20));
6953 snprintf(buf, buflen, "%5.4gM",
6954 (double)num / (1 << 10));
6956 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6960 #define STRBUF_LEN 32
6961 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6962 int rc, bool h, bool show_default)
6968 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6969 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6970 int bover = 0, iover = 0;
6971 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6972 char numbuf[3][STRBUF_LEN];
6974 char strbuf[STRBUF_LEN];
6976 if (dqb->dqb_bhardlimit &&
6977 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6979 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6980 if (dqb->dqb_btime > now) {
6987 if (dqb->dqb_ihardlimit &&
6988 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6990 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6991 if (dqb->dqb_itime > now) {
6999 if (strlen(mnt) > 15)
7000 printf("%s\n%15s", mnt, "");
7002 printf("%15s", mnt);
7005 diff2str(dqb->dqb_btime, timebuf, now);
7006 else if (show_default)
7007 snprintf(timebuf, sizeof(timebuf), "%llu",
7008 (unsigned long long)dqb->dqb_btime);
7010 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7011 strbuf, sizeof(strbuf), h);
7012 if (rc == -EREMOTEIO)
7013 sprintf(numbuf[0], "%s*", strbuf);
7015 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
7016 "%s" : "[%s]", strbuf);
7018 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
7019 if (type == QC_GENERAL)
7020 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
7021 "%s" : "[%s]", strbuf);
7023 sprintf(numbuf[1], "%s", "-");
7025 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
7026 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
7027 "%s" : "[%s]", strbuf);
7030 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7032 printf(" %7s%c %6s %7s %7s",
7033 numbuf[0], bover ? '*' : ' ', numbuf[1],
7034 numbuf[2], bover > 1 ? timebuf : "-");
7038 diff2str(dqb->dqb_itime, timebuf, now);
7039 else if (show_default)
7040 snprintf(timebuf, sizeof(timebuf), "%llu",
7041 (unsigned long long)dqb->dqb_itime);
7043 snprintf(numbuf[0], sizeof(numbuf),
7044 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
7045 (uintmax_t)dqb->dqb_curinodes);
7047 if (type == QC_GENERAL)
7048 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7050 (uintmax_t)dqb->dqb_isoftlimit);
7052 sprintf(numbuf[1], "%s", "-");
7054 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7055 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7058 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7059 else if (type != QC_OSTIDX)
7060 printf(" %7s%c %6s %7s %7s",
7061 numbuf[0], iover ? '*' : ' ', numbuf[1],
7062 numbuf[2], iover > 1 ? timebuf : "-");
7064 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7066 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
7067 qctl->qc_cmd == Q_GETOINFO) {
7071 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7072 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7074 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7075 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7076 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7078 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7080 printf("Block grace time: %s; Inode grace time: %s\n",
7081 bgtimebuf, igtimebuf);
7085 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7086 bool h, __u64 *total)
7088 int rc = 0, rc1 = 0, count = 0;
7089 __u32 valid = qctl->qc_valid;
7091 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7093 fprintf(stderr, "can not get %s count: %s\n",
7094 is_mdt ? "mdt": "ost", strerror(-rc));
7098 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
7099 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7100 rc = llapi_quotactl(mnt, qctl);
7102 /* It is remote client case. */
7103 if (rc == -EOPNOTSUPP) {
7110 fprintf(stderr, "quotactl %s%d failed.\n",
7111 is_mdt ? "mdt": "ost", qctl->qc_idx);
7115 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7116 qctl->qc_valid, 0, h, false);
7117 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7118 qctl->qc_dqblk.dqb_bhardlimit;
7121 qctl->qc_valid = valid;
7125 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7126 int verbose, int quiet, bool human_readable,
7129 int rc1 = 0, rc2 = 0, rc3 = 0;
7130 char *obd_type = (char *)qctl->obd_type;
7131 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7132 __u64 total_ialloc = 0, total_balloc = 0;
7133 bool use_default_for_blk = false;
7134 bool use_default_for_file = false;
7137 rc1 = llapi_quotactl(mnt, qctl);
7141 fprintf(stderr, "%s quotas are not enabled.\n",
7142 qtype_name(qctl->qc_type));
7145 fprintf(stderr, "Permission denied.\n");
7148 /* We already got error message. */
7151 fprintf(stderr, "Unexpected quotactl error: %s\n",
7156 if (!show_default && qctl->qc_id == 0) {
7157 qctl->qc_dqblk.dqb_bhardlimit = 0;
7158 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7159 qctl->qc_dqblk.dqb_ihardlimit = 0;
7160 qctl->qc_dqblk.dqb_isoftlimit = 0;
7161 qctl->qc_dqblk.dqb_btime = 0;
7162 qctl->qc_dqblk.dqb_itime = 0;
7163 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7166 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7167 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7168 use_default_for_blk = true;
7169 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7172 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7173 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7174 use_default_for_file = true;
7175 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7178 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7179 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7180 print_quota_title(name, qctl, human_readable, show_default);
7182 if (rc1 && *obd_type)
7183 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7185 if (qctl->qc_valid != QC_GENERAL)
7188 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
7189 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7190 (QIF_LIMITS|QIF_USAGE));
7192 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7194 if (!show_default && verbose &&
7195 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
7196 char strbuf[STRBUF_LEN];
7198 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7200 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7202 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7204 printf("Total allocated inode limit: %ju, total "
7205 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
7209 if (use_default_for_blk)
7210 printf("%cid %u is using default block quota setting\n",
7211 *qtype_name(qctl->qc_type), qctl->qc_id);
7213 if (use_default_for_file)
7214 printf("%cid %u is using default file quota setting\n",
7215 *qtype_name(qctl->qc_type), qctl->qc_id);
7217 if (rc1 || rc2 || rc3 || inacc)
7218 printf("Some errors happened when getting quota info. "
7219 "Some devices may be not working or deactivated. "
7220 "The data in \"[]\" is inaccurate.\n");
7234 static int lfs_project(int argc, char **argv)
7236 int ret = 0, err = 0, c, i;
7237 struct project_handle_control phc = { 0 };
7238 enum lfs_project_ops_t op;
7241 phc.assign_projid = false;
7242 /* default action */
7243 op = LFS_PROJECT_LIST;
7245 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7248 if (op != LFS_PROJECT_LIST) {
7250 "%s: cannot specify '-c' '-C' '-s' together\n",
7255 op = LFS_PROJECT_CHECK;
7258 if (op != LFS_PROJECT_LIST) {
7260 "%s: cannot specify '-c' '-C' '-s' together\n",
7265 op = LFS_PROJECT_CLEAR;
7268 if (op != LFS_PROJECT_LIST) {
7270 "%s: cannot specify '-c' '-C' '-s' together\n",
7275 phc.set_inherit = true;
7276 op = LFS_PROJECT_SET;
7282 phc.keep_projid = true;
7285 phc.recursive = true;
7288 phc.projid = strtoul(optarg, NULL, 0);
7289 phc.assign_projid = true;
7293 phc.newline = false;
7296 fprintf(stderr, "%s: invalid option '%c'\n",
7302 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7303 op = LFS_PROJECT_SET;
7304 phc.set_projid = true;
7305 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7306 phc.set_projid = true;
7310 case LFS_PROJECT_CHECK:
7311 if (phc.keep_projid) {
7313 "%s: '-k' is useless together with '-c'\n",
7318 case LFS_PROJECT_CLEAR:
7321 "%s: '-0' is useless together with '-C'\n",
7325 if (phc.assign_projid) {
7327 "%s: '-p' is useless together with '-C'\n",
7332 case LFS_PROJECT_SET:
7335 "%s: '-0' is useless together with '-s'\n",
7339 if (phc.keep_projid) {
7341 "%s: '-k' is useless together with '-s'\n",
7349 "%s: '-0' is useless for list operations\n",
7359 fprintf(stderr, "%s: missing file or directory target(s)\n",
7364 for (i = 0; i < argc; i++) {
7366 case LFS_PROJECT_CHECK:
7367 err = lfs_project_check(argv[i], &phc);
7369 case LFS_PROJECT_LIST:
7370 err = lfs_project_list(argv[i], &phc);
7372 case LFS_PROJECT_CLEAR:
7373 err = lfs_project_clear(argv[i], &phc);
7375 case LFS_PROJECT_SET:
7376 err = lfs_project_set(argv[i], &phc);
7388 static int lfs_quota(int argc, char **argv)
7391 char *mnt, *name = NULL;
7392 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
7393 .qc_type = ALLQUOTA };
7394 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
7395 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7397 __u32 valid = QC_GENERAL, idx = 0;
7398 bool human_readable = false;
7399 bool show_default = false;
7402 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
7405 show_default = true;
7410 show_default = true;
7415 show_default = true;
7419 if (qctl.qc_type != ALLQUOTA) {
7421 "%s quota: only one of -u, -g, or -p may be specified\n",
7425 qctl.qc_type = qtype;
7428 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7431 valid = qctl.qc_valid = QC_UUID;
7432 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7435 valid = qctl.qc_valid = QC_MDTIDX;
7436 idx = qctl.qc_idx = atoi(optarg);
7437 if (idx == 0 && *optarg != '0') {
7439 "%s quota: invalid MDT index '%s'\n",
7445 valid = qctl.qc_valid = QC_OSTIDX;
7446 idx = qctl.qc_idx = atoi(optarg);
7447 if (idx == 0 && *optarg != '0') {
7449 "%s quota: invalid OST index '%s'\n",
7461 human_readable = true;
7464 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7465 progname, argv[optind - 1]);
7470 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7471 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7472 optind == argc - 1 && !show_default) {
7474 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7477 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7478 qctl.qc_type = qtype;
7479 qctl.qc_valid = valid;
7480 if (qtype == USRQUOTA) {
7481 qctl.qc_id = geteuid();
7482 rc = uid2name(&name, qctl.qc_id);
7484 qctl.qc_id = getegid();
7485 rc = gid2name(&name, qctl.qc_id);
7486 memset(&qctl.qc_dqblk, 0,
7487 sizeof(qctl.qc_dqblk));
7492 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7493 human_readable, show_default);
7498 /* lfs quota -u username /path/to/lustre/mount */
7499 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7500 /* options should be followed by u/g-name and mntpoint */
7501 if ((!show_default && optind + 2 != argc) ||
7502 (show_default && optind + 1 != argc) ||
7503 qctl.qc_type == ALLQUOTA) {
7505 "%s quota: name and mount point must be specified\n",
7510 if (!show_default) {
7511 name = argv[optind++];
7512 switch (qctl.qc_type) {
7514 rc = name2uid(&qctl.qc_id, name);
7517 rc = name2gid(&qctl.qc_id, name);
7520 rc = name2projid(&qctl.qc_id, name);
7527 qctl.qc_valid = QC_GENERAL;
7528 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7533 qctl.qc_id = strtoul(name, &endptr, 10);
7534 if (*endptr != '\0') {
7535 fprintf(stderr, "%s quota: invalid id '%s'\n",
7540 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7541 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7547 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7548 human_readable, show_default);
7551 #endif /* HAVE_SYS_QUOTA_H! */
7553 static int flushctx_ioctl(char *mp)
7557 fd = open(mp, O_RDONLY);
7559 fprintf(stderr, "flushctx: error open %s: %s\n",
7560 mp, strerror(errno));
7564 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7566 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7567 mp, strerror(errno));
7573 static int lfs_flushctx(int argc, char **argv)
7575 int kdestroy = 0, c;
7576 char mntdir[PATH_MAX] = {'\0'};
7580 while ((c = getopt(argc, argv, "k")) != -1) {
7586 fprintf(stderr, "error: %s: option '-%c' "
7587 "unrecognized\n", argv[0], c);
7593 if ((rc = system("kdestroy > /dev/null")) != 0) {
7594 rc = WEXITSTATUS(rc);
7595 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7599 if (optind >= argc) {
7600 /* flush for all mounted lustre fs. */
7601 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7602 /* Check if we have a mount point */
7603 if (mntdir[0] == '\0')
7606 if (flushctx_ioctl(mntdir))
7609 mntdir[0] = '\0'; /* avoid matching in next loop */
7612 /* flush fs as specified */
7613 while (optind < argc) {
7614 if (flushctx_ioctl(argv[optind++]))
7621 static int lfs_changelog(int argc, char **argv)
7623 void *changelog_priv;
7624 struct changelog_rec *rec;
7625 long long startrec = 0, endrec = 0;
7627 struct option long_opts[] = {
7628 { .val = 'f', .name = "follow", .has_arg = no_argument },
7630 char short_opts[] = "f";
7633 while ((rc = getopt_long(argc, argv, short_opts,
7634 long_opts, NULL)) != -1) {
7641 "%s changelog: unrecognized option '%s'\n",
7642 progname, argv[optind - 1]);
7646 if (optind >= argc) {
7647 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7652 mdd = argv[optind++];
7654 startrec = strtoll(argv[optind++], NULL, 10);
7656 endrec = strtoll(argv[optind++], NULL, 10);
7658 rc = llapi_changelog_start(&changelog_priv,
7659 CHANGELOG_FLAG_BLOCK |
7660 CHANGELOG_FLAG_JOBID |
7661 CHANGELOG_FLAG_EXTRA_FLAGS |
7662 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7665 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7666 progname, strerror(errno = -rc));
7670 rc = llapi_changelog_set_xflags(changelog_priv,
7671 CHANGELOG_EXTRA_FLAG_UIDGID |
7672 CHANGELOG_EXTRA_FLAG_NID |
7673 CHANGELOG_EXTRA_FLAG_OMODE |
7674 CHANGELOG_EXTRA_FLAG_XATTR);
7677 "%s changelog: cannot set xflags for changelog: %s\n",
7678 progname, strerror(errno = -rc));
7682 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7686 if (endrec && rec->cr_index > endrec) {
7687 llapi_changelog_free(&rec);
7690 if (rec->cr_index < startrec) {
7691 llapi_changelog_free(&rec);
7695 secs = rec->cr_time >> 30;
7696 gmtime_r(&secs, &ts);
7697 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7698 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7699 changelog_type2str(rec->cr_type),
7700 ts.tm_hour, ts.tm_min, ts.tm_sec,
7701 (int)(rec->cr_time & ((1 << 30) - 1)),
7702 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7703 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7705 if (rec->cr_flags & CLF_JOBID) {
7706 struct changelog_ext_jobid *jid =
7707 changelog_rec_jobid(rec);
7709 if (jid->cr_jobid[0] != '\0')
7710 printf(" j=%s", jid->cr_jobid);
7713 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7714 struct changelog_ext_extra_flags *ef =
7715 changelog_rec_extra_flags(rec);
7717 printf(" ef=0x%llx",
7718 (unsigned long long)ef->cr_extra_flags);
7720 if (ef->cr_extra_flags & CLFE_UIDGID) {
7721 struct changelog_ext_uidgid *uidgid =
7722 changelog_rec_uidgid(rec);
7724 printf(" u=%llu:%llu",
7725 (unsigned long long)uidgid->cr_uid,
7726 (unsigned long long)uidgid->cr_gid);
7728 if (ef->cr_extra_flags & CLFE_NID) {
7729 struct changelog_ext_nid *nid =
7730 changelog_rec_nid(rec);
7733 libcfs_nid2str(nid->cr_nid));
7736 if (ef->cr_extra_flags & CLFE_OPEN) {
7737 struct changelog_ext_openmode *omd =
7738 changelog_rec_openmode(rec);
7739 char mode[] = "---";
7741 /* exec mode must be exclusive */
7742 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7745 if (omd->cr_openflags & MDS_FMODE_READ)
7747 if (omd->cr_openflags &
7754 if (strcmp(mode, "---") != 0)
7755 printf(" m=%s", mode);
7759 if (ef->cr_extra_flags & CLFE_XATTR) {
7760 struct changelog_ext_xattr *xattr =
7761 changelog_rec_xattr(rec);
7763 if (xattr->cr_xattr[0] != '\0')
7764 printf(" x=%s", xattr->cr_xattr);
7768 if (rec->cr_namelen)
7769 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7770 rec->cr_namelen, changelog_rec_name(rec));
7772 if (rec->cr_flags & CLF_RENAME) {
7773 struct changelog_ext_rename *rnm =
7774 changelog_rec_rename(rec);
7776 if (!fid_is_zero(&rnm->cr_sfid))
7777 printf(" s="DFID" sp="DFID" %.*s",
7778 PFID(&rnm->cr_sfid),
7779 PFID(&rnm->cr_spfid),
7780 (int)changelog_rec_snamelen(rec),
7781 changelog_rec_sname(rec));
7785 llapi_changelog_free(&rec);
7788 llapi_changelog_fini(&changelog_priv);
7791 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7792 progname, strerror(errno = -rc));
7794 return (rc == 1 ? 0 : rc);
7797 static int lfs_changelog_clear(int argc, char **argv)
7805 endrec = strtoll(argv[3], NULL, 10);
7807 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7810 fprintf(stderr, "%s: record out of range: %llu\n",
7812 else if (rc == -ENOENT)
7813 fprintf(stderr, "%s: no changelog user: %s\n",
7816 fprintf(stderr, "%s error: %s\n", argv[0],
7825 static int lfs_fid2path(int argc, char **argv)
7827 struct option long_opts[] = {
7828 { .val = 'c', .name = "cur", .has_arg = no_argument },
7829 { .val = 'l', .name = "link", .has_arg = required_argument },
7830 { .val = 'r', .name = "rec", .has_arg = required_argument },
7832 char short_opts[] = "cl:r:";
7833 char *device, *fid, *path;
7834 long long recno = -1;
7839 char *endptr = NULL;
7841 while ((rc = getopt_long(argc, argv, short_opts,
7842 long_opts, NULL)) != -1) {
7848 linkno = strtol(optarg, &endptr, 10);
7849 if (*endptr != '\0') {
7851 "%s fid2path: invalid linkno '%s'\n",
7857 recno = strtoll(optarg, &endptr, 10);
7858 if (*endptr != '\0') {
7860 "%s fid2path: invalid recno '%s'\n",
7867 "%s fid2path: unrecognized option '%s'\n",
7868 progname, argv[optind - 1]);
7875 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7880 device = argv[optind++];
7881 path = calloc(1, PATH_MAX);
7885 "%s fid2path: cannot allocate memory for path: %s\n",
7886 progname, strerror(-rc));
7891 while (optind < argc) {
7892 fid = argv[optind++];
7894 lnktmp = (linkno >= 0) ? linkno : 0;
7896 int oldtmp = lnktmp;
7897 long long rectmp = recno;
7899 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7903 "%s fid2path: cannot find '%s': %s\n",
7904 progname, fid, strerror(errno = -rc2));
7911 fprintf(stdout, "%lld ", rectmp);
7912 if (device[0] == '/') {
7913 fprintf(stdout, "%s", device);
7914 if (device[strlen(device) - 1] != '/')
7915 fprintf(stdout, "/");
7916 } else if (path[0] == '\0') {
7917 fprintf(stdout, "/");
7919 fprintf(stdout, "%s\n", path);
7922 /* specified linkno */
7924 if (oldtmp == lnktmp)
7934 static int lfs_path2fid(int argc, char **argv)
7936 struct option long_opts[] = {
7937 { .val = 'p', .name = "parents", .has_arg = no_argument },
7940 const char short_opts[] = "p";
7941 const char *sep = "";
7944 bool show_parents = false;
7946 while ((rc = getopt_long(argc, argv, short_opts,
7947 long_opts, NULL)) != -1) {
7950 show_parents = true;
7954 "%s path2fid: unrecognized option '%s'\n",
7955 progname, argv[optind - 1]);
7960 if (optind > argc - 1) {
7961 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7965 else if (optind < argc - 1)
7969 for (path = argv + optind; *path != NULL; path++) {
7971 if (!show_parents) {
7972 err = llapi_path2fid(*path, &fid);
7974 printf("%s%s"DFID"\n",
7975 *sep != '\0' ? *path : "", sep,
7978 char name[NAME_MAX + 1];
7979 unsigned int linkno = 0;
7981 while ((err = llapi_path2parent(*path, linkno, &fid,
7982 name, sizeof(name))) == 0) {
7983 if (*sep != '\0' && linkno == 0)
7984 printf("%s%s", *path, sep);
7986 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7991 /* err == -ENODATA is end-of-loop */
7992 if (linkno > 0 && err == -ENODATA) {
8000 "%s path2fid: cannot get %sfid for '%s': %s\n",
8001 progname, show_parents ? "parent " : "", *path,
8013 #define MAX_ERRNO 4095
8014 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
8016 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
8020 rc = llapi_rmfid(device, fa);
8022 fprintf(stderr, "rmfid(): rc = %d\n", rc);
8026 for (k = 0; k < fa->fa_nr; k++) {
8027 rc = (__s32)fa->fa_fids[k].f_ver;
8028 if (!IS_ERR_VALUE(rc))
8034 fa->fa_fids[k].f_ver = 0;
8035 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
8036 PFID(&fa->fa_fids[k]), rc);
8042 static int lfs_rmfid(int argc, char **argv)
8044 char *fidstr, *device;
8045 int rc = 0, rc2, nr;
8046 struct fid_array *fa;
8048 if (optind > argc - 1) {
8049 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8053 device = argv[optind++];
8056 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8062 while (optind < argc) {
8065 fidstr = argv[optind++];
8066 while (*fidstr == '[')
8068 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8070 fprintf(stderr, "unrecognized FID: %s\n",
8075 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8076 /* start another batch */
8077 rc2 = lfs_rmfid_and_show_errors(device, fa);
8084 rc2 = lfs_rmfid_and_show_errors(device, fa);
8092 static int lfs_data_version(int argc, char **argv)
8099 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8102 fprintf(stderr, "%s data_version: FILE must be specified\n",
8107 while ((c = getopt(argc, argv, "nrw")) != -1) {
8110 data_version_flags = 0;
8113 data_version_flags |= LL_DV_RD_FLUSH;
8116 data_version_flags |= LL_DV_WR_FLUSH;
8120 "%s data_version: unrecognized option '%s'\n",
8121 progname, argv[optind - 1]);
8125 if (optind == argc) {
8126 fprintf(stderr, "%s data_version: FILE must be specified\n",
8131 path = argv[optind];
8132 fd = open(path, O_RDONLY);
8135 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8136 progname, path, strerror(-rc));
8140 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8143 "%s data_version: cannot get version for '%s': %s\n",
8144 progname, path, strerror(-rc));
8146 printf("%ju" "\n", (uintmax_t)data_version);
8152 static int lfs_hsm_state(int argc, char **argv)
8157 struct hsm_user_state hus;
8165 rc = llapi_hsm_state_get(path, &hus);
8167 fprintf(stderr, "can't get hsm state for %s: %s\n",
8168 path, strerror(errno = -rc));
8172 /* Display path name and status flags */
8173 printf("%s: (0x%08x)", path, hus.hus_states);
8175 if (hus.hus_states & HS_RELEASED)
8176 printf(" released");
8177 if (hus.hus_states & HS_EXISTS)
8179 if (hus.hus_states & HS_DIRTY)
8181 if (hus.hus_states & HS_ARCHIVED)
8182 printf(" archived");
8183 /* Display user-settable flags */
8184 if (hus.hus_states & HS_NORELEASE)
8185 printf(" never_release");
8186 if (hus.hus_states & HS_NOARCHIVE)
8187 printf(" never_archive");
8188 if (hus.hus_states & HS_LOST)
8189 printf(" lost_from_hsm");
8191 if (hus.hus_archive_id != 0)
8192 printf(", archive_id:%d", hus.hus_archive_id);
8195 } while (++i < argc);
8200 #define LFS_HSM_SET 0
8201 #define LFS_HSM_CLEAR 1
8204 * Generic function to set or clear HSM flags.
8205 * Used by hsm_set and hsm_clear.
8207 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8209 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8211 struct option long_opts[] = {
8212 { .val = 'A', .name = "archived", .has_arg = no_argument },
8213 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8214 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8215 { .val = 'e', .name = "exists", .has_arg = no_argument },
8216 { .val = 'l', .name = "lost", .has_arg = no_argument },
8217 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8218 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8220 char short_opts[] = "lraAdei:";
8224 __u32 archive_id = 0;
8230 while ((c = getopt_long(argc, argv, short_opts,
8231 long_opts, NULL)) != -1) {
8237 mask |= HS_NOARCHIVE;
8240 mask |= HS_ARCHIVED;
8243 mask |= HS_NORELEASE;
8252 archive_id = strtol(optarg, &end, 10);
8254 fprintf(stderr, "invalid archive_id: '%s'\n",
8262 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8263 argv[0], argv[optind - 1]);
8268 /* User should have specified a flag */
8272 while (optind < argc) {
8274 path = argv[optind];
8276 /* If mode == 0, this means we apply the mask. */
8277 if (mode == LFS_HSM_SET)
8278 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8280 rc = llapi_hsm_state_set(path, 0, mask, 0);
8283 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8284 path, strerror(errno = -rc));
8293 static int lfs_hsm_action(int argc, char **argv)
8298 struct hsm_current_action hca;
8299 struct hsm_extent he;
8300 enum hsm_user_action hua;
8301 enum hsm_progress_states hps;
8309 rc = llapi_hsm_current_action(path, &hca);
8311 fprintf(stderr, "can't get hsm action for %s: %s\n",
8312 path, strerror(errno = -rc));
8315 he = hca.hca_location;
8316 hua = hca.hca_action;
8317 hps = hca.hca_state;
8319 printf("%s: %s", path, hsm_user_action2name(hua));
8321 /* Skip file without action */
8322 if (hca.hca_action == HUA_NONE) {
8327 printf(" %s ", hsm_progress_state2name(hps));
8329 if ((hps == HPS_RUNNING) &&
8330 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8331 printf("(%llu bytes moved)\n",
8332 (unsigned long long)he.length);
8333 else if ((he.offset + he.length) == LUSTRE_EOF)
8334 printf("(from %llu to EOF)\n",
8335 (unsigned long long)he.offset);
8337 printf("(from %llu to %llu)\n",
8338 (unsigned long long)he.offset,
8339 (unsigned long long)(he.offset + he.length));
8341 } while (++i < argc);
8346 static int lfs_hsm_set(int argc, char **argv)
8348 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8351 static int lfs_hsm_clear(int argc, char **argv)
8353 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8357 * Check file state and return its fid, to be used by lfs_hsm_request().
8359 * \param[in] file Path to file to check
8360 * \param[in,out] fid Pointer to allocated lu_fid struct.
8361 * \param[in,out] last_dev Pointer to last device id used.
8363 * \return 0 on success.
8365 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8371 rc = lstat(file, &st);
8373 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8376 /* Checking for regular file as archiving as posix copytool
8377 * rejects archiving files other than regular files
8379 if (!S_ISREG(st.st_mode)) {
8380 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8383 /* A request should be ... */
8384 if (*last_dev != st.st_dev && *last_dev != 0) {
8385 fprintf(stderr, "All files should be "
8386 "on the same filesystem: %s\n", file);
8389 *last_dev = st.st_dev;
8391 rc = llapi_path2fid(file, fid);
8393 fprintf(stderr, "Cannot read FID of %s: %s\n",
8394 file, strerror(-rc));
8400 /* Fill an HSM HUR item with a given file name.
8402 * If mntpath is set, then the filename is actually a FID, and no
8403 * lookup on the filesystem will be performed.
8405 * \param[in] hur the user request to fill
8406 * \param[in] idx index of the item inside the HUR to fill
8407 * \param[in] mntpath mountpoint of Lustre
8408 * \param[in] fname filename (if mtnpath is NULL)
8409 * or FID (if mntpath is set)
8410 * \param[in] last_dev pointer to last device id used
8412 * \retval 0 on success
8413 * \retval CMD_HELP or a negative errno on error
8415 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8416 const char *mntpath, const char *fname,
8419 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8422 hui->hui_extent.length = -1;
8424 if (mntpath != NULL) {
8425 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8427 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8430 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8434 hur->hur_request.hr_itemcount++;
8439 static int lfs_hsm_request(int argc, char **argv, int action)
8441 struct option long_opts[] = {
8442 { .val = 'a', .name = "archive", .has_arg = required_argument },
8443 { .val = 'D', .name = "data", .has_arg = required_argument },
8444 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8445 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8448 char short_opts[] = "l:D:a:m:";
8449 struct hsm_user_request *hur, *oldhur;
8454 char *filelist = NULL;
8455 char fullpath[PATH_MAX];
8456 char *opaque = NULL;
8460 int nbfile_alloc = 0;
8461 char *some_file = NULL;
8462 char *mntpath = NULL;
8468 while ((c = getopt_long(argc, argv, short_opts,
8469 long_opts, NULL)) != -1) {
8478 if (action != HUA_ARCHIVE &&
8479 action != HUA_REMOVE) {
8481 "error: -a is supported only "
8482 "when archiving or removing\n");
8485 archive_id = atoi(optarg);
8488 if (some_file == NULL) {
8490 some_file = strdup(optarg);
8496 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8497 argv[0], argv[optind - 1]);
8502 /* All remaining args are files, so we have at least nbfile */
8503 nbfile = argc - optind;
8505 if ((nbfile == 0) && (filelist == NULL))
8509 opaque_len = strlen(opaque);
8511 /* Alloc the request structure with enough place to store all files
8512 * from command line. */
8513 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8515 fprintf(stderr, "Cannot create the request: %s\n",
8519 nbfile_alloc = nbfile;
8521 hur->hur_request.hr_action = action;
8522 hur->hur_request.hr_archive_id = archive_id;
8523 hur->hur_request.hr_flags = 0;
8525 /* All remaining args are files, add them */
8526 if (nbfile != 0 && some_file == NULL)
8527 some_file = strdup(argv[optind]);
8529 for (i = 0; i < nbfile; i++) {
8530 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8536 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8538 /* If a filelist was specified, read the filelist from it. */
8539 if (filelist != NULL) {
8540 fp = fopen(filelist, "r");
8542 fprintf(stderr, "Cannot read the file list %s: %s\n",
8543 filelist, strerror(errno));
8548 while ((rc = getline(&line, &len, fp)) != -1) {
8549 /* If allocated buffer was too small, get something
8551 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8554 nbfile_alloc = nbfile_alloc * 2 + 1;
8556 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8559 fprintf(stderr, "hsm: cannot allocate "
8560 "the request: %s\n",
8567 size = hur_len(oldhur);
8569 fprintf(stderr, "hsm: cannot allocate "
8570 "%u files + %u bytes data\n",
8571 oldhur->hur_request.hr_itemcount,
8572 oldhur->hur_request.hr_data_len);
8579 memcpy(hur, oldhur, size);
8584 if (line[strlen(line) - 1] == '\n')
8585 line[strlen(line) - 1] = '\0';
8587 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8588 mntpath, line, &last_dev);
8594 if (some_file == NULL) {
8604 /* If a --data was used, add it to the request */
8605 hur->hur_request.hr_data_len = opaque_len;
8607 memcpy(hur_data(hur), opaque, opaque_len);
8609 /* Send the HSM request */
8610 if (realpath(some_file, fullpath) == NULL) {
8611 fprintf(stderr, "Could not find path '%s': %s\n",
8612 some_file, strerror(errno));
8614 rc = llapi_hsm_request(fullpath, hur);
8616 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8617 some_file, strerror(-rc));
8627 static int lfs_hsm_archive(int argc, char **argv)
8629 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8632 static int lfs_hsm_restore(int argc, char **argv)
8634 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8637 static int lfs_hsm_release(int argc, char **argv)
8639 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8642 static int lfs_hsm_remove(int argc, char **argv)
8644 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8647 static int lfs_hsm_cancel(int argc, char **argv)
8649 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8652 static int lfs_swap_layouts(int argc, char **argv)
8657 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8658 SWAP_LAYOUTS_KEEP_MTIME |
8659 SWAP_LAYOUTS_KEEP_ATIME);
8662 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8664 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8666 int lfs_get_mode(const char *string)
8668 enum lock_mode_user mode;
8670 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8671 if (lock_mode_names[mode] == NULL)
8673 if (strcmp(string, lock_mode_names[mode]) == 0)
8680 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8682 enum lu_ladvise_type advice;
8685 advice < ARRAY_SIZE(ladvise_names); advice++) {
8686 if (ladvise_names[advice] == NULL)
8688 if (strcmp(string, ladvise_names[advice]) == 0)
8692 return LU_LADVISE_INVALID;
8695 static int lfs_ladvise(int argc, char **argv)
8697 struct option long_opts[] = {
8698 { .val = 'a', .name = "advice", .has_arg = required_argument },
8699 { .val = 'b', .name = "background", .has_arg = no_argument },
8700 { .val = 'e', .name = "end", .has_arg = required_argument },
8701 { .val = 'l', .name = "length", .has_arg = required_argument },
8702 { .val = 'm', .name = "mode", .has_arg = required_argument },
8703 { .val = 's', .name = "start", .has_arg = required_argument },
8704 { .val = 'u', .name = "unset", .has_arg = no_argument },
8706 char short_opts[] = "a:be:l:m:s:u";
8711 struct llapi_lu_ladvise advice;
8712 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8713 unsigned long long start = 0;
8714 unsigned long long end = LUSTRE_EOF;
8715 unsigned long long length = 0;
8716 unsigned long long size_units;
8717 unsigned long long flags = 0;
8721 while ((c = getopt_long(argc, argv, short_opts,
8722 long_opts, NULL)) != -1) {
8725 advice_type = lfs_get_ladvice(optarg);
8726 if (advice_type == LU_LADVISE_INVALID) {
8727 fprintf(stderr, "%s: invalid advice type "
8728 "'%s'\n", argv[0], optarg);
8729 fprintf(stderr, "Valid types:");
8731 for (advice_type = 0;
8732 advice_type < ARRAY_SIZE(ladvise_names);
8734 if (ladvise_names[advice_type] == NULL)
8736 fprintf(stderr, " %s",
8737 ladvise_names[advice_type]);
8739 fprintf(stderr, "\n");
8752 rc = llapi_parse_size(optarg, &end,
8755 fprintf(stderr, "%s: bad end offset '%s'\n",
8762 rc = llapi_parse_size(optarg, &start,
8765 fprintf(stderr, "%s: bad start offset "
8766 "'%s'\n", argv[0], optarg);
8772 rc = llapi_parse_size(optarg, &length,
8775 fprintf(stderr, "%s: bad length '%s'\n",
8781 mode = lfs_get_mode(optarg);
8783 fprintf(stderr, "%s: bad mode '%s', valid "
8784 "modes are READ or WRITE\n",
8792 fprintf(stderr, "%s: option '%s' unrecognized\n",
8793 argv[0], argv[optind - 1]);
8798 if (advice_type == LU_LADVISE_INVALID) {
8799 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8800 fprintf(stderr, "Valid types:");
8801 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8803 if (ladvise_names[advice_type] == NULL)
8805 fprintf(stderr, " %s", ladvise_names[advice_type]);
8807 fprintf(stderr, "\n");
8811 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8812 fprintf(stderr, "%s: Lock no expand advice is a per file "
8813 "descriptor advice, so when called from lfs, "
8814 "it does nothing.\n", argv[0]);
8818 if (argc <= optind) {
8819 fprintf(stderr, "%s: please give one or more file names\n",
8824 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8825 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8830 if (end == LUSTRE_EOF && length != 0)
8831 end = start + length;
8834 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8835 argv[0], start, end);
8839 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8840 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8845 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8846 fprintf(stderr, "%s: mode is required with lockahead\n",
8851 while (optind < argc) {
8854 path = argv[optind++];
8856 fd = open(path, O_RDONLY);
8858 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8859 argv[0], path, strerror(errno));
8864 advice.lla_start = start;
8865 advice.lla_end = end;
8866 advice.lla_advice = advice_type;
8867 advice.lla_value1 = 0;
8868 advice.lla_value2 = 0;
8869 advice.lla_value3 = 0;
8870 advice.lla_value4 = 0;
8871 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8872 advice.lla_lockahead_mode = mode;
8873 advice.lla_peradvice_flags = flags;
8876 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8879 fprintf(stderr, "%s: cannot give advice '%s' to file "
8880 "'%s': %s\n", argv[0],
8881 ladvise_names[advice_type],
8882 path, strerror(errno));
8888 if (rc == 0 && rc2 < 0)
8894 static const char *const heat_names[] = LU_HEAT_NAMES;
8896 static int lfs_heat_get(int argc, char **argv)
8898 struct lu_heat *heat;
8907 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8909 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8914 while (optind < argc) {
8915 path = argv[optind++];
8917 fd = open(path, O_RDONLY);
8919 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8920 argv[0], path, strerror(errno));
8925 heat->lh_count = OBD_HEAT_COUNT;
8926 rc2 = llapi_heat_get(fd, heat);
8929 fprintf(stderr, "%s: cannot get heat of file '%s'"
8930 ": %s\n", argv[0], path, strerror(errno));
8934 printf("flags: %x\n", heat->lh_flags);
8935 for (i = 0; i < heat->lh_count; i++)
8936 printf("%s: %llu\n", heat_names[i],
8937 (unsigned long long)heat->lh_heat[i]);
8939 if (rc == 0 && rc2 < 0)
8947 static int lfs_heat_set(int argc, char **argv)
8949 struct option long_opts[] = {
8950 { .val = 'c', .name = "clear", .has_arg = no_argument },
8951 { .val = 'o', .name = "off", .has_arg = no_argument },
8952 { .val = 'O', .name = "on", .has_arg = no_argument },
8954 enum lu_heat_flag flags = 0;
8964 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8967 flags |= LU_HEAT_FLAG_CLEAR;
8970 flags |= LU_HEAT_FLAG_CLEAR;
8971 flags |= LU_HEAT_FLAG_OFF;
8974 flags &= ~LU_HEAT_FLAG_OFF;
8979 fprintf(stderr, "%s: option '%s' unrecognized\n",
8980 argv[0], argv[optind - 1]);
8985 if (argc <= optind) {
8986 fprintf(stderr, "%s: please give one or more file names\n",
8991 while (optind < argc) {
8992 path = argv[optind++];
8994 fd = open(path, O_RDONLY);
8996 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8997 argv[0], path, strerror(errno));
9002 rc2 = llapi_heat_set(fd, flags);
9005 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
9006 ": %s\n", argv[0], path, strerror(errno));
9010 if (rc == 0 && rc2 < 0)
9016 /** The input string contains a comma delimited list of component ids and
9017 * ranges, for example "1,2-4,7".
9019 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
9021 bool end_of_loop = false;
9029 while (!end_of_loop) {
9033 char *endptr = NULL;
9036 ptr = strchrnul(arg, ',');
9037 end_of_loop = *ptr == '\0';
9040 start_index = strtol(arg, &endptr, 0);
9041 if (endptr == arg) /* no data at all */
9043 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
9045 if (start_index < 0)
9048 end_index = start_index;
9049 if (*endptr == '-') {
9050 end_index = strtol(endptr + 1, &endptr, 0);
9051 if (*endptr != '\0')
9053 if (end_index < start_index)
9057 for (i = start_index; i <= end_index && size > 0; i++) {
9060 /* remove duplicate */
9061 for (j = 0; j < nr; j++) {
9065 if (j == nr) { /* no duplicate */
9071 if (size == 0 && i < end_index)
9078 if (!end_of_loop && ptr != NULL)
9081 return rc < 0 ? rc : nr;
9085 * struct verify_mirror_id - Mirror id to be verified.
9086 * @mirror_id: A specified mirror id.
9087 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9089 struct verify_mirror_id {
9095 * compare_mirror_ids() - Compare mirror ids.
9096 * @layout: Mirror component list.
9097 * @cbdata: Callback data in verify_mirror_id structure.
9099 * This is a callback function called by llapi_layout_comp_iterate()
9100 * to compare the specified mirror id with the one in the current
9101 * component of @layout. If they are the same, then the specified
9102 * mirror id is valid.
9104 * Return: a negative error code on failure or
9105 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9106 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9109 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9111 struct verify_mirror_id *mirror_id_cbdata =
9112 (struct verify_mirror_id *)cbdata;
9116 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9120 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9121 progname, strerror(errno));
9125 if (mirror_id_cbdata->mirror_id == mirror_id) {
9126 mirror_id_cbdata->is_valid_id = true;
9127 return LLAPI_LAYOUT_ITER_STOP;
9130 return LLAPI_LAYOUT_ITER_CONT;
9134 * verify_mirror_ids() - Verify specified mirror ids.
9135 * @fname: Mirrored file name.
9136 * @mirror_ids: Specified mirror ids to be verified.
9137 * @ids_nr: Number of specified mirror ids.
9139 * This function verifies that specified @mirror_ids are valid
9140 * in the mirrored file @fname.
9142 * Return: 0 on success or a negative error code on failure.
9145 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9147 struct llapi_layout *layout = NULL;
9148 struct verify_mirror_id mirror_id_cbdata = { 0 };
9159 if (stat(fname, &stbuf) < 0) {
9160 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9161 progname, fname, strerror(errno));
9166 if (!S_ISREG(stbuf.st_mode)) {
9167 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9173 fd = open(fname, O_DIRECT | O_RDONLY);
9175 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9176 progname, fname, strerror(errno));
9181 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9183 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9184 progname, fname, strerror(errno));
9188 layout = llapi_layout_get_by_fd(fd, 0);
9189 if (layout == NULL) {
9190 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9191 progname, fname, strerror(errno));
9193 llapi_lease_release(fd);
9197 rc = llapi_layout_flags_get(layout, &flr_state);
9199 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9200 progname, fname, strerror(errno));
9205 flr_state &= LCM_FL_FLR_MASK;
9206 switch (flr_state) {
9209 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9210 progname, fname, llapi_layout_flags_string(flr_state));
9217 for (i = 0; i < ids_nr; i++) {
9218 mirror_id_cbdata.mirror_id = mirror_ids[i];
9219 mirror_id_cbdata.is_valid_id = false;
9221 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9226 "%s: '%s' failed to verify mirror id: %u.\n",
9227 progname, fname, mirror_ids[i]);
9231 if (!mirror_id_cbdata.is_valid_id) {
9234 "%s: '%s' invalid specified mirror id: %u.\n",
9235 progname, fname, mirror_ids[i]);
9241 llapi_layout_free(layout);
9242 llapi_lease_release(fd);
9250 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9251 __u16 *mirror_ids, int ids_nr)
9253 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9254 struct llapi_layout *layout;
9265 if (stat(fname, &stbuf) < 0) {
9266 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9267 progname, fname, strerror(errno));
9271 if (!S_ISREG(stbuf.st_mode)) {
9272 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9278 fd = open(fname, O_DIRECT | O_RDWR);
9280 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9281 progname, fname, strerror(errno));
9286 layout = llapi_layout_get_by_fd(fd, 0);
9287 if (layout == NULL) {
9288 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9289 progname, fname, strerror(errno));
9294 rc = llapi_layout_flags_get(layout, &flr_state);
9296 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9297 progname, fname, strerror(errno));
9302 flr_state &= LCM_FL_FLR_MASK;
9303 if (flr_state == LCM_FL_NONE) {
9305 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9310 /* get stale component info */
9311 comp_size = llapi_mirror_find_stale(layout, comp_array,
9312 ARRAY_SIZE(comp_array),
9313 mirror_ids, ids_nr);
9314 if (comp_size <= 0) {
9319 ioc->lil_mode = LL_LEASE_WRLCK;
9320 ioc->lil_flags = LL_LEASE_RESYNC;
9321 rc = llapi_lease_set(fd, ioc);
9323 if (rc == -EALREADY)
9327 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9328 progname, fname, strerror(-rc));
9332 /* get the read range [start, end) */
9333 start = comp_array[0].lrc_start;
9334 end = comp_array[0].lrc_end;
9335 for (idx = 1; idx < comp_size; idx++) {
9336 if (comp_array[idx].lrc_start < start)
9337 start = comp_array[idx].lrc_start;
9338 if (end < comp_array[idx].lrc_end)
9339 end = comp_array[idx].lrc_end;
9342 rc = llapi_lease_check(fd);
9343 if (rc != LL_LEASE_WRLCK) {
9344 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9349 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9352 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9353 progname, fname, strerror(-rc));
9355 /* need to do the lease unlock even resync fails */
9356 ioc->lil_mode = LL_LEASE_UNLCK;
9357 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9359 for (idx = 0; idx < comp_size; idx++) {
9360 if (comp_array[idx].lrc_synced) {
9361 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9366 rc2 = llapi_lease_set(fd, ioc);
9368 * llapi_lease_set returns lease mode when it request to unlock
9372 /* rc2 == 0 means lost lease lock */
9373 if (rc2 == 0 && rc == 0)
9375 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9377 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9381 llapi_layout_free(layout);
9388 static inline int lfs_mirror_resync(int argc, char **argv)
9390 struct ll_ioc_lease *ioc = NULL;
9391 __u16 mirror_ids[128] = { 0 };
9396 struct option long_opts[] = {
9397 { .val = 'o', .name = "only", .has_arg = required_argument },
9400 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9403 rc = parse_mirror_ids(mirror_ids,
9404 sizeof(mirror_ids) / sizeof(__u16),
9408 "%s: bad mirror ids '%s'.\n",
9415 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9416 argv[0], argv[optind - 1]);
9422 if (argc == optind) {
9423 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9428 if (ids_nr > 0 && argc > optind + 1) {
9430 "%s: option '--only' cannot be used upon multiple files.\n",
9438 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9443 /* set the lease on the file */
9444 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9446 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9447 argv[0], strerror(errno));
9452 for (; optind < argc; optind++) {
9453 rc = lfs_mirror_resync_file(argv[optind], ioc,
9454 mirror_ids, ids_nr);
9455 /* ignore previous file's error, continue with next file */
9458 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9466 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9468 struct llapi_layout *layout;
9471 layout = llapi_layout_get_by_fd(fd, 0);
9472 if (layout == NULL) {
9473 fprintf(stderr, "could not get layout.\n");
9477 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9479 fprintf(stderr, "failed to iterate layout\n");
9480 llapi_layout_free(layout);
9483 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9484 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9485 llapi_layout_free(layout);
9489 llapi_layout_free(layout);
9495 * Check whether two files are the same file
9496 * \retval 0 same file
9497 * \retval 1 not the same file
9498 * \retval <0 error code
9500 static inline int check_same_file(int fd, const char *f2)
9505 if (fstat(fd, &stbuf1) < 0)
9508 if (stat(f2, &stbuf2) < 0)
9511 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9512 stbuf1.st_ino == stbuf2.st_ino)
9518 static inline int lfs_mirror_read(int argc, char **argv)
9521 __u16 mirror_id = 0;
9522 const char *outfile = NULL;
9528 const size_t buflen = 4 << 20;
9530 struct option long_opts[] = {
9531 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9532 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9535 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9540 mirror_id = strtoul(optarg, &end, 0);
9541 if (*end != '\0' || mirror_id == 0) {
9543 "%s %s: invalid mirror ID '%s'\n",
9544 progname, argv[0], optarg);
9552 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9553 progname, argv[optind - 1]);
9558 if (argc == optind) {
9559 fprintf(stderr, "%s %s: no mirrored file provided\n",
9562 } else if (argc > optind + 1) {
9563 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9567 if (mirror_id == 0) {
9568 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9573 /* open mirror file */
9574 fname = argv[optind];
9575 fd = open(fname, O_DIRECT | O_RDONLY);
9577 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9578 progname, argv[0], fname, strerror(errno));
9582 /* verify mirror id */
9583 rc = verify_mirror_id_by_fd(fd, mirror_id);
9586 "%s %s: cannot find mirror with ID %u in '%s'\n",
9587 progname, argv[0], mirror_id, fname);
9591 /* open output file - O_EXCL ensures output is not the same as input */
9593 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9595 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9596 progname, argv[0], outfile, strerror(errno));
9601 outfd = STDOUT_FILENO;
9604 /* allocate buffer */
9605 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9607 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9608 progname, argv[0], rc);
9615 ssize_t written = 0;
9617 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9618 if (bytes_read < 0) {
9621 "%s %s: fail to read data from mirror %u: %s\n",
9622 progname, argv[0], mirror_id, strerror(-rc));
9627 if (bytes_read == 0)
9630 while (written < bytes_read) {
9633 written2 = write(outfd, buf + written,
9634 bytes_read - written);
9637 "%s %s: fail to write %s: %s\n",
9638 progname, argv[0], outfile ? : "STDOUT",
9643 written += written2;
9646 if (written != bytes_read) {
9648 "%s %s: written %ld bytes does not match with %ld read.\n",
9649 progname, argv[0], written, bytes_read);
9671 static inline int lfs_mirror_write(int argc, char **argv)
9674 __u16 mirror_id = 0;
9675 const char *inputfile = NULL;
9681 const size_t buflen = 4 << 20;
9683 size_t page_size = sysconf(_SC_PAGESIZE);
9684 struct ll_ioc_lease_id ioc;
9686 struct option long_opts[] = {
9687 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9688 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9691 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9696 mirror_id = strtoul(optarg, &end, 0);
9697 if (*end != '\0' || mirror_id == 0) {
9699 "%s %s: invalid mirror ID '%s'\n",
9700 progname, argv[0], optarg);
9708 fprintf(stderr, "%s: option '%s' unrecognized\n",
9709 progname, argv[optind - 1]);
9714 if (argc == optind) {
9715 fprintf(stderr, "%s %s: no mirrored file provided\n",
9718 } else if (argc > optind + 1) {
9719 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9723 if (mirror_id == 0) {
9724 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9729 /* open mirror file */
9730 fname = argv[optind];
9731 fd = open(fname, O_DIRECT | O_WRONLY);
9733 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9734 progname, argv[0], fname, strerror(errno));
9738 /* verify mirror id */
9739 rc = verify_mirror_id_by_fd(fd, mirror_id);
9742 "%s %s: cannot find mirror with ID %u in '%s'\n",
9743 progname, argv[0], mirror_id, fname);
9747 /* open input file */
9749 rc = check_same_file(fd, inputfile);
9752 "%s %s: input file cannot be the mirrored file\n",
9759 inputfd = open(inputfile, O_RDONLY, 0644);
9761 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9762 progname, argv[0], inputfile, strerror(errno));
9767 inputfd = STDIN_FILENO;
9770 /* allocate buffer */
9771 rc = posix_memalign(&buf, page_size, buflen);
9773 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9774 progname, argv[0], rc);
9778 /* prepare target mirror components instantiation */
9779 ioc.lil_mode = LL_LEASE_WRLCK;
9780 ioc.lil_flags = LL_LEASE_RESYNC;
9781 ioc.lil_mirror_id = mirror_id;
9782 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9785 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9786 progname, argv[0], fname, strerror(errno));
9796 rc = llapi_lease_check(fd);
9797 if (rc != LL_LEASE_WRLCK) {
9798 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9799 progname, argv[0], fname);
9803 bytes_read = read(inputfd, buf, buflen);
9804 if (bytes_read < 0) {
9807 "%s %s: fail to read data from '%s': %s\n",
9808 progname, argv[0], inputfile ? : "STDIN",
9815 if (bytes_read == 0)
9818 /* round up to page align to make direct IO happy. */
9819 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9821 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9826 "%s %s: fail to write to mirror %u: %s\n",
9827 progname, argv[0], mirror_id,
9835 if (pos & (page_size - 1)) {
9836 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9841 ioc.lil_mode = LL_LEASE_UNLCK;
9842 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9844 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9849 "%s %s: release lease lock of '%s' failed: %s\n",
9850 progname, argv[0], fname, strerror(errno));
9867 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9869 struct llapi_layout *layout;
9870 struct collect_ids_data cid = { .cid_ids = ids,
9872 .cid_exclude = exclude_id, };
9875 layout = llapi_layout_get_by_fd(fd, 0);
9876 if (layout == NULL) {
9877 fprintf(stderr, "could not get layout\n");
9881 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9883 fprintf(stderr, "failed to iterate layout\n");
9884 llapi_layout_free(layout);
9888 llapi_layout_free(layout);
9890 return cid.cid_count;
9893 static inline int lfs_mirror_copy(int argc, char **argv)
9896 __u16 read_mirror_id = 0;
9897 __u16 ids[128] = { 0 };
9899 struct llapi_layout *layout = NULL;
9900 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9907 struct ll_ioc_lease *ioc = NULL;
9908 struct ll_ioc_lease_id *resync_ioc;
9910 struct option long_opts[] = {
9911 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9912 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9915 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9920 read_mirror_id = strtoul(optarg, &end, 0);
9921 if (*end != '\0' || read_mirror_id == 0) {
9923 "%s %s: invalid read mirror ID '%s'\n",
9924 progname, argv[0], optarg);
9929 if (!strcmp(optarg, "-1")) {
9930 /* specify all other mirrors */
9934 count = parse_mirror_ids((__u16 *)ids,
9942 fprintf(stderr, "%s: option '%s' unrecognized\n",
9943 progname, argv[optind - 1]);
9948 if (argc == optind) {
9949 fprintf(stderr, "%s %s: no mirrored file provided\n",
9952 } else if (argc > optind + 1) {
9953 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9957 if (read_mirror_id == 0) {
9959 "%s %s: no valid read mirror ID %d is provided\n",
9960 progname, argv[0], read_mirror_id);
9966 "%s %s: no write mirror ID is provided\n",
9971 for (i = 0; i < count; i++) {
9972 if (read_mirror_id == ids[i]) {
9974 "%s %s: read and write mirror ID cannot be the same\n",
9980 /* open mirror file */
9981 fname = argv[optind];
9983 fd = open(fname, O_DIRECT | O_RDWR);
9985 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9986 progname, argv[0], fname, strerror(errno));
9990 /* write to all other mirrors */
9991 if (ids[0] == (__u16)-1) {
9992 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9996 "%s %s: failed to get other mirror ids in '%s': %d\n",
9997 progname, argv[0], fname, rc);
10002 /* verify mirror id */
10003 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
10006 "%s %s: cannot find mirror with ID %u in '%s'\n",
10007 progname, argv[0], read_mirror_id, fname);
10011 for (i = 0; i < count; i++) {
10012 rc = verify_mirror_id_by_fd(fd, ids[i]);
10015 "%s %s: cannot find mirror with ID %u in '%s'\n",
10016 progname, argv[0], ids[i], fname);
10021 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10024 "%s %s: cannot alloc comp id array for ioc: %s\n",
10025 progname, argv[0], strerror(errno));
10030 /* get stale component info */
10031 layout = llapi_layout_get_by_fd(fd, 0);
10032 if (layout == NULL) {
10033 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10034 progname, argv[0], fname, strerror(errno));
10038 comp_size = llapi_mirror_find_stale(layout, comp_array,
10039 ARRAY_SIZE(comp_array),
10041 llapi_layout_free(layout);
10042 if (comp_size < 0) {
10047 /* prepare target mirror components instantiation */
10048 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10049 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10050 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10052 resync_ioc->lil_mirror_id = ids[0];
10054 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10055 rc = llapi_lease_set(fd, ioc);
10058 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10059 progname, argv[0], fname, strerror(errno));
10063 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10066 fprintf(stderr, "%s %s: copy error: %d\n",
10067 progname, argv[0], rc);
10071 fprintf(stdout, "mirror copied successfully: ");
10072 for (i = 0; i < copied; i++)
10073 fprintf(stdout, "%d ", ids[i]);
10074 fprintf(stdout, "\n");
10076 ioc->lil_mode = LL_LEASE_UNLCK;
10077 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10078 ioc->lil_count = 0;
10079 for (i = 0; i < comp_size; i++) {
10082 for (j = 0; j < copied; j++) {
10083 if (comp_array[i].lrc_mirror_id != ids[j])
10086 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10090 rc = llapi_lease_set(fd, ioc);
10095 "%s %s: release lease lock of '%s' failed: %s\n",
10096 progname, argv[0], fname, strerror(errno));
10110 * struct verify_chunk - Mirror chunk to be verified.
10111 * @chunk: [start, end) of the chunk.
10112 * @mirror_count: Number of mirror ids in @mirror_id array.
10113 * @mirror_id: Array of valid mirror ids that cover the chunk.
10115 struct verify_chunk {
10116 struct lu_extent chunk;
10117 unsigned int mirror_count;
10118 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10122 * print_chunks() - Print chunk information.
10123 * @fname: Mirrored file name.
10124 * @chunks: Array of chunks.
10125 * @chunk_count: Number of chunks in @chunks array.
10127 * This function prints [start, end) of each chunk in @chunks
10128 * for mirrored file @fname, and also prints the valid mirror ids
10129 * that cover the chunk.
10134 void print_chunks(const char *fname, struct verify_chunk *chunks,
10140 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10141 for (i = 0; i < chunk_count; i++) {
10142 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10144 if (chunks[i].mirror_count == 0)
10145 fprintf(stdout, "\t[");
10147 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10148 for (j = 1; j < chunks[i].mirror_count; j++)
10149 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10151 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10153 fprintf(stdout, "\n");
10157 * print_checksums() - Print CRC-32 checksum values.
10158 * @chunk: A chunk and its corresponding valid mirror ids.
10159 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10161 * This function prints CRC-32 checksum values on @chunk for
10162 * each valid mirror that covers it.
10167 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10172 "CRC-32 checksum value for chunk "DEXT":\n",
10173 PEXT(&chunk->chunk));
10174 for (i = 0; i < chunk->mirror_count; i++)
10175 fprintf(stdout, "Mirror %u:\t%#lx\n",
10176 chunk->mirror_id[i], crc[i]);
10177 fprintf(stdout, "\n");
10181 * filter_mirror_id() - Filter specified mirror ids.
10182 * @chunks: Array of chunks.
10183 * @chunk_count: Number of chunks in @chunks array.
10184 * @mirror_ids: Specified mirror ids to be verified.
10185 * @ids_nr: Number of specified mirror ids.
10187 * This function scans valid mirror ids that cover each chunk in @chunks
10188 * and filters specified mirror ids.
10193 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10194 __u16 *mirror_ids, int ids_nr)
10199 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10200 unsigned int valid_count = 0;
10202 for (i = 0; i < chunk_count; i++) {
10203 if (chunks[i].mirror_count == 0)
10207 for (j = 0; j < ids_nr; j++) {
10208 for (k = 0; k < chunks[i].mirror_count; k++) {
10209 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10210 valid_id[valid_count] = mirror_ids[j];
10217 memcpy(chunks[i].mirror_id, valid_id,
10218 sizeof(__u16) * valid_count);
10219 chunks[i].mirror_count = valid_count;
10224 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10225 * @layout: Mirror component list.
10226 * @chunks: Array of chunks.
10227 * @chunks_size: Array size of @chunks.
10229 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10230 * to find out chunk segments and store them in @chunks array.
10232 * The @mirror_id array in each element of @chunks will store the valid
10233 * mirror ids that cover the chunk. If a mirror component covering the
10234 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10235 * will not be stored into the @mirror_id array, and the chunk for that
10236 * mirror will not be verified.
10238 * The @mirror_count in each element of @chunks will store the number of
10239 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10240 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10241 * indicates the chunk is valid in only one mirror. In both cases, the
10242 * chunk will not be verified.
10244 * Here is an example:
10246 * 0 1M 2M 3M 4M EOF
10247 * +------+-------------+--------------------+
10248 * | | | S | mirror1
10249 * +------+------+------+------+-------------+
10250 * | | S | S | | mirror2
10251 * +-------------+------+------+-------------+
10253 * prepared @chunks array will contain 5 elements:
10254 * (([0, 1M), [1, 2], 2),
10255 * ([1M, 2M), [1, 2], 2),
10256 * ([2M, 3M), [1], 1),
10257 * ([3M, 4M], [], 0),
10258 * ([4M, EOF), [2], 1))
10260 * Return: the actual array size of @chunks on success
10261 * or a negative error code on failure.
10264 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10265 struct verify_chunk *chunks,
10266 size_t chunks_size)
10270 uint32_t mirror_id;
10276 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10279 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10282 "%s: move to the first layout component: %s.\n",
10283 progname, strerror(errno));
10289 chunks[idx].chunk.e_end = LUSTRE_EOF;
10291 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10294 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10295 progname, strerror(errno));
10299 if (start > chunks[idx].chunk.e_start ||
10300 end <= chunks[idx].chunk.e_start)
10303 if (end < chunks[idx].chunk.e_end)
10304 chunks[idx].chunk.e_end = end;
10306 rc = llapi_layout_comp_flags_get(layout, &flags);
10309 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10310 progname, strerror(errno));
10314 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10317 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10320 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10321 progname, strerror(errno));
10325 chunks[idx].mirror_id[i] = mirror_id;
10327 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10329 "%s: mirror_id array is too small.\n",
10336 rc = llapi_layout_comp_use(layout,
10337 LLAPI_LAYOUT_COMP_USE_NEXT);
10340 "%s: move to the next layout component: %s.\n",
10341 progname, strerror(errno));
10344 } /* loop through all components */
10346 chunks[idx].mirror_count = i;
10348 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10352 if (idx >= chunks_size) {
10353 fprintf(stderr, "%s: chunks array is too small.\n",
10359 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10363 return rc < 0 ? rc : idx + 1;
10367 * lfs_mirror_verify_chunk() - Verify a chunk.
10368 * @fd: File descriptor of the mirrored file.
10369 * @file_size: Size of the mirrored file.
10370 * @chunk: A chunk and its corresponding valid mirror ids.
10371 * @verbose: Verbose mode.
10373 * This function verifies a @chunk contains exactly the same data
10374 * ammong the mirrors that cover it.
10376 * If @verbose is specified, then the function will print where the
10377 * differences are if the data do not match. Otherwise, it will
10378 * just return an error in that case.
10380 * Return: 0 on success or a negative error code on failure.
10383 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10384 struct verify_chunk *chunk, int verbose)
10386 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10388 size_t page_size = sysconf(_SC_PAGESIZE);
10389 ssize_t bytes_read;
10390 ssize_t bytes_done;
10394 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10398 if (file_size == 0)
10401 rc = posix_memalign(&buf, page_size, buflen);
10402 if (rc) /* error code is returned directly */
10406 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10407 PEXT(&chunk->chunk));
10408 for (i = 0; i < chunk->mirror_count; i++)
10409 fprintf(stdout, " %u", chunk->mirror_id[i]);
10410 fprintf(stdout, "\n");
10414 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10415 pos = chunk->chunk.e_start;
10416 while (bytes_done < count) {
10417 /* compute initial CRC-32 checksum */
10418 crc = crc32(0L, Z_NULL, 0);
10419 memset(crc_array, 0, sizeof(crc_array));
10422 for (i = 0; i < chunk->mirror_count; i++) {
10423 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10425 if (bytes_read < 0) {
10428 "%s: failed to read data from mirror %u: %s.\n",
10429 progname, chunk->mirror_id[i],
10434 /* compute new CRC-32 checksum */
10435 crc_array[i] = crc32(crc, buf, bytes_read);
10439 print_checksums(chunk, crc_array);
10441 /* compare CRC-32 checksum values */
10442 for (i = 1; i < chunk->mirror_count; i++) {
10443 if (crc_array[i] != crc_array[0]) {
10447 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10448 progname, PEXT(&chunk->chunk),
10449 chunk->mirror_id[0],
10450 chunk->mirror_id[i]);
10455 bytes_done += bytes_read;
10458 if (verbose > 1 && rc == 0) {
10459 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10460 PEXT(&chunk->chunk));
10461 for (i = 0; i < chunk->mirror_count; i++)
10462 fprintf(stdout, " %u", chunk->mirror_id[i]);
10463 fprintf(stdout, " PASS\n\n");
10472 * lfs_mirror_verify_file() - Verify a mirrored file.
10473 * @fname: Mirrored file name.
10474 * @mirror_ids: Specified mirror ids to be verified.
10475 * @ids_nr: Number of specified mirror ids.
10476 * @verbose: Verbose mode.
10478 * This function verifies that each SYNC mirror of a mirrored file
10479 * specified by @fname contains exactly the same data.
10481 * If @mirror_ids is specified, then the function will verify the
10482 * mirrors specified by @mirror_ids contain exactly the same data.
10484 * If @verbose is specified, then the function will print where the
10485 * differences are if the data do not match. Otherwise, it will
10486 * just return an error in that case.
10488 * Return: 0 on success or a negative error code on failure.
10491 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10494 struct verify_chunk chunks_array[1024] = { };
10495 struct llapi_layout *layout = NULL;
10497 uint32_t flr_state;
10499 int chunk_count = 0;
10505 if (stat(fname, &stbuf) < 0) {
10506 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10507 progname, fname, strerror(errno));
10512 if (!S_ISREG(stbuf.st_mode)) {
10513 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10519 if (stbuf.st_size == 0) {
10521 fprintf(stdout, "%s: '%s' file size is 0.\n",
10527 fd = open(fname, O_DIRECT | O_RDONLY);
10529 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10530 progname, fname, strerror(errno));
10535 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10537 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10538 progname, fname, strerror(errno));
10542 layout = llapi_layout_get_by_fd(fd, 0);
10543 if (layout == NULL) {
10544 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10545 progname, fname, strerror(errno));
10547 llapi_lease_release(fd);
10551 rc = llapi_layout_flags_get(layout, &flr_state);
10553 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10554 progname, fname, strerror(errno));
10559 flr_state &= LCM_FL_FLR_MASK;
10560 switch (flr_state) {
10563 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10564 progname, fname, llapi_layout_flags_string(flr_state));
10570 /* find out mirror chunks to be verified */
10571 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10572 ARRAY_SIZE(chunks_array));
10573 if (chunk_count < 0) {
10579 /* filter specified mirror ids */
10580 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10583 print_chunks(fname, chunks_array, chunk_count);
10585 for (idx = 0; idx < chunk_count; idx++) {
10586 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10589 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10591 PEXT(&chunks_array[idx].chunk),
10592 (unsigned long long)stbuf.st_size);
10596 if (chunks_array[idx].mirror_count == 0) {
10598 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10600 PEXT(&chunks_array[idx].chunk));
10602 fprintf(stderr, "skipped\n");
10606 fprintf(stderr, "failed\n");
10610 if (chunks_array[idx].mirror_count == 1) {
10613 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10615 PEXT(&chunks_array[idx].chunk),
10616 chunks_array[idx].mirror_id[0]);
10620 rc = llapi_lease_check(fd);
10621 if (rc != LL_LEASE_RDLCK) {
10622 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10627 /* verify one chunk */
10628 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10629 &chunks_array[idx], verbose);
10643 llapi_layout_free(layout);
10644 llapi_lease_release(fd);
10652 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10653 * @argc: The count of lfs mirror verify command line arguments.
10654 * @argv: Array of strings for lfs mirror verify command line arguments.
10656 * This function parses lfs mirror verify command and verifies the
10657 * specified mirrored file(s).
10659 * Return: 0 on success or a negative error code on failure.
10661 static inline int lfs_mirror_verify(int argc, char **argv)
10663 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10669 char cmd[PATH_MAX];
10671 struct option long_opts[] = {
10672 { .val = 'o', .name = "only", .has_arg = required_argument },
10673 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10674 { .name = NULL } };
10676 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10678 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10681 rc = parse_mirror_ids(mirror_ids,
10682 ARRAY_SIZE(mirror_ids),
10686 "%s: bad mirror ids '%s'.\n",
10693 "%s: at least 2 mirror ids needed with '--only' option.\n",
10703 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10704 progname, argv[optind - 1]);
10710 if (argc == optind) {
10711 fprintf(stderr, "%s: no file name given.\n", progname);
10716 if (ids_nr > 0 && argc > optind + 1) {
10718 "%s: '--only' cannot be used upon multiple files.\n",
10726 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10732 for (; optind < argc; optind++) {
10733 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10743 * lfs_mirror() - Parse and execute lfs mirror commands.
10744 * @argc: The count of lfs mirror command line arguments.
10745 * @argv: Array of strings for lfs mirror command line arguments.
10747 * This function parses lfs mirror commands and performs the
10748 * corresponding functions specified in mirror_cmdlist[].
10750 * Return: 0 on success or an error code on failure.
10752 static int lfs_mirror(int argc, char **argv)
10754 char cmd[PATH_MAX];
10757 setlinebuf(stdout);
10759 Parser_init("lfs-mirror > ", mirror_cmdlist);
10761 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10763 program_invocation_short_name = cmd;
10765 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10767 rc = Parser_commands();
10769 return rc < 0 ? -rc : rc;
10772 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10774 #if __BYTE_ORDER == __BIG_ENDIAN
10775 __swab16s(&attrs->lsa_valid);
10776 __swab64s(&attrs->lsa_size);
10777 __swab64s(&attrs->lsa_blocks);
10781 enum lfs_som_type {
10782 LFS_SOM_SIZE = 0x1,
10783 LFS_SOM_BLOCKS = 0x2,
10784 LFS_SOM_FLAGS = 0x4,
10785 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10789 static int lfs_getsom(int argc, char **argv)
10792 struct lustre_som_attrs *attrs;
10793 char buf[sizeof(*attrs) + 64];
10794 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10797 while ((c = getopt(argc, argv, "sbf")) != -1) {
10800 type = LFS_SOM_SIZE;
10803 type = LFS_SOM_BLOCKS;
10806 type = LFS_SOM_FLAGS;
10809 fprintf(stderr, "%s: invalid option '%c'\n",
10819 fprintf(stderr, "%s: %s\n",
10820 progname, argc == 0 ? "miss file target" :
10821 "input more than 2 files");
10826 attrs = (void *)buf;
10827 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10830 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10831 argv[0], strerror(errno), errno);
10835 lustre_som_swab(attrs);
10838 case LFS_SOM_ATTR_ALL:
10839 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10840 path, (unsigned long long)attrs->lsa_size,
10841 (unsigned long long)attrs->lsa_blocks,
10845 printf("%llu\n", (unsigned long long)attrs->lsa_size);
10847 case LFS_SOM_BLOCKS:
10848 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
10850 case LFS_SOM_FLAGS:
10851 printf("%x\n", attrs->lsa_valid);
10854 fprintf(stderr, "%s: unknown option\n", progname);
10862 * lfs_mirror_list_commands() - List lfs mirror commands.
10863 * @argc: The count of command line arguments.
10864 * @argv: Array of strings for command line arguments.
10866 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10868 * Return: 0 on success.
10870 static int lfs_mirror_list_commands(int argc, char **argv)
10872 char buffer[81] = "";
10874 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10880 static int lfs_pcc_attach(int argc, char **argv)
10882 struct option long_opts[] = {
10883 { .val = 'i', .name = "id", .has_arg = required_argument },
10884 { .name = NULL } };
10887 __u32 archive_id = 0;
10890 char fullpath[PATH_MAX];
10891 enum lu_pcc_type type = LU_PCC_READWRITE;
10894 while ((c = getopt_long(argc, argv, "i:",
10895 long_opts, NULL)) != -1) {
10898 archive_id = strtoul(optarg, &end, 0);
10899 if (*end != '\0' || archive_id == 0) {
10900 fprintf(stderr, "error: %s: bad archive ID "
10901 "'%s'\n", argv[0], optarg);
10908 fprintf(stderr, "%s: option '%s' unrecognized\n",
10909 argv[0], argv[optind - 1]);
10914 if (archive_id == 0) {
10915 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
10919 if (argc <= optind) {
10920 fprintf(stderr, "%s: must specify one or more file names\n",
10925 while (optind < argc) {
10928 path = argv[optind++];
10929 if (realpath(path, fullpath) == NULL) {
10930 fprintf(stderr, "%s: could not find path '%s': %s\n",
10931 argv[0], path, strerror(errno));
10937 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10939 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10940 "with archive ID '%u': %s\n", argv[0],
10941 path, archive_id, strerror(-rc2));
10949 static int lfs_pcc_attach_fid(int argc, char **argv)
10951 struct option long_opts[] = {
10952 { .val = 'i', .name = "id", .has_arg = required_argument },
10953 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10954 { .name = NULL } };
10955 char short_opts[] = "i:m:";
10958 __u32 archive_id = 0;
10960 const char *mntpath = NULL;
10961 const char *fidstr;
10962 enum lu_pcc_type type = LU_PCC_READWRITE;
10965 while ((c = getopt_long(argc, argv, short_opts,
10966 long_opts, NULL)) != -1) {
10969 archive_id = strtoul(optarg, &end, 0);
10970 if (*end != '\0') {
10971 fprintf(stderr, "error: %s: bad archive ID "
10972 "'%s'\n", argv[0], optarg);
10982 fprintf(stderr, "%s: option '%s' unrecognized\n",
10983 argv[0], argv[optind - 1]);
10988 if (archive_id == 0) {
10989 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10993 if (mntpath == NULL) {
10994 fprintf(stderr, "%s: must specify Lustre mount point\n",
10999 if (argc <= optind) {
11000 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
11004 while (optind < argc) {
11007 fidstr = argv[optind++];
11009 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
11012 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
11013 "with archive ID '%u': %s\n", argv[0],
11014 fidstr, mntpath, archive_id, strerror(rc2));
11016 if (rc == 0 && rc2 < 0)
11022 static int lfs_pcc_detach(int argc, char **argv)
11024 struct option long_opts[] = {
11025 { .val = 'k', .name = "keep", .has_arg = no_argument },
11026 { .name = NULL } };
11027 char short_opts[] = "k";
11031 char fullpath[PATH_MAX];
11032 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11035 while ((c = getopt_long(argc, argv, short_opts,
11036 long_opts, NULL)) != -1) {
11039 detach_opt = PCC_DETACH_OPT_NONE;
11044 fprintf(stderr, "%s: option '%s' unrecognized\n",
11045 argv[0], argv[optind - 1]);
11050 while (optind < argc) {
11053 path = argv[optind++];
11054 if (realpath(path, fullpath) == NULL) {
11055 fprintf(stderr, "%s: could not find path '%s': %s\n",
11056 argv[0], path, strerror(errno));
11062 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11065 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
11066 "%s\n", argv[0], path, strerror(errno));
11074 static int lfs_pcc_detach_fid(int argc, char **argv)
11076 struct option long_opts[] = {
11077 { .val = 'k', .name = "keep", .has_arg = no_argument },
11078 { .name = NULL } };
11079 char short_opts[] = "k";
11083 const char *mntpath;
11084 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11087 while ((c = getopt_long(argc, argv, short_opts,
11088 long_opts, NULL)) != -1) {
11091 detach_opt = PCC_DETACH_OPT_NONE;
11096 fprintf(stderr, "%s: option '%s' unrecognized\n",
11097 argv[0], argv[optind - 1]);
11102 mntpath = argv[optind++];
11104 while (optind < argc) {
11107 fid = argv[optind++];
11109 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11111 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
11112 "from PCC: %s\n", argv[0], fid, mntpath,
11121 static int lfs_pcc_state(int argc, char **argv)
11125 char fullpath[PATH_MAX];
11126 struct lu_pcc_state state;
11131 fprintf(stderr, "%s: must specify one or more file names\n",
11136 while (optind < argc) {
11139 path = argv[optind++];
11140 if (realpath(path, fullpath) == NULL) {
11141 fprintf(stderr, "%s: could not find path '%s': %s\n",
11142 argv[0], path, strerror(errno));
11148 rc2 = llapi_pcc_state_get(fullpath, &state);
11152 fprintf(stderr, "%s: cannot get PCC state of '%s': "
11153 "%s\n", argv[0], path, strerror(-rc2));
11157 printf("file: %s", path);
11158 printf(", type: %s", pcc_type2string(state.pccs_type));
11159 if (state.pccs_type == LU_PCC_NONE &&
11160 state.pccs_open_count == 0) {
11165 printf(", PCC file: %s", state.pccs_path);
11166 printf(", user number: %u", state.pccs_open_count);
11167 printf(", flags: %x", state.pccs_flags);
11174 * lfs_pcc_list_commands() - List lfs pcc commands.
11175 * @argc: The count of command line arguments.
11176 * @argv: Array of strings for command line arguments.
11178 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11180 * Return: 0 on success.
11182 static int lfs_pcc_list_commands(int argc, char **argv)
11184 char buffer[81] = "";
11186 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11193 * lfs_pcc() - Parse and execute lfs pcc commands.
11194 * @argc: The count of lfs pcc command line arguments.
11195 * @argv: Array of strings for lfs pcc command line arguments.
11197 * This function parses lfs pcc commands and performs the
11198 * corresponding functions specified in pcc_cmdlist[].
11200 * Return: 0 on success or an error code on failure.
11202 static int lfs_pcc(int argc, char **argv)
11204 char cmd[PATH_MAX];
11207 setlinebuf(stdout);
11209 Parser_init("lfs-pcc > ", pcc_cmdlist);
11211 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11213 program_invocation_short_name = cmd;
11215 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11217 rc = Parser_commands();
11219 return rc < 0 ? -rc : rc;
11222 static int lfs_list_commands(int argc, char **argv)
11224 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11226 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11231 int main(int argc, char **argv)
11235 /* Ensure that liblustreapi constructor has run */
11236 if (!llapi_liblustreapi_initialized())
11237 fprintf(stderr, "liblustreapi was not properly initialized\n");
11239 setlinebuf(stdout);
11242 Parser_init("lfs > ", cmdlist);
11244 progname = program_invocation_short_name; /* Used in error messages */
11246 llapi_set_command_name(argv[1]);
11247 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11248 llapi_clear_command_name();
11250 rc = Parser_commands();
11253 return rc < 0 ? -rc : rc;
11256 #ifdef _LUSTRE_IDL_H_
11257 /* Everything we need here should be included by lustreapi.h. */
11258 # error "lfs should not depend on lustre_idl.h"
11259 #endif /* _LUSTRE_IDL_H_ */