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>
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 " fnv_1a_64 FNV-1a hash algorithm (default)\n" \
280 " all_char sum of characters % MDT_COUNT (not recommended)\n" \
281 " space create subdirectories with balanced space usage\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|-m 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 "To create a striped directory on a specified MDT. This can only\n"
433 "be done on MDT0 with the right of administrator.\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 "To create a striped directory on a specified MDT. This can only\n"
445 "be done on MDT0 with the right of administrator.\n"
446 "usage: mkdir [OPTION] <directory>\n"
448 {"rm_entry", lfs_rmentry, 0,
449 "To remove the name entry of the remote directory. Note: This\n"
450 "command will only delete the name entry, i.e. the remote directory\n"
451 "will become inaccessable after this command. This can only be done\n"
452 "by the administrator\n"
453 "usage: rm_entry <dir>\n"},
454 {"pool_list", lfs_poollist, 0,
455 "List pools or pool OSTs\n"
456 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
457 {"find", lfs_find, 0,
458 "find files matching given attributes recursively in directory tree.\n"
459 "usage: find <directory|filename> ...\n"
460 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
461 " [[!] --mtime|-M [+-]N[smhdwy]] [[!] --blocks|-b N]\n"
462 " [[!] --newer[XY] <reference>]\n"
463 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
464 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
465 " [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
466 " [[!] --stripe-count|-c [+-]<stripes>]\n"
467 " [[!] --stripe-index|-i <index,...>]\n"
468 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
469 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
470 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
471 " [[!] --projid <projid>]\n"
472 " [[!] --foreign[=<foreign_type>]]\n"
473 " [[!] --layout|-L released,raid0,mdt]\n"
474 " [[!] --foreign[=<foreign_type>]]\n"
475 " [[!] --component-count [+-]<comp_cnt>]\n"
476 " [[!] --component-start [+-]N[kMGTPE]]\n"
477 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
478 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
479 " [[!] --mirror-count|-N [+-]<n>]\n"
480 " [[!] --mirror-state <[^]state>]\n"
481 " [[!] --mdt-count|-T [+-]<stripes>]\n"
482 " [[!] --mdt-hash|-H <hashtype>\n"
483 " [[!] --mdt-index|-m <uuid|index,...>]\n"
484 "\t !: used before an option indicates 'NOT' requested attribute\n"
485 "\t -: used before a value indicates less than requested value\n"
486 "\t +: used before a value indicates more than requested value\n"
487 "\thashtype: hash type of the striped directory.\n"
488 "\t fnv_1a_64 FNV-1a hash algorithm\n"
489 "\t all_char sum of characters % MDT_COUNT\n"},
490 {"check", lfs_check, 0,
491 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
492 "or all the servers (MGTs, MDTs and OSTs).\n"
493 "usage: check <mgts|osts|mdts|all>"},
494 {"osts", lfs_osts, 0, "list OSTs connected to client "
495 "[for specified path only]\n" "usage: osts [path]"},
496 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
497 "[for specified path only]\n" "usage: mdts [path]"},
499 "report filesystem disk space usage or inodes usage "
500 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
501 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
502 " [--pool|-p <fsname>[.<pool>]] [path]"},
503 {"getname", lfs_getname, 0,
504 "list instances and specified mount points [for specified path only]\n"
505 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
506 #ifdef HAVE_SYS_QUOTA_H
507 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
508 "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
509 " -b <block-softlimit> -B <block-hardlimit>\n"
510 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
511 " setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
512 " [--block-softlimit <block-softlimit>]\n"
513 " [--block-hardlimit <block-hardlimit>]\n"
514 " [--inode-softlimit <inode-softlimit>]\n"
515 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
516 " setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
517 " [--block-grace 'notify'|<block-grace>]\n"
518 " [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
519 " setquota <-U|-G|-P>\n"
520 " -b <block-softlimit> -B <block-hardlimit>\n"
521 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
522 " setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
523 " [--block-softlimit <block-softlimit>]\n"
524 " [--block-hardlimit <block-hardlimit>]\n"
525 " [--inode-softlimit <inode-softlimit>]\n"
526 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
527 " setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
529 " -b can be used instead of --block-softlimit/--block-grace\n"
530 " -B can be used instead of --block-hardlimit\n"
531 " -i can be used instead of --inode-softlimit/--inode-grace\n"
532 " -I can be used instead of --inode-hardlimit\n"
533 " -d can be used instead of --default\n\n"
534 "Note: The total quota space will be split into many qunits and\n"
535 " balanced over all server targets, the minimal qunit size is\n"
536 " 1M bytes for block space and 1K inodes for inode space.\n\n"
537 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
538 " Quota space rebalancing process will stop when this mininum\n"
539 " value is reached. As a result, quota exceeded can be returned\n"
540 " while many targets still have 1MB or 1K inodes of spare\n"
542 " When setting the grace time, 'notify' can be used as grace to\n"
543 " be notified after the quota is over soft limit but prevents\n"
544 " the soft limit from becoming the hard limit."},
545 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
546 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
548 " [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
549 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
550 " quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
551 {"project", lfs_project, 0,
552 "Change or list project attribute for specified file or directory.\n"
553 "usage: project [-d|-r] <file|directory...>\n"
554 " list project ID and flags on file(s) or directories\n"
555 " project [-p id] [-s] [-r] <file|directory...>\n"
556 " set project ID and/or inherit flag for specified file(s) or directories\n"
557 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
558 " check project ID and flags on file(s) or directories, print outliers\n"
559 " project -C [-r] [-k] <file|directory...>\n"
560 " clear the project inherit flag and ID on the file or directory\n"
563 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
564 "usage: flushctx [-k] [mountpoint...]"},
565 {"changelog", lfs_changelog, 0,
566 "Show the metadata changes on an MDT."
567 "\nusage: changelog <mdtname> [startrec [endrec]]"},
568 {"changelog_clear", lfs_changelog_clear, 0,
569 "Indicate that old changelog records up to <endrec> are no longer of "
570 "interest to consumer <id>, allowing the system to free up space.\n"
571 "An <endrec> of 0 means all records.\n"
572 "usage: changelog_clear <mdtname> <id> <endrec>"},
573 {"fid2path", lfs_fid2path, 0,
574 "Resolve the full path(s) for given FID(s). For a specific hardlink "
575 "specify link number <linkno>.\n"
576 "usage: fid2path [-c] [--link|-l <linkno>] <fsname|root> <fid> ..."},
577 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
578 "usage: path2fid [--parents] <path> ..."},
579 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
580 "usage: rmfid <fsname|rootpath> <fid> ..."},
581 {"data_version", lfs_data_version, 0, "Display file data version for "
582 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
583 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
584 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
585 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
586 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
587 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
588 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
590 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
591 "[--archived] [--lost] <file> ..."},
592 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
593 "given files.\n" "usage: hsm_action <file> ..."},
594 {"hsm_archive", lfs_hsm_archive, 0,
595 "Archive file to external storage.\n"
596 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
598 {"hsm_restore", lfs_hsm_restore, 0,
599 "Restore file from external storage.\n"
600 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
601 {"hsm_release", lfs_hsm_release, 0,
602 "Release files from Lustre.\n"
603 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
604 {"hsm_remove", lfs_hsm_remove, 0,
605 "Remove file copy from external storage.\n"
606 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
608 " (FILE [FILE ...] | "
609 "--mntpath MOUNTPATH FID [FID ...])\n"
611 "Note: To remove an archived copy of a file already deleted from a "
613 "--mntpath option and a list of FIDs must be specified"
615 {"hsm_cancel", lfs_hsm_cancel, 0,
616 "Cancel requests related to specified files.\n"
617 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
618 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
619 "usage: swap_layouts <path1> <path2>"},
620 {"migrate", lfs_setstripe_migrate, 0,
621 "migrate a directory between MDTs.\n"
622 "usage: migrate [--mdt-count|-c] <stripe_count>\n"
623 " [--mdt-hash|-H] <hash_type>\n"
624 " [--mdt-index|-m] <start_mdt_index>\n"
627 "\tmdt: MDTs to stripe over, if only one MDT is specified\n"
628 " it's the MDT index of first stripe\n"
629 "\tmdt_count: number of MDTs to stripe a directory over\n"
630 "\tmdt_hash: hash type of the striped directory. mdt types:\n"
631 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
632 " all_char sum of characters % MDT_COUNT\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)
716 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
717 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
723 static uint32_t check_foreign_type_name(const char *foreign_type_name)
727 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
728 if (lu_foreign_types[i].lft_name == NULL)
730 if (strcmp(foreign_type_name,
731 lu_foreign_types[i].lft_name) == 0)
732 return lu_foreign_types[i].lft_type;
735 return LU_FOREIGN_TYPE_UNKNOWN;
738 static const char *error_loc = "syserror";
741 MIGRATION_NONBLOCK = 0x0001,
742 MIGRATION_MIRROR = 0x0002,
743 MIGRATION_NONDIRECT = 0x0004,
744 MIGRATION_VERBOSE = 0x0008,
747 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
748 struct llapi_layout *layout);
751 migrate_open_files(const char *name, __u64 migration_flags,
752 const struct llapi_stripe_param *param,
753 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
760 char parent[PATH_MAX];
761 char volatile_file[PATH_MAX];
767 if (param == NULL && layout == NULL) {
768 error_loc = "layout information";
772 /* search for file directory pathname */
773 if (strlen(name) > sizeof(parent) - 1) {
774 error_loc = "source file name";
778 strncpy(parent, name, sizeof(parent));
779 ptr = strrchr(parent, '/');
781 if (getcwd(parent, sizeof(parent)) == NULL) {
782 error_loc = "getcwd";
786 if (ptr == parent) /* leading '/' */
791 /* open file, direct io */
792 /* even if the file is only read, WR mode is nedeed to allow
793 * layout swap on fd */
795 if (!(migration_flags & MIGRATION_NONDIRECT))
797 fd = open(name, rflags);
800 error_loc = "cannot open source file";
804 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
806 error_loc = "cannot get MDT index";
811 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
812 mode_t open_mode = S_IRUSR | S_IWUSR;
814 random_value = random();
815 rc = snprintf(volatile_file, sizeof(volatile_file),
816 "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
817 mdt_index, random_value);
818 if (rc >= sizeof(volatile_file)) {
823 /* create, open a volatile file, use caching (ie no directio) */
825 fdv = llapi_file_open_param(volatile_file, open_flags,
828 fdv = lfs_component_create(volatile_file, open_flags,
830 } while (fdv < 0 && (rc = fdv) == -EEXIST);
833 error_loc = "cannot create volatile file";
837 /* In case the MDT does not support creation of volatile files
838 * we should try to unlink it. */
839 (void)unlink(volatile_file);
841 /* Not-owner (root?) special case.
842 * Need to set owner/group of volatile file like original.
843 * This will allow to pass related check during layout_swap.
848 error_loc = "cannot stat source file";
852 rc = fstat(fdv, &stv);
855 error_loc = "cannot stat volatile";
859 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
860 rc = fchown(fdv, st.st_uid, st.st_gid);
863 error_loc = "cannot change ownwership of volatile";
882 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
884 struct llapi_layout *layout;
885 size_t buf_size = 4 * 1024 * 1024;
894 layout = llapi_layout_get_by_fd(fd_src, 0);
895 if (layout != NULL) {
896 uint64_t stripe_size;
898 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
900 buf_size = stripe_size;
902 llapi_layout_free(layout);
905 /* Use a page-aligned buffer for direct I/O */
906 rc = posix_memalign(&buf, getpagesize(), buf_size);
911 /* read new data only if we have written all
912 * previously read data */
915 rc = check_file(fd_src);
920 rsize = read(fd_src, buf, buf_size);
932 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
951 static int migrate_copy_timestamps(int fd, int fdv)
955 if (fstat(fd, &st) == 0) {
956 struct timeval tv[2] = {
957 {.tv_sec = st.st_atime},
958 {.tv_sec = st.st_mtime}
961 return futimes(fdv, tv);
967 static int migrate_block(int fd, int fdv)
974 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
976 error_loc = "cannot get dataversion";
984 /* The grouplock blocks all concurrent accesses to the file.
985 * It has to be taken after llapi_get_data_version as it would
987 rc = llapi_group_lock(fd, gid);
989 error_loc = "cannot get group lock";
993 rc = migrate_copy_data(fd, fdv, NULL);
995 error_loc = "data copy failed";
999 /* Make sure we keep original atime/mtime values */
1000 rc = migrate_copy_timestamps(fd, fdv);
1002 error_loc = "timestamp copy failed";
1007 * for a migration we need to check data version on file did
1010 * Pass in gid=0 since we already own grouplock. */
1011 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1012 SWAP_LAYOUTS_CHECK_DV1);
1013 if (rc == -EAGAIN) {
1014 error_loc = "file changed";
1016 } else if (rc < 0) {
1017 error_loc = "cannot swap layout";
1022 rc2 = llapi_group_unlock(fd, gid);
1023 if (rc2 < 0 && rc == 0) {
1024 error_loc = "unlock group lock";
1032 * Internal helper for migrate_copy_data(). Check lease and report error if
1035 * \param[in] fd File descriptor on which to check the lease.
1037 * \retval 0 Migration can keep on going.
1038 * \retval -errno Error occurred, abort migration.
1040 static int check_lease(int fd)
1044 rc = llapi_lease_check(fd);
1046 return 0; /* llapi_check_lease returns > 0 on success. */
1051 static int migrate_nonblock(int fd, int fdv)
1057 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1059 error_loc = "cannot get data version";
1063 rc = migrate_copy_data(fd, fdv, check_lease);
1065 error_loc = "data copy failed";
1069 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1071 error_loc = "cannot get data version";
1077 error_loc = "source file changed";
1081 /* Make sure we keep original atime/mtime values */
1082 rc = migrate_copy_timestamps(fd, fdv);
1084 error_loc = "timestamp copy failed";
1091 static int lfs_component_set(char *fname, int comp_id,
1092 __u32 flags, __u32 neg_flags)
1095 __u32 flags_array[2];
1100 ids[count] = comp_id;
1101 flags_array[count] = flags;
1106 if (neg_flags & LCME_FL_STALE) {
1107 fprintf(stderr, "%s: cannot clear 'stale' flags from "
1108 "component. Please use lfs-mirror-resync(1) "
1109 "instead\n", progname);
1113 ids[count] = comp_id;
1114 flags_array[count] = neg_flags | LCME_FL_NEG;
1118 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1120 if (errno == EUCLEAN) {
1123 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1124 progname, comp_id, fname);
1127 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1128 progname, comp_id, fname, flags, neg_flags);
1135 static int lfs_component_del(char *fname, __u32 comp_id,
1136 __u32 flags, __u32 neg_flags)
1140 if (flags && neg_flags)
1143 if (!flags && neg_flags)
1144 flags = neg_flags | LCME_FL_NEG;
1146 if ((flags && comp_id) || (!flags && !comp_id))
1150 if (flags & ~LCME_KNOWN_FLAGS) {
1152 "%s setstripe: unknown flags %#x\n",
1156 } else if (comp_id > LCME_ID_MAX) {
1157 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1162 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1165 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1166 progname, comp_id, fname, strerror(errno));
1170 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1177 rc = llapi_layout_file_comp_add(fname, layout);
1179 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1180 fname, strerror(errno));
1184 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1185 struct llapi_layout *layout)
1193 fd = lstat(fname, &st);
1194 if (fd == 0 && S_ISDIR(st.st_mode))
1195 open_flags = O_DIRECTORY | O_RDONLY;
1197 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1199 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1200 S_ISDIR(st.st_mode) ?
1201 "set default composite layout for" :
1202 "create composite file",
1203 fname, strerror(errno));
1207 static int lfs_migrate(char *name, __u64 migration_flags,
1208 struct llapi_stripe_param *param,
1209 struct llapi_layout *layout)
1211 struct llapi_layout *existing;
1212 uint64_t dom_new, dom_cur;
1217 rc = migrate_open_files(name, migration_flags, param, layout,
1222 rc = llapi_layout_dom_size(layout, &dom_new);
1224 error_loc = "cannot get new layout DoM size";
1227 /* special case for migration to DOM layout*/
1228 existing = llapi_layout_get_by_fd(fd, 0);
1230 error_loc = "cannot get existing layout";
1234 rc = llapi_layout_dom_size(existing, &dom_cur);
1236 error_loc = "cannot get current layout DoM size";
1240 /* if file has DoM layout already then migration is possible to
1241 * the new layout with the same DoM component via swap layout,
1242 * if new layout used bigger DOM size, then mirroring is used
1244 if (dom_new > dom_cur) {
1245 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1248 error_loc = "cannot migrate to DOM layout";
1252 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1253 /* Blocking mode (forced if servers do not support file lease).
1254 * It is also the default mode, since we cannot distinguish
1255 * between a broken lease and a server that does not support
1256 * atomic swap/close (LU-6785) */
1257 rc = migrate_block(fd, fdv);
1261 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1263 error_loc = "cannot get lease";
1267 rc = migrate_nonblock(fd, fdv);
1269 llapi_lease_release(fd);
1273 /* Atomically put lease, swap layouts and close.
1274 * for a migration we need to check data version on file did
1276 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1278 error_loc = "cannot swap layout";
1290 fprintf(stderr, "error: %s: %s: %s: %s\n",
1291 progname, name, error_loc, strerror(-rc));
1292 else if (migration_flags & MIGRATION_VERBOSE)
1293 printf("%s\n", name);
1298 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1307 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1311 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1312 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1313 const char *comp_name = comp_flags_table[i].cfn_name;
1315 if (strcmp(name, comp_name) == 0) {
1316 *flags |= comp_flag;
1318 } else if (strncmp(name, "^", 1) == 0 &&
1319 strcmp(name + 1, comp_name) == 0) {
1320 *neg_flags |= comp_flag;
1325 llapi_printf(LLAPI_MSG_ERROR,
1326 "%s: component flag '%s' not supported\n",
1332 if (!*flags && !*neg_flags)
1335 /* don't allow to set and exclude the same flag */
1336 if (*flags & *neg_flags)
1342 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1350 if (strncmp(string, "^", 1) == 0) {
1351 *neg_state = llapi_layout_string_flags(string + 1);
1352 if (*neg_state != 0)
1355 *state = llapi_layout_string_flags(string);
1360 llapi_printf(LLAPI_MSG_ERROR,
1361 "%s: mirrored file state '%s' not supported\n",
1367 * struct mirror_args - Command-line arguments for mirror(s).
1368 * @m_count: Number of mirrors to be created with this layout.
1369 * @m_flags: Mirror level flags, only 'prefer' is supported.
1370 * @m_layout: Mirror layout.
1371 * @m_file: A victim file. Its layout will be split and used as a mirror.
1372 * @m_next: Point to the next node of the list.
1374 * Command-line arguments for mirror(s) will be parsed and stored in
1375 * a linked list that consists of this structure.
1377 struct mirror_args {
1380 struct llapi_layout *m_layout;
1382 struct mirror_args *m_next;
1386 * enum mirror_flags - Flags for extending a mirrored file.
1387 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1388 * in case the victim file(s) contains the same data as the
1389 * original mirrored file.
1390 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1391 * @MF_COMP_ID: specified component id instead of mirror id
1393 * Flags for extending a mirrored file.
1403 * mirror_create_sanity_check() - Check mirror list.
1404 * @list: A linked list that stores the mirror arguments.
1406 * This function does a sanity check on @list for creating
1409 * Return: 0 on success or a negative error code on failure.
1411 static int mirror_create_sanity_check(const char *fname,
1412 struct mirror_args *list)
1415 bool has_m_file = false;
1416 bool has_m_layout = false;
1422 struct llapi_layout *layout;
1424 layout = llapi_layout_get_by_path(fname, 0);
1427 "error: %s: file '%s' couldn't get layout\n",
1432 rc = llapi_layout_sanity(layout, false, true);
1434 llapi_layout_free(layout);
1437 llapi_layout_sanity_perror(rc);
1442 while (list != NULL) {
1443 if (list->m_file != NULL) {
1445 llapi_layout_free(list->m_layout);
1448 llapi_layout_get_by_path(list->m_file, 0);
1449 if (list->m_layout == NULL) {
1451 "error: %s: file '%s' has no layout\n",
1452 progname, list->m_file);
1456 has_m_layout = true;
1457 if (list->m_layout == NULL) {
1458 fprintf(stderr, "error: %s: no mirror layout\n",
1464 rc = llapi_layout_sanity(list->m_layout, false, true);
1466 llapi_layout_sanity_perror(rc);
1470 list = list->m_next;
1473 if (has_m_file && has_m_layout) {
1475 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1483 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1485 __u32 mirror_flags = *(__u32 *)cbdata;
1489 rc = llapi_layout_comp_flags_get(layout, &flags);
1494 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1499 return LLAPI_LAYOUT_ITER_CONT;
1503 * mirror_create() - Create a mirrored file.
1504 * @fname: The file to be created.
1505 * @mirror_list: A linked list that stores the mirror arguments.
1507 * This function creates a mirrored file @fname with the mirror(s)
1508 * from @mirror_list.
1510 * Return: 0 on success or a negative error code on failure.
1512 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1514 struct llapi_layout *layout = NULL;
1515 struct mirror_args *cur_mirror = NULL;
1516 uint16_t mirror_count = 0;
1520 rc = mirror_create_sanity_check(NULL, mirror_list);
1524 cur_mirror = mirror_list;
1525 while (cur_mirror != NULL) {
1526 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1528 &cur_mirror->m_flags);
1531 fprintf(stderr, "%s: failed to set mirror flags\n",
1536 for (i = 0; i < cur_mirror->m_count; i++) {
1537 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1540 fprintf(stderr, "error: %s: "
1541 "merge layout failed: %s\n",
1542 progname, strerror(errno));
1546 mirror_count += cur_mirror->m_count;
1547 cur_mirror = cur_mirror->m_next;
1550 if (layout == NULL) {
1551 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1555 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1558 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1559 progname, strerror(errno));
1563 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1571 llapi_layout_free(layout);
1576 * Compare files and check lease on @fd.
1578 * \retval bytes number of bytes are the same
1580 static ssize_t mirror_file_compare(int fd, int fdv)
1582 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1584 ssize_t bytes_done = 0;
1585 ssize_t bytes_read = 0;
1587 buf = malloc(buflen * 2);
1592 if (!llapi_lease_check(fd)) {
1593 bytes_done = -EBUSY;
1597 bytes_read = read(fd, buf, buflen);
1598 if (bytes_read <= 0)
1601 if (bytes_read != read(fdv, buf + buflen, buflen))
1604 /* XXX: should compute the checksum on each buffer and then
1605 * compare checksum to avoid cache collision */
1606 if (memcmp(buf, buf + buflen, bytes_read))
1609 bytes_done += bytes_read;
1617 static int mirror_extend_file(const char *fname, const char *victim_file,
1618 enum mirror_flags mirror_flags)
1623 struct stat stbuf_v;
1624 struct ll_ioc_lease *data = NULL;
1627 fd = open(fname, O_RDWR);
1629 error_loc = "open source file";
1634 fdv = open(victim_file, O_RDWR);
1636 error_loc = "open target file";
1641 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1642 error_loc = "stat source or target file";
1647 if (stbuf.st_dev != stbuf_v.st_dev) {
1648 error_loc = "stat source and target file";
1653 /* mirrors should be of the same size */
1654 if (stbuf.st_size != stbuf_v.st_size) {
1655 error_loc = "file sizes don't match";
1660 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1662 error_loc = "cannot get lease";
1666 if (!(mirror_flags & MF_NO_VERIFY)) {
1668 /* mirrors should have the same contents */
1669 ret = mirror_file_compare(fd, fdv);
1670 if (ret != stbuf.st_size) {
1671 error_loc = "file busy or contents don't match";
1672 rc = ret < 0 ? ret : -EINVAL;
1677 /* Get rid of caching pages from clients */
1678 rc = llapi_file_flush(fd);
1680 error_loc = "cannot get data version";
1684 rc = llapi_file_flush(fdv);
1686 error_loc = "cannot get data version";
1691 /* Make sure we keep original atime/mtime values */
1692 rc = migrate_copy_timestamps(fd, fdv);
1694 error_loc = "cannot copy timestamp";
1698 /* Atomically put lease, merge layouts and close. */
1699 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1701 error_loc = "memory allocation";
1704 data->lil_mode = LL_LEASE_UNLCK;
1705 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1706 data->lil_count = 1;
1707 data->lil_ids[0] = fdv;
1708 rc = llapi_lease_set(fd, data);
1710 error_loc = "cannot merge layout";
1712 } else if (rc == 0) {
1714 error_loc = "lost lease lock";
1727 (void) unlink(victim_file);
1729 fprintf(stderr, "error: %s: %s: %s: %s\n",
1730 progname, fname, error_loc, strerror(-rc));
1734 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1736 struct ll_ioc_lease *data = NULL;
1741 rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1745 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1747 error_loc = "cannot get lease";
1751 rc = migrate_nonblock(fd, fdv);
1753 llapi_lease_release(fd);
1757 /* Atomically put lease, merge layouts and close. */
1758 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1760 error_loc = "memory allocation";
1763 data->lil_mode = LL_LEASE_UNLCK;
1764 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1765 data->lil_count = 1;
1766 data->lil_ids[0] = fdv;
1767 rc = llapi_lease_set(fd, data);
1769 error_loc = "cannot merge layout";
1771 } else if (rc == 0) {
1773 error_loc = "lost lease lock";
1786 fprintf(stderr, "error: %s: %s: %s: %s\n",
1787 progname, name, error_loc, strerror(-rc));
1791 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1792 enum mirror_flags mirror_flags)
1796 rc = mirror_create_sanity_check(fname, mirror_list);
1800 while (mirror_list) {
1801 if (mirror_list->m_file != NULL) {
1802 rc = mirror_extend_file(fname, mirror_list->m_file,
1805 __u32 mirror_count = mirror_list->m_count;
1807 while (mirror_count > 0) {
1808 rc = mirror_extend_layout(fname,
1809 mirror_list->m_layout);
1819 mirror_list = mirror_list->m_next;
1825 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1830 rc = llapi_layout_mirror_id_get(layout, &id);
1834 if ((__u16)id == *(__u16 *)cbdata)
1835 return LLAPI_LAYOUT_ITER_STOP;
1837 return LLAPI_LAYOUT_ITER_CONT;
1840 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1845 rc = llapi_layout_comp_id_get(layout, &id);
1849 if (id == *(__u32 *)cbdata)
1850 return LLAPI_LAYOUT_ITER_STOP;
1852 return LLAPI_LAYOUT_ITER_CONT;
1855 struct pool_to_id_cbdata {
1859 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1861 char buf[LOV_MAXPOOLNAME + 1];
1862 struct pool_to_id_cbdata *d = (void *)cbdata;
1866 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1869 if (strcmp(d->pool, buf))
1870 return LLAPI_LAYOUT_ITER_CONT;
1872 rc = llapi_layout_mirror_id_get(layout, &id);
1877 return LLAPI_LAYOUT_ITER_STOP;
1880 struct collect_ids_data {
1886 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
1888 struct collect_ids_data *cid = cbdata;
1892 rc = llapi_layout_mirror_id_get(layout, &id);
1896 if ((__u16)id != cid->cid_exclude) {
1899 for (i = 0; i < cid->cid_count; i++) {
1900 /* already collected the mirror id */
1901 if (id == cid->cid_ids[i])
1902 return LLAPI_LAYOUT_ITER_CONT;
1904 cid->cid_ids[cid->cid_count] = id;
1908 return LLAPI_LAYOUT_ITER_CONT;
1912 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
1913 * @mirror_id: Mirror id to be checked.
1914 * @layout: Mirror component list.
1916 * This function checks if a mirror with specified @mirror_id is the last
1917 * non-stale mirror of a layout @layout.
1919 * Return: true or false.
1922 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
1924 __u16 mirror_ids[128] = { 0 };
1925 struct collect_ids_data cid = { .cid_ids = mirror_ids,
1927 .cid_exclude = mirror_id, };
1930 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
1932 for (i = 0; i < cid.cid_count; i++) {
1933 struct llapi_resync_comp comp_array[1024] = { { 0 } };
1936 comp_size = llapi_mirror_find_stale(layout, comp_array,
1937 ARRAY_SIZE(comp_array),
1946 static int mirror_split(const char *fname, __u32 id, const char *pool,
1947 enum mirror_flags mflags, const char *victim_file)
1949 struct llapi_layout *layout;
1950 char parent[PATH_MAX];
1951 char victim[PATH_MAX];
1952 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1954 struct ll_ioc_lease *data;
1955 uint16_t mirror_count;
1960 /* check fname contains mirror with mirror_id/comp_id */
1961 layout = llapi_layout_get_by_path(fname, 0);
1964 "error %s: file '%s' couldn't get layout\n",
1969 rc = llapi_layout_sanity(layout, false, true);
1971 llapi_layout_sanity_perror(rc);
1975 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1978 "error %s: file '%s' couldn't get mirror count\n",
1982 if (mirror_count < 2) {
1984 "error %s: file '%s' has %d component, cannot split\n",
1985 progname, fname, mirror_count);
1989 if (mflags & MF_COMP_POOL) {
1990 struct pool_to_id_cbdata data = { .pool = pool };
1992 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool,
1995 } else if (mflags & MF_COMP_ID) {
1996 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1997 id = mirror_id_of(id);
1999 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2002 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2005 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2007 "error %s: file '%s' does not contain mirror with id %u\n",
2008 progname, fname, id);
2012 fd = open(fname, O_RDWR);
2015 "error %s: open file '%s' failed: %s\n",
2016 progname, fname, strerror(errno));
2020 /* get victim file directory pathname */
2021 if (strlen(fname) > sizeof(parent) - 1) {
2022 fprintf(stderr, "error %s: file name of '%s' too long\n",
2027 strncpy(parent, fname, sizeof(parent));
2028 ptr = strrchr(parent, '/');
2030 if (getcwd(parent, sizeof(parent)) == NULL) {
2031 fprintf(stderr, "error %s: getcwd failed: %s\n",
2032 progname, strerror(errno));
2042 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2044 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2049 if (victim_file == NULL) {
2050 /* use a temp file to store the splitted layout */
2051 if (mflags & MF_DESTROY) {
2052 if (last_non_stale_mirror(id, layout)) {
2055 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2060 fdv = llapi_create_volatile_idx(parent, mdt_index,
2061 O_LOV_DELAY_CREATE);
2063 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2065 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2068 /* user specified victim file */
2069 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2074 "error %s: create victim file failed: %s\n",
2075 progname, strerror(errno));
2079 /* get lease lock of fname */
2080 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2083 "error %s: cannot get lease of file '%s': %d\n",
2084 progname, fname, rc);
2088 /* Atomatically put lease, split layouts and close. */
2089 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2095 data->lil_mode = LL_LEASE_UNLCK;
2096 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2097 data->lil_count = 2;
2098 data->lil_ids[0] = fdv;
2099 data->lil_ids[1] = id;
2100 rc = llapi_lease_set(fd, data);
2102 if (rc == 0) /* lost lease lock */
2105 "error %s: cannot split '%s': %s\n",
2106 progname, fname, strerror(-rc));
2117 llapi_layout_free(layout);
2122 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2123 __u16 *mirror_ids, int ids_nr);
2125 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2126 __u64 migration_flags,
2127 struct llapi_stripe_param *param,
2128 struct llapi_layout *layout)
2130 struct ll_ioc_lease *data = NULL;
2133 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2135 error_loc = "cannot get lease";
2139 /* Atomically put lease, merge layouts, resync and close. */
2140 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2142 error_loc = "memory allocation";
2145 data->lil_mode = LL_LEASE_UNLCK;
2146 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2147 data->lil_count = 1;
2148 data->lil_ids[0] = fdv;
2149 rc = llapi_lease_set(fd, data);
2151 error_loc = "cannot merge layout";
2153 } else if (rc == 0) {
2155 error_loc = "lost lease lock";
2161 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2163 error_loc = "cannot resync file";
2167 /* delete first mirror now */
2168 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2170 error_loc = "cannot delete old layout";
2178 fprintf(stderr, "error: %s: %s: %s: %s\n",
2179 progname, name, error_loc, strerror(-rc));
2180 else if (migration_flags & MIGRATION_VERBOSE)
2181 printf("%s\n", name);
2188 * Parse a string containing an target index list into an array of integers.
2190 * The input string contains a comma delimited list of individual
2191 * indices and ranges, for example "1,2-4,7". Add the indices into the
2192 * \a tgts array and remove duplicates.
2194 * \param[out] tgts array to store indices in
2195 * \param[in] size size of \a tgts array
2196 * \param[in] offset starting index in \a tgts
2197 * \param[in] arg string containing OST index list
2198 * \param[in/out] overstriping index list may contain duplicates
2200 * \retval positive number of indices in \a tgts
2201 * \retval -EINVAL unable to parse \a arg
2203 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2204 unsigned long long *pattern)
2208 int slots = size - offset;
2210 bool overstriped = false;
2216 end_of_loop = false;
2217 while (!end_of_loop) {
2218 int start_index = 0;
2221 char *endptr = NULL;
2225 ptr = strchrnul(arg, ',');
2227 end_of_loop = *ptr == '\0';
2230 start_index = strtol(arg, &endptr, 0);
2231 if (endptr == arg) /* no data at all */
2233 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
2236 end_index = start_index;
2237 if (*endptr == '-') {
2238 end_index = strtol(endptr + 1, &endptr, 0);
2239 if (*endptr != '\0')
2241 if (end_index < start_index)
2245 for (i = start_index; i <= end_index && slots > 0; i++) {
2248 /* remove duplicate */
2249 for (j = 0; j < offset; j++) {
2250 if (tgts[j] == i && pattern &&
2251 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2253 else if (tgts[j] == i)
2259 if (j == offset) { /* check complete */
2265 if (slots == 0 && i < end_index)
2273 if (!end_of_loop && ptr != NULL)
2276 if (!overstriped && pattern)
2277 *pattern = LLAPI_LAYOUT_DEFAULT;
2279 return rc < 0 ? rc : nr;
2282 struct lfs_setstripe_args {
2283 unsigned long long lsa_comp_end;
2284 unsigned long long lsa_stripe_size;
2285 unsigned long long lsa_extension_size;
2286 long long lsa_stripe_count;
2287 long long lsa_stripe_off;
2288 __u32 lsa_comp_flags;
2289 __u32 lsa_comp_neg_flags;
2290 unsigned long long lsa_pattern;
2291 unsigned int lsa_mirror_count;
2293 bool lsa_first_comp;
2294 bool lsa_extension_comp;
2296 char *lsa_pool_name;
2299 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2301 unsigned int mirror_count = lsa->lsa_mirror_count;
2302 bool first_comp = lsa->lsa_first_comp;
2304 memset(lsa, 0, sizeof(*lsa));
2306 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2307 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2308 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2309 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2310 lsa->lsa_pool_name = NULL;
2312 lsa->lsa_mirror_count = mirror_count;
2313 lsa->lsa_first_comp = first_comp;
2317 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2318 * @lsa: Stripe options to be initialized and inherited.
2320 * This function initializes stripe options in @lsa and inherit
2321 * stripe_size, stripe_count and OST pool_name options.
2325 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2327 unsigned long long stripe_size;
2328 long long stripe_count;
2329 char *pool_name = NULL;
2331 stripe_size = lsa->lsa_stripe_size;
2332 stripe_count = lsa->lsa_stripe_count;
2333 pool_name = lsa->lsa_pool_name;
2335 setstripe_args_init(lsa);
2337 lsa->lsa_stripe_size = stripe_size;
2338 lsa->lsa_stripe_count = stripe_count;
2339 lsa->lsa_pool_name = pool_name;
2342 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2344 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2345 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2346 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2347 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2348 lsa->lsa_comp_end != 0);
2352 * comp_args_to_layout() - Create or extend a composite layout.
2353 * @composite: Pointer to the composite layout.
2354 * @lsa: Stripe options for the new component.
2356 * This function creates or extends a composite layout by adding a new
2357 * component with stripe options from @lsa.
2359 * Return: 0 on success or an error code on failure.
2361 static int comp_args_to_layout(struct llapi_layout **composite,
2362 struct lfs_setstripe_args *lsa,
2365 struct llapi_layout *layout = *composite;
2366 uint64_t prev_end = 0;
2371 if (layout == NULL) {
2372 layout = llapi_layout_alloc();
2373 if (layout == NULL) {
2374 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2379 *composite = layout;
2380 lsa->lsa_first_comp = true;
2384 /* Get current component extent, current component
2385 * must be the tail component. */
2386 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2388 fprintf(stderr, "Get comp extent failed. %s\n",
2393 if (lsa->lsa_first_comp)
2396 if (lsa->lsa_first_comp)
2397 rc = llapi_layout_add_first_comp(layout);
2399 rc = llapi_layout_comp_add(layout);
2401 fprintf(stderr, "Add component failed. %s\n",
2407 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2409 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2410 lsa->lsa_comp_flags, strerror(errno));
2415 uint64_t comp_end = lsa->lsa_comp_end;
2417 /* The extendable component is 0-length, so it can be removed
2418 * if there is insufficient space to extend it. */
2419 if (lsa->lsa_extension_comp)
2420 comp_end = prev_end;
2422 rc = llapi_layout_comp_extent_set(layout, prev_end,
2425 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2426 prev_end, comp_end, strerror(errno));
2430 /* reset lsa_first_comp */
2431 lsa->lsa_first_comp = false;
2433 /* Data-on-MDT component setting */
2434 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2435 /* In case of Data-on-MDT patterns the only extra option
2436 * applicable is stripe size option. */
2437 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2438 fprintf(stderr, "Option 'stripe-count' can't be "
2439 "specified with Data-on-MDT component: %lld\n",
2440 lsa->lsa_stripe_count);
2444 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2445 fprintf(stderr, "Option 'stripe-size' can't be "
2446 "specified with Data-on-MDT component: %llu\n",
2447 lsa->lsa_stripe_size);
2451 if (lsa->lsa_nr_tgts != 0) {
2452 fprintf(stderr, "Option 'ost-list' can't be specified "
2453 "with Data-on-MDT component: '%i'\n",
2458 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2459 fprintf(stderr, "Option 'stripe-offset' can't be "
2460 "specified with Data-on-MDT component: %lld\n",
2461 lsa->lsa_stripe_off);
2465 if (lsa->lsa_pool_name != 0) {
2466 fprintf(stderr, "Option 'pool' can't be specified "
2467 "with Data-on-MDT component: '%s'\n",
2468 lsa->lsa_pool_name);
2473 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2475 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2480 /* Data-on-MDT component has always single stripe up to end */
2481 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2482 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2483 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2485 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2492 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2493 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2495 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2496 rc = llapi_layout_extension_size_set(layout, size);
2498 rc = llapi_layout_stripe_size_set(layout, size);
2501 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2502 size, strerror(errno));
2506 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2508 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2509 lsa->lsa_stripe_count, strerror(errno));
2513 if (lsa->lsa_pool_name != NULL) {
2514 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2516 fprintf(stderr, "Set pool name: %s failed. %s\n",
2517 lsa->lsa_pool_name, strerror(errno));
2521 rc = llapi_layout_pool_name_set(layout, "");
2523 fprintf(stderr, "Clear pool name failed: %s\n",
2529 if (lsa->lsa_nr_tgts > 0) {
2530 if (lsa->lsa_stripe_count > 0 &&
2531 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2532 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2533 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2534 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2535 lsa->lsa_stripe_count,
2540 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2541 rc = llapi_layout_ost_index_set(layout, i,
2546 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2547 lsa->lsa_stripe_off != -1) {
2548 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2551 fprintf(stderr, "Set ost index %d failed. %s\n",
2552 i, strerror(errno));
2556 /* Create the second, virtual component of extension space */
2557 if (lsa->lsa_extension_comp) {
2558 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2559 lsa->lsa_extension_comp = false;
2566 static int build_component(struct llapi_layout **layout,
2567 struct lfs_setstripe_args *lsa, bool set_extent)
2571 rc = comp_args_to_layout(layout, lsa, set_extent);
2575 if (lsa->lsa_mirror_count > 0) {
2576 rc = llapi_layout_mirror_count_set(*layout,
2577 lsa->lsa_mirror_count);
2581 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2584 lsa->lsa_mirror_count = 0;
2590 static int build_layout_from_yaml_node(struct cYAML *node,
2591 struct llapi_layout **layout,
2592 struct lfs_setstripe_args *lsa,
2599 if (node->cy_type == CYAML_TYPE_OBJECT) {
2600 /* go deep to sub blocks */
2601 rc = build_layout_from_yaml_node(node->cy_child, layout,
2606 if (node->cy_string == NULL)
2609 string = node->cy_string;
2610 /* skip leading lmm_ if present, to simplify parsing */
2611 if (strncmp(string, "lmm_", 4) == 0)
2614 if (node->cy_type == CYAML_TYPE_STRING) {
2615 if (!strcmp(string, "lcme_extent.e_end")) {
2616 if (!strcmp(node->cy_valuestring, "EOF") ||
2617 !strcmp(node->cy_valuestring, "eof"))
2618 lsa->lsa_comp_end = LUSTRE_EOF;
2619 } else if (!strcmp(string, "pool")) {
2620 lsa->lsa_pool_name = node->cy_valuestring;
2621 } else if (!strcmp(string, "pattern")) {
2622 if (!strcmp(node->cy_valuestring, "mdt"))
2623 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2624 if (!strcmp(node->cy_valuestring,
2625 "raid0,overstriped"))
2627 LLAPI_LAYOUT_OVERSTRIPING;
2628 } else if (!strcmp(string, "lcme_flags")) {
2629 rc = comp_str2flags(node->cy_valuestring,
2630 &lsa->lsa_comp_flags,
2631 &lsa->lsa_comp_neg_flags);
2634 /* Only template flags have meaning in
2635 * the layout for a new file
2637 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2639 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2640 if (!strcmp(string, "lcm_mirror_count")) {
2641 lsa->lsa_mirror_count = node->cy_valueint;
2642 } else if (!strcmp(string, "lcme_extent.e_start")) {
2643 if (node->cy_valueint != 0 || *layout != NULL) {
2644 rc = build_component(layout, lsa, true);
2649 if (node->cy_valueint == 0)
2650 lsa->lsa_first_comp = true;
2652 /* initialize lsa */
2653 setstripe_args_init(lsa);
2654 lsa->lsa_tgts = osts;
2655 } else if (!strcmp(string, "lcme_extent.e_end")) {
2656 if (node->cy_valueint == -1)
2657 lsa->lsa_comp_end = LUSTRE_EOF;
2659 lsa->lsa_comp_end = node->cy_valueint;
2660 } else if (!strcmp(string, "stripe_count")) {
2661 lsa->lsa_stripe_count = node->cy_valueint;
2662 } else if (!strcmp(string, "stripe_size")) {
2663 lsa->lsa_stripe_size = node->cy_valueint;
2664 } else if (!strcmp(string, "stripe_offset")) {
2665 lsa->lsa_stripe_off = node->cy_valueint;
2666 } else if (!strcmp(string, "l_ost_idx")) {
2667 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2672 node = node->cy_next;
2678 static int lfs_comp_create_from_yaml(char *template,
2679 struct llapi_layout **layout,
2680 struct lfs_setstripe_args *lsa,
2683 struct cYAML *tree = NULL, *err_rc = NULL;
2686 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2688 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2689 progname, template);
2690 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2691 "can't parse", &err_rc);
2692 cYAML_print_tree2file(stderr, err_rc);
2693 cYAML_free_tree(err_rc);
2698 /* initialize lsa for plain file */
2699 setstripe_args_init(lsa);
2700 lsa->lsa_tgts = osts;
2702 rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2704 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2705 progname, template);
2708 rc = build_component(layout, lsa, *layout != NULL);
2710 /* clean clean lsa */
2711 setstripe_args_init(lsa);
2715 cYAML_free_tree(tree);
2720 * Get the extension size from the next (SEL) component and extend the
2721 * current component on it. The start of the next component is to be
2724 * \param[in] layout the current layout
2725 * \param[in] start the start of the current component
2726 * \param[in,out] end the end of the current component
2727 * \param[in] offset the offset to adjust the end position to instead of
2730 * \retval 0 - extended successfully
2731 * \retval < 0 - error
2733 static int layout_extend_comp(struct llapi_layout *layout,
2734 uint64_t start, uint64_t *end,
2737 uint64_t size, next_start, next_end;
2740 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2742 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2743 "%s\n", progname, strerror(errno));
2747 /* Even if the @size will not be used below, this will fail if
2748 * this is not a SEL component - a good confirmation we are
2749 * working on right components. */
2750 rc = llapi_layout_extension_size_get(layout, &size);
2752 fprintf(stderr, "%s setstripe: cannot get component ext size: "
2753 "%s\n", progname, strerror(errno));
2757 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
2759 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2760 progname, strerror(errno));
2764 next_start += offset ?: size;
2765 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
2767 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2768 progname, strerror(errno));
2772 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
2774 fprintf(stderr, "%s setstripe: cannot move component cursor: "
2775 "%s\n", progname, strerror(errno));
2779 *end += offset ?: size;
2780 rc = llapi_layout_comp_extent_set(layout, start, *end);
2782 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
2783 progname, strerror(errno));
2791 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
2792 * end of the last component in the existing file, and adjust the
2793 * first extent start of the components to be added accordingly.
2795 * In the create mode, we need to check if the first component is an extendable
2796 * SEL component and extend its length to the extension size (first component
2797 * of the PFL file is initialised at the create time, cannot be 0-lenght.
2799 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
2802 struct llapi_layout *head;
2803 uint64_t start = 0, prev_end = 0;
2812 head = llapi_layout_get_by_path(fname, 0);
2815 "%s setstripe: cannot read layout from '%s': "
2816 "%s\n", progname, fname, strerror(errno));
2818 } else if (errno == ENODATA) {
2819 /* file without LOVEA, this component-add will be turned
2820 * into a component-create. */
2821 llapi_layout_free(head);
2824 /* the new layout will be added to an empty one, it
2825 * still needs to be adjusted below */
2828 } else if (!llapi_layout_is_composite(head)) {
2829 fprintf(stderr, "%s setstripe: '%s' not a composite "
2830 "file\n", progname, fname);
2831 llapi_layout_free(head);
2835 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2837 fprintf(stderr, "%s setstripe: cannot get prev "
2838 "extent: %s\n", progname, strerror(errno));
2839 llapi_layout_free(head);
2843 llapi_layout_free(head);
2847 /* Make sure we use the first component of the layout to be added. */
2848 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2851 "%s setstripe: cannot move component cursor: %s\n",
2852 progname, strerror(errno));
2856 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2858 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2859 progname, strerror(errno));
2863 if (start == 0 && end == 0) {
2864 rc = layout_extend_comp(layout, start, &end,
2865 comp_add ? prev_end : 0);
2870 if (start > prev_end || end < prev_end) {
2871 fprintf(stderr, "%s setstripe: first extent [%lu, %lu) not "
2872 "adjacent with extent end %lu\n",
2873 progname, start, end, prev_end);
2877 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2879 fprintf(stderr, "%s setstripe: cannot set component extent "
2881 progname, prev_end, end, strerror(errno));
2888 static int mirror_adjust_first_extents(struct mirror_args *list)
2895 while (list != NULL) {
2896 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
2899 list = list->m_next;
2905 static inline bool arg_is_eof(char *arg)
2907 return !strncmp(arg, "-1", strlen("-1")) ||
2908 !strncmp(arg, "EOF", strlen("EOF")) ||
2909 !strncmp(arg, "eof", strlen("eof"));
2913 * lfs_mirror_alloc() - Allocate a mirror argument structure.
2915 * Return: Valid mirror_args pointer on success and
2916 * NULL if memory allocation fails.
2918 static struct mirror_args *lfs_mirror_alloc(void)
2920 struct mirror_args *mirror = NULL;
2923 mirror = calloc(1, sizeof(*mirror));
2934 * lfs_mirror_free() - Free memory allocated for a mirror argument
2936 * @mirror: Previously allocated mirror argument structure by
2937 * lfs_mirror_alloc().
2939 * Free memory allocated for @mirror.
2943 static void lfs_mirror_free(struct mirror_args *mirror)
2945 if (mirror->m_layout != NULL)
2946 llapi_layout_free(mirror->m_layout);
2951 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2952 * @mirror_list: Previously allocated mirror list.
2954 * Free memory allocated for @mirror_list.
2958 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2960 struct mirror_args *next_mirror = NULL;
2962 while (mirror_list != NULL) {
2963 next_mirror = mirror_list->m_next;
2964 lfs_mirror_free(mirror_list);
2965 mirror_list = next_mirror;
2977 LFS_COMP_NO_VERIFY_OPT,
2979 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
2981 LFS_MIRROR_STATE_OPT,
2983 LFS_MIRROR_INDEX_OPT,
2984 LFS_LAYOUT_FOREIGN_OPT,
2990 static int lfs_setstripe_internal(int argc, char **argv,
2991 enum setstripe_origin opc)
2993 struct lfs_setstripe_args lsa = { 0 };
2994 struct llapi_stripe_param *param = NULL;
2995 struct find_param migrate_mdt_param = {
3005 unsigned long long size_units = 1;
3006 bool migrate_mode = false;
3007 bool migrate_mdt_mode = false;
3008 bool setstripe_mode = false;
3009 bool migration_block = false;
3010 __u64 migration_flags = 0;
3011 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3012 int comp_del = 0, comp_set = 0;
3015 struct llapi_layout *layout = NULL;
3016 struct llapi_layout **lpp = &layout;
3017 bool mirror_mode = false;
3018 bool has_m_file = false;
3019 __u32 mirror_count = 0;
3020 enum mirror_flags mirror_flags = 0;
3021 struct mirror_args *mirror_list = NULL;
3022 struct mirror_args *new_mirror = NULL;
3023 struct mirror_args *last_mirror = NULL;
3024 __u16 mirror_id = 0;
3026 bool from_yaml = false;
3027 bool from_copy = false;
3028 char *template = NULL;
3029 bool foreign_mode = false;
3031 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3032 char *mode_opt = NULL;
3033 mode_t previous_umask = 0;
3034 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3036 struct option long_opts[] = {
3037 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3038 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3039 /* --block is only valid in migrate mode */
3040 { .val = 'b', .name = "block", .has_arg = no_argument },
3041 { .val = LFS_COMP_ADD_OPT,
3042 .name = "comp-add", .has_arg = no_argument },
3043 { .val = LFS_COMP_ADD_OPT,
3044 .name = "component-add", .has_arg = no_argument },
3045 { .val = LFS_COMP_DEL_OPT,
3046 .name = "comp-del", .has_arg = no_argument },
3047 { .val = LFS_COMP_DEL_OPT,
3048 .name = "component-del", .has_arg = no_argument },
3049 { .val = LFS_COMP_FLAGS_OPT,
3050 .name = "comp-flags", .has_arg = required_argument },
3051 { .val = LFS_COMP_FLAGS_OPT,
3052 .name = "component-flags",
3053 .has_arg = required_argument },
3054 { .val = LFS_COMP_SET_OPT,
3055 .name = "comp-set", .has_arg = no_argument },
3056 { .val = LFS_COMP_SET_OPT,
3057 .name = "component-set",
3058 .has_arg = no_argument},
3059 { .val = LFS_COMP_NO_VERIFY_OPT,
3060 .name = "no-verify", .has_arg = no_argument},
3061 { .val = LFS_LAYOUT_FLAGS_OPT,
3062 .name = "flags", .has_arg = required_argument},
3063 { .val = LFS_LAYOUT_FOREIGN_OPT,
3064 .name = "foreign", .has_arg = optional_argument},
3065 { .val = LFS_MIRROR_ID_OPT,
3066 .name = "mirror-id", .has_arg = required_argument},
3067 { .val = LFS_MODE_OPT,
3068 .name = "mode", .has_arg = required_argument},
3069 { .val = LFS_LAYOUT_COPY,
3070 .name = "copy", .has_arg = required_argument},
3071 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3072 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3073 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3074 { .val = 'C', .name = "overstripe-count",
3075 .has_arg = required_argument},
3076 { .val = 'd', .name = "delete", .has_arg = no_argument},
3077 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3078 /* --non-direct is only valid in migrate mode */
3079 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3080 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3081 { .val = 'E', .name = "component-end",
3082 .has_arg = required_argument},
3083 { .val = 'f', .name = "file", .has_arg = required_argument },
3084 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3085 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3086 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3087 /* find { .val = 'h', .name = "help", .has_arg = no_argument }, */
3088 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3089 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3090 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3091 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3092 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3093 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3094 { .val = 'L', .name = "layout", .has_arg = required_argument },
3095 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3096 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3097 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3098 /* --non-block is only valid in migrate mode */
3099 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3100 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3101 { .val = 'o', .name = "ost", .has_arg = required_argument },
3102 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3103 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3104 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3106 { .val = 'p', .name = "pool", .has_arg = required_argument },
3107 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3108 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3109 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3110 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3111 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3112 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3113 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3114 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3115 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3116 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3117 /* --verbose is only valid in migrate mode */
3118 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3119 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3120 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3121 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3122 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3125 setstripe_args_init(&lsa);
3127 migrate_mode = (opc == SO_MIGRATE);
3128 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3129 setstripe_mode = (opc == SO_SETSTRIPE);
3130 if (opc == SO_MIRROR_DELETE) {
3132 mirror_flags = MF_DESTROY;
3135 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3137 while ((c = getopt_long(argc, argv,
3138 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3139 long_opts, NULL)) >= 0) {
3145 case LFS_COMP_ADD_OPT:
3148 case LFS_COMP_DEL_OPT:
3151 case LFS_COMP_FLAGS_OPT:
3152 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3153 &lsa.lsa_comp_neg_flags);
3156 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3157 fprintf(stderr, "%s: inverted flags are not supported\n",
3162 case LFS_COMP_SET_OPT:
3165 case LFS_COMP_NO_VERIFY_OPT:
3166 mirror_flags |= MF_NO_VERIFY;
3168 case LFS_MIRROR_ID_OPT:
3169 mirror_id = strtoul(optarg, &end, 0);
3170 if (*end != '\0' || mirror_id == 0) {
3172 "%s %s: invalid mirror ID '%s'\n",
3173 progname, argv[0], optarg);
3177 case LFS_LAYOUT_FLAGS_OPT: {
3180 /* check for numeric flags (foreign and mirror cases) */
3181 if (setstripe_mode && !mirror_mode && !last_mirror) {
3182 flags = strtoul(optarg, &end, 16);
3185 "%s %s: bad flags '%s'\n",
3186 progname, argv[0], optarg);
3192 if (!mirror_mode || !last_mirror) {
3193 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
3198 result = comp_str2flags(optarg, &last_mirror->m_flags,
3204 fprintf(stderr, "%s: inverted flags are not supported\n",
3209 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3211 "%s: unsupported mirror flags: %s\n",
3218 case LFS_LAYOUT_FOREIGN_OPT:
3219 if (optarg != NULL) {
3220 /* check pure numeric */
3221 type = strtoul(optarg, &end, 0);
3224 type = check_foreign_type_name(optarg);
3225 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3227 "%s %s: unrecognized foreign type '%s'\n",
3234 foreign_mode = true;
3238 if (mode_opt != NULL) {
3239 mode = strtoul(mode_opt, &end, 8);
3242 "%s %s: bad mode '%s'\n",
3243 progname, argv[0], mode_opt);
3246 previous_umask = umask(0);
3249 case LFS_LAYOUT_COPY:
3254 if (!migrate_mode) {
3256 "%s %s: -b|--block valid only for migrate command\n",
3260 migration_block = true;
3263 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3265 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3269 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3272 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3275 "%s %s: invalid stripe count '%s'\n",
3276 progname, argv[0], optarg);
3280 if (lsa.lsa_stripe_count == -1)
3281 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3284 /* delete the default striping pattern */
3286 if (opc == SO_MIRROR_SPLIT) {
3289 "%s %s: -d cannot used with -f\n",
3293 mirror_flags |= MF_DESTROY;
3297 if (!migrate_mode) {
3299 "%s %s: -D|--non-direct is valid "
3300 "only for migrate command\n",
3304 migration_flags |= MIGRATION_NONDIRECT;
3307 if (lsa.lsa_comp_end != 0) {
3308 result = comp_args_to_layout(lpp, &lsa, true);
3310 fprintf(stderr, "%s: invalid layout\n",
3315 setstripe_args_init_inherit(&lsa);
3318 if (arg_is_eof(optarg)) {
3319 lsa.lsa_comp_end = LUSTRE_EOF;
3321 result = llapi_parse_size(optarg,
3326 "%s %s: invalid component end '%s'\n",
3327 progname, argv[0], optarg);
3333 if (!migrate_mode) {
3334 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
3338 lsa.lsa_pattern = check_hashtype(optarg);
3339 if (lsa.lsa_pattern == 0) {
3341 "%s %s: bad stripe hash type '%s'\n",
3342 progname, argv[0], optarg);
3347 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3350 "%s %s: invalid stripe offset '%s'\n",
3351 progname, argv[0], optarg);
3354 if (lsa.lsa_stripe_off == -1)
3355 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3358 comp_id = strtoul(optarg, &end, 0);
3359 if (*end != '\0' || comp_id == 0 ||
3360 comp_id > LCME_ID_MAX) {
3362 "%s %s: invalid component ID '%s'\n",
3363 progname, argv[0], optarg);
3368 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3370 "error: %s: invalid option: %s\n",
3371 progname, argv[optopt + 1]);
3374 if (opc == SO_MIRROR_EXTEND) {
3375 if (last_mirror == NULL) {
3377 "error: %s: '-N' must exist in front of '%s'\n",
3378 progname, argv[optopt + 1]);
3381 last_mirror->m_file = optarg;
3382 last_mirror->m_count = 1;
3385 if (mirror_list == NULL)
3386 mirror_list = lfs_mirror_alloc();
3387 mirror_list->m_file = optarg;
3392 if (strcmp(argv[optind - 1], "mdt") == 0) {
3393 /* Can be only the first component */
3394 if (layout != NULL) {
3396 fprintf(stderr, "error: 'mdt' layout "
3397 "can be only the first one\n");
3400 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3402 fprintf(stderr, "error: 'mdt' layout "
3403 "size is too big\n");
3406 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3407 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3409 fprintf(stderr, "error: layout '%s' is "
3410 "unknown, supported layouts are: "
3411 "'mdt', 'raid0'\n", argv[optind]);
3416 if (!migrate_mode) {
3418 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3422 migrate_mdt_mode = true;
3423 lsa.lsa_nr_tgts = parse_targets(tgts,
3424 sizeof(tgts) / sizeof(__u32),
3425 lsa.lsa_nr_tgts, optarg, NULL);
3426 if (lsa.lsa_nr_tgts < 0) {
3428 "%s %s: invalid MDT target(s) '%s'\n",
3429 progname, argv[0], optarg);
3433 lsa.lsa_tgts = tgts;
3434 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3435 lsa.lsa_stripe_off = tgts[0];
3438 if (!migrate_mode) {
3440 "%s %s: -n|--non-block valid only for migrate command\n",
3444 migration_flags |= MIGRATION_NONBLOCK;
3447 if (opc == SO_SETSTRIPE) {
3448 opc = SO_MIRROR_CREATE;
3452 if (optarg != NULL) {
3453 mirror_count = strtoul(optarg, &end, 0);
3454 if (*end != '\0' || mirror_count == 0) {
3456 "error: %s: bad mirror count: %s\n",
3463 new_mirror = lfs_mirror_alloc();
3464 new_mirror->m_count = mirror_count;
3466 if (mirror_list == NULL)
3467 mirror_list = new_mirror;
3469 if (last_mirror != NULL) {
3470 /* wrap up last mirror */
3471 if (lsa.lsa_comp_end == 0)
3472 lsa.lsa_comp_end = LUSTRE_EOF;
3474 result = comp_args_to_layout(lpp, &lsa, true);
3476 lfs_mirror_free(new_mirror);
3480 setstripe_args_init_inherit(&lsa);
3482 last_mirror->m_next = new_mirror;
3485 last_mirror = new_mirror;
3486 lpp = &last_mirror->m_layout;
3489 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3490 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3491 fprintf(stderr, "warning: '--ost-list' is "
3492 "deprecated, use '--ost' instead\n");
3494 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3496 "%s %s: -o|--ost incompatible with DoM layout\n",
3500 /* -o allows overstriping, and must note it because
3501 * parse_targets is shared with MDT striping, which
3502 * does not allow duplicates
3504 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3505 lsa.lsa_nr_tgts = parse_targets(tgts,
3506 sizeof(tgts) / sizeof(__u32),
3507 lsa.lsa_nr_tgts, optarg,
3509 if (lsa.lsa_nr_tgts < 0) {
3511 "%s %s: invalid OST target(s) '%s'\n",
3512 progname, argv[0], optarg);
3516 lsa.lsa_tgts = tgts;
3517 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3518 lsa.lsa_stripe_off = tgts[0];
3523 lsa.lsa_pool_name = optarg;
3525 if (strlen(lsa.lsa_pool_name) == 0 ||
3526 strncmp(lsa.lsa_pool_name, "none",
3527 LOV_MAXPOOLNAME) == 0)
3528 lsa.lsa_pool_name = NULL;
3531 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3535 "%s %s: invalid stripe size '%s'\n",
3536 progname, argv[0], optarg);
3541 if (!migrate_mode) {
3543 "%s %s: -v|--verbose valid only for migrate command\n",
3547 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3548 migration_flags = MIGRATION_VERBOSE;
3558 result = llapi_parse_size(optarg,
3559 &lsa.lsa_extension_size,
3563 "%s %s: invalid extension size '%s'\n",
3564 progname, argv[0], optarg);
3568 lsa.lsa_extension_comp = true;
3571 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3572 progname, argv[0], argv[optind - 1]);
3577 fname = argv[optind];
3579 if (optind == argc) {
3580 fprintf(stderr, "%s %s: FILE must be specified\n",
3585 if (xattr && !foreign_mode) {
3586 /* only print a warning as this is harmless and will be ignored
3589 "%s %s: xattr has been specified for non-foreign layout\n",
3591 } else if (foreign_mode && !xattr) {
3593 "%s %s: xattr must be provided in foreign mode\n",
3598 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
3599 comp_set || comp_id || delete || from_copy ||
3600 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
3603 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
3608 if (mirror_mode && mirror_count == 0) {
3610 "error: %s: --mirror-count|-N option is required\n",
3617 if (lsa.lsa_comp_end == 0)
3618 lsa.lsa_comp_end = LUSTRE_EOF;
3621 if (lsa.lsa_comp_end != 0) {
3622 result = comp_args_to_layout(lpp, &lsa, true);
3624 fprintf(stderr, "error: %s: invalid layout\n",
3631 if (mirror_flags & MF_NO_VERIFY) {
3632 if (opc != SO_MIRROR_EXTEND) {
3634 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3638 } else if (!has_m_file) {
3640 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3647 if (comp_set && !comp_id) {
3648 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3653 if ((delete + comp_set + comp_del + comp_add) > 1) {
3655 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3660 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3661 lsa.lsa_comp_flags != 0 || layout != NULL)) {
3663 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3668 if ((comp_set || comp_del) &&
3669 (setstripe_args_specified(&lsa) || layout != NULL)) {
3671 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3676 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3678 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3683 if (comp_add || comp_del) {
3686 result = lstat(fname, &st);
3687 if (result == 0 && S_ISDIR(st.st_mode)) {
3689 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3695 fprintf(stderr, "error: %s: can't use --component-add "
3696 "or --component-del for mirror operation\n",
3703 if (layout == NULL) {
3705 "%s %s: option -E must be specified with --component-add\n",
3711 if (layout != NULL || mirror_list != NULL) {
3713 result = mirror_adjust_first_extents(mirror_list);
3715 result = layout_adjust_first_extent(fname, layout,
3717 if (result == -ENODATA)
3719 else if (result != 0) {
3720 fprintf(stderr, "error: %s: invalid layout\n",
3726 if (from_yaml && from_copy) {
3728 "%s: can't specify --yaml and --copy together\n",
3733 if ((from_yaml || from_copy) &&
3734 (setstripe_args_specified(&lsa) || layout != NULL)) {
3735 fprintf(stderr, "error: %s: can't specify --yaml with "
3736 "-c, -S, -i, -o, -p or -E options.\n",
3741 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3743 "%s %s: options --non-block and --block are mutually exclusive\n",
3748 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
3749 opc != SO_MIRROR_DELETE && comp_id != 0) {
3751 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3756 if (migrate_mdt_mode) {
3757 struct lmv_user_md *lmu;
3759 /* initialize migrate mdt parameters */
3760 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3761 LMV_USER_MAGIC_SPECIFIC));
3764 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3765 progname, argv[0], strerror(ENOMEM));
3769 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3770 lmu->lum_stripe_count = lsa.lsa_stripe_count;
3771 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3773 "%s %s: migrate should specify MDT index\n",
3778 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3779 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3780 lmu->lum_hash_type = lsa.lsa_pattern;
3782 lmu->lum_hash_type = LMV_HASH_TYPE_DEFAULT;
3783 if (lsa.lsa_pool_name) {
3784 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3785 sizeof(lmu->lum_pool_name) - 1);
3786 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
3788 if (lsa.lsa_nr_tgts > 1) {
3791 if (lsa.lsa_stripe_count > 0 &&
3792 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3793 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3795 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3796 progname, lsa.lsa_stripe_count,
3802 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3803 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3804 for (i = 0; i < lsa.lsa_nr_tgts; i++)
3805 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3807 lmu->lum_magic = LMV_USER_MAGIC;
3810 migrate_mdt_param.fp_lmv_md = lmu;
3811 migrate_mdt_param.fp_migrate = 1;
3812 } else if (layout == NULL) {
3813 /* initialize stripe parameters */
3814 param = calloc(1, offsetof(typeof(*param),
3815 lsp_osts[lsa.lsa_nr_tgts]));
3816 if (param == NULL) {
3818 "%s %s: cannot allocate memory for parameters: %s\n",
3819 progname, argv[0], strerror(ENOMEM));
3824 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3825 param->lsp_stripe_size = lsa.lsa_stripe_size;
3826 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3827 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3828 param->lsp_stripe_count = -1;
3830 param->lsp_stripe_count = lsa.lsa_stripe_count;
3832 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3833 param->lsp_stripe_offset = -1;
3835 param->lsp_stripe_offset = lsa.lsa_stripe_off;
3836 param->lsp_stripe_pattern =
3837 llapi_pattern_to_lov(lsa.lsa_pattern);
3838 if (param->lsp_stripe_pattern == EINVAL) {
3839 fprintf(stderr, "error: %s: invalid stripe pattern\n",
3844 param->lsp_pool = lsa.lsa_pool_name;
3845 param->lsp_is_specific = false;
3846 if (lsa.lsa_nr_tgts > 0) {
3847 if (lsa.lsa_stripe_count > 0 &&
3848 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3849 lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3850 lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3852 "error: %s: stripe count %lld doesn't match the number of OSTs: %d\n",
3853 argv[0], lsa.lsa_stripe_count,
3859 param->lsp_is_specific = true;
3860 param->lsp_stripe_count = lsa.lsa_nr_tgts;
3861 memcpy(param->lsp_osts, tgts,
3862 sizeof(*tgts) * lsa.lsa_nr_tgts);
3867 /* generate a layout from a YAML template */
3868 result = lfs_comp_create_from_yaml(template, &layout,
3871 fprintf(stderr, "error: %s: can't create composite "
3872 "layout from template file %s\n",
3876 } else if (from_copy) {
3877 layout = llapi_layout_get_by_path(template, 0);
3878 if (layout == NULL) {
3880 "%s: can't create composite layout from file %s.\n",
3881 progname, template);
3886 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3887 if (migrate_mdt_mode) {
3888 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3889 } else if (migrate_mode) {
3890 result = lfs_migrate(fname, migration_flags, param,
3892 } else if (comp_set != 0) {
3893 result = lfs_component_set(fname, comp_id,
3895 lsa.lsa_comp_neg_flags);
3896 } else if (comp_del != 0) {
3897 result = lfs_component_del(fname, comp_id,
3899 lsa.lsa_comp_neg_flags);
3900 } else if (comp_add != 0) {
3901 result = lfs_component_add(fname, layout);
3902 } else if (opc == SO_MIRROR_CREATE) {
3903 result = mirror_create(fname, mirror_list);
3904 } else if (opc == SO_MIRROR_EXTEND) {
3905 result = mirror_extend(fname, mirror_list,
3907 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
3908 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
3910 "%s: no mirror specified to delete from '%s'\n",
3914 if (lsa.lsa_pool_name)
3915 mirror_flags |= MF_COMP_POOL;
3916 else if (mirror_id != 0)
3917 comp_id = mirror_id;
3919 mirror_flags |= MF_COMP_ID;
3920 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
3922 has_m_file ? mirror_list->m_file :
3924 } else if (layout != NULL) {
3925 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3931 } else if (foreign_mode) {
3932 result = llapi_file_create_foreign(fname, mode, type,
3939 result = llapi_file_open_param(fname,
3948 /* Save the first error encountered. */
3955 if (mode_opt != NULL)
3956 umask(previous_umask);
3959 free(migrate_mdt_param.fp_lmv_md);
3960 llapi_layout_free(layout);
3961 lfs_mirror_list_free(mirror_list);
3966 llapi_layout_free(layout);
3967 lfs_mirror_list_free(mirror_list);
3971 static int lfs_poollist(int argc, char **argv)
3976 return llapi_poollist(argv[1]);
3979 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
3984 char *endptr = "AD";
3989 else if (str[0] == '-')
3995 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
3996 long long val = strtoll(timebuf, &endptr, 0);
4001 unit *= 52; /* 52 weeks + 1 day below */
4002 case 'w': /* fallthrough */
4004 case '\0': /* days are default unit if none used */
4005 case 'd': /* fallthrough */
4006 unit = (unit + (*endptr == 'y')) * 24;
4007 case 'h': /* fallthrough */
4009 case 'm': /* fallthrough */
4011 case 's': /* fallthrough */
4013 /* don't need to multiply by 1 for seconds */
4016 "%s find: bad time string '%s': %s\n",
4017 progname, timebuf, strerror(EINVAL));
4021 if (param->fp_time_margin == 0 ||
4022 (*endptr && unit < param->fp_time_margin))
4023 param->fp_time_margin = unit;
4030 fprintf(stderr, "%s find: bad time '%s': too large\n",
4040 static int name2uid(unsigned int *id, const char *name)
4042 struct passwd *passwd;
4044 passwd = getpwnam(name);
4047 *id = passwd->pw_uid;
4052 static int name2gid(unsigned int *id, const char *name)
4054 struct group *group;
4056 group = getgrnam(name);
4059 *id = group->gr_gid;
4064 static inline int name2projid(unsigned int *id, const char *name)
4069 static int uid2name(char **name, unsigned int id)
4071 struct passwd *passwd;
4073 passwd = getpwuid(id);
4076 *name = passwd->pw_name;
4081 static inline int gid2name(char **name, unsigned int id)
4083 struct group *group;
4085 group = getgrgid(id);
4088 *name = group->gr_name;
4093 static int name2layout(__u32 *layout, char *name)
4095 char *ptr, *layout_name;
4098 for (ptr = name; ; ptr = NULL) {
4099 layout_name = strtok(ptr, ",");
4100 if (layout_name == NULL)
4102 if (strcmp(layout_name, "released") == 0)
4103 *layout |= LOV_PATTERN_F_RELEASED;
4104 else if (strcmp(layout_name, "raid0") == 0)
4105 *layout |= LOV_PATTERN_RAID0;
4106 else if (strcmp(layout_name, "mdt") == 0)
4107 *layout |= LOV_PATTERN_MDT;
4108 else if (strcmp(layout_name, "overstriping") == 0)
4109 *layout |= LOV_PATTERN_OVERSTRIPING;
4116 static int lfs_find(int argc, char **argv)
4121 struct find_param param = {
4124 .fp_time_margin = 24 * 60 * 60,
4126 struct option long_opts[] = {
4127 { .val = 'A', .name = "atime", .has_arg = required_argument },
4128 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4129 { .val = LFS_COMP_COUNT_OPT,
4130 .name = "comp-count", .has_arg = required_argument },
4131 { .val = LFS_COMP_COUNT_OPT,
4132 .name = "component-count",
4133 .has_arg = required_argument },
4134 { .val = LFS_COMP_FLAGS_OPT,
4135 .name = "comp-flags", .has_arg = required_argument },
4136 { .val = LFS_COMP_FLAGS_OPT,
4137 .name = "component-flags",
4138 .has_arg = required_argument },
4139 { .val = LFS_COMP_START_OPT,
4140 .name = "comp-start", .has_arg = required_argument },
4141 { .val = LFS_COMP_START_OPT,
4142 .name = "component-start",
4143 .has_arg = required_argument },
4144 { .val = LFS_MIRROR_STATE_OPT,
4145 .name = "mirror-state", .has_arg = required_argument },
4146 { .val = LFS_LAYOUT_FOREIGN_OPT,
4147 .name = "foreign", .has_arg = optional_argument},
4148 { .val = LFS_NEWERXY_OPT,
4149 .name = "newer", .has_arg = required_argument},
4150 { .val = LFS_NEWERXY_OPT,
4151 .name = "neweraa", .has_arg = required_argument},
4152 { .val = LFS_NEWERXY_OPT,
4153 .name = "neweram", .has_arg = required_argument},
4154 { .val = LFS_NEWERXY_OPT,
4155 .name = "newerac", .has_arg = required_argument},
4156 { .val = LFS_NEWERXY_OPT,
4157 .name = "newerma", .has_arg = required_argument},
4158 { .val = LFS_NEWERXY_OPT,
4159 .name = "newermm", .has_arg = required_argument},
4160 { .val = LFS_NEWERXY_OPT,
4161 .name = "newermc", .has_arg = required_argument},
4162 { .val = LFS_NEWERXY_OPT,
4163 .name = "newerca", .has_arg = required_argument},
4164 { .val = LFS_NEWERXY_OPT,
4165 .name = "newercm", .has_arg = required_argument},
4166 { .val = LFS_NEWERXY_OPT,
4167 .name = "newercc", .has_arg = required_argument},
4168 { .val = LFS_NEWERXY_OPT,
4169 .name = "newerat", .has_arg = required_argument},
4170 { .val = LFS_NEWERXY_OPT,
4171 .name = "newermt", .has_arg = required_argument},
4172 { .val = LFS_NEWERXY_OPT,
4173 .name = "newerct", .has_arg = required_argument},
4174 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4175 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4176 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4177 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4178 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4179 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4180 { .val = 'E', .name = "component-end",
4181 .has_arg = required_argument },
4182 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4183 { .val = LFS_LAYOUT_FOREIGN_OPT,
4184 .name = "foreign", .has_arg = optional_argument},
4185 { .val = 'g', .name = "gid", .has_arg = required_argument },
4186 { .val = 'G', .name = "group", .has_arg = required_argument },
4187 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4188 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4189 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4190 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4191 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4192 { .val = 'L', .name = "layout", .has_arg = required_argument },
4193 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4194 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4195 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4196 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4197 { .val = 'n', .name = "name", .has_arg = required_argument },
4198 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4199 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4200 { .val = 'O', .name = "obd", .has_arg = required_argument },
4201 { .val = 'O', .name = "ost", .has_arg = required_argument },
4202 /* no short option for pool yet, can be 'p' after 2.18 */
4203 { .val = LFS_POOL_OPT,
4204 .name = "pool", .has_arg = required_argument },
4205 { .val = '0', .name = "print0", .has_arg = no_argument },
4206 { .val = 'P', .name = "print", .has_arg = no_argument },
4207 { .val = LFS_PROJID_OPT,
4208 .name = "projid", .has_arg = required_argument },
4209 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4210 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4211 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4212 { .val = 's', .name = "size", .has_arg = required_argument },
4213 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4214 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4215 { .val = 't', .name = "type", .has_arg = required_argument },
4216 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4217 { .val = 'u', .name = "uid", .has_arg = required_argument },
4218 { .val = 'U', .name = "user", .has_arg = required_argument },
4219 { .val = 'z', .name = "extension-size",
4220 .has_arg = required_argument },
4221 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4222 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4223 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4237 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4238 while ((c = getopt_long_only(argc, argv,
4239 "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4240 long_opts, &optidx)) >= 0) {
4245 /* '!' is part of option */
4246 /* when getopt_long_only() finds a string which is not
4247 * an option nor a known option argument it returns 1
4248 * in that case if we already have found pathstart and pathend
4249 * (i.e. we have the list of pathnames),
4250 * the only supported value is "!"
4252 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4253 if (!isoption && pathend != -1) {
4254 fprintf(stderr, "err: %s: filename|dirname must either "
4255 "precede options or follow options\n",
4260 if (!isoption && pathstart == -1)
4261 pathstart = optind - 1;
4262 if (isoption && pathstart != -1 && pathend == -1)
4263 pathend = optind - 2;
4269 /* unknown; opt is "!" or path component,
4270 * checking done above.
4272 if (strcmp(optarg, "!") == 0)
4276 xtime = ¶m.fp_atime;
4277 xsign = ¶m.fp_asign;
4278 param.fp_exclude_atime = !!neg_opt;
4279 /* no break, this falls through to 'C' for ctime */
4282 xtime = ¶m.fp_ctime;
4283 xsign = ¶m.fp_csign;
4284 param.fp_exclude_ctime = !!neg_opt;
4286 /* no break, this falls through to 'M' for mtime */
4289 xtime = ¶m.fp_mtime;
4290 xsign = ¶m.fp_msign;
4291 param.fp_exclude_mtime = !!neg_opt;
4293 rc = set_time(¶m, &t, xtime, optarg);
4294 if (rc == LONG_MAX) {
4302 if (optarg[0] == '+') {
4303 param.fp_blocks_sign = -1;
4305 } else if (optarg[0] == '-') {
4306 param.fp_blocks_sign = 1;
4310 param.fp_blocks_units = 1024;
4311 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
4312 ¶m.fp_blocks_units, 0);
4314 fprintf(stderr, "error: bad blocks '%s'\n",
4318 param.fp_check_blocks = 1;
4319 param.fp_exclude_blocks = !!neg_opt;
4321 case LFS_COMP_COUNT_OPT:
4322 if (optarg[0] == '+') {
4323 param.fp_comp_count_sign = -1;
4325 } else if (optarg[0] == '-') {
4326 param.fp_comp_count_sign = 1;
4330 param.fp_comp_count = strtoul(optarg, &endptr, 0);
4331 if (*endptr != '\0') {
4332 fprintf(stderr, "error: bad component count "
4336 param.fp_check_comp_count = 1;
4337 param.fp_exclude_comp_count = !!neg_opt;
4339 case LFS_COMP_FLAGS_OPT:
4340 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
4341 ¶m.fp_comp_neg_flags);
4343 fprintf(stderr, "error: bad component flags "
4347 param.fp_check_comp_flags = 1;
4349 __u32 flags = param.fp_comp_neg_flags;
4350 param.fp_comp_neg_flags = param.fp_comp_flags;
4351 param.fp_comp_flags = flags;
4354 case LFS_COMP_START_OPT:
4355 if (optarg[0] == '+') {
4356 param.fp_comp_start_sign = -1;
4358 } else if (optarg[0] == '-') {
4359 param.fp_comp_start_sign = 1;
4363 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
4364 ¶m.fp_comp_start_units, 0);
4366 fprintf(stderr, "error: bad component start "
4370 param.fp_check_comp_start = 1;
4371 param.fp_exclude_comp_start = !!neg_opt;
4373 case LFS_MIRROR_STATE_OPT:
4374 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
4375 ¶m.fp_mirror_neg_state);
4378 "error: bad mirrored file state '%s'\n",
4382 param.fp_check_mirror_state = 1;
4384 __u16 state = param.fp_mirror_neg_state;
4385 param.fp_mirror_neg_state =
4386 param.fp_mirror_state;
4387 param.fp_mirror_state = state;
4391 if (optarg[0] == '+') {
4392 param.fp_stripe_count_sign = -1;
4394 } else if (optarg[0] == '-') {
4395 param.fp_stripe_count_sign = 1;
4399 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
4400 if (*endptr != '\0') {
4401 fprintf(stderr,"error: bad stripe_count '%s'\n",
4406 param.fp_check_stripe_count = 1;
4407 param.fp_exclude_stripe_count = !!neg_opt;
4410 param.fp_max_depth = strtol(optarg, 0, 0);
4413 if (optarg[0] == '+') {
4414 param.fp_comp_end_sign = -1;
4416 } else if (optarg[0] == '-') {
4417 param.fp_comp_end_sign = 1;
4421 if (arg_is_eof(optarg)) {
4422 param.fp_comp_end = LUSTRE_EOF;
4423 param.fp_comp_end_units = 1;
4426 rc = llapi_parse_size(optarg,
4428 ¶m.fp_comp_end_units, 0);
4431 fprintf(stderr, "error: bad component end "
4435 param.fp_check_comp_end = 1;
4436 param.fp_exclude_comp_end = !!neg_opt;
4438 case LFS_LAYOUT_FOREIGN_OPT: {
4439 /* all types by default */
4440 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
4442 if (optarg != NULL) {
4443 /* check pure numeric */
4444 type = strtoul(optarg, &endptr, 0);
4447 type = check_foreign_type_name(optarg);
4448 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
4450 "%s %s: unknown foreign type '%s'\n",
4457 param.fp_foreign_type = type;
4458 param.fp_check_foreign = 1;
4459 param.fp_exclude_foreign = !!neg_opt;
4462 case LFS_NEWERXY_OPT: {
4468 time_t ref = time(NULL);
4470 /* no need to check bad options, they won't get here */
4471 if (strlen(long_opts[optidx].name) == 7) {
4472 x = long_opts[optidx].name[5];
4473 y = long_opts[optidx].name[6];
4477 static const char *const fmts[] = {
4478 "%Y-%m-%d %H:%M:%S",
4481 "%H:%M:%S", /* sometime today */
4490 for (i = 0; fmts[i] != NULL; i++) {
4493 /* Init for times relative to today */
4494 if (strncmp(fmts[i], "%H", 2) == 0)
4495 localtime_r(&ref, &tm);
4497 memset(&tm, 0, sizeof(tm));
4498 ptr = strptime(optarg, fmts[i], &tm);
4500 while (ptr && isspace(*ptr))
4502 if (ptr == optarg + strlen(optarg)) {
4510 "%s: invalid time '%s'\n",
4513 "supported formats are:\n ");
4514 for (i = 0; fmts[i] != NULL; i++)
4515 fprintf(stderr, "'%s', ",
4517 fprintf(stderr, "\n");
4524 struct stat statbuf;
4526 if (stat(optarg, &statbuf) < 0) {
4528 "%s: cannot stat file '%s': %s\n",
4537 ref = statbuf.st_atime;
4540 ref = statbuf.st_mtime;
4543 ref = statbuf.st_ctime;
4547 "%s: invalid Y argument: '%c'\n",
4556 xidx = NEWERXY_ATIME;
4559 xidx = NEWERXY_MTIME;
4562 xidx = NEWERXY_CTIME;
4566 "%s: invalid X argument: '%c'\n",
4573 newery = ¶m.fp_newery[xidx][negidx];
4579 *newery = *newery > ref ? ref : *newery;
4581 *newery = *newery > ref ? *newery : ref;
4583 param.fp_newerxy = 1;
4588 rc = name2gid(¶m.fp_gid, optarg);
4590 param.fp_gid = strtoul(optarg, &endptr, 10);
4591 if (*endptr != '\0') {
4592 fprintf(stderr, "Group/GID: %s cannot "
4593 "be found.\n", optarg);
4598 param.fp_exclude_gid = !!neg_opt;
4599 param.fp_check_gid = 1;
4602 param.fp_hash_type = check_hashtype(optarg);
4603 if (param.fp_hash_type == 0) {
4604 fprintf(stderr, "error: bad hash_type '%s'\n",
4609 param.fp_check_hash_type = 1;
4610 param.fp_exclude_hash_type = !!neg_opt;
4616 ret = name2layout(¶m.fp_layout, optarg);
4619 param.fp_exclude_layout = !!neg_opt;
4620 param.fp_check_layout = 1;
4624 rc = name2uid(¶m.fp_uid, optarg);
4626 param.fp_uid = strtoul(optarg, &endptr, 10);
4627 if (*endptr != '\0') {
4628 fprintf(stderr, "User/UID: %s cannot "
4629 "be found.\n", optarg);
4634 param.fp_exclude_uid = !!neg_opt;
4635 param.fp_check_uid = 1;
4638 param.fp_pattern = (char *)optarg;
4639 param.fp_exclude_pattern = !!neg_opt;
4642 if (optarg[0] == '+') {
4643 param.fp_mirror_count_sign = -1;
4645 } else if (optarg[0] == '-') {
4646 param.fp_mirror_count_sign = 1;
4650 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
4651 if (*endptr != '\0') {
4653 "error: bad mirror count '%s'\n",
4657 param.fp_check_mirror_count = 1;
4658 param.fp_exclude_mirror_count = !!neg_opt;
4663 char *buf, *token, *next, *p;
4667 buf = strdup(optarg);
4673 param.fp_exclude_obd = !!neg_opt;
4676 while (token && *token) {
4677 token = strchr(token, ',');
4684 param.fp_exclude_mdt = !!neg_opt;
4685 param.fp_num_alloc_mdts += len;
4686 tmp = realloc(param.fp_mdt_uuid,
4687 param.fp_num_alloc_mdts *
4688 sizeof(*param.fp_mdt_uuid));
4694 param.fp_mdt_uuid = tmp;
4696 param.fp_exclude_obd = !!neg_opt;
4697 param.fp_num_alloc_obds += len;
4698 tmp = realloc(param.fp_obd_uuid,
4699 param.fp_num_alloc_obds *
4700 sizeof(*param.fp_obd_uuid));
4706 param.fp_obd_uuid = tmp;
4708 for (token = buf; token && *token; token = next) {
4709 struct obd_uuid *puuid;
4712 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
4715 ¶m.fp_obd_uuid[param.fp_num_obds++];
4717 p = strchr(token, ',');
4724 if (strlen(token) > sizeof(puuid->uuid) - 1) {
4729 strncpy(puuid->uuid, token,
4730 sizeof(puuid->uuid));
4737 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
4741 if (strlen(optarg) > LOV_MAXPOOLNAME) {
4743 "Pool name %s is too long (max %d)\n",
4744 optarg, LOV_MAXPOOLNAME);
4749 * We do check for empty pool because empty pool
4750 * is used to find V1 LOV attributes
4752 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
4753 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
4754 param.fp_exclude_pool = !!neg_opt;
4755 param.fp_check_pool = 1;
4757 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
4758 case 'p': /* want this for --pool, to match getstripe/find */
4760 "warning: -p deprecated, use --print0 or -0\n");
4763 param.fp_zero_end = 1;
4765 case 'P': /* we always print, this option is a no-op */
4767 case LFS_PROJID_OPT:
4768 rc = name2projid(¶m.fp_projid, optarg);
4770 param.fp_projid = strtoul(optarg, &endptr, 10);
4771 if (*endptr != '\0') {
4773 "Invalid project ID: %s",
4779 param.fp_exclude_projid = !!neg_opt;
4780 param.fp_check_projid = 1;
4783 if (optarg[0] == '+') {
4784 param.fp_size_sign = -1;
4786 } else if (optarg[0] == '-') {
4787 param.fp_size_sign = 1;
4791 ret = llapi_parse_size(optarg, ¶m.fp_size,
4792 ¶m.fp_size_units, 0);
4794 fprintf(stderr, "error: bad file size '%s'\n",
4798 param.fp_check_size = 1;
4799 param.fp_exclude_size = !!neg_opt;
4802 if (optarg[0] == '+') {
4803 param.fp_stripe_size_sign = -1;
4805 } else if (optarg[0] == '-') {
4806 param.fp_stripe_size_sign = 1;
4810 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
4811 ¶m.fp_stripe_size_units, 0);
4813 fprintf(stderr, "error: bad stripe_size '%s'\n",
4817 param.fp_check_stripe_size = 1;
4818 param.fp_exclude_stripe_size = !!neg_opt;
4821 param.fp_exclude_type = !!neg_opt;
4822 switch (optarg[0]) {
4824 param.fp_type = S_IFBLK;
4827 param.fp_type = S_IFCHR;
4830 param.fp_type = S_IFDIR;
4833 param.fp_type = S_IFREG;
4836 param.fp_type = S_IFLNK;
4839 param.fp_type = S_IFIFO;
4842 param.fp_type = S_IFSOCK;
4845 fprintf(stderr, "error: %s: bad type '%s'\n",
4852 if (optarg[0] == '+') {
4853 param.fp_mdt_count_sign = -1;
4855 } else if (optarg[0] == '-') {
4856 param.fp_mdt_count_sign = 1;
4860 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4861 if (*endptr != '\0') {
4862 fprintf(stderr, "error: bad mdt_count '%s'\n",
4867 param.fp_check_mdt_count = 1;
4868 param.fp_exclude_mdt_count = !!neg_opt;
4871 if (optarg[0] == '+') {
4872 param.fp_ext_size_sign = -1;
4874 } else if (optarg[0] == '-') {
4875 param.fp_ext_size_sign = 1;
4879 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
4880 ¶m.fp_ext_size_units, 0);
4882 fprintf(stderr, "error: bad ext-size '%s'\n",
4886 param.fp_ext_size /= SEL_UNIT_SIZE;
4887 param.fp_ext_size_units /= SEL_UNIT_SIZE;
4888 param.fp_check_ext_size = 1;
4889 param.fp_exclude_ext_size = !!neg_opt;
4897 if (pathstart == -1) {
4898 fprintf(stderr, "error: %s: no filename|pathname\n",
4902 } else if (pathend == -1) {
4908 rc = llapi_find(argv[pathstart], ¶m);
4911 pathbad = pathstart;
4913 } while (++pathstart < pathend);
4916 fprintf(stderr, "%s: failed for '%s': %s\n",
4917 progname, argv[pathbad], strerror(-rc));
4920 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4921 free(param.fp_obd_uuid);
4923 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4924 free(param.fp_mdt_uuid);
4929 static int lfs_getstripe_internal(int argc, char **argv,
4930 struct find_param *param)
4932 struct option long_opts[] = {
4933 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
4934 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
4935 { .val = LFS_COMP_COUNT_OPT,
4936 .name = "comp-count", .has_arg = no_argument },
4937 { .val = LFS_COMP_COUNT_OPT,
4938 .name = "component-count", .has_arg = no_argument },
4939 { .val = LFS_COMP_FLAGS_OPT,
4940 .name = "comp-flags", .has_arg = optional_argument },
4941 { .val = LFS_COMP_FLAGS_OPT,
4942 .name = "component-flags", .has_arg = optional_argument },
4943 { .val = LFS_COMP_START_OPT,
4944 .name = "comp-start", .has_arg = optional_argument },
4945 { .val = LFS_COMP_START_OPT,
4946 .name = "component-start", .has_arg = optional_argument },
4947 { .val = LFS_MIRROR_INDEX_OPT,
4948 .name = "mirror-index", .has_arg = required_argument },
4949 { .val = LFS_MIRROR_ID_OPT,
4950 .name = "mirror-id", .has_arg = required_argument },
4951 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
4952 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
4953 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
4954 { .val = 'd', .name = "directory", .has_arg = no_argument },
4955 { .val = 'D', .name = "default", .has_arg = no_argument },
4956 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
4957 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
4958 { .val = 'F', .name = "fid", .has_arg = no_argument },
4959 { .val = 'g', .name = "generation", .has_arg = no_argument },
4960 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
4961 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
4962 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
4963 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
4964 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
4965 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
4966 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
4967 { .val = 'L', .name = "layout", .has_arg = no_argument },
4968 { .val = 'm', .name = "mdt", .has_arg = no_argument },
4969 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
4970 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
4971 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
4972 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
4973 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
4974 { .val = 'O', .name = "obd", .has_arg = required_argument },
4975 { .val = 'O', .name = "ost", .has_arg = required_argument },
4976 { .val = 'p', .name = "pool", .has_arg = no_argument },
4977 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
4978 { .val = 'q', .name = "quiet", .has_arg = no_argument },
4979 { .val = 'r', .name = "recursive", .has_arg = no_argument },
4980 { .val = 'R', .name = "raw", .has_arg = no_argument },
4981 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
4982 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
4983 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
4984 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4985 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
4986 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
4987 { .val = 'v', .name = "verbose", .has_arg = no_argument },
4988 { .val = 'y', .name = "yaml", .has_arg = no_argument },
4989 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
4990 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
4994 int pathstart = -1, pathend = -1;
4998 while ((c = getopt_long(argc, argv,
4999 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5000 long_opts, NULL)) != -1) {
5004 /* '!' is part of option */
5005 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5006 if (!isoption && pathend != -1) {
5008 "error: %s: filename|dirname must either precede options or follow options\n",
5012 if (!isoption && pathstart == -1)
5013 pathstart = optind - 1;
5014 if (isoption && pathstart != -1 && pathend == -1)
5015 pathend = optind - 2;
5019 /* unknown: opt is "!" */
5020 if (strcmp(optarg, "!") == 0)
5024 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5025 param->fp_verbose |= VERBOSE_COUNT;
5026 param->fp_max_depth = 0;
5029 case LFS_COMP_COUNT_OPT:
5030 param->fp_verbose |= VERBOSE_COMP_COUNT;
5031 param->fp_max_depth = 0;
5033 case LFS_COMP_FLAGS_OPT:
5034 if (optarg != NULL) {
5035 rc = comp_str2flags(optarg,
5036 ¶m->fp_comp_flags,
5037 ¶m->fp_comp_neg_flags);
5039 fprintf(stderr, "error: %s bad "
5040 "component flags '%s'.\n",
5044 param->fp_check_comp_flags = 1;
5046 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5047 param->fp_max_depth = 0;
5050 case LFS_COMP_START_OPT:
5051 if (optarg != NULL) {
5053 if (tmp[0] == '+') {
5054 param->fp_comp_start_sign = -1;
5056 } else if (tmp[0] == '-') {
5057 param->fp_comp_start_sign = 1;
5060 rc = llapi_parse_size(tmp,
5061 ¶m->fp_comp_start,
5062 ¶m->fp_comp_start_units, 0);
5064 fprintf(stderr, "error: %s bad "
5065 "component start '%s'.\n",
5069 param->fp_check_comp_start = 1;
5072 param->fp_verbose |= VERBOSE_COMP_START;
5073 param->fp_max_depth = 0;
5076 case LFS_MIRROR_INDEX_OPT:
5077 if (optarg[0] == '+') {
5078 param->fp_mirror_index_sign = -1;
5080 } else if (optarg[0] == '-') {
5081 param->fp_mirror_index_sign = 1;
5085 param->fp_mirror_index = strtoul(optarg, &end, 0);
5086 if (*end != '\0' || (param->fp_mirror_index == 0 &&
5087 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5089 "%s %s: invalid mirror index '%s'\n",
5090 progname, argv[0], optarg);
5093 if (param->fp_mirror_id != 0) {
5095 "%s %s: can't specify both mirror index and mirror ID\n",
5099 param->fp_check_mirror_index = 1;
5100 param->fp_exclude_mirror_index = !!neg_opt;
5102 case LFS_MIRROR_ID_OPT:
5103 if (optarg[0] == '+') {
5104 param->fp_mirror_id_sign = -1;
5106 } else if (optarg[0] == '-') {
5107 param->fp_mirror_id_sign = 1;
5111 param->fp_mirror_id = strtoul(optarg, &end, 0);
5112 if (*end != '\0' || (param->fp_mirror_id == 0 &&
5113 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5115 "%s %s: invalid mirror ID '%s'\n",
5116 progname, argv[0], optarg);
5119 if (param->fp_mirror_index != 0) {
5121 "%s %s: can't specify both mirror index and mirror ID\n",
5125 param->fp_check_mirror_id = 1;
5126 param->fp_exclude_mirror_id = !!neg_opt;
5129 param->fp_max_depth = 0;
5132 param->fp_get_default_lmv = 1;
5135 if (optarg != NULL) {
5137 if (tmp[0] == '+') {
5138 param->fp_comp_end_sign = -1;
5140 } else if (tmp[0] == '-') {
5141 param->fp_comp_end_sign = 1;
5145 if (arg_is_eof(tmp)) {
5146 param->fp_comp_end = LUSTRE_EOF;
5147 param->fp_comp_end_units = 1;
5150 rc = llapi_parse_size(tmp,
5151 ¶m->fp_comp_end,
5152 ¶m->fp_comp_end_units, 0);
5155 fprintf(stderr, "error: %s bad "
5156 "component end '%s'.\n",
5160 param->fp_check_comp_end = 1;
5162 param->fp_verbose |= VERBOSE_COMP_END;
5163 param->fp_max_depth = 0;
5167 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5168 param->fp_verbose |= VERBOSE_DFID;
5169 param->fp_max_depth = 0;
5173 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5174 param->fp_verbose |= VERBOSE_GENERATION;
5175 param->fp_max_depth = 0;
5179 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5180 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5181 param->fp_max_depth = 0;
5185 if (optarg != NULL) {
5186 param->fp_comp_id = strtoul(optarg, &end, 0);
5187 if (*end != '\0' || param->fp_comp_id == 0 ||
5188 param->fp_comp_id > LCME_ID_MAX) {
5189 fprintf(stderr, "error: %s bad "
5190 "component id '%s'\n",
5194 param->fp_check_comp_id = 1;
5197 param->fp_max_depth = 0;
5198 param->fp_verbose |= VERBOSE_COMP_ID;
5202 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5203 param->fp_verbose |= VERBOSE_PATTERN;
5204 param->fp_max_depth = 0;
5207 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5209 fprintf(stderr, "warning: '-M' deprecated"
5210 ", use '--mdt-index' or '-m' instead\n");
5213 if (!(param->fp_verbose & VERBOSE_DETAIL))
5214 param->fp_max_depth = 0;
5215 param->fp_verbose |= VERBOSE_MDTINDEX;
5218 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5219 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
5220 param->fp_max_depth = 0;
5224 if (param->fp_obd_uuid) {
5226 "error: %s: only one obduuid allowed",
5230 param->fp_obd_uuid = (struct obd_uuid *)optarg;
5233 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5234 param->fp_verbose |= VERBOSE_POOL;
5235 param->fp_max_depth = 0;
5242 param->fp_recursive = 1;
5248 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5249 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
5250 param->fp_max_depth = 0;
5254 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
5260 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5261 param->fp_verbose |= VERBOSE_EXT_SIZE;
5262 param->fp_max_depth = 0;
5270 if (pathstart == -1) {
5271 fprintf(stderr, "error: %s: no filename|pathname\n",
5274 } else if (pathend == -1) {
5282 if (param->fp_recursive)
5283 param->fp_max_depth = -1;
5284 else if (param->fp_verbose & VERBOSE_DETAIL)
5285 param->fp_max_depth = 1;
5287 if (!param->fp_verbose)
5288 param->fp_verbose = VERBOSE_DEFAULT;
5289 if (param->fp_quiet)
5290 param->fp_verbose = VERBOSE_OBJID;
5293 rc = llapi_getstripe(argv[pathstart], param);
5294 } while (++pathstart < pathend && !rc);
5297 fprintf(stderr, "error: %s failed for %s.\n",
5298 argv[0], argv[optind - 1]);
5302 static int lfs_tgts(int argc, char **argv)
5304 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5305 struct find_param param;
5306 int index = 0, rc=0;
5311 if (argc == 2 && !realpath(argv[1], path)) {
5313 fprintf(stderr, "error: invalid path '%s': %s\n",
5314 argv[1], strerror(-rc));
5318 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
5319 /* Check if we have a mount point */
5320 if (mntdir[0] == '\0')
5323 memset(¶m, 0, sizeof(param));
5324 if (!strcmp(argv[0], "mdts"))
5325 param.fp_get_lmv = 1;
5327 rc = llapi_ostlist(mntdir, ¶m);
5329 fprintf(stderr, "error: %s: failed on %s\n",
5332 if (path[0] != '\0')
5334 memset(mntdir, 0, PATH_MAX);
5340 static int lfs_getstripe(int argc, char **argv)
5342 struct find_param param = { 0 };
5344 param.fp_max_depth = 1;
5345 return lfs_getstripe_internal(argc, argv, ¶m);
5349 static int lfs_getdirstripe(int argc, char **argv)
5351 struct find_param param = { 0 };
5352 struct option long_opts[] = {
5353 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
5354 { .val = 'D', .name = "default", .has_arg = no_argument },
5355 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
5356 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
5357 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5358 { .val = 'O', .name = "obd", .has_arg = required_argument },
5359 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5360 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
5361 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5362 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5366 param.fp_get_lmv = 1;
5368 while ((c = getopt_long(argc, argv,
5369 "cDHimO:rtTvy", long_opts, NULL)) != -1)
5374 param.fp_verbose |= VERBOSE_COUNT;
5377 param.fp_get_default_lmv = 1;
5381 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
5383 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5385 fprintf(stderr, "warning: '-t' deprecated, "
5386 "use '--mdt-hash' or '-H' instead\n");
5389 param.fp_verbose |= VERBOSE_HASH_TYPE;
5392 if (param.fp_obd_uuid) {
5394 "error: %s: only one obduuid allowed",
5398 param.fp_obd_uuid = (struct obd_uuid *)optarg;
5401 param.fp_recursive = 1;
5404 param.fp_verbose |= VERBOSE_DETAIL;
5410 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5411 progname, argv[0], argv[optind - 1]);
5419 if (param.fp_recursive)
5420 param.fp_max_depth = -1;
5422 if (!param.fp_verbose)
5423 param.fp_verbose = VERBOSE_DEFAULT;
5426 rc = llapi_getstripe(argv[optind], ¶m);
5427 } while (++optind < argc && !rc);
5430 fprintf(stderr, "error: %s failed for %s.\n",
5431 argv[0], argv[optind - 1]);
5436 MNTDF_INODES = 0x0001,
5437 MNTDF_COOKED = 0x0002,
5438 MNTDF_LAZY = 0x0004,
5439 MNTDF_VERBOSE = 0x0008,
5440 MNTDF_SHOW = 0x0010,
5443 #define COOK(value) \
5446 while (value > 1024) { \
5454 #define CDF "%11llu"
5455 #define HDF "%8.1f%c"
5459 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
5461 double avail, used, ratio = 0;
5464 avail = st->os_ffree;
5465 used = st->os_files - st->os_ffree;
5467 avail = st->os_bavail;
5468 used = st->os_blocks - st->os_bfree;
5470 if (avail + used > 0)
5471 ratio = used / (used + avail) * 100;
5473 /* Round up to match df(1) usage percentage */
5474 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
5477 /* This is to identify various problem states for "lfs df" if .osn_err = true,
5478 * so only show flags reflecting those states by default. Informational states
5479 * are only shown with "-v" and use lower-case names to distinguish them.
5480 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
5482 static struct obd_statfs_state_names {
5483 enum obd_statfs_state osn_state;
5484 const char osn_name;
5487 { .osn_state = OS_STATE_DEGRADED, .osn_name = 'D', .osn_err = true },
5488 { .osn_state = OS_STATE_READONLY, .osn_name = 'R', .osn_err = true },
5489 { .osn_state = OS_STATE_NOPRECREATE, .osn_name = 'N', .osn_err = true },
5490 { .osn_state = OS_STATE_UNUSED1, .osn_name = '?', .osn_err = true },
5491 { .osn_state = OS_STATE_UNUSED2, .osn_name = '?', .osn_err = true },
5492 { .osn_state = OS_STATE_ENOSPC, .osn_name = 'S', .osn_err = true },
5493 { .osn_state = OS_STATE_ENOINO, .osn_name = 'I', .osn_err = true },
5494 { .osn_state = OS_STATE_SUM, .osn_name = 'a', /* aggregate */ },
5495 { .osn_state = OS_STATE_NONROT, .osn_name = 'f', /* flash */ },
5498 static int showdf(char *mntdir, struct obd_statfs *stat,
5499 char *uuid, enum mntdf_flags flags,
5500 char *type, int index, int rc)
5502 long long avail, used, total;
5504 char *suffix = "KMGTPEZY";
5505 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
5506 char tbuf[3 * sizeof(__u64)];
5507 char ubuf[3 * sizeof(__u64)];
5508 char abuf[3 * sizeof(__u64)];
5509 char rbuf[3 * sizeof(__u64)];
5516 if (flags & MNTDF_INODES) {
5517 avail = stat->os_ffree;
5518 used = stat->os_files - stat->os_ffree;
5519 total = stat->os_files;
5521 int shift = flags & MNTDF_COOKED ? 0 : 10;
5523 avail = (stat->os_bavail * stat->os_bsize) >> shift;
5524 used = ((stat->os_blocks - stat->os_bfree) *
5525 stat->os_bsize) >> shift;
5526 total = (stat->os_blocks * stat->os_bsize) >> shift;
5529 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
5531 if (flags & MNTDF_COOKED) {
5535 cook_val = (double)total;
5538 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
5541 snprintf(tbuf, sizeof(tbuf), CDF, total);
5543 cook_val = (double)used;
5546 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
5549 snprintf(ubuf, sizeof(ubuf), CDF, used);
5551 cook_val = (double)avail;
5554 snprintf(abuf, sizeof(abuf), HDF, cook_val,
5557 snprintf(abuf, sizeof(abuf), CDF, avail);
5559 snprintf(tbuf, sizeof(tbuf), CDF, total);
5560 snprintf(ubuf, sizeof(tbuf), CDF, used);
5561 snprintf(abuf, sizeof(tbuf), CDF, avail);
5564 sprintf(rbuf, RDF, ratio);
5565 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
5566 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
5568 printf("[%s:%d]", type, index);
5570 if (stat->os_state) {
5574 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
5575 if (oss_names[i].osn_state & stat->os_state &&
5576 (oss_names[i].osn_err ||
5577 flags & MNTDF_VERBOSE))
5578 printf("%c", oss_names[i].osn_name);
5585 printf(UUF": inactive device\n", uuid);
5588 printf(UUF": %s\n", uuid, strerror(-rc));
5595 struct ll_stat_type {
5600 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
5602 struct ll_statfs_data {
5604 struct obd_statfs sd_st;
5607 struct ll_statfs_buf {
5609 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
5612 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
5613 int ops, struct ll_statfs_buf *lsb)
5615 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
5616 struct obd_uuid uuid_buf;
5617 char *poolname = NULL;
5618 struct ll_stat_type types[] = {
5619 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
5620 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
5621 { .st_name = NULL } };
5622 struct ll_stat_type *tp;
5623 __u64 ost_files = 0;
5624 __u64 ost_ffree = 0;
5632 poolname = strchr(pool, '.');
5633 if (poolname != NULL) {
5634 if (strncmp(fsname, pool, strlen(fsname))) {
5635 fprintf(stderr, "filesystem name incorrect\n");
5643 fd = open(mntdir, O_RDONLY);
5646 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
5651 if (flags & MNTDF_SHOW) {
5652 if (flags & MNTDF_INODES)
5653 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5654 "UUID", "Inodes", "IUsed", "IFree",
5655 "IUse%", "Mounted on");
5657 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
5659 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
5660 "Used", "Available", "Use%", "Mounted on");
5663 for (tp = types; tp->st_name != NULL; tp++) {
5664 bool have_ost = false;
5666 if (!(tp->st_op & ops))
5669 for (index = 0; ; index++) {
5670 memset(&stat_buf, 0, sizeof(struct obd_statfs));
5671 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
5672 type = flags & MNTDF_LAZY ?
5673 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
5674 rc2 = llapi_obd_fstatfs(fd, type, index,
5675 &stat_buf, &uuid_buf);
5680 if (rc2 == -ENODATA) { /* Inactive device, OK. */
5681 if (!(flags & MNTDF_VERBOSE))
5683 } else if (rc2 < 0 && rc == 0) {
5687 /* If we have OSTs then don't report MDT block counts.
5688 * For MDT-only filesystems the expectation is that all
5689 * layouts have a DoM component. For filesystems with
5690 * OSTs, files are not necessarily going to store data
5691 * on MDTs, and MDT space is limited to a fraction of
5692 * OST space, so don't include it in the summary.
5694 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
5701 if (poolname && tp->st_op == LL_STATFS_LOV &&
5702 llapi_search_ost(fsname, poolname,
5703 obd_uuid2str(&uuid_buf)) != 1)
5706 /* the llapi_obd_statfs() call may have returned with
5707 * an error, but if it filled in uuid_buf we will at
5708 * lease use that to print out a message for that OBD.
5709 * If we didn't get anything in the uuid_buf, then fill
5710 * it in so that we can print an error message. */
5711 if (uuid_buf.uuid[0] == '\0')
5712 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
5713 "%s%04x", tp->st_name, index);
5715 lsb->sb_buf[lsb->sb_count].sd_index = index;
5716 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
5719 if (flags & MNTDF_SHOW)
5720 showdf(mntdir, &stat_buf,
5721 obd_uuid2str(&uuid_buf), flags,
5722 tp->st_name, index, rc2);
5727 if (tp->st_op == LL_STATFS_LMV) {
5728 sum.os_ffree += stat_buf.os_ffree;
5729 sum.os_files += stat_buf.os_files;
5730 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
5731 ost_files += stat_buf.os_files;
5732 ost_ffree += stat_buf.os_ffree;
5734 sum.os_blocks += stat_buf.os_blocks *
5736 sum.os_bfree += stat_buf.os_bfree *
5738 sum.os_bavail += stat_buf.os_bavail *
5745 /* If we have _some_ OSTs, but don't have as many free objects on the
5746 * OST as inodes on the MDTs, reduce the reported number of inodes
5747 * to compensate, so that the "inodes in use" number is correct.
5748 * This should be kept in sync with ll_statfs_internal().
5750 if (ost_files && ost_ffree < sum.os_ffree) {
5751 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
5752 sum.os_ffree = ost_ffree;
5754 if (flags & MNTDF_SHOW) {
5756 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
5764 static int lfs_setdirstripe(int argc, char **argv)
5767 struct lfs_setstripe_args lsa = { 0 };
5768 struct llapi_stripe_param *param = NULL;
5769 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
5772 char *mode_opt = NULL;
5773 bool default_stripe = false;
5774 bool delete = false;
5775 bool foreign_mode = false;
5776 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
5777 mode_t previous_mode = 0;
5779 __u32 type = LU_FOREIGN_TYPE_DAOS, flags = 0;
5780 struct option long_opts[] = {
5781 { .val = 'c', .name = "count", .has_arg = required_argument },
5782 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
5783 { .val = 'd', .name = "delete", .has_arg = no_argument },
5784 { .val = 'D', .name = "default", .has_arg = no_argument },
5785 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
5786 { .val = LFS_LAYOUT_FLAGS_OPT,
5787 .name = "flags", .has_arg = required_argument },
5788 { .val = LFS_LAYOUT_FOREIGN_OPT,
5789 .name = "foreign", .has_arg = optional_argument},
5790 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
5791 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
5792 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
5793 { .val = 'i', .name = "mdt", .has_arg = required_argument },
5795 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5796 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
5797 { .val = 'm', .name = "mdt", .has_arg = required_argument },
5799 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5800 { .val = 'i', .name = "index", .has_arg = required_argument },
5802 { .val = 'o', .name = "mode", .has_arg = required_argument },
5803 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5804 { .val = 't', .name = "hash-type", .has_arg = required_argument },
5806 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
5807 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
5808 { .val = 'x', .name = "xattr", .has_arg = required_argument },
5812 setstripe_args_init(&lsa);
5814 while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:",
5815 long_opts, NULL)) >= 0) {
5822 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
5825 "%s %s: invalid stripe count '%s'\n",
5826 progname, argv[0], optarg);
5832 default_stripe = true;
5835 default_stripe = true;
5837 case LFS_LAYOUT_FOREIGN_OPT:
5838 if (optarg != NULL) {
5839 /* check pure numeric */
5840 type = strtoul(optarg, &end, 0);
5843 type = check_foreign_type_name(optarg);
5844 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5846 "%s %s: unknown foreign type '%s'\n",
5853 foreign_mode = true;
5855 case LFS_LAYOUT_FLAGS_OPT:
5856 flags = strtoul(optarg, &end, 16);
5859 "%s %s: bad flags '%s'\n",
5860 progname, argv[0], optarg);
5864 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5866 fprintf(stderr, "warning: '--hash-type' and '-t' "
5867 "deprecated, use '--mdt-hash' or '-H' instead\n");
5870 lsa.lsa_pattern = check_hashtype(optarg);
5871 if (lsa.lsa_pattern == 0) {
5873 "%s %s: bad stripe hash type '%s'\n",
5874 progname, argv[0], optarg);
5879 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
5882 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5883 if (strcmp(argv[optind - 1], "--index") == 0)
5885 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
5888 lsa.lsa_nr_tgts = parse_targets(mdts,
5889 sizeof(mdts) / sizeof(__u32),
5890 lsa.lsa_nr_tgts, optarg, NULL);
5891 if (lsa.lsa_nr_tgts < 0) {
5893 "%s %s: invalid MDT target(s) '%s'\n",
5894 progname, argv[0], optarg);
5898 lsa.lsa_tgts = mdts;
5899 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5900 lsa.lsa_stripe_off = mdts[0];
5902 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
5904 fprintf(stderr, "warning: '-m' is deprecated, "
5905 "use '--mode' or '-o' instead\n");
5914 fprintf(stderr, "%s %s: unrecognized option '%s'\n",
5915 progname, argv[0], argv[optind - 1]);
5920 if (optind == argc) {
5921 fprintf(stderr, "%s %s: DIR must be specified\n",
5926 if (xattr && !foreign_mode) {
5927 /* only print a warning as this is armless and will be
5931 "%s %s: xattr has been specified for non-foreign layout\n",
5933 } else if (foreign_mode && !xattr) {
5935 "%s %s: xattr must be provided in foreign mode\n",
5940 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
5941 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
5943 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
5948 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5949 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
5951 "%s %s: stripe offset and count must be specified\n",
5957 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5958 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5960 "%s %s: cannot specify -d with -c or -i options\n",
5965 if (mode_opt != NULL) {
5966 mode = strtoul(mode_opt, &end, 8);
5969 "%s %s: bad MODE '%s'\n",
5970 progname, argv[0], mode_opt);
5973 previous_mode = umask(0);
5976 /* foreign LMV/dir case */
5978 if (argc > optind + 1) {
5980 "%s %s: cannot specify multiple foreign dirs\n",
5985 dname = argv[optind];
5986 result = llapi_dir_create_foreign(dname, mode, type, flags,
5990 "%s mkdir: can't create foreign dir '%s': %s\n",
5991 progname, dname, strerror(-result));
5996 * initialize stripe parameters, in case param is converted to specific,
5997 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5999 param = calloc(1, offsetof(typeof(*param),
6000 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6001 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6002 if (param == NULL) {
6004 "%s %s: cannot allocate memory for parameters: %s\n",
6005 progname, argv[0], strerror(ENOMEM));
6009 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6010 param->lsp_stripe_count = lsa.lsa_stripe_count;
6011 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6012 param->lsp_stripe_offset = -1;
6014 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6015 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6016 param->lsp_stripe_pattern = lsa.lsa_pattern;
6018 param->lsp_stripe_pattern = LMV_HASH_TYPE_DEFAULT;
6019 param->lsp_pool = lsa.lsa_pool_name;
6020 param->lsp_is_specific = false;
6021 if (lsa.lsa_nr_tgts > 1) {
6022 if (lsa.lsa_stripe_count > 0 &&
6023 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6024 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6026 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6027 argv[0], lsa.lsa_stripe_count,
6033 param->lsp_is_specific = true;
6034 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6035 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6038 dname = argv[optind];
6040 if (default_stripe) {
6041 result = llapi_dir_set_default_lmv(dname, param);
6044 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6045 progname, dname, strerror(-result));
6049 result = llapi_dir_create(dname, mode, param);
6052 "%s setdirstripe: cannot create dir '%s': %s\n",
6053 progname, dname, strerror(-result));
6054 } while (!result && (dname = argv[++optind]));
6056 if (mode_opt != NULL)
6057 umask(previous_mode);
6064 static int lfs_rmentry(int argc, char **argv)
6071 fprintf(stderr, "error: %s: missing dirname\n",
6077 dname = argv[index];
6078 while (dname != NULL) {
6079 result = llapi_direntry_remove(dname);
6081 fprintf(stderr, "error: %s: remove dir entry '%s' "
6082 "failed\n", argv[0], dname);
6085 dname = argv[++index];
6090 static int lfs_mv(int argc, char **argv)
6092 struct lmv_user_md lmu = { LMV_USER_MAGIC };
6093 struct find_param param = {
6100 struct option long_opts[] = {
6101 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6102 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6103 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6106 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
6108 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6110 fprintf(stderr, "warning: '-M' deprecated"
6111 ", use '--mdt-index' or '-m' instead\n");
6114 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
6116 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
6122 param.fp_verbose = VERBOSE_DETAIL;
6125 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
6126 progname, argv[optind - 1]);
6131 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
6132 fprintf(stderr, "%s mv: MDT index must be specified\n",
6137 if (optind >= argc) {
6138 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
6143 /* initialize migrate mdt parameters */
6144 param.fp_lmv_md = &lmu;
6145 param.fp_migrate = 1;
6146 rc = llapi_migrate_mdt(argv[optind], ¶m);
6148 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
6149 progname, argv[optind], param.fp_mdt_index,
6154 static int lfs_osts(int argc, char **argv)
6156 return lfs_tgts(argc, argv);
6159 static int lfs_mdts(int argc, char **argv)
6161 return lfs_tgts(argc, argv);
6164 static int lfs_df(int argc, char **argv)
6166 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6167 enum mntdf_flags flags = MNTDF_SHOW;
6168 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
6169 int c, rc = 0, index = 0;
6170 char fsname[PATH_MAX] = "", *pool_name = NULL;
6171 struct option long_opts[] = {
6172 { .val = 'h', .name = "human-readable",
6173 .has_arg = no_argument },
6174 { .val = 'i', .name = "inodes", .has_arg = no_argument },
6175 { .val = 'l', .name = "lazy", .has_arg = no_argument },
6176 { .val = 'p', .name = "pool", .has_arg = required_argument },
6177 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6180 while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
6183 flags |= MNTDF_COOKED;
6186 flags |= MNTDF_INODES;
6189 flags |= MNTDF_LAZY;
6195 flags |= MNTDF_VERBOSE;
6201 if (optind < argc && !realpath(argv[optind], path)) {
6203 fprintf(stderr, "error: invalid path '%s': %s\n",
6204 argv[optind], strerror(-rc));
6208 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6209 /* Check if we have a mount point */
6210 if (mntdir[0] == '\0')
6213 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
6214 if (rc || path[0] != '\0')
6216 fsname[0] = '\0'; /* avoid matching in next loop */
6217 mntdir[0] = '\0'; /* avoid matching in next loop */
6223 static int print_instance(const char *mntdir, char *buf, size_t buflen,
6224 bool opt_instance, bool opt_fsname, bool opt_mntdir)
6228 if (opt_fsname == opt_instance) { /* both true or both false */
6229 rc = llapi_getname(mntdir, buf, buflen);
6230 } else if (opt_fsname) {
6231 /* llapi_search_mounts() fills @buf with fsname, but that is not
6232 * called if explicit paths are specified on the command-line
6235 rc = llapi_get_fsname(mntdir, buf, buflen);
6236 } else /* if (opt_instance) */ {
6237 rc = llapi_get_instance(mntdir, buf, buflen);
6241 fprintf(stderr, "cannot get instance for '%s': %s\n",
6242 mntdir, strerror(-rc));
6247 printf("%s %s\n", buf, mntdir);
6249 printf("%s\n", buf);
6254 static int lfs_getname(int argc, char **argv)
6256 struct option long_opts[] = {
6257 { .val = 'h', .name = "help", .has_arg = no_argument },
6258 { .val = 'i', .name = "instance", .has_arg = no_argument },
6259 { .val = 'n', .name = "fsname", .has_arg = no_argument },
6261 bool opt_instance = false, opt_fsname = false;
6262 char fsname[PATH_MAX] = "";
6265 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
6268 opt_instance = true;
6279 if (optind == argc) { /* no paths specified, get all paths. */
6280 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
6283 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
6284 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
6285 opt_instance, opt_fsname, true);
6288 path[0] = fsname[0] = mntdir[0] = '\0';
6290 } else { /* paths specified, only attempt to search these. */
6293 /* if only one path is given, print only requested info */
6294 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
6296 for (; optind < argc; optind++) {
6297 rc2 = print_instance(argv[optind], fsname,
6298 sizeof(fsname), opt_instance,
6299 opt_fsname, opt_mntdir);
6309 static int lfs_check(int argc, char **argv)
6311 char mntdir[PATH_MAX] = {'\0'};
6320 fprintf(stderr, "%s check: server type must be specified\n",
6325 obd_types[0] = obd_type1;
6326 obd_types[1] = obd_type2;
6327 obd_types[2] = obd_type3;
6329 if (strcmp(argv[1], "osts") == 0) {
6330 strcpy(obd_types[0], "osc");
6331 } else if (strcmp(argv[1], "mdts") == 0 ||
6332 strcmp(argv[1], "mds") == 0) {
6333 strcpy(obd_types[0], "mdc");
6334 } else if (strcmp(argv[1], "mgts") == 0) {
6335 strcpy(obd_types[0], "mgc");
6336 } else if (strcmp(argv[1], "all") == 0 ||
6337 strcmp(argv[1], "servers") == 0) {
6339 strcpy(obd_types[0], "osc");
6340 strcpy(obd_types[1], "mdc");
6341 strcpy(obd_types[2], "mgc");
6343 fprintf(stderr, "%s check: unrecognized option '%s'\n",
6348 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
6349 if (rc < 0 || mntdir[0] == '\0') {
6351 "%s check: cannot find mounted Lustre filesystem: %s\n",
6352 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
6356 rc = llapi_target_check(num_types, obd_types, mntdir);
6358 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
6359 progname, argv[1], strerror(-rc));
6365 #ifdef HAVE_SYS_QUOTA_H
6366 #define ARG2INT(nr, str, msg) \
6369 nr = strtol(str, &endp, 0); \
6370 if (*endp != '\0') { \
6371 fprintf(stderr, "%s: bad %s '%s'\n", \
6372 progname, msg, str); \
6377 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
6379 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
6380 * returns the value or ULONG_MAX on integer overflow or incorrect format
6382 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
6383 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
6384 * 3. empty integer value is interpreted as 0
6386 static unsigned long str2sec(const char* timestr)
6388 const char spec[] = "smhdw";
6389 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6390 unsigned long val = 0;
6393 if (strpbrk(timestr, spec) == NULL) {
6394 /* no specifiers inside the time string,
6395 should treat it as an integer value */
6396 val = strtoul(timestr, &tail, 10);
6397 return *tail ? ULONG_MAX : val;
6400 /* format string is XXwXXdXXhXXmXXs */
6406 v = strtoul(timestr, &tail, 10);
6407 if (v == ULONG_MAX || *tail == '\0')
6408 /* value too large (ULONG_MAX or more)
6409 or missing specifier */
6412 ptr = strchr(spec, *tail);
6414 /* unknown specifier */
6419 /* check if product will overflow the type */
6420 if (!(v < ULONG_MAX / mult[ind]))
6423 ADD_OVERFLOW(val, mult[ind] * v);
6424 if (val == ULONG_MAX)
6436 #define ARG2ULL(nr, str, def_units) \
6438 unsigned long long limit, units = def_units; \
6441 rc = llapi_parse_size(str, &limit, &units, 1); \
6443 fprintf(stderr, "%s: invalid limit '%s'\n", \
6450 static inline int has_times_option(int argc, char **argv)
6454 for (i = 1; i < argc; i++)
6455 if (!strcmp(argv[i], "-t"))
6461 int lfs_setquota_times(int argc, char **argv)
6464 struct if_quotactl qctl;
6465 char *mnt, *obd_type = (char *)qctl.obd_type;
6466 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6467 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
6468 struct option long_opts[] = {
6469 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
6470 { .val = 'g', .name = "group", .has_arg = no_argument },
6471 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
6472 { .val = 'p', .name = "projid", .has_arg = no_argument },
6473 { .val = 't', .name = "times", .has_arg = no_argument },
6474 { .val = 'u', .name = "user", .has_arg = no_argument },
6478 memset(&qctl, 0, sizeof(qctl));
6479 qctl.qc_cmd = LUSTRE_Q_SETINFO;
6480 qctl.qc_type = ALLQUOTA;
6482 while ((c = getopt_long(argc, argv, "b:gi:ptu",
6483 long_opts, NULL)) != -1) {
6494 if (qctl.qc_type != ALLQUOTA) {
6495 fprintf(stderr, "error: -u/g/p can't be used "
6496 "more than once\n");
6499 qctl.qc_type = qtype;
6502 if (strncmp(optarg, NOTIFY_GRACE,
6503 strlen(NOTIFY_GRACE)) == 0) {
6504 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
6506 dqi->dqi_bgrace = str2sec(optarg);
6507 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
6508 fprintf(stderr, "error: bad "
6509 "block-grace: %s\n", optarg);
6513 dqb->dqb_valid |= QIF_BTIME;
6516 if (strncmp(optarg, NOTIFY_GRACE,
6517 strlen(NOTIFY_GRACE)) == 0) {
6518 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
6520 dqi->dqi_igrace = str2sec(optarg);
6521 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
6522 fprintf(stderr, "error: bad "
6523 "inode-grace: %s\n", optarg);
6527 dqb->dqb_valid |= QIF_ITIME;
6529 case 't': /* Yes, of course! */
6531 /* getopt prints error message for us when opterr != 0 */
6537 if (qctl.qc_type == ALLQUOTA) {
6538 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
6542 if (optind != argc - 1) {
6543 fprintf(stderr, "error: unexpected parameters encountered\n");
6548 rc = llapi_quotactl(mnt, &qctl);
6551 fprintf(stderr, "%s %s ", obd_type,
6552 obd_uuid2str(&qctl.obd_uuid));
6553 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
6560 #define BSLIMIT (1 << 0)
6561 #define BHLIMIT (1 << 1)
6562 #define ISLIMIT (1 << 2)
6563 #define IHLIMIT (1 << 3)
6565 int lfs_setquota(int argc, char **argv)
6568 struct if_quotactl qctl;
6569 char *mnt, *obd_type = (char *)qctl.obd_type;
6570 struct obd_dqblk *dqb = &qctl.qc_dqblk;
6571 struct option long_opts[] = {
6572 { .val = 'b', .name = "block-softlimit",
6573 .has_arg = required_argument },
6574 { .val = 'B', .name = "block-hardlimit",
6575 .has_arg = required_argument },
6576 { .val = 'd', .name = "default", .has_arg = no_argument },
6577 { .val = 'g', .name = "group", .has_arg = required_argument },
6578 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
6579 { .val = 'i', .name = "inode-softlimit",
6580 .has_arg = required_argument },
6581 { .val = 'I', .name = "inode-hardlimit",
6582 .has_arg = required_argument },
6583 { .val = 'p', .name = "projid", .has_arg = required_argument },
6584 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
6585 { .val = 'u', .name = "user", .has_arg = required_argument },
6586 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
6588 unsigned limit_mask = 0;
6590 bool use_default = false;
6593 if (has_times_option(argc, argv))
6594 return lfs_setquota_times(argc, argv);
6596 memset(&qctl, 0, sizeof(qctl));
6597 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
6598 qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
6599 * so it can be used as a marker that qc_type
6600 * isn't reinitialized from command line */
6602 while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
6603 long_opts, NULL)) != -1) {
6606 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6609 goto quota_type_def;
6612 rc = name2uid(&qctl.qc_id, optarg);
6615 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6618 goto quota_type_def;
6621 rc = name2gid(&qctl.qc_id, optarg);
6624 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6627 goto quota_type_def;
6630 rc = name2projid(&qctl.qc_id, optarg);
6633 qctl.qc_id = strtoul(optarg, &endptr, 10);
6634 if (*endptr != '\0') {
6635 fprintf(stderr, "%s setquota: invalid"
6636 " id '%s'\n", progname, optarg);
6641 if (qctl.qc_id == 0) {
6642 fprintf(stderr, "%s setquota: can't set quota"
6643 " for root usr/group/project.\n",
6649 if (qctl.qc_type != ALLQUOTA) {
6651 "%s setquota: only one of -u, -U, -g,"
6652 " -G, -p or -P may be specified\n",
6656 qctl.qc_type = qtype;
6659 qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
6663 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
6664 dqb->dqb_bsoftlimit >>= 10;
6665 limit_mask |= BSLIMIT;
6666 if (dqb->dqb_bsoftlimit &&
6667 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
6669 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
6670 "See '%s help setquota' or Lustre manual for details\n",
6672 (unsigned long long)dqb->dqb_bsoftlimit,
6676 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
6677 dqb->dqb_bhardlimit >>= 10;
6678 limit_mask |= BHLIMIT;
6679 if (dqb->dqb_bhardlimit &&
6680 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
6682 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
6683 "See '%s help setquota' or Lustre manual for details\n",
6685 (unsigned long long)dqb->dqb_bhardlimit,
6689 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
6690 limit_mask |= ISLIMIT;
6691 if (dqb->dqb_isoftlimit &&
6692 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
6694 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
6695 "See '%s help setquota' or Lustre manual for details\n",
6697 (unsigned long long)dqb->dqb_isoftlimit,
6701 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
6702 limit_mask |= IHLIMIT;
6703 if (dqb->dqb_ihardlimit &&
6704 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
6706 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
6707 "See '%s help setquota' or Lustre manual for details\n",
6709 (unsigned long long)dqb->dqb_ihardlimit,
6714 "%s setquota: unrecognized option '%s'\n",
6715 progname, argv[optind - 1]);
6720 if (qctl.qc_type == ALLQUOTA) {
6722 "%s setquota: either -u or -g must be specified\n",
6727 if (!use_default && limit_mask == 0) {
6729 "%s setquota: at least one limit must be specified\n",
6734 if (use_default && limit_mask != 0) {
6736 "%s setquota: limits should not be specified when"
6737 " using default quota\n",
6742 if (use_default && qctl.qc_id == 0) {
6744 "%s setquota: can not set default quota for root"
6745 " user/group/project\n",
6750 if (optind != argc - 1) {
6752 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
6753 progname, argv[optind]);
6760 dqb->dqb_bhardlimit = 0;
6761 dqb->dqb_bsoftlimit = 0;
6762 dqb->dqb_ihardlimit = 0;
6763 dqb->dqb_isoftlimit = 0;
6766 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
6767 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
6768 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
6769 /* sigh, we can't just set blimits/ilimits */
6770 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
6771 .qc_type = qctl.qc_type,
6772 .qc_id = qctl.qc_id};
6774 rc = llapi_quotactl(mnt, &tmp_qctl);
6778 if (!(limit_mask & BHLIMIT))
6779 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
6780 if (!(limit_mask & BSLIMIT))
6781 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
6782 if (!(limit_mask & IHLIMIT))
6783 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
6784 if (!(limit_mask & ISLIMIT))
6785 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
6787 /* Keep grace times if we have got no softlimit arguments */
6788 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
6789 dqb->dqb_valid |= QIF_BTIME;
6790 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
6793 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
6794 dqb->dqb_valid |= QIF_ITIME;
6795 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
6799 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
6800 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
6802 rc = llapi_quotactl(mnt, &qctl);
6806 "%s setquota: cannot quotactl '%s' '%s': %s",
6808 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
6815 /* Converts seconds value into format string
6816 * result is returned in buf
6818 * 1. result is in descenting order: 1w2d3h4m5s
6819 * 2. zero fields are not filled (except for p. 3): 5d1s
6820 * 3. zero seconds value is presented as "0s"
6822 static char * __sec2str(time_t seconds, char *buf)
6824 const char spec[] = "smhdw";
6825 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
6830 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
6831 c = seconds / mult[i];
6833 if (c > 0 || (i == 0 && buf == tail))
6834 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
6843 static void sec2str(time_t seconds, char *buf, int rc)
6850 tail = __sec2str(seconds, tail);
6852 if (rc && tail - buf < 39) {
6858 static void diff2str(time_t seconds, char *buf, time_t now)
6864 if (seconds <= now) {
6865 strcpy(buf, "none");
6868 __sec2str(seconds - now, buf);
6871 static void print_quota_title(char *name, struct if_quotactl *qctl,
6872 bool human_readable, bool show_default)
6875 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
6876 printf("%15s %8s%8s%8s %8s%8s%8s\n",
6877 "Filesystem", "bquota", "blimit", "bgrace",
6878 "iquota", "ilimit", "igrace");
6880 printf("Disk quotas for %s %s (%cid %u):\n",
6881 qtype_name(qctl->qc_type), name,
6882 *qtype_name(qctl->qc_type), qctl->qc_id);
6883 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
6884 "Filesystem", human_readable ? "used" : "kbytes",
6885 "quota", "limit", "grace",
6886 "files", "quota", "limit", "grace");
6890 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
6893 snprintf(buf, buflen, "%ju", (uintmax_t)num);
6896 snprintf(buf, buflen, "%5.4gP",
6897 (double)num / ((__u64)1 << 40));
6899 snprintf(buf, buflen, "%5.4gT",
6900 (double)num / (1 << 30));
6902 snprintf(buf, buflen, "%5.4gG",
6903 (double)num / (1 << 20));
6905 snprintf(buf, buflen, "%5.4gM",
6906 (double)num / (1 << 10));
6908 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
6912 #define STRBUF_LEN 32
6913 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
6914 int rc, bool h, bool show_default)
6920 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
6921 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
6922 int bover = 0, iover = 0;
6923 struct obd_dqblk *dqb = &qctl->qc_dqblk;
6924 char numbuf[3][STRBUF_LEN];
6926 char strbuf[STRBUF_LEN];
6928 if (dqb->dqb_bhardlimit &&
6929 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
6931 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
6932 if (dqb->dqb_btime > now) {
6939 if (dqb->dqb_ihardlimit &&
6940 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
6942 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
6943 if (dqb->dqb_itime > now) {
6951 if (strlen(mnt) > 15)
6952 printf("%s\n%15s", mnt, "");
6954 printf("%15s", mnt);
6957 diff2str(dqb->dqb_btime, timebuf, now);
6958 else if (show_default)
6959 snprintf(timebuf, sizeof(timebuf), "%llu",
6960 (unsigned long long)dqb->dqb_btime);
6962 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6963 strbuf, sizeof(strbuf), h);
6964 if (rc == -EREMOTEIO)
6965 sprintf(numbuf[0], "%s*", strbuf);
6967 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6968 "%s" : "[%s]", strbuf);
6970 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6971 if (type == QC_GENERAL)
6972 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6973 "%s" : "[%s]", strbuf);
6975 sprintf(numbuf[1], "%s", "-");
6977 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6978 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6979 "%s" : "[%s]", strbuf);
6982 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6984 printf(" %7s%c %6s %7s %7s",
6985 numbuf[0], bover ? '*' : ' ', numbuf[1],
6986 numbuf[2], bover > 1 ? timebuf : "-");
6990 diff2str(dqb->dqb_itime, timebuf, now);
6991 else if (show_default)
6992 snprintf(timebuf, sizeof(timebuf), "%llu",
6993 (unsigned long long)dqb->dqb_itime);
6995 snprintf(numbuf[0], sizeof(numbuf),
6996 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6997 (uintmax_t)dqb->dqb_curinodes);
6999 if (type == QC_GENERAL)
7000 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
7002 (uintmax_t)dqb->dqb_isoftlimit);
7004 sprintf(numbuf[1], "%s", "-");
7006 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
7007 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
7010 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
7011 else if (type != QC_OSTIDX)
7012 printf(" %7s%c %6s %7s %7s",
7013 numbuf[0], iover ? '*' : ' ', numbuf[1],
7014 numbuf[2], iover > 1 ? timebuf : "-");
7016 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
7018 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
7019 qctl->qc_cmd == Q_GETOINFO) {
7023 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
7024 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
7026 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
7027 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
7028 strncpy(igtimebuf, NOTIFY_GRACE, 40);
7030 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
7032 printf("Block grace time: %s; Inode grace time: %s\n",
7033 bgtimebuf, igtimebuf);
7037 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
7038 bool h, __u64 *total)
7040 int rc = 0, rc1 = 0, count = 0;
7041 __u32 valid = qctl->qc_valid;
7043 rc = llapi_get_obd_count(mnt, &count, is_mdt);
7045 fprintf(stderr, "can not get %s count: %s\n",
7046 is_mdt ? "mdt": "ost", strerror(-rc));
7050 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
7051 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
7052 rc = llapi_quotactl(mnt, qctl);
7054 /* It is remote client case. */
7055 if (rc == -EOPNOTSUPP) {
7062 fprintf(stderr, "quotactl %s%d failed.\n",
7063 is_mdt ? "mdt": "ost", qctl->qc_idx);
7067 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
7068 qctl->qc_valid, 0, h, false);
7069 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
7070 qctl->qc_dqblk.dqb_bhardlimit;
7073 qctl->qc_valid = valid;
7077 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
7078 int verbose, int quiet, bool human_readable,
7081 int rc1 = 0, rc2 = 0, rc3 = 0;
7082 char *obd_type = (char *)qctl->obd_type;
7083 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
7084 __u64 total_ialloc = 0, total_balloc = 0;
7085 bool use_default_for_blk = false;
7086 bool use_default_for_file = false;
7089 rc1 = llapi_quotactl(mnt, qctl);
7093 fprintf(stderr, "%s quotas are not enabled.\n",
7094 qtype_name(qctl->qc_type));
7097 fprintf(stderr, "Permission denied.\n");
7100 /* We already got error message. */
7103 fprintf(stderr, "Unexpected quotactl error: %s\n",
7108 if (!show_default && qctl->qc_id == 0) {
7109 qctl->qc_dqblk.dqb_bhardlimit = 0;
7110 qctl->qc_dqblk.dqb_bsoftlimit = 0;
7111 qctl->qc_dqblk.dqb_ihardlimit = 0;
7112 qctl->qc_dqblk.dqb_isoftlimit = 0;
7113 qctl->qc_dqblk.dqb_btime = 0;
7114 qctl->qc_dqblk.dqb_itime = 0;
7115 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
7118 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
7119 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
7120 use_default_for_blk = true;
7121 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
7124 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
7125 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
7126 use_default_for_file = true;
7127 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
7130 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
7131 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
7132 print_quota_title(name, qctl, human_readable, show_default);
7134 if (rc1 && *obd_type)
7135 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
7137 if (qctl->qc_valid != QC_GENERAL)
7140 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
7141 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
7142 (QIF_LIMITS|QIF_USAGE));
7144 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
7146 if (!show_default && verbose &&
7147 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
7148 char strbuf[STRBUF_LEN];
7150 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
7152 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
7154 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
7156 printf("Total allocated inode limit: %ju, total "
7157 "allocated block limit: %s\n", (uintmax_t)total_ialloc,
7161 if (use_default_for_blk)
7162 printf("%cid %u is using default block quota setting\n",
7163 *qtype_name(qctl->qc_type), qctl->qc_id);
7165 if (use_default_for_file)
7166 printf("%cid %u is using default file quota setting\n",
7167 *qtype_name(qctl->qc_type), qctl->qc_id);
7169 if (rc1 || rc2 || rc3 || inacc)
7170 printf("Some errors happened when getting quota info. "
7171 "Some devices may be not working or deactivated. "
7172 "The data in \"[]\" is inaccurate.\n");
7186 static int lfs_project(int argc, char **argv)
7188 int ret = 0, err = 0, c, i;
7189 struct project_handle_control phc = { 0 };
7190 enum lfs_project_ops_t op;
7193 phc.assign_projid = false;
7194 /* default action */
7195 op = LFS_PROJECT_LIST;
7197 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
7200 if (op != LFS_PROJECT_LIST) {
7202 "%s: cannot specify '-c' '-C' '-s' together\n",
7207 op = LFS_PROJECT_CHECK;
7210 if (op != LFS_PROJECT_LIST) {
7212 "%s: cannot specify '-c' '-C' '-s' together\n",
7217 op = LFS_PROJECT_CLEAR;
7220 if (op != LFS_PROJECT_LIST) {
7222 "%s: cannot specify '-c' '-C' '-s' together\n",
7227 phc.set_inherit = true;
7228 op = LFS_PROJECT_SET;
7234 phc.keep_projid = true;
7237 phc.recursive = true;
7240 phc.projid = strtoul(optarg, NULL, 0);
7241 phc.assign_projid = true;
7245 phc.newline = false;
7248 fprintf(stderr, "%s: invalid option '%c'\n",
7254 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
7255 op = LFS_PROJECT_SET;
7256 phc.set_projid = true;
7257 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
7258 phc.set_projid = true;
7262 case LFS_PROJECT_CHECK:
7263 if (phc.keep_projid) {
7265 "%s: '-k' is useless together with '-c'\n",
7270 case LFS_PROJECT_CLEAR:
7273 "%s: '-0' is useless together with '-C'\n",
7277 if (phc.assign_projid) {
7279 "%s: '-p' is useless together with '-C'\n",
7284 case LFS_PROJECT_SET:
7287 "%s: '-0' is useless together with '-s'\n",
7291 if (phc.keep_projid) {
7293 "%s: '-k' is useless together with '-s'\n",
7301 "%s: '-0' is useless for list operations\n",
7311 fprintf(stderr, "%s: missing file or directory target(s)\n",
7316 for (i = 0; i < argc; i++) {
7318 case LFS_PROJECT_CHECK:
7319 err = lfs_project_check(argv[i], &phc);
7321 case LFS_PROJECT_LIST:
7322 err = lfs_project_list(argv[i], &phc);
7324 case LFS_PROJECT_CLEAR:
7325 err = lfs_project_clear(argv[i], &phc);
7327 case LFS_PROJECT_SET:
7328 err = lfs_project_set(argv[i], &phc);
7340 static int lfs_quota(int argc, char **argv)
7343 char *mnt, *name = NULL;
7344 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
7345 .qc_type = ALLQUOTA };
7346 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
7347 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
7349 __u32 valid = QC_GENERAL, idx = 0;
7350 bool human_readable = false;
7351 bool show_default = false;
7354 while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
7357 show_default = true;
7362 show_default = true;
7367 show_default = true;
7371 if (qctl.qc_type != ALLQUOTA) {
7373 "%s quota: only one of -u, -g, or -p may be specified\n",
7377 qctl.qc_type = qtype;
7380 qctl.qc_cmd = LUSTRE_Q_GETINFO;
7383 valid = qctl.qc_valid = QC_UUID;
7384 snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
7387 valid = qctl.qc_valid = QC_MDTIDX;
7388 idx = qctl.qc_idx = atoi(optarg);
7389 if (idx == 0 && *optarg != '0') {
7391 "%s quota: invalid MDT index '%s'\n",
7397 valid = qctl.qc_valid = QC_OSTIDX;
7398 idx = qctl.qc_idx = atoi(optarg);
7399 if (idx == 0 && *optarg != '0') {
7401 "%s quota: invalid OST index '%s'\n",
7413 human_readable = true;
7416 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
7417 progname, argv[optind - 1]);
7422 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
7423 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
7424 optind == argc - 1 && !show_default) {
7426 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7429 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
7430 qctl.qc_type = qtype;
7431 qctl.qc_valid = valid;
7432 if (qtype == USRQUOTA) {
7433 qctl.qc_id = geteuid();
7434 rc = uid2name(&name, qctl.qc_id);
7436 qctl.qc_id = getegid();
7437 rc = gid2name(&name, qctl.qc_id);
7438 memset(&qctl.qc_dqblk, 0,
7439 sizeof(qctl.qc_dqblk));
7444 rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
7445 human_readable, show_default);
7450 /* lfs quota -u username /path/to/lustre/mount */
7451 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
7452 /* options should be followed by u/g-name and mntpoint */
7453 if ((!show_default && optind + 2 != argc) ||
7454 (show_default && optind + 1 != argc) ||
7455 qctl.qc_type == ALLQUOTA) {
7457 "%s quota: name and mount point must be specified\n",
7462 if (!show_default) {
7463 name = argv[optind++];
7464 switch (qctl.qc_type) {
7466 rc = name2uid(&qctl.qc_id, name);
7469 rc = name2gid(&qctl.qc_id, name);
7472 rc = name2projid(&qctl.qc_id, name);
7479 qctl.qc_valid = QC_GENERAL;
7480 qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
7485 qctl.qc_id = strtoul(name, &endptr, 10);
7486 if (*endptr != '\0') {
7487 fprintf(stderr, "%s quota: invalid id '%s'\n",
7492 } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
7493 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
7499 rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
7500 human_readable, show_default);
7503 #endif /* HAVE_SYS_QUOTA_H! */
7505 static int flushctx_ioctl(char *mp)
7509 fd = open(mp, O_RDONLY);
7511 fprintf(stderr, "flushctx: error open %s: %s\n",
7512 mp, strerror(errno));
7516 rc = ioctl(fd, LL_IOC_FLUSHCTX);
7518 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
7519 mp, strerror(errno));
7525 static int lfs_flushctx(int argc, char **argv)
7527 int kdestroy = 0, c;
7528 char mntdir[PATH_MAX] = {'\0'};
7532 while ((c = getopt(argc, argv, "k")) != -1) {
7538 fprintf(stderr, "error: %s: option '-%c' "
7539 "unrecognized\n", argv[0], c);
7545 if ((rc = system("kdestroy > /dev/null")) != 0) {
7546 rc = WEXITSTATUS(rc);
7547 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
7551 if (optind >= argc) {
7552 /* flush for all mounted lustre fs. */
7553 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
7554 /* Check if we have a mount point */
7555 if (mntdir[0] == '\0')
7558 if (flushctx_ioctl(mntdir))
7561 mntdir[0] = '\0'; /* avoid matching in next loop */
7564 /* flush fs as specified */
7565 while (optind < argc) {
7566 if (flushctx_ioctl(argv[optind++]))
7573 static int lfs_changelog(int argc, char **argv)
7575 void *changelog_priv;
7576 struct changelog_rec *rec;
7577 long long startrec = 0, endrec = 0;
7579 struct option long_opts[] = {
7580 { .val = 'f', .name = "follow", .has_arg = no_argument },
7582 char short_opts[] = "f";
7585 while ((rc = getopt_long(argc, argv, short_opts,
7586 long_opts, NULL)) != -1) {
7593 "%s changelog: unrecognized option '%s'\n",
7594 progname, argv[optind - 1]);
7598 if (optind >= argc) {
7599 fprintf(stderr, "%s changelog: mdtname must be specified\n",
7604 mdd = argv[optind++];
7606 startrec = strtoll(argv[optind++], NULL, 10);
7608 endrec = strtoll(argv[optind++], NULL, 10);
7610 rc = llapi_changelog_start(&changelog_priv,
7611 CHANGELOG_FLAG_BLOCK |
7612 CHANGELOG_FLAG_JOBID |
7613 CHANGELOG_FLAG_EXTRA_FLAGS |
7614 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
7617 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
7618 progname, strerror(errno = -rc));
7622 rc = llapi_changelog_set_xflags(changelog_priv,
7623 CHANGELOG_EXTRA_FLAG_UIDGID |
7624 CHANGELOG_EXTRA_FLAG_NID |
7625 CHANGELOG_EXTRA_FLAG_OMODE |
7626 CHANGELOG_EXTRA_FLAG_XATTR);
7629 "%s changelog: cannot set xflags for changelog: %s\n",
7630 progname, strerror(errno = -rc));
7634 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
7638 if (endrec && rec->cr_index > endrec) {
7639 llapi_changelog_free(&rec);
7642 if (rec->cr_index < startrec) {
7643 llapi_changelog_free(&rec);
7647 secs = rec->cr_time >> 30;
7648 gmtime_r(&secs, &ts);
7649 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
7650 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
7651 changelog_type2str(rec->cr_type),
7652 ts.tm_hour, ts.tm_min, ts.tm_sec,
7653 (int)(rec->cr_time & ((1 << 30) - 1)),
7654 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
7655 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
7657 if (rec->cr_flags & CLF_JOBID) {
7658 struct changelog_ext_jobid *jid =
7659 changelog_rec_jobid(rec);
7661 if (jid->cr_jobid[0] != '\0')
7662 printf(" j=%s", jid->cr_jobid);
7665 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
7666 struct changelog_ext_extra_flags *ef =
7667 changelog_rec_extra_flags(rec);
7669 printf(" ef=0x%llx",
7670 (unsigned long long)ef->cr_extra_flags);
7672 if (ef->cr_extra_flags & CLFE_UIDGID) {
7673 struct changelog_ext_uidgid *uidgid =
7674 changelog_rec_uidgid(rec);
7676 printf(" u=%llu:%llu",
7677 (unsigned long long)uidgid->cr_uid,
7678 (unsigned long long)uidgid->cr_gid);
7680 if (ef->cr_extra_flags & CLFE_NID) {
7681 struct changelog_ext_nid *nid =
7682 changelog_rec_nid(rec);
7685 libcfs_nid2str(nid->cr_nid));
7688 if (ef->cr_extra_flags & CLFE_OPEN) {
7689 struct changelog_ext_openmode *omd =
7690 changelog_rec_openmode(rec);
7691 char mode[] = "---";
7693 /* exec mode must be exclusive */
7694 if (omd->cr_openflags & MDS_FMODE_EXEC) {
7697 if (omd->cr_openflags & MDS_FMODE_READ)
7699 if (omd->cr_openflags &
7706 if (strcmp(mode, "---") != 0)
7707 printf(" m=%s", mode);
7711 if (ef->cr_extra_flags & CLFE_XATTR) {
7712 struct changelog_ext_xattr *xattr =
7713 changelog_rec_xattr(rec);
7715 if (xattr->cr_xattr[0] != '\0')
7716 printf(" x=%s", xattr->cr_xattr);
7720 if (rec->cr_namelen)
7721 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
7722 rec->cr_namelen, changelog_rec_name(rec));
7724 if (rec->cr_flags & CLF_RENAME) {
7725 struct changelog_ext_rename *rnm =
7726 changelog_rec_rename(rec);
7728 if (!fid_is_zero(&rnm->cr_sfid))
7729 printf(" s="DFID" sp="DFID" %.*s",
7730 PFID(&rnm->cr_sfid),
7731 PFID(&rnm->cr_spfid),
7732 (int)changelog_rec_snamelen(rec),
7733 changelog_rec_sname(rec));
7737 llapi_changelog_free(&rec);
7740 llapi_changelog_fini(&changelog_priv);
7743 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
7744 progname, strerror(errno = -rc));
7746 return (rc == 1 ? 0 : rc);
7749 static int lfs_changelog_clear(int argc, char **argv)
7757 endrec = strtoll(argv[3], NULL, 10);
7759 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
7762 fprintf(stderr, "%s: record out of range: %llu\n",
7764 else if (rc == -ENOENT)
7765 fprintf(stderr, "%s: no changelog user: %s\n",
7768 fprintf(stderr, "%s error: %s\n", argv[0],
7777 static int lfs_fid2path(int argc, char **argv)
7779 struct option long_opts[] = {
7780 { .val = 'c', .name = "cur", .has_arg = no_argument },
7781 { .val = 'l', .name = "link", .has_arg = required_argument },
7782 { .val = 'r', .name = "rec", .has_arg = required_argument },
7784 char short_opts[] = "cl:r:";
7785 char *device, *fid, *path;
7786 long long recno = -1;
7791 char *endptr = NULL;
7793 while ((rc = getopt_long(argc, argv, short_opts,
7794 long_opts, NULL)) != -1) {
7800 linkno = strtol(optarg, &endptr, 10);
7801 if (*endptr != '\0') {
7803 "%s fid2path: invalid linkno '%s'\n",
7809 recno = strtoll(optarg, &endptr, 10);
7810 if (*endptr != '\0') {
7812 "%s fid2path: invalid recno '%s'\n",
7819 "%s fid2path: unrecognized option '%s'\n",
7820 progname, argv[optind - 1]);
7827 "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
7832 device = argv[optind++];
7833 path = calloc(1, PATH_MAX);
7837 "%s fid2path: cannot allocate memory for path: %s\n",
7838 progname, strerror(-rc));
7843 while (optind < argc) {
7844 fid = argv[optind++];
7846 lnktmp = (linkno >= 0) ? linkno : 0;
7848 int oldtmp = lnktmp;
7849 long long rectmp = recno;
7851 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
7855 "%s fid2path: cannot find '%s': %s\n",
7856 progname, fid, strerror(errno = -rc2));
7863 fprintf(stdout, "%lld ", rectmp);
7864 if (device[0] == '/') {
7865 fprintf(stdout, "%s", device);
7866 if (device[strlen(device) - 1] != '/')
7867 fprintf(stdout, "/");
7868 } else if (path[0] == '\0') {
7869 fprintf(stdout, "/");
7871 fprintf(stdout, "%s\n", path);
7874 /* specified linkno */
7876 if (oldtmp == lnktmp)
7886 static int lfs_path2fid(int argc, char **argv)
7888 struct option long_opts[] = {
7889 { .val = 'p', .name = "parents", .has_arg = no_argument },
7892 const char short_opts[] = "p";
7893 const char *sep = "";
7896 bool show_parents = false;
7898 while ((rc = getopt_long(argc, argv, short_opts,
7899 long_opts, NULL)) != -1) {
7902 show_parents = true;
7906 "%s path2fid: unrecognized option '%s'\n",
7907 progname, argv[optind - 1]);
7912 if (optind > argc - 1) {
7913 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
7917 else if (optind < argc - 1)
7921 for (path = argv + optind; *path != NULL; path++) {
7923 if (!show_parents) {
7924 err = llapi_path2fid(*path, &fid);
7926 printf("%s%s"DFID"\n",
7927 *sep != '\0' ? *path : "", sep,
7930 char name[NAME_MAX + 1];
7931 unsigned int linkno = 0;
7933 while ((err = llapi_path2parent(*path, linkno, &fid,
7934 name, sizeof(name))) == 0) {
7935 if (*sep != '\0' && linkno == 0)
7936 printf("%s%s", *path, sep);
7938 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
7943 /* err == -ENODATA is end-of-loop */
7944 if (linkno > 0 && err == -ENODATA) {
7952 "%s path2fid: cannot get %sfid for '%s': %s\n",
7953 progname, show_parents ? "parent " : "", *path,
7965 #define MAX_ERRNO 4095
7966 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
7968 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
7972 rc = llapi_rmfid(device, fa);
7974 fprintf(stderr, "rmfid(): rc = %d\n", rc);
7978 for (k = 0; k < fa->fa_nr; k++) {
7979 rc = (__s32)fa->fa_fids[k].f_ver;
7980 if (!IS_ERR_VALUE(rc))
7986 fa->fa_fids[k].f_ver = 0;
7987 fprintf(stderr, "rmfid("DFID"): rc = %d\n",
7988 PFID(&fa->fa_fids[k]), rc);
7994 static int lfs_rmfid(int argc, char **argv)
7996 char *fidstr, *device;
7997 int rc = 0, rc2, nr;
7998 struct fid_array *fa;
8000 if (optind > argc - 1) {
8001 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
8005 device = argv[optind++];
8008 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
8014 while (optind < argc) {
8017 fidstr = argv[optind++];
8018 while (*fidstr == '[')
8020 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
8022 fprintf(stderr, "unrecognized FID: %s\n",
8027 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
8028 /* start another batch */
8029 rc2 = lfs_rmfid_and_show_errors(device, fa);
8036 rc2 = lfs_rmfid_and_show_errors(device, fa);
8044 static int lfs_data_version(int argc, char **argv)
8051 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
8054 fprintf(stderr, "%s data_version: FILE must be specified\n",
8059 while ((c = getopt(argc, argv, "nrw")) != -1) {
8062 data_version_flags = 0;
8065 data_version_flags |= LL_DV_RD_FLUSH;
8068 data_version_flags |= LL_DV_WR_FLUSH;
8072 "%s data_version: unrecognized option '%s'\n",
8073 progname, argv[optind - 1]);
8077 if (optind == argc) {
8078 fprintf(stderr, "%s data_version: FILE must be specified\n",
8083 path = argv[optind];
8084 fd = open(path, O_RDONLY);
8087 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
8088 progname, path, strerror(-rc));
8092 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
8095 "%s data_version: cannot get version for '%s': %s\n",
8096 progname, path, strerror(-rc));
8098 printf("%ju" "\n", (uintmax_t)data_version);
8104 static int lfs_hsm_state(int argc, char **argv)
8109 struct hsm_user_state hus;
8117 rc = llapi_hsm_state_get(path, &hus);
8119 fprintf(stderr, "can't get hsm state for %s: %s\n",
8120 path, strerror(errno = -rc));
8124 /* Display path name and status flags */
8125 printf("%s: (0x%08x)", path, hus.hus_states);
8127 if (hus.hus_states & HS_RELEASED)
8128 printf(" released");
8129 if (hus.hus_states & HS_EXISTS)
8131 if (hus.hus_states & HS_DIRTY)
8133 if (hus.hus_states & HS_ARCHIVED)
8134 printf(" archived");
8135 /* Display user-settable flags */
8136 if (hus.hus_states & HS_NORELEASE)
8137 printf(" never_release");
8138 if (hus.hus_states & HS_NOARCHIVE)
8139 printf(" never_archive");
8140 if (hus.hus_states & HS_LOST)
8141 printf(" lost_from_hsm");
8143 if (hus.hus_archive_id != 0)
8144 printf(", archive_id:%d", hus.hus_archive_id);
8147 } while (++i < argc);
8152 #define LFS_HSM_SET 0
8153 #define LFS_HSM_CLEAR 1
8156 * Generic function to set or clear HSM flags.
8157 * Used by hsm_set and hsm_clear.
8159 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
8161 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
8163 struct option long_opts[] = {
8164 { .val = 'A', .name = "archived", .has_arg = no_argument },
8165 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
8166 { .val = 'd', .name = "dirty", .has_arg = no_argument },
8167 { .val = 'e', .name = "exists", .has_arg = no_argument },
8168 { .val = 'l', .name = "lost", .has_arg = no_argument },
8169 { .val = 'r', .name = "norelease", .has_arg = no_argument },
8170 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
8172 char short_opts[] = "lraAdei:";
8176 __u32 archive_id = 0;
8182 while ((c = getopt_long(argc, argv, short_opts,
8183 long_opts, NULL)) != -1) {
8189 mask |= HS_NOARCHIVE;
8192 mask |= HS_ARCHIVED;
8195 mask |= HS_NORELEASE;
8204 archive_id = strtol(optarg, &end, 10);
8206 fprintf(stderr, "invalid archive_id: '%s'\n",
8214 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8215 argv[0], argv[optind - 1]);
8220 /* User should have specified a flag */
8224 while (optind < argc) {
8226 path = argv[optind];
8228 /* If mode == 0, this means we apply the mask. */
8229 if (mode == LFS_HSM_SET)
8230 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
8232 rc = llapi_hsm_state_set(path, 0, mask, 0);
8235 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
8236 path, strerror(errno = -rc));
8245 static int lfs_hsm_action(int argc, char **argv)
8250 struct hsm_current_action hca;
8251 struct hsm_extent he;
8252 enum hsm_user_action hua;
8253 enum hsm_progress_states hps;
8261 rc = llapi_hsm_current_action(path, &hca);
8263 fprintf(stderr, "can't get hsm action for %s: %s\n",
8264 path, strerror(errno = -rc));
8267 he = hca.hca_location;
8268 hua = hca.hca_action;
8269 hps = hca.hca_state;
8271 printf("%s: %s", path, hsm_user_action2name(hua));
8273 /* Skip file without action */
8274 if (hca.hca_action == HUA_NONE) {
8279 printf(" %s ", hsm_progress_state2name(hps));
8281 if ((hps == HPS_RUNNING) &&
8282 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
8283 printf("(%llu bytes moved)\n",
8284 (unsigned long long)he.length);
8285 else if ((he.offset + he.length) == LUSTRE_EOF)
8286 printf("(from %llu to EOF)\n",
8287 (unsigned long long)he.offset);
8289 printf("(from %llu to %llu)\n",
8290 (unsigned long long)he.offset,
8291 (unsigned long long)(he.offset + he.length));
8293 } while (++i < argc);
8298 static int lfs_hsm_set(int argc, char **argv)
8300 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
8303 static int lfs_hsm_clear(int argc, char **argv)
8305 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
8309 * Check file state and return its fid, to be used by lfs_hsm_request().
8311 * \param[in] file Path to file to check
8312 * \param[in,out] fid Pointer to allocated lu_fid struct.
8313 * \param[in,out] last_dev Pointer to last device id used.
8315 * \return 0 on success.
8317 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
8323 rc = lstat(file, &st);
8325 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
8328 /* Checking for regular file as archiving as posix copytool
8329 * rejects archiving files other than regular files
8331 if (!S_ISREG(st.st_mode)) {
8332 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
8335 /* A request should be ... */
8336 if (*last_dev != st.st_dev && *last_dev != 0) {
8337 fprintf(stderr, "All files should be "
8338 "on the same filesystem: %s\n", file);
8341 *last_dev = st.st_dev;
8343 rc = llapi_path2fid(file, fid);
8345 fprintf(stderr, "Cannot read FID of %s: %s\n",
8346 file, strerror(-rc));
8352 /* Fill an HSM HUR item with a given file name.
8354 * If mntpath is set, then the filename is actually a FID, and no
8355 * lookup on the filesystem will be performed.
8357 * \param[in] hur the user request to fill
8358 * \param[in] idx index of the item inside the HUR to fill
8359 * \param[in] mntpath mountpoint of Lustre
8360 * \param[in] fname filename (if mtnpath is NULL)
8361 * or FID (if mntpath is set)
8362 * \param[in] last_dev pointer to last device id used
8364 * \retval 0 on success
8365 * \retval CMD_HELP or a negative errno on error
8367 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
8368 const char *mntpath, const char *fname,
8371 struct hsm_user_item *hui = &hur->hur_user_item[idx];
8374 hui->hui_extent.length = -1;
8376 if (mntpath != NULL) {
8377 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
8379 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
8382 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
8386 hur->hur_request.hr_itemcount++;
8391 static int lfs_hsm_request(int argc, char **argv, int action)
8393 struct option long_opts[] = {
8394 { .val = 'a', .name = "archive", .has_arg = required_argument },
8395 { .val = 'D', .name = "data", .has_arg = required_argument },
8396 { .val = 'l', .name = "filelist", .has_arg = required_argument },
8397 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
8400 char short_opts[] = "l:D:a:m:";
8401 struct hsm_user_request *hur, *oldhur;
8406 char *filelist = NULL;
8407 char fullpath[PATH_MAX];
8408 char *opaque = NULL;
8412 int nbfile_alloc = 0;
8413 char *some_file = NULL;
8414 char *mntpath = NULL;
8420 while ((c = getopt_long(argc, argv, short_opts,
8421 long_opts, NULL)) != -1) {
8430 if (action != HUA_ARCHIVE &&
8431 action != HUA_REMOVE) {
8433 "error: -a is supported only "
8434 "when archiving or removing\n");
8437 archive_id = atoi(optarg);
8440 if (some_file == NULL) {
8442 some_file = strdup(optarg);
8448 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
8449 argv[0], argv[optind - 1]);
8454 /* All remaining args are files, so we have at least nbfile */
8455 nbfile = argc - optind;
8457 if ((nbfile == 0) && (filelist == NULL))
8461 opaque_len = strlen(opaque);
8463 /* Alloc the request structure with enough place to store all files
8464 * from command line. */
8465 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
8467 fprintf(stderr, "Cannot create the request: %s\n",
8471 nbfile_alloc = nbfile;
8473 hur->hur_request.hr_action = action;
8474 hur->hur_request.hr_archive_id = archive_id;
8475 hur->hur_request.hr_flags = 0;
8477 /* All remaining args are files, add them */
8478 if (nbfile != 0 && some_file == NULL)
8479 some_file = strdup(argv[optind]);
8481 for (i = 0; i < nbfile; i++) {
8482 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
8488 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
8490 /* If a filelist was specified, read the filelist from it. */
8491 if (filelist != NULL) {
8492 fp = fopen(filelist, "r");
8494 fprintf(stderr, "Cannot read the file list %s: %s\n",
8495 filelist, strerror(errno));
8500 while ((rc = getline(&line, &len, fp)) != -1) {
8501 /* If allocated buffer was too small, get something
8503 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
8506 nbfile_alloc = nbfile_alloc * 2 + 1;
8508 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
8511 fprintf(stderr, "hsm: cannot allocate "
8512 "the request: %s\n",
8519 size = hur_len(oldhur);
8521 fprintf(stderr, "hsm: cannot allocate "
8522 "%u files + %u bytes data\n",
8523 oldhur->hur_request.hr_itemcount,
8524 oldhur->hur_request.hr_data_len);
8531 memcpy(hur, oldhur, size);
8536 if (line[strlen(line) - 1] == '\n')
8537 line[strlen(line) - 1] = '\0';
8539 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
8540 mntpath, line, &last_dev);
8546 if (some_file == NULL) {
8556 /* If a --data was used, add it to the request */
8557 hur->hur_request.hr_data_len = opaque_len;
8559 memcpy(hur_data(hur), opaque, opaque_len);
8561 /* Send the HSM request */
8562 if (realpath(some_file, fullpath) == NULL) {
8563 fprintf(stderr, "Could not find path '%s': %s\n",
8564 some_file, strerror(errno));
8566 rc = llapi_hsm_request(fullpath, hur);
8568 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
8569 some_file, strerror(-rc));
8579 static int lfs_hsm_archive(int argc, char **argv)
8581 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
8584 static int lfs_hsm_restore(int argc, char **argv)
8586 return lfs_hsm_request(argc, argv, HUA_RESTORE);
8589 static int lfs_hsm_release(int argc, char **argv)
8591 return lfs_hsm_request(argc, argv, HUA_RELEASE);
8594 static int lfs_hsm_remove(int argc, char **argv)
8596 return lfs_hsm_request(argc, argv, HUA_REMOVE);
8599 static int lfs_hsm_cancel(int argc, char **argv)
8601 return lfs_hsm_request(argc, argv, HUA_CANCEL);
8604 static int lfs_swap_layouts(int argc, char **argv)
8609 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
8610 SWAP_LAYOUTS_KEEP_MTIME |
8611 SWAP_LAYOUTS_KEEP_ATIME);
8614 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
8616 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
8618 int lfs_get_mode(const char *string)
8620 enum lock_mode_user mode;
8622 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
8623 if (lock_mode_names[mode] == NULL)
8625 if (strcmp(string, lock_mode_names[mode]) == 0)
8632 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
8634 enum lu_ladvise_type advice;
8637 advice < ARRAY_SIZE(ladvise_names); advice++) {
8638 if (ladvise_names[advice] == NULL)
8640 if (strcmp(string, ladvise_names[advice]) == 0)
8644 return LU_LADVISE_INVALID;
8647 static int lfs_ladvise(int argc, char **argv)
8649 struct option long_opts[] = {
8650 { .val = 'a', .name = "advice", .has_arg = required_argument },
8651 { .val = 'b', .name = "background", .has_arg = no_argument },
8652 { .val = 'e', .name = "end", .has_arg = required_argument },
8653 { .val = 'l', .name = "length", .has_arg = required_argument },
8654 { .val = 'm', .name = "mode", .has_arg = required_argument },
8655 { .val = 's', .name = "start", .has_arg = required_argument },
8656 { .val = 'u', .name = "unset", .has_arg = no_argument },
8658 char short_opts[] = "a:be:l:m:s:u";
8663 struct llapi_lu_ladvise advice;
8664 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
8665 unsigned long long start = 0;
8666 unsigned long long end = LUSTRE_EOF;
8667 unsigned long long length = 0;
8668 unsigned long long size_units;
8669 unsigned long long flags = 0;
8673 while ((c = getopt_long(argc, argv, short_opts,
8674 long_opts, NULL)) != -1) {
8677 advice_type = lfs_get_ladvice(optarg);
8678 if (advice_type == LU_LADVISE_INVALID) {
8679 fprintf(stderr, "%s: invalid advice type "
8680 "'%s'\n", argv[0], optarg);
8681 fprintf(stderr, "Valid types:");
8683 for (advice_type = 0;
8684 advice_type < ARRAY_SIZE(ladvise_names);
8686 if (ladvise_names[advice_type] == NULL)
8688 fprintf(stderr, " %s",
8689 ladvise_names[advice_type]);
8691 fprintf(stderr, "\n");
8704 rc = llapi_parse_size(optarg, &end,
8707 fprintf(stderr, "%s: bad end offset '%s'\n",
8714 rc = llapi_parse_size(optarg, &start,
8717 fprintf(stderr, "%s: bad start offset "
8718 "'%s'\n", argv[0], optarg);
8724 rc = llapi_parse_size(optarg, &length,
8727 fprintf(stderr, "%s: bad length '%s'\n",
8733 mode = lfs_get_mode(optarg);
8735 fprintf(stderr, "%s: bad mode '%s', valid "
8736 "modes are READ or WRITE\n",
8744 fprintf(stderr, "%s: option '%s' unrecognized\n",
8745 argv[0], argv[optind - 1]);
8750 if (advice_type == LU_LADVISE_INVALID) {
8751 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
8752 fprintf(stderr, "Valid types:");
8753 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
8755 if (ladvise_names[advice_type] == NULL)
8757 fprintf(stderr, " %s", ladvise_names[advice_type]);
8759 fprintf(stderr, "\n");
8763 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
8764 fprintf(stderr, "%s: Lock no expand advice is a per file "
8765 "descriptor advice, so when called from lfs, "
8766 "it does nothing.\n", argv[0]);
8770 if (argc <= optind) {
8771 fprintf(stderr, "%s: please give one or more file names\n",
8776 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
8777 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
8782 if (end == LUSTRE_EOF && length != 0)
8783 end = start + length;
8786 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
8787 argv[0], start, end);
8791 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
8792 fprintf(stderr, "%s: mode is only valid with lockahead\n",
8797 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
8798 fprintf(stderr, "%s: mode is required with lockahead\n",
8803 while (optind < argc) {
8806 path = argv[optind++];
8808 fd = open(path, O_RDONLY);
8810 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8811 argv[0], path, strerror(errno));
8816 advice.lla_start = start;
8817 advice.lla_end = end;
8818 advice.lla_advice = advice_type;
8819 advice.lla_value1 = 0;
8820 advice.lla_value2 = 0;
8821 advice.lla_value3 = 0;
8822 advice.lla_value4 = 0;
8823 if (advice_type == LU_LADVISE_LOCKAHEAD) {
8824 advice.lla_lockahead_mode = mode;
8825 advice.lla_peradvice_flags = flags;
8828 rc2 = llapi_ladvise(fd, flags, 1, &advice);
8831 fprintf(stderr, "%s: cannot give advice '%s' to file "
8832 "'%s': %s\n", argv[0],
8833 ladvise_names[advice_type],
8834 path, strerror(errno));
8840 if (rc == 0 && rc2 < 0)
8846 static const char *const heat_names[] = LU_HEAT_NAMES;
8848 static int lfs_heat_get(int argc, char **argv)
8850 struct lu_heat *heat;
8859 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
8861 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
8866 while (optind < argc) {
8867 path = argv[optind++];
8869 fd = open(path, O_RDONLY);
8871 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8872 argv[0], path, strerror(errno));
8877 heat->lh_count = OBD_HEAT_COUNT;
8878 rc2 = llapi_heat_get(fd, heat);
8881 fprintf(stderr, "%s: cannot get heat of file '%s'"
8882 ": %s\n", argv[0], path, strerror(errno));
8886 printf("flags: %x\n", heat->lh_flags);
8887 for (i = 0; i < heat->lh_count; i++)
8888 printf("%s: %llu\n", heat_names[i],
8889 (unsigned long long)heat->lh_heat[i]);
8891 if (rc == 0 && rc2 < 0)
8899 static int lfs_heat_set(int argc, char **argv)
8901 struct option long_opts[] = {
8902 { .val = 'c', .name = "clear", .has_arg = no_argument },
8903 { .val = 'o', .name = "off", .has_arg = no_argument },
8904 { .val = 'O', .name = "on", .has_arg = no_argument },
8906 enum lu_heat_flag flags = 0;
8916 while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
8919 flags |= LU_HEAT_FLAG_CLEAR;
8922 flags |= LU_HEAT_FLAG_CLEAR;
8923 flags |= LU_HEAT_FLAG_OFF;
8926 flags &= ~LU_HEAT_FLAG_OFF;
8931 fprintf(stderr, "%s: option '%s' unrecognized\n",
8932 argv[0], argv[optind - 1]);
8937 if (argc <= optind) {
8938 fprintf(stderr, "%s: please give one or more file names\n",
8943 while (optind < argc) {
8944 path = argv[optind++];
8946 fd = open(path, O_RDONLY);
8948 fprintf(stderr, "%s: cannot open file '%s': %s\n",
8949 argv[0], path, strerror(errno));
8954 rc2 = llapi_heat_set(fd, flags);
8957 fprintf(stderr, "%s: cannot setflags heat of file '%s'"
8958 ": %s\n", argv[0], path, strerror(errno));
8962 if (rc == 0 && rc2 < 0)
8968 /** The input string contains a comma delimited list of component ids and
8969 * ranges, for example "1,2-4,7".
8971 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
8973 bool end_of_loop = false;
8981 while (!end_of_loop) {
8985 char *endptr = NULL;
8988 ptr = strchrnul(arg, ',');
8989 end_of_loop = *ptr == '\0';
8992 start_index = strtol(arg, &endptr, 0);
8993 if (endptr == arg) /* no data at all */
8995 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
8997 if (start_index < 0)
9000 end_index = start_index;
9001 if (*endptr == '-') {
9002 end_index = strtol(endptr + 1, &endptr, 0);
9003 if (*endptr != '\0')
9005 if (end_index < start_index)
9009 for (i = start_index; i <= end_index && size > 0; i++) {
9012 /* remove duplicate */
9013 for (j = 0; j < nr; j++) {
9017 if (j == nr) { /* no duplicate */
9023 if (size == 0 && i < end_index)
9030 if (!end_of_loop && ptr != NULL)
9033 return rc < 0 ? rc : nr;
9037 * struct verify_mirror_id - Mirror id to be verified.
9038 * @mirror_id: A specified mirror id.
9039 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
9041 struct verify_mirror_id {
9047 * compare_mirror_ids() - Compare mirror ids.
9048 * @layout: Mirror component list.
9049 * @cbdata: Callback data in verify_mirror_id structure.
9051 * This is a callback function called by llapi_layout_comp_iterate()
9052 * to compare the specified mirror id with the one in the current
9053 * component of @layout. If they are the same, then the specified
9054 * mirror id is valid.
9056 * Return: a negative error code on failure or
9057 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
9058 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
9061 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
9063 struct verify_mirror_id *mirror_id_cbdata =
9064 (struct verify_mirror_id *)cbdata;
9068 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
9072 "%s: llapi_layout_mirror_id_get failed: %s.\n",
9073 progname, strerror(errno));
9077 if (mirror_id_cbdata->mirror_id == mirror_id) {
9078 mirror_id_cbdata->is_valid_id = true;
9079 return LLAPI_LAYOUT_ITER_STOP;
9082 return LLAPI_LAYOUT_ITER_CONT;
9086 * verify_mirror_ids() - Verify specified mirror ids.
9087 * @fname: Mirrored file name.
9088 * @mirror_ids: Specified mirror ids to be verified.
9089 * @ids_nr: Number of specified mirror ids.
9091 * This function verifies that specified @mirror_ids are valid
9092 * in the mirrored file @fname.
9094 * Return: 0 on success or a negative error code on failure.
9097 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
9099 struct llapi_layout *layout = NULL;
9100 struct verify_mirror_id mirror_id_cbdata = { 0 };
9111 if (stat(fname, &stbuf) < 0) {
9112 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9113 progname, fname, strerror(errno));
9118 if (!S_ISREG(stbuf.st_mode)) {
9119 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9125 fd = open(fname, O_DIRECT | O_RDONLY);
9127 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9128 progname, fname, strerror(errno));
9133 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
9135 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
9136 progname, fname, strerror(errno));
9140 layout = llapi_layout_get_by_fd(fd, 0);
9141 if (layout == NULL) {
9142 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9143 progname, fname, strerror(errno));
9145 llapi_lease_release(fd);
9149 rc = llapi_layout_flags_get(layout, &flr_state);
9151 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9152 progname, fname, strerror(errno));
9157 flr_state &= LCM_FL_FLR_MASK;
9158 switch (flr_state) {
9161 fprintf(stderr, "%s: '%s' file state error: %s.\n",
9162 progname, fname, llapi_layout_flags_string(flr_state));
9169 for (i = 0; i < ids_nr; i++) {
9170 mirror_id_cbdata.mirror_id = mirror_ids[i];
9171 mirror_id_cbdata.is_valid_id = false;
9173 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
9178 "%s: '%s' failed to verify mirror id: %u.\n",
9179 progname, fname, mirror_ids[i]);
9183 if (!mirror_id_cbdata.is_valid_id) {
9186 "%s: '%s' invalid specified mirror id: %u.\n",
9187 progname, fname, mirror_ids[i]);
9193 llapi_layout_free(layout);
9194 llapi_lease_release(fd);
9202 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
9203 __u16 *mirror_ids, int ids_nr)
9205 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9206 struct llapi_layout *layout;
9217 if (stat(fname, &stbuf) < 0) {
9218 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
9219 progname, fname, strerror(errno));
9223 if (!S_ISREG(stbuf.st_mode)) {
9224 fprintf(stderr, "%s: '%s' is not a regular file.\n",
9230 fd = open(fname, O_DIRECT | O_RDWR);
9232 fprintf(stderr, "%s: cannot open '%s': %s.\n",
9233 progname, fname, strerror(errno));
9238 layout = llapi_layout_get_by_fd(fd, 0);
9239 if (layout == NULL) {
9240 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
9241 progname, fname, strerror(errno));
9246 rc = llapi_layout_flags_get(layout, &flr_state);
9248 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
9249 progname, fname, strerror(errno));
9254 flr_state &= LCM_FL_FLR_MASK;
9255 if (flr_state == LCM_FL_NONE) {
9257 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
9262 /* get stale component info */
9263 comp_size = llapi_mirror_find_stale(layout, comp_array,
9264 ARRAY_SIZE(comp_array),
9265 mirror_ids, ids_nr);
9266 if (comp_size <= 0) {
9271 ioc->lil_mode = LL_LEASE_WRLCK;
9272 ioc->lil_flags = LL_LEASE_RESYNC;
9273 rc = llapi_lease_set(fd, ioc);
9275 if (rc == -EALREADY)
9279 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
9280 progname, fname, strerror(-rc));
9284 /* get the read range [start, end) */
9285 start = comp_array[0].lrc_start;
9286 end = comp_array[0].lrc_end;
9287 for (idx = 1; idx < comp_size; idx++) {
9288 if (comp_array[idx].lrc_start < start)
9289 start = comp_array[idx].lrc_start;
9290 if (end < comp_array[idx].lrc_end)
9291 end = comp_array[idx].lrc_end;
9294 rc = llapi_lease_check(fd);
9295 if (rc != LL_LEASE_WRLCK) {
9296 fprintf(stderr, "%s: '%s' lost lease lock.\n",
9301 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
9304 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
9305 progname, fname, strerror(-rc));
9307 /* need to do the lease unlock even resync fails */
9308 ioc->lil_mode = LL_LEASE_UNLCK;
9309 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
9311 for (idx = 0; idx < comp_size; idx++) {
9312 if (comp_array[idx].lrc_synced) {
9313 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
9318 rc2 = llapi_lease_set(fd, ioc);
9320 * llapi_lease_set returns lease mode when it request to unlock
9324 /* rc2 == 0 means lost lease lock */
9325 if (rc2 == 0 && rc == 0)
9327 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
9329 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
9333 llapi_layout_free(layout);
9340 static inline int lfs_mirror_resync(int argc, char **argv)
9342 struct ll_ioc_lease *ioc = NULL;
9343 __u16 mirror_ids[128] = { 0 };
9348 struct option long_opts[] = {
9349 { .val = 'o', .name = "only", .has_arg = required_argument },
9352 while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
9355 rc = parse_mirror_ids(mirror_ids,
9356 sizeof(mirror_ids) / sizeof(__u16),
9360 "%s: bad mirror ids '%s'.\n",
9367 fprintf(stderr, "%s: options '%s' unrecognized.\n",
9368 argv[0], argv[optind - 1]);
9374 if (argc == optind) {
9375 fprintf(stderr, "%s: no file name given.\n", argv[0]);
9380 if (ids_nr > 0 && argc > optind + 1) {
9382 "%s: option '--only' cannot be used upon multiple files.\n",
9390 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9395 /* set the lease on the file */
9396 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9398 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
9399 argv[0], strerror(errno));
9404 for (; optind < argc; optind++) {
9405 rc = lfs_mirror_resync_file(argv[optind], ioc,
9406 mirror_ids, ids_nr);
9407 /* ignore previous file's error, continue with next file */
9410 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
9418 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
9420 struct llapi_layout *layout;
9423 layout = llapi_layout_get_by_fd(fd, 0);
9424 if (layout == NULL) {
9425 fprintf(stderr, "could not get layout.\n");
9429 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
9431 fprintf(stderr, "failed to iterate layout\n");
9432 llapi_layout_free(layout);
9435 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
9436 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
9437 llapi_layout_free(layout);
9441 llapi_layout_free(layout);
9447 * Check whether two files are the same file
9448 * \retval 0 same file
9449 * \retval 1 not the same file
9450 * \retval <0 error code
9452 static inline int check_same_file(const char *f1, const char *f2)
9457 if (stat(f1, &stbuf1) < 0) {
9458 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9459 progname, f1, strerror(errno));
9463 if (stat(f2, &stbuf2) < 0) {
9464 fprintf(stderr, "%s: cannot stat file '%s': %s\n",
9465 progname, f2, strerror(errno));
9469 if (stbuf1.st_rdev == stbuf2.st_rdev &&
9470 stbuf1.st_ino == stbuf2.st_ino)
9476 static inline int lfs_mirror_read(int argc, char **argv)
9479 __u16 mirror_id = 0;
9480 const char *outfile = NULL;
9486 const size_t buflen = 4 << 20;
9488 struct option long_opts[] = {
9489 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9490 { .val = 'o', .name = "outfile", .has_arg = required_argument },
9493 while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
9498 mirror_id = strtoul(optarg, &end, 0);
9499 if (*end != '\0' || mirror_id == 0) {
9501 "%s %s: invalid mirror ID '%s'\n",
9502 progname, argv[0], optarg);
9510 fprintf(stderr, "%s: option '%s' unrecognized.\n",
9511 progname, argv[optind - 1]);
9516 if (argc == optind) {
9517 fprintf(stderr, "%s %s: no mirrored file provided\n",
9520 } else if (argc > optind + 1) {
9521 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9525 if (mirror_id == 0) {
9526 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9531 /* open mirror file */
9532 fname = argv[optind];
9535 rc = check_same_file(fname, outfile);
9538 "%s %s: output file cannot be the mirrored file\n",
9546 fd = open(fname, O_DIRECT | O_RDONLY);
9548 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9549 progname, argv[0], fname, strerror(errno));
9553 /* verify mirror id */
9554 rc = verify_mirror_id_by_fd(fd, mirror_id);
9557 "%s %s: cannot find mirror with ID %u in '%s'\n",
9558 progname, argv[0], mirror_id, fname);
9562 /* open output file */
9564 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
9566 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
9567 progname, argv[0], outfile, strerror(errno));
9572 outfd = STDOUT_FILENO;
9575 /* allocate buffer */
9576 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
9578 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9579 progname, argv[0], rc);
9586 ssize_t written = 0;
9588 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
9589 if (bytes_read < 0) {
9592 "%s %s: fail to read data from mirror %u: %s\n",
9593 progname, argv[0], mirror_id, strerror(-rc));
9598 if (bytes_read == 0)
9601 while (written < bytes_read) {
9604 written2 = write(outfd, buf + written,
9605 bytes_read - written);
9608 "%s %s: fail to write %s: %s\n",
9609 progname, argv[0], outfile ? : "STDOUT",
9614 written += written2;
9617 if (written != bytes_read) {
9619 "%s %s: written %ld bytes does not match with %ld read.\n",
9620 progname, argv[0], written, bytes_read);
9642 static inline int lfs_mirror_write(int argc, char **argv)
9645 __u16 mirror_id = 0;
9646 const char *inputfile = NULL;
9652 const size_t buflen = 4 << 20;
9654 size_t page_size = sysconf(_SC_PAGESIZE);
9655 struct ll_ioc_lease_id ioc;
9657 struct option long_opts[] = {
9658 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
9659 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
9662 while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
9667 mirror_id = strtoul(optarg, &end, 0);
9668 if (*end != '\0' || mirror_id == 0) {
9670 "%s %s: invalid mirror ID '%s'\n",
9671 progname, argv[0], optarg);
9679 fprintf(stderr, "%s: option '%s' unrecognized\n",
9680 progname, argv[optind - 1]);
9685 if (argc == optind) {
9686 fprintf(stderr, "%s %s: no mirrored file provided\n",
9689 } else if (argc > optind + 1) {
9690 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9694 if (mirror_id == 0) {
9695 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
9700 /* open mirror file */
9701 fname = argv[optind];
9704 rc = check_same_file(fname, inputfile);
9707 "%s %s: input file cannot be the mirrored file\n",
9715 fd = open(fname, O_DIRECT | O_WRONLY);
9717 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9718 progname, argv[0], fname, strerror(errno));
9722 /* verify mirror id */
9723 rc = verify_mirror_id_by_fd(fd, mirror_id);
9726 "%s %s: cannot find mirror with ID %u in '%s'\n",
9727 progname, argv[0], mirror_id, fname);
9731 /* open input file */
9733 inputfd = open(inputfile, O_RDONLY, 0644);
9735 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
9736 progname, argv[0], inputfile, strerror(errno));
9741 inputfd = STDIN_FILENO;
9744 /* allocate buffer */
9745 rc = posix_memalign(&buf, page_size, buflen);
9747 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
9748 progname, argv[0], rc);
9752 /* prepare target mirror components instantiation */
9753 ioc.lil_mode = LL_LEASE_WRLCK;
9754 ioc.lil_flags = LL_LEASE_RESYNC;
9755 ioc.lil_mirror_id = mirror_id;
9756 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9759 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
9760 progname, argv[0], fname, strerror(errno));
9770 rc = llapi_lease_check(fd);
9771 if (rc != LL_LEASE_WRLCK) {
9772 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
9773 progname, argv[0], fname);
9777 bytes_read = read(inputfd, buf, buflen);
9778 if (bytes_read < 0) {
9781 "%s %s: fail to read data from '%s': %s\n",
9782 progname, argv[0], inputfile ? : "STDIN",
9789 if (bytes_read == 0)
9792 /* round up to page align to make direct IO happy. */
9793 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
9795 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
9800 "%s %s: fail to write to mirror %u: %s\n",
9801 progname, argv[0], mirror_id,
9809 if (pos & (page_size - 1)) {
9810 rc = llapi_mirror_truncate(fd, mirror_id, pos);
9815 ioc.lil_mode = LL_LEASE_UNLCK;
9816 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
9818 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
9823 "%s %s: release lease lock of '%s' failed: %s\n",
9824 progname, argv[0], fname, strerror(errno));
9841 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
9843 struct llapi_layout *layout;
9844 struct collect_ids_data cid = { .cid_ids = ids,
9846 .cid_exclude = exclude_id, };
9849 layout = llapi_layout_get_by_fd(fd, 0);
9850 if (layout == NULL) {
9851 fprintf(stderr, "could not get layout\n");
9855 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
9857 fprintf(stderr, "failed to iterate layout\n");
9858 llapi_layout_free(layout);
9862 llapi_layout_free(layout);
9864 return cid.cid_count;
9867 static inline int lfs_mirror_copy(int argc, char **argv)
9870 __u16 read_mirror_id = 0;
9871 __u16 ids[128] = { 0 };
9873 struct llapi_layout *layout = NULL;
9874 struct llapi_resync_comp comp_array[1024] = { { 0 } };
9881 struct ll_ioc_lease *ioc = NULL;
9882 struct ll_ioc_lease_id *resync_ioc;
9884 struct option long_opts[] = {
9885 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
9886 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
9889 while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
9894 read_mirror_id = strtoul(optarg, &end, 0);
9895 if (*end != '\0' || read_mirror_id == 0) {
9897 "%s %s: invalid read mirror ID '%s'\n",
9898 progname, argv[0], optarg);
9903 if (!strcmp(optarg, "-1")) {
9904 /* specify all other mirrors */
9908 count = parse_mirror_ids((__u16 *)ids,
9916 fprintf(stderr, "%s: option '%s' unrecognized\n",
9917 progname, argv[optind - 1]);
9922 if (argc == optind) {
9923 fprintf(stderr, "%s %s: no mirrored file provided\n",
9926 } else if (argc > optind + 1) {
9927 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
9931 if (read_mirror_id == 0) {
9933 "%s %s: no valid read mirror ID %d is provided\n",
9934 progname, argv[0], read_mirror_id);
9940 "%s %s: no write mirror ID is provided\n",
9945 for (i = 0; i < count; i++) {
9946 if (read_mirror_id == ids[i]) {
9948 "%s %s: read and write mirror ID cannot be the same\n",
9954 /* open mirror file */
9955 fname = argv[optind];
9957 fd = open(fname, O_DIRECT | O_RDWR);
9959 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
9960 progname, argv[0], fname, strerror(errno));
9964 /* write to all other mirrors */
9965 if (ids[0] == (__u16)-1) {
9966 count = get_other_mirror_ids(fd, ids, read_mirror_id);
9970 "%s %s: failed to get other mirror ids in '%s': %d\n",
9971 progname, argv[0], fname, rc);
9976 /* verify mirror id */
9977 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
9980 "%s %s: cannot find mirror with ID %u in '%s'\n",
9981 progname, argv[0], read_mirror_id, fname);
9985 for (i = 0; i < count; i++) {
9986 rc = verify_mirror_id_by_fd(fd, ids[i]);
9989 "%s %s: cannot find mirror with ID %u in '%s'\n",
9990 progname, argv[0], ids[i], fname);
9995 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
9998 "%s %s: cannot alloc comp id array for ioc: %s\n",
9999 progname, argv[0], strerror(errno));
10004 /* get stale component info */
10005 layout = llapi_layout_get_by_fd(fd, 0);
10006 if (layout == NULL) {
10007 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
10008 progname, argv[0], fname, strerror(errno));
10012 comp_size = llapi_mirror_find_stale(layout, comp_array,
10013 ARRAY_SIZE(comp_array),
10015 llapi_layout_free(layout);
10016 if (comp_size < 0) {
10021 /* prepare target mirror components instantiation */
10022 resync_ioc = (struct ll_ioc_lease_id *)ioc;
10023 resync_ioc->lil_mode = LL_LEASE_WRLCK;
10024 resync_ioc->lil_flags = LL_LEASE_RESYNC;
10026 resync_ioc->lil_mirror_id = ids[0];
10028 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
10029 rc = llapi_lease_set(fd, ioc);
10032 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
10033 progname, argv[0], fname, strerror(errno));
10037 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
10040 fprintf(stderr, "%s %s: copy error: %d\n",
10041 progname, argv[0], rc);
10045 fprintf(stdout, "mirror copied successfully: ");
10046 for (i = 0; i < copied; i++)
10047 fprintf(stdout, "%d ", ids[i]);
10048 fprintf(stdout, "\n");
10050 ioc->lil_mode = LL_LEASE_UNLCK;
10051 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10052 ioc->lil_count = 0;
10053 for (i = 0; i < comp_size; i++) {
10056 for (j = 0; j < copied; j++) {
10057 if (comp_array[i].lrc_mirror_id != ids[j])
10060 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
10064 rc = llapi_lease_set(fd, ioc);
10069 "%s %s: release lease lock of '%s' failed: %s\n",
10070 progname, argv[0], fname, strerror(errno));
10084 * struct verify_chunk - Mirror chunk to be verified.
10085 * @chunk: [start, end) of the chunk.
10086 * @mirror_count: Number of mirror ids in @mirror_id array.
10087 * @mirror_id: Array of valid mirror ids that cover the chunk.
10089 struct verify_chunk {
10090 struct lu_extent chunk;
10091 unsigned int mirror_count;
10092 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
10096 * print_chunks() - Print chunk information.
10097 * @fname: Mirrored file name.
10098 * @chunks: Array of chunks.
10099 * @chunk_count: Number of chunks in @chunks array.
10101 * This function prints [start, end) of each chunk in @chunks
10102 * for mirrored file @fname, and also prints the valid mirror ids
10103 * that cover the chunk.
10108 void print_chunks(const char *fname, struct verify_chunk *chunks,
10114 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
10115 for (i = 0; i < chunk_count; i++) {
10116 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
10118 if (chunks[i].mirror_count == 0)
10119 fprintf(stdout, "\t[");
10121 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
10122 for (j = 1; j < chunks[i].mirror_count; j++)
10123 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
10125 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
10127 fprintf(stdout, "\n");
10131 * print_checksums() - Print CRC-32 checksum values.
10132 * @chunk: A chunk and its corresponding valid mirror ids.
10133 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
10135 * This function prints CRC-32 checksum values on @chunk for
10136 * each valid mirror that covers it.
10141 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
10146 "CRC-32 checksum value for chunk "DEXT":\n",
10147 PEXT(&chunk->chunk));
10148 for (i = 0; i < chunk->mirror_count; i++)
10149 fprintf(stdout, "Mirror %u:\t%#lx\n",
10150 chunk->mirror_id[i], crc[i]);
10151 fprintf(stdout, "\n");
10155 * filter_mirror_id() - Filter specified mirror ids.
10156 * @chunks: Array of chunks.
10157 * @chunk_count: Number of chunks in @chunks array.
10158 * @mirror_ids: Specified mirror ids to be verified.
10159 * @ids_nr: Number of specified mirror ids.
10161 * This function scans valid mirror ids that cover each chunk in @chunks
10162 * and filters specified mirror ids.
10167 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
10168 __u16 *mirror_ids, int ids_nr)
10173 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10174 unsigned int valid_count = 0;
10176 for (i = 0; i < chunk_count; i++) {
10177 if (chunks[i].mirror_count == 0)
10181 for (j = 0; j < ids_nr; j++) {
10182 for (k = 0; k < chunks[i].mirror_count; k++) {
10183 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
10184 valid_id[valid_count] = mirror_ids[j];
10191 memcpy(chunks[i].mirror_id, valid_id,
10192 sizeof(__u16) * valid_count);
10193 chunks[i].mirror_count = valid_count;
10198 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
10199 * @layout: Mirror component list.
10200 * @chunks: Array of chunks.
10201 * @chunks_size: Array size of @chunks.
10203 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
10204 * to find out chunk segments and store them in @chunks array.
10206 * The @mirror_id array in each element of @chunks will store the valid
10207 * mirror ids that cover the chunk. If a mirror component covering the
10208 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
10209 * will not be stored into the @mirror_id array, and the chunk for that
10210 * mirror will not be verified.
10212 * The @mirror_count in each element of @chunks will store the number of
10213 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
10214 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
10215 * indicates the chunk is valid in only one mirror. In both cases, the
10216 * chunk will not be verified.
10218 * Here is an example:
10220 * 0 1M 2M 3M 4M EOF
10221 * +------+-------------+--------------------+
10222 * | | | S | mirror1
10223 * +------+------+------+------+-------------+
10224 * | | S | S | | mirror2
10225 * +-------------+------+------+-------------+
10227 * prepared @chunks array will contain 5 elements:
10228 * (([0, 1M), [1, 2], 2),
10229 * ([1M, 2M), [1, 2], 2),
10230 * ([2M, 3M), [1], 1),
10231 * ([3M, 4M], [], 0),
10232 * ([4M, EOF), [2], 1))
10234 * Return: the actual array size of @chunks on success
10235 * or a negative error code on failure.
10238 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
10239 struct verify_chunk *chunks,
10240 size_t chunks_size)
10244 uint32_t mirror_id;
10250 memset(chunks, 0, sizeof(*chunks) * chunks_size);
10253 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
10256 "%s: move to the first layout component: %s.\n",
10257 progname, strerror(errno));
10263 chunks[idx].chunk.e_end = LUSTRE_EOF;
10265 rc = llapi_layout_comp_extent_get(layout, &start, &end);
10268 "%s: llapi_layout_comp_extent_get failed: %s.\n",
10269 progname, strerror(errno));
10273 if (start > chunks[idx].chunk.e_start ||
10274 end <= chunks[idx].chunk.e_start)
10277 if (end < chunks[idx].chunk.e_end)
10278 chunks[idx].chunk.e_end = end;
10280 rc = llapi_layout_comp_flags_get(layout, &flags);
10283 "%s: llapi_layout_comp_flags_get failed: %s.\n",
10284 progname, strerror(errno));
10288 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
10291 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10294 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10295 progname, strerror(errno));
10299 chunks[idx].mirror_id[i] = mirror_id;
10301 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
10303 "%s: mirror_id array is too small.\n",
10310 rc = llapi_layout_comp_use(layout,
10311 LLAPI_LAYOUT_COMP_USE_NEXT);
10314 "%s: move to the next layout component: %s.\n",
10315 progname, strerror(errno));
10318 } /* loop through all components */
10320 chunks[idx].mirror_count = i;
10322 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
10326 if (idx >= chunks_size) {
10327 fprintf(stderr, "%s: chunks array is too small.\n",
10333 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
10337 return rc < 0 ? rc : idx + 1;
10341 * lfs_mirror_verify_chunk() - Verify a chunk.
10342 * @fd: File descriptor of the mirrored file.
10343 * @file_size: Size of the mirrored file.
10344 * @chunk: A chunk and its corresponding valid mirror ids.
10345 * @verbose: Verbose mode.
10347 * This function verifies a @chunk contains exactly the same data
10348 * ammong the mirrors that cover it.
10350 * If @verbose is specified, then the function will print where the
10351 * differences are if the data do not match. Otherwise, it will
10352 * just return an error in that case.
10354 * Return: 0 on success or a negative error code on failure.
10357 int lfs_mirror_verify_chunk(int fd, size_t file_size,
10358 struct verify_chunk *chunk, int verbose)
10360 const size_t buflen = 4 * 1024 * 1024; /* 4M */
10362 size_t page_size = sysconf(_SC_PAGESIZE);
10363 ssize_t bytes_read;
10364 ssize_t bytes_done;
10368 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10372 if (file_size == 0)
10375 rc = posix_memalign(&buf, page_size, buflen);
10376 if (rc) /* error code is returned directly */
10380 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10381 PEXT(&chunk->chunk));
10382 for (i = 0; i < chunk->mirror_count; i++)
10383 fprintf(stdout, " %u", chunk->mirror_id[i]);
10384 fprintf(stdout, "\n");
10388 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
10389 pos = chunk->chunk.e_start;
10390 while (bytes_done < count) {
10391 /* compute initial CRC-32 checksum */
10392 crc = crc32(0L, Z_NULL, 0);
10393 memset(crc_array, 0, sizeof(crc_array));
10396 for (i = 0; i < chunk->mirror_count; i++) {
10397 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
10399 if (bytes_read < 0) {
10402 "%s: failed to read data from mirror %u: %s.\n",
10403 progname, chunk->mirror_id[i],
10408 /* compute new CRC-32 checksum */
10409 crc_array[i] = crc32(crc, buf, bytes_read);
10413 print_checksums(chunk, crc_array);
10415 /* compare CRC-32 checksum values */
10416 for (i = 1; i < chunk->mirror_count; i++) {
10417 if (crc_array[i] != crc_array[0]) {
10421 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
10422 progname, PEXT(&chunk->chunk),
10423 chunk->mirror_id[0],
10424 chunk->mirror_id[i]);
10429 bytes_done += bytes_read;
10432 if (verbose > 1 && rc == 0) {
10433 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
10434 PEXT(&chunk->chunk));
10435 for (i = 0; i < chunk->mirror_count; i++)
10436 fprintf(stdout, " %u", chunk->mirror_id[i]);
10437 fprintf(stdout, " PASS\n\n");
10446 * lfs_mirror_verify_file() - Verify a mirrored file.
10447 * @fname: Mirrored file name.
10448 * @mirror_ids: Specified mirror ids to be verified.
10449 * @ids_nr: Number of specified mirror ids.
10450 * @verbose: Verbose mode.
10452 * This function verifies that each SYNC mirror of a mirrored file
10453 * specified by @fname contains exactly the same data.
10455 * If @mirror_ids is specified, then the function will verify the
10456 * mirrors specified by @mirror_ids contain exactly the same data.
10458 * If @verbose is specified, then the function will print where the
10459 * differences are if the data do not match. Otherwise, it will
10460 * just return an error in that case.
10462 * Return: 0 on success or a negative error code on failure.
10465 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
10468 struct verify_chunk chunks_array[1024] = { };
10469 struct llapi_layout *layout = NULL;
10471 uint32_t flr_state;
10473 int chunk_count = 0;
10479 if (stat(fname, &stbuf) < 0) {
10480 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10481 progname, fname, strerror(errno));
10486 if (!S_ISREG(stbuf.st_mode)) {
10487 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10493 if (stbuf.st_size == 0) {
10495 fprintf(stdout, "%s: '%s' file size is 0.\n",
10501 fd = open(fname, O_DIRECT | O_RDONLY);
10503 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10504 progname, fname, strerror(errno));
10509 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10511 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10512 progname, fname, strerror(errno));
10516 layout = llapi_layout_get_by_fd(fd, 0);
10517 if (layout == NULL) {
10518 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10519 progname, fname, strerror(errno));
10521 llapi_lease_release(fd);
10525 rc = llapi_layout_flags_get(layout, &flr_state);
10527 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10528 progname, fname, strerror(errno));
10533 flr_state &= LCM_FL_FLR_MASK;
10534 switch (flr_state) {
10537 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10538 progname, fname, llapi_layout_flags_string(flr_state));
10544 /* find out mirror chunks to be verified */
10545 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
10546 ARRAY_SIZE(chunks_array));
10547 if (chunk_count < 0) {
10553 /* filter specified mirror ids */
10554 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
10557 print_chunks(fname, chunks_array, chunk_count);
10559 for (idx = 0; idx < chunk_count; idx++) {
10560 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
10563 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
10565 PEXT(&chunks_array[idx].chunk),
10566 (unsigned long long)stbuf.st_size);
10570 if (chunks_array[idx].mirror_count == 0) {
10572 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
10574 PEXT(&chunks_array[idx].chunk));
10576 fprintf(stderr, "skipped\n");
10580 fprintf(stderr, "failed\n");
10584 if (chunks_array[idx].mirror_count == 1) {
10587 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
10589 PEXT(&chunks_array[idx].chunk),
10590 chunks_array[idx].mirror_id[0]);
10594 rc = llapi_lease_check(fd);
10595 if (rc != LL_LEASE_RDLCK) {
10596 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10601 /* verify one chunk */
10602 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
10603 &chunks_array[idx], verbose);
10617 llapi_layout_free(layout);
10618 llapi_lease_release(fd);
10626 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
10627 * @argc: The count of lfs mirror verify command line arguments.
10628 * @argv: Array of strings for lfs mirror verify command line arguments.
10630 * This function parses lfs mirror verify command and verifies the
10631 * specified mirrored file(s).
10633 * Return: 0 on success or a negative error code on failure.
10635 static inline int lfs_mirror_verify(int argc, char **argv)
10637 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
10643 char cmd[PATH_MAX];
10645 struct option long_opts[] = {
10646 { .val = 'o', .name = "only", .has_arg = required_argument },
10647 { .val = 'v', .name = "verbose", .has_arg = no_argument },
10648 { .name = NULL } };
10650 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10652 while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
10655 rc = parse_mirror_ids(mirror_ids,
10656 ARRAY_SIZE(mirror_ids),
10660 "%s: bad mirror ids '%s'.\n",
10667 "%s: at least 2 mirror ids needed with '--only' option.\n",
10677 fprintf(stderr, "%s: option '%s' unrecognized.\n",
10678 progname, argv[optind - 1]);
10684 if (argc == optind) {
10685 fprintf(stderr, "%s: no file name given.\n", progname);
10690 if (ids_nr > 0 && argc > optind + 1) {
10692 "%s: '--only' cannot be used upon multiple files.\n",
10700 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10706 for (; optind < argc; optind++) {
10707 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
10717 * lfs_mirror() - Parse and execute lfs mirror commands.
10718 * @argc: The count of lfs mirror command line arguments.
10719 * @argv: Array of strings for lfs mirror command line arguments.
10721 * This function parses lfs mirror commands and performs the
10722 * corresponding functions specified in mirror_cmdlist[].
10724 * Return: 0 on success or an error code on failure.
10726 static int lfs_mirror(int argc, char **argv)
10728 char cmd[PATH_MAX];
10731 setlinebuf(stdout);
10733 Parser_init("lfs-mirror > ", mirror_cmdlist);
10735 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
10737 program_invocation_short_name = cmd;
10739 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
10741 rc = Parser_commands();
10743 return rc < 0 ? -rc : rc;
10746 static void lustre_som_swab(struct lustre_som_attrs *attrs)
10748 #if __BYTE_ORDER == __BIG_ENDIAN
10749 __swab16s(&attrs->lsa_valid);
10750 __swab64s(&attrs->lsa_size);
10751 __swab64s(&attrs->lsa_blocks);
10755 enum lfs_som_type {
10756 LFS_SOM_SIZE = 0x1,
10757 LFS_SOM_BLOCKS = 0x2,
10758 LFS_SOM_FLAGS = 0x4,
10759 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
10763 static int lfs_getsom(int argc, char **argv)
10766 struct lustre_som_attrs *attrs;
10767 char buf[sizeof(*attrs) + 64];
10768 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
10771 while ((c = getopt(argc, argv, "sbf")) != -1) {
10774 type = LFS_SOM_SIZE;
10777 type = LFS_SOM_BLOCKS;
10780 type = LFS_SOM_FLAGS;
10783 fprintf(stderr, "%s: invalid option '%c'\n",
10793 fprintf(stderr, "%s: %s\n",
10794 progname, argc == 0 ? "miss file target" :
10795 "input more than 2 files");
10800 attrs = (void *)buf;
10801 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
10804 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
10805 argv[0], strerror(errno), errno);
10809 lustre_som_swab(attrs);
10812 case LFS_SOM_ATTR_ALL:
10813 printf("file: %s size: %llu blocks: %llu flags: %x\n",
10814 path, (unsigned long long)attrs->lsa_size,
10815 (unsigned long long)attrs->lsa_blocks,
10819 printf("%llu\n", (unsigned long long)attrs->lsa_size);
10821 case LFS_SOM_BLOCKS:
10822 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
10824 case LFS_SOM_FLAGS:
10825 printf("%x\n", attrs->lsa_valid);
10828 fprintf(stderr, "%s: unknown option\n", progname);
10836 * lfs_mirror_list_commands() - List lfs mirror commands.
10837 * @argc: The count of command line arguments.
10838 * @argv: Array of strings for command line arguments.
10840 * This function lists lfs mirror commands defined in mirror_cmdlist[].
10842 * Return: 0 on success.
10844 static int lfs_mirror_list_commands(int argc, char **argv)
10846 char buffer[81] = "";
10848 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
10854 static int lfs_pcc_attach(int argc, char **argv)
10856 struct option long_opts[] = {
10857 { .val = 'i', .name = "id", .has_arg = required_argument },
10858 { .name = NULL } };
10861 __u32 archive_id = 0;
10864 char fullpath[PATH_MAX];
10865 enum lu_pcc_type type = LU_PCC_READWRITE;
10868 while ((c = getopt_long(argc, argv, "i:",
10869 long_opts, NULL)) != -1) {
10872 archive_id = strtoul(optarg, &end, 0);
10873 if (*end != '\0' || archive_id == 0) {
10874 fprintf(stderr, "error: %s: bad archive ID "
10875 "'%s'\n", argv[0], optarg);
10882 fprintf(stderr, "%s: option '%s' unrecognized\n",
10883 argv[0], argv[optind - 1]);
10888 if (archive_id == 0) {
10889 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
10893 if (argc <= optind) {
10894 fprintf(stderr, "%s: must specify one or more file names\n",
10899 while (optind < argc) {
10902 path = argv[optind++];
10903 if (realpath(path, fullpath) == NULL) {
10904 fprintf(stderr, "%s: could not find path '%s': %s\n",
10905 argv[0], path, strerror(errno));
10911 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
10913 fprintf(stderr, "%s: cannot attach '%s' to PCC "
10914 "with archive ID '%u': %s\n", argv[0],
10915 path, archive_id, strerror(-rc2));
10923 static int lfs_pcc_attach_fid(int argc, char **argv)
10925 struct option long_opts[] = {
10926 { .val = 'i', .name = "id", .has_arg = required_argument },
10927 { .val = 'm', .name = "mnt", .has_arg = required_argument },
10928 { .name = NULL } };
10929 char short_opts[] = "i:m:";
10932 __u32 archive_id = 0;
10934 const char *mntpath = NULL;
10935 const char *fidstr;
10936 enum lu_pcc_type type = LU_PCC_READWRITE;
10939 while ((c = getopt_long(argc, argv, short_opts,
10940 long_opts, NULL)) != -1) {
10943 archive_id = strtoul(optarg, &end, 0);
10944 if (*end != '\0') {
10945 fprintf(stderr, "error: %s: bad archive ID "
10946 "'%s'\n", argv[0], optarg);
10956 fprintf(stderr, "%s: option '%s' unrecognized\n",
10957 argv[0], argv[optind - 1]);
10962 if (archive_id == 0) {
10963 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
10967 if (mntpath == NULL) {
10968 fprintf(stderr, "%s: must specify Lustre mount point\n",
10973 if (argc <= optind) {
10974 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
10978 while (optind < argc) {
10981 fidstr = argv[optind++];
10983 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
10986 fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
10987 "with archive ID '%u': %s\n", argv[0],
10988 fidstr, mntpath, archive_id, strerror(rc2));
10990 if (rc == 0 && rc2 < 0)
10996 static int lfs_pcc_detach(int argc, char **argv)
10998 struct option long_opts[] = {
10999 { .val = 'k', .name = "keep", .has_arg = no_argument },
11000 { .name = NULL } };
11001 char short_opts[] = "k";
11005 char fullpath[PATH_MAX];
11006 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11009 while ((c = getopt_long(argc, argv, short_opts,
11010 long_opts, NULL)) != -1) {
11013 detach_opt = PCC_DETACH_OPT_NONE;
11018 fprintf(stderr, "%s: option '%s' unrecognized\n",
11019 argv[0], argv[optind - 1]);
11024 while (optind < argc) {
11027 path = argv[optind++];
11028 if (realpath(path, fullpath) == NULL) {
11029 fprintf(stderr, "%s: could not find path '%s': %s\n",
11030 argv[0], path, strerror(errno));
11036 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
11039 fprintf(stderr, "%s: cannot detach '%s' from PCC: "
11040 "%s\n", argv[0], path, strerror(errno));
11048 static int lfs_pcc_detach_fid(int argc, char **argv)
11050 struct option long_opts[] = {
11051 { .val = 'k', .name = "keep", .has_arg = no_argument },
11052 { .name = NULL } };
11053 char short_opts[] = "k";
11057 const char *mntpath;
11058 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
11061 while ((c = getopt_long(argc, argv, short_opts,
11062 long_opts, NULL)) != -1) {
11065 detach_opt = PCC_DETACH_OPT_NONE;
11070 fprintf(stderr, "%s: option '%s' unrecognized\n",
11071 argv[0], argv[optind - 1]);
11076 mntpath = argv[optind++];
11078 while (optind < argc) {
11081 fid = argv[optind++];
11083 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
11085 fprintf(stderr, "%s: cannot detach '%s' on '%s' "
11086 "from PCC: %s\n", argv[0], fid, mntpath,
11095 static int lfs_pcc_state(int argc, char **argv)
11099 char fullpath[PATH_MAX];
11100 struct lu_pcc_state state;
11105 fprintf(stderr, "%s: must specify one or more file names\n",
11110 while (optind < argc) {
11113 path = argv[optind++];
11114 if (realpath(path, fullpath) == NULL) {
11115 fprintf(stderr, "%s: could not find path '%s': %s\n",
11116 argv[0], path, strerror(errno));
11122 rc2 = llapi_pcc_state_get(fullpath, &state);
11126 fprintf(stderr, "%s: cannot get PCC state of '%s': "
11127 "%s\n", argv[0], path, strerror(-rc2));
11131 printf("file: %s", path);
11132 printf(", type: %s", pcc_type2string(state.pccs_type));
11133 if (state.pccs_type == LU_PCC_NONE &&
11134 state.pccs_open_count == 0) {
11139 printf(", PCC file: %s", state.pccs_path);
11140 printf(", user number: %u", state.pccs_open_count);
11141 printf(", flags: %x", state.pccs_flags);
11148 * lfs_pcc_list_commands() - List lfs pcc commands.
11149 * @argc: The count of command line arguments.
11150 * @argv: Array of strings for command line arguments.
11152 * This function lists lfs pcc commands defined in pcc_cmdlist[].
11154 * Return: 0 on success.
11156 static int lfs_pcc_list_commands(int argc, char **argv)
11158 char buffer[81] = "";
11160 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
11167 * lfs_pcc() - Parse and execute lfs pcc commands.
11168 * @argc: The count of lfs pcc command line arguments.
11169 * @argv: Array of strings for lfs pcc command line arguments.
11171 * This function parses lfs pcc commands and performs the
11172 * corresponding functions specified in pcc_cmdlist[].
11174 * Return: 0 on success or an error code on failure.
11176 static int lfs_pcc(int argc, char **argv)
11178 char cmd[PATH_MAX];
11181 setlinebuf(stdout);
11183 Parser_init("lfs-pcc > ", pcc_cmdlist);
11185 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11187 program_invocation_short_name = cmd;
11189 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
11191 rc = Parser_commands();
11193 return rc < 0 ? -rc : rc;
11196 static int lfs_list_commands(int argc, char **argv)
11198 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
11200 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
11205 int main(int argc, char **argv)
11209 /* Ensure that liblustreapi constructor has run */
11210 if (!llapi_liblustreapi_initialized())
11211 fprintf(stderr, "liblustreapi was not properly initialized\n");
11213 setlinebuf(stdout);
11216 Parser_init("lfs > ", cmdlist);
11218 progname = program_invocation_short_name; /* Used in error messages */
11220 llapi_set_command_name(argv[1]);
11221 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
11222 llapi_clear_command_name();
11224 rc = Parser_commands();
11227 return rc < 0 ? -rc : rc;
11230 #ifdef _LUSTRE_IDL_H_
11231 /* Everything we need here should be included by lustreapi.h. */
11232 # error "lfs should not depend on lustre_idl.h"
11233 #endif /* _LUSTRE_IDL_H_ */